Index: head/sys/powerpc/powernv/opal.h =================================================================== --- head/sys/powerpc/powernv/opal.h (revision 345974) +++ head/sys/powerpc/powernv/opal.h (revision 345975) @@ -1,219 +1,219 @@ /*- * Copyright (c) 2015 Nathan Whitehorn * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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. * * $FreeBSD$ */ #ifndef _POWERNV_OPAL_H #define _POWERNV_OPAL_H #include #include #include /* Check if OPAL is correctly instantiated. Will try to instantiate it. */ int opal_check(void); /* Call an OPAL method. Any pointers passed must be real-mode accessible! */ int opal_call(uint64_t token, ...); #define OPAL_CONSOLE_WRITE 1 #define OPAL_CONSOLE_READ 2 #define OPAL_RTC_READ 3 #define OPAL_RTC_WRITE 4 #define OPAL_CEC_POWER_DOWN 5 #define OPAL_CEC_REBOOT 6 #define OPAL_HANDLE_INTERRUPT 9 #define OPAL_POLL_EVENTS 10 #define OPAL_PCI_CONFIG_READ_BYTE 13 #define OPAL_PCI_CONFIG_READ_HALF_WORD 14 #define OPAL_PCI_CONFIG_READ_WORD 15 #define OPAL_PCI_CONFIG_WRITE_BYTE 16 #define OPAL_PCI_CONFIG_WRITE_HALF_WORD 17 #define OPAL_PCI_CONFIG_WRITE_WORD 18 #define OPAL_PCI_EEH_FREEZE_CLEAR 26 #define OPAL_PCI_PHB_MMIO_ENABLE 27 #define OPAL_PCI_SET_PHB_MEM_WINDOW 28 #define OPAL_PCI_MAP_PE_MMIO_WINDOW 29 #define OPAL_PCI_SET_XIVE_PE 37 #define OPAL_PCI_RESET 49 #define OPAL_PCI_POLL 62 #define OPAL_SET_XIVE 19 #define OPAL_GET_XIVE 20 #define OPAL_PCI_SET_PE 31 #define OPAL_GET_MSI_32 39 #define OPAL_GET_MSI_64 40 #define OPAL_PCI_MSI_EOI 63 #define OPAL_PCI_GET_PHB_DIAG_DATA2 64 #define OPAL_START_CPU 41 #define OPAL_PCI_MAP_PE_DMA_WINDOW 44 #define OPAL_PCI_MAP_PE_DMA_WINDOW_REAL 45 #define OPAL_RETURN_CPU 69 #define OPAL_REINIT_CPUS 70 #define OPAL_CHECK_TOKEN 80 #define OPAL_GET_MSG 85 #define OPAL_CHECK_ASYNC_COMPLETION 86 #define OPAL_SENSOR_READ 88 #define OPAL_HANDLE_HMI 98 #define OPAL_IPMI_SEND 107 #define OPAL_IPMI_RECV 108 #define OPAL_I2C_REQUEST 109 #define OPAL_FLASH_READ 110 #define OPAL_FLASH_WRITE 111 -#define OPAL_FLASH_ERASE 111 +#define OPAL_FLASH_ERASE 112 #define OPAL_INT_GET_XIRR 122 #define OPAL_INT_SET_CPPR 123 #define OPAL_INT_EOI 124 #define OPAL_INT_SET_MFRR 125 #define OPAL_PCI_TCE_KILL 126 #define OPAL_XIVE_RESET 128 #define OPAL_XIVE_GET_IRQ_INFO 129 #define OPAL_XIVE_GET_IRQ_CONFIG 130 #define OPAL_XIVE_SET_IRQ_CONFIG 131 #define OPAL_XIVE_GET_QUEUE_INFO 132 #define OPAL_XIVE_SET_QUEUE_INFO 133 #define OPAL_XIVE_DONATE_PAGE 134 #define OPAL_XIVE_ALLOCATE_VP_BLOCK 135 #define OPAL_XIVE_FREE_VP_BLOCK 136 #define OPAL_XIVE_GET_VP_INFO 137 #define OPAL_XIVE_SET_VP_INFO 138 #define OPAL_XIVE_ALLOCATE_IRQ 139 #define OPAL_XIVE_FREE_IRQ 140 #define OPAL_XIVE_SYNC 141 #define OPAL_XIVE_DUMP 142 #define OPAL_SENSOR_GROUP_CLEAR 156 #define OPAL_SENSOR_READ_U64 162 #define OPAL_SENSOR_GROUP_ENABLE 163 #define OPAL_HANDLE_HMI2 166 /* For OPAL_PCI_SET_PE */ #define OPAL_UNMAP_PE 0 #define OPAL_MAP_PE 1 #define OPAL_PCI_BUS_ANY 0 #define OPAL_PCI_BUS_3BITS 2 #define OPAL_PCI_BUS_4BITS 3 #define OPAL_PCI_BUS_5BITS 4 #define OPAL_PCI_BUS_6BITS 5 #define OPAL_PCI_BUS_7BITS 6 #define OPAL_PCI_BUS_ALL 7 /* Match bus number exactly */ #define OPAL_IGNORE_RID_DEVICE_NUMBER 0 #define OPAL_COMPARE_RID_DEVICE_NUMBER 1 #define OPAL_IGNORE_RID_FUNC_NUMBER 0 #define OPAL_COMPARE_RID_FUNC_NUMBER 1 #define OPAL_SUCCESS 0 #define OPAL_PARAMETER -1 #define OPAL_BUSY -2 #define OPAL_CLOSED -5 #define OPAL_HARDWARE -6 #define OPAL_UNSUPPORTED -7 #define OPAL_RESOURCE -10 #define OPAL_BUSY_EVENT -12 #define OPAL_ASYNC_COMPLETION -15 #define OPAL_EMPTY -16 #define OPAL_XIVE_PROVISIONING -31 #define OPAL_XIVE_FREE_ACTIVE -32 #define OPAL_TOKEN_ABSENT 0 #define OPAL_TOKEN_PRESENT 1 #define OPAL_EVENT_OPAL_INTERNAL 0x1 #define OPAL_EVENT_NVRAM 0x2 #define OPAL_EVENT_RTC 0x4 #define OPAL_EVENT_CONSOLE_INPUT 0x8 #define OPAL_EVENT_CONSOLE_OUTPUT 0x10 #define OPAL_EVENT_ERROR_LOG_AVAIL 0x20 #define OPAL_EVENT_ERROR_LOG 0x40 #define OPAL_EVENT_EPOW 0x80 #define OPAL_EVENT_LED_STATUS 0x100 #define OPAL_EVENT_PCI_ERROR 0x200 #define OPAL_EVENT_DUMP_AVAIL 0x400 #define OPAL_EVENT_MSG_PENDING 0x800 #define OPAL_HMI_FLAGS_TB_RESYNC (1ull << 0) #define OPAL_HMI_FLAGS_DEC_LOST (1ull << 1) #define OPAL_HMI_FLAGS_HDEC_LOST (1ull << 2) #define OPAL_HMI_FLAGS_TOD_TB_FAIL (1ull << 3) #define OPAL_HMI_FLAGS_NEW_EVENT (1ull << 63) #define OPAL_XIVE_XICS_MODE_EMU 0 #define OPAL_XIVE_XICS_MODE_EXP 1 #define OPAL_XIVE_VP_ENABLED 0x00000001 #define OPAL_XIVE_VP_SINGLE_ESCALATION 0x00000002 #define OPAL_XIVE_EQ_ENABLED 0x00000001 #define OPAL_XIVE_EQ_ALWAYS_NOTIFY 0x00000002 #define OPAL_XIVE_EQ_ESCALATE 0x00000004 struct opal_msg { uint32_t msg_type; uint32_t reserved; uint64_t params[8]; }; enum opal_msg_type { OPAL_MSG_ASYNC_COMP = 0, OPAL_MSG_MEM_ERR = 1, OPAL_MSG_EPOW = 2, OPAL_MSG_SHUTDOWN = 3, OPAL_MSG_HMI_EVT = 4, OPAL_MSG_DPO = 5, OPAL_MSG_PRD = 6, OPAL_MSG_OCC = 7, OPAL_MSG_TYPE_MAX, }; #define OPAL_IPMI_MSG_FORMAT_VERSION_1 1 struct opal_ipmi_msg { uint8_t version; uint8_t netfn; uint8_t cmd; uint8_t data[]; }; int opal_init_async_tokens(int); int opal_alloc_async_token(void); void opal_free_async_token(int); int opal_wait_completion(void *, uint64_t, int); typedef void (*opal_msg_handler_fn)(void *, struct opal_msg *); EVENTHANDLER_DECLARE(OPAL_ASYNC_COMP, opal_msg_handler_fn); EVENTHANDLER_DECLARE(OPAL_EPOW, opal_msg_handler_fn); EVENTHANDLER_DECLARE(OPAL_SHUTDOWN, opal_msg_handler_fn); EVENTHANDLER_DECLARE(OPAL_HMI_EVT, opal_msg_handler_fn); EVENTHANDLER_DECLARE(OPAL_DPO, opal_msg_handler_fn); EVENTHANDLER_DECLARE(OPAL_OCC, opal_msg_handler_fn); EVENTHANDLER_LIST_DECLARE(OPAL_ASYNC_COMP); EVENTHANDLER_LIST_DECLARE(OPAL_EPOW); EVENTHANDLER_LIST_DECLARE(OPAL_SHUTDOWN); EVENTHANDLER_LIST_DECLARE(OPAL_HMI_EVT); EVENTHANDLER_LIST_DECLARE(OPAL_DPO); EVENTHANDLER_LIST_DECLARE(OPAL_OCC); #endif Index: head/sys/powerpc/powernv/opal_flash.c =================================================================== --- head/sys/powerpc/powernv/opal_flash.c (revision 345974) +++ head/sys/powerpc/powernv/opal_flash.c (revision 345975) @@ -1,372 +1,386 @@ /*- * Copyright (c) 2019 Justin Hibbits * * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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 __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "opal.h" /* * OPAL System flash driver, using OPAL firmware calls to access the device. * * This just presents the base block interface. The fdt_slicer can be used on * top to present the partitions listed in the fdt. * * There are three OPAL methods used: OPAL_FLASH_READ, OPAL_FLASH_WRITE, and * OPAL_FLASH_ERASE. At the firmware layer, READ and WRITE can be on arbitrary * boundaries, but ERASE is only at flash-block-size block alignments and sizes. * To account for this, the following restrictions are in place: * * - Reads are on a 512-byte block boundary and size * - Writes and Erases are aligned and sized on flash-block-size bytes. * * In order to support the fdt_slicer we present a type attribute of * NAND::device. */ struct opalflash_softc { device_t sc_dev; struct mtx sc_mtx; struct disk *sc_disk; struct proc *sc_p; struct bio_queue_head sc_bio_queue; int sc_opal_id; }; #define OPALFLASH_LOCK(sc) mtx_lock(&(sc)->sc_mtx) #define OPALFLASH_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx) #define OPALFLASH_LOCK_INIT(sc) \ mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->sc_dev), \ "opalflash", MTX_DEF) #define FLASH_BLOCKSIZE 512 static int opalflash_probe(device_t); static int opalflash_attach(device_t); static device_method_t opalflash_methods[] = { /* Device interface */ DEVMETHOD(device_probe, opalflash_probe), DEVMETHOD(device_attach, opalflash_attach), DEVMETHOD_END }; static driver_t opalflash_driver = { "opalflash", opalflash_methods, sizeof(struct opalflash_softc) }; static devclass_t opalflash_devclass; DRIVER_MODULE(opalflash, opal, opalflash_driver, opalflash_devclass, 0, 0); /* GEOM Disk interfaces. */ static int opalflash_open(struct disk *dp) { return (0); } static int opalflash_close(struct disk *dp) { return (0); } static int opalflash_ioctl(struct disk *dp, u_long cmd, void *data, int fflag, struct thread *td) { return (EINVAL); } /* Handle the one attribute we need to play nice with geom_flashmap. */ static int opalflash_getattr(struct bio *bp) { struct opalflash_softc *sc; device_t dev; if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL) return (ENXIO); sc = bp->bio_disk->d_drv1; dev = sc->sc_dev; if (strcmp(bp->bio_attribute, "NAND::device") == 0) { if (bp->bio_length != sizeof(dev)) return (EFAULT); bcopy(&dev, bp->bio_data, sizeof(dev)); } else return (-1); return (0); } static void opalflash_strategy(struct bio *bp) { struct opalflash_softc *sc; sc = (struct opalflash_softc *)bp->bio_disk->d_drv1; OPALFLASH_LOCK(sc); bioq_disksort(&sc->sc_bio_queue, bp); wakeup(sc); OPALFLASH_UNLOCK(sc); } static int opalflash_read(struct opalflash_softc *sc, off_t off, caddr_t data, off_t count) { struct opal_msg msg; int rv, size, token; /* Ensure we write aligned to a full block size. */ if (off % sc->sc_disk->d_sectorsize != 0 || count % sc->sc_disk->d_sectorsize != 0) return (EIO); token = opal_alloc_async_token(); /* * Read one page at a time. It's not guaranteed that the buffer is * physically contiguous. */ + rv = 0; while (count > 0) { size = MIN(count, PAGE_SIZE); + size = MIN(size, PAGE_SIZE - ((u_long)data & PAGE_MASK)); rv = opal_call(OPAL_FLASH_READ, sc->sc_opal_id, off, vtophys(data), size, token); - if (rv == OPAL_ASYNC_COMPLETION) + if (rv == OPAL_ASYNC_COMPLETION) { rv = opal_wait_completion(&msg, sizeof(msg), token); + if (rv == OPAL_SUCCESS) + rv = msg.params[1]; + } if (rv != OPAL_SUCCESS) break; count -= size; off += size; + data += size; } opal_free_async_token(token); if (rv == OPAL_SUCCESS) rv = 0; else rv = EIO; return (rv); } static int opalflash_erase(struct opalflash_softc *sc, off_t off, off_t count) { struct opal_msg msg; int rv, token; /* Ensure we write aligned to a full block size. */ if (off % sc->sc_disk->d_stripesize != 0 || count % sc->sc_disk->d_stripesize != 0) return (EIO); token = opal_alloc_async_token(); rv = opal_call(OPAL_FLASH_ERASE, sc->sc_opal_id, off, count, token); - if (rv == OPAL_ASYNC_COMPLETION) + if (rv == OPAL_ASYNC_COMPLETION) { rv = opal_wait_completion(&msg, sizeof(msg), token); + if (rv == OPAL_SUCCESS) + rv = msg.params[1]; + } opal_free_async_token(token); if (rv == OPAL_SUCCESS) rv = 0; else rv = EIO; return (rv); } static int opalflash_write(struct opalflash_softc *sc, off_t off, caddr_t data, off_t count) { struct opal_msg msg; int rv, size, token; /* Ensure we write aligned to a full block size. */ if (off % sc->sc_disk->d_stripesize != 0 || count % sc->sc_disk->d_stripesize != 0) return (EIO); /* Erase the full block first, then write in page chunks. */ rv = opalflash_erase(sc, off, count); if (rv != 0) return (rv); token = opal_alloc_async_token(); /* * Write one page at a time. It's not guaranteed that the buffer is * physically contiguous. */ while (count > 0) { size = MIN(count, PAGE_SIZE); + size = MIN(size, PAGE_SIZE - ((u_long)data & PAGE_MASK)); rv = opal_call(OPAL_FLASH_WRITE, sc->sc_opal_id, off, vtophys(data), size, token); - if (rv == OPAL_ASYNC_COMPLETION) + if (rv == OPAL_ASYNC_COMPLETION) { rv = opal_wait_completion(&msg, sizeof(msg), token); + if (rv == OPAL_SUCCESS) + rv = msg.params[1]; + } if (rv != OPAL_SUCCESS) break; count -= size; off += size; + data += size; } opal_free_async_token(token); if (rv == OPAL_SUCCESS) rv = 0; else rv = EIO; return (rv); } /* Main flash handling task. */ static void opalflash_task(void *arg) { struct opalflash_softc *sc; struct bio *bp; device_t dev; sc = arg; for (;;) { dev = sc->sc_dev; OPALFLASH_LOCK(sc); do { bp = bioq_first(&sc->sc_bio_queue); if (bp == NULL) msleep(sc, &sc->sc_mtx, PRIBIO, "opalflash", 0); } while (bp == NULL); bioq_remove(&sc->sc_bio_queue, bp); OPALFLASH_UNLOCK(sc); switch (bp->bio_cmd) { case BIO_DELETE: bp->bio_error = opalflash_erase(sc, bp->bio_offset, bp->bio_bcount); break; case BIO_READ: bp->bio_error = opalflash_read(sc, bp->bio_offset, bp->bio_data, bp->bio_bcount); break; case BIO_WRITE: bp->bio_error = opalflash_write(sc, bp->bio_offset, bp->bio_data, bp->bio_bcount); break; default: bp->bio_error = EINVAL; } biodone(bp); } } /* Device driver interfaces. */ static int opalflash_probe(device_t dev) { if (!ofw_bus_is_compatible(dev, "ibm,opal-flash")) return (ENXIO); device_set_desc(dev, "OPAL System Flash"); return (BUS_PROBE_GENERIC); } static int opalflash_attach(device_t dev) { struct opalflash_softc *sc; phandle_t node; cell_t flash_blocksize, opal_id; uint32_t regs[2]; sc = device_get_softc(dev); sc->sc_dev = dev; node = ofw_bus_get_node(dev); OF_getencprop(node, "ibm,opal-id", &opal_id, sizeof(opal_id)); sc->sc_opal_id = opal_id; if (OF_getencprop(node, "ibm,flash-block-size", &flash_blocksize, sizeof(flash_blocksize)) < 0) { device_printf(dev, "Cannot determine flash block size.\n"); return (ENXIO); } OPALFLASH_LOCK_INIT(sc); if (OF_getencprop(node, "reg", regs, sizeof(regs)) < 0) { device_printf(dev, "Unable to get flash size.\n"); return (ENXIO); } sc->sc_disk = disk_alloc(); sc->sc_disk->d_name = "opalflash"; sc->sc_disk->d_open = opalflash_open; sc->sc_disk->d_close = opalflash_close; sc->sc_disk->d_strategy = opalflash_strategy; sc->sc_disk->d_ioctl = opalflash_ioctl; sc->sc_disk->d_getattr = opalflash_getattr; sc->sc_disk->d_drv1 = sc; sc->sc_disk->d_maxsize = DFLTPHYS; sc->sc_disk->d_mediasize = regs[1]; sc->sc_disk->d_unit = device_get_unit(sc->sc_dev); sc->sc_disk->d_sectorsize = FLASH_BLOCKSIZE; sc->sc_disk->d_stripesize = flash_blocksize; sc->sc_disk->d_dump = NULL; disk_create(sc->sc_disk, DISK_VERSION); bioq_init(&sc->sc_bio_queue); kproc_create(&opalflash_task, sc, &sc->sc_p, 0, 0, "task: OPAL Flash"); return (0); }