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