2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
4 * PAPR Inter-VM Logical Lan, aka ibmveth
6 * Copyright (c) 2010,2011 David Gibson, IBM Corporation.
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
27 #include "qemu/osdep.h"
31 #include "hw/ppc/spapr.h"
32 #include "hw/ppc/spapr_vio.h"
33 #include "sysemu/sysemu.h"
38 #define MAX_PACKET_SIZE 65536
43 #define DPRINTF(fmt...) do { fprintf(stderr, fmt); } while (0)
45 #define DPRINTF(fmt...)
52 typedef uint64_t vlan_bd_t
;
54 #define VLAN_BD_VALID 0x8000000000000000ULL
55 #define VLAN_BD_TOGGLE 0x4000000000000000ULL
56 #define VLAN_BD_NO_CSUM 0x0200000000000000ULL
57 #define VLAN_BD_CSUM_GOOD 0x0100000000000000ULL
58 #define VLAN_BD_LEN_MASK 0x00ffffff00000000ULL
59 #define VLAN_BD_LEN(bd) (((bd) & VLAN_BD_LEN_MASK) >> 32)
60 #define VLAN_BD_ADDR_MASK 0x00000000ffffffffULL
61 #define VLAN_BD_ADDR(bd) ((bd) & VLAN_BD_ADDR_MASK)
63 #define VLAN_VALID_BD(addr, len) (VLAN_BD_VALID | \
64 (((len) << 32) & VLAN_BD_LEN_MASK) | \
65 (addr & VLAN_BD_ADDR_MASK))
67 #define VLAN_RXQC_TOGGLE 0x80
68 #define VLAN_RXQC_VALID 0x40
69 #define VLAN_RXQC_NO_CSUM 0x02
70 #define VLAN_RXQC_CSUM_GOOD 0x01
72 #define VLAN_RQ_ALIGNMENT 16
73 #define VLAN_RXQ_BD_OFF 0
74 #define VLAN_FILTER_BD_OFF 8
75 #define VLAN_RX_BDS_OFF 16
77 * The final 8 bytes of the buffer list is a counter of frames dropped
78 * because there was not a buffer in the buffer list capable of holding
79 * the frame. We must avoid it, or the operating system will report garbage
82 #define VLAN_RX_BDS_LEN (SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF - 8)
83 #define VLAN_MAX_BUFS (VLAN_RX_BDS_LEN / 8)
85 #define TYPE_VIO_SPAPR_VLAN_DEVICE "spapr-vlan"
86 #define VIO_SPAPR_VLAN_DEVICE(obj) \
87 OBJECT_CHECK(VIOsPAPRVLANDevice, (obj), TYPE_VIO_SPAPR_VLAN_DEVICE)
89 typedef struct VIOsPAPRVLANDevice
{
94 target_ulong buf_list
;
95 uint32_t add_buf_ptr
, use_buf_ptr
, rx_bufs
;
99 static int spapr_vlan_can_receive(NetClientState
*nc
)
101 VIOsPAPRVLANDevice
*dev
= qemu_get_nic_opaque(nc
);
103 return (dev
->isopen
&& dev
->rx_bufs
> 0);
106 static ssize_t
spapr_vlan_receive(NetClientState
*nc
, const uint8_t *buf
,
109 VIOsPAPRVLANDevice
*dev
= qemu_get_nic_opaque(nc
);
110 VIOsPAPRDevice
*sdev
= VIO_SPAPR_DEVICE(dev
);
111 vlan_bd_t rxq_bd
= vio_ldq(sdev
, dev
->buf_list
+ VLAN_RXQ_BD_OFF
);
113 int buf_ptr
= dev
->use_buf_ptr
;
117 DPRINTF("spapr_vlan_receive() [%s] rx_bufs=%d\n", sdev
->qdev
.id
,
130 if (buf_ptr
>= (VLAN_RX_BDS_LEN
+ VLAN_RX_BDS_OFF
)) {
131 buf_ptr
= VLAN_RX_BDS_OFF
;
134 bd
= vio_ldq(sdev
, dev
->buf_list
+ buf_ptr
);
135 DPRINTF("use_buf_ptr=%d bd=0x%016llx\n",
136 buf_ptr
, (unsigned long long)bd
);
137 } while ((!(bd
& VLAN_BD_VALID
) || (VLAN_BD_LEN(bd
) < (size
+ 8)))
138 && (buf_ptr
!= dev
->use_buf_ptr
));
140 if (!(bd
& VLAN_BD_VALID
) || (VLAN_BD_LEN(bd
) < (size
+ 8))) {
141 /* Failed to find a suitable buffer */
145 /* Remove the buffer from the pool */
147 dev
->use_buf_ptr
= buf_ptr
;
148 vio_stq(sdev
, dev
->buf_list
+ dev
->use_buf_ptr
, 0);
150 DPRINTF("Found buffer: ptr=%d num=%d\n", dev
->use_buf_ptr
, dev
->rx_bufs
);
152 /* Transfer the packet data */
153 if (spapr_vio_dma_write(sdev
, VLAN_BD_ADDR(bd
) + 8, buf
, size
) < 0) {
157 DPRINTF("spapr_vlan_receive: DMA write completed\n");
159 /* Update the receive queue */
160 control
= VLAN_RXQC_TOGGLE
| VLAN_RXQC_VALID
;
161 if (rxq_bd
& VLAN_BD_TOGGLE
) {
162 control
^= VLAN_RXQC_TOGGLE
;
165 handle
= vio_ldq(sdev
, VLAN_BD_ADDR(bd
));
166 vio_stq(sdev
, VLAN_BD_ADDR(rxq_bd
) + dev
->rxq_ptr
+ 8, handle
);
167 vio_stl(sdev
, VLAN_BD_ADDR(rxq_bd
) + dev
->rxq_ptr
+ 4, size
);
168 vio_sth(sdev
, VLAN_BD_ADDR(rxq_bd
) + dev
->rxq_ptr
+ 2, 8);
169 vio_stb(sdev
, VLAN_BD_ADDR(rxq_bd
) + dev
->rxq_ptr
, control
);
171 DPRINTF("wrote rxq entry (ptr=0x%llx): 0x%016llx 0x%016llx\n",
172 (unsigned long long)dev
->rxq_ptr
,
173 (unsigned long long)vio_ldq(sdev
, VLAN_BD_ADDR(rxq_bd
) +
175 (unsigned long long)vio_ldq(sdev
, VLAN_BD_ADDR(rxq_bd
) +
179 if (dev
->rxq_ptr
>= VLAN_BD_LEN(rxq_bd
)) {
181 vio_stq(sdev
, dev
->buf_list
+ VLAN_RXQ_BD_OFF
, rxq_bd
^ VLAN_BD_TOGGLE
);
184 if (sdev
->signal_state
& 1) {
185 qemu_irq_pulse(spapr_vio_qirq(sdev
));
191 static NetClientInfo net_spapr_vlan_info
= {
192 .type
= NET_CLIENT_OPTIONS_KIND_NIC
,
193 .size
= sizeof(NICState
),
194 .can_receive
= spapr_vlan_can_receive
,
195 .receive
= spapr_vlan_receive
,
198 static void spapr_vlan_reset(VIOsPAPRDevice
*sdev
)
200 VIOsPAPRVLANDevice
*dev
= VIO_SPAPR_VLAN_DEVICE(sdev
);
207 static void spapr_vlan_realize(VIOsPAPRDevice
*sdev
, Error
**errp
)
209 VIOsPAPRVLANDevice
*dev
= VIO_SPAPR_VLAN_DEVICE(sdev
);
211 qemu_macaddr_default_if_unset(&dev
->nicconf
.macaddr
);
213 dev
->nic
= qemu_new_nic(&net_spapr_vlan_info
, &dev
->nicconf
,
214 object_get_typename(OBJECT(sdev
)), sdev
->qdev
.id
, dev
);
215 qemu_format_nic_info_str(qemu_get_queue(dev
->nic
), dev
->nicconf
.macaddr
.a
);
218 static void spapr_vlan_instance_init(Object
*obj
)
220 VIOsPAPRVLANDevice
*dev
= VIO_SPAPR_VLAN_DEVICE(obj
);
222 device_add_bootindex_property(obj
, &dev
->nicconf
.bootindex
,
227 void spapr_vlan_create(VIOsPAPRBus
*bus
, NICInfo
*nd
)
231 dev
= qdev_create(&bus
->bus
, "spapr-vlan");
233 qdev_set_nic_properties(dev
, nd
);
235 qdev_init_nofail(dev
);
238 static int spapr_vlan_devnode(VIOsPAPRDevice
*dev
, void *fdt
, int node_off
)
240 VIOsPAPRVLANDevice
*vdev
= VIO_SPAPR_VLAN_DEVICE(dev
);
241 uint8_t padded_mac
[8] = {0, 0};
244 /* Some old phyp versions give the mac address in an 8-byte
245 * property. The kernel driver has an insane workaround for this;
246 * rather than doing the obvious thing and checking the property
247 * length, it checks whether the first byte has 0b10 in the low
248 * bits. If a correct 6-byte property has a different first byte
249 * the kernel will get the wrong mac address, overrunning its
250 * buffer in the process (read only, thank goodness).
252 * Here we workaround the kernel workaround by always supplying an
253 * 8-byte property, with the mac address in the last six bytes */
254 memcpy(&padded_mac
[2], &vdev
->nicconf
.macaddr
, ETH_ALEN
);
255 ret
= fdt_setprop(fdt
, node_off
, "local-mac-address",
256 padded_mac
, sizeof(padded_mac
));
261 ret
= fdt_setprop_cell(fdt
, node_off
, "ibm,mac-address-filters", 0);
269 static int check_bd(VIOsPAPRVLANDevice
*dev
, vlan_bd_t bd
,
270 target_ulong alignment
)
272 if ((VLAN_BD_ADDR(bd
) % alignment
)
273 || (VLAN_BD_LEN(bd
) % alignment
)) {
277 if (!spapr_vio_dma_valid(&dev
->sdev
, VLAN_BD_ADDR(bd
),
278 VLAN_BD_LEN(bd
), DMA_DIRECTION_FROM_DEVICE
)
279 || !spapr_vio_dma_valid(&dev
->sdev
, VLAN_BD_ADDR(bd
),
280 VLAN_BD_LEN(bd
), DMA_DIRECTION_TO_DEVICE
)) {
287 static target_ulong
h_register_logical_lan(PowerPCCPU
*cpu
,
288 sPAPRMachineState
*spapr
,
292 target_ulong reg
= args
[0];
293 target_ulong buf_list
= args
[1];
294 target_ulong rec_queue
= args
[2];
295 target_ulong filter_list
= args
[3];
296 VIOsPAPRDevice
*sdev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
297 VIOsPAPRVLANDevice
*dev
= VIO_SPAPR_VLAN_DEVICE(sdev
);
298 vlan_bd_t filter_list_bd
;
305 hcall_dprintf("H_REGISTER_LOGICAL_LAN called twice without "
306 "H_FREE_LOGICAL_LAN\n");
310 if (check_bd(dev
, VLAN_VALID_BD(buf_list
, SPAPR_TCE_PAGE_SIZE
),
311 SPAPR_TCE_PAGE_SIZE
) < 0) {
312 hcall_dprintf("Bad buf_list 0x" TARGET_FMT_lx
"\n", buf_list
);
316 filter_list_bd
= VLAN_VALID_BD(filter_list
, SPAPR_TCE_PAGE_SIZE
);
317 if (check_bd(dev
, filter_list_bd
, SPAPR_TCE_PAGE_SIZE
) < 0) {
318 hcall_dprintf("Bad filter_list 0x" TARGET_FMT_lx
"\n", filter_list
);
322 if (!(rec_queue
& VLAN_BD_VALID
)
323 || (check_bd(dev
, rec_queue
, VLAN_RQ_ALIGNMENT
) < 0)) {
324 hcall_dprintf("Bad receive queue\n");
328 dev
->buf_list
= buf_list
;
329 sdev
->signal_state
= 0;
331 rec_queue
&= ~VLAN_BD_TOGGLE
;
333 /* Initialize the buffer list */
334 vio_stq(sdev
, buf_list
, rec_queue
);
335 vio_stq(sdev
, buf_list
+ 8, filter_list_bd
);
336 spapr_vio_dma_set(sdev
, buf_list
+ VLAN_RX_BDS_OFF
, 0,
337 SPAPR_TCE_PAGE_SIZE
- VLAN_RX_BDS_OFF
);
338 dev
->add_buf_ptr
= VLAN_RX_BDS_OFF
- 8;
339 dev
->use_buf_ptr
= VLAN_RX_BDS_OFF
- 8;
343 /* Initialize the receive queue */
344 spapr_vio_dma_set(sdev
, VLAN_BD_ADDR(rec_queue
), 0, VLAN_BD_LEN(rec_queue
));
347 qemu_flush_queued_packets(qemu_get_queue(dev
->nic
));
353 static target_ulong
h_free_logical_lan(PowerPCCPU
*cpu
,
354 sPAPRMachineState
*spapr
,
355 target_ulong opcode
, target_ulong
*args
)
357 target_ulong reg
= args
[0];
358 VIOsPAPRDevice
*sdev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
359 VIOsPAPRVLANDevice
*dev
= VIO_SPAPR_VLAN_DEVICE(sdev
);
366 hcall_dprintf("H_FREE_LOGICAL_LAN called without "
367 "H_REGISTER_LOGICAL_LAN\n");
371 spapr_vlan_reset(sdev
);
375 static target_ulong
h_add_logical_lan_buffer(PowerPCCPU
*cpu
,
376 sPAPRMachineState
*spapr
,
380 target_ulong reg
= args
[0];
381 target_ulong buf
= args
[1];
382 VIOsPAPRDevice
*sdev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
383 VIOsPAPRVLANDevice
*dev
= VIO_SPAPR_VLAN_DEVICE(sdev
);
386 DPRINTF("H_ADD_LOGICAL_LAN_BUFFER(0x" TARGET_FMT_lx
387 ", 0x" TARGET_FMT_lx
")\n", reg
, buf
);
390 hcall_dprintf("Bad device\n");
394 if ((check_bd(dev
, buf
, 4) < 0)
395 || (VLAN_BD_LEN(buf
) < 16)) {
396 hcall_dprintf("Bad buffer enqueued\n");
400 if (!dev
->isopen
|| dev
->rx_bufs
>= VLAN_MAX_BUFS
) {
405 dev
->add_buf_ptr
+= 8;
406 if (dev
->add_buf_ptr
>= (VLAN_RX_BDS_LEN
+ VLAN_RX_BDS_OFF
)) {
407 dev
->add_buf_ptr
= VLAN_RX_BDS_OFF
;
410 bd
= vio_ldq(sdev
, dev
->buf_list
+ dev
->add_buf_ptr
);
411 } while (bd
& VLAN_BD_VALID
);
413 vio_stq(sdev
, dev
->buf_list
+ dev
->add_buf_ptr
, buf
);
417 qemu_flush_queued_packets(qemu_get_queue(dev
->nic
));
419 DPRINTF("h_add_logical_lan_buffer(): Added buf ptr=%d rx_bufs=%d"
420 " bd=0x%016llx\n", dev
->add_buf_ptr
, dev
->rx_bufs
,
421 (unsigned long long)buf
);
426 static target_ulong
h_send_logical_lan(PowerPCCPU
*cpu
,
427 sPAPRMachineState
*spapr
,
428 target_ulong opcode
, target_ulong
*args
)
430 target_ulong reg
= args
[0];
431 target_ulong
*bufs
= args
+ 1;
432 target_ulong continue_token
= args
[7];
433 VIOsPAPRDevice
*sdev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
434 VIOsPAPRVLANDevice
*dev
= VIO_SPAPR_VLAN_DEVICE(sdev
);
440 DPRINTF("H_SEND_LOGICAL_LAN(0x" TARGET_FMT_lx
", <bufs>, 0x"
441 TARGET_FMT_lx
")\n", reg
, continue_token
);
447 DPRINTF("rxbufs = %d\n", dev
->rx_bufs
);
453 if (continue_token
) {
454 return H_HARDWARE
; /* FIXME actually handle this */
458 for (i
= 0; i
< 6; i
++) {
459 DPRINTF(" buf desc: 0x" TARGET_FMT_lx
"\n", bufs
[i
]);
460 if (!(bufs
[i
] & VLAN_BD_VALID
)) {
463 total_len
+= VLAN_BD_LEN(bufs
[i
]);
467 DPRINTF("h_send_logical_lan() %d buffers, total length 0x%x\n",
470 if (total_len
== 0) {
474 if (total_len
> MAX_PACKET_SIZE
) {
475 /* Don't let the guest force too large an allocation */
479 lbuf
= alloca(total_len
);
481 for (i
= 0; i
< nbufs
; i
++) {
482 ret
= spapr_vio_dma_read(sdev
, VLAN_BD_ADDR(bufs
[i
]),
483 p
, VLAN_BD_LEN(bufs
[i
]));
488 p
+= VLAN_BD_LEN(bufs
[i
]);
491 qemu_send_packet(qemu_get_queue(dev
->nic
), lbuf
, total_len
);
496 static target_ulong
h_multicast_ctrl(PowerPCCPU
*cpu
, sPAPRMachineState
*spapr
,
497 target_ulong opcode
, target_ulong
*args
)
499 target_ulong reg
= args
[0];
500 VIOsPAPRDevice
*dev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
509 static Property spapr_vlan_properties
[] = {
510 DEFINE_SPAPR_PROPERTIES(VIOsPAPRVLANDevice
, sdev
),
511 DEFINE_NIC_PROPERTIES(VIOsPAPRVLANDevice
, nicconf
),
512 DEFINE_PROP_END_OF_LIST(),
515 static const VMStateDescription vmstate_spapr_llan
= {
516 .name
= "spapr_llan",
518 .minimum_version_id
= 1,
519 .fields
= (VMStateField
[]) {
520 VMSTATE_SPAPR_VIO(sdev
, VIOsPAPRVLANDevice
),
522 VMSTATE_BOOL(isopen
, VIOsPAPRVLANDevice
),
523 VMSTATE_UINTTL(buf_list
, VIOsPAPRVLANDevice
),
524 VMSTATE_UINT32(add_buf_ptr
, VIOsPAPRVLANDevice
),
525 VMSTATE_UINT32(use_buf_ptr
, VIOsPAPRVLANDevice
),
526 VMSTATE_UINT32(rx_bufs
, VIOsPAPRVLANDevice
),
527 VMSTATE_UINTTL(rxq_ptr
, VIOsPAPRVLANDevice
),
529 VMSTATE_END_OF_LIST()
533 static void spapr_vlan_class_init(ObjectClass
*klass
, void *data
)
535 DeviceClass
*dc
= DEVICE_CLASS(klass
);
536 VIOsPAPRDeviceClass
*k
= VIO_SPAPR_DEVICE_CLASS(klass
);
538 k
->realize
= spapr_vlan_realize
;
539 k
->reset
= spapr_vlan_reset
;
540 k
->devnode
= spapr_vlan_devnode
;
541 k
->dt_name
= "l-lan";
542 k
->dt_type
= "network";
543 k
->dt_compatible
= "IBM,l-lan";
544 k
->signal_mask
= 0x1;
545 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
546 dc
->props
= spapr_vlan_properties
;
547 k
->rtce_window_size
= 0x10000000;
548 dc
->vmsd
= &vmstate_spapr_llan
;
551 static const TypeInfo spapr_vlan_info
= {
552 .name
= TYPE_VIO_SPAPR_VLAN_DEVICE
,
553 .parent
= TYPE_VIO_SPAPR_DEVICE
,
554 .instance_size
= sizeof(VIOsPAPRVLANDevice
),
555 .class_init
= spapr_vlan_class_init
,
556 .instance_init
= spapr_vlan_instance_init
,
559 static void spapr_vlan_register_types(void)
561 spapr_register_hypercall(H_REGISTER_LOGICAL_LAN
, h_register_logical_lan
);
562 spapr_register_hypercall(H_FREE_LOGICAL_LAN
, h_free_logical_lan
);
563 spapr_register_hypercall(H_SEND_LOGICAL_LAN
, h_send_logical_lan
);
564 spapr_register_hypercall(H_ADD_LOGICAL_LAN_BUFFER
,
565 h_add_logical_lan_buffer
);
566 spapr_register_hypercall(H_MULTICAST_CTRL
, h_multicast_ctrl
);
567 type_register_static(&spapr_vlan_info
);
570 type_init(spapr_vlan_register_types
)