ld_nvme.c revision 1.17 1 /* $NetBSD: ld_nvme.c,v 1.17 2017/08/09 16:44:40 mlelstv Exp $ */
2
3 /*-
4 * Copyright (C) 2016 NONAKA Kimihiro <nonaka (at) netbsd.org>
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 ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: ld_nvme.c,v 1.17 2017/08/09 16:44:40 mlelstv Exp $");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/device.h>
35 #include <sys/buf.h>
36 #include <sys/bufq.h>
37 #include <sys/disk.h>
38 #include <sys/kmem.h>
39 #include <sys/module.h>
40
41 #include <dev/ldvar.h>
42 #include <dev/ic/nvmereg.h>
43 #include <dev/ic/nvmevar.h>
44
45 #include "ioconf.h"
46
47 struct ld_nvme_softc {
48 struct ld_softc sc_ld;
49 struct nvme_softc *sc_nvme;
50
51 uint16_t sc_nsid;
52
53 /* getcache handling */
54 kmutex_t sc_getcache_lock;
55 kcondvar_t sc_getcache_cv;
56 kcondvar_t sc_getcache_ready_cv;
57 bool sc_getcache_waiting;
58 bool sc_getcache_ready;
59 int sc_getcache_result;
60 };
61
62 static int ld_nvme_match(device_t, cfdata_t, void *);
63 static void ld_nvme_attach(device_t, device_t, void *);
64 static int ld_nvme_detach(device_t, int);
65
66 CFATTACH_DECL_NEW(ld_nvme, sizeof(struct ld_nvme_softc),
67 ld_nvme_match, ld_nvme_attach, ld_nvme_detach, NULL);
68
69 static int ld_nvme_start(struct ld_softc *, struct buf *);
70 static int ld_nvme_dump(struct ld_softc *, void *, int, int);
71 static int ld_nvme_flush(struct ld_softc *, bool);
72 static int ld_nvme_getcache(struct ld_softc *, int *);
73 static int ld_nvme_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
74
75 static void ld_nvme_biodone(void *, struct buf *, uint16_t, uint32_t);
76 static void ld_nvme_syncdone(void *, struct buf *, uint16_t, uint32_t);
77 static void ld_nvme_getcache_done(void *, struct buf *, uint16_t, uint32_t);
78
79 static int
80 ld_nvme_match(device_t parent, cfdata_t match, void *aux)
81 {
82 struct nvme_attach_args *naa = aux;
83
84 if (naa->naa_nsid == 0)
85 return 0;
86
87 return 1;
88 }
89
90 static void
91 ld_nvme_attach(device_t parent, device_t self, void *aux)
92 {
93 struct ld_nvme_softc *sc = device_private(self);
94 struct ld_softc *ld = &sc->sc_ld;
95 struct nvme_softc *nsc = device_private(parent);
96 struct nvme_attach_args *naa = aux;
97 struct nvme_namespace *ns;
98 struct nvm_namespace_format *f;
99 uint64_t nsze;
100 int error;
101
102 ld->sc_dv = self;
103 sc->sc_nvme = nsc;
104 sc->sc_nsid = naa->naa_nsid;
105
106 mutex_init(&sc->sc_getcache_lock, MUTEX_DEFAULT, IPL_SOFTBIO);
107 cv_init(&sc->sc_getcache_cv, "nvmegcq");
108 cv_init(&sc->sc_getcache_ready_cv, "nvmegcr");
109
110 aprint_naive("\n");
111 aprint_normal("\n");
112
113 error = nvme_ns_identify(sc->sc_nvme, sc->sc_nsid);
114 if (error) {
115 aprint_error_dev(self, "couldn't identify namespace\n");
116 return;
117 }
118
119 ns = nvme_ns_get(sc->sc_nvme, sc->sc_nsid);
120 KASSERT(ns);
121 nsze = lemtoh64(&ns->ident->nsze);
122 f = &ns->ident->lbaf[NVME_ID_NS_FLBAS(ns->ident->flbas)];
123
124 ld->sc_secsize = 1 << f->lbads;
125 ld->sc_secperunit = nsze;
126 ld->sc_maxxfer = naa->naa_maxphys;
127 ld->sc_maxqueuecnt = naa->naa_qentries;
128 ld->sc_start = ld_nvme_start;
129 ld->sc_dump = ld_nvme_dump;
130 ld->sc_ioctl = ld_nvme_ioctl;
131 ld->sc_flags = LDF_ENABLED | LDF_NO_RND | LDF_MPSAFE;
132 ldattach(ld, "fcfs");
133 }
134
135 static int
136 ld_nvme_detach(device_t self, int flags)
137 {
138 struct ld_nvme_softc *sc = device_private(self);
139 struct ld_softc *ld = &sc->sc_ld;
140 int rv;
141
142 if ((rv = ldbegindetach(ld, flags)) != 0)
143 return rv;
144 ldenddetach(ld);
145
146 nvme_ns_free(sc->sc_nvme, sc->sc_nsid);
147
148 return 0;
149 }
150
151 static int
152 ld_nvme_start(struct ld_softc *ld, struct buf *bp)
153 {
154 struct ld_nvme_softc *sc = device_private(ld->sc_dv);
155 int flags = BUF_ISWRITE(bp) ? 0 : NVME_NS_CTX_F_READ;
156
157 if (bp->b_flags & B_MEDIA_FUA)
158 flags |= NVME_NS_CTX_F_FUA;
159
160 return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
161 bp, bp->b_data, bp->b_bcount,
162 sc->sc_ld.sc_secsize, bp->b_rawblkno,
163 flags,
164 ld_nvme_biodone);
165 }
166
167 static int
168 ld_nvme_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
169 {
170 struct ld_nvme_softc *sc = device_private(ld->sc_dv);
171
172 return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
173 NULL, data, blkcnt * ld->sc_secsize,
174 sc->sc_ld.sc_secsize, blkno,
175 NVME_NS_CTX_F_POLL,
176 ld_nvme_biodone);
177 }
178
179 static void
180 ld_nvme_biodone(void *xc, struct buf *bp, uint16_t cmd_status, uint32_t cdw0)
181 {
182 struct ld_nvme_softc *sc = xc;
183 uint16_t status = NVME_CQE_SC(cmd_status);
184
185 if (bp != NULL) {
186 if (status != NVME_CQE_SC_SUCCESS) {
187 bp->b_error = EIO;
188 bp->b_resid = bp->b_bcount;
189 aprint_error_dev(sc->sc_ld.sc_dv, "I/O error\n");
190 } else {
191 bp->b_resid = 0;
192 }
193 lddone(&sc->sc_ld, bp);
194 } else {
195 if (status != NVME_CQE_SC_SUCCESS) {
196 aprint_error_dev(sc->sc_ld.sc_dv, "I/O error\n");
197 }
198 }
199 }
200
201 static int
202 ld_nvme_flush(struct ld_softc *ld, bool poll)
203 {
204 struct ld_nvme_softc *sc = device_private(ld->sc_dv);
205
206 if (!nvme_has_volatile_write_cache(sc->sc_nvme)) {
207 /* cache not present, no value in trying to flush it */
208 return 0;
209 }
210
211 return nvme_ns_sync(sc->sc_nvme, sc->sc_nsid, sc,
212 poll ? NVME_NS_CTX_F_POLL : 0,
213 ld_nvme_syncdone);
214 }
215
216 static void
217 ld_nvme_syncdone(void *xc, struct buf *bp, uint16_t cmd_status, uint32_t cdw0)
218 {
219 /* nothing to do */
220 }
221
222 static int
223 ld_nvme_getcache(struct ld_softc *ld, int *addr)
224 {
225 int error;
226 struct ld_nvme_softc *sc = device_private(ld->sc_dv);
227
228 /*
229 * DPO not supported, Dataset Management (DSM) field doesn't specify
230 * the same semantics.
231 */
232 *addr = DKCACHE_FUA;
233
234 if (!nvme_has_volatile_write_cache(sc->sc_nvme)) {
235 /* cache simply not present */
236 return 0;
237 }
238
239 /*
240 * This is admin queue request. The queue is relatively limited in size,
241 * and this is not performance critical call, so have at most one pending
242 * cache request at a time to avoid spurious EWOULDBLOCK failures.
243 */
244 mutex_enter(&sc->sc_getcache_lock);
245 while (sc->sc_getcache_waiting) {
246 error = cv_wait_sig(&sc->sc_getcache_cv, &sc->sc_getcache_lock);
247 if (error)
248 goto out;
249 }
250 sc->sc_getcache_waiting = true;
251 sc->sc_getcache_ready = false;
252 mutex_exit(&sc->sc_getcache_lock);
253
254 error = nvme_admin_getcache(sc->sc_nvme, sc, ld_nvme_getcache_done);
255 if (error) {
256 mutex_enter(&sc->sc_getcache_lock);
257 goto out;
258 }
259
260 mutex_enter(&sc->sc_getcache_lock);
261 while (!sc->sc_getcache_ready) {
262 error = cv_wait_sig(&sc->sc_getcache_ready_cv,
263 &sc->sc_getcache_lock);
264 if (error)
265 goto out;
266 }
267
268 KDASSERT(sc->sc_getcache_ready);
269
270 if (sc->sc_getcache_result >= 0)
271 *addr |= sc->sc_getcache_result;
272 else
273 error = EINVAL;
274
275 out:
276 sc->sc_getcache_waiting = false;
277
278 /* wake one of eventual waiters */
279 cv_signal(&sc->sc_getcache_cv);
280
281 mutex_exit(&sc->sc_getcache_lock);
282
283 return error;
284 }
285
286 static void
287 ld_nvme_getcache_done(void *xc, struct buf *bp, uint16_t cmd_status, uint32_t cdw0)
288 {
289 struct ld_nvme_softc *sc = xc;
290 uint16_t status = NVME_CQE_SC(cmd_status);
291 int result;
292
293 if (status == NVME_CQE_SC_SUCCESS) {
294 result = 0;
295
296 if (cdw0 & NVME_CQE_CDW0_VWC_WCE)
297 result |= DKCACHE_WRITE;
298
299 /*
300 * If volatile write cache is present, the flag shall also be
301 * settable.
302 */
303 result |= DKCACHE_WCHANGE;
304 } else {
305 result = -1;
306 }
307
308 mutex_enter(&sc->sc_getcache_lock);
309 sc->sc_getcache_result = result;
310 sc->sc_getcache_ready = true;
311
312 /* wake up the waiter */
313 cv_signal(&sc->sc_getcache_ready_cv);
314
315 mutex_exit(&sc->sc_getcache_lock);
316 }
317
318 static int
319 ld_nvme_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag, bool poll)
320 {
321 int error;
322
323 switch (cmd) {
324 case DIOCCACHESYNC:
325 error = ld_nvme_flush(ld, poll);
326 break;
327
328 case DIOCGCACHE:
329 error = ld_nvme_getcache(ld, (int *)addr);
330 break;
331
332 default:
333 error = EPASSTHROUGH;
334 break;
335 }
336
337 return error;
338 }
339
340 MODULE(MODULE_CLASS_DRIVER, ld_nvme, "ld,nvme");
341
342 #ifdef _MODULE
343 /*
344 * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
345 * XXX it will be defined in the common-code module
346 */
347 #undef CFDRIVER_DECL
348 #define CFDRIVER_DECL(name, class, attr)
349 #include "ioconf.c"
350 #endif
351
352 static int
353 ld_nvme_modcmd(modcmd_t cmd, void *opaque)
354 {
355 #ifdef _MODULE
356 /*
357 * We ignore the cfdriver_vec[] that ioconf provides, since
358 * the cfdrivers are attached already.
359 */
360 static struct cfdriver * const no_cfdriver_vec[] = { NULL };
361 #endif
362 int error = 0;
363
364 #ifdef _MODULE
365 switch (cmd) {
366 case MODULE_CMD_INIT:
367 error = config_init_component(no_cfdriver_vec,
368 cfattach_ioconf_ld_nvme, cfdata_ioconf_ld_nvme);
369 break;
370 case MODULE_CMD_FINI:
371 error = config_fini_component(no_cfdriver_vec,
372 cfattach_ioconf_ld_nvme, cfdata_ioconf_ld_nvme);
373 break;
374 default:
375 error = ENOTTY;
376 break;
377 }
378 #endif
379
380 return error;
381 }
382