xref: /netbsd/sys/dev/ic/ld_nvme.c (revision 66bf8f3f)
1 /*	$NetBSD: ld_nvme.c,v 1.25 2022/07/30 12:48:17 mlelstv Exp $	*/
2 
3 /*-
4  * Copyright (C) 2016 NONAKA Kimihiro <nonaka@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.25 2022/07/30 12:48:17 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 
54 static int	ld_nvme_match(device_t, cfdata_t, void *);
55 static void	ld_nvme_attach(device_t, device_t, void *);
56 static int	ld_nvme_detach(device_t, int);
57 
58 CFATTACH_DECL_NEW(ld_nvme, sizeof(struct ld_nvme_softc),
59     ld_nvme_match, ld_nvme_attach, ld_nvme_detach, NULL);
60 
61 static int	ld_nvme_start(struct ld_softc *, struct buf *);
62 static int	ld_nvme_dump(struct ld_softc *, void *, int, int);
63 static int	ld_nvme_flush(struct ld_softc *, bool);
64 static int	ld_nvme_getcache(struct ld_softc *, int *);
65 static int	ld_nvme_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
66 
67 static void	ld_nvme_biodone(void *, struct buf *, uint16_t, uint32_t);
68 
69 static int
ld_nvme_match(device_t parent,cfdata_t match,void * aux)70 ld_nvme_match(device_t parent, cfdata_t match, void *aux)
71 {
72 	struct nvme_attach_args *naa = aux;
73 
74 	if (naa->naa_nsid == 0)
75 		return 0;
76 
77 	return 1;
78 }
79 
80 static void
ld_nvme_attach(device_t parent,device_t self,void * aux)81 ld_nvme_attach(device_t parent, device_t self, void *aux)
82 {
83 	struct ld_nvme_softc *sc = device_private(self);
84 	struct ld_softc *ld = &sc->sc_ld;
85 	struct nvme_softc *nsc = device_private(parent);
86 	struct nvme_attach_args *naa = aux;
87 	struct nvme_namespace *ns;
88 	struct nvm_namespace_format *f;
89 
90 	ld->sc_dv = self;
91 	sc->sc_nvme = nsc;
92 	sc->sc_nsid = naa->naa_nsid;
93 
94 	aprint_naive("\n");
95 	aprint_normal("\n");
96 
97 	ns = nvme_ns_get(sc->sc_nvme, sc->sc_nsid);
98 	KASSERT(ns);
99 
100 	f = &ns->ident->lbaf[NVME_ID_NS_FLBAS(ns->ident->flbas)];
101 	KASSERT(f->lbads >= 9); /* only valid LBS data sizes allowed here */
102 
103 	ld->sc_secsize = 1 << f->lbads;
104 	ld->sc_secperunit = ns->ident->nsze;
105 	ld->sc_maxxfer = naa->naa_maxphys;
106 	ld->sc_maxqueuecnt = naa->naa_qentries;
107 	ld->sc_start = ld_nvme_start;
108 	ld->sc_dump = ld_nvme_dump;
109 	ld->sc_ioctl = ld_nvme_ioctl;
110 	ld->sc_flags = LDF_ENABLED | LDF_NO_RND | LDF_MPSAFE;
111 	ld->sc_typename = kmem_asprintf("%s", naa->naa_typename);
112 	ldattach(ld, "fcfs");
113 }
114 
115 static int
ld_nvme_detach(device_t self,int flags)116 ld_nvme_detach(device_t self, int flags)
117 {
118 	struct ld_nvme_softc *sc = device_private(self);
119 	struct ld_softc *ld = &sc->sc_ld;
120 	int rv;
121 
122 	if ((rv = ldbegindetach(ld, flags)) != 0)
123 		return rv;
124 	ldenddetach(ld);
125 
126 	kmem_free(ld->sc_typename, strlen(ld->sc_typename) + 1);
127 
128 	nvme_ns_free(sc->sc_nvme, sc->sc_nsid);
129 
130 	return 0;
131 }
132 
133 static int
ld_nvme_start(struct ld_softc * ld,struct buf * bp)134 ld_nvme_start(struct ld_softc *ld, struct buf *bp)
135 {
136 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
137 	int flags = BUF_ISWRITE(bp) ? 0 : NVME_NS_CTX_F_READ;
138 
139 	if (bp->b_flags & B_MEDIA_FUA)
140 		flags |= NVME_NS_CTX_F_FUA;
141 
142 	return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
143 	    bp, bp->b_data, bp->b_bcount,
144 	    sc->sc_ld.sc_secsize, bp->b_rawblkno,
145 	    flags,
146 	    ld_nvme_biodone);
147 }
148 
149 static int
ld_nvme_dump(struct ld_softc * ld,void * data,int blkno,int blkcnt)150 ld_nvme_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
151 {
152 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
153 
154 	return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
155 	    NULL, data, blkcnt * ld->sc_secsize,
156 	    sc->sc_ld.sc_secsize, blkno,
157 	    NVME_NS_CTX_F_POLL,
158 	    ld_nvme_biodone);
159 }
160 
161 static void
ld_nvme_biodone(void * xc,struct buf * bp,uint16_t cmd_status,uint32_t cdw0)162 ld_nvme_biodone(void *xc, struct buf *bp, uint16_t cmd_status, uint32_t cdw0)
163 {
164 	struct ld_nvme_softc *sc = xc;
165 	uint16_t status = NVME_CQE_SC(cmd_status);
166 
167 	if (bp != NULL) {
168 		if (status != NVME_CQE_SC_SUCCESS) {
169 			bp->b_error = EIO;
170 			bp->b_resid = bp->b_bcount;
171 			device_printf(sc->sc_ld.sc_dv, "I/O error\n");
172 		} else {
173 			bp->b_resid = 0;
174 		}
175 		lddone(&sc->sc_ld, bp);
176 	} else {
177 		if (status != NVME_CQE_SC_SUCCESS) {
178 			device_printf(sc->sc_ld.sc_dv, "I/O error\n");
179 		}
180 	}
181 }
182 
183 static int
ld_nvme_flush(struct ld_softc * ld,bool poll)184 ld_nvme_flush(struct ld_softc *ld, bool poll)
185 {
186 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
187 
188 	return nvme_ns_sync(sc->sc_nvme, sc->sc_nsid,
189 	    poll ? NVME_NS_CTX_F_POLL : 0);
190 }
191 
192 static int
ld_nvme_getcache(struct ld_softc * ld,int * addr)193 ld_nvme_getcache(struct ld_softc *ld, int *addr)
194 {
195 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
196 
197 	return nvme_admin_getcache(sc->sc_nvme, addr);
198 }
199 
200 static int
ld_nvme_setcache(struct ld_softc * ld,int addr)201 ld_nvme_setcache(struct ld_softc *ld, int addr)
202 {
203 	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
204 
205 	return nvme_admin_setcache(sc->sc_nvme, addr);
206 }
207 
208 static int
ld_nvme_ioctl(struct ld_softc * ld,u_long cmd,void * addr,int32_t flag,bool poll)209 ld_nvme_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag, bool poll)
210 {
211 	int error;
212 
213 	switch (cmd) {
214 	case DIOCCACHESYNC:
215 		error = ld_nvme_flush(ld, poll);
216 		break;
217 
218 	case DIOCGCACHE:
219 		error = ld_nvme_getcache(ld, (int *)addr);
220 		break;
221 
222 	case DIOCSCACHE:
223 		error = ld_nvme_setcache(ld, *(int *)addr);
224 		break;
225 
226 	default:
227 		error = EPASSTHROUGH;
228 		break;
229 	}
230 
231 	return error;
232 }
233 
234 MODULE(MODULE_CLASS_DRIVER, ld_nvme, "ld,nvme,bufq_fcfs");
235 
236 #ifdef _MODULE
237 /*
238  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
239  * XXX it will be defined in the common-code module
240  */
241 #undef	CFDRIVER_DECL
242 #define	CFDRIVER_DECL(name, class, attr)
243 #include "ioconf.c"
244 #endif
245 
246 static int
ld_nvme_modcmd(modcmd_t cmd,void * opaque)247 ld_nvme_modcmd(modcmd_t cmd, void *opaque)
248 {
249 #ifdef _MODULE
250 	/*
251 	 * We ignore the cfdriver_vec[] that ioconf provides, since
252 	 * the cfdrivers are attached already.
253 	 */
254 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
255 #endif
256 	int error = 0;
257 
258 #ifdef _MODULE
259 	switch (cmd) {
260 	case MODULE_CMD_INIT:
261 		error = config_init_component(no_cfdriver_vec,
262 		    cfattach_ioconf_ld_nvme, cfdata_ioconf_ld_nvme);
263 		break;
264 	case MODULE_CMD_FINI:
265 		error = config_fini_component(no_cfdriver_vec,
266 		    cfattach_ioconf_ld_nvme, cfdata_ioconf_ld_nvme);
267 		break;
268 	default:
269 		error = ENOTTY;
270 		break;
271 	}
272 #endif
273 
274 	return error;
275 }
276