]> Git Repo - qemu.git/blob - hw/net/spapr_llan.c
hw/net/spapr_llan: Delay flushing of the RX queue while adding new RX buffers
[qemu.git] / hw / net / spapr_llan.c
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
38 #include <libfdt.h>
39
40 #define ETH_ALEN        6
41 #define MAX_PACKET_SIZE 65536
42
43 /*#define DEBUG*/
44
45 #ifdef DEBUG
46 #define DPRINTF(fmt...) do { fprintf(stderr, fmt); } while (0)
47 #else
48 #define DPRINTF(fmt...)
49 #endif
50
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)
54
55 /*
56  * Virtual LAN device
57  */
58
59 typedef uint64_t vlan_bd_t;
60
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)
69
70 #define VLAN_VALID_BD(addr, len) (VLAN_BD_VALID | \
71                                   (((len) << 32) & VLAN_BD_LEN_MASK) |  \
72                                   (addr & VLAN_BD_ADDR_MASK))
73
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
78
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
83 /*
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.
88  */
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)
91
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)
95
96 #define RX_POOL_MAX_BDS 4096
97 #define RX_MAX_POOLS 5
98
99 typedef struct {
100     int32_t bufsize;
101     int32_t count;
102     vlan_bd_t bds[RX_POOL_MAX_BDS];
103 } RxBufPool;
104
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;
117
118 static int spapr_vlan_can_receive(NetClientState *nc)
119 {
120     VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc);
121
122     return (dev->isopen && dev->rx_bufs > 0);
123 }
124
125 /**
126  * Get buffer descriptor from one of our receive buffer pools
127  */
128 static vlan_bd_t spapr_vlan_get_rx_bd_from_pool(VIOsPAPRVLANDevice *dev,
129                                                 size_t size)
130 {
131     vlan_bd_t bd;
132     int pool;
133
134     for (pool = 0; pool < RX_MAX_POOLS; pool++) {
135         if (dev->rx_pool[pool]->count > 0 &&
136             dev->rx_pool[pool]->bufsize >= size + 8) {
137             break;
138         }
139     }
140     if (pool == RX_MAX_POOLS) {
141         /* Failed to find a suitable buffer */
142         return 0;
143     }
144
145     DPRINTF("Found buffer: pool=%d count=%d rxbufs=%d\n", pool,
146             dev->rx_pool[pool]->count, dev->rx_bufs);
147
148     /* Remove the buffer from the pool */
149     dev->rx_pool[pool]->count--;
150     bd = dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count];
151     dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count] = 0;
152
153     return bd;
154 }
155
156 /**
157  * Get buffer descriptor from the receive buffer list page that has been
158  * supplied by the guest with the H_REGISTER_LOGICAL_LAN call
159  */
160 static vlan_bd_t spapr_vlan_get_rx_bd_from_page(VIOsPAPRVLANDevice *dev,
161                                                 size_t size)
162 {
163     int buf_ptr = dev->use_buf_ptr;
164     vlan_bd_t bd;
165
166     do {
167         buf_ptr += 8;
168         if (buf_ptr >= VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF) {
169             buf_ptr = VLAN_RX_BDS_OFF;
170         }
171
172         bd = vio_ldq(&dev->sdev, dev->buf_list + buf_ptr);
173         DPRINTF("use_buf_ptr=%d bd=0x%016llx\n",
174                 buf_ptr, (unsigned long long)bd);
175     } while ((!(bd & VLAN_BD_VALID) || VLAN_BD_LEN(bd) < size + 8)
176              && buf_ptr != dev->use_buf_ptr);
177
178     if (!(bd & VLAN_BD_VALID) || VLAN_BD_LEN(bd) < size + 8) {
179         /* Failed to find a suitable buffer */
180         return 0;
181     }
182
183     /* Remove the buffer from the pool */
184     dev->use_buf_ptr = buf_ptr;
185     vio_stq(&dev->sdev, dev->buf_list + dev->use_buf_ptr, 0);
186
187     DPRINTF("Found buffer: ptr=%d rxbufs=%d\n", dev->use_buf_ptr, dev->rx_bufs);
188
189     return bd;
190 }
191
192 static ssize_t spapr_vlan_receive(NetClientState *nc, const uint8_t *buf,
193                                   size_t size)
194 {
195     VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc);
196     VIOsPAPRDevice *sdev = VIO_SPAPR_DEVICE(dev);
197     vlan_bd_t rxq_bd = vio_ldq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF);
198     vlan_bd_t bd;
199     uint64_t handle;
200     uint8_t control;
201
202     DPRINTF("spapr_vlan_receive() [%s] rx_bufs=%d\n", sdev->qdev.id,
203             dev->rx_bufs);
204
205     if (!dev->isopen) {
206         return -1;
207     }
208
209     if (!dev->rx_bufs) {
210         return 0;
211     }
212
213     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
214         bd = spapr_vlan_get_rx_bd_from_pool(dev, size);
215     } else {
216         bd = spapr_vlan_get_rx_bd_from_page(dev, size);
217     }
218     if (!bd) {
219         return 0;
220     }
221
222     dev->rx_bufs--;
223
224     /* Transfer the packet data */
225     if (spapr_vio_dma_write(sdev, VLAN_BD_ADDR(bd) + 8, buf, size) < 0) {
226         return -1;
227     }
228
229     DPRINTF("spapr_vlan_receive: DMA write completed\n");
230
231     /* Update the receive queue */
232     control = VLAN_RXQC_TOGGLE | VLAN_RXQC_VALID;
233     if (rxq_bd & VLAN_BD_TOGGLE) {
234         control ^= VLAN_RXQC_TOGGLE;
235     }
236
237     handle = vio_ldq(sdev, VLAN_BD_ADDR(bd));
238     vio_stq(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 8, handle);
239     vio_stl(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 4, size);
240     vio_sth(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 2, 8);
241     vio_stb(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr, control);
242
243     DPRINTF("wrote rxq entry (ptr=0x%llx): 0x%016llx 0x%016llx\n",
244             (unsigned long long)dev->rxq_ptr,
245             (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) +
246                                         dev->rxq_ptr),
247             (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) +
248                                         dev->rxq_ptr + 8));
249
250     dev->rxq_ptr += 16;
251     if (dev->rxq_ptr >= VLAN_BD_LEN(rxq_bd)) {
252         dev->rxq_ptr = 0;
253         vio_stq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF, rxq_bd ^ VLAN_BD_TOGGLE);
254     }
255
256     if (sdev->signal_state & 1) {
257         qemu_irq_pulse(spapr_vio_qirq(sdev));
258     }
259
260     return size;
261 }
262
263 static NetClientInfo net_spapr_vlan_info = {
264     .type = NET_CLIENT_OPTIONS_KIND_NIC,
265     .size = sizeof(NICState),
266     .can_receive = spapr_vlan_can_receive,
267     .receive = spapr_vlan_receive,
268 };
269
270 static void spapr_vlan_flush_rx_queue(void *opaque)
271 {
272     VIOsPAPRVLANDevice *dev = opaque;
273
274     qemu_flush_queued_packets(qemu_get_queue(dev->nic));
275 }
276
277 static void spapr_vlan_reset_rx_pool(RxBufPool *rxp)
278 {
279     /*
280      * Use INT_MAX as bufsize so that unused buffers are moved to the end
281      * of the list during the qsort in spapr_vlan_add_rxbuf_to_pool() later.
282      */
283     rxp->bufsize = INT_MAX;
284     rxp->count = 0;
285     memset(rxp->bds, 0, sizeof(rxp->bds));
286 }
287
288 static void spapr_vlan_reset(VIOsPAPRDevice *sdev)
289 {
290     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
291     int i;
292
293     dev->buf_list = 0;
294     dev->rx_bufs = 0;
295     dev->isopen = 0;
296
297     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
298         for (i = 0; i < RX_MAX_POOLS; i++) {
299             spapr_vlan_reset_rx_pool(dev->rx_pool[i]);
300         }
301     }
302 }
303
304 static void spapr_vlan_realize(VIOsPAPRDevice *sdev, Error **errp)
305 {
306     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
307
308     qemu_macaddr_default_if_unset(&dev->nicconf.macaddr);
309
310     dev->nic = qemu_new_nic(&net_spapr_vlan_info, &dev->nicconf,
311                             object_get_typename(OBJECT(sdev)), sdev->qdev.id, dev);
312     qemu_format_nic_info_str(qemu_get_queue(dev->nic), dev->nicconf.macaddr.a);
313
314     dev->rxp_timer = timer_new_us(QEMU_CLOCK_VIRTUAL, spapr_vlan_flush_rx_queue,
315                                   dev);
316 }
317
318 static void spapr_vlan_instance_init(Object *obj)
319 {
320     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(obj);
321     int i;
322
323     device_add_bootindex_property(obj, &dev->nicconf.bootindex,
324                                   "bootindex", "",
325                                   DEVICE(dev), NULL);
326
327     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
328         for (i = 0; i < RX_MAX_POOLS; i++) {
329             dev->rx_pool[i] = g_new(RxBufPool, 1);
330             spapr_vlan_reset_rx_pool(dev->rx_pool[i]);
331         }
332     }
333 }
334
335 static void spapr_vlan_instance_finalize(Object *obj)
336 {
337     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(obj);
338     int i;
339
340     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
341         for (i = 0; i < RX_MAX_POOLS; i++) {
342             g_free(dev->rx_pool[i]);
343             dev->rx_pool[i] = NULL;
344         }
345     }
346
347     if (dev->rxp_timer) {
348         timer_del(dev->rxp_timer);
349         timer_free(dev->rxp_timer);
350     }
351 }
352
353 void spapr_vlan_create(VIOsPAPRBus *bus, NICInfo *nd)
354 {
355     DeviceState *dev;
356
357     dev = qdev_create(&bus->bus, "spapr-vlan");
358
359     qdev_set_nic_properties(dev, nd);
360
361     qdev_init_nofail(dev);
362 }
363
364 static int spapr_vlan_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
365 {
366     VIOsPAPRVLANDevice *vdev = VIO_SPAPR_VLAN_DEVICE(dev);
367     uint8_t padded_mac[8] = {0, 0};
368     int ret;
369
370     /* Some old phyp versions give the mac address in an 8-byte
371      * property.  The kernel driver has an insane workaround for this;
372      * rather than doing the obvious thing and checking the property
373      * length, it checks whether the first byte has 0b10 in the low
374      * bits.  If a correct 6-byte property has a different first byte
375      * the kernel will get the wrong mac address, overrunning its
376      * buffer in the process (read only, thank goodness).
377      *
378      * Here we workaround the kernel workaround by always supplying an
379      * 8-byte property, with the mac address in the last six bytes */
380     memcpy(&padded_mac[2], &vdev->nicconf.macaddr, ETH_ALEN);
381     ret = fdt_setprop(fdt, node_off, "local-mac-address",
382                       padded_mac, sizeof(padded_mac));
383     if (ret < 0) {
384         return ret;
385     }
386
387     ret = fdt_setprop_cell(fdt, node_off, "ibm,mac-address-filters", 0);
388     if (ret < 0) {
389         return ret;
390     }
391
392     return 0;
393 }
394
395 static int check_bd(VIOsPAPRVLANDevice *dev, vlan_bd_t bd,
396                     target_ulong alignment)
397 {
398     if ((VLAN_BD_ADDR(bd) % alignment)
399         || (VLAN_BD_LEN(bd) % alignment)) {
400         return -1;
401     }
402
403     if (!spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd),
404                              VLAN_BD_LEN(bd), DMA_DIRECTION_FROM_DEVICE)
405         || !spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd),
406                                 VLAN_BD_LEN(bd), DMA_DIRECTION_TO_DEVICE)) {
407         return -1;
408     }
409
410     return 0;
411 }
412
413 static target_ulong h_register_logical_lan(PowerPCCPU *cpu,
414                                            sPAPRMachineState *spapr,
415                                            target_ulong opcode,
416                                            target_ulong *args)
417 {
418     target_ulong reg = args[0];
419     target_ulong buf_list = args[1];
420     target_ulong rec_queue = args[2];
421     target_ulong filter_list = args[3];
422     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
423     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
424     vlan_bd_t filter_list_bd;
425
426     if (!dev) {
427         return H_PARAMETER;
428     }
429
430     if (dev->isopen) {
431         hcall_dprintf("H_REGISTER_LOGICAL_LAN called twice without "
432                       "H_FREE_LOGICAL_LAN\n");
433         return H_RESOURCE;
434     }
435
436     if (check_bd(dev, VLAN_VALID_BD(buf_list, SPAPR_TCE_PAGE_SIZE),
437                  SPAPR_TCE_PAGE_SIZE) < 0) {
438         hcall_dprintf("Bad buf_list 0x" TARGET_FMT_lx "\n", buf_list);
439         return H_PARAMETER;
440     }
441
442     filter_list_bd = VLAN_VALID_BD(filter_list, SPAPR_TCE_PAGE_SIZE);
443     if (check_bd(dev, filter_list_bd, SPAPR_TCE_PAGE_SIZE) < 0) {
444         hcall_dprintf("Bad filter_list 0x" TARGET_FMT_lx "\n", filter_list);
445         return H_PARAMETER;
446     }
447
448     if (!(rec_queue & VLAN_BD_VALID)
449         || (check_bd(dev, rec_queue, VLAN_RQ_ALIGNMENT) < 0)) {
450         hcall_dprintf("Bad receive queue\n");
451         return H_PARAMETER;
452     }
453
454     dev->buf_list = buf_list;
455     sdev->signal_state = 0;
456
457     rec_queue &= ~VLAN_BD_TOGGLE;
458
459     /* Initialize the buffer list */
460     vio_stq(sdev, buf_list, rec_queue);
461     vio_stq(sdev, buf_list + 8, filter_list_bd);
462     spapr_vio_dma_set(sdev, buf_list + VLAN_RX_BDS_OFF, 0,
463                       SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF);
464     dev->add_buf_ptr = VLAN_RX_BDS_OFF - 8;
465     dev->use_buf_ptr = VLAN_RX_BDS_OFF - 8;
466     dev->rx_bufs = 0;
467     dev->rxq_ptr = 0;
468
469     /* Initialize the receive queue */
470     spapr_vio_dma_set(sdev, VLAN_BD_ADDR(rec_queue), 0, VLAN_BD_LEN(rec_queue));
471
472     dev->isopen = 1;
473     qemu_flush_queued_packets(qemu_get_queue(dev->nic));
474
475     return H_SUCCESS;
476 }
477
478
479 static target_ulong h_free_logical_lan(PowerPCCPU *cpu,
480                                        sPAPRMachineState *spapr,
481                                        target_ulong opcode, target_ulong *args)
482 {
483     target_ulong reg = args[0];
484     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
485     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
486
487     if (!dev) {
488         return H_PARAMETER;
489     }
490
491     if (!dev->isopen) {
492         hcall_dprintf("H_FREE_LOGICAL_LAN called without "
493                       "H_REGISTER_LOGICAL_LAN\n");
494         return H_RESOURCE;
495     }
496
497     spapr_vlan_reset(sdev);
498     return H_SUCCESS;
499 }
500
501 /**
502  * Used for qsort, this function compares two RxBufPools by size.
503  */
504 static int rx_pool_size_compare(const void *p1, const void *p2)
505 {
506     const RxBufPool *pool1 = *(RxBufPool **)p1;
507     const RxBufPool *pool2 = *(RxBufPool **)p2;
508
509     if (pool1->bufsize < pool2->bufsize) {
510         return -1;
511     }
512     return pool1->bufsize > pool2->bufsize;
513 }
514
515 /**
516  * Search for a matching buffer pool with exact matching size,
517  * or return -1 if no matching pool has been found.
518  */
519 static int spapr_vlan_get_rx_pool_id(VIOsPAPRVLANDevice *dev, int size)
520 {
521     int pool;
522
523     for (pool = 0; pool < RX_MAX_POOLS; pool++) {
524         if (dev->rx_pool[pool]->bufsize == size) {
525             return pool;
526         }
527     }
528
529     return -1;
530 }
531
532 /**
533  * Enqueuing receive buffer by adding it to one of our receive buffer pools
534  */
535 static target_long spapr_vlan_add_rxbuf_to_pool(VIOsPAPRVLANDevice *dev,
536                                                 target_ulong buf)
537 {
538     int size = VLAN_BD_LEN(buf);
539     int pool;
540
541     pool = spapr_vlan_get_rx_pool_id(dev, size);
542     if (pool < 0) {
543         /*
544          * No matching pool found? Try to use a new one. If the guest used all
545          * pools before, but changed the size of one pool inbetween, we might
546          * need to recycle that pool here (if it's empty already). Thus scan
547          * all buffer pools now, starting with the last (likely empty) one.
548          */
549         for (pool = RX_MAX_POOLS - 1; pool >= 0 ; pool--) {
550             if (dev->rx_pool[pool]->count == 0) {
551                 dev->rx_pool[pool]->bufsize = size;
552                 /*
553                  * Sort pools by size so that spapr_vlan_receive()
554                  * can later find the smallest buffer pool easily.
555                  */
556                 qsort(dev->rx_pool, RX_MAX_POOLS, sizeof(dev->rx_pool[0]),
557                       rx_pool_size_compare);
558                 pool = spapr_vlan_get_rx_pool_id(dev, size);
559                 DPRINTF("created RX pool %d for size %lld\n", pool,
560                         VLAN_BD_LEN(buf));
561                 break;
562             }
563         }
564     }
565     /* Still no usable pool? Give up */
566     if (pool < 0 || dev->rx_pool[pool]->count >= RX_POOL_MAX_BDS) {
567         return H_RESOURCE;
568     }
569
570     DPRINTF("h_add_llan_buf():  Add buf using pool %i (size %lli, count=%i)\n",
571             pool, VLAN_BD_LEN(buf), dev->rx_pool[pool]->count);
572
573     dev->rx_pool[pool]->bds[dev->rx_pool[pool]->count++] = buf;
574
575     return 0;
576 }
577
578 /**
579  * This is the old way of enqueuing receive buffers: Add it to the rx queue
580  * page that has been supplied by the guest (which is quite limited in size).
581  */
582 static target_long spapr_vlan_add_rxbuf_to_page(VIOsPAPRVLANDevice *dev,
583                                                 target_ulong buf)
584 {
585     vlan_bd_t bd;
586
587     if (dev->rx_bufs >= VLAN_MAX_BUFS) {
588         return H_RESOURCE;
589     }
590
591     do {
592         dev->add_buf_ptr += 8;
593         if (dev->add_buf_ptr >= VLAN_RX_BDS_LEN + VLAN_RX_BDS_OFF) {
594             dev->add_buf_ptr = VLAN_RX_BDS_OFF;
595         }
596
597         bd = vio_ldq(&dev->sdev, dev->buf_list + dev->add_buf_ptr);
598     } while (bd & VLAN_BD_VALID);
599
600     vio_stq(&dev->sdev, dev->buf_list + dev->add_buf_ptr, buf);
601
602     DPRINTF("h_add_llan_buf():  Added buf  ptr=%d  rx_bufs=%d bd=0x%016llx\n",
603             dev->add_buf_ptr, dev->rx_bufs, (unsigned long long)buf);
604
605     return 0;
606 }
607
608 static target_ulong h_add_logical_lan_buffer(PowerPCCPU *cpu,
609                                              sPAPRMachineState *spapr,
610                                              target_ulong opcode,
611                                              target_ulong *args)
612 {
613     target_ulong reg = args[0];
614     target_ulong buf = args[1];
615     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
616     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
617     target_long ret;
618
619     DPRINTF("H_ADD_LOGICAL_LAN_BUFFER(0x" TARGET_FMT_lx
620             ", 0x" TARGET_FMT_lx ")\n", reg, buf);
621
622     if (!sdev) {
623         hcall_dprintf("Bad device\n");
624         return H_PARAMETER;
625     }
626
627     if ((check_bd(dev, buf, 4) < 0)
628         || (VLAN_BD_LEN(buf) < 16)) {
629         hcall_dprintf("Bad buffer enqueued\n");
630         return H_PARAMETER;
631     }
632
633     if (!dev->isopen) {
634         return H_RESOURCE;
635     }
636
637     if (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) {
638         ret = spapr_vlan_add_rxbuf_to_pool(dev, buf);
639     } else {
640         ret = spapr_vlan_add_rxbuf_to_page(dev, buf);
641     }
642     if (ret) {
643         return ret;
644     }
645
646     dev->rx_bufs++;
647
648     /*
649      * Give guest some more time to add additional RX buffers before we
650      * flush the receive queue, so that e.g. fragmented IP packets can
651      * be passed to the guest in one go later (instead of passing single
652      * fragments if there is only one receive buffer available).
653      */
654     timer_mod(dev->rxp_timer, qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + 500);
655
656     return H_SUCCESS;
657 }
658
659 static target_ulong h_send_logical_lan(PowerPCCPU *cpu,
660                                        sPAPRMachineState *spapr,
661                                        target_ulong opcode, target_ulong *args)
662 {
663     target_ulong reg = args[0];
664     target_ulong *bufs = args + 1;
665     target_ulong continue_token = args[7];
666     VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
667     VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev);
668     unsigned total_len;
669     uint8_t *lbuf, *p;
670     int i, nbufs;
671     int ret;
672
673     DPRINTF("H_SEND_LOGICAL_LAN(0x" TARGET_FMT_lx ", <bufs>, 0x"
674             TARGET_FMT_lx ")\n", reg, continue_token);
675
676     if (!sdev) {
677         return H_PARAMETER;
678     }
679
680     DPRINTF("rxbufs = %d\n", dev->rx_bufs);
681
682     if (!dev->isopen) {
683         return H_DROPPED;
684     }
685
686     if (continue_token) {
687         return H_HARDWARE; /* FIXME actually handle this */
688     }
689
690     total_len = 0;
691     for (i = 0; i < 6; i++) {
692         DPRINTF("   buf desc: 0x" TARGET_FMT_lx "\n", bufs[i]);
693         if (!(bufs[i] & VLAN_BD_VALID)) {
694             break;
695         }
696         total_len += VLAN_BD_LEN(bufs[i]);
697     }
698
699     nbufs = i;
700     DPRINTF("h_send_logical_lan() %d buffers, total length 0x%x\n",
701             nbufs, total_len);
702
703     if (total_len == 0) {
704         return H_SUCCESS;
705     }
706
707     if (total_len > MAX_PACKET_SIZE) {
708         /* Don't let the guest force too large an allocation */
709         return H_RESOURCE;
710     }
711
712     lbuf = alloca(total_len);
713     p = lbuf;
714     for (i = 0; i < nbufs; i++) {
715         ret = spapr_vio_dma_read(sdev, VLAN_BD_ADDR(bufs[i]),
716                                  p, VLAN_BD_LEN(bufs[i]));
717         if (ret < 0) {
718             return ret;
719         }
720
721         p += VLAN_BD_LEN(bufs[i]);
722     }
723
724     qemu_send_packet(qemu_get_queue(dev->nic), lbuf, total_len);
725
726     return H_SUCCESS;
727 }
728
729 static target_ulong h_multicast_ctrl(PowerPCCPU *cpu, sPAPRMachineState *spapr,
730                                      target_ulong opcode, target_ulong *args)
731 {
732     target_ulong reg = args[0];
733     VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
734
735     if (!dev) {
736         return H_PARAMETER;
737     }
738
739     return H_SUCCESS;
740 }
741
742 static Property spapr_vlan_properties[] = {
743     DEFINE_SPAPR_PROPERTIES(VIOsPAPRVLANDevice, sdev),
744     DEFINE_NIC_PROPERTIES(VIOsPAPRVLANDevice, nicconf),
745     DEFINE_PROP_BIT("use-rx-buffer-pools", VIOsPAPRVLANDevice,
746                     compat_flags, SPAPRVLAN_FLAG_RX_BUF_POOLS_BIT, true),
747     DEFINE_PROP_END_OF_LIST(),
748 };
749
750 static bool spapr_vlan_rx_buffer_pools_needed(void *opaque)
751 {
752     VIOsPAPRVLANDevice *dev = opaque;
753
754     return (dev->compat_flags & SPAPRVLAN_FLAG_RX_BUF_POOLS) != 0;
755 }
756
757 static const VMStateDescription vmstate_rx_buffer_pool = {
758     .name = "spapr_llan/rx_buffer_pool",
759     .version_id = 1,
760     .minimum_version_id = 1,
761     .needed = spapr_vlan_rx_buffer_pools_needed,
762     .fields = (VMStateField[]) {
763         VMSTATE_INT32(bufsize, RxBufPool),
764         VMSTATE_INT32(count, RxBufPool),
765         VMSTATE_UINT64_ARRAY(bds, RxBufPool, RX_POOL_MAX_BDS),
766         VMSTATE_END_OF_LIST()
767     }
768 };
769
770 static const VMStateDescription vmstate_rx_pools = {
771     .name = "spapr_llan/rx_pools",
772     .version_id = 1,
773     .minimum_version_id = 1,
774     .needed = spapr_vlan_rx_buffer_pools_needed,
775     .fields = (VMStateField[]) {
776         VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(rx_pool, VIOsPAPRVLANDevice,
777                                            RX_MAX_POOLS, 1,
778                                            vmstate_rx_buffer_pool, RxBufPool),
779         VMSTATE_END_OF_LIST()
780     }
781 };
782
783 static const VMStateDescription vmstate_spapr_llan = {
784     .name = "spapr_llan",
785     .version_id = 1,
786     .minimum_version_id = 1,
787     .fields = (VMStateField[]) {
788         VMSTATE_SPAPR_VIO(sdev, VIOsPAPRVLANDevice),
789         /* LLAN state */
790         VMSTATE_BOOL(isopen, VIOsPAPRVLANDevice),
791         VMSTATE_UINT64(buf_list, VIOsPAPRVLANDevice),
792         VMSTATE_UINT32(add_buf_ptr, VIOsPAPRVLANDevice),
793         VMSTATE_UINT32(use_buf_ptr, VIOsPAPRVLANDevice),
794         VMSTATE_UINT32(rx_bufs, VIOsPAPRVLANDevice),
795         VMSTATE_UINT64(rxq_ptr, VIOsPAPRVLANDevice),
796
797         VMSTATE_END_OF_LIST()
798     },
799     .subsections = (const VMStateDescription * []) {
800         &vmstate_rx_pools,
801         NULL
802     }
803 };
804
805 static void spapr_vlan_class_init(ObjectClass *klass, void *data)
806 {
807     DeviceClass *dc = DEVICE_CLASS(klass);
808     VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
809
810     k->realize = spapr_vlan_realize;
811     k->reset = spapr_vlan_reset;
812     k->devnode = spapr_vlan_devnode;
813     k->dt_name = "l-lan";
814     k->dt_type = "network";
815     k->dt_compatible = "IBM,l-lan";
816     k->signal_mask = 0x1;
817     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
818     dc->props = spapr_vlan_properties;
819     k->rtce_window_size = 0x10000000;
820     dc->vmsd = &vmstate_spapr_llan;
821 }
822
823 static const TypeInfo spapr_vlan_info = {
824     .name          = TYPE_VIO_SPAPR_VLAN_DEVICE,
825     .parent        = TYPE_VIO_SPAPR_DEVICE,
826     .instance_size = sizeof(VIOsPAPRVLANDevice),
827     .class_init    = spapr_vlan_class_init,
828     .instance_init = spapr_vlan_instance_init,
829     .instance_finalize = spapr_vlan_instance_finalize,
830 };
831
832 static void spapr_vlan_register_types(void)
833 {
834     spapr_register_hypercall(H_REGISTER_LOGICAL_LAN, h_register_logical_lan);
835     spapr_register_hypercall(H_FREE_LOGICAL_LAN, h_free_logical_lan);
836     spapr_register_hypercall(H_SEND_LOGICAL_LAN, h_send_logical_lan);
837     spapr_register_hypercall(H_ADD_LOGICAL_LAN_BUFFER,
838                              h_add_logical_lan_buffer);
839     spapr_register_hypercall(H_MULTICAST_CTRL, h_multicast_ctrl);
840     type_register_static(&spapr_vlan_info);
841 }
842
843 type_init(spapr_vlan_register_types)
This page took 0.070014 seconds and 4 git commands to generate.