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 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/bio.h>
30 #include <sys/bus.h>
31 #include <sys/kernel.h>
32 #include <sys/kthread.h>
33 #include <sys/lock.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/proc.h>
37 #include <geom/geom_disk.h>
38
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 #include <dev/ofw/openfirm.h>
45 #include "opal.h"
46
47 /*
48 * OPAL System flash driver, using OPAL firmware calls to access the device.
49 *
50 * This just presents the base block interface. The fdt_slicer can be used on
51 * top to present the partitions listed in the fdt.
52 *
53 * There are three OPAL methods used: OPAL_FLASH_READ, OPAL_FLASH_WRITE, and
54 * OPAL_FLASH_ERASE. At the firmware layer, READ and WRITE can be on arbitrary
55 * boundaries, but ERASE is only at flash-block-size block alignments and sizes.
56 * To account for this, the following restrictions are in place:
57 *
58 * - Reads are on a 512-byte block boundary and size
59 * - Writes and Erases are aligned and sized on flash-block-size bytes.
60 *
61 * In order to support the fdt_slicer we present a type attribute of
62 * NAND::device.
63 */
64 struct opalflash_softc {
65 device_t sc_dev;
66 struct mtx sc_mtx;
67 struct disk *sc_disk;
68 struct proc *sc_p;
69 struct bio_queue_head sc_bio_queue;
70 int sc_opal_id;
71 bool sc_erase; /* Erase is needed before write. */
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 DRIVER_MODULE(opalflash, opal, opalflash_driver, 0, 0);
100
101 /* GEOM Disk interfaces. */
102 static int
opalflash_open(struct disk * dp)103 opalflash_open(struct disk *dp)
104 {
105
106 return (0);
107 }
108
109 static int
opalflash_close(struct disk * dp)110 opalflash_close(struct disk *dp)
111 {
112
113 return (0);
114 }
115
116 static int
opalflash_ioctl(struct disk * dp,u_long cmd,void * data,int fflag,struct thread * td)117 opalflash_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
118 struct thread *td)
119 {
120
121 return (EINVAL);
122 }
123
124 /* Handle the one attribute we need to play nice with geom_flashmap. */
125 static int
opalflash_getattr(struct bio * bp)126 opalflash_getattr(struct bio *bp)
127 {
128 struct opalflash_softc *sc;
129 device_t dev;
130
131 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
132 return (ENXIO);
133
134 sc = bp->bio_disk->d_drv1;
135 dev = sc->sc_dev;
136
137 if (strcmp(bp->bio_attribute, "NAND::device") == 0) {
138 if (bp->bio_length != sizeof(dev))
139 return (EFAULT);
140 bcopy(&dev, bp->bio_data, sizeof(dev));
141 } else
142 return (-1);
143 return (0);
144 }
145
146 static void
opalflash_strategy(struct bio * bp)147 opalflash_strategy(struct bio *bp)
148 {
149 struct opalflash_softc *sc;
150
151 sc = (struct opalflash_softc *)bp->bio_disk->d_drv1;
152 OPALFLASH_LOCK(sc);
153 bioq_disksort(&sc->sc_bio_queue, bp);
154 wakeup(sc);
155 OPALFLASH_UNLOCK(sc);
156 }
157
158 static int
opalflash_read(struct opalflash_softc * sc,off_t off,caddr_t data,off_t count)159 opalflash_read(struct opalflash_softc *sc, off_t off,
160 caddr_t data, off_t count)
161 {
162 struct opal_msg msg;
163 int rv, size, token;
164
165 /* Ensure we write aligned to a full block size. */
166 if (off % sc->sc_disk->d_sectorsize != 0 ||
167 count % sc->sc_disk->d_sectorsize != 0)
168 return (EIO);
169
170 token = opal_alloc_async_token();
171
172 /*
173 * Read one page at a time. It's not guaranteed that the buffer is
174 * physically contiguous.
175 */
176 rv = 0;
177 while (count > 0) {
178 size = MIN(count, PAGE_SIZE);
179 size = MIN(size, PAGE_SIZE - ((u_long)data & PAGE_MASK));
180 rv = opal_call(OPAL_FLASH_READ, sc->sc_opal_id, off,
181 vtophys(data), size, token);
182 if (rv == OPAL_ASYNC_COMPLETION) {
183 rv = opal_wait_completion(&msg, sizeof(msg), token);
184 if (rv == OPAL_SUCCESS)
185 rv = msg.params[1];
186 }
187 if (rv != OPAL_SUCCESS)
188 break;
189 count -= size;
190 off += size;
191 data += size;
192 }
193 opal_free_async_token(token);
194 if (rv == OPAL_SUCCESS)
195 rv = 0;
196 else
197 rv = EIO;
198
199 return (rv);
200 }
201
202 static int
opalflash_erase(struct opalflash_softc * sc,off_t off,off_t count)203 opalflash_erase(struct opalflash_softc *sc, off_t off, off_t count)
204 {
205 struct opal_msg msg;
206 int rv, token;
207
208 /* Ensure we write aligned to a full block size. */
209 if (off % sc->sc_disk->d_stripesize != 0 ||
210 count % sc->sc_disk->d_stripesize != 0)
211 return (EIO);
212
213 token = opal_alloc_async_token();
214
215 rv = opal_call(OPAL_FLASH_ERASE, sc->sc_opal_id, off, count, token);
216 if (rv == OPAL_ASYNC_COMPLETION) {
217 rv = opal_wait_completion(&msg, sizeof(msg), token);
218 if (rv == OPAL_SUCCESS)
219 rv = msg.params[1];
220 }
221 opal_free_async_token(token);
222
223 if (rv == OPAL_SUCCESS)
224 rv = 0;
225 else
226 rv = EIO;
227
228 return (rv);
229 }
230
231 static int
opalflash_write(struct opalflash_softc * sc,off_t off,caddr_t data,off_t count)232 opalflash_write(struct opalflash_softc *sc, off_t off,
233 caddr_t data, off_t count)
234 {
235 struct opal_msg msg;
236 int rv, size, token;
237
238 /* Ensure we write aligned to a full block size. */
239 if (off % sc->sc_disk->d_sectorsize != 0 ||
240 count % sc->sc_disk->d_sectorsize != 0)
241 return (EIO);
242
243 if (sc->sc_erase) {
244 /* Erase the full block first, then write in page chunks. */
245 rv = opalflash_erase(sc, off, count);
246 if (rv != 0)
247 return (rv);
248 }
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
opalflash_task(void * arg)284 opalflash_task(void *arg)
285 {
286 struct opalflash_softc *sc;
287 struct bio *bp;
288
289 sc = arg;
290
291 for (;;) {
292 OPALFLASH_LOCK(sc);
293 do {
294 bp = bioq_first(&sc->sc_bio_queue);
295 if (bp == NULL)
296 msleep(sc, &sc->sc_mtx, PRIBIO, "opalflash", 0);
297 } while (bp == NULL);
298 bioq_remove(&sc->sc_bio_queue, bp);
299 OPALFLASH_UNLOCK(sc);
300
301 switch (bp->bio_cmd) {
302 case BIO_DELETE:
303 bp->bio_error = opalflash_erase(sc, bp->bio_offset,
304 bp->bio_bcount);
305 break;
306 case BIO_READ:
307 bp->bio_error = opalflash_read(sc, bp->bio_offset,
308 bp->bio_data, bp->bio_bcount);
309 break;
310 case BIO_WRITE:
311 bp->bio_error = opalflash_write(sc, bp->bio_offset,
312 bp->bio_data, bp->bio_bcount);
313 break;
314 default:
315 bp->bio_error = EINVAL;
316 }
317 biodone(bp);
318 }
319 }
320
321 /* Device driver interfaces. */
322
323 static int
opalflash_probe(device_t dev)324 opalflash_probe(device_t dev)
325 {
326 if (!ofw_bus_is_compatible(dev, "ibm,opal-flash"))
327 return (ENXIO);
328
329 device_set_desc(dev, "OPAL System Flash");
330
331 return (BUS_PROBE_GENERIC);
332 }
333
334 static int
opalflash_attach(device_t dev)335 opalflash_attach(device_t dev)
336 {
337 struct opalflash_softc *sc;
338 phandle_t node;
339 cell_t flash_blocksize, opal_id;
340 uint32_t regs[2];
341
342 sc = device_get_softc(dev);
343 sc->sc_dev = dev;
344
345 node = ofw_bus_get_node(dev);
346 OF_getencprop(node, "ibm,opal-id", &opal_id, sizeof(opal_id));
347 sc->sc_opal_id = opal_id;
348
349 if (OF_getencprop(node, "ibm,flash-block-size",
350 &flash_blocksize, sizeof(flash_blocksize)) < 0) {
351 device_printf(dev, "Cannot determine flash block size.\n");
352 return (ENXIO);
353 }
354
355 if (!OF_hasprop(node, "no-erase"))
356 sc->sc_erase = true;
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