usb-bot: hotplug support
[qemu/kevin.git] / hw / net / spapr_llan.c
blob8b2eebd4e3e3f37d0ac7e348d7f35ee30aa03e3e
1 /*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
4 * PAPR Inter-VM Logical Lan, aka ibmveth
6 * Copyright (c) 2010,2011 David Gibson, IBM Corporation.
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
27 #include "qemu/osdep.h"
28 #include "qemu-common.h"
29 #include "cpu.h"
30 #include "hw/hw.h"
31 #include "qemu/log.h"
32 #include "net/net.h"
33 #include "hw/qdev.h"
34 #include "hw/ppc/spapr.h"
35 #include "hw/ppc/spapr_vio.h"
36 #include "sysemu/sysemu.h"
38 #include <libfdt.h>
40 #define ETH_ALEN 6
41 #define MAX_PACKET_SIZE 65536
43 /*#define DEBUG*/
45 #ifdef DEBUG
46 #define DPRINTF(fmt...) do { fprintf(stderr, fmt); } while (0)
47 #else
48 #define DPRINTF(fmt...)
49 #endif
51 /* Compatibility flags for migration */
52 #define SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT 0
53 #define SPAPRVLAN_FLAG_RX_BUF_POOLS (1 << SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT)
56 * Virtual LAN device
59 typedef uint64_t vlan_bd_t;
61 #define VLAN_BD_VALID 0x8000000000000000ULL
62 #define VLAN_BD_TOGGLE 0x4000000000000000ULL
63 #define VLAN_BD_NO_CSUM 0x0200000000000000ULL
64 #define VLAN_BD_CSUM_GOOD 0x0100000000000000ULL
65 #define VLAN_BD_LEN_MASK 0x00ffffff00000000ULL
66 #define VLAN_BD_LEN(bd) (((bd) & VLAN_BD_LEN_MASK) >> 32)
67 #define VLAN_BD_ADDR_MASK 0x00000000ffffffffULL
68 #define VLAN_BD_ADDR(bd) ((bd) & VLAN_BD_ADDR_MASK)
70 #define VLAN_VALID_BD(addr, len) (VLAN_BD_VALID | \
71 (((len) << 32) & VLAN_BD_LEN_MASK) | \
72 (addr & VLAN_BD_ADDR_MASK))
74 #define VLAN_RXQC_TOGGLE 0x80
75 #define VLAN_RXQC_VALID 0x40
76 #define VLAN_RXQC_NO_CSUM 0x02
77 #define VLAN_RXQC_CSUM_GOOD 0x01
79 #define VLAN_RQ_ALIGNMENT 16
80 #define VLAN_RXQ_BD_OFF 0
81 #define VLAN_FILTER_BD_OFF 8
82 #define VLAN_RX_BDS_OFF 16
84 * The final 8 bytes of the buffer list is a counter of frames dropped
85 * because there was not a buffer in the buffer list capable of holding
86 * the frame. We must avoid it, or the operating system will report garbage
87 * for this statistic.
89 #define VLAN_RX_BDS_LEN (SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF - 8)
90 #define VLAN_MAX_BUFS (VLAN_RX_BDS_LEN / 8)
92 #define TYPE_VIO_SPAPR_VLAN_DEVICE "spapr-vlan"
93 #define VIO_SPAPR_VLAN_DEVICE(obj) \
94 OBJECT_CHECK(VIOsPAPRVLANDevice, (obj), TYPE_VIO_SPAPR_VLAN_DEVICE)
96 #define RX_POOL_MAX_BDS 4096
97 #define RX_MAX_POOLS 5
99 typedef struct {
100 int32_t bufsize;
101 int32_t count;
102 vlan_bd_t bds[RX_POOL_MAX_BDS];
103 } RxBufPool;
105 typedef struct VIOsPAPRVLANDevice {
106 VIOsPAPRDevice sdev;
107 NICConf nicconf;
108 NICState *nic;
109 bool isopen;
110 hwaddr buf_list;
111 uint32_t add_buf_ptr, use_buf_ptr, rx_bufs;
112 hwaddr rxq_ptr;
113 QEMUTimer *rxp_timer;
114 uint32_t compat_flags; /* Compatability flags for migration */
115 RxBufPool *rx_pool[RX_MAX_POOLS]; /* Receive buffer descriptor pools */
116 } VIOsPAPRVLANDevice;
118 static int spapr_vlan_can_receive(NetClientState *nc)
120 VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc);
122 return (dev->isopen && dev->rx_bufs > 0);
126 * The last 8 bytes of the receive buffer list page (that has been
127 * supplied by the guest with the H_REGISTER_LOGICAL_LAN call) contain
128 * a counter for frames that have been dropped because there was no
129 * suitable receive buffer available. This function is used to increase
130 * this counter by one.
132 static void spapr_vlan_record_dropped_rx_frame(VIOsPAPRVLANDevice *dev)
134 uint64_t cnt;
136 cnt = vio_ldq(&dev->sdev, dev->buf_list + 4096 - 8);
137 vio_stq(&dev->sdev, dev->buf_list + 4096 - 8, cnt + 1);
141 * Get buffer descriptor from one of our receive buffer pools
143 static vlan_bd_t spapr_vlan_get_rx_bd_from_pool(VIOsPAPRVLANDevice *dev,
144 size_t size)
146 vlan_bd_t bd;
147 int pool;
149 for (pool = 0; pool < RX_MAX_POOLS; pool++) {
150 if (dev->rx_pool[pool]->count > 0 &&
151 dev->rx_pool[pool]->bufsize >= size + 8) {
152 break;
155 if (pool == RX_MAX_POOLS) {
156 /* Failed to find a suitable buffer */
157 return 0;
160 DPRINTF("Found buffer: pool=%d count=%d rxbufs=%d\n", pool,
161 dev->rx_pool[pool]->count, dev->rx_bufs);
163 /* Remove the buffer from the pool */
164 dev->rx_pool[pool]->count--;
165 bd = dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count];
166 dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count] = 0;
168 return bd;
172 * Get buffer descriptor from the receive buffer list page that has been
173 * supplied by the guest with the H_REGISTER_LOGICAL_LAN call
175 static vlan_bd_t spapr_vlan_get_rx_bd_from_page(VIOsPAPRVLANDevice *dev,
176 size_t size)
178 int buf_ptr = dev->use_buf_ptr;
179 vlan_bd_t bd;
181 do {
182 buf_ptr += 8;
183 if (buf_ptr >= VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF) {
184 buf_ptr = VLAN_RX_BDS_OFF;
187 bd = vio_ldq(&dev->sdev, dev->buf_list + buf_ptr);
188 DPRINTF("use_buf_ptr=%d bd=0x%016llx\n",
189 buf_ptr, (unsigned long long)bd);
190 } while ((!(bd & VLAN_BD_VALID) || VLAN_BD_LEN(bd) < size + 8)
191 && buf_ptr != dev->use_buf_ptr);
193 if (!(bd & VLAN_BD_VALID) || VLAN_BD_LEN(bd) < size + 8) {
194 /* Failed to find a suitable buffer */
195 return 0;
198 /* Remove the buffer from the pool */
199 dev->use_buf_ptr = buf_ptr;
200 vio_stq(&dev->sdev, dev->buf_list + dev->use_buf_ptr, 0);
202 DPRINTF("Found buffer: ptr=%d rxbufs=%d\n", dev->use_buf_ptr, dev->rx_bufs);
204 return bd;
207 static ssize_t spapr_vlan_receive(NetClientState *nc, const uint8_t *buf,
208 size_t size)
210 VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc);
211 VIOsPAPRDevice *sdev = VIO_SPAPR_DEVICE(dev);
212 vlan_bd_t rxq_bd = vio_ldq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF);
213 vlan_bd_t bd;
214 uint64_t handle;
215 uint8_t control;
217 DPRINTF("spapr_vlan_receive() [%s] rx_bufs=%d\n", sdev->qdev.id,
218 dev->rx_bufs);
220 if (!dev->isopen) {
221 return -1;
224 if (!dev->rx_bufs) {
225 spapr_vlan_record_dropped_rx_frame(dev);
226 return 0;
229 if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
230 bd = spapr_vlan_get_rx_bd_from_pool(dev, size);
231 } else {
232 bd = spapr_vlan_get_rx_bd_from_page(dev, size);
234 if (!bd) {
235 spapr_vlan_record_dropped_rx_frame(dev);
236 return 0;
239 dev->rx_bufs--;
241 /* Transfer the packet data */
242 if (spapr_vio_dma_write(sdev, VLAN_BD_ADDR(bd) + 8, buf, size) < 0) {
243 return -1;
246 DPRINTF("spapr_vlan_receive: DMA write completed\n");
248 /* Update the receive queue */
249 control = VLAN_RXQC_TOGGLE | VLAN_RXQC_VALID;
250 if (rxq_bd & VLAN_BD_TOGGLE) {
251 control ^= VLAN_RXQC_TOGGLE;
254 handle = vio_ldq(sdev, VLAN_BD_ADDR(bd));
255 vio_stq(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 8, handle);
256 vio_stl(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 4, size);
257 vio_sth(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 2, 8);
258 vio_stb(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr, control);
260 DPRINTF("wrote rxq entry (ptr=0x%llx): 0x%016llx 0x%016llx\n",
261 (unsigned long long)dev->rxq_ptr,
262 (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) +
263 dev->rxq_ptr),
264 (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) +
265 dev->rxq_ptr + 8));
267 dev->rxq_ptr += 16;
268 if (dev->rxq_ptr >= VLAN_BD_LEN(rxq_bd)) {
269 dev->rxq_ptr = 0;
270 vio_stq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF, rxq_bd ^ VLAN_BD_TOGGLE);
273 if (sdev->signal_state & 1) {
274 qemu_irq_pulse(spapr_vio_qirq(sdev));
277 return size;
280 static NetClientInfo net_spapr_vlan_info = {
281 .type = NET_CLIENT_OPTIONS_KIND_NIC,
282 .size = sizeof(NICState),
283 .can_receive = spapr_vlan_can_receive,
284 .receive = spapr_vlan_receive,
287 static void spapr_vlan_flush_rx_queue(void *opaque)
289 VIOsPAPRVLANDevice *dev = opaque;
291 qemu_flush_queued_packets(qemu_get_queue(dev->nic));
294 static void spapr_vlan_reset_rx_pool(RxBufPool *rxp)
297 * Use INT_MAX as bufsize so that unused buffers are moved to the end
298 * of the list during the qsort in spapr_vlan_add_rxbuf_to_pool() later.
300 rxp->bufsize = INT_MAX;
301 rxp->count = 0;
302 memset(rxp->bds, 0, sizeof(rxp->bds));
305 static void spapr_vlan_reset(VIOsPAPRDevice *sdev)
307 VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
308 int i;
310 dev->buf_list = 0;
311 dev->rx_bufs = 0;
312 dev->isopen = 0;
314 if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
315 for (i = 0; i < RX_MAX_POOLS; i++) {
316 spapr_vlan_reset_rx_pool(dev->rx_pool[i]);
321 static void spapr_vlan_realize(VIOsPAPRDevice *sdev, Error **errp)
323 VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
325 qemu_macaddr_default_if_unset(&dev->nicconf.macaddr);
327 dev->nic = qemu_new_nic(&net_spapr_vlan_info, &dev->nicconf,
328 object_get_typename(OBJECT(sdev)), sdev->qdev.id, dev);
329 qemu_format_nic_info_str(qemu_get_queue(dev->nic), dev->nicconf.macaddr.a);
331 dev->rxp_timer = timer_new_us(QEMU_CLOCK_VIRTUAL, spapr_vlan_flush_rx_queue,
332 dev);
335 static void spapr_vlan_instance_init(Object *obj)
337 VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(obj);
338 int i;
340 device_add_bootindex_property(obj, &dev->nicconf.bootindex,
341 "bootindex", "",
342 DEVICE(dev), NULL);
344 if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
345 for (i = 0; i < RX_MAX_POOLS; i++) {
346 dev->rx_pool[i] = g_new(RxBufPool, 1);
347 spapr_vlan_reset_rx_pool(dev->rx_pool[i]);
352 static void spapr_vlan_instance_finalize(Object *obj)
354 VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(obj);
355 int i;
357 if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
358 for (i = 0; i < RX_MAX_POOLS; i++) {
359 g_free(dev->rx_pool[i]);
360 dev->rx_pool[i] = NULL;
364 if (dev->rxp_timer) {
365 timer_del(dev->rxp_timer);
366 timer_free(dev->rxp_timer);
370 void spapr_vlan_create(VIOsPAPRBus *bus, NICInfo *nd)
372 DeviceState *dev;
374 dev = qdev_create(&bus->bus, "spapr-vlan");
376 qdev_set_nic_properties(dev, nd);
378 qdev_init_nofail(dev);
381 static int spapr_vlan_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
383 VIOsPAPRVLANDevice *vdev = VIO_SPAPR_VLAN_DEVICE(dev);
384 uint8_t padded_mac[8] = {0, 0};
385 int ret;
387 /* Some old phyp versions give the mac address in an 8-byte
388 * property. The kernel driver has an insane workaround for this;
389 * rather than doing the obvious thing and checking the property
390 * length, it checks whether the first byte has 0b10 in the low
391 * bits. If a correct 6-byte property has a different first byte
392 * the kernel will get the wrong mac address, overrunning its
393 * buffer in the process (read only, thank goodness).
395 * Here we workaround the kernel workaround by always supplying an
396 * 8-byte property, with the mac address in the last six bytes */
397 memcpy(&padded_mac[2], &vdev->nicconf.macaddr, ETH_ALEN);
398 ret = fdt_setprop(fdt, node_off, "local-mac-address",
399 padded_mac, sizeof(padded_mac));
400 if (ret < 0) {
401 return ret;
404 ret = fdt_setprop_cell(fdt, node_off, "ibm,mac-address-filters", 0);
405 if (ret < 0) {
406 return ret;
409 return 0;
412 static int check_bd(VIOsPAPRVLANDevice *dev, vlan_bd_t bd,
413 target_ulong alignment)
415 if ((VLAN_BD_ADDR(bd) % alignment)
416 || (VLAN_BD_LEN(bd) % alignment)) {
417 return -1;
420 if (!spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd),
421 VLAN_BD_LEN(bd), DMA_DIRECTION_FROM_DEVICE)
422 || !spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd),
423 VLAN_BD_LEN(bd), DMA_DIRECTION_TO_DEVICE)) {
424 return -1;
427 return 0;
430 static target_ulong h_register_logical_lan(PowerPCCPU *cpu,
431 sPAPRMachineState *spapr,
432 target_ulong opcode,
433 target_ulong *args)
435 target_ulong reg = args[0];
436 target_ulong buf_list = args[1];
437 target_ulong rec_queue = args[2];
438 target_ulong filter_list = args[3];
439 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
440 VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
441 vlan_bd_t filter_list_bd;
443 if (!dev) {
444 return H_PARAMETER;
447 if (dev->isopen) {
448 hcall_dprintf("H_REGISTER_LOGICAL_LAN called twice without "
449 "H_FREE_LOGICAL_LAN\n");
450 return H_RESOURCE;
453 if (check_bd(dev, VLAN_VALID_BD(buf_list, SPAPR_TCE_PAGE_SIZE),
454 SPAPR_TCE_PAGE_SIZE) < 0) {
455 hcall_dprintf("Bad buf_list 0x" TARGET_FMT_lx "\n", buf_list);
456 return H_PARAMETER;
459 filter_list_bd = VLAN_VALID_BD(filter_list, SPAPR_TCE_PAGE_SIZE);
460 if (check_bd(dev, filter_list_bd, SPAPR_TCE_PAGE_SIZE) < 0) {
461 hcall_dprintf("Bad filter_list 0x" TARGET_FMT_lx "\n", filter_list);
462 return H_PARAMETER;
465 if (!(rec_queue & VLAN_BD_VALID)
466 || (check_bd(dev, rec_queue, VLAN_RQ_ALIGNMENT) < 0)) {
467 hcall_dprintf("Bad receive queue\n");
468 return H_PARAMETER;
471 dev->buf_list = buf_list;
472 sdev->signal_state = 0;
474 rec_queue &= ~VLAN_BD_TOGGLE;
476 /* Initialize the buffer list */
477 vio_stq(sdev, buf_list, rec_queue);
478 vio_stq(sdev, buf_list + 8, filter_list_bd);
479 spapr_vio_dma_set(sdev, buf_list + VLAN_RX_BDS_OFF, 0,
480 SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF);
481 dev->add_buf_ptr = VLAN_RX_BDS_OFF - 8;
482 dev->use_buf_ptr = VLAN_RX_BDS_OFF - 8;
483 dev->rx_bufs = 0;
484 dev->rxq_ptr = 0;
486 /* Initialize the receive queue */
487 spapr_vio_dma_set(sdev, VLAN_BD_ADDR(rec_queue), 0, VLAN_BD_LEN(rec_queue));
489 dev->isopen = 1;
490 qemu_flush_queued_packets(qemu_get_queue(dev->nic));
492 return H_SUCCESS;
496 static target_ulong h_free_logical_lan(PowerPCCPU *cpu,
497 sPAPRMachineState *spapr,
498 target_ulong opcode, target_ulong *args)
500 target_ulong reg = args[0];
501 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
502 VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
504 if (!dev) {
505 return H_PARAMETER;
508 if (!dev->isopen) {
509 hcall_dprintf("H_FREE_LOGICAL_LAN called without "
510 "H_REGISTER_LOGICAL_LAN\n");
511 return H_RESOURCE;
514 spapr_vlan_reset(sdev);
515 return H_SUCCESS;
519 * Used for qsort, this function compares two RxBufPools by size.
521 static int rx_pool_size_compare(const void *p1, const void *p2)
523 const RxBufPool *pool1 = *(RxBufPool **)p1;
524 const RxBufPool *pool2 = *(RxBufPool **)p2;
526 if (pool1->bufsize < pool2->bufsize) {
527 return -1;
529 return pool1->bufsize > pool2->bufsize;
533 * Search for a matching buffer pool with exact matching size,
534 * or return -1 if no matching pool has been found.
536 static int spapr_vlan_get_rx_pool_id(VIOsPAPRVLANDevice *dev, int size)
538 int pool;
540 for (pool = 0; pool < RX_MAX_POOLS; pool++) {
541 if (dev->rx_pool[pool]->bufsize == size) {
542 return pool;
546 return -1;
550 * Enqueuing receive buffer by adding it to one of our receive buffer pools
552 static target_long spapr_vlan_add_rxbuf_to_pool(VIOsPAPRVLANDevice *dev,
553 target_ulong buf)
555 int size = VLAN_BD_LEN(buf);
556 int pool;
558 pool = spapr_vlan_get_rx_pool_id(dev, size);
559 if (pool < 0) {
561 * No matching pool found? Try to use a new one. If the guest used all
562 * pools before, but changed the size of one pool inbetween, we might
563 * need to recycle that pool here (if it's empty already). Thus scan
564 * all buffer pools now, starting with the last (likely empty) one.
566 for (pool = RX_MAX_POOLS - 1; pool >= 0 ; pool--) {
567 if (dev->rx_pool[pool]->count == 0) {
568 dev->rx_pool[pool]->bufsize = size;
570 * Sort pools by size so that spapr_vlan_receive()
571 * can later find the smallest buffer pool easily.
573 qsort(dev->rx_pool, RX_MAX_POOLS, sizeof(dev->rx_pool[0]),
574 rx_pool_size_compare);
575 pool = spapr_vlan_get_rx_pool_id(dev, size);
576 DPRINTF("created RX pool %d for size %lld\n", pool,
577 VLAN_BD_LEN(buf));
578 break;
582 /* Still no usable pool? Give up */
583 if (pool < 0 || dev->rx_pool[pool]->count >= RX_POOL_MAX_BDS) {
584 return H_RESOURCE;
587 DPRINTF("h_add_llan_buf(): Add buf using pool %i (size %lli, count=%i)\n",
588 pool, VLAN_BD_LEN(buf), dev->rx_pool[pool]->count);
590 dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count++] = buf;
592 return 0;
596 * This is the old way of enqueuing receive buffers: Add it to the rx queue
597 * page that has been supplied by the guest (which is quite limited in size).
599 static target_long spapr_vlan_add_rxbuf_to_page(VIOsPAPRVLANDevice *dev,
600 target_ulong buf)
602 vlan_bd_t bd;
604 if (dev->rx_bufs >= VLAN_MAX_BUFS) {
605 return H_RESOURCE;
608 do {
609 dev->add_buf_ptr += 8;
610 if (dev->add_buf_ptr >= VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF) {
611 dev->add_buf_ptr = VLAN_RX_BDS_OFF;
614 bd = vio_ldq(&dev->sdev, dev->buf_list + dev->add_buf_ptr);
615 } while (bd & VLAN_BD_VALID);
617 vio_stq(&dev->sdev, dev->buf_list + dev->add_buf_ptr, buf);
619 DPRINTF("h_add_llan_buf(): Added buf ptr=%d rx_bufs=%d bd=0x%016llx\n",
620 dev->add_buf_ptr, dev->rx_bufs, (unsigned long long)buf);
622 return 0;
625 static target_ulong h_add_logical_lan_buffer(PowerPCCPU *cpu,
626 sPAPRMachineState *spapr,
627 target_ulong opcode,
628 target_ulong *args)
630 target_ulong reg = args[0];
631 target_ulong buf = args[1];
632 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
633 VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
634 target_long ret;
636 DPRINTF("H_ADD_LOGICAL_LAN_BUFFER(0x" TARGET_FMT_lx
637 ", 0x" TARGET_FMT_lx ")\n", reg, buf);
639 if (!sdev) {
640 hcall_dprintf("Bad device\n");
641 return H_PARAMETER;
644 if ((check_bd(dev, buf, 4) < 0)
645 || (VLAN_BD_LEN(buf) < 16)) {
646 hcall_dprintf("Bad buffer enqueued\n");
647 return H_PARAMETER;
650 if (!dev->isopen) {
651 return H_RESOURCE;
654 if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
655 ret = spapr_vlan_add_rxbuf_to_pool(dev, buf);
656 } else {
657 ret = spapr_vlan_add_rxbuf_to_page(dev, buf);
659 if (ret) {
660 return ret;
663 dev->rx_bufs++;
666 * Give guest some more time to add additional RX buffers before we
667 * flush the receive queue, so that e.g. fragmented IP packets can
668 * be passed to the guest in one go later (instead of passing single
669 * fragments if there is only one receive buffer available).
671 timer_mod(dev->rxp_timer, qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + 500);
673 return H_SUCCESS;
676 static target_ulong h_send_logical_lan(PowerPCCPU *cpu,
677 sPAPRMachineState *spapr,
678 target_ulong opcode, target_ulong *args)
680 target_ulong reg = args[0];
681 target_ulong *bufs = args + 1;
682 target_ulong continue_token = args[7];
683 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
684 VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
685 unsigned total_len;
686 uint8_t *lbuf, *p;
687 int i, nbufs;
688 int ret;
690 DPRINTF("H_SEND_LOGICAL_LAN(0x" TARGET_FMT_lx ", <bufs>, 0x"
691 TARGET_FMT_lx ")\n", reg, continue_token);
693 if (!sdev) {
694 return H_PARAMETER;
697 DPRINTF("rxbufs = %d\n", dev->rx_bufs);
699 if (!dev->isopen) {
700 return H_DROPPED;
703 if (continue_token) {
704 return H_HARDWARE; /* FIXME actually handle this */
707 total_len = 0;
708 for (i = 0; i < 6; i++) {
709 DPRINTF(" buf desc: 0x" TARGET_FMT_lx "\n", bufs[i]);
710 if (!(bufs[i] & VLAN_BD_VALID)) {
711 break;
713 total_len += VLAN_BD_LEN(bufs[i]);
716 nbufs = i;
717 DPRINTF("h_send_logical_lan() %d buffers, total length 0x%x\n",
718 nbufs, total_len);
720 if (total_len == 0) {
721 return H_SUCCESS;
724 if (total_len > MAX_PACKET_SIZE) {
725 /* Don't let the guest force too large an allocation */
726 return H_RESOURCE;
729 lbuf = alloca(total_len);
730 p = lbuf;
731 for (i = 0; i < nbufs; i++) {
732 ret = spapr_vio_dma_read(sdev, VLAN_BD_ADDR(bufs[i]),
733 p, VLAN_BD_LEN(bufs[i]));
734 if (ret < 0) {
735 return ret;
738 p += VLAN_BD_LEN(bufs[i]);
741 qemu_send_packet(qemu_get_queue(dev->nic), lbuf, total_len);
743 return H_SUCCESS;
746 static target_ulong h_multicast_ctrl(PowerPCCPU *cpu, sPAPRMachineState *spapr,
747 target_ulong opcode, target_ulong *args)
749 target_ulong reg = args[0];
750 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
752 if (!dev) {
753 return H_PARAMETER;
756 return H_SUCCESS;
759 static Property spapr_vlan_properties[] = {
760 DEFINE_SPAPR_PROPERTIES(VIOsPAPRVLANDevice, sdev),
761 DEFINE_NIC_PROPERTIES(VIOsPAPRVLANDevice, nicconf),
762 DEFINE_PROP_BIT("use-rx-buffer-pools", VIOsPAPRVLANDevice,
763 compat_flags, SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT, true),
764 DEFINE_PROP_END_OF_LIST(),
767 static bool spapr_vlan_rx_buffer_pools_needed(void *opaque)
769 VIOsPAPRVLANDevice *dev = opaque;
771 return (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) != 0;
774 static const VMStateDescription vmstate_rx_buffer_pool = {
775 .name = "spapr_llan/rx_buffer_pool",
776 .version_id = 1,
777 .minimum_version_id = 1,
778 .needed = spapr_vlan_rx_buffer_pools_needed,
779 .fields = (VMStateField[]) {
780 VMSTATE_INT32(bufsize, RxBufPool),
781 VMSTATE_INT32(count, RxBufPool),
782 VMSTATE_UINT64_ARRAY(bds, RxBufPool, RX_POOL_MAX_BDS),
783 VMSTATE_END_OF_LIST()
787 static const VMStateDescription vmstate_rx_pools = {
788 .name = "spapr_llan/rx_pools",
789 .version_id = 1,
790 .minimum_version_id = 1,
791 .needed = spapr_vlan_rx_buffer_pools_needed,
792 .fields = (VMStateField[]) {
793 VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(rx_pool, VIOsPAPRVLANDevice,
794 RX_MAX_POOLS, 1,
795 vmstate_rx_buffer_pool, RxBufPool),
796 VMSTATE_END_OF_LIST()
800 static const VMStateDescription vmstate_spapr_llan = {
801 .name = "spapr_llan",
802 .version_id = 1,
803 .minimum_version_id = 1,
804 .fields = (VMStateField[]) {
805 VMSTATE_SPAPR_VIO(sdev, VIOsPAPRVLANDevice),
806 /* LLAN state */
807 VMSTATE_BOOL(isopen, VIOsPAPRVLANDevice),
808 VMSTATE_UINT64(buf_list, VIOsPAPRVLANDevice),
809 VMSTATE_UINT32(add_buf_ptr, VIOsPAPRVLANDevice),
810 VMSTATE_UINT32(use_buf_ptr, VIOsPAPRVLANDevice),
811 VMSTATE_UINT32(rx_bufs, VIOsPAPRVLANDevice),
812 VMSTATE_UINT64(rxq_ptr, VIOsPAPRVLANDevice),
814 VMSTATE_END_OF_LIST()
816 .subsections = (const VMStateDescription * []) {
817 &vmstate_rx_pools,
818 NULL
822 static void spapr_vlan_class_init(ObjectClass *klass, void *data)
824 DeviceClass *dc = DEVICE_CLASS(klass);
825 VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
827 k->realize = spapr_vlan_realize;
828 k->reset = spapr_vlan_reset;
829 k->devnode = spapr_vlan_devnode;
830 k->dt_name = "l-lan";
831 k->dt_type = "network";
832 k->dt_compatible = "IBM,l-lan";
833 k->signal_mask = 0x1;
834 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
835 dc->props = spapr_vlan_properties;
836 k->rtce_window_size = 0x10000000;
837 dc->vmsd = &vmstate_spapr_llan;
840 static const TypeInfo spapr_vlan_info = {
841 .name = TYPE_VIO_SPAPR_VLAN_DEVICE,
842 .parent = TYPE_VIO_SPAPR_DEVICE,
843 .instance_size = sizeof(VIOsPAPRVLANDevice),
844 .class_init = spapr_vlan_class_init,
845 .instance_init = spapr_vlan_instance_init,
846 .instance_finalize = spapr_vlan_instance_finalize,
849 static void spapr_vlan_register_types(void)
851 spapr_register_hypercall(H_REGISTER_LOGICAL_LAN, h_register_logical_lan);
852 spapr_register_hypercall(H_FREE_LOGICAL_LAN, h_free_logical_lan);
853 spapr_register_hypercall(H_SEND_LOGICAL_LAN, h_send_logical_lan);
854 spapr_register_hypercall(H_ADD_LOGICAL_LAN_BUFFER,
855 h_add_logical_lan_buffer);
856 spapr_register_hypercall(H_MULTICAST_CTRL, h_multicast_ctrl);
857 type_register_static(&spapr_vlan_info);
860 type_init(spapr_vlan_register_types)