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