xref: /freebsd/sys/powerpc/powernv/opal_flash.c (revision 731d06abf2105cc0873fa84e972178f9f37ca760)
1 /*-
2  * Copyright (c) 2019 Justin Hibbits
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bio.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/kthread.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/proc.h>
38 #include <geom/geom_disk.h>
39 
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
42 
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 #include <dev/ofw/openfirm.h>
46 #include "opal.h"
47 
48 /*
49  * OPAL System flash driver, using OPAL firmware calls to access the device.
50  *
51  * This just presents the base block interface.  The fdt_slicer can be used on
52  * top to present the partitions listed in the fdt.
53  *
54  * There are three OPAL methods used: OPAL_FLASH_READ, OPAL_FLASH_WRITE, and
55  * OPAL_FLASH_ERASE.  At the firmware layer, READ and WRITE can be on arbitrary
56  * boundaries, but ERASE is only at flash-block-size block alignments and sizes.
57  * To account for this, the following restrictions are in place:
58  *
59  * - Reads are on a 512-byte block boundary and size
60  * - Writes and Erases are aligned and sized on flash-block-size bytes.
61  *
62  * In order to support the fdt_slicer we present a type attribute of
63  * NAND::device.
64  */
65 struct opalflash_softc {
66 	device_t		 sc_dev;
67 	struct mtx		 sc_mtx;
68 	struct disk		*sc_disk;
69 	struct proc		*sc_p;
70 	struct bio_queue_head	 sc_bio_queue;
71 	int		 	 sc_opal_id;
72 };
73 
74 #define	OPALFLASH_LOCK(sc)		mtx_lock(&(sc)->sc_mtx)
75 #define	OPALFLASH_UNLOCK(sc)		mtx_unlock(&(sc)->sc_mtx)
76 #define	OPALFLASH_LOCK_INIT(sc) \
77 	mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->sc_dev), \
78 	    "opalflash", MTX_DEF)
79 
80 #define	FLASH_BLOCKSIZE			512
81 
82 static int	opalflash_probe(device_t);
83 static int	opalflash_attach(device_t);
84 
85 static device_method_t  opalflash_methods[] = {
86 	/* Device interface */
87 	DEVMETHOD(device_probe,		opalflash_probe),
88 	DEVMETHOD(device_attach,	opalflash_attach),
89 
90 	DEVMETHOD_END
91 };
92 
93 static driver_t opalflash_driver = {
94 	"opalflash",
95 	opalflash_methods,
96 	sizeof(struct opalflash_softc)
97 };
98 
99 static devclass_t opalflash_devclass;
100 
101 DRIVER_MODULE(opalflash, opal, opalflash_driver, opalflash_devclass, 0, 0);
102 
103 /* GEOM Disk interfaces. */
104 static int
105 opalflash_open(struct disk *dp)
106 {
107 
108 	return (0);
109 }
110 
111 static int
112 opalflash_close(struct disk *dp)
113 {
114 
115 	return (0);
116 }
117 
118 static int
119 opalflash_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
120 	struct thread *td)
121 {
122 
123 	return (EINVAL);
124 }
125 
126 /* Handle the one attribute we need to play nice with geom_flashmap. */
127 static int
128 opalflash_getattr(struct bio *bp)
129 {
130 	struct opalflash_softc *sc;
131 	device_t dev;
132 
133 	if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
134 		return (ENXIO);
135 
136 	sc = bp->bio_disk->d_drv1;
137 	dev = sc->sc_dev;
138 
139 	if (strcmp(bp->bio_attribute, "NAND::device") == 0) {
140 		if (bp->bio_length != sizeof(dev))
141 			return (EFAULT);
142 		bcopy(&dev, bp->bio_data, sizeof(dev));
143 	} else
144 		return (-1);
145 	return (0);
146 }
147 
148 static void
149 opalflash_strategy(struct bio *bp)
150 {
151 	struct opalflash_softc *sc;
152 
153 	sc = (struct opalflash_softc *)bp->bio_disk->d_drv1;
154 	OPALFLASH_LOCK(sc);
155 	bioq_disksort(&sc->sc_bio_queue, bp);
156 	wakeup(sc);
157 	OPALFLASH_UNLOCK(sc);
158 }
159 
160 static int
161 opalflash_read(struct opalflash_softc *sc, off_t off,
162     caddr_t data, off_t count)
163 {
164 	struct opal_msg msg;
165 	int rv, size, token;
166 
167 	/* Ensure we write aligned to a full block size. */
168 	if (off % sc->sc_disk->d_sectorsize != 0 ||
169 	    count % sc->sc_disk->d_sectorsize != 0)
170 		return (EIO);
171 
172 	token = opal_alloc_async_token();
173 
174 	/*
175 	 * Read one page at a time.  It's not guaranteed that the buffer is
176 	 * physically contiguous.
177 	 */
178 	rv = 0;
179 	while (count > 0) {
180 		size = MIN(count, PAGE_SIZE);
181 		size = MIN(size, PAGE_SIZE - ((u_long)data & PAGE_MASK));
182 		rv = opal_call(OPAL_FLASH_READ, sc->sc_opal_id, off,
183 		    vtophys(data), size, token);
184 		if (rv == OPAL_ASYNC_COMPLETION) {
185 			rv = opal_wait_completion(&msg, sizeof(msg), token);
186 			if (rv == OPAL_SUCCESS)
187 				rv = msg.params[1];
188 		}
189 		if (rv != OPAL_SUCCESS)
190 			break;
191 		count -= size;
192 		off += size;
193 		data += size;
194 	}
195 	opal_free_async_token(token);
196 	if (rv == OPAL_SUCCESS)
197 		rv = 0;
198 	else
199 		rv = EIO;
200 
201 	return (rv);
202 }
203 
204 static int
205 opalflash_erase(struct opalflash_softc *sc, off_t off, off_t count)
206 {
207 	struct opal_msg msg;
208 	int rv, token;
209 
210 	/* Ensure we write aligned to a full block size. */
211 	if (off % sc->sc_disk->d_stripesize != 0 ||
212 	    count % sc->sc_disk->d_stripesize != 0)
213 		return (EIO);
214 
215 	token = opal_alloc_async_token();
216 
217 	rv = opal_call(OPAL_FLASH_ERASE, sc->sc_opal_id, off, count, token);
218 	if (rv == OPAL_ASYNC_COMPLETION) {
219 		rv = opal_wait_completion(&msg, sizeof(msg), token);
220 		if (rv == OPAL_SUCCESS)
221 			rv = msg.params[1];
222 	}
223 	opal_free_async_token(token);
224 
225 	if (rv == OPAL_SUCCESS)
226 		rv = 0;
227 	else
228 		rv = EIO;
229 
230 	return (rv);
231 }
232 
233 static int
234 opalflash_write(struct opalflash_softc *sc, off_t off,
235     caddr_t data, off_t count)
236 {
237 	struct opal_msg msg;
238 	int rv, size, token;
239 
240 	/* Ensure we write aligned to a full block size. */
241 	if (off % sc->sc_disk->d_stripesize != 0 ||
242 	    count % sc->sc_disk->d_stripesize != 0)
243 		return (EIO);
244 
245 	/* Erase the full block first, then write in page chunks. */
246 	rv = opalflash_erase(sc, off, count);
247 	if (rv != 0)
248 		return (rv);
249 
250 	token = opal_alloc_async_token();
251 
252 	/*
253 	 * Write one page at a time.  It's not guaranteed that the buffer is
254 	 * physically contiguous.
255 	 */
256 	while (count > 0) {
257 		size = MIN(count, PAGE_SIZE);
258 		size = MIN(size, PAGE_SIZE - ((u_long)data & PAGE_MASK));
259 		rv = opal_call(OPAL_FLASH_WRITE, sc->sc_opal_id, off,
260 		    vtophys(data), size, token);
261 		if (rv == OPAL_ASYNC_COMPLETION) {
262 			rv = opal_wait_completion(&msg, sizeof(msg), token);
263 			if (rv == OPAL_SUCCESS)
264 				rv = msg.params[1];
265 		}
266 		if (rv != OPAL_SUCCESS)
267 			break;
268 		count -= size;
269 		off += size;
270 		data += size;
271 	}
272 	opal_free_async_token(token);
273 
274 	if (rv == OPAL_SUCCESS)
275 		rv = 0;
276 	else
277 		rv = EIO;
278 
279 	return (rv);
280 }
281 
282 /* Main flash handling task. */
283 static void
284 opalflash_task(void *arg)
285 {
286 	struct opalflash_softc *sc;
287 	struct bio *bp;
288 	device_t dev;
289 
290 	sc = arg;
291 
292 	for (;;) {
293 		dev = sc->sc_dev;
294 		OPALFLASH_LOCK(sc);
295 		do {
296 			bp = bioq_first(&sc->sc_bio_queue);
297 			if (bp == NULL)
298 				msleep(sc, &sc->sc_mtx, PRIBIO, "opalflash", 0);
299 		} while (bp == NULL);
300 		bioq_remove(&sc->sc_bio_queue, bp);
301 		OPALFLASH_UNLOCK(sc);
302 
303 		switch (bp->bio_cmd) {
304 		case BIO_DELETE:
305 			bp->bio_error = opalflash_erase(sc, bp->bio_offset,
306 			    bp->bio_bcount);
307 			break;
308 		case BIO_READ:
309 			bp->bio_error = opalflash_read(sc, bp->bio_offset,
310 			    bp->bio_data, bp->bio_bcount);
311 			break;
312 		case BIO_WRITE:
313 			bp->bio_error = opalflash_write(sc, bp->bio_offset,
314 			    bp->bio_data, bp->bio_bcount);
315 			break;
316 		default:
317 			bp->bio_error = EINVAL;
318 		}
319 		biodone(bp);
320 	}
321 }
322 
323 
324 /* Device driver interfaces. */
325 
326 static int
327 opalflash_probe(device_t dev)
328 {
329 	if (!ofw_bus_is_compatible(dev, "ibm,opal-flash"))
330 		return (ENXIO);
331 
332 	device_set_desc(dev, "OPAL System Flash");
333 
334 	return (BUS_PROBE_GENERIC);
335 }
336 
337 static int
338 opalflash_attach(device_t dev)
339 {
340 	struct opalflash_softc *sc;
341 	phandle_t node;
342 	cell_t flash_blocksize, opal_id;
343 	uint32_t regs[2];
344 
345 	sc = device_get_softc(dev);
346 	sc->sc_dev = dev;
347 
348 	node = ofw_bus_get_node(dev);
349 	OF_getencprop(node, "ibm,opal-id", &opal_id, sizeof(opal_id));
350 	sc->sc_opal_id = opal_id;
351 
352 	if (OF_getencprop(node, "ibm,flash-block-size",
353 	    &flash_blocksize, sizeof(flash_blocksize)) < 0) {
354 		device_printf(dev, "Cannot determine flash block size.\n");
355 		return (ENXIO);
356 	}
357 
358 	OPALFLASH_LOCK_INIT(sc);
359 
360 	if (OF_getencprop(node, "reg", regs, sizeof(regs)) < 0) {
361 		device_printf(dev, "Unable to get flash size.\n");
362 		return (ENXIO);
363 	}
364 
365 	sc->sc_disk = disk_alloc();
366 	sc->sc_disk->d_name = "opalflash";
367 	sc->sc_disk->d_open = opalflash_open;
368 	sc->sc_disk->d_close = opalflash_close;
369 	sc->sc_disk->d_strategy = opalflash_strategy;
370 	sc->sc_disk->d_ioctl = opalflash_ioctl;
371 	sc->sc_disk->d_getattr = opalflash_getattr;
372 	sc->sc_disk->d_drv1 = sc;
373 	sc->sc_disk->d_maxsize = DFLTPHYS;
374 	sc->sc_disk->d_mediasize = regs[1];
375 	sc->sc_disk->d_unit = device_get_unit(sc->sc_dev);
376 	sc->sc_disk->d_sectorsize = FLASH_BLOCKSIZE;
377 	    sc->sc_disk->d_stripesize = flash_blocksize;
378 	sc->sc_disk->d_dump = NULL;
379 
380 	disk_create(sc->sc_disk, DISK_VERSION);
381 	bioq_init(&sc->sc_bio_queue);
382 
383 	kproc_create(&opalflash_task, sc, &sc->sc_p, 0, 0, "task: OPAL Flash");
384 
385 	return (0);
386 }
387