xref: /qemu/hw/net/spapr_llan.c (revision 831e8822530bb511a24329494f9121ad1f8b94ab)
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"
2883c9f4caSPaolo Bonzini #include "hw/hw.h"
291422e32dSPaolo Bonzini #include "net/net.h"
308d90ad90SDavid Gibson #include "hw/qdev.h"
310d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
320d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
33ad4f62d0SAlexey Kardashevskiy #include "sysemu/sysemu.h"
348d90ad90SDavid Gibson 
358d90ad90SDavid Gibson #include <libfdt.h>
368d90ad90SDavid Gibson 
378d90ad90SDavid Gibson #define ETH_ALEN        6
388d90ad90SDavid Gibson #define MAX_PACKET_SIZE 65536
398d90ad90SDavid Gibson 
408d90ad90SDavid Gibson /*#define DEBUG*/
418d90ad90SDavid Gibson 
428d90ad90SDavid Gibson #ifdef DEBUG
43f6bda9cbSPeter Maydell #define DPRINTF(fmt...) do { fprintf(stderr, fmt); } while (0)
448d90ad90SDavid Gibson #else
45f6bda9cbSPeter Maydell #define DPRINTF(fmt...)
468d90ad90SDavid Gibson #endif
478d90ad90SDavid Gibson 
48*831e8822SThomas Huth /* Compatibility flags for migration */
49*831e8822SThomas Huth #define SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT  0
50*831e8822SThomas Huth #define SPAPRVLAN_FLAG_RX_BUF_POOLS      (1 << SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT)
51*831e8822SThomas Huth 
528d90ad90SDavid Gibson /*
538d90ad90SDavid Gibson  * Virtual LAN device
548d90ad90SDavid Gibson  */
558d90ad90SDavid Gibson 
568d90ad90SDavid Gibson typedef uint64_t vlan_bd_t;
578d90ad90SDavid Gibson 
588d90ad90SDavid Gibson #define VLAN_BD_VALID        0x8000000000000000ULL
598d90ad90SDavid Gibson #define VLAN_BD_TOGGLE       0x4000000000000000ULL
608d90ad90SDavid Gibson #define VLAN_BD_NO_CSUM      0x0200000000000000ULL
618d90ad90SDavid Gibson #define VLAN_BD_CSUM_GOOD    0x0100000000000000ULL
628d90ad90SDavid Gibson #define VLAN_BD_LEN_MASK     0x00ffffff00000000ULL
638d90ad90SDavid Gibson #define VLAN_BD_LEN(bd)      (((bd) & VLAN_BD_LEN_MASK) >> 32)
648d90ad90SDavid Gibson #define VLAN_BD_ADDR_MASK    0x00000000ffffffffULL
658d90ad90SDavid Gibson #define VLAN_BD_ADDR(bd)     ((bd) & VLAN_BD_ADDR_MASK)
668d90ad90SDavid Gibson 
678d90ad90SDavid Gibson #define VLAN_VALID_BD(addr, len) (VLAN_BD_VALID | \
688d90ad90SDavid Gibson                                   (((len) << 32) & VLAN_BD_LEN_MASK) |  \
698d90ad90SDavid Gibson                                   (addr & VLAN_BD_ADDR_MASK))
708d90ad90SDavid Gibson 
718d90ad90SDavid Gibson #define VLAN_RXQC_TOGGLE     0x80
728d90ad90SDavid Gibson #define VLAN_RXQC_VALID      0x40
738d90ad90SDavid Gibson #define VLAN_RXQC_NO_CSUM    0x02
748d90ad90SDavid Gibson #define VLAN_RXQC_CSUM_GOOD  0x01
758d90ad90SDavid Gibson 
768d90ad90SDavid Gibson #define VLAN_RQ_ALIGNMENT    16
778d90ad90SDavid Gibson #define VLAN_RXQ_BD_OFF      0
788d90ad90SDavid Gibson #define VLAN_FILTER_BD_OFF   8
798d90ad90SDavid Gibson #define VLAN_RX_BDS_OFF      16
80439ce140SAnton Blanchard /*
81439ce140SAnton Blanchard  * The final 8 bytes of the buffer list is a counter of frames dropped
82439ce140SAnton Blanchard  * because there was not a buffer in the buffer list capable of holding
83439ce140SAnton Blanchard  * the frame. We must avoid it, or the operating system will report garbage
84439ce140SAnton Blanchard  * for this statistic.
85439ce140SAnton Blanchard  */
86439ce140SAnton Blanchard #define VLAN_RX_BDS_LEN      (SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF - 8)
87439ce140SAnton Blanchard #define VLAN_MAX_BUFS        (VLAN_RX_BDS_LEN / 8)
888d90ad90SDavid Gibson 
89fd506b4fSDavid Gibson #define TYPE_VIO_SPAPR_VLAN_DEVICE "spapr-vlan"
90fd506b4fSDavid Gibson #define VIO_SPAPR_VLAN_DEVICE(obj) \
91fd506b4fSDavid Gibson      OBJECT_CHECK(VIOsPAPRVLANDevice, (obj), TYPE_VIO_SPAPR_VLAN_DEVICE)
92fd506b4fSDavid Gibson 
93*831e8822SThomas Huth #define RX_POOL_MAX_BDS 4096
94*831e8822SThomas Huth #define RX_MAX_POOLS 5
95*831e8822SThomas Huth 
96*831e8822SThomas Huth typedef struct {
97*831e8822SThomas Huth     int32_t bufsize;
98*831e8822SThomas Huth     int32_t count;
99*831e8822SThomas Huth     vlan_bd_t bds[RX_POOL_MAX_BDS];
100*831e8822SThomas Huth } RxBufPool;
101*831e8822SThomas Huth 
1028d90ad90SDavid Gibson typedef struct VIOsPAPRVLANDevice {
1038d90ad90SDavid Gibson     VIOsPAPRDevice sdev;
1048d90ad90SDavid Gibson     NICConf nicconf;
1058d90ad90SDavid Gibson     NICState *nic;
106686fefe4SDavid Gibson     bool isopen;
1078d90ad90SDavid Gibson     target_ulong buf_list;
108686fefe4SDavid Gibson     uint32_t add_buf_ptr, use_buf_ptr, rx_bufs;
1098d90ad90SDavid Gibson     target_ulong rxq_ptr;
110*831e8822SThomas Huth     uint32_t compat_flags;             /* Compatability flags for migration */
111*831e8822SThomas Huth     RxBufPool *rx_pool[RX_MAX_POOLS];  /* Receive buffer descriptor pools */
1128d90ad90SDavid Gibson } VIOsPAPRVLANDevice;
1138d90ad90SDavid Gibson 
1144e68f7a0SStefan Hajnoczi static int spapr_vlan_can_receive(NetClientState *nc)
1158d90ad90SDavid Gibson {
116cc1f0f45SJason Wang     VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc);
1178d90ad90SDavid Gibson 
1188d90ad90SDavid Gibson     return (dev->isopen && dev->rx_bufs > 0);
1198d90ad90SDavid Gibson }
1208d90ad90SDavid Gibson 
121d6f39fdfSThomas Huth /**
122*831e8822SThomas Huth  * Get buffer descriptor from one of our receive buffer pools
123*831e8822SThomas Huth  */
124*831e8822SThomas Huth static vlan_bd_t spapr_vlan_get_rx_bd_from_pool(VIOsPAPRVLANDevice *dev,
125*831e8822SThomas Huth                                                 size_t size)
126*831e8822SThomas Huth {
127*831e8822SThomas Huth     vlan_bd_t bd;
128*831e8822SThomas Huth     int pool;
129*831e8822SThomas Huth 
130*831e8822SThomas Huth     for (pool = 0; pool < RX_MAX_POOLS; pool++) {
131*831e8822SThomas Huth         if (dev->rx_pool[pool]->count > 0 &&
132*831e8822SThomas Huth             dev->rx_pool[pool]->bufsize >= size + 8) {
133*831e8822SThomas Huth             break;
134*831e8822SThomas Huth         }
135*831e8822SThomas Huth     }
136*831e8822SThomas Huth     if (pool == RX_MAX_POOLS) {
137*831e8822SThomas Huth         /* Failed to find a suitable buffer */
138*831e8822SThomas Huth         return 0;
139*831e8822SThomas Huth     }
140*831e8822SThomas Huth 
141*831e8822SThomas Huth     DPRINTF("Found buffer: pool=%d count=%d rxbufs=%d\n", pool,
142*831e8822SThomas Huth             dev->rx_pool[pool]->count, dev->rx_bufs);
143*831e8822SThomas Huth 
144*831e8822SThomas Huth     /* Remove the buffer from the pool */
145*831e8822SThomas Huth     dev->rx_pool[pool]->count--;
146*831e8822SThomas Huth     bd = dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count];
147*831e8822SThomas Huth     dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count] = 0;
148*831e8822SThomas Huth 
149*831e8822SThomas Huth     return bd;
150*831e8822SThomas Huth }
151*831e8822SThomas Huth 
152*831e8822SThomas Huth /**
153d6f39fdfSThomas Huth  * Get buffer descriptor from the receive buffer list page that has been
154d6f39fdfSThomas Huth  * supplied by the guest with the H_REGISTER_LOGICAL_LAN call
155d6f39fdfSThomas Huth  */
156d6f39fdfSThomas Huth static vlan_bd_t spapr_vlan_get_rx_bd_from_page(VIOsPAPRVLANDevice *dev,
157d6f39fdfSThomas Huth                                                 size_t size)
158d6f39fdfSThomas Huth {
159d6f39fdfSThomas Huth     int buf_ptr = dev->use_buf_ptr;
160d6f39fdfSThomas Huth     vlan_bd_t bd;
161d6f39fdfSThomas Huth 
162d6f39fdfSThomas Huth     do {
163d6f39fdfSThomas Huth         buf_ptr += 8;
164d6f39fdfSThomas Huth         if (buf_ptr >= VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF) {
165d6f39fdfSThomas Huth             buf_ptr = VLAN_RX_BDS_OFF;
166d6f39fdfSThomas Huth         }
167d6f39fdfSThomas Huth 
168d6f39fdfSThomas Huth         bd = vio_ldq(&dev->sdev, dev->buf_list + buf_ptr);
169d6f39fdfSThomas Huth         DPRINTF("use_buf_ptr=%d bd=0x%016llx\n",
170d6f39fdfSThomas Huth                 buf_ptr, (unsigned long long)bd);
171d6f39fdfSThomas Huth     } while ((!(bd & VLAN_BD_VALID) || VLAN_BD_LEN(bd) < size + 8)
172d6f39fdfSThomas Huth              && buf_ptr != dev->use_buf_ptr);
173d6f39fdfSThomas Huth 
174d6f39fdfSThomas Huth     if (!(bd & VLAN_BD_VALID) || VLAN_BD_LEN(bd) < size + 8) {
175d6f39fdfSThomas Huth         /* Failed to find a suitable buffer */
176d6f39fdfSThomas Huth         return 0;
177d6f39fdfSThomas Huth     }
178d6f39fdfSThomas Huth 
179d6f39fdfSThomas Huth     /* Remove the buffer from the pool */
180d6f39fdfSThomas Huth     dev->use_buf_ptr = buf_ptr;
181d6f39fdfSThomas Huth     vio_stq(&dev->sdev, dev->buf_list + dev->use_buf_ptr, 0);
182d6f39fdfSThomas Huth 
183d6f39fdfSThomas Huth     DPRINTF("Found buffer: ptr=%d rxbufs=%d\n", dev->use_buf_ptr, dev->rx_bufs);
184d6f39fdfSThomas Huth 
185d6f39fdfSThomas Huth     return bd;
186d6f39fdfSThomas Huth }
187d6f39fdfSThomas Huth 
1884e68f7a0SStefan Hajnoczi static ssize_t spapr_vlan_receive(NetClientState *nc, const uint8_t *buf,
1898d90ad90SDavid Gibson                                   size_t size)
1908d90ad90SDavid Gibson {
191fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc);
192fd506b4fSDavid Gibson     VIOsPAPRDevice *sdev = VIO_SPAPR_DEVICE(dev);
193ad0ebb91SDavid Gibson     vlan_bd_t rxq_bd = vio_ldq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF);
1948d90ad90SDavid Gibson     vlan_bd_t bd;
1958d90ad90SDavid Gibson     uint64_t handle;
1968d90ad90SDavid Gibson     uint8_t control;
1978d90ad90SDavid Gibson 
198f6bda9cbSPeter Maydell     DPRINTF("spapr_vlan_receive() [%s] rx_bufs=%d\n", sdev->qdev.id,
1998d90ad90SDavid Gibson             dev->rx_bufs);
2008d90ad90SDavid Gibson 
2018d90ad90SDavid Gibson     if (!dev->isopen) {
2028d90ad90SDavid Gibson         return -1;
2038d90ad90SDavid Gibson     }
2048d90ad90SDavid Gibson 
2058d90ad90SDavid Gibson     if (!dev->rx_bufs) {
2068d90ad90SDavid Gibson         return -1;
2078d90ad90SDavid Gibson     }
2088d90ad90SDavid Gibson 
209*831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
210*831e8822SThomas Huth         bd = spapr_vlan_get_rx_bd_from_pool(dev, size);
211*831e8822SThomas Huth     } else {
212d6f39fdfSThomas Huth         bd = spapr_vlan_get_rx_bd_from_page(dev, size);
213*831e8822SThomas Huth     }
214d6f39fdfSThomas Huth     if (!bd) {
2158d90ad90SDavid Gibson         return -1;
2168d90ad90SDavid Gibson     }
2178d90ad90SDavid Gibson 
2188d90ad90SDavid Gibson     dev->rx_bufs--;
2198d90ad90SDavid Gibson 
2208d90ad90SDavid Gibson     /* Transfer the packet data */
221ad0ebb91SDavid Gibson     if (spapr_vio_dma_write(sdev, VLAN_BD_ADDR(bd) + 8, buf, size) < 0) {
2228d90ad90SDavid Gibson         return -1;
2238d90ad90SDavid Gibson     }
2248d90ad90SDavid Gibson 
225f6bda9cbSPeter Maydell     DPRINTF("spapr_vlan_receive: DMA write completed\n");
2268d90ad90SDavid Gibson 
2278d90ad90SDavid Gibson     /* Update the receive queue */
2288d90ad90SDavid Gibson     control = VLAN_RXQC_TOGGLE | VLAN_RXQC_VALID;
2298d90ad90SDavid Gibson     if (rxq_bd & VLAN_BD_TOGGLE) {
2308d90ad90SDavid Gibson         control ^= VLAN_RXQC_TOGGLE;
2318d90ad90SDavid Gibson     }
2328d90ad90SDavid Gibson 
233ad0ebb91SDavid Gibson     handle = vio_ldq(sdev, VLAN_BD_ADDR(bd));
234ad0ebb91SDavid Gibson     vio_stq(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 8, handle);
235ad0ebb91SDavid Gibson     vio_stl(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 4, size);
236ad0ebb91SDavid Gibson     vio_sth(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 2, 8);
237ad0ebb91SDavid Gibson     vio_stb(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr, control);
2388d90ad90SDavid Gibson 
239f6bda9cbSPeter Maydell     DPRINTF("wrote rxq entry (ptr=0x%llx): 0x%016llx 0x%016llx\n",
2408d90ad90SDavid Gibson             (unsigned long long)dev->rxq_ptr,
241ad0ebb91SDavid Gibson             (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) +
2428d90ad90SDavid Gibson                                         dev->rxq_ptr),
243ad0ebb91SDavid Gibson             (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) +
2448d90ad90SDavid Gibson                                         dev->rxq_ptr + 8));
2458d90ad90SDavid Gibson 
2468d90ad90SDavid Gibson     dev->rxq_ptr += 16;
2478d90ad90SDavid Gibson     if (dev->rxq_ptr >= VLAN_BD_LEN(rxq_bd)) {
2488d90ad90SDavid Gibson         dev->rxq_ptr = 0;
249ad0ebb91SDavid Gibson         vio_stq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF, rxq_bd ^ VLAN_BD_TOGGLE);
2508d90ad90SDavid Gibson     }
2518d90ad90SDavid Gibson 
2528d90ad90SDavid Gibson     if (sdev->signal_state & 1) {
253a307d594SAlexey Kardashevskiy         qemu_irq_pulse(spapr_vio_qirq(sdev));
2548d90ad90SDavid Gibson     }
2558d90ad90SDavid Gibson 
2568d90ad90SDavid Gibson     return size;
2578d90ad90SDavid Gibson }
2588d90ad90SDavid Gibson 
2598d90ad90SDavid Gibson static NetClientInfo net_spapr_vlan_info = {
2602be64a68SLaszlo Ersek     .type = NET_CLIENT_OPTIONS_KIND_NIC,
2618d90ad90SDavid Gibson     .size = sizeof(NICState),
2628d90ad90SDavid Gibson     .can_receive = spapr_vlan_can_receive,
2638d90ad90SDavid Gibson     .receive = spapr_vlan_receive,
2648d90ad90SDavid Gibson };
2658d90ad90SDavid Gibson 
266*831e8822SThomas Huth static void spapr_vlan_reset_rx_pool(RxBufPool *rxp)
267*831e8822SThomas Huth {
268*831e8822SThomas Huth     /*
269*831e8822SThomas Huth      * Use INT_MAX as bufsize so that unused buffers are moved to the end
270*831e8822SThomas Huth      * of the list during the qsort in spapr_vlan_add_rxbuf_to_pool() later.
271*831e8822SThomas Huth      */
272*831e8822SThomas Huth     rxp->bufsize = INT_MAX;
273*831e8822SThomas Huth     rxp->count = 0;
274*831e8822SThomas Huth     memset(rxp->bds, 0, sizeof(rxp->bds));
275*831e8822SThomas Huth }
276*831e8822SThomas Huth 
277c17491b6SDavid Gibson static void spapr_vlan_reset(VIOsPAPRDevice *sdev)
278c17491b6SDavid Gibson {
279fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
280*831e8822SThomas Huth     int i;
281c17491b6SDavid Gibson 
282c17491b6SDavid Gibson     dev->buf_list = 0;
283c17491b6SDavid Gibson     dev->rx_bufs = 0;
284c17491b6SDavid Gibson     dev->isopen = 0;
285*831e8822SThomas Huth 
286*831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
287*831e8822SThomas Huth         for (i = 0; i < RX_MAX_POOLS; i++) {
288*831e8822SThomas Huth             spapr_vlan_reset_rx_pool(dev->rx_pool[i]);
289*831e8822SThomas Huth         }
290*831e8822SThomas Huth     }
291c17491b6SDavid Gibson }
292c17491b6SDavid Gibson 
29328b07e73SMarkus Armbruster static void spapr_vlan_realize(VIOsPAPRDevice *sdev, Error **errp)
2948d90ad90SDavid Gibson {
295fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
2968d90ad90SDavid Gibson 
2978d90ad90SDavid Gibson     qemu_macaddr_default_if_unset(&dev->nicconf.macaddr);
2988d90ad90SDavid Gibson 
2998d90ad90SDavid Gibson     dev->nic = qemu_new_nic(&net_spapr_vlan_info, &dev->nicconf,
300f79f2bfcSAnthony Liguori                             object_get_typename(OBJECT(sdev)), sdev->qdev.id, dev);
301b356f76dSJason Wang     qemu_format_nic_info_str(qemu_get_queue(dev->nic), dev->nicconf.macaddr.a);
3028d90ad90SDavid Gibson }
3038d90ad90SDavid Gibson 
304dfe79cf2SGonglei static void spapr_vlan_instance_init(Object *obj)
305dfe79cf2SGonglei {
306dfe79cf2SGonglei     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(obj);
307*831e8822SThomas Huth     int i;
308dfe79cf2SGonglei 
309dfe79cf2SGonglei     device_add_bootindex_property(obj, &dev->nicconf.bootindex,
310dfe79cf2SGonglei                                   "bootindex", "",
311dfe79cf2SGonglei                                   DEVICE(dev), NULL);
312*831e8822SThomas Huth 
313*831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
314*831e8822SThomas Huth         for (i = 0; i < RX_MAX_POOLS; i++) {
315*831e8822SThomas Huth             dev->rx_pool[i] = g_new(RxBufPool, 1);
316*831e8822SThomas Huth             spapr_vlan_reset_rx_pool(dev->rx_pool[i]);
317*831e8822SThomas Huth         }
318*831e8822SThomas Huth     }
319*831e8822SThomas Huth }
320*831e8822SThomas Huth 
321*831e8822SThomas Huth static void spapr_vlan_instance_finalize(Object *obj)
322*831e8822SThomas Huth {
323*831e8822SThomas Huth     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(obj);
324*831e8822SThomas Huth     int i;
325*831e8822SThomas Huth 
326*831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
327*831e8822SThomas Huth         for (i = 0; i < RX_MAX_POOLS; i++) {
328*831e8822SThomas Huth             g_free(dev->rx_pool[i]);
329*831e8822SThomas Huth             dev->rx_pool[i] = NULL;
330*831e8822SThomas Huth         }
331*831e8822SThomas Huth     }
332dfe79cf2SGonglei }
333dfe79cf2SGonglei 
334d601fac4SDavid Gibson void spapr_vlan_create(VIOsPAPRBus *bus, NICInfo *nd)
3358d90ad90SDavid Gibson {
3368d90ad90SDavid Gibson     DeviceState *dev;
3378d90ad90SDavid Gibson 
3388d90ad90SDavid Gibson     dev = qdev_create(&bus->bus, "spapr-vlan");
3398d90ad90SDavid Gibson 
3408d90ad90SDavid Gibson     qdev_set_nic_properties(dev, nd);
3418d90ad90SDavid Gibson 
3428d90ad90SDavid Gibson     qdev_init_nofail(dev);
3438d90ad90SDavid Gibson }
3448d90ad90SDavid Gibson 
3458d90ad90SDavid Gibson static int spapr_vlan_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
3468d90ad90SDavid Gibson {
347fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *vdev = VIO_SPAPR_VLAN_DEVICE(dev);
3488d90ad90SDavid Gibson     uint8_t padded_mac[8] = {0, 0};
3498d90ad90SDavid Gibson     int ret;
3508d90ad90SDavid Gibson 
3518d90ad90SDavid Gibson     /* Some old phyp versions give the mac address in an 8-byte
3528d90ad90SDavid Gibson      * property.  The kernel driver has an insane workaround for this;
3538d90ad90SDavid Gibson      * rather than doing the obvious thing and checking the property
3548d90ad90SDavid Gibson      * length, it checks whether the first byte has 0b10 in the low
3558d90ad90SDavid Gibson      * bits.  If a correct 6-byte property has a different first byte
3568d90ad90SDavid Gibson      * the kernel will get the wrong mac address, overrunning its
3578d90ad90SDavid Gibson      * buffer in the process (read only, thank goodness).
3588d90ad90SDavid Gibson      *
3598d90ad90SDavid Gibson      * Here we workaround the kernel workaround by always supplying an
3608d90ad90SDavid Gibson      * 8-byte property, with the mac address in the last six bytes */
3618d90ad90SDavid Gibson     memcpy(&padded_mac[2], &vdev->nicconf.macaddr, ETH_ALEN);
3628d90ad90SDavid Gibson     ret = fdt_setprop(fdt, node_off, "local-mac-address",
3638d90ad90SDavid Gibson                       padded_mac, sizeof(padded_mac));
3648d90ad90SDavid Gibson     if (ret < 0) {
3658d90ad90SDavid Gibson         return ret;
3668d90ad90SDavid Gibson     }
3678d90ad90SDavid Gibson 
3688d90ad90SDavid Gibson     ret = fdt_setprop_cell(fdt, node_off, "ibm,mac-address-filters", 0);
3698d90ad90SDavid Gibson     if (ret < 0) {
3708d90ad90SDavid Gibson         return ret;
3718d90ad90SDavid Gibson     }
3728d90ad90SDavid Gibson 
3738d90ad90SDavid Gibson     return 0;
3748d90ad90SDavid Gibson }
3758d90ad90SDavid Gibson 
3768d90ad90SDavid Gibson static int check_bd(VIOsPAPRVLANDevice *dev, vlan_bd_t bd,
3778d90ad90SDavid Gibson                     target_ulong alignment)
3788d90ad90SDavid Gibson {
3798d90ad90SDavid Gibson     if ((VLAN_BD_ADDR(bd) % alignment)
3808d90ad90SDavid Gibson         || (VLAN_BD_LEN(bd) % alignment)) {
3818d90ad90SDavid Gibson         return -1;
3828d90ad90SDavid Gibson     }
3838d90ad90SDavid Gibson 
384ad0ebb91SDavid Gibson     if (!spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd),
385ad0ebb91SDavid Gibson                              VLAN_BD_LEN(bd), DMA_DIRECTION_FROM_DEVICE)
386ad0ebb91SDavid Gibson         || !spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd),
387ad0ebb91SDavid Gibson                                 VLAN_BD_LEN(bd), DMA_DIRECTION_TO_DEVICE)) {
3888d90ad90SDavid Gibson         return -1;
3898d90ad90SDavid Gibson     }
3908d90ad90SDavid Gibson 
3918d90ad90SDavid Gibson     return 0;
3928d90ad90SDavid Gibson }
3938d90ad90SDavid Gibson 
394b13ce26dSAndreas Färber static target_ulong h_register_logical_lan(PowerPCCPU *cpu,
39528e02042SDavid Gibson                                            sPAPRMachineState *spapr,
3968d90ad90SDavid Gibson                                            target_ulong opcode,
3978d90ad90SDavid Gibson                                            target_ulong *args)
3988d90ad90SDavid Gibson {
3998d90ad90SDavid Gibson     target_ulong reg = args[0];
4008d90ad90SDavid Gibson     target_ulong buf_list = args[1];
4018d90ad90SDavid Gibson     target_ulong rec_queue = args[2];
4028d90ad90SDavid Gibson     target_ulong filter_list = args[3];
4038d90ad90SDavid Gibson     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
404fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
4058d90ad90SDavid Gibson     vlan_bd_t filter_list_bd;
4068d90ad90SDavid Gibson 
4078d90ad90SDavid Gibson     if (!dev) {
4088d90ad90SDavid Gibson         return H_PARAMETER;
4098d90ad90SDavid Gibson     }
4108d90ad90SDavid Gibson 
4118d90ad90SDavid Gibson     if (dev->isopen) {
4128d90ad90SDavid Gibson         hcall_dprintf("H_REGISTER_LOGICAL_LAN called twice without "
4138d90ad90SDavid Gibson                       "H_FREE_LOGICAL_LAN\n");
4148d90ad90SDavid Gibson         return H_RESOURCE;
4158d90ad90SDavid Gibson     }
4168d90ad90SDavid Gibson 
417ad0ebb91SDavid Gibson     if (check_bd(dev, VLAN_VALID_BD(buf_list, SPAPR_TCE_PAGE_SIZE),
418ad0ebb91SDavid Gibson                  SPAPR_TCE_PAGE_SIZE) < 0) {
419d9599c92SDavid Gibson         hcall_dprintf("Bad buf_list 0x" TARGET_FMT_lx "\n", buf_list);
4208d90ad90SDavid Gibson         return H_PARAMETER;
4218d90ad90SDavid Gibson     }
4228d90ad90SDavid Gibson 
423ad0ebb91SDavid Gibson     filter_list_bd = VLAN_VALID_BD(filter_list, SPAPR_TCE_PAGE_SIZE);
424ad0ebb91SDavid Gibson     if (check_bd(dev, filter_list_bd, SPAPR_TCE_PAGE_SIZE) < 0) {
425d9599c92SDavid Gibson         hcall_dprintf("Bad filter_list 0x" TARGET_FMT_lx "\n", filter_list);
4268d90ad90SDavid Gibson         return H_PARAMETER;
4278d90ad90SDavid Gibson     }
4288d90ad90SDavid Gibson 
4298d90ad90SDavid Gibson     if (!(rec_queue & VLAN_BD_VALID)
4308d90ad90SDavid Gibson         || (check_bd(dev, rec_queue, VLAN_RQ_ALIGNMENT) < 0)) {
431d9599c92SDavid Gibson         hcall_dprintf("Bad receive queue\n");
4328d90ad90SDavid Gibson         return H_PARAMETER;
4338d90ad90SDavid Gibson     }
4348d90ad90SDavid Gibson 
4358d90ad90SDavid Gibson     dev->buf_list = buf_list;
4368d90ad90SDavid Gibson     sdev->signal_state = 0;
4378d90ad90SDavid Gibson 
4388d90ad90SDavid Gibson     rec_queue &= ~VLAN_BD_TOGGLE;
4398d90ad90SDavid Gibson 
4408d90ad90SDavid Gibson     /* Initialize the buffer list */
441ad0ebb91SDavid Gibson     vio_stq(sdev, buf_list, rec_queue);
442ad0ebb91SDavid Gibson     vio_stq(sdev, buf_list + 8, filter_list_bd);
443ad0ebb91SDavid Gibson     spapr_vio_dma_set(sdev, buf_list + VLAN_RX_BDS_OFF, 0,
444ad0ebb91SDavid Gibson                       SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF);
4458d90ad90SDavid Gibson     dev->add_buf_ptr = VLAN_RX_BDS_OFF - 8;
4468d90ad90SDavid Gibson     dev->use_buf_ptr = VLAN_RX_BDS_OFF - 8;
4478d90ad90SDavid Gibson     dev->rx_bufs = 0;
4488d90ad90SDavid Gibson     dev->rxq_ptr = 0;
4498d90ad90SDavid Gibson 
4508d90ad90SDavid Gibson     /* Initialize the receive queue */
451ad0ebb91SDavid Gibson     spapr_vio_dma_set(sdev, VLAN_BD_ADDR(rec_queue), 0, VLAN_BD_LEN(rec_queue));
4528d90ad90SDavid Gibson 
4538d90ad90SDavid Gibson     dev->isopen = 1;
454e0ff466cSAlexey Kardashevskiy     qemu_flush_queued_packets(qemu_get_queue(dev->nic));
455e0ff466cSAlexey Kardashevskiy 
4568d90ad90SDavid Gibson     return H_SUCCESS;
4578d90ad90SDavid Gibson }
4588d90ad90SDavid Gibson 
4598d90ad90SDavid Gibson 
46028e02042SDavid Gibson static target_ulong h_free_logical_lan(PowerPCCPU *cpu,
46128e02042SDavid Gibson                                        sPAPRMachineState *spapr,
4628d90ad90SDavid Gibson                                        target_ulong opcode, target_ulong *args)
4638d90ad90SDavid Gibson {
4648d90ad90SDavid Gibson     target_ulong reg = args[0];
4658d90ad90SDavid Gibson     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
466fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
4678d90ad90SDavid Gibson 
4688d90ad90SDavid Gibson     if (!dev) {
4698d90ad90SDavid Gibson         return H_PARAMETER;
4708d90ad90SDavid Gibson     }
4718d90ad90SDavid Gibson 
4728d90ad90SDavid Gibson     if (!dev->isopen) {
4738d90ad90SDavid Gibson         hcall_dprintf("H_FREE_LOGICAL_LAN called without "
4748d90ad90SDavid Gibson                       "H_REGISTER_LOGICAL_LAN\n");
4758d90ad90SDavid Gibson         return H_RESOURCE;
4768d90ad90SDavid Gibson     }
4778d90ad90SDavid Gibson 
478c17491b6SDavid Gibson     spapr_vlan_reset(sdev);
4798d90ad90SDavid Gibson     return H_SUCCESS;
4808d90ad90SDavid Gibson }
4818d90ad90SDavid Gibson 
482*831e8822SThomas Huth /**
483*831e8822SThomas Huth  * Used for qsort, this function compares two RxBufPools by size.
484*831e8822SThomas Huth  */
485*831e8822SThomas Huth static int rx_pool_size_compare(const void *p1, const void *p2)
486*831e8822SThomas Huth {
487*831e8822SThomas Huth     const RxBufPool *pool1 = *(RxBufPool **)p1;
488*831e8822SThomas Huth     const RxBufPool *pool2 = *(RxBufPool **)p2;
489*831e8822SThomas Huth 
490*831e8822SThomas Huth     if (pool1->bufsize < pool2->bufsize) {
491*831e8822SThomas Huth         return -1;
492*831e8822SThomas Huth     }
493*831e8822SThomas Huth     return pool1->bufsize > pool2->bufsize;
494*831e8822SThomas Huth }
495*831e8822SThomas Huth 
496*831e8822SThomas Huth /**
497*831e8822SThomas Huth  * Search for a matching buffer pool with exact matching size,
498*831e8822SThomas Huth  * or return -1 if no matching pool has been found.
499*831e8822SThomas Huth  */
500*831e8822SThomas Huth static int spapr_vlan_get_rx_pool_id(VIOsPAPRVLANDevice *dev, int size)
501*831e8822SThomas Huth {
502*831e8822SThomas Huth     int pool;
503*831e8822SThomas Huth 
504*831e8822SThomas Huth     for (pool = 0; pool < RX_MAX_POOLS; pool++) {
505*831e8822SThomas Huth         if (dev->rx_pool[pool]->bufsize == size) {
506*831e8822SThomas Huth             return pool;
507*831e8822SThomas Huth         }
508*831e8822SThomas Huth     }
509*831e8822SThomas Huth 
510*831e8822SThomas Huth     return -1;
511*831e8822SThomas Huth }
512*831e8822SThomas Huth 
513*831e8822SThomas Huth /**
514*831e8822SThomas Huth  * Enqueuing receive buffer by adding it to one of our receive buffer pools
515*831e8822SThomas Huth  */
516*831e8822SThomas Huth static target_long spapr_vlan_add_rxbuf_to_pool(VIOsPAPRVLANDevice *dev,
517*831e8822SThomas Huth                                                 target_ulong buf)
518*831e8822SThomas Huth {
519*831e8822SThomas Huth     int size = VLAN_BD_LEN(buf);
520*831e8822SThomas Huth     int pool;
521*831e8822SThomas Huth 
522*831e8822SThomas Huth     pool = spapr_vlan_get_rx_pool_id(dev, size);
523*831e8822SThomas Huth     if (pool < 0) {
524*831e8822SThomas Huth         /*
525*831e8822SThomas Huth          * No matching pool found? Try to use a new one. If the guest used all
526*831e8822SThomas Huth          * pools before, but changed the size of one pool inbetween, we might
527*831e8822SThomas Huth          * need to recycle that pool here (if it's empty already). Thus scan
528*831e8822SThomas Huth          * all buffer pools now, starting with the last (likely empty) one.
529*831e8822SThomas Huth          */
530*831e8822SThomas Huth         for (pool = RX_MAX_POOLS - 1; pool >= 0 ; pool--) {
531*831e8822SThomas Huth             if (dev->rx_pool[pool]->count == 0) {
532*831e8822SThomas Huth                 dev->rx_pool[pool]->bufsize = size;
533*831e8822SThomas Huth                 /*
534*831e8822SThomas Huth                  * Sort pools by size so that spapr_vlan_receive()
535*831e8822SThomas Huth                  * can later find the smallest buffer pool easily.
536*831e8822SThomas Huth                  */
537*831e8822SThomas Huth                 qsort(dev->rx_pool, RX_MAX_POOLS, sizeof(dev->rx_pool[0]),
538*831e8822SThomas Huth                       rx_pool_size_compare);
539*831e8822SThomas Huth                 pool = spapr_vlan_get_rx_pool_id(dev, size);
540*831e8822SThomas Huth                 DPRINTF("created RX pool %d for size %lld\n", pool,
541*831e8822SThomas Huth                         VLAN_BD_LEN(buf));
542*831e8822SThomas Huth                 break;
543*831e8822SThomas Huth             }
544*831e8822SThomas Huth         }
545*831e8822SThomas Huth     }
546*831e8822SThomas Huth     /* Still no usable pool? Give up */
547*831e8822SThomas Huth     if (pool < 0 || dev->rx_pool[pool]->count >= RX_POOL_MAX_BDS) {
548*831e8822SThomas Huth         return H_RESOURCE;
549*831e8822SThomas Huth     }
550*831e8822SThomas Huth 
551*831e8822SThomas Huth     DPRINTF("h_add_llan_buf():  Add buf using pool %i (size %lli, count=%i)\n",
552*831e8822SThomas Huth             pool, VLAN_BD_LEN(buf), dev->rx_pool[pool]->count);
553*831e8822SThomas Huth 
554*831e8822SThomas Huth     dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count++] = buf;
555*831e8822SThomas Huth 
556*831e8822SThomas Huth     return 0;
557*831e8822SThomas Huth }
558*831e8822SThomas Huth 
559*831e8822SThomas Huth /**
560*831e8822SThomas Huth  * This is the old way of enqueuing receive buffers: Add it to the rx queue
561*831e8822SThomas Huth  * page that has been supplied by the guest (which is quite limited in size).
562*831e8822SThomas Huth  */
563d6f39fdfSThomas Huth static target_long spapr_vlan_add_rxbuf_to_page(VIOsPAPRVLANDevice *dev,
564d6f39fdfSThomas Huth                                                 target_ulong buf)
565d6f39fdfSThomas Huth {
566d6f39fdfSThomas Huth     vlan_bd_t bd;
567d6f39fdfSThomas Huth 
568d6f39fdfSThomas Huth     if (dev->rx_bufs >= VLAN_MAX_BUFS) {
569d6f39fdfSThomas Huth         return H_RESOURCE;
570d6f39fdfSThomas Huth     }
571d6f39fdfSThomas Huth 
572d6f39fdfSThomas Huth     do {
573d6f39fdfSThomas Huth         dev->add_buf_ptr += 8;
574d6f39fdfSThomas Huth         if (dev->add_buf_ptr >= VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF) {
575d6f39fdfSThomas Huth             dev->add_buf_ptr = VLAN_RX_BDS_OFF;
576d6f39fdfSThomas Huth         }
577d6f39fdfSThomas Huth 
578d6f39fdfSThomas Huth         bd = vio_ldq(&dev->sdev, dev->buf_list + dev->add_buf_ptr);
579d6f39fdfSThomas Huth     } while (bd & VLAN_BD_VALID);
580d6f39fdfSThomas Huth 
581d6f39fdfSThomas Huth     vio_stq(&dev->sdev, dev->buf_list + dev->add_buf_ptr, buf);
582d6f39fdfSThomas Huth 
583d6f39fdfSThomas Huth     DPRINTF("h_add_llan_buf():  Added buf  ptr=%d  rx_bufs=%d bd=0x%016llx\n",
584d6f39fdfSThomas Huth             dev->add_buf_ptr, dev->rx_bufs, (unsigned long long)buf);
585d6f39fdfSThomas Huth 
586d6f39fdfSThomas Huth     return 0;
587d6f39fdfSThomas Huth }
588d6f39fdfSThomas Huth 
589b13ce26dSAndreas Färber static target_ulong h_add_logical_lan_buffer(PowerPCCPU *cpu,
59028e02042SDavid Gibson                                              sPAPRMachineState *spapr,
5918d90ad90SDavid Gibson                                              target_ulong opcode,
5928d90ad90SDavid Gibson                                              target_ulong *args)
5938d90ad90SDavid Gibson {
5948d90ad90SDavid Gibson     target_ulong reg = args[0];
5958d90ad90SDavid Gibson     target_ulong buf = args[1];
5968d90ad90SDavid Gibson     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
597fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
598d6f39fdfSThomas Huth     target_long ret;
5998d90ad90SDavid Gibson 
600f6bda9cbSPeter Maydell     DPRINTF("H_ADD_LOGICAL_LAN_BUFFER(0x" TARGET_FMT_lx
6018d90ad90SDavid Gibson             ", 0x" TARGET_FMT_lx ")\n", reg, buf);
6028d90ad90SDavid Gibson 
6038d90ad90SDavid Gibson     if (!sdev) {
604d9599c92SDavid Gibson         hcall_dprintf("Bad device\n");
6058d90ad90SDavid Gibson         return H_PARAMETER;
6068d90ad90SDavid Gibson     }
6078d90ad90SDavid Gibson 
6088d90ad90SDavid Gibson     if ((check_bd(dev, buf, 4) < 0)
6098d90ad90SDavid Gibson         || (VLAN_BD_LEN(buf) < 16)) {
610d9599c92SDavid Gibson         hcall_dprintf("Bad buffer enqueued\n");
6118d90ad90SDavid Gibson         return H_PARAMETER;
6128d90ad90SDavid Gibson     }
6138d90ad90SDavid Gibson 
614d6f39fdfSThomas Huth     if (!dev->isopen) {
6158d90ad90SDavid Gibson         return H_RESOURCE;
6168d90ad90SDavid Gibson     }
6178d90ad90SDavid Gibson 
618*831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
619*831e8822SThomas Huth         ret = spapr_vlan_add_rxbuf_to_pool(dev, buf);
620*831e8822SThomas Huth     } else {
621d6f39fdfSThomas Huth         ret = spapr_vlan_add_rxbuf_to_page(dev, buf);
622*831e8822SThomas Huth     }
623d6f39fdfSThomas Huth     if (ret) {
624d6f39fdfSThomas Huth         return ret;
6258d90ad90SDavid Gibson     }
6268d90ad90SDavid Gibson 
6278d90ad90SDavid Gibson     dev->rx_bufs++;
6288d90ad90SDavid Gibson 
6290a61f3b4SAlexey Kardashevskiy     qemu_flush_queued_packets(qemu_get_queue(dev->nic));
6300a61f3b4SAlexey Kardashevskiy 
6318d90ad90SDavid Gibson     return H_SUCCESS;
6328d90ad90SDavid Gibson }
6338d90ad90SDavid Gibson 
63428e02042SDavid Gibson static target_ulong h_send_logical_lan(PowerPCCPU *cpu,
63528e02042SDavid Gibson                                        sPAPRMachineState *spapr,
6368d90ad90SDavid Gibson                                        target_ulong opcode, target_ulong *args)
6378d90ad90SDavid Gibson {
6388d90ad90SDavid Gibson     target_ulong reg = args[0];
6398d90ad90SDavid Gibson     target_ulong *bufs = args + 1;
6408d90ad90SDavid Gibson     target_ulong continue_token = args[7];
6418d90ad90SDavid Gibson     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
642fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
6438d90ad90SDavid Gibson     unsigned total_len;
6448d90ad90SDavid Gibson     uint8_t *lbuf, *p;
6458d90ad90SDavid Gibson     int i, nbufs;
6468d90ad90SDavid Gibson     int ret;
6478d90ad90SDavid Gibson 
648f6bda9cbSPeter Maydell     DPRINTF("H_SEND_LOGICAL_LAN(0x" TARGET_FMT_lx ", <bufs>, 0x"
6498d90ad90SDavid Gibson             TARGET_FMT_lx ")\n", reg, continue_token);
6508d90ad90SDavid Gibson 
6518d90ad90SDavid Gibson     if (!sdev) {
6528d90ad90SDavid Gibson         return H_PARAMETER;
6538d90ad90SDavid Gibson     }
6548d90ad90SDavid Gibson 
655f6bda9cbSPeter Maydell     DPRINTF("rxbufs = %d\n", dev->rx_bufs);
6568d90ad90SDavid Gibson 
6578d90ad90SDavid Gibson     if (!dev->isopen) {
6588d90ad90SDavid Gibson         return H_DROPPED;
6598d90ad90SDavid Gibson     }
6608d90ad90SDavid Gibson 
6618d90ad90SDavid Gibson     if (continue_token) {
6628d90ad90SDavid Gibson         return H_HARDWARE; /* FIXME actually handle this */
6638d90ad90SDavid Gibson     }
6648d90ad90SDavid Gibson 
6658d90ad90SDavid Gibson     total_len = 0;
6668d90ad90SDavid Gibson     for (i = 0; i < 6; i++) {
667f6bda9cbSPeter Maydell         DPRINTF("   buf desc: 0x" TARGET_FMT_lx "\n", bufs[i]);
6688d90ad90SDavid Gibson         if (!(bufs[i] & VLAN_BD_VALID)) {
6698d90ad90SDavid Gibson             break;
6708d90ad90SDavid Gibson         }
6718d90ad90SDavid Gibson         total_len += VLAN_BD_LEN(bufs[i]);
6728d90ad90SDavid Gibson     }
6738d90ad90SDavid Gibson 
6748d90ad90SDavid Gibson     nbufs = i;
675f6bda9cbSPeter Maydell     DPRINTF("h_send_logical_lan() %d buffers, total length 0x%x\n",
6768d90ad90SDavid Gibson             nbufs, total_len);
6778d90ad90SDavid Gibson 
6788d90ad90SDavid Gibson     if (total_len == 0) {
6798d90ad90SDavid Gibson         return H_SUCCESS;
6808d90ad90SDavid Gibson     }
6818d90ad90SDavid Gibson 
6828d90ad90SDavid Gibson     if (total_len > MAX_PACKET_SIZE) {
6838d90ad90SDavid Gibson         /* Don't let the guest force too large an allocation */
6848d90ad90SDavid Gibson         return H_RESOURCE;
6858d90ad90SDavid Gibson     }
6868d90ad90SDavid Gibson 
6878d90ad90SDavid Gibson     lbuf = alloca(total_len);
6888d90ad90SDavid Gibson     p = lbuf;
6898d90ad90SDavid Gibson     for (i = 0; i < nbufs; i++) {
690ad0ebb91SDavid Gibson         ret = spapr_vio_dma_read(sdev, VLAN_BD_ADDR(bufs[i]),
6918d90ad90SDavid Gibson                                  p, VLAN_BD_LEN(bufs[i]));
6928d90ad90SDavid Gibson         if (ret < 0) {
6938d90ad90SDavid Gibson             return ret;
6948d90ad90SDavid Gibson         }
6958d90ad90SDavid Gibson 
6968d90ad90SDavid Gibson         p += VLAN_BD_LEN(bufs[i]);
6978d90ad90SDavid Gibson     }
6988d90ad90SDavid Gibson 
699b356f76dSJason Wang     qemu_send_packet(qemu_get_queue(dev->nic), lbuf, total_len);
7008d90ad90SDavid Gibson 
7018d90ad90SDavid Gibson     return H_SUCCESS;
7028d90ad90SDavid Gibson }
7038d90ad90SDavid Gibson 
70428e02042SDavid Gibson static target_ulong h_multicast_ctrl(PowerPCCPU *cpu, sPAPRMachineState *spapr,
7058d90ad90SDavid Gibson                                      target_ulong opcode, target_ulong *args)
7068d90ad90SDavid Gibson {
7078d90ad90SDavid Gibson     target_ulong reg = args[0];
7088d90ad90SDavid Gibson     VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
7098d90ad90SDavid Gibson 
7108d90ad90SDavid Gibson     if (!dev) {
7118d90ad90SDavid Gibson         return H_PARAMETER;
7128d90ad90SDavid Gibson     }
7138d90ad90SDavid Gibson 
7148d90ad90SDavid Gibson     return H_SUCCESS;
7158d90ad90SDavid Gibson }
7168d90ad90SDavid Gibson 
7173954d33aSAnthony Liguori static Property spapr_vlan_properties[] = {
718ad0ebb91SDavid Gibson     DEFINE_SPAPR_PROPERTIES(VIOsPAPRVLANDevice, sdev),
7198d90ad90SDavid Gibson     DEFINE_NIC_PROPERTIES(VIOsPAPRVLANDevice, nicconf),
720*831e8822SThomas Huth     DEFINE_PROP_BIT("use-rx-buffer-pools", VIOsPAPRVLANDevice,
721*831e8822SThomas Huth                     compat_flags, SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT, false),
7228d90ad90SDavid Gibson     DEFINE_PROP_END_OF_LIST(),
7233954d33aSAnthony Liguori };
7243954d33aSAnthony Liguori 
725*831e8822SThomas Huth static bool spapr_vlan_rx_buffer_pools_needed(void *opaque)
726*831e8822SThomas Huth {
727*831e8822SThomas Huth     VIOsPAPRVLANDevice *dev = opaque;
728*831e8822SThomas Huth 
729*831e8822SThomas Huth     return (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) != 0;
730*831e8822SThomas Huth }
731*831e8822SThomas Huth 
732*831e8822SThomas Huth static const VMStateDescription vmstate_rx_buffer_pool = {
733*831e8822SThomas Huth     .name = "spapr_llan/rx_buffer_pool",
734*831e8822SThomas Huth     .version_id = 1,
735*831e8822SThomas Huth     .minimum_version_id = 1,
736*831e8822SThomas Huth     .needed = spapr_vlan_rx_buffer_pools_needed,
737*831e8822SThomas Huth     .fields = (VMStateField[]) {
738*831e8822SThomas Huth         VMSTATE_INT32(bufsize, RxBufPool),
739*831e8822SThomas Huth         VMSTATE_INT32(count, RxBufPool),
740*831e8822SThomas Huth         VMSTATE_UINT64_ARRAY(bds, RxBufPool, RX_POOL_MAX_BDS),
741*831e8822SThomas Huth         VMSTATE_END_OF_LIST()
742*831e8822SThomas Huth     }
743*831e8822SThomas Huth };
744*831e8822SThomas Huth 
745*831e8822SThomas Huth static const VMStateDescription vmstate_rx_pools = {
746*831e8822SThomas Huth     .name = "spapr_llan/rx_pools",
747*831e8822SThomas Huth     .version_id = 1,
748*831e8822SThomas Huth     .minimum_version_id = 1,
749*831e8822SThomas Huth     .needed = spapr_vlan_rx_buffer_pools_needed,
750*831e8822SThomas Huth     .fields = (VMStateField[]) {
751*831e8822SThomas Huth         VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(rx_pool, VIOsPAPRVLANDevice,
752*831e8822SThomas Huth                                            RX_MAX_POOLS, 1,
753*831e8822SThomas Huth                                            vmstate_rx_buffer_pool, RxBufPool),
754*831e8822SThomas Huth         VMSTATE_END_OF_LIST()
755*831e8822SThomas Huth     }
756*831e8822SThomas Huth };
757*831e8822SThomas Huth 
758686fefe4SDavid Gibson static const VMStateDescription vmstate_spapr_llan = {
759686fefe4SDavid Gibson     .name = "spapr_llan",
760686fefe4SDavid Gibson     .version_id = 1,
761686fefe4SDavid Gibson     .minimum_version_id = 1,
762686fefe4SDavid Gibson     .fields = (VMStateField[]) {
763686fefe4SDavid Gibson         VMSTATE_SPAPR_VIO(sdev, VIOsPAPRVLANDevice),
764686fefe4SDavid Gibson         /* LLAN state */
765686fefe4SDavid Gibson         VMSTATE_BOOL(isopen, VIOsPAPRVLANDevice),
766686fefe4SDavid Gibson         VMSTATE_UINTTL(buf_list, VIOsPAPRVLANDevice),
767686fefe4SDavid Gibson         VMSTATE_UINT32(add_buf_ptr, VIOsPAPRVLANDevice),
768686fefe4SDavid Gibson         VMSTATE_UINT32(use_buf_ptr, VIOsPAPRVLANDevice),
769686fefe4SDavid Gibson         VMSTATE_UINT32(rx_bufs, VIOsPAPRVLANDevice),
770686fefe4SDavid Gibson         VMSTATE_UINTTL(rxq_ptr, VIOsPAPRVLANDevice),
771686fefe4SDavid Gibson 
772686fefe4SDavid Gibson         VMSTATE_END_OF_LIST()
773686fefe4SDavid Gibson     },
774*831e8822SThomas Huth     .subsections = (const VMStateDescription * []) {
775*831e8822SThomas Huth         &vmstate_rx_pools,
776*831e8822SThomas Huth         NULL
777*831e8822SThomas Huth     }
778686fefe4SDavid Gibson };
779686fefe4SDavid Gibson 
7803954d33aSAnthony Liguori static void spapr_vlan_class_init(ObjectClass *klass, void *data)
7813954d33aSAnthony Liguori {
78239bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
7833954d33aSAnthony Liguori     VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
7843954d33aSAnthony Liguori 
78528b07e73SMarkus Armbruster     k->realize = spapr_vlan_realize;
786c17491b6SDavid Gibson     k->reset = spapr_vlan_reset;
7873954d33aSAnthony Liguori     k->devnode = spapr_vlan_devnode;
7883954d33aSAnthony Liguori     k->dt_name = "l-lan";
7893954d33aSAnthony Liguori     k->dt_type = "network";
7903954d33aSAnthony Liguori     k->dt_compatible = "IBM,l-lan";
7913954d33aSAnthony Liguori     k->signal_mask = 0x1;
79229fdedfeSAlexey Kardashevskiy     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
79339bffca2SAnthony Liguori     dc->props = spapr_vlan_properties;
794ad0ebb91SDavid Gibson     k->rtce_window_size = 0x10000000;
795686fefe4SDavid Gibson     dc->vmsd = &vmstate_spapr_llan;
7963954d33aSAnthony Liguori }
7973954d33aSAnthony Liguori 
7988c43a6f0SAndreas Färber static const TypeInfo spapr_vlan_info = {
799fd506b4fSDavid Gibson     .name          = TYPE_VIO_SPAPR_VLAN_DEVICE,
80039bffca2SAnthony Liguori     .parent        = TYPE_VIO_SPAPR_DEVICE,
80139bffca2SAnthony Liguori     .instance_size = sizeof(VIOsPAPRVLANDevice),
8023954d33aSAnthony Liguori     .class_init    = spapr_vlan_class_init,
803dfe79cf2SGonglei     .instance_init = spapr_vlan_instance_init,
804*831e8822SThomas Huth     .instance_finalize = spapr_vlan_instance_finalize,
8058d90ad90SDavid Gibson };
8068d90ad90SDavid Gibson 
80783f7d43aSAndreas Färber static void spapr_vlan_register_types(void)
8088d90ad90SDavid Gibson {
8091fc02533SDavid Gibson     spapr_register_hypercall(H_REGISTER_LOGICAL_LAN, h_register_logical_lan);
8101fc02533SDavid Gibson     spapr_register_hypercall(H_FREE_LOGICAL_LAN, h_free_logical_lan);
8111fc02533SDavid Gibson     spapr_register_hypercall(H_SEND_LOGICAL_LAN, h_send_logical_lan);
8121fc02533SDavid Gibson     spapr_register_hypercall(H_ADD_LOGICAL_LAN_BUFFER,
8131fc02533SDavid Gibson                              h_add_logical_lan_buffer);
8141fc02533SDavid Gibson     spapr_register_hypercall(H_MULTICAST_CTRL, h_multicast_ctrl);
81539bffca2SAnthony Liguori     type_register_static(&spapr_vlan_info);
8168d90ad90SDavid Gibson }
81783f7d43aSAndreas Färber 
81883f7d43aSAndreas Färber type_init(spapr_vlan_register_types)
819