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