nvme.c revision 1.30 1 /* $NetBSD: nvme.c,v 1.30 2017/06/01 02:45:10 chs Exp $ */
2 /* $OpenBSD: nvme.c,v 1.49 2016/04/18 05:59:50 dlg Exp $ */
3
4 /*
5 * Copyright (c) 2014 David Gwynne <dlg (at) openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <sys/cdefs.h>
21 __KERNEL_RCSID(0, "$NetBSD: nvme.c,v 1.30 2017/06/01 02:45:10 chs Exp $");
22
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/kernel.h>
26 #include <sys/atomic.h>
27 #include <sys/bus.h>
28 #include <sys/buf.h>
29 #include <sys/conf.h>
30 #include <sys/device.h>
31 #include <sys/kmem.h>
32 #include <sys/once.h>
33 #include <sys/proc.h>
34 #include <sys/queue.h>
35 #include <sys/mutex.h>
36
37 #include <uvm/uvm_extern.h>
38
39 #include <dev/ic/nvmereg.h>
40 #include <dev/ic/nvmevar.h>
41 #include <dev/ic/nvmeio.h>
42
43 int nvme_adminq_size = 32;
44 int nvme_ioq_size = 1024;
45
46 static int nvme_print(void *, const char *);
47
48 static int nvme_ready(struct nvme_softc *, uint32_t);
49 static int nvme_enable(struct nvme_softc *, u_int);
50 static int nvme_disable(struct nvme_softc *);
51 static int nvme_shutdown(struct nvme_softc *);
52
53 #ifdef NVME_DEBUG
54 static void nvme_dumpregs(struct nvme_softc *);
55 #endif
56 static int nvme_identify(struct nvme_softc *, u_int);
57 static void nvme_fill_identify(struct nvme_queue *, struct nvme_ccb *,
58 void *);
59
60 static int nvme_ccbs_alloc(struct nvme_queue *, uint16_t);
61 static void nvme_ccbs_free(struct nvme_queue *);
62
63 static struct nvme_ccb *
64 nvme_ccb_get(struct nvme_queue *);
65 static void nvme_ccb_put(struct nvme_queue *, struct nvme_ccb *);
66
67 static int nvme_poll(struct nvme_softc *, struct nvme_queue *,
68 struct nvme_ccb *, void (*)(struct nvme_queue *,
69 struct nvme_ccb *, void *), int);
70 static void nvme_poll_fill(struct nvme_queue *, struct nvme_ccb *, void *);
71 static void nvme_poll_done(struct nvme_queue *, struct nvme_ccb *,
72 struct nvme_cqe *);
73 static void nvme_sqe_fill(struct nvme_queue *, struct nvme_ccb *, void *);
74 static void nvme_empty_done(struct nvme_queue *, struct nvme_ccb *,
75 struct nvme_cqe *);
76
77 static struct nvme_queue *
78 nvme_q_alloc(struct nvme_softc *, uint16_t, u_int, u_int);
79 static int nvme_q_create(struct nvme_softc *, struct nvme_queue *);
80 static int nvme_q_delete(struct nvme_softc *, struct nvme_queue *);
81 static void nvme_q_submit(struct nvme_softc *, struct nvme_queue *,
82 struct nvme_ccb *, void (*)(struct nvme_queue *,
83 struct nvme_ccb *, void *));
84 static int nvme_q_complete(struct nvme_softc *, struct nvme_queue *q);
85 static void nvme_q_free(struct nvme_softc *, struct nvme_queue *);
86
87 static struct nvme_dmamem *
88 nvme_dmamem_alloc(struct nvme_softc *, size_t);
89 static void nvme_dmamem_free(struct nvme_softc *, struct nvme_dmamem *);
90 static void nvme_dmamem_sync(struct nvme_softc *, struct nvme_dmamem *,
91 int);
92
93 static void nvme_ns_io_fill(struct nvme_queue *, struct nvme_ccb *,
94 void *);
95 static void nvme_ns_io_done(struct nvme_queue *, struct nvme_ccb *,
96 struct nvme_cqe *);
97 static void nvme_ns_sync_fill(struct nvme_queue *, struct nvme_ccb *,
98 void *);
99 static void nvme_ns_sync_done(struct nvme_queue *, struct nvme_ccb *,
100 struct nvme_cqe *);
101 static void nvme_getcache_fill(struct nvme_queue *, struct nvme_ccb *,
102 void *);
103 static void nvme_getcache_done(struct nvme_queue *, struct nvme_ccb *,
104 struct nvme_cqe *);
105
106 static void nvme_pt_fill(struct nvme_queue *, struct nvme_ccb *,
107 void *);
108 static void nvme_pt_done(struct nvme_queue *, struct nvme_ccb *,
109 struct nvme_cqe *);
110 static int nvme_command_passthrough(struct nvme_softc *,
111 struct nvme_pt_command *, uint16_t, struct lwp *, bool);
112
113 static int nvme_get_number_of_queues(struct nvme_softc *, u_int *);
114
115 #define NVME_TIMO_QOP 5 /* queue create and delete timeout */
116 #define NVME_TIMO_IDENT 10 /* probe identify timeout */
117 #define NVME_TIMO_PT -1 /* passthrough cmd timeout */
118 #define NVME_TIMO_SY 60 /* sync cache timeout */
119
120 #define nvme_read4(_s, _r) \
121 bus_space_read_4((_s)->sc_iot, (_s)->sc_ioh, (_r))
122 #define nvme_write4(_s, _r, _v) \
123 bus_space_write_4((_s)->sc_iot, (_s)->sc_ioh, (_r), (_v))
124 /*
125 * Some controllers, at least Apple NVMe, always require split
126 * transfers, so don't use bus_space_{read,write}_8() on LP64.
127 */
128 static inline uint64_t
129 nvme_read8(struct nvme_softc *sc, bus_size_t r)
130 {
131 uint64_t v;
132 uint32_t *a = (uint32_t *)&v;
133
134 #if _BYTE_ORDER == _LITTLE_ENDIAN
135 a[0] = nvme_read4(sc, r);
136 a[1] = nvme_read4(sc, r + 4);
137 #else /* _BYTE_ORDER == _LITTLE_ENDIAN */
138 a[1] = nvme_read4(sc, r);
139 a[0] = nvme_read4(sc, r + 4);
140 #endif
141
142 return v;
143 }
144
145 static inline void
146 nvme_write8(struct nvme_softc *sc, bus_size_t r, uint64_t v)
147 {
148 uint32_t *a = (uint32_t *)&v;
149
150 #if _BYTE_ORDER == _LITTLE_ENDIAN
151 nvme_write4(sc, r, a[0]);
152 nvme_write4(sc, r + 4, a[1]);
153 #else /* _BYTE_ORDER == _LITTLE_ENDIAN */
154 nvme_write4(sc, r, a[1]);
155 nvme_write4(sc, r + 4, a[0]);
156 #endif
157 }
158 #define nvme_barrier(_s, _r, _l, _f) \
159 bus_space_barrier((_s)->sc_iot, (_s)->sc_ioh, (_r), (_l), (_f))
160
161 #ifdef NVME_DEBUG
162 static __used void
163 nvme_dumpregs(struct nvme_softc *sc)
164 {
165 uint64_t r8;
166 uint32_t r4;
167
168 #define DEVNAME(_sc) device_xname((_sc)->sc_dev)
169 r8 = nvme_read8(sc, NVME_CAP);
170 printf("%s: cap 0x%016"PRIx64"\n", DEVNAME(sc), nvme_read8(sc, NVME_CAP));
171 printf("%s: mpsmax %u (%u)\n", DEVNAME(sc),
172 (u_int)NVME_CAP_MPSMAX(r8), (1 << NVME_CAP_MPSMAX(r8)));
173 printf("%s: mpsmin %u (%u)\n", DEVNAME(sc),
174 (u_int)NVME_CAP_MPSMIN(r8), (1 << NVME_CAP_MPSMIN(r8)));
175 printf("%s: css %"PRIu64"\n", DEVNAME(sc), NVME_CAP_CSS(r8));
176 printf("%s: nssrs %"PRIu64"\n", DEVNAME(sc), NVME_CAP_NSSRS(r8));
177 printf("%s: dstrd %"PRIu64"\n", DEVNAME(sc), NVME_CAP_DSTRD(r8));
178 printf("%s: to %"PRIu64" msec\n", DEVNAME(sc), NVME_CAP_TO(r8));
179 printf("%s: ams %"PRIu64"\n", DEVNAME(sc), NVME_CAP_AMS(r8));
180 printf("%s: cqr %"PRIu64"\n", DEVNAME(sc), NVME_CAP_CQR(r8));
181 printf("%s: mqes %"PRIu64"\n", DEVNAME(sc), NVME_CAP_MQES(r8));
182
183 printf("%s: vs 0x%04x\n", DEVNAME(sc), nvme_read4(sc, NVME_VS));
184
185 r4 = nvme_read4(sc, NVME_CC);
186 printf("%s: cc 0x%04x\n", DEVNAME(sc), r4);
187 printf("%s: iocqes %u (%u)\n", DEVNAME(sc), NVME_CC_IOCQES_R(r4),
188 (1 << NVME_CC_IOCQES_R(r4)));
189 printf("%s: iosqes %u (%u)\n", DEVNAME(sc), NVME_CC_IOSQES_R(r4),
190 (1 << NVME_CC_IOSQES_R(r4)));
191 printf("%s: shn %u\n", DEVNAME(sc), NVME_CC_SHN_R(r4));
192 printf("%s: ams %u\n", DEVNAME(sc), NVME_CC_AMS_R(r4));
193 printf("%s: mps %u (%u)\n", DEVNAME(sc), NVME_CC_MPS_R(r4),
194 (1 << NVME_CC_MPS_R(r4)));
195 printf("%s: css %u\n", DEVNAME(sc), NVME_CC_CSS_R(r4));
196 printf("%s: en %u\n", DEVNAME(sc), ISSET(r4, NVME_CC_EN) ? 1 : 0);
197
198 r4 = nvme_read4(sc, NVME_CSTS);
199 printf("%s: csts 0x%08x\n", DEVNAME(sc), r4);
200 printf("%s: rdy %u\n", DEVNAME(sc), r4 & NVME_CSTS_RDY);
201 printf("%s: cfs %u\n", DEVNAME(sc), r4 & NVME_CSTS_CFS);
202 printf("%s: shst %x\n", DEVNAME(sc), r4 & NVME_CSTS_SHST_MASK);
203
204 r4 = nvme_read4(sc, NVME_AQA);
205 printf("%s: aqa 0x%08x\n", DEVNAME(sc), r4);
206 printf("%s: acqs %u\n", DEVNAME(sc), NVME_AQA_ACQS_R(r4));
207 printf("%s: asqs %u\n", DEVNAME(sc), NVME_AQA_ASQS_R(r4));
208
209 printf("%s: asq 0x%016"PRIx64"\n", DEVNAME(sc), nvme_read8(sc, NVME_ASQ));
210 printf("%s: acq 0x%016"PRIx64"\n", DEVNAME(sc), nvme_read8(sc, NVME_ACQ));
211 #undef DEVNAME
212 }
213 #endif /* NVME_DEBUG */
214
215 static int
216 nvme_ready(struct nvme_softc *sc, uint32_t rdy)
217 {
218 u_int i = 0;
219 uint32_t cc;
220
221 cc = nvme_read4(sc, NVME_CC);
222 if (((cc & NVME_CC_EN) != 0) != (rdy != 0)) {
223 aprint_error_dev(sc->sc_dev,
224 "controller enabled status expected %d, found to be %d\n",
225 (rdy != 0), ((cc & NVME_CC_EN) != 0));
226 return ENXIO;
227 }
228
229 while ((nvme_read4(sc, NVME_CSTS) & NVME_CSTS_RDY) != rdy) {
230 if (i++ > sc->sc_rdy_to)
231 return ENXIO;
232
233 delay(1000);
234 nvme_barrier(sc, NVME_CSTS, 4, BUS_SPACE_BARRIER_READ);
235 }
236
237 return 0;
238 }
239
240 static int
241 nvme_enable(struct nvme_softc *sc, u_int mps)
242 {
243 uint32_t cc, csts;
244
245 cc = nvme_read4(sc, NVME_CC);
246 csts = nvme_read4(sc, NVME_CSTS);
247
248 if (ISSET(cc, NVME_CC_EN)) {
249 aprint_error_dev(sc->sc_dev, "controller unexpectedly enabled, failed to stay disabled\n");
250
251 if (ISSET(csts, NVME_CSTS_RDY))
252 return 1;
253
254 goto waitready;
255 }
256
257 nvme_write8(sc, NVME_ASQ, NVME_DMA_DVA(sc->sc_admin_q->q_sq_dmamem));
258 nvme_barrier(sc, 0, sc->sc_ios, BUS_SPACE_BARRIER_WRITE);
259 delay(5000);
260 nvme_write8(sc, NVME_ACQ, NVME_DMA_DVA(sc->sc_admin_q->q_cq_dmamem));
261 nvme_barrier(sc, 0, sc->sc_ios, BUS_SPACE_BARRIER_WRITE);
262 delay(5000);
263
264 nvme_write4(sc, NVME_AQA, NVME_AQA_ACQS(sc->sc_admin_q->q_entries) |
265 NVME_AQA_ASQS(sc->sc_admin_q->q_entries));
266 nvme_barrier(sc, 0, sc->sc_ios, BUS_SPACE_BARRIER_WRITE);
267 delay(5000);
268
269 CLR(cc, NVME_CC_IOCQES_MASK | NVME_CC_IOSQES_MASK | NVME_CC_SHN_MASK |
270 NVME_CC_AMS_MASK | NVME_CC_MPS_MASK | NVME_CC_CSS_MASK);
271 SET(cc, NVME_CC_IOSQES(ffs(64) - 1) | NVME_CC_IOCQES(ffs(16) - 1));
272 SET(cc, NVME_CC_SHN(NVME_CC_SHN_NONE));
273 SET(cc, NVME_CC_CSS(NVME_CC_CSS_NVM));
274 SET(cc, NVME_CC_AMS(NVME_CC_AMS_RR));
275 SET(cc, NVME_CC_MPS(mps));
276 SET(cc, NVME_CC_EN);
277
278 nvme_write4(sc, NVME_CC, cc);
279 nvme_barrier(sc, 0, sc->sc_ios,
280 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
281 delay(5000);
282
283 waitready:
284 return nvme_ready(sc, NVME_CSTS_RDY);
285 }
286
287 static int
288 nvme_disable(struct nvme_softc *sc)
289 {
290 uint32_t cc, csts;
291
292 cc = nvme_read4(sc, NVME_CC);
293 csts = nvme_read4(sc, NVME_CSTS);
294
295 if (ISSET(cc, NVME_CC_EN) && !ISSET(csts, NVME_CSTS_RDY))
296 nvme_ready(sc, NVME_CSTS_RDY);
297
298 CLR(cc, NVME_CC_EN);
299
300 nvme_write4(sc, NVME_CC, cc);
301 nvme_barrier(sc, 0, sc->sc_ios, BUS_SPACE_BARRIER_READ);
302
303 delay(5000);
304
305 return nvme_ready(sc, 0);
306 }
307
308 int
309 nvme_attach(struct nvme_softc *sc)
310 {
311 uint64_t cap;
312 uint32_t reg;
313 u_int dstrd;
314 u_int mps = PAGE_SHIFT;
315 u_int ioq_allocated;
316 uint16_t adminq_entries = nvme_adminq_size;
317 uint16_t ioq_entries = nvme_ioq_size;
318 int i;
319
320 reg = nvme_read4(sc, NVME_VS);
321 if (reg == 0xffffffff) {
322 aprint_error_dev(sc->sc_dev, "invalid mapping\n");
323 return 1;
324 }
325
326 if (NVME_VS_TER(reg) == 0)
327 aprint_normal_dev(sc->sc_dev, "NVMe %d.%d\n", NVME_VS_MJR(reg),
328 NVME_VS_MNR(reg));
329 else
330 aprint_normal_dev(sc->sc_dev, "NVMe %d.%d.%d\n", NVME_VS_MJR(reg),
331 NVME_VS_MNR(reg), NVME_VS_TER(reg));
332
333 cap = nvme_read8(sc, NVME_CAP);
334 dstrd = NVME_CAP_DSTRD(cap);
335 if (NVME_CAP_MPSMIN(cap) > PAGE_SHIFT) {
336 aprint_error_dev(sc->sc_dev, "NVMe minimum page size %u "
337 "is greater than CPU page size %u\n",
338 1 << NVME_CAP_MPSMIN(cap), 1 << PAGE_SHIFT);
339 return 1;
340 }
341 if (NVME_CAP_MPSMAX(cap) < mps)
342 mps = NVME_CAP_MPSMAX(cap);
343 if (ioq_entries > NVME_CAP_MQES(cap))
344 ioq_entries = NVME_CAP_MQES(cap);
345
346 /* set initial values to be used for admin queue during probe */
347 sc->sc_rdy_to = NVME_CAP_TO(cap);
348 sc->sc_mps = 1 << mps;
349 sc->sc_mdts = MAXPHYS;
350 sc->sc_max_sgl = 2;
351
352 if (nvme_disable(sc) != 0) {
353 aprint_error_dev(sc->sc_dev, "unable to disable controller\n");
354 return 1;
355 }
356
357 sc->sc_admin_q = nvme_q_alloc(sc, NVME_ADMIN_Q, adminq_entries, dstrd);
358 if (sc->sc_admin_q == NULL) {
359 aprint_error_dev(sc->sc_dev,
360 "unable to allocate admin queue\n");
361 return 1;
362 }
363 if (sc->sc_intr_establish(sc, NVME_ADMIN_Q, sc->sc_admin_q))
364 goto free_admin_q;
365
366 if (nvme_enable(sc, mps) != 0) {
367 aprint_error_dev(sc->sc_dev, "unable to enable controller\n");
368 goto disestablish_admin_q;
369 }
370
371 if (nvme_identify(sc, NVME_CAP_MPSMIN(cap)) != 0) {
372 aprint_error_dev(sc->sc_dev, "unable to identify controller\n");
373 goto disable;
374 }
375
376 /* we know how big things are now */
377 sc->sc_max_sgl = sc->sc_mdts / sc->sc_mps;
378
379 /* reallocate ccbs of admin queue with new max sgl. */
380 nvme_ccbs_free(sc->sc_admin_q);
381 nvme_ccbs_alloc(sc->sc_admin_q, sc->sc_admin_q->q_entries);
382
383 if (sc->sc_use_mq) {
384 /* Limit the number of queues to the number allocated in HW */
385 if (nvme_get_number_of_queues(sc, &ioq_allocated) != 0) {
386 aprint_error_dev(sc->sc_dev,
387 "unable to get number of queues\n");
388 goto disable;
389 }
390 if (sc->sc_nq > ioq_allocated)
391 sc->sc_nq = ioq_allocated;
392 }
393
394 sc->sc_q = kmem_zalloc(sizeof(*sc->sc_q) * sc->sc_nq, KM_SLEEP);
395 for (i = 0; i < sc->sc_nq; i++) {
396 sc->sc_q[i] = nvme_q_alloc(sc, i + 1, ioq_entries, dstrd);
397 if (sc->sc_q[i] == NULL) {
398 aprint_error_dev(sc->sc_dev,
399 "unable to allocate io queue\n");
400 goto free_q;
401 }
402 if (nvme_q_create(sc, sc->sc_q[i]) != 0) {
403 aprint_error_dev(sc->sc_dev,
404 "unable to create io queue\n");
405 nvme_q_free(sc, sc->sc_q[i]);
406 goto free_q;
407 }
408 }
409
410 if (!sc->sc_use_mq)
411 nvme_write4(sc, NVME_INTMC, 1);
412
413 /* probe subdevices */
414 sc->sc_namespaces = kmem_zalloc(sizeof(*sc->sc_namespaces) * sc->sc_nn,
415 KM_SLEEP);
416 nvme_rescan(sc->sc_dev, "nvme", &i);
417
418 return 0;
419
420 free_q:
421 while (--i >= 0) {
422 nvme_q_delete(sc, sc->sc_q[i]);
423 nvme_q_free(sc, sc->sc_q[i]);
424 }
425 disable:
426 nvme_disable(sc);
427 disestablish_admin_q:
428 sc->sc_intr_disestablish(sc, NVME_ADMIN_Q);
429 free_admin_q:
430 nvme_q_free(sc, sc->sc_admin_q);
431
432 return 1;
433 }
434
435 int
436 nvme_rescan(device_t self, const char *attr, const int *flags)
437 {
438 struct nvme_softc *sc = device_private(self);
439 struct nvme_attach_args naa;
440 uint64_t cap;
441 int ioq_entries = nvme_ioq_size;
442 int i;
443
444 cap = nvme_read8(sc, NVME_CAP);
445 if (ioq_entries > NVME_CAP_MQES(cap))
446 ioq_entries = NVME_CAP_MQES(cap);
447
448 for (i = 0; i < sc->sc_nn; i++) {
449 if (sc->sc_namespaces[i].dev)
450 continue;
451 memset(&naa, 0, sizeof(naa));
452 naa.naa_nsid = i + 1;
453 naa.naa_qentries = (ioq_entries - 1) * sc->sc_nq;
454 naa.naa_maxphys = sc->sc_mdts;
455 sc->sc_namespaces[i].dev = config_found(sc->sc_dev, &naa,
456 nvme_print);
457 }
458 return 0;
459 }
460
461 static int
462 nvme_print(void *aux, const char *pnp)
463 {
464 struct nvme_attach_args *naa = aux;
465
466 if (pnp)
467 aprint_normal("at %s", pnp);
468
469 if (naa->naa_nsid > 0)
470 aprint_normal(" nsid %d", naa->naa_nsid);
471
472 return UNCONF;
473 }
474
475 int
476 nvme_detach(struct nvme_softc *sc, int flags)
477 {
478 int i, error;
479
480 error = config_detach_children(sc->sc_dev, flags);
481 if (error)
482 return error;
483
484 error = nvme_shutdown(sc);
485 if (error)
486 return error;
487
488 /* from now on we are committed to detach, following will never fail */
489 for (i = 0; i < sc->sc_nq; i++)
490 nvme_q_free(sc, sc->sc_q[i]);
491 kmem_free(sc->sc_q, sizeof(*sc->sc_q) * sc->sc_nq);
492 nvme_q_free(sc, sc->sc_admin_q);
493
494 return 0;
495 }
496
497 static int
498 nvme_shutdown(struct nvme_softc *sc)
499 {
500 uint32_t cc, csts;
501 bool disabled = false;
502 int i;
503
504 if (!sc->sc_use_mq)
505 nvme_write4(sc, NVME_INTMS, 1);
506
507 for (i = 0; i < sc->sc_nq; i++) {
508 if (nvme_q_delete(sc, sc->sc_q[i]) != 0) {
509 aprint_error_dev(sc->sc_dev,
510 "unable to delete io queue %d, disabling\n", i + 1);
511 disabled = true;
512 }
513 }
514 sc->sc_intr_disestablish(sc, NVME_ADMIN_Q);
515 if (disabled)
516 goto disable;
517
518 cc = nvme_read4(sc, NVME_CC);
519 CLR(cc, NVME_CC_SHN_MASK);
520 SET(cc, NVME_CC_SHN(NVME_CC_SHN_NORMAL));
521 nvme_write4(sc, NVME_CC, cc);
522
523 for (i = 0; i < 4000; i++) {
524 nvme_barrier(sc, 0, sc->sc_ios,
525 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
526 csts = nvme_read4(sc, NVME_CSTS);
527 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_DONE)
528 return 0;
529
530 delay(1000);
531 }
532
533 aprint_error_dev(sc->sc_dev, "unable to shudown, disabling\n");
534
535 disable:
536 nvme_disable(sc);
537 return 0;
538 }
539
540 void
541 nvme_childdet(device_t self, device_t child)
542 {
543 struct nvme_softc *sc = device_private(self);
544 int i;
545
546 for (i = 0; i < sc->sc_nn; i++) {
547 if (sc->sc_namespaces[i].dev == child) {
548 /* Already freed ns->ident. */
549 sc->sc_namespaces[i].dev = NULL;
550 break;
551 }
552 }
553 }
554
555 int
556 nvme_ns_identify(struct nvme_softc *sc, uint16_t nsid)
557 {
558 struct nvme_sqe sqe;
559 struct nvm_identify_namespace *identify;
560 struct nvme_dmamem *mem;
561 struct nvme_ccb *ccb;
562 struct nvme_namespace *ns;
563 int rv;
564
565 KASSERT(nsid > 0);
566
567 ccb = nvme_ccb_get(sc->sc_admin_q);
568 KASSERT(ccb != NULL); /* it's a bug if we don't have spare ccb here */
569
570 mem = nvme_dmamem_alloc(sc, sizeof(*identify));
571 if (mem == NULL)
572 return ENOMEM;
573
574 memset(&sqe, 0, sizeof(sqe));
575 sqe.opcode = NVM_ADMIN_IDENTIFY;
576 htolem32(&sqe.nsid, nsid);
577 htolem64(&sqe.entry.prp[0], NVME_DMA_DVA(mem));
578 htolem32(&sqe.cdw10, 0);
579
580 ccb->ccb_done = nvme_empty_done;
581 ccb->ccb_cookie = &sqe;
582
583 nvme_dmamem_sync(sc, mem, BUS_DMASYNC_PREREAD);
584 rv = nvme_poll(sc, sc->sc_admin_q, ccb, nvme_sqe_fill, NVME_TIMO_IDENT);
585 nvme_dmamem_sync(sc, mem, BUS_DMASYNC_POSTREAD);
586
587 nvme_ccb_put(sc->sc_admin_q, ccb);
588
589 if (rv != 0) {
590 rv = EIO;
591 goto done;
592 }
593
594 /* commit */
595
596 identify = kmem_zalloc(sizeof(*identify), KM_SLEEP);
597 *identify = *((volatile struct nvm_identify_namespace *)NVME_DMA_KVA(mem));
598 //memcpy(identify, NVME_DMA_KVA(mem), sizeof(*identify));
599
600 ns = nvme_ns_get(sc, nsid);
601 KASSERT(ns);
602 ns->ident = identify;
603
604 done:
605 nvme_dmamem_free(sc, mem);
606
607 return rv;
608 }
609
610 int
611 nvme_ns_dobio(struct nvme_softc *sc, uint16_t nsid, void *cookie,
612 struct buf *bp, void *data, size_t datasize,
613 int secsize, daddr_t blkno, int flags, nvme_nnc_done nnc_done)
614 {
615 struct nvme_queue *q = nvme_get_q(sc);
616 struct nvme_ccb *ccb;
617 bus_dmamap_t dmap;
618 int i, error;
619
620 ccb = nvme_ccb_get(q);
621 if (ccb == NULL)
622 return EAGAIN;
623
624 ccb->ccb_done = nvme_ns_io_done;
625 ccb->ccb_cookie = cookie;
626
627 /* namespace context */
628 ccb->nnc_nsid = nsid;
629 ccb->nnc_flags = flags;
630 ccb->nnc_buf = bp;
631 ccb->nnc_datasize = datasize;
632 ccb->nnc_secsize = secsize;
633 ccb->nnc_blkno = blkno;
634 ccb->nnc_done = nnc_done;
635
636 dmap = ccb->ccb_dmamap;
637 error = bus_dmamap_load(sc->sc_dmat, dmap, data,
638 datasize, NULL,
639 (ISSET(flags, NVME_NS_CTX_F_POLL) ?
640 BUS_DMA_NOWAIT : BUS_DMA_WAITOK) |
641 (ISSET(flags, NVME_NS_CTX_F_READ) ?
642 BUS_DMA_READ : BUS_DMA_WRITE));
643 if (error) {
644 nvme_ccb_put(q, ccb);
645 return error;
646 }
647
648 bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
649 ISSET(flags, NVME_NS_CTX_F_READ) ?
650 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
651
652 if (dmap->dm_nsegs > 2) {
653 for (i = 1; i < dmap->dm_nsegs; i++) {
654 htolem64(&ccb->ccb_prpl[i - 1],
655 dmap->dm_segs[i].ds_addr);
656 }
657 bus_dmamap_sync(sc->sc_dmat,
658 NVME_DMA_MAP(q->q_ccb_prpls),
659 ccb->ccb_prpl_off,
660 sizeof(*ccb->ccb_prpl) * (dmap->dm_nsegs - 1),
661 BUS_DMASYNC_PREWRITE);
662 }
663
664 if (ISSET(flags, NVME_NS_CTX_F_POLL)) {
665 if (nvme_poll(sc, q, ccb, nvme_ns_io_fill, NVME_TIMO_PT) != 0)
666 return EIO;
667 return 0;
668 }
669
670 nvme_q_submit(sc, q, ccb, nvme_ns_io_fill);
671 return 0;
672 }
673
674 static void
675 nvme_ns_io_fill(struct nvme_queue *q, struct nvme_ccb *ccb, void *slot)
676 {
677 struct nvme_sqe_io *sqe = slot;
678 bus_dmamap_t dmap = ccb->ccb_dmamap;
679
680 sqe->opcode = ISSET(ccb->nnc_flags, NVME_NS_CTX_F_READ) ?
681 NVM_CMD_READ : NVM_CMD_WRITE;
682 htolem32(&sqe->nsid, ccb->nnc_nsid);
683
684 htolem64(&sqe->entry.prp[0], dmap->dm_segs[0].ds_addr);
685 switch (dmap->dm_nsegs) {
686 case 1:
687 break;
688 case 2:
689 htolem64(&sqe->entry.prp[1], dmap->dm_segs[1].ds_addr);
690 break;
691 default:
692 /* the prp list is already set up and synced */
693 htolem64(&sqe->entry.prp[1], ccb->ccb_prpl_dva);
694 break;
695 }
696
697 htolem64(&sqe->slba, ccb->nnc_blkno);
698
699 if (ISSET(ccb->nnc_flags, NVME_NS_CTX_F_FUA))
700 htolem16(&sqe->ioflags, NVM_SQE_IO_FUA);
701
702 /* guaranteed by upper layers, but check just in case */
703 KASSERT((ccb->nnc_datasize % ccb->nnc_secsize) == 0);
704 htolem16(&sqe->nlb, (ccb->nnc_datasize / ccb->nnc_secsize) - 1);
705 }
706
707 static void
708 nvme_ns_io_done(struct nvme_queue *q, struct nvme_ccb *ccb,
709 struct nvme_cqe *cqe)
710 {
711 struct nvme_softc *sc = q->q_sc;
712 bus_dmamap_t dmap = ccb->ccb_dmamap;
713 void *nnc_cookie = ccb->ccb_cookie;
714 nvme_nnc_done nnc_done = ccb->nnc_done;
715 struct buf *bp = ccb->nnc_buf;
716
717 if (dmap->dm_nsegs > 2) {
718 bus_dmamap_sync(sc->sc_dmat,
719 NVME_DMA_MAP(q->q_ccb_prpls),
720 ccb->ccb_prpl_off,
721 sizeof(*ccb->ccb_prpl) * (dmap->dm_nsegs - 1),
722 BUS_DMASYNC_POSTWRITE);
723 }
724
725 bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
726 ISSET(ccb->nnc_flags, NVME_NS_CTX_F_READ) ?
727 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
728
729 bus_dmamap_unload(sc->sc_dmat, dmap);
730 nvme_ccb_put(q, ccb);
731
732 nnc_done(nnc_cookie, bp, lemtoh16(&cqe->flags), lemtoh32(&cqe->cdw0));
733 }
734
735 /*
736 * If there is no volatile write cache, it makes no sense to issue
737 * flush commands or query for the status.
738 */
739 bool
740 nvme_has_volatile_write_cache(struct nvme_softc *sc)
741 {
742 /* sc_identify is filled during attachment */
743 return ((sc->sc_identify.vwc & NVME_ID_CTRLR_VWC_PRESENT) != 0);
744 }
745
746 int
747 nvme_ns_sync(struct nvme_softc *sc, uint16_t nsid, void *cookie,
748 int flags, nvme_nnc_done nnc_done)
749 {
750 struct nvme_queue *q = nvme_get_q(sc);
751 struct nvme_ccb *ccb;
752
753 ccb = nvme_ccb_get(q);
754 if (ccb == NULL)
755 return EAGAIN;
756
757 ccb->ccb_done = nvme_ns_sync_done;
758 ccb->ccb_cookie = cookie;
759
760 /* namespace context */
761 ccb->nnc_nsid = nsid;
762 ccb->nnc_flags = flags;
763 ccb->nnc_done = nnc_done;
764
765 if (ISSET(flags, NVME_NS_CTX_F_POLL)) {
766 if (nvme_poll(sc, q, ccb, nvme_ns_sync_fill, NVME_TIMO_SY) != 0)
767 return EIO;
768 return 0;
769 }
770
771 nvme_q_submit(sc, q, ccb, nvme_ns_sync_fill);
772 return 0;
773 }
774
775 static void
776 nvme_ns_sync_fill(struct nvme_queue *q, struct nvme_ccb *ccb, void *slot)
777 {
778 struct nvme_sqe *sqe = slot;
779
780 sqe->opcode = NVM_CMD_FLUSH;
781 htolem32(&sqe->nsid, ccb->nnc_nsid);
782 }
783
784 static void
785 nvme_ns_sync_done(struct nvme_queue *q, struct nvme_ccb *ccb,
786 struct nvme_cqe *cqe)
787 {
788 void *cookie = ccb->ccb_cookie;
789 nvme_nnc_done nnc_done = ccb->nnc_done;
790
791 nvme_ccb_put(q, ccb);
792
793 nnc_done(cookie, NULL, lemtoh16(&cqe->flags), lemtoh32(&cqe->cdw0));
794 }
795
796 /*
797 * Get status of volatile write cache. Always asynchronous.
798 */
799 int
800 nvme_admin_getcache(struct nvme_softc *sc, void *cookie, nvme_nnc_done nnc_done)
801 {
802 struct nvme_ccb *ccb;
803 struct nvme_queue *q = sc->sc_admin_q;
804
805 ccb = nvme_ccb_get(q);
806 if (ccb == NULL)
807 return EAGAIN;
808
809 ccb->ccb_done = nvme_getcache_done;
810 ccb->ccb_cookie = cookie;
811
812 /* namespace context */
813 ccb->nnc_flags = 0;
814 ccb->nnc_done = nnc_done;
815
816 nvme_q_submit(sc, q, ccb, nvme_getcache_fill);
817 return 0;
818 }
819
820 static void
821 nvme_getcache_fill(struct nvme_queue *q, struct nvme_ccb *ccb, void *slot)
822 {
823 struct nvme_sqe *sqe = slot;
824
825 sqe->opcode = NVM_ADMIN_GET_FEATURES;
826 sqe->cdw10 = NVM_FEATURE_VOLATILE_WRITE_CACHE;
827 }
828
829 static void
830 nvme_getcache_done(struct nvme_queue *q, struct nvme_ccb *ccb,
831 struct nvme_cqe *cqe)
832 {
833 void *cookie = ccb->ccb_cookie;
834 nvme_nnc_done nnc_done = ccb->nnc_done;
835
836 nvme_ccb_put(q, ccb);
837
838 nnc_done(cookie, NULL, lemtoh16(&cqe->flags), lemtoh32(&cqe->cdw0));
839 }
840
841 void
842 nvme_ns_free(struct nvme_softc *sc, uint16_t nsid)
843 {
844 struct nvme_namespace *ns;
845 struct nvm_identify_namespace *identify;
846
847 ns = nvme_ns_get(sc, nsid);
848 KASSERT(ns);
849
850 identify = ns->ident;
851 ns->ident = NULL;
852 if (identify != NULL)
853 kmem_free(identify, sizeof(*identify));
854 }
855
856 static void
857 nvme_pt_fill(struct nvme_queue *q, struct nvme_ccb *ccb, void *slot)
858 {
859 struct nvme_softc *sc = q->q_sc;
860 struct nvme_sqe *sqe = slot;
861 struct nvme_pt_command *pt = ccb->ccb_cookie;
862 bus_dmamap_t dmap = ccb->ccb_dmamap;
863 int i;
864
865 sqe->opcode = pt->cmd.opcode;
866 htolem32(&sqe->nsid, pt->cmd.nsid);
867
868 if (pt->buf != NULL && pt->len > 0) {
869 htolem64(&sqe->entry.prp[0], dmap->dm_segs[0].ds_addr);
870 switch (dmap->dm_nsegs) {
871 case 1:
872 break;
873 case 2:
874 htolem64(&sqe->entry.prp[1], dmap->dm_segs[1].ds_addr);
875 break;
876 default:
877 for (i = 1; i < dmap->dm_nsegs; i++) {
878 htolem64(&ccb->ccb_prpl[i - 1],
879 dmap->dm_segs[i].ds_addr);
880 }
881 bus_dmamap_sync(sc->sc_dmat,
882 NVME_DMA_MAP(q->q_ccb_prpls),
883 ccb->ccb_prpl_off,
884 sizeof(*ccb->ccb_prpl) * (dmap->dm_nsegs - 1),
885 BUS_DMASYNC_PREWRITE);
886 htolem64(&sqe->entry.prp[1], ccb->ccb_prpl_dva);
887 break;
888 }
889 }
890
891 htolem32(&sqe->cdw10, pt->cmd.cdw10);
892 htolem32(&sqe->cdw11, pt->cmd.cdw11);
893 htolem32(&sqe->cdw12, pt->cmd.cdw12);
894 htolem32(&sqe->cdw13, pt->cmd.cdw13);
895 htolem32(&sqe->cdw14, pt->cmd.cdw14);
896 htolem32(&sqe->cdw15, pt->cmd.cdw15);
897 }
898
899 static void
900 nvme_pt_done(struct nvme_queue *q, struct nvme_ccb *ccb, struct nvme_cqe *cqe)
901 {
902 struct nvme_softc *sc = q->q_sc;
903 struct nvme_pt_command *pt = ccb->ccb_cookie;
904 bus_dmamap_t dmap = ccb->ccb_dmamap;
905
906 if (pt->buf != NULL && pt->len > 0) {
907 if (dmap->dm_nsegs > 2) {
908 bus_dmamap_sync(sc->sc_dmat,
909 NVME_DMA_MAP(q->q_ccb_prpls),
910 ccb->ccb_prpl_off,
911 sizeof(*ccb->ccb_prpl) * (dmap->dm_nsegs - 1),
912 BUS_DMASYNC_POSTWRITE);
913 }
914
915 bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
916 pt->is_read ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
917 bus_dmamap_unload(sc->sc_dmat, dmap);
918 }
919
920 pt->cpl.cdw0 = lemtoh32(&cqe->cdw0);
921 pt->cpl.flags = lemtoh16(&cqe->flags) & ~NVME_CQE_PHASE;
922 }
923
924 static int
925 nvme_command_passthrough(struct nvme_softc *sc, struct nvme_pt_command *pt,
926 uint16_t nsid, struct lwp *l, bool is_adminq)
927 {
928 struct nvme_queue *q;
929 struct nvme_ccb *ccb;
930 void *buf = NULL;
931 int error;
932
933 /* limit command size to maximum data transfer size */
934 if ((pt->buf == NULL && pt->len > 0) ||
935 (pt->buf != NULL && (pt->len == 0 || pt->len > sc->sc_mdts)))
936 return EINVAL;
937
938 q = is_adminq ? sc->sc_admin_q : nvme_get_q(sc);
939 ccb = nvme_ccb_get(q);
940 if (ccb == NULL)
941 return EBUSY;
942
943 if (pt->buf != NULL) {
944 KASSERT(pt->len > 0);
945 buf = kmem_alloc(pt->len, KM_SLEEP);
946 if (!pt->is_read) {
947 error = copyin(pt->buf, buf, pt->len);
948 if (error)
949 goto kmem_free;
950 }
951 error = bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap, buf,
952 pt->len, NULL,
953 BUS_DMA_WAITOK |
954 (pt->is_read ? BUS_DMA_READ : BUS_DMA_WRITE));
955 if (error)
956 goto kmem_free;
957 bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap,
958 0, ccb->ccb_dmamap->dm_mapsize,
959 pt->is_read ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
960 }
961
962 ccb->ccb_done = nvme_pt_done;
963 ccb->ccb_cookie = pt;
964
965 pt->cmd.nsid = nsid;
966 if (nvme_poll(sc, q, ccb, nvme_pt_fill, NVME_TIMO_PT)) {
967 error = EIO;
968 goto out;
969 }
970
971 error = 0;
972 out:
973 if (buf != NULL) {
974 if (error == 0 && pt->is_read)
975 error = copyout(buf, pt->buf, pt->len);
976 kmem_free:
977 kmem_free(buf, pt->len);
978 }
979 nvme_ccb_put(q, ccb);
980 return error;
981 }
982
983 static void
984 nvme_q_submit(struct nvme_softc *sc, struct nvme_queue *q, struct nvme_ccb *ccb,
985 void (*fill)(struct nvme_queue *, struct nvme_ccb *, void *))
986 {
987 struct nvme_sqe *sqe = NVME_DMA_KVA(q->q_sq_dmamem);
988 uint32_t tail;
989
990 mutex_enter(&q->q_sq_mtx);
991 tail = q->q_sq_tail;
992 if (++q->q_sq_tail >= q->q_entries)
993 q->q_sq_tail = 0;
994
995 sqe += tail;
996
997 bus_dmamap_sync(sc->sc_dmat, NVME_DMA_MAP(q->q_sq_dmamem),
998 sizeof(*sqe) * tail, sizeof(*sqe), BUS_DMASYNC_POSTWRITE);
999 memset(sqe, 0, sizeof(*sqe));
1000 (*fill)(q, ccb, sqe);
1001 sqe->cid = ccb->ccb_id;
1002 bus_dmamap_sync(sc->sc_dmat, NVME_DMA_MAP(q->q_sq_dmamem),
1003 sizeof(*sqe) * tail, sizeof(*sqe), BUS_DMASYNC_PREWRITE);
1004
1005 nvme_write4(sc, q->q_sqtdbl, q->q_sq_tail);
1006 mutex_exit(&q->q_sq_mtx);
1007 }
1008
1009 struct nvme_poll_state {
1010 struct nvme_sqe s;
1011 struct nvme_cqe c;
1012 };
1013
1014 static int
1015 nvme_poll(struct nvme_softc *sc, struct nvme_queue *q, struct nvme_ccb *ccb,
1016 void (*fill)(struct nvme_queue *, struct nvme_ccb *, void *), int timo_sec)
1017 {
1018 struct nvme_poll_state state;
1019 void (*done)(struct nvme_queue *, struct nvme_ccb *, struct nvme_cqe *);
1020 void *cookie;
1021 uint16_t flags;
1022 int step = 10;
1023 int maxloop = timo_sec * 1000000 / step;
1024 int error = 0;
1025
1026 memset(&state, 0, sizeof(state));
1027 (*fill)(q, ccb, &state.s);
1028
1029 done = ccb->ccb_done;
1030 cookie = ccb->ccb_cookie;
1031
1032 ccb->ccb_done = nvme_poll_done;
1033 ccb->ccb_cookie = &state;
1034
1035 nvme_q_submit(sc, q, ccb, nvme_poll_fill);
1036 while (!ISSET(state.c.flags, htole16(NVME_CQE_PHASE))) {
1037 if (nvme_q_complete(sc, q) == 0)
1038 delay(step);
1039
1040 if (timo_sec >= 0 && --maxloop <= 0) {
1041 error = ETIMEDOUT;
1042 break;
1043 }
1044 }
1045
1046 ccb->ccb_cookie = cookie;
1047 done(q, ccb, &state.c);
1048
1049 if (error == 0) {
1050 flags = lemtoh16(&state.c.flags);
1051 return flags & ~NVME_CQE_PHASE;
1052 } else {
1053 return 1;
1054 }
1055 }
1056
1057 static void
1058 nvme_poll_fill(struct nvme_queue *q, struct nvme_ccb *ccb, void *slot)
1059 {
1060 struct nvme_sqe *sqe = slot;
1061 struct nvme_poll_state *state = ccb->ccb_cookie;
1062
1063 *sqe = state->s;
1064 }
1065
1066 static void
1067 nvme_poll_done(struct nvme_queue *q, struct nvme_ccb *ccb,
1068 struct nvme_cqe *cqe)
1069 {
1070 struct nvme_poll_state *state = ccb->ccb_cookie;
1071
1072 SET(cqe->flags, htole16(NVME_CQE_PHASE));
1073 state->c = *cqe;
1074 }
1075
1076 static void
1077 nvme_sqe_fill(struct nvme_queue *q, struct nvme_ccb *ccb, void *slot)
1078 {
1079 struct nvme_sqe *src = ccb->ccb_cookie;
1080 struct nvme_sqe *dst = slot;
1081
1082 *dst = *src;
1083 }
1084
1085 static void
1086 nvme_empty_done(struct nvme_queue *q, struct nvme_ccb *ccb,
1087 struct nvme_cqe *cqe)
1088 {
1089 }
1090
1091 static int
1092 nvme_q_complete(struct nvme_softc *sc, struct nvme_queue *q)
1093 {
1094 struct nvme_ccb *ccb;
1095 struct nvme_cqe *ring = NVME_DMA_KVA(q->q_cq_dmamem), *cqe;
1096 uint16_t flags;
1097 int rv = 0;
1098
1099 mutex_enter(&q->q_cq_mtx);
1100
1101 nvme_dmamem_sync(sc, q->q_cq_dmamem, BUS_DMASYNC_POSTREAD);
1102 for (;;) {
1103 cqe = &ring[q->q_cq_head];
1104 flags = lemtoh16(&cqe->flags);
1105 if ((flags & NVME_CQE_PHASE) != q->q_cq_phase)
1106 break;
1107
1108 ccb = &q->q_ccbs[cqe->cid];
1109
1110 if (++q->q_cq_head >= q->q_entries) {
1111 q->q_cq_head = 0;
1112 q->q_cq_phase ^= NVME_CQE_PHASE;
1113 }
1114
1115 #ifdef DEBUG
1116 /*
1117 * If we get spurious completion notification, something
1118 * is seriously hosed up. Very likely DMA to some random
1119 * memory place happened, so just bail out.
1120 */
1121 if ((intptr_t)ccb->ccb_cookie == NVME_CCB_FREE) {
1122 panic("%s: invalid ccb detected",
1123 device_xname(sc->sc_dev));
1124 /* NOTREACHED */
1125 }
1126 #endif
1127
1128 rv++;
1129
1130 /*
1131 * Unlock the mutex before calling the ccb_done callback
1132 * and re-lock afterwards. The callback triggers lddone()
1133 * which schedules another i/o, and also calls nvme_ccb_put().
1134 * Unlock/relock avoids possibility of deadlock.
1135 */
1136 mutex_exit(&q->q_cq_mtx);
1137 ccb->ccb_done(q, ccb, cqe);
1138 mutex_enter(&q->q_cq_mtx);
1139 }
1140 nvme_dmamem_sync(sc, q->q_cq_dmamem, BUS_DMASYNC_PREREAD);
1141
1142 if (rv)
1143 nvme_write4(sc, q->q_cqhdbl, q->q_cq_head);
1144
1145 mutex_exit(&q->q_cq_mtx);
1146
1147 if (rv) {
1148 mutex_enter(&q->q_ccb_mtx);
1149 q->q_nccbs_avail += rv;
1150 mutex_exit(&q->q_ccb_mtx);
1151 }
1152
1153 return rv;
1154 }
1155
1156 static int
1157 nvme_identify(struct nvme_softc *sc, u_int mps)
1158 {
1159 char sn[41], mn[81], fr[17];
1160 struct nvm_identify_controller *identify;
1161 struct nvme_dmamem *mem;
1162 struct nvme_ccb *ccb;
1163 u_int mdts;
1164 int rv = 1;
1165
1166 ccb = nvme_ccb_get(sc->sc_admin_q);
1167 KASSERT(ccb != NULL); /* it's a bug if we don't have spare ccb here */
1168
1169 mem = nvme_dmamem_alloc(sc, sizeof(*identify));
1170 if (mem == NULL)
1171 return 1;
1172
1173 ccb->ccb_done = nvme_empty_done;
1174 ccb->ccb_cookie = mem;
1175
1176 nvme_dmamem_sync(sc, mem, BUS_DMASYNC_PREREAD);
1177 rv = nvme_poll(sc, sc->sc_admin_q, ccb, nvme_fill_identify,
1178 NVME_TIMO_IDENT);
1179 nvme_dmamem_sync(sc, mem, BUS_DMASYNC_POSTREAD);
1180
1181 nvme_ccb_put(sc->sc_admin_q, ccb);
1182
1183 if (rv != 0)
1184 goto done;
1185
1186 identify = NVME_DMA_KVA(mem);
1187
1188 strnvisx(sn, sizeof(sn), (const char *)identify->sn,
1189 sizeof(identify->sn), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
1190 strnvisx(mn, sizeof(mn), (const char *)identify->mn,
1191 sizeof(identify->mn), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
1192 strnvisx(fr, sizeof(fr), (const char *)identify->fr,
1193 sizeof(identify->fr), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
1194 aprint_normal_dev(sc->sc_dev, "%s, firmware %s, serial %s\n", mn, fr,
1195 sn);
1196
1197 if (identify->mdts > 0) {
1198 mdts = (1 << identify->mdts) * (1 << mps);
1199 if (mdts < sc->sc_mdts)
1200 sc->sc_mdts = mdts;
1201 }
1202
1203 sc->sc_nn = lemtoh32(&identify->nn);
1204
1205 memcpy(&sc->sc_identify, identify, sizeof(sc->sc_identify));
1206
1207 done:
1208 nvme_dmamem_free(sc, mem);
1209
1210 return rv;
1211 }
1212
1213 static int
1214 nvme_q_create(struct nvme_softc *sc, struct nvme_queue *q)
1215 {
1216 struct nvme_sqe_q sqe;
1217 struct nvme_ccb *ccb;
1218 int rv;
1219
1220 if (sc->sc_use_mq && sc->sc_intr_establish(sc, q->q_id, q) != 0)
1221 return 1;
1222
1223 ccb = nvme_ccb_get(sc->sc_admin_q);
1224 KASSERT(ccb != NULL);
1225
1226 ccb->ccb_done = nvme_empty_done;
1227 ccb->ccb_cookie = &sqe;
1228
1229 memset(&sqe, 0, sizeof(sqe));
1230 sqe.opcode = NVM_ADMIN_ADD_IOCQ;
1231 htolem64(&sqe.prp1, NVME_DMA_DVA(q->q_cq_dmamem));
1232 htolem16(&sqe.qsize, q->q_entries - 1);
1233 htolem16(&sqe.qid, q->q_id);
1234 sqe.qflags = NVM_SQE_CQ_IEN | NVM_SQE_Q_PC;
1235 if (sc->sc_use_mq)
1236 htolem16(&sqe.cqid, q->q_id); /* qid == vector */
1237
1238 rv = nvme_poll(sc, sc->sc_admin_q, ccb, nvme_sqe_fill, NVME_TIMO_QOP);
1239 if (rv != 0)
1240 goto fail;
1241
1242 ccb->ccb_done = nvme_empty_done;
1243 ccb->ccb_cookie = &sqe;
1244
1245 memset(&sqe, 0, sizeof(sqe));
1246 sqe.opcode = NVM_ADMIN_ADD_IOSQ;
1247 htolem64(&sqe.prp1, NVME_DMA_DVA(q->q_sq_dmamem));
1248 htolem16(&sqe.qsize, q->q_entries - 1);
1249 htolem16(&sqe.qid, q->q_id);
1250 htolem16(&sqe.cqid, q->q_id);
1251 sqe.qflags = NVM_SQE_Q_PC;
1252
1253 rv = nvme_poll(sc, sc->sc_admin_q, ccb, nvme_sqe_fill, NVME_TIMO_QOP);
1254 if (rv != 0)
1255 goto fail;
1256
1257 fail:
1258 nvme_ccb_put(sc->sc_admin_q, ccb);
1259 return rv;
1260 }
1261
1262 static int
1263 nvme_q_delete(struct nvme_softc *sc, struct nvme_queue *q)
1264 {
1265 struct nvme_sqe_q sqe;
1266 struct nvme_ccb *ccb;
1267 int rv;
1268
1269 ccb = nvme_ccb_get(sc->sc_admin_q);
1270 KASSERT(ccb != NULL);
1271
1272 ccb->ccb_done = nvme_empty_done;
1273 ccb->ccb_cookie = &sqe;
1274
1275 memset(&sqe, 0, sizeof(sqe));
1276 sqe.opcode = NVM_ADMIN_DEL_IOSQ;
1277 htolem16(&sqe.qid, q->q_id);
1278
1279 rv = nvme_poll(sc, sc->sc_admin_q, ccb, nvme_sqe_fill, NVME_TIMO_QOP);
1280 if (rv != 0)
1281 goto fail;
1282
1283 ccb->ccb_done = nvme_empty_done;
1284 ccb->ccb_cookie = &sqe;
1285
1286 memset(&sqe, 0, sizeof(sqe));
1287 sqe.opcode = NVM_ADMIN_DEL_IOCQ;
1288 htolem16(&sqe.qid, q->q_id);
1289
1290 rv = nvme_poll(sc, sc->sc_admin_q, ccb, nvme_sqe_fill, NVME_TIMO_QOP);
1291 if (rv != 0)
1292 goto fail;
1293
1294 fail:
1295 nvme_ccb_put(sc->sc_admin_q, ccb);
1296
1297 if (rv == 0 && sc->sc_use_mq) {
1298 if (sc->sc_intr_disestablish(sc, q->q_id))
1299 rv = 1;
1300 }
1301
1302 return rv;
1303 }
1304
1305 static void
1306 nvme_fill_identify(struct nvme_queue *q, struct nvme_ccb *ccb, void *slot)
1307 {
1308 struct nvme_sqe *sqe = slot;
1309 struct nvme_dmamem *mem = ccb->ccb_cookie;
1310
1311 sqe->opcode = NVM_ADMIN_IDENTIFY;
1312 htolem64(&sqe->entry.prp[0], NVME_DMA_DVA(mem));
1313 htolem32(&sqe->cdw10, 1);
1314 }
1315
1316 static int
1317 nvme_get_number_of_queues(struct nvme_softc *sc, u_int *nqap)
1318 {
1319 struct nvme_pt_command pt;
1320 struct nvme_ccb *ccb;
1321 uint16_t ncqa, nsqa;
1322 int rv;
1323
1324 ccb = nvme_ccb_get(sc->sc_admin_q);
1325 KASSERT(ccb != NULL); /* it's a bug if we don't have spare ccb here */
1326
1327 memset(&pt, 0, sizeof(pt));
1328 pt.cmd.opcode = NVM_ADMIN_GET_FEATURES;
1329 pt.cmd.cdw10 = NVM_FEATURE_NUMBER_OF_QUEUES;
1330
1331 ccb->ccb_done = nvme_pt_done;
1332 ccb->ccb_cookie = &pt;
1333
1334 rv = nvme_poll(sc, sc->sc_admin_q, ccb, nvme_pt_fill, NVME_TIMO_QOP);
1335
1336 nvme_ccb_put(sc->sc_admin_q, ccb);
1337
1338 if (rv != 0) {
1339 *nqap = 0;
1340 return EIO;
1341 }
1342
1343 ncqa = pt.cpl.cdw0 >> 16;
1344 nsqa = pt.cpl.cdw0 & 0xffff;
1345 *nqap = MIN(ncqa, nsqa) + 1;
1346
1347 return 0;
1348 }
1349
1350 static int
1351 nvme_ccbs_alloc(struct nvme_queue *q, uint16_t nccbs)
1352 {
1353 struct nvme_softc *sc = q->q_sc;
1354 struct nvme_ccb *ccb;
1355 bus_addr_t off;
1356 uint64_t *prpl;
1357 u_int i;
1358
1359 mutex_init(&q->q_ccb_mtx, MUTEX_DEFAULT, IPL_BIO);
1360 SIMPLEQ_INIT(&q->q_ccb_list);
1361
1362 q->q_ccbs = kmem_alloc(sizeof(*ccb) * nccbs, KM_SLEEP);
1363
1364 q->q_nccbs = nccbs;
1365 q->q_nccbs_avail = nccbs;
1366 q->q_ccb_prpls = nvme_dmamem_alloc(sc,
1367 sizeof(*prpl) * sc->sc_max_sgl * nccbs);
1368
1369 prpl = NVME_DMA_KVA(q->q_ccb_prpls);
1370 off = 0;
1371
1372 for (i = 0; i < nccbs; i++) {
1373 ccb = &q->q_ccbs[i];
1374
1375 if (bus_dmamap_create(sc->sc_dmat, sc->sc_mdts,
1376 sc->sc_max_sgl + 1 /* we get a free prp in the sqe */,
1377 sc->sc_mps, sc->sc_mps, BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW,
1378 &ccb->ccb_dmamap) != 0)
1379 goto free_maps;
1380
1381 ccb->ccb_id = i;
1382 ccb->ccb_prpl = prpl;
1383 ccb->ccb_prpl_off = off;
1384 ccb->ccb_prpl_dva = NVME_DMA_DVA(q->q_ccb_prpls) + off;
1385
1386 SIMPLEQ_INSERT_TAIL(&q->q_ccb_list, ccb, ccb_entry);
1387
1388 prpl += sc->sc_max_sgl;
1389 off += sizeof(*prpl) * sc->sc_max_sgl;
1390 }
1391
1392 return 0;
1393
1394 free_maps:
1395 nvme_ccbs_free(q);
1396 return 1;
1397 }
1398
1399 static struct nvme_ccb *
1400 nvme_ccb_get(struct nvme_queue *q)
1401 {
1402 struct nvme_ccb *ccb = NULL;
1403
1404 mutex_enter(&q->q_ccb_mtx);
1405 if (q->q_nccbs_avail > 0) {
1406 ccb = SIMPLEQ_FIRST(&q->q_ccb_list);
1407 KASSERT(ccb != NULL);
1408 q->q_nccbs_avail--;
1409
1410 SIMPLEQ_REMOVE_HEAD(&q->q_ccb_list, ccb_entry);
1411 #ifdef DEBUG
1412 ccb->ccb_cookie = NULL;
1413 #endif
1414 }
1415 mutex_exit(&q->q_ccb_mtx);
1416
1417 return ccb;
1418 }
1419
1420 static void
1421 nvme_ccb_put(struct nvme_queue *q, struct nvme_ccb *ccb)
1422 {
1423
1424 mutex_enter(&q->q_ccb_mtx);
1425 #ifdef DEBUG
1426 ccb->ccb_cookie = (void *)NVME_CCB_FREE;
1427 #endif
1428 SIMPLEQ_INSERT_HEAD(&q->q_ccb_list, ccb, ccb_entry);
1429 mutex_exit(&q->q_ccb_mtx);
1430 }
1431
1432 static void
1433 nvme_ccbs_free(struct nvme_queue *q)
1434 {
1435 struct nvme_softc *sc = q->q_sc;
1436 struct nvme_ccb *ccb;
1437
1438 mutex_enter(&q->q_ccb_mtx);
1439 while ((ccb = SIMPLEQ_FIRST(&q->q_ccb_list)) != NULL) {
1440 SIMPLEQ_REMOVE_HEAD(&q->q_ccb_list, ccb_entry);
1441 bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
1442 }
1443 mutex_exit(&q->q_ccb_mtx);
1444
1445 nvme_dmamem_free(sc, q->q_ccb_prpls);
1446 kmem_free(q->q_ccbs, sizeof(*ccb) * q->q_nccbs);
1447 q->q_ccbs = NULL;
1448 mutex_destroy(&q->q_ccb_mtx);
1449 }
1450
1451 static struct nvme_queue *
1452 nvme_q_alloc(struct nvme_softc *sc, uint16_t id, u_int entries, u_int dstrd)
1453 {
1454 struct nvme_queue *q;
1455
1456 q = kmem_alloc(sizeof(*q), KM_SLEEP);
1457 q->q_sc = sc;
1458 q->q_sq_dmamem = nvme_dmamem_alloc(sc,
1459 sizeof(struct nvme_sqe) * entries);
1460 if (q->q_sq_dmamem == NULL)
1461 goto free;
1462
1463 q->q_cq_dmamem = nvme_dmamem_alloc(sc,
1464 sizeof(struct nvme_cqe) * entries);
1465 if (q->q_cq_dmamem == NULL)
1466 goto free_sq;
1467
1468 memset(NVME_DMA_KVA(q->q_sq_dmamem), 0, NVME_DMA_LEN(q->q_sq_dmamem));
1469 memset(NVME_DMA_KVA(q->q_cq_dmamem), 0, NVME_DMA_LEN(q->q_cq_dmamem));
1470
1471 mutex_init(&q->q_sq_mtx, MUTEX_DEFAULT, IPL_BIO);
1472 mutex_init(&q->q_cq_mtx, MUTEX_DEFAULT, IPL_BIO);
1473 q->q_sqtdbl = NVME_SQTDBL(id, dstrd);
1474 q->q_cqhdbl = NVME_CQHDBL(id, dstrd);
1475 q->q_id = id;
1476 q->q_entries = entries;
1477 q->q_sq_tail = 0;
1478 q->q_cq_head = 0;
1479 q->q_cq_phase = NVME_CQE_PHASE;
1480
1481 nvme_dmamem_sync(sc, q->q_sq_dmamem, BUS_DMASYNC_PREWRITE);
1482 nvme_dmamem_sync(sc, q->q_cq_dmamem, BUS_DMASYNC_PREREAD);
1483
1484 /*
1485 * Due to definition of full and empty queue (queue is empty
1486 * when head == tail, full when tail is one less then head),
1487 * we can actually only have (entries - 1) in-flight commands.
1488 */
1489 if (nvme_ccbs_alloc(q, entries - 1) != 0) {
1490 aprint_error_dev(sc->sc_dev, "unable to allocate ccbs\n");
1491 goto free_cq;
1492 }
1493
1494 return q;
1495
1496 free_cq:
1497 nvme_dmamem_free(sc, q->q_cq_dmamem);
1498 free_sq:
1499 nvme_dmamem_free(sc, q->q_sq_dmamem);
1500 free:
1501 kmem_free(q, sizeof(*q));
1502
1503 return NULL;
1504 }
1505
1506 static void
1507 nvme_q_free(struct nvme_softc *sc, struct nvme_queue *q)
1508 {
1509 nvme_ccbs_free(q);
1510 mutex_destroy(&q->q_sq_mtx);
1511 mutex_destroy(&q->q_cq_mtx);
1512 nvme_dmamem_sync(sc, q->q_cq_dmamem, BUS_DMASYNC_POSTREAD);
1513 nvme_dmamem_sync(sc, q->q_sq_dmamem, BUS_DMASYNC_POSTWRITE);
1514 nvme_dmamem_free(sc, q->q_cq_dmamem);
1515 nvme_dmamem_free(sc, q->q_sq_dmamem);
1516 kmem_free(q, sizeof(*q));
1517 }
1518
1519 int
1520 nvme_intr(void *xsc)
1521 {
1522 struct nvme_softc *sc = xsc;
1523
1524 /*
1525 * INTx is level triggered, controller deasserts the interrupt only
1526 * when we advance command queue head via write to the doorbell.
1527 * Tell the controller to block the interrupts while we process
1528 * the queue(s).
1529 */
1530 nvme_write4(sc, NVME_INTMS, 1);
1531
1532 softint_schedule(sc->sc_softih[0]);
1533
1534 /* don't know, might not have been for us */
1535 return 1;
1536 }
1537
1538 void
1539 nvme_softintr_intx(void *xq)
1540 {
1541 struct nvme_queue *q = xq;
1542 struct nvme_softc *sc = q->q_sc;
1543
1544 nvme_q_complete(sc, sc->sc_admin_q);
1545 if (sc->sc_q != NULL)
1546 nvme_q_complete(sc, sc->sc_q[0]);
1547
1548 /*
1549 * Processing done, tell controller to issue interrupts again. There
1550 * is no race, as NVMe spec requires the controller to maintain state,
1551 * and assert the interrupt whenever there are unacknowledged
1552 * completion queue entries.
1553 */
1554 nvme_write4(sc, NVME_INTMC, 1);
1555 }
1556
1557 int
1558 nvme_intr_msi(void *xq)
1559 {
1560 struct nvme_queue *q = xq;
1561
1562 KASSERT(q && q->q_sc && q->q_sc->sc_softih
1563 && q->q_sc->sc_softih[q->q_id]);
1564
1565 /*
1566 * MSI/MSI-X are edge triggered, so can handover processing to softint
1567 * without masking the interrupt.
1568 */
1569 softint_schedule(q->q_sc->sc_softih[q->q_id]);
1570
1571 return 1;
1572 }
1573
1574 void
1575 nvme_softintr_msi(void *xq)
1576 {
1577 struct nvme_queue *q = xq;
1578 struct nvme_softc *sc = q->q_sc;
1579
1580 nvme_q_complete(sc, q);
1581 }
1582
1583 static struct nvme_dmamem *
1584 nvme_dmamem_alloc(struct nvme_softc *sc, size_t size)
1585 {
1586 struct nvme_dmamem *ndm;
1587 int nsegs;
1588
1589 ndm = kmem_zalloc(sizeof(*ndm), KM_SLEEP);
1590 if (ndm == NULL)
1591 return NULL;
1592
1593 ndm->ndm_size = size;
1594
1595 if (bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
1596 BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &ndm->ndm_map) != 0)
1597 goto ndmfree;
1598
1599 if (bus_dmamem_alloc(sc->sc_dmat, size, sc->sc_mps, 0, &ndm->ndm_seg,
1600 1, &nsegs, BUS_DMA_WAITOK) != 0)
1601 goto destroy;
1602
1603 if (bus_dmamem_map(sc->sc_dmat, &ndm->ndm_seg, nsegs, size,
1604 &ndm->ndm_kva, BUS_DMA_WAITOK) != 0)
1605 goto free;
1606 memset(ndm->ndm_kva, 0, size);
1607
1608 if (bus_dmamap_load(sc->sc_dmat, ndm->ndm_map, ndm->ndm_kva, size,
1609 NULL, BUS_DMA_WAITOK) != 0)
1610 goto unmap;
1611
1612 return ndm;
1613
1614 unmap:
1615 bus_dmamem_unmap(sc->sc_dmat, ndm->ndm_kva, size);
1616 free:
1617 bus_dmamem_free(sc->sc_dmat, &ndm->ndm_seg, 1);
1618 destroy:
1619 bus_dmamap_destroy(sc->sc_dmat, ndm->ndm_map);
1620 ndmfree:
1621 kmem_free(ndm, sizeof(*ndm));
1622 return NULL;
1623 }
1624
1625 static void
1626 nvme_dmamem_sync(struct nvme_softc *sc, struct nvme_dmamem *mem, int ops)
1627 {
1628 bus_dmamap_sync(sc->sc_dmat, NVME_DMA_MAP(mem),
1629 0, NVME_DMA_LEN(mem), ops);
1630 }
1631
1632 void
1633 nvme_dmamem_free(struct nvme_softc *sc, struct nvme_dmamem *ndm)
1634 {
1635 bus_dmamap_unload(sc->sc_dmat, ndm->ndm_map);
1636 bus_dmamem_unmap(sc->sc_dmat, ndm->ndm_kva, ndm->ndm_size);
1637 bus_dmamem_free(sc->sc_dmat, &ndm->ndm_seg, 1);
1638 bus_dmamap_destroy(sc->sc_dmat, ndm->ndm_map);
1639 kmem_free(ndm, sizeof(*ndm));
1640 }
1641
1642 /*
1643 * ioctl
1644 */
1645
1646 dev_type_open(nvmeopen);
1647 dev_type_close(nvmeclose);
1648 dev_type_ioctl(nvmeioctl);
1649
1650 const struct cdevsw nvme_cdevsw = {
1651 .d_open = nvmeopen,
1652 .d_close = nvmeclose,
1653 .d_read = noread,
1654 .d_write = nowrite,
1655 .d_ioctl = nvmeioctl,
1656 .d_stop = nostop,
1657 .d_tty = notty,
1658 .d_poll = nopoll,
1659 .d_mmap = nommap,
1660 .d_kqfilter = nokqfilter,
1661 .d_discard = nodiscard,
1662 .d_flag = D_OTHER,
1663 };
1664
1665 extern struct cfdriver nvme_cd;
1666
1667 /*
1668 * Accept an open operation on the control device.
1669 */
1670 int
1671 nvmeopen(dev_t dev, int flag, int mode, struct lwp *l)
1672 {
1673 struct nvme_softc *sc;
1674 int unit = minor(dev) / 0x10000;
1675 int nsid = minor(dev) & 0xffff;
1676 int nsidx;
1677
1678 if ((sc = device_lookup_private(&nvme_cd, unit)) == NULL)
1679 return ENXIO;
1680 if ((sc->sc_flags & NVME_F_ATTACHED) == 0)
1681 return ENXIO;
1682
1683 if (nsid == 0) {
1684 /* controller */
1685 if (ISSET(sc->sc_flags, NVME_F_OPEN))
1686 return EBUSY;
1687 SET(sc->sc_flags, NVME_F_OPEN);
1688 } else {
1689 /* namespace */
1690 nsidx = nsid - 1;
1691 if (nsidx >= sc->sc_nn || sc->sc_namespaces[nsidx].dev == NULL)
1692 return ENXIO;
1693 if (ISSET(sc->sc_namespaces[nsidx].flags, NVME_NS_F_OPEN))
1694 return EBUSY;
1695 SET(sc->sc_namespaces[nsidx].flags, NVME_NS_F_OPEN);
1696 }
1697 return 0;
1698 }
1699
1700 /*
1701 * Accept the last close on the control device.
1702 */
1703 int
1704 nvmeclose(dev_t dev, int flag, int mode, struct lwp *l)
1705 {
1706 struct nvme_softc *sc;
1707 int unit = minor(dev) / 0x10000;
1708 int nsid = minor(dev) & 0xffff;
1709 int nsidx;
1710
1711 sc = device_lookup_private(&nvme_cd, unit);
1712 if (sc == NULL)
1713 return ENXIO;
1714
1715 if (nsid == 0) {
1716 /* controller */
1717 CLR(sc->sc_flags, NVME_F_OPEN);
1718 } else {
1719 /* namespace */
1720 nsidx = nsid - 1;
1721 if (nsidx >= sc->sc_nn)
1722 return ENXIO;
1723 CLR(sc->sc_namespaces[nsidx].flags, NVME_NS_F_OPEN);
1724 }
1725
1726 return 0;
1727 }
1728
1729 /*
1730 * Handle control operations.
1731 */
1732 int
1733 nvmeioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1734 {
1735 struct nvme_softc *sc;
1736 int unit = minor(dev) / 0x10000;
1737 int nsid = minor(dev) & 0xffff;
1738 struct nvme_pt_command *pt;
1739
1740 sc = device_lookup_private(&nvme_cd, unit);
1741 if (sc == NULL)
1742 return ENXIO;
1743
1744 switch (cmd) {
1745 case NVME_PASSTHROUGH_CMD:
1746 pt = data;
1747 return nvme_command_passthrough(sc, data,
1748 nsid == 0 ? pt->cmd.nsid : nsid, l, nsid == 0);
1749 }
1750
1751 return ENOTTY;
1752 }
1753