18d90ad90SDavid Gibson /* 28d90ad90SDavid Gibson * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator 38d90ad90SDavid Gibson * 48d90ad90SDavid Gibson * PAPR Inter-VM Logical Lan, aka ibmveth 58d90ad90SDavid Gibson * 68d90ad90SDavid Gibson * Copyright (c) 2010,2011 David Gibson, IBM Corporation. 78d90ad90SDavid Gibson * 88d90ad90SDavid Gibson * Permission is hereby granted, free of charge, to any person obtaining a copy 98d90ad90SDavid Gibson * of this software and associated documentation files (the "Software"), to deal 108d90ad90SDavid Gibson * in the Software without restriction, including without limitation the rights 118d90ad90SDavid Gibson * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 128d90ad90SDavid Gibson * copies of the Software, and to permit persons to whom the Software is 138d90ad90SDavid Gibson * furnished to do so, subject to the following conditions: 148d90ad90SDavid Gibson * 158d90ad90SDavid Gibson * The above copyright notice and this permission notice shall be included in 168d90ad90SDavid Gibson * all copies or substantial portions of the Software. 178d90ad90SDavid Gibson * 188d90ad90SDavid Gibson * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 198d90ad90SDavid Gibson * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 208d90ad90SDavid Gibson * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 218d90ad90SDavid Gibson * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 228d90ad90SDavid Gibson * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 238d90ad90SDavid Gibson * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 248d90ad90SDavid Gibson * THE SOFTWARE. 258d90ad90SDavid Gibson * 268d90ad90SDavid Gibson */ 270d75590dSPeter Maydell #include "qemu/osdep.h" 284771d756SPaolo Bonzini #include "qemu-common.h" 294771d756SPaolo Bonzini #include "cpu.h" 3083c9f4caSPaolo Bonzini #include "hw/hw.h" 311422e32dSPaolo Bonzini #include "net/net.h" 328d90ad90SDavid Gibson #include "hw/qdev.h" 330d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h" 340d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h" 35ad4f62d0SAlexey Kardashevskiy #include "sysemu/sysemu.h" 368d90ad90SDavid Gibson 378d90ad90SDavid Gibson #include <libfdt.h> 388d90ad90SDavid Gibson 398d90ad90SDavid Gibson #define ETH_ALEN 6 408d90ad90SDavid Gibson #define MAX_PACKET_SIZE 65536 418d90ad90SDavid Gibson 428d90ad90SDavid Gibson /*#define DEBUG*/ 438d90ad90SDavid Gibson 448d90ad90SDavid Gibson #ifdef DEBUG 45f6bda9cbSPeter Maydell #define DPRINTF(fmt...) do { fprintf(stderr, fmt); } while (0) 468d90ad90SDavid Gibson #else 47f6bda9cbSPeter Maydell #define DPRINTF(fmt...) 488d90ad90SDavid Gibson #endif 498d90ad90SDavid Gibson 50831e8822SThomas Huth /* Compatibility flags for migration */ 51831e8822SThomas Huth #define SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT 0 52831e8822SThomas Huth #define SPAPRVLAN_FLAG_RX_BUF_POOLS (1 << SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT) 53831e8822SThomas Huth 548d90ad90SDavid Gibson /* 558d90ad90SDavid Gibson * Virtual LAN device 568d90ad90SDavid Gibson */ 578d90ad90SDavid Gibson 588d90ad90SDavid Gibson typedef uint64_t vlan_bd_t; 598d90ad90SDavid Gibson 608d90ad90SDavid Gibson #define VLAN_BD_VALID 0x8000000000000000ULL 618d90ad90SDavid Gibson #define VLAN_BD_TOGGLE 0x4000000000000000ULL 628d90ad90SDavid Gibson #define VLAN_BD_NO_CSUM 0x0200000000000000ULL 638d90ad90SDavid Gibson #define VLAN_BD_CSUM_GOOD 0x0100000000000000ULL 648d90ad90SDavid Gibson #define VLAN_BD_LEN_MASK 0x00ffffff00000000ULL 658d90ad90SDavid Gibson #define VLAN_BD_LEN(bd) (((bd) & VLAN_BD_LEN_MASK) >> 32) 668d90ad90SDavid Gibson #define VLAN_BD_ADDR_MASK 0x00000000ffffffffULL 678d90ad90SDavid Gibson #define VLAN_BD_ADDR(bd) ((bd) & VLAN_BD_ADDR_MASK) 688d90ad90SDavid Gibson 698d90ad90SDavid Gibson #define VLAN_VALID_BD(addr, len) (VLAN_BD_VALID | \ 708d90ad90SDavid Gibson (((len) << 32) & VLAN_BD_LEN_MASK) | \ 718d90ad90SDavid Gibson (addr & VLAN_BD_ADDR_MASK)) 728d90ad90SDavid Gibson 738d90ad90SDavid Gibson #define VLAN_RXQC_TOGGLE 0x80 748d90ad90SDavid Gibson #define VLAN_RXQC_VALID 0x40 758d90ad90SDavid Gibson #define VLAN_RXQC_NO_CSUM 0x02 768d90ad90SDavid Gibson #define VLAN_RXQC_CSUM_GOOD 0x01 778d90ad90SDavid Gibson 788d90ad90SDavid Gibson #define VLAN_RQ_ALIGNMENT 16 798d90ad90SDavid Gibson #define VLAN_RXQ_BD_OFF 0 808d90ad90SDavid Gibson #define VLAN_FILTER_BD_OFF 8 818d90ad90SDavid Gibson #define VLAN_RX_BDS_OFF 16 82439ce140SAnton Blanchard /* 83439ce140SAnton Blanchard * The final 8 bytes of the buffer list is a counter of frames dropped 84439ce140SAnton Blanchard * because there was not a buffer in the buffer list capable of holding 85439ce140SAnton Blanchard * the frame. We must avoid it, or the operating system will report garbage 86439ce140SAnton Blanchard * for this statistic. 87439ce140SAnton Blanchard */ 88439ce140SAnton Blanchard #define VLAN_RX_BDS_LEN (SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF - 8) 89439ce140SAnton Blanchard #define VLAN_MAX_BUFS (VLAN_RX_BDS_LEN / 8) 908d90ad90SDavid Gibson 91fd506b4fSDavid Gibson #define TYPE_VIO_SPAPR_VLAN_DEVICE "spapr-vlan" 92fd506b4fSDavid Gibson #define VIO_SPAPR_VLAN_DEVICE(obj) \ 93fd506b4fSDavid Gibson OBJECT_CHECK(VIOsPAPRVLANDevice, (obj), TYPE_VIO_SPAPR_VLAN_DEVICE) 94fd506b4fSDavid Gibson 95831e8822SThomas Huth #define RX_POOL_MAX_BDS 4096 96831e8822SThomas Huth #define RX_MAX_POOLS 5 97831e8822SThomas Huth 98831e8822SThomas Huth typedef struct { 99831e8822SThomas Huth int32_t bufsize; 100831e8822SThomas Huth int32_t count; 101831e8822SThomas Huth vlan_bd_t bds[RX_POOL_MAX_BDS]; 102831e8822SThomas Huth } RxBufPool; 103831e8822SThomas Huth 1048d90ad90SDavid Gibson typedef struct VIOsPAPRVLANDevice { 1058d90ad90SDavid Gibson VIOsPAPRDevice sdev; 1068d90ad90SDavid Gibson NICConf nicconf; 1078d90ad90SDavid Gibson NICState *nic; 108686fefe4SDavid Gibson bool isopen; 109*cbd62f86SPaolo Bonzini hwaddr buf_list; 110686fefe4SDavid Gibson uint32_t add_buf_ptr, use_buf_ptr, rx_bufs; 111*cbd62f86SPaolo Bonzini hwaddr rxq_ptr; 112831e8822SThomas Huth uint32_t compat_flags; /* Compatability flags for migration */ 113831e8822SThomas Huth RxBufPool *rx_pool[RX_MAX_POOLS]; /* Receive buffer descriptor pools */ 1148d90ad90SDavid Gibson } VIOsPAPRVLANDevice; 1158d90ad90SDavid Gibson 1164e68f7a0SStefan Hajnoczi static int spapr_vlan_can_receive(NetClientState *nc) 1178d90ad90SDavid Gibson { 118cc1f0f45SJason Wang VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc); 1198d90ad90SDavid Gibson 1208d90ad90SDavid Gibson return (dev->isopen && dev->rx_bufs > 0); 1218d90ad90SDavid Gibson } 1228d90ad90SDavid Gibson 123d6f39fdfSThomas Huth /** 124831e8822SThomas Huth * Get buffer descriptor from one of our receive buffer pools 125831e8822SThomas Huth */ 126831e8822SThomas Huth static vlan_bd_t spapr_vlan_get_rx_bd_from_pool(VIOsPAPRVLANDevice *dev, 127831e8822SThomas Huth size_t size) 128831e8822SThomas Huth { 129831e8822SThomas Huth vlan_bd_t bd; 130831e8822SThomas Huth int pool; 131831e8822SThomas Huth 132831e8822SThomas Huth for (pool = 0; pool < RX_MAX_POOLS; pool++) { 133831e8822SThomas Huth if (dev->rx_pool[pool]->count > 0 && 134831e8822SThomas Huth dev->rx_pool[pool]->bufsize >= size + 8) { 135831e8822SThomas Huth break; 136831e8822SThomas Huth } 137831e8822SThomas Huth } 138831e8822SThomas Huth if (pool == RX_MAX_POOLS) { 139831e8822SThomas Huth /* Failed to find a suitable buffer */ 140831e8822SThomas Huth return 0; 141831e8822SThomas Huth } 142831e8822SThomas Huth 143831e8822SThomas Huth DPRINTF("Found buffer: pool=%d count=%d rxbufs=%d\n", pool, 144831e8822SThomas Huth dev->rx_pool[pool]->count, dev->rx_bufs); 145831e8822SThomas Huth 146831e8822SThomas Huth /* Remove the buffer from the pool */ 147831e8822SThomas Huth dev->rx_pool[pool]->count--; 148831e8822SThomas Huth bd = dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count]; 149831e8822SThomas Huth dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count] = 0; 150831e8822SThomas Huth 151831e8822SThomas Huth return bd; 152831e8822SThomas Huth } 153831e8822SThomas Huth 154831e8822SThomas Huth /** 155d6f39fdfSThomas Huth * Get buffer descriptor from the receive buffer list page that has been 156d6f39fdfSThomas Huth * supplied by the guest with the H_REGISTER_LOGICAL_LAN call 157d6f39fdfSThomas Huth */ 158d6f39fdfSThomas Huth static vlan_bd_t spapr_vlan_get_rx_bd_from_page(VIOsPAPRVLANDevice *dev, 159d6f39fdfSThomas Huth size_t size) 160d6f39fdfSThomas Huth { 161d6f39fdfSThomas Huth int buf_ptr = dev->use_buf_ptr; 162d6f39fdfSThomas Huth vlan_bd_t bd; 163d6f39fdfSThomas Huth 164d6f39fdfSThomas Huth do { 165d6f39fdfSThomas Huth buf_ptr += 8; 166d6f39fdfSThomas Huth if (buf_ptr >= VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF) { 167d6f39fdfSThomas Huth buf_ptr = VLAN_RX_BDS_OFF; 168d6f39fdfSThomas Huth } 169d6f39fdfSThomas Huth 170d6f39fdfSThomas Huth bd = vio_ldq(&dev->sdev, dev->buf_list + buf_ptr); 171d6f39fdfSThomas Huth DPRINTF("use_buf_ptr=%d bd=0x%016llx\n", 172d6f39fdfSThomas Huth buf_ptr, (unsigned long long)bd); 173d6f39fdfSThomas Huth } while ((!(bd & VLAN_BD_VALID) || VLAN_BD_LEN(bd) < size + 8) 174d6f39fdfSThomas Huth && buf_ptr != dev->use_buf_ptr); 175d6f39fdfSThomas Huth 176d6f39fdfSThomas Huth if (!(bd & VLAN_BD_VALID) || VLAN_BD_LEN(bd) < size + 8) { 177d6f39fdfSThomas Huth /* Failed to find a suitable buffer */ 178d6f39fdfSThomas Huth return 0; 179d6f39fdfSThomas Huth } 180d6f39fdfSThomas Huth 181d6f39fdfSThomas Huth /* Remove the buffer from the pool */ 182d6f39fdfSThomas Huth dev->use_buf_ptr = buf_ptr; 183d6f39fdfSThomas Huth vio_stq(&dev->sdev, dev->buf_list + dev->use_buf_ptr, 0); 184d6f39fdfSThomas Huth 185d6f39fdfSThomas Huth DPRINTF("Found buffer: ptr=%d rxbufs=%d\n", dev->use_buf_ptr, dev->rx_bufs); 186d6f39fdfSThomas Huth 187d6f39fdfSThomas Huth return bd; 188d6f39fdfSThomas Huth } 189d6f39fdfSThomas Huth 1904e68f7a0SStefan Hajnoczi static ssize_t spapr_vlan_receive(NetClientState *nc, const uint8_t *buf, 1918d90ad90SDavid Gibson size_t size) 1928d90ad90SDavid Gibson { 193fd506b4fSDavid Gibson VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc); 194fd506b4fSDavid Gibson VIOsPAPRDevice *sdev = VIO_SPAPR_DEVICE(dev); 195ad0ebb91SDavid Gibson vlan_bd_t rxq_bd = vio_ldq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF); 1968d90ad90SDavid Gibson vlan_bd_t bd; 1978d90ad90SDavid Gibson uint64_t handle; 1988d90ad90SDavid Gibson uint8_t control; 1998d90ad90SDavid Gibson 200f6bda9cbSPeter Maydell DPRINTF("spapr_vlan_receive() [%s] rx_bufs=%d\n", sdev->qdev.id, 2018d90ad90SDavid Gibson dev->rx_bufs); 2028d90ad90SDavid Gibson 2038d90ad90SDavid Gibson if (!dev->isopen) { 2048d90ad90SDavid Gibson return -1; 2058d90ad90SDavid Gibson } 2068d90ad90SDavid Gibson 2078d90ad90SDavid Gibson if (!dev->rx_bufs) { 2088d90ad90SDavid Gibson return -1; 2098d90ad90SDavid Gibson } 2108d90ad90SDavid Gibson 211831e8822SThomas Huth if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) { 212831e8822SThomas Huth bd = spapr_vlan_get_rx_bd_from_pool(dev, size); 213831e8822SThomas Huth } else { 214d6f39fdfSThomas Huth bd = spapr_vlan_get_rx_bd_from_page(dev, size); 215831e8822SThomas Huth } 216d6f39fdfSThomas Huth if (!bd) { 2178d90ad90SDavid Gibson return -1; 2188d90ad90SDavid Gibson } 2198d90ad90SDavid Gibson 2208d90ad90SDavid Gibson dev->rx_bufs--; 2218d90ad90SDavid Gibson 2228d90ad90SDavid Gibson /* Transfer the packet data */ 223ad0ebb91SDavid Gibson if (spapr_vio_dma_write(sdev, VLAN_BD_ADDR(bd) + 8, buf, size) < 0) { 2248d90ad90SDavid Gibson return -1; 2258d90ad90SDavid Gibson } 2268d90ad90SDavid Gibson 227f6bda9cbSPeter Maydell DPRINTF("spapr_vlan_receive: DMA write completed\n"); 2288d90ad90SDavid Gibson 2298d90ad90SDavid Gibson /* Update the receive queue */ 2308d90ad90SDavid Gibson control = VLAN_RXQC_TOGGLE | VLAN_RXQC_VALID; 2318d90ad90SDavid Gibson if (rxq_bd & VLAN_BD_TOGGLE) { 2328d90ad90SDavid Gibson control ^= VLAN_RXQC_TOGGLE; 2338d90ad90SDavid Gibson } 2348d90ad90SDavid Gibson 235ad0ebb91SDavid Gibson handle = vio_ldq(sdev, VLAN_BD_ADDR(bd)); 236ad0ebb91SDavid Gibson vio_stq(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 8, handle); 237ad0ebb91SDavid Gibson vio_stl(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 4, size); 238ad0ebb91SDavid Gibson vio_sth(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 2, 8); 239ad0ebb91SDavid Gibson vio_stb(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr, control); 2408d90ad90SDavid Gibson 241f6bda9cbSPeter Maydell DPRINTF("wrote rxq entry (ptr=0x%llx): 0x%016llx 0x%016llx\n", 2428d90ad90SDavid Gibson (unsigned long long)dev->rxq_ptr, 243ad0ebb91SDavid Gibson (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) + 2448d90ad90SDavid Gibson dev->rxq_ptr), 245ad0ebb91SDavid Gibson (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) + 2468d90ad90SDavid Gibson dev->rxq_ptr + 8)); 2478d90ad90SDavid Gibson 2488d90ad90SDavid Gibson dev->rxq_ptr += 16; 2498d90ad90SDavid Gibson if (dev->rxq_ptr >= VLAN_BD_LEN(rxq_bd)) { 2508d90ad90SDavid Gibson dev->rxq_ptr = 0; 251ad0ebb91SDavid Gibson vio_stq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF, rxq_bd ^ VLAN_BD_TOGGLE); 2528d90ad90SDavid Gibson } 2538d90ad90SDavid Gibson 2548d90ad90SDavid Gibson if (sdev->signal_state & 1) { 255a307d594SAlexey Kardashevskiy qemu_irq_pulse(spapr_vio_qirq(sdev)); 2568d90ad90SDavid Gibson } 2578d90ad90SDavid Gibson 2588d90ad90SDavid Gibson return size; 2598d90ad90SDavid Gibson } 2608d90ad90SDavid Gibson 2618d90ad90SDavid Gibson static NetClientInfo net_spapr_vlan_info = { 2622be64a68SLaszlo Ersek .type = NET_CLIENT_OPTIONS_KIND_NIC, 2638d90ad90SDavid Gibson .size = sizeof(NICState), 2648d90ad90SDavid Gibson .can_receive = spapr_vlan_can_receive, 2658d90ad90SDavid Gibson .receive = spapr_vlan_receive, 2668d90ad90SDavid Gibson }; 2678d90ad90SDavid Gibson 268831e8822SThomas Huth static void spapr_vlan_reset_rx_pool(RxBufPool *rxp) 269831e8822SThomas Huth { 270831e8822SThomas Huth /* 271831e8822SThomas Huth * Use INT_MAX as bufsize so that unused buffers are moved to the end 272831e8822SThomas Huth * of the list during the qsort in spapr_vlan_add_rxbuf_to_pool() later. 273831e8822SThomas Huth */ 274831e8822SThomas Huth rxp->bufsize = INT_MAX; 275831e8822SThomas Huth rxp->count = 0; 276831e8822SThomas Huth memset(rxp->bds, 0, sizeof(rxp->bds)); 277831e8822SThomas Huth } 278831e8822SThomas Huth 279c17491b6SDavid Gibson static void spapr_vlan_reset(VIOsPAPRDevice *sdev) 280c17491b6SDavid Gibson { 281fd506b4fSDavid Gibson VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); 282831e8822SThomas Huth int i; 283c17491b6SDavid Gibson 284c17491b6SDavid Gibson dev->buf_list = 0; 285c17491b6SDavid Gibson dev->rx_bufs = 0; 286c17491b6SDavid Gibson dev->isopen = 0; 287831e8822SThomas Huth 288831e8822SThomas Huth if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) { 289831e8822SThomas Huth for (i = 0; i < RX_MAX_POOLS; i++) { 290831e8822SThomas Huth spapr_vlan_reset_rx_pool(dev->rx_pool[i]); 291831e8822SThomas Huth } 292831e8822SThomas Huth } 293c17491b6SDavid Gibson } 294c17491b6SDavid Gibson 29528b07e73SMarkus Armbruster static void spapr_vlan_realize(VIOsPAPRDevice *sdev, Error **errp) 2968d90ad90SDavid Gibson { 297fd506b4fSDavid Gibson VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); 2988d90ad90SDavid Gibson 2998d90ad90SDavid Gibson qemu_macaddr_default_if_unset(&dev->nicconf.macaddr); 3008d90ad90SDavid Gibson 3018d90ad90SDavid Gibson dev->nic = qemu_new_nic(&net_spapr_vlan_info, &dev->nicconf, 302f79f2bfcSAnthony Liguori object_get_typename(OBJECT(sdev)), sdev->qdev.id, dev); 303b356f76dSJason Wang qemu_format_nic_info_str(qemu_get_queue(dev->nic), dev->nicconf.macaddr.a); 3048d90ad90SDavid Gibson } 3058d90ad90SDavid Gibson 306dfe79cf2SGonglei static void spapr_vlan_instance_init(Object *obj) 307dfe79cf2SGonglei { 308dfe79cf2SGonglei VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(obj); 309831e8822SThomas Huth int i; 310dfe79cf2SGonglei 311dfe79cf2SGonglei device_add_bootindex_property(obj, &dev->nicconf.bootindex, 312dfe79cf2SGonglei "bootindex", "", 313dfe79cf2SGonglei DEVICE(dev), NULL); 314831e8822SThomas Huth 315831e8822SThomas Huth if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) { 316831e8822SThomas Huth for (i = 0; i < RX_MAX_POOLS; i++) { 317831e8822SThomas Huth dev->rx_pool[i] = g_new(RxBufPool, 1); 318831e8822SThomas Huth spapr_vlan_reset_rx_pool(dev->rx_pool[i]); 319831e8822SThomas Huth } 320831e8822SThomas Huth } 321831e8822SThomas Huth } 322831e8822SThomas Huth 323831e8822SThomas Huth static void spapr_vlan_instance_finalize(Object *obj) 324831e8822SThomas Huth { 325831e8822SThomas Huth VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(obj); 326831e8822SThomas Huth int i; 327831e8822SThomas Huth 328831e8822SThomas Huth if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) { 329831e8822SThomas Huth for (i = 0; i < RX_MAX_POOLS; i++) { 330831e8822SThomas Huth g_free(dev->rx_pool[i]); 331831e8822SThomas Huth dev->rx_pool[i] = NULL; 332831e8822SThomas Huth } 333831e8822SThomas Huth } 334dfe79cf2SGonglei } 335dfe79cf2SGonglei 336d601fac4SDavid Gibson void spapr_vlan_create(VIOsPAPRBus *bus, NICInfo *nd) 3378d90ad90SDavid Gibson { 3388d90ad90SDavid Gibson DeviceState *dev; 3398d90ad90SDavid Gibson 3408d90ad90SDavid Gibson dev = qdev_create(&bus->bus, "spapr-vlan"); 3418d90ad90SDavid Gibson 3428d90ad90SDavid Gibson qdev_set_nic_properties(dev, nd); 3438d90ad90SDavid Gibson 3448d90ad90SDavid Gibson qdev_init_nofail(dev); 3458d90ad90SDavid Gibson } 3468d90ad90SDavid Gibson 3478d90ad90SDavid Gibson static int spapr_vlan_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off) 3488d90ad90SDavid Gibson { 349fd506b4fSDavid Gibson VIOsPAPRVLANDevice *vdev = VIO_SPAPR_VLAN_DEVICE(dev); 3508d90ad90SDavid Gibson uint8_t padded_mac[8] = {0, 0}; 3518d90ad90SDavid Gibson int ret; 3528d90ad90SDavid Gibson 3538d90ad90SDavid Gibson /* Some old phyp versions give the mac address in an 8-byte 3548d90ad90SDavid Gibson * property. The kernel driver has an insane workaround for this; 3558d90ad90SDavid Gibson * rather than doing the obvious thing and checking the property 3568d90ad90SDavid Gibson * length, it checks whether the first byte has 0b10 in the low 3578d90ad90SDavid Gibson * bits. If a correct 6-byte property has a different first byte 3588d90ad90SDavid Gibson * the kernel will get the wrong mac address, overrunning its 3598d90ad90SDavid Gibson * buffer in the process (read only, thank goodness). 3608d90ad90SDavid Gibson * 3618d90ad90SDavid Gibson * Here we workaround the kernel workaround by always supplying an 3628d90ad90SDavid Gibson * 8-byte property, with the mac address in the last six bytes */ 3638d90ad90SDavid Gibson memcpy(&padded_mac[2], &vdev->nicconf.macaddr, ETH_ALEN); 3648d90ad90SDavid Gibson ret = fdt_setprop(fdt, node_off, "local-mac-address", 3658d90ad90SDavid Gibson padded_mac, sizeof(padded_mac)); 3668d90ad90SDavid Gibson if (ret < 0) { 3678d90ad90SDavid Gibson return ret; 3688d90ad90SDavid Gibson } 3698d90ad90SDavid Gibson 3708d90ad90SDavid Gibson ret = fdt_setprop_cell(fdt, node_off, "ibm,mac-address-filters", 0); 3718d90ad90SDavid Gibson if (ret < 0) { 3728d90ad90SDavid Gibson return ret; 3738d90ad90SDavid Gibson } 3748d90ad90SDavid Gibson 3758d90ad90SDavid Gibson return 0; 3768d90ad90SDavid Gibson } 3778d90ad90SDavid Gibson 3788d90ad90SDavid Gibson static int check_bd(VIOsPAPRVLANDevice *dev, vlan_bd_t bd, 3798d90ad90SDavid Gibson target_ulong alignment) 3808d90ad90SDavid Gibson { 3818d90ad90SDavid Gibson if ((VLAN_BD_ADDR(bd) % alignment) 3828d90ad90SDavid Gibson || (VLAN_BD_LEN(bd) % alignment)) { 3838d90ad90SDavid Gibson return -1; 3848d90ad90SDavid Gibson } 3858d90ad90SDavid Gibson 386ad0ebb91SDavid Gibson if (!spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd), 387ad0ebb91SDavid Gibson VLAN_BD_LEN(bd), DMA_DIRECTION_FROM_DEVICE) 388ad0ebb91SDavid Gibson || !spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd), 389ad0ebb91SDavid Gibson VLAN_BD_LEN(bd), DMA_DIRECTION_TO_DEVICE)) { 3908d90ad90SDavid Gibson return -1; 3918d90ad90SDavid Gibson } 3928d90ad90SDavid Gibson 3938d90ad90SDavid Gibson return 0; 3948d90ad90SDavid Gibson } 3958d90ad90SDavid Gibson 396b13ce26dSAndreas Färber static target_ulong h_register_logical_lan(PowerPCCPU *cpu, 39728e02042SDavid Gibson sPAPRMachineState *spapr, 3988d90ad90SDavid Gibson target_ulong opcode, 3998d90ad90SDavid Gibson target_ulong *args) 4008d90ad90SDavid Gibson { 4018d90ad90SDavid Gibson target_ulong reg = args[0]; 4028d90ad90SDavid Gibson target_ulong buf_list = args[1]; 4038d90ad90SDavid Gibson target_ulong rec_queue = args[2]; 4048d90ad90SDavid Gibson target_ulong filter_list = args[3]; 4058d90ad90SDavid Gibson VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg); 406fd506b4fSDavid Gibson VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); 4078d90ad90SDavid Gibson vlan_bd_t filter_list_bd; 4088d90ad90SDavid Gibson 4098d90ad90SDavid Gibson if (!dev) { 4108d90ad90SDavid Gibson return H_PARAMETER; 4118d90ad90SDavid Gibson } 4128d90ad90SDavid Gibson 4138d90ad90SDavid Gibson if (dev->isopen) { 4148d90ad90SDavid Gibson hcall_dprintf("H_REGISTER_LOGICAL_LAN called twice without " 4158d90ad90SDavid Gibson "H_FREE_LOGICAL_LAN\n"); 4168d90ad90SDavid Gibson return H_RESOURCE; 4178d90ad90SDavid Gibson } 4188d90ad90SDavid Gibson 419ad0ebb91SDavid Gibson if (check_bd(dev, VLAN_VALID_BD(buf_list, SPAPR_TCE_PAGE_SIZE), 420ad0ebb91SDavid Gibson SPAPR_TCE_PAGE_SIZE) < 0) { 421d9599c92SDavid Gibson hcall_dprintf("Bad buf_list 0x" TARGET_FMT_lx "\n", buf_list); 4228d90ad90SDavid Gibson return H_PARAMETER; 4238d90ad90SDavid Gibson } 4248d90ad90SDavid Gibson 425ad0ebb91SDavid Gibson filter_list_bd = VLAN_VALID_BD(filter_list, SPAPR_TCE_PAGE_SIZE); 426ad0ebb91SDavid Gibson if (check_bd(dev, filter_list_bd, SPAPR_TCE_PAGE_SIZE) < 0) { 427d9599c92SDavid Gibson hcall_dprintf("Bad filter_list 0x" TARGET_FMT_lx "\n", filter_list); 4288d90ad90SDavid Gibson return H_PARAMETER; 4298d90ad90SDavid Gibson } 4308d90ad90SDavid Gibson 4318d90ad90SDavid Gibson if (!(rec_queue & VLAN_BD_VALID) 4328d90ad90SDavid Gibson || (check_bd(dev, rec_queue, VLAN_RQ_ALIGNMENT) < 0)) { 433d9599c92SDavid Gibson hcall_dprintf("Bad receive queue\n"); 4348d90ad90SDavid Gibson return H_PARAMETER; 4358d90ad90SDavid Gibson } 4368d90ad90SDavid Gibson 4378d90ad90SDavid Gibson dev->buf_list = buf_list; 4388d90ad90SDavid Gibson sdev->signal_state = 0; 4398d90ad90SDavid Gibson 4408d90ad90SDavid Gibson rec_queue &= ~VLAN_BD_TOGGLE; 4418d90ad90SDavid Gibson 4428d90ad90SDavid Gibson /* Initialize the buffer list */ 443ad0ebb91SDavid Gibson vio_stq(sdev, buf_list, rec_queue); 444ad0ebb91SDavid Gibson vio_stq(sdev, buf_list + 8, filter_list_bd); 445ad0ebb91SDavid Gibson spapr_vio_dma_set(sdev, buf_list + VLAN_RX_BDS_OFF, 0, 446ad0ebb91SDavid Gibson SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF); 4478d90ad90SDavid Gibson dev->add_buf_ptr = VLAN_RX_BDS_OFF - 8; 4488d90ad90SDavid Gibson dev->use_buf_ptr = VLAN_RX_BDS_OFF - 8; 4498d90ad90SDavid Gibson dev->rx_bufs = 0; 4508d90ad90SDavid Gibson dev->rxq_ptr = 0; 4518d90ad90SDavid Gibson 4528d90ad90SDavid Gibson /* Initialize the receive queue */ 453ad0ebb91SDavid Gibson spapr_vio_dma_set(sdev, VLAN_BD_ADDR(rec_queue), 0, VLAN_BD_LEN(rec_queue)); 4548d90ad90SDavid Gibson 4558d90ad90SDavid Gibson dev->isopen = 1; 456e0ff466cSAlexey Kardashevskiy qemu_flush_queued_packets(qemu_get_queue(dev->nic)); 457e0ff466cSAlexey Kardashevskiy 4588d90ad90SDavid Gibson return H_SUCCESS; 4598d90ad90SDavid Gibson } 4608d90ad90SDavid Gibson 4618d90ad90SDavid Gibson 46228e02042SDavid Gibson static target_ulong h_free_logical_lan(PowerPCCPU *cpu, 46328e02042SDavid Gibson sPAPRMachineState *spapr, 4648d90ad90SDavid Gibson target_ulong opcode, target_ulong *args) 4658d90ad90SDavid Gibson { 4668d90ad90SDavid Gibson target_ulong reg = args[0]; 4678d90ad90SDavid Gibson VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg); 468fd506b4fSDavid Gibson VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); 4698d90ad90SDavid Gibson 4708d90ad90SDavid Gibson if (!dev) { 4718d90ad90SDavid Gibson return H_PARAMETER; 4728d90ad90SDavid Gibson } 4738d90ad90SDavid Gibson 4748d90ad90SDavid Gibson if (!dev->isopen) { 4758d90ad90SDavid Gibson hcall_dprintf("H_FREE_LOGICAL_LAN called without " 4768d90ad90SDavid Gibson "H_REGISTER_LOGICAL_LAN\n"); 4778d90ad90SDavid Gibson return H_RESOURCE; 4788d90ad90SDavid Gibson } 4798d90ad90SDavid Gibson 480c17491b6SDavid Gibson spapr_vlan_reset(sdev); 4818d90ad90SDavid Gibson return H_SUCCESS; 4828d90ad90SDavid Gibson } 4838d90ad90SDavid Gibson 484831e8822SThomas Huth /** 485831e8822SThomas Huth * Used for qsort, this function compares two RxBufPools by size. 486831e8822SThomas Huth */ 487831e8822SThomas Huth static int rx_pool_size_compare(const void *p1, const void *p2) 488831e8822SThomas Huth { 489831e8822SThomas Huth const RxBufPool *pool1 = *(RxBufPool **)p1; 490831e8822SThomas Huth const RxBufPool *pool2 = *(RxBufPool **)p2; 491831e8822SThomas Huth 492831e8822SThomas Huth if (pool1->bufsize < pool2->bufsize) { 493831e8822SThomas Huth return -1; 494831e8822SThomas Huth } 495831e8822SThomas Huth return pool1->bufsize > pool2->bufsize; 496831e8822SThomas Huth } 497831e8822SThomas Huth 498831e8822SThomas Huth /** 499831e8822SThomas Huth * Search for a matching buffer pool with exact matching size, 500831e8822SThomas Huth * or return -1 if no matching pool has been found. 501831e8822SThomas Huth */ 502831e8822SThomas Huth static int spapr_vlan_get_rx_pool_id(VIOsPAPRVLANDevice *dev, int size) 503831e8822SThomas Huth { 504831e8822SThomas Huth int pool; 505831e8822SThomas Huth 506831e8822SThomas Huth for (pool = 0; pool < RX_MAX_POOLS; pool++) { 507831e8822SThomas Huth if (dev->rx_pool[pool]->bufsize == size) { 508831e8822SThomas Huth return pool; 509831e8822SThomas Huth } 510831e8822SThomas Huth } 511831e8822SThomas Huth 512831e8822SThomas Huth return -1; 513831e8822SThomas Huth } 514831e8822SThomas Huth 515831e8822SThomas Huth /** 516831e8822SThomas Huth * Enqueuing receive buffer by adding it to one of our receive buffer pools 517831e8822SThomas Huth */ 518831e8822SThomas Huth static target_long spapr_vlan_add_rxbuf_to_pool(VIOsPAPRVLANDevice *dev, 519831e8822SThomas Huth target_ulong buf) 520831e8822SThomas Huth { 521831e8822SThomas Huth int size = VLAN_BD_LEN(buf); 522831e8822SThomas Huth int pool; 523831e8822SThomas Huth 524831e8822SThomas Huth pool = spapr_vlan_get_rx_pool_id(dev, size); 525831e8822SThomas Huth if (pool < 0) { 526831e8822SThomas Huth /* 527831e8822SThomas Huth * No matching pool found? Try to use a new one. If the guest used all 528831e8822SThomas Huth * pools before, but changed the size of one pool inbetween, we might 529831e8822SThomas Huth * need to recycle that pool here (if it's empty already). Thus scan 530831e8822SThomas Huth * all buffer pools now, starting with the last (likely empty) one. 531831e8822SThomas Huth */ 532831e8822SThomas Huth for (pool = RX_MAX_POOLS - 1; pool >= 0 ; pool--) { 533831e8822SThomas Huth if (dev->rx_pool[pool]->count == 0) { 534831e8822SThomas Huth dev->rx_pool[pool]->bufsize = size; 535831e8822SThomas Huth /* 536831e8822SThomas Huth * Sort pools by size so that spapr_vlan_receive() 537831e8822SThomas Huth * can later find the smallest buffer pool easily. 538831e8822SThomas Huth */ 539831e8822SThomas Huth qsort(dev->rx_pool, RX_MAX_POOLS, sizeof(dev->rx_pool[0]), 540831e8822SThomas Huth rx_pool_size_compare); 541831e8822SThomas Huth pool = spapr_vlan_get_rx_pool_id(dev, size); 542831e8822SThomas Huth DPRINTF("created RX pool %d for size %lld\n", pool, 543831e8822SThomas Huth VLAN_BD_LEN(buf)); 544831e8822SThomas Huth break; 545831e8822SThomas Huth } 546831e8822SThomas Huth } 547831e8822SThomas Huth } 548831e8822SThomas Huth /* Still no usable pool? Give up */ 549831e8822SThomas Huth if (pool < 0 || dev->rx_pool[pool]->count >= RX_POOL_MAX_BDS) { 550831e8822SThomas Huth return H_RESOURCE; 551831e8822SThomas Huth } 552831e8822SThomas Huth 553831e8822SThomas Huth DPRINTF("h_add_llan_buf(): Add buf using pool %i (size %lli, count=%i)\n", 554831e8822SThomas Huth pool, VLAN_BD_LEN(buf), dev->rx_pool[pool]->count); 555831e8822SThomas Huth 556831e8822SThomas Huth dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count++] = buf; 557831e8822SThomas Huth 558831e8822SThomas Huth return 0; 559831e8822SThomas Huth } 560831e8822SThomas Huth 561831e8822SThomas Huth /** 562831e8822SThomas Huth * This is the old way of enqueuing receive buffers: Add it to the rx queue 563831e8822SThomas Huth * page that has been supplied by the guest (which is quite limited in size). 564831e8822SThomas Huth */ 565d6f39fdfSThomas Huth static target_long spapr_vlan_add_rxbuf_to_page(VIOsPAPRVLANDevice *dev, 566d6f39fdfSThomas Huth target_ulong buf) 567d6f39fdfSThomas Huth { 568d6f39fdfSThomas Huth vlan_bd_t bd; 569d6f39fdfSThomas Huth 570d6f39fdfSThomas Huth if (dev->rx_bufs >= VLAN_MAX_BUFS) { 571d6f39fdfSThomas Huth return H_RESOURCE; 572d6f39fdfSThomas Huth } 573d6f39fdfSThomas Huth 574d6f39fdfSThomas Huth do { 575d6f39fdfSThomas Huth dev->add_buf_ptr += 8; 576d6f39fdfSThomas Huth if (dev->add_buf_ptr >= VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF) { 577d6f39fdfSThomas Huth dev->add_buf_ptr = VLAN_RX_BDS_OFF; 578d6f39fdfSThomas Huth } 579d6f39fdfSThomas Huth 580d6f39fdfSThomas Huth bd = vio_ldq(&dev->sdev, dev->buf_list + dev->add_buf_ptr); 581d6f39fdfSThomas Huth } while (bd & VLAN_BD_VALID); 582d6f39fdfSThomas Huth 583d6f39fdfSThomas Huth vio_stq(&dev->sdev, dev->buf_list + dev->add_buf_ptr, buf); 584d6f39fdfSThomas Huth 585d6f39fdfSThomas Huth DPRINTF("h_add_llan_buf(): Added buf ptr=%d rx_bufs=%d bd=0x%016llx\n", 586d6f39fdfSThomas Huth dev->add_buf_ptr, dev->rx_bufs, (unsigned long long)buf); 587d6f39fdfSThomas Huth 588d6f39fdfSThomas Huth return 0; 589d6f39fdfSThomas Huth } 590d6f39fdfSThomas Huth 591b13ce26dSAndreas Färber static target_ulong h_add_logical_lan_buffer(PowerPCCPU *cpu, 59228e02042SDavid Gibson sPAPRMachineState *spapr, 5938d90ad90SDavid Gibson target_ulong opcode, 5948d90ad90SDavid Gibson target_ulong *args) 5958d90ad90SDavid Gibson { 5968d90ad90SDavid Gibson target_ulong reg = args[0]; 5978d90ad90SDavid Gibson target_ulong buf = args[1]; 5988d90ad90SDavid Gibson VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg); 599fd506b4fSDavid Gibson VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); 600d6f39fdfSThomas Huth target_long ret; 6018d90ad90SDavid Gibson 602f6bda9cbSPeter Maydell DPRINTF("H_ADD_LOGICAL_LAN_BUFFER(0x" TARGET_FMT_lx 6038d90ad90SDavid Gibson ", 0x" TARGET_FMT_lx ")\n", reg, buf); 6048d90ad90SDavid Gibson 6058d90ad90SDavid Gibson if (!sdev) { 606d9599c92SDavid Gibson hcall_dprintf("Bad device\n"); 6078d90ad90SDavid Gibson return H_PARAMETER; 6088d90ad90SDavid Gibson } 6098d90ad90SDavid Gibson 6108d90ad90SDavid Gibson if ((check_bd(dev, buf, 4) < 0) 6118d90ad90SDavid Gibson || (VLAN_BD_LEN(buf) < 16)) { 612d9599c92SDavid Gibson hcall_dprintf("Bad buffer enqueued\n"); 6138d90ad90SDavid Gibson return H_PARAMETER; 6148d90ad90SDavid Gibson } 6158d90ad90SDavid Gibson 616d6f39fdfSThomas Huth if (!dev->isopen) { 6178d90ad90SDavid Gibson return H_RESOURCE; 6188d90ad90SDavid Gibson } 6198d90ad90SDavid Gibson 620831e8822SThomas Huth if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) { 621831e8822SThomas Huth ret = spapr_vlan_add_rxbuf_to_pool(dev, buf); 622831e8822SThomas Huth } else { 623d6f39fdfSThomas Huth ret = spapr_vlan_add_rxbuf_to_page(dev, buf); 624831e8822SThomas Huth } 625d6f39fdfSThomas Huth if (ret) { 626d6f39fdfSThomas Huth return ret; 6278d90ad90SDavid Gibson } 6288d90ad90SDavid Gibson 6298d90ad90SDavid Gibson dev->rx_bufs++; 6308d90ad90SDavid Gibson 6310a61f3b4SAlexey Kardashevskiy qemu_flush_queued_packets(qemu_get_queue(dev->nic)); 6320a61f3b4SAlexey Kardashevskiy 6338d90ad90SDavid Gibson return H_SUCCESS; 6348d90ad90SDavid Gibson } 6358d90ad90SDavid Gibson 63628e02042SDavid Gibson static target_ulong h_send_logical_lan(PowerPCCPU *cpu, 63728e02042SDavid Gibson sPAPRMachineState *spapr, 6388d90ad90SDavid Gibson target_ulong opcode, target_ulong *args) 6398d90ad90SDavid Gibson { 6408d90ad90SDavid Gibson target_ulong reg = args[0]; 6418d90ad90SDavid Gibson target_ulong *bufs = args + 1; 6428d90ad90SDavid Gibson target_ulong continue_token = args[7]; 6438d90ad90SDavid Gibson VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg); 644fd506b4fSDavid Gibson VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); 6458d90ad90SDavid Gibson unsigned total_len; 6468d90ad90SDavid Gibson uint8_t *lbuf, *p; 6478d90ad90SDavid Gibson int i, nbufs; 6488d90ad90SDavid Gibson int ret; 6498d90ad90SDavid Gibson 650f6bda9cbSPeter Maydell DPRINTF("H_SEND_LOGICAL_LAN(0x" TARGET_FMT_lx ", <bufs>, 0x" 6518d90ad90SDavid Gibson TARGET_FMT_lx ")\n", reg, continue_token); 6528d90ad90SDavid Gibson 6538d90ad90SDavid Gibson if (!sdev) { 6548d90ad90SDavid Gibson return H_PARAMETER; 6558d90ad90SDavid Gibson } 6568d90ad90SDavid Gibson 657f6bda9cbSPeter Maydell DPRINTF("rxbufs = %d\n", dev->rx_bufs); 6588d90ad90SDavid Gibson 6598d90ad90SDavid Gibson if (!dev->isopen) { 6608d90ad90SDavid Gibson return H_DROPPED; 6618d90ad90SDavid Gibson } 6628d90ad90SDavid Gibson 6638d90ad90SDavid Gibson if (continue_token) { 6648d90ad90SDavid Gibson return H_HARDWARE; /* FIXME actually handle this */ 6658d90ad90SDavid Gibson } 6668d90ad90SDavid Gibson 6678d90ad90SDavid Gibson total_len = 0; 6688d90ad90SDavid Gibson for (i = 0; i < 6; i++) { 669f6bda9cbSPeter Maydell DPRINTF(" buf desc: 0x" TARGET_FMT_lx "\n", bufs[i]); 6708d90ad90SDavid Gibson if (!(bufs[i] & VLAN_BD_VALID)) { 6718d90ad90SDavid Gibson break; 6728d90ad90SDavid Gibson } 6738d90ad90SDavid Gibson total_len += VLAN_BD_LEN(bufs[i]); 6748d90ad90SDavid Gibson } 6758d90ad90SDavid Gibson 6768d90ad90SDavid Gibson nbufs = i; 677f6bda9cbSPeter Maydell DPRINTF("h_send_logical_lan() %d buffers, total length 0x%x\n", 6788d90ad90SDavid Gibson nbufs, total_len); 6798d90ad90SDavid Gibson 6808d90ad90SDavid Gibson if (total_len == 0) { 6818d90ad90SDavid Gibson return H_SUCCESS; 6828d90ad90SDavid Gibson } 6838d90ad90SDavid Gibson 6848d90ad90SDavid Gibson if (total_len > MAX_PACKET_SIZE) { 6858d90ad90SDavid Gibson /* Don't let the guest force too large an allocation */ 6868d90ad90SDavid Gibson return H_RESOURCE; 6878d90ad90SDavid Gibson } 6888d90ad90SDavid Gibson 6898d90ad90SDavid Gibson lbuf = alloca(total_len); 6908d90ad90SDavid Gibson p = lbuf; 6918d90ad90SDavid Gibson for (i = 0; i < nbufs; i++) { 692ad0ebb91SDavid Gibson ret = spapr_vio_dma_read(sdev, VLAN_BD_ADDR(bufs[i]), 6938d90ad90SDavid Gibson p, VLAN_BD_LEN(bufs[i])); 6948d90ad90SDavid Gibson if (ret < 0) { 6958d90ad90SDavid Gibson return ret; 6968d90ad90SDavid Gibson } 6978d90ad90SDavid Gibson 6988d90ad90SDavid Gibson p += VLAN_BD_LEN(bufs[i]); 6998d90ad90SDavid Gibson } 7008d90ad90SDavid Gibson 701b356f76dSJason Wang qemu_send_packet(qemu_get_queue(dev->nic), lbuf, total_len); 7028d90ad90SDavid Gibson 7038d90ad90SDavid Gibson return H_SUCCESS; 7048d90ad90SDavid Gibson } 7058d90ad90SDavid Gibson 70628e02042SDavid Gibson static target_ulong h_multicast_ctrl(PowerPCCPU *cpu, sPAPRMachineState *spapr, 7078d90ad90SDavid Gibson target_ulong opcode, target_ulong *args) 7088d90ad90SDavid Gibson { 7098d90ad90SDavid Gibson target_ulong reg = args[0]; 7108d90ad90SDavid Gibson VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg); 7118d90ad90SDavid Gibson 7128d90ad90SDavid Gibson if (!dev) { 7138d90ad90SDavid Gibson return H_PARAMETER; 7148d90ad90SDavid Gibson } 7158d90ad90SDavid Gibson 7168d90ad90SDavid Gibson return H_SUCCESS; 7178d90ad90SDavid Gibson } 7188d90ad90SDavid Gibson 7193954d33aSAnthony Liguori static Property spapr_vlan_properties[] = { 720ad0ebb91SDavid Gibson DEFINE_SPAPR_PROPERTIES(VIOsPAPRVLANDevice, sdev), 7218d90ad90SDavid Gibson DEFINE_NIC_PROPERTIES(VIOsPAPRVLANDevice, nicconf), 722831e8822SThomas Huth DEFINE_PROP_BIT("use-rx-buffer-pools", VIOsPAPRVLANDevice, 72357c522f4SThomas Huth compat_flags, SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT, true), 7248d90ad90SDavid Gibson DEFINE_PROP_END_OF_LIST(), 7253954d33aSAnthony Liguori }; 7263954d33aSAnthony Liguori 727831e8822SThomas Huth static bool spapr_vlan_rx_buffer_pools_needed(void *opaque) 728831e8822SThomas Huth { 729831e8822SThomas Huth VIOsPAPRVLANDevice *dev = opaque; 730831e8822SThomas Huth 731831e8822SThomas Huth return (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) != 0; 732831e8822SThomas Huth } 733831e8822SThomas Huth 734831e8822SThomas Huth static const VMStateDescription vmstate_rx_buffer_pool = { 735831e8822SThomas Huth .name = "spapr_llan/rx_buffer_pool", 736831e8822SThomas Huth .version_id = 1, 737831e8822SThomas Huth .minimum_version_id = 1, 738831e8822SThomas Huth .needed = spapr_vlan_rx_buffer_pools_needed, 739831e8822SThomas Huth .fields = (VMStateField[]) { 740831e8822SThomas Huth VMSTATE_INT32(bufsize, RxBufPool), 741831e8822SThomas Huth VMSTATE_INT32(count, RxBufPool), 742831e8822SThomas Huth VMSTATE_UINT64_ARRAY(bds, RxBufPool, RX_POOL_MAX_BDS), 743831e8822SThomas Huth VMSTATE_END_OF_LIST() 744831e8822SThomas Huth } 745831e8822SThomas Huth }; 746831e8822SThomas Huth 747831e8822SThomas Huth static const VMStateDescription vmstate_rx_pools = { 748831e8822SThomas Huth .name = "spapr_llan/rx_pools", 749831e8822SThomas Huth .version_id = 1, 750831e8822SThomas Huth .minimum_version_id = 1, 751831e8822SThomas Huth .needed = spapr_vlan_rx_buffer_pools_needed, 752831e8822SThomas Huth .fields = (VMStateField[]) { 753831e8822SThomas Huth VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(rx_pool, VIOsPAPRVLANDevice, 754831e8822SThomas Huth RX_MAX_POOLS, 1, 755831e8822SThomas Huth vmstate_rx_buffer_pool, RxBufPool), 756831e8822SThomas Huth VMSTATE_END_OF_LIST() 757831e8822SThomas Huth } 758831e8822SThomas Huth }; 759831e8822SThomas Huth 760686fefe4SDavid Gibson static const VMStateDescription vmstate_spapr_llan = { 761686fefe4SDavid Gibson .name = "spapr_llan", 762686fefe4SDavid Gibson .version_id = 1, 763686fefe4SDavid Gibson .minimum_version_id = 1, 764686fefe4SDavid Gibson .fields = (VMStateField[]) { 765686fefe4SDavid Gibson VMSTATE_SPAPR_VIO(sdev, VIOsPAPRVLANDevice), 766686fefe4SDavid Gibson /* LLAN state */ 767686fefe4SDavid Gibson VMSTATE_BOOL(isopen, VIOsPAPRVLANDevice), 768*cbd62f86SPaolo Bonzini VMSTATE_UINT64(buf_list, VIOsPAPRVLANDevice), 769686fefe4SDavid Gibson VMSTATE_UINT32(add_buf_ptr, VIOsPAPRVLANDevice), 770686fefe4SDavid Gibson VMSTATE_UINT32(use_buf_ptr, VIOsPAPRVLANDevice), 771686fefe4SDavid Gibson VMSTATE_UINT32(rx_bufs, VIOsPAPRVLANDevice), 772*cbd62f86SPaolo Bonzini VMSTATE_UINT64(rxq_ptr, VIOsPAPRVLANDevice), 773686fefe4SDavid Gibson 774686fefe4SDavid Gibson VMSTATE_END_OF_LIST() 775686fefe4SDavid Gibson }, 776831e8822SThomas Huth .subsections = (const VMStateDescription * []) { 777831e8822SThomas Huth &vmstate_rx_pools, 778831e8822SThomas Huth NULL 779831e8822SThomas Huth } 780686fefe4SDavid Gibson }; 781686fefe4SDavid Gibson 7823954d33aSAnthony Liguori static void spapr_vlan_class_init(ObjectClass *klass, void *data) 7833954d33aSAnthony Liguori { 78439bffca2SAnthony Liguori DeviceClass *dc = DEVICE_CLASS(klass); 7853954d33aSAnthony Liguori VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass); 7863954d33aSAnthony Liguori 78728b07e73SMarkus Armbruster k->realize = spapr_vlan_realize; 788c17491b6SDavid Gibson k->reset = spapr_vlan_reset; 7893954d33aSAnthony Liguori k->devnode = spapr_vlan_devnode; 7903954d33aSAnthony Liguori k->dt_name = "l-lan"; 7913954d33aSAnthony Liguori k->dt_type = "network"; 7923954d33aSAnthony Liguori k->dt_compatible = "IBM,l-lan"; 7933954d33aSAnthony Liguori k->signal_mask = 0x1; 79429fdedfeSAlexey Kardashevskiy set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 79539bffca2SAnthony Liguori dc->props = spapr_vlan_properties; 796ad0ebb91SDavid Gibson k->rtce_window_size = 0x10000000; 797686fefe4SDavid Gibson dc->vmsd = &vmstate_spapr_llan; 7983954d33aSAnthony Liguori } 7993954d33aSAnthony Liguori 8008c43a6f0SAndreas Färber static const TypeInfo spapr_vlan_info = { 801fd506b4fSDavid Gibson .name = TYPE_VIO_SPAPR_VLAN_DEVICE, 80239bffca2SAnthony Liguori .parent = TYPE_VIO_SPAPR_DEVICE, 80339bffca2SAnthony Liguori .instance_size = sizeof(VIOsPAPRVLANDevice), 8043954d33aSAnthony Liguori .class_init = spapr_vlan_class_init, 805dfe79cf2SGonglei .instance_init = spapr_vlan_instance_init, 806831e8822SThomas Huth .instance_finalize = spapr_vlan_instance_finalize, 8078d90ad90SDavid Gibson }; 8088d90ad90SDavid Gibson 80983f7d43aSAndreas Färber static void spapr_vlan_register_types(void) 8108d90ad90SDavid Gibson { 8111fc02533SDavid Gibson spapr_register_hypercall(H_REGISTER_LOGICAL_LAN, h_register_logical_lan); 8121fc02533SDavid Gibson spapr_register_hypercall(H_FREE_LOGICAL_LAN, h_free_logical_lan); 8131fc02533SDavid Gibson spapr_register_hypercall(H_SEND_LOGICAL_LAN, h_send_logical_lan); 8141fc02533SDavid Gibson spapr_register_hypercall(H_ADD_LOGICAL_LAN_BUFFER, 8151fc02533SDavid Gibson h_add_logical_lan_buffer); 8161fc02533SDavid Gibson spapr_register_hypercall(H_MULTICAST_CTRL, h_multicast_ctrl); 81739bffca2SAnthony Liguori type_register_static(&spapr_vlan_info); 8188d90ad90SDavid Gibson } 81983f7d43aSAndreas Färber 82083f7d43aSAndreas Färber type_init(spapr_vlan_register_types) 821