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