fix spelling in linux-user sub directory
[qemu.git] / hw / spapr_llan.c
blobabe12973e6fce6f0002f5fc9fe1fce33f4fc1572
1 /*
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
24 * THE SOFTWARE.
27 #include "hw.h"
28 #include "net.h"
29 #include "hw/qdev.h"
30 #include "hw/spapr.h"
31 #include "hw/spapr_vio.h"
33 #include <libfdt.h>
35 #define ETH_ALEN 6
36 #define MAX_PACKET_SIZE 65536
38 /*#define DEBUG*/
40 #ifdef DEBUG
41 #define dprintf(fmt...) do { fprintf(stderr, fmt); } while (0)
42 #else
43 #define dprintf(fmt...)
44 #endif
47 * Virtual LAN device
50 typedef uint64_t vlan_bd_t;
52 #define VLAN_BD_VALID 0x8000000000000000ULL
53 #define VLAN_BD_TOGGLE 0x4000000000000000ULL
54 #define VLAN_BD_NO_CSUM 0x0200000000000000ULL
55 #define VLAN_BD_CSUM_GOOD 0x0100000000000000ULL
56 #define VLAN_BD_LEN_MASK 0x00ffffff00000000ULL
57 #define VLAN_BD_LEN(bd) (((bd) & VLAN_BD_LEN_MASK) >> 32)
58 #define VLAN_BD_ADDR_MASK 0x00000000ffffffffULL
59 #define VLAN_BD_ADDR(bd) ((bd) & VLAN_BD_ADDR_MASK)
61 #define VLAN_VALID_BD(addr, len) (VLAN_BD_VALID | \
62 (((len) << 32) & VLAN_BD_LEN_MASK) | \
63 (addr & VLAN_BD_ADDR_MASK))
65 #define VLAN_RXQC_TOGGLE 0x80
66 #define VLAN_RXQC_VALID 0x40
67 #define VLAN_RXQC_NO_CSUM 0x02
68 #define VLAN_RXQC_CSUM_GOOD 0x01
70 #define VLAN_RQ_ALIGNMENT 16
71 #define VLAN_RXQ_BD_OFF 0
72 #define VLAN_FILTER_BD_OFF 8
73 #define VLAN_RX_BDS_OFF 16
74 #define VLAN_MAX_BUFS ((SPAPR_VIO_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF) / 8)
76 typedef struct VIOsPAPRVLANDevice {
77 VIOsPAPRDevice sdev;
78 NICConf nicconf;
79 NICState *nic;
80 int isopen;
81 target_ulong buf_list;
82 int add_buf_ptr, use_buf_ptr, rx_bufs;
83 target_ulong rxq_ptr;
84 } VIOsPAPRVLANDevice;
86 static int spapr_vlan_can_receive(VLANClientState *nc)
88 VIOsPAPRVLANDevice *dev = DO_UPCAST(NICState, nc, nc)->opaque;
90 return (dev->isopen && dev->rx_bufs > 0);
93 static ssize_t spapr_vlan_receive(VLANClientState *nc, const uint8_t *buf,
94 size_t size)
96 VIOsPAPRDevice *sdev = DO_UPCAST(NICState, nc, nc)->opaque;
97 VIOsPAPRVLANDevice *dev = (VIOsPAPRVLANDevice *)sdev;
98 vlan_bd_t rxq_bd = ldq_tce(sdev, dev->buf_list + VLAN_RXQ_BD_OFF);
99 vlan_bd_t bd;
100 int buf_ptr = dev->use_buf_ptr;
101 uint64_t handle;
102 uint8_t control;
104 dprintf("spapr_vlan_receive() [%s] rx_bufs=%d\n", sdev->qdev.id,
105 dev->rx_bufs);
107 if (!dev->isopen) {
108 return -1;
111 if (!dev->rx_bufs) {
112 return -1;
115 do {
116 buf_ptr += 8;
117 if (buf_ptr >= SPAPR_VIO_TCE_PAGE_SIZE) {
118 buf_ptr = VLAN_RX_BDS_OFF;
121 bd = ldq_tce(sdev, dev->buf_list + buf_ptr);
122 dprintf("use_buf_ptr=%d bd=0x%016llx\n",
123 buf_ptr, (unsigned long long)bd);
124 } while ((!(bd & VLAN_BD_VALID) || (VLAN_BD_LEN(bd) < (size + 8)))
125 && (buf_ptr != dev->use_buf_ptr));
127 if (!(bd & VLAN_BD_VALID) || (VLAN_BD_LEN(bd) < (size + 8))) {
128 /* Failed to find a suitable buffer */
129 return -1;
132 /* Remove the buffer from the pool */
133 dev->rx_bufs--;
134 dev->use_buf_ptr = buf_ptr;
135 stq_tce(sdev, dev->buf_list + dev->use_buf_ptr, 0);
137 dprintf("Found buffer: ptr=%d num=%d\n", dev->use_buf_ptr, dev->rx_bufs);
139 /* Transfer the packet data */
140 if (spapr_tce_dma_write(sdev, VLAN_BD_ADDR(bd) + 8, buf, size) < 0) {
141 return -1;
144 dprintf("spapr_vlan_receive: DMA write completed\n");
146 /* Update the receive queue */
147 control = VLAN_RXQC_TOGGLE | VLAN_RXQC_VALID;
148 if (rxq_bd & VLAN_BD_TOGGLE) {
149 control ^= VLAN_RXQC_TOGGLE;
152 handle = ldq_tce(sdev, VLAN_BD_ADDR(bd));
153 stq_tce(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 8, handle);
154 stw_tce(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 4, size);
155 sth_tce(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 2, 8);
156 stb_tce(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr, control);
158 dprintf("wrote rxq entry (ptr=0x%llx): 0x%016llx 0x%016llx\n",
159 (unsigned long long)dev->rxq_ptr,
160 (unsigned long long)ldq_tce(sdev, VLAN_BD_ADDR(rxq_bd) +
161 dev->rxq_ptr),
162 (unsigned long long)ldq_tce(sdev, VLAN_BD_ADDR(rxq_bd) +
163 dev->rxq_ptr + 8));
165 dev->rxq_ptr += 16;
166 if (dev->rxq_ptr >= VLAN_BD_LEN(rxq_bd)) {
167 dev->rxq_ptr = 0;
168 stq_tce(sdev, dev->buf_list + VLAN_RXQ_BD_OFF, rxq_bd ^ VLAN_BD_TOGGLE);
171 if (sdev->signal_state & 1) {
172 qemu_irq_pulse(sdev->qirq);
175 return size;
178 static NetClientInfo net_spapr_vlan_info = {
179 .type = NET_CLIENT_TYPE_NIC,
180 .size = sizeof(NICState),
181 .can_receive = spapr_vlan_can_receive,
182 .receive = spapr_vlan_receive,
185 static int spapr_vlan_init(VIOsPAPRDevice *sdev)
187 VIOsPAPRVLANDevice *dev = (VIOsPAPRVLANDevice *)sdev;
189 qemu_macaddr_default_if_unset(&dev->nicconf.macaddr);
191 dev->nic = qemu_new_nic(&net_spapr_vlan_info, &dev->nicconf,
192 sdev->qdev.info->name, sdev->qdev.id, dev);
193 qemu_format_nic_info_str(&dev->nic->nc, dev->nicconf.macaddr.a);
195 return 0;
198 void spapr_vlan_create(VIOsPAPRBus *bus, uint32_t reg, NICInfo *nd)
200 DeviceState *dev;
202 dev = qdev_create(&bus->bus, "spapr-vlan");
203 qdev_prop_set_uint32(dev, "reg", reg);
205 qdev_set_nic_properties(dev, nd);
207 qdev_init_nofail(dev);
210 static int spapr_vlan_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
212 VIOsPAPRVLANDevice *vdev = (VIOsPAPRVLANDevice *)dev;
213 uint8_t padded_mac[8] = {0, 0};
214 int ret;
216 /* Some old phyp versions give the mac address in an 8-byte
217 * property. The kernel driver has an insane workaround for this;
218 * rather than doing the obvious thing and checking the property
219 * length, it checks whether the first byte has 0b10 in the low
220 * bits. If a correct 6-byte property has a different first byte
221 * the kernel will get the wrong mac address, overrunning its
222 * buffer in the process (read only, thank goodness).
224 * Here we workaround the kernel workaround by always supplying an
225 * 8-byte property, with the mac address in the last six bytes */
226 memcpy(&padded_mac[2], &vdev->nicconf.macaddr, ETH_ALEN);
227 ret = fdt_setprop(fdt, node_off, "local-mac-address",
228 padded_mac, sizeof(padded_mac));
229 if (ret < 0) {
230 return ret;
233 ret = fdt_setprop_cell(fdt, node_off, "ibm,mac-address-filters", 0);
234 if (ret < 0) {
235 return ret;
238 return 0;
241 static int check_bd(VIOsPAPRVLANDevice *dev, vlan_bd_t bd,
242 target_ulong alignment)
244 if ((VLAN_BD_ADDR(bd) % alignment)
245 || (VLAN_BD_LEN(bd) % alignment)) {
246 return -1;
249 if (spapr_vio_check_tces(&dev->sdev, VLAN_BD_ADDR(bd),
250 VLAN_BD_LEN(bd), SPAPR_TCE_RW) != 0) {
251 return -1;
254 return 0;
257 static target_ulong h_register_logical_lan(CPUState *env,
258 sPAPREnvironment *spapr,
259 target_ulong opcode,
260 target_ulong *args)
262 target_ulong reg = args[0];
263 target_ulong buf_list = args[1];
264 target_ulong rec_queue = args[2];
265 target_ulong filter_list = args[3];
266 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
267 VIOsPAPRVLANDevice *dev = (VIOsPAPRVLANDevice *)sdev;
268 vlan_bd_t filter_list_bd;
270 if (!dev) {
271 return H_PARAMETER;
274 if (dev->isopen) {
275 hcall_dprintf("H_REGISTER_LOGICAL_LAN called twice without "
276 "H_FREE_LOGICAL_LAN\n");
277 return H_RESOURCE;
280 if (check_bd(dev, VLAN_VALID_BD(buf_list, SPAPR_VIO_TCE_PAGE_SIZE),
281 SPAPR_VIO_TCE_PAGE_SIZE) < 0) {
282 hcall_dprintf("Bad buf_list 0x" TARGET_FMT_lx " for "
283 "H_REGISTER_LOGICAL_LAN\n", buf_list);
284 return H_PARAMETER;
287 filter_list_bd = VLAN_VALID_BD(filter_list, SPAPR_VIO_TCE_PAGE_SIZE);
288 if (check_bd(dev, filter_list_bd, SPAPR_VIO_TCE_PAGE_SIZE) < 0) {
289 hcall_dprintf("Bad filter_list 0x" TARGET_FMT_lx " for "
290 "H_REGISTER_LOGICAL_LAN\n", filter_list);
291 return H_PARAMETER;
294 if (!(rec_queue & VLAN_BD_VALID)
295 || (check_bd(dev, rec_queue, VLAN_RQ_ALIGNMENT) < 0)) {
296 hcall_dprintf("Bad receive queue for H_REGISTER_LOGICAL_LAN\n");
297 return H_PARAMETER;
300 dev->buf_list = buf_list;
301 sdev->signal_state = 0;
303 rec_queue &= ~VLAN_BD_TOGGLE;
305 /* Initialize the buffer list */
306 stq_tce(sdev, buf_list, rec_queue);
307 stq_tce(sdev, buf_list + 8, filter_list_bd);
308 spapr_tce_dma_zero(sdev, buf_list + VLAN_RX_BDS_OFF,
309 SPAPR_VIO_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF);
310 dev->add_buf_ptr = VLAN_RX_BDS_OFF - 8;
311 dev->use_buf_ptr = VLAN_RX_BDS_OFF - 8;
312 dev->rx_bufs = 0;
313 dev->rxq_ptr = 0;
315 /* Initialize the receive queue */
316 spapr_tce_dma_zero(sdev, VLAN_BD_ADDR(rec_queue), VLAN_BD_LEN(rec_queue));
318 dev->isopen = 1;
319 return H_SUCCESS;
323 static target_ulong h_free_logical_lan(CPUState *env, sPAPREnvironment *spapr,
324 target_ulong opcode, target_ulong *args)
326 target_ulong reg = args[0];
327 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
328 VIOsPAPRVLANDevice *dev = (VIOsPAPRVLANDevice *)sdev;
330 if (!dev) {
331 return H_PARAMETER;
334 if (!dev->isopen) {
335 hcall_dprintf("H_FREE_LOGICAL_LAN called without "
336 "H_REGISTER_LOGICAL_LAN\n");
337 return H_RESOURCE;
340 dev->buf_list = 0;
341 dev->rx_bufs = 0;
342 dev->isopen = 0;
343 return H_SUCCESS;
346 static target_ulong h_add_logical_lan_buffer(CPUState *env,
347 sPAPREnvironment *spapr,
348 target_ulong opcode,
349 target_ulong *args)
351 target_ulong reg = args[0];
352 target_ulong buf = args[1];
353 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
354 VIOsPAPRVLANDevice *dev = (VIOsPAPRVLANDevice *)sdev;
355 vlan_bd_t bd;
357 dprintf("H_ADD_LOGICAL_LAN_BUFFER(0x" TARGET_FMT_lx
358 ", 0x" TARGET_FMT_lx ")\n", reg, buf);
360 if (!sdev) {
361 hcall_dprintf("Wrong device in h_add_logical_lan_buffer\n");
362 return H_PARAMETER;
365 if ((check_bd(dev, buf, 4) < 0)
366 || (VLAN_BD_LEN(buf) < 16)) {
367 hcall_dprintf("Bad buffer enqueued in h_add_logical_lan_buffer\n");
368 return H_PARAMETER;
371 if (!dev->isopen || dev->rx_bufs >= VLAN_MAX_BUFS) {
372 return H_RESOURCE;
375 do {
376 dev->add_buf_ptr += 8;
377 if (dev->add_buf_ptr >= SPAPR_VIO_TCE_PAGE_SIZE) {
378 dev->add_buf_ptr = VLAN_RX_BDS_OFF;
381 bd = ldq_tce(sdev, dev->buf_list + dev->add_buf_ptr);
382 } while (bd & VLAN_BD_VALID);
384 stq_tce(sdev, dev->buf_list + dev->add_buf_ptr, buf);
386 dev->rx_bufs++;
388 dprintf("h_add_logical_lan_buffer(): Added buf ptr=%d rx_bufs=%d"
389 " bd=0x%016llx\n", dev->add_buf_ptr, dev->rx_bufs,
390 (unsigned long long)buf);
392 return H_SUCCESS;
395 static target_ulong h_send_logical_lan(CPUState *env, sPAPREnvironment *spapr,
396 target_ulong opcode, target_ulong *args)
398 target_ulong reg = args[0];
399 target_ulong *bufs = args + 1;
400 target_ulong continue_token = args[7];
401 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
402 VIOsPAPRVLANDevice *dev = (VIOsPAPRVLANDevice *)sdev;
403 unsigned total_len;
404 uint8_t *lbuf, *p;
405 int i, nbufs;
406 int ret;
408 dprintf("H_SEND_LOGICAL_LAN(0x" TARGET_FMT_lx ", <bufs>, 0x"
409 TARGET_FMT_lx ")\n", reg, continue_token);
411 if (!sdev) {
412 return H_PARAMETER;
415 dprintf("rxbufs = %d\n", dev->rx_bufs);
417 if (!dev->isopen) {
418 return H_DROPPED;
421 if (continue_token) {
422 return H_HARDWARE; /* FIXME actually handle this */
425 total_len = 0;
426 for (i = 0; i < 6; i++) {
427 dprintf(" buf desc: 0x" TARGET_FMT_lx "\n", bufs[i]);
428 if (!(bufs[i] & VLAN_BD_VALID)) {
429 break;
431 total_len += VLAN_BD_LEN(bufs[i]);
434 nbufs = i;
435 dprintf("h_send_logical_lan() %d buffers, total length 0x%x\n",
436 nbufs, total_len);
438 if (total_len == 0) {
439 return H_SUCCESS;
442 if (total_len > MAX_PACKET_SIZE) {
443 /* Don't let the guest force too large an allocation */
444 return H_RESOURCE;
447 lbuf = alloca(total_len);
448 p = lbuf;
449 for (i = 0; i < nbufs; i++) {
450 ret = spapr_tce_dma_read(sdev, VLAN_BD_ADDR(bufs[i]),
451 p, VLAN_BD_LEN(bufs[i]));
452 if (ret < 0) {
453 return ret;
456 p += VLAN_BD_LEN(bufs[i]);
459 qemu_send_packet(&dev->nic->nc, lbuf, total_len);
461 return H_SUCCESS;
464 static target_ulong h_multicast_ctrl(CPUState *env, sPAPREnvironment *spapr,
465 target_ulong opcode, target_ulong *args)
467 target_ulong reg = args[0];
468 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
470 if (!dev) {
471 return H_PARAMETER;
474 return H_SUCCESS;
477 static void vlan_hcalls(VIOsPAPRBus *bus)
479 spapr_register_hypercall(H_REGISTER_LOGICAL_LAN, h_register_logical_lan);
480 spapr_register_hypercall(H_FREE_LOGICAL_LAN, h_free_logical_lan);
481 spapr_register_hypercall(H_SEND_LOGICAL_LAN, h_send_logical_lan);
482 spapr_register_hypercall(H_ADD_LOGICAL_LAN_BUFFER,
483 h_add_logical_lan_buffer);
484 spapr_register_hypercall(H_MULTICAST_CTRL, h_multicast_ctrl);
487 static VIOsPAPRDeviceInfo spapr_vlan = {
488 .init = spapr_vlan_init,
489 .devnode = spapr_vlan_devnode,
490 .dt_name = "l-lan",
491 .dt_type = "network",
492 .dt_compatible = "IBM,l-lan",
493 .signal_mask = 0x1,
494 .hcalls = vlan_hcalls,
495 .qdev.name = "spapr-vlan",
496 .qdev.size = sizeof(VIOsPAPRVLANDevice),
497 .qdev.props = (Property[]) {
498 DEFINE_SPAPR_PROPERTIES(VIOsPAPRVLANDevice, sdev, 0x1000, 0x10000000),
499 DEFINE_NIC_PROPERTIES(VIOsPAPRVLANDevice, nicconf),
500 DEFINE_PROP_END_OF_LIST(),
504 static void spapr_vlan_register(void)
506 spapr_vio_bus_register_withprop(&spapr_vlan);
508 device_init(spapr_vlan_register);