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 sprintf(vdds->hio_ifname, "%s%d", ddi_driver_name(dip), 418 ddi_get_instance(dip)); 419 420 rv = vnet_hio_mac_init(vnetp, vdds->hio_ifname); 421 if (rv != 0) { 422 /* failed - cleanup, send failed DDS message */ 423 DERR(vdds, "HIO mac init failed, cleaning up"); 424 rv = vdds_destroy_niu_node(dip, hio_cookie); 425 if (rv == 0) { 426 /* use DERR to print by default */ 427 DERR(vdds, "Successfully destroyed" 428 " Hybrid node"); 429 } else { 430 cmn_err(CE_WARN, "vnet%d:Failed to " 431 "destroy Hybrid node", 432 vnetp->instance); 433 } 434 vdds->hio_dip = NULL; 435 vdds->hio_cookie = 0; 436 (void) vdds_send_dds_resp_msg(vnetp, 437 dmsg, B_FALSE); 438 } else { 439 (void) vdds_send_dds_resp_msg(vnetp, 440 dmsg, B_TRUE); 441 } 442 /* DERR used only print by default */ 443 DERR(vdds, "Successfully created HIO node"); 444 } 445 break; 446 447 case VNET_DDS_TASK_DEL_SHARE: 448 DBG2(vdds, "DEL_SHARE task..."); 449 if (vnetp->vdds_info.hio_dip == NULL) { 450 DBG2(vdds, "NACK: No HIO device destroy"); 451 (void) vdds_send_dds_resp_msg(vnetp, dmsg, B_FALSE); 452 } else { 453 vnet_hio_mac_cleanup(vnetp); 454 rv = vdds_destroy_niu_node(vnetp->vdds_info.hio_dip, 455 vdds->hio_cookie); 456 if (rv == 0) { 457 /* use DERR to print by default */ 458 DERR(vdds, "Successfully destroyed" 459 " Hybrid node"); 460 } else { 461 cmn_err(CE_WARN, "vnet%d:Failed to " 462 "destroy Hybrid node", vnetp->instance); 463 } 464 /* TODO: send ACK even for failure? */ 465 DBG2(vdds, "ACK: HIO device destroyed"); 466 (void) vdds_send_dds_resp_msg(vnetp, dmsg, B_TRUE); 467 vdds->hio_dip = 0; 468 vdds->hio_cookie = 0; 469 } 470 break; 471 case VNET_DDS_TASK_REL_SHARE: 472 DBG2(vdds, "REL_SHARE task..."); 473 if (vnetp->vdds_info.hio_dip != NULL) { 474 vnet_hio_mac_cleanup(vnetp); 475 rv = vdds_destroy_niu_node(vnetp->vdds_info.hio_dip, 476 vdds->hio_cookie); 477 if (rv == 0) { 478 DERR(vdds, "Successfully destroyed " 479 "Hybrid node"); 480 } else { 481 cmn_err(CE_WARN, "vnet%d:Failed to " 482 "destroy HIO node", vnetp->instance); 483 } 484 /* TODO: failure case */ 485 (void) vdds_send_dds_rel_msg(vnetp); 486 vdds->hio_dip = 0; 487 vdds->hio_cookie = 0; 488 } 489 break; 490 default: 491 break; 492 } 493 vdds->task_flags = 0; 494 mutex_exit(&vdds->lock); 495 } 496 497 /* 498 * vdds_send_dds_rel_msg -- Send a DDS_REL_SHARE message. 499 */ 500 static int 501 vdds_send_dds_rel_msg(vnet_t *vnetp) 502 { 503 vnet_dds_info_t *vdds = &vnetp->vdds_info; 504 vio_dds_msg_t vmsg; 505 dds_share_msg_t *smsg = &vmsg.msg.share_msg; 506 int rv; 507 508 DBG1(vdds, "Sending DDS_VNET_REL_SHARE message"); 509 vmsg.tag.vio_msgtype = VIO_TYPE_CTRL; 510 vmsg.tag.vio_subtype = VIO_SUBTYPE_INFO; 511 vmsg.tag.vio_subtype_env = VIO_DDS_INFO; 512 /* vio_sid filled by the LDC module */ 513 vmsg.dds_class = DDS_VNET_NIU; 514 vmsg.dds_subclass = DDS_VNET_REL_SHARE; 515 vmsg.dds_req_id = (++vdds->dds_req_id); 516 smsg->macaddr = vnet_macaddr_strtoul(vnetp->curr_macaddr); 517 smsg->cookie = vdds->hio_cookie; 518 rv = vnet_send_dds_msg(vnetp, &vmsg); 519 return (rv); 520 } 521 522 /* 523 * vdds_send_dds_resp_msg -- Send a DDS response message. 524 */ 525 static int 526 vdds_send_dds_resp_msg(vnet_t *vnetp, vio_dds_msg_t *dmsg, int ack) 527 { 528 vnet_dds_info_t *vdds = &vnetp->vdds_info; 529 int rv; 530 531 DBG1(vdds, "Sending a response mesage=%d", ack); 532 if (ack == B_TRUE) { 533 dmsg->tag.vio_subtype = VIO_SUBTYPE_ACK; 534 dmsg->msg.share_resp_msg.status = DDS_VNET_SUCCESS; 535 } else { 536 dmsg->tag.vio_subtype = VIO_SUBTYPE_NACK; 537 dmsg->msg.share_resp_msg.status = DDS_VNET_FAIL; 538 } 539 rv = vnet_send_dds_msg(vnetp, dmsg); 540 return (rv); 541 } 542 543 /* 544 * vdds_create_niu_node -- Create NIU Hybrid node. The NIU nexus 545 * node also created if it doesn't exist already. 546 */ 547 dev_info_t * 548 vdds_create_niu_node(uint64_t cookie, uint64_t macaddr, uint32_t max_frame_size) 549 { 550 dev_info_t *nexus_dip; 551 dev_info_t *niu_dip; 552 vdds_cb_arg_t cba; 553 554 DBG1(NULL, "Called"); 555 556 if (vdds_hv_hio_capable == B_FALSE) { 557 return (NULL); 558 } 559 mutex_enter(&vdds_dev_lock); 560 /* Check if the nexus node exists already */ 561 nexus_dip = vdds_find_node(cookie, ddi_root_node(), 562 vdds_match_niu_nexus); 563 if (nexus_dip == NULL) { 564 /* 565 * NIU nexus node not found, so create it now. 566 */ 567 cba.dip = NULL; 568 cba.cookie = cookie; 569 cba.macaddr = macaddr; 570 cba.max_frame_size = max_frame_size; 571 nexus_dip = vdds_create_new_node(&cba, NULL, 572 vdds_new_nexus_node); 573 if (nexus_dip == NULL) { 574 mutex_exit(&vdds_dev_lock); 575 return (NULL); 576 } 577 } 578 DBG2(NULL, "nexus_dip = 0x%p", nexus_dip); 579 580 /* Check if NIU node exists already before creating one */ 581 niu_dip = vdds_find_node(cookie, nexus_dip, 582 vdds_match_niu_node); 583 if (niu_dip == NULL) { 584 cba.dip = NULL; 585 cba.cookie = cookie; 586 cba.macaddr = macaddr; 587 cba.max_frame_size = max_frame_size; 588 niu_dip = vdds_create_new_node(&cba, nexus_dip, 589 vdds_new_niu_node); 590 /* 591 * Hold the niu_dip to prevent it from 592 * detaching. 593 */ 594 if (niu_dip != NULL) { 595 e_ddi_hold_devi(niu_dip); 596 } else { 597 DWARN(NULL, "niumx/network node creation failed"); 598 } 599 } else { 600 DWARN(NULL, "niumx/network node already exists(dip=0x%p)", 601 niu_dip); 602 } 603 /* release the hold that was done in find/create */ 604 if ((niu_dip != NULL) && (e_ddi_branch_held(niu_dip))) 605 e_ddi_branch_rele(niu_dip); 606 if (e_ddi_branch_held(nexus_dip)) 607 e_ddi_branch_rele(nexus_dip); 608 mutex_exit(&vdds_dev_lock); 609 DBG1(NULL, "returning niu_dip=0x%p", niu_dip); 610 return (niu_dip); 611 } 612 613 /* 614 * vdds_destroy_niu_node -- Destroy the NIU node. 615 */ 616 int 617 vdds_destroy_niu_node(dev_info_t *niu_dip, uint64_t cookie) 618 { 619 int rv; 620 dev_info_t *fdip = NULL; 621 dev_info_t *nexus_dip = ddi_get_parent(niu_dip); 622 623 624 DBG1(NULL, "Called"); 625 ASSERT(nexus_dip != NULL); 626 mutex_enter(&vdds_dev_lock); 627 628 if (!e_ddi_branch_held(niu_dip)) 629 e_ddi_branch_hold(niu_dip); 630 /* 631 * As we are destroying now, release the 632 * hold that was done in during the creation. 633 */ 634 ddi_release_devi(niu_dip); 635 rv = e_ddi_branch_destroy(niu_dip, &fdip, 0); 636 if (rv != 0) { 637 DERR(NULL, "Failed to destroy niumx/network node dip=0x%p", 638 niu_dip); 639 if (fdip != NULL) { 640 ddi_release_devi(fdip); 641 } 642 rv = EBUSY; 643 goto dest_exit; 644 } 645 /* 646 * Cleanup the parent's ranges property set 647 * for this Hybrid device. 648 */ 649 vdds_release_range_prop(nexus_dip, cookie); 650 651 dest_exit: 652 mutex_exit(&vdds_dev_lock); 653 DBG1(NULL, "returning rv=%d", rv); 654 return (rv); 655 } 656 657 /* 658 * vdds_match_niu_nexus -- callback function to verify a node is the 659 * NIU nexus node. 660 */ 661 static int 662 vdds_match_niu_nexus(dev_info_t *dip, void *arg) 663 { 664 vdds_cb_arg_t *warg = (vdds_cb_arg_t *)arg; 665 vdds_reg_t *reg_p; 666 char *name; 667 uint64_t hdl; 668 uint_t reglen; 669 int rv; 670 671 if (dip == ddi_root_node()) { 672 return (DDI_WALK_CONTINUE); 673 } 674 675 name = ddi_node_name(dip); 676 if (strcmp(name, "niu") != 0) { 677 return (DDI_WALK_CONTINUE); 678 } 679 rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, 680 DDI_PROP_DONTPASS, "reg", (int **)®_p, ®len); 681 if (rv != DDI_PROP_SUCCESS) { 682 DWARN(NULL, "Failed to get reg property dip=0x%p", dip); 683 return (DDI_WALK_CONTINUE); 684 } 685 686 hdl = reg_p->addr_hi & 0x0FFFFFFF; 687 ddi_prop_free(reg_p); 688 689 DBG2(NULL, "Handle = 0x%lx dip=0x%p", hdl, dip); 690 if (hdl == NIUCFGHDL(warg->cookie)) { 691 /* Hold before returning */ 692 if (!e_ddi_branch_held(dip)) 693 e_ddi_branch_hold(dip); 694 warg->dip = dip; 695 DBG2(NULL, "Found dip = 0x%p", dip); 696 return (DDI_WALK_TERMINATE); 697 } 698 return (DDI_WALK_CONTINUE); 699 } 700 701 /* 702 * vdds_match_niu_node -- callback function to verify a node is the 703 * NIU Hybrid node. 704 */ 705 static int 706 vdds_match_niu_node(dev_info_t *dip, void *arg) 707 { 708 vdds_cb_arg_t *warg = (vdds_cb_arg_t *)arg; 709 char *name; 710 vdds_reg_t *reg_p; 711 uint_t reglen; 712 int rv; 713 uint32_t addr_hi; 714 715 name = ddi_node_name(dip); 716 if (strcmp(name, "network") != 0) { 717 return (DDI_WALK_CONTINUE); 718 } 719 rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, 720 DDI_PROP_DONTPASS, "reg", (int **)®_p, ®len); 721 if (rv != DDI_PROP_SUCCESS) { 722 DWARN(NULL, "Failed to get reg property dip=0x%p", dip); 723 return (DDI_WALK_CONTINUE); 724 } 725 726 addr_hi = reg_p->addr_hi; 727 DBG1(NULL, "addr_hi = 0x%x dip=0x%p", addr_hi, dip); 728 ddi_prop_free(reg_p); 729 if (addr_hi == HVCOOKIE(warg->cookie)) { 730 warg->dip = dip; 731 if (!e_ddi_branch_held(dip)) 732 e_ddi_branch_hold(dip); 733 DBG1(NULL, "Found dip = 0x%p", dip); 734 return (DDI_WALK_TERMINATE); 735 } 736 return (DDI_WALK_CONTINUE); 737 } 738 739 /* 740 * vdds_new_nexus_node -- callback function to set all the properties 741 * a new NIU nexus node. 742 */ 743 static int 744 vdds_new_nexus_node(dev_info_t *dip, void *arg, uint_t flags) 745 { 746 vdds_cb_arg_t *cba = (vdds_cb_arg_t *)arg; 747 char *compat[] = { "SUNW,niumx" }; 748 vdds_ranges_t *rangesp; 749 vdds_reg_t reg; 750 uint64_t nranges; 751 int n; 752 753 DBG1(NULL, "Called dip=0x%p, flags=0x%X", dip, flags); 754 755 /* create "niu" property */ 756 if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, "name", "niu") != 757 DDI_SUCCESS) { 758 DERR(NULL, "Failed to create name property(dip=0x%p)", dip); 759 return (DDI_WALK_ERROR); 760 } 761 762 /* create "compatible" property */ 763 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, dip, "compatible", 764 compat, 1) != DDI_SUCCESS) { 765 DERR(NULL, "Failed to create compatible property(dip=0x%p)", 766 dip); 767 return (DDI_WALK_ERROR); 768 } 769 770 /* create "device_type" property */ 771 if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, 772 "device_type", "sun4v") != DDI_SUCCESS) { 773 DERR(NULL, "Failed to create device_type property(dip=0x%p)", 774 dip); 775 return (DDI_WALK_ERROR); 776 } 777 778 /* 779 * create "reg" property. The first 28 bits of 780 * 'addr_hi' are NIU cfg_handle, the 0xc in 28-31 bits 781 * indicates non-cacheable config. 782 */ 783 reg.addr_hi = 0xc0000000 | NIUCFGHDL(cba->cookie); 784 reg.addr_lo = 0; 785 reg.size_hi = 0; 786 reg.size_lo = 0; 787 if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, 788 "reg", (int *)®, sizeof (reg)/sizeof (int)) != DDI_SUCCESS) { 789 DERR(NULL, "Failed to create reg property(dip=0x%p)", dip); 790 return (DDI_WALK_ERROR); 791 } 792 793 /* 794 * Create VDDS_MAX_RANGES so that they are already in place 795 * before the children are created. While creating the child 796 * we just modify one of this ranges entries. 797 */ 798 nranges = VDDS_MAX_RANGES; /* One range for each VR */ 799 rangesp = (vdds_ranges_t *)kmem_zalloc( 800 (sizeof (vdds_ranges_t) * nranges), KM_SLEEP); 801 802 for (n = 0; n < nranges; n++) { 803 /* zero all child_hi/lo */ 804 rangesp[n].child_hi = 0; 805 rangesp[n].child_lo = 0; 806 } 807 808 if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges", 809 (int *)rangesp, (nranges * 6)) != DDI_SUCCESS) { 810 DERR(NULL, "Failed to create ranges property(dip=0x%p)", dip); 811 kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges)); 812 return (DDI_WALK_ERROR); 813 } 814 815 /* create "#size-cells" property */ 816 if (ndi_prop_update_int(DDI_DEV_T_NONE, dip, 817 "#size-cells", 2) != DDI_SUCCESS) { 818 DERR(NULL, "Failed to create #size-cells property(dip=0x%p)", 819 dip); 820 kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges)); 821 return (DDI_WALK_ERROR); 822 } 823 824 /* create "#address-cells" property */ 825 if (ndi_prop_update_int(DDI_DEV_T_NONE, dip, 826 "#address-cells", 2) != DDI_SUCCESS) { 827 DERR(NULL, "Failed to create #address-cells prop(dip=0x%p)", 828 dip); 829 kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges)); 830 return (DDI_WALK_ERROR); 831 } 832 833 kmem_free(rangesp, (sizeof (vdds_ranges_t) * nranges)); 834 cba->dip = dip; 835 DBG1(NULL, "Returning (dip=0x%p)", dip); 836 return (DDI_WALK_TERMINATE); 837 } 838 839 /* 840 * vdds_new_niu_node -- callback function to create a new NIU Hybrid node. 841 */ 842 static int 843 vdds_new_niu_node(dev_info_t *dip, void *arg, uint_t flags) 844 { 845 vdds_cb_arg_t *cba = (vdds_cb_arg_t *)arg; 846 char *compat[] = { "SUNW,niusl" }; 847 uint8_t macaddrbytes[ETHERADDRL]; 848 int interrupts[VDDS_MAX_VRINTRS]; 849 vdds_ranges_t *prng; 850 vdds_ranges_t *prp; 851 vdds_reg_t reg; 852 dev_info_t *pdip; 853 uint64_t start; 854 uint64_t size; 855 int prnglen; 856 int nintr = 0; 857 int nrng; 858 int rnum; 859 int rv; 860 861 DBG1(NULL, "Called dip=0x%p flags=0x%X", dip, flags); 862 pdip = ddi_get_parent(dip); 863 864 if (pdip == NULL) { 865 DWARN(NULL, "Failed to get parent dip(dip=0x%p)", dip); 866 return (DDI_WALK_ERROR); 867 } 868 869 /* create "network" property */ 870 if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, "name", "network") != 871 DDI_SUCCESS) { 872 DERR(NULL, "Failed to create name property(dip=0x%p)", dip); 873 return (DDI_WALK_ERROR); 874 } 875 876 /* 877 * create "niutype" property, it is set to n2niu to 878 * indicate NIU Hybrid node. 879 */ 880 if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, "niutype", 881 "n2niu") != DDI_SUCCESS) { 882 DERR(NULL, "Failed to create niuopmode property(dip=0x%p)", 883 dip); 884 return (DDI_WALK_ERROR); 885 } 886 887 /* create "compatible" property */ 888 if (ndi_prop_update_string_array(DDI_DEV_T_NONE, dip, "compatible", 889 compat, 1) != DDI_SUCCESS) { 890 DERR(NULL, "Failed to create compatible property(dip=0x%p)", 891 dip); 892 return (DDI_WALK_ERROR); 893 } 894 895 /* create "device_type" property */ 896 if (ndi_prop_update_string(DDI_DEV_T_NONE, dip, 897 "device_type", "network") != DDI_SUCCESS) { 898 DERR(NULL, "Failed to create device_type property(dip=0x%p)", 899 dip); 900 return (DDI_WALK_ERROR); 901 } 902 903 /* create "reg" property */ 904 if (vdds_hv_niu_vr_getinfo(HVCOOKIE(cba->cookie), 905 &start, &size) != H_EOK) { 906 DERR(NULL, "Failed to get vrinfo for cookie(0x%lX)", 907 cba->cookie); 908 return (DDI_WALK_ERROR); 909 } 910 reg.addr_hi = HVCOOKIE(cba->cookie); 911 reg.addr_lo = 0; 912 reg.size_hi = 0; 913 reg.size_lo = size; 914 915 if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg", 916 (int *)®, sizeof (reg) / sizeof (int)) != DDI_SUCCESS) { 917 DERR(NULL, "Failed to create reg property(dip=0x%p)", dip); 918 return (DDI_WALK_ERROR); 919 } 920 921 /* 922 * Modify the parent's ranges property to map the "reg" property 923 * of the new child. 924 */ 925 if ((rv = ddi_getlongprop(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS, 926 "ranges", (caddr_t)&prng, &prnglen)) != DDI_SUCCESS) { 927 DERR(NULL, 928 "Failed to get parent's ranges property(pdip=0x%p) rv=%d", 929 pdip, rv); 930 return (DDI_WALK_ERROR); 931 } 932 nrng = prnglen/(sizeof (vdds_ranges_t)); 933 /* 934 * First scan all ranges to see if a range corresponding 935 * to this virtual NIU exists already. 936 */ 937 for (rnum = 0; rnum < nrng; rnum++) { 938 prp = &prng[rnum]; 939 if (prp->child_hi == HVCOOKIE(cba->cookie)) { 940 break; 941 } 942 } 943 if (rnum == nrng) { 944 /* Now to try to find an empty range */ 945 for (rnum = 0; rnum < nrng; rnum++) { 946 prp = &prng[rnum]; 947 if (prp->child_hi == 0) { 948 break; 949 } 950 } 951 } 952 if (rnum == nrng) { 953 DERR(NULL, "No free ranges entry found"); 954 return (DDI_WALK_ERROR); 955 } 956 957 /* 958 * child_hi will have HV cookie as HV cookie is more like 959 * a port in the HybridIO. 960 */ 961 prp->child_hi = HVCOOKIE(cba->cookie); 962 prp->child_lo = 0; 963 prp->parent_hi = 0x80000000 | (start >> 32); 964 prp->parent_lo = start & 0x00000000FFFFFFFF; 965 prp->size_hi = (size >> 32); 966 prp->size_lo = size & 0x00000000FFFFFFFF; 967 968 if (ndi_prop_update_int_array(DDI_DEV_T_NONE, pdip, "ranges", 969 (int *)prng, (nrng * 6)) != DDI_SUCCESS) { 970 DERR(NULL, "Failed to update parent ranges prop(pdip=0x%p)", 971 pdip); 972 return (DDI_WALK_ERROR); 973 } 974 kmem_free((void *)prng, prnglen); 975 976 vnet_macaddr_ultostr(cba->macaddr, macaddrbytes); 977 978 /* 979 * create "local-mac-address" property, this will be same as 980 * the vnet's mac-address. 981 */ 982 if (ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, "local-mac-address", 983 macaddrbytes, ETHERADDRL) != DDI_SUCCESS) { 984 DERR(NULL, "Failed to update mac-addresses property(dip=0x%p)", 985 dip); 986 return (DDI_WALK_ERROR); 987 } 988 989 rv = vdds_get_interrupts(cba->cookie, rnum, interrupts, &nintr); 990 if (rv != 0) { 991 DERR(NULL, "Failed to get interrupts for cookie=0x%lx", 992 cba->cookie); 993 return (DDI_WALK_ERROR); 994 } 995 996 /* create "interrupts" property */ 997 if (ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "interrupts", 998 interrupts, nintr) != DDI_SUCCESS) { 999 DERR(NULL, "Failed to update interrupts property(dip=0x%p)", 1000 dip); 1001 return (DDI_WALK_ERROR); 1002 } 1003 1004 1005 /* create "max_frame_size" property */ 1006 if (ndi_prop_update_int(DDI_DEV_T_NONE, dip, "max-frame-size", 1007 cba->max_frame_size) != DDI_SUCCESS) { 1008 DERR(NULL, "Failed to update max-frame-size property(dip=0x%p)", 1009 dip); 1010 return (DDI_WALK_ERROR); 1011 } 1012 1013 cba->dip = dip; 1014 DBG1(NULL, "Returning dip=0x%p", dip); 1015 return (DDI_WALK_TERMINATE); 1016 } 1017 1018 1019 /* 1020 * vdds_find_node -- A common function to find a NIU nexus or NIU node. 1021 */ 1022 static dev_info_t * 1023 vdds_find_node(uint64_t cookie, dev_info_t *sdip, 1024 int (*match_func)(dev_info_t *dip, void *arg)) 1025 { 1026 vdds_cb_arg_t arg; 1027 dev_info_t *pdip; 1028 int circ; 1029 1030 DBG1(NULL, "Called cookie=%lx\n", cookie); 1031 1032 arg.dip = NULL; 1033 arg.cookie = cookie; 1034 1035 if (pdip = ddi_get_parent(sdip)) { 1036 ndi_devi_enter(pdip, &circ); 1037 } 1038 1039 ddi_walk_devs(sdip, match_func, (void *)&arg); 1040 if (pdip != NULL) { 1041 ndi_devi_exit(pdip, circ); 1042 } 1043 1044 DBG1(NULL, "Returning dip=0x%p", arg.dip); 1045 return (arg.dip); 1046 } 1047 1048 /* 1049 * vdds_create_new_node -- A common function to create NIU nexus/NIU node. 1050 */ 1051 static dev_info_t * 1052 vdds_create_new_node(vdds_cb_arg_t *cbap, dev_info_t *pdip, 1053 int (*new_node_func)(dev_info_t *dip, void *arg, uint_t flags)) 1054 { 1055 devi_branch_t br; 1056 int rv; 1057 1058 DBG1(NULL, "Called cookie=0x%lx", cbap->cookie); 1059 1060 br.arg = (void *)cbap; 1061 br.type = DEVI_BRANCH_SID; 1062 br.create.sid_branch_create = new_node_func; 1063 br.devi_branch_callback = NULL; 1064 1065 if (pdip == NULL) { 1066 pdip = ddi_root_node(); 1067 } 1068 DBG1(NULL, "calling e_ddi_branch_create"); 1069 if ((rv = e_ddi_branch_create(pdip, &br, NULL, 1070 DEVI_BRANCH_CHILD | DEVI_BRANCH_CONFIGURE))) { 1071 DERR(NULL, "e_ddi_branch_create failed=%d", rv); 1072 return (NULL); 1073 } 1074 DBG1(NULL, "Returning(dip=0x%p", cbap->dip); 1075 return (cbap->dip); 1076 } 1077 1078 /* 1079 * vdds_get_interrupts -- A function that binds ino's to channels and 1080 * then provides them to create interrupts property. 1081 */ 1082 static int 1083 vdds_get_interrupts(uint64_t cookie, int ino_range, int *intrs, int *nintr) 1084 { 1085 uint32_t hvcookie = HVCOOKIE(cookie); 1086 uint64_t txmap; 1087 uint64_t rxmap; 1088 uint32_t ino = VDDS_INO_RANGE_START(ino_range); 1089 int rv; 1090 uint64_t i; 1091 1092 *nintr = 0; 1093 rv = vdds_hv_niu_vr_get_txmap(hvcookie, &txmap); 1094 if (rv != H_EOK) { 1095 DWARN(NULL, "Failed to get txmap for hvcookie=0x%X rv=%d\n", 1096 hvcookie, rv); 1097 return (EIO); 1098 } 1099 rv = vdds_hv_niu_vr_get_rxmap(hvcookie, &rxmap); 1100 if (rv != H_EOK) { 1101 DWARN(NULL, "Failed to get rxmap for hvcookie=0x%X, rv=%d\n", 1102 hvcookie, rv); 1103 return (EIO); 1104 } 1105 /* Check if the number of total channels to be more than 8 */ 1106 for (i = 0; i < 4; i++) { 1107 if (rxmap & (((uint64_t)0x1) << i)) { 1108 rv = vdds_hv_niu_vrrx_set_ino(hvcookie, i, ino); 1109 if (rv != H_EOK) { 1110 DWARN(NULL, "Failed to get Rx ino for " 1111 "hvcookie=0x%X vch_idx=0x%lx rv=%d\n", 1112 hvcookie, i, rv); 1113 return (EIO); 1114 } 1115 DWARN(NULL, 1116 "hvcookie=0x%X RX vch_idx=0x%lx ino=0x%X\n", 1117 hvcookie, i, ino); 1118 *intrs = ino; 1119 ino++; 1120 } else { 1121 *intrs = VDDS_MAX_INTR_NUM; 1122 } 1123 intrs++; 1124 *nintr += 1; 1125 } 1126 for (i = 0; i < 4; i++) { 1127 if (txmap & (((uint64_t)0x1) << i)) { 1128 rv = vdds_hv_niu_vrtx_set_ino(hvcookie, i, ino); 1129 if (rv != H_EOK) { 1130 DWARN(NULL, "Failed to get Tx ino for " 1131 "hvcookie=0x%X vch_idx=0x%lx rv=%d\n", 1132 hvcookie, i, rv); 1133 return (EIO); 1134 } 1135 DWARN(NULL, "hvcookie=0x%X TX vch_idx=0x%lx ino=0x%X\n", 1136 hvcookie, i, ino); 1137 *intrs = ino; 1138 ino++; 1139 } else { 1140 *intrs = VDDS_MAX_INTR_NUM; 1141 } 1142 intrs++; 1143 *nintr += 1; 1144 } 1145 return (0); 1146 } 1147 1148 /* 1149 * vdds_release_range_prop -- cleanups an entry in the ranges property 1150 * corresponding to a cookie. 1151 */ 1152 static void 1153 vdds_release_range_prop(dev_info_t *nexus_dip, uint64_t cookie) 1154 { 1155 vdds_ranges_t *prng; 1156 vdds_ranges_t *prp; 1157 int prnglen; 1158 int nrng; 1159 int rnum; 1160 boolean_t success = B_FALSE; 1161 int rv; 1162 1163 if ((rv = ddi_getlongprop(DDI_DEV_T_ANY, nexus_dip, DDI_PROP_DONTPASS, 1164 "ranges", (caddr_t)&prng, &prnglen)) != DDI_SUCCESS) { 1165 DERR(NULL, 1166 "Failed to get nexus ranges property(dip=0x%p) rv=%d", 1167 nexus_dip, rv); 1168 return; 1169 } 1170 nrng = prnglen/(sizeof (vdds_ranges_t)); 1171 for (rnum = 0; rnum < nrng; rnum++) { 1172 prp = &prng[rnum]; 1173 if (prp->child_hi == HVCOOKIE(cookie)) { 1174 prp->child_hi = 0; 1175 success = B_TRUE; 1176 break; 1177 } 1178 } 1179 if (success) { 1180 if (ndi_prop_update_int_array(DDI_DEV_T_NONE, nexus_dip, 1181 "ranges", (int *)prng, (nrng * 6)) != DDI_SUCCESS) { 1182 DERR(NULL, 1183 "Failed to update nexus ranges prop(dip=0x%p)", 1184 nexus_dip); 1185 } 1186 } 1187 } 1188