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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright 2020 Joyent, Inc. 25 * Copyright 2015 Garrett D'Amore <garrett@damore.org> 26 * Copyright 2020 RackTop Systems, Inc. 27 */ 28 29 /* 30 * MAC Services Module 31 * 32 * The GLDv3 framework locking - The MAC layer 33 * -------------------------------------------- 34 * 35 * The MAC layer is central to the GLD framework and can provide the locking 36 * framework needed for itself and for the use of MAC clients. MAC end points 37 * are fairly disjoint and don't share a lot of state. So a coarse grained 38 * multi-threading scheme is to single thread all create/modify/delete or set 39 * type of control operations on a per mac end point while allowing data threads 40 * concurrently. 41 * 42 * Control operations (set) that modify a mac end point are always serialized on 43 * a per mac end point basis, We have at most 1 such thread per mac end point 44 * at a time. 45 * 46 * All other operations that are not serialized are essentially multi-threaded. 47 * For example a control operation (get) like getting statistics which may not 48 * care about reading values atomically or data threads sending or receiving 49 * data. Mostly these type of operations don't modify the control state. Any 50 * state these operations care about are protected using traditional locks. 51 * 52 * The perimeter only serializes serial operations. It does not imply there 53 * aren't any other concurrent operations. However a serialized operation may 54 * sometimes need to make sure it is the only thread. In this case it needs 55 * to use reference counting mechanisms to cv_wait until any current data 56 * threads are done. 57 * 58 * The mac layer itself does not hold any locks across a call to another layer. 59 * The perimeter is however held across a down call to the driver to make the 60 * whole control operation atomic with respect to other control operations. 61 * Also the data path and get type control operations may proceed concurrently. 62 * These operations synchronize with the single serial operation on a given mac 63 * end point using regular locks. The perimeter ensures that conflicting 64 * operations like say a mac_multicast_add and a mac_multicast_remove on the 65 * same mac end point don't interfere with each other and also ensures that the 66 * changes in the mac layer and the call to the underlying driver to say add a 67 * multicast address are done atomically without interference from a thread 68 * trying to delete the same address. 69 * 70 * For example, consider 71 * mac_multicst_add() 72 * { 73 * mac_perimeter_enter(); serialize all control operations 74 * 75 * grab list lock protect against access by data threads 76 * add to list 77 * drop list lock 78 * 79 * call driver's mi_multicst 80 * 81 * mac_perimeter_exit(); 82 * } 83 * 84 * To lessen the number of serialization locks and simplify the lock hierarchy, 85 * we serialize all the control operations on a per mac end point by using a 86 * single serialization lock called the perimeter. We allow recursive entry into 87 * the perimeter to facilitate use of this mechanism by both the mac client and 88 * the MAC layer itself. 89 * 90 * MAC client means an entity that does an operation on a mac handle 91 * obtained from a mac_open/mac_client_open. Similarly MAC driver means 92 * an entity that does an operation on a mac handle obtained from a 93 * mac_register. An entity could be both client and driver but on different 94 * handles eg. aggr. and should only make the corresponding mac interface calls 95 * i.e. mac driver interface or mac client interface as appropriate for that 96 * mac handle. 97 * 98 * General rules. 99 * ------------- 100 * 101 * R1. The lock order of upcall threads is natually opposite to downcall 102 * threads. Hence upcalls must not hold any locks across layers for fear of 103 * recursive lock enter and lock order violation. This applies to all layers. 104 * 105 * R2. The perimeter is just another lock. Since it is held in the down 106 * direction, acquiring the perimeter in an upcall is prohibited as it would 107 * cause a deadlock. This applies to all layers. 108 * 109 * Note that upcalls that need to grab the mac perimeter (for example 110 * mac_notify upcalls) can still achieve that by posting the request to a 111 * thread, which can then grab all the required perimeters and locks in the 112 * right global order. Note that in the above example the mac layer iself 113 * won't grab the mac perimeter in the mac_notify upcall, instead the upcall 114 * to the client must do that. Please see the aggr code for an example. 115 * 116 * MAC client rules 117 * ---------------- 118 * 119 * R3. A MAC client may use the MAC provided perimeter facility to serialize 120 * control operations on a per mac end point. It does this by by acquring 121 * and holding the perimeter across a sequence of calls to the mac layer. 122 * This ensures atomicity across the entire block of mac calls. In this 123 * model the MAC client must not hold any client locks across the calls to 124 * the mac layer. This model is the preferred solution. 125 * 126 * R4. However if a MAC client has a lot of global state across all mac end 127 * points the per mac end point serialization may not be sufficient. In this 128 * case the client may choose to use global locks or use its own serialization. 129 * To avoid deadlocks, these client layer locks held across the mac calls 130 * in the control path must never be acquired by the data path for the reason 131 * mentioned below. 132 * 133 * (Assume that a control operation that holds a client lock blocks in the 134 * mac layer waiting for upcall reference counts to drop to zero. If an upcall 135 * data thread that holds this reference count, tries to acquire the same 136 * client lock subsequently it will deadlock). 137 * 138 * A MAC client may follow either the R3 model or the R4 model, but can't 139 * mix both. In the former, the hierarchy is Perim -> client locks, but in 140 * the latter it is client locks -> Perim. 141 * 142 * R5. MAC clients must make MAC calls (excluding data calls) in a cv_wait'able 143 * context since they may block while trying to acquire the perimeter. 144 * In addition some calls may block waiting for upcall refcnts to come down to 145 * zero. 146 * 147 * R6. MAC clients must make sure that they are single threaded and all threads 148 * from the top (in particular data threads) have finished before calling 149 * mac_client_close. The MAC framework does not track the number of client 150 * threads using the mac client handle. Also mac clients must make sure 151 * they have undone all the control operations before calling mac_client_close. 152 * For example mac_unicast_remove/mac_multicast_remove to undo the corresponding 153 * mac_unicast_add/mac_multicast_add. 154 * 155 * MAC framework rules 156 * ------------------- 157 * 158 * R7. The mac layer itself must not hold any mac layer locks (except the mac 159 * perimeter) across a call to any other layer from the mac layer. The call to 160 * any other layer could be via mi_* entry points, classifier entry points into 161 * the driver or via upcall pointers into layers above. The mac perimeter may 162 * be acquired or held only in the down direction, for e.g. when calling into 163 * a mi_* driver enty point to provide atomicity of the operation. 164 * 165 * R8. Since it is not guaranteed (see R14) that drivers won't hold locks across 166 * mac driver interfaces, the MAC layer must provide a cut out for control 167 * interfaces like upcall notifications and start them in a separate thread. 168 * 169 * R9. Note that locking order also implies a plumbing order. For example 170 * VNICs are allowed to be created over aggrs, but not vice-versa. An attempt 171 * to plumb in any other order must be failed at mac_open time, otherwise it 172 * could lead to deadlocks due to inverse locking order. 173 * 174 * R10. MAC driver interfaces must not block since the driver could call them 175 * in interrupt context. 176 * 177 * R11. Walkers must preferably not hold any locks while calling walker 178 * callbacks. Instead these can operate on reference counts. In simple 179 * callbacks it may be ok to hold a lock and call the callbacks, but this is 180 * harder to maintain in the general case of arbitrary callbacks. 181 * 182 * R12. The MAC layer must protect upcall notification callbacks using reference 183 * counts rather than holding locks across the callbacks. 184 * 185 * R13. Given the variety of drivers, it is preferable if the MAC layer can make 186 * sure that any pointers (such as mac ring pointers) it passes to the driver 187 * remain valid until mac unregister time. Currently the mac layer achieves 188 * this by using generation numbers for rings and freeing the mac rings only 189 * at unregister time. The MAC layer must provide a layer of indirection and 190 * must not expose underlying driver rings or driver data structures/pointers 191 * directly to MAC clients. 192 * 193 * MAC driver rules 194 * ---------------- 195 * 196 * R14. It would be preferable if MAC drivers don't hold any locks across any 197 * mac call. However at a minimum they must not hold any locks across data 198 * upcalls. They must also make sure that all references to mac data structures 199 * are cleaned up and that it is single threaded at mac_unregister time. 200 * 201 * R15. MAC driver interfaces don't block and so the action may be done 202 * asynchronously in a separate thread as for example handling notifications. 203 * The driver must not assume that the action is complete when the call 204 * returns. 205 * 206 * R16. Drivers must maintain a generation number per Rx ring, and pass it 207 * back to mac_rx_ring(); They are expected to increment the generation 208 * number whenever the ring's stop routine is invoked. 209 * See comments in mac_rx_ring(); 210 * 211 * R17 Similarly mi_stop is another synchronization point and the driver must 212 * ensure that all upcalls are done and there won't be any future upcall 213 * before returning from mi_stop. 214 * 215 * R18. The driver may assume that all set/modify control operations via 216 * the mi_* entry points are single threaded on a per mac end point. 217 * 218 * Lock and Perimeter hierarchy scenarios 219 * --------------------------------------- 220 * 221 * i_mac_impl_lock -> mi_rw_lock -> srs_lock -> s_ring_lock[i_mac_tx_srs_notify] 222 * 223 * ft_lock -> fe_lock [mac_flow_lookup] 224 * 225 * mi_rw_lock -> fe_lock [mac_bcast_send] 226 * 227 * srs_lock -> mac_bw_lock [mac_rx_srs_drain_bw] 228 * 229 * cpu_lock -> mac_srs_g_lock -> srs_lock -> s_ring_lock [mac_walk_srs_and_bind] 230 * 231 * i_dls_devnet_lock -> mac layer locks [dls_devnet_rename] 232 * 233 * Perimeters are ordered P1 -> P2 -> P3 from top to bottom in order of mac 234 * client to driver. In the case of clients that explictly use the mac provided 235 * perimeter mechanism for its serialization, the hierarchy is 236 * Perimeter -> mac layer locks, since the client never holds any locks across 237 * the mac calls. In the case of clients that use its own locks the hierarchy 238 * is Client locks -> Mac Perim -> Mac layer locks. The client never explicitly 239 * calls mac_perim_enter/exit in this case. 240 * 241 * Subflow creation rules 242 * --------------------------- 243 * o In case of a user specified cpulist present on underlying link and flows, 244 * the flows cpulist must be a subset of the underlying link. 245 * o In case of a user specified fanout mode present on link and flow, the 246 * subflow fanout count has to be less than or equal to that of the 247 * underlying link. The cpu-bindings for the subflows will be a subset of 248 * the underlying link. 249 * o In case if no cpulist specified on both underlying link and flow, the 250 * underlying link relies on a MAC tunable to provide out of box fanout. 251 * The subflow will have no cpulist (the subflow will be unbound) 252 * o In case if no cpulist is specified on the underlying link, a subflow can 253 * carry either a user-specified cpulist or fanout count. The cpu-bindings 254 * for the subflow will not adhere to restriction that they need to be subset 255 * of the underlying link. 256 * o In case where the underlying link is carrying either a user specified 257 * cpulist or fanout mode and for a unspecified subflow, the subflow will be 258 * created unbound. 259 * o While creating unbound subflows, bandwidth mode changes attempt to 260 * figure a right fanout count. In such cases the fanout count will override 261 * the unbound cpu-binding behavior. 262 * o In addition to this, while cycling between flow and link properties, we 263 * impose a restriction that if a link property has a subflow with 264 * user-specified attributes, we will not allow changing the link property. 265 * The administrator needs to reset all the user specified properties for the 266 * subflows before attempting a link property change. 267 * Some of the above rules can be overridden by specifying additional command 268 * line options while creating or modifying link or subflow properties. 269 * 270 * Datapath 271 * -------- 272 * 273 * For information on the datapath, the world of soft rings, hardware rings, how 274 * it is structured, and the path of an mblk_t between a driver and a mac 275 * client, see mac_sched.c. 276 */ 277 278 #include <sys/types.h> 279 #include <sys/conf.h> 280 #include <sys/id_space.h> 281 #include <sys/esunddi.h> 282 #include <sys/stat.h> 283 #include <sys/mkdev.h> 284 #include <sys/stream.h> 285 #include <sys/strsun.h> 286 #include <sys/strsubr.h> 287 #include <sys/dlpi.h> 288 #include <sys/list.h> 289 #include <sys/modhash.h> 290 #include <sys/mac_provider.h> 291 #include <sys/mac_client_impl.h> 292 #include <sys/mac_soft_ring.h> 293 #include <sys/mac_stat.h> 294 #include <sys/mac_impl.h> 295 #include <sys/mac.h> 296 #include <sys/dls.h> 297 #include <sys/dld.h> 298 #include <sys/modctl.h> 299 #include <sys/fs/dv_node.h> 300 #include <sys/thread.h> 301 #include <sys/proc.h> 302 #include <sys/callb.h> 303 #include <sys/cpuvar.h> 304 #include <sys/atomic.h> 305 #include <sys/bitmap.h> 306 #include <sys/sdt.h> 307 #include <sys/mac_flow.h> 308 #include <sys/ddi_intr_impl.h> 309 #include <sys/disp.h> 310 #include <sys/sdt.h> 311 #include <sys/vnic.h> 312 #include <sys/vnic_impl.h> 313 #include <sys/vlan.h> 314 #include <inet/ip.h> 315 #include <inet/ip6.h> 316 #include <sys/exacct.h> 317 #include <sys/exacct_impl.h> 318 #include <inet/nd.h> 319 #include <sys/ethernet.h> 320 #include <sys/pool.h> 321 #include <sys/pool_pset.h> 322 #include <sys/cpupart.h> 323 #include <inet/wifi_ioctl.h> 324 #include <net/wpa.h> 325 326 #define IMPL_HASHSZ 67 /* prime */ 327 328 kmem_cache_t *i_mac_impl_cachep; 329 mod_hash_t *i_mac_impl_hash; 330 krwlock_t i_mac_impl_lock; 331 uint_t i_mac_impl_count; 332 static kmem_cache_t *mac_ring_cache; 333 static id_space_t *minor_ids; 334 static uint32_t minor_count; 335 static pool_event_cb_t mac_pool_event_reg; 336 337 /* 338 * Logging stuff. Perhaps mac_logging_interval could be broken into 339 * mac_flow_log_interval and mac_link_log_interval if we want to be 340 * able to schedule them differently. 341 */ 342 uint_t mac_logging_interval; 343 boolean_t mac_flow_log_enable; 344 boolean_t mac_link_log_enable; 345 timeout_id_t mac_logging_timer; 346 347 #define MACTYPE_KMODDIR "mac" 348 #define MACTYPE_HASHSZ 67 349 static mod_hash_t *i_mactype_hash; 350 /* 351 * i_mactype_lock synchronizes threads that obtain references to mactype_t 352 * structures through i_mactype_getplugin(). 353 */ 354 static kmutex_t i_mactype_lock; 355 356 /* 357 * mac_tx_percpu_cnt 358 * 359 * Number of per cpu locks per mac_client_impl_t. Used by the transmit side 360 * in mac_tx to reduce lock contention. This is sized at boot time in mac_init. 361 * mac_tx_percpu_cnt_max is settable in /etc/system and must be a power of 2. 362 * Per cpu locks may be disabled by setting mac_tx_percpu_cnt_max to 1. 363 */ 364 int mac_tx_percpu_cnt; 365 int mac_tx_percpu_cnt_max = 128; 366 367 /* 368 * Call back functions for the bridge module. These are guaranteed to be valid 369 * when holding a reference on a link or when holding mip->mi_bridge_lock and 370 * mi_bridge_link is non-NULL. 371 */ 372 mac_bridge_tx_t mac_bridge_tx_cb; 373 mac_bridge_rx_t mac_bridge_rx_cb; 374 mac_bridge_ref_t mac_bridge_ref_cb; 375 mac_bridge_ls_t mac_bridge_ls_cb; 376 377 static int i_mac_constructor(void *, void *, int); 378 static void i_mac_destructor(void *, void *); 379 static int i_mac_ring_ctor(void *, void *, int); 380 static void i_mac_ring_dtor(void *, void *); 381 static mblk_t *mac_rx_classify(mac_impl_t *, mac_resource_handle_t, mblk_t *); 382 void mac_tx_client_flush(mac_client_impl_t *); 383 void mac_tx_client_block(mac_client_impl_t *); 384 static void mac_rx_ring_quiesce(mac_ring_t *, uint_t); 385 static int mac_start_group_and_rings(mac_group_t *); 386 static void mac_stop_group_and_rings(mac_group_t *); 387 static void mac_pool_event_cb(pool_event_t, int, void *); 388 389 typedef struct netinfo_s { 390 list_node_t ni_link; 391 void *ni_record; 392 int ni_size; 393 int ni_type; 394 } netinfo_t; 395 396 /* 397 * Module initialization functions. 398 */ 399 400 void 401 mac_init(void) 402 { 403 mac_tx_percpu_cnt = ((boot_max_ncpus == -1) ? max_ncpus : 404 boot_max_ncpus); 405 406 /* Upper bound is mac_tx_percpu_cnt_max */ 407 if (mac_tx_percpu_cnt > mac_tx_percpu_cnt_max) 408 mac_tx_percpu_cnt = mac_tx_percpu_cnt_max; 409 410 if (mac_tx_percpu_cnt < 1) { 411 /* Someone set max_tx_percpu_cnt_max to 0 or less */ 412 mac_tx_percpu_cnt = 1; 413 } 414 415 ASSERT(mac_tx_percpu_cnt >= 1); 416 mac_tx_percpu_cnt = (1 << highbit(mac_tx_percpu_cnt - 1)); 417 /* 418 * Make it of the form 2**N - 1 in the range 419 * [0 .. mac_tx_percpu_cnt_max - 1] 420 */ 421 mac_tx_percpu_cnt--; 422 423 i_mac_impl_cachep = kmem_cache_create("mac_impl_cache", 424 sizeof (mac_impl_t), 0, i_mac_constructor, i_mac_destructor, 425 NULL, NULL, NULL, 0); 426 ASSERT(i_mac_impl_cachep != NULL); 427 428 mac_ring_cache = kmem_cache_create("mac_ring_cache", 429 sizeof (mac_ring_t), 0, i_mac_ring_ctor, i_mac_ring_dtor, NULL, 430 NULL, NULL, 0); 431 ASSERT(mac_ring_cache != NULL); 432 433 i_mac_impl_hash = mod_hash_create_extended("mac_impl_hash", 434 IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor, 435 mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP); 436 rw_init(&i_mac_impl_lock, NULL, RW_DEFAULT, NULL); 437 438 mac_flow_init(); 439 mac_soft_ring_init(); 440 mac_bcast_init(); 441 mac_client_init(); 442 443 i_mac_impl_count = 0; 444 445 i_mactype_hash = mod_hash_create_extended("mactype_hash", 446 MACTYPE_HASHSZ, 447 mod_hash_null_keydtor, mod_hash_null_valdtor, 448 mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP); 449 450 /* 451 * Allocate an id space to manage minor numbers. The range of the 452 * space will be from MAC_MAX_MINOR+1 to MAC_PRIVATE_MINOR-1. This 453 * leaves half of the 32-bit minors available for driver private use. 454 */ 455 minor_ids = id_space_create("mac_minor_ids", MAC_MAX_MINOR+1, 456 MAC_PRIVATE_MINOR-1); 457 ASSERT(minor_ids != NULL); 458 minor_count = 0; 459 460 /* Let's default to 20 seconds */ 461 mac_logging_interval = 20; 462 mac_flow_log_enable = B_FALSE; 463 mac_link_log_enable = B_FALSE; 464 mac_logging_timer = NULL; 465 466 /* Register to be notified of noteworthy pools events */ 467 mac_pool_event_reg.pec_func = mac_pool_event_cb; 468 mac_pool_event_reg.pec_arg = NULL; 469 pool_event_cb_register(&mac_pool_event_reg); 470 } 471 472 int 473 mac_fini(void) 474 { 475 476 if (i_mac_impl_count > 0 || minor_count > 0) 477 return (EBUSY); 478 479 pool_event_cb_unregister(&mac_pool_event_reg); 480 481 id_space_destroy(minor_ids); 482 mac_flow_fini(); 483 484 mod_hash_destroy_hash(i_mac_impl_hash); 485 rw_destroy(&i_mac_impl_lock); 486 487 mac_client_fini(); 488 kmem_cache_destroy(mac_ring_cache); 489 490 mod_hash_destroy_hash(i_mactype_hash); 491 mac_soft_ring_finish(); 492 493 494 return (0); 495 } 496 497 /* 498 * Initialize a GLDv3 driver's device ops. A driver that manages its own ops 499 * (e.g. softmac) may pass in a NULL ops argument. 500 */ 501 void 502 mac_init_ops(struct dev_ops *ops, const char *name) 503 { 504 major_t major = ddi_name_to_major((char *)name); 505 506 /* 507 * By returning on error below, we are not letting the driver continue 508 * in an undefined context. The mac_register() function will faill if 509 * DN_GLDV3_DRIVER isn't set. 510 */ 511 if (major == DDI_MAJOR_T_NONE) 512 return; 513 LOCK_DEV_OPS(&devnamesp[major].dn_lock); 514 devnamesp[major].dn_flags |= (DN_GLDV3_DRIVER | DN_NETWORK_DRIVER); 515 UNLOCK_DEV_OPS(&devnamesp[major].dn_lock); 516 if (ops != NULL) 517 dld_init_ops(ops, name); 518 } 519 520 void 521 mac_fini_ops(struct dev_ops *ops) 522 { 523 dld_fini_ops(ops); 524 } 525 526 /*ARGSUSED*/ 527 static int 528 i_mac_constructor(void *buf, void *arg, int kmflag) 529 { 530 mac_impl_t *mip = buf; 531 532 bzero(buf, sizeof (mac_impl_t)); 533 534 mip->mi_linkstate = LINK_STATE_UNKNOWN; 535 536 rw_init(&mip->mi_rw_lock, NULL, RW_DRIVER, NULL); 537 mutex_init(&mip->mi_notify_lock, NULL, MUTEX_DRIVER, NULL); 538 mutex_init(&mip->mi_promisc_lock, NULL, MUTEX_DRIVER, NULL); 539 mutex_init(&mip->mi_ring_lock, NULL, MUTEX_DEFAULT, NULL); 540 541 mip->mi_notify_cb_info.mcbi_lockp = &mip->mi_notify_lock; 542 cv_init(&mip->mi_notify_cb_info.mcbi_cv, NULL, CV_DRIVER, NULL); 543 mip->mi_promisc_cb_info.mcbi_lockp = &mip->mi_promisc_lock; 544 cv_init(&mip->mi_promisc_cb_info.mcbi_cv, NULL, CV_DRIVER, NULL); 545 546 mutex_init(&mip->mi_bridge_lock, NULL, MUTEX_DEFAULT, NULL); 547 548 return (0); 549 } 550 551 /*ARGSUSED*/ 552 static void 553 i_mac_destructor(void *buf, void *arg) 554 { 555 mac_impl_t *mip = buf; 556 mac_cb_info_t *mcbi; 557 558 ASSERT(mip->mi_ref == 0); 559 ASSERT(mip->mi_active == 0); 560 ASSERT(mip->mi_linkstate == LINK_STATE_UNKNOWN); 561 ASSERT(mip->mi_devpromisc == 0); 562 ASSERT(mip->mi_ksp == NULL); 563 ASSERT(mip->mi_kstat_count == 0); 564 ASSERT(mip->mi_nclients == 0); 565 ASSERT(mip->mi_nactiveclients == 0); 566 ASSERT(mip->mi_single_active_client == NULL); 567 ASSERT(mip->mi_state_flags == 0); 568 ASSERT(mip->mi_factory_addr == NULL); 569 ASSERT(mip->mi_factory_addr_num == 0); 570 ASSERT(mip->mi_default_tx_ring == NULL); 571 572 mcbi = &mip->mi_notify_cb_info; 573 ASSERT(mcbi->mcbi_del_cnt == 0 && mcbi->mcbi_walker_cnt == 0); 574 ASSERT(mip->mi_notify_bits == 0); 575 ASSERT(mip->mi_notify_thread == NULL); 576 ASSERT(mcbi->mcbi_lockp == &mip->mi_notify_lock); 577 mcbi->mcbi_lockp = NULL; 578 579 mcbi = &mip->mi_promisc_cb_info; 580 ASSERT(mcbi->mcbi_del_cnt == 0 && mip->mi_promisc_list == NULL); 581 ASSERT(mip->mi_promisc_list == NULL); 582 ASSERT(mcbi->mcbi_lockp == &mip->mi_promisc_lock); 583 mcbi->mcbi_lockp = NULL; 584 585 ASSERT(mip->mi_bcast_ngrps == 0 && mip->mi_bcast_grp == NULL); 586 ASSERT(mip->mi_perim_owner == NULL && mip->mi_perim_ocnt == 0); 587 588 rw_destroy(&mip->mi_rw_lock); 589 590 mutex_destroy(&mip->mi_promisc_lock); 591 cv_destroy(&mip->mi_promisc_cb_info.mcbi_cv); 592 mutex_destroy(&mip->mi_notify_lock); 593 cv_destroy(&mip->mi_notify_cb_info.mcbi_cv); 594 mutex_destroy(&mip->mi_ring_lock); 595 596 ASSERT(mip->mi_bridge_link == NULL); 597 } 598 599 /* ARGSUSED */ 600 static int 601 i_mac_ring_ctor(void *buf, void *arg, int kmflag) 602 { 603 mac_ring_t *ring = (mac_ring_t *)buf; 604 605 bzero(ring, sizeof (mac_ring_t)); 606 cv_init(&ring->mr_cv, NULL, CV_DEFAULT, NULL); 607 mutex_init(&ring->mr_lock, NULL, MUTEX_DEFAULT, NULL); 608 ring->mr_state = MR_FREE; 609 return (0); 610 } 611 612 /* ARGSUSED */ 613 static void 614 i_mac_ring_dtor(void *buf, void *arg) 615 { 616 mac_ring_t *ring = (mac_ring_t *)buf; 617 618 cv_destroy(&ring->mr_cv); 619 mutex_destroy(&ring->mr_lock); 620 } 621 622 /* 623 * Common functions to do mac callback addition and deletion. Currently this is 624 * used by promisc callbacks and notify callbacks. List addition and deletion 625 * need to take care of list walkers. List walkers in general, can't hold list 626 * locks and make upcall callbacks due to potential lock order and recursive 627 * reentry issues. Instead list walkers increment the list walker count to mark 628 * the presence of a walker thread. Addition can be carefully done to ensure 629 * that the list walker always sees either the old list or the new list. 630 * However the deletion can't be done while the walker is active, instead the 631 * deleting thread simply marks the entry as logically deleted. The last walker 632 * physically deletes and frees up the logically deleted entries when the walk 633 * is complete. 634 */ 635 void 636 mac_callback_add(mac_cb_info_t *mcbi, mac_cb_t **mcb_head, 637 mac_cb_t *mcb_elem) 638 { 639 mac_cb_t *p; 640 mac_cb_t **pp; 641 642 /* Verify it is not already in the list */ 643 for (pp = mcb_head; (p = *pp) != NULL; pp = &p->mcb_nextp) { 644 if (p == mcb_elem) 645 break; 646 } 647 VERIFY(p == NULL); 648 649 /* 650 * Add it to the head of the callback list. The membar ensures that 651 * the following list pointer manipulations reach global visibility 652 * in exactly the program order below. 653 */ 654 ASSERT(MUTEX_HELD(mcbi->mcbi_lockp)); 655 656 mcb_elem->mcb_nextp = *mcb_head; 657 membar_producer(); 658 *mcb_head = mcb_elem; 659 } 660 661 /* 662 * Mark the entry as logically deleted. If there aren't any walkers unlink 663 * from the list. In either case return the corresponding status. 664 */ 665 boolean_t 666 mac_callback_remove(mac_cb_info_t *mcbi, mac_cb_t **mcb_head, 667 mac_cb_t *mcb_elem) 668 { 669 mac_cb_t *p; 670 mac_cb_t **pp; 671 672 ASSERT(MUTEX_HELD(mcbi->mcbi_lockp)); 673 /* 674 * Search the callback list for the entry to be removed 675 */ 676 for (pp = mcb_head; (p = *pp) != NULL; pp = &p->mcb_nextp) { 677 if (p == mcb_elem) 678 break; 679 } 680 VERIFY(p != NULL); 681 682 /* 683 * If there are walkers just mark it as deleted and the last walker 684 * will remove from the list and free it. 685 */ 686 if (mcbi->mcbi_walker_cnt != 0) { 687 p->mcb_flags |= MCB_CONDEMNED; 688 mcbi->mcbi_del_cnt++; 689 return (B_FALSE); 690 } 691 692 ASSERT(mcbi->mcbi_del_cnt == 0); 693 *pp = p->mcb_nextp; 694 p->mcb_nextp = NULL; 695 return (B_TRUE); 696 } 697 698 /* 699 * Wait for all pending callback removals to be completed 700 */ 701 void 702 mac_callback_remove_wait(mac_cb_info_t *mcbi) 703 { 704 ASSERT(MUTEX_HELD(mcbi->mcbi_lockp)); 705 while (mcbi->mcbi_del_cnt != 0) { 706 DTRACE_PROBE1(need_wait, mac_cb_info_t *, mcbi); 707 cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp); 708 } 709 } 710 711 void 712 mac_callback_barrier(mac_cb_info_t *mcbi) 713 { 714 ASSERT(MUTEX_HELD(mcbi->mcbi_lockp)); 715 ASSERT3U(mcbi->mcbi_barrier_cnt, <, UINT_MAX); 716 717 if (mcbi->mcbi_walker_cnt == 0) { 718 return; 719 } 720 721 mcbi->mcbi_barrier_cnt++; 722 do { 723 cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp); 724 } while (mcbi->mcbi_walker_cnt > 0); 725 mcbi->mcbi_barrier_cnt--; 726 cv_broadcast(&mcbi->mcbi_cv); 727 } 728 729 void 730 mac_callback_walker_enter(mac_cb_info_t *mcbi) 731 { 732 mutex_enter(mcbi->mcbi_lockp); 733 /* 734 * Incoming walkers should give precedence to timely clean-up of 735 * deleted callback entries and requested barriers. 736 */ 737 while (mcbi->mcbi_del_cnt > 0 || mcbi->mcbi_barrier_cnt > 0) { 738 cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp); 739 } 740 mcbi->mcbi_walker_cnt++; 741 mutex_exit(mcbi->mcbi_lockp); 742 } 743 744 /* 745 * The last mac callback walker does the cleanup. Walk the list and unlik 746 * all the logically deleted entries and construct a temporary list of 747 * removed entries. Return the list of removed entries to the caller. 748 */ 749 static mac_cb_t * 750 mac_callback_walker_cleanup(mac_cb_info_t *mcbi, mac_cb_t **mcb_head) 751 { 752 mac_cb_t *p; 753 mac_cb_t **pp; 754 mac_cb_t *rmlist = NULL; /* List of removed elements */ 755 int cnt = 0; 756 757 ASSERT(MUTEX_HELD(mcbi->mcbi_lockp)); 758 ASSERT(mcbi->mcbi_del_cnt != 0 && mcbi->mcbi_walker_cnt == 0); 759 760 pp = mcb_head; 761 while (*pp != NULL) { 762 if ((*pp)->mcb_flags & MCB_CONDEMNED) { 763 p = *pp; 764 *pp = p->mcb_nextp; 765 p->mcb_nextp = rmlist; 766 rmlist = p; 767 cnt++; 768 continue; 769 } 770 pp = &(*pp)->mcb_nextp; 771 } 772 773 ASSERT(mcbi->mcbi_del_cnt == cnt); 774 mcbi->mcbi_del_cnt = 0; 775 return (rmlist); 776 } 777 778 void 779 mac_callback_walker_exit(mac_cb_info_t *mcbi, mac_cb_t **headp, 780 boolean_t is_promisc) 781 { 782 boolean_t do_wake = B_FALSE; 783 784 mutex_enter(mcbi->mcbi_lockp); 785 786 /* If walkers remain, nothing more can be done for now */ 787 if (--mcbi->mcbi_walker_cnt != 0) { 788 mutex_exit(mcbi->mcbi_lockp); 789 return; 790 } 791 792 if (mcbi->mcbi_del_cnt != 0) { 793 mac_cb_t *rmlist; 794 795 rmlist = mac_callback_walker_cleanup(mcbi, headp); 796 797 if (!is_promisc) { 798 /* The "normal" non-promisc callback clean-up */ 799 mac_callback_free(rmlist); 800 } else { 801 mac_cb_t *mcb, *mcb_next; 802 803 /* 804 * The promisc callbacks are in 2 lists, one off the 805 * 'mip' and another off the 'mcip' threaded by 806 * mpi_mi_link and mpi_mci_link respectively. There 807 * is, however, only a single shared total walker 808 * count, and an entry cannot be physically unlinked if 809 * a walker is active on either list. The last walker 810 * does this cleanup of logically deleted entries. 811 * 812 * With a list of callbacks deleted from above from 813 * mi_promisc_list (headp), remove the corresponding 814 * entry from mci_promisc_list (headp_pair) and free 815 * the structure. 816 */ 817 for (mcb = rmlist; mcb != NULL; mcb = mcb_next) { 818 mac_promisc_impl_t *mpip; 819 mac_client_impl_t *mcip; 820 821 mcb_next = mcb->mcb_nextp; 822 mpip = (mac_promisc_impl_t *)mcb->mcb_objp; 823 mcip = mpip->mpi_mcip; 824 825 ASSERT3P(&mcip->mci_mip->mi_promisc_cb_info, 826 ==, mcbi); 827 ASSERT3P(&mcip->mci_mip->mi_promisc_list, 828 ==, headp); 829 830 VERIFY(mac_callback_remove(mcbi, 831 &mcip->mci_promisc_list, 832 &mpip->mpi_mci_link)); 833 mcb->mcb_flags = 0; 834 mcb->mcb_nextp = NULL; 835 kmem_cache_free(mac_promisc_impl_cache, mpip); 836 } 837 } 838 839 /* 840 * Wake any walker threads that could be waiting in 841 * mac_callback_walker_enter() until deleted items have been 842 * cleaned from the list. 843 */ 844 do_wake = B_TRUE; 845 } 846 847 if (mcbi->mcbi_barrier_cnt != 0) { 848 /* 849 * One or more threads are waiting for all walkers to exit the 850 * callback list. Notify them, now that the list is clear. 851 */ 852 do_wake = B_TRUE; 853 } 854 855 if (do_wake) { 856 cv_broadcast(&mcbi->mcbi_cv); 857 } 858 mutex_exit(mcbi->mcbi_lockp); 859 } 860 861 static boolean_t 862 mac_callback_lookup(mac_cb_t **mcb_headp, mac_cb_t *mcb_elem) 863 { 864 mac_cb_t *mcb; 865 866 /* Verify it is not already in the list */ 867 for (mcb = *mcb_headp; mcb != NULL; mcb = mcb->mcb_nextp) { 868 if (mcb == mcb_elem) 869 return (B_TRUE); 870 } 871 872 return (B_FALSE); 873 } 874 875 static boolean_t 876 mac_callback_find(mac_cb_info_t *mcbi, mac_cb_t **mcb_headp, mac_cb_t *mcb_elem) 877 { 878 boolean_t found; 879 880 mutex_enter(mcbi->mcbi_lockp); 881 found = mac_callback_lookup(mcb_headp, mcb_elem); 882 mutex_exit(mcbi->mcbi_lockp); 883 884 return (found); 885 } 886 887 /* Free the list of removed callbacks */ 888 void 889 mac_callback_free(mac_cb_t *rmlist) 890 { 891 mac_cb_t *mcb; 892 mac_cb_t *mcb_next; 893 894 for (mcb = rmlist; mcb != NULL; mcb = mcb_next) { 895 mcb_next = mcb->mcb_nextp; 896 kmem_free(mcb->mcb_objp, mcb->mcb_objsize); 897 } 898 } 899 900 void 901 i_mac_notify(mac_impl_t *mip, mac_notify_type_t type) 902 { 903 mac_cb_info_t *mcbi; 904 905 /* 906 * Signal the notify thread even after mi_ref has become zero and 907 * mi_disabled is set. The synchronization with the notify thread 908 * happens in mac_unregister and that implies the driver must make 909 * sure it is single-threaded (with respect to mac calls) and that 910 * all pending mac calls have returned before it calls mac_unregister 911 */ 912 rw_enter(&i_mac_impl_lock, RW_READER); 913 if (mip->mi_state_flags & MIS_DISABLED) 914 goto exit; 915 916 /* 917 * Guard against incorrect notifications. (Running a newer 918 * mac client against an older implementation?) 919 */ 920 if (type >= MAC_NNOTE) 921 goto exit; 922 923 mcbi = &mip->mi_notify_cb_info; 924 mutex_enter(mcbi->mcbi_lockp); 925 mip->mi_notify_bits |= (1 << type); 926 cv_broadcast(&mcbi->mcbi_cv); 927 mutex_exit(mcbi->mcbi_lockp); 928 929 exit: 930 rw_exit(&i_mac_impl_lock); 931 } 932 933 /* 934 * Mac serialization primitives. Please see the block comment at the 935 * top of the file. 936 */ 937 void 938 i_mac_perim_enter(mac_impl_t *mip) 939 { 940 mac_client_impl_t *mcip; 941 942 if (mip->mi_state_flags & MIS_IS_VNIC) { 943 /* 944 * This is a VNIC. Return the lower mac since that is what 945 * we want to serialize on. 946 */ 947 mcip = mac_vnic_lower(mip); 948 mip = mcip->mci_mip; 949 } 950 951 mutex_enter(&mip->mi_perim_lock); 952 if (mip->mi_perim_owner == curthread) { 953 mip->mi_perim_ocnt++; 954 mutex_exit(&mip->mi_perim_lock); 955 return; 956 } 957 958 while (mip->mi_perim_owner != NULL) 959 cv_wait(&mip->mi_perim_cv, &mip->mi_perim_lock); 960 961 mip->mi_perim_owner = curthread; 962 ASSERT(mip->mi_perim_ocnt == 0); 963 mip->mi_perim_ocnt++; 964 #ifdef DEBUG 965 mip->mi_perim_stack_depth = getpcstack(mip->mi_perim_stack, 966 MAC_PERIM_STACK_DEPTH); 967 #endif 968 mutex_exit(&mip->mi_perim_lock); 969 } 970 971 int 972 i_mac_perim_enter_nowait(mac_impl_t *mip) 973 { 974 /* 975 * The vnic is a special case, since the serialization is done based 976 * on the lower mac. If the lower mac is busy, it does not imply the 977 * vnic can't be unregistered. But in the case of other drivers, 978 * a busy perimeter or open mac handles implies that the mac is busy 979 * and can't be unregistered. 980 */ 981 if (mip->mi_state_flags & MIS_IS_VNIC) { 982 i_mac_perim_enter(mip); 983 return (0); 984 } 985 986 mutex_enter(&mip->mi_perim_lock); 987 if (mip->mi_perim_owner != NULL) { 988 mutex_exit(&mip->mi_perim_lock); 989 return (EBUSY); 990 } 991 ASSERT(mip->mi_perim_ocnt == 0); 992 mip->mi_perim_owner = curthread; 993 mip->mi_perim_ocnt++; 994 mutex_exit(&mip->mi_perim_lock); 995 996 return (0); 997 } 998 999 void 1000 i_mac_perim_exit(mac_impl_t *mip) 1001 { 1002 mac_client_impl_t *mcip; 1003 1004 if (mip->mi_state_flags & MIS_IS_VNIC) { 1005 /* 1006 * This is a VNIC. Return the lower mac since that is what 1007 * we want to serialize on. 1008 */ 1009 mcip = mac_vnic_lower(mip); 1010 mip = mcip->mci_mip; 1011 } 1012 1013 ASSERT(mip->mi_perim_owner == curthread && mip->mi_perim_ocnt != 0); 1014 1015 mutex_enter(&mip->mi_perim_lock); 1016 if (--mip->mi_perim_ocnt == 0) { 1017 mip->mi_perim_owner = NULL; 1018 cv_signal(&mip->mi_perim_cv); 1019 } 1020 mutex_exit(&mip->mi_perim_lock); 1021 } 1022 1023 /* 1024 * Returns whether the current thread holds the mac perimeter. Used in making 1025 * assertions. 1026 */ 1027 boolean_t 1028 mac_perim_held(mac_handle_t mh) 1029 { 1030 mac_impl_t *mip = (mac_impl_t *)mh; 1031 mac_client_impl_t *mcip; 1032 1033 if (mip->mi_state_flags & MIS_IS_VNIC) { 1034 /* 1035 * This is a VNIC. Return the lower mac since that is what 1036 * we want to serialize on. 1037 */ 1038 mcip = mac_vnic_lower(mip); 1039 mip = mcip->mci_mip; 1040 } 1041 return (mip->mi_perim_owner == curthread); 1042 } 1043 1044 /* 1045 * mac client interfaces to enter the mac perimeter of a mac end point, given 1046 * its mac handle, or macname or linkid. 1047 */ 1048 void 1049 mac_perim_enter_by_mh(mac_handle_t mh, mac_perim_handle_t *mphp) 1050 { 1051 mac_impl_t *mip = (mac_impl_t *)mh; 1052 1053 i_mac_perim_enter(mip); 1054 /* 1055 * The mac_perim_handle_t returned encodes the 'mip' and whether a 1056 * mac_open has been done internally while entering the perimeter. 1057 * This information is used in mac_perim_exit 1058 */ 1059 MAC_ENCODE_MPH(*mphp, mip, 0); 1060 } 1061 1062 int 1063 mac_perim_enter_by_macname(const char *name, mac_perim_handle_t *mphp) 1064 { 1065 int err; 1066 mac_handle_t mh; 1067 1068 if ((err = mac_open(name, &mh)) != 0) 1069 return (err); 1070 1071 mac_perim_enter_by_mh(mh, mphp); 1072 MAC_ENCODE_MPH(*mphp, mh, 1); 1073 return (0); 1074 } 1075 1076 int 1077 mac_perim_enter_by_linkid(datalink_id_t linkid, mac_perim_handle_t *mphp) 1078 { 1079 int err; 1080 mac_handle_t mh; 1081 1082 if ((err = mac_open_by_linkid(linkid, &mh)) != 0) 1083 return (err); 1084 1085 mac_perim_enter_by_mh(mh, mphp); 1086 MAC_ENCODE_MPH(*mphp, mh, 1); 1087 return (0); 1088 } 1089 1090 void 1091 mac_perim_exit(mac_perim_handle_t mph) 1092 { 1093 mac_impl_t *mip; 1094 boolean_t need_close; 1095 1096 MAC_DECODE_MPH(mph, mip, need_close); 1097 i_mac_perim_exit(mip); 1098 if (need_close) 1099 mac_close((mac_handle_t)mip); 1100 } 1101 1102 int 1103 mac_hold(const char *macname, mac_impl_t **pmip) 1104 { 1105 mac_impl_t *mip; 1106 int err; 1107 1108 /* 1109 * Check the device name length to make sure it won't overflow our 1110 * buffer. 1111 */ 1112 if (strlen(macname) >= MAXNAMELEN) 1113 return (EINVAL); 1114 1115 /* 1116 * Look up its entry in the global hash table. 1117 */ 1118 rw_enter(&i_mac_impl_lock, RW_WRITER); 1119 err = mod_hash_find(i_mac_impl_hash, (mod_hash_key_t)macname, 1120 (mod_hash_val_t *)&mip); 1121 1122 if (err != 0) { 1123 rw_exit(&i_mac_impl_lock); 1124 return (ENOENT); 1125 } 1126 1127 if (mip->mi_state_flags & MIS_DISABLED) { 1128 rw_exit(&i_mac_impl_lock); 1129 return (ENOENT); 1130 } 1131 1132 if (mip->mi_state_flags & MIS_EXCLUSIVE_HELD) { 1133 rw_exit(&i_mac_impl_lock); 1134 return (EBUSY); 1135 } 1136 1137 mip->mi_ref++; 1138 rw_exit(&i_mac_impl_lock); 1139 1140 *pmip = mip; 1141 return (0); 1142 } 1143 1144 void 1145 mac_rele(mac_impl_t *mip) 1146 { 1147 rw_enter(&i_mac_impl_lock, RW_WRITER); 1148 ASSERT(mip->mi_ref != 0); 1149 if (--mip->mi_ref == 0) { 1150 ASSERT(mip->mi_nactiveclients == 0 && 1151 !(mip->mi_state_flags & MIS_EXCLUSIVE)); 1152 } 1153 rw_exit(&i_mac_impl_lock); 1154 } 1155 1156 /* 1157 * Private GLDv3 function to start a MAC instance. 1158 */ 1159 int 1160 mac_start(mac_handle_t mh) 1161 { 1162 mac_impl_t *mip = (mac_impl_t *)mh; 1163 int err = 0; 1164 mac_group_t *defgrp; 1165 1166 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 1167 ASSERT(mip->mi_start != NULL); 1168 1169 /* 1170 * Check whether the device is already started. 1171 */ 1172 if (mip->mi_active++ == 0) { 1173 mac_ring_t *ring = NULL; 1174 1175 /* 1176 * Start the device. 1177 */ 1178 err = mip->mi_start(mip->mi_driver); 1179 if (err != 0) { 1180 mip->mi_active--; 1181 return (err); 1182 } 1183 1184 /* 1185 * Start the default tx ring. 1186 */ 1187 if (mip->mi_default_tx_ring != NULL) { 1188 1189 ring = (mac_ring_t *)mip->mi_default_tx_ring; 1190 if (ring->mr_state != MR_INUSE) { 1191 err = mac_start_ring(ring); 1192 if (err != 0) { 1193 mip->mi_active--; 1194 return (err); 1195 } 1196 } 1197 } 1198 1199 if ((defgrp = MAC_DEFAULT_RX_GROUP(mip)) != NULL) { 1200 /* 1201 * Start the default group which is responsible 1202 * for receiving broadcast and multicast 1203 * traffic for both primary and non-primary 1204 * MAC clients. 1205 */ 1206 ASSERT(defgrp->mrg_state == MAC_GROUP_STATE_REGISTERED); 1207 err = mac_start_group_and_rings(defgrp); 1208 if (err != 0) { 1209 mip->mi_active--; 1210 if ((ring != NULL) && 1211 (ring->mr_state == MR_INUSE)) 1212 mac_stop_ring(ring); 1213 return (err); 1214 } 1215 mac_set_group_state(defgrp, MAC_GROUP_STATE_SHARED); 1216 } 1217 } 1218 1219 return (err); 1220 } 1221 1222 /* 1223 * Private GLDv3 function to stop a MAC instance. 1224 */ 1225 void 1226 mac_stop(mac_handle_t mh) 1227 { 1228 mac_impl_t *mip = (mac_impl_t *)mh; 1229 mac_group_t *grp; 1230 1231 ASSERT(mip->mi_stop != NULL); 1232 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 1233 1234 /* 1235 * Check whether the device is still needed. 1236 */ 1237 ASSERT(mip->mi_active != 0); 1238 if (--mip->mi_active == 0) { 1239 if ((grp = MAC_DEFAULT_RX_GROUP(mip)) != NULL) { 1240 /* 1241 * There should be no more active clients since the 1242 * MAC is being stopped. Stop the default RX group 1243 * and transition it back to registered state. 1244 * 1245 * When clients are torn down, the groups 1246 * are release via mac_release_rx_group which 1247 * knows the the default group is always in 1248 * started mode since broadcast uses it. So 1249 * we can assert that their are no clients 1250 * (since mac_bcast_add doesn't register itself 1251 * as a client) and group is in SHARED state. 1252 */ 1253 ASSERT(grp->mrg_state == MAC_GROUP_STATE_SHARED); 1254 ASSERT(MAC_GROUP_NO_CLIENT(grp) && 1255 mip->mi_nactiveclients == 0); 1256 mac_stop_group_and_rings(grp); 1257 mac_set_group_state(grp, MAC_GROUP_STATE_REGISTERED); 1258 } 1259 1260 if (mip->mi_default_tx_ring != NULL) { 1261 mac_ring_t *ring; 1262 1263 ring = (mac_ring_t *)mip->mi_default_tx_ring; 1264 if (ring->mr_state == MR_INUSE) { 1265 mac_stop_ring(ring); 1266 ring->mr_flag = 0; 1267 } 1268 } 1269 1270 /* 1271 * Stop the device. 1272 */ 1273 mip->mi_stop(mip->mi_driver); 1274 } 1275 } 1276 1277 int 1278 i_mac_promisc_set(mac_impl_t *mip, boolean_t on) 1279 { 1280 int err = 0; 1281 1282 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 1283 ASSERT(mip->mi_setpromisc != NULL); 1284 1285 if (on) { 1286 /* 1287 * Enable promiscuous mode on the device if not yet enabled. 1288 */ 1289 if (mip->mi_devpromisc++ == 0) { 1290 err = mip->mi_setpromisc(mip->mi_driver, B_TRUE); 1291 if (err != 0) { 1292 mip->mi_devpromisc--; 1293 return (err); 1294 } 1295 i_mac_notify(mip, MAC_NOTE_DEVPROMISC); 1296 } 1297 } else { 1298 if (mip->mi_devpromisc == 0) 1299 return (EPROTO); 1300 1301 /* 1302 * Disable promiscuous mode on the device if this is the last 1303 * enabling. 1304 */ 1305 if (--mip->mi_devpromisc == 0) { 1306 err = mip->mi_setpromisc(mip->mi_driver, B_FALSE); 1307 if (err != 0) { 1308 mip->mi_devpromisc++; 1309 return (err); 1310 } 1311 i_mac_notify(mip, MAC_NOTE_DEVPROMISC); 1312 } 1313 } 1314 1315 return (0); 1316 } 1317 1318 /* 1319 * The promiscuity state can change any time. If the caller needs to take 1320 * actions that are atomic with the promiscuity state, then the caller needs 1321 * to bracket the entire sequence with mac_perim_enter/exit 1322 */ 1323 boolean_t 1324 mac_promisc_get(mac_handle_t mh) 1325 { 1326 mac_impl_t *mip = (mac_impl_t *)mh; 1327 1328 /* 1329 * Return the current promiscuity. 1330 */ 1331 return (mip->mi_devpromisc != 0); 1332 } 1333 1334 /* 1335 * Invoked at MAC instance attach time to initialize the list 1336 * of factory MAC addresses supported by a MAC instance. This function 1337 * builds a local cache in the mac_impl_t for the MAC addresses 1338 * supported by the underlying hardware. The MAC clients themselves 1339 * use the mac_addr_factory*() functions to query and reserve 1340 * factory MAC addresses. 1341 */ 1342 void 1343 mac_addr_factory_init(mac_impl_t *mip) 1344 { 1345 mac_capab_multifactaddr_t capab; 1346 uint8_t *addr; 1347 int i; 1348 1349 /* 1350 * First round to see how many factory MAC addresses are available. 1351 */ 1352 bzero(&capab, sizeof (capab)); 1353 if (!i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_MULTIFACTADDR, 1354 &capab) || (capab.mcm_naddr == 0)) { 1355 /* 1356 * The MAC instance doesn't support multiple factory 1357 * MAC addresses, we're done here. 1358 */ 1359 return; 1360 } 1361 1362 /* 1363 * Allocate the space and get all the factory addresses. 1364 */ 1365 addr = kmem_alloc(capab.mcm_naddr * MAXMACADDRLEN, KM_SLEEP); 1366 capab.mcm_getaddr(mip->mi_driver, capab.mcm_naddr, addr); 1367 1368 mip->mi_factory_addr_num = capab.mcm_naddr; 1369 mip->mi_factory_addr = kmem_zalloc(mip->mi_factory_addr_num * 1370 sizeof (mac_factory_addr_t), KM_SLEEP); 1371 1372 for (i = 0; i < capab.mcm_naddr; i++) { 1373 bcopy(addr + i * MAXMACADDRLEN, 1374 mip->mi_factory_addr[i].mfa_addr, 1375 mip->mi_type->mt_addr_length); 1376 mip->mi_factory_addr[i].mfa_in_use = B_FALSE; 1377 } 1378 1379 kmem_free(addr, capab.mcm_naddr * MAXMACADDRLEN); 1380 } 1381 1382 void 1383 mac_addr_factory_fini(mac_impl_t *mip) 1384 { 1385 if (mip->mi_factory_addr == NULL) { 1386 ASSERT(mip->mi_factory_addr_num == 0); 1387 return; 1388 } 1389 1390 kmem_free(mip->mi_factory_addr, mip->mi_factory_addr_num * 1391 sizeof (mac_factory_addr_t)); 1392 1393 mip->mi_factory_addr = NULL; 1394 mip->mi_factory_addr_num = 0; 1395 } 1396 1397 /* 1398 * Reserve a factory MAC address. If *slot is set to -1, the function 1399 * attempts to reserve any of the available factory MAC addresses and 1400 * returns the reserved slot id. If no slots are available, the function 1401 * returns ENOSPC. If *slot is not set to -1, the function reserves 1402 * the specified slot if it is available, or returns EBUSY is the slot 1403 * is already used. Returns ENOTSUP if the underlying MAC does not 1404 * support multiple factory addresses. If the slot number is not -1 but 1405 * is invalid, returns EINVAL. 1406 */ 1407 int 1408 mac_addr_factory_reserve(mac_client_handle_t mch, int *slot) 1409 { 1410 mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 1411 mac_impl_t *mip = mcip->mci_mip; 1412 int i, ret = 0; 1413 1414 i_mac_perim_enter(mip); 1415 /* 1416 * Protect against concurrent readers that may need a self-consistent 1417 * view of the factory addresses 1418 */ 1419 rw_enter(&mip->mi_rw_lock, RW_WRITER); 1420 1421 if (mip->mi_factory_addr_num == 0) { 1422 ret = ENOTSUP; 1423 goto bail; 1424 } 1425 1426 if (*slot != -1) { 1427 /* check the specified slot */ 1428 if (*slot < 1 || *slot > mip->mi_factory_addr_num) { 1429 ret = EINVAL; 1430 goto bail; 1431 } 1432 if (mip->mi_factory_addr[*slot-1].mfa_in_use) { 1433 ret = EBUSY; 1434 goto bail; 1435 } 1436 } else { 1437 /* pick the next available slot */ 1438 for (i = 0; i < mip->mi_factory_addr_num; i++) { 1439 if (!mip->mi_factory_addr[i].mfa_in_use) 1440 break; 1441 } 1442 1443 if (i == mip->mi_factory_addr_num) { 1444 ret = ENOSPC; 1445 goto bail; 1446 } 1447 *slot = i+1; 1448 } 1449 1450 mip->mi_factory_addr[*slot-1].mfa_in_use = B_TRUE; 1451 mip->mi_factory_addr[*slot-1].mfa_client = mcip; 1452 1453 bail: 1454 rw_exit(&mip->mi_rw_lock); 1455 i_mac_perim_exit(mip); 1456 return (ret); 1457 } 1458 1459 /* 1460 * Release the specified factory MAC address slot. 1461 */ 1462 void 1463 mac_addr_factory_release(mac_client_handle_t mch, uint_t slot) 1464 { 1465 mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 1466 mac_impl_t *mip = mcip->mci_mip; 1467 1468 i_mac_perim_enter(mip); 1469 /* 1470 * Protect against concurrent readers that may need a self-consistent 1471 * view of the factory addresses 1472 */ 1473 rw_enter(&mip->mi_rw_lock, RW_WRITER); 1474 1475 ASSERT(slot > 0 && slot <= mip->mi_factory_addr_num); 1476 ASSERT(mip->mi_factory_addr[slot-1].mfa_in_use); 1477 1478 mip->mi_factory_addr[slot-1].mfa_in_use = B_FALSE; 1479 1480 rw_exit(&mip->mi_rw_lock); 1481 i_mac_perim_exit(mip); 1482 } 1483 1484 /* 1485 * Stores in mac_addr the value of the specified MAC address. Returns 1486 * 0 on success, or EINVAL if the slot number is not valid for the MAC. 1487 * The caller must provide a string of at least MAXNAMELEN bytes. 1488 */ 1489 void 1490 mac_addr_factory_value(mac_handle_t mh, int slot, uchar_t *mac_addr, 1491 uint_t *addr_len, char *client_name, boolean_t *in_use_arg) 1492 { 1493 mac_impl_t *mip = (mac_impl_t *)mh; 1494 boolean_t in_use; 1495 1496 ASSERT(slot > 0 && slot <= mip->mi_factory_addr_num); 1497 1498 /* 1499 * Readers need to hold mi_rw_lock. Writers need to hold mac perimeter 1500 * and mi_rw_lock 1501 */ 1502 rw_enter(&mip->mi_rw_lock, RW_READER); 1503 bcopy(mip->mi_factory_addr[slot-1].mfa_addr, mac_addr, MAXMACADDRLEN); 1504 *addr_len = mip->mi_type->mt_addr_length; 1505 in_use = mip->mi_factory_addr[slot-1].mfa_in_use; 1506 if (in_use && client_name != NULL) { 1507 bcopy(mip->mi_factory_addr[slot-1].mfa_client->mci_name, 1508 client_name, MAXNAMELEN); 1509 } 1510 if (in_use_arg != NULL) 1511 *in_use_arg = in_use; 1512 rw_exit(&mip->mi_rw_lock); 1513 } 1514 1515 /* 1516 * Returns the number of factory MAC addresses (in addition to the 1517 * primary MAC address), 0 if the underlying MAC doesn't support 1518 * that feature. 1519 */ 1520 uint_t 1521 mac_addr_factory_num(mac_handle_t mh) 1522 { 1523 mac_impl_t *mip = (mac_impl_t *)mh; 1524 1525 return (mip->mi_factory_addr_num); 1526 } 1527 1528 1529 void 1530 mac_rx_group_unmark(mac_group_t *grp, uint_t flag) 1531 { 1532 mac_ring_t *ring; 1533 1534 for (ring = grp->mrg_rings; ring != NULL; ring = ring->mr_next) 1535 ring->mr_flag &= ~flag; 1536 } 1537 1538 /* 1539 * The following mac_hwrings_xxx() functions are private mac client functions 1540 * used by the aggr driver to access and control the underlying HW Rx group 1541 * and rings. In this case, the aggr driver has exclusive control of the 1542 * underlying HW Rx group/rings, it calls the following functions to 1543 * start/stop the HW Rx rings, disable/enable polling, add/remove MAC 1544 * addresses, or set up the Rx callback. 1545 */ 1546 /* ARGSUSED */ 1547 static void 1548 mac_hwrings_rx_process(void *arg, mac_resource_handle_t srs, 1549 mblk_t *mp_chain, boolean_t loopback) 1550 { 1551 mac_soft_ring_set_t *mac_srs = (mac_soft_ring_set_t *)srs; 1552 mac_srs_rx_t *srs_rx = &mac_srs->srs_rx; 1553 mac_direct_rx_t proc; 1554 void *arg1; 1555 mac_resource_handle_t arg2; 1556 1557 proc = srs_rx->sr_func; 1558 arg1 = srs_rx->sr_arg1; 1559 arg2 = mac_srs->srs_mrh; 1560 1561 proc(arg1, arg2, mp_chain, NULL); 1562 } 1563 1564 /* 1565 * This function is called to get the list of HW rings that are reserved by 1566 * an exclusive mac client. 1567 * 1568 * Return value: the number of HW rings. 1569 */ 1570 int 1571 mac_hwrings_get(mac_client_handle_t mch, mac_group_handle_t *hwgh, 1572 mac_ring_handle_t *hwrh, mac_ring_type_t rtype) 1573 { 1574 mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 1575 flow_entry_t *flent = mcip->mci_flent; 1576 mac_group_t *grp; 1577 mac_ring_t *ring; 1578 int cnt = 0; 1579 1580 if (rtype == MAC_RING_TYPE_RX) { 1581 grp = flent->fe_rx_ring_group; 1582 } else if (rtype == MAC_RING_TYPE_TX) { 1583 grp = flent->fe_tx_ring_group; 1584 } else { 1585 ASSERT(B_FALSE); 1586 return (-1); 1587 } 1588 1589 /* 1590 * The MAC client did not reserve an Rx group, return directly. 1591 * This is probably because the underlying MAC does not support 1592 * any groups. 1593 */ 1594 if (hwgh != NULL) 1595 *hwgh = NULL; 1596 if (grp == NULL) 1597 return (0); 1598 /* 1599 * This group must be reserved by this MAC client. 1600 */ 1601 ASSERT((grp->mrg_state == MAC_GROUP_STATE_RESERVED) && 1602 (mcip == MAC_GROUP_ONLY_CLIENT(grp))); 1603 1604 for (ring = grp->mrg_rings; ring != NULL; ring = ring->mr_next, cnt++) { 1605 ASSERT(cnt < MAX_RINGS_PER_GROUP); 1606 hwrh[cnt] = (mac_ring_handle_t)ring; 1607 } 1608 if (hwgh != NULL) 1609 *hwgh = (mac_group_handle_t)grp; 1610 1611 return (cnt); 1612 } 1613 1614 /* 1615 * Get the HW ring handles of the given group index. If the MAC 1616 * doesn't have a group at this index, or any groups at all, then 0 is 1617 * returned and hwgh is set to NULL. This is a private client API. The 1618 * MAC perimeter must be held when calling this function. 1619 * 1620 * mh: A handle to the MAC that owns the group. 1621 * 1622 * idx: The index of the HW group to be read. 1623 * 1624 * hwgh: If non-NULL, contains a handle to the HW group on return. 1625 * 1626 * hwrh: An array of ring handles pointing to the HW rings in the 1627 * group. The array must be large enough to hold a handle to each ring 1628 * in the group. To be safe, this array should be of size MAX_RINGS_PER_GROUP. 1629 * 1630 * rtype: Used to determine if we are fetching Rx or Tx rings. 1631 * 1632 * Returns the number of rings in the group. 1633 */ 1634 uint_t 1635 mac_hwrings_idx_get(mac_handle_t mh, uint_t idx, mac_group_handle_t *hwgh, 1636 mac_ring_handle_t *hwrh, mac_ring_type_t rtype) 1637 { 1638 mac_impl_t *mip = (mac_impl_t *)mh; 1639 mac_group_t *grp; 1640 mac_ring_t *ring; 1641 uint_t cnt = 0; 1642 1643 /* 1644 * The MAC perimeter must be held when accessing the 1645 * mi_{rx,tx}_groups fields. 1646 */ 1647 ASSERT(MAC_PERIM_HELD(mh)); 1648 ASSERT(rtype == MAC_RING_TYPE_RX || rtype == MAC_RING_TYPE_TX); 1649 1650 if (rtype == MAC_RING_TYPE_RX) { 1651 grp = mip->mi_rx_groups; 1652 } else { 1653 ASSERT(rtype == MAC_RING_TYPE_TX); 1654 grp = mip->mi_tx_groups; 1655 } 1656 1657 while (grp != NULL && grp->mrg_index != idx) 1658 grp = grp->mrg_next; 1659 1660 /* 1661 * If the MAC doesn't have a group at this index or doesn't 1662 * impelement RINGS capab, then set hwgh to NULL and return 0. 1663 */ 1664 if (hwgh != NULL) 1665 *hwgh = NULL; 1666 1667 if (grp == NULL) 1668 return (0); 1669 1670 ASSERT3U(idx, ==, grp->mrg_index); 1671 1672 for (ring = grp->mrg_rings; ring != NULL; ring = ring->mr_next, cnt++) { 1673 ASSERT3U(cnt, <, MAX_RINGS_PER_GROUP); 1674 hwrh[cnt] = (mac_ring_handle_t)ring; 1675 } 1676 1677 /* A group should always have at least one ring. */ 1678 ASSERT3U(cnt, >, 0); 1679 1680 if (hwgh != NULL) 1681 *hwgh = (mac_group_handle_t)grp; 1682 1683 return (cnt); 1684 } 1685 1686 /* 1687 * This function is called to get info about Tx/Rx rings. 1688 * 1689 * Return value: returns uint_t which will have various bits set 1690 * that indicates different properties of the ring. 1691 */ 1692 uint_t 1693 mac_hwring_getinfo(mac_ring_handle_t rh) 1694 { 1695 mac_ring_t *ring = (mac_ring_t *)rh; 1696 mac_ring_info_t *info = &ring->mr_info; 1697 1698 return (info->mri_flags); 1699 } 1700 1701 /* 1702 * Set the passthru callback on the hardware ring. 1703 */ 1704 void 1705 mac_hwring_set_passthru(mac_ring_handle_t hwrh, mac_rx_t fn, void *arg1, 1706 mac_resource_handle_t arg2) 1707 { 1708 mac_ring_t *hwring = (mac_ring_t *)hwrh; 1709 1710 ASSERT3S(hwring->mr_type, ==, MAC_RING_TYPE_RX); 1711 1712 hwring->mr_classify_type = MAC_PASSTHRU_CLASSIFIER; 1713 1714 hwring->mr_pt_fn = fn; 1715 hwring->mr_pt_arg1 = arg1; 1716 hwring->mr_pt_arg2 = arg2; 1717 } 1718 1719 /* 1720 * Clear the passthru callback on the hardware ring. 1721 */ 1722 void 1723 mac_hwring_clear_passthru(mac_ring_handle_t hwrh) 1724 { 1725 mac_ring_t *hwring = (mac_ring_t *)hwrh; 1726 1727 ASSERT3S(hwring->mr_type, ==, MAC_RING_TYPE_RX); 1728 1729 hwring->mr_classify_type = MAC_NO_CLASSIFIER; 1730 1731 hwring->mr_pt_fn = NULL; 1732 hwring->mr_pt_arg1 = NULL; 1733 hwring->mr_pt_arg2 = NULL; 1734 } 1735 1736 void 1737 mac_client_set_flow_cb(mac_client_handle_t mch, mac_rx_t func, void *arg1) 1738 { 1739 mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 1740 flow_entry_t *flent = mcip->mci_flent; 1741 1742 mutex_enter(&flent->fe_lock); 1743 flent->fe_cb_fn = (flow_fn_t)func; 1744 flent->fe_cb_arg1 = arg1; 1745 flent->fe_cb_arg2 = NULL; 1746 flent->fe_flags &= ~FE_MC_NO_DATAPATH; 1747 mutex_exit(&flent->fe_lock); 1748 } 1749 1750 void 1751 mac_client_clear_flow_cb(mac_client_handle_t mch) 1752 { 1753 mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 1754 flow_entry_t *flent = mcip->mci_flent; 1755 1756 mutex_enter(&flent->fe_lock); 1757 flent->fe_cb_fn = (flow_fn_t)mac_rx_def; 1758 flent->fe_cb_arg1 = NULL; 1759 flent->fe_cb_arg2 = NULL; 1760 flent->fe_flags |= FE_MC_NO_DATAPATH; 1761 mutex_exit(&flent->fe_lock); 1762 } 1763 1764 /* 1765 * Export ddi interrupt handles from the HW ring to the pseudo ring and 1766 * setup the RX callback of the mac client which exclusively controls 1767 * HW ring. 1768 */ 1769 void 1770 mac_hwring_setup(mac_ring_handle_t hwrh, mac_resource_handle_t prh, 1771 mac_ring_handle_t pseudo_rh) 1772 { 1773 mac_ring_t *hw_ring = (mac_ring_t *)hwrh; 1774 mac_ring_t *pseudo_ring; 1775 mac_soft_ring_set_t *mac_srs = hw_ring->mr_srs; 1776 1777 if (pseudo_rh != NULL) { 1778 pseudo_ring = (mac_ring_t *)pseudo_rh; 1779 /* Export the ddi handles to pseudo ring */ 1780 pseudo_ring->mr_info.mri_intr.mi_ddi_handle = 1781 hw_ring->mr_info.mri_intr.mi_ddi_handle; 1782 pseudo_ring->mr_info.mri_intr.mi_ddi_shared = 1783 hw_ring->mr_info.mri_intr.mi_ddi_shared; 1784 /* 1785 * Save a pointer to pseudo ring in the hw ring. If 1786 * interrupt handle changes, the hw ring will be 1787 * notified of the change (see mac_ring_intr_set()) 1788 * and the appropriate change has to be made to 1789 * the pseudo ring that has exported the ddi handle. 1790 */ 1791 hw_ring->mr_prh = pseudo_rh; 1792 } 1793 1794 if (hw_ring->mr_type == MAC_RING_TYPE_RX) { 1795 ASSERT(!(mac_srs->srs_type & SRST_TX)); 1796 mac_srs->srs_mrh = prh; 1797 mac_srs->srs_rx.sr_lower_proc = mac_hwrings_rx_process; 1798 } 1799 } 1800 1801 void 1802 mac_hwring_teardown(mac_ring_handle_t hwrh) 1803 { 1804 mac_ring_t *hw_ring = (mac_ring_t *)hwrh; 1805 mac_soft_ring_set_t *mac_srs; 1806 1807 if (hw_ring == NULL) 1808 return; 1809 hw_ring->mr_prh = NULL; 1810 if (hw_ring->mr_type == MAC_RING_TYPE_RX) { 1811 mac_srs = hw_ring->mr_srs; 1812 ASSERT(!(mac_srs->srs_type & SRST_TX)); 1813 mac_srs->srs_rx.sr_lower_proc = mac_rx_srs_process; 1814 mac_srs->srs_mrh = NULL; 1815 } 1816 } 1817 1818 int 1819 mac_hwring_disable_intr(mac_ring_handle_t rh) 1820 { 1821 mac_ring_t *rr_ring = (mac_ring_t *)rh; 1822 mac_intr_t *intr = &rr_ring->mr_info.mri_intr; 1823 1824 return (intr->mi_disable(intr->mi_handle)); 1825 } 1826 1827 int 1828 mac_hwring_enable_intr(mac_ring_handle_t rh) 1829 { 1830 mac_ring_t *rr_ring = (mac_ring_t *)rh; 1831 mac_intr_t *intr = &rr_ring->mr_info.mri_intr; 1832 1833 return (intr->mi_enable(intr->mi_handle)); 1834 } 1835 1836 /* 1837 * Start the HW ring pointed to by rh. 1838 * 1839 * This is used by special MAC clients that are MAC themselves and 1840 * need to exert control over the underlying HW rings of the NIC. 1841 */ 1842 int 1843 mac_hwring_start(mac_ring_handle_t rh) 1844 { 1845 mac_ring_t *rr_ring = (mac_ring_t *)rh; 1846 int rv = 0; 1847 1848 if (rr_ring->mr_state != MR_INUSE) 1849 rv = mac_start_ring(rr_ring); 1850 1851 return (rv); 1852 } 1853 1854 /* 1855 * Stop the HW ring pointed to by rh. Also see mac_hwring_start(). 1856 */ 1857 void 1858 mac_hwring_stop(mac_ring_handle_t rh) 1859 { 1860 mac_ring_t *rr_ring = (mac_ring_t *)rh; 1861 1862 if (rr_ring->mr_state != MR_FREE) 1863 mac_stop_ring(rr_ring); 1864 } 1865 1866 /* 1867 * Remove the quiesced flag from the HW ring pointed to by rh. 1868 * 1869 * This is used by special MAC clients that are MAC themselves and 1870 * need to exert control over the underlying HW rings of the NIC. 1871 */ 1872 int 1873 mac_hwring_activate(mac_ring_handle_t rh) 1874 { 1875 mac_ring_t *rr_ring = (mac_ring_t *)rh; 1876 1877 MAC_RING_UNMARK(rr_ring, MR_QUIESCE); 1878 return (0); 1879 } 1880 1881 /* 1882 * Quiesce the HW ring pointed to by rh. Also see mac_hwring_activate(). 1883 */ 1884 void 1885 mac_hwring_quiesce(mac_ring_handle_t rh) 1886 { 1887 mac_ring_t *rr_ring = (mac_ring_t *)rh; 1888 1889 mac_rx_ring_quiesce(rr_ring, MR_QUIESCE); 1890 } 1891 1892 mblk_t * 1893 mac_hwring_poll(mac_ring_handle_t rh, int bytes_to_pickup) 1894 { 1895 mac_ring_t *rr_ring = (mac_ring_t *)rh; 1896 mac_ring_info_t *info = &rr_ring->mr_info; 1897 1898 return (info->mri_poll(info->mri_driver, bytes_to_pickup)); 1899 } 1900 1901 /* 1902 * Send packets through a selected tx ring. 1903 */ 1904 mblk_t * 1905 mac_hwring_tx(mac_ring_handle_t rh, mblk_t *mp) 1906 { 1907 mac_ring_t *ring = (mac_ring_t *)rh; 1908 mac_ring_info_t *info = &ring->mr_info; 1909 1910 ASSERT(ring->mr_type == MAC_RING_TYPE_TX && 1911 ring->mr_state >= MR_INUSE); 1912 return (info->mri_tx(info->mri_driver, mp)); 1913 } 1914 1915 /* 1916 * Query stats for a particular rx/tx ring 1917 */ 1918 int 1919 mac_hwring_getstat(mac_ring_handle_t rh, uint_t stat, uint64_t *val) 1920 { 1921 mac_ring_t *ring = (mac_ring_t *)rh; 1922 mac_ring_info_t *info = &ring->mr_info; 1923 1924 return (info->mri_stat(info->mri_driver, stat, val)); 1925 } 1926 1927 /* 1928 * Private function that is only used by aggr to send packets through 1929 * a port/Tx ring. Since aggr exposes a pseudo Tx ring even for ports 1930 * that does not expose Tx rings, aggr_ring_tx() entry point needs 1931 * access to mac_impl_t to send packets through m_tx() entry point. 1932 * It accomplishes this by calling mac_hwring_send_priv() function. 1933 */ 1934 mblk_t * 1935 mac_hwring_send_priv(mac_client_handle_t mch, mac_ring_handle_t rh, mblk_t *mp) 1936 { 1937 mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 1938 mac_impl_t *mip = mcip->mci_mip; 1939 1940 return (mac_provider_tx(mip, rh, mp, mcip)); 1941 } 1942 1943 /* 1944 * Private function that is only used by aggr to update the default transmission 1945 * ring. Because aggr exposes a pseudo Tx ring even for ports that may 1946 * temporarily be down, it may need to update the default ring that is used by 1947 * MAC such that it refers to a link that can actively be used to send traffic. 1948 * Note that this is different from the case where the port has been removed 1949 * from the group. In those cases, all of the rings will be torn down because 1950 * the ring will no longer exist. It's important to give aggr a case where the 1951 * rings can still exist such that it may be able to continue to send LACP PDUs 1952 * to potentially restore the link. 1953 * 1954 * Finally, we explicitly don't do anything if the ring hasn't been enabled yet. 1955 * This is to help out aggr which doesn't really know the internal state that 1956 * MAC does about the rings and can't know that it's not quite ready for use 1957 * yet. 1958 */ 1959 void 1960 mac_hwring_set_default(mac_handle_t mh, mac_ring_handle_t rh) 1961 { 1962 mac_impl_t *mip = (mac_impl_t *)mh; 1963 mac_ring_t *ring = (mac_ring_t *)rh; 1964 1965 ASSERT(MAC_PERIM_HELD(mh)); 1966 VERIFY(mip->mi_state_flags & MIS_IS_AGGR); 1967 1968 if (ring->mr_state != MR_INUSE) 1969 return; 1970 1971 mip->mi_default_tx_ring = rh; 1972 } 1973 1974 int 1975 mac_hwgroup_addmac(mac_group_handle_t gh, const uint8_t *addr) 1976 { 1977 mac_group_t *group = (mac_group_t *)gh; 1978 1979 return (mac_group_addmac(group, addr)); 1980 } 1981 1982 int 1983 mac_hwgroup_remmac(mac_group_handle_t gh, const uint8_t *addr) 1984 { 1985 mac_group_t *group = (mac_group_t *)gh; 1986 1987 return (mac_group_remmac(group, addr)); 1988 } 1989 1990 /* 1991 * Program the group's HW VLAN filter if it has such support. 1992 * Otherwise, the group will implicitly accept tagged traffic and 1993 * there is nothing to do. 1994 */ 1995 int 1996 mac_hwgroup_addvlan(mac_group_handle_t gh, uint16_t vid) 1997 { 1998 mac_group_t *group = (mac_group_t *)gh; 1999 2000 if (!MAC_GROUP_HW_VLAN(group)) 2001 return (0); 2002 2003 return (mac_group_addvlan(group, vid)); 2004 } 2005 2006 int 2007 mac_hwgroup_remvlan(mac_group_handle_t gh, uint16_t vid) 2008 { 2009 mac_group_t *group = (mac_group_t *)gh; 2010 2011 if (!MAC_GROUP_HW_VLAN(group)) 2012 return (0); 2013 2014 return (mac_group_remvlan(group, vid)); 2015 } 2016 2017 /* 2018 * Determine if a MAC has HW VLAN support. This is a private API 2019 * consumed by aggr. In the future it might be nice to have a bitfield 2020 * in mac_capab_rings_t to track which forms of HW filtering are 2021 * supported by the MAC. 2022 */ 2023 boolean_t 2024 mac_has_hw_vlan(mac_handle_t mh) 2025 { 2026 mac_impl_t *mip = (mac_impl_t *)mh; 2027 2028 return (MAC_GROUP_HW_VLAN(mip->mi_rx_groups)); 2029 } 2030 2031 /* 2032 * Get the number of Rx HW groups on this MAC. 2033 */ 2034 uint_t 2035 mac_get_num_rx_groups(mac_handle_t mh) 2036 { 2037 mac_impl_t *mip = (mac_impl_t *)mh; 2038 2039 ASSERT(MAC_PERIM_HELD(mh)); 2040 return (mip->mi_rx_group_count); 2041 } 2042 2043 int 2044 mac_set_promisc(mac_handle_t mh, boolean_t value) 2045 { 2046 mac_impl_t *mip = (mac_impl_t *)mh; 2047 2048 ASSERT(MAC_PERIM_HELD(mh)); 2049 return (i_mac_promisc_set(mip, value)); 2050 } 2051 2052 /* 2053 * Set the RX group to be shared/reserved. Note that the group must be 2054 * started/stopped outside of this function. 2055 */ 2056 void 2057 mac_set_group_state(mac_group_t *grp, mac_group_state_t state) 2058 { 2059 /* 2060 * If there is no change in the group state, just return. 2061 */ 2062 if (grp->mrg_state == state) 2063 return; 2064 2065 switch (state) { 2066 case MAC_GROUP_STATE_RESERVED: 2067 /* 2068 * Successfully reserved the group. 2069 * 2070 * Given that there is an exclusive client controlling this 2071 * group, we enable the group level polling when available, 2072 * so that SRSs get to turn on/off individual rings they's 2073 * assigned to. 2074 */ 2075 ASSERT(MAC_PERIM_HELD(grp->mrg_mh)); 2076 2077 if (grp->mrg_type == MAC_RING_TYPE_RX && 2078 GROUP_INTR_DISABLE_FUNC(grp) != NULL) { 2079 GROUP_INTR_DISABLE_FUNC(grp)(GROUP_INTR_HANDLE(grp)); 2080 } 2081 break; 2082 2083 case MAC_GROUP_STATE_SHARED: 2084 /* 2085 * Set all rings of this group to software classified. 2086 * If the group has an overriding interrupt, then re-enable it. 2087 */ 2088 ASSERT(MAC_PERIM_HELD(grp->mrg_mh)); 2089 2090 if (grp->mrg_type == MAC_RING_TYPE_RX && 2091 GROUP_INTR_ENABLE_FUNC(grp) != NULL) { 2092 GROUP_INTR_ENABLE_FUNC(grp)(GROUP_INTR_HANDLE(grp)); 2093 } 2094 /* The ring is not available for reservations any more */ 2095 break; 2096 2097 case MAC_GROUP_STATE_REGISTERED: 2098 /* Also callable from mac_register, perim is not held */ 2099 break; 2100 2101 default: 2102 ASSERT(B_FALSE); 2103 break; 2104 } 2105 2106 grp->mrg_state = state; 2107 } 2108 2109 /* 2110 * Quiesce future hardware classified packets for the specified Rx ring 2111 */ 2112 static void 2113 mac_rx_ring_quiesce(mac_ring_t *rx_ring, uint_t ring_flag) 2114 { 2115 ASSERT(rx_ring->mr_classify_type == MAC_HW_CLASSIFIER); 2116 ASSERT(ring_flag == MR_CONDEMNED || ring_flag == MR_QUIESCE); 2117 2118 mutex_enter(&rx_ring->mr_lock); 2119 rx_ring->mr_flag |= ring_flag; 2120 while (rx_ring->mr_refcnt != 0) 2121 cv_wait(&rx_ring->mr_cv, &rx_ring->mr_lock); 2122 mutex_exit(&rx_ring->mr_lock); 2123 } 2124 2125 /* 2126 * Please see mac_tx for details about the per cpu locking scheme 2127 */ 2128 static void 2129 mac_tx_lock_all(mac_client_impl_t *mcip) 2130 { 2131 int i; 2132 2133 for (i = 0; i <= mac_tx_percpu_cnt; i++) 2134 mutex_enter(&mcip->mci_tx_pcpu[i].pcpu_tx_lock); 2135 } 2136 2137 static void 2138 mac_tx_unlock_all(mac_client_impl_t *mcip) 2139 { 2140 int i; 2141 2142 for (i = mac_tx_percpu_cnt; i >= 0; i--) 2143 mutex_exit(&mcip->mci_tx_pcpu[i].pcpu_tx_lock); 2144 } 2145 2146 static void 2147 mac_tx_unlock_allbutzero(mac_client_impl_t *mcip) 2148 { 2149 int i; 2150 2151 for (i = mac_tx_percpu_cnt; i > 0; i--) 2152 mutex_exit(&mcip->mci_tx_pcpu[i].pcpu_tx_lock); 2153 } 2154 2155 static int 2156 mac_tx_sum_refcnt(mac_client_impl_t *mcip) 2157 { 2158 int i; 2159 int refcnt = 0; 2160 2161 for (i = 0; i <= mac_tx_percpu_cnt; i++) 2162 refcnt += mcip->mci_tx_pcpu[i].pcpu_tx_refcnt; 2163 2164 return (refcnt); 2165 } 2166 2167 /* 2168 * Stop future Tx packets coming down from the client in preparation for 2169 * quiescing the Tx side. This is needed for dynamic reclaim and reassignment 2170 * of rings between clients 2171 */ 2172 void 2173 mac_tx_client_block(mac_client_impl_t *mcip) 2174 { 2175 mac_tx_lock_all(mcip); 2176 mcip->mci_tx_flag |= MCI_TX_QUIESCE; 2177 while (mac_tx_sum_refcnt(mcip) != 0) { 2178 mac_tx_unlock_allbutzero(mcip); 2179 cv_wait(&mcip->mci_tx_cv, &mcip->mci_tx_pcpu[0].pcpu_tx_lock); 2180 mutex_exit(&mcip->mci_tx_pcpu[0].pcpu_tx_lock); 2181 mac_tx_lock_all(mcip); 2182 } 2183 mac_tx_unlock_all(mcip); 2184 } 2185 2186 void 2187 mac_tx_client_unblock(mac_client_impl_t *mcip) 2188 { 2189 mac_tx_lock_all(mcip); 2190 mcip->mci_tx_flag &= ~MCI_TX_QUIESCE; 2191 mac_tx_unlock_all(mcip); 2192 /* 2193 * We may fail to disable flow control for the last MAC_NOTE_TX 2194 * notification because the MAC client is quiesced. Send the 2195 * notification again. 2196 */ 2197 i_mac_notify(mcip->mci_mip, MAC_NOTE_TX); 2198 } 2199 2200 /* 2201 * Wait for an SRS to quiesce. The SRS worker will signal us when the 2202 * quiesce is done. 2203 */ 2204 static void 2205 mac_srs_quiesce_wait(mac_soft_ring_set_t *srs, uint_t srs_flag) 2206 { 2207 mutex_enter(&srs->srs_lock); 2208 while (!(srs->srs_state & srs_flag)) 2209 cv_wait(&srs->srs_quiesce_done_cv, &srs->srs_lock); 2210 mutex_exit(&srs->srs_lock); 2211 } 2212 2213 /* 2214 * Quiescing an Rx SRS is achieved by the following sequence. The protocol 2215 * works bottom up by cutting off packet flow from the bottommost point in the 2216 * mac, then the SRS, and then the soft rings. There are 2 use cases of this 2217 * mechanism. One is a temporary quiesce of the SRS, such as say while changing 2218 * the Rx callbacks. Another use case is Rx SRS teardown. In the former case 2219 * the QUIESCE prefix/suffix is used and in the latter the CONDEMNED is used 2220 * for the SRS and MR flags. In the former case the threads pause waiting for 2221 * a restart, while in the latter case the threads exit. The Tx SRS teardown 2222 * is also mostly similar to the above. 2223 * 2224 * 1. Stop future hardware classified packets at the lowest level in the mac. 2225 * Remove any hardware classification rule (CONDEMNED case) and mark the 2226 * rings as CONDEMNED or QUIESCE as appropriate. This prevents the mr_refcnt 2227 * from increasing. Upcalls from the driver that come through hardware 2228 * classification will be dropped in mac_rx from now on. Then we wait for 2229 * the mr_refcnt to drop to zero. When the mr_refcnt reaches zero we are 2230 * sure there aren't any upcall threads from the driver through hardware 2231 * classification. In the case of SRS teardown we also remove the 2232 * classification rule in the driver. 2233 * 2234 * 2. Stop future software classified packets by marking the flow entry with 2235 * FE_QUIESCE or FE_CONDEMNED as appropriate which prevents the refcnt from 2236 * increasing. We also remove the flow entry from the table in the latter 2237 * case. Then wait for the fe_refcnt to reach an appropriate quiescent value 2238 * that indicates there aren't any active threads using that flow entry. 2239 * 2240 * 3. Quiesce the SRS and softrings by signaling the SRS. The SRS poll thread, 2241 * SRS worker thread, and the soft ring threads are quiesced in sequence 2242 * with the SRS worker thread serving as a master controller. This 2243 * mechansim is explained in mac_srs_worker_quiesce(). 2244 * 2245 * The restart mechanism to reactivate the SRS and softrings is explained 2246 * in mac_srs_worker_restart(). Here we just signal the SRS worker to start the 2247 * restart sequence. 2248 */ 2249 void 2250 mac_rx_srs_quiesce(mac_soft_ring_set_t *srs, uint_t srs_quiesce_flag) 2251 { 2252 flow_entry_t *flent = srs->srs_flent; 2253 uint_t mr_flag, srs_done_flag; 2254 2255 ASSERT(MAC_PERIM_HELD((mac_handle_t)FLENT_TO_MIP(flent))); 2256 ASSERT(!(srs->srs_type & SRST_TX)); 2257 2258 if (srs_quiesce_flag == SRS_CONDEMNED) { 2259 mr_flag = MR_CONDEMNED; 2260 srs_done_flag = SRS_CONDEMNED_DONE; 2261 if (srs->srs_type & SRST_CLIENT_POLL_ENABLED) 2262 mac_srs_client_poll_disable(srs->srs_mcip, srs); 2263 } else { 2264 ASSERT(srs_quiesce_flag == SRS_QUIESCE); 2265 mr_flag = MR_QUIESCE; 2266 srs_done_flag = SRS_QUIESCE_DONE; 2267 if (srs->srs_type & SRST_CLIENT_POLL_ENABLED) 2268 mac_srs_client_poll_quiesce(srs->srs_mcip, srs); 2269 } 2270 2271 if (srs->srs_ring != NULL) { 2272 mac_rx_ring_quiesce(srs->srs_ring, mr_flag); 2273 } else { 2274 /* 2275 * SRS is driven by software classification. In case 2276 * of CONDEMNED, the top level teardown functions will 2277 * deal with flow removal. 2278 */ 2279 if (srs_quiesce_flag != SRS_CONDEMNED) { 2280 FLOW_MARK(flent, FE_QUIESCE); 2281 mac_flow_wait(flent, FLOW_DRIVER_UPCALL); 2282 } 2283 } 2284 2285 /* 2286 * Signal the SRS to quiesce itself, and then cv_wait for the 2287 * SRS quiesce to complete. The SRS worker thread will wake us 2288 * up when the quiesce is complete 2289 */ 2290 mac_srs_signal(srs, srs_quiesce_flag); 2291 mac_srs_quiesce_wait(srs, srs_done_flag); 2292 } 2293 2294 /* 2295 * Remove an SRS. 2296 */ 2297 void 2298 mac_rx_srs_remove(mac_soft_ring_set_t *srs) 2299 { 2300 flow_entry_t *flent = srs->srs_flent; 2301 int i; 2302 2303 mac_rx_srs_quiesce(srs, SRS_CONDEMNED); 2304 /* 2305 * Locate and remove our entry in the fe_rx_srs[] array, and 2306 * adjust the fe_rx_srs array entries and array count by 2307 * moving the last entry into the vacated spot. 2308 */ 2309 mutex_enter(&flent->fe_lock); 2310 for (i = 0; i < flent->fe_rx_srs_cnt; i++) { 2311 if (flent->fe_rx_srs[i] == srs) 2312 break; 2313 } 2314 2315 ASSERT(i != 0 && i < flent->fe_rx_srs_cnt); 2316 if (i != flent->fe_rx_srs_cnt - 1) { 2317 flent->fe_rx_srs[i] = 2318 flent->fe_rx_srs[flent->fe_rx_srs_cnt - 1]; 2319 i = flent->fe_rx_srs_cnt - 1; 2320 } 2321 2322 flent->fe_rx_srs[i] = NULL; 2323 flent->fe_rx_srs_cnt--; 2324 mutex_exit(&flent->fe_lock); 2325 2326 mac_srs_free(srs); 2327 } 2328 2329 static void 2330 mac_srs_clear_flag(mac_soft_ring_set_t *srs, uint_t flag) 2331 { 2332 mutex_enter(&srs->srs_lock); 2333 srs->srs_state &= ~flag; 2334 mutex_exit(&srs->srs_lock); 2335 } 2336 2337 void 2338 mac_rx_srs_restart(mac_soft_ring_set_t *srs) 2339 { 2340 flow_entry_t *flent = srs->srs_flent; 2341 mac_ring_t *mr; 2342 2343 ASSERT(MAC_PERIM_HELD((mac_handle_t)FLENT_TO_MIP(flent))); 2344 ASSERT((srs->srs_type & SRST_TX) == 0); 2345 2346 /* 2347 * This handles a change in the number of SRSs between the quiesce and 2348 * and restart operation of a flow. 2349 */ 2350 if (!SRS_QUIESCED(srs)) 2351 return; 2352 2353 /* 2354 * Signal the SRS to restart itself. Wait for the restart to complete 2355 * Note that we only restart the SRS if it is not marked as 2356 * permanently quiesced. 2357 */ 2358 if (!SRS_QUIESCED_PERMANENT(srs)) { 2359 mac_srs_signal(srs, SRS_RESTART); 2360 mac_srs_quiesce_wait(srs, SRS_RESTART_DONE); 2361 mac_srs_clear_flag(srs, SRS_RESTART_DONE); 2362 2363 mac_srs_client_poll_restart(srs->srs_mcip, srs); 2364 } 2365 2366 /* Finally clear the flags to let the packets in */ 2367 mr = srs->srs_ring; 2368 if (mr != NULL) { 2369 MAC_RING_UNMARK(mr, MR_QUIESCE); 2370 /* In case the ring was stopped, safely restart it */ 2371 if (mr->mr_state != MR_INUSE) 2372 (void) mac_start_ring(mr); 2373 } else { 2374 FLOW_UNMARK(flent, FE_QUIESCE); 2375 } 2376 } 2377 2378 /* 2379 * Temporary quiesce of a flow and associated Rx SRS. 2380 * Please see block comment above mac_rx_classify_flow_rem. 2381 */ 2382 /* ARGSUSED */ 2383 int 2384 mac_rx_classify_flow_quiesce(flow_entry_t *flent, void *arg) 2385 { 2386 int i; 2387 2388 for (i = 0; i < flent->fe_rx_srs_cnt; i++) { 2389 mac_rx_srs_quiesce((mac_soft_ring_set_t *)flent->fe_rx_srs[i], 2390 SRS_QUIESCE); 2391 } 2392 return (0); 2393 } 2394 2395 /* 2396 * Restart a flow and associated Rx SRS that has been quiesced temporarily 2397 * Please see block comment above mac_rx_classify_flow_rem 2398 */ 2399 /* ARGSUSED */ 2400 int 2401 mac_rx_classify_flow_restart(flow_entry_t *flent, void *arg) 2402 { 2403 int i; 2404 2405 for (i = 0; i < flent->fe_rx_srs_cnt; i++) 2406 mac_rx_srs_restart((mac_soft_ring_set_t *)flent->fe_rx_srs[i]); 2407 2408 return (0); 2409 } 2410 2411 void 2412 mac_srs_perm_quiesce(mac_client_handle_t mch, boolean_t on) 2413 { 2414 mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 2415 flow_entry_t *flent = mcip->mci_flent; 2416 mac_impl_t *mip = mcip->mci_mip; 2417 mac_soft_ring_set_t *mac_srs; 2418 int i; 2419 2420 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 2421 2422 if (flent == NULL) 2423 return; 2424 2425 for (i = 0; i < flent->fe_rx_srs_cnt; i++) { 2426 mac_srs = flent->fe_rx_srs[i]; 2427 mutex_enter(&mac_srs->srs_lock); 2428 if (on) 2429 mac_srs->srs_state |= SRS_QUIESCE_PERM; 2430 else 2431 mac_srs->srs_state &= ~SRS_QUIESCE_PERM; 2432 mutex_exit(&mac_srs->srs_lock); 2433 } 2434 } 2435 2436 void 2437 mac_rx_client_quiesce(mac_client_handle_t mch) 2438 { 2439 mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 2440 mac_impl_t *mip = mcip->mci_mip; 2441 2442 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 2443 2444 if (MCIP_DATAPATH_SETUP(mcip)) { 2445 (void) mac_rx_classify_flow_quiesce(mcip->mci_flent, 2446 NULL); 2447 (void) mac_flow_walk_nolock(mcip->mci_subflow_tab, 2448 mac_rx_classify_flow_quiesce, NULL); 2449 } 2450 } 2451 2452 void 2453 mac_rx_client_restart(mac_client_handle_t mch) 2454 { 2455 mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 2456 mac_impl_t *mip = mcip->mci_mip; 2457 2458 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 2459 2460 if (MCIP_DATAPATH_SETUP(mcip)) { 2461 (void) mac_rx_classify_flow_restart(mcip->mci_flent, NULL); 2462 (void) mac_flow_walk_nolock(mcip->mci_subflow_tab, 2463 mac_rx_classify_flow_restart, NULL); 2464 } 2465 } 2466 2467 /* 2468 * This function only quiesces the Tx SRS and softring worker threads. Callers 2469 * need to make sure that there aren't any mac client threads doing current or 2470 * future transmits in the mac before calling this function. 2471 */ 2472 void 2473 mac_tx_srs_quiesce(mac_soft_ring_set_t *srs, uint_t srs_quiesce_flag) 2474 { 2475 mac_client_impl_t *mcip = srs->srs_mcip; 2476 2477 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip)); 2478 2479 ASSERT(srs->srs_type & SRST_TX); 2480 ASSERT(srs_quiesce_flag == SRS_CONDEMNED || 2481 srs_quiesce_flag == SRS_QUIESCE); 2482 2483 /* 2484 * Signal the SRS to quiesce itself, and then cv_wait for the 2485 * SRS quiesce to complete. The SRS worker thread will wake us 2486 * up when the quiesce is complete 2487 */ 2488 mac_srs_signal(srs, srs_quiesce_flag); 2489 mac_srs_quiesce_wait(srs, srs_quiesce_flag == SRS_QUIESCE ? 2490 SRS_QUIESCE_DONE : SRS_CONDEMNED_DONE); 2491 } 2492 2493 void 2494 mac_tx_srs_restart(mac_soft_ring_set_t *srs) 2495 { 2496 /* 2497 * Resizing the fanout could result in creation of new SRSs. 2498 * They may not necessarily be in the quiesced state in which 2499 * case it need be restarted 2500 */ 2501 if (!SRS_QUIESCED(srs)) 2502 return; 2503 2504 mac_srs_signal(srs, SRS_RESTART); 2505 mac_srs_quiesce_wait(srs, SRS_RESTART_DONE); 2506 mac_srs_clear_flag(srs, SRS_RESTART_DONE); 2507 } 2508 2509 /* 2510 * Temporary quiesce of a flow and associated Rx SRS. 2511 * Please see block comment above mac_rx_srs_quiesce 2512 */ 2513 /* ARGSUSED */ 2514 int 2515 mac_tx_flow_quiesce(flow_entry_t *flent, void *arg) 2516 { 2517 /* 2518 * The fe_tx_srs is null for a subflow on an interface that is 2519 * not plumbed 2520 */ 2521 if (flent->fe_tx_srs != NULL) 2522 mac_tx_srs_quiesce(flent->fe_tx_srs, SRS_QUIESCE); 2523 return (0); 2524 } 2525 2526 /* ARGSUSED */ 2527 int 2528 mac_tx_flow_restart(flow_entry_t *flent, void *arg) 2529 { 2530 /* 2531 * The fe_tx_srs is null for a subflow on an interface that is 2532 * not plumbed 2533 */ 2534 if (flent->fe_tx_srs != NULL) 2535 mac_tx_srs_restart(flent->fe_tx_srs); 2536 return (0); 2537 } 2538 2539 static void 2540 i_mac_tx_client_quiesce(mac_client_handle_t mch, uint_t srs_quiesce_flag) 2541 { 2542 mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 2543 2544 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip)); 2545 2546 mac_tx_client_block(mcip); 2547 if (MCIP_TX_SRS(mcip) != NULL) { 2548 mac_tx_srs_quiesce(MCIP_TX_SRS(mcip), srs_quiesce_flag); 2549 (void) mac_flow_walk_nolock(mcip->mci_subflow_tab, 2550 mac_tx_flow_quiesce, NULL); 2551 } 2552 } 2553 2554 void 2555 mac_tx_client_quiesce(mac_client_handle_t mch) 2556 { 2557 i_mac_tx_client_quiesce(mch, SRS_QUIESCE); 2558 } 2559 2560 void 2561 mac_tx_client_condemn(mac_client_handle_t mch) 2562 { 2563 i_mac_tx_client_quiesce(mch, SRS_CONDEMNED); 2564 } 2565 2566 void 2567 mac_tx_client_restart(mac_client_handle_t mch) 2568 { 2569 mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 2570 2571 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip)); 2572 2573 mac_tx_client_unblock(mcip); 2574 if (MCIP_TX_SRS(mcip) != NULL) { 2575 mac_tx_srs_restart(MCIP_TX_SRS(mcip)); 2576 (void) mac_flow_walk_nolock(mcip->mci_subflow_tab, 2577 mac_tx_flow_restart, NULL); 2578 } 2579 } 2580 2581 void 2582 mac_tx_client_flush(mac_client_impl_t *mcip) 2583 { 2584 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip)); 2585 2586 mac_tx_client_quiesce((mac_client_handle_t)mcip); 2587 mac_tx_client_restart((mac_client_handle_t)mcip); 2588 } 2589 2590 void 2591 mac_client_quiesce(mac_client_impl_t *mcip) 2592 { 2593 mac_rx_client_quiesce((mac_client_handle_t)mcip); 2594 mac_tx_client_quiesce((mac_client_handle_t)mcip); 2595 } 2596 2597 void 2598 mac_client_restart(mac_client_impl_t *mcip) 2599 { 2600 mac_rx_client_restart((mac_client_handle_t)mcip); 2601 mac_tx_client_restart((mac_client_handle_t)mcip); 2602 } 2603 2604 /* 2605 * Allocate a minor number. 2606 */ 2607 minor_t 2608 mac_minor_hold(boolean_t sleep) 2609 { 2610 id_t id; 2611 2612 /* 2613 * Grab a value from the arena. 2614 */ 2615 atomic_inc_32(&minor_count); 2616 2617 if (sleep) 2618 return ((uint_t)id_alloc(minor_ids)); 2619 2620 if ((id = id_alloc_nosleep(minor_ids)) == -1) { 2621 atomic_dec_32(&minor_count); 2622 return (0); 2623 } 2624 2625 return ((uint_t)id); 2626 } 2627 2628 /* 2629 * Release a previously allocated minor number. 2630 */ 2631 void 2632 mac_minor_rele(minor_t minor) 2633 { 2634 /* 2635 * Return the value to the arena. 2636 */ 2637 id_free(minor_ids, minor); 2638 atomic_dec_32(&minor_count); 2639 } 2640 2641 uint32_t 2642 mac_no_notification(mac_handle_t mh) 2643 { 2644 mac_impl_t *mip = (mac_impl_t *)mh; 2645 2646 return (((mip->mi_state_flags & MIS_LEGACY) != 0) ? 2647 mip->mi_capab_legacy.ml_unsup_note : 0); 2648 } 2649 2650 /* 2651 * Prevent any new opens of this mac in preparation for unregister 2652 */ 2653 int 2654 i_mac_disable(mac_impl_t *mip) 2655 { 2656 mac_client_impl_t *mcip; 2657 2658 rw_enter(&i_mac_impl_lock, RW_WRITER); 2659 if (mip->mi_state_flags & MIS_DISABLED) { 2660 /* Already disabled, return success */ 2661 rw_exit(&i_mac_impl_lock); 2662 return (0); 2663 } 2664 /* 2665 * See if there are any other references to this mac_t (e.g., VLAN's). 2666 * If so return failure. If all the other checks below pass, then 2667 * set mi_disabled atomically under the i_mac_impl_lock to prevent 2668 * any new VLAN's from being created or new mac client opens of this 2669 * mac end point. 2670 */ 2671 if (mip->mi_ref > 0) { 2672 rw_exit(&i_mac_impl_lock); 2673 return (EBUSY); 2674 } 2675 2676 /* 2677 * mac clients must delete all multicast groups they join before 2678 * closing. bcast groups are reference counted, the last client 2679 * to delete the group will wait till the group is physically 2680 * deleted. Since all clients have closed this mac end point 2681 * mi_bcast_ngrps must be zero at this point 2682 */ 2683 ASSERT(mip->mi_bcast_ngrps == 0); 2684 2685 /* 2686 * Don't let go of this if it has some flows. 2687 * All other code guarantees no flows are added to a disabled 2688 * mac, therefore it is sufficient to check for the flow table 2689 * only here. 2690 */ 2691 mcip = mac_primary_client_handle(mip); 2692 if ((mcip != NULL) && mac_link_has_flows((mac_client_handle_t)mcip)) { 2693 rw_exit(&i_mac_impl_lock); 2694 return (ENOTEMPTY); 2695 } 2696 2697 mip->mi_state_flags |= MIS_DISABLED; 2698 rw_exit(&i_mac_impl_lock); 2699 return (0); 2700 } 2701 2702 int 2703 mac_disable_nowait(mac_handle_t mh) 2704 { 2705 mac_impl_t *mip = (mac_impl_t *)mh; 2706 int err; 2707 2708 if ((err = i_mac_perim_enter_nowait(mip)) != 0) 2709 return (err); 2710 err = i_mac_disable(mip); 2711 i_mac_perim_exit(mip); 2712 return (err); 2713 } 2714 2715 int 2716 mac_disable(mac_handle_t mh) 2717 { 2718 mac_impl_t *mip = (mac_impl_t *)mh; 2719 int err; 2720 2721 i_mac_perim_enter(mip); 2722 err = i_mac_disable(mip); 2723 i_mac_perim_exit(mip); 2724 2725 /* 2726 * Clean up notification thread and wait for it to exit. 2727 */ 2728 if (err == 0) 2729 i_mac_notify_exit(mip); 2730 2731 return (err); 2732 } 2733 2734 /* 2735 * Called when the MAC instance has a non empty flow table, to de-multiplex 2736 * incoming packets to the right flow. 2737 */ 2738 /* ARGSUSED */ 2739 static mblk_t * 2740 mac_rx_classify(mac_impl_t *mip, mac_resource_handle_t mrh, mblk_t *mp) 2741 { 2742 flow_entry_t *flent = NULL; 2743 uint_t flags = FLOW_INBOUND; 2744 int err; 2745 2746 err = mac_flow_lookup(mip->mi_flow_tab, mp, flags, &flent); 2747 if (err != 0) { 2748 /* no registered receive function */ 2749 return (mp); 2750 } else { 2751 mac_client_impl_t *mcip; 2752 2753 /* 2754 * This flent might just be an additional one on the MAC client, 2755 * i.e. for classification purposes (different fdesc), however 2756 * the resources, SRS et. al., are in the mci_flent, so if 2757 * this isn't the mci_flent, we need to get it. 2758 */ 2759 if ((mcip = flent->fe_mcip) != NULL && 2760 mcip->mci_flent != flent) { 2761 FLOW_REFRELE(flent); 2762 flent = mcip->mci_flent; 2763 FLOW_TRY_REFHOLD(flent, err); 2764 if (err != 0) 2765 return (mp); 2766 } 2767 (flent->fe_cb_fn)(flent->fe_cb_arg1, flent->fe_cb_arg2, mp, 2768 B_FALSE); 2769 FLOW_REFRELE(flent); 2770 } 2771 return (NULL); 2772 } 2773 2774 mblk_t * 2775 mac_rx_flow(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain) 2776 { 2777 mac_impl_t *mip = (mac_impl_t *)mh; 2778 mblk_t *bp, *bp1, **bpp, *list = NULL; 2779 2780 /* 2781 * We walk the chain and attempt to classify each packet. 2782 * The packets that couldn't be classified will be returned 2783 * back to the caller. 2784 */ 2785 bp = mp_chain; 2786 bpp = &list; 2787 while (bp != NULL) { 2788 bp1 = bp; 2789 bp = bp->b_next; 2790 bp1->b_next = NULL; 2791 2792 if (mac_rx_classify(mip, mrh, bp1) != NULL) { 2793 *bpp = bp1; 2794 bpp = &bp1->b_next; 2795 } 2796 } 2797 return (list); 2798 } 2799 2800 static int 2801 mac_tx_flow_srs_wakeup(flow_entry_t *flent, void *arg) 2802 { 2803 mac_ring_handle_t ring = arg; 2804 2805 if (flent->fe_tx_srs) 2806 mac_tx_srs_wakeup(flent->fe_tx_srs, ring); 2807 return (0); 2808 } 2809 2810 void 2811 i_mac_tx_srs_notify(mac_impl_t *mip, mac_ring_handle_t ring) 2812 { 2813 mac_client_impl_t *cclient; 2814 mac_soft_ring_set_t *mac_srs; 2815 2816 /* 2817 * After grabbing the mi_rw_lock, the list of clients can't change. 2818 * If there are any clients mi_disabled must be B_FALSE and can't 2819 * get set since there are clients. If there aren't any clients we 2820 * don't do anything. In any case the mip has to be valid. The driver 2821 * must make sure that it goes single threaded (with respect to mac 2822 * calls) and wait for all pending mac calls to finish before calling 2823 * mac_unregister. 2824 */ 2825 rw_enter(&i_mac_impl_lock, RW_READER); 2826 if (mip->mi_state_flags & MIS_DISABLED) { 2827 rw_exit(&i_mac_impl_lock); 2828 return; 2829 } 2830 2831 /* 2832 * Get MAC tx srs from walking mac_client_handle list. 2833 */ 2834 rw_enter(&mip->mi_rw_lock, RW_READER); 2835 for (cclient = mip->mi_clients_list; cclient != NULL; 2836 cclient = cclient->mci_client_next) { 2837 if ((mac_srs = MCIP_TX_SRS(cclient)) != NULL) { 2838 mac_tx_srs_wakeup(mac_srs, ring); 2839 } else { 2840 /* 2841 * Aggr opens underlying ports in exclusive mode 2842 * and registers flow control callbacks using 2843 * mac_tx_client_notify(). When opened in 2844 * exclusive mode, Tx SRS won't be created 2845 * during mac_unicast_add(). 2846 */ 2847 if (cclient->mci_state_flags & MCIS_EXCLUSIVE) { 2848 mac_tx_invoke_callbacks(cclient, 2849 (mac_tx_cookie_t)ring); 2850 } 2851 } 2852 (void) mac_flow_walk(cclient->mci_subflow_tab, 2853 mac_tx_flow_srs_wakeup, ring); 2854 } 2855 rw_exit(&mip->mi_rw_lock); 2856 rw_exit(&i_mac_impl_lock); 2857 } 2858 2859 /* ARGSUSED */ 2860 void 2861 mac_multicast_refresh(mac_handle_t mh, mac_multicst_t refresh, void *arg, 2862 boolean_t add) 2863 { 2864 mac_impl_t *mip = (mac_impl_t *)mh; 2865 2866 i_mac_perim_enter((mac_impl_t *)mh); 2867 /* 2868 * If no specific refresh function was given then default to the 2869 * driver's m_multicst entry point. 2870 */ 2871 if (refresh == NULL) { 2872 refresh = mip->mi_multicst; 2873 arg = mip->mi_driver; 2874 } 2875 2876 mac_bcast_refresh(mip, refresh, arg, add); 2877 i_mac_perim_exit((mac_impl_t *)mh); 2878 } 2879 2880 void 2881 mac_promisc_refresh(mac_handle_t mh, mac_setpromisc_t refresh, void *arg) 2882 { 2883 mac_impl_t *mip = (mac_impl_t *)mh; 2884 2885 /* 2886 * If no specific refresh function was given then default to the 2887 * driver's m_promisc entry point. 2888 */ 2889 if (refresh == NULL) { 2890 refresh = mip->mi_setpromisc; 2891 arg = mip->mi_driver; 2892 } 2893 ASSERT(refresh != NULL); 2894 2895 /* 2896 * Call the refresh function with the current promiscuity. 2897 */ 2898 refresh(arg, (mip->mi_devpromisc != 0)); 2899 } 2900 2901 /* 2902 * The mac client requests that the mac not to change its margin size to 2903 * be less than the specified value. If "current" is B_TRUE, then the client 2904 * requests the mac not to change its margin size to be smaller than the 2905 * current size. Further, return the current margin size value in this case. 2906 * 2907 * We keep every requested size in an ordered list from largest to smallest. 2908 */ 2909 int 2910 mac_margin_add(mac_handle_t mh, uint32_t *marginp, boolean_t current) 2911 { 2912 mac_impl_t *mip = (mac_impl_t *)mh; 2913 mac_margin_req_t **pp, *p; 2914 int err = 0; 2915 2916 rw_enter(&(mip->mi_rw_lock), RW_WRITER); 2917 if (current) 2918 *marginp = mip->mi_margin; 2919 2920 /* 2921 * If the current margin value cannot satisfy the margin requested, 2922 * return ENOTSUP directly. 2923 */ 2924 if (*marginp > mip->mi_margin) { 2925 err = ENOTSUP; 2926 goto done; 2927 } 2928 2929 /* 2930 * Check whether the given margin is already in the list. If so, 2931 * bump the reference count. 2932 */ 2933 for (pp = &mip->mi_mmrp; (p = *pp) != NULL; pp = &p->mmr_nextp) { 2934 if (p->mmr_margin == *marginp) { 2935 /* 2936 * The margin requested is already in the list, 2937 * so just bump the reference count. 2938 */ 2939 p->mmr_ref++; 2940 goto done; 2941 } 2942 if (p->mmr_margin < *marginp) 2943 break; 2944 } 2945 2946 2947 p = kmem_zalloc(sizeof (mac_margin_req_t), KM_SLEEP); 2948 p->mmr_margin = *marginp; 2949 p->mmr_ref++; 2950 p->mmr_nextp = *pp; 2951 *pp = p; 2952 2953 done: 2954 rw_exit(&(mip->mi_rw_lock)); 2955 return (err); 2956 } 2957 2958 /* 2959 * The mac client requests to cancel its previous mac_margin_add() request. 2960 * We remove the requested margin size from the list. 2961 */ 2962 int 2963 mac_margin_remove(mac_handle_t mh, uint32_t margin) 2964 { 2965 mac_impl_t *mip = (mac_impl_t *)mh; 2966 mac_margin_req_t **pp, *p; 2967 int err = 0; 2968 2969 rw_enter(&(mip->mi_rw_lock), RW_WRITER); 2970 /* 2971 * Find the entry in the list for the given margin. 2972 */ 2973 for (pp = &(mip->mi_mmrp); (p = *pp) != NULL; pp = &(p->mmr_nextp)) { 2974 if (p->mmr_margin == margin) { 2975 if (--p->mmr_ref == 0) 2976 break; 2977 2978 /* 2979 * There is still a reference to this address so 2980 * there's nothing more to do. 2981 */ 2982 goto done; 2983 } 2984 } 2985 2986 /* 2987 * We did not find an entry for the given margin. 2988 */ 2989 if (p == NULL) { 2990 err = ENOENT; 2991 goto done; 2992 } 2993 2994 ASSERT(p->mmr_ref == 0); 2995 2996 /* 2997 * Remove it from the list. 2998 */ 2999 *pp = p->mmr_nextp; 3000 kmem_free(p, sizeof (mac_margin_req_t)); 3001 done: 3002 rw_exit(&(mip->mi_rw_lock)); 3003 return (err); 3004 } 3005 3006 boolean_t 3007 mac_margin_update(mac_handle_t mh, uint32_t margin) 3008 { 3009 mac_impl_t *mip = (mac_impl_t *)mh; 3010 uint32_t margin_needed = 0; 3011 3012 rw_enter(&(mip->mi_rw_lock), RW_WRITER); 3013 3014 if (mip->mi_mmrp != NULL) 3015 margin_needed = mip->mi_mmrp->mmr_margin; 3016 3017 if (margin_needed <= margin) 3018 mip->mi_margin = margin; 3019 3020 rw_exit(&(mip->mi_rw_lock)); 3021 3022 if (margin_needed <= margin) 3023 i_mac_notify(mip, MAC_NOTE_MARGIN); 3024 3025 return (margin_needed <= margin); 3026 } 3027 3028 /* 3029 * MAC clients use this interface to request that a MAC device not change its 3030 * MTU below the specified amount. At this time, that amount must be within the 3031 * range of the device's current minimum and the device's current maximum. eg. a 3032 * client cannot request a 3000 byte MTU when the device's MTU is currently 3033 * 2000. 3034 * 3035 * If "current" is set to B_TRUE, then the request is to simply to reserve the 3036 * current underlying mac's maximum for this mac client and return it in mtup. 3037 */ 3038 int 3039 mac_mtu_add(mac_handle_t mh, uint32_t *mtup, boolean_t current) 3040 { 3041 mac_impl_t *mip = (mac_impl_t *)mh; 3042 mac_mtu_req_t *prev, *cur; 3043 mac_propval_range_t mpr; 3044 int err; 3045 3046 i_mac_perim_enter(mip); 3047 rw_enter(&mip->mi_rw_lock, RW_WRITER); 3048 3049 if (current == B_TRUE) 3050 *mtup = mip->mi_sdu_max; 3051 mpr.mpr_count = 1; 3052 err = mac_prop_info(mh, MAC_PROP_MTU, "mtu", NULL, 0, &mpr, NULL); 3053 if (err != 0) { 3054 rw_exit(&mip->mi_rw_lock); 3055 i_mac_perim_exit(mip); 3056 return (err); 3057 } 3058 3059 if (*mtup > mip->mi_sdu_max || 3060 *mtup < mpr.mpr_range_uint32[0].mpur_min) { 3061 rw_exit(&mip->mi_rw_lock); 3062 i_mac_perim_exit(mip); 3063 return (ENOTSUP); 3064 } 3065 3066 prev = NULL; 3067 for (cur = mip->mi_mtrp; cur != NULL; cur = cur->mtr_nextp) { 3068 if (*mtup == cur->mtr_mtu) { 3069 cur->mtr_ref++; 3070 rw_exit(&mip->mi_rw_lock); 3071 i_mac_perim_exit(mip); 3072 return (0); 3073 } 3074 3075 if (*mtup > cur->mtr_mtu) 3076 break; 3077 3078 prev = cur; 3079 } 3080 3081 cur = kmem_alloc(sizeof (mac_mtu_req_t), KM_SLEEP); 3082 cur->mtr_mtu = *mtup; 3083 cur->mtr_ref = 1; 3084 if (prev != NULL) { 3085 cur->mtr_nextp = prev->mtr_nextp; 3086 prev->mtr_nextp = cur; 3087 } else { 3088 cur->mtr_nextp = mip->mi_mtrp; 3089 mip->mi_mtrp = cur; 3090 } 3091 3092 rw_exit(&mip->mi_rw_lock); 3093 i_mac_perim_exit(mip); 3094 return (0); 3095 } 3096 3097 int 3098 mac_mtu_remove(mac_handle_t mh, uint32_t mtu) 3099 { 3100 mac_impl_t *mip = (mac_impl_t *)mh; 3101 mac_mtu_req_t *cur, *prev; 3102 3103 i_mac_perim_enter(mip); 3104 rw_enter(&mip->mi_rw_lock, RW_WRITER); 3105 3106 prev = NULL; 3107 for (cur = mip->mi_mtrp; cur != NULL; cur = cur->mtr_nextp) { 3108 if (cur->mtr_mtu == mtu) { 3109 ASSERT(cur->mtr_ref > 0); 3110 cur->mtr_ref--; 3111 if (cur->mtr_ref == 0) { 3112 if (prev == NULL) { 3113 mip->mi_mtrp = cur->mtr_nextp; 3114 } else { 3115 prev->mtr_nextp = cur->mtr_nextp; 3116 } 3117 kmem_free(cur, sizeof (mac_mtu_req_t)); 3118 } 3119 rw_exit(&mip->mi_rw_lock); 3120 i_mac_perim_exit(mip); 3121 return (0); 3122 } 3123 3124 prev = cur; 3125 } 3126 3127 rw_exit(&mip->mi_rw_lock); 3128 i_mac_perim_exit(mip); 3129 return (ENOENT); 3130 } 3131 3132 /* 3133 * MAC Type Plugin functions. 3134 */ 3135 3136 mactype_t * 3137 mactype_getplugin(const char *pname) 3138 { 3139 mactype_t *mtype = NULL; 3140 boolean_t tried_modload = B_FALSE; 3141 3142 mutex_enter(&i_mactype_lock); 3143 3144 find_registered_mactype: 3145 if (mod_hash_find(i_mactype_hash, (mod_hash_key_t)pname, 3146 (mod_hash_val_t *)&mtype) != 0) { 3147 if (!tried_modload) { 3148 /* 3149 * If the plugin has not yet been loaded, then 3150 * attempt to load it now. If modload() succeeds, 3151 * the plugin should have registered using 3152 * mactype_register(), in which case we can go back 3153 * and attempt to find it again. 3154 */ 3155 if (modload(MACTYPE_KMODDIR, (char *)pname) != -1) { 3156 tried_modload = B_TRUE; 3157 goto find_registered_mactype; 3158 } 3159 } 3160 } else { 3161 /* 3162 * Note that there's no danger that the plugin we've loaded 3163 * could be unloaded between the modload() step and the 3164 * reference count bump here, as we're holding 3165 * i_mactype_lock, which mactype_unregister() also holds. 3166 */ 3167 atomic_inc_32(&mtype->mt_ref); 3168 } 3169 3170 mutex_exit(&i_mactype_lock); 3171 return (mtype); 3172 } 3173 3174 mactype_register_t * 3175 mactype_alloc(uint_t mactype_version) 3176 { 3177 mactype_register_t *mtrp; 3178 3179 /* 3180 * Make sure there isn't a version mismatch between the plugin and 3181 * the framework. In the future, if multiple versions are 3182 * supported, this check could become more sophisticated. 3183 */ 3184 if (mactype_version != MACTYPE_VERSION) 3185 return (NULL); 3186 3187 mtrp = kmem_zalloc(sizeof (mactype_register_t), KM_SLEEP); 3188 mtrp->mtr_version = mactype_version; 3189 return (mtrp); 3190 } 3191 3192 void 3193 mactype_free(mactype_register_t *mtrp) 3194 { 3195 kmem_free(mtrp, sizeof (mactype_register_t)); 3196 } 3197 3198 int 3199 mactype_register(mactype_register_t *mtrp) 3200 { 3201 mactype_t *mtp; 3202 mactype_ops_t *ops = mtrp->mtr_ops; 3203 3204 /* Do some sanity checking before we register this MAC type. */ 3205 if (mtrp->mtr_ident == NULL || ops == NULL) 3206 return (EINVAL); 3207 3208 /* 3209 * Verify that all mandatory callbacks are set in the ops 3210 * vector. 3211 */ 3212 if (ops->mtops_unicst_verify == NULL || 3213 ops->mtops_multicst_verify == NULL || 3214 ops->mtops_sap_verify == NULL || 3215 ops->mtops_header == NULL || 3216 ops->mtops_header_info == NULL) { 3217 return (EINVAL); 3218 } 3219 3220 mtp = kmem_zalloc(sizeof (*mtp), KM_SLEEP); 3221 mtp->mt_ident = mtrp->mtr_ident; 3222 mtp->mt_ops = *ops; 3223 mtp->mt_type = mtrp->mtr_mactype; 3224 mtp->mt_nativetype = mtrp->mtr_nativetype; 3225 mtp->mt_addr_length = mtrp->mtr_addrlen; 3226 if (mtrp->mtr_brdcst_addr != NULL) { 3227 mtp->mt_brdcst_addr = kmem_alloc(mtrp->mtr_addrlen, KM_SLEEP); 3228 bcopy(mtrp->mtr_brdcst_addr, mtp->mt_brdcst_addr, 3229 mtrp->mtr_addrlen); 3230 } 3231 3232 mtp->mt_stats = mtrp->mtr_stats; 3233 mtp->mt_statcount = mtrp->mtr_statcount; 3234 3235 mtp->mt_mapping = mtrp->mtr_mapping; 3236 mtp->mt_mappingcount = mtrp->mtr_mappingcount; 3237 3238 if (mod_hash_insert(i_mactype_hash, 3239 (mod_hash_key_t)mtp->mt_ident, (mod_hash_val_t)mtp) != 0) { 3240 kmem_free(mtp->mt_brdcst_addr, mtp->mt_addr_length); 3241 kmem_free(mtp, sizeof (*mtp)); 3242 return (EEXIST); 3243 } 3244 return (0); 3245 } 3246 3247 int 3248 mactype_unregister(const char *ident) 3249 { 3250 mactype_t *mtp; 3251 mod_hash_val_t val; 3252 int err; 3253 3254 /* 3255 * Let's not allow MAC drivers to use this plugin while we're 3256 * trying to unregister it. Holding i_mactype_lock also prevents a 3257 * plugin from unregistering while a MAC driver is attempting to 3258 * hold a reference to it in i_mactype_getplugin(). 3259 */ 3260 mutex_enter(&i_mactype_lock); 3261 3262 if ((err = mod_hash_find(i_mactype_hash, (mod_hash_key_t)ident, 3263 (mod_hash_val_t *)&mtp)) != 0) { 3264 /* A plugin is trying to unregister, but it never registered. */ 3265 err = ENXIO; 3266 goto done; 3267 } 3268 3269 if (mtp->mt_ref != 0) { 3270 err = EBUSY; 3271 goto done; 3272 } 3273 3274 err = mod_hash_remove(i_mactype_hash, (mod_hash_key_t)ident, &val); 3275 ASSERT(err == 0); 3276 if (err != 0) { 3277 /* This should never happen, thus the ASSERT() above. */ 3278 err = EINVAL; 3279 goto done; 3280 } 3281 ASSERT(mtp == (mactype_t *)val); 3282 3283 if (mtp->mt_brdcst_addr != NULL) 3284 kmem_free(mtp->mt_brdcst_addr, mtp->mt_addr_length); 3285 kmem_free(mtp, sizeof (mactype_t)); 3286 done: 3287 mutex_exit(&i_mactype_lock); 3288 return (err); 3289 } 3290 3291 /* 3292 * Checks the size of the value size specified for a property as 3293 * part of a property operation. Returns B_TRUE if the size is 3294 * correct, B_FALSE otherwise. 3295 */ 3296 boolean_t 3297 mac_prop_check_size(mac_prop_id_t id, uint_t valsize, boolean_t is_range) 3298 { 3299 uint_t minsize = 0; 3300 3301 if (is_range) 3302 return (valsize >= sizeof (mac_propval_range_t)); 3303 3304 switch (id) { 3305 case MAC_PROP_ZONE: 3306 minsize = sizeof (dld_ioc_zid_t); 3307 break; 3308 case MAC_PROP_AUTOPUSH: 3309 if (valsize != 0) 3310 minsize = sizeof (struct dlautopush); 3311 break; 3312 case MAC_PROP_TAGMODE: 3313 minsize = sizeof (link_tagmode_t); 3314 break; 3315 case MAC_PROP_RESOURCE: 3316 case MAC_PROP_RESOURCE_EFF: 3317 minsize = sizeof (mac_resource_props_t); 3318 break; 3319 case MAC_PROP_DUPLEX: 3320 minsize = sizeof (link_duplex_t); 3321 break; 3322 case MAC_PROP_SPEED: 3323 minsize = sizeof (uint64_t); 3324 break; 3325 case MAC_PROP_STATUS: 3326 minsize = sizeof (link_state_t); 3327 break; 3328 case MAC_PROP_AUTONEG: 3329 case MAC_PROP_EN_AUTONEG: 3330 minsize = sizeof (uint8_t); 3331 break; 3332 case MAC_PROP_MTU: 3333 case MAC_PROP_LLIMIT: 3334 case MAC_PROP_LDECAY: 3335 minsize = sizeof (uint32_t); 3336 break; 3337 case MAC_PROP_FLOWCTRL: 3338 minsize = sizeof (link_flowctrl_t); 3339 break; 3340 case MAC_PROP_ADV_FEC_CAP: 3341 case MAC_PROP_EN_FEC_CAP: 3342 minsize = sizeof (link_fec_t); 3343 break; 3344 case MAC_PROP_ADV_5000FDX_CAP: 3345 case MAC_PROP_EN_5000FDX_CAP: 3346 case MAC_PROP_ADV_2500FDX_CAP: 3347 case MAC_PROP_EN_2500FDX_CAP: 3348 case MAC_PROP_ADV_100GFDX_CAP: 3349 case MAC_PROP_EN_100GFDX_CAP: 3350 case MAC_PROP_ADV_50GFDX_CAP: 3351 case MAC_PROP_EN_50GFDX_CAP: 3352 case MAC_PROP_ADV_40GFDX_CAP: 3353 case MAC_PROP_EN_40GFDX_CAP: 3354 case MAC_PROP_ADV_25GFDX_CAP: 3355 case MAC_PROP_EN_25GFDX_CAP: 3356 case MAC_PROP_ADV_10GFDX_CAP: 3357 case MAC_PROP_EN_10GFDX_CAP: 3358 case MAC_PROP_ADV_1000HDX_CAP: 3359 case MAC_PROP_EN_1000HDX_CAP: 3360 case MAC_PROP_ADV_100FDX_CAP: 3361 case MAC_PROP_EN_100FDX_CAP: 3362 case MAC_PROP_ADV_100HDX_CAP: 3363 case MAC_PROP_EN_100HDX_CAP: 3364 case MAC_PROP_ADV_10FDX_CAP: 3365 case MAC_PROP_EN_10FDX_CAP: 3366 case MAC_PROP_ADV_10HDX_CAP: 3367 case MAC_PROP_EN_10HDX_CAP: 3368 case MAC_PROP_ADV_100T4_CAP: 3369 case MAC_PROP_EN_100T4_CAP: 3370 minsize = sizeof (uint8_t); 3371 break; 3372 case MAC_PROP_PVID: 3373 minsize = sizeof (uint16_t); 3374 break; 3375 case MAC_PROP_IPTUN_HOPLIMIT: 3376 minsize = sizeof (uint32_t); 3377 break; 3378 case MAC_PROP_IPTUN_ENCAPLIMIT: 3379 minsize = sizeof (uint32_t); 3380 break; 3381 case MAC_PROP_MAX_TX_RINGS_AVAIL: 3382 case MAC_PROP_MAX_RX_RINGS_AVAIL: 3383 case MAC_PROP_MAX_RXHWCLNT_AVAIL: 3384 case MAC_PROP_MAX_TXHWCLNT_AVAIL: 3385 minsize = sizeof (uint_t); 3386 break; 3387 case MAC_PROP_WL_ESSID: 3388 minsize = sizeof (wl_linkstatus_t); 3389 break; 3390 case MAC_PROP_WL_BSSID: 3391 minsize = sizeof (wl_bssid_t); 3392 break; 3393 case MAC_PROP_WL_BSSTYPE: 3394 minsize = sizeof (wl_bss_type_t); 3395 break; 3396 case MAC_PROP_WL_LINKSTATUS: 3397 minsize = sizeof (wl_linkstatus_t); 3398 break; 3399 case MAC_PROP_WL_DESIRED_RATES: 3400 minsize = sizeof (wl_rates_t); 3401 break; 3402 case MAC_PROP_WL_SUPPORTED_RATES: 3403 minsize = sizeof (wl_rates_t); 3404 break; 3405 case MAC_PROP_WL_AUTH_MODE: 3406 minsize = sizeof (wl_authmode_t); 3407 break; 3408 case MAC_PROP_WL_ENCRYPTION: 3409 minsize = sizeof (wl_encryption_t); 3410 break; 3411 case MAC_PROP_WL_RSSI: 3412 minsize = sizeof (wl_rssi_t); 3413 break; 3414 case MAC_PROP_WL_PHY_CONFIG: 3415 minsize = sizeof (wl_phy_conf_t); 3416 break; 3417 case MAC_PROP_WL_CAPABILITY: 3418 minsize = sizeof (wl_capability_t); 3419 break; 3420 case MAC_PROP_WL_WPA: 3421 minsize = sizeof (wl_wpa_t); 3422 break; 3423 case MAC_PROP_WL_SCANRESULTS: 3424 minsize = sizeof (wl_wpa_ess_t); 3425 break; 3426 case MAC_PROP_WL_POWER_MODE: 3427 minsize = sizeof (wl_ps_mode_t); 3428 break; 3429 case MAC_PROP_WL_RADIO: 3430 minsize = sizeof (wl_radio_t); 3431 break; 3432 case MAC_PROP_WL_ESS_LIST: 3433 minsize = sizeof (wl_ess_list_t); 3434 break; 3435 case MAC_PROP_WL_KEY_TAB: 3436 minsize = sizeof (wl_wep_key_tab_t); 3437 break; 3438 case MAC_PROP_WL_CREATE_IBSS: 3439 minsize = sizeof (wl_create_ibss_t); 3440 break; 3441 case MAC_PROP_WL_SETOPTIE: 3442 minsize = sizeof (wl_wpa_ie_t); 3443 break; 3444 case MAC_PROP_WL_DELKEY: 3445 minsize = sizeof (wl_del_key_t); 3446 break; 3447 case MAC_PROP_WL_KEY: 3448 minsize = sizeof (wl_key_t); 3449 break; 3450 case MAC_PROP_WL_MLME: 3451 minsize = sizeof (wl_mlme_t); 3452 break; 3453 case MAC_PROP_VN_PROMISC_FILTERED: 3454 minsize = sizeof (boolean_t); 3455 break; 3456 } 3457 3458 return (valsize >= minsize); 3459 } 3460 3461 /* 3462 * mac_set_prop() sets MAC or hardware driver properties: 3463 * 3464 * - MAC-managed properties such as resource properties include maxbw, 3465 * priority, and cpu binding list, as well as the default port VID 3466 * used by bridging. These properties are consumed by the MAC layer 3467 * itself and not passed down to the driver. For resource control 3468 * properties, this function invokes mac_set_resources() which will 3469 * cache the property value in mac_impl_t and may call 3470 * mac_client_set_resource() to update property value of the primary 3471 * mac client, if it exists. 3472 * 3473 * - Properties which act on the hardware and must be passed to the 3474 * driver, such as MTU, through the driver's mc_setprop() entry point. 3475 */ 3476 int 3477 mac_set_prop(mac_handle_t mh, mac_prop_id_t id, char *name, void *val, 3478 uint_t valsize) 3479 { 3480 int err = ENOTSUP; 3481 mac_impl_t *mip = (mac_impl_t *)mh; 3482 3483 ASSERT(MAC_PERIM_HELD(mh)); 3484 3485 switch (id) { 3486 case MAC_PROP_RESOURCE: { 3487 mac_resource_props_t *mrp; 3488 3489 /* call mac_set_resources() for MAC properties */ 3490 ASSERT(valsize >= sizeof (mac_resource_props_t)); 3491 mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP); 3492 bcopy(val, mrp, sizeof (*mrp)); 3493 err = mac_set_resources(mh, mrp); 3494 kmem_free(mrp, sizeof (*mrp)); 3495 break; 3496 } 3497 3498 case MAC_PROP_PVID: 3499 ASSERT(valsize >= sizeof (uint16_t)); 3500 if (mip->mi_state_flags & MIS_IS_VNIC) 3501 return (EINVAL); 3502 err = mac_set_pvid(mh, *(uint16_t *)val); 3503 break; 3504 3505 case MAC_PROP_MTU: { 3506 uint32_t mtu; 3507 3508 ASSERT(valsize >= sizeof (uint32_t)); 3509 bcopy(val, &mtu, sizeof (mtu)); 3510 err = mac_set_mtu(mh, mtu, NULL); 3511 break; 3512 } 3513 3514 case MAC_PROP_LLIMIT: 3515 case MAC_PROP_LDECAY: { 3516 uint32_t learnval; 3517 3518 if (valsize < sizeof (learnval) || 3519 (mip->mi_state_flags & MIS_IS_VNIC)) 3520 return (EINVAL); 3521 bcopy(val, &learnval, sizeof (learnval)); 3522 if (learnval == 0 && id == MAC_PROP_LDECAY) 3523 return (EINVAL); 3524 if (id == MAC_PROP_LLIMIT) 3525 mip->mi_llimit = learnval; 3526 else 3527 mip->mi_ldecay = learnval; 3528 err = 0; 3529 break; 3530 } 3531 3532 case MAC_PROP_ADV_FEC_CAP: 3533 case MAC_PROP_EN_FEC_CAP: { 3534 link_fec_t fec; 3535 3536 ASSERT(valsize >= sizeof (link_fec_t)); 3537 3538 /* 3539 * fec cannot be zero, and auto must be set exclusively. 3540 */ 3541 bcopy(val, &fec, sizeof (link_fec_t)); 3542 if (fec == 0) 3543 return (EINVAL); 3544 if ((fec & LINK_FEC_AUTO) != 0 && (fec & ~LINK_FEC_AUTO) != 0) 3545 return (EINVAL); 3546 3547 if (mip->mi_callbacks->mc_callbacks & MC_SETPROP) { 3548 err = mip->mi_callbacks->mc_setprop(mip->mi_driver, 3549 name, id, valsize, val); 3550 } 3551 break; 3552 } 3553 3554 default: 3555 /* For other driver properties, call driver's callback */ 3556 if (mip->mi_callbacks->mc_callbacks & MC_SETPROP) { 3557 err = mip->mi_callbacks->mc_setprop(mip->mi_driver, 3558 name, id, valsize, val); 3559 } 3560 } 3561 return (err); 3562 } 3563 3564 /* 3565 * mac_get_prop() gets MAC or device driver properties. 3566 * 3567 * If the property is a driver property, mac_get_prop() calls driver's callback 3568 * entry point to get it. 3569 * If the property is a MAC property, mac_get_prop() invokes mac_get_resources() 3570 * which returns the cached value in mac_impl_t. 3571 */ 3572 int 3573 mac_get_prop(mac_handle_t mh, mac_prop_id_t id, char *name, void *val, 3574 uint_t valsize) 3575 { 3576 int err = ENOTSUP; 3577 mac_impl_t *mip = (mac_impl_t *)mh; 3578 uint_t rings; 3579 uint_t vlinks; 3580 3581 bzero(val, valsize); 3582 3583 switch (id) { 3584 case MAC_PROP_RESOURCE: { 3585 mac_resource_props_t *mrp; 3586 3587 /* If mac property, read from cache */ 3588 ASSERT(valsize >= sizeof (mac_resource_props_t)); 3589 mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP); 3590 mac_get_resources(mh, mrp); 3591 bcopy(mrp, val, sizeof (*mrp)); 3592 kmem_free(mrp, sizeof (*mrp)); 3593 return (0); 3594 } 3595 case MAC_PROP_RESOURCE_EFF: { 3596 mac_resource_props_t *mrp; 3597 3598 /* If mac effective property, read from client */ 3599 ASSERT(valsize >= sizeof (mac_resource_props_t)); 3600 mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP); 3601 mac_get_effective_resources(mh, mrp); 3602 bcopy(mrp, val, sizeof (*mrp)); 3603 kmem_free(mrp, sizeof (*mrp)); 3604 return (0); 3605 } 3606 3607 case MAC_PROP_PVID: 3608 ASSERT(valsize >= sizeof (uint16_t)); 3609 if (mip->mi_state_flags & MIS_IS_VNIC) 3610 return (EINVAL); 3611 *(uint16_t *)val = mac_get_pvid(mh); 3612 return (0); 3613 3614 case MAC_PROP_LLIMIT: 3615 case MAC_PROP_LDECAY: 3616 ASSERT(valsize >= sizeof (uint32_t)); 3617 if (mip->mi_state_flags & MIS_IS_VNIC) 3618 return (EINVAL); 3619 if (id == MAC_PROP_LLIMIT) 3620 bcopy(&mip->mi_llimit, val, sizeof (mip->mi_llimit)); 3621 else 3622 bcopy(&mip->mi_ldecay, val, sizeof (mip->mi_ldecay)); 3623 return (0); 3624 3625 case MAC_PROP_MTU: { 3626 uint32_t sdu; 3627 3628 ASSERT(valsize >= sizeof (uint32_t)); 3629 mac_sdu_get2(mh, NULL, &sdu, NULL); 3630 bcopy(&sdu, val, sizeof (sdu)); 3631 3632 return (0); 3633 } 3634 case MAC_PROP_STATUS: { 3635 link_state_t link_state; 3636 3637 if (valsize < sizeof (link_state)) 3638 return (EINVAL); 3639 link_state = mac_link_get(mh); 3640 bcopy(&link_state, val, sizeof (link_state)); 3641 3642 return (0); 3643 } 3644 3645 case MAC_PROP_MAX_RX_RINGS_AVAIL: 3646 case MAC_PROP_MAX_TX_RINGS_AVAIL: 3647 ASSERT(valsize >= sizeof (uint_t)); 3648 rings = id == MAC_PROP_MAX_RX_RINGS_AVAIL ? 3649 mac_rxavail_get(mh) : mac_txavail_get(mh); 3650 bcopy(&rings, val, sizeof (uint_t)); 3651 return (0); 3652 3653 case MAC_PROP_MAX_RXHWCLNT_AVAIL: 3654 case MAC_PROP_MAX_TXHWCLNT_AVAIL: 3655 ASSERT(valsize >= sizeof (uint_t)); 3656 vlinks = id == MAC_PROP_MAX_RXHWCLNT_AVAIL ? 3657 mac_rxhwlnksavail_get(mh) : mac_txhwlnksavail_get(mh); 3658 bcopy(&vlinks, val, sizeof (uint_t)); 3659 return (0); 3660 3661 case MAC_PROP_RXRINGSRANGE: 3662 case MAC_PROP_TXRINGSRANGE: 3663 /* 3664 * The value for these properties are returned through 3665 * the MAC_PROP_RESOURCE property. 3666 */ 3667 return (0); 3668 3669 default: 3670 break; 3671 3672 } 3673 3674 /* If driver property, request from driver */ 3675 if (mip->mi_callbacks->mc_callbacks & MC_GETPROP) { 3676 err = mip->mi_callbacks->mc_getprop(mip->mi_driver, name, id, 3677 valsize, val); 3678 } 3679 3680 return (err); 3681 } 3682 3683 /* 3684 * Helper function to initialize the range structure for use in 3685 * mac_get_prop. If the type can be other than uint32, we can 3686 * pass that as an arg. 3687 */ 3688 static void 3689 _mac_set_range(mac_propval_range_t *range, uint32_t min, uint32_t max) 3690 { 3691 range->mpr_count = 1; 3692 range->mpr_type = MAC_PROPVAL_UINT32; 3693 range->mpr_range_uint32[0].mpur_min = min; 3694 range->mpr_range_uint32[0].mpur_max = max; 3695 } 3696 3697 /* 3698 * Returns information about the specified property, such as default 3699 * values or permissions. 3700 */ 3701 int 3702 mac_prop_info(mac_handle_t mh, mac_prop_id_t id, char *name, 3703 void *default_val, uint_t default_size, mac_propval_range_t *range, 3704 uint_t *perm) 3705 { 3706 mac_prop_info_state_t state; 3707 mac_impl_t *mip = (mac_impl_t *)mh; 3708 uint_t max; 3709 3710 /* 3711 * A property is read/write by default unless the driver says 3712 * otherwise. 3713 */ 3714 if (perm != NULL) 3715 *perm = MAC_PROP_PERM_RW; 3716 3717 if (default_val != NULL) 3718 bzero(default_val, default_size); 3719 3720 /* 3721 * First, handle framework properties for which we don't need to 3722 * involve the driver. 3723 */ 3724 switch (id) { 3725 case MAC_PROP_RESOURCE: 3726 case MAC_PROP_PVID: 3727 case MAC_PROP_LLIMIT: 3728 case MAC_PROP_LDECAY: 3729 return (0); 3730 3731 case MAC_PROP_MAX_RX_RINGS_AVAIL: 3732 case MAC_PROP_MAX_TX_RINGS_AVAIL: 3733 case MAC_PROP_MAX_RXHWCLNT_AVAIL: 3734 case MAC_PROP_MAX_TXHWCLNT_AVAIL: 3735 if (perm != NULL) 3736 *perm = MAC_PROP_PERM_READ; 3737 return (0); 3738 3739 case MAC_PROP_RXRINGSRANGE: 3740 case MAC_PROP_TXRINGSRANGE: 3741 /* 3742 * Currently, we support range for RX and TX rings properties. 3743 * When we extend this support to maxbw, cpus and priority, 3744 * we should move this to mac_get_resources. 3745 * There is no default value for RX or TX rings. 3746 */ 3747 if ((mip->mi_state_flags & MIS_IS_VNIC) && 3748 mac_is_vnic_primary(mh)) { 3749 /* 3750 * We don't support setting rings for a VLAN 3751 * data link because it shares its ring with the 3752 * primary MAC client. 3753 */ 3754 if (perm != NULL) 3755 *perm = MAC_PROP_PERM_READ; 3756 if (range != NULL) 3757 range->mpr_count = 0; 3758 } else if (range != NULL) { 3759 if (mip->mi_state_flags & MIS_IS_VNIC) 3760 mh = mac_get_lower_mac_handle(mh); 3761 mip = (mac_impl_t *)mh; 3762 if ((id == MAC_PROP_RXRINGSRANGE && 3763 mip->mi_rx_group_type == MAC_GROUP_TYPE_STATIC) || 3764 (id == MAC_PROP_TXRINGSRANGE && 3765 mip->mi_tx_group_type == MAC_GROUP_TYPE_STATIC)) { 3766 if (id == MAC_PROP_RXRINGSRANGE) { 3767 if ((mac_rxhwlnksavail_get(mh) + 3768 mac_rxhwlnksrsvd_get(mh)) <= 1) { 3769 /* 3770 * doesn't support groups or 3771 * rings 3772 */ 3773 range->mpr_count = 0; 3774 } else { 3775 /* 3776 * supports specifying groups, 3777 * but not rings 3778 */ 3779 _mac_set_range(range, 0, 0); 3780 } 3781 } else { 3782 if ((mac_txhwlnksavail_get(mh) + 3783 mac_txhwlnksrsvd_get(mh)) <= 1) { 3784 /* 3785 * doesn't support groups or 3786 * rings 3787 */ 3788 range->mpr_count = 0; 3789 } else { 3790 /* 3791 * supports specifying groups, 3792 * but not rings 3793 */ 3794 _mac_set_range(range, 0, 0); 3795 } 3796 } 3797 } else { 3798 max = id == MAC_PROP_RXRINGSRANGE ? 3799 mac_rxavail_get(mh) + mac_rxrsvd_get(mh) : 3800 mac_txavail_get(mh) + mac_txrsvd_get(mh); 3801 if (max <= 1) { 3802 /* 3803 * doesn't support groups or 3804 * rings 3805 */ 3806 range->mpr_count = 0; 3807 } else { 3808 /* 3809 * -1 because we have to leave out the 3810 * default ring. 3811 */ 3812 _mac_set_range(range, 1, max - 1); 3813 } 3814 } 3815 } 3816 return (0); 3817 3818 case MAC_PROP_STATUS: 3819 if (perm != NULL) 3820 *perm = MAC_PROP_PERM_READ; 3821 return (0); 3822 } 3823 3824 /* 3825 * Get the property info from the driver if it implements the 3826 * property info entry point. 3827 */ 3828 bzero(&state, sizeof (state)); 3829 3830 if (mip->mi_callbacks->mc_callbacks & MC_PROPINFO) { 3831 state.pr_default = default_val; 3832 state.pr_default_size = default_size; 3833 3834 /* 3835 * The caller specifies the maximum number of ranges 3836 * it can accomodate using mpr_count. We don't touch 3837 * this value until the driver returns from its 3838 * mc_propinfo() callback, and ensure we don't exceed 3839 * this number of range as the driver defines 3840 * supported range from its mc_propinfo(). 3841 * 3842 * pr_range_cur_count keeps track of how many ranges 3843 * were defined by the driver from its mc_propinfo() 3844 * entry point. 3845 * 3846 * On exit, the user-specified range mpr_count returns 3847 * the number of ranges specified by the driver on 3848 * success, or the number of ranges it wanted to 3849 * define if that number of ranges could not be 3850 * accomodated by the specified range structure. In 3851 * the latter case, the caller will be able to 3852 * allocate a larger range structure, and query the 3853 * property again. 3854 */ 3855 state.pr_range_cur_count = 0; 3856 state.pr_range = range; 3857 3858 mip->mi_callbacks->mc_propinfo(mip->mi_driver, name, id, 3859 (mac_prop_info_handle_t)&state); 3860 3861 if (state.pr_flags & MAC_PROP_INFO_RANGE) 3862 range->mpr_count = state.pr_range_cur_count; 3863 3864 /* 3865 * The operation could fail if the buffer supplied by 3866 * the user was too small for the range or default 3867 * value of the property. 3868 */ 3869 if (state.pr_errno != 0) 3870 return (state.pr_errno); 3871 3872 if (perm != NULL && state.pr_flags & MAC_PROP_INFO_PERM) 3873 *perm = state.pr_perm; 3874 } 3875 3876 /* 3877 * The MAC layer may want to provide default values or allowed 3878 * ranges for properties if the driver does not provide a 3879 * property info entry point, or that entry point exists, but 3880 * it did not provide a default value or allowed ranges for 3881 * that property. 3882 */ 3883 switch (id) { 3884 case MAC_PROP_MTU: { 3885 uint32_t sdu; 3886 3887 mac_sdu_get2(mh, NULL, &sdu, NULL); 3888 3889 if (range != NULL && !(state.pr_flags & 3890 MAC_PROP_INFO_RANGE)) { 3891 /* MTU range */ 3892 _mac_set_range(range, sdu, sdu); 3893 } 3894 3895 if (default_val != NULL && !(state.pr_flags & 3896 MAC_PROP_INFO_DEFAULT)) { 3897 if (mip->mi_info.mi_media == DL_ETHER) 3898 sdu = ETHERMTU; 3899 /* default MTU value */ 3900 bcopy(&sdu, default_val, sizeof (sdu)); 3901 } 3902 } 3903 } 3904 3905 return (0); 3906 } 3907 3908 int 3909 mac_fastpath_disable(mac_handle_t mh) 3910 { 3911 mac_impl_t *mip = (mac_impl_t *)mh; 3912 3913 if ((mip->mi_state_flags & MIS_LEGACY) == 0) 3914 return (0); 3915 3916 return (mip->mi_capab_legacy.ml_fastpath_disable(mip->mi_driver)); 3917 } 3918 3919 void 3920 mac_fastpath_enable(mac_handle_t mh) 3921 { 3922 mac_impl_t *mip = (mac_impl_t *)mh; 3923 3924 if ((mip->mi_state_flags & MIS_LEGACY) == 0) 3925 return; 3926 3927 mip->mi_capab_legacy.ml_fastpath_enable(mip->mi_driver); 3928 } 3929 3930 void 3931 mac_register_priv_prop(mac_impl_t *mip, char **priv_props) 3932 { 3933 uint_t nprops, i; 3934 3935 if (priv_props == NULL) 3936 return; 3937 3938 nprops = 0; 3939 while (priv_props[nprops] != NULL) 3940 nprops++; 3941 if (nprops == 0) 3942 return; 3943 3944 3945 mip->mi_priv_prop = kmem_zalloc(nprops * sizeof (char *), KM_SLEEP); 3946 3947 for (i = 0; i < nprops; i++) { 3948 mip->mi_priv_prop[i] = kmem_zalloc(MAXLINKPROPNAME, KM_SLEEP); 3949 (void) strlcpy(mip->mi_priv_prop[i], priv_props[i], 3950 MAXLINKPROPNAME); 3951 } 3952 3953 mip->mi_priv_prop_count = nprops; 3954 } 3955 3956 void 3957 mac_unregister_priv_prop(mac_impl_t *mip) 3958 { 3959 uint_t i; 3960 3961 if (mip->mi_priv_prop_count == 0) { 3962 ASSERT(mip->mi_priv_prop == NULL); 3963 return; 3964 } 3965 3966 for (i = 0; i < mip->mi_priv_prop_count; i++) 3967 kmem_free(mip->mi_priv_prop[i], MAXLINKPROPNAME); 3968 kmem_free(mip->mi_priv_prop, mip->mi_priv_prop_count * 3969 sizeof (char *)); 3970 3971 mip->mi_priv_prop = NULL; 3972 mip->mi_priv_prop_count = 0; 3973 } 3974 3975 /* 3976 * mac_ring_t 'mr' macros. Some rogue drivers may access ring structure 3977 * (by invoking mac_rx()) even after processing mac_stop_ring(). In such 3978 * cases if MAC free's the ring structure after mac_stop_ring(), any 3979 * illegal access to the ring structure coming from the driver will panic 3980 * the system. In order to protect the system from such inadverent access, 3981 * we maintain a cache of rings in the mac_impl_t after they get free'd up. 3982 * When packets are received on free'd up rings, MAC (through the generation 3983 * count mechanism) will drop such packets. 3984 */ 3985 static mac_ring_t * 3986 mac_ring_alloc(mac_impl_t *mip) 3987 { 3988 mac_ring_t *ring; 3989 3990 mutex_enter(&mip->mi_ring_lock); 3991 if (mip->mi_ring_freelist != NULL) { 3992 ring = mip->mi_ring_freelist; 3993 mip->mi_ring_freelist = ring->mr_next; 3994 bzero(ring, sizeof (mac_ring_t)); 3995 mutex_exit(&mip->mi_ring_lock); 3996 } else { 3997 mutex_exit(&mip->mi_ring_lock); 3998 ring = kmem_cache_alloc(mac_ring_cache, KM_SLEEP); 3999 } 4000 ASSERT((ring != NULL) && (ring->mr_state == MR_FREE)); 4001 return (ring); 4002 } 4003 4004 static void 4005 mac_ring_free(mac_impl_t *mip, mac_ring_t *ring) 4006 { 4007 ASSERT(ring->mr_state == MR_FREE); 4008 4009 mutex_enter(&mip->mi_ring_lock); 4010 ring->mr_state = MR_FREE; 4011 ring->mr_flag = 0; 4012 ring->mr_next = mip->mi_ring_freelist; 4013 ring->mr_mip = NULL; 4014 mip->mi_ring_freelist = ring; 4015 mac_ring_stat_delete(ring); 4016 mutex_exit(&mip->mi_ring_lock); 4017 } 4018 4019 static void 4020 mac_ring_freeall(mac_impl_t *mip) 4021 { 4022 mac_ring_t *ring_next; 4023 mutex_enter(&mip->mi_ring_lock); 4024 mac_ring_t *ring = mip->mi_ring_freelist; 4025 while (ring != NULL) { 4026 ring_next = ring->mr_next; 4027 kmem_cache_free(mac_ring_cache, ring); 4028 ring = ring_next; 4029 } 4030 mip->mi_ring_freelist = NULL; 4031 mutex_exit(&mip->mi_ring_lock); 4032 } 4033 4034 int 4035 mac_start_ring(mac_ring_t *ring) 4036 { 4037 int rv = 0; 4038 4039 ASSERT(ring->mr_state == MR_FREE); 4040 4041 if (ring->mr_start != NULL) { 4042 rv = ring->mr_start(ring->mr_driver, ring->mr_gen_num); 4043 if (rv != 0) 4044 return (rv); 4045 } 4046 4047 ring->mr_state = MR_INUSE; 4048 return (rv); 4049 } 4050 4051 void 4052 mac_stop_ring(mac_ring_t *ring) 4053 { 4054 ASSERT(ring->mr_state == MR_INUSE); 4055 4056 if (ring->mr_stop != NULL) 4057 ring->mr_stop(ring->mr_driver); 4058 4059 ring->mr_state = MR_FREE; 4060 4061 /* 4062 * Increment the ring generation number for this ring. 4063 */ 4064 ring->mr_gen_num++; 4065 } 4066 4067 int 4068 mac_start_group(mac_group_t *group) 4069 { 4070 int rv = 0; 4071 4072 if (group->mrg_start != NULL) 4073 rv = group->mrg_start(group->mrg_driver); 4074 4075 return (rv); 4076 } 4077 4078 void 4079 mac_stop_group(mac_group_t *group) 4080 { 4081 if (group->mrg_stop != NULL) 4082 group->mrg_stop(group->mrg_driver); 4083 } 4084 4085 /* 4086 * Called from mac_start() on the default Rx group. Broadcast and multicast 4087 * packets are received only on the default group. Hence the default group 4088 * needs to be up even if the primary client is not up, for the other groups 4089 * to be functional. We do this by calling this function at mac_start time 4090 * itself. However the broadcast packets that are received can't make their 4091 * way beyond mac_rx until a mac client creates a broadcast flow. 4092 */ 4093 static int 4094 mac_start_group_and_rings(mac_group_t *group) 4095 { 4096 mac_ring_t *ring; 4097 int rv = 0; 4098 4099 ASSERT(group->mrg_state == MAC_GROUP_STATE_REGISTERED); 4100 if ((rv = mac_start_group(group)) != 0) 4101 return (rv); 4102 4103 for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next) { 4104 ASSERT(ring->mr_state == MR_FREE); 4105 4106 if ((rv = mac_start_ring(ring)) != 0) 4107 goto error; 4108 4109 /* 4110 * When aggr_set_port_sdu() is called, it will remove 4111 * the port client's unicast address. This will cause 4112 * MAC to stop the default group's rings on the port 4113 * MAC. After it modifies the SDU, it will then re-add 4114 * the unicast address. At which time, this function is 4115 * called to start the default group's rings. Normally 4116 * this function would set the classify type to 4117 * MAC_SW_CLASSIFIER; but that will break aggr which 4118 * relies on the passthru classify mode being set for 4119 * correct delivery (see mac_rx_common()). To avoid 4120 * that, we check for a passthru callback and set the 4121 * classify type to MAC_PASSTHRU_CLASSIFIER; as it was 4122 * before the rings were stopped. 4123 */ 4124 ring->mr_classify_type = (ring->mr_pt_fn != NULL) ? 4125 MAC_PASSTHRU_CLASSIFIER : MAC_SW_CLASSIFIER; 4126 } 4127 return (0); 4128 4129 error: 4130 mac_stop_group_and_rings(group); 4131 return (rv); 4132 } 4133 4134 /* Called from mac_stop on the default Rx group */ 4135 static void 4136 mac_stop_group_and_rings(mac_group_t *group) 4137 { 4138 mac_ring_t *ring; 4139 4140 for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next) { 4141 if (ring->mr_state != MR_FREE) { 4142 mac_stop_ring(ring); 4143 ring->mr_flag = 0; 4144 ring->mr_classify_type = MAC_NO_CLASSIFIER; 4145 } 4146 } 4147 mac_stop_group(group); 4148 } 4149 4150 4151 static mac_ring_t * 4152 mac_init_ring(mac_impl_t *mip, mac_group_t *group, int index, 4153 mac_capab_rings_t *cap_rings) 4154 { 4155 mac_ring_t *ring, *rnext; 4156 mac_ring_info_t ring_info; 4157 ddi_intr_handle_t ddi_handle; 4158 4159 ring = mac_ring_alloc(mip); 4160 4161 /* Prepare basic information of ring */ 4162 4163 /* 4164 * Ring index is numbered to be unique across a particular device. 4165 * Ring index computation makes following assumptions: 4166 * - For drivers with static grouping (e.g. ixgbe, bge), 4167 * ring index exchanged with the driver (e.g. during mr_rget) 4168 * is unique only across the group the ring belongs to. 4169 * - Drivers with dynamic grouping (e.g. nxge), start 4170 * with single group (mrg_index = 0). 4171 */ 4172 ring->mr_index = group->mrg_index * group->mrg_info.mgi_count + index; 4173 ring->mr_type = group->mrg_type; 4174 ring->mr_gh = (mac_group_handle_t)group; 4175 4176 /* Insert the new ring to the list. */ 4177 ring->mr_next = group->mrg_rings; 4178 group->mrg_rings = ring; 4179 4180 /* Zero to reuse the info data structure */ 4181 bzero(&ring_info, sizeof (ring_info)); 4182 4183 /* Query ring information from driver */ 4184 cap_rings->mr_rget(mip->mi_driver, group->mrg_type, group->mrg_index, 4185 index, &ring_info, (mac_ring_handle_t)ring); 4186 4187 ring->mr_info = ring_info; 4188 4189 /* 4190 * The interrupt handle could be shared among multiple rings. 4191 * Thus if there is a bunch of rings that are sharing an 4192 * interrupt, then only one ring among the bunch will be made 4193 * available for interrupt re-targeting; the rest will have 4194 * ddi_shared flag set to TRUE and would not be available for 4195 * be interrupt re-targeting. 4196 */ 4197 if ((ddi_handle = ring_info.mri_intr.mi_ddi_handle) != NULL) { 4198 rnext = ring->mr_next; 4199 while (rnext != NULL) { 4200 if (rnext->mr_info.mri_intr.mi_ddi_handle == 4201 ddi_handle) { 4202 /* 4203 * If default ring (mr_index == 0) is part 4204 * of a group of rings sharing an 4205 * interrupt, then set ddi_shared flag for 4206 * the default ring and give another ring 4207 * the chance to be re-targeted. 4208 */ 4209 if (rnext->mr_index == 0 && 4210 !rnext->mr_info.mri_intr.mi_ddi_shared) { 4211 rnext->mr_info.mri_intr.mi_ddi_shared = 4212 B_TRUE; 4213 } else { 4214 ring->mr_info.mri_intr.mi_ddi_shared = 4215 B_TRUE; 4216 } 4217 break; 4218 } 4219 rnext = rnext->mr_next; 4220 } 4221 /* 4222 * If rnext is NULL, then no matching ddi_handle was found. 4223 * Rx rings get registered first. So if this is a Tx ring, 4224 * then go through all the Rx rings and see if there is a 4225 * matching ddi handle. 4226 */ 4227 if (rnext == NULL && ring->mr_type == MAC_RING_TYPE_TX) { 4228 mac_compare_ddi_handle(mip->mi_rx_groups, 4229 mip->mi_rx_group_count, ring); 4230 } 4231 } 4232 4233 /* Update ring's status */ 4234 ring->mr_state = MR_FREE; 4235 ring->mr_flag = 0; 4236 4237 /* Update the ring count of the group */ 4238 group->mrg_cur_count++; 4239 4240 /* Create per ring kstats */ 4241 if (ring->mr_stat != NULL) { 4242 ring->mr_mip = mip; 4243 mac_ring_stat_create(ring); 4244 } 4245 4246 return (ring); 4247 } 4248 4249 /* 4250 * Rings are chained together for easy regrouping. 4251 */ 4252 static void 4253 mac_init_group(mac_impl_t *mip, mac_group_t *group, int size, 4254 mac_capab_rings_t *cap_rings) 4255 { 4256 int index; 4257 4258 /* 4259 * Initialize all ring members of this group. Size of zero will not 4260 * enter the loop, so it's safe for initializing an empty group. 4261 */ 4262 for (index = size - 1; index >= 0; index--) 4263 (void) mac_init_ring(mip, group, index, cap_rings); 4264 } 4265 4266 int 4267 mac_init_rings(mac_impl_t *mip, mac_ring_type_t rtype) 4268 { 4269 mac_capab_rings_t *cap_rings; 4270 mac_group_t *group; 4271 mac_group_t *groups; 4272 mac_group_info_t group_info; 4273 uint_t group_free = 0; 4274 uint_t ring_left; 4275 mac_ring_t *ring; 4276 int g; 4277 int err = 0; 4278 uint_t grpcnt; 4279 boolean_t pseudo_txgrp = B_FALSE; 4280 4281 switch (rtype) { 4282 case MAC_RING_TYPE_RX: 4283 ASSERT(mip->mi_rx_groups == NULL); 4284 4285 cap_rings = &mip->mi_rx_rings_cap; 4286 cap_rings->mr_type = MAC_RING_TYPE_RX; 4287 break; 4288 case MAC_RING_TYPE_TX: 4289 ASSERT(mip->mi_tx_groups == NULL); 4290 4291 cap_rings = &mip->mi_tx_rings_cap; 4292 cap_rings->mr_type = MAC_RING_TYPE_TX; 4293 break; 4294 default: 4295 ASSERT(B_FALSE); 4296 } 4297 4298 if (!i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_RINGS, cap_rings)) 4299 return (0); 4300 grpcnt = cap_rings->mr_gnum; 4301 4302 /* 4303 * If we have multiple TX rings, but only one TX group, we can 4304 * create pseudo TX groups (one per TX ring) in the MAC layer, 4305 * except for an aggr. For an aggr currently we maintain only 4306 * one group with all the rings (for all its ports), going 4307 * forwards we might change this. 4308 */ 4309 if (rtype == MAC_RING_TYPE_TX && 4310 cap_rings->mr_gnum == 0 && cap_rings->mr_rnum > 0 && 4311 (mip->mi_state_flags & MIS_IS_AGGR) == 0) { 4312 /* 4313 * The -1 here is because we create a default TX group 4314 * with all the rings in it. 4315 */ 4316 grpcnt = cap_rings->mr_rnum - 1; 4317 pseudo_txgrp = B_TRUE; 4318 } 4319 4320 /* 4321 * Allocate a contiguous buffer for all groups. 4322 */ 4323 groups = kmem_zalloc(sizeof (mac_group_t) * (grpcnt+ 1), KM_SLEEP); 4324 4325 ring_left = cap_rings->mr_rnum; 4326 4327 /* 4328 * Get all ring groups if any, and get their ring members 4329 * if any. 4330 */ 4331 for (g = 0; g < grpcnt; g++) { 4332 group = groups + g; 4333 4334 /* Prepare basic information of the group */ 4335 group->mrg_index = g; 4336 group->mrg_type = rtype; 4337 group->mrg_state = MAC_GROUP_STATE_UNINIT; 4338 group->mrg_mh = (mac_handle_t)mip; 4339 group->mrg_next = group + 1; 4340 4341 /* Zero to reuse the info data structure */ 4342 bzero(&group_info, sizeof (group_info)); 4343 4344 if (pseudo_txgrp) { 4345 /* 4346 * This is a pseudo group that we created, apart 4347 * from setting the state there is nothing to be 4348 * done. 4349 */ 4350 group->mrg_state = MAC_GROUP_STATE_REGISTERED; 4351 group_free++; 4352 continue; 4353 } 4354 /* Query group information from driver */ 4355 cap_rings->mr_gget(mip->mi_driver, rtype, g, &group_info, 4356 (mac_group_handle_t)group); 4357 4358 switch (cap_rings->mr_group_type) { 4359 case MAC_GROUP_TYPE_DYNAMIC: 4360 if (cap_rings->mr_gaddring == NULL || 4361 cap_rings->mr_gremring == NULL) { 4362 DTRACE_PROBE3( 4363 mac__init__rings_no_addremring, 4364 char *, mip->mi_name, 4365 mac_group_add_ring_t, 4366 cap_rings->mr_gaddring, 4367 mac_group_add_ring_t, 4368 cap_rings->mr_gremring); 4369 err = EINVAL; 4370 goto bail; 4371 } 4372 4373 switch (rtype) { 4374 case MAC_RING_TYPE_RX: 4375 /* 4376 * The first RX group must have non-zero 4377 * rings, and the following groups must 4378 * have zero rings. 4379 */ 4380 if (g == 0 && group_info.mgi_count == 0) { 4381 DTRACE_PROBE1( 4382 mac__init__rings__rx__def__zero, 4383 char *, mip->mi_name); 4384 err = EINVAL; 4385 goto bail; 4386 } 4387 if (g > 0 && group_info.mgi_count != 0) { 4388 DTRACE_PROBE3( 4389 mac__init__rings__rx__nonzero, 4390 char *, mip->mi_name, 4391 int, g, int, group_info.mgi_count); 4392 err = EINVAL; 4393 goto bail; 4394 } 4395 break; 4396 case MAC_RING_TYPE_TX: 4397 /* 4398 * All TX ring groups must have zero rings. 4399 */ 4400 if (group_info.mgi_count != 0) { 4401 DTRACE_PROBE3( 4402 mac__init__rings__tx__nonzero, 4403 char *, mip->mi_name, 4404 int, g, int, group_info.mgi_count); 4405 err = EINVAL; 4406 goto bail; 4407 } 4408 break; 4409 } 4410 break; 4411 case MAC_GROUP_TYPE_STATIC: 4412 /* 4413 * Note that an empty group is allowed, e.g., an aggr 4414 * would start with an empty group. 4415 */ 4416 break; 4417 default: 4418 /* unknown group type */ 4419 DTRACE_PROBE2(mac__init__rings__unknown__type, 4420 char *, mip->mi_name, 4421 int, cap_rings->mr_group_type); 4422 err = EINVAL; 4423 goto bail; 4424 } 4425 4426 4427 /* 4428 * The driver must register some form of hardware MAC 4429 * filter in order for Rx groups to support multiple 4430 * MAC addresses. 4431 */ 4432 if (rtype == MAC_RING_TYPE_RX && 4433 (group_info.mgi_addmac == NULL || 4434 group_info.mgi_remmac == NULL)) { 4435 DTRACE_PROBE1(mac__init__rings__no__mac__filter, 4436 char *, mip->mi_name); 4437 err = EINVAL; 4438 goto bail; 4439 } 4440 4441 /* Cache driver-supplied information */ 4442 group->mrg_info = group_info; 4443 4444 /* Update the group's status and group count. */ 4445 mac_set_group_state(group, MAC_GROUP_STATE_REGISTERED); 4446 group_free++; 4447 4448 group->mrg_rings = NULL; 4449 group->mrg_cur_count = 0; 4450 mac_init_group(mip, group, group_info.mgi_count, cap_rings); 4451 ring_left -= group_info.mgi_count; 4452 4453 /* The current group size should be equal to default value */ 4454 ASSERT(group->mrg_cur_count == group_info.mgi_count); 4455 } 4456 4457 /* Build up a dummy group for free resources as a pool */ 4458 group = groups + grpcnt; 4459 4460 /* Prepare basic information of the group */ 4461 group->mrg_index = -1; 4462 group->mrg_type = rtype; 4463 group->mrg_state = MAC_GROUP_STATE_UNINIT; 4464 group->mrg_mh = (mac_handle_t)mip; 4465 group->mrg_next = NULL; 4466 4467 /* 4468 * If there are ungrouped rings, allocate a continuous buffer for 4469 * remaining resources. 4470 */ 4471 if (ring_left != 0) { 4472 group->mrg_rings = NULL; 4473 group->mrg_cur_count = 0; 4474 mac_init_group(mip, group, ring_left, cap_rings); 4475 4476 /* The current group size should be equal to ring_left */ 4477 ASSERT(group->mrg_cur_count == ring_left); 4478 4479 ring_left = 0; 4480 4481 /* Update this group's status */ 4482 mac_set_group_state(group, MAC_GROUP_STATE_REGISTERED); 4483 } else { 4484 group->mrg_rings = NULL; 4485 } 4486 4487 ASSERT(ring_left == 0); 4488 4489 bail: 4490 4491 /* Cache other important information to finalize the initialization */ 4492 switch (rtype) { 4493 case MAC_RING_TYPE_RX: 4494 mip->mi_rx_group_type = cap_rings->mr_group_type; 4495 mip->mi_rx_group_count = cap_rings->mr_gnum; 4496 mip->mi_rx_groups = groups; 4497 mip->mi_rx_donor_grp = groups; 4498 if (mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) { 4499 /* 4500 * The default ring is reserved since it is 4501 * used for sending the broadcast etc. packets. 4502 */ 4503 mip->mi_rxrings_avail = 4504 mip->mi_rx_groups->mrg_cur_count - 1; 4505 mip->mi_rxrings_rsvd = 1; 4506 } 4507 /* 4508 * The default group cannot be reserved. It is used by 4509 * all the clients that do not have an exclusive group. 4510 */ 4511 mip->mi_rxhwclnt_avail = mip->mi_rx_group_count - 1; 4512 mip->mi_rxhwclnt_used = 1; 4513 break; 4514 case MAC_RING_TYPE_TX: 4515 mip->mi_tx_group_type = pseudo_txgrp ? MAC_GROUP_TYPE_DYNAMIC : 4516 cap_rings->mr_group_type; 4517 mip->mi_tx_group_count = grpcnt; 4518 mip->mi_tx_group_free = group_free; 4519 mip->mi_tx_groups = groups; 4520 4521 group = groups + grpcnt; 4522 ring = group->mrg_rings; 4523 /* 4524 * The ring can be NULL in the case of aggr. Aggr will 4525 * have an empty Tx group which will get populated 4526 * later when pseudo Tx rings are added after 4527 * mac_register() is done. 4528 */ 4529 if (ring == NULL) { 4530 ASSERT(mip->mi_state_flags & MIS_IS_AGGR); 4531 /* 4532 * pass the group to aggr so it can add Tx 4533 * rings to the group later. 4534 */ 4535 cap_rings->mr_gget(mip->mi_driver, rtype, 0, NULL, 4536 (mac_group_handle_t)group); 4537 /* 4538 * Even though there are no rings at this time 4539 * (rings will come later), set the group 4540 * state to registered. 4541 */ 4542 group->mrg_state = MAC_GROUP_STATE_REGISTERED; 4543 } else { 4544 /* 4545 * Ring 0 is used as the default one and it could be 4546 * assigned to a client as well. 4547 */ 4548 while ((ring->mr_index != 0) && (ring->mr_next != NULL)) 4549 ring = ring->mr_next; 4550 ASSERT(ring->mr_index == 0); 4551 mip->mi_default_tx_ring = (mac_ring_handle_t)ring; 4552 } 4553 if (mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC) { 4554 mip->mi_txrings_avail = group->mrg_cur_count - 1; 4555 /* 4556 * The default ring cannot be reserved. 4557 */ 4558 mip->mi_txrings_rsvd = 1; 4559 } 4560 /* 4561 * The default group cannot be reserved. It will be shared 4562 * by clients that do not have an exclusive group. 4563 */ 4564 mip->mi_txhwclnt_avail = mip->mi_tx_group_count; 4565 mip->mi_txhwclnt_used = 1; 4566 break; 4567 default: 4568 ASSERT(B_FALSE); 4569 } 4570 4571 if (err != 0) 4572 mac_free_rings(mip, rtype); 4573 4574 return (err); 4575 } 4576 4577 /* 4578 * The ddi interrupt handle could be shared amoung rings. If so, compare 4579 * the new ring's ddi handle with the existing ones and set ddi_shared 4580 * flag. 4581 */ 4582 void 4583 mac_compare_ddi_handle(mac_group_t *groups, uint_t grpcnt, mac_ring_t *cring) 4584 { 4585 mac_group_t *group; 4586 mac_ring_t *ring; 4587 ddi_intr_handle_t ddi_handle; 4588 int g; 4589 4590 ddi_handle = cring->mr_info.mri_intr.mi_ddi_handle; 4591 for (g = 0; g < grpcnt; g++) { 4592 group = groups + g; 4593 for (ring = group->mrg_rings; ring != NULL; 4594 ring = ring->mr_next) { 4595 if (ring == cring) 4596 continue; 4597 if (ring->mr_info.mri_intr.mi_ddi_handle == 4598 ddi_handle) { 4599 if (cring->mr_type == MAC_RING_TYPE_RX && 4600 ring->mr_index == 0 && 4601 !ring->mr_info.mri_intr.mi_ddi_shared) { 4602 ring->mr_info.mri_intr.mi_ddi_shared = 4603 B_TRUE; 4604 } else { 4605 cring->mr_info.mri_intr.mi_ddi_shared = 4606 B_TRUE; 4607 } 4608 return; 4609 } 4610 } 4611 } 4612 } 4613 4614 /* 4615 * Called to free all groups of particular type (RX or TX). It's assumed that 4616 * no clients are using these groups. 4617 */ 4618 void 4619 mac_free_rings(mac_impl_t *mip, mac_ring_type_t rtype) 4620 { 4621 mac_group_t *group, *groups; 4622 uint_t group_count; 4623 4624 switch (rtype) { 4625 case MAC_RING_TYPE_RX: 4626 if (mip->mi_rx_groups == NULL) 4627 return; 4628 4629 groups = mip->mi_rx_groups; 4630 group_count = mip->mi_rx_group_count; 4631 4632 mip->mi_rx_groups = NULL; 4633 mip->mi_rx_donor_grp = NULL; 4634 mip->mi_rx_group_count = 0; 4635 break; 4636 case MAC_RING_TYPE_TX: 4637 ASSERT(mip->mi_tx_group_count == mip->mi_tx_group_free); 4638 4639 if (mip->mi_tx_groups == NULL) 4640 return; 4641 4642 groups = mip->mi_tx_groups; 4643 group_count = mip->mi_tx_group_count; 4644 4645 mip->mi_tx_groups = NULL; 4646 mip->mi_tx_group_count = 0; 4647 mip->mi_tx_group_free = 0; 4648 mip->mi_default_tx_ring = NULL; 4649 break; 4650 default: 4651 ASSERT(B_FALSE); 4652 } 4653 4654 for (group = groups; group != NULL; group = group->mrg_next) { 4655 mac_ring_t *ring; 4656 4657 if (group->mrg_cur_count == 0) 4658 continue; 4659 4660 ASSERT(group->mrg_rings != NULL); 4661 4662 while ((ring = group->mrg_rings) != NULL) { 4663 group->mrg_rings = ring->mr_next; 4664 mac_ring_free(mip, ring); 4665 } 4666 } 4667 4668 /* Free all the cached rings */ 4669 mac_ring_freeall(mip); 4670 /* Free the block of group data strutures */ 4671 kmem_free(groups, sizeof (mac_group_t) * (group_count + 1)); 4672 } 4673 4674 /* 4675 * Associate the VLAN filter to the receive group. 4676 */ 4677 int 4678 mac_group_addvlan(mac_group_t *group, uint16_t vlan) 4679 { 4680 VERIFY3S(group->mrg_type, ==, MAC_RING_TYPE_RX); 4681 VERIFY3P(group->mrg_info.mgi_addvlan, !=, NULL); 4682 4683 if (vlan > VLAN_ID_MAX) 4684 return (EINVAL); 4685 4686 vlan = MAC_VLAN_UNTAGGED_VID(vlan); 4687 return (group->mrg_info.mgi_addvlan(group->mrg_info.mgi_driver, vlan)); 4688 } 4689 4690 /* 4691 * Dissociate the VLAN from the receive group. 4692 */ 4693 int 4694 mac_group_remvlan(mac_group_t *group, uint16_t vlan) 4695 { 4696 VERIFY3S(group->mrg_type, ==, MAC_RING_TYPE_RX); 4697 VERIFY3P(group->mrg_info.mgi_remvlan, !=, NULL); 4698 4699 if (vlan > VLAN_ID_MAX) 4700 return (EINVAL); 4701 4702 vlan = MAC_VLAN_UNTAGGED_VID(vlan); 4703 return (group->mrg_info.mgi_remvlan(group->mrg_info.mgi_driver, vlan)); 4704 } 4705 4706 /* 4707 * Associate a MAC address with a receive group. 4708 * 4709 * The return value of this function should always be checked properly, because 4710 * any type of failure could cause unexpected results. A group can be added 4711 * or removed with a MAC address only after it has been reserved. Ideally, 4712 * a successful reservation always leads to calling mac_group_addmac() to 4713 * steer desired traffic. Failure of adding an unicast MAC address doesn't 4714 * always imply that the group is functioning abnormally. 4715 * 4716 * Currently this function is called everywhere, and it reflects assumptions 4717 * about MAC addresses in the implementation. CR 6735196. 4718 */ 4719 int 4720 mac_group_addmac(mac_group_t *group, const uint8_t *addr) 4721 { 4722 VERIFY3S(group->mrg_type, ==, MAC_RING_TYPE_RX); 4723 VERIFY3P(group->mrg_info.mgi_addmac, !=, NULL); 4724 4725 return (group->mrg_info.mgi_addmac(group->mrg_info.mgi_driver, addr)); 4726 } 4727 4728 /* 4729 * Remove the association between MAC address and receive group. 4730 */ 4731 int 4732 mac_group_remmac(mac_group_t *group, const uint8_t *addr) 4733 { 4734 VERIFY3S(group->mrg_type, ==, MAC_RING_TYPE_RX); 4735 VERIFY3P(group->mrg_info.mgi_remmac, !=, NULL); 4736 4737 return (group->mrg_info.mgi_remmac(group->mrg_info.mgi_driver, addr)); 4738 } 4739 4740 /* 4741 * This is the entry point for packets transmitted through the bridge 4742 * code. If no bridge is in place, mac_ring_tx() transmits via the tx 4743 * ring. The 'rh' pointer may be NULL to select the default ring. 4744 */ 4745 mblk_t * 4746 mac_bridge_tx(mac_impl_t *mip, mac_ring_handle_t rh, mblk_t *mp) 4747 { 4748 mac_handle_t mh; 4749 4750 /* 4751 * Once we take a reference on the bridge link, the bridge 4752 * module itself can't unload, so the callback pointers are 4753 * stable. 4754 */ 4755 mutex_enter(&mip->mi_bridge_lock); 4756 if ((mh = mip->mi_bridge_link) != NULL) 4757 mac_bridge_ref_cb(mh, B_TRUE); 4758 mutex_exit(&mip->mi_bridge_lock); 4759 if (mh == NULL) { 4760 mp = mac_ring_tx((mac_handle_t)mip, rh, mp); 4761 } else { 4762 /* 4763 * The bridge may place this mblk on a provider's Tx 4764 * path, a mac's Rx path, or both. Since we don't have 4765 * enough information at this point, we can't be sure 4766 * that the destination(s) are capable of handling the 4767 * hardware offloads requested by the mblk. We emulate 4768 * them here as it is the safest choice. In the 4769 * future, if bridge performance becomes a priority, 4770 * we can elide the emulation here and leave the 4771 * choice up to bridge. 4772 * 4773 * We don't clear the DB_CKSUMFLAGS here because 4774 * HCK_IPV4_HDRCKSUM (Tx) and HCK_IPV4_HDRCKSUM_OK 4775 * (Rx) still have the same value. If the bridge 4776 * receives a packet from a HCKSUM_IPHDRCKSUM NIC then 4777 * the mac(s) it is forwarded on may calculate the 4778 * checksum again, but incorrectly (because the 4779 * checksum field is not zero). Until the 4780 * HCK_IPV4_HDRCKSUM/HCK_IPV4_HDRCKSUM_OK issue is 4781 * resovled, we leave the flag clearing in bridge 4782 * itself. 4783 */ 4784 if ((DB_CKSUMFLAGS(mp) & (HCK_TX_FLAGS | HW_LSO_FLAGS)) != 0) { 4785 mac_hw_emul(&mp, NULL, NULL, MAC_ALL_EMULS); 4786 } 4787 4788 mp = mac_bridge_tx_cb(mh, rh, mp); 4789 mac_bridge_ref_cb(mh, B_FALSE); 4790 } 4791 4792 return (mp); 4793 } 4794 4795 /* 4796 * Find a ring from its index. 4797 */ 4798 mac_ring_handle_t 4799 mac_find_ring(mac_group_handle_t gh, int index) 4800 { 4801 mac_group_t *group = (mac_group_t *)gh; 4802 mac_ring_t *ring = group->mrg_rings; 4803 4804 for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next) 4805 if (ring->mr_index == index) 4806 break; 4807 4808 return ((mac_ring_handle_t)ring); 4809 } 4810 /* 4811 * Add a ring to an existing group. 4812 * 4813 * The ring must be either passed directly (for example if the ring 4814 * movement is initiated by the framework), or specified through a driver 4815 * index (for example when the ring is added by the driver. 4816 * 4817 * The caller needs to call mac_perim_enter() before calling this function. 4818 */ 4819 int 4820 i_mac_group_add_ring(mac_group_t *group, mac_ring_t *ring, int index) 4821 { 4822 mac_impl_t *mip = (mac_impl_t *)group->mrg_mh; 4823 mac_capab_rings_t *cap_rings; 4824 boolean_t driver_call = (ring == NULL); 4825 mac_group_type_t group_type; 4826 int ret = 0; 4827 flow_entry_t *flent; 4828 4829 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 4830 4831 switch (group->mrg_type) { 4832 case MAC_RING_TYPE_RX: 4833 cap_rings = &mip->mi_rx_rings_cap; 4834 group_type = mip->mi_rx_group_type; 4835 break; 4836 case MAC_RING_TYPE_TX: 4837 cap_rings = &mip->mi_tx_rings_cap; 4838 group_type = mip->mi_tx_group_type; 4839 break; 4840 default: 4841 ASSERT(B_FALSE); 4842 } 4843 4844 /* 4845 * There should be no ring with the same ring index in the target 4846 * group. 4847 */ 4848 ASSERT(mac_find_ring((mac_group_handle_t)group, 4849 driver_call ? index : ring->mr_index) == NULL); 4850 4851 if (driver_call) { 4852 /* 4853 * The function is called as a result of a request from 4854 * a driver to add a ring to an existing group, for example 4855 * from the aggregation driver. Allocate a new mac_ring_t 4856 * for that ring. 4857 */ 4858 ring = mac_init_ring(mip, group, index, cap_rings); 4859 ASSERT(group->mrg_state > MAC_GROUP_STATE_UNINIT); 4860 } else { 4861 /* 4862 * The function is called as a result of a MAC layer request 4863 * to add a ring to an existing group. In this case the 4864 * ring is being moved between groups, which requires 4865 * the underlying driver to support dynamic grouping, 4866 * and the mac_ring_t already exists. 4867 */ 4868 ASSERT(group_type == MAC_GROUP_TYPE_DYNAMIC); 4869 ASSERT(group->mrg_driver == NULL || 4870 cap_rings->mr_gaddring != NULL); 4871 ASSERT(ring->mr_gh == NULL); 4872 } 4873 4874 /* 4875 * At this point the ring should not be in use, and it should be 4876 * of the right for the target group. 4877 */ 4878 ASSERT(ring->mr_state < MR_INUSE); 4879 ASSERT(ring->mr_srs == NULL); 4880 ASSERT(ring->mr_type == group->mrg_type); 4881 4882 if (!driver_call) { 4883 /* 4884 * Add the driver level hardware ring if the process was not 4885 * initiated by the driver, and the target group is not the 4886 * group. 4887 */ 4888 if (group->mrg_driver != NULL) { 4889 cap_rings->mr_gaddring(group->mrg_driver, 4890 ring->mr_driver, ring->mr_type); 4891 } 4892 4893 /* 4894 * Insert the ring ahead existing rings. 4895 */ 4896 ring->mr_next = group->mrg_rings; 4897 group->mrg_rings = ring; 4898 ring->mr_gh = (mac_group_handle_t)group; 4899 group->mrg_cur_count++; 4900 } 4901 4902 /* 4903 * If the group has not been actively used, we're done. 4904 */ 4905 if (group->mrg_index != -1 && 4906 group->mrg_state < MAC_GROUP_STATE_RESERVED) 4907 return (0); 4908 4909 /* 4910 * Start the ring if needed. Failure causes to undo the grouping action. 4911 */ 4912 if (ring->mr_state != MR_INUSE) { 4913 if ((ret = mac_start_ring(ring)) != 0) { 4914 if (!driver_call) { 4915 cap_rings->mr_gremring(group->mrg_driver, 4916 ring->mr_driver, ring->mr_type); 4917 } 4918 group->mrg_cur_count--; 4919 group->mrg_rings = ring->mr_next; 4920 4921 ring->mr_gh = NULL; 4922 4923 if (driver_call) 4924 mac_ring_free(mip, ring); 4925 4926 return (ret); 4927 } 4928 } 4929 4930 /* 4931 * Set up SRS/SR according to the ring type. 4932 */ 4933 switch (ring->mr_type) { 4934 case MAC_RING_TYPE_RX: 4935 /* 4936 * Setup an SRS on top of the new ring if the group is 4937 * reserved for someone's exclusive use. 4938 */ 4939 if (group->mrg_state == MAC_GROUP_STATE_RESERVED) { 4940 mac_client_impl_t *mcip = MAC_GROUP_ONLY_CLIENT(group); 4941 4942 VERIFY3P(mcip, !=, NULL); 4943 flent = mcip->mci_flent; 4944 VERIFY3S(flent->fe_rx_srs_cnt, >, 0); 4945 mac_rx_srs_group_setup(mcip, flent, SRST_LINK); 4946 mac_fanout_setup(mcip, flent, MCIP_RESOURCE_PROPS(mcip), 4947 mac_rx_deliver, mcip, NULL, NULL); 4948 } else { 4949 ring->mr_classify_type = MAC_SW_CLASSIFIER; 4950 } 4951 break; 4952 case MAC_RING_TYPE_TX: 4953 { 4954 mac_grp_client_t *mgcp = group->mrg_clients; 4955 mac_client_impl_t *mcip; 4956 mac_soft_ring_set_t *mac_srs; 4957 mac_srs_tx_t *tx; 4958 4959 if (MAC_GROUP_NO_CLIENT(group)) { 4960 if (ring->mr_state == MR_INUSE) 4961 mac_stop_ring(ring); 4962 ring->mr_flag = 0; 4963 break; 4964 } 4965 /* 4966 * If the rings are being moved to a group that has 4967 * clients using it, then add the new rings to the 4968 * clients SRS. 4969 */ 4970 while (mgcp != NULL) { 4971 boolean_t is_aggr; 4972 4973 mcip = mgcp->mgc_client; 4974 flent = mcip->mci_flent; 4975 is_aggr = (mcip->mci_state_flags & MCIS_IS_AGGR_CLIENT); 4976 mac_srs = MCIP_TX_SRS(mcip); 4977 tx = &mac_srs->srs_tx; 4978 mac_tx_client_quiesce((mac_client_handle_t)mcip); 4979 /* 4980 * If we are growing from 1 to multiple rings. 4981 */ 4982 if (tx->st_mode == SRS_TX_BW || 4983 tx->st_mode == SRS_TX_SERIALIZE || 4984 tx->st_mode == SRS_TX_DEFAULT) { 4985 mac_ring_t *tx_ring = tx->st_arg2; 4986 4987 tx->st_arg2 = NULL; 4988 mac_tx_srs_stat_recreate(mac_srs, B_TRUE); 4989 mac_tx_srs_add_ring(mac_srs, tx_ring); 4990 if (mac_srs->srs_type & SRST_BW_CONTROL) { 4991 tx->st_mode = is_aggr ? SRS_TX_BW_AGGR : 4992 SRS_TX_BW_FANOUT; 4993 } else { 4994 tx->st_mode = is_aggr ? SRS_TX_AGGR : 4995 SRS_TX_FANOUT; 4996 } 4997 tx->st_func = mac_tx_get_func(tx->st_mode); 4998 } 4999 mac_tx_srs_add_ring(mac_srs, ring); 5000 mac_fanout_setup(mcip, flent, MCIP_RESOURCE_PROPS(mcip), 5001 mac_rx_deliver, mcip, NULL, NULL); 5002 mac_tx_client_restart((mac_client_handle_t)mcip); 5003 mgcp = mgcp->mgc_next; 5004 } 5005 break; 5006 } 5007 default: 5008 ASSERT(B_FALSE); 5009 } 5010 /* 5011 * For aggr, the default ring will be NULL to begin with. If it 5012 * is NULL, then pick the first ring that gets added as the 5013 * default ring. Any ring in an aggregation can be removed at 5014 * any time (by the user action of removing a link) and if the 5015 * current default ring gets removed, then a new one gets 5016 * picked (see i_mac_group_rem_ring()). 5017 */ 5018 if (mip->mi_state_flags & MIS_IS_AGGR && 5019 mip->mi_default_tx_ring == NULL && 5020 ring->mr_type == MAC_RING_TYPE_TX) { 5021 mip->mi_default_tx_ring = (mac_ring_handle_t)ring; 5022 } 5023 5024 MAC_RING_UNMARK(ring, MR_INCIPIENT); 5025 return (0); 5026 } 5027 5028 /* 5029 * Remove a ring from it's current group. MAC internal function for dynamic 5030 * grouping. 5031 * 5032 * The caller needs to call mac_perim_enter() before calling this function. 5033 */ 5034 void 5035 i_mac_group_rem_ring(mac_group_t *group, mac_ring_t *ring, 5036 boolean_t driver_call) 5037 { 5038 mac_impl_t *mip = (mac_impl_t *)group->mrg_mh; 5039 mac_capab_rings_t *cap_rings = NULL; 5040 mac_group_type_t group_type; 5041 5042 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 5043 5044 ASSERT(mac_find_ring((mac_group_handle_t)group, 5045 ring->mr_index) == (mac_ring_handle_t)ring); 5046 ASSERT((mac_group_t *)ring->mr_gh == group); 5047 ASSERT(ring->mr_type == group->mrg_type); 5048 5049 if (ring->mr_state == MR_INUSE) 5050 mac_stop_ring(ring); 5051 switch (ring->mr_type) { 5052 case MAC_RING_TYPE_RX: 5053 group_type = mip->mi_rx_group_type; 5054 cap_rings = &mip->mi_rx_rings_cap; 5055 5056 /* 5057 * Only hardware classified packets hold a reference to the 5058 * ring all the way up the Rx path. mac_rx_srs_remove() 5059 * will take care of quiescing the Rx path and removing the 5060 * SRS. The software classified path neither holds a reference 5061 * nor any association with the ring in mac_rx. 5062 */ 5063 if (ring->mr_srs != NULL) { 5064 mac_rx_srs_remove(ring->mr_srs); 5065 ring->mr_srs = NULL; 5066 } 5067 5068 break; 5069 case MAC_RING_TYPE_TX: 5070 { 5071 mac_grp_client_t *mgcp; 5072 mac_client_impl_t *mcip; 5073 mac_soft_ring_set_t *mac_srs; 5074 mac_srs_tx_t *tx; 5075 mac_ring_t *rem_ring; 5076 mac_group_t *defgrp; 5077 uint_t ring_info = 0; 5078 5079 /* 5080 * For TX this function is invoked in three 5081 * cases: 5082 * 5083 * 1) In the case of a failure during the 5084 * initial creation of a group when a share is 5085 * associated with a MAC client. So the SRS is not 5086 * yet setup, and will be setup later after the 5087 * group has been reserved and populated. 5088 * 5089 * 2) From mac_release_tx_group() when freeing 5090 * a TX SRS. 5091 * 5092 * 3) In the case of aggr, when a port gets removed, 5093 * the pseudo Tx rings that it exposed gets removed. 5094 * 5095 * In the first two cases the SRS and its soft 5096 * rings are already quiesced. 5097 */ 5098 if (driver_call) { 5099 mac_client_impl_t *mcip; 5100 mac_soft_ring_set_t *mac_srs; 5101 mac_soft_ring_t *sringp; 5102 mac_srs_tx_t *srs_tx; 5103 5104 if (mip->mi_state_flags & MIS_IS_AGGR && 5105 mip->mi_default_tx_ring == 5106 (mac_ring_handle_t)ring) { 5107 /* pick a new default Tx ring */ 5108 mip->mi_default_tx_ring = 5109 (group->mrg_rings != ring) ? 5110 (mac_ring_handle_t)group->mrg_rings : 5111 (mac_ring_handle_t)(ring->mr_next); 5112 } 5113 /* Presently only aggr case comes here */ 5114 if (group->mrg_state != MAC_GROUP_STATE_RESERVED) 5115 break; 5116 5117 mcip = MAC_GROUP_ONLY_CLIENT(group); 5118 ASSERT(mcip != NULL); 5119 ASSERT(mcip->mci_state_flags & MCIS_IS_AGGR_CLIENT); 5120 mac_srs = MCIP_TX_SRS(mcip); 5121 ASSERT(mac_srs->srs_tx.st_mode == SRS_TX_AGGR || 5122 mac_srs->srs_tx.st_mode == SRS_TX_BW_AGGR); 5123 srs_tx = &mac_srs->srs_tx; 5124 /* 5125 * Wakeup any callers blocked on this 5126 * Tx ring due to flow control. 5127 */ 5128 sringp = srs_tx->st_soft_rings[ring->mr_index]; 5129 ASSERT(sringp != NULL); 5130 mac_tx_invoke_callbacks(mcip, (mac_tx_cookie_t)sringp); 5131 mac_tx_client_quiesce((mac_client_handle_t)mcip); 5132 mac_tx_srs_del_ring(mac_srs, ring); 5133 mac_tx_client_restart((mac_client_handle_t)mcip); 5134 break; 5135 } 5136 ASSERT(ring != (mac_ring_t *)mip->mi_default_tx_ring); 5137 group_type = mip->mi_tx_group_type; 5138 cap_rings = &mip->mi_tx_rings_cap; 5139 /* 5140 * See if we need to take it out of the MAC clients using 5141 * this group 5142 */ 5143 if (MAC_GROUP_NO_CLIENT(group)) 5144 break; 5145 mgcp = group->mrg_clients; 5146 defgrp = MAC_DEFAULT_TX_GROUP(mip); 5147 while (mgcp != NULL) { 5148 mcip = mgcp->mgc_client; 5149 mac_srs = MCIP_TX_SRS(mcip); 5150 tx = &mac_srs->srs_tx; 5151 mac_tx_client_quiesce((mac_client_handle_t)mcip); 5152 /* 5153 * If we are here when removing rings from the 5154 * defgroup, mac_reserve_tx_ring would have 5155 * already deleted the ring from the MAC 5156 * clients in the group. 5157 */ 5158 if (group != defgrp) { 5159 mac_tx_invoke_callbacks(mcip, 5160 (mac_tx_cookie_t) 5161 mac_tx_srs_get_soft_ring(mac_srs, ring)); 5162 mac_tx_srs_del_ring(mac_srs, ring); 5163 } 5164 /* 5165 * Additionally, if we are left with only 5166 * one ring in the group after this, we need 5167 * to modify the mode etc. to. (We haven't 5168 * yet taken the ring out, so we check with 2). 5169 */ 5170 if (group->mrg_cur_count == 2) { 5171 if (ring->mr_next == NULL) 5172 rem_ring = group->mrg_rings; 5173 else 5174 rem_ring = ring->mr_next; 5175 mac_tx_invoke_callbacks(mcip, 5176 (mac_tx_cookie_t) 5177 mac_tx_srs_get_soft_ring(mac_srs, 5178 rem_ring)); 5179 mac_tx_srs_del_ring(mac_srs, rem_ring); 5180 if (rem_ring->mr_state != MR_INUSE) { 5181 (void) mac_start_ring(rem_ring); 5182 } 5183 tx->st_arg2 = (void *)rem_ring; 5184 mac_tx_srs_stat_recreate(mac_srs, B_FALSE); 5185 ring_info = mac_hwring_getinfo( 5186 (mac_ring_handle_t)rem_ring); 5187 /* 5188 * We are shrinking from multiple 5189 * to 1 ring. 5190 */ 5191 if (mac_srs->srs_type & SRST_BW_CONTROL) { 5192 tx->st_mode = SRS_TX_BW; 5193 } else if (mac_tx_serialize || 5194 (ring_info & MAC_RING_TX_SERIALIZE)) { 5195 tx->st_mode = SRS_TX_SERIALIZE; 5196 } else { 5197 tx->st_mode = SRS_TX_DEFAULT; 5198 } 5199 tx->st_func = mac_tx_get_func(tx->st_mode); 5200 } 5201 mac_tx_client_restart((mac_client_handle_t)mcip); 5202 mgcp = mgcp->mgc_next; 5203 } 5204 break; 5205 } 5206 default: 5207 ASSERT(B_FALSE); 5208 } 5209 5210 /* 5211 * Remove the ring from the group. 5212 */ 5213 if (ring == group->mrg_rings) 5214 group->mrg_rings = ring->mr_next; 5215 else { 5216 mac_ring_t *pre; 5217 5218 pre = group->mrg_rings; 5219 while (pre->mr_next != ring) 5220 pre = pre->mr_next; 5221 pre->mr_next = ring->mr_next; 5222 } 5223 group->mrg_cur_count--; 5224 5225 if (!driver_call) { 5226 ASSERT(group_type == MAC_GROUP_TYPE_DYNAMIC); 5227 ASSERT(group->mrg_driver == NULL || 5228 cap_rings->mr_gremring != NULL); 5229 5230 /* 5231 * Remove the driver level hardware ring. 5232 */ 5233 if (group->mrg_driver != NULL) { 5234 cap_rings->mr_gremring(group->mrg_driver, 5235 ring->mr_driver, ring->mr_type); 5236 } 5237 } 5238 5239 ring->mr_gh = NULL; 5240 if (driver_call) 5241 mac_ring_free(mip, ring); 5242 else 5243 ring->mr_flag = 0; 5244 } 5245 5246 /* 5247 * Move a ring to the target group. If needed, remove the ring from the group 5248 * that it currently belongs to. 5249 * 5250 * The caller need to enter MAC's perimeter by calling mac_perim_enter(). 5251 */ 5252 static int 5253 mac_group_mov_ring(mac_impl_t *mip, mac_group_t *d_group, mac_ring_t *ring) 5254 { 5255 mac_group_t *s_group = (mac_group_t *)ring->mr_gh; 5256 int rv; 5257 5258 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 5259 ASSERT(d_group != NULL); 5260 ASSERT(s_group == NULL || s_group->mrg_mh == d_group->mrg_mh); 5261 5262 if (s_group == d_group) 5263 return (0); 5264 5265 /* 5266 * Remove it from current group first. 5267 */ 5268 if (s_group != NULL) 5269 i_mac_group_rem_ring(s_group, ring, B_FALSE); 5270 5271 /* 5272 * Add it to the new group. 5273 */ 5274 rv = i_mac_group_add_ring(d_group, ring, 0); 5275 if (rv != 0) { 5276 /* 5277 * Failed to add ring back to source group. If 5278 * that fails, the ring is stuck in limbo, log message. 5279 */ 5280 if (i_mac_group_add_ring(s_group, ring, 0)) { 5281 cmn_err(CE_WARN, "%s: failed to move ring %p\n", 5282 mip->mi_name, (void *)ring); 5283 } 5284 } 5285 5286 return (rv); 5287 } 5288 5289 /* 5290 * Find a MAC address according to its value. 5291 */ 5292 mac_address_t * 5293 mac_find_macaddr(mac_impl_t *mip, uint8_t *mac_addr) 5294 { 5295 mac_address_t *map; 5296 5297 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 5298 5299 for (map = mip->mi_addresses; map != NULL; map = map->ma_next) { 5300 if (bcmp(mac_addr, map->ma_addr, map->ma_len) == 0) 5301 break; 5302 } 5303 5304 return (map); 5305 } 5306 5307 /* 5308 * Check whether the MAC address is shared by multiple clients. 5309 */ 5310 boolean_t 5311 mac_check_macaddr_shared(mac_address_t *map) 5312 { 5313 ASSERT(MAC_PERIM_HELD((mac_handle_t)map->ma_mip)); 5314 5315 return (map->ma_nusers > 1); 5316 } 5317 5318 /* 5319 * Remove the specified MAC address from the MAC address list and free it. 5320 */ 5321 static void 5322 mac_free_macaddr(mac_address_t *map) 5323 { 5324 mac_impl_t *mip = map->ma_mip; 5325 5326 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 5327 VERIFY3P(mip->mi_addresses, !=, NULL); 5328 5329 VERIFY3P(map, ==, mac_find_macaddr(mip, map->ma_addr)); 5330 VERIFY3P(map, !=, NULL); 5331 VERIFY3S(map->ma_nusers, ==, 0); 5332 VERIFY3P(map->ma_vlans, ==, NULL); 5333 5334 if (map == mip->mi_addresses) { 5335 mip->mi_addresses = map->ma_next; 5336 } else { 5337 mac_address_t *pre; 5338 5339 pre = mip->mi_addresses; 5340 while (pre->ma_next != map) 5341 pre = pre->ma_next; 5342 pre->ma_next = map->ma_next; 5343 } 5344 5345 kmem_free(map, sizeof (mac_address_t)); 5346 } 5347 5348 static mac_vlan_t * 5349 mac_find_vlan(mac_address_t *map, uint16_t vid) 5350 { 5351 mac_vlan_t *mvp; 5352 5353 for (mvp = map->ma_vlans; mvp != NULL; mvp = mvp->mv_next) { 5354 if (mvp->mv_vid == vid) 5355 return (mvp); 5356 } 5357 5358 return (NULL); 5359 } 5360 5361 static mac_vlan_t * 5362 mac_add_vlan(mac_address_t *map, uint16_t vid) 5363 { 5364 mac_vlan_t *mvp; 5365 5366 /* 5367 * We should never add the same {addr, VID} tuple more 5368 * than once, but let's be sure. 5369 */ 5370 for (mvp = map->ma_vlans; mvp != NULL; mvp = mvp->mv_next) 5371 VERIFY3U(mvp->mv_vid, !=, vid); 5372 5373 /* Add the VLAN to the head of the VLAN list. */ 5374 mvp = kmem_zalloc(sizeof (mac_vlan_t), KM_SLEEP); 5375 mvp->mv_vid = vid; 5376 mvp->mv_next = map->ma_vlans; 5377 map->ma_vlans = mvp; 5378 5379 return (mvp); 5380 } 5381 5382 static void 5383 mac_rem_vlan(mac_address_t *map, mac_vlan_t *mvp) 5384 { 5385 mac_vlan_t *pre; 5386 5387 if (map->ma_vlans == mvp) { 5388 map->ma_vlans = mvp->mv_next; 5389 } else { 5390 pre = map->ma_vlans; 5391 while (pre->mv_next != mvp) { 5392 pre = pre->mv_next; 5393 5394 /* 5395 * We've reached the end of the list without 5396 * finding mvp. 5397 */ 5398 VERIFY3P(pre, !=, NULL); 5399 } 5400 pre->mv_next = mvp->mv_next; 5401 } 5402 5403 kmem_free(mvp, sizeof (mac_vlan_t)); 5404 } 5405 5406 /* 5407 * Create a new mac_address_t if this is the first use of the address 5408 * or add a VID to an existing address. In either case, the 5409 * mac_address_t acts as a list of {addr, VID} tuples where each tuple 5410 * shares the same addr. If group is non-NULL then attempt to program 5411 * the MAC's HW filters for this group. Otherwise, if group is NULL, 5412 * then the MAC has no rings and there is nothing to program. 5413 */ 5414 int 5415 mac_add_macaddr_vlan(mac_impl_t *mip, mac_group_t *group, uint8_t *addr, 5416 uint16_t vid, boolean_t use_hw) 5417 { 5418 mac_address_t *map; 5419 mac_vlan_t *mvp; 5420 int err = 0; 5421 boolean_t allocated_map = B_FALSE; 5422 boolean_t hw_mac = B_FALSE; 5423 boolean_t hw_vlan = B_FALSE; 5424 5425 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 5426 5427 map = mac_find_macaddr(mip, addr); 5428 5429 /* 5430 * If this is the first use of this MAC address then allocate 5431 * and initialize a new structure. 5432 */ 5433 if (map == NULL) { 5434 map = kmem_zalloc(sizeof (mac_address_t), KM_SLEEP); 5435 map->ma_len = mip->mi_type->mt_addr_length; 5436 bcopy(addr, map->ma_addr, map->ma_len); 5437 map->ma_nusers = 0; 5438 map->ma_group = group; 5439 map->ma_mip = mip; 5440 map->ma_untagged = B_FALSE; 5441 5442 /* Add the new MAC address to the head of the address list. */ 5443 map->ma_next = mip->mi_addresses; 5444 mip->mi_addresses = map; 5445 5446 allocated_map = B_TRUE; 5447 } 5448 5449 VERIFY(map->ma_group == NULL || map->ma_group == group); 5450 if (map->ma_group == NULL) 5451 map->ma_group = group; 5452 5453 if (vid == VLAN_ID_NONE) { 5454 map->ma_untagged = B_TRUE; 5455 mvp = NULL; 5456 } else { 5457 mvp = mac_add_vlan(map, vid); 5458 } 5459 5460 /* 5461 * Set the VLAN HW filter if: 5462 * 5463 * o the MAC's VLAN HW filtering is enabled, and 5464 * o the address does not currently rely on promisc mode. 5465 * 5466 * This is called even when the client specifies an untagged 5467 * address (VLAN_ID_NONE) because some MAC providers require 5468 * setting additional bits to accept untagged traffic when 5469 * VLAN HW filtering is enabled. 5470 */ 5471 if (MAC_GROUP_HW_VLAN(group) && 5472 map->ma_type != MAC_ADDRESS_TYPE_UNICAST_PROMISC) { 5473 if ((err = mac_group_addvlan(group, vid)) != 0) 5474 goto bail; 5475 5476 hw_vlan = B_TRUE; 5477 } 5478 5479 VERIFY3S(map->ma_nusers, >=, 0); 5480 map->ma_nusers++; 5481 5482 /* 5483 * If this MAC address already has a HW filter then simply 5484 * increment the counter. 5485 */ 5486 if (map->ma_nusers > 1) 5487 return (0); 5488 5489 /* 5490 * All logic from here on out is executed during initial 5491 * creation only. 5492 */ 5493 VERIFY3S(map->ma_nusers, ==, 1); 5494 5495 /* 5496 * Activate this MAC address by adding it to the reserved group. 5497 */ 5498 if (group != NULL) { 5499 err = mac_group_addmac(group, (const uint8_t *)addr); 5500 5501 /* 5502 * If the driver is out of filters then we can 5503 * continue and use promisc mode. For any other error, 5504 * assume the driver is in a state where we can't 5505 * program the filters or use promisc mode; so we must 5506 * bail. 5507 */ 5508 if (err != 0 && err != ENOSPC) { 5509 map->ma_nusers--; 5510 goto bail; 5511 } 5512 5513 hw_mac = (err == 0); 5514 } 5515 5516 if (hw_mac) { 5517 map->ma_type = MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED; 5518 return (0); 5519 } 5520 5521 /* 5522 * The MAC address addition failed. If the client requires a 5523 * hardware classified MAC address, fail the operation. This 5524 * feature is only used by sun4v vsw. 5525 */ 5526 if (use_hw && !hw_mac) { 5527 err = ENOSPC; 5528 map->ma_nusers--; 5529 goto bail; 5530 } 5531 5532 /* 5533 * If we reach this point then either the MAC doesn't have 5534 * RINGS capability or we are out of MAC address HW filters. 5535 * In any case we must put the MAC into promiscuous mode. 5536 */ 5537 VERIFY(group == NULL || !hw_mac); 5538 5539 /* 5540 * The one exception is the primary address. A non-RINGS 5541 * driver filters the primary address by default; promisc mode 5542 * is not needed. 5543 */ 5544 if ((group == NULL) && 5545 (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) == 0)) { 5546 map->ma_type = MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED; 5547 return (0); 5548 } 5549 5550 /* 5551 * Enable promiscuous mode in order to receive traffic to the 5552 * new MAC address. All existing HW filters still send their 5553 * traffic to their respective group/SRSes. But with promisc 5554 * enabled all unknown traffic is delivered to the default 5555 * group where it is SW classified via mac_rx_classify(). 5556 */ 5557 if ((err = i_mac_promisc_set(mip, B_TRUE)) == 0) { 5558 map->ma_type = MAC_ADDRESS_TYPE_UNICAST_PROMISC; 5559 return (0); 5560 } 5561 5562 /* 5563 * We failed to set promisc mode and we are about to free 'map'. 5564 */ 5565 map->ma_nusers = 0; 5566 5567 bail: 5568 if (hw_vlan) { 5569 int err2 = mac_group_remvlan(group, vid); 5570 5571 if (err2 != 0) { 5572 cmn_err(CE_WARN, "Failed to remove VLAN %u from group" 5573 " %d on MAC %s: %d.", vid, group->mrg_index, 5574 mip->mi_name, err2); 5575 } 5576 } 5577 5578 if (mvp != NULL) 5579 mac_rem_vlan(map, mvp); 5580 5581 if (allocated_map) 5582 mac_free_macaddr(map); 5583 5584 return (err); 5585 } 5586 5587 int 5588 mac_remove_macaddr_vlan(mac_address_t *map, uint16_t vid) 5589 { 5590 mac_vlan_t *mvp; 5591 mac_impl_t *mip = map->ma_mip; 5592 mac_group_t *group = map->ma_group; 5593 int err = 0; 5594 5595 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 5596 VERIFY3P(map, ==, mac_find_macaddr(mip, map->ma_addr)); 5597 5598 if (vid == VLAN_ID_NONE) { 5599 map->ma_untagged = B_FALSE; 5600 mvp = NULL; 5601 } else { 5602 mvp = mac_find_vlan(map, vid); 5603 VERIFY3P(mvp, !=, NULL); 5604 } 5605 5606 if (MAC_GROUP_HW_VLAN(group) && 5607 map->ma_type == MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED && 5608 ((err = mac_group_remvlan(group, vid)) != 0)) 5609 return (err); 5610 5611 if (mvp != NULL) 5612 mac_rem_vlan(map, mvp); 5613 5614 /* 5615 * If it's not the last client using this MAC address, only update 5616 * the MAC clients count. 5617 */ 5618 map->ma_nusers--; 5619 if (map->ma_nusers > 0) 5620 return (0); 5621 5622 VERIFY3S(map->ma_nusers, ==, 0); 5623 5624 /* 5625 * The MAC address is no longer used by any MAC client, so 5626 * remove it from its associated group. Turn off promiscuous 5627 * mode if this is the last address relying on it. 5628 */ 5629 switch (map->ma_type) { 5630 case MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED: 5631 /* 5632 * Don't free the preset primary address for drivers that 5633 * don't advertise RINGS capability. 5634 */ 5635 if (group == NULL) 5636 return (0); 5637 5638 if ((err = mac_group_remmac(group, map->ma_addr)) != 0) { 5639 if (vid == VLAN_ID_NONE) 5640 map->ma_untagged = B_TRUE; 5641 else 5642 (void) mac_add_vlan(map, vid); 5643 5644 /* 5645 * If we fail to remove the MAC address HW 5646 * filter but then also fail to re-add the 5647 * VLAN HW filter then we are in a busted 5648 * state. We do our best by logging a warning 5649 * and returning the original 'err' that got 5650 * us here. At this point, traffic for this 5651 * address + VLAN combination will be dropped 5652 * until the user reboots the system. In the 5653 * future, it would be nice to have a system 5654 * that can compare the state of expected 5655 * classification according to mac to the 5656 * actual state of the provider, and report 5657 * and fix any inconsistencies. 5658 */ 5659 if (MAC_GROUP_HW_VLAN(group)) { 5660 int err2; 5661 5662 err2 = mac_group_addvlan(group, vid); 5663 if (err2 != 0) { 5664 cmn_err(CE_WARN, "Failed to readd VLAN" 5665 " %u to group %d on MAC %s: %d.", 5666 vid, group->mrg_index, mip->mi_name, 5667 err2); 5668 } 5669 } 5670 5671 map->ma_nusers = 1; 5672 return (err); 5673 } 5674 5675 map->ma_group = NULL; 5676 break; 5677 case MAC_ADDRESS_TYPE_UNICAST_PROMISC: 5678 err = i_mac_promisc_set(mip, B_FALSE); 5679 break; 5680 default: 5681 panic("Unexpected ma_type 0x%x, file: %s, line %d", 5682 map->ma_type, __FILE__, __LINE__); 5683 } 5684 5685 if (err != 0) { 5686 map->ma_nusers = 1; 5687 return (err); 5688 } 5689 5690 /* 5691 * We created MAC address for the primary one at registration, so we 5692 * won't free it here. mac_fini_macaddr() will take care of it. 5693 */ 5694 if (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) != 0) 5695 mac_free_macaddr(map); 5696 5697 return (0); 5698 } 5699 5700 /* 5701 * Update an existing MAC address. The caller need to make sure that the new 5702 * value has not been used. 5703 */ 5704 int 5705 mac_update_macaddr(mac_address_t *map, uint8_t *mac_addr) 5706 { 5707 mac_impl_t *mip = map->ma_mip; 5708 int err = 0; 5709 5710 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 5711 ASSERT(mac_find_macaddr(mip, mac_addr) == NULL); 5712 5713 switch (map->ma_type) { 5714 case MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED: 5715 /* 5716 * Update the primary address for drivers that are not 5717 * RINGS capable. 5718 */ 5719 if (mip->mi_rx_groups == NULL) { 5720 err = mip->mi_unicst(mip->mi_driver, (const uint8_t *) 5721 mac_addr); 5722 if (err != 0) 5723 return (err); 5724 break; 5725 } 5726 5727 /* 5728 * If this MAC address is not currently in use, 5729 * simply break out and update the value. 5730 */ 5731 if (map->ma_nusers == 0) 5732 break; 5733 5734 /* 5735 * Need to replace the MAC address associated with a group. 5736 */ 5737 err = mac_group_remmac(map->ma_group, map->ma_addr); 5738 if (err != 0) 5739 return (err); 5740 5741 err = mac_group_addmac(map->ma_group, mac_addr); 5742 5743 /* 5744 * Failure hints hardware error. The MAC layer needs to 5745 * have error notification facility to handle this. 5746 * Now, simply try to restore the value. 5747 */ 5748 if (err != 0) 5749 (void) mac_group_addmac(map->ma_group, map->ma_addr); 5750 5751 break; 5752 case MAC_ADDRESS_TYPE_UNICAST_PROMISC: 5753 /* 5754 * Need to do nothing more if in promiscuous mode. 5755 */ 5756 break; 5757 default: 5758 ASSERT(B_FALSE); 5759 } 5760 5761 /* 5762 * Successfully replaced the MAC address. 5763 */ 5764 if (err == 0) 5765 bcopy(mac_addr, map->ma_addr, map->ma_len); 5766 5767 return (err); 5768 } 5769 5770 /* 5771 * Freshen the MAC address with new value. Its caller must have updated the 5772 * hardware MAC address before calling this function. 5773 * This funcitons is supposed to be used to handle the MAC address change 5774 * notification from underlying drivers. 5775 */ 5776 void 5777 mac_freshen_macaddr(mac_address_t *map, uint8_t *mac_addr) 5778 { 5779 mac_impl_t *mip = map->ma_mip; 5780 5781 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 5782 ASSERT(mac_find_macaddr(mip, mac_addr) == NULL); 5783 5784 /* 5785 * Freshen the MAC address with new value. 5786 */ 5787 bcopy(mac_addr, map->ma_addr, map->ma_len); 5788 bcopy(mac_addr, mip->mi_addr, map->ma_len); 5789 5790 /* 5791 * Update all MAC clients that share this MAC address. 5792 */ 5793 mac_unicast_update_clients(mip, map); 5794 } 5795 5796 /* 5797 * Set up the primary MAC address. 5798 */ 5799 void 5800 mac_init_macaddr(mac_impl_t *mip) 5801 { 5802 mac_address_t *map; 5803 5804 /* 5805 * The reference count is initialized to zero, until it's really 5806 * activated. 5807 */ 5808 map = kmem_zalloc(sizeof (mac_address_t), KM_SLEEP); 5809 map->ma_len = mip->mi_type->mt_addr_length; 5810 bcopy(mip->mi_addr, map->ma_addr, map->ma_len); 5811 5812 /* 5813 * If driver advertises RINGS capability, it shouldn't have initialized 5814 * its primary MAC address. For other drivers, including VNIC, the 5815 * primary address must work after registration. 5816 */ 5817 if (mip->mi_rx_groups == NULL) 5818 map->ma_type = MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED; 5819 5820 map->ma_mip = mip; 5821 5822 mip->mi_addresses = map; 5823 } 5824 5825 /* 5826 * Clean up the primary MAC address. Note, only one primary MAC address 5827 * is allowed. All other MAC addresses must have been freed appropriately. 5828 */ 5829 void 5830 mac_fini_macaddr(mac_impl_t *mip) 5831 { 5832 mac_address_t *map = mip->mi_addresses; 5833 5834 if (map == NULL) 5835 return; 5836 5837 /* 5838 * If mi_addresses is initialized, there should be exactly one 5839 * entry left on the list with no users. 5840 */ 5841 VERIFY3S(map->ma_nusers, ==, 0); 5842 VERIFY3P(map->ma_next, ==, NULL); 5843 VERIFY3P(map->ma_vlans, ==, NULL); 5844 5845 kmem_free(map, sizeof (mac_address_t)); 5846 mip->mi_addresses = NULL; 5847 } 5848 5849 /* 5850 * Logging related functions. 5851 * 5852 * Note that Kernel statistics have been extended to maintain fine 5853 * granularity of statistics viz. hardware lane, software lane, fanout 5854 * stats etc. However, extended accounting continues to support only 5855 * aggregate statistics like before. 5856 */ 5857 5858 /* Write the flow description to a netinfo_t record */ 5859 static netinfo_t * 5860 mac_write_flow_desc(flow_entry_t *flent, mac_client_impl_t *mcip) 5861 { 5862 netinfo_t *ninfo; 5863 net_desc_t *ndesc; 5864 flow_desc_t *fdesc; 5865 mac_resource_props_t *mrp; 5866 5867 ninfo = kmem_zalloc(sizeof (netinfo_t), KM_NOSLEEP); 5868 if (ninfo == NULL) 5869 return (NULL); 5870 ndesc = kmem_zalloc(sizeof (net_desc_t), KM_NOSLEEP); 5871 if (ndesc == NULL) { 5872 kmem_free(ninfo, sizeof (netinfo_t)); 5873 return (NULL); 5874 } 5875 5876 /* 5877 * Grab the fe_lock to see a self-consistent fe_flow_desc. 5878 * Updates to the fe_flow_desc are done under the fe_lock 5879 */ 5880 mutex_enter(&flent->fe_lock); 5881 fdesc = &flent->fe_flow_desc; 5882 mrp = &flent->fe_resource_props; 5883 5884 ndesc->nd_name = flent->fe_flow_name; 5885 ndesc->nd_devname = mcip->mci_name; 5886 bcopy(fdesc->fd_src_mac, ndesc->nd_ehost, ETHERADDRL); 5887 bcopy(fdesc->fd_dst_mac, ndesc->nd_edest, ETHERADDRL); 5888 ndesc->nd_sap = htonl(fdesc->fd_sap); 5889 ndesc->nd_isv4 = (uint8_t)fdesc->fd_ipversion == IPV4_VERSION; 5890 ndesc->nd_bw_limit = mrp->mrp_maxbw; 5891 if (ndesc->nd_isv4) { 5892 ndesc->nd_saddr[3] = htonl(fdesc->fd_local_addr.s6_addr32[3]); 5893 ndesc->nd_daddr[3] = htonl(fdesc->fd_remote_addr.s6_addr32[3]); 5894 } else { 5895 bcopy(&fdesc->fd_local_addr, ndesc->nd_saddr, IPV6_ADDR_LEN); 5896 bcopy(&fdesc->fd_remote_addr, ndesc->nd_daddr, IPV6_ADDR_LEN); 5897 } 5898 ndesc->nd_sport = htons(fdesc->fd_local_port); 5899 ndesc->nd_dport = htons(fdesc->fd_remote_port); 5900 ndesc->nd_protocol = (uint8_t)fdesc->fd_protocol; 5901 mutex_exit(&flent->fe_lock); 5902 5903 ninfo->ni_record = ndesc; 5904 ninfo->ni_size = sizeof (net_desc_t); 5905 ninfo->ni_type = EX_NET_FLDESC_REC; 5906 5907 return (ninfo); 5908 } 5909 5910 /* Write the flow statistics to a netinfo_t record */ 5911 static netinfo_t * 5912 mac_write_flow_stats(flow_entry_t *flent) 5913 { 5914 netinfo_t *ninfo; 5915 net_stat_t *nstat; 5916 mac_soft_ring_set_t *mac_srs; 5917 mac_rx_stats_t *mac_rx_stat; 5918 mac_tx_stats_t *mac_tx_stat; 5919 int i; 5920 5921 ninfo = kmem_zalloc(sizeof (netinfo_t), KM_NOSLEEP); 5922 if (ninfo == NULL) 5923 return (NULL); 5924 nstat = kmem_zalloc(sizeof (net_stat_t), KM_NOSLEEP); 5925 if (nstat == NULL) { 5926 kmem_free(ninfo, sizeof (netinfo_t)); 5927 return (NULL); 5928 } 5929 5930 nstat->ns_name = flent->fe_flow_name; 5931 for (i = 0; i < flent->fe_rx_srs_cnt; i++) { 5932 mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i]; 5933 mac_rx_stat = &mac_srs->srs_rx.sr_stat; 5934 5935 nstat->ns_ibytes += mac_rx_stat->mrs_intrbytes + 5936 mac_rx_stat->mrs_pollbytes + mac_rx_stat->mrs_lclbytes; 5937 nstat->ns_ipackets += mac_rx_stat->mrs_intrcnt + 5938 mac_rx_stat->mrs_pollcnt + mac_rx_stat->mrs_lclcnt; 5939 nstat->ns_oerrors += mac_rx_stat->mrs_ierrors; 5940 } 5941 5942 mac_srs = (mac_soft_ring_set_t *)(flent->fe_tx_srs); 5943 if (mac_srs != NULL) { 5944 mac_tx_stat = &mac_srs->srs_tx.st_stat; 5945 5946 nstat->ns_obytes = mac_tx_stat->mts_obytes; 5947 nstat->ns_opackets = mac_tx_stat->mts_opackets; 5948 nstat->ns_oerrors = mac_tx_stat->mts_oerrors; 5949 } 5950 5951 ninfo->ni_record = nstat; 5952 ninfo->ni_size = sizeof (net_stat_t); 5953 ninfo->ni_type = EX_NET_FLSTAT_REC; 5954 5955 return (ninfo); 5956 } 5957 5958 /* Write the link description to a netinfo_t record */ 5959 static netinfo_t * 5960 mac_write_link_desc(mac_client_impl_t *mcip) 5961 { 5962 netinfo_t *ninfo; 5963 net_desc_t *ndesc; 5964 flow_entry_t *flent = mcip->mci_flent; 5965 5966 ninfo = kmem_zalloc(sizeof (netinfo_t), KM_NOSLEEP); 5967 if (ninfo == NULL) 5968 return (NULL); 5969 ndesc = kmem_zalloc(sizeof (net_desc_t), KM_NOSLEEP); 5970 if (ndesc == NULL) { 5971 kmem_free(ninfo, sizeof (netinfo_t)); 5972 return (NULL); 5973 } 5974 5975 ndesc->nd_name = mcip->mci_name; 5976 ndesc->nd_devname = mcip->mci_name; 5977 ndesc->nd_isv4 = B_TRUE; 5978 /* 5979 * Grab the fe_lock to see a self-consistent fe_flow_desc. 5980 * Updates to the fe_flow_desc are done under the fe_lock 5981 * after removing the flent from the flow table. 5982 */ 5983 mutex_enter(&flent->fe_lock); 5984 bcopy(flent->fe_flow_desc.fd_src_mac, ndesc->nd_ehost, ETHERADDRL); 5985 mutex_exit(&flent->fe_lock); 5986 5987 ninfo->ni_record = ndesc; 5988 ninfo->ni_size = sizeof (net_desc_t); 5989 ninfo->ni_type = EX_NET_LNDESC_REC; 5990 5991 return (ninfo); 5992 } 5993 5994 /* Write the link statistics to a netinfo_t record */ 5995 static netinfo_t * 5996 mac_write_link_stats(mac_client_impl_t *mcip) 5997 { 5998 netinfo_t *ninfo; 5999 net_stat_t *nstat; 6000 flow_entry_t *flent; 6001 mac_soft_ring_set_t *mac_srs; 6002 mac_rx_stats_t *mac_rx_stat; 6003 mac_tx_stats_t *mac_tx_stat; 6004 int i; 6005 6006 ninfo = kmem_zalloc(sizeof (netinfo_t), KM_NOSLEEP); 6007 if (ninfo == NULL) 6008 return (NULL); 6009 nstat = kmem_zalloc(sizeof (net_stat_t), KM_NOSLEEP); 6010 if (nstat == NULL) { 6011 kmem_free(ninfo, sizeof (netinfo_t)); 6012 return (NULL); 6013 } 6014 6015 nstat->ns_name = mcip->mci_name; 6016 flent = mcip->mci_flent; 6017 if (flent != NULL) { 6018 for (i = 0; i < flent->fe_rx_srs_cnt; i++) { 6019 mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i]; 6020 mac_rx_stat = &mac_srs->srs_rx.sr_stat; 6021 6022 nstat->ns_ibytes += mac_rx_stat->mrs_intrbytes + 6023 mac_rx_stat->mrs_pollbytes + 6024 mac_rx_stat->mrs_lclbytes; 6025 nstat->ns_ipackets += mac_rx_stat->mrs_intrcnt + 6026 mac_rx_stat->mrs_pollcnt + mac_rx_stat->mrs_lclcnt; 6027 nstat->ns_oerrors += mac_rx_stat->mrs_ierrors; 6028 } 6029 } 6030 6031 mac_srs = (mac_soft_ring_set_t *)(mcip->mci_flent->fe_tx_srs); 6032 if (mac_srs != NULL) { 6033 mac_tx_stat = &mac_srs->srs_tx.st_stat; 6034 6035 nstat->ns_obytes = mac_tx_stat->mts_obytes; 6036 nstat->ns_opackets = mac_tx_stat->mts_opackets; 6037 nstat->ns_oerrors = mac_tx_stat->mts_oerrors; 6038 } 6039 6040 ninfo->ni_record = nstat; 6041 ninfo->ni_size = sizeof (net_stat_t); 6042 ninfo->ni_type = EX_NET_LNSTAT_REC; 6043 6044 return (ninfo); 6045 } 6046 6047 typedef struct i_mac_log_state_s { 6048 boolean_t mi_last; 6049 int mi_fenable; 6050 int mi_lenable; 6051 list_t *mi_list; 6052 } i_mac_log_state_t; 6053 6054 /* 6055 * For a given flow, if the description has not been logged before, do it now. 6056 * If it is a VNIC, then we have collected information about it from the MAC 6057 * table, so skip it. 6058 * 6059 * Called through mac_flow_walk_nolock() 6060 * 6061 * Return 0 if successful. 6062 */ 6063 static int 6064 mac_log_flowinfo(flow_entry_t *flent, void *arg) 6065 { 6066 mac_client_impl_t *mcip = flent->fe_mcip; 6067 i_mac_log_state_t *lstate = arg; 6068 netinfo_t *ninfo; 6069 6070 if (mcip == NULL) 6071 return (0); 6072 6073 /* 6074 * If the name starts with "vnic", and fe_user_generated is true (to 6075 * exclude the mcast and active flow entries created implicitly for 6076 * a vnic, it is a VNIC flow. i.e. vnic1 is a vnic flow, 6077 * vnic/bge1/mcast1 is not and neither is vnic/bge1/active. 6078 */ 6079 if (strncasecmp(flent->fe_flow_name, "vnic", 4) == 0 && 6080 (flent->fe_type & FLOW_USER) != 0) { 6081 return (0); 6082 } 6083 6084 if (!flent->fe_desc_logged) { 6085 /* 6086 * We don't return error because we want to continue the 6087 * walk in case this is the last walk which means we 6088 * need to reset fe_desc_logged in all the flows. 6089 */ 6090 if ((ninfo = mac_write_flow_desc(flent, mcip)) == NULL) 6091 return (0); 6092 list_insert_tail(lstate->mi_list, ninfo); 6093 flent->fe_desc_logged = B_TRUE; 6094 } 6095 6096 /* 6097 * Regardless of the error, we want to proceed in case we have to 6098 * reset fe_desc_logged. 6099 */ 6100 ninfo = mac_write_flow_stats(flent); 6101 if (ninfo == NULL) 6102 return (-1); 6103 6104 list_insert_tail(lstate->mi_list, ninfo); 6105 6106 if (mcip != NULL && !(mcip->mci_state_flags & MCIS_DESC_LOGGED)) 6107 flent->fe_desc_logged = B_FALSE; 6108 6109 return (0); 6110 } 6111 6112 /* 6113 * Log the description for each mac client of this mac_impl_t, if it 6114 * hasn't already been done. Additionally, log statistics for the link as 6115 * well. Walk the flow table and log information for each flow as well. 6116 * If it is the last walk (mci_last), then we turn off mci_desc_logged (and 6117 * also fe_desc_logged, if flow logging is on) since we want to log the 6118 * description if and when logging is restarted. 6119 * 6120 * Return 0 upon success or -1 upon failure 6121 */ 6122 static int 6123 i_mac_impl_log(mac_impl_t *mip, i_mac_log_state_t *lstate) 6124 { 6125 mac_client_impl_t *mcip; 6126 netinfo_t *ninfo; 6127 6128 i_mac_perim_enter(mip); 6129 /* 6130 * Only walk the client list for NIC and etherstub 6131 */ 6132 if ((mip->mi_state_flags & MIS_DISABLED) || 6133 ((mip->mi_state_flags & MIS_IS_VNIC) && 6134 (mac_get_lower_mac_handle((mac_handle_t)mip) != NULL))) { 6135 i_mac_perim_exit(mip); 6136 return (0); 6137 } 6138 6139 for (mcip = mip->mi_clients_list; mcip != NULL; 6140 mcip = mcip->mci_client_next) { 6141 if (!MCIP_DATAPATH_SETUP(mcip)) 6142 continue; 6143 if (lstate->mi_lenable) { 6144 if (!(mcip->mci_state_flags & MCIS_DESC_LOGGED)) { 6145 ninfo = mac_write_link_desc(mcip); 6146 if (ninfo == NULL) { 6147 /* 6148 * We can't terminate it if this is the last 6149 * walk, else there might be some links with 6150 * mi_desc_logged set to true, which means 6151 * their description won't be logged the next 6152 * time logging is started (similarly for the 6153 * flows within such links). We can continue 6154 * without walking the flow table (i.e. to 6155 * set fe_desc_logged to false) because we 6156 * won't have written any flow stuff for this 6157 * link as we haven't logged the link itself. 6158 */ 6159 i_mac_perim_exit(mip); 6160 if (lstate->mi_last) 6161 return (0); 6162 else 6163 return (-1); 6164 } 6165 mcip->mci_state_flags |= MCIS_DESC_LOGGED; 6166 list_insert_tail(lstate->mi_list, ninfo); 6167 } 6168 } 6169 6170 ninfo = mac_write_link_stats(mcip); 6171 if (ninfo == NULL && !lstate->mi_last) { 6172 i_mac_perim_exit(mip); 6173 return (-1); 6174 } 6175 list_insert_tail(lstate->mi_list, ninfo); 6176 6177 if (lstate->mi_last) 6178 mcip->mci_state_flags &= ~MCIS_DESC_LOGGED; 6179 6180 if (lstate->mi_fenable) { 6181 if (mcip->mci_subflow_tab != NULL) { 6182 (void) mac_flow_walk_nolock( 6183 mcip->mci_subflow_tab, mac_log_flowinfo, 6184 lstate); 6185 } 6186 } 6187 } 6188 i_mac_perim_exit(mip); 6189 return (0); 6190 } 6191 6192 /* 6193 * modhash walker function to add a mac_impl_t to a list 6194 */ 6195 /*ARGSUSED*/ 6196 static uint_t 6197 i_mac_impl_list_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 6198 { 6199 list_t *list = (list_t *)arg; 6200 mac_impl_t *mip = (mac_impl_t *)val; 6201 6202 if ((mip->mi_state_flags & MIS_DISABLED) == 0) { 6203 list_insert_tail(list, mip); 6204 mip->mi_ref++; 6205 } 6206 6207 return (MH_WALK_CONTINUE); 6208 } 6209 6210 void 6211 i_mac_log_info(list_t *net_log_list, i_mac_log_state_t *lstate) 6212 { 6213 list_t mac_impl_list; 6214 mac_impl_t *mip; 6215 netinfo_t *ninfo; 6216 6217 /* Create list of mac_impls */ 6218 ASSERT(RW_LOCK_HELD(&i_mac_impl_lock)); 6219 list_create(&mac_impl_list, sizeof (mac_impl_t), offsetof(mac_impl_t, 6220 mi_node)); 6221 mod_hash_walk(i_mac_impl_hash, i_mac_impl_list_walker, &mac_impl_list); 6222 rw_exit(&i_mac_impl_lock); 6223 6224 /* Create log entries for each mac_impl */ 6225 for (mip = list_head(&mac_impl_list); mip != NULL; 6226 mip = list_next(&mac_impl_list, mip)) { 6227 if (i_mac_impl_log(mip, lstate) != 0) 6228 continue; 6229 } 6230 6231 /* Remove elements and destroy list of mac_impls */ 6232 rw_enter(&i_mac_impl_lock, RW_WRITER); 6233 while ((mip = list_remove_tail(&mac_impl_list)) != NULL) { 6234 mip->mi_ref--; 6235 } 6236 rw_exit(&i_mac_impl_lock); 6237 list_destroy(&mac_impl_list); 6238 6239 /* 6240 * Write log entries to files outside of locks, free associated 6241 * structures, and remove entries from the list. 6242 */ 6243 while ((ninfo = list_head(net_log_list)) != NULL) { 6244 (void) exacct_commit_netinfo(ninfo->ni_record, ninfo->ni_type); 6245 list_remove(net_log_list, ninfo); 6246 kmem_free(ninfo->ni_record, ninfo->ni_size); 6247 kmem_free(ninfo, sizeof (*ninfo)); 6248 } 6249 list_destroy(net_log_list); 6250 } 6251 6252 /* 6253 * The timer thread that runs every mac_logging_interval seconds and logs 6254 * link and/or flow information. 6255 */ 6256 /* ARGSUSED */ 6257 void 6258 mac_log_linkinfo(void *arg) 6259 { 6260 i_mac_log_state_t lstate; 6261 list_t net_log_list; 6262 6263 list_create(&net_log_list, sizeof (netinfo_t), 6264 offsetof(netinfo_t, ni_link)); 6265 6266 rw_enter(&i_mac_impl_lock, RW_READER); 6267 if (!mac_flow_log_enable && !mac_link_log_enable) { 6268 rw_exit(&i_mac_impl_lock); 6269 return; 6270 } 6271 lstate.mi_fenable = mac_flow_log_enable; 6272 lstate.mi_lenable = mac_link_log_enable; 6273 lstate.mi_last = B_FALSE; 6274 lstate.mi_list = &net_log_list; 6275 6276 /* Write log entries for each mac_impl in the list */ 6277 i_mac_log_info(&net_log_list, &lstate); 6278 6279 if (mac_flow_log_enable || mac_link_log_enable) { 6280 mac_logging_timer = timeout(mac_log_linkinfo, NULL, 6281 SEC_TO_TICK(mac_logging_interval)); 6282 } 6283 } 6284 6285 typedef struct i_mac_fastpath_state_s { 6286 boolean_t mf_disable; 6287 int mf_err; 6288 } i_mac_fastpath_state_t; 6289 6290 /* modhash walker function to enable or disable fastpath */ 6291 /*ARGSUSED*/ 6292 static uint_t 6293 i_mac_fastpath_walker(mod_hash_key_t key, mod_hash_val_t *val, 6294 void *arg) 6295 { 6296 i_mac_fastpath_state_t *state = arg; 6297 mac_handle_t mh = (mac_handle_t)val; 6298 6299 if (state->mf_disable) 6300 state->mf_err = mac_fastpath_disable(mh); 6301 else 6302 mac_fastpath_enable(mh); 6303 6304 return (state->mf_err == 0 ? MH_WALK_CONTINUE : MH_WALK_TERMINATE); 6305 } 6306 6307 /* 6308 * Start the logging timer. 6309 */ 6310 int 6311 mac_start_logusage(mac_logtype_t type, uint_t interval) 6312 { 6313 i_mac_fastpath_state_t dstate = {B_TRUE, 0}; 6314 i_mac_fastpath_state_t estate = {B_FALSE, 0}; 6315 int err; 6316 6317 rw_enter(&i_mac_impl_lock, RW_WRITER); 6318 switch (type) { 6319 case MAC_LOGTYPE_FLOW: 6320 if (mac_flow_log_enable) { 6321 rw_exit(&i_mac_impl_lock); 6322 return (0); 6323 } 6324 /* FALLTHRU */ 6325 case MAC_LOGTYPE_LINK: 6326 if (mac_link_log_enable) { 6327 rw_exit(&i_mac_impl_lock); 6328 return (0); 6329 } 6330 break; 6331 default: 6332 ASSERT(0); 6333 } 6334 6335 /* Disable fastpath */ 6336 mod_hash_walk(i_mac_impl_hash, i_mac_fastpath_walker, &dstate); 6337 if ((err = dstate.mf_err) != 0) { 6338 /* Reenable fastpath */ 6339 mod_hash_walk(i_mac_impl_hash, i_mac_fastpath_walker, &estate); 6340 rw_exit(&i_mac_impl_lock); 6341 return (err); 6342 } 6343 6344 switch (type) { 6345 case MAC_LOGTYPE_FLOW: 6346 mac_flow_log_enable = B_TRUE; 6347 /* FALLTHRU */ 6348 case MAC_LOGTYPE_LINK: 6349 mac_link_log_enable = B_TRUE; 6350 break; 6351 } 6352 6353 mac_logging_interval = interval; 6354 rw_exit(&i_mac_impl_lock); 6355 mac_log_linkinfo(NULL); 6356 return (0); 6357 } 6358 6359 /* 6360 * Stop the logging timer if both link and flow logging are turned off. 6361 */ 6362 void 6363 mac_stop_logusage(mac_logtype_t type) 6364 { 6365 i_mac_log_state_t lstate; 6366 i_mac_fastpath_state_t estate = {B_FALSE, 0}; 6367 list_t net_log_list; 6368 6369 list_create(&net_log_list, sizeof (netinfo_t), 6370 offsetof(netinfo_t, ni_link)); 6371 6372 rw_enter(&i_mac_impl_lock, RW_WRITER); 6373 6374 lstate.mi_fenable = mac_flow_log_enable; 6375 lstate.mi_lenable = mac_link_log_enable; 6376 lstate.mi_list = &net_log_list; 6377 6378 /* Last walk */ 6379 lstate.mi_last = B_TRUE; 6380 6381 switch (type) { 6382 case MAC_LOGTYPE_FLOW: 6383 if (lstate.mi_fenable) { 6384 ASSERT(mac_link_log_enable); 6385 mac_flow_log_enable = B_FALSE; 6386 mac_link_log_enable = B_FALSE; 6387 break; 6388 } 6389 /* FALLTHRU */ 6390 case MAC_LOGTYPE_LINK: 6391 if (!lstate.mi_lenable || mac_flow_log_enable) { 6392 rw_exit(&i_mac_impl_lock); 6393 return; 6394 } 6395 mac_link_log_enable = B_FALSE; 6396 break; 6397 default: 6398 ASSERT(0); 6399 } 6400 6401 /* Reenable fastpath */ 6402 mod_hash_walk(i_mac_impl_hash, i_mac_fastpath_walker, &estate); 6403 6404 (void) untimeout(mac_logging_timer); 6405 mac_logging_timer = NULL; 6406 6407 /* Write log entries for each mac_impl in the list */ 6408 i_mac_log_info(&net_log_list, &lstate); 6409 } 6410 6411 /* 6412 * Walk the rx and tx SRS/SRs for a flow and update the priority value. 6413 */ 6414 void 6415 mac_flow_update_priority(mac_client_impl_t *mcip, flow_entry_t *flent) 6416 { 6417 pri_t pri; 6418 int count; 6419 mac_soft_ring_set_t *mac_srs; 6420 6421 if (flent->fe_rx_srs_cnt <= 0) 6422 return; 6423 6424 if (((mac_soft_ring_set_t *)flent->fe_rx_srs[0])->srs_type == 6425 SRST_FLOW) { 6426 pri = FLOW_PRIORITY(mcip->mci_min_pri, 6427 mcip->mci_max_pri, 6428 flent->fe_resource_props.mrp_priority); 6429 } else { 6430 pri = mcip->mci_max_pri; 6431 } 6432 6433 for (count = 0; count < flent->fe_rx_srs_cnt; count++) { 6434 mac_srs = flent->fe_rx_srs[count]; 6435 mac_update_srs_priority(mac_srs, pri); 6436 } 6437 /* 6438 * If we have a Tx SRS, we need to modify all the threads associated 6439 * with it. 6440 */ 6441 if (flent->fe_tx_srs != NULL) 6442 mac_update_srs_priority(flent->fe_tx_srs, pri); 6443 } 6444 6445 /* 6446 * RX and TX rings are reserved according to different semantics depending 6447 * on the requests from the MAC clients and type of rings: 6448 * 6449 * On the Tx side, by default we reserve individual rings, independently from 6450 * the groups. 6451 * 6452 * On the Rx side, the reservation is at the granularity of the group 6453 * of rings, and used for v12n level 1 only. It has a special case for the 6454 * primary client. 6455 * 6456 * If a share is allocated to a MAC client, we allocate a TX group and an 6457 * RX group to the client, and assign TX rings and RX rings to these 6458 * groups according to information gathered from the driver through 6459 * the share capability. 6460 * 6461 * The foreseable evolution of Rx rings will handle v12n level 2 and higher 6462 * to allocate individual rings out of a group and program the hw classifier 6463 * based on IP address or higher level criteria. 6464 */ 6465 6466 /* 6467 * mac_reserve_tx_ring() 6468 * Reserve a unused ring by marking it with MR_INUSE state. 6469 * As reserved, the ring is ready to function. 6470 * 6471 * Notes for Hybrid I/O: 6472 * 6473 * If a specific ring is needed, it is specified through the desired_ring 6474 * argument. Otherwise that argument is set to NULL. 6475 * If the desired ring was previous allocated to another client, this 6476 * function swaps it with a new ring from the group of unassigned rings. 6477 */ 6478 mac_ring_t * 6479 mac_reserve_tx_ring(mac_impl_t *mip, mac_ring_t *desired_ring) 6480 { 6481 mac_group_t *group; 6482 mac_grp_client_t *mgcp; 6483 mac_client_impl_t *mcip; 6484 mac_soft_ring_set_t *srs; 6485 6486 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 6487 6488 /* 6489 * Find an available ring and start it before changing its status. 6490 * The unassigned rings are at the end of the mi_tx_groups 6491 * array. 6492 */ 6493 group = MAC_DEFAULT_TX_GROUP(mip); 6494 6495 /* Can't take the default ring out of the default group */ 6496 ASSERT(desired_ring != (mac_ring_t *)mip->mi_default_tx_ring); 6497 6498 if (desired_ring->mr_state == MR_FREE) { 6499 ASSERT(MAC_GROUP_NO_CLIENT(group)); 6500 if (mac_start_ring(desired_ring) != 0) 6501 return (NULL); 6502 return (desired_ring); 6503 } 6504 /* 6505 * There are clients using this ring, so let's move the clients 6506 * away from using this ring. 6507 */ 6508 for (mgcp = group->mrg_clients; mgcp != NULL; mgcp = mgcp->mgc_next) { 6509 mcip = mgcp->mgc_client; 6510 mac_tx_client_quiesce((mac_client_handle_t)mcip); 6511 srs = MCIP_TX_SRS(mcip); 6512 ASSERT(mac_tx_srs_ring_present(srs, desired_ring)); 6513 mac_tx_invoke_callbacks(mcip, 6514 (mac_tx_cookie_t)mac_tx_srs_get_soft_ring(srs, 6515 desired_ring)); 6516 mac_tx_srs_del_ring(srs, desired_ring); 6517 mac_tx_client_restart((mac_client_handle_t)mcip); 6518 } 6519 return (desired_ring); 6520 } 6521 6522 /* 6523 * For a non-default group with multiple clients, return the primary client. 6524 */ 6525 static mac_client_impl_t * 6526 mac_get_grp_primary(mac_group_t *grp) 6527 { 6528 mac_grp_client_t *mgcp = grp->mrg_clients; 6529 mac_client_impl_t *mcip; 6530 6531 while (mgcp != NULL) { 6532 mcip = mgcp->mgc_client; 6533 if (mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) 6534 return (mcip); 6535 mgcp = mgcp->mgc_next; 6536 } 6537 return (NULL); 6538 } 6539 6540 /* 6541 * Hybrid I/O specifies the ring that should be given to a share. 6542 * If the ring is already used by clients, then we need to release 6543 * the ring back to the default group so that we can give it to 6544 * the share. This means the clients using this ring now get a 6545 * replacement ring. If there aren't any replacement rings, this 6546 * function returns a failure. 6547 */ 6548 static int 6549 mac_reclaim_ring_from_grp(mac_impl_t *mip, mac_ring_type_t ring_type, 6550 mac_ring_t *ring, mac_ring_t **rings, int nrings) 6551 { 6552 mac_group_t *group = (mac_group_t *)ring->mr_gh; 6553 mac_resource_props_t *mrp; 6554 mac_client_impl_t *mcip; 6555 mac_group_t *defgrp; 6556 mac_ring_t *tring; 6557 mac_group_t *tgrp; 6558 int i; 6559 int j; 6560 6561 mcip = MAC_GROUP_ONLY_CLIENT(group); 6562 if (mcip == NULL) 6563 mcip = mac_get_grp_primary(group); 6564 ASSERT(mcip != NULL); 6565 ASSERT(mcip->mci_share == 0); 6566 6567 mrp = MCIP_RESOURCE_PROPS(mcip); 6568 if (ring_type == MAC_RING_TYPE_RX) { 6569 defgrp = mip->mi_rx_donor_grp; 6570 if ((mrp->mrp_mask & MRP_RX_RINGS) == 0) { 6571 /* Need to put this mac client in the default group */ 6572 if (mac_rx_switch_group(mcip, group, defgrp) != 0) 6573 return (ENOSPC); 6574 } else { 6575 /* 6576 * Switch this ring with some other ring from 6577 * the default group. 6578 */ 6579 for (tring = defgrp->mrg_rings; tring != NULL; 6580 tring = tring->mr_next) { 6581 if (tring->mr_index == 0) 6582 continue; 6583 for (j = 0; j < nrings; j++) { 6584 if (rings[j] == tring) 6585 break; 6586 } 6587 if (j >= nrings) 6588 break; 6589 } 6590 if (tring == NULL) 6591 return (ENOSPC); 6592 if (mac_group_mov_ring(mip, group, tring) != 0) 6593 return (ENOSPC); 6594 if (mac_group_mov_ring(mip, defgrp, ring) != 0) { 6595 (void) mac_group_mov_ring(mip, defgrp, tring); 6596 return (ENOSPC); 6597 } 6598 } 6599 ASSERT(ring->mr_gh == (mac_group_handle_t)defgrp); 6600 return (0); 6601 } 6602 6603 defgrp = MAC_DEFAULT_TX_GROUP(mip); 6604 if (ring == (mac_ring_t *)mip->mi_default_tx_ring) { 6605 /* 6606 * See if we can get a spare ring to replace the default 6607 * ring. 6608 */ 6609 if (defgrp->mrg_cur_count == 1) { 6610 /* 6611 * Need to get a ring from another client, see if 6612 * there are any clients that can be moved to 6613 * the default group, thereby freeing some rings. 6614 */ 6615 for (i = 0; i < mip->mi_tx_group_count; i++) { 6616 tgrp = &mip->mi_tx_groups[i]; 6617 if (tgrp->mrg_state == 6618 MAC_GROUP_STATE_REGISTERED) { 6619 continue; 6620 } 6621 mcip = MAC_GROUP_ONLY_CLIENT(tgrp); 6622 if (mcip == NULL) 6623 mcip = mac_get_grp_primary(tgrp); 6624 ASSERT(mcip != NULL); 6625 mrp = MCIP_RESOURCE_PROPS(mcip); 6626 if ((mrp->mrp_mask & MRP_TX_RINGS) == 0) { 6627 ASSERT(tgrp->mrg_cur_count == 1); 6628 /* 6629 * If this ring is part of the 6630 * rings asked by the share we cannot 6631 * use it as the default ring. 6632 */ 6633 for (j = 0; j < nrings; j++) { 6634 if (rings[j] == tgrp->mrg_rings) 6635 break; 6636 } 6637 if (j < nrings) 6638 continue; 6639 mac_tx_client_quiesce( 6640 (mac_client_handle_t)mcip); 6641 mac_tx_switch_group(mcip, tgrp, 6642 defgrp); 6643 mac_tx_client_restart( 6644 (mac_client_handle_t)mcip); 6645 break; 6646 } 6647 } 6648 /* 6649 * All the rings are reserved, can't give up the 6650 * default ring. 6651 */ 6652 if (defgrp->mrg_cur_count <= 1) 6653 return (ENOSPC); 6654 } 6655 /* 6656 * Swap the default ring with another. 6657 */ 6658 for (tring = defgrp->mrg_rings; tring != NULL; 6659 tring = tring->mr_next) { 6660 /* 6661 * If this ring is part of the rings asked by the 6662 * share we cannot use it as the default ring. 6663 */ 6664 for (j = 0; j < nrings; j++) { 6665 if (rings[j] == tring) 6666 break; 6667 } 6668 if (j >= nrings) 6669 break; 6670 } 6671 ASSERT(tring != NULL); 6672 mip->mi_default_tx_ring = (mac_ring_handle_t)tring; 6673 return (0); 6674 } 6675 /* 6676 * The Tx ring is with a group reserved by a MAC client. See if 6677 * we can swap it. 6678 */ 6679 ASSERT(group->mrg_state == MAC_GROUP_STATE_RESERVED); 6680 mcip = MAC_GROUP_ONLY_CLIENT(group); 6681 if (mcip == NULL) 6682 mcip = mac_get_grp_primary(group); 6683 ASSERT(mcip != NULL); 6684 mrp = MCIP_RESOURCE_PROPS(mcip); 6685 mac_tx_client_quiesce((mac_client_handle_t)mcip); 6686 if ((mrp->mrp_mask & MRP_TX_RINGS) == 0) { 6687 ASSERT(group->mrg_cur_count == 1); 6688 /* Put this mac client in the default group */ 6689 mac_tx_switch_group(mcip, group, defgrp); 6690 } else { 6691 /* 6692 * Switch this ring with some other ring from 6693 * the default group. 6694 */ 6695 for (tring = defgrp->mrg_rings; tring != NULL; 6696 tring = tring->mr_next) { 6697 if (tring == (mac_ring_t *)mip->mi_default_tx_ring) 6698 continue; 6699 /* 6700 * If this ring is part of the rings asked by the 6701 * share we cannot use it for swapping. 6702 */ 6703 for (j = 0; j < nrings; j++) { 6704 if (rings[j] == tring) 6705 break; 6706 } 6707 if (j >= nrings) 6708 break; 6709 } 6710 if (tring == NULL) { 6711 mac_tx_client_restart((mac_client_handle_t)mcip); 6712 return (ENOSPC); 6713 } 6714 if (mac_group_mov_ring(mip, group, tring) != 0) { 6715 mac_tx_client_restart((mac_client_handle_t)mcip); 6716 return (ENOSPC); 6717 } 6718 if (mac_group_mov_ring(mip, defgrp, ring) != 0) { 6719 (void) mac_group_mov_ring(mip, defgrp, tring); 6720 mac_tx_client_restart((mac_client_handle_t)mcip); 6721 return (ENOSPC); 6722 } 6723 } 6724 mac_tx_client_restart((mac_client_handle_t)mcip); 6725 ASSERT(ring->mr_gh == (mac_group_handle_t)defgrp); 6726 return (0); 6727 } 6728 6729 /* 6730 * Populate a zero-ring group with rings. If the share is non-NULL, 6731 * the rings are chosen according to that share. 6732 * Invoked after allocating a new RX or TX group through 6733 * mac_reserve_rx_group() or mac_reserve_tx_group(), respectively. 6734 * Returns zero on success, an errno otherwise. 6735 */ 6736 int 6737 i_mac_group_allocate_rings(mac_impl_t *mip, mac_ring_type_t ring_type, 6738 mac_group_t *src_group, mac_group_t *new_group, mac_share_handle_t share, 6739 uint32_t ringcnt) 6740 { 6741 mac_ring_t **rings, *ring; 6742 uint_t nrings; 6743 int rv = 0, i = 0, j; 6744 6745 ASSERT((ring_type == MAC_RING_TYPE_RX && 6746 mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) || 6747 (ring_type == MAC_RING_TYPE_TX && 6748 mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC)); 6749 6750 /* 6751 * First find the rings to allocate to the group. 6752 */ 6753 if (share != 0) { 6754 /* get rings through ms_squery() */ 6755 mip->mi_share_capab.ms_squery(share, ring_type, NULL, &nrings); 6756 ASSERT(nrings != 0); 6757 rings = kmem_alloc(nrings * sizeof (mac_ring_handle_t), 6758 KM_SLEEP); 6759 mip->mi_share_capab.ms_squery(share, ring_type, 6760 (mac_ring_handle_t *)rings, &nrings); 6761 for (i = 0; i < nrings; i++) { 6762 /* 6763 * If we have given this ring to a non-default 6764 * group, we need to check if we can get this 6765 * ring. 6766 */ 6767 ring = rings[i]; 6768 if (ring->mr_gh != (mac_group_handle_t)src_group || 6769 ring == (mac_ring_t *)mip->mi_default_tx_ring) { 6770 if (mac_reclaim_ring_from_grp(mip, ring_type, 6771 ring, rings, nrings) != 0) { 6772 rv = ENOSPC; 6773 goto bail; 6774 } 6775 } 6776 } 6777 } else { 6778 /* 6779 * Pick one ring from default group. 6780 * 6781 * for now pick the second ring which requires the first ring 6782 * at index 0 to stay in the default group, since it is the 6783 * ring which carries the multicast traffic. 6784 * We need a better way for a driver to indicate this, 6785 * for example a per-ring flag. 6786 */ 6787 rings = kmem_alloc(ringcnt * sizeof (mac_ring_handle_t), 6788 KM_SLEEP); 6789 for (ring = src_group->mrg_rings; ring != NULL; 6790 ring = ring->mr_next) { 6791 if (ring_type == MAC_RING_TYPE_RX && 6792 ring->mr_index == 0) { 6793 continue; 6794 } 6795 if (ring_type == MAC_RING_TYPE_TX && 6796 ring == (mac_ring_t *)mip->mi_default_tx_ring) { 6797 continue; 6798 } 6799 rings[i++] = ring; 6800 if (i == ringcnt) 6801 break; 6802 } 6803 ASSERT(ring != NULL); 6804 nrings = i; 6805 /* Not enough rings as required */ 6806 if (nrings != ringcnt) { 6807 rv = ENOSPC; 6808 goto bail; 6809 } 6810 } 6811 6812 switch (ring_type) { 6813 case MAC_RING_TYPE_RX: 6814 if (src_group->mrg_cur_count - nrings < 1) { 6815 /* we ran out of rings */ 6816 rv = ENOSPC; 6817 goto bail; 6818 } 6819 6820 /* move receive rings to new group */ 6821 for (i = 0; i < nrings; i++) { 6822 rv = mac_group_mov_ring(mip, new_group, rings[i]); 6823 if (rv != 0) { 6824 /* move rings back on failure */ 6825 for (j = 0; j < i; j++) { 6826 (void) mac_group_mov_ring(mip, 6827 src_group, rings[j]); 6828 } 6829 goto bail; 6830 } 6831 } 6832 break; 6833 6834 case MAC_RING_TYPE_TX: { 6835 mac_ring_t *tmp_ring; 6836 6837 /* move the TX rings to the new group */ 6838 for (i = 0; i < nrings; i++) { 6839 /* get the desired ring */ 6840 tmp_ring = mac_reserve_tx_ring(mip, rings[i]); 6841 if (tmp_ring == NULL) { 6842 rv = ENOSPC; 6843 goto bail; 6844 } 6845 ASSERT(tmp_ring == rings[i]); 6846 rv = mac_group_mov_ring(mip, new_group, rings[i]); 6847 if (rv != 0) { 6848 /* cleanup on failure */ 6849 for (j = 0; j < i; j++) { 6850 (void) mac_group_mov_ring(mip, 6851 MAC_DEFAULT_TX_GROUP(mip), 6852 rings[j]); 6853 } 6854 goto bail; 6855 } 6856 } 6857 break; 6858 } 6859 } 6860 6861 /* add group to share */ 6862 if (share != 0) 6863 mip->mi_share_capab.ms_sadd(share, new_group->mrg_driver); 6864 6865 bail: 6866 /* free temporary array of rings */ 6867 kmem_free(rings, nrings * sizeof (mac_ring_handle_t)); 6868 6869 return (rv); 6870 } 6871 6872 void 6873 mac_group_add_client(mac_group_t *grp, mac_client_impl_t *mcip) 6874 { 6875 mac_grp_client_t *mgcp; 6876 6877 for (mgcp = grp->mrg_clients; mgcp != NULL; mgcp = mgcp->mgc_next) { 6878 if (mgcp->mgc_client == mcip) 6879 break; 6880 } 6881 6882 ASSERT(mgcp == NULL); 6883 6884 mgcp = kmem_zalloc(sizeof (mac_grp_client_t), KM_SLEEP); 6885 mgcp->mgc_client = mcip; 6886 mgcp->mgc_next = grp->mrg_clients; 6887 grp->mrg_clients = mgcp; 6888 } 6889 6890 void 6891 mac_group_remove_client(mac_group_t *grp, mac_client_impl_t *mcip) 6892 { 6893 mac_grp_client_t *mgcp, **pprev; 6894 6895 for (pprev = &grp->mrg_clients, mgcp = *pprev; mgcp != NULL; 6896 pprev = &mgcp->mgc_next, mgcp = *pprev) { 6897 if (mgcp->mgc_client == mcip) 6898 break; 6899 } 6900 6901 ASSERT(mgcp != NULL); 6902 6903 *pprev = mgcp->mgc_next; 6904 kmem_free(mgcp, sizeof (mac_grp_client_t)); 6905 } 6906 6907 /* 6908 * Return true if any client on this group explicitly asked for HW 6909 * rings (of type mask) or have a bound share. 6910 */ 6911 static boolean_t 6912 i_mac_clients_hw(mac_group_t *grp, uint32_t mask) 6913 { 6914 mac_grp_client_t *mgcip; 6915 mac_client_impl_t *mcip; 6916 mac_resource_props_t *mrp; 6917 6918 for (mgcip = grp->mrg_clients; mgcip != NULL; mgcip = mgcip->mgc_next) { 6919 mcip = mgcip->mgc_client; 6920 mrp = MCIP_RESOURCE_PROPS(mcip); 6921 if (mcip->mci_share != 0 || (mrp->mrp_mask & mask) != 0) 6922 return (B_TRUE); 6923 } 6924 6925 return (B_FALSE); 6926 } 6927 6928 /* 6929 * Finds an available group and exclusively reserves it for a client. 6930 * The group is chosen to suit the flow's resource controls (bandwidth and 6931 * fanout requirements) and the address type. 6932 * If the requestor is the pimary MAC then return the group with the 6933 * largest number of rings, otherwise the default ring when available. 6934 */ 6935 mac_group_t * 6936 mac_reserve_rx_group(mac_client_impl_t *mcip, uint8_t *mac_addr, boolean_t move) 6937 { 6938 mac_share_handle_t share = mcip->mci_share; 6939 mac_impl_t *mip = mcip->mci_mip; 6940 mac_group_t *grp = NULL; 6941 int i; 6942 int err = 0; 6943 mac_address_t *map; 6944 mac_resource_props_t *mrp = MCIP_RESOURCE_PROPS(mcip); 6945 int nrings; 6946 int donor_grp_rcnt; 6947 boolean_t need_exclgrp = B_FALSE; 6948 int need_rings = 0; 6949 mac_group_t *candidate_grp = NULL; 6950 mac_client_impl_t *gclient; 6951 mac_group_t *donorgrp = NULL; 6952 boolean_t rxhw = mrp->mrp_mask & MRP_RX_RINGS; 6953 boolean_t unspec = mrp->mrp_mask & MRP_RXRINGS_UNSPEC; 6954 boolean_t isprimary; 6955 6956 ASSERT(MAC_PERIM_HELD((mac_handle_t)mip)); 6957 6958 isprimary = mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC; 6959 6960 /* 6961 * Check if a group already has this MAC address (case of VLANs) 6962 * unless we are moving this MAC client from one group to another. 6963 */ 6964 if (!move && (map = mac_find_macaddr(mip, mac_addr)) != NULL) { 6965 if (map->ma_group != NULL) 6966 return (map->ma_group); 6967 } 6968 6969 if (mip->mi_rx_groups == NULL || mip->mi_rx_group_count == 0) 6970 return (NULL); 6971 6972 /* 6973 * If this client is requesting exclusive MAC access then 6974 * return NULL to ensure the client uses the default group. 6975 */ 6976 if (mcip->mci_state_flags & MCIS_EXCLUSIVE) 6977 return (NULL); 6978 6979 /* For dynamic groups default unspecified to 1 */ 6980 if (rxhw && unspec && 6981 mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) { 6982 mrp->mrp_nrxrings = 1; 6983 } 6984 6985 /* 6986 * For static grouping we allow only specifying rings=0 and 6987 * unspecified 6988 */ 6989 if (rxhw && mrp->mrp_nrxrings > 0 && 6990 mip->mi_rx_group_type == MAC_GROUP_TYPE_STATIC) { 6991 return (NULL); 6992 } 6993 6994 if (rxhw) { 6995 /* 6996 * We have explicitly asked for a group (with nrxrings, 6997 * if unspec). 6998 */ 6999 if (unspec || mrp->mrp_nrxrings > 0) { 7000 need_exclgrp = B_TRUE; 7001 need_rings = mrp->mrp_nrxrings; 7002 } else if (mrp->mrp_nrxrings == 0) { 7003 /* 7004 * We have asked for a software group. 7005 */ 7006 return (NULL); 7007 } 7008 } else if (isprimary && mip->mi_nactiveclients == 1 && 7009 mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) { 7010 /* 7011 * If the primary is the only active client on this 7012 * mip and we have not asked for any rings, we give 7013 * it the default group so that the primary gets to 7014 * use all the rings. 7015 */ 7016 return (NULL); 7017 } 7018 7019 /* The group that can donate rings */ 7020 donorgrp = mip->mi_rx_donor_grp; 7021 7022 /* 7023 * The number of rings that the default group can donate. 7024 * We need to leave at least one ring. 7025 */ 7026 donor_grp_rcnt = donorgrp->mrg_cur_count - 1; 7027 7028 /* 7029 * Try to exclusively reserve a RX group. 7030 * 7031 * For flows requiring HW_DEFAULT_RING (unicast flow of the primary 7032 * client), try to reserve the a non-default RX group and give 7033 * it all the rings from the donor group, except the default ring 7034 * 7035 * For flows requiring HW_RING (unicast flow of other clients), try 7036 * to reserve non-default RX group with the specified number of 7037 * rings, if available. 7038 * 7039 * For flows that have not asked for software or hardware ring, 7040 * try to reserve a non-default group with 1 ring, if available. 7041 */ 7042 for (i = 1; i < mip->mi_rx_group_count; i++) { 7043 grp = &mip->mi_rx_groups[i]; 7044 7045 DTRACE_PROBE3(rx__group__trying, char *, mip->mi_name, 7046 int, grp->mrg_index, mac_group_state_t, grp->mrg_state); 7047 7048 /* 7049 * Check if this group could be a candidate group for 7050 * eviction if we need a group for this MAC client, 7051 * but there aren't any. A candidate group is one 7052 * that didn't ask for an exclusive group, but got 7053 * one and it has enough rings (combined with what 7054 * the donor group can donate) for the new MAC 7055 * client. 7056 */ 7057 if (grp->mrg_state >= MAC_GROUP_STATE_RESERVED) { 7058 /* 7059 * If the donor group is not the default 7060 * group, don't bother looking for a candidate 7061 * group. If we don't have enough rings we 7062 * will check if the primary group can be 7063 * vacated. 7064 */ 7065 if (candidate_grp == NULL && 7066 donorgrp == MAC_DEFAULT_RX_GROUP(mip)) { 7067 if (!i_mac_clients_hw(grp, MRP_RX_RINGS) && 7068 (unspec || 7069 (grp->mrg_cur_count + donor_grp_rcnt >= 7070 need_rings))) { 7071 candidate_grp = grp; 7072 } 7073 } 7074 continue; 7075 } 7076 /* 7077 * This group could already be SHARED by other multicast 7078 * flows on this client. In that case, the group would 7079 * be shared and has already been started. 7080 */ 7081 ASSERT(grp->mrg_state != MAC_GROUP_STATE_UNINIT); 7082 7083 if ((grp->mrg_state == MAC_GROUP_STATE_REGISTERED) && 7084 (mac_start_group(grp) != 0)) { 7085 continue; 7086 } 7087 7088 if (mip->mi_rx_group_type != MAC_GROUP_TYPE_DYNAMIC) 7089 break; 7090 ASSERT(grp->mrg_cur_count == 0); 7091 7092 /* 7093 * Populate the group. Rings should be taken 7094 * from the donor group. 7095 */ 7096 nrings = rxhw ? need_rings : isprimary ? donor_grp_rcnt: 1; 7097 7098 /* 7099 * If the donor group can't donate, let's just walk and 7100 * see if someone can vacate a group, so that we have 7101 * enough rings for this, unless we already have 7102 * identified a candiate group.. 7103 */ 7104 if (nrings <= donor_grp_rcnt) { 7105 err = i_mac_group_allocate_rings(mip, MAC_RING_TYPE_RX, 7106 donorgrp, grp, share, nrings); 7107 if (err == 0) { 7108 /* 7109 * For a share i_mac_group_allocate_rings gets 7110 * the rings from the driver, let's populate 7111 * the property for the client now. 7112 */ 7113 if (share != 0) { 7114 mac_client_set_rings( 7115 (mac_client_handle_t)mcip, 7116 grp->mrg_cur_count, -1); 7117 } 7118 if (mac_is_primary_client(mcip) && !rxhw) 7119 mip->mi_rx_donor_grp = grp; 7120 break; 7121 } 7122 } 7123 7124 DTRACE_PROBE3(rx__group__reserve__alloc__rings, char *, 7125 mip->mi_name, int, grp->mrg_index, int, err); 7126 7127 /* 7128 * It's a dynamic group but the grouping operation 7129 * failed. 7130 */ 7131 mac_stop_group(grp); 7132 } 7133 7134 /* We didn't find an exclusive group for this MAC client */ 7135 if (i >= mip->mi_rx_group_count) { 7136 7137 if (!need_exclgrp) 7138 return (NULL); 7139 7140 /* 7141 * If we found a candidate group then move the 7142 * existing MAC client from the candidate_group to the 7143 * default group and give the candidate_group to the 7144 * new MAC client. If we didn't find a candidate 7145 * group, then check if the primary is in its own 7146 * group and if it can make way for this MAC client. 7147 */ 7148 if (candidate_grp == NULL && 7149 donorgrp != MAC_DEFAULT_RX_GROUP(mip) && 7150 donorgrp->mrg_cur_count >= need_rings) { 7151 candidate_grp = donorgrp; 7152 } 7153 if (candidate_grp != NULL) { 7154 boolean_t prim_grp = B_FALSE; 7155 7156 /* 7157 * Switch the existing MAC client from the 7158 * candidate group to the default group. If 7159 * the candidate group is the donor group, 7160 * then after the switch we need to update the 7161 * donor group too. 7162 */ 7163 grp = candidate_grp; 7164 gclient = grp->mrg_clients->mgc_client; 7165 VERIFY3P(gclient, !=, NULL); 7166 if (grp == mip->mi_rx_donor_grp) 7167 prim_grp = B_TRUE; 7168 if (mac_rx_switch_group(gclient, grp, 7169 MAC_DEFAULT_RX_GROUP(mip)) != 0) { 7170 return (NULL); 7171 } 7172 if (prim_grp) { 7173 mip->mi_rx_donor_grp = 7174 MAC_DEFAULT_RX_GROUP(mip); 7175 donorgrp = MAC_DEFAULT_RX_GROUP(mip); 7176 } 7177 7178 /* 7179 * Now give this group with the required rings 7180 * to this MAC client. 7181 */ 7182 ASSERT(grp->mrg_state == MAC_GROUP_STATE_REGISTERED); 7183 if (mac_start_group(grp) != 0) 7184 return (NULL); 7185 7186 if (mip->mi_rx_group_type != MAC_GROUP_TYPE_DYNAMIC) 7187 return (grp); 7188 7189 donor_grp_rcnt = donorgrp->mrg_cur_count - 1; 7190 ASSERT(grp->mrg_cur_count == 0); 7191 ASSERT(donor_grp_rcnt >= need_rings); 7192 err = i_mac_group_allocate_rings(mip, MAC_RING_TYPE_RX, 7193 donorgrp, grp, share, need_rings); 7194 if (err == 0) { 7195 /* 7196 * For a share i_mac_group_allocate_rings gets 7197 * the rings from the driver, let's populate 7198 * the property for the client now. 7199 */ 7200 if (share != 0) { 7201 mac_client_set_rings( 7202 (mac_client_handle_t)mcip, 7203 grp->mrg_cur_count, -1); 7204 } 7205 DTRACE_PROBE2(rx__group__reserved, 7206 char *, mip->mi_name, int, grp->mrg_index); 7207 return (grp); 7208 } 7209 DTRACE_PROBE3(rx__group__reserve__alloc__rings, char *, 7210 mip->mi_name, int, grp->mrg_index, int, err); 7211 mac_stop_group(grp); 7212 } 7213 return (NULL); 7214 } 7215 ASSERT(grp != NULL); 7216 7217 DTRACE_PROBE2(rx__group__reserved, 7218 char *, mip->mi_name, int, grp->mrg_index); 7219 return (grp); 7220 } 7221 7222 /* 7223 * mac_rx_release_group() 7224 * 7225 * Release the group when it has no remaining clients. The group is 7226 * stopped and its shares are removed and all rings are assigned back 7227 * to default group. This should never be called against the default 7228 * group. 7229 */ 7230 void 7231 mac_release_rx_group(mac_client_impl_t *mcip, mac_group_t *group) 7232 { 7233 mac_impl_t *mip = mcip->mci_mip; 7234 mac_ring_t *ring; 7235 7236 ASSERT(group != MAC_DEFAULT_RX_GROUP(mip)); 7237 ASSERT(MAC_GROUP_NO_CLIENT(group) == B_TRUE); 7238 7239 if (mip->mi_rx_donor_grp == group) 7240 mip->mi_rx_donor_grp = MAC_DEFAULT_RX_GROUP(mip); 7241 7242 /* 7243 * This is the case where there are no clients left. Any 7244 * SRS etc on this group have also be quiesced. 7245 */ 7246 for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next) { 7247 if (ring->mr_classify_type == MAC_HW_CLASSIFIER) { 7248 ASSERT(group->mrg_state == MAC_GROUP_STATE_RESERVED); 7249 /* 7250 * Remove the SRS associated with the HW ring. 7251 * As a result, polling will be disabled. 7252 */ 7253 ring->mr_srs = NULL; 7254 } 7255 ASSERT(group->mrg_state < MAC_GROUP_STATE_RESERVED || 7256 ring->mr_state == MR_INUSE); 7257 if (ring->mr_state == MR_INUSE) { 7258 mac_stop_ring(ring); 7259 ring->mr_flag = 0; 7260 } 7261 } 7262 7263 /* remove group from share */ 7264 if (mcip->mci_share != 0) { 7265 mip->mi_share_capab.ms_sremove(mcip->mci_share, 7266 group->mrg_driver); 7267 } 7268 7269 if (mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) { 7270 mac_ring_t *ring; 7271 7272 /* 7273 * Rings were dynamically allocated to group. 7274 * Move rings back to default group. 7275 */ 7276 while ((ring = group->mrg_rings) != NULL) { 7277 (void) mac_group_mov_ring(mip, mip->mi_rx_donor_grp, 7278 ring); 7279 } 7280 } 7281 mac_stop_group(group); 7282 /* 7283 * Possible improvement: See if we can assign the group just released 7284 * to a another client of the mip 7285 */ 7286 } 7287 7288 /* 7289 * Move the MAC address from fgrp to tgrp. 7290 */ 7291 static int 7292 mac_rx_move_macaddr(mac_client_impl_t *mcip, mac_group_t *fgrp, 7293 mac_group_t *tgrp) 7294 { 7295 mac_impl_t *mip = mcip->mci_mip; 7296 uint8_t maddr[MAXMACADDRLEN]; 7297 int err = 0; 7298 uint16_t vid; 7299 mac_unicast_impl_t *muip; 7300 boolean_t use_hw; 7301 7302 mac_rx_client_quiesce((mac_client_handle_t)mcip); 7303 VERIFY3P(mcip->mci_unicast, !=, NULL); 7304 bcopy(mcip->mci_unicast->ma_addr, maddr, mcip->mci_unicast->ma_len); 7305 7306 /* 7307 * Does the client require MAC address hardware classifiction? 7308 */ 7309 use_hw = (mcip->mci_state_flags & MCIS_UNICAST_HW) != 0; 7310 vid = i_mac_flow_vid(mcip->mci_flent); 7311 7312 /* 7313 * You can never move an address that is shared by multiple 7314 * clients. mac_datapath_setup() ensures that clients sharing 7315 * an address are placed on the default group. This guarantees 7316 * that a non-default group will only ever have one client and 7317 * thus make full use of HW filters. 7318 */ 7319 if (mac_check_macaddr_shared(mcip->mci_unicast)) 7320 return (EINVAL); 7321 7322 err = mac_remove_macaddr_vlan(mcip->mci_unicast, vid); 7323 7324 if (err != 0) { 7325 mac_rx_client_restart((mac_client_handle_t)mcip); 7326 return (err); 7327 } 7328 7329 /* 7330 * If this isn't the primary MAC address then the 7331 * mac_address_t has been freed by the last call to 7332 * mac_remove_macaddr_vlan(). In any case, NULL the reference 7333 * to avoid a dangling pointer. 7334 */ 7335 mcip->mci_unicast = NULL; 7336 7337 /* 7338 * We also have to NULL all the mui_map references -- sun4v 7339 * strikes again! 7340 */ 7341 rw_enter(&mcip->mci_rw_lock, RW_WRITER); 7342 for (muip = mcip->mci_unicast_list; muip != NULL; muip = muip->mui_next) 7343 muip->mui_map = NULL; 7344 rw_exit(&mcip->mci_rw_lock); 7345 7346 /* 7347 * Program the H/W Classifier first, if this fails we need not 7348 * proceed with the other stuff. 7349 */ 7350 if ((err = mac_add_macaddr_vlan(mip, tgrp, maddr, vid, use_hw)) != 0) { 7351 int err2; 7352 7353 /* Revert back the H/W Classifier */ 7354 err2 = mac_add_macaddr_vlan(mip, fgrp, maddr, vid, use_hw); 7355 7356 if (err2 != 0) { 7357 cmn_err(CE_WARN, "Failed to revert HW classification" 7358 " on MAC %s, for client %s: %d.", mip->mi_name, 7359 mcip->mci_name, err2); 7360 } 7361 7362 mac_rx_client_restart((mac_client_handle_t)mcip); 7363 return (err); 7364 } 7365 7366 /* 7367 * Get a reference to the new mac_address_t and update the 7368 * client's reference. Then restart the client and add the 7369 * other clients of this MAC addr (if they exsit). 7370 */ 7371 mcip->mci_unicast = mac_find_macaddr(mip, maddr); 7372 rw_enter(&mcip->mci_rw_lock, RW_WRITER); 7373 for (muip = mcip->mci_unicast_list; muip != NULL; muip = muip->mui_next) 7374 muip->mui_map = mcip->mci_unicast; 7375 rw_exit(&mcip->mci_rw_lock); 7376 mac_rx_client_restart((mac_client_handle_t)mcip); 7377 return (0); 7378 } 7379 7380 /* 7381 * Switch the MAC client from one group to another. This means we need 7382 * to remove the MAC address from the group, remove the MAC client, 7383 * teardown the SRSs and revert the group state. Then, we add the client 7384 * to the destination group, set the SRSs, and add the MAC address to the 7385 * group. 7386 */ 7387 int 7388 mac_rx_switch_group(mac_client_impl_t *mcip, mac_group_t *fgrp, 7389 mac_group_t *tgrp) 7390 { 7391 int err; 7392 mac_group_state_t next_state; 7393 mac_client_impl_t *group_only_mcip; 7394 mac_client_impl_t *gmcip; 7395 mac_impl_t *mip = mcip->mci_mip; 7396 mac_grp_client_t *mgcp; 7397 7398 VERIFY3P(fgrp, ==, mcip->mci_flent->fe_rx_ring_group); 7399 7400 if ((err = mac_rx_move_macaddr(mcip, fgrp, tgrp)) != 0) 7401 return (err); 7402 7403 /* 7404 * If the group is marked as reserved and in use by a single 7405 * client, then there is an SRS to teardown. 7406 */ 7407 if (fgrp->mrg_state == MAC_GROUP_STATE_RESERVED && 7408 MAC_GROUP_ONLY_CLIENT(fgrp) != NULL) { 7409 mac_rx_srs_group_teardown(mcip->mci_flent, B_TRUE); 7410 } 7411 7412 /* 7413 * If we are moving the client from a non-default group, then 7414 * we know that any additional clients on this group share the 7415 * same MAC address. Since we moved the MAC address filter, we 7416 * need to move these clients too. 7417 * 7418 * If we are moving the client from the default group and its 7419 * MAC address has VLAN clients, then we must move those 7420 * clients as well. 7421 * 7422 * In both cases the idea is the same: we moved the MAC 7423 * address filter to the tgrp, so we must move all clients 7424 * using that MAC address to tgrp as well. 7425 */ 7426 if (fgrp != MAC_DEFAULT_RX_GROUP(mip)) { 7427 mgcp = fgrp->mrg_clients; 7428 while (mgcp != NULL) { 7429 gmcip = mgcp->mgc_client; 7430 mgcp = mgcp->mgc_next; 7431 mac_group_remove_client(fgrp, gmcip); 7432 mac_group_add_client(tgrp, gmcip); 7433 gmcip->mci_flent->fe_rx_ring_group = tgrp; 7434 } 7435 mac_release_rx_group(mcip, fgrp); 7436 VERIFY3B(MAC_GROUP_NO_CLIENT(fgrp), ==, B_TRUE); 7437 mac_set_group_state(fgrp, MAC_GROUP_STATE_REGISTERED); 7438 } else { 7439 mac_group_remove_client(fgrp, mcip); 7440 mac_group_add_client(tgrp, mcip); 7441 mcip->mci_flent->fe_rx_ring_group = tgrp; 7442 7443 /* 7444 * If there are other clients (VLANs) sharing this address 7445 * then move them too. 7446 */ 7447 if (mac_check_macaddr_shared(mcip->mci_unicast)) { 7448 /* 7449 * We need to move all the clients that are using 7450 * this MAC address. 7451 */ 7452 mgcp = fgrp->mrg_clients; 7453 while (mgcp != NULL) { 7454 gmcip = mgcp->mgc_client; 7455 mgcp = mgcp->mgc_next; 7456 if (mcip->mci_unicast == gmcip->mci_unicast) { 7457 mac_group_remove_client(fgrp, gmcip); 7458 mac_group_add_client(tgrp, gmcip); 7459 gmcip->mci_flent->fe_rx_ring_group = 7460 tgrp; 7461 } 7462 } 7463 } 7464 7465 /* 7466 * The default group still handles multicast and 7467 * broadcast traffic; it won't transition to 7468 * MAC_GROUP_STATE_REGISTERED. 7469 */ 7470 if (fgrp->mrg_state == MAC_GROUP_STATE_RESERVED) 7471 mac_rx_group_unmark(fgrp, MR_CONDEMNED); 7472 mac_set_group_state(fgrp, MAC_GROUP_STATE_SHARED); 7473 } 7474 7475 next_state = mac_group_next_state(tgrp, &group_only_mcip, 7476 MAC_DEFAULT_RX_GROUP(mip), B_TRUE); 7477 mac_set_group_state(tgrp, next_state); 7478 7479 /* 7480 * If the destination group is reserved, then setup the SRSes. 7481 * Otherwise make sure to use SW classification. 7482 */ 7483 if (tgrp->mrg_state == MAC_GROUP_STATE_RESERVED) { 7484 mac_rx_srs_group_setup(mcip, mcip->mci_flent, SRST_LINK); 7485 mac_fanout_setup(mcip, mcip->mci_flent, 7486 MCIP_RESOURCE_PROPS(mcip), mac_rx_deliver, mcip, NULL, 7487 NULL); 7488 mac_rx_group_unmark(tgrp, MR_INCIPIENT); 7489 } else { 7490 mac_rx_switch_grp_to_sw(tgrp); 7491 } 7492 7493 return (0); 7494 } 7495 7496 /* 7497 * Reserves a TX group for the specified share. Invoked by mac_tx_srs_setup() 7498 * when a share was allocated to the client. 7499 */ 7500 mac_group_t * 7501 mac_reserve_tx_group(mac_client_impl_t *mcip, boolean_t move) 7502 { 7503 mac_impl_t *mip = mcip->mci_mip; 7504 mac_group_t *grp = NULL; 7505 int rv; 7506 int i; 7507 int err; 7508 mac_group_t *defgrp; 7509 mac_share_handle_t share = mcip->mci_share; 7510 mac_resource_props_t *mrp = MCIP_RESOURCE_PROPS(mcip); 7511 int nrings; 7512 int defnrings; 7513 boolean_t need_exclgrp = B_FALSE; 7514 int need_rings = 0; 7515 mac_group_t *candidate_grp = NULL; 7516 mac_client_impl_t *gclient; 7517 mac_resource_props_t *gmrp; 7518 boolean_t txhw = mrp->mrp_mask & MRP_TX_RINGS; 7519 boolean_t unspec = mrp->mrp_mask & MRP_TXRINGS_UNSPEC; 7520 boolean_t isprimary; 7521 7522 isprimary = mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC; 7523 7524 /* 7525 * When we come here for a VLAN on the primary (dladm create-vlan), 7526 * we need to pair it along with the primary (to keep it consistent 7527 * with the RX side). So, we check if the primary is already assigned 7528 * to a group and return the group if so. The other way is also 7529 * true, i.e. the VLAN is already created and now we are plumbing 7530 * the primary. 7531 */ 7532 if (!move && isprimary) { 7533 for (gclient = mip->mi_clients_list; gclient != NULL; 7534 gclient = gclient->mci_client_next) { 7535 if (gclient->mci_flent->fe_type & FLOW_PRIMARY_MAC && 7536 gclient->mci_flent->fe_tx_ring_group != NULL) { 7537 return (gclient->mci_flent->fe_tx_ring_group); 7538 } 7539 } 7540 } 7541 7542 if (mip->mi_tx_groups == NULL || mip->mi_tx_group_count == 0) 7543 return (NULL); 7544 7545 /* For dynamic groups, default unspec to 1 */ 7546 if (txhw && unspec && 7547 mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC) { 7548 mrp->mrp_ntxrings = 1; 7549 } 7550 /* 7551 * For static grouping we allow only specifying rings=0 and 7552 * unspecified 7553 */ 7554 if (txhw && mrp->mrp_ntxrings > 0 && 7555 mip->mi_tx_group_type == MAC_GROUP_TYPE_STATIC) { 7556 return (NULL); 7557 } 7558 7559 if (txhw) { 7560 /* 7561 * We have explicitly asked for a group (with ntxrings, 7562 * if unspec). 7563 */ 7564 if (unspec || mrp->mrp_ntxrings > 0) { 7565 need_exclgrp = B_TRUE; 7566 need_rings = mrp->mrp_ntxrings; 7567 } else if (mrp->mrp_ntxrings == 0) { 7568 /* 7569 * We have asked for a software group. 7570 */ 7571 return (NULL); 7572 } 7573 } 7574 defgrp = MAC_DEFAULT_TX_GROUP(mip); 7575 /* 7576 * The number of rings that the default group can donate. 7577 * We need to leave at least one ring - the default ring - in 7578 * this group. 7579 */ 7580 defnrings = defgrp->mrg_cur_count - 1; 7581 7582 /* 7583 * Primary gets default group unless explicitly told not 7584 * to (i.e. rings > 0). 7585 */ 7586 if (isprimary && !need_exclgrp) 7587 return (NULL); 7588 7589 nrings = (mrp->mrp_mask & MRP_TX_RINGS) != 0 ? mrp->mrp_ntxrings : 1; 7590 for (i = 0; i < mip->mi_tx_group_count; i++) { 7591 grp = &mip->mi_tx_groups[i]; 7592 if ((grp->mrg_state == MAC_GROUP_STATE_RESERVED) || 7593 (grp->mrg_state == MAC_GROUP_STATE_UNINIT)) { 7594 /* 7595 * Select a candidate for replacement if we don't 7596 * get an exclusive group. A candidate group is one 7597 * that didn't ask for an exclusive group, but got 7598 * one and it has enough rings (combined with what 7599 * the default group can donate) for the new MAC 7600 * client. 7601 */ 7602 if (grp->mrg_state == MAC_GROUP_STATE_RESERVED && 7603 candidate_grp == NULL) { 7604 gclient = MAC_GROUP_ONLY_CLIENT(grp); 7605 VERIFY3P(gclient, !=, NULL); 7606 gmrp = MCIP_RESOURCE_PROPS(gclient); 7607 if (gclient->mci_share == 0 && 7608 (gmrp->mrp_mask & MRP_TX_RINGS) == 0 && 7609 (unspec || 7610 (grp->mrg_cur_count + defnrings) >= 7611 need_rings)) { 7612 candidate_grp = grp; 7613 } 7614 } 7615 continue; 7616 } 7617 /* 7618 * If the default can't donate let's just walk and 7619 * see if someone can vacate a group, so that we have 7620 * enough rings for this. 7621 */ 7622 if (mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC || 7623 nrings <= defnrings) { 7624 if (grp->mrg_state == MAC_GROUP_STATE_REGISTERED) { 7625 rv = mac_start_group(grp); 7626 ASSERT(rv == 0); 7627 } 7628 break; 7629 } 7630 } 7631 7632 /* The default group */ 7633 if (i >= mip->mi_tx_group_count) { 7634 /* 7635 * If we need an exclusive group and have identified a 7636 * candidate group we switch the MAC client from the 7637 * candidate group to the default group and give the 7638 * candidate group to this client. 7639 */ 7640 if (need_exclgrp && candidate_grp != NULL) { 7641 /* 7642 * Switch the MAC client from the candidate 7643 * group to the default group. We know the 7644 * candidate_grp came from a reserved group 7645 * and thus only has one client. 7646 */ 7647 grp = candidate_grp; 7648 gclient = MAC_GROUP_ONLY_CLIENT(grp); 7649 VERIFY3P(gclient, !=, NULL); 7650 mac_tx_client_quiesce((mac_client_handle_t)gclient); 7651 mac_tx_switch_group(gclient, grp, defgrp); 7652 mac_tx_client_restart((mac_client_handle_t)gclient); 7653 7654 /* 7655 * Give the candidate group with the specified number 7656 * of rings to this MAC client. 7657 */ 7658 ASSERT(grp->mrg_state == MAC_GROUP_STATE_REGISTERED); 7659 rv = mac_start_group(grp); 7660 ASSERT(rv == 0); 7661 7662 if (mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC) 7663 return (grp); 7664 7665 ASSERT(grp->mrg_cur_count == 0); 7666 ASSERT(defgrp->mrg_cur_count > need_rings); 7667 7668 err = i_mac_group_allocate_rings(mip, MAC_RING_TYPE_TX, 7669 defgrp, grp, share, need_rings); 7670 if (err == 0) { 7671 /* 7672 * For a share i_mac_group_allocate_rings gets 7673 * the rings from the driver, let's populate 7674 * the property for the client now. 7675 */ 7676 if (share != 0) { 7677 mac_client_set_rings( 7678 (mac_client_handle_t)mcip, -1, 7679 grp->mrg_cur_count); 7680 } 7681 mip->mi_tx_group_free--; 7682 return (grp); 7683 } 7684 DTRACE_PROBE3(tx__group__reserve__alloc__rings, char *, 7685 mip->mi_name, int, grp->mrg_index, int, err); 7686 mac_stop_group(grp); 7687 } 7688 return (NULL); 7689 } 7690 /* 7691 * We got an exclusive group, but it is not dynamic. 7692 */ 7693 if (mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC) { 7694 mip->mi_tx_group_free--; 7695 return (grp); 7696 } 7697 7698 rv = i_mac_group_allocate_rings(mip, MAC_RING_TYPE_TX, defgrp, grp, 7699 share, nrings); 7700 if (rv != 0) { 7701 DTRACE_PROBE3(tx__group__reserve__alloc__rings, 7702 char *, mip->mi_name, int, grp->mrg_index, int, rv); 7703 mac_stop_group(grp); 7704 return (NULL); 7705 } 7706 /* 7707 * For a share i_mac_group_allocate_rings gets the rings from the 7708 * driver, let's populate the property for the client now. 7709 */ 7710 if (share != 0) { 7711 mac_client_set_rings((mac_client_handle_t)mcip, -1, 7712 grp->mrg_cur_count); 7713 } 7714 mip->mi_tx_group_free--; 7715 return (grp); 7716 } 7717 7718 void 7719 mac_release_tx_group(mac_client_impl_t *mcip, mac_group_t *grp) 7720 { 7721 mac_impl_t *mip = mcip->mci_mip; 7722 mac_share_handle_t share = mcip->mci_share; 7723 mac_ring_t *ring; 7724 mac_soft_ring_set_t *srs = MCIP_TX_SRS(mcip); 7725 mac_group_t *defgrp; 7726 7727 defgrp = MAC_DEFAULT_TX_GROUP(mip); 7728 if (srs != NULL) { 7729 if (srs->srs_soft_ring_count > 0) { 7730 for (ring = grp->mrg_rings; ring != NULL; 7731 ring = ring->mr_next) { 7732 ASSERT(mac_tx_srs_ring_present(srs, ring)); 7733 mac_tx_invoke_callbacks(mcip, 7734 (mac_tx_cookie_t) 7735 mac_tx_srs_get_soft_ring(srs, ring)); 7736 mac_tx_srs_del_ring(srs, ring); 7737 } 7738 } else { 7739 ASSERT(srs->srs_tx.st_arg2 != NULL); 7740 srs->srs_tx.st_arg2 = NULL; 7741 mac_srs_stat_delete(srs); 7742 } 7743 } 7744 if (share != 0) 7745 mip->mi_share_capab.ms_sremove(share, grp->mrg_driver); 7746 7747 /* move the ring back to the pool */ 7748 if (mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC) { 7749 while ((ring = grp->mrg_rings) != NULL) 7750 (void) mac_group_mov_ring(mip, defgrp, ring); 7751 } 7752 mac_stop_group(grp); 7753 mip->mi_tx_group_free++; 7754 } 7755 7756 /* 7757 * Disassociate a MAC client from a group, i.e go through the rings in the 7758 * group and delete all the soft rings tied to them. 7759 */ 7760 static void 7761 mac_tx_dismantle_soft_rings(mac_group_t *fgrp, flow_entry_t *flent) 7762 { 7763 mac_client_impl_t *mcip = flent->fe_mcip; 7764 mac_soft_ring_set_t *tx_srs; 7765 mac_srs_tx_t *tx; 7766 mac_ring_t *ring; 7767 7768 tx_srs = flent->fe_tx_srs; 7769 tx = &tx_srs->srs_tx; 7770 7771 /* Single ring case we haven't created any soft rings */ 7772 if (tx->st_mode == SRS_TX_BW || tx->st_mode == SRS_TX_SERIALIZE || 7773 tx->st_mode == SRS_TX_DEFAULT) { 7774 tx->st_arg2 = NULL; 7775 mac_srs_stat_delete(tx_srs); 7776 /* Fanout case, where we have to dismantle the soft rings */ 7777 } else { 7778 for (ring = fgrp->mrg_rings; ring != NULL; 7779 ring = ring->mr_next) { 7780 ASSERT(mac_tx_srs_ring_present(tx_srs, ring)); 7781 mac_tx_invoke_callbacks(mcip, 7782 (mac_tx_cookie_t)mac_tx_srs_get_soft_ring(tx_srs, 7783 ring)); 7784 mac_tx_srs_del_ring(tx_srs, ring); 7785 } 7786 ASSERT(tx->st_arg2 == NULL); 7787 } 7788 } 7789 7790 /* 7791 * Switch the MAC client from one group to another. This means we need 7792 * to remove the MAC client, teardown the SRSs and revert the group state. 7793 * Then, we add the client to the destination roup, set the SRSs etc. 7794 */ 7795 void 7796 mac_tx_switch_group(mac_client_impl_t *mcip, mac_group_t *fgrp, 7797 mac_group_t *tgrp) 7798 { 7799 mac_client_impl_t *group_only_mcip; 7800 mac_impl_t *mip = mcip->mci_mip; 7801 flow_entry_t *flent = mcip->mci_flent; 7802 mac_group_t *defgrp; 7803 mac_grp_client_t *mgcp; 7804 mac_client_impl_t *gmcip; 7805 flow_entry_t *gflent; 7806 7807 defgrp = MAC_DEFAULT_TX_GROUP(mip); 7808 ASSERT(fgrp == flent->fe_tx_ring_group); 7809 7810 if (fgrp == defgrp) { 7811 /* 7812 * If this is the primary we need to find any VLANs on 7813 * the primary and move them too. 7814 */ 7815 mac_group_remove_client(fgrp, mcip); 7816 mac_tx_dismantle_soft_rings(fgrp, flent); 7817 if (mac_check_macaddr_shared(mcip->mci_unicast)) { 7818 mgcp = fgrp->mrg_clients; 7819 while (mgcp != NULL) { 7820 gmcip = mgcp->mgc_client; 7821 mgcp = mgcp->mgc_next; 7822 if (mcip->mci_unicast != gmcip->mci_unicast) 7823 continue; 7824 mac_tx_client_quiesce( 7825 (mac_client_handle_t)gmcip); 7826 7827 gflent = gmcip->mci_flent; 7828 mac_group_remove_client(fgrp, gmcip); 7829 mac_tx_dismantle_soft_rings(fgrp, gflent); 7830 7831 mac_group_add_client(tgrp, gmcip); 7832 gflent->fe_tx_ring_group = tgrp; 7833 /* We could directly set this to SHARED */ 7834 tgrp->mrg_state = mac_group_next_state(tgrp, 7835 &group_only_mcip, defgrp, B_FALSE); 7836 7837 mac_tx_srs_group_setup(gmcip, gflent, 7838 SRST_LINK); 7839 mac_fanout_setup(gmcip, gflent, 7840 MCIP_RESOURCE_PROPS(gmcip), mac_rx_deliver, 7841 gmcip, NULL, NULL); 7842 7843 mac_tx_client_restart( 7844 (mac_client_handle_t)gmcip); 7845 } 7846 } 7847 if (MAC_GROUP_NO_CLIENT(fgrp)) { 7848 mac_ring_t *ring; 7849 int cnt; 7850 int ringcnt; 7851 7852 fgrp->mrg_state = MAC_GROUP_STATE_REGISTERED; 7853 /* 7854 * Additionally, we also need to stop all 7855 * the rings in the default group, except 7856 * the default ring. The reason being 7857 * this group won't be released since it is 7858 * the default group, so the rings won't 7859 * be stopped otherwise. 7860 */ 7861 ringcnt = fgrp->mrg_cur_count; 7862 ring = fgrp->mrg_rings; 7863 for (cnt = 0; cnt < ringcnt; cnt++) { 7864 if (ring->mr_state == MR_INUSE && 7865 ring != 7866 (mac_ring_t *)mip->mi_default_tx_ring) { 7867 mac_stop_ring(ring); 7868 ring->mr_flag = 0; 7869 } 7870 ring = ring->mr_next; 7871 } 7872 } else if (MAC_GROUP_ONLY_CLIENT(fgrp) != NULL) { 7873 fgrp->mrg_state = MAC_GROUP_STATE_RESERVED; 7874 } else { 7875 ASSERT(fgrp->mrg_state == MAC_GROUP_STATE_SHARED); 7876 } 7877 } else { 7878 /* 7879 * We could have VLANs sharing the non-default group with 7880 * the primary. 7881 */ 7882 mgcp = fgrp->mrg_clients; 7883 while (mgcp != NULL) { 7884 gmcip = mgcp->mgc_client; 7885 mgcp = mgcp->mgc_next; 7886 if (gmcip == mcip) 7887 continue; 7888 mac_tx_client_quiesce((mac_client_handle_t)gmcip); 7889 gflent = gmcip->mci_flent; 7890 7891 mac_group_remove_client(fgrp, gmcip); 7892 mac_tx_dismantle_soft_rings(fgrp, gflent); 7893 7894 mac_group_add_client(tgrp, gmcip); 7895 gflent->fe_tx_ring_group = tgrp; 7896 /* We could directly set this to SHARED */ 7897 tgrp->mrg_state = mac_group_next_state(tgrp, 7898 &group_only_mcip, defgrp, B_FALSE); 7899 mac_tx_srs_group_setup(gmcip, gflent, SRST_LINK); 7900 mac_fanout_setup(gmcip, gflent, 7901 MCIP_RESOURCE_PROPS(gmcip), mac_rx_deliver, 7902 gmcip, NULL, NULL); 7903 7904 mac_tx_client_restart((mac_client_handle_t)gmcip); 7905 } 7906 mac_group_remove_client(fgrp, mcip); 7907 mac_release_tx_group(mcip, fgrp); 7908 fgrp->mrg_state = MAC_GROUP_STATE_REGISTERED; 7909 } 7910 7911 /* Add it to the tgroup */ 7912 mac_group_add_client(tgrp, mcip); 7913 flent->fe_tx_ring_group = tgrp; 7914 tgrp->mrg_state = mac_group_next_state(tgrp, &group_only_mcip, 7915 defgrp, B_FALSE); 7916 7917 mac_tx_srs_group_setup(mcip, flent, SRST_LINK); 7918 mac_fanout_setup(mcip, flent, MCIP_RESOURCE_PROPS(mcip), 7919 mac_rx_deliver, mcip, NULL, NULL); 7920 } 7921 7922 /* 7923 * This is a 1-time control path activity initiated by the client (IP). 7924 * The mac perimeter protects against other simultaneous control activities, 7925 * for example an ioctl that attempts to change the degree of fanout and 7926 * increase or decrease the number of softrings associated with this Tx SRS. 7927 */ 7928 static mac_tx_notify_cb_t * 7929 mac_client_tx_notify_add(mac_client_impl_t *mcip, 7930 mac_tx_notify_t notify, void *arg) 7931 { 7932 mac_cb_info_t *mcbi; 7933 mac_tx_notify_cb_t *mtnfp; 7934 7935 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip)); 7936 7937 mtnfp = kmem_zalloc(sizeof (mac_tx_notify_cb_t), KM_SLEEP); 7938 mtnfp->mtnf_fn = notify; 7939 mtnfp->mtnf_arg = arg; 7940 mtnfp->mtnf_link.mcb_objp = mtnfp; 7941 mtnfp->mtnf_link.mcb_objsize = sizeof (mac_tx_notify_cb_t); 7942 mtnfp->mtnf_link.mcb_flags = MCB_TX_NOTIFY_CB_T; 7943 7944 mcbi = &mcip->mci_tx_notify_cb_info; 7945 mutex_enter(mcbi->mcbi_lockp); 7946 mac_callback_add(mcbi, &mcip->mci_tx_notify_cb_list, &mtnfp->mtnf_link); 7947 mutex_exit(mcbi->mcbi_lockp); 7948 return (mtnfp); 7949 } 7950 7951 static void 7952 mac_client_tx_notify_remove(mac_client_impl_t *mcip, mac_tx_notify_cb_t *mtnfp) 7953 { 7954 mac_cb_info_t *mcbi; 7955 mac_cb_t **cblist; 7956 7957 ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip)); 7958 7959 if (!mac_callback_find(&mcip->mci_tx_notify_cb_info, 7960 &mcip->mci_tx_notify_cb_list, &mtnfp->mtnf_link)) { 7961 cmn_err(CE_WARN, 7962 "mac_client_tx_notify_remove: callback not " 7963 "found, mcip 0x%p mtnfp 0x%p", (void *)mcip, (void *)mtnfp); 7964 return; 7965 } 7966 7967 mcbi = &mcip->mci_tx_notify_cb_info; 7968 cblist = &mcip->mci_tx_notify_cb_list; 7969 mutex_enter(mcbi->mcbi_lockp); 7970 if (mac_callback_remove(mcbi, cblist, &mtnfp->mtnf_link)) 7971 kmem_free(mtnfp, sizeof (mac_tx_notify_cb_t)); 7972 else 7973 mac_callback_remove_wait(&mcip->mci_tx_notify_cb_info); 7974 mutex_exit(mcbi->mcbi_lockp); 7975 } 7976 7977 /* 7978 * mac_client_tx_notify(): 7979 * call to add and remove flow control callback routine. 7980 */ 7981 mac_tx_notify_handle_t 7982 mac_client_tx_notify(mac_client_handle_t mch, mac_tx_notify_t callb_func, 7983 void *ptr) 7984 { 7985 mac_client_impl_t *mcip = (mac_client_impl_t *)mch; 7986 mac_tx_notify_cb_t *mtnfp = NULL; 7987 7988 i_mac_perim_enter(mcip->mci_mip); 7989 7990 if (callb_func != NULL) { 7991 /* Add a notify callback */ 7992 mtnfp = mac_client_tx_notify_add(mcip, callb_func, ptr); 7993 } else { 7994 mac_client_tx_notify_remove(mcip, (mac_tx_notify_cb_t *)ptr); 7995 } 7996 i_mac_perim_exit(mcip->mci_mip); 7997 7998 return ((mac_tx_notify_handle_t)mtnfp); 7999 } 8000 8001 void 8002 mac_bridge_vectors(mac_bridge_tx_t txf, mac_bridge_rx_t rxf, 8003 mac_bridge_ref_t reff, mac_bridge_ls_t lsf) 8004 { 8005 mac_bridge_tx_cb = txf; 8006 mac_bridge_rx_cb = rxf; 8007 mac_bridge_ref_cb = reff; 8008 mac_bridge_ls_cb = lsf; 8009 } 8010 8011 int 8012 mac_bridge_set(mac_handle_t mh, mac_handle_t link) 8013 { 8014 mac_impl_t *mip = (mac_impl_t *)mh; 8015 int retv; 8016 8017 mutex_enter(&mip->mi_bridge_lock); 8018 if (mip->mi_bridge_link == NULL) { 8019 mip->mi_bridge_link = link; 8020 retv = 0; 8021 } else { 8022 retv = EBUSY; 8023 } 8024 mutex_exit(&mip->mi_bridge_lock); 8025 if (retv == 0) { 8026 mac_poll_state_change(mh, B_FALSE); 8027 mac_capab_update(mh); 8028 } 8029 return (retv); 8030 } 8031 8032 /* 8033 * Disable bridging on the indicated link. 8034 */ 8035 void 8036 mac_bridge_clear(mac_handle_t mh, mac_handle_t link) 8037 { 8038 mac_impl_t *mip = (mac_impl_t *)mh; 8039 8040 mutex_enter(&mip->mi_bridge_lock); 8041 ASSERT(mip->mi_bridge_link == link); 8042 mip->mi_bridge_link = NULL; 8043 mutex_exit(&mip->mi_bridge_lock); 8044 mac_poll_state_change(mh, B_TRUE); 8045 mac_capab_update(mh); 8046 } 8047 8048 void 8049 mac_no_active(mac_handle_t mh) 8050 { 8051 mac_impl_t *mip = (mac_impl_t *)mh; 8052 8053 i_mac_perim_enter(mip); 8054 mip->mi_state_flags |= MIS_NO_ACTIVE; 8055 i_mac_perim_exit(mip); 8056 } 8057 8058 /* 8059 * Walk the primary VLAN clients whenever the primary's rings property 8060 * changes and update the mac_resource_props_t for the VLAN's client. 8061 * We need to do this since we don't support setting these properties 8062 * on the primary's VLAN clients, but the VLAN clients have to 8063 * follow the primary w.r.t the rings property. 8064 */ 8065 void 8066 mac_set_prim_vlan_rings(mac_impl_t *mip, mac_resource_props_t *mrp) 8067 { 8068 mac_client_impl_t *vmcip; 8069 mac_resource_props_t *vmrp; 8070 8071 for (vmcip = mip->mi_clients_list; vmcip != NULL; 8072 vmcip = vmcip->mci_client_next) { 8073 if (!(vmcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) || 8074 mac_client_vid((mac_client_handle_t)vmcip) == 8075 VLAN_ID_NONE) { 8076 continue; 8077 } 8078 vmrp = MCIP_RESOURCE_PROPS(vmcip); 8079 8080 vmrp->mrp_nrxrings = mrp->mrp_nrxrings; 8081 if (mrp->mrp_mask & MRP_RX_RINGS) 8082 vmrp->mrp_mask |= MRP_RX_RINGS; 8083 else if (vmrp->mrp_mask & MRP_RX_RINGS) 8084 vmrp->mrp_mask &= ~MRP_RX_RINGS; 8085 8086 vmrp->mrp_ntxrings = mrp->mrp_ntxrings; 8087 if (mrp->mrp_mask & MRP_TX_RINGS) 8088 vmrp->mrp_mask |= MRP_TX_RINGS; 8089 else if (vmrp->mrp_mask & MRP_TX_RINGS) 8090 vmrp->mrp_mask &= ~MRP_TX_RINGS; 8091 8092 if (mrp->mrp_mask & MRP_RXRINGS_UNSPEC) 8093 vmrp->mrp_mask |= MRP_RXRINGS_UNSPEC; 8094 else 8095 vmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC; 8096 8097 if (mrp->mrp_mask & MRP_TXRINGS_UNSPEC) 8098 vmrp->mrp_mask |= MRP_TXRINGS_UNSPEC; 8099 else 8100 vmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC; 8101 } 8102 } 8103 8104 /* 8105 * We are adding or removing ring(s) from a group. The source for taking 8106 * rings is the default group. The destination for giving rings back is 8107 * the default group. 8108 */ 8109 int 8110 mac_group_ring_modify(mac_client_impl_t *mcip, mac_group_t *group, 8111 mac_group_t *defgrp) 8112 { 8113 mac_resource_props_t *mrp = MCIP_RESOURCE_PROPS(mcip); 8114 uint_t modify; 8115 int count; 8116 mac_ring_t *ring; 8117 mac_ring_t *next; 8118 mac_impl_t *mip = mcip->mci_mip; 8119 mac_ring_t **rings; 8120 uint_t ringcnt; 8121 int i = 0; 8122 boolean_t rx_group = group->mrg_type == MAC_RING_TYPE_RX; 8123 int start; 8124 int end; 8125 mac_group_t *tgrp; 8126 int j; 8127 int rv = 0; 8128 8129 /* 8130 * If we are asked for just a group, we give 1 ring, else 8131 * the specified number of rings. 8132 */ 8133 if (rx_group) { 8134 ringcnt = (mrp->mrp_mask & MRP_RXRINGS_UNSPEC) ? 1: 8135 mrp->mrp_nrxrings; 8136 } else { 8137 ringcnt = (mrp->mrp_mask & MRP_TXRINGS_UNSPEC) ? 1: 8138 mrp->mrp_ntxrings; 8139 } 8140 8141 /* don't allow modifying rings for a share for now. */ 8142 ASSERT(mcip->mci_share == 0); 8143 8144 if (ringcnt == group->mrg_cur_count) 8145 return (0); 8146 8147 if (group->mrg_cur_count > ringcnt) { 8148 modify = group->mrg_cur_count - ringcnt; 8149 if (rx_group) { 8150 if (mip->mi_rx_donor_grp == group) { 8151 ASSERT(mac_is_primary_client(mcip)); 8152 mip->mi_rx_donor_grp = defgrp; 8153 } else { 8154 defgrp = mip->mi_rx_donor_grp; 8155 } 8156 } 8157 ring = group->mrg_rings; 8158 rings = kmem_alloc(modify * sizeof (mac_ring_handle_t), 8159 KM_SLEEP); 8160 j = 0; 8161 for (count = 0; count < modify; count++) { 8162 next = ring->mr_next; 8163 rv = mac_group_mov_ring(mip, defgrp, ring); 8164 if (rv != 0) { 8165 /* cleanup on failure */ 8166 for (j = 0; j < count; j++) { 8167 (void) mac_group_mov_ring(mip, group, 8168 rings[j]); 8169 } 8170 break; 8171 } 8172 rings[j++] = ring; 8173 ring = next; 8174 } 8175 kmem_free(rings, modify * sizeof (mac_ring_handle_t)); 8176 return (rv); 8177 } 8178 if (ringcnt >= MAX_RINGS_PER_GROUP) 8179 return (EINVAL); 8180 8181 modify = ringcnt - group->mrg_cur_count; 8182 8183 if (rx_group) { 8184 if (group != mip->mi_rx_donor_grp) 8185 defgrp = mip->mi_rx_donor_grp; 8186 else 8187 /* 8188 * This is the donor group with all the remaining 8189 * rings. Default group now gets to be the donor 8190 */ 8191 mip->mi_rx_donor_grp = defgrp; 8192 start = 1; 8193 end = mip->mi_rx_group_count; 8194 } else { 8195 start = 0; 8196 end = mip->mi_tx_group_count - 1; 8197 } 8198 /* 8199 * If the default doesn't have any rings, lets see if we can 8200 * take rings given to an h/w client that doesn't need it. 8201 * For now, we just see if there is any one client that can donate 8202 * all the required rings. 8203 */ 8204 if (defgrp->mrg_cur_count < (modify + 1)) { 8205 for (i = start; i < end; i++) { 8206 if (rx_group) { 8207 tgrp = &mip->mi_rx_groups[i]; 8208 if (tgrp == group || tgrp->mrg_state < 8209 MAC_GROUP_STATE_RESERVED) { 8210 continue; 8211 } 8212 if (i_mac_clients_hw(tgrp, MRP_RX_RINGS)) 8213 continue; 8214 mcip = tgrp->mrg_clients->mgc_client; 8215 VERIFY3P(mcip, !=, NULL); 8216 if ((tgrp->mrg_cur_count + 8217 defgrp->mrg_cur_count) < (modify + 1)) { 8218 continue; 8219 } 8220 if (mac_rx_switch_group(mcip, tgrp, 8221 defgrp) != 0) { 8222 return (ENOSPC); 8223 } 8224 } else { 8225 tgrp = &mip->mi_tx_groups[i]; 8226 if (tgrp == group || tgrp->mrg_state < 8227 MAC_GROUP_STATE_RESERVED) { 8228 continue; 8229 } 8230 if (i_mac_clients_hw(tgrp, MRP_TX_RINGS)) 8231 continue; 8232 mcip = tgrp->mrg_clients->mgc_client; 8233 VERIFY3P(mcip, !=, NULL); 8234 if ((tgrp->mrg_cur_count + 8235 defgrp->mrg_cur_count) < (modify + 1)) { 8236 continue; 8237 } 8238 /* OK, we can switch this to s/w */ 8239 mac_tx_client_quiesce( 8240 (mac_client_handle_t)mcip); 8241 mac_tx_switch_group(mcip, tgrp, defgrp); 8242 mac_tx_client_restart( 8243 (mac_client_handle_t)mcip); 8244 } 8245 } 8246 if (defgrp->mrg_cur_count < (modify + 1)) 8247 return (ENOSPC); 8248 } 8249 if ((rv = i_mac_group_allocate_rings(mip, group->mrg_type, defgrp, 8250 group, mcip->mci_share, modify)) != 0) { 8251 return (rv); 8252 } 8253 return (0); 8254 } 8255 8256 /* 8257 * Given the poolname in mac_resource_props, find the cpupart 8258 * that is associated with this pool. The cpupart will be used 8259 * later for finding the cpus to be bound to the networking threads. 8260 * 8261 * use_default is set B_TRUE if pools are enabled and pool_default 8262 * is returned. This avoids a 2nd lookup to set the poolname 8263 * for pool-effective. 8264 * 8265 * returns: 8266 * 8267 * NULL - pools are disabled or if the 'cpus' property is set. 8268 * cpupart of pool_default - pools are enabled and the pool 8269 * is not available or poolname is blank 8270 * cpupart of named pool - pools are enabled and the pool 8271 * is available. 8272 */ 8273 cpupart_t * 8274 mac_pset_find(mac_resource_props_t *mrp, boolean_t *use_default) 8275 { 8276 pool_t *pool; 8277 cpupart_t *cpupart; 8278 8279 *use_default = B_FALSE; 8280 8281 /* CPUs property is set */ 8282 if (mrp->mrp_mask & MRP_CPUS) 8283 return (NULL); 8284 8285 ASSERT(pool_lock_held()); 8286 8287 /* Pools are disabled, no pset */ 8288 if (pool_state == POOL_DISABLED) 8289 return (NULL); 8290 8291 /* Pools property is set */ 8292 if (mrp->mrp_mask & MRP_POOL) { 8293 if ((pool = pool_lookup_pool_by_name(mrp->mrp_pool)) == NULL) { 8294 /* Pool not found */ 8295 DTRACE_PROBE1(mac_pset_find_no_pool, char *, 8296 mrp->mrp_pool); 8297 *use_default = B_TRUE; 8298 pool = pool_default; 8299 } 8300 /* Pools property is not set */ 8301 } else { 8302 *use_default = B_TRUE; 8303 pool = pool_default; 8304 } 8305 8306 /* Find the CPU pset that corresponds to the pool */ 8307 mutex_enter(&cpu_lock); 8308 if ((cpupart = cpupart_find(pool->pool_pset->pset_id)) == NULL) { 8309 DTRACE_PROBE1(mac_find_pset_no_pset, psetid_t, 8310 pool->pool_pset->pset_id); 8311 } 8312 mutex_exit(&cpu_lock); 8313 8314 return (cpupart); 8315 } 8316 8317 void 8318 mac_set_pool_effective(boolean_t use_default, cpupart_t *cpupart, 8319 mac_resource_props_t *mrp, mac_resource_props_t *emrp) 8320 { 8321 ASSERT(pool_lock_held()); 8322 8323 if (cpupart != NULL) { 8324 emrp->mrp_mask |= MRP_POOL; 8325 if (use_default) { 8326 (void) strcpy(emrp->mrp_pool, 8327 "pool_default"); 8328 } else { 8329 ASSERT(strlen(mrp->mrp_pool) != 0); 8330 (void) strcpy(emrp->mrp_pool, 8331 mrp->mrp_pool); 8332 } 8333 } else { 8334 emrp->mrp_mask &= ~MRP_POOL; 8335 bzero(emrp->mrp_pool, MAXPATHLEN); 8336 } 8337 } 8338 8339 struct mac_pool_arg { 8340 char mpa_poolname[MAXPATHLEN]; 8341 pool_event_t mpa_what; 8342 }; 8343 8344 /*ARGSUSED*/ 8345 static uint_t 8346 mac_pool_link_update(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 8347 { 8348 struct mac_pool_arg *mpa = arg; 8349 mac_impl_t *mip = (mac_impl_t *)val; 8350 mac_client_impl_t *mcip; 8351 mac_resource_props_t *mrp, *emrp; 8352 boolean_t pool_update = B_FALSE; 8353 boolean_t pool_clear = B_FALSE; 8354 boolean_t use_default = B_FALSE; 8355 cpupart_t *cpupart = NULL; 8356 8357 mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP); 8358 i_mac_perim_enter(mip); 8359 for (mcip = mip->mi_clients_list; mcip != NULL; 8360 mcip = mcip->mci_client_next) { 8361 pool_update = B_FALSE; 8362 pool_clear = B_FALSE; 8363 use_default = B_FALSE; 8364 mac_client_get_resources((mac_client_handle_t)mcip, mrp); 8365 emrp = MCIP_EFFECTIVE_PROPS(mcip); 8366 8367 /* 8368 * When pools are enabled 8369 */ 8370 if ((mpa->mpa_what == POOL_E_ENABLE) && 8371 ((mrp->mrp_mask & MRP_CPUS) == 0)) { 8372 mrp->mrp_mask |= MRP_POOL; 8373 pool_update = B_TRUE; 8374 } 8375 8376 /* 8377 * When pools are disabled 8378 */ 8379 if ((mpa->mpa_what == POOL_E_DISABLE) && 8380 ((mrp->mrp_mask & MRP_CPUS) == 0)) { 8381 mrp->mrp_mask |= MRP_POOL; 8382 pool_clear = B_TRUE; 8383 } 8384 8385 /* 8386 * Look for links with the pool property set and the poolname 8387 * matching the one which is changing. 8388 */ 8389 if (strcmp(mrp->mrp_pool, mpa->mpa_poolname) == 0) { 8390 /* 8391 * The pool associated with the link has changed. 8392 */ 8393 if (mpa->mpa_what == POOL_E_CHANGE) { 8394 mrp->mrp_mask |= MRP_POOL; 8395 pool_update = B_TRUE; 8396 } 8397 } 8398 8399 /* 8400 * This link is associated with pool_default and 8401 * pool_default has changed. 8402 */ 8403 if ((mpa->mpa_what == POOL_E_CHANGE) && 8404 (strcmp(emrp->mrp_pool, "pool_default") == 0) && 8405 (strcmp(mpa->mpa_poolname, "pool_default") == 0)) { 8406 mrp->mrp_mask |= MRP_POOL; 8407 pool_update = B_TRUE; 8408 } 8409 8410 /* 8411 * Get new list of cpus for the pool, bind network 8412 * threads to new list of cpus and update resources. 8413 */ 8414 if (pool_update) { 8415 if (MCIP_DATAPATH_SETUP(mcip)) { 8416 pool_lock(); 8417 cpupart = mac_pset_find(mrp, &use_default); 8418 mac_fanout_setup(mcip, mcip->mci_flent, mrp, 8419 mac_rx_deliver, mcip, NULL, cpupart); 8420 mac_set_pool_effective(use_default, cpupart, 8421 mrp, emrp); 8422 pool_unlock(); 8423 } 8424 mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), 8425 B_FALSE); 8426 } 8427 8428 /* 8429 * Clear the effective pool and bind network threads 8430 * to any available CPU. 8431 */ 8432 if (pool_clear) { 8433 if (MCIP_DATAPATH_SETUP(mcip)) { 8434 emrp->mrp_mask &= ~MRP_POOL; 8435 bzero(emrp->mrp_pool, MAXPATHLEN); 8436 mac_fanout_setup(mcip, mcip->mci_flent, mrp, 8437 mac_rx_deliver, mcip, NULL, NULL); 8438 } 8439 mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), 8440 B_FALSE); 8441 } 8442 } 8443 i_mac_perim_exit(mip); 8444 kmem_free(mrp, sizeof (*mrp)); 8445 return (MH_WALK_CONTINUE); 8446 } 8447 8448 static void 8449 mac_pool_update(void *arg) 8450 { 8451 mod_hash_walk(i_mac_impl_hash, mac_pool_link_update, arg); 8452 kmem_free(arg, sizeof (struct mac_pool_arg)); 8453 } 8454 8455 /* 8456 * Callback function to be executed when a noteworthy pool event 8457 * takes place. 8458 */ 8459 /* ARGSUSED */ 8460 static void 8461 mac_pool_event_cb(pool_event_t what, poolid_t id, void *arg) 8462 { 8463 pool_t *pool; 8464 char *poolname = NULL; 8465 struct mac_pool_arg *mpa; 8466 8467 pool_lock(); 8468 mpa = kmem_zalloc(sizeof (struct mac_pool_arg), KM_SLEEP); 8469 8470 switch (what) { 8471 case POOL_E_ENABLE: 8472 case POOL_E_DISABLE: 8473 break; 8474 8475 case POOL_E_CHANGE: 8476 pool = pool_lookup_pool_by_id(id); 8477 if (pool == NULL) { 8478 kmem_free(mpa, sizeof (struct mac_pool_arg)); 8479 pool_unlock(); 8480 return; 8481 } 8482 pool_get_name(pool, &poolname); 8483 (void) strlcpy(mpa->mpa_poolname, poolname, 8484 sizeof (mpa->mpa_poolname)); 8485 break; 8486 8487 default: 8488 kmem_free(mpa, sizeof (struct mac_pool_arg)); 8489 pool_unlock(); 8490 return; 8491 } 8492 pool_unlock(); 8493 8494 mpa->mpa_what = what; 8495 8496 mac_pool_update(mpa); 8497 } 8498 8499 /* 8500 * Set effective rings property. This could be called from datapath_setup/ 8501 * datapath_teardown or set-linkprop. 8502 * If the group is reserved we just go ahead and set the effective rings. 8503 * Additionally, for TX this could mean the default group has lost/gained 8504 * some rings, so if the default group is reserved, we need to adjust the 8505 * effective rings for the default group clients. For RX, if we are working 8506 * with the non-default group, we just need to reset the effective props 8507 * for the default group clients. 8508 */ 8509 void 8510 mac_set_rings_effective(mac_client_impl_t *mcip) 8511 { 8512 mac_impl_t *mip = mcip->mci_mip; 8513 mac_group_t *grp; 8514 mac_group_t *defgrp; 8515 flow_entry_t *flent = mcip->mci_flent; 8516 mac_resource_props_t *emrp = MCIP_EFFECTIVE_PROPS(mcip); 8517 mac_grp_client_t *mgcp; 8518 mac_client_impl_t *gmcip; 8519 8520 grp = flent->fe_rx_ring_group; 8521 if (grp != NULL) { 8522 defgrp = MAC_DEFAULT_RX_GROUP(mip); 8523 /* 8524 * If we have reserved a group, set the effective rings 8525 * to the ring count in the group. 8526 */ 8527 if (grp->mrg_state == MAC_GROUP_STATE_RESERVED) { 8528 emrp->mrp_mask |= MRP_RX_RINGS; 8529 emrp->mrp_nrxrings = grp->mrg_cur_count; 8530 } 8531 8532 /* 8533 * We go through the clients in the shared group and 8534 * reset the effective properties. It is possible this 8535 * might have already been done for some client (i.e. 8536 * if some client is being moved to a group that is 8537 * already shared). The case where the default group is 8538 * RESERVED is taken care of above (note in the RX side if 8539 * there is a non-default group, the default group is always 8540 * SHARED). 8541 */ 8542 if (grp != defgrp || grp->mrg_state == MAC_GROUP_STATE_SHARED) { 8543 if (grp->mrg_state == MAC_GROUP_STATE_SHARED) 8544 mgcp = grp->mrg_clients; 8545 else 8546 mgcp = defgrp->mrg_clients; 8547 while (mgcp != NULL) { 8548 gmcip = mgcp->mgc_client; 8549 emrp = MCIP_EFFECTIVE_PROPS(gmcip); 8550 if (emrp->mrp_mask & MRP_RX_RINGS) { 8551 emrp->mrp_mask &= ~MRP_RX_RINGS; 8552 emrp->mrp_nrxrings = 0; 8553 } 8554 mgcp = mgcp->mgc_next; 8555 } 8556 } 8557 } 8558 8559 /* Now the TX side */ 8560 grp = flent->fe_tx_ring_group; 8561 if (grp != NULL) { 8562 defgrp = MAC_DEFAULT_TX_GROUP(mip); 8563 8564 if (grp->mrg_state == MAC_GROUP_STATE_RESERVED) { 8565 emrp->mrp_mask |= MRP_TX_RINGS; 8566 emrp->mrp_ntxrings = grp->mrg_cur_count; 8567 } else if (grp->mrg_state == MAC_GROUP_STATE_SHARED) { 8568 mgcp = grp->mrg_clients; 8569 while (mgcp != NULL) { 8570 gmcip = mgcp->mgc_client; 8571 emrp = MCIP_EFFECTIVE_PROPS(gmcip); 8572 if (emrp->mrp_mask & MRP_TX_RINGS) { 8573 emrp->mrp_mask &= ~MRP_TX_RINGS; 8574 emrp->mrp_ntxrings = 0; 8575 } 8576 mgcp = mgcp->mgc_next; 8577 } 8578 } 8579 8580 /* 8581 * If the group is not the default group and the default 8582 * group is reserved, the ring count in the default group 8583 * might have changed, update it. 8584 */ 8585 if (grp != defgrp && 8586 defgrp->mrg_state == MAC_GROUP_STATE_RESERVED) { 8587 gmcip = MAC_GROUP_ONLY_CLIENT(defgrp); 8588 emrp = MCIP_EFFECTIVE_PROPS(gmcip); 8589 emrp->mrp_ntxrings = defgrp->mrg_cur_count; 8590 } 8591 } 8592 emrp = MCIP_EFFECTIVE_PROPS(mcip); 8593 } 8594 8595 /* 8596 * Check if the primary is in the default group. If so, see if we 8597 * can give it a an exclusive group now that another client is 8598 * being configured. We take the primary out of the default group 8599 * because the multicast/broadcast packets for the all the clients 8600 * will land in the default ring in the default group which means 8601 * any client in the default group, even if it is the only on in 8602 * the group, will lose exclusive access to the rings, hence 8603 * polling. 8604 */ 8605 mac_client_impl_t * 8606 mac_check_primary_relocation(mac_client_impl_t *mcip, boolean_t rxhw) 8607 { 8608 mac_impl_t *mip = mcip->mci_mip; 8609 mac_group_t *defgrp = MAC_DEFAULT_RX_GROUP(mip); 8610 flow_entry_t *flent = mcip->mci_flent; 8611 mac_resource_props_t *mrp = MCIP_RESOURCE_PROPS(mcip); 8612 uint8_t *mac_addr; 8613 mac_group_t *ngrp; 8614 8615 /* 8616 * Check if the primary is in the default group, if not 8617 * or if it is explicitly configured to be in the default 8618 * group OR set the RX rings property, return. 8619 */ 8620 if (flent->fe_rx_ring_group != defgrp || mrp->mrp_mask & MRP_RX_RINGS) 8621 return (NULL); 8622 8623 /* 8624 * If the new client needs an exclusive group and we 8625 * don't have another for the primary, return. 8626 */ 8627 if (rxhw && mip->mi_rxhwclnt_avail < 2) 8628 return (NULL); 8629 8630 mac_addr = flent->fe_flow_desc.fd_dst_mac; 8631 /* 8632 * We call this when we are setting up the datapath for 8633 * the first non-primary. 8634 */ 8635 ASSERT(mip->mi_nactiveclients == 2); 8636 8637 /* 8638 * OK, now we have the primary that needs to be relocated. 8639 */ 8640 ngrp = mac_reserve_rx_group(mcip, mac_addr, B_TRUE); 8641 if (ngrp == NULL) 8642 return (NULL); 8643 if (mac_rx_switch_group(mcip, defgrp, ngrp) != 0) { 8644 mac_stop_group(ngrp); 8645 return (NULL); 8646 } 8647 return (mcip); 8648 } 8649 8650 void 8651 mac_transceiver_init(mac_impl_t *mip) 8652 { 8653 if (mac_capab_get((mac_handle_t)mip, MAC_CAPAB_TRANSCEIVER, 8654 &mip->mi_transceiver)) { 8655 /* 8656 * The driver set a flag that we don't know about. In this case, 8657 * we need to warn about that case and ignore this capability. 8658 */ 8659 if (mip->mi_transceiver.mct_flags != 0) { 8660 dev_err(mip->mi_dip, CE_WARN, "driver set transceiver " 8661 "flags to invalid value: 0x%x, ignoring " 8662 "capability", mip->mi_transceiver.mct_flags); 8663 bzero(&mip->mi_transceiver, 8664 sizeof (mac_capab_transceiver_t)); 8665 } 8666 } else { 8667 bzero(&mip->mi_transceiver, 8668 sizeof (mac_capab_transceiver_t)); 8669 } 8670 } 8671 8672 int 8673 mac_transceiver_count(mac_handle_t mh, uint_t *countp) 8674 { 8675 mac_impl_t *mip = (mac_impl_t *)mh; 8676 8677 ASSERT(MAC_PERIM_HELD(mh)); 8678 8679 if (mip->mi_transceiver.mct_ntransceivers == 0) 8680 return (ENOTSUP); 8681 8682 *countp = mip->mi_transceiver.mct_ntransceivers; 8683 return (0); 8684 } 8685 8686 int 8687 mac_transceiver_info(mac_handle_t mh, uint_t tranid, boolean_t *present, 8688 boolean_t *usable) 8689 { 8690 int ret; 8691 mac_transceiver_info_t info; 8692 8693 mac_impl_t *mip = (mac_impl_t *)mh; 8694 8695 ASSERT(MAC_PERIM_HELD(mh)); 8696 8697 if (mip->mi_transceiver.mct_info == NULL || 8698 mip->mi_transceiver.mct_ntransceivers == 0) 8699 return (ENOTSUP); 8700 8701 if (tranid >= mip->mi_transceiver.mct_ntransceivers) 8702 return (EINVAL); 8703 8704 bzero(&info, sizeof (mac_transceiver_info_t)); 8705 if ((ret = mip->mi_transceiver.mct_info(mip->mi_driver, tranid, 8706 &info)) != 0) { 8707 return (ret); 8708 } 8709 8710 *present = info.mti_present; 8711 *usable = info.mti_usable; 8712 return (0); 8713 } 8714 8715 int 8716 mac_transceiver_read(mac_handle_t mh, uint_t tranid, uint_t page, void *buf, 8717 size_t nbytes, off_t offset, size_t *nread) 8718 { 8719 int ret; 8720 size_t nr; 8721 mac_impl_t *mip = (mac_impl_t *)mh; 8722 8723 ASSERT(MAC_PERIM_HELD(mh)); 8724 8725 if (mip->mi_transceiver.mct_read == NULL) 8726 return (ENOTSUP); 8727 8728 if (tranid >= mip->mi_transceiver.mct_ntransceivers) 8729 return (EINVAL); 8730 8731 /* 8732 * All supported pages today are 256 bytes wide. Make sure offset + 8733 * nbytes never exceeds that. 8734 */ 8735 if (offset < 0 || offset >= 256 || nbytes > 256 || 8736 offset + nbytes > 256) 8737 return (EINVAL); 8738 8739 if (nread == NULL) 8740 nread = &nr; 8741 ret = mip->mi_transceiver.mct_read(mip->mi_driver, tranid, page, buf, 8742 nbytes, offset, nread); 8743 if (ret == 0 && *nread > nbytes) { 8744 dev_err(mip->mi_dip, CE_PANIC, "driver wrote %lu bytes into " 8745 "%lu byte sized buffer, possible memory corruption", 8746 *nread, nbytes); 8747 } 8748 8749 return (ret); 8750 } 8751 8752 void 8753 mac_led_init(mac_impl_t *mip) 8754 { 8755 mip->mi_led_modes = MAC_LED_DEFAULT; 8756 8757 if (!mac_capab_get((mac_handle_t)mip, MAC_CAPAB_LED, &mip->mi_led)) { 8758 bzero(&mip->mi_led, sizeof (mac_capab_led_t)); 8759 return; 8760 } 8761 8762 if (mip->mi_led.mcl_flags != 0) { 8763 dev_err(mip->mi_dip, CE_WARN, "driver set led capability " 8764 "flags to invalid value: 0x%x, ignoring " 8765 "capability", mip->mi_transceiver.mct_flags); 8766 bzero(&mip->mi_led, sizeof (mac_capab_led_t)); 8767 return; 8768 } 8769 8770 if ((mip->mi_led.mcl_modes & ~MAC_LED_ALL) != 0) { 8771 dev_err(mip->mi_dip, CE_WARN, "driver set led capability " 8772 "supported modes to invalid value: 0x%x, ignoring " 8773 "capability", mip->mi_transceiver.mct_flags); 8774 bzero(&mip->mi_led, sizeof (mac_capab_led_t)); 8775 return; 8776 } 8777 } 8778 8779 int 8780 mac_led_get(mac_handle_t mh, mac_led_mode_t *supported, mac_led_mode_t *active) 8781 { 8782 mac_impl_t *mip = (mac_impl_t *)mh; 8783 8784 ASSERT(MAC_PERIM_HELD(mh)); 8785 8786 if (mip->mi_led.mcl_set == NULL) 8787 return (ENOTSUP); 8788 8789 *supported = mip->mi_led.mcl_modes; 8790 *active = mip->mi_led_modes; 8791 8792 return (0); 8793 } 8794 8795 /* 8796 * Update and multiplex the various LED requests. We only ever send one LED to 8797 * the underlying driver at a time. As such, we end up multiplexing all 8798 * requested states and picking one to send down to the driver. 8799 */ 8800 int 8801 mac_led_set(mac_handle_t mh, mac_led_mode_t desired) 8802 { 8803 int ret; 8804 mac_led_mode_t driver; 8805 8806 mac_impl_t *mip = (mac_impl_t *)mh; 8807 8808 ASSERT(MAC_PERIM_HELD(mh)); 8809 8810 /* 8811 * If we've been passed a desired value of zero, that indicates that 8812 * we're basically resetting to the value of zero, which is our default 8813 * value. 8814 */ 8815 if (desired == 0) 8816 desired = MAC_LED_DEFAULT; 8817 8818 if (mip->mi_led.mcl_set == NULL) 8819 return (ENOTSUP); 8820 8821 /* 8822 * Catch both values that we don't know about and those that the driver 8823 * doesn't support. 8824 */ 8825 if ((desired & ~MAC_LED_ALL) != 0) 8826 return (EINVAL); 8827 8828 if ((desired & ~mip->mi_led.mcl_modes) != 0) 8829 return (ENOTSUP); 8830 8831 /* 8832 * If we have the same value, then there is nothing to do. 8833 */ 8834 if (desired == mip->mi_led_modes) 8835 return (0); 8836 8837 /* 8838 * Based on the desired value, determine what to send to the driver. We 8839 * only will send a single bit to the driver at any given time. IDENT 8840 * takes priority over OFF or ON. We also let OFF take priority over the 8841 * rest. 8842 */ 8843 if (desired & MAC_LED_IDENT) { 8844 driver = MAC_LED_IDENT; 8845 } else if (desired & MAC_LED_OFF) { 8846 driver = MAC_LED_OFF; 8847 } else if (desired & MAC_LED_ON) { 8848 driver = MAC_LED_ON; 8849 } else { 8850 driver = MAC_LED_DEFAULT; 8851 } 8852 8853 if ((ret = mip->mi_led.mcl_set(mip->mi_driver, driver, 0)) == 0) { 8854 mip->mi_led_modes = desired; 8855 } 8856 8857 return (ret); 8858 } 8859 8860 /* 8861 * Send packets through the Tx ring ('mrh') or through the default 8862 * handler if no ring is specified. Before passing the packet down to 8863 * the MAC provider, emulate any hardware offloads which have been 8864 * requested but are not supported by the provider. 8865 */ 8866 mblk_t * 8867 mac_ring_tx(mac_handle_t mh, mac_ring_handle_t mrh, mblk_t *mp) 8868 { 8869 mac_impl_t *mip = (mac_impl_t *)mh; 8870 8871 if (mrh == NULL) 8872 mrh = mip->mi_default_tx_ring; 8873 8874 if (mrh == NULL) 8875 return (mip->mi_tx(mip->mi_driver, mp)); 8876 else 8877 return (mac_hwring_tx(mrh, mp)); 8878 } 8879 8880 /* 8881 * This is the final stop before reaching the underlying MAC provider. 8882 * This is also where the bridging hook is inserted. Packets that are 8883 * bridged will return through mac_bridge_tx(), with rh nulled out if 8884 * the bridge chooses to send output on a different link due to 8885 * forwarding. 8886 */ 8887 mblk_t * 8888 mac_provider_tx(mac_impl_t *mip, mac_ring_handle_t rh, mblk_t *mp, 8889 mac_client_impl_t *mcip) 8890 { 8891 /* 8892 * If there is a bound Hybrid I/O share, send packets through 8893 * the default tx ring. When there's a bound Hybrid I/O share, 8894 * the tx rings of this client are mapped in the guest domain 8895 * and not accessible from here. 8896 */ 8897 if (mcip->mci_state_flags & MCIS_SHARE_BOUND) 8898 rh = mip->mi_default_tx_ring; 8899 8900 if (mip->mi_promisc_list != NULL) 8901 mac_promisc_dispatch(mip, mp, mcip, B_FALSE); 8902 8903 if (mip->mi_bridge_link == NULL) 8904 return (mac_ring_tx((mac_handle_t)mip, rh, mp)); 8905 else 8906 return (mac_bridge_tx(mip, rh, mp)); 8907 } 8908