ld_nvme.c revision 1.10 1 1.10 jdolecek /* $NetBSD: ld_nvme.c,v 1.10 2016/11/01 14:39:38 jdolecek Exp $ */
2 1.1 nonaka
3 1.1 nonaka /*-
4 1.1 nonaka * Copyright (C) 2016 NONAKA Kimihiro <nonaka (at) netbsd.org>
5 1.1 nonaka * All rights reserved.
6 1.1 nonaka *
7 1.1 nonaka * Redistribution and use in source and binary forms, with or without
8 1.1 nonaka * modification, are permitted provided that the following conditions
9 1.1 nonaka * are met:
10 1.1 nonaka * 1. Redistributions of source code must retain the above copyright
11 1.1 nonaka * notice, this list of conditions and the following disclaimer.
12 1.1 nonaka * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 nonaka * notice, this list of conditions and the following disclaimer in the
14 1.1 nonaka * documentation and/or other materials provided with the distribution.
15 1.1 nonaka *
16 1.1 nonaka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 nonaka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 nonaka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 nonaka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 nonaka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 nonaka * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 nonaka * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 nonaka * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 nonaka * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 nonaka * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 nonaka */
27 1.1 nonaka
28 1.1 nonaka #include <sys/cdefs.h>
29 1.10 jdolecek __KERNEL_RCSID(0, "$NetBSD: ld_nvme.c,v 1.10 2016/11/01 14:39:38 jdolecek Exp $");
30 1.1 nonaka
31 1.1 nonaka #include <sys/param.h>
32 1.1 nonaka #include <sys/systm.h>
33 1.1 nonaka #include <sys/kernel.h>
34 1.1 nonaka #include <sys/device.h>
35 1.1 nonaka #include <sys/buf.h>
36 1.2 jdolecek #include <sys/bufq.h>
37 1.1 nonaka #include <sys/disk.h>
38 1.1 nonaka #include <sys/kmem.h>
39 1.8 pgoyette #include <sys/module.h>
40 1.1 nonaka
41 1.1 nonaka #include <dev/ldvar.h>
42 1.1 nonaka #include <dev/ic/nvmereg.h>
43 1.1 nonaka #include <dev/ic/nvmevar.h>
44 1.1 nonaka
45 1.8 pgoyette #include "ioconf.h"
46 1.8 pgoyette
47 1.1 nonaka struct ld_nvme_softc {
48 1.1 nonaka struct ld_softc sc_ld;
49 1.1 nonaka struct nvme_softc *sc_nvme;
50 1.1 nonaka
51 1.1 nonaka uint16_t sc_nsid;
52 1.1 nonaka };
53 1.1 nonaka
54 1.1 nonaka static int ld_nvme_match(device_t, cfdata_t, void *);
55 1.1 nonaka static void ld_nvme_attach(device_t, device_t, void *);
56 1.1 nonaka static int ld_nvme_detach(device_t, int);
57 1.1 nonaka
58 1.1 nonaka CFATTACH_DECL_NEW(ld_nvme, sizeof(struct ld_nvme_softc),
59 1.1 nonaka ld_nvme_match, ld_nvme_attach, ld_nvme_detach, NULL);
60 1.1 nonaka
61 1.1 nonaka static int ld_nvme_start(struct ld_softc *, struct buf *);
62 1.1 nonaka static int ld_nvme_dump(struct ld_softc *, void *, int, int);
63 1.1 nonaka static int ld_nvme_flush(struct ld_softc *, int);
64 1.1 nonaka
65 1.6 jdolecek static void ld_nvme_biodone(void *, struct buf *, uint16_t);
66 1.6 jdolecek static void ld_nvme_syncdone(void *, struct buf *, uint16_t);
67 1.1 nonaka
68 1.1 nonaka static int
69 1.1 nonaka ld_nvme_match(device_t parent, cfdata_t match, void *aux)
70 1.1 nonaka {
71 1.1 nonaka struct nvme_attach_args *naa = aux;
72 1.1 nonaka
73 1.1 nonaka if (naa->naa_nsid == 0)
74 1.1 nonaka return 0;
75 1.1 nonaka
76 1.1 nonaka return 1;
77 1.1 nonaka }
78 1.1 nonaka
79 1.1 nonaka static void
80 1.1 nonaka ld_nvme_attach(device_t parent, device_t self, void *aux)
81 1.1 nonaka {
82 1.1 nonaka struct ld_nvme_softc *sc = device_private(self);
83 1.1 nonaka struct ld_softc *ld = &sc->sc_ld;
84 1.1 nonaka struct nvme_softc *nsc = device_private(parent);
85 1.1 nonaka struct nvme_attach_args *naa = aux;
86 1.1 nonaka struct nvme_namespace *ns;
87 1.1 nonaka struct nvm_namespace_format *f;
88 1.1 nonaka uint64_t nsze;
89 1.1 nonaka int error;
90 1.1 nonaka
91 1.1 nonaka ld->sc_dv = self;
92 1.1 nonaka sc->sc_nvme = nsc;
93 1.1 nonaka sc->sc_nsid = naa->naa_nsid;
94 1.1 nonaka
95 1.1 nonaka aprint_naive("\n");
96 1.1 nonaka aprint_normal("\n");
97 1.1 nonaka
98 1.1 nonaka error = nvme_ns_identify(sc->sc_nvme, sc->sc_nsid);
99 1.1 nonaka if (error) {
100 1.1 nonaka aprint_error_dev(self, "couldn't identify namespace\n");
101 1.1 nonaka return;
102 1.1 nonaka }
103 1.1 nonaka
104 1.1 nonaka ns = nvme_ns_get(sc->sc_nvme, sc->sc_nsid);
105 1.1 nonaka KASSERT(ns);
106 1.1 nonaka nsze = lemtoh64(&ns->ident->nsze);
107 1.1 nonaka f = &ns->ident->lbaf[NVME_ID_NS_FLBAS(ns->ident->flbas)];
108 1.1 nonaka
109 1.1 nonaka ld->sc_secsize = 1 << f->lbads;
110 1.1 nonaka ld->sc_secperunit = nsze;
111 1.10 jdolecek ld->sc_maxxfer = naa->naa_maxphys;
112 1.10 jdolecek ld->sc_maxqueuecnt = naa->naa_qentries;
113 1.1 nonaka ld->sc_start = ld_nvme_start;
114 1.1 nonaka ld->sc_dump = ld_nvme_dump;
115 1.1 nonaka ld->sc_flush = ld_nvme_flush;
116 1.1 nonaka ld->sc_flags = LDF_ENABLED;
117 1.3 jdolecek ldattach(ld, "fcfs");
118 1.1 nonaka }
119 1.1 nonaka
120 1.1 nonaka static int
121 1.1 nonaka ld_nvme_detach(device_t self, int flags)
122 1.1 nonaka {
123 1.1 nonaka struct ld_nvme_softc *sc = device_private(self);
124 1.1 nonaka struct ld_softc *ld = &sc->sc_ld;
125 1.1 nonaka int rv;
126 1.1 nonaka
127 1.1 nonaka if ((rv = ldbegindetach(ld, flags)) != 0)
128 1.1 nonaka return rv;
129 1.1 nonaka ldenddetach(ld);
130 1.1 nonaka
131 1.1 nonaka nvme_ns_free(sc->sc_nvme, sc->sc_nsid);
132 1.1 nonaka
133 1.1 nonaka return 0;
134 1.1 nonaka }
135 1.1 nonaka
136 1.1 nonaka static int
137 1.1 nonaka ld_nvme_start(struct ld_softc *ld, struct buf *bp)
138 1.1 nonaka {
139 1.1 nonaka struct ld_nvme_softc *sc = device_private(ld->sc_dv);
140 1.1 nonaka
141 1.6 jdolecek return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
142 1.6 jdolecek bp, bp->b_data, bp->b_bcount,
143 1.6 jdolecek sc->sc_ld.sc_secsize, bp->b_rawblkno,
144 1.6 jdolecek BUF_ISWRITE(bp) ? 0 : NVME_NS_CTX_F_READ,
145 1.6 jdolecek ld_nvme_biodone);
146 1.1 nonaka }
147 1.1 nonaka
148 1.1 nonaka static int
149 1.1 nonaka ld_nvme_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
150 1.1 nonaka {
151 1.1 nonaka struct ld_nvme_softc *sc = device_private(ld->sc_dv);
152 1.1 nonaka
153 1.6 jdolecek return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
154 1.6 jdolecek NULL, data, blkcnt * ld->sc_secsize,
155 1.6 jdolecek sc->sc_ld.sc_secsize, blkno,
156 1.6 jdolecek NVME_NS_CTX_F_POLL,
157 1.6 jdolecek ld_nvme_biodone);
158 1.1 nonaka }
159 1.1 nonaka
160 1.1 nonaka static void
161 1.6 jdolecek ld_nvme_biodone(void *xc, struct buf *bp, uint16_t cmd_status)
162 1.1 nonaka {
163 1.6 jdolecek struct ld_nvme_softc *sc = xc;
164 1.6 jdolecek uint16_t status = NVME_CQE_SC(cmd_status);
165 1.4 jdolecek
166 1.1 nonaka if (bp != NULL) {
167 1.1 nonaka if (status != NVME_CQE_SC_SUCCESS) {
168 1.1 nonaka bp->b_error = EIO;
169 1.1 nonaka bp->b_resid = bp->b_bcount;
170 1.1 nonaka aprint_error_dev(sc->sc_ld.sc_dv, "I/O error\n");
171 1.1 nonaka } else {
172 1.1 nonaka bp->b_resid = 0;
173 1.1 nonaka }
174 1.1 nonaka lddone(&sc->sc_ld, bp);
175 1.1 nonaka } else {
176 1.1 nonaka if (status != NVME_CQE_SC_SUCCESS) {
177 1.1 nonaka aprint_error_dev(sc->sc_ld.sc_dv, "I/O error\n");
178 1.1 nonaka }
179 1.1 nonaka }
180 1.1 nonaka }
181 1.1 nonaka
182 1.1 nonaka static int
183 1.1 nonaka ld_nvme_flush(struct ld_softc *ld, int flags)
184 1.1 nonaka {
185 1.1 nonaka struct ld_nvme_softc *sc = device_private(ld->sc_dv);
186 1.1 nonaka
187 1.7 jdolecek /* wait for the sync to finish */
188 1.6 jdolecek return nvme_ns_sync(sc->sc_nvme, sc->sc_nsid, sc,
189 1.9 jdolecek (flags & LDFL_POLL) ? NVME_NS_CTX_F_POLL : 0,
190 1.9 jdolecek ld_nvme_syncdone);
191 1.1 nonaka }
192 1.1 nonaka
193 1.1 nonaka static void
194 1.6 jdolecek ld_nvme_syncdone(void *xc, struct buf *bp, uint16_t cmd_status)
195 1.1 nonaka {
196 1.6 jdolecek /* nothing to do */
197 1.1 nonaka }
198 1.8 pgoyette
199 1.8 pgoyette MODULE(MODULE_CLASS_DRIVER, ld_nvme, "ld,nvme");
200 1.8 pgoyette
201 1.8 pgoyette #ifdef _MODULE
202 1.8 pgoyette /*
203 1.8 pgoyette * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
204 1.8 pgoyette * XXX it will be defined in the common-code module
205 1.8 pgoyette */
206 1.8 pgoyette #undef CFDRIVER_DECL
207 1.8 pgoyette #define CFDRIVER_DECL(name, class, attr)
208 1.8 pgoyette #include "ioconf.c"
209 1.8 pgoyette #endif
210 1.8 pgoyette
211 1.8 pgoyette static int
212 1.8 pgoyette ld_nvme_modcmd(modcmd_t cmd, void *opaque)
213 1.8 pgoyette {
214 1.8 pgoyette #ifdef _MODULE
215 1.8 pgoyette /*
216 1.8 pgoyette * We ignore the cfdriver_vec[] that ioconf provides, since
217 1.8 pgoyette * the cfdrivers are attached already.
218 1.8 pgoyette */
219 1.8 pgoyette static struct cfdriver * const no_cfdriver_vec[] = { NULL };
220 1.8 pgoyette #endif
221 1.8 pgoyette int error = 0;
222 1.8 pgoyette
223 1.8 pgoyette #ifdef _MODULE
224 1.8 pgoyette switch (cmd) {
225 1.8 pgoyette case MODULE_CMD_INIT:
226 1.8 pgoyette error = config_init_component(no_cfdriver_vec,
227 1.8 pgoyette cfattach_ioconf_ld_nvme, cfdata_ioconf_ld_nvme);
228 1.8 pgoyette break;
229 1.8 pgoyette case MODULE_CMD_FINI:
230 1.8 pgoyette error = config_fini_component(no_cfdriver_vec,
231 1.8 pgoyette cfattach_ioconf_ld_nvme, cfdata_ioconf_ld_nvme);
232 1.8 pgoyette break;
233 1.8 pgoyette default:
234 1.8 pgoyette error = ENOTTY;
235 1.8 pgoyette break;
236 1.8 pgoyette }
237 1.8 pgoyette #endif
238 1.8 pgoyette
239 1.8 pgoyette return error;
240 1.8 pgoyette }
241