David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator |
| 3 | * |
| 4 | * PAPR Inter-VM Logical Lan, aka ibmveth |
| 5 | * |
| 6 | * Copyright (c) 2010,2011 David Gibson, IBM Corporation. |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | * |
| 26 | */ |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 27 | #include "hw/hw.h" |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 28 | #include "net/net.h" |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 29 | #include "hw/qdev.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 30 | #include "hw/ppc/spapr.h" |
| 31 | #include "hw/ppc/spapr_vio.h" |
Alexey Kardashevskiy | ad4f62d | 2014-03-17 13:40:24 +1100 | [diff] [blame] | 32 | #include "sysemu/sysemu.h" |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 33 | |
| 34 | #include <libfdt.h> |
| 35 | |
| 36 | #define ETH_ALEN 6 |
| 37 | #define MAX_PACKET_SIZE 65536 |
| 38 | |
| 39 | /*#define DEBUG*/ |
| 40 | |
| 41 | #ifdef DEBUG |
Peter Maydell | f6bda9c | 2013-07-29 13:16:39 +0100 | [diff] [blame] | 42 | #define DPRINTF(fmt...) do { fprintf(stderr, fmt); } while (0) |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 43 | #else |
Peter Maydell | f6bda9c | 2013-07-29 13:16:39 +0100 | [diff] [blame] | 44 | #define DPRINTF(fmt...) |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 45 | #endif |
| 46 | |
| 47 | /* |
| 48 | * Virtual LAN device |
| 49 | */ |
| 50 | |
| 51 | typedef uint64_t vlan_bd_t; |
| 52 | |
| 53 | #define VLAN_BD_VALID 0x8000000000000000ULL |
| 54 | #define VLAN_BD_TOGGLE 0x4000000000000000ULL |
| 55 | #define VLAN_BD_NO_CSUM 0x0200000000000000ULL |
| 56 | #define VLAN_BD_CSUM_GOOD 0x0100000000000000ULL |
| 57 | #define VLAN_BD_LEN_MASK 0x00ffffff00000000ULL |
| 58 | #define VLAN_BD_LEN(bd) (((bd) & VLAN_BD_LEN_MASK) >> 32) |
| 59 | #define VLAN_BD_ADDR_MASK 0x00000000ffffffffULL |
| 60 | #define VLAN_BD_ADDR(bd) ((bd) & VLAN_BD_ADDR_MASK) |
| 61 | |
| 62 | #define VLAN_VALID_BD(addr, len) (VLAN_BD_VALID | \ |
| 63 | (((len) << 32) & VLAN_BD_LEN_MASK) | \ |
| 64 | (addr & VLAN_BD_ADDR_MASK)) |
| 65 | |
| 66 | #define VLAN_RXQC_TOGGLE 0x80 |
| 67 | #define VLAN_RXQC_VALID 0x40 |
| 68 | #define VLAN_RXQC_NO_CSUM 0x02 |
| 69 | #define VLAN_RXQC_CSUM_GOOD 0x01 |
| 70 | |
| 71 | #define VLAN_RQ_ALIGNMENT 16 |
| 72 | #define VLAN_RXQ_BD_OFF 0 |
| 73 | #define VLAN_FILTER_BD_OFF 8 |
| 74 | #define VLAN_RX_BDS_OFF 16 |
Anton Blanchard | 439ce14 | 2014-08-22 11:50:57 +1000 | [diff] [blame] | 75 | /* |
| 76 | * The final 8 bytes of the buffer list is a counter of frames dropped |
| 77 | * because there was not a buffer in the buffer list capable of holding |
| 78 | * the frame. We must avoid it, or the operating system will report garbage |
| 79 | * for this statistic. |
| 80 | */ |
| 81 | #define VLAN_RX_BDS_LEN (SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF - 8) |
| 82 | #define VLAN_MAX_BUFS (VLAN_RX_BDS_LEN / 8) |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 83 | |
David Gibson | fd506b4f | 2013-04-07 19:08:16 +0000 | [diff] [blame] | 84 | #define TYPE_VIO_SPAPR_VLAN_DEVICE "spapr-vlan" |
| 85 | #define VIO_SPAPR_VLAN_DEVICE(obj) \ |
| 86 | OBJECT_CHECK(VIOsPAPRVLANDevice, (obj), TYPE_VIO_SPAPR_VLAN_DEVICE) |
| 87 | |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 88 | typedef struct VIOsPAPRVLANDevice { |
| 89 | VIOsPAPRDevice sdev; |
| 90 | NICConf nicconf; |
| 91 | NICState *nic; |
David Gibson | 686fefe | 2013-07-18 14:32:56 -0500 | [diff] [blame] | 92 | bool isopen; |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 93 | target_ulong buf_list; |
David Gibson | 686fefe | 2013-07-18 14:32:56 -0500 | [diff] [blame] | 94 | uint32_t add_buf_ptr, use_buf_ptr, rx_bufs; |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 95 | target_ulong rxq_ptr; |
| 96 | } VIOsPAPRVLANDevice; |
| 97 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 98 | static int spapr_vlan_can_receive(NetClientState *nc) |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 99 | { |
Jason Wang | cc1f0f4 | 2013-01-30 19:12:23 +0800 | [diff] [blame] | 100 | VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 101 | |
| 102 | return (dev->isopen && dev->rx_bufs > 0); |
| 103 | } |
| 104 | |
Stefan Hajnoczi | 4e68f7a | 2012-07-24 16:35:13 +0100 | [diff] [blame] | 105 | static ssize_t spapr_vlan_receive(NetClientState *nc, const uint8_t *buf, |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 106 | size_t size) |
| 107 | { |
David Gibson | fd506b4f | 2013-04-07 19:08:16 +0000 | [diff] [blame] | 108 | VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc); |
| 109 | VIOsPAPRDevice *sdev = VIO_SPAPR_DEVICE(dev); |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 110 | vlan_bd_t rxq_bd = vio_ldq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 111 | vlan_bd_t bd; |
| 112 | int buf_ptr = dev->use_buf_ptr; |
| 113 | uint64_t handle; |
| 114 | uint8_t control; |
| 115 | |
Peter Maydell | f6bda9c | 2013-07-29 13:16:39 +0100 | [diff] [blame] | 116 | DPRINTF("spapr_vlan_receive() [%s] rx_bufs=%d\n", sdev->qdev.id, |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 117 | dev->rx_bufs); |
| 118 | |
| 119 | if (!dev->isopen) { |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | if (!dev->rx_bufs) { |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | do { |
| 128 | buf_ptr += 8; |
Anton Blanchard | 439ce14 | 2014-08-22 11:50:57 +1000 | [diff] [blame] | 129 | if (buf_ptr >= (VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF)) { |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 130 | buf_ptr = VLAN_RX_BDS_OFF; |
| 131 | } |
| 132 | |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 133 | bd = vio_ldq(sdev, dev->buf_list + buf_ptr); |
Peter Maydell | f6bda9c | 2013-07-29 13:16:39 +0100 | [diff] [blame] | 134 | DPRINTF("use_buf_ptr=%d bd=0x%016llx\n", |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 135 | buf_ptr, (unsigned long long)bd); |
| 136 | } while ((!(bd & VLAN_BD_VALID) || (VLAN_BD_LEN(bd) < (size + 8))) |
| 137 | && (buf_ptr != dev->use_buf_ptr)); |
| 138 | |
| 139 | if (!(bd & VLAN_BD_VALID) || (VLAN_BD_LEN(bd) < (size + 8))) { |
| 140 | /* Failed to find a suitable buffer */ |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | /* Remove the buffer from the pool */ |
| 145 | dev->rx_bufs--; |
| 146 | dev->use_buf_ptr = buf_ptr; |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 147 | vio_stq(sdev, dev->buf_list + dev->use_buf_ptr, 0); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 148 | |
Peter Maydell | f6bda9c | 2013-07-29 13:16:39 +0100 | [diff] [blame] | 149 | DPRINTF("Found buffer: ptr=%d num=%d\n", dev->use_buf_ptr, dev->rx_bufs); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 150 | |
| 151 | /* Transfer the packet data */ |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 152 | if (spapr_vio_dma_write(sdev, VLAN_BD_ADDR(bd) + 8, buf, size) < 0) { |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 153 | return -1; |
| 154 | } |
| 155 | |
Peter Maydell | f6bda9c | 2013-07-29 13:16:39 +0100 | [diff] [blame] | 156 | DPRINTF("spapr_vlan_receive: DMA write completed\n"); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 157 | |
| 158 | /* Update the receive queue */ |
| 159 | control = VLAN_RXQC_TOGGLE | VLAN_RXQC_VALID; |
| 160 | if (rxq_bd & VLAN_BD_TOGGLE) { |
| 161 | control ^= VLAN_RXQC_TOGGLE; |
| 162 | } |
| 163 | |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 164 | handle = vio_ldq(sdev, VLAN_BD_ADDR(bd)); |
| 165 | vio_stq(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 8, handle); |
| 166 | vio_stl(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 4, size); |
| 167 | vio_sth(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 2, 8); |
| 168 | vio_stb(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr, control); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 169 | |
Peter Maydell | f6bda9c | 2013-07-29 13:16:39 +0100 | [diff] [blame] | 170 | DPRINTF("wrote rxq entry (ptr=0x%llx): 0x%016llx 0x%016llx\n", |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 171 | (unsigned long long)dev->rxq_ptr, |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 172 | (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) + |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 173 | dev->rxq_ptr), |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 174 | (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) + |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 175 | dev->rxq_ptr + 8)); |
| 176 | |
| 177 | dev->rxq_ptr += 16; |
| 178 | if (dev->rxq_ptr >= VLAN_BD_LEN(rxq_bd)) { |
| 179 | dev->rxq_ptr = 0; |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 180 | vio_stq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF, rxq_bd ^ VLAN_BD_TOGGLE); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | if (sdev->signal_state & 1) { |
Alexey Kardashevskiy | a307d59 | 2012-08-07 16:10:32 +0000 | [diff] [blame] | 184 | qemu_irq_pulse(spapr_vio_qirq(sdev)); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return size; |
| 188 | } |
| 189 | |
| 190 | static NetClientInfo net_spapr_vlan_info = { |
Laszlo Ersek | 2be64a6 | 2012-07-17 16:17:12 +0200 | [diff] [blame] | 191 | .type = NET_CLIENT_OPTIONS_KIND_NIC, |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 192 | .size = sizeof(NICState), |
| 193 | .can_receive = spapr_vlan_can_receive, |
| 194 | .receive = spapr_vlan_receive, |
| 195 | }; |
| 196 | |
David Gibson | c17491b | 2012-04-12 12:44:15 +1000 | [diff] [blame] | 197 | static void spapr_vlan_reset(VIOsPAPRDevice *sdev) |
| 198 | { |
David Gibson | fd506b4f | 2013-04-07 19:08:16 +0000 | [diff] [blame] | 199 | VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); |
David Gibson | c17491b | 2012-04-12 12:44:15 +1000 | [diff] [blame] | 200 | |
| 201 | dev->buf_list = 0; |
| 202 | dev->rx_bufs = 0; |
| 203 | dev->isopen = 0; |
| 204 | } |
| 205 | |
Markus Armbruster | 28b07e7 | 2015-02-27 11:52:17 +0100 | [diff] [blame] | 206 | static void spapr_vlan_realize(VIOsPAPRDevice *sdev, Error **errp) |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 207 | { |
David Gibson | fd506b4f | 2013-04-07 19:08:16 +0000 | [diff] [blame] | 208 | VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 209 | |
| 210 | qemu_macaddr_default_if_unset(&dev->nicconf.macaddr); |
| 211 | |
| 212 | dev->nic = qemu_new_nic(&net_spapr_vlan_info, &dev->nicconf, |
Anthony Liguori | f79f2bf | 2011-12-04 11:17:51 -0600 | [diff] [blame] | 213 | object_get_typename(OBJECT(sdev)), sdev->qdev.id, dev); |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 214 | qemu_format_nic_info_str(qemu_get_queue(dev->nic), dev->nicconf.macaddr.a); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 215 | } |
| 216 | |
Gonglei | dfe79cf | 2014-10-07 16:00:18 +0800 | [diff] [blame] | 217 | static void spapr_vlan_instance_init(Object *obj) |
| 218 | { |
| 219 | VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(obj); |
| 220 | |
| 221 | device_add_bootindex_property(obj, &dev->nicconf.bootindex, |
| 222 | "bootindex", "", |
| 223 | DEVICE(dev), NULL); |
| 224 | } |
| 225 | |
David Gibson | d601fac | 2012-04-25 17:55:41 +0000 | [diff] [blame] | 226 | void spapr_vlan_create(VIOsPAPRBus *bus, NICInfo *nd) |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 227 | { |
| 228 | DeviceState *dev; |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 229 | |
| 230 | dev = qdev_create(&bus->bus, "spapr-vlan"); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 231 | |
| 232 | qdev_set_nic_properties(dev, nd); |
| 233 | |
| 234 | qdev_init_nofail(dev); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static int spapr_vlan_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off) |
| 238 | { |
David Gibson | fd506b4f | 2013-04-07 19:08:16 +0000 | [diff] [blame] | 239 | VIOsPAPRVLANDevice *vdev = VIO_SPAPR_VLAN_DEVICE(dev); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 240 | uint8_t padded_mac[8] = {0, 0}; |
| 241 | int ret; |
| 242 | |
| 243 | /* Some old phyp versions give the mac address in an 8-byte |
| 244 | * property. The kernel driver has an insane workaround for this; |
| 245 | * rather than doing the obvious thing and checking the property |
| 246 | * length, it checks whether the first byte has 0b10 in the low |
| 247 | * bits. If a correct 6-byte property has a different first byte |
| 248 | * the kernel will get the wrong mac address, overrunning its |
| 249 | * buffer in the process (read only, thank goodness). |
| 250 | * |
| 251 | * Here we workaround the kernel workaround by always supplying an |
| 252 | * 8-byte property, with the mac address in the last six bytes */ |
| 253 | memcpy(&padded_mac[2], &vdev->nicconf.macaddr, ETH_ALEN); |
| 254 | ret = fdt_setprop(fdt, node_off, "local-mac-address", |
| 255 | padded_mac, sizeof(padded_mac)); |
| 256 | if (ret < 0) { |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | ret = fdt_setprop_cell(fdt, node_off, "ibm,mac-address-filters", 0); |
| 261 | if (ret < 0) { |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int check_bd(VIOsPAPRVLANDevice *dev, vlan_bd_t bd, |
| 269 | target_ulong alignment) |
| 270 | { |
| 271 | if ((VLAN_BD_ADDR(bd) % alignment) |
| 272 | || (VLAN_BD_LEN(bd) % alignment)) { |
| 273 | return -1; |
| 274 | } |
| 275 | |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 276 | if (!spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd), |
| 277 | VLAN_BD_LEN(bd), DMA_DIRECTION_FROM_DEVICE) |
| 278 | || !spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd), |
| 279 | VLAN_BD_LEN(bd), DMA_DIRECTION_TO_DEVICE)) { |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 280 | return -1; |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
Andreas Färber | b13ce26 | 2012-05-03 06:23:01 +0200 | [diff] [blame] | 286 | static target_ulong h_register_logical_lan(PowerPCCPU *cpu, |
David Gibson | 28e0204 | 2015-07-02 16:23:04 +1000 | [diff] [blame] | 287 | sPAPRMachineState *spapr, |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 288 | target_ulong opcode, |
| 289 | target_ulong *args) |
| 290 | { |
| 291 | target_ulong reg = args[0]; |
| 292 | target_ulong buf_list = args[1]; |
| 293 | target_ulong rec_queue = args[2]; |
| 294 | target_ulong filter_list = args[3]; |
| 295 | VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg); |
David Gibson | fd506b4f | 2013-04-07 19:08:16 +0000 | [diff] [blame] | 296 | VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 297 | vlan_bd_t filter_list_bd; |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 298 | |
| 299 | if (!dev) { |
| 300 | return H_PARAMETER; |
| 301 | } |
| 302 | |
| 303 | if (dev->isopen) { |
| 304 | hcall_dprintf("H_REGISTER_LOGICAL_LAN called twice without " |
| 305 | "H_FREE_LOGICAL_LAN\n"); |
| 306 | return H_RESOURCE; |
| 307 | } |
| 308 | |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 309 | if (check_bd(dev, VLAN_VALID_BD(buf_list, SPAPR_TCE_PAGE_SIZE), |
| 310 | SPAPR_TCE_PAGE_SIZE) < 0) { |
David Gibson | d9599c9 | 2012-03-29 08:39:45 +1100 | [diff] [blame] | 311 | hcall_dprintf("Bad buf_list 0x" TARGET_FMT_lx "\n", buf_list); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 312 | return H_PARAMETER; |
| 313 | } |
| 314 | |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 315 | filter_list_bd = VLAN_VALID_BD(filter_list, SPAPR_TCE_PAGE_SIZE); |
| 316 | if (check_bd(dev, filter_list_bd, SPAPR_TCE_PAGE_SIZE) < 0) { |
David Gibson | d9599c9 | 2012-03-29 08:39:45 +1100 | [diff] [blame] | 317 | hcall_dprintf("Bad filter_list 0x" TARGET_FMT_lx "\n", filter_list); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 318 | return H_PARAMETER; |
| 319 | } |
| 320 | |
| 321 | if (!(rec_queue & VLAN_BD_VALID) |
| 322 | || (check_bd(dev, rec_queue, VLAN_RQ_ALIGNMENT) < 0)) { |
David Gibson | d9599c9 | 2012-03-29 08:39:45 +1100 | [diff] [blame] | 323 | hcall_dprintf("Bad receive queue\n"); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 324 | return H_PARAMETER; |
| 325 | } |
| 326 | |
| 327 | dev->buf_list = buf_list; |
| 328 | sdev->signal_state = 0; |
| 329 | |
| 330 | rec_queue &= ~VLAN_BD_TOGGLE; |
| 331 | |
| 332 | /* Initialize the buffer list */ |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 333 | vio_stq(sdev, buf_list, rec_queue); |
| 334 | vio_stq(sdev, buf_list + 8, filter_list_bd); |
| 335 | spapr_vio_dma_set(sdev, buf_list + VLAN_RX_BDS_OFF, 0, |
| 336 | SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 337 | dev->add_buf_ptr = VLAN_RX_BDS_OFF - 8; |
| 338 | dev->use_buf_ptr = VLAN_RX_BDS_OFF - 8; |
| 339 | dev->rx_bufs = 0; |
| 340 | dev->rxq_ptr = 0; |
| 341 | |
| 342 | /* Initialize the receive queue */ |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 343 | spapr_vio_dma_set(sdev, VLAN_BD_ADDR(rec_queue), 0, VLAN_BD_LEN(rec_queue)); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 344 | |
| 345 | dev->isopen = 1; |
Alexey Kardashevskiy | e0ff466 | 2013-05-02 20:22:03 +0000 | [diff] [blame] | 346 | qemu_flush_queued_packets(qemu_get_queue(dev->nic)); |
| 347 | |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 348 | return H_SUCCESS; |
| 349 | } |
| 350 | |
| 351 | |
David Gibson | 28e0204 | 2015-07-02 16:23:04 +1000 | [diff] [blame] | 352 | static target_ulong h_free_logical_lan(PowerPCCPU *cpu, |
| 353 | sPAPRMachineState *spapr, |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 354 | target_ulong opcode, target_ulong *args) |
| 355 | { |
| 356 | target_ulong reg = args[0]; |
| 357 | VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg); |
David Gibson | fd506b4f | 2013-04-07 19:08:16 +0000 | [diff] [blame] | 358 | VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 359 | |
| 360 | if (!dev) { |
| 361 | return H_PARAMETER; |
| 362 | } |
| 363 | |
| 364 | if (!dev->isopen) { |
| 365 | hcall_dprintf("H_FREE_LOGICAL_LAN called without " |
| 366 | "H_REGISTER_LOGICAL_LAN\n"); |
| 367 | return H_RESOURCE; |
| 368 | } |
| 369 | |
David Gibson | c17491b | 2012-04-12 12:44:15 +1000 | [diff] [blame] | 370 | spapr_vlan_reset(sdev); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 371 | return H_SUCCESS; |
| 372 | } |
| 373 | |
Andreas Färber | b13ce26 | 2012-05-03 06:23:01 +0200 | [diff] [blame] | 374 | static target_ulong h_add_logical_lan_buffer(PowerPCCPU *cpu, |
David Gibson | 28e0204 | 2015-07-02 16:23:04 +1000 | [diff] [blame] | 375 | sPAPRMachineState *spapr, |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 376 | target_ulong opcode, |
| 377 | target_ulong *args) |
| 378 | { |
| 379 | target_ulong reg = args[0]; |
| 380 | target_ulong buf = args[1]; |
| 381 | VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg); |
David Gibson | fd506b4f | 2013-04-07 19:08:16 +0000 | [diff] [blame] | 382 | VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 383 | vlan_bd_t bd; |
| 384 | |
Peter Maydell | f6bda9c | 2013-07-29 13:16:39 +0100 | [diff] [blame] | 385 | DPRINTF("H_ADD_LOGICAL_LAN_BUFFER(0x" TARGET_FMT_lx |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 386 | ", 0x" TARGET_FMT_lx ")\n", reg, buf); |
| 387 | |
| 388 | if (!sdev) { |
David Gibson | d9599c9 | 2012-03-29 08:39:45 +1100 | [diff] [blame] | 389 | hcall_dprintf("Bad device\n"); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 390 | return H_PARAMETER; |
| 391 | } |
| 392 | |
| 393 | if ((check_bd(dev, buf, 4) < 0) |
| 394 | || (VLAN_BD_LEN(buf) < 16)) { |
David Gibson | d9599c9 | 2012-03-29 08:39:45 +1100 | [diff] [blame] | 395 | hcall_dprintf("Bad buffer enqueued\n"); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 396 | return H_PARAMETER; |
| 397 | } |
| 398 | |
| 399 | if (!dev->isopen || dev->rx_bufs >= VLAN_MAX_BUFS) { |
| 400 | return H_RESOURCE; |
| 401 | } |
| 402 | |
| 403 | do { |
| 404 | dev->add_buf_ptr += 8; |
Anton Blanchard | 439ce14 | 2014-08-22 11:50:57 +1000 | [diff] [blame] | 405 | if (dev->add_buf_ptr >= (VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF)) { |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 406 | dev->add_buf_ptr = VLAN_RX_BDS_OFF; |
| 407 | } |
| 408 | |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 409 | bd = vio_ldq(sdev, dev->buf_list + dev->add_buf_ptr); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 410 | } while (bd & VLAN_BD_VALID); |
| 411 | |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 412 | vio_stq(sdev, dev->buf_list + dev->add_buf_ptr, buf); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 413 | |
| 414 | dev->rx_bufs++; |
| 415 | |
Alexey Kardashevskiy | 0a61f3b | 2014-02-14 12:27:04 +1100 | [diff] [blame] | 416 | qemu_flush_queued_packets(qemu_get_queue(dev->nic)); |
| 417 | |
Peter Maydell | f6bda9c | 2013-07-29 13:16:39 +0100 | [diff] [blame] | 418 | DPRINTF("h_add_logical_lan_buffer(): Added buf ptr=%d rx_bufs=%d" |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 419 | " bd=0x%016llx\n", dev->add_buf_ptr, dev->rx_bufs, |
| 420 | (unsigned long long)buf); |
| 421 | |
| 422 | return H_SUCCESS; |
| 423 | } |
| 424 | |
David Gibson | 28e0204 | 2015-07-02 16:23:04 +1000 | [diff] [blame] | 425 | static target_ulong h_send_logical_lan(PowerPCCPU *cpu, |
| 426 | sPAPRMachineState *spapr, |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 427 | target_ulong opcode, target_ulong *args) |
| 428 | { |
| 429 | target_ulong reg = args[0]; |
| 430 | target_ulong *bufs = args + 1; |
| 431 | target_ulong continue_token = args[7]; |
| 432 | VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg); |
David Gibson | fd506b4f | 2013-04-07 19:08:16 +0000 | [diff] [blame] | 433 | VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 434 | unsigned total_len; |
| 435 | uint8_t *lbuf, *p; |
| 436 | int i, nbufs; |
| 437 | int ret; |
| 438 | |
Peter Maydell | f6bda9c | 2013-07-29 13:16:39 +0100 | [diff] [blame] | 439 | DPRINTF("H_SEND_LOGICAL_LAN(0x" TARGET_FMT_lx ", <bufs>, 0x" |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 440 | TARGET_FMT_lx ")\n", reg, continue_token); |
| 441 | |
| 442 | if (!sdev) { |
| 443 | return H_PARAMETER; |
| 444 | } |
| 445 | |
Peter Maydell | f6bda9c | 2013-07-29 13:16:39 +0100 | [diff] [blame] | 446 | DPRINTF("rxbufs = %d\n", dev->rx_bufs); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 447 | |
| 448 | if (!dev->isopen) { |
| 449 | return H_DROPPED; |
| 450 | } |
| 451 | |
| 452 | if (continue_token) { |
| 453 | return H_HARDWARE; /* FIXME actually handle this */ |
| 454 | } |
| 455 | |
| 456 | total_len = 0; |
| 457 | for (i = 0; i < 6; i++) { |
Peter Maydell | f6bda9c | 2013-07-29 13:16:39 +0100 | [diff] [blame] | 458 | DPRINTF(" buf desc: 0x" TARGET_FMT_lx "\n", bufs[i]); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 459 | if (!(bufs[i] & VLAN_BD_VALID)) { |
| 460 | break; |
| 461 | } |
| 462 | total_len += VLAN_BD_LEN(bufs[i]); |
| 463 | } |
| 464 | |
| 465 | nbufs = i; |
Peter Maydell | f6bda9c | 2013-07-29 13:16:39 +0100 | [diff] [blame] | 466 | DPRINTF("h_send_logical_lan() %d buffers, total length 0x%x\n", |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 467 | nbufs, total_len); |
| 468 | |
| 469 | if (total_len == 0) { |
| 470 | return H_SUCCESS; |
| 471 | } |
| 472 | |
| 473 | if (total_len > MAX_PACKET_SIZE) { |
| 474 | /* Don't let the guest force too large an allocation */ |
| 475 | return H_RESOURCE; |
| 476 | } |
| 477 | |
| 478 | lbuf = alloca(total_len); |
| 479 | p = lbuf; |
| 480 | for (i = 0; i < nbufs; i++) { |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 481 | ret = spapr_vio_dma_read(sdev, VLAN_BD_ADDR(bufs[i]), |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 482 | p, VLAN_BD_LEN(bufs[i])); |
| 483 | if (ret < 0) { |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | p += VLAN_BD_LEN(bufs[i]); |
| 488 | } |
| 489 | |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 490 | qemu_send_packet(qemu_get_queue(dev->nic), lbuf, total_len); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 491 | |
| 492 | return H_SUCCESS; |
| 493 | } |
| 494 | |
David Gibson | 28e0204 | 2015-07-02 16:23:04 +1000 | [diff] [blame] | 495 | static target_ulong h_multicast_ctrl(PowerPCCPU *cpu, sPAPRMachineState *spapr, |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 496 | target_ulong opcode, target_ulong *args) |
| 497 | { |
| 498 | target_ulong reg = args[0]; |
| 499 | VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg); |
| 500 | |
| 501 | if (!dev) { |
| 502 | return H_PARAMETER; |
| 503 | } |
| 504 | |
| 505 | return H_SUCCESS; |
| 506 | } |
| 507 | |
Anthony Liguori | 3954d33 | 2011-12-15 16:31:06 -0600 | [diff] [blame] | 508 | static Property spapr_vlan_properties[] = { |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 509 | DEFINE_SPAPR_PROPERTIES(VIOsPAPRVLANDevice, sdev), |
Anthony Liguori | 3954d33 | 2011-12-15 16:31:06 -0600 | [diff] [blame] | 510 | DEFINE_NIC_PROPERTIES(VIOsPAPRVLANDevice, nicconf), |
| 511 | DEFINE_PROP_END_OF_LIST(), |
| 512 | }; |
| 513 | |
David Gibson | 686fefe | 2013-07-18 14:32:56 -0500 | [diff] [blame] | 514 | static const VMStateDescription vmstate_spapr_llan = { |
| 515 | .name = "spapr_llan", |
| 516 | .version_id = 1, |
| 517 | .minimum_version_id = 1, |
Juan Quintela | 3aff6c2 | 2014-04-16 15:24:04 +0200 | [diff] [blame] | 518 | .fields = (VMStateField[]) { |
David Gibson | 686fefe | 2013-07-18 14:32:56 -0500 | [diff] [blame] | 519 | VMSTATE_SPAPR_VIO(sdev, VIOsPAPRVLANDevice), |
| 520 | /* LLAN state */ |
| 521 | VMSTATE_BOOL(isopen, VIOsPAPRVLANDevice), |
| 522 | VMSTATE_UINTTL(buf_list, VIOsPAPRVLANDevice), |
| 523 | VMSTATE_UINT32(add_buf_ptr, VIOsPAPRVLANDevice), |
| 524 | VMSTATE_UINT32(use_buf_ptr, VIOsPAPRVLANDevice), |
| 525 | VMSTATE_UINT32(rx_bufs, VIOsPAPRVLANDevice), |
| 526 | VMSTATE_UINTTL(rxq_ptr, VIOsPAPRVLANDevice), |
| 527 | |
| 528 | VMSTATE_END_OF_LIST() |
| 529 | }, |
| 530 | }; |
| 531 | |
Anthony Liguori | 3954d33 | 2011-12-15 16:31:06 -0600 | [diff] [blame] | 532 | static void spapr_vlan_class_init(ObjectClass *klass, void *data) |
| 533 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 534 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 3954d33 | 2011-12-15 16:31:06 -0600 | [diff] [blame] | 535 | VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass); |
| 536 | |
Markus Armbruster | 28b07e7 | 2015-02-27 11:52:17 +0100 | [diff] [blame] | 537 | k->realize = spapr_vlan_realize; |
David Gibson | c17491b | 2012-04-12 12:44:15 +1000 | [diff] [blame] | 538 | k->reset = spapr_vlan_reset; |
Anthony Liguori | 3954d33 | 2011-12-15 16:31:06 -0600 | [diff] [blame] | 539 | k->devnode = spapr_vlan_devnode; |
| 540 | k->dt_name = "l-lan"; |
| 541 | k->dt_type = "network"; |
| 542 | k->dt_compatible = "IBM,l-lan"; |
| 543 | k->signal_mask = 0x1; |
Alexey Kardashevskiy | 29fdedf | 2013-10-11 14:08:20 +1100 | [diff] [blame] | 544 | set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 545 | dc->props = spapr_vlan_properties; |
David Gibson | ad0ebb9 | 2012-06-27 14:50:44 +1000 | [diff] [blame] | 546 | k->rtce_window_size = 0x10000000; |
David Gibson | 686fefe | 2013-07-18 14:32:56 -0500 | [diff] [blame] | 547 | dc->vmsd = &vmstate_spapr_llan; |
Anthony Liguori | 3954d33 | 2011-12-15 16:31:06 -0600 | [diff] [blame] | 548 | } |
| 549 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 550 | static const TypeInfo spapr_vlan_info = { |
David Gibson | fd506b4f | 2013-04-07 19:08:16 +0000 | [diff] [blame] | 551 | .name = TYPE_VIO_SPAPR_VLAN_DEVICE, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 552 | .parent = TYPE_VIO_SPAPR_DEVICE, |
| 553 | .instance_size = sizeof(VIOsPAPRVLANDevice), |
| 554 | .class_init = spapr_vlan_class_init, |
Gonglei | dfe79cf | 2014-10-07 16:00:18 +0800 | [diff] [blame] | 555 | .instance_init = spapr_vlan_instance_init, |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 556 | }; |
| 557 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 558 | static void spapr_vlan_register_types(void) |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 559 | { |
David Gibson | 1fc0253 | 2011-12-12 18:24:28 +0000 | [diff] [blame] | 560 | spapr_register_hypercall(H_REGISTER_LOGICAL_LAN, h_register_logical_lan); |
| 561 | spapr_register_hypercall(H_FREE_LOGICAL_LAN, h_free_logical_lan); |
| 562 | spapr_register_hypercall(H_SEND_LOGICAL_LAN, h_send_logical_lan); |
| 563 | spapr_register_hypercall(H_ADD_LOGICAL_LAN_BUFFER, |
| 564 | h_add_logical_lan_buffer); |
| 565 | spapr_register_hypercall(H_MULTICAST_CTRL, h_multicast_ctrl); |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 566 | type_register_static(&spapr_vlan_info); |
David Gibson | 8d90ad9 | 2011-04-01 15:15:29 +1100 | [diff] [blame] | 567 | } |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 568 | |
| 569 | type_init(spapr_vlan_register_types) |