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