xref: /qemu/hw/net/spapr_llan.c (revision 06b40d250ecfa1633209c2e431a7a38acfd03a98)
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  */
270b8fa32fSMarkus Armbruster 
280d75590dSPeter Maydell #include "qemu/osdep.h"
2903dd024fSPaolo Bonzini #include "qemu/log.h"
300b8fa32fSMarkus Armbruster #include "qemu/module.h"
311422e32dSPaolo Bonzini #include "net/net.h"
32d6454270SMarkus Armbruster #include "migration/vmstate.h"
330d09e41aSPaolo Bonzini #include "hw/ppc/spapr.h"
340d09e41aSPaolo Bonzini #include "hw/ppc/spapr_vio.h"
35a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
3632cad1ffSPhilippe Mathieu-Daudé #include "system/system.h"
37e8bb33deSLaurent Vivier #include "trace.h"
388d90ad90SDavid Gibson 
398d90ad90SDavid Gibson #include <libfdt.h>
40db1015e9SEduardo Habkost #include "qom/object.h"
418d90ad90SDavid Gibson 
428d90ad90SDavid Gibson #define ETH_ALEN        6
438d90ad90SDavid Gibson #define MAX_PACKET_SIZE 65536
448d90ad90SDavid Gibson 
45831e8822SThomas Huth /* Compatibility flags for migration */
46831e8822SThomas Huth #define SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT  0
47831e8822SThomas Huth #define SPAPRVLAN_FLAG_RX_BUF_POOLS      (1 << SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT)
48831e8822SThomas Huth 
498d90ad90SDavid Gibson /*
508d90ad90SDavid Gibson  * Virtual LAN device
518d90ad90SDavid Gibson  */
528d90ad90SDavid Gibson 
538d90ad90SDavid Gibson typedef uint64_t vlan_bd_t;
548d90ad90SDavid Gibson 
558d90ad90SDavid Gibson #define VLAN_BD_VALID        0x8000000000000000ULL
568d90ad90SDavid Gibson #define VLAN_BD_TOGGLE       0x4000000000000000ULL
578d90ad90SDavid Gibson #define VLAN_BD_NO_CSUM      0x0200000000000000ULL
588d90ad90SDavid Gibson #define VLAN_BD_CSUM_GOOD    0x0100000000000000ULL
598d90ad90SDavid Gibson #define VLAN_BD_LEN_MASK     0x00ffffff00000000ULL
608d90ad90SDavid Gibson #define VLAN_BD_LEN(bd)      (((bd) & VLAN_BD_LEN_MASK) >> 32)
618d90ad90SDavid Gibson #define VLAN_BD_ADDR_MASK    0x00000000ffffffffULL
628d90ad90SDavid Gibson #define VLAN_BD_ADDR(bd)     ((bd) & VLAN_BD_ADDR_MASK)
638d90ad90SDavid Gibson 
648d90ad90SDavid Gibson #define VLAN_VALID_BD(addr, len) (VLAN_BD_VALID | \
658d90ad90SDavid Gibson                                   (((len) << 32) & VLAN_BD_LEN_MASK) |  \
668d90ad90SDavid Gibson                                   (addr & VLAN_BD_ADDR_MASK))
678d90ad90SDavid Gibson 
688d90ad90SDavid Gibson #define VLAN_RXQC_TOGGLE     0x80
698d90ad90SDavid Gibson #define VLAN_RXQC_VALID      0x40
708d90ad90SDavid Gibson #define VLAN_RXQC_NO_CSUM    0x02
718d90ad90SDavid Gibson #define VLAN_RXQC_CSUM_GOOD  0x01
728d90ad90SDavid Gibson 
738d90ad90SDavid Gibson #define VLAN_RQ_ALIGNMENT    16
748d90ad90SDavid Gibson #define VLAN_RXQ_BD_OFF      0
758d90ad90SDavid Gibson #define VLAN_FILTER_BD_OFF   8
768d90ad90SDavid Gibson #define VLAN_RX_BDS_OFF      16
77439ce140SAnton Blanchard /*
78439ce140SAnton Blanchard  * The final 8 bytes of the buffer list is a counter of frames dropped
79439ce140SAnton Blanchard  * because there was not a buffer in the buffer list capable of holding
80439ce140SAnton Blanchard  * the frame. We must avoid it, or the operating system will report garbage
81439ce140SAnton Blanchard  * for this statistic.
82439ce140SAnton Blanchard  */
83439ce140SAnton Blanchard #define VLAN_RX_BDS_LEN      (SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF - 8)
84439ce140SAnton Blanchard #define VLAN_MAX_BUFS        (VLAN_RX_BDS_LEN / 8)
858d90ad90SDavid Gibson 
86fd506b4fSDavid Gibson #define TYPE_VIO_SPAPR_VLAN_DEVICE "spapr-vlan"
878063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(SpaprVioVlan, VIO_SPAPR_VLAN_DEVICE)
88fd506b4fSDavid Gibson 
89831e8822SThomas Huth #define RX_POOL_MAX_BDS 4096
90831e8822SThomas Huth #define RX_MAX_POOLS 5
91831e8822SThomas Huth 
92831e8822SThomas Huth typedef struct {
93831e8822SThomas Huth     int32_t bufsize;
94831e8822SThomas Huth     int32_t count;
95831e8822SThomas Huth     vlan_bd_t bds[RX_POOL_MAX_BDS];
96831e8822SThomas Huth } RxBufPool;
97831e8822SThomas Huth 
98db1015e9SEduardo Habkost struct SpaprVioVlan {
99ce2918cbSDavid Gibson     SpaprVioDevice sdev;
1008d90ad90SDavid Gibson     NICConf nicconf;
1018d90ad90SDavid Gibson     NICState *nic;
10232f5f50dSLaurent Vivier     MACAddr perm_mac;
103686fefe4SDavid Gibson     bool isopen;
104cbd62f86SPaolo Bonzini     hwaddr buf_list;
105686fefe4SDavid Gibson     uint32_t add_buf_ptr, use_buf_ptr, rx_bufs;
106cbd62f86SPaolo Bonzini     hwaddr rxq_ptr;
1078836630fSThomas Huth     QEMUTimer *rxp_timer;
108b12227afSStefan Weil     uint32_t compat_flags;             /* Compatibility flags for migration */
109831e8822SThomas Huth     RxBufPool *rx_pool[RX_MAX_POOLS];  /* Receive buffer descriptor pools */
110db1015e9SEduardo Habkost };
1118d90ad90SDavid Gibson 
spapr_vlan_can_receive(NetClientState * nc)112b8c4b67eSPhilippe Mathieu-Daudé static bool spapr_vlan_can_receive(NetClientState *nc)
1138d90ad90SDavid Gibson {
114ce2918cbSDavid Gibson     SpaprVioVlan *dev = qemu_get_nic_opaque(nc);
1158d90ad90SDavid Gibson 
116b8c4b67eSPhilippe Mathieu-Daudé     return dev->isopen && dev->rx_bufs > 0;
1178d90ad90SDavid Gibson }
1188d90ad90SDavid Gibson 
119d6f39fdfSThomas Huth /**
1205c29dd8cSThomas Huth  * The last 8 bytes of the receive buffer list page (that has been
1215c29dd8cSThomas Huth  * supplied by the guest with the H_REGISTER_LOGICAL_LAN call) contain
1225c29dd8cSThomas Huth  * a counter for frames that have been dropped because there was no
1235c29dd8cSThomas Huth  * suitable receive buffer available. This function is used to increase
1245c29dd8cSThomas Huth  * this counter by one.
1255c29dd8cSThomas Huth  */
spapr_vlan_record_dropped_rx_frame(SpaprVioVlan * dev)126ce2918cbSDavid Gibson static void spapr_vlan_record_dropped_rx_frame(SpaprVioVlan *dev)
1275c29dd8cSThomas Huth {
1285c29dd8cSThomas Huth     uint64_t cnt;
1295c29dd8cSThomas Huth 
1305c29dd8cSThomas Huth     cnt = vio_ldq(&dev->sdev, dev->buf_list + 4096 - 8);
1315c29dd8cSThomas Huth     vio_stq(&dev->sdev, dev->buf_list + 4096 - 8, cnt + 1);
1325c29dd8cSThomas Huth }
1335c29dd8cSThomas Huth 
1345c29dd8cSThomas Huth /**
135831e8822SThomas Huth  * Get buffer descriptor from one of our receive buffer pools
136831e8822SThomas Huth  */
spapr_vlan_get_rx_bd_from_pool(SpaprVioVlan * dev,size_t size)137ce2918cbSDavid Gibson static vlan_bd_t spapr_vlan_get_rx_bd_from_pool(SpaprVioVlan *dev,
138831e8822SThomas Huth                                                 size_t size)
139831e8822SThomas Huth {
140831e8822SThomas Huth     vlan_bd_t bd;
141831e8822SThomas Huth     int pool;
142831e8822SThomas Huth 
143831e8822SThomas Huth     for (pool = 0; pool < RX_MAX_POOLS; pool++) {
144831e8822SThomas Huth         if (dev->rx_pool[pool]->count > 0 &&
145831e8822SThomas Huth             dev->rx_pool[pool]->bufsize >= size + 8) {
146831e8822SThomas Huth             break;
147831e8822SThomas Huth         }
148831e8822SThomas Huth     }
149831e8822SThomas Huth     if (pool == RX_MAX_POOLS) {
150831e8822SThomas Huth         /* Failed to find a suitable buffer */
151831e8822SThomas Huth         return 0;
152831e8822SThomas Huth     }
153831e8822SThomas Huth 
154e8bb33deSLaurent Vivier 
155e8bb33deSLaurent Vivier     trace_spapr_vlan_get_rx_bd_from_pool_found(pool,
156e8bb33deSLaurent Vivier                                                dev->rx_pool[pool]->count,
157e8bb33deSLaurent Vivier                                                dev->rx_bufs);
158831e8822SThomas Huth 
159831e8822SThomas Huth     /* Remove the buffer from the pool */
160831e8822SThomas Huth     dev->rx_pool[pool]->count--;
161831e8822SThomas Huth     bd = dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count];
162831e8822SThomas Huth     dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count] = 0;
163831e8822SThomas Huth 
164831e8822SThomas Huth     return bd;
165831e8822SThomas Huth }
166831e8822SThomas Huth 
167831e8822SThomas Huth /**
168d6f39fdfSThomas Huth  * Get buffer descriptor from the receive buffer list page that has been
169d6f39fdfSThomas Huth  * supplied by the guest with the H_REGISTER_LOGICAL_LAN call
170d6f39fdfSThomas Huth  */
spapr_vlan_get_rx_bd_from_page(SpaprVioVlan * dev,size_t size)171ce2918cbSDavid Gibson static vlan_bd_t spapr_vlan_get_rx_bd_from_page(SpaprVioVlan *dev,
172d6f39fdfSThomas Huth                                                 size_t size)
173d6f39fdfSThomas Huth {
174d6f39fdfSThomas Huth     int buf_ptr = dev->use_buf_ptr;
175d6f39fdfSThomas Huth     vlan_bd_t bd;
176d6f39fdfSThomas Huth 
177d6f39fdfSThomas Huth     do {
178d6f39fdfSThomas Huth         buf_ptr += 8;
179d6f39fdfSThomas Huth         if (buf_ptr >= VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF) {
180d6f39fdfSThomas Huth             buf_ptr = VLAN_RX_BDS_OFF;
181d6f39fdfSThomas Huth         }
182d6f39fdfSThomas Huth 
183d6f39fdfSThomas Huth         bd = vio_ldq(&dev->sdev, dev->buf_list + buf_ptr);
184e8bb33deSLaurent Vivier 
185e8bb33deSLaurent Vivier         trace_spapr_vlan_get_rx_bd_from_page(buf_ptr, (uint64_t)bd);
186d6f39fdfSThomas Huth     } while ((!(bd & VLAN_BD_VALID) || VLAN_BD_LEN(bd) < size + 8)
187d6f39fdfSThomas Huth              && buf_ptr != dev->use_buf_ptr);
188d6f39fdfSThomas Huth 
189d6f39fdfSThomas Huth     if (!(bd & VLAN_BD_VALID) || VLAN_BD_LEN(bd) < size + 8) {
190d6f39fdfSThomas Huth         /* Failed to find a suitable buffer */
191d6f39fdfSThomas Huth         return 0;
192d6f39fdfSThomas Huth     }
193d6f39fdfSThomas Huth 
194d6f39fdfSThomas Huth     /* Remove the buffer from the pool */
195d6f39fdfSThomas Huth     dev->use_buf_ptr = buf_ptr;
196d6f39fdfSThomas Huth     vio_stq(&dev->sdev, dev->buf_list + dev->use_buf_ptr, 0);
197d6f39fdfSThomas Huth 
198e8bb33deSLaurent Vivier     trace_spapr_vlan_get_rx_bd_from_page_found(dev->use_buf_ptr, dev->rx_bufs);
199d6f39fdfSThomas Huth 
200d6f39fdfSThomas Huth     return bd;
201d6f39fdfSThomas Huth }
202d6f39fdfSThomas Huth 
spapr_vlan_receive(NetClientState * nc,const uint8_t * buf,size_t size)2034e68f7a0SStefan Hajnoczi static ssize_t spapr_vlan_receive(NetClientState *nc, const uint8_t *buf,
2048d90ad90SDavid Gibson                                   size_t size)
2058d90ad90SDavid Gibson {
206ce2918cbSDavid Gibson     SpaprVioVlan *dev = qemu_get_nic_opaque(nc);
207ce2918cbSDavid Gibson     SpaprVioDevice *sdev = VIO_SPAPR_DEVICE(dev);
208ad0ebb91SDavid Gibson     vlan_bd_t rxq_bd = vio_ldq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF);
2098d90ad90SDavid Gibson     vlan_bd_t bd;
2108d90ad90SDavid Gibson     uint64_t handle;
2118d90ad90SDavid Gibson     uint8_t control;
2128d90ad90SDavid Gibson 
213e8bb33deSLaurent Vivier     trace_spapr_vlan_receive(sdev->qdev.id, dev->rx_bufs);
2148d90ad90SDavid Gibson 
2158d90ad90SDavid Gibson     if (!dev->isopen) {
2168d90ad90SDavid Gibson         return -1;
2178d90ad90SDavid Gibson     }
2188d90ad90SDavid Gibson 
2198d90ad90SDavid Gibson     if (!dev->rx_bufs) {
2205c29dd8cSThomas Huth         spapr_vlan_record_dropped_rx_frame(dev);
2218836630fSThomas Huth         return 0;
2228d90ad90SDavid Gibson     }
2238d90ad90SDavid Gibson 
224831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
225831e8822SThomas Huth         bd = spapr_vlan_get_rx_bd_from_pool(dev, size);
226831e8822SThomas Huth     } else {
227d6f39fdfSThomas Huth         bd = spapr_vlan_get_rx_bd_from_page(dev, size);
228831e8822SThomas Huth     }
229d6f39fdfSThomas Huth     if (!bd) {
2305c29dd8cSThomas Huth         spapr_vlan_record_dropped_rx_frame(dev);
2318836630fSThomas Huth         return 0;
2328d90ad90SDavid Gibson     }
2338d90ad90SDavid Gibson 
2348d90ad90SDavid Gibson     dev->rx_bufs--;
2358d90ad90SDavid Gibson 
2368d90ad90SDavid Gibson     /* Transfer the packet data */
237ad0ebb91SDavid Gibson     if (spapr_vio_dma_write(sdev, VLAN_BD_ADDR(bd) + 8, buf, size) < 0) {
2388d90ad90SDavid Gibson         return -1;
2398d90ad90SDavid Gibson     }
2408d90ad90SDavid Gibson 
241e8bb33deSLaurent Vivier     trace_spapr_vlan_receive_dma_completed();
2428d90ad90SDavid Gibson 
2438d90ad90SDavid Gibson     /* Update the receive queue */
2448d90ad90SDavid Gibson     control = VLAN_RXQC_TOGGLE | VLAN_RXQC_VALID;
2458d90ad90SDavid Gibson     if (rxq_bd & VLAN_BD_TOGGLE) {
2468d90ad90SDavid Gibson         control ^= VLAN_RXQC_TOGGLE;
2478d90ad90SDavid Gibson     }
2488d90ad90SDavid Gibson 
249ad0ebb91SDavid Gibson     handle = vio_ldq(sdev, VLAN_BD_ADDR(bd));
250ad0ebb91SDavid Gibson     vio_stq(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 8, handle);
251ad0ebb91SDavid Gibson     vio_stl(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 4, size);
252ad0ebb91SDavid Gibson     vio_sth(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 2, 8);
253ad0ebb91SDavid Gibson     vio_stb(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr, control);
2548d90ad90SDavid Gibson 
255e8bb33deSLaurent Vivier     trace_spapr_vlan_receive_wrote(dev->rxq_ptr,
256e8bb33deSLaurent Vivier                                    vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) +
2578d90ad90SDavid Gibson                                                  dev->rxq_ptr),
258e8bb33deSLaurent Vivier                                    vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) +
2598d90ad90SDavid Gibson                                                  dev->rxq_ptr + 8));
2608d90ad90SDavid Gibson 
2618d90ad90SDavid Gibson     dev->rxq_ptr += 16;
2628d90ad90SDavid Gibson     if (dev->rxq_ptr >= VLAN_BD_LEN(rxq_bd)) {
2638d90ad90SDavid Gibson         dev->rxq_ptr = 0;
264ad0ebb91SDavid Gibson         vio_stq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF, rxq_bd ^ VLAN_BD_TOGGLE);
2658d90ad90SDavid Gibson     }
2668d90ad90SDavid Gibson 
2678d90ad90SDavid Gibson     if (sdev->signal_state & 1) {
2687678b74aSDavid Gibson         spapr_vio_irq_pulse(sdev);
2698d90ad90SDavid Gibson     }
2708d90ad90SDavid Gibson 
2718d90ad90SDavid Gibson     return size;
2728d90ad90SDavid Gibson }
2738d90ad90SDavid Gibson 
2748d90ad90SDavid Gibson static NetClientInfo net_spapr_vlan_info = {
275f394b2e2SEric Blake     .type = NET_CLIENT_DRIVER_NIC,
2768d90ad90SDavid Gibson     .size = sizeof(NICState),
2778d90ad90SDavid Gibson     .can_receive = spapr_vlan_can_receive,
2788d90ad90SDavid Gibson     .receive = spapr_vlan_receive,
2798d90ad90SDavid Gibson };
2808d90ad90SDavid Gibson 
spapr_vlan_flush_rx_queue(void * opaque)2818836630fSThomas Huth static void spapr_vlan_flush_rx_queue(void *opaque)
2828836630fSThomas Huth {
283ce2918cbSDavid Gibson     SpaprVioVlan *dev = opaque;
2848836630fSThomas Huth 
2858836630fSThomas Huth     qemu_flush_queued_packets(qemu_get_queue(dev->nic));
2868836630fSThomas Huth }
2878836630fSThomas Huth 
spapr_vlan_reset_rx_pool(RxBufPool * rxp)288831e8822SThomas Huth static void spapr_vlan_reset_rx_pool(RxBufPool *rxp)
289831e8822SThomas Huth {
290831e8822SThomas Huth     /*
291831e8822SThomas Huth      * Use INT_MAX as bufsize so that unused buffers are moved to the end
292831e8822SThomas Huth      * of the list during the qsort in spapr_vlan_add_rxbuf_to_pool() later.
293831e8822SThomas Huth      */
294831e8822SThomas Huth     rxp->bufsize = INT_MAX;
295831e8822SThomas Huth     rxp->count = 0;
296831e8822SThomas Huth     memset(rxp->bds, 0, sizeof(rxp->bds));
297831e8822SThomas Huth }
298831e8822SThomas Huth 
spapr_vlan_reset(SpaprVioDevice * sdev)299ce2918cbSDavid Gibson static void spapr_vlan_reset(SpaprVioDevice *sdev)
300c17491b6SDavid Gibson {
301ce2918cbSDavid Gibson     SpaprVioVlan *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
302831e8822SThomas Huth     int i;
303c17491b6SDavid Gibson 
304c17491b6SDavid Gibson     dev->buf_list = 0;
305c17491b6SDavid Gibson     dev->rx_bufs = 0;
306c17491b6SDavid Gibson     dev->isopen = 0;
307831e8822SThomas Huth 
308831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
309831e8822SThomas Huth         for (i = 0; i < RX_MAX_POOLS; i++) {
310831e8822SThomas Huth             spapr_vlan_reset_rx_pool(dev->rx_pool[i]);
311831e8822SThomas Huth         }
312831e8822SThomas Huth     }
31332f5f50dSLaurent Vivier 
31432f5f50dSLaurent Vivier     memcpy(&dev->nicconf.macaddr.a, &dev->perm_mac.a,
31532f5f50dSLaurent Vivier            sizeof(dev->nicconf.macaddr.a));
31632f5f50dSLaurent Vivier     qemu_format_nic_info_str(qemu_get_queue(dev->nic), dev->nicconf.macaddr.a);
317c17491b6SDavid Gibson }
318c17491b6SDavid Gibson 
spapr_vlan_realize(SpaprVioDevice * sdev,Error ** errp)319ce2918cbSDavid Gibson static void spapr_vlan_realize(SpaprVioDevice *sdev, Error **errp)
3208d90ad90SDavid Gibson {
321ce2918cbSDavid Gibson     SpaprVioVlan *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
3228d90ad90SDavid Gibson 
3238d90ad90SDavid Gibson     qemu_macaddr_default_if_unset(&dev->nicconf.macaddr);
3248d90ad90SDavid Gibson 
32532f5f50dSLaurent Vivier     memcpy(&dev->perm_mac.a, &dev->nicconf.macaddr.a, sizeof(dev->perm_mac.a));
32632f5f50dSLaurent Vivier 
3278d90ad90SDavid Gibson     dev->nic = qemu_new_nic(&net_spapr_vlan_info, &dev->nicconf,
3287d0fefdfSAkihiko Odaki                             object_get_typename(OBJECT(sdev)), sdev->qdev.id,
3297d0fefdfSAkihiko Odaki                             &sdev->qdev.mem_reentrancy_guard, dev);
330b356f76dSJason Wang     qemu_format_nic_info_str(qemu_get_queue(dev->nic), dev->nicconf.macaddr.a);
3318836630fSThomas Huth 
3328836630fSThomas Huth     dev->rxp_timer = timer_new_us(QEMU_CLOCK_VIRTUAL, spapr_vlan_flush_rx_queue,
3338836630fSThomas Huth                                   dev);
3348d90ad90SDavid Gibson }
3358d90ad90SDavid Gibson 
spapr_vlan_instance_init(Object * obj)336dfe79cf2SGonglei static void spapr_vlan_instance_init(Object *obj)
337dfe79cf2SGonglei {
338ce2918cbSDavid Gibson     SpaprVioVlan *dev = VIO_SPAPR_VLAN_DEVICE(obj);
339831e8822SThomas Huth     int i;
340dfe79cf2SGonglei 
341dfe79cf2SGonglei     device_add_bootindex_property(obj, &dev->nicconf.bootindex,
342dfe79cf2SGonglei                                   "bootindex", "",
34340c2281cSMarkus Armbruster                                   DEVICE(dev));
344831e8822SThomas Huth 
345831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
346831e8822SThomas Huth         for (i = 0; i < RX_MAX_POOLS; i++) {
347831e8822SThomas Huth             dev->rx_pool[i] = g_new(RxBufPool, 1);
348831e8822SThomas Huth             spapr_vlan_reset_rx_pool(dev->rx_pool[i]);
349831e8822SThomas Huth         }
350831e8822SThomas Huth     }
351831e8822SThomas Huth }
352831e8822SThomas Huth 
spapr_vlan_instance_finalize(Object * obj)353831e8822SThomas Huth static void spapr_vlan_instance_finalize(Object *obj)
354831e8822SThomas Huth {
355ce2918cbSDavid Gibson     SpaprVioVlan *dev = VIO_SPAPR_VLAN_DEVICE(obj);
356831e8822SThomas Huth     int i;
357831e8822SThomas Huth 
358831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
359831e8822SThomas Huth         for (i = 0; i < RX_MAX_POOLS; i++) {
360831e8822SThomas Huth             g_free(dev->rx_pool[i]);
361831e8822SThomas Huth             dev->rx_pool[i] = NULL;
362831e8822SThomas Huth         }
363831e8822SThomas Huth     }
3648836630fSThomas Huth 
3658836630fSThomas Huth     if (dev->rxp_timer) {
3668836630fSThomas Huth         timer_free(dev->rxp_timer);
3678836630fSThomas Huth     }
368dfe79cf2SGonglei }
369dfe79cf2SGonglei 
spapr_vlan_create(SpaprVioBus * bus,NICInfo * nd)370ce2918cbSDavid Gibson void spapr_vlan_create(SpaprVioBus *bus, NICInfo *nd)
3718d90ad90SDavid Gibson {
3728d90ad90SDavid Gibson     DeviceState *dev;
3738d90ad90SDavid Gibson 
3743e80f690SMarkus Armbruster     dev = qdev_new("spapr-vlan");
3758d90ad90SDavid Gibson 
3768d90ad90SDavid Gibson     qdev_set_nic_properties(dev, nd);
3778d90ad90SDavid Gibson 
3783e80f690SMarkus Armbruster     qdev_realize_and_unref(dev, &bus->bus, &error_fatal);
3798d90ad90SDavid Gibson }
3808d90ad90SDavid Gibson 
spapr_vlan_devnode(SpaprVioDevice * dev,void * fdt,int node_off)381ce2918cbSDavid Gibson static int spapr_vlan_devnode(SpaprVioDevice *dev, void *fdt, int node_off)
3828d90ad90SDavid Gibson {
383ce2918cbSDavid Gibson     SpaprVioVlan *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
38887684b4cSSam Bobroff      * property.  The kernel driver (before 3.10) has an insane workaround;
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      *
39587684b4cSSam Bobroff      * Here we return a 6-byte address unless that would break a pre-3.10
39687684b4cSSam Bobroff      * driver.  In that case we return a padded 8-byte address to allow the old
39787684b4cSSam Bobroff      * workaround to succeed. */
39887684b4cSSam Bobroff     if ((vdev->nicconf.macaddr.a[0] & 0x3) == 0x2) {
39987684b4cSSam Bobroff         ret = fdt_setprop(fdt, node_off, "local-mac-address",
40087684b4cSSam Bobroff                           &vdev->nicconf.macaddr, ETH_ALEN);
40187684b4cSSam Bobroff     } else {
4028d90ad90SDavid Gibson         memcpy(&padded_mac[2], &vdev->nicconf.macaddr, ETH_ALEN);
4038d90ad90SDavid Gibson         ret = fdt_setprop(fdt, node_off, "local-mac-address",
4048d90ad90SDavid Gibson                           padded_mac, sizeof(padded_mac));
40587684b4cSSam Bobroff     }
4068d90ad90SDavid Gibson     if (ret < 0) {
4078d90ad90SDavid Gibson         return ret;
4088d90ad90SDavid Gibson     }
4098d90ad90SDavid Gibson 
4108d90ad90SDavid Gibson     ret = fdt_setprop_cell(fdt, node_off, "ibm,mac-address-filters", 0);
4118d90ad90SDavid Gibson     if (ret < 0) {
4128d90ad90SDavid Gibson         return ret;
4138d90ad90SDavid Gibson     }
4148d90ad90SDavid Gibson 
4158d90ad90SDavid Gibson     return 0;
4168d90ad90SDavid Gibson }
4178d90ad90SDavid Gibson 
check_bd(SpaprVioVlan * dev,vlan_bd_t bd,target_ulong alignment)418ce2918cbSDavid Gibson static int check_bd(SpaprVioVlan *dev, vlan_bd_t bd,
4198d90ad90SDavid Gibson                     target_ulong alignment)
4208d90ad90SDavid Gibson {
4218d90ad90SDavid Gibson     if ((VLAN_BD_ADDR(bd) % alignment)
4228d90ad90SDavid Gibson         || (VLAN_BD_LEN(bd) % alignment)) {
4238d90ad90SDavid Gibson         return -1;
4248d90ad90SDavid Gibson     }
4258d90ad90SDavid Gibson 
426ad0ebb91SDavid Gibson     if (!spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd),
427ad0ebb91SDavid Gibson                              VLAN_BD_LEN(bd), DMA_DIRECTION_FROM_DEVICE)
428ad0ebb91SDavid Gibson         || !spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd),
429ad0ebb91SDavid Gibson                                 VLAN_BD_LEN(bd), DMA_DIRECTION_TO_DEVICE)) {
4308d90ad90SDavid Gibson         return -1;
4318d90ad90SDavid Gibson     }
4328d90ad90SDavid Gibson 
4338d90ad90SDavid Gibson     return 0;
4348d90ad90SDavid Gibson }
4358d90ad90SDavid Gibson 
h_register_logical_lan(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)436b13ce26dSAndreas Färber static target_ulong h_register_logical_lan(PowerPCCPU *cpu,
437ce2918cbSDavid Gibson                                            SpaprMachineState *spapr,
4388d90ad90SDavid Gibson                                            target_ulong opcode,
4398d90ad90SDavid Gibson                                            target_ulong *args)
4408d90ad90SDavid Gibson {
4418d90ad90SDavid Gibson     target_ulong reg = args[0];
4428d90ad90SDavid Gibson     target_ulong buf_list = args[1];
4438d90ad90SDavid Gibson     target_ulong rec_queue = args[2];
4448d90ad90SDavid Gibson     target_ulong filter_list = args[3];
445ce2918cbSDavid Gibson     SpaprVioDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
446ce2918cbSDavid Gibson     SpaprVioVlan *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
4478d90ad90SDavid Gibson     vlan_bd_t filter_list_bd;
4488d90ad90SDavid Gibson 
4498d90ad90SDavid Gibson     if (!dev) {
4508d90ad90SDavid Gibson         return H_PARAMETER;
4518d90ad90SDavid Gibson     }
4528d90ad90SDavid Gibson 
4538d90ad90SDavid Gibson     if (dev->isopen) {
4548d90ad90SDavid Gibson         hcall_dprintf("H_REGISTER_LOGICAL_LAN called twice without "
4558d90ad90SDavid Gibson                       "H_FREE_LOGICAL_LAN\n");
4568d90ad90SDavid Gibson         return H_RESOURCE;
4578d90ad90SDavid Gibson     }
4588d90ad90SDavid Gibson 
459ad0ebb91SDavid Gibson     if (check_bd(dev, VLAN_VALID_BD(buf_list, SPAPR_TCE_PAGE_SIZE),
460ad0ebb91SDavid Gibson                  SPAPR_TCE_PAGE_SIZE) < 0) {
461d9599c92SDavid Gibson         hcall_dprintf("Bad buf_list 0x" TARGET_FMT_lx "\n", buf_list);
4628d90ad90SDavid Gibson         return H_PARAMETER;
4638d90ad90SDavid Gibson     }
4648d90ad90SDavid Gibson 
465ad0ebb91SDavid Gibson     filter_list_bd = VLAN_VALID_BD(filter_list, SPAPR_TCE_PAGE_SIZE);
466ad0ebb91SDavid Gibson     if (check_bd(dev, filter_list_bd, SPAPR_TCE_PAGE_SIZE) < 0) {
467d9599c92SDavid Gibson         hcall_dprintf("Bad filter_list 0x" TARGET_FMT_lx "\n", filter_list);
4688d90ad90SDavid Gibson         return H_PARAMETER;
4698d90ad90SDavid Gibson     }
4708d90ad90SDavid Gibson 
4718d90ad90SDavid Gibson     if (!(rec_queue & VLAN_BD_VALID)
4728d90ad90SDavid Gibson         || (check_bd(dev, rec_queue, VLAN_RQ_ALIGNMENT) < 0)) {
473d9599c92SDavid Gibson         hcall_dprintf("Bad receive queue\n");
4748d90ad90SDavid Gibson         return H_PARAMETER;
4758d90ad90SDavid Gibson     }
4768d90ad90SDavid Gibson 
4778d90ad90SDavid Gibson     dev->buf_list = buf_list;
4788d90ad90SDavid Gibson     sdev->signal_state = 0;
4798d90ad90SDavid Gibson 
4808d90ad90SDavid Gibson     rec_queue &= ~VLAN_BD_TOGGLE;
4818d90ad90SDavid Gibson 
4828d90ad90SDavid Gibson     /* Initialize the buffer list */
483ad0ebb91SDavid Gibson     vio_stq(sdev, buf_list, rec_queue);
484ad0ebb91SDavid Gibson     vio_stq(sdev, buf_list + 8, filter_list_bd);
485ad0ebb91SDavid Gibson     spapr_vio_dma_set(sdev, buf_list + VLAN_RX_BDS_OFF, 0,
486ad0ebb91SDavid Gibson                       SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF);
4878d90ad90SDavid Gibson     dev->add_buf_ptr = VLAN_RX_BDS_OFF - 8;
4888d90ad90SDavid Gibson     dev->use_buf_ptr = VLAN_RX_BDS_OFF - 8;
4898d90ad90SDavid Gibson     dev->rx_bufs = 0;
4908d90ad90SDavid Gibson     dev->rxq_ptr = 0;
4918d90ad90SDavid Gibson 
4928d90ad90SDavid Gibson     /* Initialize the receive queue */
493ad0ebb91SDavid Gibson     spapr_vio_dma_set(sdev, VLAN_BD_ADDR(rec_queue), 0, VLAN_BD_LEN(rec_queue));
4948d90ad90SDavid Gibson 
4958d90ad90SDavid Gibson     dev->isopen = 1;
496e0ff466cSAlexey Kardashevskiy     qemu_flush_queued_packets(qemu_get_queue(dev->nic));
497e0ff466cSAlexey Kardashevskiy 
4988d90ad90SDavid Gibson     return H_SUCCESS;
4998d90ad90SDavid Gibson }
5008d90ad90SDavid Gibson 
5018d90ad90SDavid Gibson 
h_free_logical_lan(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)50228e02042SDavid Gibson static target_ulong h_free_logical_lan(PowerPCCPU *cpu,
503ce2918cbSDavid Gibson                                        SpaprMachineState *spapr,
5048d90ad90SDavid Gibson                                        target_ulong opcode, target_ulong *args)
5058d90ad90SDavid Gibson {
5068d90ad90SDavid Gibson     target_ulong reg = args[0];
507ce2918cbSDavid Gibson     SpaprVioDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
508ce2918cbSDavid Gibson     SpaprVioVlan *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
5098d90ad90SDavid Gibson 
5108d90ad90SDavid Gibson     if (!dev) {
5118d90ad90SDavid Gibson         return H_PARAMETER;
5128d90ad90SDavid Gibson     }
5138d90ad90SDavid Gibson 
5148d90ad90SDavid Gibson     if (!dev->isopen) {
5158d90ad90SDavid Gibson         hcall_dprintf("H_FREE_LOGICAL_LAN called without "
5168d90ad90SDavid Gibson                       "H_REGISTER_LOGICAL_LAN\n");
5178d90ad90SDavid Gibson         return H_RESOURCE;
5188d90ad90SDavid Gibson     }
5198d90ad90SDavid Gibson 
520c17491b6SDavid Gibson     spapr_vlan_reset(sdev);
5218d90ad90SDavid Gibson     return H_SUCCESS;
5228d90ad90SDavid Gibson }
5238d90ad90SDavid Gibson 
524831e8822SThomas Huth /**
525831e8822SThomas Huth  * Used for qsort, this function compares two RxBufPools by size.
526831e8822SThomas Huth  */
rx_pool_size_compare(const void * p1,const void * p2)527831e8822SThomas Huth static int rx_pool_size_compare(const void *p1, const void *p2)
528831e8822SThomas Huth {
529831e8822SThomas Huth     const RxBufPool *pool1 = *(RxBufPool **)p1;
530831e8822SThomas Huth     const RxBufPool *pool2 = *(RxBufPool **)p2;
531831e8822SThomas Huth 
532831e8822SThomas Huth     if (pool1->bufsize < pool2->bufsize) {
533831e8822SThomas Huth         return -1;
534831e8822SThomas Huth     }
535831e8822SThomas Huth     return pool1->bufsize > pool2->bufsize;
536831e8822SThomas Huth }
537831e8822SThomas Huth 
538831e8822SThomas Huth /**
539831e8822SThomas Huth  * Search for a matching buffer pool with exact matching size,
540831e8822SThomas Huth  * or return -1 if no matching pool has been found.
541831e8822SThomas Huth  */
spapr_vlan_get_rx_pool_id(SpaprVioVlan * dev,int size)542ce2918cbSDavid Gibson static int spapr_vlan_get_rx_pool_id(SpaprVioVlan *dev, int size)
543831e8822SThomas Huth {
544831e8822SThomas Huth     int pool;
545831e8822SThomas Huth 
546831e8822SThomas Huth     for (pool = 0; pool < RX_MAX_POOLS; pool++) {
547831e8822SThomas Huth         if (dev->rx_pool[pool]->bufsize == size) {
548831e8822SThomas Huth             return pool;
549831e8822SThomas Huth         }
550831e8822SThomas Huth     }
551831e8822SThomas Huth 
552831e8822SThomas Huth     return -1;
553831e8822SThomas Huth }
554831e8822SThomas Huth 
555831e8822SThomas Huth /**
556831e8822SThomas Huth  * Enqueuing receive buffer by adding it to one of our receive buffer pools
557831e8822SThomas Huth  */
spapr_vlan_add_rxbuf_to_pool(SpaprVioVlan * dev,target_ulong buf)558ce2918cbSDavid Gibson static target_long spapr_vlan_add_rxbuf_to_pool(SpaprVioVlan *dev,
559831e8822SThomas Huth                                                 target_ulong buf)
560831e8822SThomas Huth {
561831e8822SThomas Huth     int size = VLAN_BD_LEN(buf);
562831e8822SThomas Huth     int pool;
563831e8822SThomas Huth 
564831e8822SThomas Huth     pool = spapr_vlan_get_rx_pool_id(dev, size);
565831e8822SThomas Huth     if (pool < 0) {
566831e8822SThomas Huth         /*
567831e8822SThomas Huth          * No matching pool found? Try to use a new one. If the guest used all
568b12227afSStefan Weil          * pools before, but changed the size of one pool in the meantime, we might
569831e8822SThomas Huth          * need to recycle that pool here (if it's empty already). Thus scan
570831e8822SThomas Huth          * all buffer pools now, starting with the last (likely empty) one.
571831e8822SThomas Huth          */
572831e8822SThomas Huth         for (pool = RX_MAX_POOLS - 1; pool >= 0 ; pool--) {
573831e8822SThomas Huth             if (dev->rx_pool[pool]->count == 0) {
574831e8822SThomas Huth                 dev->rx_pool[pool]->bufsize = size;
575831e8822SThomas Huth                 /*
576831e8822SThomas Huth                  * Sort pools by size so that spapr_vlan_receive()
577831e8822SThomas Huth                  * can later find the smallest buffer pool easily.
578831e8822SThomas Huth                  */
579831e8822SThomas Huth                 qsort(dev->rx_pool, RX_MAX_POOLS, sizeof(dev->rx_pool[0]),
580831e8822SThomas Huth                       rx_pool_size_compare);
581831e8822SThomas Huth                 pool = spapr_vlan_get_rx_pool_id(dev, size);
582e8bb33deSLaurent Vivier                 trace_spapr_vlan_add_rxbuf_to_pool_create(pool,
583831e8822SThomas Huth                                                           VLAN_BD_LEN(buf));
584831e8822SThomas Huth                 break;
585831e8822SThomas Huth             }
586831e8822SThomas Huth         }
587831e8822SThomas Huth     }
588831e8822SThomas Huth     /* Still no usable pool? Give up */
589831e8822SThomas Huth     if (pool < 0 || dev->rx_pool[pool]->count >= RX_POOL_MAX_BDS) {
590831e8822SThomas Huth         return H_RESOURCE;
591831e8822SThomas Huth     }
592831e8822SThomas Huth 
593e8bb33deSLaurent Vivier     trace_spapr_vlan_add_rxbuf_to_pool(pool, VLAN_BD_LEN(buf),
594e8bb33deSLaurent Vivier                                        dev->rx_pool[pool]->count);
595831e8822SThomas Huth 
596831e8822SThomas Huth     dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count++] = buf;
597831e8822SThomas Huth 
598831e8822SThomas Huth     return 0;
599831e8822SThomas Huth }
600831e8822SThomas Huth 
601831e8822SThomas Huth /**
602831e8822SThomas Huth  * This is the old way of enqueuing receive buffers: Add it to the rx queue
603831e8822SThomas Huth  * page that has been supplied by the guest (which is quite limited in size).
604831e8822SThomas Huth  */
spapr_vlan_add_rxbuf_to_page(SpaprVioVlan * dev,target_ulong buf)605ce2918cbSDavid Gibson static target_long spapr_vlan_add_rxbuf_to_page(SpaprVioVlan *dev,
606d6f39fdfSThomas Huth                                                 target_ulong buf)
607d6f39fdfSThomas Huth {
608d6f39fdfSThomas Huth     vlan_bd_t bd;
609d6f39fdfSThomas Huth 
610d6f39fdfSThomas Huth     if (dev->rx_bufs >= VLAN_MAX_BUFS) {
611d6f39fdfSThomas Huth         return H_RESOURCE;
612d6f39fdfSThomas Huth     }
613d6f39fdfSThomas Huth 
614d6f39fdfSThomas Huth     do {
615d6f39fdfSThomas Huth         dev->add_buf_ptr += 8;
616d6f39fdfSThomas Huth         if (dev->add_buf_ptr >= VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF) {
617d6f39fdfSThomas Huth             dev->add_buf_ptr = VLAN_RX_BDS_OFF;
618d6f39fdfSThomas Huth         }
619d6f39fdfSThomas Huth 
620d6f39fdfSThomas Huth         bd = vio_ldq(&dev->sdev, dev->buf_list + dev->add_buf_ptr);
621d6f39fdfSThomas Huth     } while (bd & VLAN_BD_VALID);
622d6f39fdfSThomas Huth 
623d6f39fdfSThomas Huth     vio_stq(&dev->sdev, dev->buf_list + dev->add_buf_ptr, buf);
624d6f39fdfSThomas Huth 
625e8bb33deSLaurent Vivier     trace_spapr_vlan_add_rxbuf_to_page(dev->add_buf_ptr, dev->rx_bufs, buf);
626d6f39fdfSThomas Huth 
627d6f39fdfSThomas Huth     return 0;
628d6f39fdfSThomas Huth }
629d6f39fdfSThomas Huth 
h_add_logical_lan_buffer(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)630b13ce26dSAndreas Färber static target_ulong h_add_logical_lan_buffer(PowerPCCPU *cpu,
631ce2918cbSDavid Gibson                                              SpaprMachineState *spapr,
6328d90ad90SDavid Gibson                                              target_ulong opcode,
6338d90ad90SDavid Gibson                                              target_ulong *args)
6348d90ad90SDavid Gibson {
6358d90ad90SDavid Gibson     target_ulong reg = args[0];
6368d90ad90SDavid Gibson     target_ulong buf = args[1];
637ce2918cbSDavid Gibson     SpaprVioDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
638ce2918cbSDavid Gibson     SpaprVioVlan *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
639d6f39fdfSThomas Huth     target_long ret;
6408d90ad90SDavid Gibson 
641e8bb33deSLaurent Vivier     trace_spapr_vlan_h_add_logical_lan_buffer(reg, buf);
6428d90ad90SDavid Gibson 
6438d90ad90SDavid Gibson     if (!sdev) {
644d9599c92SDavid Gibson         hcall_dprintf("Bad device\n");
6458d90ad90SDavid Gibson         return H_PARAMETER;
6468d90ad90SDavid Gibson     }
6478d90ad90SDavid Gibson 
6488d90ad90SDavid Gibson     if ((check_bd(dev, buf, 4) < 0)
6498d90ad90SDavid Gibson         || (VLAN_BD_LEN(buf) < 16)) {
650d9599c92SDavid Gibson         hcall_dprintf("Bad buffer enqueued\n");
6518d90ad90SDavid Gibson         return H_PARAMETER;
6528d90ad90SDavid Gibson     }
6538d90ad90SDavid Gibson 
654d6f39fdfSThomas Huth     if (!dev->isopen) {
6558d90ad90SDavid Gibson         return H_RESOURCE;
6568d90ad90SDavid Gibson     }
6578d90ad90SDavid Gibson 
658831e8822SThomas Huth     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
659831e8822SThomas Huth         ret = spapr_vlan_add_rxbuf_to_pool(dev, buf);
660831e8822SThomas Huth     } else {
661d6f39fdfSThomas Huth         ret = spapr_vlan_add_rxbuf_to_page(dev, buf);
662831e8822SThomas Huth     }
663d6f39fdfSThomas Huth     if (ret) {
664d6f39fdfSThomas Huth         return ret;
6658d90ad90SDavid Gibson     }
6668d90ad90SDavid Gibson 
6678d90ad90SDavid Gibson     dev->rx_bufs++;
6688d90ad90SDavid Gibson 
6698836630fSThomas Huth     /*
6708836630fSThomas Huth      * Give guest some more time to add additional RX buffers before we
6718836630fSThomas Huth      * flush the receive queue, so that e.g. fragmented IP packets can
6728836630fSThomas Huth      * be passed to the guest in one go later (instead of passing single
6738836630fSThomas Huth      * fragments if there is only one receive buffer available).
6748836630fSThomas Huth      */
6758836630fSThomas Huth     timer_mod(dev->rxp_timer, qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + 500);
6760a61f3b4SAlexey Kardashevskiy 
6778d90ad90SDavid Gibson     return H_SUCCESS;
6788d90ad90SDavid Gibson }
6798d90ad90SDavid Gibson 
h_send_logical_lan(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)68028e02042SDavid Gibson static target_ulong h_send_logical_lan(PowerPCCPU *cpu,
681ce2918cbSDavid Gibson                                        SpaprMachineState *spapr,
6828d90ad90SDavid Gibson                                        target_ulong opcode, target_ulong *args)
6838d90ad90SDavid Gibson {
6848d90ad90SDavid Gibson     target_ulong reg = args[0];
6858d90ad90SDavid Gibson     target_ulong *bufs = args + 1;
6868d90ad90SDavid Gibson     target_ulong continue_token = args[7];
687ce2918cbSDavid Gibson     SpaprVioDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
688ce2918cbSDavid Gibson     SpaprVioVlan *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
6898d90ad90SDavid Gibson     unsigned total_len;
6902d154d26SElena Afanasova     uint8_t *p;
6912d154d26SElena Afanasova     g_autofree uint8_t *lbuf = NULL;
6928d90ad90SDavid Gibson     int i, nbufs;
6938d90ad90SDavid Gibson     int ret;
6948d90ad90SDavid Gibson 
695e8bb33deSLaurent Vivier     trace_spapr_vlan_h_send_logical_lan(reg, continue_token);
6968d90ad90SDavid Gibson 
6978d90ad90SDavid Gibson     if (!sdev) {
6988d90ad90SDavid Gibson         return H_PARAMETER;
6998d90ad90SDavid Gibson     }
7008d90ad90SDavid Gibson 
701e8bb33deSLaurent Vivier     trace_spapr_vlan_h_send_logical_lan_rxbufs(dev->rx_bufs);
7028d90ad90SDavid Gibson 
7038d90ad90SDavid Gibson     if (!dev->isopen) {
7048d90ad90SDavid Gibson         return H_DROPPED;
7058d90ad90SDavid Gibson     }
7068d90ad90SDavid Gibson 
7078d90ad90SDavid Gibson     if (continue_token) {
7088d90ad90SDavid Gibson         return H_HARDWARE; /* FIXME actually handle this */
7098d90ad90SDavid Gibson     }
7108d90ad90SDavid Gibson 
7118d90ad90SDavid Gibson     total_len = 0;
7128d90ad90SDavid Gibson     for (i = 0; i < 6; i++) {
713e8bb33deSLaurent Vivier         trace_spapr_vlan_h_send_logical_lan_buf_desc(bufs[i]);
7148d90ad90SDavid Gibson         if (!(bufs[i] & VLAN_BD_VALID)) {
7158d90ad90SDavid Gibson             break;
7168d90ad90SDavid Gibson         }
7178d90ad90SDavid Gibson         total_len += VLAN_BD_LEN(bufs[i]);
7188d90ad90SDavid Gibson     }
7198d90ad90SDavid Gibson 
7208d90ad90SDavid Gibson     nbufs = i;
721e8bb33deSLaurent Vivier     trace_spapr_vlan_h_send_logical_lan_total(nbufs, total_len);
7228d90ad90SDavid Gibson 
7238d90ad90SDavid Gibson     if (total_len == 0) {
7248d90ad90SDavid Gibson         return H_SUCCESS;
7258d90ad90SDavid Gibson     }
7268d90ad90SDavid Gibson 
7278d90ad90SDavid Gibson     if (total_len > MAX_PACKET_SIZE) {
7288d90ad90SDavid Gibson         /* Don't let the guest force too large an allocation */
7298d90ad90SDavid Gibson         return H_RESOURCE;
7308d90ad90SDavid Gibson     }
7318d90ad90SDavid Gibson 
7322d154d26SElena Afanasova     lbuf = g_malloc(total_len);
7338d90ad90SDavid Gibson     p = lbuf;
7348d90ad90SDavid Gibson     for (i = 0; i < nbufs; i++) {
735ad0ebb91SDavid Gibson         ret = spapr_vio_dma_read(sdev, VLAN_BD_ADDR(bufs[i]),
7368d90ad90SDavid Gibson                                  p, VLAN_BD_LEN(bufs[i]));
7378d90ad90SDavid Gibson         if (ret < 0) {
7388d90ad90SDavid Gibson             return ret;
7398d90ad90SDavid Gibson         }
7408d90ad90SDavid Gibson 
7418d90ad90SDavid Gibson         p += VLAN_BD_LEN(bufs[i]);
7428d90ad90SDavid Gibson     }
7438d90ad90SDavid Gibson 
744b356f76dSJason Wang     qemu_send_packet(qemu_get_queue(dev->nic), lbuf, total_len);
7458d90ad90SDavid Gibson 
7468d90ad90SDavid Gibson     return H_SUCCESS;
7478d90ad90SDavid Gibson }
7488d90ad90SDavid Gibson 
h_multicast_ctrl(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)749ce2918cbSDavid Gibson static target_ulong h_multicast_ctrl(PowerPCCPU *cpu, SpaprMachineState *spapr,
7508d90ad90SDavid Gibson                                      target_ulong opcode, target_ulong *args)
7518d90ad90SDavid Gibson {
7528d90ad90SDavid Gibson     target_ulong reg = args[0];
753ce2918cbSDavid Gibson     SpaprVioDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
7548d90ad90SDavid Gibson 
7558d90ad90SDavid Gibson     if (!dev) {
7568d90ad90SDavid Gibson         return H_PARAMETER;
7578d90ad90SDavid Gibson     }
7588d90ad90SDavid Gibson 
7598d90ad90SDavid Gibson     return H_SUCCESS;
7608d90ad90SDavid Gibson }
7618d90ad90SDavid Gibson 
h_change_logical_lan_mac(PowerPCCPU * cpu,SpaprMachineState * spapr,target_ulong opcode,target_ulong * args)76232f5f50dSLaurent Vivier static target_ulong h_change_logical_lan_mac(PowerPCCPU *cpu,
763ce2918cbSDavid Gibson                                              SpaprMachineState *spapr,
76432f5f50dSLaurent Vivier                                              target_ulong opcode,
76532f5f50dSLaurent Vivier                                              target_ulong *args)
76632f5f50dSLaurent Vivier {
76732f5f50dSLaurent Vivier     target_ulong reg = args[0];
76832f5f50dSLaurent Vivier     target_ulong macaddr = args[1];
769ce2918cbSDavid Gibson     SpaprVioDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
770ce2918cbSDavid Gibson     SpaprVioVlan *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
77132f5f50dSLaurent Vivier     int i;
77232f5f50dSLaurent Vivier 
77367f67bd8SOleg Sviridov     if (!dev) {
77467f67bd8SOleg Sviridov         hcall_dprintf("H_CHANGE_LOGICAL_LAN_MAC called when "
77567f67bd8SOleg Sviridov                       "no NIC is present\n");
77667f67bd8SOleg Sviridov         return H_PARAMETER;
77767f67bd8SOleg Sviridov     }
77867f67bd8SOleg Sviridov 
77932f5f50dSLaurent Vivier     for (i = 0; i < ETH_ALEN; i++) {
78032f5f50dSLaurent Vivier         dev->nicconf.macaddr.a[ETH_ALEN - i - 1] = macaddr & 0xff;
78132f5f50dSLaurent Vivier         macaddr >>= 8;
78232f5f50dSLaurent Vivier     }
78332f5f50dSLaurent Vivier 
78432f5f50dSLaurent Vivier     qemu_format_nic_info_str(qemu_get_queue(dev->nic), dev->nicconf.macaddr.a);
78532f5f50dSLaurent Vivier 
78632f5f50dSLaurent Vivier     return H_SUCCESS;
78732f5f50dSLaurent Vivier }
78832f5f50dSLaurent Vivier 
789e732f00fSRichard Henderson static const Property spapr_vlan_properties[] = {
790ce2918cbSDavid Gibson     DEFINE_SPAPR_PROPERTIES(SpaprVioVlan, sdev),
791ce2918cbSDavid Gibson     DEFINE_NIC_PROPERTIES(SpaprVioVlan, nicconf),
792ce2918cbSDavid Gibson     DEFINE_PROP_BIT("use-rx-buffer-pools", SpaprVioVlan,
79357c522f4SThomas Huth                     compat_flags, SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT, true),
7943954d33aSAnthony Liguori };
7953954d33aSAnthony Liguori 
spapr_vlan_rx_buffer_pools_needed(void * opaque)796831e8822SThomas Huth static bool spapr_vlan_rx_buffer_pools_needed(void *opaque)
797831e8822SThomas Huth {
798ce2918cbSDavid Gibson     SpaprVioVlan *dev = opaque;
799831e8822SThomas Huth 
800831e8822SThomas Huth     return (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) != 0;
801831e8822SThomas Huth }
802831e8822SThomas Huth 
803831e8822SThomas Huth static const VMStateDescription vmstate_rx_buffer_pool = {
804831e8822SThomas Huth     .name = "spapr_llan/rx_buffer_pool",
805831e8822SThomas Huth     .version_id = 1,
806831e8822SThomas Huth     .minimum_version_id = 1,
807831e8822SThomas Huth     .needed = spapr_vlan_rx_buffer_pools_needed,
8081de81b42SRichard Henderson     .fields = (const VMStateField[]) {
809831e8822SThomas Huth         VMSTATE_INT32(bufsize, RxBufPool),
810831e8822SThomas Huth         VMSTATE_INT32(count, RxBufPool),
811831e8822SThomas Huth         VMSTATE_UINT64_ARRAY(bds, RxBufPool, RX_POOL_MAX_BDS),
812831e8822SThomas Huth         VMSTATE_END_OF_LIST()
813831e8822SThomas Huth     }
814831e8822SThomas Huth };
815831e8822SThomas Huth 
816831e8822SThomas Huth static const VMStateDescription vmstate_rx_pools = {
817831e8822SThomas Huth     .name = "spapr_llan/rx_pools",
818831e8822SThomas Huth     .version_id = 1,
819831e8822SThomas Huth     .minimum_version_id = 1,
820831e8822SThomas Huth     .needed = spapr_vlan_rx_buffer_pools_needed,
8211de81b42SRichard Henderson     .fields = (const VMStateField[]) {
822ce2918cbSDavid Gibson         VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(rx_pool, SpaprVioVlan,
823831e8822SThomas Huth                                            RX_MAX_POOLS, 1,
824831e8822SThomas Huth                                            vmstate_rx_buffer_pool, RxBufPool),
825831e8822SThomas Huth         VMSTATE_END_OF_LIST()
826831e8822SThomas Huth     }
827831e8822SThomas Huth };
828831e8822SThomas Huth 
829686fefe4SDavid Gibson static const VMStateDescription vmstate_spapr_llan = {
830686fefe4SDavid Gibson     .name = "spapr_llan",
831686fefe4SDavid Gibson     .version_id = 1,
832686fefe4SDavid Gibson     .minimum_version_id = 1,
8331de81b42SRichard Henderson     .fields = (const VMStateField[]) {
834ce2918cbSDavid Gibson         VMSTATE_SPAPR_VIO(sdev, SpaprVioVlan),
835686fefe4SDavid Gibson         /* LLAN state */
836ce2918cbSDavid Gibson         VMSTATE_BOOL(isopen, SpaprVioVlan),
837ce2918cbSDavid Gibson         VMSTATE_UINT64(buf_list, SpaprVioVlan),
838ce2918cbSDavid Gibson         VMSTATE_UINT32(add_buf_ptr, SpaprVioVlan),
839ce2918cbSDavid Gibson         VMSTATE_UINT32(use_buf_ptr, SpaprVioVlan),
840ce2918cbSDavid Gibson         VMSTATE_UINT32(rx_bufs, SpaprVioVlan),
841ce2918cbSDavid Gibson         VMSTATE_UINT64(rxq_ptr, SpaprVioVlan),
842686fefe4SDavid Gibson 
843686fefe4SDavid Gibson         VMSTATE_END_OF_LIST()
844686fefe4SDavid Gibson     },
8451de81b42SRichard Henderson     .subsections = (const VMStateDescription * const []) {
846831e8822SThomas Huth         &vmstate_rx_pools,
847831e8822SThomas Huth         NULL
848831e8822SThomas Huth     }
849686fefe4SDavid Gibson };
850686fefe4SDavid Gibson 
spapr_vlan_class_init(ObjectClass * klass,const void * data)851*12d1a768SPhilippe Mathieu-Daudé static void spapr_vlan_class_init(ObjectClass *klass, const void *data)
8523954d33aSAnthony Liguori {
85339bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
854ce2918cbSDavid Gibson     SpaprVioDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
8553954d33aSAnthony Liguori 
85628b07e73SMarkus Armbruster     k->realize = spapr_vlan_realize;
857c17491b6SDavid Gibson     k->reset = spapr_vlan_reset;
8583954d33aSAnthony Liguori     k->devnode = spapr_vlan_devnode;
8593954d33aSAnthony Liguori     k->dt_name = "l-lan";
8603954d33aSAnthony Liguori     k->dt_type = "network";
8613954d33aSAnthony Liguori     k->dt_compatible = "IBM,l-lan";
8623954d33aSAnthony Liguori     k->signal_mask = 0x1;
86329fdedfeSAlexey Kardashevskiy     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
8644f67d30bSMarc-André Lureau     device_class_set_props(dc, spapr_vlan_properties);
865ad0ebb91SDavid Gibson     k->rtce_window_size = 0x10000000;
866686fefe4SDavid Gibson     dc->vmsd = &vmstate_spapr_llan;
8673954d33aSAnthony Liguori }
8683954d33aSAnthony Liguori 
8698c43a6f0SAndreas Färber static const TypeInfo spapr_vlan_info = {
870fd506b4fSDavid Gibson     .name          = TYPE_VIO_SPAPR_VLAN_DEVICE,
87139bffca2SAnthony Liguori     .parent        = TYPE_VIO_SPAPR_DEVICE,
872ce2918cbSDavid Gibson     .instance_size = sizeof(SpaprVioVlan),
8733954d33aSAnthony Liguori     .class_init    = spapr_vlan_class_init,
874dfe79cf2SGonglei     .instance_init = spapr_vlan_instance_init,
875831e8822SThomas Huth     .instance_finalize = spapr_vlan_instance_finalize,
8768d90ad90SDavid Gibson };
8778d90ad90SDavid Gibson 
spapr_vlan_register_types(void)87883f7d43aSAndreas Färber static void spapr_vlan_register_types(void)
8798d90ad90SDavid Gibson {
8801fc02533SDavid Gibson     spapr_register_hypercall(H_REGISTER_LOGICAL_LAN, h_register_logical_lan);
8811fc02533SDavid Gibson     spapr_register_hypercall(H_FREE_LOGICAL_LAN, h_free_logical_lan);
8821fc02533SDavid Gibson     spapr_register_hypercall(H_SEND_LOGICAL_LAN, h_send_logical_lan);
8831fc02533SDavid Gibson     spapr_register_hypercall(H_ADD_LOGICAL_LAN_BUFFER,
8841fc02533SDavid Gibson                              h_add_logical_lan_buffer);
8851fc02533SDavid Gibson     spapr_register_hypercall(H_MULTICAST_CTRL, h_multicast_ctrl);
88632f5f50dSLaurent Vivier     spapr_register_hypercall(H_CHANGE_LOGICAL_LAN_MAC,
88732f5f50dSLaurent Vivier                              h_change_logical_lan_mac);
88839bffca2SAnthony Liguori     type_register_static(&spapr_vlan_info);
8898d90ad90SDavid Gibson }
89083f7d43aSAndreas Färber 
89183f7d43aSAndreas Färber type_init(spapr_vlan_register_types)
892