ld_nvme.c revision 1.4 1 /* $NetBSD: ld_nvme.c,v 1.4 2016/09/18 21:19:39 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.4 2016/09/18 21:19:39 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
40 #include <dev/ldvar.h>
41 #include <dev/ic/nvmereg.h>
42 #include <dev/ic/nvmevar.h>
43
44 struct ld_nvme_softc {
45 struct ld_softc sc_ld;
46 struct nvme_softc *sc_nvme;
47
48 uint16_t sc_nsid;
49 };
50
51 static int ld_nvme_match(device_t, cfdata_t, void *);
52 static void ld_nvme_attach(device_t, device_t, void *);
53 static int ld_nvme_detach(device_t, int);
54
55 CFATTACH_DECL_NEW(ld_nvme, sizeof(struct ld_nvme_softc),
56 ld_nvme_match, ld_nvme_attach, ld_nvme_detach, NULL);
57
58 static int ld_nvme_start(struct ld_softc *, struct buf *);
59 static int ld_nvme_dump(struct ld_softc *, void *, int, int);
60 static int ld_nvme_flush(struct ld_softc *, int);
61
62 static int ld_nvme_dobio(struct ld_nvme_softc *, void *, int, daddr_t,
63 int, struct buf *);
64 static void ld_nvme_biodone(struct nvme_ns_context *);
65 static void ld_nvme_syncdone(struct nvme_ns_context *);
66
67
68 static int
69 ld_nvme_match(device_t parent, cfdata_t match, void *aux)
70 {
71 struct nvme_attach_args *naa = aux;
72
73 if (naa->naa_nsid == 0)
74 return 0;
75
76 return 1;
77 }
78
79 static void
80 ld_nvme_attach(device_t parent, device_t self, void *aux)
81 {
82 struct ld_nvme_softc *sc = device_private(self);
83 struct ld_softc *ld = &sc->sc_ld;
84 struct nvme_softc *nsc = device_private(parent);
85 struct nvme_attach_args *naa = aux;
86 struct nvme_namespace *ns;
87 struct nvm_namespace_format *f;
88 uint64_t nsze;
89 int error;
90
91 ld->sc_dv = self;
92 sc->sc_nvme = nsc;
93 sc->sc_nsid = naa->naa_nsid;
94
95 aprint_naive("\n");
96 aprint_normal("\n");
97
98 error = nvme_ns_identify(sc->sc_nvme, sc->sc_nsid);
99 if (error) {
100 aprint_error_dev(self, "couldn't identify namespace\n");
101 return;
102 }
103
104 ns = nvme_ns_get(sc->sc_nvme, sc->sc_nsid);
105 KASSERT(ns);
106 nsze = lemtoh64(&ns->ident->nsze);
107 f = &ns->ident->lbaf[NVME_ID_NS_FLBAS(ns->ident->flbas)];
108
109 ld->sc_secsize = 1 << f->lbads;
110 ld->sc_secperunit = nsze;
111 ld->sc_maxxfer = MAXPHYS; /* XXX set according to device */
112 ld->sc_maxqueuecnt = naa->naa_qentries; /* XXX set to max allowed by device, not current config */
113 ld->sc_start = ld_nvme_start;
114 ld->sc_dump = ld_nvme_dump;
115 ld->sc_flush = ld_nvme_flush;
116 ld->sc_flags = LDF_ENABLED;
117 ldattach(ld, "fcfs");
118 }
119
120 static int
121 ld_nvme_detach(device_t self, int flags)
122 {
123 struct ld_nvme_softc *sc = device_private(self);
124 struct ld_softc *ld = &sc->sc_ld;
125 int rv;
126
127 if ((rv = ldbegindetach(ld, flags)) != 0)
128 return rv;
129 ldenddetach(ld);
130
131 nvme_ns_free(sc->sc_nvme, sc->sc_nsid);
132
133 return 0;
134 }
135
136 static int
137 ld_nvme_start(struct ld_softc *ld, struct buf *bp)
138 {
139 struct ld_nvme_softc *sc = device_private(ld->sc_dv);
140
141 return ld_nvme_dobio(sc, bp->b_data, bp->b_bcount, bp->b_rawblkno,
142 BUF_ISWRITE(bp), bp);
143 }
144
145 static int
146 ld_nvme_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
147 {
148 struct ld_nvme_softc *sc = device_private(ld->sc_dv);
149
150 return ld_nvme_dobio(sc, data, blkcnt * ld->sc_secsize, blkno, 1, NULL);
151 }
152
153 static int
154 ld_nvme_dobio(struct ld_nvme_softc *sc, void *data, int datasize, daddr_t blkno,
155 int dowrite, struct buf *bp)
156 {
157 struct nvme_ns_context *ctx;
158 int error;
159 int waitok = (bp != NULL && !cpu_softintr_p() && !cpu_intr_p());
160
161 ctx = nvme_ns_get_ctx(sc, waitok ? PR_WAITOK : PR_NOWAIT);
162 if (ctx == NULL)
163 return EAGAIN;
164
165 ctx->nnc_cookie = sc;
166 ctx->nnc_nsid = sc->sc_nsid;
167 ctx->nnc_done = ld_nvme_biodone;
168 ctx->nnc_buf = bp;
169 ctx->nnc_data = data;
170 ctx->nnc_datasize = datasize;
171 ctx->nnc_secsize = sc->sc_ld.sc_secsize;
172 ctx->nnc_blkno = blkno;
173 ctx->nnc_flags = dowrite ? 0 : NVME_NS_CTX_F_READ;
174 if (bp == NULL)
175 SET(ctx->nnc_flags, NVME_NS_CTX_F_POLL);
176
177 error = nvme_ns_dobio(sc->sc_nvme, ctx);
178 if (error)
179 nvme_ns_put_ctx(sc, ctx);
180
181 return error;
182 }
183
184 static void
185 ld_nvme_biodone(struct nvme_ns_context *ctx)
186 {
187 struct ld_nvme_softc *sc = ctx->nnc_cookie;
188 struct buf *bp = ctx->nnc_buf;
189 int status = NVME_CQE_SC(ctx->nnc_status);
190
191 /* free before processing to avoid starvation, lddone() could trigger
192 * another i/o request */
193 nvme_ns_put_ctx(sc, ctx);
194
195 if (bp != NULL) {
196 if (status != NVME_CQE_SC_SUCCESS) {
197 bp->b_error = EIO;
198 bp->b_resid = bp->b_bcount;
199 aprint_error_dev(sc->sc_ld.sc_dv, "I/O error\n");
200 } else {
201 bp->b_resid = 0;
202 }
203 lddone(&sc->sc_ld, bp);
204 } else {
205 if (status != NVME_CQE_SC_SUCCESS) {
206 aprint_error_dev(sc->sc_ld.sc_dv, "I/O error\n");
207 }
208 }
209 }
210
211 static int
212 ld_nvme_flush(struct ld_softc *ld, int flags)
213 {
214 struct ld_nvme_softc *sc = device_private(ld->sc_dv);
215 struct nvme_ns_context *ctx;
216 int error;
217 int waitok = (!ISSET(flags, LDFL_POLL)
218 && !cpu_softintr_p() && !cpu_intr_p());
219
220 ctx = nvme_ns_get_ctx(sc, waitok ? PR_WAITOK : PR_NOWAIT);
221 if (ctx == NULL)
222 return EAGAIN;
223
224 ctx->nnc_cookie = sc;
225 ctx->nnc_nsid = sc->sc_nsid;
226 ctx->nnc_done = ld_nvme_syncdone;
227 ctx->nnc_flags = 0;
228 if (flags & LDFL_POLL)
229 SET(ctx->nnc_flags, NVME_NS_CTX_F_POLL);
230
231 error = nvme_ns_sync(sc->sc_nvme, ctx);
232 if (error)
233 nvme_ns_put_ctx(sc, ctx);
234
235 return error;
236 }
237
238 static void
239 ld_nvme_syncdone(struct nvme_ns_context *ctx)
240 {
241 struct ld_nvme_softc *sc = ctx->nnc_cookie;
242
243 nvme_ns_put_ctx(sc, ctx);
244 }
245