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