ld_nvme.c revision 1.13 1 /* $NetBSD: ld_nvme.c,v 1.13 2017/02/28 20:53:50 jdolecek 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.13 2017/02/28 20:53:50 jdolecek 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;
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
156 return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
157 bp, bp->b_data, bp->b_bcount,
158 sc->sc_ld.sc_secsize, bp->b_rawblkno,
159 BUF_ISWRITE(bp) ? 0 : NVME_NS_CTX_F_READ,
160 ld_nvme_biodone);
161 }
162
163 static int
164 ld_nvme_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
165 {
166 struct ld_nvme_softc *sc = device_private(ld->sc_dv);
167
168 return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
169 NULL, data, blkcnt * ld->sc_secsize,
170 sc->sc_ld.sc_secsize, blkno,
171 NVME_NS_CTX_F_POLL,
172 ld_nvme_biodone);
173 }
174
175 static void
176 ld_nvme_biodone(void *xc, struct buf *bp, uint16_t cmd_status, uint32_t cdw0)
177 {
178 struct ld_nvme_softc *sc = xc;
179 uint16_t status = NVME_CQE_SC(cmd_status);
180
181 if (bp != NULL) {
182 if (status != NVME_CQE_SC_SUCCESS) {
183 bp->b_error = EIO;
184 bp->b_resid = bp->b_bcount;
185 aprint_error_dev(sc->sc_ld.sc_dv, "I/O error\n");
186 } else {
187 bp->b_resid = 0;
188 }
189 lddone(&sc->sc_ld, bp);
190 } else {
191 if (status != NVME_CQE_SC_SUCCESS) {
192 aprint_error_dev(sc->sc_ld.sc_dv, "I/O error\n");
193 }
194 }
195 }
196
197 static int
198 ld_nvme_flush(struct ld_softc *ld, bool poll)
199 {
200 struct ld_nvme_softc *sc = device_private(ld->sc_dv);
201
202 return nvme_ns_sync(sc->sc_nvme, sc->sc_nsid, sc,
203 poll ? NVME_NS_CTX_F_POLL : 0,
204 ld_nvme_syncdone);
205 }
206
207 static void
208 ld_nvme_syncdone(void *xc, struct buf *bp, uint16_t cmd_status, uint32_t cdw0)
209 {
210 /* nothing to do */
211 }
212
213 static int
214 ld_nvme_getcache(struct ld_softc *ld, int *addr)
215 {
216 int error;
217 struct ld_nvme_softc *sc = device_private(ld->sc_dv);
218
219 *addr = 0;
220
221 if (!nvme_has_volatile_write_cache(sc->sc_nvme)) {
222 /* cache simply not present */
223 return 0;
224 }
225
226 /*
227 * This is admin queue request. The queue is relatively limited in size,
228 * and this is not performance critical call, so have at most one pending
229 * cache request at a time to avoid spurious EWOULDBLOCK failures.
230 */
231 mutex_enter(&sc->sc_getcache_lock);
232 while (sc->sc_getcache_waiting) {
233 error = cv_wait_sig(&sc->sc_getcache_cv, &sc->sc_getcache_lock);
234 if (error)
235 goto out;
236 }
237 sc->sc_getcache_waiting = true;
238 sc->sc_getcache_ready = false;
239 mutex_exit(&sc->sc_getcache_lock);
240
241 error = nvme_admin_getcache(sc->sc_nvme, sc, ld_nvme_getcache_done);
242 if (error) {
243 mutex_enter(&sc->sc_getcache_lock);
244 goto out;
245 }
246
247 mutex_enter(&sc->sc_getcache_lock);
248 while (!sc->sc_getcache_ready) {
249 error = cv_wait_sig(&sc->sc_getcache_ready_cv,
250 &sc->sc_getcache_lock);
251 if (error)
252 goto out;
253 }
254
255 KDASSERT(sc->sc_getcache_ready);
256
257 if (sc->sc_getcache_result >= 0)
258 *addr |= sc->sc_getcache_result;
259 else
260 error = EINVAL;
261
262 out:
263 sc->sc_getcache_waiting = false;
264
265 /* wake one of eventual waiters */
266 cv_signal(&sc->sc_getcache_cv);
267
268 mutex_exit(&sc->sc_getcache_lock);
269
270 return error;
271 }
272
273 static void
274 ld_nvme_getcache_done(void *xc, struct buf *bp, uint16_t cmd_status, uint32_t cdw0)
275 {
276 struct ld_nvme_softc *sc = xc;
277 uint16_t status = NVME_CQE_SC(cmd_status);
278 int result;
279
280 if (status == NVME_CQE_SC_SUCCESS) {
281 result = 0;
282
283 if (cdw0 & NVME_CQE_CDW0_VWC_WCE)
284 result |= DKCACHE_WRITE;
285
286 /*
287 * If volatile write cache is present, the flag shall also be
288 * settable.
289 */
290 result |= DKCACHE_WCHANGE;
291 } else {
292 result = -1;
293 }
294
295 mutex_enter(&sc->sc_getcache_lock);
296 sc->sc_getcache_result = result;
297 sc->sc_getcache_ready = true;
298
299 /* wake up the waiter */
300 cv_signal(&sc->sc_getcache_ready_cv);
301
302 mutex_exit(&sc->sc_getcache_lock);
303 }
304
305 static int
306 ld_nvme_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag, bool poll)
307 {
308 int error;
309
310 switch (cmd) {
311 case DIOCCACHESYNC:
312 error = ld_nvme_flush(ld, poll);
313 break;
314
315 case DIOCGCACHE:
316 error = ld_nvme_getcache(ld, (int *)addr);
317 break;
318
319 default:
320 error = EPASSTHROUGH;
321 break;
322 }
323
324 return error;
325 }
326
327 MODULE(MODULE_CLASS_DRIVER, ld_nvme, "ld,nvme");
328
329 #ifdef _MODULE
330 /*
331 * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
332 * XXX it will be defined in the common-code module
333 */
334 #undef CFDRIVER_DECL
335 #define CFDRIVER_DECL(name, class, attr)
336 #include "ioconf.c"
337 #endif
338
339 static int
340 ld_nvme_modcmd(modcmd_t cmd, void *opaque)
341 {
342 #ifdef _MODULE
343 /*
344 * We ignore the cfdriver_vec[] that ioconf provides, since
345 * the cfdrivers are attached already.
346 */
347 static struct cfdriver * const no_cfdriver_vec[] = { NULL };
348 #endif
349 int error = 0;
350
351 #ifdef _MODULE
352 switch (cmd) {
353 case MODULE_CMD_INIT:
354 error = config_init_component(no_cfdriver_vec,
355 cfattach_ioconf_ld_nvme, cfdata_ioconf_ld_nvme);
356 break;
357 case MODULE_CMD_FINI:
358 error = config_fini_component(no_cfdriver_vec,
359 cfattach_ioconf_ld_nvme, cfdata_ioconf_ld_nvme);
360 break;
361 default:
362 error = ENOTTY;
363 break;
364 }
365 #endif
366
367 return error;
368 }
369