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 "hw/hw.h" 28 #include "net/net.h" 29 #include "hw/qdev.h" 30 #include "hw/ppc/spapr.h" 31 #include "hw/ppc/spapr_vio.h" 32 #include "sysemu/sysemu.h" 33 34 #include <libfdt.h> 35 36 #define ETH_ALEN 6 37 #define MAX_PACKET_SIZE 65536 38 39 /*#define DEBUG*/ 40 41 #ifdef DEBUG 42 #define DPRINTF(fmt...) do { fprintf(stderr, fmt); } while (0) 43 #else 44 #define DPRINTF(fmt...) 45 #endif 46 47 /* 48 * Virtual LAN device 49 */ 50 51 typedef uint64_t vlan_bd_t; 52 53 #define VLAN_BD_VALID 0x8000000000000000ULL 54 #define VLAN_BD_TOGGLE 0x4000000000000000ULL 55 #define VLAN_BD_NO_CSUM 0x0200000000000000ULL 56 #define VLAN_BD_CSUM_GOOD 0x0100000000000000ULL 57 #define VLAN_BD_LEN_MASK 0x00ffffff00000000ULL 58 #define VLAN_BD_LEN(bd) (((bd) & VLAN_BD_LEN_MASK) >> 32) 59 #define VLAN_BD_ADDR_MASK 0x00000000ffffffffULL 60 #define VLAN_BD_ADDR(bd) ((bd) & VLAN_BD_ADDR_MASK) 61 62 #define VLAN_VALID_BD(addr, len) (VLAN_BD_VALID | \ 63 (((len) << 32) & VLAN_BD_LEN_MASK) | \ 64 (addr & VLAN_BD_ADDR_MASK)) 65 66 #define VLAN_RXQC_TOGGLE 0x80 67 #define VLAN_RXQC_VALID 0x40 68 #define VLAN_RXQC_NO_CSUM 0x02 69 #define VLAN_RXQC_CSUM_GOOD 0x01 70 71 #define VLAN_RQ_ALIGNMENT 16 72 #define VLAN_RXQ_BD_OFF 0 73 #define VLAN_FILTER_BD_OFF 8 74 #define VLAN_RX_BDS_OFF 16 75 #define VLAN_MAX_BUFS ((SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF) / 8) 76 77 #define TYPE_VIO_SPAPR_VLAN_DEVICE "spapr-vlan" 78 #define VIO_SPAPR_VLAN_DEVICE(obj) \ 79 OBJECT_CHECK(VIOsPAPRVLANDevice, (obj), TYPE_VIO_SPAPR_VLAN_DEVICE) 80 81 typedef struct VIOsPAPRVLANDevice { 82 VIOsPAPRDevice sdev; 83 NICConf nicconf; 84 NICState *nic; 85 bool isopen; 86 target_ulong buf_list; 87 uint32_t add_buf_ptr, use_buf_ptr, rx_bufs; 88 target_ulong rxq_ptr; 89 } VIOsPAPRVLANDevice; 90 91 static int spapr_vlan_can_receive(NetClientState *nc) 92 { 93 VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc); 94 95 return (dev->isopen && dev->rx_bufs > 0); 96 } 97 98 static ssize_t spapr_vlan_receive(NetClientState *nc, const uint8_t *buf, 99 size_t size) 100 { 101 VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc); 102 VIOsPAPRDevice *sdev = VIO_SPAPR_DEVICE(dev); 103 vlan_bd_t rxq_bd = vio_ldq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF); 104 vlan_bd_t bd; 105 int buf_ptr = dev->use_buf_ptr; 106 uint64_t handle; 107 uint8_t control; 108 109 DPRINTF("spapr_vlan_receive() [%s] rx_bufs=%d\n", sdev->qdev.id, 110 dev->rx_bufs); 111 112 if (!dev->isopen) { 113 return -1; 114 } 115 116 if (!dev->rx_bufs) { 117 return -1; 118 } 119 120 do { 121 buf_ptr += 8; 122 if (buf_ptr >= SPAPR_TCE_PAGE_SIZE) { 123 buf_ptr = VLAN_RX_BDS_OFF; 124 } 125 126 bd = vio_ldq(sdev, dev->buf_list + buf_ptr); 127 DPRINTF("use_buf_ptr=%d bd=0x%016llx\n", 128 buf_ptr, (unsigned long long)bd); 129 } while ((!(bd & VLAN_BD_VALID) || (VLAN_BD_LEN(bd) < (size + 8))) 130 && (buf_ptr != dev->use_buf_ptr)); 131 132 if (!(bd & VLAN_BD_VALID) || (VLAN_BD_LEN(bd) < (size + 8))) { 133 /* Failed to find a suitable buffer */ 134 return -1; 135 } 136 137 /* Remove the buffer from the pool */ 138 dev->rx_bufs--; 139 dev->use_buf_ptr = buf_ptr; 140 vio_stq(sdev, dev->buf_list + dev->use_buf_ptr, 0); 141 142 DPRINTF("Found buffer: ptr=%d num=%d\n", dev->use_buf_ptr, dev->rx_bufs); 143 144 /* Transfer the packet data */ 145 if (spapr_vio_dma_write(sdev, VLAN_BD_ADDR(bd) + 8, buf, size) < 0) { 146 return -1; 147 } 148 149 DPRINTF("spapr_vlan_receive: DMA write completed\n"); 150 151 /* Update the receive queue */ 152 control = VLAN_RXQC_TOGGLE | VLAN_RXQC_VALID; 153 if (rxq_bd & VLAN_BD_TOGGLE) { 154 control ^= VLAN_RXQC_TOGGLE; 155 } 156 157 handle = vio_ldq(sdev, VLAN_BD_ADDR(bd)); 158 vio_stq(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 8, handle); 159 vio_stl(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 4, size); 160 vio_sth(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 2, 8); 161 vio_stb(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr, control); 162 163 DPRINTF("wrote rxq entry (ptr=0x%llx): 0x%016llx 0x%016llx\n", 164 (unsigned long long)dev->rxq_ptr, 165 (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) + 166 dev->rxq_ptr), 167 (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) + 168 dev->rxq_ptr + 8)); 169 170 dev->rxq_ptr += 16; 171 if (dev->rxq_ptr >= VLAN_BD_LEN(rxq_bd)) { 172 dev->rxq_ptr = 0; 173 vio_stq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF, rxq_bd ^ VLAN_BD_TOGGLE); 174 } 175 176 if (sdev->signal_state & 1) { 177 qemu_irq_pulse(spapr_vio_qirq(sdev)); 178 } 179 180 return size; 181 } 182 183 static void spapr_vlan_cleanup(NetClientState *nc) 184 { 185 VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc); 186 187 dev->nic = NULL; 188 } 189 190 static NetClientInfo net_spapr_vlan_info = { 191 .type = NET_CLIENT_OPTIONS_KIND_NIC, 192 .size = sizeof(NICState), 193 .can_receive = spapr_vlan_can_receive, 194 .receive = spapr_vlan_receive, 195 .cleanup = spapr_vlan_cleanup, 196 }; 197 198 static void spapr_vlan_reset(VIOsPAPRDevice *sdev) 199 { 200 VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); 201 202 dev->buf_list = 0; 203 dev->rx_bufs = 0; 204 dev->isopen = 0; 205 } 206 207 static int spapr_vlan_init(VIOsPAPRDevice *sdev) 208 { 209 VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); 210 211 qemu_macaddr_default_if_unset(&dev->nicconf.macaddr); 212 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); 216 217 add_boot_device_path(dev->nicconf.bootindex, DEVICE(dev), ""); 218 219 return 0; 220 } 221 222 void spapr_vlan_create(VIOsPAPRBus *bus, NICInfo *nd) 223 { 224 DeviceState *dev; 225 226 dev = qdev_create(&bus->bus, "spapr-vlan"); 227 228 qdev_set_nic_properties(dev, nd); 229 230 qdev_init_nofail(dev); 231 } 232 233 static int spapr_vlan_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off) 234 { 235 VIOsPAPRVLANDevice *vdev = VIO_SPAPR_VLAN_DEVICE(dev); 236 uint8_t padded_mac[8] = {0, 0}; 237 int ret; 238 239 /* Some old phyp versions give the mac address in an 8-byte 240 * property. The kernel driver has an insane workaround for this; 241 * rather than doing the obvious thing and checking the property 242 * length, it checks whether the first byte has 0b10 in the low 243 * bits. If a correct 6-byte property has a different first byte 244 * the kernel will get the wrong mac address, overrunning its 245 * buffer in the process (read only, thank goodness). 246 * 247 * Here we workaround the kernel workaround by always supplying an 248 * 8-byte property, with the mac address in the last six bytes */ 249 memcpy(&padded_mac[2], &vdev->nicconf.macaddr, ETH_ALEN); 250 ret = fdt_setprop(fdt, node_off, "local-mac-address", 251 padded_mac, sizeof(padded_mac)); 252 if (ret < 0) { 253 return ret; 254 } 255 256 ret = fdt_setprop_cell(fdt, node_off, "ibm,mac-address-filters", 0); 257 if (ret < 0) { 258 return ret; 259 } 260 261 return 0; 262 } 263 264 static int check_bd(VIOsPAPRVLANDevice *dev, vlan_bd_t bd, 265 target_ulong alignment) 266 { 267 if ((VLAN_BD_ADDR(bd) % alignment) 268 || (VLAN_BD_LEN(bd) % alignment)) { 269 return -1; 270 } 271 272 if (!spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd), 273 VLAN_BD_LEN(bd), DMA_DIRECTION_FROM_DEVICE) 274 || !spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd), 275 VLAN_BD_LEN(bd), DMA_DIRECTION_TO_DEVICE)) { 276 return -1; 277 } 278 279 return 0; 280 } 281 282 static target_ulong h_register_logical_lan(PowerPCCPU *cpu, 283 sPAPREnvironment *spapr, 284 target_ulong opcode, 285 target_ulong *args) 286 { 287 target_ulong reg = args[0]; 288 target_ulong buf_list = args[1]; 289 target_ulong rec_queue = args[2]; 290 target_ulong filter_list = args[3]; 291 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg); 292 VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); 293 vlan_bd_t filter_list_bd; 294 295 if (!dev) { 296 return H_PARAMETER; 297 } 298 299 if (dev->isopen) { 300 hcall_dprintf("H_REGISTER_LOGICAL_LAN called twice without " 301 "H_FREE_LOGICAL_LAN\n"); 302 return H_RESOURCE; 303 } 304 305 if (check_bd(dev, VLAN_VALID_BD(buf_list, SPAPR_TCE_PAGE_SIZE), 306 SPAPR_TCE_PAGE_SIZE) < 0) { 307 hcall_dprintf("Bad buf_list 0x" TARGET_FMT_lx "\n", buf_list); 308 return H_PARAMETER; 309 } 310 311 filter_list_bd = VLAN_VALID_BD(filter_list, SPAPR_TCE_PAGE_SIZE); 312 if (check_bd(dev, filter_list_bd, SPAPR_TCE_PAGE_SIZE) < 0) { 313 hcall_dprintf("Bad filter_list 0x" TARGET_FMT_lx "\n", filter_list); 314 return H_PARAMETER; 315 } 316 317 if (!(rec_queue & VLAN_BD_VALID) 318 || (check_bd(dev, rec_queue, VLAN_RQ_ALIGNMENT) < 0)) { 319 hcall_dprintf("Bad receive queue\n"); 320 return H_PARAMETER; 321 } 322 323 dev->buf_list = buf_list; 324 sdev->signal_state = 0; 325 326 rec_queue &= ~VLAN_BD_TOGGLE; 327 328 /* Initialize the buffer list */ 329 vio_stq(sdev, buf_list, rec_queue); 330 vio_stq(sdev, buf_list + 8, filter_list_bd); 331 spapr_vio_dma_set(sdev, buf_list + VLAN_RX_BDS_OFF, 0, 332 SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF); 333 dev->add_buf_ptr = VLAN_RX_BDS_OFF - 8; 334 dev->use_buf_ptr = VLAN_RX_BDS_OFF - 8; 335 dev->rx_bufs = 0; 336 dev->rxq_ptr = 0; 337 338 /* Initialize the receive queue */ 339 spapr_vio_dma_set(sdev, VLAN_BD_ADDR(rec_queue), 0, VLAN_BD_LEN(rec_queue)); 340 341 dev->isopen = 1; 342 qemu_flush_queued_packets(qemu_get_queue(dev->nic)); 343 344 return H_SUCCESS; 345 } 346 347 348 static target_ulong h_free_logical_lan(PowerPCCPU *cpu, sPAPREnvironment *spapr, 349 target_ulong opcode, target_ulong *args) 350 { 351 target_ulong reg = args[0]; 352 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg); 353 VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); 354 355 if (!dev) { 356 return H_PARAMETER; 357 } 358 359 if (!dev->isopen) { 360 hcall_dprintf("H_FREE_LOGICAL_LAN called without " 361 "H_REGISTER_LOGICAL_LAN\n"); 362 return H_RESOURCE; 363 } 364 365 spapr_vlan_reset(sdev); 366 return H_SUCCESS; 367 } 368 369 static target_ulong h_add_logical_lan_buffer(PowerPCCPU *cpu, 370 sPAPREnvironment *spapr, 371 target_ulong opcode, 372 target_ulong *args) 373 { 374 target_ulong reg = args[0]; 375 target_ulong buf = args[1]; 376 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg); 377 VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); 378 vlan_bd_t bd; 379 380 DPRINTF("H_ADD_LOGICAL_LAN_BUFFER(0x" TARGET_FMT_lx 381 ", 0x" TARGET_FMT_lx ")\n", reg, buf); 382 383 if (!sdev) { 384 hcall_dprintf("Bad device\n"); 385 return H_PARAMETER; 386 } 387 388 if ((check_bd(dev, buf, 4) < 0) 389 || (VLAN_BD_LEN(buf) < 16)) { 390 hcall_dprintf("Bad buffer enqueued\n"); 391 return H_PARAMETER; 392 } 393 394 if (!dev->isopen || dev->rx_bufs >= VLAN_MAX_BUFS) { 395 return H_RESOURCE; 396 } 397 398 do { 399 dev->add_buf_ptr += 8; 400 if (dev->add_buf_ptr >= SPAPR_TCE_PAGE_SIZE) { 401 dev->add_buf_ptr = VLAN_RX_BDS_OFF; 402 } 403 404 bd = vio_ldq(sdev, dev->buf_list + dev->add_buf_ptr); 405 } while (bd & VLAN_BD_VALID); 406 407 vio_stq(sdev, dev->buf_list + dev->add_buf_ptr, buf); 408 409 dev->rx_bufs++; 410 411 qemu_flush_queued_packets(qemu_get_queue(dev->nic)); 412 413 DPRINTF("h_add_logical_lan_buffer(): Added buf ptr=%d rx_bufs=%d" 414 " bd=0x%016llx\n", dev->add_buf_ptr, dev->rx_bufs, 415 (unsigned long long)buf); 416 417 return H_SUCCESS; 418 } 419 420 static target_ulong h_send_logical_lan(PowerPCCPU *cpu, sPAPREnvironment *spapr, 421 target_ulong opcode, target_ulong *args) 422 { 423 target_ulong reg = args[0]; 424 target_ulong *bufs = args + 1; 425 target_ulong continue_token = args[7]; 426 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg); 427 VIOsPAPRVLANDevice *dev = VIO_SPAPR_VLAN_DEVICE(sdev); 428 unsigned total_len; 429 uint8_t *lbuf, *p; 430 int i, nbufs; 431 int ret; 432 433 DPRINTF("H_SEND_LOGICAL_LAN(0x" TARGET_FMT_lx ", <bufs>, 0x" 434 TARGET_FMT_lx ")\n", reg, continue_token); 435 436 if (!sdev) { 437 return H_PARAMETER; 438 } 439 440 DPRINTF("rxbufs = %d\n", dev->rx_bufs); 441 442 if (!dev->isopen) { 443 return H_DROPPED; 444 } 445 446 if (continue_token) { 447 return H_HARDWARE; /* FIXME actually handle this */ 448 } 449 450 total_len = 0; 451 for (i = 0; i < 6; i++) { 452 DPRINTF(" buf desc: 0x" TARGET_FMT_lx "\n", bufs[i]); 453 if (!(bufs[i] & VLAN_BD_VALID)) { 454 break; 455 } 456 total_len += VLAN_BD_LEN(bufs[i]); 457 } 458 459 nbufs = i; 460 DPRINTF("h_send_logical_lan() %d buffers, total length 0x%x\n", 461 nbufs, total_len); 462 463 if (total_len == 0) { 464 return H_SUCCESS; 465 } 466 467 if (total_len > MAX_PACKET_SIZE) { 468 /* Don't let the guest force too large an allocation */ 469 return H_RESOURCE; 470 } 471 472 lbuf = alloca(total_len); 473 p = lbuf; 474 for (i = 0; i < nbufs; i++) { 475 ret = spapr_vio_dma_read(sdev, VLAN_BD_ADDR(bufs[i]), 476 p, VLAN_BD_LEN(bufs[i])); 477 if (ret < 0) { 478 return ret; 479 } 480 481 p += VLAN_BD_LEN(bufs[i]); 482 } 483 484 qemu_send_packet(qemu_get_queue(dev->nic), lbuf, total_len); 485 486 return H_SUCCESS; 487 } 488 489 static target_ulong h_multicast_ctrl(PowerPCCPU *cpu, sPAPREnvironment *spapr, 490 target_ulong opcode, target_ulong *args) 491 { 492 target_ulong reg = args[0]; 493 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg); 494 495 if (!dev) { 496 return H_PARAMETER; 497 } 498 499 return H_SUCCESS; 500 } 501 502 static Property spapr_vlan_properties[] = { 503 DEFINE_SPAPR_PROPERTIES(VIOsPAPRVLANDevice, sdev), 504 DEFINE_NIC_PROPERTIES(VIOsPAPRVLANDevice, nicconf), 505 DEFINE_PROP_END_OF_LIST(), 506 }; 507 508 static const VMStateDescription vmstate_spapr_llan = { 509 .name = "spapr_llan", 510 .version_id = 1, 511 .minimum_version_id = 1, 512 .fields = (VMStateField[]) { 513 VMSTATE_SPAPR_VIO(sdev, VIOsPAPRVLANDevice), 514 /* LLAN state */ 515 VMSTATE_BOOL(isopen, VIOsPAPRVLANDevice), 516 VMSTATE_UINTTL(buf_list, VIOsPAPRVLANDevice), 517 VMSTATE_UINT32(add_buf_ptr, VIOsPAPRVLANDevice), 518 VMSTATE_UINT32(use_buf_ptr, VIOsPAPRVLANDevice), 519 VMSTATE_UINT32(rx_bufs, VIOsPAPRVLANDevice), 520 VMSTATE_UINTTL(rxq_ptr, VIOsPAPRVLANDevice), 521 522 VMSTATE_END_OF_LIST() 523 }, 524 }; 525 526 static void spapr_vlan_class_init(ObjectClass *klass, void *data) 527 { 528 DeviceClass *dc = DEVICE_CLASS(klass); 529 VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass); 530 531 k->init = spapr_vlan_init; 532 k->reset = spapr_vlan_reset; 533 k->devnode = spapr_vlan_devnode; 534 k->dt_name = "l-lan"; 535 k->dt_type = "network"; 536 k->dt_compatible = "IBM,l-lan"; 537 k->signal_mask = 0x1; 538 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 539 dc->props = spapr_vlan_properties; 540 k->rtce_window_size = 0x10000000; 541 dc->vmsd = &vmstate_spapr_llan; 542 } 543 544 static const TypeInfo spapr_vlan_info = { 545 .name = TYPE_VIO_SPAPR_VLAN_DEVICE, 546 .parent = TYPE_VIO_SPAPR_DEVICE, 547 .instance_size = sizeof(VIOsPAPRVLANDevice), 548 .class_init = spapr_vlan_class_init, 549 }; 550 551 static void spapr_vlan_register_types(void) 552 { 553 spapr_register_hypercall(H_REGISTER_LOGICAL_LAN, h_register_logical_lan); 554 spapr_register_hypercall(H_FREE_LOGICAL_LAN, h_free_logical_lan); 555 spapr_register_hypercall(H_SEND_LOGICAL_LAN, h_send_logical_lan); 556 spapr_register_hypercall(H_ADD_LOGICAL_LAN_BUFFER, 557 h_add_logical_lan_buffer); 558 spapr_register_hypercall(H_MULTICAST_CTRL, h_multicast_ctrl); 559 type_register_static(&spapr_vlan_info); 560 } 561 562 type_init(spapr_vlan_register_types) 563