1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <sys/modctl.h> 28 #include <sys/prom_plat.h> 29 #include <sys/ddi.h> 30 #include <sys/sunddi.h> 31 #include <sys/sunndi.h> 32 #include <sys/ndi_impldefs.h> 33 #include <sys/ddi_impldefs.h> 34 #include <sys/ethernet.h> 35 #include <sys/machsystm.h> 36 #include <sys/hypervisor_api.h> 37 #include <sys/mach_descrip.h> 38 #include <sys/drctl.h> 39 #include <sys/dr_util.h> 40 #include <sys/mac.h> 41 #include <sys/vnet.h> 42 #include <sys/vnet_mailbox.h> 43 #include <sys/vnet_common.h> 44 #include <sys/hsvc.h> 45 46 47 #define VDDS_MAX_RANGES 6 /* 6 possible VRs */ 48 #define VDDS_MAX_VRINTRS 8 /* limited to 8 intrs/VR */ 49 #define VDDS_MAX_INTR_NUM 64 /* 0-63 or valid */ 50 51 #define VDDS_INO_RANGE_START(x) (x * VDDS_MAX_VRINTRS) 52 #define HVCOOKIE(c) ((c) & 0xFFFFFFFFF) 53 #define NIUCFGHDL(c) ((c) >> 32) 54 55 56 /* For "ranges" property */ 57 typedef struct vdds_ranges { 58 uint32_t child_hi; 59 uint32_t child_lo; 60 uint32_t parent_hi; 61 uint32_t parent_lo; 62 uint32_t size_hi; 63 uint32_t size_lo; 64 } vdds_ranges_t; 65 66 /* For "reg" property */ 67 typedef struct vdds_reg { 68 uint32_t addr_hi; 69 uint32_t addr_lo; 70 uint32_t size_hi; 71 uint32_t size_lo; 72 } vdds_reg_t; 73 74 /* For ddi callback argument */ 75 typedef struct vdds_cb_arg { 76 dev_info_t *dip; 77 uint64_t cookie; 78 uint64_t macaddr; 79 uint32_t max_frame_size; 80 } vdds_cb_arg_t; 81 82 83 /* Functions exported to other files */ 84 void vdds_mod_init(void); 85 void vdds_mod_fini(void); 86 int vdds_init(vnet_t *vnetp); 87 void vdds_cleanup(vnet_t *vnetp); 88 void vdds_process_dds_msg(vnet_t *vnetp, vio_dds_msg_t *dmsg); 89 void vdds_cleanup_hybrid_res(void *arg); 90 void vdds_cleanup_hio(vnet_t *vnetp); 91 92 /* Support functions to create/destory Hybrid device */ 93 static dev_info_t *vdds_create_niu_node(uint64_t cookie, 94 uint64_t macaddr, uint32_t max_frame_size); 95 static int vdds_destroy_niu_node(dev_info_t *niu_dip, uint64_t cookie); 96 static dev_info_t *vdds_create_new_node(vdds_cb_arg_t *cba, 97 dev_info_t *pdip, int (*new_node_func)(dev_info_t *dip, 98 void *arg, uint_t flags)); 99 static int vdds_new_nexus_node(dev_info_t *dip, void *arg, uint_t flags); 100 static int vdds_new_niu_node(dev_info_t *dip, void *arg, uint_t flags); 101 static dev_info_t *vdds_find_node(uint64_t cookie, dev_info_t *sdip, 102 int (*match_func)(dev_info_t *dip, void *arg)); 103 static int vdds_match_niu_nexus(dev_info_t *dip, void *arg); 104 static int vdds_match_niu_node(dev_info_t *dip, void *arg); 105 static int vdds_get_interrupts(uint64_t cookie, int ino_range, 106 int *intrs, int *nintr); 107 108 /* DDS message processing related functions */ 109 static void vdds_process_dds_msg_task(void *arg); 110 static int vdds_send_dds_resp_msg(vnet_t *vnetp, vio_dds_msg_t *dmsg, int ack); 111 static int vdds_send_dds_rel_msg(vnet_t *vnetp); 112 static void vdds_release_range_prop(dev_info_t *nexus_dip, uint64_t cookie); 113 114 /* Functions imported from other files */ 115 extern int vnet_send_dds_msg(vnet_t *vnetp, void *dmsg); 116 extern int vnet_hio_mac_init(vnet_t *vnetp, char *ifname); 117 extern void vnet_hio_mac_cleanup(vnet_t *vnetp); 118 119 /* HV functions that are used in this file */ 120 extern uint64_t vdds_hv_niu_vr_getinfo(uint32_t hvcookie, 121 uint64_t *real_start, uint64_t *size); 122 extern uint64_t vdds_hv_niu_vr_get_txmap(uint32_t hvcookie, uint64_t *dma_map); 123 extern uint64_t vdds_hv_niu_vr_get_rxmap(uint32_t hvcookie, uint64_t *dma_map); 124 extern uint64_t vdds_hv_niu_vrtx_set_ino(uint32_t cookie, uint64_t vch_idx, 125 uint32_t ino); 126 extern uint64_t vdds_hv_niu_vrrx_set_ino(uint32_t cookie, uint64_t vch_idx, 127 uint32_t ino); 128 129 130 #ifdef DEBUG 131 132 extern int vnet_dbglevel; 133 134 static void 135 debug_printf(const char *fname, void *arg, const char *fmt, ...) 136 { 137 char buf[512]; 138 va_list ap; 139 char *bufp = buf; 140 vnet_dds_info_t *vdds = arg; 141 142 if (vdds != NULL) { 143 (void) sprintf(bufp, "vnet%d: %s: ", 144 vdds->vnetp->instance, fname); 145 } else { 146 (void) sprintf(bufp, "%s: ", fname); 147 } 148 bufp += strlen(bufp); 149 va_start(ap, fmt); 150 (void) vsprintf(bufp, fmt, ap); 151 va_end(ap); 152 cmn_err(CE_CONT, "%s\n", buf); 153 } 154 #endif 155 156 /* 157 * Hypervisor N2/NIU services information. 158 */ 159 static hsvc_info_t niu_hsvc = { 160 HSVC_REV_1, NULL, HSVC_GROUP_NIU, 1, 1, "vnet_dds" 161 }; 162 163 /* 164 * Lock to serialize the NIU device node related operations. 165 */ 166 kmutex_t vdds_dev_lock; 167 168 boolean_t vdds_hv_hio_capable = B_FALSE; 169 170 /* 171 * vdds_mod_init -- one time initialization. 172 */ 173 void 174 vdds_mod_init(void) 175 { 176 int rv; 177 uint64_t minor; 178 179 rv = hsvc_register(&niu_hsvc, &minor); 180 /* 181 * Only HV version 1.1 is capable of NIU Hybrid IO. 182 */ 183 if ((rv == 0) && (minor == 1)) { 184 vdds_hv_hio_capable = B_TRUE; 185 } 186 mutex_init(&vdds_dev_lock, NULL, MUTEX_DRIVER, NULL); 187 DBG1(NULL, "HV HIO capable"); 188 } 189 190 /* 191 * vdds_mod_fini -- one time cleanup. 192 */ 193 void 194 vdds_mod_fini(void) 195 { 196 (void) hsvc_unregister(&niu_hsvc); 197 mutex_destroy(&vdds_dev_lock); 198 } 199 200 /* 201 * vdds_init -- vnet instance related DDS related initialization. 202 */ 203 int 204 vdds_init(vnet_t *vnetp) 205 { 206 vnet_dds_info_t *vdds = &vnetp->vdds_info; 207 char qname[TASKQ_NAMELEN]; 208 209 vdds->vnetp = vnetp; 210 DBG1(vdds, "Initializing.."); 211 (void) snprintf(qname, TASKQ_NAMELEN, "vdds_taskq%d", vnetp->instance); 212 if ((vdds->dds_taskqp = ddi_taskq_create(vnetp->dip, qname, 1, 213 TASKQ_DEFAULTPRI, 0)) == NULL) { 214 cmn_err(CE_WARN, "!vnet%d: Unable to create DDS task queue", 215 vnetp->instance); 216 return (ENOMEM); 217 } 218 mutex_init(&vdds->lock, NULL, MUTEX_DRIVER, NULL); 219 return (0); 220 } 221 222 /* 223 * vdds_cleanup -- vnet instance related cleanup. 224 */ 225 void 226 vdds_cleanup(vnet_t *vnetp) 227 { 228 vnet_dds_info_t *vdds = &vnetp->vdds_info; 229 230 DBG1(vdds, "Cleanup..."); 231 /* Cleanup/destroy any hybrid resouce that exists */ 232 vdds_cleanup_hybrid_res(vnetp); 233 234 /* taskq_destroy will wait for all taskqs to complete */ 235 ddi_taskq_destroy(vdds->dds_taskqp); 236 vdds->dds_taskqp = NULL; 237 mutex_destroy(&vdds->lock); 238 DBG1(vdds, "Cleanup complete"); 239 } 240 241 /* 242 * vdds_cleanup_hybrid_res -- Cleanup Hybrid resource. 243 */ 244 void 245 vdds_cleanup_hybrid_res(void *arg) 246 { 247 vnet_t *vnetp = arg; 248 vnet_dds_info_t *vdds = &vnetp->vdds_info; 249 250 DBG1(vdds, "Hybrid device cleanup..."); 251 mutex_enter(&vdds->lock); 252 if (vdds->task_flags == VNET_DDS_TASK_ADD_SHARE) { 253 /* 254 * Task for ADD_SHARE is pending, simply 255 * cleanup the flags, the task will quit without 256 * any changes. 257 */ 258 vdds->task_flags = 0; 259 DBG2(vdds, "Task for ADD is pending, clean flags only"); 260 } else if ((vdds->hio_dip != NULL) && (vdds->task_flags == 0)) { 261 /* 262 * There is no task pending and a hybrid device 263 * is present, so dispatch a task to release the share. 264 */ 265 vdds->task_flags = VNET_DDS_TASK_REL_SHARE; 266 (void) ddi_taskq_dispatch(vdds->dds_taskqp, 267 vdds_process_dds_msg_task, vnetp, DDI_NOSLEEP); 268 DBG2(vdds, "Dispatched a task to destroy HIO device"); 269 } 270 /* 271 * Other possible cases include either DEL_SHARE or 272 * REL_SHARE as pending. In that case, there is nothing 273 * to do as a task is already pending to do the cleanup. 274 */ 275 mutex_exit(&vdds->lock); 276 DBG1(vdds, "Hybrid device cleanup complete"); 277 } 278 279 /* 280 * vdds_cleanup_hio -- An interface to cleanup the hio resources before 281 * resetting the vswitch port. 282 */ 283 void 284 vdds_cleanup_hio(vnet_t *vnetp) 285 { 286 vnet_dds_info_t *vdds = &vnetp->vdds_info; 287 288 /* Wait for any pending vdds tasks to complete */ 289 ddi_taskq_wait(vdds->dds_taskqp); 290 vdds_cleanup_hybrid_res(vnetp); 291 /* Wait for the cleanup task to complete */ 292 ddi_taskq_wait(vdds->dds_taskqp); 293 } 294 295 /* 296 * vdds_process_dds_msg -- Process a DDS message. 297 */ 298 void 299 vdds_process_dds_msg(vnet_t *vnetp, vio_dds_msg_t *dmsg) 300 { 301 vnet_dds_info_t *vdds = &vnetp->vdds_info; 302 int rv; 303 304 DBG1(vdds, "DDS message received..."); 305 306 if (dmsg->dds_class != DDS_VNET_NIU) { 307 DBG2(vdds, "Invalid class send NACK"); 308 (void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE); 309 return; 310 } 311 mutex_enter(&vdds->lock); 312 switch (dmsg->dds_subclass) { 313 case DDS_VNET_ADD_SHARE: 314 DBG2(vdds, "DDS_VNET_ADD_SHARE message..."); 315 if ((vdds->task_flags != 0) || (vdds->hio_dip != NULL)) { 316 /* 317 * Either a task is already pending or 318 * a hybrid device already exists. 319 */ 320 DWARN(vdds, "NACK: Already pending DDS task"); 321 (void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE); 322 mutex_exit(&vdds->lock); 323 return; 324 } 325 vdds->task_flags = VNET_DDS_TASK_ADD_SHARE; 326 bcopy(dmsg, &vnetp->vdds_info.dmsg, sizeof (vio_dds_msg_t)); 327 DBG2(vdds, "Dispatching task for ADD_SHARE"); 328 rv = ddi_taskq_dispatch(vdds->dds_taskqp, 329 vdds_process_dds_msg_task, vnetp, DDI_NOSLEEP); 330 if (rv != 0) { 331 /* Send NACK */ 332 DBG2(vdds, "NACK: Failed to dispatch task"); 333 (void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE); 334 vdds->task_flags = 0; 335 } 336 break; 337 338 case DDS_VNET_DEL_SHARE: 339 DBG2(vdds, "DDS_VNET_DEL_SHARE message..."); 340 if (vdds->task_flags == VNET_DDS_TASK_ADD_SHARE) { 341 /* 342 * ADD_SHARE task still pending, simply clear 343 * task falgs and ACK. 344 */ 345 DBG2(vdds, "ACK:ADD_SHARE task still pending"); 346 vdds->task_flags = 0; 347 (void) vdds_send_dds_resp_msg(vnetp, dmsg, B_TRUE); 348 mutex_exit(&vdds->lock); 349 return; 350 } 351 if ((vdds->task_flags == 0) && (vdds->hio_dip == NULL)) { 352 /* Send NACK */ 353 DBG2(vdds, "NACK:No HIO device exists"); 354 (void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE); 355 mutex_exit(&vdds->lock); 356 return; 357 } 358 vdds->task_flags = VNET_DDS_TASK_DEL_SHARE; 359 bcopy(dmsg, &vdds->dmsg, sizeof (vio_dds_msg_t)); 360 DBG2(vdds, "Dispatching DEL_SHARE task"); 361 rv = ddi_taskq_dispatch(vdds->dds_taskqp, 362 vdds_process_dds_msg_task, vnetp, DDI_NOSLEEP); 363 if (rv != 0) { 364 /* Send NACK */ 365 DBG2(vdds, "NACK: failed to dispatch task"); 366 (void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE); 367 vdds->task_flags = 0; 368 } 369 break; 370 case DDS_VNET_REL_SHARE: 371 DBG2(vdds, "Reply for REL_SHARE reply=%d", 372 dmsg->tag.vio_subtype); 373 break; 374 default: 375 DWARN(vdds, "Discarding Unknown DDS message"); 376 break; 377 } 378 mutex_exit(&vdds->lock); 379 } 380 381 /* 382 * vdds_process_dds_msg_task -- Called from a taskq to process the 383 * DDS message. 384 */ 385 static void 386 vdds_process_dds_msg_task(void *arg) 387 { 388 vnet_t *vnetp = arg; 389 vnet_dds_info_t *vdds = &vnetp->vdds_info; 390 vio_dds_msg_t *dmsg = &vdds->dmsg; 391 dev_info_t *dip; 392 uint32_t max_frame_size; 393 uint64_t hio_cookie; 394 int rv; 395 396 DBG1(vdds, "DDS task started..."); 397 mutex_enter(&vdds->lock); 398 switch (vdds->task_flags) { 399 case VNET_DDS_TASK_ADD_SHARE: 400 DBG2(vdds, "ADD_SHARE task..."); 401 hio_cookie = dmsg->msg.share_msg.cookie; 402 /* 403 * max-frame-size value need to be set to 404 * the full ethernet frame size. That is, 405 * header + payload + checksum. 406 */ 407 max_frame_size = vnetp->mtu + 408 sizeof (struct ether_vlan_header) + ETHERFCSL; 409 dip = vdds_create_niu_node(hio_cookie, 410 dmsg->msg.share_msg.macaddr, max_frame_size); 411 if (dip == NULL) { 412 (void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE); 413 DERR(vdds, "Failed to create HIO node"); 414 } else { 415 vdds->hio_dip = dip; 416 vdds->hio_cookie = hio_cookie; 417 (void) snprintf(vdds->hio_ifname, 418 sizeof (vdds->hio_ifname), "%s%d", 419 ddi_driver_name(dip), ddi_get_instance(dip)); 420 421 rv = vnet_hio_mac_init(vnetp, vdds->hio_ifname); 422 if (rv != 0) { 423 /* failed - cleanup, send failed DDS message */ 424 DERR(vdds, "HIO mac init failed, cleaning up"); 425 rv = vdds_destroy_niu_node(dip, hio_cookie); 426 if (rv == 0) { 427 /* use DERR to print by default */ 428 DERR(vdds, "Successfully destroyed" 429 " Hybrid node"); 430 } else { 431 cmn_err(CE_WARN, "vnet%d:Failed to " 432 "destroy Hybrid node", 433 vnetp->instance); 434 } 435 vdds->hio_dip = NULL; 436 vdds->hio_cookie = 0; 437 (void) vdds_send_dds_resp_msg(vnetp, 438 dmsg, B_FALSE); 439 } else { 440 (void) vdds_send_dds_resp_msg(vnetp, 441 dmsg, B_TRUE); 442 } 443 /* DERR used only print by default */ 444 DERR(vdds, "Successfully created HIO node"); 445 } 446 break; 447 448 case VNET_DDS_TASK_DEL_SHARE: 449 DBG2(vdds, "DEL_SHARE task..."); 450 if (vnetp->vdds_info.hio_dip == NULL) { 451 DBG2(vdds, "NACK: No HIO device destroy"); 452 (void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE); 453 } else { 454 vnet_hio_mac_cleanup(vnetp); 455 rv = vdds_destroy_niu_node(vnetp->vdds_info.hio_dip, 456 vdds->hio_cookie); 457 if (rv == 0) { 458 /* use DERR to print by default */ 459 DERR(vdds, "Successfully destroyed" 460 " Hybrid node"); 461 } else { 462 cmn_err(CE_WARN, "vnet%d:Failed to " 463 "destroy Hybrid node", vnetp->instance); 464 } 465 /* TODO: send ACK even for failure? */ 466 DBG2(vdds, "ACK: HIO device destroyed"); 467 (void) vdds_send_dds_resp_msg(vnetp, dmsg, B_TRUE); 468 vdds->hio_dip = 0; 469 vdds->hio_cookie = 0; 470 } 471 break; 472 case VNET_DDS_TASK_REL_SHARE: 473 DBG2(vdds, "REL_SHARE task..."); 474 if (vnetp->vdds_info.hio_dip != NULL) { 475 vnet_hio_mac_cleanup(vnetp); 476 rv = vdds_destroy_niu_node(vnetp->vdds_info.hio_dip, 477 vdds->hio_cookie); 478 if (rv == 0) { 479 DERR(vdds, "Successfully destroyed " 480 "Hybrid node"); 481 } else { 482 cmn_err(CE_WARN, "vnet%d:Failed to " 483 "destroy HIO node", vnetp->instance); 484 } 485 /* TODO: failure case */ 486 (void) vdds_send_dds_rel_msg(vnetp); 487 vdds->hio_dip = 0; 488 vdds->hio_cookie = 0; 489 } 490 break; 491 default: 492 break; 493 } 494 vdds->task_flags = 0; 495 mutex_exit(&vdds->lock); 496 } 497 498 /* 499 * vdds_send_dds_rel_msg -- Send a DDS_REL_SHARE message. 500 */ 501 static int 502 vdds_send_dds_rel_msg(vnet_t *vnetp) 503 { 504 vnet_dds_info_t *vdds = &vnetp->vdds_info; 505 vio_dds_msg_t vmsg; 506 dds_share_msg_t *smsg = &vmsg.msg.share_msg; 507 int rv; 508 509 DBG1(vdds, "Sending DDS_VNET_REL_SHARE message"); 510 vmsg.tag.vio_msgtype = VIO_TYPE_CTRL; 511 vmsg.tag.vio_subtype = VIO_SUBTYPE_INFO; 512 vmsg.tag.vio_subtype_env = VIO_DDS_INFO; 513 /* vio_sid filled by the LDC module */ 514 vmsg.dds_class = DDS_VNET_NIU; 515 vmsg.dds_subclass = DDS_VNET_REL_SHARE; 516 vmsg.dds_req_id = (++vdds->dds_req_id); 517 smsg->macaddr = vnet_macaddr_strtoul(vnetp->curr_macaddr); 518 smsg->cookie = vdds->hio_cookie; 519 rv = vnet_send_dds_msg(vnetp, &vmsg); 520 return (rv); 521 } 522 523 /* 524 * vdds_send_dds_resp_msg -- Send a DDS response message. 525 */ 526 static int 527 vdds_send_dds_resp_msg(vnet_t *vnetp, vio_dds_msg_t *dmsg, int ack) 528 { 529 vnet_dds_info_t *vdds = &vnetp->vdds_info; 530 int rv; 531 532 DBG1(vdds, "Sending a response mesage=%d", ack); 533 if (ack == B_TRUE) { 534 dmsg->tag.vio_subtype = VIO_SUBTYPE_ACK; 535 dmsg->msg.share_resp_msg.status = DDS_VNET_SUCCESS; 536 } else { 537 dmsg->tag.vio_subtype = VIO_SUBTYPE_NACK; 538 dmsg->msg.share_resp_msg.status = DDS_VNET_FAIL; 539 } 540 rv = vnet_send_dds_msg(vnetp, dmsg); 541 return (rv); 542 } 543 544 /* 545 * vdds_create_niu_node -- Create NIU Hybrid node. The NIU nexus 546 * node also created if it doesn't exist already. 547 */ 548 dev_info_t * 549 vdds_create_niu_node(uint64_t cookie, uint64_t macaddr, uint32_t max_frame_size) 550 { 551 dev_info_t *nexus_dip; 552 dev_info_t *niu_dip; 553 vdds_cb_arg_t cba; 554 555 DBG1(NULL, "Called"); 556 557 if (vdds_hv_hio_capable == B_FALSE) { 558 return (NULL); 559 } 560 mutex_enter(&vdds_dev_lock); 561 /* Check if the nexus node exists already */ 562 nexus_dip = vdds_find_node(cookie, ddi_root_node(), 563 vdds_match_niu_nexus); 564 if (nexus_dip == NULL) { 565 /* 566 * NIU nexus node not found, so create it now. 567 */ 568 cba.dip = NULL; 569 cba.cookie = cookie; 570 cba.macaddr = macaddr; 571 cba.max_frame_size = max_frame_size; 572 nexus_dip = vdds_create_new_node(&cba, NULL, 573 vdds_new_nexus_node); 574 if (nexus_dip == NULL) { 575 mutex_exit(&vdds_dev_lock); 576 return (NULL); 577 } 578 } 579 DBG2(NULL, "nexus_dip = 0x%p", nexus_dip); 580 581 /* Check if NIU node exists already before creating one */ 582 niu_dip = vdds_find_node(cookie, nexus_dip, 583 vdds_match_niu_node); 584 if (niu_dip == NULL) { 585 cba.dip = NULL; 586 cba.cookie = cookie; 587 cba.macaddr = macaddr; 588 cba.max_frame_size = max_frame_size; 589 niu_dip = vdds_create_new_node(&cba, nexus_dip, 590 vdds_new_niu_node); 591 /* 592 * Hold the niu_dip to prevent it from 593 * detaching. 594 */ 595 if (niu_dip != NULL) { 596 e_ddi_hold_devi(niu_dip); 597 } else { 598 DWARN(NULL, "niumx/network node creation failed"); 599 } 600 } else { 601 DWARN(NULL, "niumx/network node already exists(dip=0x%p)", 602 niu_dip); 603 } 604 /* release the hold that was done in find/create */ 605 if ((niu_dip != NULL) && (e_ddi_branch_held(niu_dip))) 606 e_ddi_branch_rele(niu_dip); 607 if (e_ddi_branch_held(nexus_dip)) 608 e_ddi_branch_rele(nexus_dip); 609 mutex_exit(&vdds_dev_lock); 610 DBG1(NULL, "returning niu_dip=0x%p", niu_dip); 611 return (niu_dip); 612 } 613 614 /* 615 * vdds_destroy_niu_node -- Destroy the NIU node. 616 */ 617 int 618 vdds_destroy_niu_node(dev_info_t *niu_dip, uint64_t cookie) 619 { 620 int rv; 621 dev_info_t *fdip = NULL; 622 dev_info_t *nexus_dip = ddi_get_parent(niu_dip); 623 624 625 DBG1(NULL, "Called"); 626 ASSERT(nexus_dip != NULL); 627 mutex_enter(&vdds_dev_lock); 628 629 if (!e_ddi_branch_held(niu_dip)) 630 e_ddi_branch_hold(niu_dip); 631 /* 632 * As we are destroying now, release the 633 * hold that was done in during the creation. 634 */ 635 ddi_release_devi(niu_dip); 636 rv = e_ddi_branch_destroy(niu_dip, &fdip, 0); 637 if (rv != 0) { 638 DERR(NULL, "Failed to destroy niumx/network node dip=0x%p", 639 niu_dip); 640 if (fdip != NULL) { 641 ddi_release_devi(fdip); 642 } 643 rv = EBUSY; 644 goto dest_exit; 645 } 646 /* 647 * Cleanup the parent's ranges property set 648 * for this Hybrid device. 649 */ 650 vdds_release_range_prop(nexus_dip, cookie); 651 652 dest_exit: 653 mutex_exit(&vdds_dev_lock); 654 DBG1(NULL, "returning rv=%d", rv); 655 return (rv); 656 } 657 658 /* 659 * vdds_match_niu_nexus -- callback function to verify a node is the 660 * NIU nexus node. 661 */ 662 static int 663 vdds_match_niu_nexus(dev_info_t *dip, void *arg) 664 { 665 vdds_cb_arg_t *warg = (vdds_cb_arg_t *)arg; 666 vdds_reg_t *reg_p; 667 char *name; 668 uint64_t hdl; 669 uint_t reglen; 670 int rv; 671 672 if (dip == ddi_root_node()) { 673 return (DDI_WALK_CONTINUE); 674 } 675 676 name = ddi_node_name(dip); 677 if (strcmp(name, "niu") != 0) { 678 return (DDI_WALK_CONTINUE); 679 } 680 rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, 681 DDI_PROP_DONTPASS, "reg", (int **)®_p, ®len); 682 if (rv != DDI_PROP_SUCCESS) { 683 DWARN(NULL, "Failed to get reg property dip=0x%p", dip); 684 return (DDI_WALK_CONTINUE); 685 } 686 687 hdl = reg_p->addr_hi & 0x0FFFFFFF; 688 ddi_prop_free(reg_p); 689 690 DBG2(NULL, "Handle = 0x%lx dip=0x%p", hdl, dip); 691 if (hdl == NIUCFGHDL(warg->cookie)) { 692 /* Hold before returning */ 693 if (!e_ddi_branch_held(dip)) 694 e_ddi_branch_hold(dip); 695 warg->dip = dip; 696 DBG2(NULL, "Found dip = 0x%p", dip); 697 return (DDI_WALK_TERMINATE); 698 } 699 return (DDI_WALK_CONTINUE); 700 } 701 702 /* 703 * vdds_match_niu_node -- callback function to verify a node is the 704 * NIU Hybrid node. 705 */ 706 static int 707 vdds_match_niu_node(dev_info_t *dip, void *arg) 708 { 709 vdds_cb_arg_t *warg = (vdds_cb_arg_t *)arg; 710 char *name; 711 vdds_reg_t *reg_p; 712 uint_t reglen; 713 int rv; 714 uint32_t addr_hi; 715 716 name = ddi_node_name(dip); 717 if (strcmp(name, "network") != 0) { 718 return (DDI_WALK_CONTINUE); 719 } 720 rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, 721 DDI_PROP_DONTPASS, "reg", (int **)®_p, ®len); 722 if (rv != DDI_PROP_SUCCESS) { 723 DWARN(NULL, "Failed to get reg property dip=0x%p", dip); 724 return (DDI_WALK_CONTINUE); 725 } 726 727 addr_hi = reg_p->addr_hi; 728 DBG1(NULL, "addr_hi = 0x%x dip=0x%p", addr_hi, dip); 729 ddi_prop_free(reg_p); 730 if (addr_hi == HVCOOKIE(warg->cookie)) { 731 warg->dip = dip; 732 if (!e_ddi_branch_held(dip)) 733 e_ddi_branch_hold(dip); 734 DBG1(NULL, "Found dip = 0x%p", dip); 735 return (DDI_WALK_TERMINATE); 736 } 737 return (DDI_WALK_CONTINUE); 738 } 739 740 /* 741 * vdds_new_nexus_node -- callback function to set all the properties 742 * a new NIU nexus node. 743 */ 744 static int 745 vdds_new_nexus_node(dev_info_t *dip, void *arg, uint_t flags) 746 { 747 vdds_cb_arg_t *cba = (vdds_cb_arg_t *)arg; 748 char *compat[] = { "SUNW,niumx" }; 749 vdds_ranges_t *rangesp; 750 vdds_reg_t reg; 751 uint64_t nranges; 752 int n; 753 754 DBG1(NULL, "Called dip=0x%p, flags=0x%X", dip, flags); 755 756 /* create "niu" property */ 757 if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, "name", "niu") != 758 DDI_SUCCESS) { 759 DERR(NULL, "Failed to create name property(dip=0x%p)", dip); 760 return (DDI_WALK_ERROR); 761 } 762 763 /* create "compatible" property */ 764 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, dip, "compatible", 765 compat, 1) != DDI_SUCCESS) { 766 DERR(NULL, "Failed to create compatible property(dip=0x%p)", 767 dip); 768 return (DDI_WALK_ERROR); 769 } 770 771 /* create "device_type" property */ 772 if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, 773 "device_type", "sun4v") != DDI_SUCCESS) { 774 DERR(NULL, "Failed to create device_type property(dip=0x%p)", 775 dip); 776 return (DDI_WALK_ERROR); 777 } 778 779 /* 780 * create "reg" property. The first 28 bits of 781 * 'addr_hi' are NIU cfg_handle, the 0xc in 28-31 bits 782 * indicates non-cacheable config. 783 */ 784 reg.addr_hi = 0xc0000000 | NIUCFGHDL(cba->cookie); 785 reg.addr_lo = 0; 786 reg.size_hi = 0; 787 reg.size_lo = 0; 788 if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, 789 "reg", (int *)®, sizeof (reg)/sizeof (int)) != DDI_SUCCESS) { 790 DERR(NULL, "Failed to create reg property(dip=0x%p)", dip); 791 return (DDI_WALK_ERROR); 792 } 793 794 /* 795 * Create VDDS_MAX_RANGES so that they are already in place 796 * before the children are created. While creating the child 797 * we just modify one of this ranges entries. 798 */ 799 nranges = VDDS_MAX_RANGES; /* One range for each VR */ 800 rangesp = (vdds_ranges_t *)kmem_zalloc( 801 (sizeof (vdds_ranges_t) * nranges), KM_SLEEP); 802 803 for (n = 0; n < nranges; n++) { 804 /* zero all child_hi/lo */ 805 rangesp[n].child_hi = 0; 806 rangesp[n].child_lo = 0; 807 } 808 809 if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges", 810 (int *)rangesp, (nranges * 6)) != DDI_SUCCESS) { 811 DERR(NULL, "Failed to create ranges property(dip=0x%p)", dip); 812 kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges)); 813 return (DDI_WALK_ERROR); 814 } 815 816 /* create "#size-cells" property */ 817 if (ndi_prop_update_int(DDI_DEV_T_NONE, dip, 818 "#size-cells", 2) != DDI_SUCCESS) { 819 DERR(NULL, "Failed to create #size-cells property(dip=0x%p)", 820 dip); 821 kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges)); 822 return (DDI_WALK_ERROR); 823 } 824 825 /* create "#address-cells" property */ 826 if (ndi_prop_update_int(DDI_DEV_T_NONE, dip, 827 "#address-cells", 2) != DDI_SUCCESS) { 828 DERR(NULL, "Failed to create #address-cells prop(dip=0x%p)", 829 dip); 830 kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges)); 831 return (DDI_WALK_ERROR); 832 } 833 834 kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges)); 835 cba->dip = dip; 836 DBG1(NULL, "Returning (dip=0x%p)", dip); 837 return (DDI_WALK_TERMINATE); 838 } 839 840 /* 841 * vdds_new_niu_node -- callback function to create a new NIU Hybrid node. 842 */ 843 static int 844 vdds_new_niu_node(dev_info_t *dip, void *arg, uint_t flags) 845 { 846 vdds_cb_arg_t *cba = (vdds_cb_arg_t *)arg; 847 char *compat[] = { "SUNW,niusl" }; 848 uint8_t macaddrbytes[ETHERADDRL]; 849 int interrupts[VDDS_MAX_VRINTRS]; 850 vdds_ranges_t *prng; 851 vdds_ranges_t *prp; 852 vdds_reg_t reg; 853 dev_info_t *pdip; 854 uint64_t start; 855 uint64_t size; 856 int prnglen; 857 int nintr = 0; 858 int nrng; 859 int rnum; 860 int rv; 861 862 DBG1(NULL, "Called dip=0x%p flags=0x%X", dip, flags); 863 pdip = ddi_get_parent(dip); 864 865 if (pdip == NULL) { 866 DWARN(NULL, "Failed to get parent dip(dip=0x%p)", dip); 867 return (DDI_WALK_ERROR); 868 } 869 870 /* create "network" property */ 871 if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, "name", "network") != 872 DDI_SUCCESS) { 873 DERR(NULL, "Failed to create name property(dip=0x%p)", dip); 874 return (DDI_WALK_ERROR); 875 } 876 877 /* 878 * create "niutype" property, it is set to n2niu to 879 * indicate NIU Hybrid node. 880 */ 881 if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, "niutype", 882 "n2niu") != DDI_SUCCESS) { 883 DERR(NULL, "Failed to create niuopmode property(dip=0x%p)", 884 dip); 885 return (DDI_WALK_ERROR); 886 } 887 888 /* create "compatible" property */ 889 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, dip, "compatible", 890 compat, 1) != DDI_SUCCESS) { 891 DERR(NULL, "Failed to create compatible property(dip=0x%p)", 892 dip); 893 return (DDI_WALK_ERROR); 894 } 895 896 /* create "device_type" property */ 897 if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, 898 "device_type", "network") != DDI_SUCCESS) { 899 DERR(NULL, "Failed to create device_type property(dip=0x%p)", 900 dip); 901 return (DDI_WALK_ERROR); 902 } 903 904 /* create "reg" property */ 905 if (vdds_hv_niu_vr_getinfo(HVCOOKIE(cba->cookie), 906 &start, &size) != H_EOK) { 907 DERR(NULL, "Failed to get vrinfo for cookie(0x%lX)", 908 cba->cookie); 909 return (DDI_WALK_ERROR); 910 } 911 reg.addr_hi = HVCOOKIE(cba->cookie); 912 reg.addr_lo = 0; 913 reg.size_hi = 0; 914 reg.size_lo = size; 915 916 if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg", 917 (int *)®, sizeof (reg) / sizeof (int)) != DDI_SUCCESS) { 918 DERR(NULL, "Failed to create reg property(dip=0x%p)", dip); 919 return (DDI_WALK_ERROR); 920 } 921 922 /* 923 * Modify the parent's ranges property to map the "reg" property 924 * of the new child. 925 */ 926 if ((rv = ddi_getlongprop(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS, 927 "ranges", (caddr_t)&prng, &prnglen)) != DDI_SUCCESS) { 928 DERR(NULL, 929 "Failed to get parent's ranges property(pdip=0x%p) rv=%d", 930 pdip, rv); 931 return (DDI_WALK_ERROR); 932 } 933 nrng = prnglen/(sizeof (vdds_ranges_t)); 934 /* 935 * First scan all ranges to see if a range corresponding 936 * to this virtual NIU exists already. 937 */ 938 for (rnum = 0; rnum < nrng; rnum++) { 939 prp = &prng[rnum]; 940 if (prp->child_hi == HVCOOKIE(cba->cookie)) { 941 break; 942 } 943 } 944 if (rnum == nrng) { 945 /* Now to try to find an empty range */ 946 for (rnum = 0; rnum < nrng; rnum++) { 947 prp = &prng[rnum]; 948 if (prp->child_hi == 0) { 949 break; 950 } 951 } 952 } 953 if (rnum == nrng) { 954 DERR(NULL, "No free ranges entry found"); 955 return (DDI_WALK_ERROR); 956 } 957 958 /* 959 * child_hi will have HV cookie as HV cookie is more like 960 * a port in the HybridIO. 961 */ 962 prp->child_hi = HVCOOKIE(cba->cookie); 963 prp->child_lo = 0; 964 prp->parent_hi = 0x80000000 | (start >> 32); 965 prp->parent_lo = start & 0x00000000FFFFFFFF; 966 prp->size_hi = (size >> 32); 967 prp->size_lo = size & 0x00000000FFFFFFFF; 968 969 if (ndi_prop_update_int_array(DDI_DEV_T_NONE, pdip, "ranges", 970 (int *)prng, (nrng * 6)) != DDI_SUCCESS) { 971 DERR(NULL, "Failed to update parent ranges prop(pdip=0x%p)", 972 pdip); 973 return (DDI_WALK_ERROR); 974 } 975 kmem_free((void *)prng, prnglen); 976 977 vnet_macaddr_ultostr(cba->macaddr, macaddrbytes); 978 979 /* 980 * create "local-mac-address" property, this will be same as 981 * the vnet's mac-address. 982 */ 983 if (ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, "local-mac-address", 984 macaddrbytes, ETHERADDRL) != DDI_SUCCESS) { 985 DERR(NULL, "Failed to update mac-addresses property(dip=0x%p)", 986 dip); 987 return (DDI_WALK_ERROR); 988 } 989 990 rv = vdds_get_interrupts(cba->cookie, rnum, interrupts, &nintr); 991 if (rv != 0) { 992 DERR(NULL, "Failed to get interrupts for cookie=0x%lx", 993 cba->cookie); 994 return (DDI_WALK_ERROR); 995 } 996 997 /* create "interrupts" property */ 998 if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "interrupts", 999 interrupts, nintr) != DDI_SUCCESS) { 1000 DERR(NULL, "Failed to update interrupts property(dip=0x%p)", 1001 dip); 1002 return (DDI_WALK_ERROR); 1003 } 1004 1005 1006 /* create "max_frame_size" property */ 1007 if (ndi_prop_update_int(DDI_DEV_T_NONE, dip, "max-frame-size", 1008 cba->max_frame_size) != DDI_SUCCESS) { 1009 DERR(NULL, "Failed to update max-frame-size property(dip=0x%p)", 1010 dip); 1011 return (DDI_WALK_ERROR); 1012 } 1013 1014 cba->dip = dip; 1015 DBG1(NULL, "Returning dip=0x%p", dip); 1016 return (DDI_WALK_TERMINATE); 1017 } 1018 1019 1020 /* 1021 * vdds_find_node -- A common function to find a NIU nexus or NIU node. 1022 */ 1023 static dev_info_t * 1024 vdds_find_node(uint64_t cookie, dev_info_t *sdip, 1025 int (*match_func)(dev_info_t *dip, void *arg)) 1026 { 1027 vdds_cb_arg_t arg; 1028 dev_info_t *pdip; 1029 int circ; 1030 1031 DBG1(NULL, "Called cookie=%lx\n", cookie); 1032 1033 arg.dip = NULL; 1034 arg.cookie = cookie; 1035 1036 if (pdip = ddi_get_parent(sdip)) { 1037 ndi_devi_enter(pdip, &circ); 1038 } 1039 1040 ddi_walk_devs(sdip, match_func, (void *)&arg); 1041 if (pdip != NULL) { 1042 ndi_devi_exit(pdip, circ); 1043 } 1044 1045 DBG1(NULL, "Returning dip=0x%p", arg.dip); 1046 return (arg.dip); 1047 } 1048 1049 /* 1050 * vdds_create_new_node -- A common function to create NIU nexus/NIU node. 1051 */ 1052 static dev_info_t * 1053 vdds_create_new_node(vdds_cb_arg_t *cbap, dev_info_t *pdip, 1054 int (*new_node_func)(dev_info_t *dip, void *arg, uint_t flags)) 1055 { 1056 devi_branch_t br; 1057 int rv; 1058 1059 DBG1(NULL, "Called cookie=0x%lx", cbap->cookie); 1060 1061 br.arg = (void *)cbap; 1062 br.type = DEVI_BRANCH_SID; 1063 br.create.sid_branch_create = new_node_func; 1064 br.devi_branch_callback = NULL; 1065 1066 if (pdip == NULL) { 1067 pdip = ddi_root_node(); 1068 } 1069 DBG1(NULL, "calling e_ddi_branch_create"); 1070 if ((rv = e_ddi_branch_create(pdip, &br, NULL, 1071 DEVI_BRANCH_CHILD | DEVI_BRANCH_CONFIGURE))) { 1072 DERR(NULL, "e_ddi_branch_create failed=%d", rv); 1073 return (NULL); 1074 } 1075 DBG1(NULL, "Returning(dip=0x%p", cbap->dip); 1076 return (cbap->dip); 1077 } 1078 1079 /* 1080 * vdds_get_interrupts -- A function that binds ino's to channels and 1081 * then provides them to create interrupts property. 1082 */ 1083 static int 1084 vdds_get_interrupts(uint64_t cookie, int ino_range, int *intrs, int *nintr) 1085 { 1086 uint32_t hvcookie = HVCOOKIE(cookie); 1087 uint64_t txmap; 1088 uint64_t rxmap; 1089 uint32_t ino = VDDS_INO_RANGE_START(ino_range); 1090 int rv; 1091 uint64_t i; 1092 1093 *nintr = 0; 1094 rv = vdds_hv_niu_vr_get_txmap(hvcookie, &txmap); 1095 if (rv != H_EOK) { 1096 DWARN(NULL, "Failed to get txmap for hvcookie=0x%X rv=%d\n", 1097 hvcookie, rv); 1098 return (EIO); 1099 } 1100 rv = vdds_hv_niu_vr_get_rxmap(hvcookie, &rxmap); 1101 if (rv != H_EOK) { 1102 DWARN(NULL, "Failed to get rxmap for hvcookie=0x%X, rv=%d\n", 1103 hvcookie, rv); 1104 return (EIO); 1105 } 1106 /* Check if the number of total channels to be more than 8 */ 1107 for (i = 0; i < 4; i++) { 1108 if (rxmap & (((uint64_t)0x1) << i)) { 1109 rv = vdds_hv_niu_vrrx_set_ino(hvcookie, i, ino); 1110 if (rv != H_EOK) { 1111 DWARN(NULL, "Failed to get Rx ino for " 1112 "hvcookie=0x%X vch_idx=0x%lx rv=%d\n", 1113 hvcookie, i, rv); 1114 return (EIO); 1115 } 1116 DWARN(NULL, 1117 "hvcookie=0x%X RX vch_idx=0x%lx ino=0x%X\n", 1118 hvcookie, i, ino); 1119 *intrs = ino; 1120 ino++; 1121 } else { 1122 *intrs = VDDS_MAX_INTR_NUM; 1123 } 1124 intrs++; 1125 *nintr += 1; 1126 } 1127 for (i = 0; i < 4; i++) { 1128 if (txmap & (((uint64_t)0x1) << i)) { 1129 rv = vdds_hv_niu_vrtx_set_ino(hvcookie, i, ino); 1130 if (rv != H_EOK) { 1131 DWARN(NULL, "Failed to get Tx ino for " 1132 "hvcookie=0x%X vch_idx=0x%lx rv=%d\n", 1133 hvcookie, i, rv); 1134 return (EIO); 1135 } 1136 DWARN(NULL, "hvcookie=0x%X TX vch_idx=0x%lx ino=0x%X\n", 1137 hvcookie, i, ino); 1138 *intrs = ino; 1139 ino++; 1140 } else { 1141 *intrs = VDDS_MAX_INTR_NUM; 1142 } 1143 intrs++; 1144 *nintr += 1; 1145 } 1146 return (0); 1147 } 1148 1149 /* 1150 * vdds_release_range_prop -- cleanups an entry in the ranges property 1151 * corresponding to a cookie. 1152 */ 1153 static void 1154 vdds_release_range_prop(dev_info_t *nexus_dip, uint64_t cookie) 1155 { 1156 vdds_ranges_t *prng; 1157 vdds_ranges_t *prp; 1158 int prnglen; 1159 int nrng; 1160 int rnum; 1161 boolean_t success = B_FALSE; 1162 int rv; 1163 1164 if ((rv = ddi_getlongprop(DDI_DEV_T_ANY, nexus_dip, DDI_PROP_DONTPASS, 1165 "ranges", (caddr_t)&prng, &prnglen)) != DDI_SUCCESS) { 1166 DERR(NULL, 1167 "Failed to get nexus ranges property(dip=0x%p) rv=%d", 1168 nexus_dip, rv); 1169 return; 1170 } 1171 nrng = prnglen/(sizeof (vdds_ranges_t)); 1172 for (rnum = 0; rnum < nrng; rnum++) { 1173 prp = &prng[rnum]; 1174 if (prp->child_hi == HVCOOKIE(cookie)) { 1175 prp->child_hi = 0; 1176 success = B_TRUE; 1177 break; 1178 } 1179 } 1180 if (success) { 1181 if (ndi_prop_update_int_array(DDI_DEV_T_NONE, nexus_dip, 1182 "ranges", (int *)prng, (nrng * 6)) != DDI_SUCCESS) { 1183 DERR(NULL, 1184 "Failed to update nexus ranges prop(dip=0x%p)", 1185 nexus_dip); 1186 } 1187 } 1188 } 1189