xref: /openbmc/qemu/hw/net/spapr_llan.c (revision 0ec7b3e7)
1 /*
2  * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
3  *
4  * PAPR Inter-VM Logical Lan, aka ibmveth
5  *
6  * Copyright (c) 2010,2011 David Gibson, IBM Corporation.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  *
26  */
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"
37 #include "trace.h"
38 
39 #include <libfdt.h>
40 
41 #define ETH_ALEN        6
42 #define MAX_PACKET_SIZE 65536
43 
44 /* Compatibility flags for migration */
45 #define SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT  0
46 #define SPAPRVLAN_FLAG_RX_BUF_POOLS      (1 << SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT)
47 
48 /*
49  * Virtual LAN device
50  */
51 
52 typedef uint64_t vlan_bd_t;
53 
54 #define VLAN_BD_VALID        0x8000000000000000ULL
55 #define VLAN_BD_TOGGLE       0x4000000000000000ULL
56 #define VLAN_BD_NO_CSUM      0x0200000000000000ULL
57 #define VLAN_BD_CSUM_GOOD    0x0100000000000000ULL
58 #define VLAN_BD_LEN_MASK     0x00ffffff00000000ULL
59 #define VLAN_BD_LEN(bd)      (((bd) & VLAN_BD_LEN_MASK) >> 32)
60 #define VLAN_BD_ADDR_MASK    0x00000000ffffffffULL
61 #define VLAN_BD_ADDR(bd)     ((bd) & VLAN_BD_ADDR_MASK)
62 
63 #define VLAN_VALID_BD(addr, len) (VLAN_BD_VALID | \
64                                   (((len) << 32) & VLAN_BD_LEN_MASK) |  \
65                                   (addr & VLAN_BD_ADDR_MASK))
66 
67 #define VLAN_RXQC_TOGGLE     0x80
68 #define VLAN_RXQC_VALID      0x40
69 #define VLAN_RXQC_NO_CSUM    0x02
70 #define VLAN_RXQC_CSUM_GOOD  0x01
71 
72 #define VLAN_RQ_ALIGNMENT    16
73 #define VLAN_RXQ_BD_OFF      0
74 #define VLAN_FILTER_BD_OFF   8
75 #define VLAN_RX_BDS_OFF      16
76 /*
77  * The final 8 bytes of the buffer list is a counter of frames dropped
78  * because there was not a buffer in the buffer list capable of holding
79  * the frame. We must avoid it, or the operating system will report garbage
80  * for this statistic.
81  */
82 #define VLAN_RX_BDS_LEN      (SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF - 8)
83 #define VLAN_MAX_BUFS        (VLAN_RX_BDS_LEN / 8)
84 
85 #define TYPE_VIO_SPAPR_VLAN_DEVICE "spapr-vlan"
86 #define VIO_SPAPR_VLAN_DEVICE(obj) \
87      OBJECT_CHECK(VIOsPAPRVLANDevice, (obj), TYPE_VIO_SPAPR_VLAN_DEVICE)
88 
89 #define RX_POOL_MAX_BDS 4096
90 #define RX_MAX_POOLS 5
91 
92 typedef struct {
93     int32_t bufsize;
94     int32_t count;
95     vlan_bd_t bds[RX_POOL_MAX_BDS];
96 } RxBufPool;
97 
98 typedef struct VIOsPAPRVLANDevice {
99     VIOsPAPRDevice sdev;
100     NICConf nicconf;
101     NICState *nic;
102     MACAddr perm_mac;
103     bool isopen;
104     hwaddr buf_list;
105     uint32_t add_buf_ptr, use_buf_ptr, rx_bufs;
106     hwaddr rxq_ptr;
107     QEMUTimer *rxp_timer;
108     uint32_t compat_flags;             /* Compatibility flags for migration */
109     RxBufPool *rx_pool[RX_MAX_POOLS];  /* Receive buffer descriptor pools */
110 } VIOsPAPRVLANDevice;
111 
112 static int spapr_vlan_can_receive(NetClientState *nc)
113 {
114     VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc);
115 
116     return (dev->isopen && dev->rx_bufs > 0);
117 }
118 
119 /**
120  * The last 8 bytes of the receive buffer list page (that has been
121  * supplied by the guest with the H_REGISTER_LOGICAL_LAN call) contain
122  * a counter for frames that have been dropped because there was no
123  * suitable receive buffer available. This function is used to increase
124  * this counter by one.
125  */
126 static void spapr_vlan_record_dropped_rx_frame(VIOsPAPRVLANDevice *dev)
127 {
128     uint64_t cnt;
129 
130     cnt = vio_ldq(&dev->sdev, dev->buf_list + 4096 - 8);
131     vio_stq(&dev->sdev, dev->buf_list + 4096 - 8, cnt + 1);
132 }
133 
134 /**
135  * Get buffer descriptor from one of our receive buffer pools
136  */
137 static vlan_bd_t spapr_vlan_get_rx_bd_from_pool(VIOsPAPRVLANDevice *dev,
138                                                 size_t size)
139 {
140     vlan_bd_t bd;
141     int pool;
142 
143     for (pool = 0; pool < RX_MAX_POOLS; pool++) {
144         if (dev->rx_pool[pool]->count > 0 &&
145             dev->rx_pool[pool]->bufsize >= size + 8) {
146             break;
147         }
148     }
149     if (pool == RX_MAX_POOLS) {
150         /* Failed to find a suitable buffer */
151         return 0;
152     }
153 
154 
155     trace_spapr_vlan_get_rx_bd_from_pool_found(pool,
156                                                dev->rx_pool[pool]->count,
157                                                dev->rx_bufs);
158 
159     /* Remove the buffer from the pool */
160     dev->rx_pool[pool]->count--;
161     bd = dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count];
162     dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count] = 0;
163 
164     return bd;
165 }
166 
167 /**
168  * Get buffer descriptor from the receive buffer list page that has been
169  * supplied by the guest with the H_REGISTER_LOGICAL_LAN call
170  */
171 static vlan_bd_t spapr_vlan_get_rx_bd_from_page(VIOsPAPRVLANDevice *dev,
172                                                 size_t size)
173 {
174     int buf_ptr = dev->use_buf_ptr;
175     vlan_bd_t bd;
176 
177     do {
178         buf_ptr += 8;
179         if (buf_ptr >= VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF) {
180             buf_ptr = VLAN_RX_BDS_OFF;
181         }
182 
183         bd = vio_ldq(&dev->sdev, dev->buf_list + buf_ptr);
184 
185         trace_spapr_vlan_get_rx_bd_from_page(buf_ptr, (uint64_t)bd);
186     } while ((!(bd & VLAN_BD_VALID) || VLAN_BD_LEN(bd) < size + 8)
187              && buf_ptr != dev->use_buf_ptr);
188 
189     if (!(bd & VLAN_BD_VALID) || VLAN_BD_LEN(bd) < size + 8) {
190         /* Failed to find a suitable buffer */
191         return 0;
192     }
193 
194     /* Remove the buffer from the pool */
195     dev->use_buf_ptr = buf_ptr;
196     vio_stq(&dev->sdev, dev->buf_list + dev->use_buf_ptr, 0);
197 
198     trace_spapr_vlan_get_rx_bd_from_page_found(dev->use_buf_ptr, dev->rx_bufs);
199 
200     return bd;
201 }
202 
203 static ssize_t spapr_vlan_receive(NetClientState *nc, const uint8_t *buf,
204                                   size_t size)
205 {
206     VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc);
207     VIOsPAPRDevice *sdev = VIO_SPAPR_DEVICE(dev);
208     vlan_bd_t rxq_bd = vio_ldq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF);
209     vlan_bd_t bd;
210     uint64_t handle;
211     uint8_t control;
212 
213     trace_spapr_vlan_receive(sdev->qdev.id, dev->rx_bufs);
214 
215     if (!dev->isopen) {
216         return -1;
217     }
218 
219     if (!dev->rx_bufs) {
220         spapr_vlan_record_dropped_rx_frame(dev);
221         return 0;
222     }
223 
224     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
225         bd = spapr_vlan_get_rx_bd_from_pool(dev, size);
226     } else {
227         bd = spapr_vlan_get_rx_bd_from_page(dev, size);
228     }
229     if (!bd) {
230         spapr_vlan_record_dropped_rx_frame(dev);
231         return 0;
232     }
233 
234     dev->rx_bufs--;
235 
236     /* Transfer the packet data */
237     if (spapr_vio_dma_write(sdev, VLAN_BD_ADDR(bd) + 8, buf, size) < 0) {
238         return -1;
239     }
240 
241     trace_spapr_vlan_receive_dma_completed();
242 
243     /* Update the receive queue */
244     control = VLAN_RXQC_TOGGLE | VLAN_RXQC_VALID;
245     if (rxq_bd & VLAN_BD_TOGGLE) {
246         control ^= VLAN_RXQC_TOGGLE;
247     }
248 
249     handle = vio_ldq(sdev, VLAN_BD_ADDR(bd));
250     vio_stq(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 8, handle);
251     vio_stl(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 4, size);
252     vio_sth(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 2, 8);
253     vio_stb(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr, control);
254 
255     trace_spapr_vlan_receive_wrote(dev->rxq_ptr,
256                                    vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) +
257                                                  dev->rxq_ptr),
258                                    vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) +
259                                                  dev->rxq_ptr + 8));
260 
261     dev->rxq_ptr += 16;
262     if (dev->rxq_ptr >= VLAN_BD_LEN(rxq_bd)) {
263         dev->rxq_ptr = 0;
264         vio_stq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF, rxq_bd ^ VLAN_BD_TOGGLE);
265     }
266 
267     if (sdev->signal_state & 1) {
268         qemu_irq_pulse(spapr_vio_qirq(sdev));
269     }
270 
271     return size;
272 }
273 
274 static NetClientInfo net_spapr_vlan_info = {
275     .type = NET_CLIENT_DRIVER_NIC,
276     .size = sizeof(NICState),
277     .can_receive = spapr_vlan_can_receive,
278     .receive = spapr_vlan_receive,
279 };
280 
281 static void spapr_vlan_flush_rx_queue(void *opaque)
282 {
283     VIOsPAPRVLANDevice *dev = opaque;
284 
285     qemu_flush_queued_packets(qemu_get_queue(dev->nic));
286 }
287 
288 static void spapr_vlan_reset_rx_pool(RxBufPool *rxp)
289 {
290     /*
291      * Use INT_MAX as bufsize so that unused buffers are moved to the end
292      * of the list during the qsort in spapr_vlan_add_rxbuf_to_pool() later.
293      */
294     rxp->bufsize = INT_MAX;
295     rxp->count = 0;
296     memset(rxp->bds, 0, sizeof(rxp->bds));
297 }
298 
299 static void spapr_vlan_reset(VIOsPAPRDevice *sdev)
300 {
301     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
302     int i;
303 
304     dev->buf_list = 0;
305     dev->rx_bufs = 0;
306     dev->isopen = 0;
307 
308     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
309         for (i = 0; i < RX_MAX_POOLS; i++) {
310             spapr_vlan_reset_rx_pool(dev->rx_pool[i]);
311         }
312     }
313 
314     memcpy(&dev->nicconf.macaddr.a, &dev->perm_mac.a,
315            sizeof(dev->nicconf.macaddr.a));
316     qemu_format_nic_info_str(qemu_get_queue(dev->nic), dev->nicconf.macaddr.a);
317 }
318 
319 static void spapr_vlan_realize(VIOsPAPRDevice *sdev, Error **errp)
320 {
321     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
322 
323     qemu_macaddr_default_if_unset(&dev->nicconf.macaddr);
324 
325     memcpy(&dev->perm_mac.a, &dev->nicconf.macaddr.a, sizeof(dev->perm_mac.a));
326 
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);
330 
331     dev->rxp_timer = timer_new_us(QEMU_CLOCK_VIRTUAL, spapr_vlan_flush_rx_queue,
332                                   dev);
333 }
334 
335 static void spapr_vlan_instance_init(Object *obj)
336 {
337     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(obj);
338     int i;
339 
340     device_add_bootindex_property(obj, &dev->nicconf.bootindex,
341                                   "bootindex", "",
342                                   DEVICE(dev), NULL);
343 
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]);
348         }
349     }
350 }
351 
352 static void spapr_vlan_instance_finalize(Object *obj)
353 {
354     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(obj);
355     int i;
356 
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;
361         }
362     }
363 
364     if (dev->rxp_timer) {
365         timer_del(dev->rxp_timer);
366         timer_free(dev->rxp_timer);
367     }
368 }
369 
370 void spapr_vlan_create(VIOsPAPRBus *bus, NICInfo *nd)
371 {
372     DeviceState *dev;
373 
374     dev = qdev_create(&bus->bus, "spapr-vlan");
375 
376     qdev_set_nic_properties(dev, nd);
377 
378     qdev_init_nofail(dev);
379 }
380 
381 static int spapr_vlan_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
382 {
383     VIOsPAPRVLANDevice *vdev = VIO_SPAPR_VLAN_DEVICE(dev);
384     uint8_t padded_mac[8] = {0, 0};
385     int ret;
386 
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).
394      *
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;
402     }
403 
404     ret = fdt_setprop_cell(fdt, node_off, "ibm,mac-address-filters", 0);
405     if (ret < 0) {
406         return ret;
407     }
408 
409     return 0;
410 }
411 
412 static int check_bd(VIOsPAPRVLANDevice *dev, vlan_bd_t bd,
413                     target_ulong alignment)
414 {
415     if ((VLAN_BD_ADDR(bd) % alignment)
416         || (VLAN_BD_LEN(bd) % alignment)) {
417         return -1;
418     }
419 
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;
425     }
426 
427     return 0;
428 }
429 
430 static target_ulong h_register_logical_lan(PowerPCCPU *cpu,
431                                            sPAPRMachineState *spapr,
432                                            target_ulong opcode,
433                                            target_ulong *args)
434 {
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;
442 
443     if (!dev) {
444         return H_PARAMETER;
445     }
446 
447     if (dev->isopen) {
448         hcall_dprintf("H_REGISTER_LOGICAL_LAN called twice without "
449                       "H_FREE_LOGICAL_LAN\n");
450         return H_RESOURCE;
451     }
452 
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;
457     }
458 
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;
463     }
464 
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;
469     }
470 
471     dev->buf_list = buf_list;
472     sdev->signal_state = 0;
473 
474     rec_queue &= ~VLAN_BD_TOGGLE;
475 
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;
485 
486     /* Initialize the receive queue */
487     spapr_vio_dma_set(sdev, VLAN_BD_ADDR(rec_queue), 0, VLAN_BD_LEN(rec_queue));
488 
489     dev->isopen = 1;
490     qemu_flush_queued_packets(qemu_get_queue(dev->nic));
491 
492     return H_SUCCESS;
493 }
494 
495 
496 static target_ulong h_free_logical_lan(PowerPCCPU *cpu,
497                                        sPAPRMachineState *spapr,
498                                        target_ulong opcode, target_ulong *args)
499 {
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);
503 
504     if (!dev) {
505         return H_PARAMETER;
506     }
507 
508     if (!dev->isopen) {
509         hcall_dprintf("H_FREE_LOGICAL_LAN called without "
510                       "H_REGISTER_LOGICAL_LAN\n");
511         return H_RESOURCE;
512     }
513 
514     spapr_vlan_reset(sdev);
515     return H_SUCCESS;
516 }
517 
518 /**
519  * Used for qsort, this function compares two RxBufPools by size.
520  */
521 static int rx_pool_size_compare(const void *p1, const void *p2)
522 {
523     const RxBufPool *pool1 = *(RxBufPool **)p1;
524     const RxBufPool *pool2 = *(RxBufPool **)p2;
525 
526     if (pool1->bufsize < pool2->bufsize) {
527         return -1;
528     }
529     return pool1->bufsize > pool2->bufsize;
530 }
531 
532 /**
533  * Search for a matching buffer pool with exact matching size,
534  * or return -1 if no matching pool has been found.
535  */
536 static int spapr_vlan_get_rx_pool_id(VIOsPAPRVLANDevice *dev, int size)
537 {
538     int pool;
539 
540     for (pool = 0; pool < RX_MAX_POOLS; pool++) {
541         if (dev->rx_pool[pool]->bufsize == size) {
542             return pool;
543         }
544     }
545 
546     return -1;
547 }
548 
549 /**
550  * Enqueuing receive buffer by adding it to one of our receive buffer pools
551  */
552 static target_long spapr_vlan_add_rxbuf_to_pool(VIOsPAPRVLANDevice *dev,
553                                                 target_ulong buf)
554 {
555     int size = VLAN_BD_LEN(buf);
556     int pool;
557 
558     pool = spapr_vlan_get_rx_pool_id(dev, size);
559     if (pool < 0) {
560         /*
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 in the meantime, 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.
565          */
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;
569                 /*
570                  * Sort pools by size so that spapr_vlan_receive()
571                  * can later find the smallest buffer pool easily.
572                  */
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                 trace_spapr_vlan_add_rxbuf_to_pool_create(pool,
577                                                           VLAN_BD_LEN(buf));
578                 break;
579             }
580         }
581     }
582     /* Still no usable pool? Give up */
583     if (pool < 0 || dev->rx_pool[pool]->count >= RX_POOL_MAX_BDS) {
584         return H_RESOURCE;
585     }
586 
587     trace_spapr_vlan_add_rxbuf_to_pool(pool, VLAN_BD_LEN(buf),
588                                        dev->rx_pool[pool]->count);
589 
590     dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count++] = buf;
591 
592     return 0;
593 }
594 
595 /**
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).
598  */
599 static target_long spapr_vlan_add_rxbuf_to_page(VIOsPAPRVLANDevice *dev,
600                                                 target_ulong buf)
601 {
602     vlan_bd_t bd;
603 
604     if (dev->rx_bufs >= VLAN_MAX_BUFS) {
605         return H_RESOURCE;
606     }
607 
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;
612         }
613 
614         bd = vio_ldq(&dev->sdev, dev->buf_list + dev->add_buf_ptr);
615     } while (bd & VLAN_BD_VALID);
616 
617     vio_stq(&dev->sdev, dev->buf_list + dev->add_buf_ptr, buf);
618 
619     trace_spapr_vlan_add_rxbuf_to_page(dev->add_buf_ptr, dev->rx_bufs, buf);
620 
621     return 0;
622 }
623 
624 static target_ulong h_add_logical_lan_buffer(PowerPCCPU *cpu,
625                                              sPAPRMachineState *spapr,
626                                              target_ulong opcode,
627                                              target_ulong *args)
628 {
629     target_ulong reg = args[0];
630     target_ulong buf = args[1];
631     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
632     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
633     target_long ret;
634 
635     trace_spapr_vlan_h_add_logical_lan_buffer(reg, buf);
636 
637     if (!sdev) {
638         hcall_dprintf("Bad device\n");
639         return H_PARAMETER;
640     }
641 
642     if ((check_bd(dev, buf, 4) < 0)
643         || (VLAN_BD_LEN(buf) < 16)) {
644         hcall_dprintf("Bad buffer enqueued\n");
645         return H_PARAMETER;
646     }
647 
648     if (!dev->isopen) {
649         return H_RESOURCE;
650     }
651 
652     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
653         ret = spapr_vlan_add_rxbuf_to_pool(dev, buf);
654     } else {
655         ret = spapr_vlan_add_rxbuf_to_page(dev, buf);
656     }
657     if (ret) {
658         return ret;
659     }
660 
661     dev->rx_bufs++;
662 
663     /*
664      * Give guest some more time to add additional RX buffers before we
665      * flush the receive queue, so that e.g. fragmented IP packets can
666      * be passed to the guest in one go later (instead of passing single
667      * fragments if there is only one receive buffer available).
668      */
669     timer_mod(dev->rxp_timer, qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + 500);
670 
671     return H_SUCCESS;
672 }
673 
674 static target_ulong h_send_logical_lan(PowerPCCPU *cpu,
675                                        sPAPRMachineState *spapr,
676                                        target_ulong opcode, target_ulong *args)
677 {
678     target_ulong reg = args[0];
679     target_ulong *bufs = args + 1;
680     target_ulong continue_token = args[7];
681     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
682     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
683     unsigned total_len;
684     uint8_t *lbuf, *p;
685     int i, nbufs;
686     int ret;
687 
688     trace_spapr_vlan_h_send_logical_lan(reg, continue_token);
689 
690     if (!sdev) {
691         return H_PARAMETER;
692     }
693 
694     trace_spapr_vlan_h_send_logical_lan_rxbufs(dev->rx_bufs);
695 
696     if (!dev->isopen) {
697         return H_DROPPED;
698     }
699 
700     if (continue_token) {
701         return H_HARDWARE; /* FIXME actually handle this */
702     }
703 
704     total_len = 0;
705     for (i = 0; i < 6; i++) {
706         trace_spapr_vlan_h_send_logical_lan_buf_desc(bufs[i]);
707         if (!(bufs[i] & VLAN_BD_VALID)) {
708             break;
709         }
710         total_len += VLAN_BD_LEN(bufs[i]);
711     }
712 
713     nbufs = i;
714     trace_spapr_vlan_h_send_logical_lan_total(nbufs, total_len);
715 
716     if (total_len == 0) {
717         return H_SUCCESS;
718     }
719 
720     if (total_len > MAX_PACKET_SIZE) {
721         /* Don't let the guest force too large an allocation */
722         return H_RESOURCE;
723     }
724 
725     lbuf = alloca(total_len);
726     p = lbuf;
727     for (i = 0; i < nbufs; i++) {
728         ret = spapr_vio_dma_read(sdev, VLAN_BD_ADDR(bufs[i]),
729                                  p, VLAN_BD_LEN(bufs[i]));
730         if (ret < 0) {
731             return ret;
732         }
733 
734         p += VLAN_BD_LEN(bufs[i]);
735     }
736 
737     qemu_send_packet(qemu_get_queue(dev->nic), lbuf, total_len);
738 
739     return H_SUCCESS;
740 }
741 
742 static target_ulong h_multicast_ctrl(PowerPCCPU *cpu, sPAPRMachineState *spapr,
743                                      target_ulong opcode, target_ulong *args)
744 {
745     target_ulong reg = args[0];
746     VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
747 
748     if (!dev) {
749         return H_PARAMETER;
750     }
751 
752     return H_SUCCESS;
753 }
754 
755 static target_ulong h_change_logical_lan_mac(PowerPCCPU *cpu,
756                                              sPAPRMachineState *spapr,
757                                              target_ulong opcode,
758                                              target_ulong *args)
759 {
760     target_ulong reg = args[0];
761     target_ulong macaddr = args[1];
762     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
763     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
764     int i;
765 
766     for (i = 0; i < ETH_ALEN; i++) {
767         dev->nicconf.macaddr.a[ETH_ALEN - i - 1] = macaddr & 0xff;
768         macaddr >>= 8;
769     }
770 
771     qemu_format_nic_info_str(qemu_get_queue(dev->nic), dev->nicconf.macaddr.a);
772 
773     return H_SUCCESS;
774 }
775 
776 static Property spapr_vlan_properties[] = {
777     DEFINE_SPAPR_PROPERTIES(VIOsPAPRVLANDevice, sdev),
778     DEFINE_NIC_PROPERTIES(VIOsPAPRVLANDevice, nicconf),
779     DEFINE_PROP_BIT("use-rx-buffer-pools", VIOsPAPRVLANDevice,
780                     compat_flags, SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT, true),
781     DEFINE_PROP_END_OF_LIST(),
782 };
783 
784 static bool spapr_vlan_rx_buffer_pools_needed(void *opaque)
785 {
786     VIOsPAPRVLANDevice *dev = opaque;
787 
788     return (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) != 0;
789 }
790 
791 static const VMStateDescription vmstate_rx_buffer_pool = {
792     .name = "spapr_llan/rx_buffer_pool",
793     .version_id = 1,
794     .minimum_version_id = 1,
795     .needed = spapr_vlan_rx_buffer_pools_needed,
796     .fields = (VMStateField[]) {
797         VMSTATE_INT32(bufsize, RxBufPool),
798         VMSTATE_INT32(count, RxBufPool),
799         VMSTATE_UINT64_ARRAY(bds, RxBufPool, RX_POOL_MAX_BDS),
800         VMSTATE_END_OF_LIST()
801     }
802 };
803 
804 static const VMStateDescription vmstate_rx_pools = {
805     .name = "spapr_llan/rx_pools",
806     .version_id = 1,
807     .minimum_version_id = 1,
808     .needed = spapr_vlan_rx_buffer_pools_needed,
809     .fields = (VMStateField[]) {
810         VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(rx_pool, VIOsPAPRVLANDevice,
811                                            RX_MAX_POOLS, 1,
812                                            vmstate_rx_buffer_pool, RxBufPool),
813         VMSTATE_END_OF_LIST()
814     }
815 };
816 
817 static const VMStateDescription vmstate_spapr_llan = {
818     .name = "spapr_llan",
819     .version_id = 1,
820     .minimum_version_id = 1,
821     .fields = (VMStateField[]) {
822         VMSTATE_SPAPR_VIO(sdev, VIOsPAPRVLANDevice),
823         /* LLAN state */
824         VMSTATE_BOOL(isopen, VIOsPAPRVLANDevice),
825         VMSTATE_UINT64(buf_list, VIOsPAPRVLANDevice),
826         VMSTATE_UINT32(add_buf_ptr, VIOsPAPRVLANDevice),
827         VMSTATE_UINT32(use_buf_ptr, VIOsPAPRVLANDevice),
828         VMSTATE_UINT32(rx_bufs, VIOsPAPRVLANDevice),
829         VMSTATE_UINT64(rxq_ptr, VIOsPAPRVLANDevice),
830 
831         VMSTATE_END_OF_LIST()
832     },
833     .subsections = (const VMStateDescription * []) {
834         &vmstate_rx_pools,
835         NULL
836     }
837 };
838 
839 static void spapr_vlan_class_init(ObjectClass *klass, void *data)
840 {
841     DeviceClass *dc = DEVICE_CLASS(klass);
842     VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
843 
844     k->realize = spapr_vlan_realize;
845     k->reset = spapr_vlan_reset;
846     k->devnode = spapr_vlan_devnode;
847     k->dt_name = "l-lan";
848     k->dt_type = "network";
849     k->dt_compatible = "IBM,l-lan";
850     k->signal_mask = 0x1;
851     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
852     dc->props = spapr_vlan_properties;
853     k->rtce_window_size = 0x10000000;
854     dc->vmsd = &vmstate_spapr_llan;
855 }
856 
857 static const TypeInfo spapr_vlan_info = {
858     .name          = TYPE_VIO_SPAPR_VLAN_DEVICE,
859     .parent        = TYPE_VIO_SPAPR_DEVICE,
860     .instance_size = sizeof(VIOsPAPRVLANDevice),
861     .class_init    = spapr_vlan_class_init,
862     .instance_init = spapr_vlan_instance_init,
863     .instance_finalize = spapr_vlan_instance_finalize,
864 };
865 
866 static void spapr_vlan_register_types(void)
867 {
868     spapr_register_hypercall(H_REGISTER_LOGICAL_LAN, h_register_logical_lan);
869     spapr_register_hypercall(H_FREE_LOGICAL_LAN, h_free_logical_lan);
870     spapr_register_hypercall(H_SEND_LOGICAL_LAN, h_send_logical_lan);
871     spapr_register_hypercall(H_ADD_LOGICAL_LAN_BUFFER,
872                              h_add_logical_lan_buffer);
873     spapr_register_hypercall(H_MULTICAST_CTRL, h_multicast_ctrl);
874     spapr_register_hypercall(H_CHANGE_LOGICAL_LAN_MAC,
875                              h_change_logical_lan_mac);
876     type_register_static(&spapr_vlan_info);
877 }
878 
879 type_init(spapr_vlan_register_types)
880