xref: /qemu/hw/net/spapr_llan.c (revision 5c29dd8c28c3c7718cc37b38c5171b3caf629928)
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"
3103dd024fSPaolo Bonzini #include "qemu/log.h"
321422e32dSPaolo Bonzini #include "net/net.h"
338d90ad90SDavid Gibson #include "hw/qdev.h"
340d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
350d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
36ad4f62d0SAlexey Kardashevskiy #include "sysemu/sysemu.h"
378d90ad90SDavid Gibson 
388d90ad90SDavid Gibson #include <libfdt.h>
398d90ad90SDavid Gibson 
408d90ad90SDavid Gibson #define ETH_ALEN        6
418d90ad90SDavid Gibson #define MAX_PACKET_SIZE 65536
428d90ad90SDavid Gibson 
438d90ad90SDavid Gibson /*#define DEBUG*/
448d90ad90SDavid Gibson 
458d90ad90SDavid Gibson #ifdef DEBUG
46f6bda9cbSPeter Maydell #define DPRINTF(fmt...) do { fprintf(stderr, fmt); } while (0)
478d90ad90SDavid Gibson #else
48f6bda9cbSPeter Maydell #define DPRINTF(fmt...)
498d90ad90SDavid Gibson #endif
508d90ad90SDavid Gibson 
51831e8822SThomas Huth /* Compatibility flags for migration */
52831e8822SThomas Huth #define SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT  0
53831e8822SThomas Huth #define SPAPRVLAN_FLAG_RX_BUF_POOLS      (1 << SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT)
54831e8822SThomas Huth 
558d90ad90SDavid Gibson /*
568d90ad90SDavid Gibson  * Virtual LAN device
578d90ad90SDavid Gibson  */
588d90ad90SDavid Gibson 
598d90ad90SDavid Gibson typedef uint64_t vlan_bd_t;
608d90ad90SDavid Gibson 
618d90ad90SDavid Gibson #define VLAN_BD_VALID        0x8000000000000000ULL
628d90ad90SDavid Gibson #define VLAN_BD_TOGGLE       0x4000000000000000ULL
638d90ad90SDavid Gibson #define VLAN_BD_NO_CSUM      0x0200000000000000ULL
648d90ad90SDavid Gibson #define VLAN_BD_CSUM_GOOD    0x0100000000000000ULL
658d90ad90SDavid Gibson #define VLAN_BD_LEN_MASK     0x00ffffff00000000ULL
668d90ad90SDavid Gibson #define VLAN_BD_LEN(bd)      (((bd) & VLAN_BD_LEN_MASK) >> 32)
678d90ad90SDavid Gibson #define VLAN_BD_ADDR_MASK    0x00000000ffffffffULL
688d90ad90SDavid Gibson #define VLAN_BD_ADDR(bd)     ((bd) & VLAN_BD_ADDR_MASK)
698d90ad90SDavid Gibson 
708d90ad90SDavid Gibson #define VLAN_VALID_BD(addr, len) (VLAN_BD_VALID | \
718d90ad90SDavid Gibson                                   (((len) << 32) & VLAN_BD_LEN_MASK) |  \
728d90ad90SDavid Gibson                                   (addr & VLAN_BD_ADDR_MASK))
738d90ad90SDavid Gibson 
748d90ad90SDavid Gibson #define VLAN_RXQC_TOGGLE     0x80
758d90ad90SDavid Gibson #define VLAN_RXQC_VALID      0x40
768d90ad90SDavid Gibson #define VLAN_RXQC_NO_CSUM    0x02
778d90ad90SDavid Gibson #define VLAN_RXQC_CSUM_GOOD  0x01
788d90ad90SDavid Gibson 
798d90ad90SDavid Gibson #define VLAN_RQ_ALIGNMENT    16
808d90ad90SDavid Gibson #define VLAN_RXQ_BD_OFF      0
818d90ad90SDavid Gibson #define VLAN_FILTER_BD_OFF   8
828d90ad90SDavid Gibson #define VLAN_RX_BDS_OFF      16
83439ce140SAnton Blanchard /*
84439ce140SAnton Blanchard  * The final 8 bytes of the buffer list is a counter of frames dropped
85439ce140SAnton Blanchard  * because there was not a buffer in the buffer list capable of holding
86439ce140SAnton Blanchard  * the frame. We must avoid it, or the operating system will report garbage
87439ce140SAnton Blanchard  * for this statistic.
88439ce140SAnton Blanchard  */
89439ce140SAnton Blanchard #define VLAN_RX_BDS_LEN      (SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF - 8)
90439ce140SAnton Blanchard #define VLAN_MAX_BUFS        (VLAN_RX_BDS_LEN / 8)
918d90ad90SDavid Gibson 
92fd506b4fSDavid Gibson #define TYPE_VIO_SPAPR_VLAN_DEVICE "spapr-vlan"
93fd506b4fSDavid Gibson #define VIO_SPAPR_VLAN_DEVICE(obj) \
94fd506b4fSDavid Gibson      OBJECT_CHECK(VIOsPAPRVLANDevice, (obj), TYPE_VIO_SPAPR_VLAN_DEVICE)
95fd506b4fSDavid Gibson 
96831e8822SThomas Huth #define RX_POOL_MAX_BDS 4096
97831e8822SThomas Huth #define RX_MAX_POOLS 5
98831e8822SThomas Huth 
99831e8822SThomas Huth typedef struct {
100831e8822SThomas Huth     int32_t bufsize;
101831e8822SThomas Huth     int32_t count;
102831e8822SThomas Huth     vlan_bd_t bds[RX_POOL_MAX_BDS];
103831e8822SThomas Huth } RxBufPool;
104831e8822SThomas Huth 
1058d90ad90SDavid Gibson typedef struct VIOsPAPRVLANDevice {
1068d90ad90SDavid Gibson     VIOsPAPRDevice sdev;
1078d90ad90SDavid Gibson     NICConf nicconf;
1088d90ad90SDavid Gibson     NICState *nic;
109686fefe4SDavid Gibson     bool isopen;
110cbd62f86SPaolo Bonzini     hwaddr buf_list;
111686fefe4SDavid Gibson     uint32_t add_buf_ptr, use_buf_ptr, rx_bufs;
112cbd62f86SPaolo Bonzini     hwaddr rxq_ptr;
1138836630fSThomas Huth     QEMUTimer *rxp_timer;
114831e8822SThomas Huth     uint32_t compat_flags;             /* Compatability flags for migration */
115831e8822SThomas Huth     RxBufPool *rx_pool[RX_MAX_POOLS];  /* Receive buffer descriptor pools */
1168d90ad90SDavid Gibson } VIOsPAPRVLANDevice;
1178d90ad90SDavid Gibson 
1184e68f7a0SStefan Hajnoczi static int spapr_vlan_can_receive(NetClientState *nc)
1198d90ad90SDavid Gibson {
120cc1f0f45SJason Wang     VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc);
1218d90ad90SDavid Gibson 
1228d90ad90SDavid Gibson     return (dev->isopen && dev->rx_bufs > 0);
1238d90ad90SDavid Gibson }
1248d90ad90SDavid Gibson 
125d6f39fdfSThomas Huth /**
126*5c29dd8cSThomas Huth  * The last 8 bytes of the receive buffer list page (that has been
127*5c29dd8cSThomas Huth  * supplied by the guest with the H_REGISTER_LOGICAL_LAN call) contain
128*5c29dd8cSThomas Huth  * a counter for frames that have been dropped because there was no
129*5c29dd8cSThomas Huth  * suitable receive buffer available. This function is used to increase
130*5c29dd8cSThomas Huth  * this counter by one.
131*5c29dd8cSThomas Huth  */
132*5c29dd8cSThomas Huth static void spapr_vlan_record_dropped_rx_frame(VIOsPAPRVLANDevice *dev)
133*5c29dd8cSThomas Huth {
134*5c29dd8cSThomas Huth     uint64_t cnt;
135*5c29dd8cSThomas Huth 
136*5c29dd8cSThomas Huth     cnt = vio_ldq(&dev->sdev, dev->buf_list + 4096 - 8);
137*5c29dd8cSThomas Huth     vio_stq(&dev->sdev, dev->buf_list + 4096 - 8, cnt + 1);
138*5c29dd8cSThomas Huth }
139*5c29dd8cSThomas Huth 
140*5c29dd8cSThomas Huth /**
141831e8822SThomas Huth  * Get buffer descriptor from one of our receive buffer pools
142831e8822SThomas Huth  */
143831e8822SThomas Huth static vlan_bd_t spapr_vlan_get_rx_bd_from_pool(VIOsPAPRVLANDevice *dev,
144831e8822SThomas Huth                                                 size_t size)
145831e8822SThomas Huth {
146831e8822SThomas Huth     vlan_bd_t bd;
147831e8822SThomas Huth     int pool;
148831e8822SThomas Huth 
149831e8822SThomas Huth     for (pool = 0; pool < RX_MAX_POOLS; pool++) {
150831e8822SThomas Huth         if (dev->rx_pool[pool]->count > 0 &&
151831e8822SThomas Huth             dev->rx_pool[pool]->bufsize >= size + 8) {
152831e8822SThomas Huth             break;
153831e8822SThomas Huth         }
154831e8822SThomas Huth     }
155831e8822SThomas Huth     if (pool == RX_MAX_POOLS) {
156831e8822SThomas Huth         /* Failed to find a suitable buffer */
157831e8822SThomas Huth         return 0;
158831e8822SThomas Huth     }
159831e8822SThomas Huth 
160831e8822SThomas Huth     DPRINTF("Found buffer: pool=%d count=%d rxbufs=%d\n", pool,
161831e8822SThomas Huth             dev->rx_pool[pool]->count, dev->rx_bufs);
162831e8822SThomas Huth 
163831e8822SThomas Huth     /* Remove the buffer from the pool */
164831e8822SThomas Huth     dev->rx_pool[pool]->count--;
165831e8822SThomas Huth     bd = dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count];
166831e8822SThomas Huth     dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count] = 0;
167831e8822SThomas Huth 
168831e8822SThomas Huth     return bd;
169831e8822SThomas Huth }
170831e8822SThomas Huth 
171831e8822SThomas Huth /**
172d6f39fdfSThomas Huth  * Get buffer descriptor from the receive buffer list page that has been
173d6f39fdfSThomas Huth  * supplied by the guest with the H_REGISTER_LOGICAL_LAN call
174d6f39fdfSThomas Huth  */
175d6f39fdfSThomas Huth static vlan_bd_t spapr_vlan_get_rx_bd_from_page(VIOsPAPRVLANDevice *dev,
176d6f39fdfSThomas Huth                                                 size_t size)
177d6f39fdfSThomas Huth {
178d6f39fdfSThomas Huth     int buf_ptr = dev->use_buf_ptr;
179d6f39fdfSThomas Huth     vlan_bd_t bd;
180d6f39fdfSThomas Huth 
181d6f39fdfSThomas Huth     do {
182d6f39fdfSThomas Huth         buf_ptr += 8;
183d6f39fdfSThomas Huth         if (buf_ptr >= VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF) {
184d6f39fdfSThomas Huth             buf_ptr = VLAN_RX_BDS_OFF;
185d6f39fdfSThomas Huth         }
186d6f39fdfSThomas Huth 
187d6f39fdfSThomas Huth         bd = vio_ldq(&dev->sdev, dev->buf_list + buf_ptr);
188d6f39fdfSThomas Huth         DPRINTF("use_buf_ptr=%d bd=0x%016llx\n",
189d6f39fdfSThomas Huth                 buf_ptr, (unsigned long long)bd);
190d6f39fdfSThomas Huth     } while ((!(bd & VLAN_BD_VALID) || VLAN_BD_LEN(bd) < size + 8)
191d6f39fdfSThomas Huth              && buf_ptr != dev->use_buf_ptr);
192d6f39fdfSThomas Huth 
193d6f39fdfSThomas Huth     if (!(bd & VLAN_BD_VALID) || VLAN_BD_LEN(bd) < size + 8) {
194d6f39fdfSThomas Huth         /* Failed to find a suitable buffer */
195d6f39fdfSThomas Huth         return 0;
196d6f39fdfSThomas Huth     }
197d6f39fdfSThomas Huth 
198d6f39fdfSThomas Huth     /* Remove the buffer from the pool */
199d6f39fdfSThomas Huth     dev->use_buf_ptr = buf_ptr;
200d6f39fdfSThomas Huth     vio_stq(&dev->sdev, dev->buf_list + dev->use_buf_ptr, 0);
201d6f39fdfSThomas Huth 
202d6f39fdfSThomas Huth     DPRINTF("Found buffer: ptr=%d rxbufs=%d\n", dev->use_buf_ptr, dev->rx_bufs);
203d6f39fdfSThomas Huth 
204d6f39fdfSThomas Huth     return bd;
205d6f39fdfSThomas Huth }
206d6f39fdfSThomas Huth 
2074e68f7a0SStefan Hajnoczi static ssize_t spapr_vlan_receive(NetClientState *nc, const uint8_t *buf,
2088d90ad90SDavid Gibson                                   size_t size)
2098d90ad90SDavid Gibson {
210fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc);
211fd506b4fSDavid Gibson     VIOsPAPRDevice *sdev = VIO_SPAPR_DEVICE(dev);
212ad0ebb91SDavid Gibson     vlan_bd_t rxq_bd = vio_ldq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF);
2138d90ad90SDavid Gibson     vlan_bd_t bd;
2148d90ad90SDavid Gibson     uint64_t handle;
2158d90ad90SDavid Gibson     uint8_t control;
2168d90ad90SDavid Gibson 
217f6bda9cbSPeter Maydell     DPRINTF("spapr_vlan_receive() [%s] rx_bufs=%d\n", sdev->qdev.id,
2188d90ad90SDavid Gibson             dev->rx_bufs);
2198d90ad90SDavid Gibson 
2208d90ad90SDavid Gibson     if (!dev->isopen) {
2218d90ad90SDavid Gibson         return -1;
2228d90ad90SDavid Gibson     }
2238d90ad90SDavid Gibson 
2248d90ad90SDavid Gibson     if (!dev->rx_bufs) {
225*5c29dd8cSThomas Huth         spapr_vlan_record_dropped_rx_frame(dev);
2268836630fSThomas Huth         return 0;
2278d90ad90SDavid Gibson     }
2288d90ad90SDavid Gibson 
229831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
230831e8822SThomas Huth         bd = spapr_vlan_get_rx_bd_from_pool(dev, size);
231831e8822SThomas Huth     } else {
232d6f39fdfSThomas Huth         bd = spapr_vlan_get_rx_bd_from_page(dev, size);
233831e8822SThomas Huth     }
234d6f39fdfSThomas Huth     if (!bd) {
235*5c29dd8cSThomas Huth         spapr_vlan_record_dropped_rx_frame(dev);
2368836630fSThomas Huth         return 0;
2378d90ad90SDavid Gibson     }
2388d90ad90SDavid Gibson 
2398d90ad90SDavid Gibson     dev->rx_bufs--;
2408d90ad90SDavid Gibson 
2418d90ad90SDavid Gibson     /* Transfer the packet data */
242ad0ebb91SDavid Gibson     if (spapr_vio_dma_write(sdev, VLAN_BD_ADDR(bd) + 8, buf, size) < 0) {
2438d90ad90SDavid Gibson         return -1;
2448d90ad90SDavid Gibson     }
2458d90ad90SDavid Gibson 
246f6bda9cbSPeter Maydell     DPRINTF("spapr_vlan_receive: DMA write completed\n");
2478d90ad90SDavid Gibson 
2488d90ad90SDavid Gibson     /* Update the receive queue */
2498d90ad90SDavid Gibson     control = VLAN_RXQC_TOGGLE | VLAN_RXQC_VALID;
2508d90ad90SDavid Gibson     if (rxq_bd & VLAN_BD_TOGGLE) {
2518d90ad90SDavid Gibson         control ^= VLAN_RXQC_TOGGLE;
2528d90ad90SDavid Gibson     }
2538d90ad90SDavid Gibson 
254ad0ebb91SDavid Gibson     handle = vio_ldq(sdev, VLAN_BD_ADDR(bd));
255ad0ebb91SDavid Gibson     vio_stq(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 8, handle);
256ad0ebb91SDavid Gibson     vio_stl(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 4, size);
257ad0ebb91SDavid Gibson     vio_sth(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 2, 8);
258ad0ebb91SDavid Gibson     vio_stb(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr, control);
2598d90ad90SDavid Gibson 
260f6bda9cbSPeter Maydell     DPRINTF("wrote rxq entry (ptr=0x%llx): 0x%016llx 0x%016llx\n",
2618d90ad90SDavid Gibson             (unsigned long long)dev->rxq_ptr,
262ad0ebb91SDavid Gibson             (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) +
2638d90ad90SDavid Gibson                                         dev->rxq_ptr),
264ad0ebb91SDavid Gibson             (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) +
2658d90ad90SDavid Gibson                                         dev->rxq_ptr + 8));
2668d90ad90SDavid Gibson 
2678d90ad90SDavid Gibson     dev->rxq_ptr += 16;
2688d90ad90SDavid Gibson     if (dev->rxq_ptr >= VLAN_BD_LEN(rxq_bd)) {
2698d90ad90SDavid Gibson         dev->rxq_ptr = 0;
270ad0ebb91SDavid Gibson         vio_stq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF, rxq_bd ^ VLAN_BD_TOGGLE);
2718d90ad90SDavid Gibson     }
2728d90ad90SDavid Gibson 
2738d90ad90SDavid Gibson     if (sdev->signal_state & 1) {
274a307d594SAlexey Kardashevskiy         qemu_irq_pulse(spapr_vio_qirq(sdev));
2758d90ad90SDavid Gibson     }
2768d90ad90SDavid Gibson 
2778d90ad90SDavid Gibson     return size;
2788d90ad90SDavid Gibson }
2798d90ad90SDavid Gibson 
2808d90ad90SDavid Gibson static NetClientInfo net_spapr_vlan_info = {
2812be64a68SLaszlo Ersek     .type = NET_CLIENT_OPTIONS_KIND_NIC,
2828d90ad90SDavid Gibson     .size = sizeof(NICState),
2838d90ad90SDavid Gibson     .can_receive = spapr_vlan_can_receive,
2848d90ad90SDavid Gibson     .receive = spapr_vlan_receive,
2858d90ad90SDavid Gibson };
2868d90ad90SDavid Gibson 
2878836630fSThomas Huth static void spapr_vlan_flush_rx_queue(void *opaque)
2888836630fSThomas Huth {
2898836630fSThomas Huth     VIOsPAPRVLANDevice *dev = opaque;
2908836630fSThomas Huth 
2918836630fSThomas Huth     qemu_flush_queued_packets(qemu_get_queue(dev->nic));
2928836630fSThomas Huth }
2938836630fSThomas Huth 
294831e8822SThomas Huth static void spapr_vlan_reset_rx_pool(RxBufPool *rxp)
295831e8822SThomas Huth {
296831e8822SThomas Huth     /*
297831e8822SThomas Huth      * Use INT_MAX as bufsize so that unused buffers are moved to the end
298831e8822SThomas Huth      * of the list during the qsort in spapr_vlan_add_rxbuf_to_pool() later.
299831e8822SThomas Huth      */
300831e8822SThomas Huth     rxp->bufsize = INT_MAX;
301831e8822SThomas Huth     rxp->count = 0;
302831e8822SThomas Huth     memset(rxp->bds, 0, sizeof(rxp->bds));
303831e8822SThomas Huth }
304831e8822SThomas Huth 
305c17491b6SDavid Gibson static void spapr_vlan_reset(VIOsPAPRDevice *sdev)
306c17491b6SDavid Gibson {
307fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
308831e8822SThomas Huth     int i;
309c17491b6SDavid Gibson 
310c17491b6SDavid Gibson     dev->buf_list = 0;
311c17491b6SDavid Gibson     dev->rx_bufs = 0;
312c17491b6SDavid Gibson     dev->isopen = 0;
313831e8822SThomas Huth 
314831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
315831e8822SThomas Huth         for (i = 0; i < RX_MAX_POOLS; i++) {
316831e8822SThomas Huth             spapr_vlan_reset_rx_pool(dev->rx_pool[i]);
317831e8822SThomas Huth         }
318831e8822SThomas Huth     }
319c17491b6SDavid Gibson }
320c17491b6SDavid Gibson 
32128b07e73SMarkus Armbruster static void spapr_vlan_realize(VIOsPAPRDevice *sdev, Error **errp)
3228d90ad90SDavid Gibson {
323fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
3248d90ad90SDavid Gibson 
3258d90ad90SDavid Gibson     qemu_macaddr_default_if_unset(&dev->nicconf.macaddr);
3268d90ad90SDavid Gibson 
3278d90ad90SDavid Gibson     dev->nic = qemu_new_nic(&net_spapr_vlan_info, &dev->nicconf,
328f79f2bfcSAnthony Liguori                             object_get_typename(OBJECT(sdev)), sdev->qdev.id, dev);
329b356f76dSJason Wang     qemu_format_nic_info_str(qemu_get_queue(dev->nic), dev->nicconf.macaddr.a);
3308836630fSThomas Huth 
3318836630fSThomas Huth     dev->rxp_timer = timer_new_us(QEMU_CLOCK_VIRTUAL, spapr_vlan_flush_rx_queue,
3328836630fSThomas Huth                                   dev);
3338d90ad90SDavid Gibson }
3348d90ad90SDavid Gibson 
335dfe79cf2SGonglei static void spapr_vlan_instance_init(Object *obj)
336dfe79cf2SGonglei {
337dfe79cf2SGonglei     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(obj);
338831e8822SThomas Huth     int i;
339dfe79cf2SGonglei 
340dfe79cf2SGonglei     device_add_bootindex_property(obj, &dev->nicconf.bootindex,
341dfe79cf2SGonglei                                   "bootindex", "",
342dfe79cf2SGonglei                                   DEVICE(dev), NULL);
343831e8822SThomas Huth 
344831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
345831e8822SThomas Huth         for (i = 0; i < RX_MAX_POOLS; i++) {
346831e8822SThomas Huth             dev->rx_pool[i] = g_new(RxBufPool, 1);
347831e8822SThomas Huth             spapr_vlan_reset_rx_pool(dev->rx_pool[i]);
348831e8822SThomas Huth         }
349831e8822SThomas Huth     }
350831e8822SThomas Huth }
351831e8822SThomas Huth 
352831e8822SThomas Huth static void spapr_vlan_instance_finalize(Object *obj)
353831e8822SThomas Huth {
354831e8822SThomas Huth     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(obj);
355831e8822SThomas Huth     int i;
356831e8822SThomas Huth 
357831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
358831e8822SThomas Huth         for (i = 0; i < RX_MAX_POOLS; i++) {
359831e8822SThomas Huth             g_free(dev->rx_pool[i]);
360831e8822SThomas Huth             dev->rx_pool[i] = NULL;
361831e8822SThomas Huth         }
362831e8822SThomas Huth     }
3638836630fSThomas Huth 
3648836630fSThomas Huth     if (dev->rxp_timer) {
3658836630fSThomas Huth         timer_del(dev->rxp_timer);
3668836630fSThomas Huth         timer_free(dev->rxp_timer);
3678836630fSThomas Huth     }
368dfe79cf2SGonglei }
369dfe79cf2SGonglei 
370d601fac4SDavid Gibson void spapr_vlan_create(VIOsPAPRBus *bus, NICInfo *nd)
3718d90ad90SDavid Gibson {
3728d90ad90SDavid Gibson     DeviceState *dev;
3738d90ad90SDavid Gibson 
3748d90ad90SDavid Gibson     dev = qdev_create(&bus->bus, "spapr-vlan");
3758d90ad90SDavid Gibson 
3768d90ad90SDavid Gibson     qdev_set_nic_properties(dev, nd);
3778d90ad90SDavid Gibson 
3788d90ad90SDavid Gibson     qdev_init_nofail(dev);
3798d90ad90SDavid Gibson }
3808d90ad90SDavid Gibson 
3818d90ad90SDavid Gibson static int spapr_vlan_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
3828d90ad90SDavid Gibson {
383fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *vdev = VIO_SPAPR_VLAN_DEVICE(dev);
3848d90ad90SDavid Gibson     uint8_t padded_mac[8] = {0, 0};
3858d90ad90SDavid Gibson     int ret;
3868d90ad90SDavid Gibson 
3878d90ad90SDavid Gibson     /* Some old phyp versions give the mac address in an 8-byte
3888d90ad90SDavid Gibson      * property.  The kernel driver has an insane workaround for this;
3898d90ad90SDavid Gibson      * rather than doing the obvious thing and checking the property
3908d90ad90SDavid Gibson      * length, it checks whether the first byte has 0b10 in the low
3918d90ad90SDavid Gibson      * bits.  If a correct 6-byte property has a different first byte
3928d90ad90SDavid Gibson      * the kernel will get the wrong mac address, overrunning its
3938d90ad90SDavid Gibson      * buffer in the process (read only, thank goodness).
3948d90ad90SDavid Gibson      *
3958d90ad90SDavid Gibson      * Here we workaround the kernel workaround by always supplying an
3968d90ad90SDavid Gibson      * 8-byte property, with the mac address in the last six bytes */
3978d90ad90SDavid Gibson     memcpy(&padded_mac[2], &vdev->nicconf.macaddr, ETH_ALEN);
3988d90ad90SDavid Gibson     ret = fdt_setprop(fdt, node_off, "local-mac-address",
3998d90ad90SDavid Gibson                       padded_mac, sizeof(padded_mac));
4008d90ad90SDavid Gibson     if (ret < 0) {
4018d90ad90SDavid Gibson         return ret;
4028d90ad90SDavid Gibson     }
4038d90ad90SDavid Gibson 
4048d90ad90SDavid Gibson     ret = fdt_setprop_cell(fdt, node_off, "ibm,mac-address-filters", 0);
4058d90ad90SDavid Gibson     if (ret < 0) {
4068d90ad90SDavid Gibson         return ret;
4078d90ad90SDavid Gibson     }
4088d90ad90SDavid Gibson 
4098d90ad90SDavid Gibson     return 0;
4108d90ad90SDavid Gibson }
4118d90ad90SDavid Gibson 
4128d90ad90SDavid Gibson static int check_bd(VIOsPAPRVLANDevice *dev, vlan_bd_t bd,
4138d90ad90SDavid Gibson                     target_ulong alignment)
4148d90ad90SDavid Gibson {
4158d90ad90SDavid Gibson     if ((VLAN_BD_ADDR(bd) % alignment)
4168d90ad90SDavid Gibson         || (VLAN_BD_LEN(bd) % alignment)) {
4178d90ad90SDavid Gibson         return -1;
4188d90ad90SDavid Gibson     }
4198d90ad90SDavid Gibson 
420ad0ebb91SDavid Gibson     if (!spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd),
421ad0ebb91SDavid Gibson                              VLAN_BD_LEN(bd), DMA_DIRECTION_FROM_DEVICE)
422ad0ebb91SDavid Gibson         || !spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd),
423ad0ebb91SDavid Gibson                                 VLAN_BD_LEN(bd), DMA_DIRECTION_TO_DEVICE)) {
4248d90ad90SDavid Gibson         return -1;
4258d90ad90SDavid Gibson     }
4268d90ad90SDavid Gibson 
4278d90ad90SDavid Gibson     return 0;
4288d90ad90SDavid Gibson }
4298d90ad90SDavid Gibson 
430b13ce26dSAndreas Färber static target_ulong h_register_logical_lan(PowerPCCPU *cpu,
43128e02042SDavid Gibson                                            sPAPRMachineState *spapr,
4328d90ad90SDavid Gibson                                            target_ulong opcode,
4338d90ad90SDavid Gibson                                            target_ulong *args)
4348d90ad90SDavid Gibson {
4358d90ad90SDavid Gibson     target_ulong reg = args[0];
4368d90ad90SDavid Gibson     target_ulong buf_list = args[1];
4378d90ad90SDavid Gibson     target_ulong rec_queue = args[2];
4388d90ad90SDavid Gibson     target_ulong filter_list = args[3];
4398d90ad90SDavid Gibson     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
440fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
4418d90ad90SDavid Gibson     vlan_bd_t filter_list_bd;
4428d90ad90SDavid Gibson 
4438d90ad90SDavid Gibson     if (!dev) {
4448d90ad90SDavid Gibson         return H_PARAMETER;
4458d90ad90SDavid Gibson     }
4468d90ad90SDavid Gibson 
4478d90ad90SDavid Gibson     if (dev->isopen) {
4488d90ad90SDavid Gibson         hcall_dprintf("H_REGISTER_LOGICAL_LAN called twice without "
4498d90ad90SDavid Gibson                       "H_FREE_LOGICAL_LAN\n");
4508d90ad90SDavid Gibson         return H_RESOURCE;
4518d90ad90SDavid Gibson     }
4528d90ad90SDavid Gibson 
453ad0ebb91SDavid Gibson     if (check_bd(dev, VLAN_VALID_BD(buf_list, SPAPR_TCE_PAGE_SIZE),
454ad0ebb91SDavid Gibson                  SPAPR_TCE_PAGE_SIZE) < 0) {
455d9599c92SDavid Gibson         hcall_dprintf("Bad buf_list 0x" TARGET_FMT_lx "\n", buf_list);
4568d90ad90SDavid Gibson         return H_PARAMETER;
4578d90ad90SDavid Gibson     }
4588d90ad90SDavid Gibson 
459ad0ebb91SDavid Gibson     filter_list_bd = VLAN_VALID_BD(filter_list, SPAPR_TCE_PAGE_SIZE);
460ad0ebb91SDavid Gibson     if (check_bd(dev, filter_list_bd, SPAPR_TCE_PAGE_SIZE) < 0) {
461d9599c92SDavid Gibson         hcall_dprintf("Bad filter_list 0x" TARGET_FMT_lx "\n", filter_list);
4628d90ad90SDavid Gibson         return H_PARAMETER;
4638d90ad90SDavid Gibson     }
4648d90ad90SDavid Gibson 
4658d90ad90SDavid Gibson     if (!(rec_queue & VLAN_BD_VALID)
4668d90ad90SDavid Gibson         || (check_bd(dev, rec_queue, VLAN_RQ_ALIGNMENT) < 0)) {
467d9599c92SDavid Gibson         hcall_dprintf("Bad receive queue\n");
4688d90ad90SDavid Gibson         return H_PARAMETER;
4698d90ad90SDavid Gibson     }
4708d90ad90SDavid Gibson 
4718d90ad90SDavid Gibson     dev->buf_list = buf_list;
4728d90ad90SDavid Gibson     sdev->signal_state = 0;
4738d90ad90SDavid Gibson 
4748d90ad90SDavid Gibson     rec_queue &= ~VLAN_BD_TOGGLE;
4758d90ad90SDavid Gibson 
4768d90ad90SDavid Gibson     /* Initialize the buffer list */
477ad0ebb91SDavid Gibson     vio_stq(sdev, buf_list, rec_queue);
478ad0ebb91SDavid Gibson     vio_stq(sdev, buf_list + 8, filter_list_bd);
479ad0ebb91SDavid Gibson     spapr_vio_dma_set(sdev, buf_list + VLAN_RX_BDS_OFF, 0,
480ad0ebb91SDavid Gibson                       SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF);
4818d90ad90SDavid Gibson     dev->add_buf_ptr = VLAN_RX_BDS_OFF - 8;
4828d90ad90SDavid Gibson     dev->use_buf_ptr = VLAN_RX_BDS_OFF - 8;
4838d90ad90SDavid Gibson     dev->rx_bufs = 0;
4848d90ad90SDavid Gibson     dev->rxq_ptr = 0;
4858d90ad90SDavid Gibson 
4868d90ad90SDavid Gibson     /* Initialize the receive queue */
487ad0ebb91SDavid Gibson     spapr_vio_dma_set(sdev, VLAN_BD_ADDR(rec_queue), 0, VLAN_BD_LEN(rec_queue));
4888d90ad90SDavid Gibson 
4898d90ad90SDavid Gibson     dev->isopen = 1;
490e0ff466cSAlexey Kardashevskiy     qemu_flush_queued_packets(qemu_get_queue(dev->nic));
491e0ff466cSAlexey Kardashevskiy 
4928d90ad90SDavid Gibson     return H_SUCCESS;
4938d90ad90SDavid Gibson }
4948d90ad90SDavid Gibson 
4958d90ad90SDavid Gibson 
49628e02042SDavid Gibson static target_ulong h_free_logical_lan(PowerPCCPU *cpu,
49728e02042SDavid Gibson                                        sPAPRMachineState *spapr,
4988d90ad90SDavid Gibson                                        target_ulong opcode, target_ulong *args)
4998d90ad90SDavid Gibson {
5008d90ad90SDavid Gibson     target_ulong reg = args[0];
5018d90ad90SDavid Gibson     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
502fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
5038d90ad90SDavid Gibson 
5048d90ad90SDavid Gibson     if (!dev) {
5058d90ad90SDavid Gibson         return H_PARAMETER;
5068d90ad90SDavid Gibson     }
5078d90ad90SDavid Gibson 
5088d90ad90SDavid Gibson     if (!dev->isopen) {
5098d90ad90SDavid Gibson         hcall_dprintf("H_FREE_LOGICAL_LAN called without "
5108d90ad90SDavid Gibson                       "H_REGISTER_LOGICAL_LAN\n");
5118d90ad90SDavid Gibson         return H_RESOURCE;
5128d90ad90SDavid Gibson     }
5138d90ad90SDavid Gibson 
514c17491b6SDavid Gibson     spapr_vlan_reset(sdev);
5158d90ad90SDavid Gibson     return H_SUCCESS;
5168d90ad90SDavid Gibson }
5178d90ad90SDavid Gibson 
518831e8822SThomas Huth /**
519831e8822SThomas Huth  * Used for qsort, this function compares two RxBufPools by size.
520831e8822SThomas Huth  */
521831e8822SThomas Huth static int rx_pool_size_compare(const void *p1, const void *p2)
522831e8822SThomas Huth {
523831e8822SThomas Huth     const RxBufPool *pool1 = *(RxBufPool **)p1;
524831e8822SThomas Huth     const RxBufPool *pool2 = *(RxBufPool **)p2;
525831e8822SThomas Huth 
526831e8822SThomas Huth     if (pool1->bufsize < pool2->bufsize) {
527831e8822SThomas Huth         return -1;
528831e8822SThomas Huth     }
529831e8822SThomas Huth     return pool1->bufsize > pool2->bufsize;
530831e8822SThomas Huth }
531831e8822SThomas Huth 
532831e8822SThomas Huth /**
533831e8822SThomas Huth  * Search for a matching buffer pool with exact matching size,
534831e8822SThomas Huth  * or return -1 if no matching pool has been found.
535831e8822SThomas Huth  */
536831e8822SThomas Huth static int spapr_vlan_get_rx_pool_id(VIOsPAPRVLANDevice *dev, int size)
537831e8822SThomas Huth {
538831e8822SThomas Huth     int pool;
539831e8822SThomas Huth 
540831e8822SThomas Huth     for (pool = 0; pool < RX_MAX_POOLS; pool++) {
541831e8822SThomas Huth         if (dev->rx_pool[pool]->bufsize == size) {
542831e8822SThomas Huth             return pool;
543831e8822SThomas Huth         }
544831e8822SThomas Huth     }
545831e8822SThomas Huth 
546831e8822SThomas Huth     return -1;
547831e8822SThomas Huth }
548831e8822SThomas Huth 
549831e8822SThomas Huth /**
550831e8822SThomas Huth  * Enqueuing receive buffer by adding it to one of our receive buffer pools
551831e8822SThomas Huth  */
552831e8822SThomas Huth static target_long spapr_vlan_add_rxbuf_to_pool(VIOsPAPRVLANDevice *dev,
553831e8822SThomas Huth                                                 target_ulong buf)
554831e8822SThomas Huth {
555831e8822SThomas Huth     int size = VLAN_BD_LEN(buf);
556831e8822SThomas Huth     int pool;
557831e8822SThomas Huth 
558831e8822SThomas Huth     pool = spapr_vlan_get_rx_pool_id(dev, size);
559831e8822SThomas Huth     if (pool < 0) {
560831e8822SThomas Huth         /*
561831e8822SThomas Huth          * No matching pool found? Try to use a new one. If the guest used all
562831e8822SThomas Huth          * pools before, but changed the size of one pool inbetween, we might
563831e8822SThomas Huth          * need to recycle that pool here (if it's empty already). Thus scan
564831e8822SThomas Huth          * all buffer pools now, starting with the last (likely empty) one.
565831e8822SThomas Huth          */
566831e8822SThomas Huth         for (pool = RX_MAX_POOLS - 1; pool >= 0 ; pool--) {
567831e8822SThomas Huth             if (dev->rx_pool[pool]->count == 0) {
568831e8822SThomas Huth                 dev->rx_pool[pool]->bufsize = size;
569831e8822SThomas Huth                 /*
570831e8822SThomas Huth                  * Sort pools by size so that spapr_vlan_receive()
571831e8822SThomas Huth                  * can later find the smallest buffer pool easily.
572831e8822SThomas Huth                  */
573831e8822SThomas Huth                 qsort(dev->rx_pool, RX_MAX_POOLS, sizeof(dev->rx_pool[0]),
574831e8822SThomas Huth                       rx_pool_size_compare);
575831e8822SThomas Huth                 pool = spapr_vlan_get_rx_pool_id(dev, size);
576831e8822SThomas Huth                 DPRINTF("created RX pool %d for size %lld\n", pool,
577831e8822SThomas Huth                         VLAN_BD_LEN(buf));
578831e8822SThomas Huth                 break;
579831e8822SThomas Huth             }
580831e8822SThomas Huth         }
581831e8822SThomas Huth     }
582831e8822SThomas Huth     /* Still no usable pool? Give up */
583831e8822SThomas Huth     if (pool < 0 || dev->rx_pool[pool]->count >= RX_POOL_MAX_BDS) {
584831e8822SThomas Huth         return H_RESOURCE;
585831e8822SThomas Huth     }
586831e8822SThomas Huth 
587831e8822SThomas Huth     DPRINTF("h_add_llan_buf():  Add buf using pool %i (size %lli, count=%i)\n",
588831e8822SThomas Huth             pool, VLAN_BD_LEN(buf), dev->rx_pool[pool]->count);
589831e8822SThomas Huth 
590831e8822SThomas Huth     dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count++] = buf;
591831e8822SThomas Huth 
592831e8822SThomas Huth     return 0;
593831e8822SThomas Huth }
594831e8822SThomas Huth 
595831e8822SThomas Huth /**
596831e8822SThomas Huth  * This is the old way of enqueuing receive buffers: Add it to the rx queue
597831e8822SThomas Huth  * page that has been supplied by the guest (which is quite limited in size).
598831e8822SThomas Huth  */
599d6f39fdfSThomas Huth static target_long spapr_vlan_add_rxbuf_to_page(VIOsPAPRVLANDevice *dev,
600d6f39fdfSThomas Huth                                                 target_ulong buf)
601d6f39fdfSThomas Huth {
602d6f39fdfSThomas Huth     vlan_bd_t bd;
603d6f39fdfSThomas Huth 
604d6f39fdfSThomas Huth     if (dev->rx_bufs >= VLAN_MAX_BUFS) {
605d6f39fdfSThomas Huth         return H_RESOURCE;
606d6f39fdfSThomas Huth     }
607d6f39fdfSThomas Huth 
608d6f39fdfSThomas Huth     do {
609d6f39fdfSThomas Huth         dev->add_buf_ptr += 8;
610d6f39fdfSThomas Huth         if (dev->add_buf_ptr >= VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF) {
611d6f39fdfSThomas Huth             dev->add_buf_ptr = VLAN_RX_BDS_OFF;
612d6f39fdfSThomas Huth         }
613d6f39fdfSThomas Huth 
614d6f39fdfSThomas Huth         bd = vio_ldq(&dev->sdev, dev->buf_list + dev->add_buf_ptr);
615d6f39fdfSThomas Huth     } while (bd & VLAN_BD_VALID);
616d6f39fdfSThomas Huth 
617d6f39fdfSThomas Huth     vio_stq(&dev->sdev, dev->buf_list + dev->add_buf_ptr, buf);
618d6f39fdfSThomas Huth 
619d6f39fdfSThomas Huth     DPRINTF("h_add_llan_buf():  Added buf  ptr=%d  rx_bufs=%d bd=0x%016llx\n",
620d6f39fdfSThomas Huth             dev->add_buf_ptr, dev->rx_bufs, (unsigned long long)buf);
621d6f39fdfSThomas Huth 
622d6f39fdfSThomas Huth     return 0;
623d6f39fdfSThomas Huth }
624d6f39fdfSThomas Huth 
625b13ce26dSAndreas Färber static target_ulong h_add_logical_lan_buffer(PowerPCCPU *cpu,
62628e02042SDavid Gibson                                              sPAPRMachineState *spapr,
6278d90ad90SDavid Gibson                                              target_ulong opcode,
6288d90ad90SDavid Gibson                                              target_ulong *args)
6298d90ad90SDavid Gibson {
6308d90ad90SDavid Gibson     target_ulong reg = args[0];
6318d90ad90SDavid Gibson     target_ulong buf = args[1];
6328d90ad90SDavid Gibson     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
633fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
634d6f39fdfSThomas Huth     target_long ret;
6358d90ad90SDavid Gibson 
636f6bda9cbSPeter Maydell     DPRINTF("H_ADD_LOGICAL_LAN_BUFFER(0x" TARGET_FMT_lx
6378d90ad90SDavid Gibson             ", 0x" TARGET_FMT_lx ")\n", reg, buf);
6388d90ad90SDavid Gibson 
6398d90ad90SDavid Gibson     if (!sdev) {
640d9599c92SDavid Gibson         hcall_dprintf("Bad device\n");
6418d90ad90SDavid Gibson         return H_PARAMETER;
6428d90ad90SDavid Gibson     }
6438d90ad90SDavid Gibson 
6448d90ad90SDavid Gibson     if ((check_bd(dev, buf, 4) < 0)
6458d90ad90SDavid Gibson         || (VLAN_BD_LEN(buf) < 16)) {
646d9599c92SDavid Gibson         hcall_dprintf("Bad buffer enqueued\n");
6478d90ad90SDavid Gibson         return H_PARAMETER;
6488d90ad90SDavid Gibson     }
6498d90ad90SDavid Gibson 
650d6f39fdfSThomas Huth     if (!dev->isopen) {
6518d90ad90SDavid Gibson         return H_RESOURCE;
6528d90ad90SDavid Gibson     }
6538d90ad90SDavid Gibson 
654831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
655831e8822SThomas Huth         ret = spapr_vlan_add_rxbuf_to_pool(dev, buf);
656831e8822SThomas Huth     } else {
657d6f39fdfSThomas Huth         ret = spapr_vlan_add_rxbuf_to_page(dev, buf);
658831e8822SThomas Huth     }
659d6f39fdfSThomas Huth     if (ret) {
660d6f39fdfSThomas Huth         return ret;
6618d90ad90SDavid Gibson     }
6628d90ad90SDavid Gibson 
6638d90ad90SDavid Gibson     dev->rx_bufs++;
6648d90ad90SDavid Gibson 
6658836630fSThomas Huth     /*
6668836630fSThomas Huth      * Give guest some more time to add additional RX buffers before we
6678836630fSThomas Huth      * flush the receive queue, so that e.g. fragmented IP packets can
6688836630fSThomas Huth      * be passed to the guest in one go later (instead of passing single
6698836630fSThomas Huth      * fragments if there is only one receive buffer available).
6708836630fSThomas Huth      */
6718836630fSThomas Huth     timer_mod(dev->rxp_timer, qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + 500);
6720a61f3b4SAlexey Kardashevskiy 
6738d90ad90SDavid Gibson     return H_SUCCESS;
6748d90ad90SDavid Gibson }
6758d90ad90SDavid Gibson 
67628e02042SDavid Gibson static target_ulong h_send_logical_lan(PowerPCCPU *cpu,
67728e02042SDavid Gibson                                        sPAPRMachineState *spapr,
6788d90ad90SDavid Gibson                                        target_ulong opcode, target_ulong *args)
6798d90ad90SDavid Gibson {
6808d90ad90SDavid Gibson     target_ulong reg = args[0];
6818d90ad90SDavid Gibson     target_ulong *bufs = args + 1;
6828d90ad90SDavid Gibson     target_ulong continue_token = args[7];
6838d90ad90SDavid Gibson     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
684fd506b4fSDavid Gibson     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
6858d90ad90SDavid Gibson     unsigned total_len;
6868d90ad90SDavid Gibson     uint8_t *lbuf, *p;
6878d90ad90SDavid Gibson     int i, nbufs;
6888d90ad90SDavid Gibson     int ret;
6898d90ad90SDavid Gibson 
690f6bda9cbSPeter Maydell     DPRINTF("H_SEND_LOGICAL_LAN(0x" TARGET_FMT_lx ", <bufs>, 0x"
6918d90ad90SDavid Gibson             TARGET_FMT_lx ")\n", reg, continue_token);
6928d90ad90SDavid Gibson 
6938d90ad90SDavid Gibson     if (!sdev) {
6948d90ad90SDavid Gibson         return H_PARAMETER;
6958d90ad90SDavid Gibson     }
6968d90ad90SDavid Gibson 
697f6bda9cbSPeter Maydell     DPRINTF("rxbufs = %d\n", dev->rx_bufs);
6988d90ad90SDavid Gibson 
6998d90ad90SDavid Gibson     if (!dev->isopen) {
7008d90ad90SDavid Gibson         return H_DROPPED;
7018d90ad90SDavid Gibson     }
7028d90ad90SDavid Gibson 
7038d90ad90SDavid Gibson     if (continue_token) {
7048d90ad90SDavid Gibson         return H_HARDWARE; /* FIXME actually handle this */
7058d90ad90SDavid Gibson     }
7068d90ad90SDavid Gibson 
7078d90ad90SDavid Gibson     total_len = 0;
7088d90ad90SDavid Gibson     for (i = 0; i < 6; i++) {
709f6bda9cbSPeter Maydell         DPRINTF("   buf desc: 0x" TARGET_FMT_lx "\n", bufs[i]);
7108d90ad90SDavid Gibson         if (!(bufs[i] & VLAN_BD_VALID)) {
7118d90ad90SDavid Gibson             break;
7128d90ad90SDavid Gibson         }
7138d90ad90SDavid Gibson         total_len += VLAN_BD_LEN(bufs[i]);
7148d90ad90SDavid Gibson     }
7158d90ad90SDavid Gibson 
7168d90ad90SDavid Gibson     nbufs = i;
717f6bda9cbSPeter Maydell     DPRINTF("h_send_logical_lan() %d buffers, total length 0x%x\n",
7188d90ad90SDavid Gibson             nbufs, total_len);
7198d90ad90SDavid Gibson 
7208d90ad90SDavid Gibson     if (total_len == 0) {
7218d90ad90SDavid Gibson         return H_SUCCESS;
7228d90ad90SDavid Gibson     }
7238d90ad90SDavid Gibson 
7248d90ad90SDavid Gibson     if (total_len > MAX_PACKET_SIZE) {
7258d90ad90SDavid Gibson         /* Don't let the guest force too large an allocation */
7268d90ad90SDavid Gibson         return H_RESOURCE;
7278d90ad90SDavid Gibson     }
7288d90ad90SDavid Gibson 
7298d90ad90SDavid Gibson     lbuf = alloca(total_len);
7308d90ad90SDavid Gibson     p = lbuf;
7318d90ad90SDavid Gibson     for (i = 0; i < nbufs; i++) {
732ad0ebb91SDavid Gibson         ret = spapr_vio_dma_read(sdev, VLAN_BD_ADDR(bufs[i]),
7338d90ad90SDavid Gibson                                  p, VLAN_BD_LEN(bufs[i]));
7348d90ad90SDavid Gibson         if (ret < 0) {
7358d90ad90SDavid Gibson             return ret;
7368d90ad90SDavid Gibson         }
7378d90ad90SDavid Gibson 
7388d90ad90SDavid Gibson         p += VLAN_BD_LEN(bufs[i]);
7398d90ad90SDavid Gibson     }
7408d90ad90SDavid Gibson 
741b356f76dSJason Wang     qemu_send_packet(qemu_get_queue(dev->nic), lbuf, total_len);
7428d90ad90SDavid Gibson 
7438d90ad90SDavid Gibson     return H_SUCCESS;
7448d90ad90SDavid Gibson }
7458d90ad90SDavid Gibson 
74628e02042SDavid Gibson static target_ulong h_multicast_ctrl(PowerPCCPU *cpu, sPAPRMachineState *spapr,
7478d90ad90SDavid Gibson                                      target_ulong opcode, target_ulong *args)
7488d90ad90SDavid Gibson {
7498d90ad90SDavid Gibson     target_ulong reg = args[0];
7508d90ad90SDavid Gibson     VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
7518d90ad90SDavid Gibson 
7528d90ad90SDavid Gibson     if (!dev) {
7538d90ad90SDavid Gibson         return H_PARAMETER;
7548d90ad90SDavid Gibson     }
7558d90ad90SDavid Gibson 
7568d90ad90SDavid Gibson     return H_SUCCESS;
7578d90ad90SDavid Gibson }
7588d90ad90SDavid Gibson 
7593954d33aSAnthony Liguori static Property spapr_vlan_properties[] = {
760ad0ebb91SDavid Gibson     DEFINE_SPAPR_PROPERTIES(VIOsPAPRVLANDevice, sdev),
7618d90ad90SDavid Gibson     DEFINE_NIC_PROPERTIES(VIOsPAPRVLANDevice, nicconf),
762831e8822SThomas Huth     DEFINE_PROP_BIT("use-rx-buffer-pools", VIOsPAPRVLANDevice,
76357c522f4SThomas Huth                     compat_flags, SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT, true),
7648d90ad90SDavid Gibson     DEFINE_PROP_END_OF_LIST(),
7653954d33aSAnthony Liguori };
7663954d33aSAnthony Liguori 
767831e8822SThomas Huth static bool spapr_vlan_rx_buffer_pools_needed(void *opaque)
768831e8822SThomas Huth {
769831e8822SThomas Huth     VIOsPAPRVLANDevice *dev = opaque;
770831e8822SThomas Huth 
771831e8822SThomas Huth     return (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) != 0;
772831e8822SThomas Huth }
773831e8822SThomas Huth 
774831e8822SThomas Huth static const VMStateDescription vmstate_rx_buffer_pool = {
775831e8822SThomas Huth     .name = "spapr_llan/rx_buffer_pool",
776831e8822SThomas Huth     .version_id = 1,
777831e8822SThomas Huth     .minimum_version_id = 1,
778831e8822SThomas Huth     .needed = spapr_vlan_rx_buffer_pools_needed,
779831e8822SThomas Huth     .fields = (VMStateField[]) {
780831e8822SThomas Huth         VMSTATE_INT32(bufsize, RxBufPool),
781831e8822SThomas Huth         VMSTATE_INT32(count, RxBufPool),
782831e8822SThomas Huth         VMSTATE_UINT64_ARRAY(bds, RxBufPool, RX_POOL_MAX_BDS),
783831e8822SThomas Huth         VMSTATE_END_OF_LIST()
784831e8822SThomas Huth     }
785831e8822SThomas Huth };
786831e8822SThomas Huth 
787831e8822SThomas Huth static const VMStateDescription vmstate_rx_pools = {
788831e8822SThomas Huth     .name = "spapr_llan/rx_pools",
789831e8822SThomas Huth     .version_id = 1,
790831e8822SThomas Huth     .minimum_version_id = 1,
791831e8822SThomas Huth     .needed = spapr_vlan_rx_buffer_pools_needed,
792831e8822SThomas Huth     .fields = (VMStateField[]) {
793831e8822SThomas Huth         VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(rx_pool, VIOsPAPRVLANDevice,
794831e8822SThomas Huth                                            RX_MAX_POOLS, 1,
795831e8822SThomas Huth                                            vmstate_rx_buffer_pool, RxBufPool),
796831e8822SThomas Huth         VMSTATE_END_OF_LIST()
797831e8822SThomas Huth     }
798831e8822SThomas Huth };
799831e8822SThomas Huth 
800686fefe4SDavid Gibson static const VMStateDescription vmstate_spapr_llan = {
801686fefe4SDavid Gibson     .name = "spapr_llan",
802686fefe4SDavid Gibson     .version_id = 1,
803686fefe4SDavid Gibson     .minimum_version_id = 1,
804686fefe4SDavid Gibson     .fields = (VMStateField[]) {
805686fefe4SDavid Gibson         VMSTATE_SPAPR_VIO(sdev, VIOsPAPRVLANDevice),
806686fefe4SDavid Gibson         /* LLAN state */
807686fefe4SDavid Gibson         VMSTATE_BOOL(isopen, VIOsPAPRVLANDevice),
808cbd62f86SPaolo Bonzini         VMSTATE_UINT64(buf_list, VIOsPAPRVLANDevice),
809686fefe4SDavid Gibson         VMSTATE_UINT32(add_buf_ptr, VIOsPAPRVLANDevice),
810686fefe4SDavid Gibson         VMSTATE_UINT32(use_buf_ptr, VIOsPAPRVLANDevice),
811686fefe4SDavid Gibson         VMSTATE_UINT32(rx_bufs, VIOsPAPRVLANDevice),
812cbd62f86SPaolo Bonzini         VMSTATE_UINT64(rxq_ptr, VIOsPAPRVLANDevice),
813686fefe4SDavid Gibson 
814686fefe4SDavid Gibson         VMSTATE_END_OF_LIST()
815686fefe4SDavid Gibson     },
816831e8822SThomas Huth     .subsections = (const VMStateDescription * []) {
817831e8822SThomas Huth         &vmstate_rx_pools,
818831e8822SThomas Huth         NULL
819831e8822SThomas Huth     }
820686fefe4SDavid Gibson };
821686fefe4SDavid Gibson 
8223954d33aSAnthony Liguori static void spapr_vlan_class_init(ObjectClass *klass, void *data)
8233954d33aSAnthony Liguori {
82439bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
8253954d33aSAnthony Liguori     VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
8263954d33aSAnthony Liguori 
82728b07e73SMarkus Armbruster     k->realize = spapr_vlan_realize;
828c17491b6SDavid Gibson     k->reset = spapr_vlan_reset;
8293954d33aSAnthony Liguori     k->devnode = spapr_vlan_devnode;
8303954d33aSAnthony Liguori     k->dt_name = "l-lan";
8313954d33aSAnthony Liguori     k->dt_type = "network";
8323954d33aSAnthony Liguori     k->dt_compatible = "IBM,l-lan";
8333954d33aSAnthony Liguori     k->signal_mask = 0x1;
83429fdedfeSAlexey Kardashevskiy     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
83539bffca2SAnthony Liguori     dc->props = spapr_vlan_properties;
836ad0ebb91SDavid Gibson     k->rtce_window_size = 0x10000000;
837686fefe4SDavid Gibson     dc->vmsd = &vmstate_spapr_llan;
8383954d33aSAnthony Liguori }
8393954d33aSAnthony Liguori 
8408c43a6f0SAndreas Färber static const TypeInfo spapr_vlan_info = {
841fd506b4fSDavid Gibson     .name          = TYPE_VIO_SPAPR_VLAN_DEVICE,
84239bffca2SAnthony Liguori     .parent        = TYPE_VIO_SPAPR_DEVICE,
84339bffca2SAnthony Liguori     .instance_size = sizeof(VIOsPAPRVLANDevice),
8443954d33aSAnthony Liguori     .class_init    = spapr_vlan_class_init,
845dfe79cf2SGonglei     .instance_init = spapr_vlan_instance_init,
846831e8822SThomas Huth     .instance_finalize = spapr_vlan_instance_finalize,
8478d90ad90SDavid Gibson };
8488d90ad90SDavid Gibson 
84983f7d43aSAndreas Färber static void spapr_vlan_register_types(void)
8508d90ad90SDavid Gibson {
8511fc02533SDavid Gibson     spapr_register_hypercall(H_REGISTER_LOGICAL_LAN, h_register_logical_lan);
8521fc02533SDavid Gibson     spapr_register_hypercall(H_FREE_LOGICAL_LAN, h_free_logical_lan);
8531fc02533SDavid Gibson     spapr_register_hypercall(H_SEND_LOGICAL_LAN, h_send_logical_lan);
8541fc02533SDavid Gibson     spapr_register_hypercall(H_ADD_LOGICAL_LAN_BUFFER,
8551fc02533SDavid Gibson                              h_add_logical_lan_buffer);
8561fc02533SDavid Gibson     spapr_register_hypercall(H_MULTICAST_CTRL, h_multicast_ctrl);
85739bffca2SAnthony Liguori     type_register_static(&spapr_vlan_info);
8588d90ad90SDavid Gibson }
85983f7d43aSAndreas Färber 
86083f7d43aSAndreas Färber type_init(spapr_vlan_register_types)
861