summaryrefslogtreecommitdiff
path: root/sys/dev/ic/ld_nvme.c
blob: c02b2ffac920ce01179a546d7f10d33f59064491 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*	$NetBSD: ld_nvme.c,v 1.25 2022/07/30 12:48:17 mlelstv Exp $	*/

/*-
 * Copyright (C) 2016 NONAKA Kimihiro <nonaka@netbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ld_nvme.c,v 1.25 2022/07/30 12:48:17 mlelstv Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/buf.h>
#include <sys/bufq.h>
#include <sys/disk.h>
#include <sys/kmem.h>
#include <sys/module.h>

#include <dev/ldvar.h>
#include <dev/ic/nvmereg.h>
#include <dev/ic/nvmevar.h>

#include "ioconf.h"

struct ld_nvme_softc {
	struct ld_softc		sc_ld;
	struct nvme_softc	*sc_nvme;

	uint16_t		sc_nsid;
};

static int	ld_nvme_match(device_t, cfdata_t, void *);
static void	ld_nvme_attach(device_t, device_t, void *);
static int	ld_nvme_detach(device_t, int);

CFATTACH_DECL_NEW(ld_nvme, sizeof(struct ld_nvme_softc),
    ld_nvme_match, ld_nvme_attach, ld_nvme_detach, NULL);

static int	ld_nvme_start(struct ld_softc *, struct buf *);
static int	ld_nvme_dump(struct ld_softc *, void *, int, int);
static int	ld_nvme_flush(struct ld_softc *, bool);
static int	ld_nvme_getcache(struct ld_softc *, int *);
static int	ld_nvme_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);

static void	ld_nvme_biodone(void *, struct buf *, uint16_t, uint32_t);

static int
ld_nvme_match(device_t parent, cfdata_t match, void *aux)
{
	struct nvme_attach_args *naa = aux;

	if (naa->naa_nsid == 0)
		return 0;

	return 1;
}

static void
ld_nvme_attach(device_t parent, device_t self, void *aux)
{
	struct ld_nvme_softc *sc = device_private(self);
	struct ld_softc *ld = &sc->sc_ld;
	struct nvme_softc *nsc = device_private(parent);
	struct nvme_attach_args *naa = aux;
	struct nvme_namespace *ns;
	struct nvm_namespace_format *f;

	ld->sc_dv = self;
	sc->sc_nvme = nsc;
	sc->sc_nsid = naa->naa_nsid;

	aprint_naive("\n");
	aprint_normal("\n");

	ns = nvme_ns_get(sc->sc_nvme, sc->sc_nsid);
	KASSERT(ns);

	f = &ns->ident->lbaf[NVME_ID_NS_FLBAS(ns->ident->flbas)];
	KASSERT(f->lbads >= 9); /* only valid LBS data sizes allowed here */

	ld->sc_secsize = 1 << f->lbads;
	ld->sc_secperunit = ns->ident->nsze;
	ld->sc_maxxfer = naa->naa_maxphys;
	ld->sc_maxqueuecnt = naa->naa_qentries;
	ld->sc_start = ld_nvme_start;
	ld->sc_dump = ld_nvme_dump;
	ld->sc_ioctl = ld_nvme_ioctl;
	ld->sc_flags = LDF_ENABLED | LDF_NO_RND | LDF_MPSAFE;
	ld->sc_typename = kmem_asprintf("%s", naa->naa_typename);
	ldattach(ld, "fcfs");
}

static int
ld_nvme_detach(device_t self, int flags)
{
	struct ld_nvme_softc *sc = device_private(self);
	struct ld_softc *ld = &sc->sc_ld;
	int rv;

	if ((rv = ldbegindetach(ld, flags)) != 0)
		return rv;
	ldenddetach(ld);

	kmem_free(ld->sc_typename, strlen(ld->sc_typename) + 1);

	nvme_ns_free(sc->sc_nvme, sc->sc_nsid);

	return 0;
}

static int
ld_nvme_start(struct ld_softc *ld, struct buf *bp)
{
	struct ld_nvme_softc *sc = device_private(ld->sc_dv);
	int flags = BUF_ISWRITE(bp) ? 0 : NVME_NS_CTX_F_READ;

	if (bp->b_flags & B_MEDIA_FUA)
		flags |= NVME_NS_CTX_F_FUA;

	return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
	    bp, bp->b_data, bp->b_bcount,
	    sc->sc_ld.sc_secsize, bp->b_rawblkno,
	    flags,
	    ld_nvme_biodone);
}

static int
ld_nvme_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
{
	struct ld_nvme_softc *sc = device_private(ld->sc_dv);

	return nvme_ns_dobio(sc->sc_nvme, sc->sc_nsid, sc,
	    NULL, data, blkcnt * ld->sc_secsize,
	    sc->sc_ld.sc_secsize, blkno,
	    NVME_NS_CTX_F_POLL,
	    ld_nvme_biodone);
}

static void
ld_nvme_biodone(void *xc, struct buf *bp, uint16_t cmd_status, uint32_t cdw0)
{
	struct ld_nvme_softc *sc = xc;
	uint16_t status = NVME_CQE_SC(cmd_status);

	if (bp != NULL) {
		if (status != NVME_CQE_SC_SUCCESS) {
			bp->b_error = EIO;
			bp->b_resid = bp->b_bcount;
			device_printf(sc->sc_ld.sc_dv, "I/O error\n");
		} else {
			bp->b_resid = 0;
		}
		lddone(&sc->sc_ld, bp);
	} else {
		if (status != NVME_CQE_SC_SUCCESS) {
			device_printf(sc->sc_ld.sc_dv, "I/O error\n");
		}
	}
}

static int
ld_nvme_flush(struct ld_softc *ld, bool poll)
{
	struct ld_nvme_softc *sc = device_private(ld->sc_dv);

	return nvme_ns_sync(sc->sc_nvme, sc->sc_nsid,
	    poll ? NVME_NS_CTX_F_POLL : 0);
}

static int
ld_nvme_getcache(struct ld_softc *ld, int *addr)
{
	struct ld_nvme_softc *sc = device_private(ld->sc_dv);

	return nvme_admin_getcache(sc->sc_nvme, addr);
}

static int
ld_nvme_setcache(struct ld_softc *ld, int addr)
{
	struct ld_nvme_softc *sc = device_private(ld->sc_dv);

	return nvme_admin_setcache(sc->sc_nvme, addr);
}

static int
ld_nvme_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag, bool poll)
{
	int error;

	switch (cmd) {
	case DIOCCACHESYNC:
		error = ld_nvme_flush(ld, poll);
		break;

	case DIOCGCACHE:
		error = ld_nvme_getcache(ld, (int *)addr);
		break;

	case DIOCSCACHE:
		error = ld_nvme_setcache(ld, *(int *)addr);
		break;

	default:
		error = EPASSTHROUGH;
		break;
	}

	return error;
}

MODULE(MODULE_CLASS_DRIVER, ld_nvme, "ld,nvme,bufq_fcfs");

#ifdef _MODULE
/*
 * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
 * XXX it will be defined in the common-code module
 */
#undef	CFDRIVER_DECL
#define	CFDRIVER_DECL(name, class, attr)
#include "ioconf.c"
#endif

static int
ld_nvme_modcmd(modcmd_t cmd, void *opaque)
{
#ifdef _MODULE
	/*
	 * We ignore the cfdriver_vec[] that ioconf provides, since
	 * the cfdrivers are attached already.
	 */
	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
#endif
	int error = 0;

#ifdef _MODULE
	switch (cmd) {
	case MODULE_CMD_INIT:
		error = config_init_component(no_cfdriver_vec,
		    cfattach_ioconf_ld_nvme, cfdata_ioconf_ld_nvme);
		break;
	case MODULE_CMD_FINI:
		error = config_fini_component(no_cfdriver_vec,
		    cfattach_ioconf_ld_nvme, cfdata_ioconf_ld_nvme);
		break;
	default:
		error = ENOTTY;
		break;
	}
#endif

	return error;
}