xref: /onnv-gate/usr/src/uts/common/io/mac/mac.c (revision 13123:9efd3d43accd)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51852Syz147064  * Common Development and Distribution License (the "License").
61852Syz147064  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
215084Sjohnlev 
220Sstevel@tonic-gate /*
2312850SPrakash.Jalan@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * MAC Services Module
288275SEric Cheng  *
298275SEric Cheng  * The GLDv3 framework locking -  The MAC layer
308275SEric Cheng  * --------------------------------------------
318275SEric Cheng  *
328275SEric Cheng  * The MAC layer is central to the GLD framework and can provide the locking
338275SEric Cheng  * framework needed for itself and for the use of MAC clients. MAC end points
348275SEric Cheng  * are fairly disjoint and don't share a lot of state. So a coarse grained
358275SEric Cheng  * multi-threading scheme is to single thread all create/modify/delete or set
368275SEric Cheng  * type of control operations on a per mac end point while allowing data threads
378275SEric Cheng  * concurrently.
388275SEric Cheng  *
398275SEric Cheng  * Control operations (set) that modify a mac end point are always serialized on
408275SEric Cheng  * a per mac end point basis, We have at most 1 such thread per mac end point
418275SEric Cheng  * at a time.
428275SEric Cheng  *
438275SEric Cheng  * All other operations that are not serialized are essentially multi-threaded.
448275SEric Cheng  * For example a control operation (get) like getting statistics which may not
458275SEric Cheng  * care about reading values atomically or data threads sending or receiving
468275SEric Cheng  * data. Mostly these type of operations don't modify the control state. Any
478275SEric Cheng  * state these operations care about are protected using traditional locks.
488275SEric Cheng  *
498275SEric Cheng  * The perimeter only serializes serial operations. It does not imply there
508275SEric Cheng  * aren't any other concurrent operations. However a serialized operation may
518275SEric Cheng  * sometimes need to make sure it is the only thread. In this case it needs
528275SEric Cheng  * to use reference counting mechanisms to cv_wait until any current data
538275SEric Cheng  * threads are done.
548275SEric Cheng  *
558275SEric Cheng  * The mac layer itself does not hold any locks across a call to another layer.
568275SEric Cheng  * The perimeter is however held across a down call to the driver to make the
578275SEric Cheng  * whole control operation atomic with respect to other control operations.
588275SEric Cheng  * Also the data path and get type control operations may proceed concurrently.
598275SEric Cheng  * These operations synchronize with the single serial operation on a given mac
608275SEric Cheng  * end point using regular locks. The perimeter ensures that conflicting
618275SEric Cheng  * operations like say a mac_multicast_add and a mac_multicast_remove on the
628275SEric Cheng  * same mac end point don't interfere with each other and also ensures that the
638275SEric Cheng  * changes in the mac layer and the call to the underlying driver to say add a
648275SEric Cheng  * multicast address are done atomically without interference from a thread
658275SEric Cheng  * trying to delete the same address.
668275SEric Cheng  *
678275SEric Cheng  * For example, consider
688275SEric Cheng  * mac_multicst_add()
698275SEric Cheng  * {
708275SEric Cheng  *	mac_perimeter_enter();	serialize all control operations
718275SEric Cheng  *
728275SEric Cheng  *	grab list lock		protect against access by data threads
738275SEric Cheng  *	add to list
748275SEric Cheng  *	drop list lock
758275SEric Cheng  *
768275SEric Cheng  *	call driver's mi_multicst
778275SEric Cheng  *
788275SEric Cheng  *	mac_perimeter_exit();
798275SEric Cheng  * }
808275SEric Cheng  *
818275SEric Cheng  * To lessen the number of serialization locks and simplify the lock hierarchy,
828275SEric Cheng  * we serialize all the control operations on a per mac end point by using a
838275SEric Cheng  * single serialization lock called the perimeter. We allow recursive entry into
848275SEric Cheng  * the perimeter to facilitate use of this mechanism by both the mac client and
858275SEric Cheng  * the MAC layer itself.
868275SEric Cheng  *
878275SEric Cheng  * MAC client means an entity that does an operation on a mac handle
888275SEric Cheng  * obtained from a mac_open/mac_client_open. Similarly MAC driver means
898275SEric Cheng  * an entity that does an operation on a mac handle obtained from a
908275SEric Cheng  * mac_register. An entity could be both client and driver but on different
918275SEric Cheng  * handles eg. aggr. and should only make the corresponding mac interface calls
928275SEric Cheng  * i.e. mac driver interface or mac client interface as appropriate for that
938275SEric Cheng  * mac handle.
948275SEric Cheng  *
958275SEric Cheng  * General rules.
968275SEric Cheng  * -------------
978275SEric Cheng  *
988275SEric Cheng  * R1. The lock order of upcall threads is natually opposite to downcall
998275SEric Cheng  * threads. Hence upcalls must not hold any locks across layers for fear of
1008275SEric Cheng  * recursive lock enter and lock order violation. This applies to all layers.
1018275SEric Cheng  *
1028275SEric Cheng  * R2. The perimeter is just another lock. Since it is held in the down
1038275SEric Cheng  * direction, acquiring the perimeter in an upcall is prohibited as it would
1048275SEric Cheng  * cause a deadlock. This applies to all layers.
1058275SEric Cheng  *
1068275SEric Cheng  * Note that upcalls that need to grab the mac perimeter (for example
1078275SEric Cheng  * mac_notify upcalls) can still achieve that by posting the request to a
1088275SEric Cheng  * thread, which can then grab all the required perimeters and locks in the
1098275SEric Cheng  * right global order. Note that in the above example the mac layer iself
1108275SEric Cheng  * won't grab the mac perimeter in the mac_notify upcall, instead the upcall
1118275SEric Cheng  * to the client must do that. Please see the aggr code for an example.
1128275SEric Cheng  *
1138275SEric Cheng  * MAC client rules
1148275SEric Cheng  * ----------------
1158275SEric Cheng  *
1168275SEric Cheng  * R3. A MAC client may use the MAC provided perimeter facility to serialize
1178275SEric Cheng  * control operations on a per mac end point. It does this by by acquring
1188275SEric Cheng  * and holding the perimeter across a sequence of calls to the mac layer.
1198275SEric Cheng  * This ensures atomicity across the entire block of mac calls. In this
1208275SEric Cheng  * model the MAC client must not hold any client locks across the calls to
1218275SEric Cheng  * the mac layer. This model is the preferred solution.
1228275SEric Cheng  *
1238275SEric Cheng  * R4. However if a MAC client has a lot of global state across all mac end
1248275SEric Cheng  * points the per mac end point serialization may not be sufficient. In this
1258275SEric Cheng  * case the client may choose to use global locks or use its own serialization.
1268275SEric Cheng  * To avoid deadlocks, these client layer locks held across the mac calls
1278275SEric Cheng  * in the control path must never be acquired by the data path for the reason
1288275SEric Cheng  * mentioned below.
1298275SEric Cheng  *
1308275SEric Cheng  * (Assume that a control operation that holds a client lock blocks in the
1318275SEric Cheng  * mac layer waiting for upcall reference counts to drop to zero. If an upcall
1328275SEric Cheng  * data thread that holds this reference count, tries to acquire the same
1338275SEric Cheng  * client lock subsequently it will deadlock).
1348275SEric Cheng  *
1358275SEric Cheng  * A MAC client may follow either the R3 model or the R4 model, but can't
1368275SEric Cheng  * mix both. In the former, the hierarchy is Perim -> client locks, but in
1378275SEric Cheng  * the latter it is client locks -> Perim.
1388275SEric Cheng  *
1398275SEric Cheng  * R5. MAC clients must make MAC calls (excluding data calls) in a cv_wait'able
1408275SEric Cheng  * context since they may block while trying to acquire the perimeter.
1418275SEric Cheng  * In addition some calls may block waiting for upcall refcnts to come down to
1428275SEric Cheng  * zero.
1438275SEric Cheng  *
1448275SEric Cheng  * R6. MAC clients must make sure that they are single threaded and all threads
1458275SEric Cheng  * from the top (in particular data threads) have finished before calling
1468275SEric Cheng  * mac_client_close. The MAC framework does not track the number of client
1478275SEric Cheng  * threads using the mac client handle. Also mac clients must make sure
1488275SEric Cheng  * they have undone all the control operations before calling mac_client_close.
1498275SEric Cheng  * For example mac_unicast_remove/mac_multicast_remove to undo the corresponding
1508275SEric Cheng  * mac_unicast_add/mac_multicast_add.
1518275SEric Cheng  *
1528275SEric Cheng  * MAC framework rules
1538275SEric Cheng  * -------------------
1548275SEric Cheng  *
1558275SEric Cheng  * R7. The mac layer itself must not hold any mac layer locks (except the mac
1568275SEric Cheng  * perimeter) across a call to any other layer from the mac layer. The call to
1578275SEric Cheng  * any other layer could be via mi_* entry points, classifier entry points into
1588275SEric Cheng  * the driver or via upcall pointers into layers above. The mac perimeter may
1598275SEric Cheng  * be acquired or held only in the down direction, for e.g. when calling into
1608275SEric Cheng  * a mi_* driver enty point to provide atomicity of the operation.
1618275SEric Cheng  *
1628275SEric Cheng  * R8. Since it is not guaranteed (see R14) that drivers won't hold locks across
1638275SEric Cheng  * mac driver interfaces, the MAC layer must provide a cut out for control
1648275SEric Cheng  * interfaces like upcall notifications and start them in a separate thread.
1658275SEric Cheng  *
1668275SEric Cheng  * R9. Note that locking order also implies a plumbing order. For example
1678275SEric Cheng  * VNICs are allowed to be created over aggrs, but not vice-versa. An attempt
1688275SEric Cheng  * to plumb in any other order must be failed at mac_open time, otherwise it
1698275SEric Cheng  * could lead to deadlocks due to inverse locking order.
1708275SEric Cheng  *
1718275SEric Cheng  * R10. MAC driver interfaces must not block since the driver could call them
1728275SEric Cheng  * in interrupt context.
1738275SEric Cheng  *
1748275SEric Cheng  * R11. Walkers must preferably not hold any locks while calling walker
1758275SEric Cheng  * callbacks. Instead these can operate on reference counts. In simple
1768275SEric Cheng  * callbacks it may be ok to hold a lock and call the callbacks, but this is
1778275SEric Cheng  * harder to maintain in the general case of arbitrary callbacks.
1788275SEric Cheng  *
1798275SEric Cheng  * R12. The MAC layer must protect upcall notification callbacks using reference
1808275SEric Cheng  * counts rather than holding locks across the callbacks.
1818275SEric Cheng  *
1828275SEric Cheng  * R13. Given the variety of drivers, it is preferable if the MAC layer can make
1838275SEric Cheng  * sure that any pointers (such as mac ring pointers) it passes to the driver
1848275SEric Cheng  * remain valid until mac unregister time. Currently the mac layer achieves
1858275SEric Cheng  * this by using generation numbers for rings and freeing the mac rings only
1868275SEric Cheng  * at unregister time.  The MAC layer must provide a layer of indirection and
1878275SEric Cheng  * must not expose underlying driver rings or driver data structures/pointers
1888275SEric Cheng  * directly to MAC clients.
1898275SEric Cheng  *
1908275SEric Cheng  * MAC driver rules
1918275SEric Cheng  * ----------------
1928275SEric Cheng  *
1938275SEric Cheng  * R14. It would be preferable if MAC drivers don't hold any locks across any
1948275SEric Cheng  * mac call. However at a minimum they must not hold any locks across data
1958275SEric Cheng  * upcalls. They must also make sure that all references to mac data structures
1968275SEric Cheng  * are cleaned up and that it is single threaded at mac_unregister time.
1978275SEric Cheng  *
1988275SEric Cheng  * R15. MAC driver interfaces don't block and so the action may be done
1998275SEric Cheng  * asynchronously in a separate thread as for example handling notifications.
2008275SEric Cheng  * The driver must not assume that the action is complete when the call
2018275SEric Cheng  * returns.
2028275SEric Cheng  *
2038275SEric Cheng  * R16. Drivers must maintain a generation number per Rx ring, and pass it
2048275SEric Cheng  * back to mac_rx_ring(); They are expected to increment the generation
2058275SEric Cheng  * number whenever the ring's stop routine is invoked.
2068275SEric Cheng  * See comments in mac_rx_ring();
2078275SEric Cheng  *
2088275SEric Cheng  * R17 Similarly mi_stop is another synchronization point and the driver must
2098275SEric Cheng  * ensure that all upcalls are done and there won't be any future upcall
2108275SEric Cheng  * before returning from mi_stop.
2118275SEric Cheng  *
2128275SEric Cheng  * R18. The driver may assume that all set/modify control operations via
2138275SEric Cheng  * the mi_* entry points are single threaded on a per mac end point.
2148275SEric Cheng  *
2158275SEric Cheng  * Lock and Perimeter hierarchy scenarios
2168275SEric Cheng  * ---------------------------------------
2178275SEric Cheng  *
2188275SEric Cheng  * i_mac_impl_lock -> mi_rw_lock -> srs_lock -> s_ring_lock[i_mac_tx_srs_notify]
2198275SEric Cheng  *
2208275SEric Cheng  * ft_lock -> fe_lock [mac_flow_lookup]
2218275SEric Cheng  *
2228275SEric Cheng  * mi_rw_lock -> fe_lock [mac_bcast_send]
2238275SEric Cheng  *
2248275SEric Cheng  * srs_lock -> mac_bw_lock [mac_rx_srs_drain_bw]
2258275SEric Cheng  *
2268275SEric Cheng  * cpu_lock -> mac_srs_g_lock -> srs_lock -> s_ring_lock [mac_walk_srs_and_bind]
2278275SEric Cheng  *
2288275SEric Cheng  * i_dls_devnet_lock -> mac layer locks [dls_devnet_rename]
2298275SEric Cheng  *
2308275SEric Cheng  * Perimeters are ordered P1 -> P2 -> P3 from top to bottom in order of mac
2318275SEric Cheng  * client to driver. In the case of clients that explictly use the mac provided
2328275SEric Cheng  * perimeter mechanism for its serialization, the hierarchy is
2338275SEric Cheng  * Perimeter -> mac layer locks, since the client never holds any locks across
2348275SEric Cheng  * the mac calls. In the case of clients that use its own locks the hierarchy
2358275SEric Cheng  * is Client locks -> Mac Perim -> Mac layer locks. The client never explicitly
2368275SEric Cheng  * calls mac_perim_enter/exit in this case.
2378275SEric Cheng  *
2388275SEric Cheng  * Subflow creation rules
2398275SEric Cheng  * ---------------------------
2408275SEric Cheng  * o In case of a user specified cpulist present on underlying link and flows,
2418275SEric Cheng  * the flows cpulist must be a subset of the underlying link.
2428275SEric Cheng  * o In case of a user specified fanout mode present on link and flow, the
2438275SEric Cheng  * subflow fanout count has to be less than or equal to that of the
2448275SEric Cheng  * underlying link. The cpu-bindings for the subflows will be a subset of
2458275SEric Cheng  * the underlying link.
2468275SEric Cheng  * o In case if no cpulist specified on both underlying link and flow, the
2478275SEric Cheng  * underlying link relies on a  MAC tunable to provide out of box fanout.
2488275SEric Cheng  * The subflow will have no cpulist (the subflow will be unbound)
2498275SEric Cheng  * o In case if no cpulist is specified on the underlying link, a subflow can
2508275SEric Cheng  * carry  either a user-specified cpulist or fanout count. The cpu-bindings
2518275SEric Cheng  * for the subflow will not adhere to restriction that they need to be subset
2528275SEric Cheng  * of the underlying link.
2538275SEric Cheng  * o In case where the underlying link is carrying either a user specified
2548275SEric Cheng  * cpulist or fanout mode and for a unspecified subflow, the subflow will be
2558275SEric Cheng  * created unbound.
2568275SEric Cheng  * o While creating unbound subflows, bandwidth mode changes attempt to
2578275SEric Cheng  * figure a right fanout count. In such cases the fanout count will override
2588275SEric Cheng  * the unbound cpu-binding behavior.
2598275SEric Cheng  * o In addition to this, while cycling between flow and link properties, we
2608275SEric Cheng  * impose a restriction that if a link property has a subflow with
2618275SEric Cheng  * user-specified attributes, we will not allow changing the link property.
2628275SEric Cheng  * The administrator needs to reset all the user specified properties for the
2638275SEric Cheng  * subflows before attempting a link property change.
2648275SEric Cheng  * Some of the above rules can be overridden by specifying additional command
2658275SEric Cheng  * line options while creating or modifying link or subflow properties.
2660Sstevel@tonic-gate  */
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate #include <sys/types.h>
2690Sstevel@tonic-gate #include <sys/conf.h>
2705895Syz147064 #include <sys/id_space.h>
2716077Syz147064 #include <sys/esunddi.h>
2720Sstevel@tonic-gate #include <sys/stat.h>
2735895Syz147064 #include <sys/mkdev.h>
2740Sstevel@tonic-gate #include <sys/stream.h>
2750Sstevel@tonic-gate #include <sys/strsun.h>
2760Sstevel@tonic-gate #include <sys/strsubr.h>
2770Sstevel@tonic-gate #include <sys/dlpi.h>
27813109Smichael.l.lim@oracle.com #include <sys/list.h>
2798275SEric Cheng #include <sys/modhash.h>
2808275SEric Cheng #include <sys/mac_provider.h>
2818275SEric Cheng #include <sys/mac_client_impl.h>
2828275SEric Cheng #include <sys/mac_soft_ring.h>
28311878SVenu.Iyer@Sun.COM #include <sys/mac_stat.h>
2848275SEric Cheng #include <sys/mac_impl.h>
2858275SEric Cheng #include <sys/mac.h>
2865895Syz147064 #include <sys/dls.h>
287269Sericheng #include <sys/dld.h>
2882311Sseb #include <sys/modctl.h>
2893448Sdh155122 #include <sys/fs/dv_node.h>
2905009Sgd78059 #include <sys/thread.h>
2915009Sgd78059 #include <sys/proc.h>
2925009Sgd78059 #include <sys/callb.h>
2935009Sgd78059 #include <sys/cpuvar.h>
2943288Sseb #include <sys/atomic.h>
2958275SEric Cheng #include <sys/bitmap.h>
2964913Sethindra #include <sys/sdt.h>
2978275SEric Cheng #include <sys/mac_flow.h>
2988275SEric Cheng #include <sys/ddi_intr_impl.h>
2998275SEric Cheng #include <sys/disp.h>
3008275SEric Cheng #include <sys/sdt.h>
3018275SEric Cheng #include <sys/vnic.h>
3028275SEric Cheng #include <sys/vnic_impl.h>
3038275SEric Cheng #include <sys/vlan.h>
3048275SEric Cheng #include <inet/ip.h>
3058275SEric Cheng #include <inet/ip6.h>
3068275SEric Cheng #include <sys/exacct.h>
3078275SEric Cheng #include <sys/exacct_impl.h>
3085903Ssowmini #include <inet/nd.h>
3096512Ssowmini #include <sys/ethernet.h>
31011878SVenu.Iyer@Sun.COM #include <sys/pool.h>
31111878SVenu.Iyer@Sun.COM #include <sys/pool_pset.h>
31211878SVenu.Iyer@Sun.COM #include <sys/cpupart.h>
31311878SVenu.Iyer@Sun.COM #include <inet/wifi_ioctl.h>
31411878SVenu.Iyer@Sun.COM #include <net/wpa.h>
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate #define	IMPL_HASHSZ	67	/* prime */
3170Sstevel@tonic-gate 
31813109Smichael.l.lim@oracle.com kmem_cache_t		*i_mac_impl_cachep;
3198275SEric Cheng mod_hash_t		*i_mac_impl_hash;
320269Sericheng krwlock_t		i_mac_impl_lock;
321269Sericheng uint_t			i_mac_impl_count;
3228275SEric Cheng static kmem_cache_t	*mac_ring_cache;
3235895Syz147064 static id_space_t	*minor_ids;
3245895Syz147064 static uint32_t		minor_count;
32511878SVenu.Iyer@Sun.COM static pool_event_cb_t	mac_pool_event_reg;
3260Sstevel@tonic-gate 
3278275SEric Cheng /*
3288275SEric Cheng  * Logging stuff. Perhaps mac_logging_interval could be broken into
3298275SEric Cheng  * mac_flow_log_interval and mac_link_log_interval if we want to be
3308275SEric Cheng  * able to schedule them differently.
3318275SEric Cheng  */
3328275SEric Cheng uint_t			mac_logging_interval;
3338275SEric Cheng boolean_t		mac_flow_log_enable;
3348275SEric Cheng boolean_t		mac_link_log_enable;
3358275SEric Cheng timeout_id_t		mac_logging_timer;
3368275SEric Cheng 
3378275SEric Cheng /* for debugging, see MAC_DBG_PRT() in mac_impl.h */
3388275SEric Cheng int mac_dbg = 0;
3398275SEric Cheng 
3402311Sseb #define	MACTYPE_KMODDIR	"mac"
3412311Sseb #define	MACTYPE_HASHSZ	67
3422311Sseb static mod_hash_t	*i_mactype_hash;
3433288Sseb /*
3443288Sseb  * i_mactype_lock synchronizes threads that obtain references to mactype_t
3453288Sseb  * structures through i_mactype_getplugin().
3463288Sseb  */
3473288Sseb static kmutex_t		i_mactype_lock;
3482311Sseb 
3490Sstevel@tonic-gate /*
3508275SEric Cheng  * mac_tx_percpu_cnt
3518275SEric Cheng  *
3528275SEric Cheng  * Number of per cpu locks per mac_client_impl_t. Used by the transmit side
3538275SEric Cheng  * in mac_tx to reduce lock contention. This is sized at boot time in mac_init.
3548275SEric Cheng  * mac_tx_percpu_cnt_max is settable in /etc/system and must be a power of 2.
3558275SEric Cheng  * Per cpu locks may be disabled by setting mac_tx_percpu_cnt_max to 1.
3565084Sjohnlev  */
3578275SEric Cheng int mac_tx_percpu_cnt;
3588275SEric Cheng int mac_tx_percpu_cnt_max = 128;
3598275SEric Cheng 
36010491SRishi.Srivatsavai@Sun.COM /*
36110491SRishi.Srivatsavai@Sun.COM  * Call back functions for the bridge module.  These are guaranteed to be valid
36210491SRishi.Srivatsavai@Sun.COM  * when holding a reference on a link or when holding mip->mi_bridge_lock and
36310491SRishi.Srivatsavai@Sun.COM  * mi_bridge_link is non-NULL.
36410491SRishi.Srivatsavai@Sun.COM  */
36510491SRishi.Srivatsavai@Sun.COM mac_bridge_tx_t mac_bridge_tx_cb;
36610491SRishi.Srivatsavai@Sun.COM mac_bridge_rx_t mac_bridge_rx_cb;
36710491SRishi.Srivatsavai@Sun.COM mac_bridge_ref_t mac_bridge_ref_cb;
36810491SRishi.Srivatsavai@Sun.COM mac_bridge_ls_t mac_bridge_ls_cb;
36910491SRishi.Srivatsavai@Sun.COM 
3708275SEric Cheng static int i_mac_constructor(void *, void *, int);
3718275SEric Cheng static void i_mac_destructor(void *, void *);
3728275SEric Cheng static int i_mac_ring_ctor(void *, void *, int);
3738275SEric Cheng static void i_mac_ring_dtor(void *, void *);
3748275SEric Cheng static mblk_t *mac_rx_classify(mac_impl_t *, mac_resource_handle_t, mblk_t *);
3758275SEric Cheng void mac_tx_client_flush(mac_client_impl_t *);
3768275SEric Cheng void mac_tx_client_block(mac_client_impl_t *);
3778275SEric Cheng static void mac_rx_ring_quiesce(mac_ring_t *, uint_t);
3788275SEric Cheng static int mac_start_group_and_rings(mac_group_t *);
3798275SEric Cheng static void mac_stop_group_and_rings(mac_group_t *);
38011878SVenu.Iyer@Sun.COM static void mac_pool_event_cb(pool_event_t, int, void *);
3812311Sseb 
38213109Smichael.l.lim@oracle.com typedef struct netinfo_s {
38313109Smichael.l.lim@oracle.com 	list_node_t	ni_link;
38413109Smichael.l.lim@oracle.com 	void		*ni_record;
38513109Smichael.l.lim@oracle.com 	int		ni_size;
38613109Smichael.l.lim@oracle.com 	int		ni_type;
38713109Smichael.l.lim@oracle.com } netinfo_t;
38813109Smichael.l.lim@oracle.com 
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate  * Module initialization functions.
3910Sstevel@tonic-gate  */
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate void
mac_init(void)3940Sstevel@tonic-gate mac_init(void)
3950Sstevel@tonic-gate {
3968275SEric Cheng 	mac_tx_percpu_cnt = ((boot_max_ncpus == -1) ? max_ncpus :
3978275SEric Cheng 	    boot_max_ncpus);
3988275SEric Cheng 
3998275SEric Cheng 	/* Upper bound is mac_tx_percpu_cnt_max */
4008275SEric Cheng 	if (mac_tx_percpu_cnt > mac_tx_percpu_cnt_max)
4018275SEric Cheng 		mac_tx_percpu_cnt = mac_tx_percpu_cnt_max;
4028275SEric Cheng 
4038275SEric Cheng 	if (mac_tx_percpu_cnt < 1) {
4048275SEric Cheng 		/* Someone set max_tx_percpu_cnt_max to 0 or less */
4058275SEric Cheng 		mac_tx_percpu_cnt = 1;
4068275SEric Cheng 	}
4078275SEric Cheng 
4088275SEric Cheng 	ASSERT(mac_tx_percpu_cnt >= 1);
4098275SEric Cheng 	mac_tx_percpu_cnt = (1 << highbit(mac_tx_percpu_cnt - 1));
4108275SEric Cheng 	/*
4118275SEric Cheng 	 * Make it of the form 2**N - 1 in the range
4128275SEric Cheng 	 * [0 .. mac_tx_percpu_cnt_max - 1]
4138275SEric Cheng 	 */
4148275SEric Cheng 	mac_tx_percpu_cnt--;
4158275SEric Cheng 
4160Sstevel@tonic-gate 	i_mac_impl_cachep = kmem_cache_create("mac_impl_cache",
4172311Sseb 	    sizeof (mac_impl_t), 0, i_mac_constructor, i_mac_destructor,
4182311Sseb 	    NULL, NULL, NULL, 0);
4190Sstevel@tonic-gate 	ASSERT(i_mac_impl_cachep != NULL);
4200Sstevel@tonic-gate 
4218275SEric Cheng 	mac_ring_cache = kmem_cache_create("mac_ring_cache",
4228275SEric Cheng 	    sizeof (mac_ring_t), 0, i_mac_ring_ctor, i_mac_ring_dtor, NULL,
4238275SEric Cheng 	    NULL, NULL, 0);
4248275SEric Cheng 	ASSERT(mac_ring_cache != NULL);
4255084Sjohnlev 
426269Sericheng 	i_mac_impl_hash = mod_hash_create_extended("mac_impl_hash",
427269Sericheng 	    IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
428269Sericheng 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
429269Sericheng 	rw_init(&i_mac_impl_lock, NULL, RW_DEFAULT, NULL);
4308275SEric Cheng 
4318275SEric Cheng 	mac_flow_init();
4328275SEric Cheng 	mac_soft_ring_init();
4338275SEric Cheng 	mac_bcast_init();
4348275SEric Cheng 	mac_client_init();
4358275SEric Cheng 
436269Sericheng 	i_mac_impl_count = 0;
4372311Sseb 
4382311Sseb 	i_mactype_hash = mod_hash_create_extended("mactype_hash",
4392311Sseb 	    MACTYPE_HASHSZ,
4402311Sseb 	    mod_hash_null_keydtor, mod_hash_null_valdtor,
4412311Sseb 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
4425895Syz147064 
4435895Syz147064 	/*
4445895Syz147064 	 * Allocate an id space to manage minor numbers. The range of the
44510283SGarrett.Damore@Sun.COM 	 * space will be from MAC_MAX_MINOR+1 to MAC_PRIVATE_MINOR-1.  This
44610283SGarrett.Damore@Sun.COM 	 * leaves half of the 32-bit minors available for driver private use.
4475895Syz147064 	 */
44810283SGarrett.Damore@Sun.COM 	minor_ids = id_space_create("mac_minor_ids", MAC_MAX_MINOR+1,
44910283SGarrett.Damore@Sun.COM 	    MAC_PRIVATE_MINOR-1);
4505895Syz147064 	ASSERT(minor_ids != NULL);
4515895Syz147064 	minor_count = 0;
4528275SEric Cheng 
4538275SEric Cheng 	/* Let's default to 20 seconds */
4548275SEric Cheng 	mac_logging_interval = 20;
4558275SEric Cheng 	mac_flow_log_enable = B_FALSE;
4568275SEric Cheng 	mac_link_log_enable = B_FALSE;
4578275SEric Cheng 	mac_logging_timer = 0;
45811878SVenu.Iyer@Sun.COM 
45911878SVenu.Iyer@Sun.COM 	/* Register to be notified of noteworthy pools events */
46011878SVenu.Iyer@Sun.COM 	mac_pool_event_reg.pec_func =  mac_pool_event_cb;
46111878SVenu.Iyer@Sun.COM 	mac_pool_event_reg.pec_arg = NULL;
46211878SVenu.Iyer@Sun.COM 	pool_event_cb_register(&mac_pool_event_reg);
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate int
mac_fini(void)4660Sstevel@tonic-gate mac_fini(void)
4670Sstevel@tonic-gate {
46811878SVenu.Iyer@Sun.COM 
4695895Syz147064 	if (i_mac_impl_count > 0 || minor_count > 0)
470269Sericheng 		return (EBUSY);
4710Sstevel@tonic-gate 
47211878SVenu.Iyer@Sun.COM 	pool_event_cb_unregister(&mac_pool_event_reg);
47311878SVenu.Iyer@Sun.COM 
4745895Syz147064 	id_space_destroy(minor_ids);
4758275SEric Cheng 	mac_flow_fini();
4765895Syz147064 
477269Sericheng 	mod_hash_destroy_hash(i_mac_impl_hash);
478269Sericheng 	rw_destroy(&i_mac_impl_lock);
4790Sstevel@tonic-gate 
4808275SEric Cheng 	mac_client_fini();
4818275SEric Cheng 	kmem_cache_destroy(mac_ring_cache);
4822311Sseb 
4832311Sseb 	mod_hash_destroy_hash(i_mactype_hash);
4848275SEric Cheng 	mac_soft_ring_finish();
48511878SVenu.Iyer@Sun.COM 
48611878SVenu.Iyer@Sun.COM 
4870Sstevel@tonic-gate 	return (0);
4880Sstevel@tonic-gate }
4890Sstevel@tonic-gate 
49010986SSebastien.Roy@Sun.COM /*
49110986SSebastien.Roy@Sun.COM  * Initialize a GLDv3 driver's device ops.  A driver that manages its own ops
49210986SSebastien.Roy@Sun.COM  * (e.g. softmac) may pass in a NULL ops argument.
49310986SSebastien.Roy@Sun.COM  */
4948275SEric Cheng void
mac_init_ops(struct dev_ops * ops,const char * name)4958275SEric Cheng mac_init_ops(struct dev_ops *ops, const char *name)
4968275SEric Cheng {
49710986SSebastien.Roy@Sun.COM 	major_t major = ddi_name_to_major((char *)name);
49810986SSebastien.Roy@Sun.COM 
49910986SSebastien.Roy@Sun.COM 	/*
50010986SSebastien.Roy@Sun.COM 	 * By returning on error below, we are not letting the driver continue
50110986SSebastien.Roy@Sun.COM 	 * in an undefined context.  The mac_register() function will faill if
50210986SSebastien.Roy@Sun.COM 	 * DN_GLDV3_DRIVER isn't set.
50310986SSebastien.Roy@Sun.COM 	 */
50410986SSebastien.Roy@Sun.COM 	if (major == DDI_MAJOR_T_NONE)
50510986SSebastien.Roy@Sun.COM 		return;
50610986SSebastien.Roy@Sun.COM 	LOCK_DEV_OPS(&devnamesp[major].dn_lock);
50710986SSebastien.Roy@Sun.COM 	devnamesp[major].dn_flags |= (DN_GLDV3_DRIVER | DN_NETWORK_DRIVER);
50810986SSebastien.Roy@Sun.COM 	UNLOCK_DEV_OPS(&devnamesp[major].dn_lock);
50910986SSebastien.Roy@Sun.COM 	if (ops != NULL)
51010986SSebastien.Roy@Sun.COM 		dld_init_ops(ops, name);
5118275SEric Cheng }
5128275SEric Cheng 
5138275SEric Cheng void
mac_fini_ops(struct dev_ops * ops)5148275SEric Cheng mac_fini_ops(struct dev_ops *ops)
5158275SEric Cheng {
5168275SEric Cheng 	dld_fini_ops(ops);
5178275SEric Cheng }
5188275SEric Cheng 
5198275SEric Cheng /*ARGSUSED*/
5208275SEric Cheng static int
i_mac_constructor(void * buf,void * arg,int kmflag)5218275SEric Cheng i_mac_constructor(void *buf, void *arg, int kmflag)
5228275SEric Cheng {
5238275SEric Cheng 	mac_impl_t	*mip = buf;
5248275SEric Cheng 
5258275SEric Cheng 	bzero(buf, sizeof (mac_impl_t));
5268275SEric Cheng 
5278275SEric Cheng 	mip->mi_linkstate = LINK_STATE_UNKNOWN;
5288275SEric Cheng 
5298275SEric Cheng 	rw_init(&mip->mi_rw_lock, NULL, RW_DRIVER, NULL);
5308275SEric Cheng 	mutex_init(&mip->mi_notify_lock, NULL, MUTEX_DRIVER, NULL);
5318275SEric Cheng 	mutex_init(&mip->mi_promisc_lock, NULL, MUTEX_DRIVER, NULL);
5328275SEric Cheng 	mutex_init(&mip->mi_ring_lock, NULL, MUTEX_DEFAULT, NULL);
5338275SEric Cheng 
5348275SEric Cheng 	mip->mi_notify_cb_info.mcbi_lockp = &mip->mi_notify_lock;
5358275SEric Cheng 	cv_init(&mip->mi_notify_cb_info.mcbi_cv, NULL, CV_DRIVER, NULL);
5368275SEric Cheng 	mip->mi_promisc_cb_info.mcbi_lockp = &mip->mi_promisc_lock;
5378275SEric Cheng 	cv_init(&mip->mi_promisc_cb_info.mcbi_cv, NULL, CV_DRIVER, NULL);
53810491SRishi.Srivatsavai@Sun.COM 
53910491SRishi.Srivatsavai@Sun.COM 	mutex_init(&mip->mi_bridge_lock, NULL, MUTEX_DEFAULT, NULL);
54010491SRishi.Srivatsavai@Sun.COM 
5418275SEric Cheng 	return (0);
5428275SEric Cheng }
5438275SEric Cheng 
5448275SEric Cheng /*ARGSUSED*/
5458275SEric Cheng static void
i_mac_destructor(void * buf,void * arg)5468275SEric Cheng i_mac_destructor(void *buf, void *arg)
5478275SEric Cheng {
5488275SEric Cheng 	mac_impl_t	*mip = buf;
5498275SEric Cheng 	mac_cb_info_t	*mcbi;
5508275SEric Cheng 
5518275SEric Cheng 	ASSERT(mip->mi_ref == 0);
5528275SEric Cheng 	ASSERT(mip->mi_active == 0);
5538275SEric Cheng 	ASSERT(mip->mi_linkstate == LINK_STATE_UNKNOWN);
5548275SEric Cheng 	ASSERT(mip->mi_devpromisc == 0);
5558275SEric Cheng 	ASSERT(mip->mi_ksp == NULL);
5568275SEric Cheng 	ASSERT(mip->mi_kstat_count == 0);
5578275SEric Cheng 	ASSERT(mip->mi_nclients == 0);
5588275SEric Cheng 	ASSERT(mip->mi_nactiveclients == 0);
5598833SVenu.Iyer@Sun.COM 	ASSERT(mip->mi_single_active_client == NULL);
5608275SEric Cheng 	ASSERT(mip->mi_state_flags == 0);
5618275SEric Cheng 	ASSERT(mip->mi_factory_addr == NULL);
5628275SEric Cheng 	ASSERT(mip->mi_factory_addr_num == 0);
5638275SEric Cheng 	ASSERT(mip->mi_default_tx_ring == NULL);
5648275SEric Cheng 
5658275SEric Cheng 	mcbi = &mip->mi_notify_cb_info;
5668275SEric Cheng 	ASSERT(mcbi->mcbi_del_cnt == 0 && mcbi->mcbi_walker_cnt == 0);
5678275SEric Cheng 	ASSERT(mip->mi_notify_bits == 0);
5688275SEric Cheng 	ASSERT(mip->mi_notify_thread == NULL);
5698275SEric Cheng 	ASSERT(mcbi->mcbi_lockp == &mip->mi_notify_lock);
5708275SEric Cheng 	mcbi->mcbi_lockp = NULL;
5718275SEric Cheng 
5728275SEric Cheng 	mcbi = &mip->mi_promisc_cb_info;
5738275SEric Cheng 	ASSERT(mcbi->mcbi_del_cnt == 0 && mip->mi_promisc_list == NULL);
5748275SEric Cheng 	ASSERT(mip->mi_promisc_list == NULL);
5758275SEric Cheng 	ASSERT(mcbi->mcbi_lockp == &mip->mi_promisc_lock);
5768275SEric Cheng 	mcbi->mcbi_lockp = NULL;
5778275SEric Cheng 
5788275SEric Cheng 	ASSERT(mip->mi_bcast_ngrps == 0 && mip->mi_bcast_grp == NULL);
5798275SEric Cheng 	ASSERT(mip->mi_perim_owner == NULL && mip->mi_perim_ocnt == 0);
5808275SEric Cheng 
5818275SEric Cheng 	rw_destroy(&mip->mi_rw_lock);
5828275SEric Cheng 
5838275SEric Cheng 	mutex_destroy(&mip->mi_promisc_lock);
5848275SEric Cheng 	cv_destroy(&mip->mi_promisc_cb_info.mcbi_cv);
5858275SEric Cheng 	mutex_destroy(&mip->mi_notify_lock);
5868275SEric Cheng 	cv_destroy(&mip->mi_notify_cb_info.mcbi_cv);
5878275SEric Cheng 	mutex_destroy(&mip->mi_ring_lock);
58810491SRishi.Srivatsavai@Sun.COM 
58910491SRishi.Srivatsavai@Sun.COM 	ASSERT(mip->mi_bridge_link == NULL);
5908275SEric Cheng }
5918275SEric Cheng 
5928275SEric Cheng /* ARGSUSED */
5938275SEric Cheng static int
i_mac_ring_ctor(void * buf,void * arg,int kmflag)5948275SEric Cheng i_mac_ring_ctor(void *buf, void *arg, int kmflag)
5958275SEric Cheng {
5968275SEric Cheng 	mac_ring_t *ring = (mac_ring_t *)buf;
5978275SEric Cheng 
5988275SEric Cheng 	bzero(ring, sizeof (mac_ring_t));
5998275SEric Cheng 	cv_init(&ring->mr_cv, NULL, CV_DEFAULT, NULL);
6008275SEric Cheng 	mutex_init(&ring->mr_lock, NULL, MUTEX_DEFAULT, NULL);
6018275SEric Cheng 	ring->mr_state = MR_FREE;
6028275SEric Cheng 	return (0);
6038275SEric Cheng }
6048275SEric Cheng 
6058275SEric Cheng /* ARGSUSED */
6068275SEric Cheng static void
i_mac_ring_dtor(void * buf,void * arg)6078275SEric Cheng i_mac_ring_dtor(void *buf, void *arg)
6088275SEric Cheng {
6098275SEric Cheng 	mac_ring_t *ring = (mac_ring_t *)buf;
6108275SEric Cheng 
6118275SEric Cheng 	cv_destroy(&ring->mr_cv);
6128275SEric Cheng 	mutex_destroy(&ring->mr_lock);
6138275SEric Cheng }
6148275SEric Cheng 
6158275SEric Cheng /*
6168275SEric Cheng  * Common functions to do mac callback addition and deletion. Currently this is
6178275SEric Cheng  * used by promisc callbacks and notify callbacks. List addition and deletion
6188275SEric Cheng  * need to take care of list walkers. List walkers in general, can't hold list
6198275SEric Cheng  * locks and make upcall callbacks due to potential lock order and recursive
6208275SEric Cheng  * reentry issues. Instead list walkers increment the list walker count to mark
6218275SEric Cheng  * the presence of a walker thread. Addition can be carefully done to ensure
6228275SEric Cheng  * that the list walker always sees either the old list or the new list.
6238275SEric Cheng  * However the deletion can't be done while the walker is active, instead the
6248275SEric Cheng  * deleting thread simply marks the entry as logically deleted. The last walker
6258275SEric Cheng  * physically deletes and frees up the logically deleted entries when the walk
6268275SEric Cheng  * is complete.
6278275SEric Cheng  */
6288275SEric Cheng void
mac_callback_add(mac_cb_info_t * mcbi,mac_cb_t ** mcb_head,mac_cb_t * mcb_elem)6298275SEric Cheng mac_callback_add(mac_cb_info_t *mcbi, mac_cb_t **mcb_head,
6308275SEric Cheng     mac_cb_t *mcb_elem)
6318275SEric Cheng {
6328275SEric Cheng 	mac_cb_t	*p;
6338275SEric Cheng 	mac_cb_t	**pp;
6348275SEric Cheng 
6358275SEric Cheng 	/* Verify it is not already in the list */
6368275SEric Cheng 	for (pp = mcb_head; (p = *pp) != NULL; pp = &p->mcb_nextp) {
6378275SEric Cheng 		if (p == mcb_elem)
6388275SEric Cheng 			break;
6398275SEric Cheng 	}
6408275SEric Cheng 	VERIFY(p == NULL);
6418275SEric Cheng 
6428275SEric Cheng 	/*
6438275SEric Cheng 	 * Add it to the head of the callback list. The membar ensures that
6448275SEric Cheng 	 * the following list pointer manipulations reach global visibility
6458275SEric Cheng 	 * in exactly the program order below.
6468275SEric Cheng 	 */
6478275SEric Cheng 	ASSERT(MUTEX_HELD(mcbi->mcbi_lockp));
6488275SEric Cheng 
6498275SEric Cheng 	mcb_elem->mcb_nextp = *mcb_head;
6508275SEric Cheng 	membar_producer();
6518275SEric Cheng 	*mcb_head = mcb_elem;
6528275SEric Cheng }
6538275SEric Cheng 
6548275SEric Cheng /*
6558275SEric Cheng  * Mark the entry as logically deleted. If there aren't any walkers unlink
6568275SEric Cheng  * from the list. In either case return the corresponding status.
6578275SEric Cheng  */
6588275SEric Cheng boolean_t
mac_callback_remove(mac_cb_info_t * mcbi,mac_cb_t ** mcb_head,mac_cb_t * mcb_elem)6598275SEric Cheng mac_callback_remove(mac_cb_info_t *mcbi, mac_cb_t **mcb_head,
6608275SEric Cheng     mac_cb_t *mcb_elem)
6618275SEric Cheng {
6628275SEric Cheng 	mac_cb_t	*p;
6638275SEric Cheng 	mac_cb_t	**pp;
6648275SEric Cheng 
6658275SEric Cheng 	ASSERT(MUTEX_HELD(mcbi->mcbi_lockp));
6668275SEric Cheng 	/*
6678275SEric Cheng 	 * Search the callback list for the entry to be removed
6688275SEric Cheng 	 */
6698275SEric Cheng 	for (pp = mcb_head; (p = *pp) != NULL; pp = &p->mcb_nextp) {
6708275SEric Cheng 		if (p == mcb_elem)
6718275SEric Cheng 			break;
6728275SEric Cheng 	}
6738275SEric Cheng 	VERIFY(p != NULL);
6748275SEric Cheng 
6758275SEric Cheng 	/*
6768275SEric Cheng 	 * If there are walkers just mark it as deleted and the last walker
6778275SEric Cheng 	 * will remove from the list and free it.
6788275SEric Cheng 	 */
6798275SEric Cheng 	if (mcbi->mcbi_walker_cnt != 0) {
6808275SEric Cheng 		p->mcb_flags |= MCB_CONDEMNED;
6818275SEric Cheng 		mcbi->mcbi_del_cnt++;
6828275SEric Cheng 		return (B_FALSE);
6838275SEric Cheng 	}
6848275SEric Cheng 
6858275SEric Cheng 	ASSERT(mcbi->mcbi_del_cnt == 0);
6868275SEric Cheng 	*pp = p->mcb_nextp;
6878275SEric Cheng 	p->mcb_nextp = NULL;
6888275SEric Cheng 	return (B_TRUE);
6898275SEric Cheng }
6908275SEric Cheng 
6918275SEric Cheng /*
6928275SEric Cheng  * Wait for all pending callback removals to be completed
6938275SEric Cheng  */
6948275SEric Cheng void
mac_callback_remove_wait(mac_cb_info_t * mcbi)6958275SEric Cheng mac_callback_remove_wait(mac_cb_info_t *mcbi)
6968275SEric Cheng {
6978275SEric Cheng 	ASSERT(MUTEX_HELD(mcbi->mcbi_lockp));
6988275SEric Cheng 	while (mcbi->mcbi_del_cnt != 0) {
6998275SEric Cheng 		DTRACE_PROBE1(need_wait, mac_cb_info_t *, mcbi);
7008275SEric Cheng 		cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
7018275SEric Cheng 	}
7028275SEric Cheng }
7038275SEric Cheng 
7040Sstevel@tonic-gate /*
7058275SEric Cheng  * The last mac callback walker does the cleanup. Walk the list and unlik
7068275SEric Cheng  * all the logically deleted entries and construct a temporary list of
7078275SEric Cheng  * removed entries. Return the list of removed entries to the caller.
7088275SEric Cheng  */
7098275SEric Cheng mac_cb_t *
mac_callback_walker_cleanup(mac_cb_info_t * mcbi,mac_cb_t ** mcb_head)7108275SEric Cheng mac_callback_walker_cleanup(mac_cb_info_t *mcbi, mac_cb_t **mcb_head)
7118275SEric Cheng {
7128275SEric Cheng 	mac_cb_t	*p;
7138275SEric Cheng 	mac_cb_t	**pp;
7148275SEric Cheng 	mac_cb_t	*rmlist = NULL;		/* List of removed elements */
7158275SEric Cheng 	int	cnt = 0;
7168275SEric Cheng 
7178275SEric Cheng 	ASSERT(MUTEX_HELD(mcbi->mcbi_lockp));
7188275SEric Cheng 	ASSERT(mcbi->mcbi_del_cnt != 0 && mcbi->mcbi_walker_cnt == 0);
7198275SEric Cheng 
7208275SEric Cheng 	pp = mcb_head;
7218275SEric Cheng 	while (*pp != NULL) {
7228275SEric Cheng 		if ((*pp)->mcb_flags & MCB_CONDEMNED) {
7238275SEric Cheng 			p = *pp;
7248275SEric Cheng 			*pp = p->mcb_nextp;
7258275SEric Cheng 			p->mcb_nextp = rmlist;
7268275SEric Cheng 			rmlist = p;
7278275SEric Cheng 			cnt++;
7288275SEric Cheng 			continue;
7298275SEric Cheng 		}
7308275SEric Cheng 		pp = &(*pp)->mcb_nextp;
7318275SEric Cheng 	}
7328275SEric Cheng 
7338275SEric Cheng 	ASSERT(mcbi->mcbi_del_cnt == cnt);
7348275SEric Cheng 	mcbi->mcbi_del_cnt = 0;
7358275SEric Cheng 	return (rmlist);
7368275SEric Cheng }
7378275SEric Cheng 
7388275SEric Cheng boolean_t
mac_callback_lookup(mac_cb_t ** mcb_headp,mac_cb_t * mcb_elem)7398275SEric Cheng mac_callback_lookup(mac_cb_t **mcb_headp, mac_cb_t *mcb_elem)
7408275SEric Cheng {
7418275SEric Cheng 	mac_cb_t	*mcb;
7428275SEric Cheng 
7438275SEric Cheng 	/* Verify it is not already in the list */
7448275SEric Cheng 	for (mcb = *mcb_headp; mcb != NULL; mcb = mcb->mcb_nextp) {
7458275SEric Cheng 		if (mcb == mcb_elem)
7468275SEric Cheng 			return (B_TRUE);
7478275SEric Cheng 	}
7488275SEric Cheng 
7498275SEric Cheng 	return (B_FALSE);
7508275SEric Cheng }
7518275SEric Cheng 
7528275SEric Cheng boolean_t
mac_callback_find(mac_cb_info_t * mcbi,mac_cb_t ** mcb_headp,mac_cb_t * mcb_elem)7538275SEric Cheng mac_callback_find(mac_cb_info_t *mcbi, mac_cb_t **mcb_headp, mac_cb_t *mcb_elem)
7548275SEric Cheng {
7558275SEric Cheng 	boolean_t	found;
7568275SEric Cheng 
7578275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
7588275SEric Cheng 	found = mac_callback_lookup(mcb_headp, mcb_elem);
7598275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
7608275SEric Cheng 
7618275SEric Cheng 	return (found);
7628275SEric Cheng }
7638275SEric Cheng 
7648275SEric Cheng /* Free the list of removed callbacks */
7658275SEric Cheng void
mac_callback_free(mac_cb_t * rmlist)7668275SEric Cheng mac_callback_free(mac_cb_t *rmlist)
7678275SEric Cheng {
7688275SEric Cheng 	mac_cb_t	*mcb;
7698275SEric Cheng 	mac_cb_t	*mcb_next;
7708275SEric Cheng 
7718275SEric Cheng 	for (mcb = rmlist; mcb != NULL; mcb = mcb_next) {
7728275SEric Cheng 		mcb_next = mcb->mcb_nextp;
7738275SEric Cheng 		kmem_free(mcb->mcb_objp, mcb->mcb_objsize);
7748275SEric Cheng 	}
7758275SEric Cheng }
7768275SEric Cheng 
7778275SEric Cheng /*
7788275SEric Cheng  * The promisc callbacks are in 2 lists, one off the 'mip' and another off the
7798275SEric Cheng  * 'mcip' threaded by mpi_mi_link and mpi_mci_link respectively. However there
7808275SEric Cheng  * is only a single shared total walker count, and an entry can't be physically
7818275SEric Cheng  * unlinked if a walker is active on either list. The last walker does this
7828275SEric Cheng  * cleanup of logically deleted entries.
7838275SEric Cheng  */
7848275SEric Cheng void
i_mac_promisc_walker_cleanup(mac_impl_t * mip)7858275SEric Cheng i_mac_promisc_walker_cleanup(mac_impl_t *mip)
7868275SEric Cheng {
7878275SEric Cheng 	mac_cb_t	*rmlist;
7888275SEric Cheng 	mac_cb_t	*mcb;
7898275SEric Cheng 	mac_cb_t	*mcb_next;
7908275SEric Cheng 	mac_promisc_impl_t	*mpip;
7918275SEric Cheng 
7928275SEric Cheng 	/*
7938275SEric Cheng 	 * Construct a temporary list of deleted callbacks by walking the
7948275SEric Cheng 	 * the mi_promisc_list. Then for each entry in the temporary list,
7958275SEric Cheng 	 * remove it from the mci_promisc_list and free the entry.
7968275SEric Cheng 	 */
7978275SEric Cheng 	rmlist = mac_callback_walker_cleanup(&mip->mi_promisc_cb_info,
7988275SEric Cheng 	    &mip->mi_promisc_list);
7998275SEric Cheng 
8008275SEric Cheng 	for (mcb = rmlist; mcb != NULL; mcb = mcb_next) {
8018275SEric Cheng 		mcb_next = mcb->mcb_nextp;
8028275SEric Cheng 		mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
8038275SEric Cheng 		VERIFY(mac_callback_remove(&mip->mi_promisc_cb_info,
8048275SEric Cheng 		    &mpip->mpi_mcip->mci_promisc_list, &mpip->mpi_mci_link));
8058275SEric Cheng 		mcb->mcb_flags = 0;
8068275SEric Cheng 		mcb->mcb_nextp = NULL;
8078275SEric Cheng 		kmem_cache_free(mac_promisc_impl_cache, mpip);
8088275SEric Cheng 	}
8098275SEric Cheng }
8108275SEric Cheng 
8118275SEric Cheng void
i_mac_notify(mac_impl_t * mip,mac_notify_type_t type)8128275SEric Cheng i_mac_notify(mac_impl_t *mip, mac_notify_type_t type)
8138275SEric Cheng {
8148275SEric Cheng 	mac_cb_info_t	*mcbi;
8158275SEric Cheng 
8168275SEric Cheng 	/*
8178275SEric Cheng 	 * Signal the notify thread even after mi_ref has become zero and
8188275SEric Cheng 	 * mi_disabled is set. The synchronization with the notify thread
8198275SEric Cheng 	 * happens in mac_unregister and that implies the driver must make
8208275SEric Cheng 	 * sure it is single-threaded (with respect to mac calls) and that
8218275SEric Cheng 	 * all pending mac calls have returned before it calls mac_unregister
8228275SEric Cheng 	 */
8238275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_READER);
8248275SEric Cheng 	if (mip->mi_state_flags & MIS_DISABLED)
8258275SEric Cheng 		goto exit;
8268275SEric Cheng 
8278275SEric Cheng 	/*
8288275SEric Cheng 	 * Guard against incorrect notifications.  (Running a newer
8298275SEric Cheng 	 * mac client against an older implementation?)
8308275SEric Cheng 	 */
8318275SEric Cheng 	if (type >= MAC_NNOTE)
8328275SEric Cheng 		goto exit;
8338275SEric Cheng 
8348275SEric Cheng 	mcbi = &mip->mi_notify_cb_info;
8358275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
8368275SEric Cheng 	mip->mi_notify_bits |= (1 << type);
8378275SEric Cheng 	cv_broadcast(&mcbi->mcbi_cv);
8388275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
8398275SEric Cheng 
8408275SEric Cheng exit:
8418275SEric Cheng 	rw_exit(&i_mac_impl_lock);
8428275SEric Cheng }
8438275SEric Cheng 
8448275SEric Cheng /*
8458275SEric Cheng  * Mac serialization primitives. Please see the block comment at the
8468275SEric Cheng  * top of the file.
8470Sstevel@tonic-gate  */
8488275SEric Cheng void
i_mac_perim_enter(mac_impl_t * mip)8498275SEric Cheng i_mac_perim_enter(mac_impl_t *mip)
8508275SEric Cheng {
8518275SEric Cheng 	mac_client_impl_t	*mcip;
8528275SEric Cheng 
8538275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
8548275SEric Cheng 		/*
8558275SEric Cheng 		 * This is a VNIC. Return the lower mac since that is what
8568275SEric Cheng 		 * we want to serialize on.
8578275SEric Cheng 		 */
8588275SEric Cheng 		mcip = mac_vnic_lower(mip);
8598275SEric Cheng 		mip = mcip->mci_mip;
8608275SEric Cheng 	}
8618275SEric Cheng 
8628275SEric Cheng 	mutex_enter(&mip->mi_perim_lock);
8638275SEric Cheng 	if (mip->mi_perim_owner == curthread) {
8648275SEric Cheng 		mip->mi_perim_ocnt++;
8658275SEric Cheng 		mutex_exit(&mip->mi_perim_lock);
8668275SEric Cheng 		return;
8678275SEric Cheng 	}
8688275SEric Cheng 
8698275SEric Cheng 	while (mip->mi_perim_owner != NULL)
8708275SEric Cheng 		cv_wait(&mip->mi_perim_cv, &mip->mi_perim_lock);
8718275SEric Cheng 
8728275SEric Cheng 	mip->mi_perim_owner = curthread;
8738275SEric Cheng 	ASSERT(mip->mi_perim_ocnt == 0);
8748275SEric Cheng 	mip->mi_perim_ocnt++;
8758275SEric Cheng #ifdef DEBUG
8768275SEric Cheng 	mip->mi_perim_stack_depth = getpcstack(mip->mi_perim_stack,
8778275SEric Cheng 	    MAC_PERIM_STACK_DEPTH);
8788275SEric Cheng #endif
8798275SEric Cheng 	mutex_exit(&mip->mi_perim_lock);
8808275SEric Cheng }
8818275SEric Cheng 
8828275SEric Cheng int
i_mac_perim_enter_nowait(mac_impl_t * mip)8838275SEric Cheng i_mac_perim_enter_nowait(mac_impl_t *mip)
8848275SEric Cheng {
8858275SEric Cheng 	/*
8868275SEric Cheng 	 * The vnic is a special case, since the serialization is done based
8878275SEric Cheng 	 * on the lower mac. If the lower mac is busy, it does not imply the
8888275SEric Cheng 	 * vnic can't be unregistered. But in the case of other drivers,
8898275SEric Cheng 	 * a busy perimeter or open mac handles implies that the mac is busy
8908275SEric Cheng 	 * and can't be unregistered.
8918275SEric Cheng 	 */
8928275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
8938275SEric Cheng 		i_mac_perim_enter(mip);
8948275SEric Cheng 		return (0);
8958275SEric Cheng 	}
8968275SEric Cheng 
8978275SEric Cheng 	mutex_enter(&mip->mi_perim_lock);
8988275SEric Cheng 	if (mip->mi_perim_owner != NULL) {
8998275SEric Cheng 		mutex_exit(&mip->mi_perim_lock);
9008275SEric Cheng 		return (EBUSY);
9018275SEric Cheng 	}
9028275SEric Cheng 	ASSERT(mip->mi_perim_ocnt == 0);
9038275SEric Cheng 	mip->mi_perim_owner = curthread;
9048275SEric Cheng 	mip->mi_perim_ocnt++;
9058275SEric Cheng 	mutex_exit(&mip->mi_perim_lock);
9068275SEric Cheng 
9078275SEric Cheng 	return (0);
9088275SEric Cheng }
9098275SEric Cheng 
9108275SEric Cheng void
i_mac_perim_exit(mac_impl_t * mip)9118275SEric Cheng i_mac_perim_exit(mac_impl_t *mip)
9128275SEric Cheng {
9138275SEric Cheng 	mac_client_impl_t *mcip;
9148275SEric Cheng 
9158275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
9168275SEric Cheng 		/*
9178275SEric Cheng 		 * This is a VNIC. Return the lower mac since that is what
9188275SEric Cheng 		 * we want to serialize on.
9198275SEric Cheng 		 */
9208275SEric Cheng 		mcip = mac_vnic_lower(mip);
9218275SEric Cheng 		mip = mcip->mci_mip;
9228275SEric Cheng 	}
9238275SEric Cheng 
9248275SEric Cheng 	ASSERT(mip->mi_perim_owner == curthread && mip->mi_perim_ocnt != 0);
9258275SEric Cheng 
9268275SEric Cheng 	mutex_enter(&mip->mi_perim_lock);
9278275SEric Cheng 	if (--mip->mi_perim_ocnt == 0) {
9288275SEric Cheng 		mip->mi_perim_owner = NULL;
9298275SEric Cheng 		cv_signal(&mip->mi_perim_cv);
9308275SEric Cheng 	}
9318275SEric Cheng 	mutex_exit(&mip->mi_perim_lock);
9328275SEric Cheng }
9338275SEric Cheng 
9348275SEric Cheng /*
9358275SEric Cheng  * Returns whether the current thread holds the mac perimeter. Used in making
9368275SEric Cheng  * assertions.
9378275SEric Cheng  */
9388275SEric Cheng boolean_t
mac_perim_held(mac_handle_t mh)9398275SEric Cheng mac_perim_held(mac_handle_t mh)
9408275SEric Cheng {
9418275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
9428275SEric Cheng 	mac_client_impl_t *mcip;
9438275SEric Cheng 
9448275SEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
9458275SEric Cheng 		/*
9468275SEric Cheng 		 * This is a VNIC. Return the lower mac since that is what
9478275SEric Cheng 		 * we want to serialize on.
9488275SEric Cheng 		 */
9498275SEric Cheng 		mcip = mac_vnic_lower(mip);
9508275SEric Cheng 		mip = mcip->mci_mip;
9518275SEric Cheng 	}
9528275SEric Cheng 	return (mip->mi_perim_owner == curthread);
9538275SEric Cheng }
9548275SEric Cheng 
9558275SEric Cheng /*
9568275SEric Cheng  * mac client interfaces to enter the mac perimeter of a mac end point, given
9578275SEric Cheng  * its mac handle, or macname or linkid.
9588275SEric Cheng  */
9598275SEric Cheng void
mac_perim_enter_by_mh(mac_handle_t mh,mac_perim_handle_t * mphp)9608275SEric Cheng mac_perim_enter_by_mh(mac_handle_t mh, mac_perim_handle_t *mphp)
9618275SEric Cheng {
9628275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
9638275SEric Cheng 
9648275SEric Cheng 	i_mac_perim_enter(mip);
9658275SEric Cheng 	/*
9668275SEric Cheng 	 * The mac_perim_handle_t returned encodes the 'mip' and whether a
9678275SEric Cheng 	 * mac_open has been done internally while entering the perimeter.
9688275SEric Cheng 	 * This information is used in mac_perim_exit
9698275SEric Cheng 	 */
9708275SEric Cheng 	MAC_ENCODE_MPH(*mphp, mip, 0);
9718275SEric Cheng }
9728275SEric Cheng 
9738275SEric Cheng int
mac_perim_enter_by_macname(const char * name,mac_perim_handle_t * mphp)9748275SEric Cheng mac_perim_enter_by_macname(const char *name, mac_perim_handle_t *mphp)
9758275SEric Cheng {
9768275SEric Cheng 	int	err;
9778275SEric Cheng 	mac_handle_t	mh;
9788275SEric Cheng 
9798275SEric Cheng 	if ((err = mac_open(name, &mh)) != 0)
9808275SEric Cheng 		return (err);
9818275SEric Cheng 
9828275SEric Cheng 	mac_perim_enter_by_mh(mh, mphp);
9838275SEric Cheng 	MAC_ENCODE_MPH(*mphp, mh, 1);
9848275SEric Cheng 	return (0);
9858275SEric Cheng }
9868275SEric Cheng 
9878275SEric Cheng int
mac_perim_enter_by_linkid(datalink_id_t linkid,mac_perim_handle_t * mphp)9888275SEric Cheng mac_perim_enter_by_linkid(datalink_id_t linkid, mac_perim_handle_t *mphp)
9898275SEric Cheng {
9908275SEric Cheng 	int	err;
9918275SEric Cheng 	mac_handle_t	mh;
9928275SEric Cheng 
9938275SEric Cheng 	if ((err = mac_open_by_linkid(linkid, &mh)) != 0)
9948275SEric Cheng 		return (err);
9958275SEric Cheng 
9968275SEric Cheng 	mac_perim_enter_by_mh(mh, mphp);
9978275SEric Cheng 	MAC_ENCODE_MPH(*mphp, mh, 1);
9988275SEric Cheng 	return (0);
9998275SEric Cheng }
10008275SEric Cheng 
10018275SEric Cheng void
mac_perim_exit(mac_perim_handle_t mph)10028275SEric Cheng mac_perim_exit(mac_perim_handle_t mph)
10038275SEric Cheng {
10048275SEric Cheng 	mac_impl_t	*mip;
10058275SEric Cheng 	boolean_t	need_close;
10068275SEric Cheng 
10078275SEric Cheng 	MAC_DECODE_MPH(mph, mip, need_close);
10088275SEric Cheng 	i_mac_perim_exit(mip);
10098275SEric Cheng 	if (need_close)
10108275SEric Cheng 		mac_close((mac_handle_t)mip);
10118275SEric Cheng }
10128275SEric Cheng 
10138275SEric Cheng int
mac_hold(const char * macname,mac_impl_t ** pmip)10145895Syz147064 mac_hold(const char *macname, mac_impl_t **pmip)
10150Sstevel@tonic-gate {
10160Sstevel@tonic-gate 	mac_impl_t	*mip;
10170Sstevel@tonic-gate 	int		err;
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	/*
10200Sstevel@tonic-gate 	 * Check the device name length to make sure it won't overflow our
10210Sstevel@tonic-gate 	 * buffer.
10220Sstevel@tonic-gate 	 */
10232311Sseb 	if (strlen(macname) >= MAXNAMELEN)
10240Sstevel@tonic-gate 		return (EINVAL);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	/*
10275895Syz147064 	 * Look up its entry in the global hash table.
10280Sstevel@tonic-gate 	 */
10295895Syz147064 	rw_enter(&i_mac_impl_lock, RW_WRITER);
10305895Syz147064 	err = mod_hash_find(i_mac_impl_hash, (mod_hash_key_t)macname,
10315895Syz147064 	    (mod_hash_val_t *)&mip);
10325895Syz147064 
10335895Syz147064 	if (err != 0) {
10345895Syz147064 		rw_exit(&i_mac_impl_lock);
10355895Syz147064 		return (ENOENT);
10365895Syz147064 	}
10375895Syz147064 
10388275SEric Cheng 	if (mip->mi_state_flags & MIS_DISABLED) {
10395895Syz147064 		rw_exit(&i_mac_impl_lock);
10405895Syz147064 		return (ENOENT);
10415895Syz147064 	}
10425895Syz147064 
10438275SEric Cheng 	if (mip->mi_state_flags & MIS_EXCLUSIVE_HELD) {
10445895Syz147064 		rw_exit(&i_mac_impl_lock);
10455895Syz147064 		return (EBUSY);
10465895Syz147064 	}
10475895Syz147064 
10485895Syz147064 	mip->mi_ref++;
10495895Syz147064 	rw_exit(&i_mac_impl_lock);
10505895Syz147064 
10515895Syz147064 	*pmip = mip;
10525895Syz147064 	return (0);
10535895Syz147064 }
10545895Syz147064 
10558275SEric Cheng void
mac_rele(mac_impl_t * mip)10565895Syz147064 mac_rele(mac_impl_t *mip)
10575895Syz147064 {
10585895Syz147064 	rw_enter(&i_mac_impl_lock, RW_WRITER);
10595895Syz147064 	ASSERT(mip->mi_ref != 0);
10608275SEric Cheng 	if (--mip->mi_ref == 0) {
10618275SEric Cheng 		ASSERT(mip->mi_nactiveclients == 0 &&
10628275SEric Cheng 		    !(mip->mi_state_flags & MIS_EXCLUSIVE));
10635895Syz147064 	}
10645895Syz147064 	rw_exit(&i_mac_impl_lock);
10655895Syz147064 }
10665895Syz147064 
10678275SEric Cheng /*
10688893SMichael.Lim@Sun.COM  * Private GLDv3 function to start a MAC instance.
10698275SEric Cheng  */
10705895Syz147064 int
mac_start(mac_handle_t mh)10718893SMichael.Lim@Sun.COM mac_start(mac_handle_t mh)
10720Sstevel@tonic-gate {
10738893SMichael.Lim@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
10748275SEric Cheng 	int		err = 0;
107511878SVenu.Iyer@Sun.COM 	mac_group_t	*defgrp;
10768275SEric Cheng 
10778275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
10782311Sseb 	ASSERT(mip->mi_start != NULL);
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	/*
10810Sstevel@tonic-gate 	 * Check whether the device is already started.
10820Sstevel@tonic-gate 	 */
10838275SEric Cheng 	if (mip->mi_active++ == 0) {
10848275SEric Cheng 		mac_ring_t *ring = NULL;
10858275SEric Cheng 
10868275SEric Cheng 		/*
10878275SEric Cheng 		 * Start the device.
10888275SEric Cheng 		 */
10898275SEric Cheng 		err = mip->mi_start(mip->mi_driver);
10908275SEric Cheng 		if (err != 0) {
10918275SEric Cheng 			mip->mi_active--;
10928275SEric Cheng 			return (err);
10938275SEric Cheng 		}
10948275SEric Cheng 
10950Sstevel@tonic-gate 		/*
10968275SEric Cheng 		 * Start the default tx ring.
10970Sstevel@tonic-gate 		 */
10988275SEric Cheng 		if (mip->mi_default_tx_ring != NULL) {
10998275SEric Cheng 
11008275SEric Cheng 			ring = (mac_ring_t *)mip->mi_default_tx_ring;
110111878SVenu.Iyer@Sun.COM 			if (ring->mr_state != MR_INUSE) {
110211878SVenu.Iyer@Sun.COM 				err = mac_start_ring(ring);
110311878SVenu.Iyer@Sun.COM 				if (err != 0) {
110411878SVenu.Iyer@Sun.COM 					mip->mi_active--;
110511878SVenu.Iyer@Sun.COM 					return (err);
110611878SVenu.Iyer@Sun.COM 				}
11078275SEric Cheng 			}
11088275SEric Cheng 		}
11098275SEric Cheng 
111011878SVenu.Iyer@Sun.COM 		if ((defgrp = MAC_DEFAULT_RX_GROUP(mip)) != NULL) {
11118275SEric Cheng 			/*
11128275SEric Cheng 			 * Start the default ring, since it will be needed
11138275SEric Cheng 			 * to receive broadcast and multicast traffic for
11148275SEric Cheng 			 * both primary and non-primary MAC clients.
11158275SEric Cheng 			 */
111611878SVenu.Iyer@Sun.COM 			ASSERT(defgrp->mrg_state == MAC_GROUP_STATE_REGISTERED);
111711878SVenu.Iyer@Sun.COM 			err = mac_start_group_and_rings(defgrp);
11188275SEric Cheng 			if (err != 0) {
11198275SEric Cheng 				mip->mi_active--;
112011878SVenu.Iyer@Sun.COM 				if ((ring != NULL) &&
112111878SVenu.Iyer@Sun.COM 				    (ring->mr_state == MR_INUSE))
11228275SEric Cheng 					mac_stop_ring(ring);
11238275SEric Cheng 				return (err);
11248275SEric Cheng 			}
112511878SVenu.Iyer@Sun.COM 			mac_set_group_state(defgrp, MAC_GROUP_STATE_SHARED);
11268275SEric Cheng 		}
11270Sstevel@tonic-gate 	}
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	return (err);
11300Sstevel@tonic-gate }
11310Sstevel@tonic-gate 
11328275SEric Cheng /*
11338893SMichael.Lim@Sun.COM  * Private GLDv3 function to stop a MAC instance.
11348275SEric Cheng  */
11350Sstevel@tonic-gate void
mac_stop(mac_handle_t mh)11368893SMichael.Lim@Sun.COM mac_stop(mac_handle_t mh)
11370Sstevel@tonic-gate {
11388893SMichael.Lim@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
113911878SVenu.Iyer@Sun.COM 	mac_group_t	*grp;
11408893SMichael.Lim@Sun.COM 
11412311Sseb 	ASSERT(mip->mi_stop != NULL);
11428275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	/*
11450Sstevel@tonic-gate 	 * Check whether the device is still needed.
11460Sstevel@tonic-gate 	 */
11470Sstevel@tonic-gate 	ASSERT(mip->mi_active != 0);
11488275SEric Cheng 	if (--mip->mi_active == 0) {
114911878SVenu.Iyer@Sun.COM 		if ((grp = MAC_DEFAULT_RX_GROUP(mip)) != NULL) {
11500Sstevel@tonic-gate 			/*
11518275SEric Cheng 			 * There should be no more active clients since the
11528275SEric Cheng 			 * MAC is being stopped. Stop the default RX group
11538275SEric Cheng 			 * and transition it back to registered state.
115411878SVenu.Iyer@Sun.COM 			 *
11558275SEric Cheng 			 * When clients are torn down, the groups
11568275SEric Cheng 			 * are release via mac_release_rx_group which
11578275SEric Cheng 			 * knows the the default group is always in
11588275SEric Cheng 			 * started mode since broadcast uses it. So
11598275SEric Cheng 			 * we can assert that their are no clients
11608275SEric Cheng 			 * (since mac_bcast_add doesn't register itself
11618275SEric Cheng 			 * as a client) and group is in SHARED state.
11620Sstevel@tonic-gate 			 */
11638275SEric Cheng 			ASSERT(grp->mrg_state == MAC_GROUP_STATE_SHARED);
116411878SVenu.Iyer@Sun.COM 			ASSERT(MAC_GROUP_NO_CLIENT(grp) &&
11658275SEric Cheng 			    mip->mi_nactiveclients == 0);
11668275SEric Cheng 			mac_stop_group_and_rings(grp);
116711878SVenu.Iyer@Sun.COM 			mac_set_group_state(grp, MAC_GROUP_STATE_REGISTERED);
11680Sstevel@tonic-gate 		}
11698275SEric Cheng 
11708275SEric Cheng 		if (mip->mi_default_tx_ring != NULL) {
11718275SEric Cheng 			mac_ring_t *ring;
11728275SEric Cheng 
11738275SEric Cheng 			ring = (mac_ring_t *)mip->mi_default_tx_ring;
117411878SVenu.Iyer@Sun.COM 			if (ring->mr_state == MR_INUSE) {
117511878SVenu.Iyer@Sun.COM 				mac_stop_ring(ring);
117611878SVenu.Iyer@Sun.COM 				ring->mr_flag = 0;
117711878SVenu.Iyer@Sun.COM 			}
11788275SEric Cheng 		}
11798275SEric Cheng 
11808275SEric Cheng 		/*
11818275SEric Cheng 		 * Stop the device.
11828275SEric Cheng 		 */
11838275SEric Cheng 		mip->mi_stop(mip->mi_driver);
11842331Skrgopi 	}
11852331Skrgopi }
11862331Skrgopi 
11870Sstevel@tonic-gate int
i_mac_promisc_set(mac_impl_t * mip,boolean_t on)11889641SGirish.Moodalbail@Sun.COM i_mac_promisc_set(mac_impl_t *mip, boolean_t on)
11890Sstevel@tonic-gate {
11900Sstevel@tonic-gate 	int		err = 0;
11910Sstevel@tonic-gate 
11928275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
11932311Sseb 	ASSERT(mip->mi_setpromisc != NULL);
11949641SGirish.Moodalbail@Sun.COM 
11950Sstevel@tonic-gate 	if (on) {
11960Sstevel@tonic-gate 		/*
11970Sstevel@tonic-gate 		 * Enable promiscuous mode on the device if not yet enabled.
11980Sstevel@tonic-gate 		 */
11990Sstevel@tonic-gate 		if (mip->mi_devpromisc++ == 0) {
12002311Sseb 			err = mip->mi_setpromisc(mip->mi_driver, B_TRUE);
12012311Sseb 			if (err != 0) {
12020Sstevel@tonic-gate 				mip->mi_devpromisc--;
12038275SEric Cheng 				return (err);
12040Sstevel@tonic-gate 			}
12050Sstevel@tonic-gate 			i_mac_notify(mip, MAC_NOTE_DEVPROMISC);
12060Sstevel@tonic-gate 		}
12070Sstevel@tonic-gate 	} else {
12088275SEric Cheng 		if (mip->mi_devpromisc == 0)
12098275SEric Cheng 			return (EPROTO);
12108275SEric Cheng 
12110Sstevel@tonic-gate 		/*
12120Sstevel@tonic-gate 		 * Disable promiscuous mode on the device if this is the last
12130Sstevel@tonic-gate 		 * enabling.
12140Sstevel@tonic-gate 		 */
12150Sstevel@tonic-gate 		if (--mip->mi_devpromisc == 0) {
12162311Sseb 			err = mip->mi_setpromisc(mip->mi_driver, B_FALSE);
12172311Sseb 			if (err != 0) {
12180Sstevel@tonic-gate 				mip->mi_devpromisc++;
12198275SEric Cheng 				return (err);
12200Sstevel@tonic-gate 			}
12210Sstevel@tonic-gate 			i_mac_notify(mip, MAC_NOTE_DEVPROMISC);
12220Sstevel@tonic-gate 		}
12230Sstevel@tonic-gate 	}
12240Sstevel@tonic-gate 
12258275SEric Cheng 	return (0);
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate 
12288275SEric Cheng /*
12298275SEric Cheng  * The promiscuity state can change any time. If the caller needs to take
12308275SEric Cheng  * actions that are atomic with the promiscuity state, then the caller needs
12318275SEric Cheng  * to bracket the entire sequence with mac_perim_enter/exit
12328275SEric Cheng  */
12330Sstevel@tonic-gate boolean_t
mac_promisc_get(mac_handle_t mh)12349641SGirish.Moodalbail@Sun.COM mac_promisc_get(mac_handle_t mh)
12350Sstevel@tonic-gate {
12360Sstevel@tonic-gate 	mac_impl_t		*mip = (mac_impl_t *)mh;
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	/*
12390Sstevel@tonic-gate 	 * Return the current promiscuity.
12400Sstevel@tonic-gate 	 */
12419641SGirish.Moodalbail@Sun.COM 	return (mip->mi_devpromisc != 0);
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate 
12448275SEric Cheng /*
12458275SEric Cheng  * Invoked at MAC instance attach time to initialize the list
12468275SEric Cheng  * of factory MAC addresses supported by a MAC instance. This function
12478275SEric Cheng  * builds a local cache in the mac_impl_t for the MAC addresses
12488275SEric Cheng  * supported by the underlying hardware. The MAC clients themselves
12498275SEric Cheng  * use the mac_addr_factory*() functions to query and reserve
12508275SEric Cheng  * factory MAC addresses.
12518275SEric Cheng  */
12520Sstevel@tonic-gate void
mac_addr_factory_init(mac_impl_t * mip)12538275SEric Cheng mac_addr_factory_init(mac_impl_t *mip)
12545903Ssowmini {
12558275SEric Cheng 	mac_capab_multifactaddr_t capab;
12568275SEric Cheng 	uint8_t *addr;
12578275SEric Cheng 	int i;
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	/*
12608275SEric Cheng 	 * First round to see how many factory MAC addresses are available.
12610Sstevel@tonic-gate 	 */
12628275SEric Cheng 	bzero(&capab, sizeof (capab));
12638275SEric Cheng 	if (!i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_MULTIFACTADDR,
12648275SEric Cheng 	    &capab) || (capab.mcm_naddr == 0)) {
12656512Ssowmini 		/*
12668275SEric Cheng 		 * The MAC instance doesn't support multiple factory
12678275SEric Cheng 		 * MAC addresses, we're done here.
12686512Ssowmini 		 */
12696512Ssowmini 		return;
12705903Ssowmini 	}
12716512Ssowmini 
12720Sstevel@tonic-gate 	/*
12738275SEric Cheng 	 * Allocate the space and get all the factory addresses.
127456Smeem 	 */
12758275SEric Cheng 	addr = kmem_alloc(capab.mcm_naddr * MAXMACADDRLEN, KM_SLEEP);
12768275SEric Cheng 	capab.mcm_getaddr(mip->mi_driver, capab.mcm_naddr, addr);
12778275SEric Cheng 
12788275SEric Cheng 	mip->mi_factory_addr_num = capab.mcm_naddr;
12798275SEric Cheng 	mip->mi_factory_addr = kmem_zalloc(mip->mi_factory_addr_num *
12808275SEric Cheng 	    sizeof (mac_factory_addr_t), KM_SLEEP);
12818275SEric Cheng 
12828275SEric Cheng 	for (i = 0; i < capab.mcm_naddr; i++) {
12838275SEric Cheng 		bcopy(addr + i * MAXMACADDRLEN,
12848275SEric Cheng 		    mip->mi_factory_addr[i].mfa_addr,
12858275SEric Cheng 		    mip->mi_type->mt_addr_length);
12868275SEric Cheng 		mip->mi_factory_addr[i].mfa_in_use = B_FALSE;
128756Smeem 	}
128856Smeem 
12898275SEric Cheng 	kmem_free(addr, capab.mcm_naddr * MAXMACADDRLEN);
12908275SEric Cheng }
12918275SEric Cheng 
12928275SEric Cheng void
mac_addr_factory_fini(mac_impl_t * mip)12938275SEric Cheng mac_addr_factory_fini(mac_impl_t *mip)
12948275SEric Cheng {
12958275SEric Cheng 	if (mip->mi_factory_addr == NULL) {
12968275SEric Cheng 		ASSERT(mip->mi_factory_addr_num == 0);
12978275SEric Cheng 		return;
12988275SEric Cheng 	}
12998275SEric Cheng 
13008275SEric Cheng 	kmem_free(mip->mi_factory_addr, mip->mi_factory_addr_num *
13018275SEric Cheng 	    sizeof (mac_factory_addr_t));
13028275SEric Cheng 
13038275SEric Cheng 	mip->mi_factory_addr = NULL;
13048275SEric Cheng 	mip->mi_factory_addr_num = 0;
13050Sstevel@tonic-gate }
13060Sstevel@tonic-gate 
13075084Sjohnlev /*
13088275SEric Cheng  * Reserve a factory MAC address. If *slot is set to -1, the function
13098275SEric Cheng  * attempts to reserve any of the available factory MAC addresses and
13108275SEric Cheng  * returns the reserved slot id. If no slots are available, the function
13118275SEric Cheng  * returns ENOSPC. If *slot is not set to -1, the function reserves
13128275SEric Cheng  * the specified slot if it is available, or returns EBUSY is the slot
13138275SEric Cheng  * is already used. Returns ENOTSUP if the underlying MAC does not
13148275SEric Cheng  * support multiple factory addresses. If the slot number is not -1 but
13158275SEric Cheng  * is invalid, returns EINVAL.
13168275SEric Cheng  */
13178275SEric Cheng int
mac_addr_factory_reserve(mac_client_handle_t mch,int * slot)13188275SEric Cheng mac_addr_factory_reserve(mac_client_handle_t mch, int *slot)
13198275SEric Cheng {
13208275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
13218275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
13228275SEric Cheng 	int i, ret = 0;
13238275SEric Cheng 
13248275SEric Cheng 	i_mac_perim_enter(mip);
13258275SEric Cheng 	/*
13268275SEric Cheng 	 * Protect against concurrent readers that may need a self-consistent
13278275SEric Cheng 	 * view of the factory addresses
13288275SEric Cheng 	 */
13298275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
13308275SEric Cheng 
13318275SEric Cheng 	if (mip->mi_factory_addr_num == 0) {
13328275SEric Cheng 		ret = ENOTSUP;
13338275SEric Cheng 		goto bail;
13348275SEric Cheng 	}
13358275SEric Cheng 
13368275SEric Cheng 	if (*slot != -1) {
13378275SEric Cheng 		/* check the specified slot */
13388275SEric Cheng 		if (*slot < 1 || *slot > mip->mi_factory_addr_num) {
13398275SEric Cheng 			ret = EINVAL;
13408275SEric Cheng 			goto bail;
13418275SEric Cheng 		}
13428275SEric Cheng 		if (mip->mi_factory_addr[*slot-1].mfa_in_use) {
13438275SEric Cheng 			ret = EBUSY;
13448275SEric Cheng 			goto bail;
13458275SEric Cheng 		}
13468275SEric Cheng 	} else {
13478275SEric Cheng 		/* pick the next available slot */
13488275SEric Cheng 		for (i = 0; i < mip->mi_factory_addr_num; i++) {
13498275SEric Cheng 			if (!mip->mi_factory_addr[i].mfa_in_use)
13508275SEric Cheng 				break;
13518275SEric Cheng 		}
13528275SEric Cheng 
13538275SEric Cheng 		if (i == mip->mi_factory_addr_num) {
13548275SEric Cheng 			ret = ENOSPC;
13558275SEric Cheng 			goto bail;
13568275SEric Cheng 		}
13578275SEric Cheng 		*slot = i+1;
13588275SEric Cheng 	}
13598275SEric Cheng 
13608275SEric Cheng 	mip->mi_factory_addr[*slot-1].mfa_in_use = B_TRUE;
13618275SEric Cheng 	mip->mi_factory_addr[*slot-1].mfa_client = mcip;
13628275SEric Cheng 
13638275SEric Cheng bail:
13648275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
13658275SEric Cheng 	i_mac_perim_exit(mip);
13668275SEric Cheng 	return (ret);
13678275SEric Cheng }
13688275SEric Cheng 
13698275SEric Cheng /*
13708275SEric Cheng  * Release the specified factory MAC address slot.
13715084Sjohnlev  */
13728275SEric Cheng void
mac_addr_factory_release(mac_client_handle_t mch,uint_t slot)13738275SEric Cheng mac_addr_factory_release(mac_client_handle_t mch, uint_t slot)
13748275SEric Cheng {
13758275SEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
13768275SEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
13778275SEric Cheng 
13788275SEric Cheng 	i_mac_perim_enter(mip);
13798275SEric Cheng 	/*
13808275SEric Cheng 	 * Protect against concurrent readers that may need a self-consistent
13818275SEric Cheng 	 * view of the factory addresses
13828275SEric Cheng 	 */
13838275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
13848275SEric Cheng 
13858275SEric Cheng 	ASSERT(slot > 0 && slot <= mip->mi_factory_addr_num);
13868275SEric Cheng 	ASSERT(mip->mi_factory_addr[slot-1].mfa_in_use);
13878275SEric Cheng 
13888275SEric Cheng 	mip->mi_factory_addr[slot-1].mfa_in_use = B_FALSE;
13898275SEric Cheng 
13908275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
13918275SEric Cheng 	i_mac_perim_exit(mip);
13928275SEric Cheng }
13938275SEric Cheng 
13948275SEric Cheng /*
13958275SEric Cheng  * Stores in mac_addr the value of the specified MAC address. Returns
13968275SEric Cheng  * 0 on success, or EINVAL if the slot number is not valid for the MAC.
13978275SEric Cheng  * The caller must provide a string of at least MAXNAMELEN bytes.
13988275SEric Cheng  */
13998275SEric Cheng void
mac_addr_factory_value(mac_handle_t mh,int slot,uchar_t * mac_addr,uint_t * addr_len,char * client_name,boolean_t * in_use_arg)14008275SEric Cheng mac_addr_factory_value(mac_handle_t mh, int slot, uchar_t *mac_addr,
14018275SEric Cheng     uint_t *addr_len, char *client_name, boolean_t *in_use_arg)
14025084Sjohnlev {
14038275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
14048275SEric Cheng 	boolean_t in_use;
14058275SEric Cheng 
14068275SEric Cheng 	ASSERT(slot > 0 && slot <= mip->mi_factory_addr_num);
14078275SEric Cheng 
14088275SEric Cheng 	/*
14098275SEric Cheng 	 * Readers need to hold mi_rw_lock. Writers need to hold mac perimeter
14108275SEric Cheng 	 * and mi_rw_lock
14118275SEric Cheng 	 */
14128275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
14138275SEric Cheng 	bcopy(mip->mi_factory_addr[slot-1].mfa_addr, mac_addr, MAXMACADDRLEN);
14148275SEric Cheng 	*addr_len = mip->mi_type->mt_addr_length;
14158275SEric Cheng 	in_use = mip->mi_factory_addr[slot-1].mfa_in_use;
14168275SEric Cheng 	if (in_use && client_name != NULL) {
14178275SEric Cheng 		bcopy(mip->mi_factory_addr[slot-1].mfa_client->mci_name,
14188275SEric Cheng 		    client_name, MAXNAMELEN);
14198275SEric Cheng 	}
14208275SEric Cheng 	if (in_use_arg != NULL)
14218275SEric Cheng 		*in_use_arg = in_use;
14228275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
14238275SEric Cheng }
14248275SEric Cheng 
14258275SEric Cheng /*
14268275SEric Cheng  * Returns the number of factory MAC addresses (in addition to the
14278275SEric Cheng  * primary MAC address), 0 if the underlying MAC doesn't support
14288275SEric Cheng  * that feature.
14298275SEric Cheng  */
14308275SEric Cheng uint_t
mac_addr_factory_num(mac_handle_t mh)14318275SEric Cheng mac_addr_factory_num(mac_handle_t mh)
14328275SEric Cheng {
14338275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
14348275SEric Cheng 
14358275SEric Cheng 	return (mip->mi_factory_addr_num);
14368275SEric Cheng }
14378275SEric Cheng 
14388275SEric Cheng 
14398275SEric Cheng void
mac_rx_group_unmark(mac_group_t * grp,uint_t flag)14408275SEric Cheng mac_rx_group_unmark(mac_group_t *grp, uint_t flag)
14418275SEric Cheng {
14428275SEric Cheng 	mac_ring_t	*ring;
14438275SEric Cheng 
14448275SEric Cheng 	for (ring = grp->mrg_rings; ring != NULL; ring = ring->mr_next)
14458275SEric Cheng 		ring->mr_flag &= ~flag;
14465084Sjohnlev }
14475084Sjohnlev 
14485084Sjohnlev /*
14498275SEric Cheng  * The following mac_hwrings_xxx() functions are private mac client functions
14508275SEric Cheng  * used by the aggr driver to access and control the underlying HW Rx group
14518275SEric Cheng  * and rings. In this case, the aggr driver has exclusive control of the
14528275SEric Cheng  * underlying HW Rx group/rings, it calls the following functions to
14538275SEric Cheng  * start/stop the HW Rx rings, disable/enable polling, add/remove mac'
14548275SEric Cheng  * addresses, or set up the Rx callback.
14555084Sjohnlev  */
14568275SEric Cheng /* ARGSUSED */
14578275SEric Cheng static void
mac_hwrings_rx_process(void * arg,mac_resource_handle_t srs,mblk_t * mp_chain,boolean_t loopback)14588275SEric Cheng mac_hwrings_rx_process(void *arg, mac_resource_handle_t srs,
14598275SEric Cheng     mblk_t *mp_chain, boolean_t loopback)
14600Sstevel@tonic-gate {
14618275SEric Cheng 	mac_soft_ring_set_t	*mac_srs = (mac_soft_ring_set_t *)srs;
14628275SEric Cheng 	mac_srs_rx_t		*srs_rx = &mac_srs->srs_rx;
14638275SEric Cheng 	mac_direct_rx_t		proc;
14648275SEric Cheng 	void			*arg1;
14658275SEric Cheng 	mac_resource_handle_t	arg2;
14668275SEric Cheng 
14678275SEric Cheng 	proc = srs_rx->sr_func;
14688275SEric Cheng 	arg1 = srs_rx->sr_arg1;
14698275SEric Cheng 	arg2 = mac_srs->srs_mrh;
14708275SEric Cheng 
14718275SEric Cheng 	proc(arg1, arg2, mp_chain, NULL);
14720Sstevel@tonic-gate }
14730Sstevel@tonic-gate 
14748275SEric Cheng /*
14758275SEric Cheng  * This function is called to get the list of HW rings that are reserved by
14768275SEric Cheng  * an exclusive mac client.
14778275SEric Cheng  *
14788275SEric Cheng  * Return value: the number of HW rings.
14798275SEric Cheng  */
14808275SEric Cheng int
mac_hwrings_get(mac_client_handle_t mch,mac_group_handle_t * hwgh,mac_ring_handle_t * hwrh,mac_ring_type_t rtype)14818275SEric Cheng mac_hwrings_get(mac_client_handle_t mch, mac_group_handle_t *hwgh,
148210309SSriharsha.Basavapatna@Sun.COM     mac_ring_handle_t *hwrh, mac_ring_type_t rtype)
14830Sstevel@tonic-gate {
14848275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
148511878SVenu.Iyer@Sun.COM 	flow_entry_t		*flent = mcip->mci_flent;
148611878SVenu.Iyer@Sun.COM 	mac_group_t		*grp;
148711878SVenu.Iyer@Sun.COM 	mac_ring_t		*ring;
14888275SEric Cheng 	int			cnt = 0;
14890Sstevel@tonic-gate 
149011878SVenu.Iyer@Sun.COM 	if (rtype == MAC_RING_TYPE_RX) {
149110309SSriharsha.Basavapatna@Sun.COM 		grp = flent->fe_rx_ring_group;
149211878SVenu.Iyer@Sun.COM 	} else if (rtype == MAC_RING_TYPE_TX) {
149311878SVenu.Iyer@Sun.COM 		grp = flent->fe_tx_ring_group;
149411878SVenu.Iyer@Sun.COM 	} else {
149510309SSriharsha.Basavapatna@Sun.COM 		ASSERT(B_FALSE);
149610309SSriharsha.Basavapatna@Sun.COM 		return (-1);
149710309SSriharsha.Basavapatna@Sun.COM 	}
149811878SVenu.Iyer@Sun.COM 	/*
149911878SVenu.Iyer@Sun.COM 	 * The mac client did not reserve any RX group, return directly.
150011878SVenu.Iyer@Sun.COM 	 * This is probably because the underlying MAC does not support
150111878SVenu.Iyer@Sun.COM 	 * any groups.
150211878SVenu.Iyer@Sun.COM 	 */
150311878SVenu.Iyer@Sun.COM 	if (hwgh != NULL)
150411878SVenu.Iyer@Sun.COM 		*hwgh = NULL;
150511878SVenu.Iyer@Sun.COM 	if (grp == NULL)
150611878SVenu.Iyer@Sun.COM 		return (0);
150711878SVenu.Iyer@Sun.COM 	/*
150811878SVenu.Iyer@Sun.COM 	 * This group must be reserved by this mac client.
150911878SVenu.Iyer@Sun.COM 	 */
151011878SVenu.Iyer@Sun.COM 	ASSERT((grp->mrg_state == MAC_GROUP_STATE_RESERVED) &&
151111878SVenu.Iyer@Sun.COM 	    (mcip == MAC_GROUP_ONLY_CLIENT(grp)));
151211878SVenu.Iyer@Sun.COM 
151311878SVenu.Iyer@Sun.COM 	for (ring = grp->mrg_rings; ring != NULL; ring = ring->mr_next, cnt++) {
151411878SVenu.Iyer@Sun.COM 		ASSERT(cnt < MAX_RINGS_PER_GROUP);
151511878SVenu.Iyer@Sun.COM 		hwrh[cnt] = (mac_ring_handle_t)ring;
151611878SVenu.Iyer@Sun.COM 	}
151711878SVenu.Iyer@Sun.COM 	if (hwgh != NULL)
151811878SVenu.Iyer@Sun.COM 		*hwgh = (mac_group_handle_t)grp;
151911878SVenu.Iyer@Sun.COM 
152011878SVenu.Iyer@Sun.COM 	return (cnt);
15218275SEric Cheng }
15228275SEric Cheng 
15238275SEric Cheng /*
152411878SVenu.Iyer@Sun.COM  * This function is called to get info about Tx/Rx rings.
152511878SVenu.Iyer@Sun.COM  *
152611878SVenu.Iyer@Sun.COM  * Return value: returns uint_t which will have various bits set
152711878SVenu.Iyer@Sun.COM  * that indicates different properties of the ring.
152811878SVenu.Iyer@Sun.COM  */
152911878SVenu.Iyer@Sun.COM uint_t
mac_hwring_getinfo(mac_ring_handle_t rh)153011878SVenu.Iyer@Sun.COM mac_hwring_getinfo(mac_ring_handle_t rh)
153111878SVenu.Iyer@Sun.COM {
153211878SVenu.Iyer@Sun.COM 	mac_ring_t *ring = (mac_ring_t *)rh;
153311878SVenu.Iyer@Sun.COM 	mac_ring_info_t *info = &ring->mr_info;
153411878SVenu.Iyer@Sun.COM 
153511878SVenu.Iyer@Sun.COM 	return (info->mri_flags);
153611878SVenu.Iyer@Sun.COM }
153711878SVenu.Iyer@Sun.COM 
153811878SVenu.Iyer@Sun.COM /*
153911878SVenu.Iyer@Sun.COM  * Export ddi interrupt handles from the HW ring to the pseudo ring and
154011878SVenu.Iyer@Sun.COM  * setup the RX callback of the mac client which exclusively controls
154111878SVenu.Iyer@Sun.COM  * HW ring.
15428275SEric Cheng  */
15438275SEric Cheng void
mac_hwring_setup(mac_ring_handle_t hwrh,mac_resource_handle_t prh,mac_ring_handle_t pseudo_rh)154411878SVenu.Iyer@Sun.COM mac_hwring_setup(mac_ring_handle_t hwrh, mac_resource_handle_t prh,
154511878SVenu.Iyer@Sun.COM     mac_ring_handle_t pseudo_rh)
15468275SEric Cheng {
15478275SEric Cheng 	mac_ring_t		*hw_ring = (mac_ring_t *)hwrh;
154811878SVenu.Iyer@Sun.COM 	mac_ring_t		*pseudo_ring;
15498275SEric Cheng 	mac_soft_ring_set_t	*mac_srs = hw_ring->mr_srs;
15508275SEric Cheng 
155111878SVenu.Iyer@Sun.COM 	if (pseudo_rh != NULL) {
155211878SVenu.Iyer@Sun.COM 		pseudo_ring = (mac_ring_t *)pseudo_rh;
155311878SVenu.Iyer@Sun.COM 		/* Export the ddi handles to pseudo ring */
155411878SVenu.Iyer@Sun.COM 		pseudo_ring->mr_info.mri_intr.mi_ddi_handle =
155511878SVenu.Iyer@Sun.COM 		    hw_ring->mr_info.mri_intr.mi_ddi_handle;
155611878SVenu.Iyer@Sun.COM 		pseudo_ring->mr_info.mri_intr.mi_ddi_shared =
155711878SVenu.Iyer@Sun.COM 		    hw_ring->mr_info.mri_intr.mi_ddi_shared;
155811878SVenu.Iyer@Sun.COM 		/*
155911878SVenu.Iyer@Sun.COM 		 * Save a pointer to pseudo ring in the hw ring. If
156011878SVenu.Iyer@Sun.COM 		 * interrupt handle changes, the hw ring will be
156111878SVenu.Iyer@Sun.COM 		 * notified of the change (see mac_ring_intr_set())
156211878SVenu.Iyer@Sun.COM 		 * and the appropriate change has to be made to
156311878SVenu.Iyer@Sun.COM 		 * the pseudo ring that has exported the ddi handle.
156411878SVenu.Iyer@Sun.COM 		 */
156511878SVenu.Iyer@Sun.COM 		hw_ring->mr_prh = pseudo_rh;
156611878SVenu.Iyer@Sun.COM 	}
156711878SVenu.Iyer@Sun.COM 
156811878SVenu.Iyer@Sun.COM 	if (hw_ring->mr_type == MAC_RING_TYPE_RX) {
156911878SVenu.Iyer@Sun.COM 		ASSERT(!(mac_srs->srs_type & SRST_TX));
157011878SVenu.Iyer@Sun.COM 		mac_srs->srs_mrh = prh;
157111878SVenu.Iyer@Sun.COM 		mac_srs->srs_rx.sr_lower_proc = mac_hwrings_rx_process;
157211878SVenu.Iyer@Sun.COM 	}
15730Sstevel@tonic-gate }
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate void
mac_hwring_teardown(mac_ring_handle_t hwrh)15768275SEric Cheng mac_hwring_teardown(mac_ring_handle_t hwrh)
15778275SEric Cheng {
15788275SEric Cheng 	mac_ring_t		*hw_ring = (mac_ring_t *)hwrh;
157911878SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t	*mac_srs;
158011878SVenu.Iyer@Sun.COM 
158111878SVenu.Iyer@Sun.COM 	if (hw_ring == NULL)
158211878SVenu.Iyer@Sun.COM 		return;
158311878SVenu.Iyer@Sun.COM 	hw_ring->mr_prh = NULL;
158411878SVenu.Iyer@Sun.COM 	if (hw_ring->mr_type == MAC_RING_TYPE_RX) {
158511878SVenu.Iyer@Sun.COM 		mac_srs = hw_ring->mr_srs;
158611878SVenu.Iyer@Sun.COM 		ASSERT(!(mac_srs->srs_type & SRST_TX));
158711878SVenu.Iyer@Sun.COM 		mac_srs->srs_rx.sr_lower_proc = mac_rx_srs_process;
158811878SVenu.Iyer@Sun.COM 		mac_srs->srs_mrh = NULL;
158911878SVenu.Iyer@Sun.COM 	}
15908275SEric Cheng }
15918275SEric Cheng 
15928275SEric Cheng int
mac_hwring_disable_intr(mac_ring_handle_t rh)15938275SEric Cheng mac_hwring_disable_intr(mac_ring_handle_t rh)
15940Sstevel@tonic-gate {
15958275SEric Cheng 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
15968275SEric Cheng 	mac_intr_t *intr = &rr_ring->mr_info.mri_intr;
15978275SEric Cheng 
15988275SEric Cheng 	return (intr->mi_disable(intr->mi_handle));
15998275SEric Cheng }
16008275SEric Cheng 
16018275SEric Cheng int
mac_hwring_enable_intr(mac_ring_handle_t rh)16028275SEric Cheng mac_hwring_enable_intr(mac_ring_handle_t rh)
16038275SEric Cheng {
16048275SEric Cheng 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
16058275SEric Cheng 	mac_intr_t *intr = &rr_ring->mr_info.mri_intr;
16068275SEric Cheng 
16078275SEric Cheng 	return (intr->mi_enable(intr->mi_handle));
16088275SEric Cheng }
16098275SEric Cheng 
16108275SEric Cheng int
mac_hwring_start(mac_ring_handle_t rh)16118275SEric Cheng mac_hwring_start(mac_ring_handle_t rh)
16128275SEric Cheng {
16138275SEric Cheng 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
16148275SEric Cheng 
16158275SEric Cheng 	MAC_RING_UNMARK(rr_ring, MR_QUIESCE);
16168275SEric Cheng 	return (0);
16170Sstevel@tonic-gate }
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate void
mac_hwring_stop(mac_ring_handle_t rh)16208275SEric Cheng mac_hwring_stop(mac_ring_handle_t rh)
16218275SEric Cheng {
16228275SEric Cheng 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
16238275SEric Cheng 
16248275SEric Cheng 	mac_rx_ring_quiesce(rr_ring, MR_QUIESCE);
16258275SEric Cheng }
16268275SEric Cheng 
16278275SEric Cheng mblk_t *
mac_hwring_poll(mac_ring_handle_t rh,int bytes_to_pickup)16288275SEric Cheng mac_hwring_poll(mac_ring_handle_t rh, int bytes_to_pickup)
16298275SEric Cheng {
16308275SEric Cheng 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
16318275SEric Cheng 	mac_ring_info_t *info = &rr_ring->mr_info;
16328275SEric Cheng 
16338275SEric Cheng 	return (info->mri_poll(info->mri_driver, bytes_to_pickup));
16348275SEric Cheng }
16358275SEric Cheng 
163610309SSriharsha.Basavapatna@Sun.COM /*
163711878SVenu.Iyer@Sun.COM  * Send packets through a selected tx ring.
163810309SSriharsha.Basavapatna@Sun.COM  */
163910309SSriharsha.Basavapatna@Sun.COM mblk_t *
mac_hwring_tx(mac_ring_handle_t rh,mblk_t * mp)164010309SSriharsha.Basavapatna@Sun.COM mac_hwring_tx(mac_ring_handle_t rh, mblk_t *mp)
164110309SSriharsha.Basavapatna@Sun.COM {
164210309SSriharsha.Basavapatna@Sun.COM 	mac_ring_t *ring = (mac_ring_t *)rh;
164310309SSriharsha.Basavapatna@Sun.COM 	mac_ring_info_t *info = &ring->mr_info;
164410309SSriharsha.Basavapatna@Sun.COM 
164510491SRishi.Srivatsavai@Sun.COM 	ASSERT(ring->mr_type == MAC_RING_TYPE_TX &&
164610491SRishi.Srivatsavai@Sun.COM 	    ring->mr_state >= MR_INUSE);
164710309SSriharsha.Basavapatna@Sun.COM 	return (info->mri_tx(info->mri_driver, mp));
164810309SSriharsha.Basavapatna@Sun.COM }
164910309SSriharsha.Basavapatna@Sun.COM 
165011878SVenu.Iyer@Sun.COM /*
165111878SVenu.Iyer@Sun.COM  * Query stats for a particular rx/tx ring
165211878SVenu.Iyer@Sun.COM  */
165311878SVenu.Iyer@Sun.COM int
mac_hwring_getstat(mac_ring_handle_t rh,uint_t stat,uint64_t * val)165411878SVenu.Iyer@Sun.COM mac_hwring_getstat(mac_ring_handle_t rh, uint_t stat, uint64_t *val)
165511878SVenu.Iyer@Sun.COM {
165611878SVenu.Iyer@Sun.COM 	mac_ring_t	*ring = (mac_ring_t *)rh;
165711878SVenu.Iyer@Sun.COM 	mac_ring_info_t *info = &ring->mr_info;
165811878SVenu.Iyer@Sun.COM 
165911878SVenu.Iyer@Sun.COM 	return (info->mri_stat(info->mri_driver, stat, val));
166011878SVenu.Iyer@Sun.COM }
166111878SVenu.Iyer@Sun.COM 
166211878SVenu.Iyer@Sun.COM /*
166311878SVenu.Iyer@Sun.COM  * Private function that is only used by aggr to send packets through
166411878SVenu.Iyer@Sun.COM  * a port/Tx ring. Since aggr exposes a pseudo Tx ring even for ports
166511878SVenu.Iyer@Sun.COM  * that does not expose Tx rings, aggr_ring_tx() entry point needs
166611878SVenu.Iyer@Sun.COM  * access to mac_impl_t to send packets through m_tx() entry point.
166711878SVenu.Iyer@Sun.COM  * It accomplishes this by calling mac_hwring_send_priv() function.
166811878SVenu.Iyer@Sun.COM  */
166911878SVenu.Iyer@Sun.COM mblk_t *
mac_hwring_send_priv(mac_client_handle_t mch,mac_ring_handle_t rh,mblk_t * mp)167011878SVenu.Iyer@Sun.COM mac_hwring_send_priv(mac_client_handle_t mch, mac_ring_handle_t rh, mblk_t *mp)
167111878SVenu.Iyer@Sun.COM {
167211878SVenu.Iyer@Sun.COM 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
167311878SVenu.Iyer@Sun.COM 	mac_impl_t *mip = mcip->mci_mip;
167411878SVenu.Iyer@Sun.COM 
167511878SVenu.Iyer@Sun.COM 	MAC_TX(mip, rh, mp, mcip);
167611878SVenu.Iyer@Sun.COM 	return (mp);
167711878SVenu.Iyer@Sun.COM }
167811878SVenu.Iyer@Sun.COM 
16798275SEric Cheng int
mac_hwgroup_addmac(mac_group_handle_t gh,const uint8_t * addr)16808275SEric Cheng mac_hwgroup_addmac(mac_group_handle_t gh, const uint8_t *addr)
16818275SEric Cheng {
16828275SEric Cheng 	mac_group_t *group = (mac_group_t *)gh;
16838275SEric Cheng 
16848275SEric Cheng 	return (mac_group_addmac(group, addr));
16858275SEric Cheng }
16868275SEric Cheng 
16878275SEric Cheng int
mac_hwgroup_remmac(mac_group_handle_t gh,const uint8_t * addr)16888275SEric Cheng mac_hwgroup_remmac(mac_group_handle_t gh, const uint8_t *addr)
16898275SEric Cheng {
16908275SEric Cheng 	mac_group_t *group = (mac_group_t *)gh;
16918275SEric Cheng 
16928275SEric Cheng 	return (mac_group_remmac(group, addr));
16938275SEric Cheng }
16948275SEric Cheng 
16958275SEric Cheng /*
16968275SEric Cheng  * Set the RX group to be shared/reserved. Note that the group must be
16978275SEric Cheng  * started/stopped outside of this function.
16988275SEric Cheng  */
16998275SEric Cheng void
mac_set_group_state(mac_group_t * grp,mac_group_state_t state)170011878SVenu.Iyer@Sun.COM mac_set_group_state(mac_group_t *grp, mac_group_state_t state)
17010Sstevel@tonic-gate {
17028275SEric Cheng 	/*
17038275SEric Cheng 	 * If there is no change in the group state, just return.
17048275SEric Cheng 	 */
17058275SEric Cheng 	if (grp->mrg_state == state)
17068275SEric Cheng 		return;
17078275SEric Cheng 
17088275SEric Cheng 	switch (state) {
17098275SEric Cheng 	case MAC_GROUP_STATE_RESERVED:
17108275SEric Cheng 		/*
17118275SEric Cheng 		 * Successfully reserved the group.
17128275SEric Cheng 		 *
17138275SEric Cheng 		 * Given that there is an exclusive client controlling this
17148275SEric Cheng 		 * group, we enable the group level polling when available,
17158275SEric Cheng 		 * so that SRSs get to turn on/off individual rings they's
17168275SEric Cheng 		 * assigned to.
17178275SEric Cheng 		 */
17188275SEric Cheng 		ASSERT(MAC_PERIM_HELD(grp->mrg_mh));
17198275SEric Cheng 
172011878SVenu.Iyer@Sun.COM 		if (grp->mrg_type == MAC_RING_TYPE_RX &&
172111878SVenu.Iyer@Sun.COM 		    GROUP_INTR_DISABLE_FUNC(grp) != NULL) {
17228275SEric Cheng 			GROUP_INTR_DISABLE_FUNC(grp)(GROUP_INTR_HANDLE(grp));
172311878SVenu.Iyer@Sun.COM 		}
17248275SEric Cheng 		break;
17258275SEric Cheng 
17268275SEric Cheng 	case MAC_GROUP_STATE_SHARED:
17278275SEric Cheng 		/*
17288275SEric Cheng 		 * Set all rings of this group to software classified.
17298275SEric Cheng 		 * If the group has an overriding interrupt, then re-enable it.
17308275SEric Cheng 		 */
17318275SEric Cheng 		ASSERT(MAC_PERIM_HELD(grp->mrg_mh));
17328275SEric Cheng 
173311878SVenu.Iyer@Sun.COM 		if (grp->mrg_type == MAC_RING_TYPE_RX &&
173411878SVenu.Iyer@Sun.COM 		    GROUP_INTR_ENABLE_FUNC(grp) != NULL) {
17358275SEric Cheng 			GROUP_INTR_ENABLE_FUNC(grp)(GROUP_INTR_HANDLE(grp));
173611878SVenu.Iyer@Sun.COM 		}
17378275SEric Cheng 		/* The ring is not available for reservations any more */
17388275SEric Cheng 		break;
17398275SEric Cheng 
17408275SEric Cheng 	case MAC_GROUP_STATE_REGISTERED:
17418275SEric Cheng 		/* Also callable from mac_register, perim is not held */
17428275SEric Cheng 		break;
17438275SEric Cheng 
17448275SEric Cheng 	default:
17458275SEric Cheng 		ASSERT(B_FALSE);
17468275SEric Cheng 		break;
17478275SEric Cheng 	}
17488275SEric Cheng 
17498275SEric Cheng 	grp->mrg_state = state;
17508275SEric Cheng }
17518275SEric Cheng 
17528275SEric Cheng /*
17538275SEric Cheng  * Quiesce future hardware classified packets for the specified Rx ring
17548275SEric Cheng  */
17558275SEric Cheng static void
mac_rx_ring_quiesce(mac_ring_t * rx_ring,uint_t ring_flag)17568275SEric Cheng mac_rx_ring_quiesce(mac_ring_t *rx_ring, uint_t ring_flag)
17578275SEric Cheng {
17588275SEric Cheng 	ASSERT(rx_ring->mr_classify_type == MAC_HW_CLASSIFIER);
17598275SEric Cheng 	ASSERT(ring_flag == MR_CONDEMNED || ring_flag  == MR_QUIESCE);
17608275SEric Cheng 
17618275SEric Cheng 	mutex_enter(&rx_ring->mr_lock);
17628275SEric Cheng 	rx_ring->mr_flag |= ring_flag;
17638275SEric Cheng 	while (rx_ring->mr_refcnt != 0)
17648275SEric Cheng 		cv_wait(&rx_ring->mr_cv, &rx_ring->mr_lock);
17658275SEric Cheng 	mutex_exit(&rx_ring->mr_lock);
17660Sstevel@tonic-gate }
17670Sstevel@tonic-gate 
17684913Sethindra /*
17698275SEric Cheng  * Please see mac_tx for details about the per cpu locking scheme
17704913Sethindra  */
17718275SEric Cheng static void
mac_tx_lock_all(mac_client_impl_t * mcip)17728275SEric Cheng mac_tx_lock_all(mac_client_impl_t *mcip)
17738275SEric Cheng {
17748275SEric Cheng 	int	i;
17758275SEric Cheng 
17768275SEric Cheng 	for (i = 0; i <= mac_tx_percpu_cnt; i++)
17778275SEric Cheng 		mutex_enter(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
17788275SEric Cheng }
17798275SEric Cheng 
17808275SEric Cheng static void
mac_tx_unlock_all(mac_client_impl_t * mcip)17818275SEric Cheng mac_tx_unlock_all(mac_client_impl_t *mcip)
17828275SEric Cheng {
17838275SEric Cheng 	int	i;
17848275SEric Cheng 
17858275SEric Cheng 	for (i = mac_tx_percpu_cnt; i >= 0; i--)
17868275SEric Cheng 		mutex_exit(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
17878275SEric Cheng }
17888275SEric Cheng 
17898275SEric Cheng static void
mac_tx_unlock_allbutzero(mac_client_impl_t * mcip)17908275SEric Cheng mac_tx_unlock_allbutzero(mac_client_impl_t *mcip)
17918275SEric Cheng {
17928275SEric Cheng 	int	i;
17938275SEric Cheng 
17948275SEric Cheng 	for (i = mac_tx_percpu_cnt; i > 0; i--)
17958275SEric Cheng 		mutex_exit(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
17968275SEric Cheng }
17978275SEric Cheng 
17988275SEric Cheng static int
mac_tx_sum_refcnt(mac_client_impl_t * mcip)17998275SEric Cheng mac_tx_sum_refcnt(mac_client_impl_t *mcip)
18000Sstevel@tonic-gate {
18018275SEric Cheng 	int	i;
18028275SEric Cheng 	int	refcnt = 0;
18038275SEric Cheng 
18048275SEric Cheng 	for (i = 0; i <= mac_tx_percpu_cnt; i++)
18058275SEric Cheng 		refcnt += mcip->mci_tx_pcpu[i].pcpu_tx_refcnt;
18068275SEric Cheng 
18078275SEric Cheng 	return (refcnt);
18080Sstevel@tonic-gate }
18090Sstevel@tonic-gate 
18108275SEric Cheng /*
18118275SEric Cheng  * Stop future Tx packets coming down from the client in preparation for
18128275SEric Cheng  * quiescing the Tx side. This is needed for dynamic reclaim and reassignment
18138275SEric Cheng  * of rings between clients
18148275SEric Cheng  */
18158275SEric Cheng void
mac_tx_client_block(mac_client_impl_t * mcip)18168275SEric Cheng mac_tx_client_block(mac_client_impl_t *mcip)
18175084Sjohnlev {
18188275SEric Cheng 	mac_tx_lock_all(mcip);
18198275SEric Cheng 	mcip->mci_tx_flag |= MCI_TX_QUIESCE;
18208275SEric Cheng 	while (mac_tx_sum_refcnt(mcip) != 0) {
18218275SEric Cheng 		mac_tx_unlock_allbutzero(mcip);
18228275SEric Cheng 		cv_wait(&mcip->mci_tx_cv, &mcip->mci_tx_pcpu[0].pcpu_tx_lock);
18238275SEric Cheng 		mutex_exit(&mcip->mci_tx_pcpu[0].pcpu_tx_lock);
18248275SEric Cheng 		mac_tx_lock_all(mcip);
18258275SEric Cheng 	}
18268275SEric Cheng 	mac_tx_unlock_all(mcip);
18275084Sjohnlev }
18285084Sjohnlev 
18298275SEric Cheng void
mac_tx_client_unblock(mac_client_impl_t * mcip)18308275SEric Cheng mac_tx_client_unblock(mac_client_impl_t *mcip)
18315084Sjohnlev {
18328275SEric Cheng 	mac_tx_lock_all(mcip);
18338275SEric Cheng 	mcip->mci_tx_flag &= ~MCI_TX_QUIESCE;
18348275SEric Cheng 	mac_tx_unlock_all(mcip);
18358833SVenu.Iyer@Sun.COM 	/*
18368833SVenu.Iyer@Sun.COM 	 * We may fail to disable flow control for the last MAC_NOTE_TX
18378833SVenu.Iyer@Sun.COM 	 * notification because the MAC client is quiesced. Send the
18388833SVenu.Iyer@Sun.COM 	 * notification again.
18398833SVenu.Iyer@Sun.COM 	 */
18408833SVenu.Iyer@Sun.COM 	i_mac_notify(mcip->mci_mip, MAC_NOTE_TX);
18415084Sjohnlev }
18425084Sjohnlev 
18430Sstevel@tonic-gate /*
18448275SEric Cheng  * Wait for an SRS to quiesce. The SRS worker will signal us when the
18458275SEric Cheng  * quiesce is done.
18468275SEric Cheng  */
18478275SEric Cheng static void
mac_srs_quiesce_wait(mac_soft_ring_set_t * srs,uint_t srs_flag)18488275SEric Cheng mac_srs_quiesce_wait(mac_soft_ring_set_t *srs, uint_t srs_flag)
18498275SEric Cheng {
18508275SEric Cheng 	mutex_enter(&srs->srs_lock);
18518275SEric Cheng 	while (!(srs->srs_state & srs_flag))
18528275SEric Cheng 		cv_wait(&srs->srs_quiesce_done_cv, &srs->srs_lock);
18538275SEric Cheng 	mutex_exit(&srs->srs_lock);
18548275SEric Cheng }
18558275SEric Cheng 
18568275SEric Cheng /*
18578275SEric Cheng  * Quiescing an Rx SRS is achieved by the following sequence. The protocol
18588275SEric Cheng  * works bottom up by cutting off packet flow from the bottommost point in the
18598275SEric Cheng  * mac, then the SRS, and then the soft rings. There are 2 use cases of this
18608275SEric Cheng  * mechanism. One is a temporary quiesce of the SRS, such as say while changing
18618275SEric Cheng  * the Rx callbacks. Another use case is Rx SRS teardown. In the former case
18628275SEric Cheng  * the QUIESCE prefix/suffix is used and in the latter the CONDEMNED is used
18638275SEric Cheng  * for the SRS and MR flags. In the former case the threads pause waiting for
18648275SEric Cheng  * a restart, while in the latter case the threads exit. The Tx SRS teardown
18658275SEric Cheng  * is also mostly similar to the above.
18668275SEric Cheng  *
18678275SEric Cheng  * 1. Stop future hardware classified packets at the lowest level in the mac.
18688275SEric Cheng  *    Remove any hardware classification rule (CONDEMNED case) and mark the
18698275SEric Cheng  *    rings as CONDEMNED or QUIESCE as appropriate. This prevents the mr_refcnt
18708275SEric Cheng  *    from increasing. Upcalls from the driver that come through hardware
18718275SEric Cheng  *    classification will be dropped in mac_rx from now on. Then we wait for
18728275SEric Cheng  *    the mr_refcnt to drop to zero. When the mr_refcnt reaches zero we are
18738275SEric Cheng  *    sure there aren't any upcall threads from the driver through hardware
18748275SEric Cheng  *    classification. In the case of SRS teardown we also remove the
18758275SEric Cheng  *    classification rule in the driver.
18768275SEric Cheng  *
18778275SEric Cheng  * 2. Stop future software classified packets by marking the flow entry with
18788275SEric Cheng  *    FE_QUIESCE or FE_CONDEMNED as appropriate which prevents the refcnt from
18798275SEric Cheng  *    increasing. We also remove the flow entry from the table in the latter
18808275SEric Cheng  *    case. Then wait for the fe_refcnt to reach an appropriate quiescent value
18818275SEric Cheng  *    that indicates there aren't any active threads using that flow entry.
18828275SEric Cheng  *
18838275SEric Cheng  * 3. Quiesce the SRS and softrings by signaling the SRS. The SRS poll thread,
18848275SEric Cheng  *    SRS worker thread, and the soft ring threads are quiesced in sequence
18858275SEric Cheng  *    with the SRS worker thread serving as a master controller. This
18868275SEric Cheng  *    mechansim is explained in mac_srs_worker_quiesce().
18878275SEric Cheng  *
18888275SEric Cheng  * The restart mechanism to reactivate the SRS and softrings is explained
18898275SEric Cheng  * in mac_srs_worker_restart(). Here we just signal the SRS worker to start the
18908275SEric Cheng  * restart sequence.
18910Sstevel@tonic-gate  */
18920Sstevel@tonic-gate void
mac_rx_srs_quiesce(mac_soft_ring_set_t * srs,uint_t srs_quiesce_flag)18938275SEric Cheng mac_rx_srs_quiesce(mac_soft_ring_set_t *srs, uint_t srs_quiesce_flag)
18940Sstevel@tonic-gate {
18958275SEric Cheng 	flow_entry_t	*flent = srs->srs_flent;
18968275SEric Cheng 	uint_t	mr_flag, srs_done_flag;
18978275SEric Cheng 
18988275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)FLENT_TO_MIP(flent)));
18998275SEric Cheng 	ASSERT(!(srs->srs_type & SRST_TX));
19008275SEric Cheng 
19018275SEric Cheng 	if (srs_quiesce_flag == SRS_CONDEMNED) {
19028275SEric Cheng 		mr_flag = MR_CONDEMNED;
19038275SEric Cheng 		srs_done_flag = SRS_CONDEMNED_DONE;
19048275SEric Cheng 		if (srs->srs_type & SRST_CLIENT_POLL_ENABLED)
19058275SEric Cheng 			mac_srs_client_poll_disable(srs->srs_mcip, srs);
19068275SEric Cheng 	} else {
19078275SEric Cheng 		ASSERT(srs_quiesce_flag == SRS_QUIESCE);
19088275SEric Cheng 		mr_flag = MR_QUIESCE;
19098275SEric Cheng 		srs_done_flag = SRS_QUIESCE_DONE;
19108275SEric Cheng 		if (srs->srs_type & SRST_CLIENT_POLL_ENABLED)
19118275SEric Cheng 			mac_srs_client_poll_quiesce(srs->srs_mcip, srs);
19128275SEric Cheng 	}
19138275SEric Cheng 
19148275SEric Cheng 	if (srs->srs_ring != NULL) {
19158275SEric Cheng 		mac_rx_ring_quiesce(srs->srs_ring, mr_flag);
19168275SEric Cheng 	} else {
19178275SEric Cheng 		/*
19188275SEric Cheng 		 * SRS is driven by software classification. In case
19198275SEric Cheng 		 * of CONDEMNED, the top level teardown functions will
19208275SEric Cheng 		 * deal with flow removal.
19218275SEric Cheng 		 */
19228275SEric Cheng 		if (srs_quiesce_flag != SRS_CONDEMNED) {
19238275SEric Cheng 			FLOW_MARK(flent, FE_QUIESCE);
19248275SEric Cheng 			mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
19258275SEric Cheng 		}
19268275SEric Cheng 	}
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 	/*
19298275SEric Cheng 	 * Signal the SRS to quiesce itself, and then cv_wait for the
19308275SEric Cheng 	 * SRS quiesce to complete. The SRS worker thread will wake us
19318275SEric Cheng 	 * up when the quiesce is complete
19324913Sethindra 	 */
19338275SEric Cheng 	mac_srs_signal(srs, srs_quiesce_flag);
19348275SEric Cheng 	mac_srs_quiesce_wait(srs, srs_done_flag);
19354913Sethindra }
19364913Sethindra 
19374913Sethindra /*
19388275SEric Cheng  * Remove an SRS.
19394913Sethindra  */
19404913Sethindra void
mac_rx_srs_remove(mac_soft_ring_set_t * srs)19418275SEric Cheng mac_rx_srs_remove(mac_soft_ring_set_t *srs)
19424913Sethindra {
19438275SEric Cheng 	flow_entry_t *flent = srs->srs_flent;
19448275SEric Cheng 	int i;
19458275SEric Cheng 
19468275SEric Cheng 	mac_rx_srs_quiesce(srs, SRS_CONDEMNED);
19478275SEric Cheng 	/*
19488275SEric Cheng 	 * Locate and remove our entry in the fe_rx_srs[] array, and
19498275SEric Cheng 	 * adjust the fe_rx_srs array entries and array count by
19508275SEric Cheng 	 * moving the last entry into the vacated spot.
19518275SEric Cheng 	 */
19528275SEric Cheng 	mutex_enter(&flent->fe_lock);
19538275SEric Cheng 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
19548275SEric Cheng 		if (flent->fe_rx_srs[i] == srs)
19558275SEric Cheng 			break;
19564913Sethindra 	}
19578275SEric Cheng 
19588275SEric Cheng 	ASSERT(i != 0 && i < flent->fe_rx_srs_cnt);
19598275SEric Cheng 	if (i != flent->fe_rx_srs_cnt - 1) {
19608275SEric Cheng 		flent->fe_rx_srs[i] =
19618275SEric Cheng 		    flent->fe_rx_srs[flent->fe_rx_srs_cnt - 1];
19628275SEric Cheng 		i = flent->fe_rx_srs_cnt - 1;
19638275SEric Cheng 	}
19648275SEric Cheng 
19658275SEric Cheng 	flent->fe_rx_srs[i] = NULL;
19668275SEric Cheng 	flent->fe_rx_srs_cnt--;
19678275SEric Cheng 	mutex_exit(&flent->fe_lock);
19688275SEric Cheng 
19698275SEric Cheng 	mac_srs_free(srs);
19700Sstevel@tonic-gate }
19710Sstevel@tonic-gate 
19728275SEric Cheng static void
mac_srs_clear_flag(mac_soft_ring_set_t * srs,uint_t flag)19738275SEric Cheng mac_srs_clear_flag(mac_soft_ring_set_t *srs, uint_t flag)
19740Sstevel@tonic-gate {
19758275SEric Cheng 	mutex_enter(&srs->srs_lock);
19768275SEric Cheng 	srs->srs_state &= ~flag;
19778275SEric Cheng 	mutex_exit(&srs->srs_lock);
19788275SEric Cheng }
19798275SEric Cheng 
19808275SEric Cheng void
mac_rx_srs_restart(mac_soft_ring_set_t * srs)19818275SEric Cheng mac_rx_srs_restart(mac_soft_ring_set_t *srs)
19828275SEric Cheng {
19838275SEric Cheng 	flow_entry_t	*flent = srs->srs_flent;
19848275SEric Cheng 	mac_ring_t	*mr;
19858275SEric Cheng 
19868275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)FLENT_TO_MIP(flent)));
19878275SEric Cheng 	ASSERT((srs->srs_type & SRST_TX) == 0);
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 	/*
19908275SEric Cheng 	 * This handles a change in the number of SRSs between the quiesce and
19918275SEric Cheng 	 * and restart operation of a flow.
19928275SEric Cheng 	 */
19938275SEric Cheng 	if (!SRS_QUIESCED(srs))
19948275SEric Cheng 		return;
19958275SEric Cheng 
19968275SEric Cheng 	/*
19978275SEric Cheng 	 * Signal the SRS to restart itself. Wait for the restart to complete
19988275SEric Cheng 	 * Note that we only restart the SRS if it is not marked as
19998275SEric Cheng 	 * permanently quiesced.
20000Sstevel@tonic-gate 	 */
20018275SEric Cheng 	if (!SRS_QUIESCED_PERMANENT(srs)) {
20028275SEric Cheng 		mac_srs_signal(srs, SRS_RESTART);
20038275SEric Cheng 		mac_srs_quiesce_wait(srs, SRS_RESTART_DONE);
20048275SEric Cheng 		mac_srs_clear_flag(srs, SRS_RESTART_DONE);
20058275SEric Cheng 
20068275SEric Cheng 		mac_srs_client_poll_restart(srs->srs_mcip, srs);
20078275SEric Cheng 	}
20088275SEric Cheng 
20098275SEric Cheng 	/* Finally clear the flags to let the packets in */
20108275SEric Cheng 	mr = srs->srs_ring;
20118275SEric Cheng 	if (mr != NULL) {
20128275SEric Cheng 		MAC_RING_UNMARK(mr, MR_QUIESCE);
20138275SEric Cheng 		/* In case the ring was stopped, safely restart it */
201411878SVenu.Iyer@Sun.COM 		if (mr->mr_state != MR_INUSE)
201511878SVenu.Iyer@Sun.COM 			(void) mac_start_ring(mr);
20168275SEric Cheng 	} else {
20178275SEric Cheng 		FLOW_UNMARK(flent, FE_QUIESCE);
20188275SEric Cheng 	}
20198275SEric Cheng }
20208275SEric Cheng 
20218275SEric Cheng /*
20228275SEric Cheng  * Temporary quiesce of a flow and associated Rx SRS.
20238275SEric Cheng  * Please see block comment above mac_rx_classify_flow_rem.
20248275SEric Cheng  */
20258275SEric Cheng /* ARGSUSED */
20268275SEric Cheng int
mac_rx_classify_flow_quiesce(flow_entry_t * flent,void * arg)20278275SEric Cheng mac_rx_classify_flow_quiesce(flow_entry_t *flent, void *arg)
20288275SEric Cheng {
20298275SEric Cheng 	int		i;
20308275SEric Cheng 
20318275SEric Cheng 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
20328275SEric Cheng 		mac_rx_srs_quiesce((mac_soft_ring_set_t *)flent->fe_rx_srs[i],
20338275SEric Cheng 		    SRS_QUIESCE);
20348275SEric Cheng 	}
20358275SEric Cheng 	return (0);
20360Sstevel@tonic-gate }
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate /*
20398275SEric Cheng  * Restart a flow and associated Rx SRS that has been quiesced temporarily
20408275SEric Cheng  * Please see block comment above mac_rx_classify_flow_rem
20410Sstevel@tonic-gate  */
20428275SEric Cheng /* ARGSUSED */
20438275SEric Cheng int
mac_rx_classify_flow_restart(flow_entry_t * flent,void * arg)20448275SEric Cheng mac_rx_classify_flow_restart(flow_entry_t *flent, void *arg)
20458275SEric Cheng {
20468275SEric Cheng 	int		i;
20478275SEric Cheng 
20488275SEric Cheng 	for (i = 0; i < flent->fe_rx_srs_cnt; i++)
20498275SEric Cheng 		mac_rx_srs_restart((mac_soft_ring_set_t *)flent->fe_rx_srs[i]);
20508275SEric Cheng 
20518275SEric Cheng 	return (0);
20528275SEric Cheng }
20538275SEric Cheng 
20540Sstevel@tonic-gate void
mac_srs_perm_quiesce(mac_client_handle_t mch,boolean_t on)20558275SEric Cheng mac_srs_perm_quiesce(mac_client_handle_t mch, boolean_t on)
20560Sstevel@tonic-gate {
20578275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
20588275SEric Cheng 	flow_entry_t		*flent = mcip->mci_flent;
20598275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
20608275SEric Cheng 	mac_soft_ring_set_t	*mac_srs;
20618275SEric Cheng 	int			i;
20628275SEric Cheng 
20638275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
20648275SEric Cheng 
20658275SEric Cheng 	if (flent == NULL)
20668275SEric Cheng 		return;
20678275SEric Cheng 
20688275SEric Cheng 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
20698275SEric Cheng 		mac_srs = flent->fe_rx_srs[i];
20708275SEric Cheng 		mutex_enter(&mac_srs->srs_lock);
20718275SEric Cheng 		if (on)
20728275SEric Cheng 			mac_srs->srs_state |= SRS_QUIESCE_PERM;
20738275SEric Cheng 		else
20748275SEric Cheng 			mac_srs->srs_state &= ~SRS_QUIESCE_PERM;
20758275SEric Cheng 		mutex_exit(&mac_srs->srs_lock);
20760Sstevel@tonic-gate 	}
20778275SEric Cheng }
20788275SEric Cheng 
20798275SEric Cheng void
mac_rx_client_quiesce(mac_client_handle_t mch)20808275SEric Cheng mac_rx_client_quiesce(mac_client_handle_t mch)
20818275SEric Cheng {
20828275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
20838275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
20848275SEric Cheng 
20858275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
20868275SEric Cheng 
20878275SEric Cheng 	if (MCIP_DATAPATH_SETUP(mcip)) {
20888275SEric Cheng 		(void) mac_rx_classify_flow_quiesce(mcip->mci_flent,
20898275SEric Cheng 		    NULL);
20908275SEric Cheng 		(void) mac_flow_walk_nolock(mcip->mci_subflow_tab,
20918275SEric Cheng 		    mac_rx_classify_flow_quiesce, NULL);
20928275SEric Cheng 	}
20930Sstevel@tonic-gate }
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate void
mac_rx_client_restart(mac_client_handle_t mch)20968275SEric Cheng mac_rx_client_restart(mac_client_handle_t mch)
20970Sstevel@tonic-gate {
20988275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
20998275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
21008275SEric Cheng 
21018275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
21028275SEric Cheng 
21038275SEric Cheng 	if (MCIP_DATAPATH_SETUP(mcip)) {
21048275SEric Cheng 		(void) mac_rx_classify_flow_restart(mcip->mci_flent, NULL);
21058275SEric Cheng 		(void) mac_flow_walk_nolock(mcip->mci_subflow_tab,
21068275SEric Cheng 		    mac_rx_classify_flow_restart, NULL);
21078275SEric Cheng 	}
21088275SEric Cheng }
21098275SEric Cheng 
21108275SEric Cheng /*
21118275SEric Cheng  * This function only quiesces the Tx SRS and softring worker threads. Callers
21128275SEric Cheng  * need to make sure that there aren't any mac client threads doing current or
21138275SEric Cheng  * future transmits in the mac before calling this function.
21148275SEric Cheng  */
21158275SEric Cheng void
mac_tx_srs_quiesce(mac_soft_ring_set_t * srs,uint_t srs_quiesce_flag)21168275SEric Cheng mac_tx_srs_quiesce(mac_soft_ring_set_t *srs, uint_t srs_quiesce_flag)
21178275SEric Cheng {
21188275SEric Cheng 	mac_client_impl_t	*mcip = srs->srs_mcip;
21198275SEric Cheng 
21208275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
21218275SEric Cheng 
21228275SEric Cheng 	ASSERT(srs->srs_type & SRST_TX);
21238275SEric Cheng 	ASSERT(srs_quiesce_flag == SRS_CONDEMNED ||
21248275SEric Cheng 	    srs_quiesce_flag == SRS_QUIESCE);
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 	/*
21278275SEric Cheng 	 * Signal the SRS to quiesce itself, and then cv_wait for the
21288275SEric Cheng 	 * SRS quiesce to complete. The SRS worker thread will wake us
21298275SEric Cheng 	 * up when the quiesce is complete
21300Sstevel@tonic-gate 	 */
21318275SEric Cheng 	mac_srs_signal(srs, srs_quiesce_flag);
21328275SEric Cheng 	mac_srs_quiesce_wait(srs, srs_quiesce_flag == SRS_QUIESCE ?
21338275SEric Cheng 	    SRS_QUIESCE_DONE : SRS_CONDEMNED_DONE);
21348275SEric Cheng }
21358275SEric Cheng 
21368275SEric Cheng void
mac_tx_srs_restart(mac_soft_ring_set_t * srs)21378275SEric Cheng mac_tx_srs_restart(mac_soft_ring_set_t *srs)
21388275SEric Cheng {
21398275SEric Cheng 	/*
21408275SEric Cheng 	 * Resizing the fanout could result in creation of new SRSs.
21418275SEric Cheng 	 * They may not necessarily be in the quiesced state in which
21428275SEric Cheng 	 * case it need be restarted
21438275SEric Cheng 	 */
21448275SEric Cheng 	if (!SRS_QUIESCED(srs))
21458275SEric Cheng 		return;
21468275SEric Cheng 
21478275SEric Cheng 	mac_srs_signal(srs, SRS_RESTART);
21488275SEric Cheng 	mac_srs_quiesce_wait(srs, SRS_RESTART_DONE);
21498275SEric Cheng 	mac_srs_clear_flag(srs, SRS_RESTART_DONE);
21500Sstevel@tonic-gate }
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate /*
21538275SEric Cheng  * Temporary quiesce of a flow and associated Rx SRS.
21548275SEric Cheng  * Please see block comment above mac_rx_srs_quiesce
21550Sstevel@tonic-gate  */
21568275SEric Cheng /* ARGSUSED */
21578275SEric Cheng int
mac_tx_flow_quiesce(flow_entry_t * flent,void * arg)21588275SEric Cheng mac_tx_flow_quiesce(flow_entry_t *flent, void *arg)
21590Sstevel@tonic-gate {
21602311Sseb 	/*
21618275SEric Cheng 	 * The fe_tx_srs is null for a subflow on an interface that is
21628275SEric Cheng 	 * not plumbed
21632311Sseb 	 */
21648275SEric Cheng 	if (flent->fe_tx_srs != NULL)
21658275SEric Cheng 		mac_tx_srs_quiesce(flent->fe_tx_srs, SRS_QUIESCE);
21668275SEric Cheng 	return (0);
21678275SEric Cheng }
21688275SEric Cheng 
21698275SEric Cheng /* ARGSUSED */
21708275SEric Cheng int
mac_tx_flow_restart(flow_entry_t * flent,void * arg)21718275SEric Cheng mac_tx_flow_restart(flow_entry_t *flent, void *arg)
21728275SEric Cheng {
21738275SEric Cheng 	/*
21748275SEric Cheng 	 * The fe_tx_srs is null for a subflow on an interface that is
21758275SEric Cheng 	 * not plumbed
21768275SEric Cheng 	 */
21778275SEric Cheng 	if (flent->fe_tx_srs != NULL)
21788275SEric Cheng 		mac_tx_srs_restart(flent->fe_tx_srs);
21798275SEric Cheng 	return (0);
21802311Sseb }
21812311Sseb 
218211878SVenu.Iyer@Sun.COM static void
i_mac_tx_client_quiesce(mac_client_handle_t mch,uint_t srs_quiesce_flag)218311878SVenu.Iyer@Sun.COM i_mac_tx_client_quiesce(mac_client_handle_t mch, uint_t srs_quiesce_flag)
218411878SVenu.Iyer@Sun.COM {
218511878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
218611878SVenu.Iyer@Sun.COM 
21878275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
21888275SEric Cheng 
21898275SEric Cheng 	mac_tx_client_block(mcip);
21908275SEric Cheng 	if (MCIP_TX_SRS(mcip) != NULL) {
21918275SEric Cheng 		mac_tx_srs_quiesce(MCIP_TX_SRS(mcip), srs_quiesce_flag);
21928275SEric Cheng 		(void) mac_flow_walk_nolock(mcip->mci_subflow_tab,
21938275SEric Cheng 		    mac_tx_flow_quiesce, NULL);
21948275SEric Cheng 	}
21958275SEric Cheng }
21968275SEric Cheng 
21978275SEric Cheng void
mac_tx_client_quiesce(mac_client_handle_t mch)219811878SVenu.Iyer@Sun.COM mac_tx_client_quiesce(mac_client_handle_t mch)
219911878SVenu.Iyer@Sun.COM {
220011878SVenu.Iyer@Sun.COM 	i_mac_tx_client_quiesce(mch, SRS_QUIESCE);
220111878SVenu.Iyer@Sun.COM }
220211878SVenu.Iyer@Sun.COM 
220311878SVenu.Iyer@Sun.COM void
mac_tx_client_condemn(mac_client_handle_t mch)220411878SVenu.Iyer@Sun.COM mac_tx_client_condemn(mac_client_handle_t mch)
220511878SVenu.Iyer@Sun.COM {
220611878SVenu.Iyer@Sun.COM 	i_mac_tx_client_quiesce(mch, SRS_CONDEMNED);
220711878SVenu.Iyer@Sun.COM }
220811878SVenu.Iyer@Sun.COM 
220911878SVenu.Iyer@Sun.COM void
mac_tx_client_restart(mac_client_handle_t mch)221011878SVenu.Iyer@Sun.COM mac_tx_client_restart(mac_client_handle_t mch)
221111878SVenu.Iyer@Sun.COM {
221211878SVenu.Iyer@Sun.COM 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
221311878SVenu.Iyer@Sun.COM 
22148275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
22158275SEric Cheng 
22168275SEric Cheng 	mac_tx_client_unblock(mcip);
22178275SEric Cheng 	if (MCIP_TX_SRS(mcip) != NULL) {
22188275SEric Cheng 		mac_tx_srs_restart(MCIP_TX_SRS(mcip));
22198275SEric Cheng 		(void) mac_flow_walk_nolock(mcip->mci_subflow_tab,
22208275SEric Cheng 		    mac_tx_flow_restart, NULL);
22218275SEric Cheng 	}
22228275SEric Cheng }
22238275SEric Cheng 
22248275SEric Cheng void
mac_tx_client_flush(mac_client_impl_t * mcip)22258275SEric Cheng mac_tx_client_flush(mac_client_impl_t *mcip)
22268275SEric Cheng {
22278275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
22288275SEric Cheng 
222911878SVenu.Iyer@Sun.COM 	mac_tx_client_quiesce((mac_client_handle_t)mcip);
223011878SVenu.Iyer@Sun.COM 	mac_tx_client_restart((mac_client_handle_t)mcip);
22318275SEric Cheng }
22328275SEric Cheng 
22338275SEric Cheng void
mac_client_quiesce(mac_client_impl_t * mcip)22348275SEric Cheng mac_client_quiesce(mac_client_impl_t *mcip)
22358275SEric Cheng {
22368275SEric Cheng 	mac_rx_client_quiesce((mac_client_handle_t)mcip);
223711878SVenu.Iyer@Sun.COM 	mac_tx_client_quiesce((mac_client_handle_t)mcip);
22388275SEric Cheng }
22398275SEric Cheng 
22408275SEric Cheng void
mac_client_restart(mac_client_impl_t * mcip)22418275SEric Cheng mac_client_restart(mac_client_impl_t *mcip)
22428275SEric Cheng {
22438275SEric Cheng 	mac_rx_client_restart((mac_client_handle_t)mcip);
224411878SVenu.Iyer@Sun.COM 	mac_tx_client_restart((mac_client_handle_t)mcip);
22452311Sseb }
22462311Sseb 
22472311Sseb /*
22485895Syz147064  * Allocate a minor number.
22495895Syz147064  */
22505895Syz147064 minor_t
mac_minor_hold(boolean_t sleep)22515895Syz147064 mac_minor_hold(boolean_t sleep)
22525895Syz147064 {
22535895Syz147064 	minor_t	minor;
22545895Syz147064 
22555895Syz147064 	/*
22565895Syz147064 	 * Grab a value from the arena.
22575895Syz147064 	 */
22585895Syz147064 	atomic_add_32(&minor_count, 1);
22595895Syz147064 
22605895Syz147064 	if (sleep)
22615895Syz147064 		minor = (uint_t)id_alloc(minor_ids);
22625895Syz147064 	else
22635895Syz147064 		minor = (uint_t)id_alloc_nosleep(minor_ids);
22645895Syz147064 
22655895Syz147064 	if (minor == 0) {
22665895Syz147064 		atomic_add_32(&minor_count, -1);
22675895Syz147064 		return (0);
22685895Syz147064 	}
22695895Syz147064 
22705895Syz147064 	return (minor);
22715895Syz147064 }
22725895Syz147064 
22735895Syz147064 /*
22745895Syz147064  * Release a previously allocated minor number.
22755895Syz147064  */
22765895Syz147064 void
mac_minor_rele(minor_t minor)22775895Syz147064 mac_minor_rele(minor_t minor)
22785895Syz147064 {
22795895Syz147064 	/*
22805895Syz147064 	 * Return the value to the arena.
22815895Syz147064 	 */
22825895Syz147064 	id_free(minor_ids, minor);
22835895Syz147064 	atomic_add_32(&minor_count, -1);
22845895Syz147064 }
22855895Syz147064 
22865895Syz147064 uint32_t
mac_no_notification(mac_handle_t mh)22875895Syz147064 mac_no_notification(mac_handle_t mh)
22885895Syz147064 {
22895895Syz147064 	mac_impl_t *mip = (mac_impl_t *)mh;
22909073SCathy.Zhou@Sun.COM 
22919073SCathy.Zhou@Sun.COM 	return (((mip->mi_state_flags & MIS_LEGACY) != 0) ?
22929073SCathy.Zhou@Sun.COM 	    mip->mi_capab_legacy.ml_unsup_note : 0);
22935895Syz147064 }
22945895Syz147064 
22955895Syz147064 /*
22968275SEric Cheng  * Prevent any new opens of this mac in preparation for unregister
22972311Sseb  */
22982311Sseb int
i_mac_disable(mac_impl_t * mip)22998275SEric Cheng i_mac_disable(mac_impl_t *mip)
23002311Sseb {
23018275SEric Cheng 	mac_client_impl_t	*mcip;
23028275SEric Cheng 
23038275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
23048275SEric Cheng 	if (mip->mi_state_flags & MIS_DISABLED) {
23058275SEric Cheng 		/* Already disabled, return success */
23068275SEric Cheng 		rw_exit(&i_mac_impl_lock);
23078275SEric Cheng 		return (0);
23085895Syz147064 	}
23092311Sseb 	/*
23108275SEric Cheng 	 * See if there are any other references to this mac_t (e.g., VLAN's).
23118275SEric Cheng 	 * If so return failure. If all the other checks below pass, then
23128275SEric Cheng 	 * set mi_disabled atomically under the i_mac_impl_lock to prevent
23138275SEric Cheng 	 * any new VLAN's from being created or new mac client opens of this
23148275SEric Cheng 	 * mac end point.
23152311Sseb 	 */
23168275SEric Cheng 	if (mip->mi_ref > 0) {
23178275SEric Cheng 		rw_exit(&i_mac_impl_lock);
23188275SEric Cheng 		return (EBUSY);
23192311Sseb 	}
23202311Sseb 
23212311Sseb 	/*
23228275SEric Cheng 	 * mac clients must delete all multicast groups they join before
23238275SEric Cheng 	 * closing. bcast groups are reference counted, the last client
23248275SEric Cheng 	 * to delete the group will wait till the group is physically
23258275SEric Cheng 	 * deleted. Since all clients have closed this mac end point
23268275SEric Cheng 	 * mi_bcast_ngrps must be zero at this point
23272311Sseb 	 */
23288275SEric Cheng 	ASSERT(mip->mi_bcast_ngrps == 0);
23295009Sgd78059 
23305009Sgd78059 	/*
23318275SEric Cheng 	 * Don't let go of this if it has some flows.
23328275SEric Cheng 	 * All other code guarantees no flows are added to a disabled
23338275SEric Cheng 	 * mac, therefore it is sufficient to check for the flow table
23348275SEric Cheng 	 * only here.
23352311Sseb 	 */
23368275SEric Cheng 	mcip = mac_primary_client_handle(mip);
23378275SEric Cheng 	if ((mcip != NULL) && mac_link_has_flows((mac_client_handle_t)mcip)) {
23388275SEric Cheng 		rw_exit(&i_mac_impl_lock);
23398275SEric Cheng 		return (ENOTEMPTY);
23405895Syz147064 	}
23415895Syz147064 
23428275SEric Cheng 	mip->mi_state_flags |= MIS_DISABLED;
23431852Syz147064 	rw_exit(&i_mac_impl_lock);
2344269Sericheng 	return (0);
23458275SEric Cheng }
23468275SEric Cheng 
23478275SEric Cheng int
mac_disable_nowait(mac_handle_t mh)23488275SEric Cheng mac_disable_nowait(mac_handle_t mh)
23498275SEric Cheng {
23508275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
23518275SEric Cheng 	int err;
23528275SEric Cheng 
23538275SEric Cheng 	if ((err = i_mac_perim_enter_nowait(mip)) != 0)
23548275SEric Cheng 		return (err);
23558275SEric Cheng 	err = i_mac_disable(mip);
23568275SEric Cheng 	i_mac_perim_exit(mip);
2357269Sericheng 	return (err);
23580Sstevel@tonic-gate }
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate int
mac_disable(mac_handle_t mh)23615084Sjohnlev mac_disable(mac_handle_t mh)
23620Sstevel@tonic-gate {
23638275SEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
23648275SEric Cheng 	int err;
23658275SEric Cheng 
23668275SEric Cheng 	i_mac_perim_enter(mip);
23678275SEric Cheng 	err = i_mac_disable(mip);
23688275SEric Cheng 	i_mac_perim_exit(mip);
23695084Sjohnlev 
23700Sstevel@tonic-gate 	/*
23718275SEric Cheng 	 * Clean up notification thread and wait for it to exit.
23725009Sgd78059 	 */
23738275SEric Cheng 	if (err == 0)
23748275SEric Cheng 		i_mac_notify_exit(mip);
23758275SEric Cheng 
23768275SEric Cheng 	return (err);
23770Sstevel@tonic-gate }
23780Sstevel@tonic-gate 
23794913Sethindra /*
23808275SEric Cheng  * Called when the MAC instance has a non empty flow table, to de-multiplex
23818275SEric Cheng  * incoming packets to the right flow.
23828275SEric Cheng  * The MAC's rw lock is assumed held as a READER.
23834913Sethindra  */
23848275SEric Cheng /* ARGSUSED */
23858275SEric Cheng static mblk_t *
mac_rx_classify(mac_impl_t * mip,mac_resource_handle_t mrh,mblk_t * mp)23868275SEric Cheng mac_rx_classify(mac_impl_t *mip, mac_resource_handle_t mrh, mblk_t *mp)
23870Sstevel@tonic-gate {
23888275SEric Cheng 	flow_entry_t	*flent = NULL;
23898275SEric Cheng 	uint_t		flags = FLOW_INBOUND;
23908275SEric Cheng 	int		err;
23914913Sethindra 
23924913Sethindra 	/*
23938275SEric Cheng 	 * If the mac is a port of an aggregation, pass FLOW_IGNORE_VLAN
23948275SEric Cheng 	 * to mac_flow_lookup() so that the VLAN packets can be successfully
23958275SEric Cheng 	 * passed to the non-VLAN aggregation flows.
23968275SEric Cheng 	 *
23978275SEric Cheng 	 * Note that there is possibly a race between this and
23988275SEric Cheng 	 * mac_unicast_remove/add() and VLAN packets could be incorrectly
23998275SEric Cheng 	 * classified to non-VLAN flows of non-aggregation mac clients. These
24008275SEric Cheng 	 * VLAN packets will be then filtered out by the mac module.
24014913Sethindra 	 */
24028275SEric Cheng 	if ((mip->mi_state_flags & MIS_EXCLUSIVE) != 0)
24038275SEric Cheng 		flags |= FLOW_IGNORE_VLAN;
24048275SEric Cheng 
24058275SEric Cheng 	err = mac_flow_lookup(mip->mi_flow_tab, mp, flags, &flent);
24068275SEric Cheng 	if (err != 0) {
24078275SEric Cheng 		/* no registered receive function */
24088275SEric Cheng 		return (mp);
24098275SEric Cheng 	} else {
24108275SEric Cheng 		mac_client_impl_t	*mcip;
24114913Sethindra 
24124913Sethindra 		/*
24138275SEric Cheng 		 * This flent might just be an additional one on the MAC client,
24148275SEric Cheng 		 * i.e. for classification purposes (different fdesc), however
24158275SEric Cheng 		 * the resources, SRS et. al., are in the mci_flent, so if
24168275SEric Cheng 		 * this isn't the mci_flent, we need to get it.
24174913Sethindra 		 */
24188275SEric Cheng 		if ((mcip = flent->fe_mcip) != NULL &&
24198275SEric Cheng 		    mcip->mci_flent != flent) {
24208275SEric Cheng 			FLOW_REFRELE(flent);
24218275SEric Cheng 			flent = mcip->mci_flent;
24228275SEric Cheng 			FLOW_TRY_REFHOLD(flent, err);
24238275SEric Cheng 			if (err != 0)
24248275SEric Cheng 				return (mp);
24258275SEric Cheng 		}
24268275SEric Cheng 		(flent->fe_cb_fn)(flent->fe_cb_arg1, flent->fe_cb_arg2, mp,
24278275SEric Cheng 		    B_FALSE);
24288275SEric Cheng 		FLOW_REFRELE(flent);
24295084Sjohnlev 	}
24305084Sjohnlev 	return (NULL);
24315084Sjohnlev }
24325084Sjohnlev 
24335084Sjohnlev mblk_t *
mac_rx_flow(mac_handle_t mh,mac_resource_handle_t mrh,mblk_t * mp_chain)24348275SEric Cheng mac_rx_flow(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain)
24350Sstevel@tonic-gate {
24362311Sseb 	mac_impl_t	*mip = (mac_impl_t *)mh;
24378275SEric Cheng 	mblk_t		*bp, *bp1, **bpp, *list = NULL;
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate 	/*
24408275SEric Cheng 	 * We walk the chain and attempt to classify each packet.
24418275SEric Cheng 	 * The packets that couldn't be classified will be returned
24428275SEric Cheng 	 * back to the caller.
24430Sstevel@tonic-gate 	 */
24448275SEric Cheng 	bp = mp_chain;
24458275SEric Cheng 	bpp = &list;
24468275SEric Cheng 	while (bp != NULL) {
24478275SEric Cheng 		bp1 = bp;
24488275SEric Cheng 		bp = bp->b_next;
24498275SEric Cheng 		bp1->b_next = NULL;
24508275SEric Cheng 
24518275SEric Cheng 		if (mac_rx_classify(mip, mrh, bp1) != NULL) {
24528275SEric Cheng 			*bpp = bp1;
24538275SEric Cheng 			bpp = &bp1->b_next;
24548275SEric Cheng 		}
24558275SEric Cheng 	}
24568275SEric Cheng 	return (list);
24570Sstevel@tonic-gate }
24580Sstevel@tonic-gate 
24598275SEric Cheng static int
mac_tx_flow_srs_wakeup(flow_entry_t * flent,void * arg)24608275SEric Cheng mac_tx_flow_srs_wakeup(flow_entry_t *flent, void *arg)
24610Sstevel@tonic-gate {
24628275SEric Cheng 	mac_ring_handle_t ring = arg;
24638275SEric Cheng 
24648275SEric Cheng 	if (flent->fe_tx_srs)
24658275SEric Cheng 		mac_tx_srs_wakeup(flent->fe_tx_srs, ring);
24662311Sseb 	return (0);
24672311Sseb }
24682311Sseb 
24690Sstevel@tonic-gate void
i_mac_tx_srs_notify(mac_impl_t * mip,mac_ring_handle_t ring)24708275SEric Cheng i_mac_tx_srs_notify(mac_impl_t *mip, mac_ring_handle_t ring)
24718275SEric Cheng {
24728275SEric Cheng 	mac_client_impl_t	*cclient;
24738275SEric Cheng 	mac_soft_ring_set_t	*mac_srs;
24748275SEric Cheng 
24758275SEric Cheng 	/*
24768275SEric Cheng 	 * After grabbing the mi_rw_lock, the list of clients can't change.
24778275SEric Cheng 	 * If there are any clients mi_disabled must be B_FALSE and can't
24788275SEric Cheng 	 * get set since there are clients. If there aren't any clients we
24798275SEric Cheng 	 * don't do anything. In any case the mip has to be valid. The driver
24808275SEric Cheng 	 * must make sure that it goes single threaded (with respect to mac
24818275SEric Cheng 	 * calls) and wait for all pending mac calls to finish before calling
24828275SEric Cheng 	 * mac_unregister.
24838275SEric Cheng 	 */
24848275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_READER);
24858275SEric Cheng 	if (mip->mi_state_flags & MIS_DISABLED) {
24868275SEric Cheng 		rw_exit(&i_mac_impl_lock);
24878275SEric Cheng 		return;
24888275SEric Cheng 	}
24898275SEric Cheng 
24908275SEric Cheng 	/*
24918275SEric Cheng 	 * Get MAC tx srs from walking mac_client_handle list.
24928275SEric Cheng 	 */
24938275SEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
24948275SEric Cheng 	for (cclient = mip->mi_clients_list; cclient != NULL;
24958275SEric Cheng 	    cclient = cclient->mci_client_next) {
249611878SVenu.Iyer@Sun.COM 		if ((mac_srs = MCIP_TX_SRS(cclient)) != NULL) {
24978275SEric Cheng 			mac_tx_srs_wakeup(mac_srs, ring);
249811878SVenu.Iyer@Sun.COM 		} else {
249911878SVenu.Iyer@Sun.COM 			/*
250011878SVenu.Iyer@Sun.COM 			 * Aggr opens underlying ports in exclusive mode
250111878SVenu.Iyer@Sun.COM 			 * and registers flow control callbacks using
250211878SVenu.Iyer@Sun.COM 			 * mac_tx_client_notify(). When opened in
250311878SVenu.Iyer@Sun.COM 			 * exclusive mode, Tx SRS won't be created
250411878SVenu.Iyer@Sun.COM 			 * during mac_unicast_add().
250511878SVenu.Iyer@Sun.COM 			 */
250611878SVenu.Iyer@Sun.COM 			if (cclient->mci_state_flags & MCIS_EXCLUSIVE) {
250711878SVenu.Iyer@Sun.COM 				mac_tx_invoke_callbacks(cclient,
250811878SVenu.Iyer@Sun.COM 				    (mac_tx_cookie_t)ring);
250911878SVenu.Iyer@Sun.COM 			}
251011878SVenu.Iyer@Sun.COM 		}
25118833SVenu.Iyer@Sun.COM 		(void) mac_flow_walk(cclient->mci_subflow_tab,
25128833SVenu.Iyer@Sun.COM 		    mac_tx_flow_srs_wakeup, ring);
25138275SEric Cheng 	}
25148275SEric Cheng 	rw_exit(&mip->mi_rw_lock);
25158275SEric Cheng 	rw_exit(&i_mac_impl_lock);
25168275SEric Cheng }
25178275SEric Cheng 
25188275SEric Cheng /* ARGSUSED */
25198275SEric Cheng void
mac_multicast_refresh(mac_handle_t mh,mac_multicst_t refresh,void * arg,boolean_t add)25208275SEric Cheng mac_multicast_refresh(mac_handle_t mh, mac_multicst_t refresh, void *arg,
25210Sstevel@tonic-gate     boolean_t add)
25220Sstevel@tonic-gate {
25238275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
25248275SEric Cheng 
25258275SEric Cheng 	i_mac_perim_enter((mac_impl_t *)mh);
25260Sstevel@tonic-gate 	/*
25270Sstevel@tonic-gate 	 * If no specific refresh function was given then default to the
25280Sstevel@tonic-gate 	 * driver's m_multicst entry point.
25290Sstevel@tonic-gate 	 */
25300Sstevel@tonic-gate 	if (refresh == NULL) {
25312311Sseb 		refresh = mip->mi_multicst;
25322311Sseb 		arg = mip->mi_driver;
25330Sstevel@tonic-gate 	}
25348275SEric Cheng 
25358275SEric Cheng 	mac_bcast_refresh(mip, refresh, arg, add);
25368275SEric Cheng 	i_mac_perim_exit((mac_impl_t *)mh);
25370Sstevel@tonic-gate }
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate void
mac_promisc_refresh(mac_handle_t mh,mac_setpromisc_t refresh,void * arg)25402311Sseb mac_promisc_refresh(mac_handle_t mh, mac_setpromisc_t refresh, void *arg)
25410Sstevel@tonic-gate {
25422311Sseb 	mac_impl_t	*mip = (mac_impl_t *)mh;
25430Sstevel@tonic-gate 
25440Sstevel@tonic-gate 	/*
25450Sstevel@tonic-gate 	 * If no specific refresh function was given then default to the
25460Sstevel@tonic-gate 	 * driver's m_promisc entry point.
25470Sstevel@tonic-gate 	 */
25480Sstevel@tonic-gate 	if (refresh == NULL) {
25492311Sseb 		refresh = mip->mi_setpromisc;
25502311Sseb 		arg = mip->mi_driver;
25510Sstevel@tonic-gate 	}
25520Sstevel@tonic-gate 	ASSERT(refresh != NULL);
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate 	/*
25550Sstevel@tonic-gate 	 * Call the refresh function with the current promiscuity.
25560Sstevel@tonic-gate 	 */
25570Sstevel@tonic-gate 	refresh(arg, (mip->mi_devpromisc != 0));
25580Sstevel@tonic-gate }
25590Sstevel@tonic-gate 
25605895Syz147064 /*
25615895Syz147064  * The mac client requests that the mac not to change its margin size to
25625895Syz147064  * be less than the specified value.  If "current" is B_TRUE, then the client
25635895Syz147064  * requests the mac not to change its margin size to be smaller than the
25645895Syz147064  * current size. Further, return the current margin size value in this case.
25655895Syz147064  *
25665895Syz147064  * We keep every requested size in an ordered list from largest to smallest.
25675895Syz147064  */
25685895Syz147064 int
mac_margin_add(mac_handle_t mh,uint32_t * marginp,boolean_t current)25695895Syz147064 mac_margin_add(mac_handle_t mh, uint32_t *marginp, boolean_t current)
25705895Syz147064 {
25715895Syz147064 	mac_impl_t		*mip = (mac_impl_t *)mh;
25725895Syz147064 	mac_margin_req_t	**pp, *p;
25735895Syz147064 	int			err = 0;
25745895Syz147064 
25758275SEric Cheng 	rw_enter(&(mip->mi_rw_lock), RW_WRITER);
25765895Syz147064 	if (current)
25775895Syz147064 		*marginp = mip->mi_margin;
25785895Syz147064 
25795895Syz147064 	/*
25805895Syz147064 	 * If the current margin value cannot satisfy the margin requested,
25815895Syz147064 	 * return ENOTSUP directly.
25825895Syz147064 	 */
25835895Syz147064 	if (*marginp > mip->mi_margin) {
25845895Syz147064 		err = ENOTSUP;
25855895Syz147064 		goto done;
25865895Syz147064 	}
25875895Syz147064 
25885895Syz147064 	/*
25895895Syz147064 	 * Check whether the given margin is already in the list. If so,
25905895Syz147064 	 * bump the reference count.
25915895Syz147064 	 */
25928275SEric Cheng 	for (pp = &mip->mi_mmrp; (p = *pp) != NULL; pp = &p->mmr_nextp) {
25935895Syz147064 		if (p->mmr_margin == *marginp) {
25945895Syz147064 			/*
25955895Syz147064 			 * The margin requested is already in the list,
25965895Syz147064 			 * so just bump the reference count.
25975895Syz147064 			 */
25985895Syz147064 			p->mmr_ref++;
25995895Syz147064 			goto done;
26005895Syz147064 		}
26015895Syz147064 		if (p->mmr_margin < *marginp)
26025895Syz147064 			break;
26035895Syz147064 	}
26045895Syz147064 
26055895Syz147064 
26068275SEric Cheng 	p = kmem_zalloc(sizeof (mac_margin_req_t), KM_SLEEP);
26075895Syz147064 	p->mmr_margin = *marginp;
26085895Syz147064 	p->mmr_ref++;
26095895Syz147064 	p->mmr_nextp = *pp;
26105895Syz147064 	*pp = p;
26115895Syz147064 
26125895Syz147064 done:
26138275SEric Cheng 	rw_exit(&(mip->mi_rw_lock));
26145895Syz147064 	return (err);
26155895Syz147064 }
26165895Syz147064 
26175895Syz147064 /*
26185895Syz147064  * The mac client requests to cancel its previous mac_margin_add() request.
26195895Syz147064  * We remove the requested margin size from the list.
26205895Syz147064  */
26215895Syz147064 int
mac_margin_remove(mac_handle_t mh,uint32_t margin)26225895Syz147064 mac_margin_remove(mac_handle_t mh, uint32_t margin)
26235895Syz147064 {
26245895Syz147064 	mac_impl_t		*mip = (mac_impl_t *)mh;
26255895Syz147064 	mac_margin_req_t	**pp, *p;
26265895Syz147064 	int			err = 0;
26275895Syz147064 
26288275SEric Cheng 	rw_enter(&(mip->mi_rw_lock), RW_WRITER);
26295895Syz147064 	/*
26305895Syz147064 	 * Find the entry in the list for the given margin.
26315895Syz147064 	 */
26325895Syz147064 	for (pp = &(mip->mi_mmrp); (p = *pp) != NULL; pp = &(p->mmr_nextp)) {
26335895Syz147064 		if (p->mmr_margin == margin) {
26345895Syz147064 			if (--p->mmr_ref == 0)
26355895Syz147064 				break;
26365895Syz147064 
26375895Syz147064 			/*
26385895Syz147064 			 * There is still a reference to this address so
26395895Syz147064 			 * there's nothing more to do.
26405895Syz147064 			 */
26415895Syz147064 			goto done;
26425895Syz147064 		}
26435895Syz147064 	}
26445895Syz147064 
26455895Syz147064 	/*
26465895Syz147064 	 * We did not find an entry for the given margin.
26475895Syz147064 	 */
26485895Syz147064 	if (p == NULL) {
26495895Syz147064 		err = ENOENT;
26505895Syz147064 		goto done;
26515895Syz147064 	}
26525895Syz147064 
26535895Syz147064 	ASSERT(p->mmr_ref == 0);
26545895Syz147064 
26555895Syz147064 	/*
26565895Syz147064 	 * Remove it from the list.
26575895Syz147064 	 */
26585895Syz147064 	*pp = p->mmr_nextp;
26595895Syz147064 	kmem_free(p, sizeof (mac_margin_req_t));
26605895Syz147064 done:
26618275SEric Cheng 	rw_exit(&(mip->mi_rw_lock));
26625895Syz147064 	return (err);
26635895Syz147064 }
26645895Syz147064 
26655895Syz147064 boolean_t
mac_margin_update(mac_handle_t mh,uint32_t margin)26665895Syz147064 mac_margin_update(mac_handle_t mh, uint32_t margin)
26675895Syz147064 {
26685895Syz147064 	mac_impl_t	*mip = (mac_impl_t *)mh;
26695895Syz147064 	uint32_t	margin_needed = 0;
26705895Syz147064 
26718275SEric Cheng 	rw_enter(&(mip->mi_rw_lock), RW_WRITER);
26725895Syz147064 
26735895Syz147064 	if (mip->mi_mmrp != NULL)
26745895Syz147064 		margin_needed = mip->mi_mmrp->mmr_margin;
26755895Syz147064 
26765895Syz147064 	if (margin_needed <= margin)
26775895Syz147064 		mip->mi_margin = margin;
26785895Syz147064 
26798275SEric Cheng 	rw_exit(&(mip->mi_rw_lock));
26805895Syz147064 
26815895Syz147064 	if (margin_needed <= margin)
26825895Syz147064 		i_mac_notify(mip, MAC_NOTE_MARGIN);
26835895Syz147064 
26845895Syz147064 	return (margin_needed <= margin);
26855895Syz147064 }
26865895Syz147064 
26872311Sseb /*
26882311Sseb  * MAC Type Plugin functions.
26892311Sseb  */
26902311Sseb 
26918275SEric Cheng mactype_t *
mactype_getplugin(const char * pname)26928275SEric Cheng mactype_getplugin(const char *pname)
26938275SEric Cheng {
26948275SEric Cheng 	mactype_t	*mtype = NULL;
26958275SEric Cheng 	boolean_t	tried_modload = B_FALSE;
26968275SEric Cheng 
26978275SEric Cheng 	mutex_enter(&i_mactype_lock);
26988275SEric Cheng 
26998275SEric Cheng find_registered_mactype:
27008275SEric Cheng 	if (mod_hash_find(i_mactype_hash, (mod_hash_key_t)pname,
27018275SEric Cheng 	    (mod_hash_val_t *)&mtype) != 0) {
27028275SEric Cheng 		if (!tried_modload) {
27038275SEric Cheng 			/*
27048275SEric Cheng 			 * If the plugin has not yet been loaded, then
27058275SEric Cheng 			 * attempt to load it now.  If modload() succeeds,
27068275SEric Cheng 			 * the plugin should have registered using
27078275SEric Cheng 			 * mactype_register(), in which case we can go back
27088275SEric Cheng 			 * and attempt to find it again.
27098275SEric Cheng 			 */
27108275SEric Cheng 			if (modload(MACTYPE_KMODDIR, (char *)pname) != -1) {
27118275SEric Cheng 				tried_modload = B_TRUE;
27128275SEric Cheng 				goto find_registered_mactype;
27138275SEric Cheng 			}
27148275SEric Cheng 		}
27158275SEric Cheng 	} else {
27168275SEric Cheng 		/*
27178275SEric Cheng 		 * Note that there's no danger that the plugin we've loaded
27188275SEric Cheng 		 * could be unloaded between the modload() step and the
27198275SEric Cheng 		 * reference count bump here, as we're holding
27208275SEric Cheng 		 * i_mactype_lock, which mactype_unregister() also holds.
27218275SEric Cheng 		 */
27228275SEric Cheng 		atomic_inc_32(&mtype->mt_ref);
27238275SEric Cheng 	}
27248275SEric Cheng 
27258275SEric Cheng 	mutex_exit(&i_mactype_lock);
27268275SEric Cheng 	return (mtype);
27278275SEric Cheng }
27288275SEric Cheng 
27292311Sseb mactype_register_t *
mactype_alloc(uint_t mactype_version)27302311Sseb mactype_alloc(uint_t mactype_version)
27312311Sseb {
27322311Sseb 	mactype_register_t *mtrp;
27332311Sseb 
27342311Sseb 	/*
27352311Sseb 	 * Make sure there isn't a version mismatch between the plugin and
27362311Sseb 	 * the framework.  In the future, if multiple versions are
27372311Sseb 	 * supported, this check could become more sophisticated.
27382311Sseb 	 */
27392311Sseb 	if (mactype_version != MACTYPE_VERSION)
27402311Sseb 		return (NULL);
27412311Sseb 
27422311Sseb 	mtrp = kmem_zalloc(sizeof (mactype_register_t), KM_SLEEP);
27432311Sseb 	mtrp->mtr_version = mactype_version;
27442311Sseb 	return (mtrp);
27452311Sseb }
27462311Sseb 
27472311Sseb void
mactype_free(mactype_register_t * mtrp)27482311Sseb mactype_free(mactype_register_t *mtrp)
27492311Sseb {
27502311Sseb 	kmem_free(mtrp, sizeof (mactype_register_t));
27512311Sseb }
27522311Sseb 
27532311Sseb int
mactype_register(mactype_register_t * mtrp)27542311Sseb mactype_register(mactype_register_t *mtrp)
27552311Sseb {
27562311Sseb 	mactype_t	*mtp;
27572311Sseb 	mactype_ops_t	*ops = mtrp->mtr_ops;
27582311Sseb 
27592311Sseb 	/* Do some sanity checking before we register this MAC type. */
27606353Sdr146992 	if (mtrp->mtr_ident == NULL || ops == NULL)
27612311Sseb 		return (EINVAL);
27622311Sseb 
27632311Sseb 	/*
27642311Sseb 	 * Verify that all mandatory callbacks are set in the ops
27652311Sseb 	 * vector.
27662311Sseb 	 */
27672311Sseb 	if (ops->mtops_unicst_verify == NULL ||
27682311Sseb 	    ops->mtops_multicst_verify == NULL ||
27692311Sseb 	    ops->mtops_sap_verify == NULL ||
27702311Sseb 	    ops->mtops_header == NULL ||
27712311Sseb 	    ops->mtops_header_info == NULL) {
27722311Sseb 		return (EINVAL);
27732311Sseb 	}
27742311Sseb 
27752311Sseb 	mtp = kmem_zalloc(sizeof (*mtp), KM_SLEEP);
27762311Sseb 	mtp->mt_ident = mtrp->mtr_ident;
27772311Sseb 	mtp->mt_ops = *ops;
27782311Sseb 	mtp->mt_type = mtrp->mtr_mactype;
27793147Sxc151355 	mtp->mt_nativetype = mtrp->mtr_nativetype;
27802311Sseb 	mtp->mt_addr_length = mtrp->mtr_addrlen;
27812311Sseb 	if (mtrp->mtr_brdcst_addr != NULL) {
27822311Sseb 		mtp->mt_brdcst_addr = kmem_alloc(mtrp->mtr_addrlen, KM_SLEEP);
27832311Sseb 		bcopy(mtrp->mtr_brdcst_addr, mtp->mt_brdcst_addr,
27842311Sseb 		    mtrp->mtr_addrlen);
27852311Sseb 	}
27862311Sseb 
27872311Sseb 	mtp->mt_stats = mtrp->mtr_stats;
27882311Sseb 	mtp->mt_statcount = mtrp->mtr_statcount;
27892311Sseb 
27906512Ssowmini 	mtp->mt_mapping = mtrp->mtr_mapping;
27916512Ssowmini 	mtp->mt_mappingcount = mtrp->mtr_mappingcount;
27926512Ssowmini 
27932311Sseb 	if (mod_hash_insert(i_mactype_hash,
27942311Sseb 	    (mod_hash_key_t)mtp->mt_ident, (mod_hash_val_t)mtp) != 0) {
27952311Sseb 		kmem_free(mtp->mt_brdcst_addr, mtp->mt_addr_length);
27962311Sseb 		kmem_free(mtp, sizeof (*mtp));
27972311Sseb 		return (EEXIST);
27982311Sseb 	}
27992311Sseb 	return (0);
28002311Sseb }
28012311Sseb 
28022311Sseb int
mactype_unregister(const char * ident)28032311Sseb mactype_unregister(const char *ident)
28042311Sseb {
28052311Sseb 	mactype_t	*mtp;
28062311Sseb 	mod_hash_val_t	val;
28072311Sseb 	int 		err;
28082311Sseb 
28092311Sseb 	/*
28102311Sseb 	 * Let's not allow MAC drivers to use this plugin while we're
28113288Sseb 	 * trying to unregister it.  Holding i_mactype_lock also prevents a
28123288Sseb 	 * plugin from unregistering while a MAC driver is attempting to
28133288Sseb 	 * hold a reference to it in i_mactype_getplugin().
28142311Sseb 	 */
28153288Sseb 	mutex_enter(&i_mactype_lock);
28162311Sseb 
28172311Sseb 	if ((err = mod_hash_find(i_mactype_hash, (mod_hash_key_t)ident,
28182311Sseb 	    (mod_hash_val_t *)&mtp)) != 0) {
28192311Sseb 		/* A plugin is trying to unregister, but it never registered. */
28203288Sseb 		err = ENXIO;
28213288Sseb 		goto done;
28222311Sseb 	}
28232311Sseb 
28243288Sseb 	if (mtp->mt_ref != 0) {
28253288Sseb 		err = EBUSY;
28263288Sseb 		goto done;
28272311Sseb 	}
28282311Sseb 
28292311Sseb 	err = mod_hash_remove(i_mactype_hash, (mod_hash_key_t)ident, &val);
28302311Sseb 	ASSERT(err == 0);
28312311Sseb 	if (err != 0) {
28322311Sseb 		/* This should never happen, thus the ASSERT() above. */
28333288Sseb 		err = EINVAL;
28343288Sseb 		goto done;
28352311Sseb 	}
28362311Sseb 	ASSERT(mtp == (mactype_t *)val);
28372311Sseb 
283810616SSebastien.Roy@Sun.COM 	if (mtp->mt_brdcst_addr != NULL)
283910616SSebastien.Roy@Sun.COM 		kmem_free(mtp->mt_brdcst_addr, mtp->mt_addr_length);
28402311Sseb 	kmem_free(mtp, sizeof (mactype_t));
28413288Sseb done:
28423288Sseb 	mutex_exit(&i_mactype_lock);
28433288Sseb 	return (err);
28442311Sseb }
28455903Ssowmini 
28468275SEric Cheng /*
284711878SVenu.Iyer@Sun.COM  * Checks the size of the value size specified for a property as
284811878SVenu.Iyer@Sun.COM  * part of a property operation. Returns B_TRUE if the size is
284911878SVenu.Iyer@Sun.COM  * correct, B_FALSE otherwise.
285011878SVenu.Iyer@Sun.COM  */
285111878SVenu.Iyer@Sun.COM boolean_t
mac_prop_check_size(mac_prop_id_t id,uint_t valsize,boolean_t is_range)285211878SVenu.Iyer@Sun.COM mac_prop_check_size(mac_prop_id_t id, uint_t valsize, boolean_t is_range)
285311878SVenu.Iyer@Sun.COM {
285411878SVenu.Iyer@Sun.COM 	uint_t minsize = 0;
285511878SVenu.Iyer@Sun.COM 
285611878SVenu.Iyer@Sun.COM 	if (is_range)
285711878SVenu.Iyer@Sun.COM 		return (valsize >= sizeof (mac_propval_range_t));
285811878SVenu.Iyer@Sun.COM 
285911878SVenu.Iyer@Sun.COM 	switch (id) {
286011878SVenu.Iyer@Sun.COM 	case MAC_PROP_ZONE:
286111878SVenu.Iyer@Sun.COM 		minsize = sizeof (dld_ioc_zid_t);
286211878SVenu.Iyer@Sun.COM 		break;
286311878SVenu.Iyer@Sun.COM 	case MAC_PROP_AUTOPUSH:
286411878SVenu.Iyer@Sun.COM 		if (valsize != 0)
286511878SVenu.Iyer@Sun.COM 			minsize = sizeof (struct dlautopush);
286611878SVenu.Iyer@Sun.COM 		break;
286711878SVenu.Iyer@Sun.COM 	case MAC_PROP_TAGMODE:
286811878SVenu.Iyer@Sun.COM 		minsize = sizeof (link_tagmode_t);
286911878SVenu.Iyer@Sun.COM 		break;
287011878SVenu.Iyer@Sun.COM 	case MAC_PROP_RESOURCE:
287111878SVenu.Iyer@Sun.COM 	case MAC_PROP_RESOURCE_EFF:
287211878SVenu.Iyer@Sun.COM 		minsize = sizeof (mac_resource_props_t);
287311878SVenu.Iyer@Sun.COM 		break;
287411878SVenu.Iyer@Sun.COM 	case MAC_PROP_DUPLEX:
287511878SVenu.Iyer@Sun.COM 		minsize = sizeof (link_duplex_t);
287611878SVenu.Iyer@Sun.COM 		break;
287711878SVenu.Iyer@Sun.COM 	case MAC_PROP_SPEED:
287811878SVenu.Iyer@Sun.COM 		minsize = sizeof (uint64_t);
287911878SVenu.Iyer@Sun.COM 		break;
288011878SVenu.Iyer@Sun.COM 	case MAC_PROP_STATUS:
288111878SVenu.Iyer@Sun.COM 		minsize = sizeof (link_state_t);
288211878SVenu.Iyer@Sun.COM 		break;
288311878SVenu.Iyer@Sun.COM 	case MAC_PROP_AUTONEG:
288411878SVenu.Iyer@Sun.COM 	case MAC_PROP_EN_AUTONEG:
288511878SVenu.Iyer@Sun.COM 		minsize = sizeof (uint8_t);
288611878SVenu.Iyer@Sun.COM 		break;
288711878SVenu.Iyer@Sun.COM 	case MAC_PROP_MTU:
288811878SVenu.Iyer@Sun.COM 	case MAC_PROP_LLIMIT:
288911878SVenu.Iyer@Sun.COM 	case MAC_PROP_LDECAY:
289011878SVenu.Iyer@Sun.COM 		minsize = sizeof (uint32_t);
289111878SVenu.Iyer@Sun.COM 		break;
289211878SVenu.Iyer@Sun.COM 	case MAC_PROP_FLOWCTRL:
289311878SVenu.Iyer@Sun.COM 		minsize = sizeof (link_flowctrl_t);
289411878SVenu.Iyer@Sun.COM 		break;
289511878SVenu.Iyer@Sun.COM 	case MAC_PROP_ADV_10GFDX_CAP:
289611878SVenu.Iyer@Sun.COM 	case MAC_PROP_EN_10GFDX_CAP:
289711878SVenu.Iyer@Sun.COM 	case MAC_PROP_ADV_1000HDX_CAP:
289811878SVenu.Iyer@Sun.COM 	case MAC_PROP_EN_1000HDX_CAP:
289911878SVenu.Iyer@Sun.COM 	case MAC_PROP_ADV_100FDX_CAP:
290011878SVenu.Iyer@Sun.COM 	case MAC_PROP_EN_100FDX_CAP:
290111878SVenu.Iyer@Sun.COM 	case MAC_PROP_ADV_100HDX_CAP:
290211878SVenu.Iyer@Sun.COM 	case MAC_PROP_EN_100HDX_CAP:
290311878SVenu.Iyer@Sun.COM 	case MAC_PROP_ADV_10FDX_CAP:
290411878SVenu.Iyer@Sun.COM 	case MAC_PROP_EN_10FDX_CAP:
290511878SVenu.Iyer@Sun.COM 	case MAC_PROP_ADV_10HDX_CAP:
290611878SVenu.Iyer@Sun.COM 	case MAC_PROP_EN_10HDX_CAP:
290711878SVenu.Iyer@Sun.COM 	case MAC_PROP_ADV_100T4_CAP:
290811878SVenu.Iyer@Sun.COM 	case MAC_PROP_EN_100T4_CAP:
290911878SVenu.Iyer@Sun.COM 		minsize = sizeof (uint8_t);
291011878SVenu.Iyer@Sun.COM 		break;
291111878SVenu.Iyer@Sun.COM 	case MAC_PROP_PVID:
291211878SVenu.Iyer@Sun.COM 		minsize = sizeof (uint16_t);
291311878SVenu.Iyer@Sun.COM 		break;
291411878SVenu.Iyer@Sun.COM 	case MAC_PROP_IPTUN_HOPLIMIT:
291511878SVenu.Iyer@Sun.COM 		minsize = sizeof (uint32_t);
291611878SVenu.Iyer@Sun.COM 		break;
291711878SVenu.Iyer@Sun.COM 	case MAC_PROP_IPTUN_ENCAPLIMIT:
291811878SVenu.Iyer@Sun.COM 		minsize = sizeof (uint32_t);
291911878SVenu.Iyer@Sun.COM 		break;
292011878SVenu.Iyer@Sun.COM 	case MAC_PROP_MAX_TX_RINGS_AVAIL:
292111878SVenu.Iyer@Sun.COM 	case MAC_PROP_MAX_RX_RINGS_AVAIL:
292211878SVenu.Iyer@Sun.COM 	case MAC_PROP_MAX_RXHWCLNT_AVAIL:
292311878SVenu.Iyer@Sun.COM 	case MAC_PROP_MAX_TXHWCLNT_AVAIL:
292411878SVenu.Iyer@Sun.COM 		minsize = sizeof (uint_t);
292511878SVenu.Iyer@Sun.COM 		break;
292611878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_ESSID:
292711878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_linkstatus_t);
292811878SVenu.Iyer@Sun.COM 		break;
292911878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_BSSID:
293011878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_bssid_t);
293111878SVenu.Iyer@Sun.COM 		break;
293211878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_BSSTYPE:
293311878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_bss_type_t);
293411878SVenu.Iyer@Sun.COM 		break;
293511878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_LINKSTATUS:
293611878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_linkstatus_t);
293711878SVenu.Iyer@Sun.COM 		break;
293811878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_DESIRED_RATES:
293911878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_rates_t);
294011878SVenu.Iyer@Sun.COM 		break;
294111878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_SUPPORTED_RATES:
294211878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_rates_t);
294311878SVenu.Iyer@Sun.COM 		break;
294411878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_AUTH_MODE:
294511878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_authmode_t);
294611878SVenu.Iyer@Sun.COM 		break;
294711878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_ENCRYPTION:
294811878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_encryption_t);
294911878SVenu.Iyer@Sun.COM 		break;
295011878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_RSSI:
295111878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_rssi_t);
295211878SVenu.Iyer@Sun.COM 		break;
295311878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_PHY_CONFIG:
295411878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_phy_conf_t);
295511878SVenu.Iyer@Sun.COM 		break;
295611878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_CAPABILITY:
295711878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_capability_t);
295811878SVenu.Iyer@Sun.COM 		break;
295911878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_WPA:
296011878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_wpa_t);
296111878SVenu.Iyer@Sun.COM 		break;
296211878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_SCANRESULTS:
296311878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_wpa_ess_t);
296411878SVenu.Iyer@Sun.COM 		break;
296511878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_POWER_MODE:
296611878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_ps_mode_t);
296711878SVenu.Iyer@Sun.COM 		break;
296811878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_RADIO:
296911878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_radio_t);
297011878SVenu.Iyer@Sun.COM 		break;
297111878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_ESS_LIST:
297211878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_ess_list_t);
297311878SVenu.Iyer@Sun.COM 		break;
297411878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_KEY_TAB:
297511878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_wep_key_tab_t);
297611878SVenu.Iyer@Sun.COM 		break;
297711878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_CREATE_IBSS:
297811878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_create_ibss_t);
297911878SVenu.Iyer@Sun.COM 		break;
298011878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_SETOPTIE:
298111878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_wpa_ie_t);
298211878SVenu.Iyer@Sun.COM 		break;
298311878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_DELKEY:
298411878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_del_key_t);
298511878SVenu.Iyer@Sun.COM 		break;
298611878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_KEY:
298711878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_key_t);
298811878SVenu.Iyer@Sun.COM 		break;
298911878SVenu.Iyer@Sun.COM 	case MAC_PROP_WL_MLME:
299011878SVenu.Iyer@Sun.COM 		minsize = sizeof (wl_mlme_t);
299111878SVenu.Iyer@Sun.COM 		break;
299211878SVenu.Iyer@Sun.COM 	}
299311878SVenu.Iyer@Sun.COM 
299411878SVenu.Iyer@Sun.COM 	return (valsize >= minsize);
299511878SVenu.Iyer@Sun.COM }
299611878SVenu.Iyer@Sun.COM 
299711878SVenu.Iyer@Sun.COM /*
299811878SVenu.Iyer@Sun.COM  * mac_set_prop() sets MAC or hardware driver properties:
299911878SVenu.Iyer@Sun.COM  *
300011878SVenu.Iyer@Sun.COM  * - MAC-managed properties such as resource properties include maxbw,
300111878SVenu.Iyer@Sun.COM  *   priority, and cpu binding list, as well as the default port VID
300211878SVenu.Iyer@Sun.COM  *   used by bridging. These properties are consumed by the MAC layer
300311878SVenu.Iyer@Sun.COM  *   itself and not passed down to the driver. For resource control
300411878SVenu.Iyer@Sun.COM  *   properties, this function invokes mac_set_resources() which will
300511878SVenu.Iyer@Sun.COM  *   cache the property value in mac_impl_t and may call
300611878SVenu.Iyer@Sun.COM  *   mac_client_set_resource() to update property value of the primary
300711878SVenu.Iyer@Sun.COM  *   mac client, if it exists.
300811878SVenu.Iyer@Sun.COM  *
300911878SVenu.Iyer@Sun.COM  * - Properties which act on the hardware and must be passed to the
301011878SVenu.Iyer@Sun.COM  *   driver, such as MTU, through the driver's mc_setprop() entry point.
30118275SEric Cheng  */
30125903Ssowmini int
mac_set_prop(mac_handle_t mh,mac_prop_id_t id,char * name,void * val,uint_t valsize)301311878SVenu.Iyer@Sun.COM mac_set_prop(mac_handle_t mh, mac_prop_id_t id, char *name, void *val,
301411878SVenu.Iyer@Sun.COM     uint_t valsize)
30155903Ssowmini {
30165903Ssowmini 	int err = ENOTSUP;
30175903Ssowmini 	mac_impl_t *mip = (mac_impl_t *)mh;
30185903Ssowmini 
30198275SEric Cheng 	ASSERT(MAC_PERIM_HELD(mh));
30208275SEric Cheng 
302111878SVenu.Iyer@Sun.COM 	switch (id) {
302211878SVenu.Iyer@Sun.COM 	case MAC_PROP_RESOURCE: {
302311878SVenu.Iyer@Sun.COM 		mac_resource_props_t *mrp;
302411878SVenu.Iyer@Sun.COM 
302511878SVenu.Iyer@Sun.COM 		/* call mac_set_resources() for MAC properties */
302611878SVenu.Iyer@Sun.COM 		ASSERT(valsize >= sizeof (mac_resource_props_t));
302711878SVenu.Iyer@Sun.COM 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
302811878SVenu.Iyer@Sun.COM 		bcopy(val, mrp, sizeof (*mrp));
302911878SVenu.Iyer@Sun.COM 		err = mac_set_resources(mh, mrp);
303011878SVenu.Iyer@Sun.COM 		kmem_free(mrp, sizeof (*mrp));
303110491SRishi.Srivatsavai@Sun.COM 		break;
30328275SEric Cheng 	}
303310491SRishi.Srivatsavai@Sun.COM 
303410491SRishi.Srivatsavai@Sun.COM 	case MAC_PROP_PVID:
303511878SVenu.Iyer@Sun.COM 		ASSERT(valsize >= sizeof (uint16_t));
303611878SVenu.Iyer@Sun.COM 		if (mip->mi_state_flags & MIS_IS_VNIC)
303710491SRishi.Srivatsavai@Sun.COM 			return (EINVAL);
303810491SRishi.Srivatsavai@Sun.COM 		err = mac_set_pvid(mh, *(uint16_t *)val);
303910491SRishi.Srivatsavai@Sun.COM 		break;
304010491SRishi.Srivatsavai@Sun.COM 
30418603SGirish.Moodalbail@Sun.COM 	case MAC_PROP_MTU: {
30428603SGirish.Moodalbail@Sun.COM 		uint32_t mtu;
30438603SGirish.Moodalbail@Sun.COM 
304411878SVenu.Iyer@Sun.COM 		ASSERT(valsize >= sizeof (uint32_t));
30458603SGirish.Moodalbail@Sun.COM 		bcopy(val, &mtu, sizeof (mtu));
30468603SGirish.Moodalbail@Sun.COM 		err = mac_set_mtu(mh, mtu, NULL);
30478603SGirish.Moodalbail@Sun.COM 		break;
30485903Ssowmini 	}
304910491SRishi.Srivatsavai@Sun.COM 
305010491SRishi.Srivatsavai@Sun.COM 	case MAC_PROP_LLIMIT:
305110491SRishi.Srivatsavai@Sun.COM 	case MAC_PROP_LDECAY: {
305210491SRishi.Srivatsavai@Sun.COM 		uint32_t learnval;
305310491SRishi.Srivatsavai@Sun.COM 
305410491SRishi.Srivatsavai@Sun.COM 		if (valsize < sizeof (learnval) ||
305510491SRishi.Srivatsavai@Sun.COM 		    (mip->mi_state_flags & MIS_IS_VNIC))
305610491SRishi.Srivatsavai@Sun.COM 			return (EINVAL);
305710491SRishi.Srivatsavai@Sun.COM 		bcopy(val, &learnval, sizeof (learnval));
305811878SVenu.Iyer@Sun.COM 		if (learnval == 0 && id == MAC_PROP_LDECAY)
305910491SRishi.Srivatsavai@Sun.COM 			return (EINVAL);
306011878SVenu.Iyer@Sun.COM 		if (id == MAC_PROP_LLIMIT)
306110491SRishi.Srivatsavai@Sun.COM 			mip->mi_llimit = learnval;
306210491SRishi.Srivatsavai@Sun.COM 		else
306310491SRishi.Srivatsavai@Sun.COM 			mip->mi_ldecay = learnval;
306410491SRishi.Srivatsavai@Sun.COM 		err = 0;
306510491SRishi.Srivatsavai@Sun.COM 		break;
306610491SRishi.Srivatsavai@Sun.COM 	}
306710491SRishi.Srivatsavai@Sun.COM 
30688603SGirish.Moodalbail@Sun.COM 	default:
30698603SGirish.Moodalbail@Sun.COM 		/* For other driver properties, call driver's callback */
30708603SGirish.Moodalbail@Sun.COM 		if (mip->mi_callbacks->mc_callbacks & MC_SETPROP) {
30718603SGirish.Moodalbail@Sun.COM 			err = mip->mi_callbacks->mc_setprop(mip->mi_driver,
307211878SVenu.Iyer@Sun.COM 			    name, id, valsize, val);
30738603SGirish.Moodalbail@Sun.COM 		}
30748603SGirish.Moodalbail@Sun.COM 	}
30755903Ssowmini 	return (err);
30765903Ssowmini }
30775903Ssowmini 
30788275SEric Cheng /*
307911878SVenu.Iyer@Sun.COM  * mac_get_prop() gets MAC or device driver properties.
30808275SEric Cheng  *
30818275SEric Cheng  * If the property is a driver property, mac_get_prop() calls driver's callback
308211878SVenu.Iyer@Sun.COM  * entry point to get it.
308311878SVenu.Iyer@Sun.COM  * If the property is a MAC property, mac_get_prop() invokes mac_get_resources()
30848275SEric Cheng  * which returns the cached value in mac_impl_t.
30858275SEric Cheng  */
30865903Ssowmini int
mac_get_prop(mac_handle_t mh,mac_prop_id_t id,char * name,void * val,uint_t valsize)308711878SVenu.Iyer@Sun.COM mac_get_prop(mac_handle_t mh, mac_prop_id_t id, char *name, void *val,
308811878SVenu.Iyer@Sun.COM     uint_t valsize)
30895903Ssowmini {
30905903Ssowmini 	int err = ENOTSUP;
30915903Ssowmini 	mac_impl_t *mip = (mac_impl_t *)mh;
309211878SVenu.Iyer@Sun.COM 	uint_t	rings;
309311878SVenu.Iyer@Sun.COM 	uint_t	vlinks;
309411878SVenu.Iyer@Sun.COM 
309511878SVenu.Iyer@Sun.COM 	bzero(val, valsize);
309611878SVenu.Iyer@Sun.COM 
309711878SVenu.Iyer@Sun.COM 	switch (id) {
309811878SVenu.Iyer@Sun.COM 	case MAC_PROP_RESOURCE: {
309911878SVenu.Iyer@Sun.COM 		mac_resource_props_t *mrp;
31008275SEric Cheng 
310110491SRishi.Srivatsavai@Sun.COM 		/* If mac property, read from cache */
310211878SVenu.Iyer@Sun.COM 		ASSERT(valsize >= sizeof (mac_resource_props_t));
310311878SVenu.Iyer@Sun.COM 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
310411878SVenu.Iyer@Sun.COM 		mac_get_resources(mh, mrp);
310511878SVenu.Iyer@Sun.COM 		bcopy(mrp, val, sizeof (*mrp));
310611878SVenu.Iyer@Sun.COM 		kmem_free(mrp, sizeof (*mrp));
310711878SVenu.Iyer@Sun.COM 		return (0);
310811878SVenu.Iyer@Sun.COM 	}
310911878SVenu.Iyer@Sun.COM 	case MAC_PROP_RESOURCE_EFF: {
311011878SVenu.Iyer@Sun.COM 		mac_resource_props_t *mrp;
311111878SVenu.Iyer@Sun.COM 
311211878SVenu.Iyer@Sun.COM 		/* If mac effective property, read from client */
311311878SVenu.Iyer@Sun.COM 		ASSERT(valsize >= sizeof (mac_resource_props_t));
311411878SVenu.Iyer@Sun.COM 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
311511878SVenu.Iyer@Sun.COM 		mac_get_effective_resources(mh, mrp);
311611878SVenu.Iyer@Sun.COM 		bcopy(mrp, val, sizeof (*mrp));
311711878SVenu.Iyer@Sun.COM 		kmem_free(mrp, sizeof (*mrp));
31188275SEric Cheng 		return (0);
31198275SEric Cheng 	}
31208275SEric Cheng 
312110491SRishi.Srivatsavai@Sun.COM 	case MAC_PROP_PVID:
312211878SVenu.Iyer@Sun.COM 		ASSERT(valsize >= sizeof (uint16_t));
312311878SVenu.Iyer@Sun.COM 		if (mip->mi_state_flags & MIS_IS_VNIC)
312410491SRishi.Srivatsavai@Sun.COM 			return (EINVAL);
312510491SRishi.Srivatsavai@Sun.COM 		*(uint16_t *)val = mac_get_pvid(mh);
312610491SRishi.Srivatsavai@Sun.COM 		return (0);
312710491SRishi.Srivatsavai@Sun.COM 
312810491SRishi.Srivatsavai@Sun.COM 	case MAC_PROP_LLIMIT:
312910491SRishi.Srivatsavai@Sun.COM 	case MAC_PROP_LDECAY:
313011878SVenu.Iyer@Sun.COM 		ASSERT(valsize >= sizeof (uint32_t));
313111878SVenu.Iyer@Sun.COM 		if (mip->mi_state_flags & MIS_IS_VNIC)
313210491SRishi.Srivatsavai@Sun.COM 			return (EINVAL);
313311878SVenu.Iyer@Sun.COM 		if (id == MAC_PROP_LLIMIT)
313410491SRishi.Srivatsavai@Sun.COM 			bcopy(&mip->mi_llimit, val, sizeof (mip->mi_llimit));
313510491SRishi.Srivatsavai@Sun.COM 		else
313610491SRishi.Srivatsavai@Sun.COM 			bcopy(&mip->mi_ldecay, val, sizeof (mip->mi_ldecay));
313710491SRishi.Srivatsavai@Sun.COM 		return (0);
313810491SRishi.Srivatsavai@Sun.COM 
31399514SGirish.Moodalbail@Sun.COM 	case MAC_PROP_MTU: {
31409514SGirish.Moodalbail@Sun.COM 		uint32_t sdu;
314111878SVenu.Iyer@Sun.COM 
314211878SVenu.Iyer@Sun.COM 		ASSERT(valsize >= sizeof (uint32_t));
3143*13123SErik.Nordmark@Sun.COM 		mac_sdu_get2(mh, NULL, &sdu, NULL);
314411878SVenu.Iyer@Sun.COM 		bcopy(&sdu, val, sizeof (sdu));
314511878SVenu.Iyer@Sun.COM 
314611878SVenu.Iyer@Sun.COM 		return (0);
314711878SVenu.Iyer@Sun.COM 	}
314811878SVenu.Iyer@Sun.COM 	case MAC_PROP_STATUS: {
314911878SVenu.Iyer@Sun.COM 		link_state_t link_state;
315011878SVenu.Iyer@Sun.COM 
31516512Ssowmini 		if (valsize < sizeof (link_state))
31526512Ssowmini 			return (EINVAL);
31536512Ssowmini 		link_state = mac_link_get(mh);
31546512Ssowmini 		bcopy(&link_state, val, sizeof (link_state));
315511878SVenu.Iyer@Sun.COM 
31566512Ssowmini 		return (0);
315711878SVenu.Iyer@Sun.COM 	}
315811878SVenu.Iyer@Sun.COM 
315911878SVenu.Iyer@Sun.COM 	case MAC_PROP_MAX_RX_RINGS_AVAIL:
316011878SVenu.Iyer@Sun.COM 	case MAC_PROP_MAX_TX_RINGS_AVAIL:
316111878SVenu.Iyer@Sun.COM 		ASSERT(valsize >= sizeof (uint_t));
316211878SVenu.Iyer@Sun.COM 		rings = id == MAC_PROP_MAX_RX_RINGS_AVAIL ?
316311878SVenu.Iyer@Sun.COM 		    mac_rxavail_get(mh) : mac_txavail_get(mh);
316411878SVenu.Iyer@Sun.COM 		bcopy(&rings, val, sizeof (uint_t));
316511878SVenu.Iyer@Sun.COM 		return (0);
316611878SVenu.Iyer@Sun.COM 
316711878SVenu.Iyer@Sun.COM 	case MAC_PROP_MAX_RXHWCLNT_AVAIL:
316811878SVenu.Iyer@Sun.COM 	case MAC_PROP_MAX_TXHWCLNT_AVAIL:
316911878SVenu.Iyer@Sun.COM 		ASSERT(valsize >= sizeof (uint_t));
317011878SVenu.Iyer@Sun.COM 		vlinks = id == MAC_PROP_MAX_RXHWCLNT_AVAIL ?
317111878SVenu.Iyer@Sun.COM 		    mac_rxhwlnksavail_get(mh) : mac_txhwlnksavail_get(mh);
317211878SVenu.Iyer@Sun.COM 		bcopy(&vlinks, val, sizeof (uint_t));
317311878SVenu.Iyer@Sun.COM 		return (0);
317411878SVenu.Iyer@Sun.COM 
317511878SVenu.Iyer@Sun.COM 	case MAC_PROP_RXRINGSRANGE:
317611878SVenu.Iyer@Sun.COM 	case MAC_PROP_TXRINGSRANGE:
317711878SVenu.Iyer@Sun.COM 		/*
317811878SVenu.Iyer@Sun.COM 		 * The value for these properties are returned through
317911878SVenu.Iyer@Sun.COM 		 * the MAC_PROP_RESOURCE property.
318011878SVenu.Iyer@Sun.COM 		 */
318111878SVenu.Iyer@Sun.COM 		return (0);
318211878SVenu.Iyer@Sun.COM 
31836512Ssowmini 	default:
31846512Ssowmini 		break;
31858275SEric Cheng 
31866512Ssowmini 	}
318711878SVenu.Iyer@Sun.COM 
31888275SEric Cheng 	/* If driver property, request from driver */
318911878SVenu.Iyer@Sun.COM 	if (mip->mi_callbacks->mc_callbacks & MC_GETPROP) {
319011878SVenu.Iyer@Sun.COM 		err = mip->mi_callbacks->mc_getprop(mip->mi_driver, name, id,
319111878SVenu.Iyer@Sun.COM 		    valsize, val);
319211878SVenu.Iyer@Sun.COM 	}
319311878SVenu.Iyer@Sun.COM 
31945903Ssowmini 	return (err);
31955903Ssowmini }
31965903Ssowmini 
319711878SVenu.Iyer@Sun.COM /*
319811878SVenu.Iyer@Sun.COM  * Helper function to initialize the range structure for use in
319911878SVenu.Iyer@Sun.COM  * mac_get_prop. If the type can be other than uint32, we can
320011878SVenu.Iyer@Sun.COM  * pass that as an arg.
320111878SVenu.Iyer@Sun.COM  */
320211878SVenu.Iyer@Sun.COM static void
_mac_set_range(mac_propval_range_t * range,uint32_t min,uint32_t max)320311878SVenu.Iyer@Sun.COM _mac_set_range(mac_propval_range_t *range, uint32_t min, uint32_t max)
320411878SVenu.Iyer@Sun.COM {
320511878SVenu.Iyer@Sun.COM 	range->mpr_count = 1;
320611878SVenu.Iyer@Sun.COM 	range->mpr_type = MAC_PROPVAL_UINT32;
320711878SVenu.Iyer@Sun.COM 	range->mpr_range_uint32[0].mpur_min = min;
320811878SVenu.Iyer@Sun.COM 	range->mpr_range_uint32[0].mpur_max = max;
320911878SVenu.Iyer@Sun.COM }
321011878SVenu.Iyer@Sun.COM 
321111878SVenu.Iyer@Sun.COM /*
321211878SVenu.Iyer@Sun.COM  * Returns information about the specified property, such as default
321311878SVenu.Iyer@Sun.COM  * values or permissions.
321411878SVenu.Iyer@Sun.COM  */
321511878SVenu.Iyer@Sun.COM int
mac_prop_info(mac_handle_t mh,mac_prop_id_t id,char * name,void * default_val,uint_t default_size,mac_propval_range_t * range,uint_t * perm)321611878SVenu.Iyer@Sun.COM mac_prop_info(mac_handle_t mh, mac_prop_id_t id, char *name,
321711878SVenu.Iyer@Sun.COM     void *default_val, uint_t default_size, mac_propval_range_t *range,
321811878SVenu.Iyer@Sun.COM     uint_t *perm)
321911878SVenu.Iyer@Sun.COM {
322011878SVenu.Iyer@Sun.COM 	mac_prop_info_state_t state;
322111878SVenu.Iyer@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
322211878SVenu.Iyer@Sun.COM 	uint_t	max;
322311878SVenu.Iyer@Sun.COM 
322411878SVenu.Iyer@Sun.COM 	/*
322511878SVenu.Iyer@Sun.COM 	 * A property is read/write by default unless the driver says
322611878SVenu.Iyer@Sun.COM 	 * otherwise.
322711878SVenu.Iyer@Sun.COM 	 */
322811878SVenu.Iyer@Sun.COM 	if (perm != NULL)
322911878SVenu.Iyer@Sun.COM 		*perm = MAC_PROP_PERM_RW;
323011878SVenu.Iyer@Sun.COM 
323111878SVenu.Iyer@Sun.COM 	if (default_val != NULL)
323211878SVenu.Iyer@Sun.COM 		bzero(default_val, default_size);
323311878SVenu.Iyer@Sun.COM 
323411878SVenu.Iyer@Sun.COM 	/*
323511878SVenu.Iyer@Sun.COM 	 * First, handle framework properties for which we don't need to
323611878SVenu.Iyer@Sun.COM 	 * involve the driver.
323711878SVenu.Iyer@Sun.COM 	 */
323811878SVenu.Iyer@Sun.COM 	switch (id) {
323911878SVenu.Iyer@Sun.COM 	case MAC_PROP_RESOURCE:
324011878SVenu.Iyer@Sun.COM 	case MAC_PROP_PVID:
324111878SVenu.Iyer@Sun.COM 	case MAC_PROP_LLIMIT:
324211878SVenu.Iyer@Sun.COM 	case MAC_PROP_LDECAY:
324311878SVenu.Iyer@Sun.COM 		return (0);
324411878SVenu.Iyer@Sun.COM 
324511878SVenu.Iyer@Sun.COM 	case MAC_PROP_MAX_RX_RINGS_AVAIL:
324611878SVenu.Iyer@Sun.COM 	case MAC_PROP_MAX_TX_RINGS_AVAIL:
324711878SVenu.Iyer@Sun.COM 	case MAC_PROP_MAX_RXHWCLNT_AVAIL:
324811878SVenu.Iyer@Sun.COM 	case MAC_PROP_MAX_TXHWCLNT_AVAIL:
324911878SVenu.Iyer@Sun.COM 		if (perm != NULL)
325011878SVenu.Iyer@Sun.COM 			*perm = MAC_PROP_PERM_READ;
325111878SVenu.Iyer@Sun.COM 		return (0);
325211878SVenu.Iyer@Sun.COM 
325311878SVenu.Iyer@Sun.COM 	case MAC_PROP_RXRINGSRANGE:
325411878SVenu.Iyer@Sun.COM 	case MAC_PROP_TXRINGSRANGE:
325511878SVenu.Iyer@Sun.COM 		/*
325611878SVenu.Iyer@Sun.COM 		 * Currently, we support range for RX and TX rings properties.
325711878SVenu.Iyer@Sun.COM 		 * When we extend this support to maxbw, cpus and priority,
325811878SVenu.Iyer@Sun.COM 		 * we should move this to mac_get_resources.
325911878SVenu.Iyer@Sun.COM 		 * There is no default value for RX or TX rings.
326011878SVenu.Iyer@Sun.COM 		 */
326111878SVenu.Iyer@Sun.COM 		if ((mip->mi_state_flags & MIS_IS_VNIC) &&
326211878SVenu.Iyer@Sun.COM 		    mac_is_vnic_primary(mh)) {
326311878SVenu.Iyer@Sun.COM 			/*
326411878SVenu.Iyer@Sun.COM 			 * We don't support setting rings for a VLAN
326511878SVenu.Iyer@Sun.COM 			 * data link because it shares its ring with the
326611878SVenu.Iyer@Sun.COM 			 * primary MAC client.
326711878SVenu.Iyer@Sun.COM 			 */
326811878SVenu.Iyer@Sun.COM 			if (perm != NULL)
326911878SVenu.Iyer@Sun.COM 				*perm = MAC_PROP_PERM_READ;
327011878SVenu.Iyer@Sun.COM 			if (range != NULL)
327111878SVenu.Iyer@Sun.COM 				range->mpr_count = 0;
327211878SVenu.Iyer@Sun.COM 		} else if (range != NULL) {
327311878SVenu.Iyer@Sun.COM 			if (mip->mi_state_flags & MIS_IS_VNIC)
327411878SVenu.Iyer@Sun.COM 				mh = mac_get_lower_mac_handle(mh);
327511878SVenu.Iyer@Sun.COM 			mip = (mac_impl_t *)mh;
327611878SVenu.Iyer@Sun.COM 			if ((id == MAC_PROP_RXRINGSRANGE &&
327711878SVenu.Iyer@Sun.COM 			    mip->mi_rx_group_type == MAC_GROUP_TYPE_STATIC) ||
327811878SVenu.Iyer@Sun.COM 			    (id == MAC_PROP_TXRINGSRANGE &&
327911878SVenu.Iyer@Sun.COM 			    mip->mi_tx_group_type == MAC_GROUP_TYPE_STATIC)) {
328011878SVenu.Iyer@Sun.COM 				if (id == MAC_PROP_RXRINGSRANGE) {
328111878SVenu.Iyer@Sun.COM 					if ((mac_rxhwlnksavail_get(mh) +
328211878SVenu.Iyer@Sun.COM 					    mac_rxhwlnksrsvd_get(mh)) <= 1) {
328311878SVenu.Iyer@Sun.COM 						/*
328411878SVenu.Iyer@Sun.COM 						 * doesn't support groups or
328511878SVenu.Iyer@Sun.COM 						 * rings
328611878SVenu.Iyer@Sun.COM 						 */
328711878SVenu.Iyer@Sun.COM 						range->mpr_count = 0;
328811878SVenu.Iyer@Sun.COM 					} else {
328911878SVenu.Iyer@Sun.COM 						/*
329011878SVenu.Iyer@Sun.COM 						 * supports specifying groups,
329111878SVenu.Iyer@Sun.COM 						 * but not rings
329211878SVenu.Iyer@Sun.COM 						 */
329311878SVenu.Iyer@Sun.COM 						_mac_set_range(range, 0, 0);
329411878SVenu.Iyer@Sun.COM 					}
329511878SVenu.Iyer@Sun.COM 				} else {
329611878SVenu.Iyer@Sun.COM 					if ((mac_txhwlnksavail_get(mh) +
329711878SVenu.Iyer@Sun.COM 					    mac_txhwlnksrsvd_get(mh)) <= 1) {
329811878SVenu.Iyer@Sun.COM 						/*
329911878SVenu.Iyer@Sun.COM 						 * doesn't support groups or
330011878SVenu.Iyer@Sun.COM 						 * rings
330111878SVenu.Iyer@Sun.COM 						 */
330211878SVenu.Iyer@Sun.COM 						range->mpr_count = 0;
330311878SVenu.Iyer@Sun.COM 					} else {
330411878SVenu.Iyer@Sun.COM 						/*
330511878SVenu.Iyer@Sun.COM 						 * supports specifying groups,
330611878SVenu.Iyer@Sun.COM 						 * but not rings
330711878SVenu.Iyer@Sun.COM 						 */
330811878SVenu.Iyer@Sun.COM 						_mac_set_range(range, 0, 0);
330911878SVenu.Iyer@Sun.COM 					}
331011878SVenu.Iyer@Sun.COM 				}
331111878SVenu.Iyer@Sun.COM 			} else {
331211878SVenu.Iyer@Sun.COM 				max = id == MAC_PROP_RXRINGSRANGE ?
331311878SVenu.Iyer@Sun.COM 				    mac_rxavail_get(mh) + mac_rxrsvd_get(mh) :
331411878SVenu.Iyer@Sun.COM 				    mac_txavail_get(mh) + mac_txrsvd_get(mh);
331511878SVenu.Iyer@Sun.COM 				if (max <= 1) {
331611878SVenu.Iyer@Sun.COM 					/*
331711878SVenu.Iyer@Sun.COM 					 * doesn't support groups or
331811878SVenu.Iyer@Sun.COM 					 * rings
331911878SVenu.Iyer@Sun.COM 					 */
332011878SVenu.Iyer@Sun.COM 					range->mpr_count = 0;
332111878SVenu.Iyer@Sun.COM 				} else  {
332211878SVenu.Iyer@Sun.COM 					/*
332311878SVenu.Iyer@Sun.COM 					 * -1 because we have to leave out the
332411878SVenu.Iyer@Sun.COM 					 * default ring.
332511878SVenu.Iyer@Sun.COM 					 */
332611878SVenu.Iyer@Sun.COM 					_mac_set_range(range, 1, max - 1);
332711878SVenu.Iyer@Sun.COM 				}
332811878SVenu.Iyer@Sun.COM 			}
332911878SVenu.Iyer@Sun.COM 		}
333011878SVenu.Iyer@Sun.COM 		return (0);
333111878SVenu.Iyer@Sun.COM 
333211878SVenu.Iyer@Sun.COM 	case MAC_PROP_STATUS:
333311878SVenu.Iyer@Sun.COM 		if (perm != NULL)
333411878SVenu.Iyer@Sun.COM 			*perm = MAC_PROP_PERM_READ;
333511878SVenu.Iyer@Sun.COM 		return (0);
333611878SVenu.Iyer@Sun.COM 	}
333711878SVenu.Iyer@Sun.COM 
333811878SVenu.Iyer@Sun.COM 	/*
333911878SVenu.Iyer@Sun.COM 	 * Get the property info from the driver if it implements the
334011878SVenu.Iyer@Sun.COM 	 * property info entry point.
334111878SVenu.Iyer@Sun.COM 	 */
334211878SVenu.Iyer@Sun.COM 	bzero(&state, sizeof (state));
334311878SVenu.Iyer@Sun.COM 
334411878SVenu.Iyer@Sun.COM 	if (mip->mi_callbacks->mc_callbacks & MC_PROPINFO) {
334511878SVenu.Iyer@Sun.COM 		state.pr_default = default_val;
334611878SVenu.Iyer@Sun.COM 		state.pr_default_size = default_size;
334712850SPrakash.Jalan@Sun.COM 
334812850SPrakash.Jalan@Sun.COM 		/*
334912850SPrakash.Jalan@Sun.COM 		 * The caller specifies the maximum number of ranges
335012850SPrakash.Jalan@Sun.COM 		 * it can accomodate using mpr_count. We don't touch
335112850SPrakash.Jalan@Sun.COM 		 * this value until the driver returns from its
335212850SPrakash.Jalan@Sun.COM 		 * mc_propinfo() callback, and ensure we don't exceed
335312850SPrakash.Jalan@Sun.COM 		 * this number of range as the driver defines
335412850SPrakash.Jalan@Sun.COM 		 * supported range from its mc_propinfo().
335512850SPrakash.Jalan@Sun.COM 		 *
335612850SPrakash.Jalan@Sun.COM 		 * pr_range_cur_count keeps track of how many ranges
335712850SPrakash.Jalan@Sun.COM 		 * were defined by the driver from its mc_propinfo()
335812850SPrakash.Jalan@Sun.COM 		 * entry point.
335912850SPrakash.Jalan@Sun.COM 		 *
336012850SPrakash.Jalan@Sun.COM 		 * On exit, the user-specified range mpr_count returns
336112850SPrakash.Jalan@Sun.COM 		 * the number of ranges specified by the driver on
336212850SPrakash.Jalan@Sun.COM 		 * success, or the number of ranges it wanted to
336312850SPrakash.Jalan@Sun.COM 		 * define if that number of ranges could not be
336412850SPrakash.Jalan@Sun.COM 		 * accomodated by the specified range structure.  In
336512850SPrakash.Jalan@Sun.COM 		 * the latter case, the caller will be able to
336612850SPrakash.Jalan@Sun.COM 		 * allocate a larger range structure, and query the
336712850SPrakash.Jalan@Sun.COM 		 * property again.
336812850SPrakash.Jalan@Sun.COM 		 */
336912850SPrakash.Jalan@Sun.COM 		state.pr_range_cur_count = 0;
337011878SVenu.Iyer@Sun.COM 		state.pr_range = range;
337111878SVenu.Iyer@Sun.COM 
337211878SVenu.Iyer@Sun.COM 		mip->mi_callbacks->mc_propinfo(mip->mi_driver, name, id,
337311878SVenu.Iyer@Sun.COM 		    (mac_prop_info_handle_t)&state);
337411878SVenu.Iyer@Sun.COM 
337512850SPrakash.Jalan@Sun.COM 		if (state.pr_flags & MAC_PROP_INFO_RANGE)
337612850SPrakash.Jalan@Sun.COM 			range->mpr_count = state.pr_range_cur_count;
337712850SPrakash.Jalan@Sun.COM 
337811878SVenu.Iyer@Sun.COM 		/*
337911878SVenu.Iyer@Sun.COM 		 * The operation could fail if the buffer supplied by
338011878SVenu.Iyer@Sun.COM 		 * the user was too small for the range or default
338111878SVenu.Iyer@Sun.COM 		 * value of the property.
338211878SVenu.Iyer@Sun.COM 		 */
338312850SPrakash.Jalan@Sun.COM 		if (state.pr_errno != 0)
338412850SPrakash.Jalan@Sun.COM 			return (state.pr_errno);
338511878SVenu.Iyer@Sun.COM 
338611878SVenu.Iyer@Sun.COM 		if (perm != NULL && state.pr_flags & MAC_PROP_INFO_PERM)
338711878SVenu.Iyer@Sun.COM 			*perm = state.pr_perm;
338811878SVenu.Iyer@Sun.COM 	}
338911878SVenu.Iyer@Sun.COM 
339011878SVenu.Iyer@Sun.COM 	/*
339111878SVenu.Iyer@Sun.COM 	 * The MAC layer may want to provide default values or allowed
339211878SVenu.Iyer@Sun.COM 	 * ranges for properties if the driver does not provide a
339311878SVenu.Iyer@Sun.COM 	 * property info entry point, or that entry point exists, but
339411878SVenu.Iyer@Sun.COM 	 * it did not provide a default value or allowed ranges for
339511878SVenu.Iyer@Sun.COM 	 * that property.
339611878SVenu.Iyer@Sun.COM 	 */
339711878SVenu.Iyer@Sun.COM 	switch (id) {
339811878SVenu.Iyer@Sun.COM 	case MAC_PROP_MTU: {
339911878SVenu.Iyer@Sun.COM 		uint32_t sdu;
340011878SVenu.Iyer@Sun.COM 
3401*13123SErik.Nordmark@Sun.COM 		mac_sdu_get2(mh, NULL, &sdu, NULL);
340211878SVenu.Iyer@Sun.COM 
340311878SVenu.Iyer@Sun.COM 		if (range != NULL && !(state.pr_flags &
340411878SVenu.Iyer@Sun.COM 		    MAC_PROP_INFO_RANGE)) {
340511878SVenu.Iyer@Sun.COM 			/* MTU range */
340611878SVenu.Iyer@Sun.COM 			_mac_set_range(range, sdu, sdu);
340711878SVenu.Iyer@Sun.COM 		}
340811878SVenu.Iyer@Sun.COM 
340911878SVenu.Iyer@Sun.COM 		if (default_val != NULL && !(state.pr_flags &
341011878SVenu.Iyer@Sun.COM 		    MAC_PROP_INFO_DEFAULT)) {
341111878SVenu.Iyer@Sun.COM 			if (mip->mi_info.mi_media == DL_ETHER)
341211878SVenu.Iyer@Sun.COM 				sdu = ETHERMTU;
341311878SVenu.Iyer@Sun.COM 			/* default MTU value */
341411878SVenu.Iyer@Sun.COM 			bcopy(&sdu, default_val, sizeof (sdu));
341511878SVenu.Iyer@Sun.COM 		}
341611878SVenu.Iyer@Sun.COM 	}
341711878SVenu.Iyer@Sun.COM 	}
341811878SVenu.Iyer@Sun.COM 
341911878SVenu.Iyer@Sun.COM 	return (0);
342011878SVenu.Iyer@Sun.COM }
342111878SVenu.Iyer@Sun.COM 
34229073SCathy.Zhou@Sun.COM int
mac_fastpath_disable(mac_handle_t mh)34239073SCathy.Zhou@Sun.COM mac_fastpath_disable(mac_handle_t mh)
34249073SCathy.Zhou@Sun.COM {
34259073SCathy.Zhou@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
34269073SCathy.Zhou@Sun.COM 
34279073SCathy.Zhou@Sun.COM 	if ((mip->mi_state_flags & MIS_LEGACY) == 0)
34289073SCathy.Zhou@Sun.COM 		return (0);
34299073SCathy.Zhou@Sun.COM 
34309073SCathy.Zhou@Sun.COM 	return (mip->mi_capab_legacy.ml_fastpath_disable(mip->mi_driver));
34319073SCathy.Zhou@Sun.COM }
34329073SCathy.Zhou@Sun.COM 
34339073SCathy.Zhou@Sun.COM void
mac_fastpath_enable(mac_handle_t mh)34349073SCathy.Zhou@Sun.COM mac_fastpath_enable(mac_handle_t mh)
34359073SCathy.Zhou@Sun.COM {
34369073SCathy.Zhou@Sun.COM 	mac_impl_t	*mip = (mac_impl_t *)mh;
34379073SCathy.Zhou@Sun.COM 
34389073SCathy.Zhou@Sun.COM 	if ((mip->mi_state_flags & MIS_LEGACY) == 0)
34399073SCathy.Zhou@Sun.COM 		return;
34409073SCathy.Zhou@Sun.COM 
34419073SCathy.Zhou@Sun.COM 	mip->mi_capab_legacy.ml_fastpath_enable(mip->mi_driver);
34429073SCathy.Zhou@Sun.COM }
34439073SCathy.Zhou@Sun.COM 
34448275SEric Cheng void
mac_register_priv_prop(mac_impl_t * mip,char ** priv_props)344511878SVenu.Iyer@Sun.COM mac_register_priv_prop(mac_impl_t *mip, char **priv_props)
344611878SVenu.Iyer@Sun.COM {
344711878SVenu.Iyer@Sun.COM 	uint_t nprops, i;
344811878SVenu.Iyer@Sun.COM 
344911878SVenu.Iyer@Sun.COM 	if (priv_props == NULL)
345011878SVenu.Iyer@Sun.COM 		return;
345111878SVenu.Iyer@Sun.COM 
345211878SVenu.Iyer@Sun.COM 	nprops = 0;
345311878SVenu.Iyer@Sun.COM 	while (priv_props[nprops] != NULL)
345411878SVenu.Iyer@Sun.COM 		nprops++;
345511878SVenu.Iyer@Sun.COM 	if (nprops == 0)
34566512Ssowmini 		return;
34576512Ssowmini 
345811878SVenu.Iyer@Sun.COM 
345911878SVenu.Iyer@Sun.COM 	mip->mi_priv_prop = kmem_zalloc(nprops * sizeof (char *), KM_SLEEP);
346011878SVenu.Iyer@Sun.COM 
346111878SVenu.Iyer@Sun.COM 	for (i = 0; i < nprops; i++) {
346211878SVenu.Iyer@Sun.COM 		mip->mi_priv_prop[i] = kmem_zalloc(MAXLINKPROPNAME, KM_SLEEP);
346311878SVenu.Iyer@Sun.COM 		(void) strlcpy(mip->mi_priv_prop[i], priv_props[i],
346411878SVenu.Iyer@Sun.COM 		    MAXLINKPROPNAME);
346511878SVenu.Iyer@Sun.COM 	}
346611878SVenu.Iyer@Sun.COM 
346711878SVenu.Iyer@Sun.COM 	mip->mi_priv_prop_count = nprops;
34686512Ssowmini }
34697406SSowmini.Varadhan@Sun.COM 
34708275SEric Cheng void
mac_unregister_priv_prop(mac_impl_t * mip)34717406SSowmini.Varadhan@Sun.COM mac_unregister_priv_prop(mac_impl_t *mip)
34727406SSowmini.Varadhan@Sun.COM {
347311878SVenu.Iyer@Sun.COM 	uint_t i;
347411878SVenu.Iyer@Sun.COM 
347511878SVenu.Iyer@Sun.COM 	if (mip->mi_priv_prop_count == 0) {
347611878SVenu.Iyer@Sun.COM 		ASSERT(mip->mi_priv_prop == NULL);
347711878SVenu.Iyer@Sun.COM 		return;
347811878SVenu.Iyer@Sun.COM 	}
347911878SVenu.Iyer@Sun.COM 
348011878SVenu.Iyer@Sun.COM 	for (i = 0; i < mip->mi_priv_prop_count; i++)
348111878SVenu.Iyer@Sun.COM 		kmem_free(mip->mi_priv_prop[i], MAXLINKPROPNAME);
348211878SVenu.Iyer@Sun.COM 	kmem_free(mip->mi_priv_prop, mip->mi_priv_prop_count *
348311878SVenu.Iyer@Sun.COM 	    sizeof (char *));
348411878SVenu.Iyer@Sun.COM 
348511878SVenu.Iyer@Sun.COM 	mip->mi_priv_prop = NULL;
34867406SSowmini.Varadhan@Sun.COM 	mip->mi_priv_prop_count = 0;
34877406SSowmini.Varadhan@Sun.COM }
34888275SEric Cheng 
34898275SEric Cheng /*
34908275SEric Cheng  * mac_ring_t 'mr' macros. Some rogue drivers may access ring structure
34918275SEric Cheng  * (by invoking mac_rx()) even after processing mac_stop_ring(). In such
34928275SEric Cheng  * cases if MAC free's the ring structure after mac_stop_ring(), any
34938275SEric Cheng  * illegal access to the ring structure coming from the driver will panic
34948275SEric Cheng  * the system. In order to protect the system from such inadverent access,
34958275SEric Cheng  * we maintain a cache of rings in the mac_impl_t after they get free'd up.
34968275SEric Cheng  * When packets are received on free'd up rings, MAC (through the generation
34978275SEric Cheng  * count mechanism) will drop such packets.
34988275SEric Cheng  */
34998275SEric Cheng static mac_ring_t *
mac_ring_alloc(mac_impl_t * mip)350011878SVenu.Iyer@Sun.COM mac_ring_alloc(mac_impl_t *mip)
35018275SEric Cheng {
35028275SEric Cheng 	mac_ring_t *ring;
35038275SEric Cheng 
350411878SVenu.Iyer@Sun.COM 	mutex_enter(&mip->mi_ring_lock);
350511878SVenu.Iyer@Sun.COM 	if (mip->mi_ring_freelist != NULL) {
350611878SVenu.Iyer@Sun.COM 		ring = mip->mi_ring_freelist;
350711878SVenu.Iyer@Sun.COM 		mip->mi_ring_freelist = ring->mr_next;
350811878SVenu.Iyer@Sun.COM 		bzero(ring, sizeof (mac_ring_t));
35098275SEric Cheng 		mutex_exit(&mip->mi_ring_lock);
35108275SEric Cheng 	} else {
351111878SVenu.Iyer@Sun.COM 		mutex_exit(&mip->mi_ring_lock);
351211878SVenu.Iyer@Sun.COM 		ring = kmem_cache_alloc(mac_ring_cache, KM_SLEEP);
35138275SEric Cheng 	}
35148275SEric Cheng 	ASSERT((ring != NULL) && (ring->mr_state == MR_FREE));
35158275SEric Cheng 	return (ring);
35168275SEric Cheng }
35178275SEric Cheng 
35188275SEric Cheng static void
mac_ring_free(mac_impl_t * mip,mac_ring_t * ring)35198275SEric Cheng mac_ring_free(mac_impl_t *mip, mac_ring_t *ring)
35208275SEric Cheng {
352111878SVenu.Iyer@Sun.COM 	ASSERT(ring->mr_state == MR_FREE);
352211878SVenu.Iyer@Sun.COM 
352311878SVenu.Iyer@Sun.COM 	mutex_enter(&mip->mi_ring_lock);
352411878SVenu.Iyer@Sun.COM 	ring->mr_state = MR_FREE;
352511878SVenu.Iyer@Sun.COM 	ring->mr_flag = 0;
352611878SVenu.Iyer@Sun.COM 	ring->mr_next = mip->mi_ring_freelist;
352711878SVenu.Iyer@Sun.COM 	ring->mr_mip = NULL;
352811878SVenu.Iyer@Sun.COM 	mip->mi_ring_freelist = ring;
352911878SVenu.Iyer@Sun.COM 	mac_ring_stat_delete(ring);
353011878SVenu.Iyer@Sun.COM 	mutex_exit(&mip->mi_ring_lock);
35318275SEric Cheng }
35328275SEric Cheng 
35338275SEric Cheng static void
mac_ring_freeall(mac_impl_t * mip)35348275SEric Cheng mac_ring_freeall(mac_impl_t *mip)
35358275SEric Cheng {
35368275SEric Cheng 	mac_ring_t *ring_next;
35378275SEric Cheng 	mutex_enter(&mip->mi_ring_lock);
35388275SEric Cheng 	mac_ring_t *ring = mip->mi_ring_freelist;
35398275SEric Cheng 	while (ring != NULL) {
35408275SEric Cheng 		ring_next = ring->mr_next;
35418275SEric Cheng 		kmem_cache_free(mac_ring_cache, ring);
35428275SEric Cheng 		ring = ring_next;
35438275SEric Cheng 	}
35448275SEric Cheng 	mip->mi_ring_freelist = NULL;
35458275SEric Cheng 	mutex_exit(&mip->mi_ring_lock);
35468275SEric Cheng }
35478275SEric Cheng 
35488275SEric Cheng int
mac_start_ring(mac_ring_t * ring)35498275SEric Cheng mac_start_ring(mac_ring_t *ring)
35508275SEric Cheng {
35518275SEric Cheng 	int rv = 0;
35528275SEric Cheng 
355311878SVenu.Iyer@Sun.COM 	ASSERT(ring->mr_state == MR_FREE);
355411878SVenu.Iyer@Sun.COM 
355511878SVenu.Iyer@Sun.COM 	if (ring->mr_start != NULL) {
35568275SEric Cheng 		rv = ring->mr_start(ring->mr_driver, ring->mr_gen_num);
355711878SVenu.Iyer@Sun.COM 		if (rv != 0)
355811878SVenu.Iyer@Sun.COM 			return (rv);
355911878SVenu.Iyer@Sun.COM 	}
356011878SVenu.Iyer@Sun.COM 
356111878SVenu.Iyer@Sun.COM 	ring->mr_state = MR_INUSE;
35628275SEric Cheng 	return (rv);
35638275SEric Cheng }
35648275SEric Cheng 
35658275SEric Cheng void
mac_stop_ring(mac_ring_t * ring)35668275SEric Cheng mac_stop_ring(mac_ring_t *ring)
35678275SEric Cheng {
356811878SVenu.Iyer@Sun.COM 	ASSERT(ring->mr_state == MR_INUSE);
356911878SVenu.Iyer@Sun.COM 
35708275SEric Cheng 	if (ring->mr_stop != NULL)
35718275SEric Cheng 		ring->mr_stop(ring->mr_driver);
35728275SEric Cheng 
357311878SVenu.Iyer@Sun.COM 	ring->mr_state = MR_FREE;
357411878SVenu.Iyer@Sun.COM 
35758275SEric Cheng 	/*
35768275SEric Cheng 	 * Increment the ring generation number for this ring.
35778275SEric Cheng 	 */
35788275SEric Cheng 	ring->mr_gen_num++;
35798275SEric Cheng }
35808275SEric Cheng 
35818275SEric Cheng int
mac_start_group(mac_group_t * group)35828275SEric Cheng mac_start_group(mac_group_t *group)
35838275SEric Cheng {
35848275SEric Cheng 	int rv = 0;
35858275SEric Cheng 
35868275SEric Cheng 	if (group->mrg_start != NULL)
35878275SEric Cheng 		rv = group->mrg_start(group->mrg_driver);
35888275SEric Cheng 
35898275SEric Cheng 	return (rv);
35908275SEric Cheng }
35918275SEric Cheng 
35928275SEric Cheng void
mac_stop_group(mac_group_t * group)35938275SEric Cheng mac_stop_group(mac_group_t *group)
35948275SEric Cheng {
35958275SEric Cheng 	if (group->mrg_stop != NULL)
35968275SEric Cheng 		group->mrg_stop(group->mrg_driver);
35978275SEric Cheng }
35988275SEric Cheng 
35998275SEric Cheng /*
36008275SEric Cheng  * Called from mac_start() on the default Rx group. Broadcast and multicast
36018275SEric Cheng  * packets are received only on the default group. Hence the default group
36028275SEric Cheng  * needs to be up even if the primary client is not up, for the other groups
36038275SEric Cheng  * to be functional. We do this by calling this function at mac_start time
36048275SEric Cheng  * itself. However the broadcast packets that are received can't make their
36058275SEric Cheng  * way beyond mac_rx until a mac client creates a broadcast flow.
36068275SEric Cheng  */
36078275SEric Cheng static int
mac_start_group_and_rings(mac_group_t * group)36088275SEric Cheng mac_start_group_and_rings(mac_group_t *group)
36098275SEric Cheng {
36108275SEric Cheng 	mac_ring_t	*ring;
36118275SEric Cheng 	int		rv = 0;
36128275SEric Cheng 
36138275SEric Cheng 	ASSERT(group->mrg_state == MAC_GROUP_STATE_REGISTERED);
36148275SEric Cheng 	if ((rv = mac_start_group(group)) != 0)
36158275SEric Cheng 		return (rv);
36168275SEric Cheng 
36178275SEric Cheng 	for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next) {
36188275SEric Cheng 		ASSERT(ring->mr_state == MR_FREE);
36198275SEric Cheng 		if ((rv = mac_start_ring(ring)) != 0)
36208275SEric Cheng 			goto error;
36218275SEric Cheng 		ring->mr_classify_type = MAC_SW_CLASSIFIER;
36228275SEric Cheng 	}
36238275SEric Cheng 	return (0);
36248275SEric Cheng 
36258275SEric Cheng error:
36268275SEric Cheng 	mac_stop_group_and_rings(group);
36278275SEric Cheng 	return (rv);
36288275SEric Cheng }
36298275SEric Cheng 
36308275SEric Cheng /* Called from mac_stop on the default Rx group */
36318275SEric Cheng static void
mac_stop_group_and_rings(mac_group_t * group)36328275SEric Cheng mac_stop_group_and_rings(mac_group_t *group)
36338275SEric Cheng {
36348275SEric Cheng 	mac_ring_t	*ring;
36358275SEric Cheng 
36368275SEric Cheng 	for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next) {
36378275SEric Cheng 		if (ring->mr_state != MR_FREE) {
36388275SEric Cheng 			mac_stop_ring(ring);
36398275SEric Cheng 			ring->mr_flag = 0;
36408275SEric Cheng 			ring->mr_classify_type = MAC_NO_CLASSIFIER;
36418275SEric Cheng 		}
36428275SEric Cheng 	}
36438275SEric Cheng 	mac_stop_group(group);
36448275SEric Cheng }
36458275SEric Cheng 
36468275SEric Cheng 
36478275SEric Cheng static mac_ring_t *
mac_init_ring(mac_impl_t * mip,mac_group_t * group,int index,mac_capab_rings_t * cap_rings)36488275SEric Cheng mac_init_ring(mac_impl_t *mip, mac_group_t *group, int index,
36498275SEric Cheng     mac_capab_rings_t *cap_rings)
36508275SEric Cheng {
365111878SVenu.Iyer@Sun.COM 	mac_ring_t *ring, *rnext;
36528275SEric Cheng 	mac_ring_info_t ring_info;
365311878SVenu.Iyer@Sun.COM 	ddi_intr_handle_t ddi_handle;
365411878SVenu.Iyer@Sun.COM 
365511878SVenu.Iyer@Sun.COM 	ring = mac_ring_alloc(mip);
36568275SEric Cheng 
36578275SEric Cheng 	/* Prepare basic information of ring */
365811878SVenu.Iyer@Sun.COM 
365911878SVenu.Iyer@Sun.COM 	/*
366011878SVenu.Iyer@Sun.COM 	 * Ring index is numbered to be unique across a particular device.
366111878SVenu.Iyer@Sun.COM 	 * Ring index computation makes following assumptions:
366211878SVenu.Iyer@Sun.COM 	 *	- For drivers with static grouping (e.g. ixgbe, bge),
366311878SVenu.Iyer@Sun.COM 	 *	ring index exchanged with the driver (e.g. during mr_rget)
366411878SVenu.Iyer@Sun.COM 	 *	is unique only across the group the ring belongs to.
366511878SVenu.Iyer@Sun.COM 	 *	- Drivers with dynamic grouping (e.g. nxge), start
366611878SVenu.Iyer@Sun.COM 	 *	with single group (mrg_index = 0).
366711878SVenu.Iyer@Sun.COM 	 */
366811878SVenu.Iyer@Sun.COM 	ring->mr_index = group->mrg_index * group->mrg_info.mgi_count + index;
36698275SEric Cheng 	ring->mr_type = group->mrg_type;
36708275SEric Cheng 	ring->mr_gh = (mac_group_handle_t)group;
36718275SEric Cheng 
36728275SEric Cheng 	/* Insert the new ring to the list. */
36738275SEric Cheng 	ring->mr_next = group->mrg_rings;
36748275SEric Cheng 	group->mrg_rings = ring;
36758275SEric Cheng 
36768275SEric Cheng 	/* Zero to reuse the info data structure */
36778275SEric Cheng 	bzero(&ring_info, sizeof (ring_info));
36788275SEric Cheng 
36798275SEric Cheng 	/* Query ring information from driver */
36808275SEric Cheng 	cap_rings->mr_rget(mip->mi_driver, group->mrg_type, group->mrg_index,
36818275SEric Cheng 	    index, &ring_info, (mac_ring_handle_t)ring);
36828275SEric Cheng 
36838275SEric Cheng 	ring->mr_info = ring_info;
36848275SEric Cheng 
368511878SVenu.Iyer@Sun.COM 	/*
368611878SVenu.Iyer@Sun.COM 	 * The interrupt handle could be shared among multiple rings.
368711878SVenu.Iyer@Sun.COM 	 * Thus if there is a bunch of rings that are sharing an
368811878SVenu.Iyer@Sun.COM 	 * interrupt, then only one ring among the bunch will be made
368911878SVenu.Iyer@Sun.COM 	 * available for interrupt re-targeting; the rest will have
369011878SVenu.Iyer@Sun.COM 	 * ddi_shared flag set to TRUE and would not be available for
369111878SVenu.Iyer@Sun.COM 	 * be interrupt re-targeting.
369211878SVenu.Iyer@Sun.COM 	 */
369311878SVenu.Iyer@Sun.COM 	if ((ddi_handle = ring_info.mri_intr.mi_ddi_handle) != NULL) {
369411878SVenu.Iyer@Sun.COM 		rnext = ring->mr_next;
369511878SVenu.Iyer@Sun.COM 		while (rnext != NULL) {
369611878SVenu.Iyer@Sun.COM 			if (rnext->mr_info.mri_intr.mi_ddi_handle ==
369711878SVenu.Iyer@Sun.COM 			    ddi_handle) {
369811878SVenu.Iyer@Sun.COM 				/*
369911878SVenu.Iyer@Sun.COM 				 * If default ring (mr_index == 0) is part
370011878SVenu.Iyer@Sun.COM 				 * of a group of rings sharing an
370111878SVenu.Iyer@Sun.COM 				 * interrupt, then set ddi_shared flag for
370211878SVenu.Iyer@Sun.COM 				 * the default ring and give another ring
370311878SVenu.Iyer@Sun.COM 				 * the chance to be re-targeted.
370411878SVenu.Iyer@Sun.COM 				 */
370511878SVenu.Iyer@Sun.COM 				if (rnext->mr_index == 0 &&
370611878SVenu.Iyer@Sun.COM 				    !rnext->mr_info.mri_intr.mi_ddi_shared) {
370711878SVenu.Iyer@Sun.COM 					rnext->mr_info.mri_intr.mi_ddi_shared =
370811878SVenu.Iyer@Sun.COM 					    B_TRUE;
370911878SVenu.Iyer@Sun.COM 				} else {
371011878SVenu.Iyer@Sun.COM 					ring->mr_info.mri_intr.mi_ddi_shared =
371111878SVenu.Iyer@Sun.COM 					    B_TRUE;
371211878SVenu.Iyer@Sun.COM 				}
371311878SVenu.Iyer@Sun.COM 				break;
371411878SVenu.Iyer@Sun.COM 			}
371511878SVenu.Iyer@Sun.COM 			rnext = rnext->mr_next;
371611878SVenu.Iyer@Sun.COM 		}
371711878SVenu.Iyer@Sun.COM 		/*
371811878SVenu.Iyer@Sun.COM 		 * If rnext is NULL, then no matching ddi_handle was found.
371911878SVenu.Iyer@Sun.COM 		 * Rx rings get registered first. So if this is a Tx ring,
372011878SVenu.Iyer@Sun.COM 		 * then go through all the Rx rings and see if there is a
372111878SVenu.Iyer@Sun.COM 		 * matching ddi handle.
372211878SVenu.Iyer@Sun.COM 		 */
372311878SVenu.Iyer@Sun.COM 		if (rnext == NULL && ring->mr_type == MAC_RING_TYPE_TX) {
372411878SVenu.Iyer@Sun.COM 			mac_compare_ddi_handle(mip->mi_rx_groups,
372511878SVenu.Iyer@Sun.COM 			    mip->mi_rx_group_count, ring);
372611878SVenu.Iyer@Sun.COM 		}
372711878SVenu.Iyer@Sun.COM 	}
372811878SVenu.Iyer@Sun.COM 
37298275SEric Cheng 	/* Update ring's status */
37308275SEric Cheng 	ring->mr_state = MR_FREE;
37318275SEric Cheng 	ring->mr_flag = 0;
37328275SEric Cheng 
37338275SEric Cheng 	/* Update the ring count of the group */
37348275SEric Cheng 	group->mrg_cur_count++;
373511878SVenu.Iyer@Sun.COM 
373611878SVenu.Iyer@Sun.COM 	/* Create per ring kstats */
373711878SVenu.Iyer@Sun.COM 	if (ring->mr_stat != NULL) {
373811878SVenu.Iyer@Sun.COM 		ring->mr_mip = mip;
373911878SVenu.Iyer@Sun.COM 		mac_ring_stat_create(ring);
374011878SVenu.Iyer@Sun.COM 	}
374111878SVenu.Iyer@Sun.COM 
37428275SEric Cheng 	return (ring);
37438275SEric Cheng }
37448275SEric Cheng 
37458275SEric Cheng /*
37468275SEric Cheng  * Rings are chained together for easy regrouping.
37478275SEric Cheng  */
37488275SEric Cheng static void
mac_init_group(mac_impl_t * mip,mac_group_t * group,int size,mac_capab_rings_t * cap_rings)37498275SEric Cheng mac_init_group(mac_impl_t *mip, mac_group_t *group, int size,
37508275SEric Cheng     mac_capab_rings_t *cap_rings)
37518275SEric Cheng {
37528275SEric Cheng 	int index;
37538275SEric Cheng 
37548275SEric Cheng 	/*
37558275SEric Cheng 	 * Initialize all ring members of this group. Size of zero will not
37568275SEric Cheng 	 * enter the loop, so it's safe for initializing an empty group.
37578275SEric Cheng 	 */
37588275SEric Cheng 	for (index = size - 1; index >= 0; index--)
37598275SEric Cheng 		(void) mac_init_ring(mip, group, index, cap_rings);
37608275SEric Cheng }
37618275SEric Cheng 
37628275SEric Cheng int
mac_init_rings(mac_impl_t * mip,mac_ring_type_t rtype)37638275SEric Cheng mac_init_rings(mac_impl_t *mip, mac_ring_type_t rtype)
37648275SEric Cheng {
376511878SVenu.Iyer@Sun.COM 	mac_capab_rings_t	*cap_rings;
376611878SVenu.Iyer@Sun.COM 	mac_group_t		*group;
376711878SVenu.Iyer@Sun.COM 	mac_group_t		*groups;
376811878SVenu.Iyer@Sun.COM 	mac_group_info_t	group_info;
376911878SVenu.Iyer@Sun.COM 	uint_t			group_free = 0;
377011878SVenu.Iyer@Sun.COM 	uint_t			ring_left;
377111878SVenu.Iyer@Sun.COM 	mac_ring_t		*ring;
377211878SVenu.Iyer@Sun.COM 	int			g;
377311878SVenu.Iyer@Sun.COM 	int			err = 0;
377411878SVenu.Iyer@Sun.COM 	uint_t			grpcnt;
377511878SVenu.Iyer@Sun.COM 	boolean_t		pseudo_txgrp = B_FALSE;
37768275SEric Cheng 
37778275SEric Cheng 	switch (rtype) {
37788275SEric Cheng 	case MAC_RING_TYPE_RX:
37798275SEric Cheng 		ASSERT(mip->mi_rx_groups == NULL);
37808275SEric Cheng 
37818275SEric Cheng 		cap_rings = &mip->mi_rx_rings_cap;
37828275SEric Cheng 		cap_rings->mr_type = MAC_RING_TYPE_RX;
37838275SEric Cheng 		break;
37848275SEric Cheng 	case MAC_RING_TYPE_TX:
37858275SEric Cheng 		ASSERT(mip->mi_tx_groups == NULL);
37868275SEric Cheng 
37878275SEric Cheng 		cap_rings = &mip->mi_tx_rings_cap;
37888275SEric Cheng 		cap_rings->mr_type = MAC_RING_TYPE_TX;
37898275SEric Cheng 		break;
37908275SEric Cheng 	default:
37918275SEric Cheng 		ASSERT(B_FALSE);
37928275SEric Cheng 	}
37938275SEric Cheng 
379411878SVenu.Iyer@Sun.COM 	if (!i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_RINGS, cap_rings))
37958275SEric Cheng 		return (0);
379611878SVenu.Iyer@Sun.COM 	grpcnt = cap_rings->mr_gnum;
379711878SVenu.Iyer@Sun.COM 
379811878SVenu.Iyer@Sun.COM 	/*
379911878SVenu.Iyer@Sun.COM 	 * If we have multiple TX rings, but only one TX group, we can
380011878SVenu.Iyer@Sun.COM 	 * create pseudo TX groups (one per TX ring) in the MAC layer,
380111878SVenu.Iyer@Sun.COM 	 * except for an aggr. For an aggr currently we maintain only
380211878SVenu.Iyer@Sun.COM 	 * one group with all the rings (for all its ports), going
380311878SVenu.Iyer@Sun.COM 	 * forwards we might change this.
380411878SVenu.Iyer@Sun.COM 	 */
380511878SVenu.Iyer@Sun.COM 	if (rtype == MAC_RING_TYPE_TX &&
380611878SVenu.Iyer@Sun.COM 	    cap_rings->mr_gnum == 0 && cap_rings->mr_rnum >  0 &&
380711878SVenu.Iyer@Sun.COM 	    (mip->mi_state_flags & MIS_IS_AGGR) == 0) {
380811878SVenu.Iyer@Sun.COM 		/*
380911878SVenu.Iyer@Sun.COM 		 * The -1 here is because we create a default TX group
381011878SVenu.Iyer@Sun.COM 		 * with all the rings in it.
381111878SVenu.Iyer@Sun.COM 		 */
381211878SVenu.Iyer@Sun.COM 		grpcnt = cap_rings->mr_rnum - 1;
381311878SVenu.Iyer@Sun.COM 		pseudo_txgrp = B_TRUE;
381411878SVenu.Iyer@Sun.COM 	}
38158275SEric Cheng 
38168275SEric Cheng 	/*
38178275SEric Cheng 	 * Allocate a contiguous buffer for all groups.
38188275SEric Cheng 	 */
381911878SVenu.Iyer@Sun.COM 	groups = kmem_zalloc(sizeof (mac_group_t) * (grpcnt+ 1), KM_SLEEP);
38208275SEric Cheng 
38218275SEric Cheng 	ring_left = cap_rings->mr_rnum;
38228275SEric Cheng 
38238275SEric Cheng 	/*
38248275SEric Cheng 	 * Get all ring groups if any, and get their ring members
38258275SEric Cheng 	 * if any.
38268275SEric Cheng 	 */
382711878SVenu.Iyer@Sun.COM 	for (g = 0; g < grpcnt; g++) {
38288275SEric Cheng 		group = groups + g;
38298275SEric Cheng 
38308275SEric Cheng 		/* Prepare basic information of the group */
38318275SEric Cheng 		group->mrg_index = g;
38328275SEric Cheng 		group->mrg_type = rtype;
38338275SEric Cheng 		group->mrg_state = MAC_GROUP_STATE_UNINIT;
38348275SEric Cheng 		group->mrg_mh = (mac_handle_t)mip;
38358275SEric Cheng 		group->mrg_next = group + 1;
38368275SEric Cheng 
38378275SEric Cheng 		/* Zero to reuse the info data structure */
38388275SEric Cheng 		bzero(&group_info, sizeof (group_info));
38398275SEric Cheng 
384011878SVenu.Iyer@Sun.COM 		if (pseudo_txgrp) {
384111878SVenu.Iyer@Sun.COM 			/*
384211878SVenu.Iyer@Sun.COM 			 * This is a pseudo group that we created, apart
384311878SVenu.Iyer@Sun.COM 			 * from setting the state there is nothing to be
384411878SVenu.Iyer@Sun.COM 			 * done.
384511878SVenu.Iyer@Sun.COM 			 */
384611878SVenu.Iyer@Sun.COM 			group->mrg_state = MAC_GROUP_STATE_REGISTERED;
384711878SVenu.Iyer@Sun.COM 			group_free++;
384811878SVenu.Iyer@Sun.COM 			continue;
384911878SVenu.Iyer@Sun.COM 		}
38508275SEric Cheng 		/* Query group information from driver */
38518275SEric Cheng 		cap_rings->mr_gget(mip->mi_driver, rtype, g, &group_info,
38528275SEric Cheng 		    (mac_group_handle_t)group);
38538275SEric Cheng 
38548275SEric Cheng 		switch (cap_rings->mr_group_type) {
38558275SEric Cheng 		case MAC_GROUP_TYPE_DYNAMIC:
38568275SEric Cheng 			if (cap_rings->mr_gaddring == NULL ||
38578275SEric Cheng 			    cap_rings->mr_gremring == NULL) {
38588275SEric Cheng 				DTRACE_PROBE3(
38598275SEric Cheng 				    mac__init__rings_no_addremring,
38608275SEric Cheng 				    char *, mip->mi_name,
38618275SEric Cheng 				    mac_group_add_ring_t,
38628275SEric Cheng 				    cap_rings->mr_gaddring,
38638275SEric Cheng 				    mac_group_add_ring_t,
38648275SEric Cheng 				    cap_rings->mr_gremring);
38658275SEric Cheng 				err = EINVAL;
38668275SEric Cheng 				goto bail;
38678275SEric Cheng 			}
38688275SEric Cheng 
38698275SEric Cheng 			switch (rtype) {
38708275SEric Cheng 			case MAC_RING_TYPE_RX:
38718275SEric Cheng 				/*
38728275SEric Cheng 				 * The first RX group must have non-zero
38738275SEric Cheng 				 * rings, and the following groups must
38748275SEric Cheng 				 * have zero rings.
38758275SEric Cheng 				 */
38768275SEric Cheng 				if (g == 0 && group_info.mgi_count == 0) {
38778275SEric Cheng 					DTRACE_PROBE1(
38788275SEric Cheng 					    mac__init__rings__rx__def__zero,
38798275SEric Cheng 					    char *, mip->mi_name);
38808275SEric Cheng 					err = EINVAL;
38818275SEric Cheng 					goto bail;
38828275SEric Cheng 				}
38838275SEric Cheng 				if (g > 0 && group_info.mgi_count != 0) {
38848275SEric Cheng 					DTRACE_PROBE3(
38858275SEric Cheng 					    mac__init__rings__rx__nonzero,
38868275SEric Cheng 					    char *, mip->mi_name,
38878275SEric Cheng 					    int, g, int, group_info.mgi_count);
38888275SEric Cheng 					err = EINVAL;
38898275SEric Cheng 					goto bail;
38908275SEric Cheng 				}
38918275SEric Cheng 				break;
38928275SEric Cheng 			case MAC_RING_TYPE_TX:
38938275SEric Cheng 				/*
38948275SEric Cheng 				 * All TX ring groups must have zero rings.
38958275SEric Cheng 				 */
38968275SEric Cheng 				if (group_info.mgi_count != 0) {
38978275SEric Cheng 					DTRACE_PROBE3(
38988275SEric Cheng 					    mac__init__rings__tx__nonzero,
38998275SEric Cheng 					    char *, mip->mi_name,
39008275SEric Cheng 					    int, g, int, group_info.mgi_count);
39018275SEric Cheng 					err = EINVAL;
39028275SEric Cheng 					goto bail;
39038275SEric Cheng 				}
39048275SEric Cheng 				break;
39058275SEric Cheng 			}
39068275SEric Cheng 			break;
39078275SEric Cheng 		case MAC_GROUP_TYPE_STATIC:
39088275SEric Cheng 			/*
39098275SEric Cheng 			 * Note that an empty group is allowed, e.g., an aggr
39108275SEric Cheng 			 * would start with an empty group.
39118275SEric Cheng 			 */
39128275SEric Cheng 			break;
39138275SEric Cheng 		default:
39148275SEric Cheng 			/* unknown group type */
39158275SEric Cheng 			DTRACE_PROBE2(mac__init__rings__unknown__type,
39168275SEric Cheng 			    char *, mip->mi_name,
39178275SEric Cheng 			    int, cap_rings->mr_group_type);
39188275SEric Cheng 			err = EINVAL;
39198275SEric Cheng 			goto bail;
39208275SEric Cheng 		}
39218275SEric Cheng 
39228275SEric Cheng 
39238275SEric Cheng 		/*
39248275SEric Cheng 		 * Driver must register group->mgi_addmac/remmac() for rx groups
39258275SEric Cheng 		 * to support multiple MAC addresses.
39268275SEric Cheng 		 */
39278275SEric Cheng 		if (rtype == MAC_RING_TYPE_RX) {
39288275SEric Cheng 			if ((group_info.mgi_addmac == NULL) ||
392911878SVenu.Iyer@Sun.COM 			    (group_info.mgi_addmac == NULL)) {
39308275SEric Cheng 				goto bail;
393111878SVenu.Iyer@Sun.COM 			}
39328275SEric Cheng 		}
39338275SEric Cheng 
39348275SEric Cheng 		/* Cache driver-supplied information */
39358275SEric Cheng 		group->mrg_info = group_info;
39368275SEric Cheng 
39378275SEric Cheng 		/* Update the group's status and group count. */
393811878SVenu.Iyer@Sun.COM 		mac_set_group_state(group, MAC_GROUP_STATE_REGISTERED);
39398275SEric Cheng 		group_free++;
39408275SEric Cheng 
39418275SEric Cheng 		group->mrg_rings = NULL;
39428275SEric Cheng 		group->mrg_cur_count = 0;
39438275SEric Cheng 		mac_init_group(mip, group, group_info.mgi_count, cap_rings);
39448275SEric Cheng 		ring_left -= group_info.mgi_count;
39458275SEric Cheng 
39468275SEric Cheng 		/* The current group size should be equal to default value */
39478275SEric Cheng 		ASSERT(group->mrg_cur_count == group_info.mgi_count);
39488275SEric Cheng 	}
39498275SEric Cheng 
39508275SEric Cheng 	/* Build up a dummy group for free resources as a pool */
395111878SVenu.Iyer@Sun.COM 	group = groups + grpcnt;
39528275SEric Cheng 
39538275SEric Cheng 	/* Prepare basic information of the group */
39548275SEric Cheng 	group->mrg_index = -1;
39558275SEric Cheng 	group->mrg_type = rtype;
39568275SEric Cheng 	group->mrg_state = MAC_GROUP_STATE_UNINIT;
39578275SEric Cheng 	group->mrg_mh = (mac_handle_t)mip;
39588275SEric Cheng 	group->mrg_next = NULL;
39598275SEric Cheng 
39608275SEric Cheng 	/*
39618275SEric Cheng 	 * If there are ungrouped rings, allocate a continuous buffer for
39628275SEric Cheng 	 * remaining resources.
39638275SEric Cheng 	 */
39648275SEric Cheng 	if (ring_left != 0) {
39658275SEric Cheng 		group->mrg_rings = NULL;
39668275SEric Cheng 		group->mrg_cur_count = 0;
39678275SEric Cheng 		mac_init_group(mip, group, ring_left, cap_rings);
39688275SEric Cheng 
39698275SEric Cheng 		/* The current group size should be equal to ring_left */
39708275SEric Cheng 		ASSERT(group->mrg_cur_count == ring_left);
39718275SEric Cheng 
39728275SEric Cheng 		ring_left = 0;
39738275SEric Cheng 
39748275SEric Cheng 		/* Update this group's status */
397511878SVenu.Iyer@Sun.COM 		mac_set_group_state(group, MAC_GROUP_STATE_REGISTERED);
39768275SEric Cheng 	} else
39778275SEric Cheng 		group->mrg_rings = NULL;
39788275SEric Cheng 
39798275SEric Cheng 	ASSERT(ring_left == 0);
39808275SEric Cheng 
39818275SEric Cheng bail:
398211878SVenu.Iyer@Sun.COM 
39838275SEric Cheng 	/* Cache other important information to finalize the initialization */
39848275SEric Cheng 	switch (rtype) {
39858275SEric Cheng 	case MAC_RING_TYPE_RX:
39868275SEric Cheng 		mip->mi_rx_group_type = cap_rings->mr_group_type;
39878275SEric Cheng 		mip->mi_rx_group_count = cap_rings->mr_gnum;
39888275SEric Cheng 		mip->mi_rx_groups = groups;
398911878SVenu.Iyer@Sun.COM 		mip->mi_rx_donor_grp = groups;
399011878SVenu.Iyer@Sun.COM 		if (mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
399111878SVenu.Iyer@Sun.COM 			/*
399211878SVenu.Iyer@Sun.COM 			 * The default ring is reserved since it is
399311878SVenu.Iyer@Sun.COM 			 * used for sending the broadcast etc. packets.
399411878SVenu.Iyer@Sun.COM 			 */
399511878SVenu.Iyer@Sun.COM 			mip->mi_rxrings_avail =
399611878SVenu.Iyer@Sun.COM 			    mip->mi_rx_groups->mrg_cur_count - 1;
399711878SVenu.Iyer@Sun.COM 			mip->mi_rxrings_rsvd = 1;
399811878SVenu.Iyer@Sun.COM 		}
399911878SVenu.Iyer@Sun.COM 		/*
400011878SVenu.Iyer@Sun.COM 		 * The default group cannot be reserved. It is used by
400111878SVenu.Iyer@Sun.COM 		 * all the clients that do not have an exclusive group.
400211878SVenu.Iyer@Sun.COM 		 */
400311878SVenu.Iyer@Sun.COM 		mip->mi_rxhwclnt_avail = mip->mi_rx_group_count - 1;
400411878SVenu.Iyer@Sun.COM 		mip->mi_rxhwclnt_used = 1;
40058275SEric Cheng 		break;
40068275SEric Cheng 	case MAC_RING_TYPE_TX:
400711878SVenu.Iyer@Sun.COM 		mip->mi_tx_group_type = pseudo_txgrp ? MAC_GROUP_TYPE_DYNAMIC :
400811878SVenu.Iyer@Sun.COM 		    cap_rings->mr_group_type;
400911878SVenu.Iyer@Sun.COM 		mip->mi_tx_group_count = grpcnt;
40108275SEric Cheng 		mip->mi_tx_group_free = group_free;
40118275SEric Cheng 		mip->mi_tx_groups = groups;
40128275SEric Cheng 
401311878SVenu.Iyer@Sun.COM 		group = groups + grpcnt;
401411878SVenu.Iyer@Sun.COM 		ring = group->mrg_rings;
40158275SEric Cheng 		/*
401611878SVenu.Iyer@Sun.COM 		 * The ring can be NULL in the case of aggr. Aggr will
401711878SVenu.Iyer@Sun.COM 		 * have an empty Tx group which will get populated
401811878SVenu.Iyer@Sun.COM 		 * later when pseudo Tx rings are added after
401911878SVenu.Iyer@Sun.COM 		 * mac_register() is done.
40208275SEric Cheng 		 */
402111878SVenu.Iyer@Sun.COM 		if (ring == NULL) {
402211878SVenu.Iyer@Sun.COM 			ASSERT(mip->mi_state_flags & MIS_IS_AGGR);
402311878SVenu.Iyer@Sun.COM 			/*
402411878SVenu.Iyer@Sun.COM 			 * pass the group to aggr so it can add Tx
402511878SVenu.Iyer@Sun.COM 			 * rings to the group later.
402611878SVenu.Iyer@Sun.COM 			 */
402711878SVenu.Iyer@Sun.COM 			cap_rings->mr_gget(mip->mi_driver, rtype, 0, NULL,
402811878SVenu.Iyer@Sun.COM 			    (mac_group_handle_t)group);
402911878SVenu.Iyer@Sun.COM 			/*
403011878SVenu.Iyer@Sun.COM 			 * Even though there are no rings at this time
403111878SVenu.Iyer@Sun.COM 			 * (rings will come later), set the group
403211878SVenu.Iyer@Sun.COM 			 * state to registered.
403311878SVenu.Iyer@Sun.COM 			 */
403411878SVenu.Iyer@Sun.COM 			group->mrg_state = MAC_GROUP_STATE_REGISTERED;
403511878SVenu.Iyer@Sun.COM 		} else {
403611878SVenu.Iyer@Sun.COM 			/*
403711878SVenu.Iyer@Sun.COM 			 * Ring 0 is used as the default one and it could be
403811878SVenu.Iyer@Sun.COM 			 * assigned to a client as well.
403911878SVenu.Iyer@Sun.COM 			 */
404011878SVenu.Iyer@Sun.COM 			while ((ring->mr_index != 0) && (ring->mr_next != NULL))
404111878SVenu.Iyer@Sun.COM 				ring = ring->mr_next;
404211878SVenu.Iyer@Sun.COM 			ASSERT(ring->mr_index == 0);
404311878SVenu.Iyer@Sun.COM 			mip->mi_default_tx_ring = (mac_ring_handle_t)ring;
404411878SVenu.Iyer@Sun.COM 		}
404511878SVenu.Iyer@Sun.COM 		if (mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC)
404611878SVenu.Iyer@Sun.COM 			mip->mi_txrings_avail = group->mrg_cur_count - 1;
404711878SVenu.Iyer@Sun.COM 			/*
404811878SVenu.Iyer@Sun.COM 			 * The default ring cannot be reserved.
404911878SVenu.Iyer@Sun.COM 			 */
405011878SVenu.Iyer@Sun.COM 			mip->mi_txrings_rsvd = 1;
405111878SVenu.Iyer@Sun.COM 		/*
405211878SVenu.Iyer@Sun.COM 		 * The default group cannot be reserved. It will be shared
405311878SVenu.Iyer@Sun.COM 		 * by clients that do not have an exclusive group.
405411878SVenu.Iyer@Sun.COM 		 */
405511878SVenu.Iyer@Sun.COM 		mip->mi_txhwclnt_avail = mip->mi_tx_group_count;
405611878SVenu.Iyer@Sun.COM 		mip->mi_txhwclnt_used = 1;
40578275SEric Cheng 		break;
40588275SEric Cheng 	default:
40598275SEric Cheng 		ASSERT(B_FALSE);
40608275SEric Cheng 	}
40618275SEric Cheng 
40628275SEric Cheng 	if (err != 0)
40638275SEric Cheng 		mac_free_rings(mip, rtype);
40648275SEric Cheng 
40658275SEric Cheng 	return (err);
40668275SEric Cheng }
40678275SEric Cheng 
40688275SEric Cheng /*
406911878SVenu.Iyer@Sun.COM  * The ddi interrupt handle could be shared amoung rings. If so, compare
407011878SVenu.Iyer@Sun.COM  * the new ring's ddi handle with the existing ones and set ddi_shared
407111878SVenu.Iyer@Sun.COM  * flag.
407211878SVenu.Iyer@Sun.COM  */
407311878SVenu.Iyer@Sun.COM void
mac_compare_ddi_handle(mac_group_t * groups,uint_t grpcnt,mac_ring_t * cring)407411878SVenu.Iyer@Sun.COM mac_compare_ddi_handle(mac_group_t *groups, uint_t grpcnt, mac_ring_t *cring)
407511878SVenu.Iyer@Sun.COM {
407611878SVenu.Iyer@Sun.COM 	mac_group_t *group;
407711878SVenu.Iyer@Sun.COM 	mac_ring_t *ring;
407811878SVenu.Iyer@Sun.COM 	ddi_intr_handle_t ddi_handle;
407911878SVenu.Iyer@Sun.COM 	int g;
408011878SVenu.Iyer@Sun.COM 
408111878SVenu.Iyer@Sun.COM 	ddi_handle = cring->mr_info.mri_intr.mi_ddi_handle;
408211878SVenu.Iyer@Sun.COM 	for (g = 0; g < grpcnt; g++) {
408311878SVenu.Iyer@Sun.COM 		group = groups + g;
408411878SVenu.Iyer@Sun.COM 		for (ring = group->mrg_rings; ring != NULL;
408511878SVenu.Iyer@Sun.COM 		    ring = ring->mr_next) {
408611878SVenu.Iyer@Sun.COM 			if (ring == cring)
408711878SVenu.Iyer@Sun.COM 				continue;
408811878SVenu.Iyer@Sun.COM 			if (ring->mr_info.mri_intr.mi_ddi_handle ==
408911878SVenu.Iyer@Sun.COM 			    ddi_handle) {
409011878SVenu.Iyer@Sun.COM 				if (cring->mr_type == MAC_RING_TYPE_RX &&
409111878SVenu.Iyer@Sun.COM 				    ring->mr_index == 0 &&
409211878SVenu.Iyer@Sun.COM 				    !ring->mr_info.mri_intr.mi_ddi_shared) {
409311878SVenu.Iyer@Sun.COM 					ring->mr_info.mri_intr.mi_ddi_shared =
409411878SVenu.Iyer@Sun.COM 					    B_TRUE;
409511878SVenu.Iyer@Sun.COM 				} else {
409611878SVenu.Iyer@Sun.COM 					cring->mr_info.mri_intr.mi_ddi_shared =
409711878SVenu.Iyer@Sun.COM 					    B_TRUE;
409811878SVenu.Iyer@Sun.COM 				}
409911878SVenu.Iyer@Sun.COM 				return;
410011878SVenu.Iyer@Sun.COM 			}
410111878SVenu.Iyer@Sun.COM 		}
410211878SVenu.Iyer@Sun.COM 	}
410311878SVenu.Iyer@Sun.COM }
410411878SVenu.Iyer@Sun.COM 
410511878SVenu.Iyer@Sun.COM /*
410611878SVenu.Iyer@Sun.COM  * Called to free all groups of particular type (RX or TX). It's assumed that
410711878SVenu.Iyer@Sun.COM  * no clients are using these groups.
41088275SEric Cheng  */
41098275SEric Cheng void
mac_free_rings(mac_impl_t * mip,mac_ring_type_t rtype)41108275SEric Cheng mac_free_rings(mac_impl_t *mip, mac_ring_type_t rtype)
41118275SEric Cheng {
41128275SEric Cheng 	mac_group_t *group, *groups;
41138275SEric Cheng 	uint_t group_count;
41148275SEric Cheng 
41158275SEric Cheng 	switch (rtype) {
41168275SEric Cheng 	case MAC_RING_TYPE_RX:
41178275SEric Cheng 		if (mip->mi_rx_groups == NULL)
41188275SEric Cheng 			return;
41198275SEric Cheng 
41208275SEric Cheng 		groups = mip->mi_rx_groups;
41218275SEric Cheng 		group_count = mip->mi_rx_group_count;
41228275SEric Cheng 
41238275SEric Cheng 		mip->mi_rx_groups = NULL;
412411878SVenu.Iyer@Sun.COM 		mip->mi_rx_donor_grp = NULL;
41258275SEric Cheng 		mip->mi_rx_group_count = 0;
41268275SEric Cheng 		break;
41278275SEric Cheng 	case MAC_RING_TYPE_TX:
41288275SEric Cheng 		ASSERT(mip->mi_tx_group_count == mip->mi_tx_group_free);
41298275SEric Cheng 
41308275SEric Cheng 		if (mip->mi_tx_groups == NULL)
41318275SEric Cheng 			return;
41328275SEric Cheng 
41338275SEric Cheng 		groups = mip->mi_tx_groups;
41348275SEric Cheng 		group_count = mip->mi_tx_group_count;
41358275SEric Cheng 
41368275SEric Cheng 		mip->mi_tx_groups = NULL;
41378275SEric Cheng 		mip->mi_tx_group_count = 0;
41388275SEric Cheng 		mip->mi_tx_group_free = 0;
41398275SEric Cheng 		mip->mi_default_tx_ring = NULL;
41408275SEric Cheng 		break;
41418275SEric Cheng 	default:
41428275SEric Cheng 		ASSERT(B_FALSE);
41438275SEric Cheng 	}
41448275SEric Cheng 
41458275SEric Cheng 	for (group = groups; group != NULL; group = group->mrg_next) {
41468275SEric Cheng 		mac_ring_t *ring;
41478275SEric Cheng 
41488275SEric Cheng 		if (group->mrg_cur_count == 0)
41498275SEric Cheng 			continue;
41508275SEric Cheng 
41518275SEric Cheng 		ASSERT(group->mrg_rings != NULL);
41528275SEric Cheng 
41538275SEric Cheng 		while ((ring = group->mrg_rings) != NULL) {
41548275SEric Cheng 			group->mrg_rings = ring->mr_next;
41558275SEric Cheng 			mac_ring_free(mip, ring);
41568275SEric Cheng 		}
41578275SEric Cheng 	}
41588275SEric Cheng 
41598275SEric Cheng 	/* Free all the cached rings */
41608275SEric Cheng 	mac_ring_freeall(mip);
41618275SEric Cheng 	/* Free the block of group data strutures */
41628275SEric Cheng 	kmem_free(groups, sizeof (mac_group_t) * (group_count + 1));
41638275SEric Cheng }
41648275SEric Cheng 
41658275SEric Cheng /*
41668275SEric Cheng  * Associate a MAC address with a receive group.
41678275SEric Cheng  *
41688275SEric Cheng  * The return value of this function should always be checked properly, because
41698275SEric Cheng  * any type of failure could cause unexpected results. A group can be added
41708275SEric Cheng  * or removed with a MAC address only after it has been reserved. Ideally,
41718275SEric Cheng  * a successful reservation always leads to calling mac_group_addmac() to
41728275SEric Cheng  * steer desired traffic. Failure of adding an unicast MAC address doesn't
41738275SEric Cheng  * always imply that the group is functioning abnormally.
41748275SEric Cheng  *
41758275SEric Cheng  * Currently this function is called everywhere, and it reflects assumptions
41768275SEric Cheng  * about MAC addresses in the implementation. CR 6735196.
41778275SEric Cheng  */
41788275SEric Cheng int
mac_group_addmac(mac_group_t * group,const uint8_t * addr)41798275SEric Cheng mac_group_addmac(mac_group_t *group, const uint8_t *addr)
41808275SEric Cheng {
41818275SEric Cheng 	ASSERT(group->mrg_type == MAC_RING_TYPE_RX);
41828275SEric Cheng 	ASSERT(group->mrg_info.mgi_addmac != NULL);
41838275SEric Cheng 
41848275SEric Cheng 	return (group->mrg_info.mgi_addmac(group->mrg_info.mgi_driver, addr));
41858275SEric Cheng }
41868275SEric Cheng 
41878275SEric Cheng /*
41888275SEric Cheng  * Remove the association between MAC address and receive group.
41898275SEric Cheng  */
41908275SEric Cheng int
mac_group_remmac(mac_group_t * group,const uint8_t * addr)41918275SEric Cheng mac_group_remmac(mac_group_t *group, const uint8_t *addr)
41928275SEric Cheng {
41938275SEric Cheng 	ASSERT(group->mrg_type == MAC_RING_TYPE_RX);
41948275SEric Cheng 	ASSERT(group->mrg_info.mgi_remmac != NULL);
41958275SEric Cheng 
41968275SEric Cheng 	return (group->mrg_info.mgi_remmac(group->mrg_info.mgi_driver, addr));
41978275SEric Cheng }
41988275SEric Cheng 
41998275SEric Cheng /*
420010491SRishi.Srivatsavai@Sun.COM  * This is the entry point for packets transmitted through the bridging code.
420110491SRishi.Srivatsavai@Sun.COM  * If no bridge is in place, MAC_RING_TX transmits using tx ring. The 'rh'
420210491SRishi.Srivatsavai@Sun.COM  * pointer may be NULL to select the default ring.
420310491SRishi.Srivatsavai@Sun.COM  */
420410491SRishi.Srivatsavai@Sun.COM mblk_t *
mac_bridge_tx(mac_impl_t * mip,mac_ring_handle_t rh,mblk_t * mp)420510491SRishi.Srivatsavai@Sun.COM mac_bridge_tx(mac_impl_t *mip, mac_ring_handle_t rh, mblk_t *mp)
420610491SRishi.Srivatsavai@Sun.COM {
420710491SRishi.Srivatsavai@Sun.COM 	mac_handle_t mh;
420810491SRishi.Srivatsavai@Sun.COM 
420910491SRishi.Srivatsavai@Sun.COM 	/*
421010491SRishi.Srivatsavai@Sun.COM 	 * Once we take a reference on the bridge link, the bridge
421110491SRishi.Srivatsavai@Sun.COM 	 * module itself can't unload, so the callback pointers are
421210491SRishi.Srivatsavai@Sun.COM 	 * stable.
421310491SRishi.Srivatsavai@Sun.COM 	 */
421410491SRishi.Srivatsavai@Sun.COM 	mutex_enter(&mip->mi_bridge_lock);
421510491SRishi.Srivatsavai@Sun.COM 	if ((mh = mip->mi_bridge_link) != NULL)
421610491SRishi.Srivatsavai@Sun.COM 		mac_bridge_ref_cb(mh, B_TRUE);
421710491SRishi.Srivatsavai@Sun.COM 	mutex_exit(&mip->mi_bridge_lock);
421810491SRishi.Srivatsavai@Sun.COM 	if (mh == NULL) {
421910491SRishi.Srivatsavai@Sun.COM 		MAC_RING_TX(mip, rh, mp, mp);
422010491SRishi.Srivatsavai@Sun.COM 	} else {
422110491SRishi.Srivatsavai@Sun.COM 		mp = mac_bridge_tx_cb(mh, rh, mp);
422210491SRishi.Srivatsavai@Sun.COM 		mac_bridge_ref_cb(mh, B_FALSE);
422310491SRishi.Srivatsavai@Sun.COM 	}
422410491SRishi.Srivatsavai@Sun.COM 
422510491SRishi.Srivatsavai@Sun.COM 	return (mp);
422610491SRishi.Srivatsavai@Sun.COM }
422710491SRishi.Srivatsavai@Sun.COM 
422810491SRishi.Srivatsavai@Sun.COM /*
42298275SEric Cheng  * Find a ring from its index.
42308275SEric Cheng  */
423111878SVenu.Iyer@Sun.COM mac_ring_handle_t
mac_find_ring(mac_group_handle_t gh,int index)423211878SVenu.Iyer@Sun.COM mac_find_ring(mac_group_handle_t gh, int index)
423311878SVenu.Iyer@Sun.COM {
423411878SVenu.Iyer@Sun.COM 	mac_group_t *group = (mac_group_t *)gh;
42358275SEric Cheng 	mac_ring_t *ring = group->mrg_rings;
42368275SEric Cheng 
42378275SEric Cheng 	for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next)
42388275SEric Cheng 		if (ring->mr_index == index)
42398275SEric Cheng 			break;
42408275SEric Cheng 
424111878SVenu.Iyer@Sun.COM 	return ((mac_ring_handle_t)ring);
42428275SEric Cheng }
42438275SEric Cheng /*
42448275SEric Cheng  * Add a ring to an existing group.
42458275SEric Cheng  *
42468275SEric Cheng  * The ring must be either passed directly (for example if the ring
42478275SEric Cheng  * movement is initiated by the framework), or specified through a driver
42488275SEric Cheng  * index (for example when the ring is added by the driver.
42498275SEric Cheng  *
42508275SEric Cheng  * The caller needs to call mac_perim_enter() before calling this function.
42518275SEric Cheng  */
42528275SEric Cheng int
i_mac_group_add_ring(mac_group_t * group,mac_ring_t * ring,int index)42538275SEric Cheng i_mac_group_add_ring(mac_group_t *group, mac_ring_t *ring, int index)
42548275SEric Cheng {
42558275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)group->mrg_mh;
42568275SEric Cheng 	mac_capab_rings_t *cap_rings;
42578275SEric Cheng 	boolean_t driver_call = (ring == NULL);
42588275SEric Cheng 	mac_group_type_t group_type;
42598275SEric Cheng 	int ret = 0;
426011878SVenu.Iyer@Sun.COM 	flow_entry_t *flent;
42618275SEric Cheng 
42628275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
42638275SEric Cheng 
42648275SEric Cheng 	switch (group->mrg_type) {
42658275SEric Cheng 	case MAC_RING_TYPE_RX:
42668275SEric Cheng 		cap_rings = &mip->mi_rx_rings_cap;
42678275SEric Cheng 		group_type = mip->mi_rx_group_type;
42688275SEric Cheng 		break;
42698275SEric Cheng 	case MAC_RING_TYPE_TX:
42708275SEric Cheng 		cap_rings = &mip->mi_tx_rings_cap;
42718275SEric Cheng 		group_type = mip->mi_tx_group_type;
42728275SEric Cheng 		break;
42738275SEric Cheng 	default:
42748275SEric Cheng 		ASSERT(B_FALSE);
42758275SEric Cheng 	}
42768275SEric Cheng 
42778275SEric Cheng 	/*
42788275SEric Cheng 	 * There should be no ring with the same ring index in the target
42798275SEric Cheng 	 * group.
42808275SEric Cheng 	 */
428111878SVenu.Iyer@Sun.COM 	ASSERT(mac_find_ring((mac_group_handle_t)group,
428211878SVenu.Iyer@Sun.COM 	    driver_call ? index : ring->mr_index) == NULL);
42838275SEric Cheng 
42848275SEric Cheng 	if (driver_call) {
42858275SEric Cheng 		/*
42868275SEric Cheng 		 * The function is called as a result of a request from
42878275SEric Cheng 		 * a driver to add a ring to an existing group, for example
42888275SEric Cheng 		 * from the aggregation driver. Allocate a new mac_ring_t
42898275SEric Cheng 		 * for that ring.
42908275SEric Cheng 		 */
42918275SEric Cheng 		ring = mac_init_ring(mip, group, index, cap_rings);
42928275SEric Cheng 		ASSERT(group->mrg_state > MAC_GROUP_STATE_UNINIT);
42938275SEric Cheng 	} else {
42948275SEric Cheng 		/*
42958275SEric Cheng 		 * The function is called as a result of a MAC layer request
42968275SEric Cheng 		 * to add a ring to an existing group. In this case the
42978275SEric Cheng 		 * ring is being moved between groups, which requires
42988275SEric Cheng 		 * the underlying driver to support dynamic grouping,
42998275SEric Cheng 		 * and the mac_ring_t already exists.
43008275SEric Cheng 		 */
43018275SEric Cheng 		ASSERT(group_type == MAC_GROUP_TYPE_DYNAMIC);
430211878SVenu.Iyer@Sun.COM 		ASSERT(group->mrg_driver == NULL ||
430311878SVenu.Iyer@Sun.COM 		    cap_rings->mr_gaddring != NULL);
43048275SEric Cheng 		ASSERT(ring->mr_gh == NULL);
43058275SEric Cheng 	}
43068275SEric Cheng 
43078275SEric Cheng 	/*
43088275SEric Cheng 	 * At this point the ring should not be in use, and it should be
43098275SEric Cheng 	 * of the right for the target group.
43108275SEric Cheng 	 */
43118275SEric Cheng 	ASSERT(ring->mr_state < MR_INUSE);
43128275SEric Cheng 	ASSERT(ring->mr_srs == NULL);
43138275SEric Cheng 	ASSERT(ring->mr_type == group->mrg_type);
43148275SEric Cheng 
43158275SEric Cheng 	if (!driver_call) {
43168275SEric Cheng 		/*
43178275SEric Cheng 		 * Add the driver level hardware ring if the process was not
43188275SEric Cheng 		 * initiated by the driver, and the target group is not the
43198275SEric Cheng 		 * group.
43208275SEric Cheng 		 */
43218275SEric Cheng 		if (group->mrg_driver != NULL) {
43228275SEric Cheng 			cap_rings->mr_gaddring(group->mrg_driver,
43238275SEric Cheng 			    ring->mr_driver, ring->mr_type);
43248275SEric Cheng 		}
43258275SEric Cheng 
43268275SEric Cheng 		/*
43278275SEric Cheng 		 * Insert the ring ahead existing rings.
43288275SEric Cheng 		 */
43298275SEric Cheng 		ring->mr_next = group->mrg_rings;
43308275SEric Cheng 		group->mrg_rings = ring;
43318275SEric Cheng 		ring->mr_gh = (mac_group_handle_t)group;
43328275SEric Cheng 		group->mrg_cur_count++;
43338275SEric Cheng 	}
43348275SEric Cheng 
43358275SEric Cheng 	/*
43368275SEric Cheng 	 * If the group has not been actively used, we're done.
43378275SEric Cheng 	 */
43388275SEric Cheng 	if (group->mrg_index != -1 &&
43398275SEric Cheng 	    group->mrg_state < MAC_GROUP_STATE_RESERVED)
43408275SEric Cheng 		return (0);
43418275SEric Cheng 
43428275SEric Cheng 	/*
434311878SVenu.Iyer@Sun.COM 	 * Start the ring if needed. Failure causes to undo the grouping action.
434411878SVenu.Iyer@Sun.COM 	 */
434511878SVenu.Iyer@Sun.COM 	if (ring->mr_state != MR_INUSE) {
434611878SVenu.Iyer@Sun.COM 		if ((ret = mac_start_ring(ring)) != 0) {
434711878SVenu.Iyer@Sun.COM 			if (!driver_call) {
434811878SVenu.Iyer@Sun.COM 				cap_rings->mr_gremring(group->mrg_driver,
434911878SVenu.Iyer@Sun.COM 				    ring->mr_driver, ring->mr_type);
435011878SVenu.Iyer@Sun.COM 			}
435111878SVenu.Iyer@Sun.COM 			group->mrg_cur_count--;
435211878SVenu.Iyer@Sun.COM 			group->mrg_rings = ring->mr_next;
435311878SVenu.Iyer@Sun.COM 
435411878SVenu.Iyer@Sun.COM 			ring->mr_gh = NULL;
435511878SVenu.Iyer@Sun.COM 
435611878SVenu.Iyer@Sun.COM 			if (driver_call)
435711878SVenu.Iyer@Sun.COM 				mac_ring_free(mip, ring);
435811878SVenu.Iyer@Sun.COM 
435911878SVenu.Iyer@Sun.COM 			return (ret);
436011878SVenu.Iyer@Sun.COM 		}
436111878SVenu.Iyer@Sun.COM 	}
436211878SVenu.Iyer@Sun.COM 
436311878SVenu.Iyer@Sun.COM 	/*
43648275SEric Cheng 	 * Set up SRS/SR according to the ring type.
43658275SEric Cheng 	 */
43668275SEric Cheng 	switch (ring->mr_type) {
43678275SEric Cheng 	case MAC_RING_TYPE_RX:
43688275SEric Cheng 		/*
43698275SEric Cheng 		 * Setup SRS on top of the new ring if the group is
43708275SEric Cheng 		 * reserved for someones exclusive use.
43718275SEric Cheng 		 */
43728275SEric Cheng 		if (group->mrg_state == MAC_GROUP_STATE_RESERVED) {
43738275SEric Cheng 			mac_client_impl_t *mcip;
43748275SEric Cheng 
437511878SVenu.Iyer@Sun.COM 			mcip = MAC_GROUP_ONLY_CLIENT(group);
437611878SVenu.Iyer@Sun.COM 			/*
437711878SVenu.Iyer@Sun.COM 			 * Even though this group is reserved we migth still
437811878SVenu.Iyer@Sun.COM 			 * have multiple clients, i.e a VLAN shares the
437911878SVenu.Iyer@Sun.COM 			 * group with the primary mac client.
438011878SVenu.Iyer@Sun.COM 			 */
438111878SVenu.Iyer@Sun.COM 			if (mcip != NULL) {
438211878SVenu.Iyer@Sun.COM 				flent = mcip->mci_flent;
438311878SVenu.Iyer@Sun.COM 				ASSERT(flent->fe_rx_srs_cnt > 0);
438411878SVenu.Iyer@Sun.COM 				mac_rx_srs_group_setup(mcip, flent, SRST_LINK);
438511878SVenu.Iyer@Sun.COM 				mac_fanout_setup(mcip, flent,
438611878SVenu.Iyer@Sun.COM 				    MCIP_RESOURCE_PROPS(mcip), mac_rx_deliver,
438711878SVenu.Iyer@Sun.COM 				    mcip, NULL, NULL);
438811878SVenu.Iyer@Sun.COM 			} else {
438911878SVenu.Iyer@Sun.COM 				ring->mr_classify_type = MAC_SW_CLASSIFIER;
439011878SVenu.Iyer@Sun.COM 			}
43918275SEric Cheng 		}
43928275SEric Cheng 		break;
43938275SEric Cheng 	case MAC_RING_TYPE_TX:
439411878SVenu.Iyer@Sun.COM 	{
439511878SVenu.Iyer@Sun.COM 		mac_grp_client_t	*mgcp = group->mrg_clients;
439611878SVenu.Iyer@Sun.COM 		mac_client_impl_t	*mcip;
439711878SVenu.Iyer@Sun.COM 		mac_soft_ring_set_t	*mac_srs;
439811878SVenu.Iyer@Sun.COM 		mac_srs_tx_t		*tx;
439911878SVenu.Iyer@Sun.COM 
440011878SVenu.Iyer@Sun.COM 		if (MAC_GROUP_NO_CLIENT(group)) {
440111878SVenu.Iyer@Sun.COM 			if (ring->mr_state == MR_INUSE)
440211878SVenu.Iyer@Sun.COM 				mac_stop_ring(ring);
440311878SVenu.Iyer@Sun.COM 			ring->mr_flag = 0;
440411878SVenu.Iyer@Sun.COM 			break;
440511878SVenu.Iyer@Sun.COM 		}
44068275SEric Cheng 		/*
440711878SVenu.Iyer@Sun.COM 		 * If the rings are being moved to a group that has
440811878SVenu.Iyer@Sun.COM 		 * clients using it, then add the new rings to the
440911878SVenu.Iyer@Sun.COM 		 * clients SRS.
44108275SEric Cheng 		 */
441111878SVenu.Iyer@Sun.COM 		while (mgcp != NULL) {
441211878SVenu.Iyer@Sun.COM 			boolean_t	is_aggr;
441311878SVenu.Iyer@Sun.COM 
441411878SVenu.Iyer@Sun.COM 			mcip = mgcp->mgc_client;
441511878SVenu.Iyer@Sun.COM 			flent = mcip->mci_flent;
441611878SVenu.Iyer@Sun.COM 			is_aggr = (mcip->mci_state_flags & MCIS_IS_AGGR);
441711878SVenu.Iyer@Sun.COM 			mac_srs = MCIP_TX_SRS(mcip);
441811878SVenu.Iyer@Sun.COM 			tx = &mac_srs->srs_tx;
441911878SVenu.Iyer@Sun.COM 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
442011878SVenu.Iyer@Sun.COM 			/*
442111878SVenu.Iyer@Sun.COM 			 * If we are  growing from 1 to multiple rings.
442211878SVenu.Iyer@Sun.COM 			 */
442311878SVenu.Iyer@Sun.COM 			if (tx->st_mode == SRS_TX_BW ||
442411878SVenu.Iyer@Sun.COM 			    tx->st_mode == SRS_TX_SERIALIZE ||
442511878SVenu.Iyer@Sun.COM 			    tx->st_mode == SRS_TX_DEFAULT) {
442611878SVenu.Iyer@Sun.COM 				mac_ring_t	*tx_ring = tx->st_arg2;
442711878SVenu.Iyer@Sun.COM 
442811878SVenu.Iyer@Sun.COM 				tx->st_arg2 = NULL;
442911878SVenu.Iyer@Sun.COM 				mac_tx_srs_stat_recreate(mac_srs, B_TRUE);
443011878SVenu.Iyer@Sun.COM 				mac_tx_srs_add_ring(mac_srs, tx_ring);
443111878SVenu.Iyer@Sun.COM 				if (mac_srs->srs_type & SRST_BW_CONTROL) {
443211878SVenu.Iyer@Sun.COM 					tx->st_mode = is_aggr ? SRS_TX_BW_AGGR :
443311878SVenu.Iyer@Sun.COM 					    SRS_TX_BW_FANOUT;
443411878SVenu.Iyer@Sun.COM 				} else {
443511878SVenu.Iyer@Sun.COM 					tx->st_mode = is_aggr ? SRS_TX_AGGR :
443611878SVenu.Iyer@Sun.COM 					    SRS_TX_FANOUT;
443711878SVenu.Iyer@Sun.COM 				}
443811878SVenu.Iyer@Sun.COM 				tx->st_func = mac_tx_get_func(tx->st_mode);
443911878SVenu.Iyer@Sun.COM 			}
444011878SVenu.Iyer@Sun.COM 			mac_tx_srs_add_ring(mac_srs, ring);
444111878SVenu.Iyer@Sun.COM 			mac_fanout_setup(mcip, flent, MCIP_RESOURCE_PROPS(mcip),
444211878SVenu.Iyer@Sun.COM 			    mac_rx_deliver, mcip, NULL, NULL);
444311878SVenu.Iyer@Sun.COM 			mac_tx_client_restart((mac_client_handle_t)mcip);
444411878SVenu.Iyer@Sun.COM 			mgcp = mgcp->mgc_next;
444511878SVenu.Iyer@Sun.COM 		}
44468275SEric Cheng 		break;
444711878SVenu.Iyer@Sun.COM 	}
44488275SEric Cheng 	default:
44498275SEric Cheng 		ASSERT(B_FALSE);
44508275SEric Cheng 	}
44518275SEric Cheng 	/*
445211878SVenu.Iyer@Sun.COM 	 * For aggr, the default ring will be NULL to begin with. If it
445311878SVenu.Iyer@Sun.COM 	 * is NULL, then pick the first ring that gets added as the
445411878SVenu.Iyer@Sun.COM 	 * default ring. Any ring in an aggregation can be removed at
445511878SVenu.Iyer@Sun.COM 	 * any time (by the user action of removing a link) and if the
445611878SVenu.Iyer@Sun.COM 	 * current default ring gets removed, then a new one gets
445711878SVenu.Iyer@Sun.COM 	 * picked (see i_mac_group_rem_ring()).
44588275SEric Cheng 	 */
445911878SVenu.Iyer@Sun.COM 	if (mip->mi_state_flags & MIS_IS_AGGR &&
446011878SVenu.Iyer@Sun.COM 	    mip->mi_default_tx_ring == NULL &&
446111878SVenu.Iyer@Sun.COM 	    ring->mr_type == MAC_RING_TYPE_TX) {
446211878SVenu.Iyer@Sun.COM 		mip->mi_default_tx_ring = (mac_ring_handle_t)ring;
446311878SVenu.Iyer@Sun.COM 	}
446411878SVenu.Iyer@Sun.COM 
44658275SEric Cheng 	MAC_RING_UNMARK(ring, MR_INCIPIENT);
44668275SEric Cheng 	return (0);
44678275SEric Cheng }
44688275SEric Cheng 
44698275SEric Cheng /*
44708275SEric Cheng  * Remove a ring from it's current group. MAC internal function for dynamic
44718275SEric Cheng  * grouping.
44728275SEric Cheng  *
44738275SEric Cheng  * The caller needs to call mac_perim_enter() before calling this function.
44748275SEric Cheng  */
44758275SEric Cheng void
i_mac_group_rem_ring(mac_group_t * group,mac_ring_t * ring,boolean_t driver_call)44768275SEric Cheng i_mac_group_rem_ring(mac_group_t *group, mac_ring_t *ring,
44778275SEric Cheng     boolean_t driver_call)
44788275SEric Cheng {
44798275SEric Cheng 	mac_impl_t *mip = (mac_impl_t *)group->mrg_mh;
44808275SEric Cheng 	mac_capab_rings_t *cap_rings = NULL;
44818275SEric Cheng 	mac_group_type_t group_type;
44828275SEric Cheng 
44838275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
44848275SEric Cheng 
448511878SVenu.Iyer@Sun.COM 	ASSERT(mac_find_ring((mac_group_handle_t)group,
448611878SVenu.Iyer@Sun.COM 	    ring->mr_index) == (mac_ring_handle_t)ring);
44878275SEric Cheng 	ASSERT((mac_group_t *)ring->mr_gh == group);
44888275SEric Cheng 	ASSERT(ring->mr_type == group->mrg_type);
44898275SEric Cheng 
449011878SVenu.Iyer@Sun.COM 	if (ring->mr_state == MR_INUSE)
449111878SVenu.Iyer@Sun.COM 		mac_stop_ring(ring);
44928275SEric Cheng 	switch (ring->mr_type) {
44938275SEric Cheng 	case MAC_RING_TYPE_RX:
44948275SEric Cheng 		group_type = mip->mi_rx_group_type;
44958275SEric Cheng 		cap_rings = &mip->mi_rx_rings_cap;
44968275SEric Cheng 
44978275SEric Cheng 		/*
44988275SEric Cheng 		 * Only hardware classified packets hold a reference to the
44998275SEric Cheng 		 * ring all the way up the Rx path. mac_rx_srs_remove()
45008275SEric Cheng 		 * will take care of quiescing the Rx path and removing the
45018275SEric Cheng 		 * SRS. The software classified path neither holds a reference
45028275SEric Cheng 		 * nor any association with the ring in mac_rx.
45038275SEric Cheng 		 */
45048275SEric Cheng 		if (ring->mr_srs != NULL) {
45058275SEric Cheng 			mac_rx_srs_remove(ring->mr_srs);
45068275SEric Cheng 			ring->mr_srs = NULL;
45078275SEric Cheng 		}
45088275SEric Cheng 
45098275SEric Cheng 		break;
45108275SEric Cheng 	case MAC_RING_TYPE_TX:
451111878SVenu.Iyer@Sun.COM 	{
451211878SVenu.Iyer@Sun.COM 		mac_grp_client_t	*mgcp;
451311878SVenu.Iyer@Sun.COM 		mac_client_impl_t	*mcip;
451411878SVenu.Iyer@Sun.COM 		mac_soft_ring_set_t	*mac_srs;
451511878SVenu.Iyer@Sun.COM 		mac_srs_tx_t		*tx;
451611878SVenu.Iyer@Sun.COM 		mac_ring_t		*rem_ring;
451711878SVenu.Iyer@Sun.COM 		mac_group_t		*defgrp;
451811878SVenu.Iyer@Sun.COM 		uint_t			ring_info = 0;
451911878SVenu.Iyer@Sun.COM 
45208275SEric Cheng 		/*
452111878SVenu.Iyer@Sun.COM 		 * For TX this function is invoked in three
45228275SEric Cheng 		 * cases:
45238275SEric Cheng 		 *
45248275SEric Cheng 		 * 1) In the case of a failure during the
45258275SEric Cheng 		 * initial creation of a group when a share is
45268275SEric Cheng 		 * associated with a MAC client. So the SRS is not
45278275SEric Cheng 		 * yet setup, and will be setup later after the
45288275SEric Cheng 		 * group has been reserved and populated.
45298275SEric Cheng 		 *
45308275SEric Cheng 		 * 2) From mac_release_tx_group() when freeing
45318275SEric Cheng 		 * a TX SRS.
45328275SEric Cheng 		 *
453311878SVenu.Iyer@Sun.COM 		 * 3) In the case of aggr, when a port gets removed,
453411878SVenu.Iyer@Sun.COM 		 * the pseudo Tx rings that it exposed gets removed.
453511878SVenu.Iyer@Sun.COM 		 *
453611878SVenu.Iyer@Sun.COM 		 * In the first two cases the SRS and its soft
453711878SVenu.Iyer@Sun.COM 		 * rings are already quiesced.
45388275SEric Cheng 		 */
453911878SVenu.Iyer@Sun.COM 		if (driver_call) {
454011878SVenu.Iyer@Sun.COM 			mac_client_impl_t *mcip;
454111878SVenu.Iyer@Sun.COM 			mac_soft_ring_set_t *mac_srs;
454211878SVenu.Iyer@Sun.COM 			mac_soft_ring_t *sringp;
454311878SVenu.Iyer@Sun.COM 			mac_srs_tx_t *srs_tx;
454411878SVenu.Iyer@Sun.COM 
454511878SVenu.Iyer@Sun.COM 			if (mip->mi_state_flags & MIS_IS_AGGR &&
454611878SVenu.Iyer@Sun.COM 			    mip->mi_default_tx_ring ==
454711878SVenu.Iyer@Sun.COM 			    (mac_ring_handle_t)ring) {
454811878SVenu.Iyer@Sun.COM 				/* pick a new default Tx ring */
454911878SVenu.Iyer@Sun.COM 				mip->mi_default_tx_ring =
455011878SVenu.Iyer@Sun.COM 				    (group->mrg_rings != ring) ?
455111878SVenu.Iyer@Sun.COM 				    (mac_ring_handle_t)group->mrg_rings :
455211878SVenu.Iyer@Sun.COM 				    (mac_ring_handle_t)(ring->mr_next);
455311878SVenu.Iyer@Sun.COM 			}
455411878SVenu.Iyer@Sun.COM 			/* Presently only aggr case comes here */
455511878SVenu.Iyer@Sun.COM 			if (group->mrg_state != MAC_GROUP_STATE_RESERVED)
455611878SVenu.Iyer@Sun.COM 				break;
455711878SVenu.Iyer@Sun.COM 
455811878SVenu.Iyer@Sun.COM 			mcip = MAC_GROUP_ONLY_CLIENT(group);
455911878SVenu.Iyer@Sun.COM 			ASSERT(mcip != NULL);
456011878SVenu.Iyer@Sun.COM 			ASSERT(mcip->mci_state_flags & MCIS_IS_AGGR);
456111878SVenu.Iyer@Sun.COM 			mac_srs = MCIP_TX_SRS(mcip);
456211878SVenu.Iyer@Sun.COM 			ASSERT(mac_srs->srs_tx.st_mode == SRS_TX_AGGR ||
456311878SVenu.Iyer@Sun.COM 			    mac_srs->srs_tx.st_mode == SRS_TX_BW_AGGR);
456411878SVenu.Iyer@Sun.COM 			srs_tx = &mac_srs->srs_tx;
456511878SVenu.Iyer@Sun.COM 			/*
456611878SVenu.Iyer@Sun.COM 			 * Wakeup any callers blocked on this
456711878SVenu.Iyer@Sun.COM 			 * Tx ring due to flow control.
456811878SVenu.Iyer@Sun.COM 			 */
456911878SVenu.Iyer@Sun.COM 			sringp = srs_tx->st_soft_rings[ring->mr_index];
457011878SVenu.Iyer@Sun.COM 			ASSERT(sringp != NULL);
457111878SVenu.Iyer@Sun.COM 			mac_tx_invoke_callbacks(mcip, (mac_tx_cookie_t)sringp);
457211878SVenu.Iyer@Sun.COM 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
457311878SVenu.Iyer@Sun.COM 			mac_tx_srs_del_ring(mac_srs, ring);
457411878SVenu.Iyer@Sun.COM 			mac_tx_client_restart((mac_client_handle_t)mcip);
457511878SVenu.Iyer@Sun.COM 			break;
457611878SVenu.Iyer@Sun.COM 		}
457711878SVenu.Iyer@Sun.COM 		ASSERT(ring != (mac_ring_t *)mip->mi_default_tx_ring);
45788275SEric Cheng 		group_type = mip->mi_tx_group_type;
45798275SEric Cheng 		cap_rings = &mip->mi_tx_rings_cap;
458011878SVenu.Iyer@Sun.COM 		/*
458111878SVenu.Iyer@Sun.COM 		 * See if we need to take it out of the MAC clients using
458211878SVenu.Iyer@Sun.COM 		 * this group
458311878SVenu.Iyer@Sun.COM 		 */
458411878SVenu.Iyer@Sun.COM 		if (MAC_GROUP_NO_CLIENT(group))
458511878SVenu.Iyer@Sun.COM 			break;
458611878SVenu.Iyer@Sun.COM 		mgcp = group->mrg_clients;
458711878SVenu.Iyer@Sun.COM 		defgrp = MAC_DEFAULT_TX_GROUP(mip);
458811878SVenu.Iyer@Sun.COM 		while (mgcp != NULL) {
458911878SVenu.Iyer@Sun.COM 			mcip = mgcp->mgc_client;
459011878SVenu.Iyer@Sun.COM 			mac_srs = MCIP_TX_SRS(mcip);
459111878SVenu.Iyer@Sun.COM 			tx = &mac_srs->srs_tx;
459211878SVenu.Iyer@Sun.COM 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
459311878SVenu.Iyer@Sun.COM 			/*
459411878SVenu.Iyer@Sun.COM 			 * If we are here when removing rings from the
459511878SVenu.Iyer@Sun.COM 			 * defgroup, mac_reserve_tx_ring would have
459611878SVenu.Iyer@Sun.COM 			 * already deleted the ring from the MAC
459711878SVenu.Iyer@Sun.COM 			 * clients in the group.
459811878SVenu.Iyer@Sun.COM 			 */
459911878SVenu.Iyer@Sun.COM 			if (group != defgrp) {
460011878SVenu.Iyer@Sun.COM 				mac_tx_invoke_callbacks(mcip,
460111878SVenu.Iyer@Sun.COM 				    (mac_tx_cookie_t)
460211878SVenu.Iyer@Sun.COM 				    mac_tx_srs_get_soft_ring(mac_srs, ring));
460311878SVenu.Iyer@Sun.COM 				mac_tx_srs_del_ring(mac_srs, ring);
460411878SVenu.Iyer@Sun.COM 			}
460511878SVenu.Iyer@Sun.COM 			/*
460611878SVenu.Iyer@Sun.COM 			 * Additionally, if  we are left with only
460711878SVenu.Iyer@Sun.COM 			 * one ring in the group after this, we need
460811878SVenu.Iyer@Sun.COM 			 * to modify the mode etc. to. (We haven't
460911878SVenu.Iyer@Sun.COM 			 * yet taken the ring out, so we check with 2).
461011878SVenu.Iyer@Sun.COM 			 */
461111878SVenu.Iyer@Sun.COM 			if (group->mrg_cur_count == 2) {
461211878SVenu.Iyer@Sun.COM 				if (ring->mr_next == NULL)
461311878SVenu.Iyer@Sun.COM 					rem_ring = group->mrg_rings;
461411878SVenu.Iyer@Sun.COM 				else
461511878SVenu.Iyer@Sun.COM 					rem_ring = ring->mr_next;
461611878SVenu.Iyer@Sun.COM 				mac_tx_invoke_callbacks(mcip,
461711878SVenu.Iyer@Sun.COM 				    (mac_tx_cookie_t)
461811878SVenu.Iyer@Sun.COM 				    mac_tx_srs_get_soft_ring(mac_srs,
461911878SVenu.Iyer@Sun.COM 				    rem_ring));
462011878SVenu.Iyer@Sun.COM 				mac_tx_srs_del_ring(mac_srs, rem_ring);
462111878SVenu.Iyer@Sun.COM 				if (rem_ring->mr_state != MR_INUSE) {
462211878SVenu.Iyer@Sun.COM 					(void) mac_start_ring(rem_ring);
462311878SVenu.Iyer@Sun.COM 				}
462411878SVenu.Iyer@Sun.COM 				tx->st_arg2 = (void *)rem_ring;
462511878SVenu.Iyer@Sun.COM 				mac_tx_srs_stat_recreate(mac_srs, B_FALSE);
462611878SVenu.Iyer@Sun.COM 				ring_info = mac_hwring_getinfo(
462711878SVenu.Iyer@Sun.COM 				    (mac_ring_handle_t)rem_ring);
462811878SVenu.Iyer@Sun.COM 				/*
462911878SVenu.Iyer@Sun.COM 				 * We are  shrinking from multiple
463011878SVenu.Iyer@Sun.COM 				 * to 1 ring.
463111878SVenu.Iyer@Sun.COM 				 */
463211878SVenu.Iyer@Sun.COM 				if (mac_srs->srs_type & SRST_BW_CONTROL) {
463311878SVenu.Iyer@Sun.COM 					tx->st_mode = SRS_TX_BW;
463411878SVenu.Iyer@Sun.COM 				} else if (mac_tx_serialize ||
463511878SVenu.Iyer@Sun.COM 				    (ring_info & MAC_RING_TX_SERIALIZE)) {
463611878SVenu.Iyer@Sun.COM 					tx->st_mode = SRS_TX_SERIALIZE;
463711878SVenu.Iyer@Sun.COM 				} else {
463811878SVenu.Iyer@Sun.COM 					tx->st_mode = SRS_TX_DEFAULT;
463911878SVenu.Iyer@Sun.COM 				}
464011878SVenu.Iyer@Sun.COM 				tx->st_func = mac_tx_get_func(tx->st_mode);
464111878SVenu.Iyer@Sun.COM 			}
464211878SVenu.Iyer@Sun.COM 			mac_tx_client_restart((mac_client_handle_t)mcip);
464311878SVenu.Iyer@Sun.COM 			mgcp = mgcp->mgc_next;
464411878SVenu.Iyer@Sun.COM 		}
46458275SEric Cheng 		break;
464611878SVenu.Iyer@Sun.COM 	}
46478275SEric Cheng 	default:
46488275SEric Cheng 		ASSERT(B_FALSE);
46498275SEric Cheng 	}
46508275SEric Cheng 
46518275SEric Cheng 	/*
46528275SEric Cheng 	 * Remove the ring from the group.
46538275SEric Cheng 	 */
46548275SEric Cheng 	if (ring == group->mrg_rings)
46558275SEric Cheng 		group->mrg_rings = ring->mr_next;
46568275SEric Cheng 	else {
46578275SEric Cheng 		mac_ring_t *pre;
46588275SEric Cheng 
46598275SEric Cheng 		pre = group->mrg_rings;
46608275SEric Cheng 		while (pre->mr_next != ring)
46618275SEric Cheng 			pre = pre->mr_next;
46628275SEric Cheng 		pre->mr_next = ring->mr_next;
46638275SEric Cheng 	}
46648275SEric Cheng 	group->mrg_cur_count--;
46658275SEric Cheng 
46668275SEric Cheng 	if (!driver_call) {
46678275SEric Cheng 		ASSERT(group_type == MAC_GROUP_TYPE_DYNAMIC);
466811878SVenu.Iyer@Sun.COM 		ASSERT(group->mrg_driver == NULL ||
466911878SVenu.Iyer@Sun.COM 		    cap_rings->mr_gremring != NULL);
46708275SEric Cheng 
46718275SEric Cheng 		/*
46728275SEric Cheng 		 * Remove the driver level hardware ring.
46738275SEric Cheng 		 */
46748275SEric Cheng 		if (group->mrg_driver != NULL) {
46758275SEric Cheng 			cap_rings->mr_gremring(group->mrg_driver,
46768275SEric Cheng 			    ring->mr_driver, ring->mr_type);
46778275SEric Cheng 		}
46788275SEric Cheng 	}
46798275SEric Cheng 
46808275SEric Cheng 	ring->mr_gh = NULL;
468111878SVenu.Iyer@Sun.COM 	if (driver_call)
46828275SEric Cheng 		mac_ring_free(mip, ring);
468311878SVenu.Iyer@Sun.COM 	else
46848275SEric Cheng 		ring->mr_flag = 0;
46858275SEric Cheng }
46868275SEric Cheng 
46878275SEric Cheng /*
46888275SEric Cheng  * Move a ring to the target group. If needed, remove the ring from the group
46898275SEric Cheng  * that it currently belongs to.
46908275SEric Cheng  *
46918275SEric Cheng  * The caller need to enter MAC's perimeter by calling mac_perim_enter().
46928275SEric Cheng  */
46938275SEric Cheng static int
mac_group_mov_ring(mac_impl_t * mip,mac_group_t * d_group,mac_ring_t * ring)46948275SEric Cheng mac_group_mov_ring(mac_impl_t *mip, mac_group_t *d_group, mac_ring_t *ring)
46958275SEric Cheng {
46968275SEric Cheng 	mac_group_t *s_group = (mac_group_t *)ring->mr_gh;
46978275SEric Cheng 	int rv;
46988275SEric Cheng 
46998275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
47008275SEric Cheng 	ASSERT(d_group != NULL);
47018275SEric Cheng 	ASSERT(s_group->mrg_mh == d_group->mrg_mh);
47028275SEric Cheng 
47038275SEric Cheng 	if (s_group == d_group)
47048275SEric Cheng 		return (0);
47058275SEric Cheng 
47068275SEric Cheng 	/*
47078275SEric Cheng 	 * Remove it from current group first.
47088275SEric Cheng 	 */
47098275SEric Cheng 	if (s_group != NULL)
47108275SEric Cheng 		i_mac_group_rem_ring(s_group, ring, B_FALSE);
47118275SEric Cheng 
47128275SEric Cheng 	/*
47138275SEric Cheng 	 * Add it to the new group.
47148275SEric Cheng 	 */
47158275SEric Cheng 	rv = i_mac_group_add_ring(d_group, ring, 0);
47168275SEric Cheng 	if (rv != 0) {
47178275SEric Cheng 		/*
47188275SEric Cheng 		 * Failed to add ring back to source group. If
47198275SEric Cheng 		 * that fails, the ring is stuck in limbo, log message.
47208275SEric Cheng 		 */
47218275SEric Cheng 		if (i_mac_group_add_ring(s_group, ring, 0)) {
47228275SEric Cheng 			cmn_err(CE_WARN, "%s: failed to move ring %p\n",
47238275SEric Cheng 			    mip->mi_name, (void *)ring);
47248275SEric Cheng 		}
47258275SEric Cheng 	}
47268275SEric Cheng 
47278275SEric Cheng 	return (rv);
47288275SEric Cheng }
47298275SEric Cheng 
47308275SEric Cheng /*
47318275SEric Cheng  * Find a MAC address according to its value.
47328275SEric Cheng  */
47338275SEric Cheng mac_address_t *
mac_find_macaddr(mac_impl_t * mip,uint8_t * mac_addr)47348275SEric Cheng mac_find_macaddr(mac_impl_t *mip, uint8_t *mac_addr)
47358275SEric Cheng {
47368275SEric Cheng 	mac_address_t *map;
47378275SEric Cheng 
47388275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
47398275SEric Cheng 
47408275SEric Cheng 	for (map = mip->mi_addresses; map != NULL; map = map->ma_next) {
47418275SEric Cheng 		if (bcmp(mac_addr, map->ma_addr, map->ma_len) == 0)
47428275SEric Cheng 			break;
47438275SEric Cheng 	}
47448275SEric Cheng 
47458275SEric Cheng 	return (map);
47468275SEric Cheng }
47478275SEric Cheng 
47488275SEric Cheng /*
47498275SEric Cheng  * Check whether the MAC address is shared by multiple clients.
47508275SEric Cheng  */
47518275SEric Cheng boolean_t
mac_check_macaddr_shared(mac_address_t * map)47528275SEric Cheng mac_check_macaddr_shared(mac_address_t *map)
47538275SEric Cheng {
47548275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)map->ma_mip));
47558275SEric Cheng 
47568275SEric Cheng 	return (map->ma_nusers > 1);
47578275SEric Cheng }
47588275SEric Cheng 
47598275SEric Cheng /*
47608275SEric Cheng  * Remove the specified MAC address from the MAC address list and free it.
47618275SEric Cheng  */
47628275SEric Cheng static void
mac_free_macaddr(mac_address_t * map)47638275SEric Cheng mac_free_macaddr(mac_address_t *map)
47648275SEric Cheng {
47658275SEric Cheng 	mac_impl_t *mip = map->ma_mip;
47668275SEric Cheng 
47678275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
47688275SEric Cheng 	ASSERT(mip->mi_addresses != NULL);
47698275SEric Cheng 
47708275SEric Cheng 	map = mac_find_macaddr(mip, map->ma_addr);
47718275SEric Cheng 
47728275SEric Cheng 	ASSERT(map != NULL);
47738275SEric Cheng 	ASSERT(map->ma_nusers == 0);
47748275SEric Cheng 
47758275SEric Cheng 	if (map == mip->mi_addresses) {
47768275SEric Cheng 		mip->mi_addresses = map->ma_next;
47778275SEric Cheng 	} else {
47788275SEric Cheng 		mac_address_t *pre;
47798275SEric Cheng 
47808275SEric Cheng 		pre = mip->mi_addresses;
47818275SEric Cheng 		while (pre->ma_next != map)
47828275SEric Cheng 			pre = pre->ma_next;
47838275SEric Cheng 		pre->ma_next = map->ma_next;
47848275SEric Cheng 	}
47858275SEric Cheng 
47868275SEric Cheng 	kmem_free(map, sizeof (mac_address_t));
47878275SEric Cheng }
47888275SEric Cheng 
47898275SEric Cheng /*
47908275SEric Cheng  * Add a MAC address reference for a client. If the desired MAC address
47918275SEric Cheng  * exists, add a reference to it. Otherwise, add the new address by adding
47928275SEric Cheng  * it to a reserved group or setting promiscuous mode. Won't try different
47938275SEric Cheng  * group is the group is non-NULL, so the caller must explictly share
47948275SEric Cheng  * default group when needed.
47958275SEric Cheng  *
47968275SEric Cheng  * Note, the primary MAC address is initialized at registration time, so
47978275SEric Cheng  * to add it to default group only need to activate it if its reference
47988275SEric Cheng  * count is still zero. Also, some drivers may not have advertised RINGS
47998275SEric Cheng  * capability.
48008275SEric Cheng  */
48018275SEric Cheng int
mac_add_macaddr(mac_impl_t * mip,mac_group_t * group,uint8_t * mac_addr,boolean_t use_hw)48028400SNicolas.Droux@Sun.COM mac_add_macaddr(mac_impl_t *mip, mac_group_t *group, uint8_t *mac_addr,
48038400SNicolas.Droux@Sun.COM     boolean_t use_hw)
48048275SEric Cheng {
48058275SEric Cheng 	mac_address_t *map;
48068275SEric Cheng 	int err = 0;
48078275SEric Cheng 	boolean_t allocated_map = B_FALSE;
48088275SEric Cheng 
48098275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
48108275SEric Cheng 
48118275SEric Cheng 	map = mac_find_macaddr(mip, mac_addr);
48128275SEric Cheng 
48138275SEric Cheng 	/*
48148275SEric Cheng 	 * If the new MAC address has not been added. Allocate a new one
48158275SEric Cheng 	 * and set it up.
48168275SEric Cheng 	 */
48178275SEric Cheng 	if (map == NULL) {
48188275SEric Cheng 		map = kmem_zalloc(sizeof (mac_address_t), KM_SLEEP);
48198275SEric Cheng 		map->ma_len = mip->mi_type->mt_addr_length;
48208275SEric Cheng 		bcopy(mac_addr, map->ma_addr, map->ma_len);
48218275SEric Cheng 		map->ma_nusers = 0;
48228275SEric Cheng 		map->ma_group = group;
48238275SEric Cheng 		map->ma_mip = mip;
48248275SEric Cheng 
48258275SEric Cheng 		/* add the new MAC address to the head of the address list */
48268275SEric Cheng 		map->ma_next = mip->mi_addresses;
48278275SEric Cheng 		mip->mi_addresses = map;
48288275SEric Cheng 
48298275SEric Cheng 		allocated_map = B_TRUE;
48308275SEric Cheng 	}
48318275SEric Cheng 
483211878SVenu.Iyer@Sun.COM 	ASSERT(map->ma_group == NULL || map->ma_group == group);
483311878SVenu.Iyer@Sun.COM 	if (map->ma_group == NULL)
483411878SVenu.Iyer@Sun.COM 		map->ma_group = group;
48358275SEric Cheng 
48368275SEric Cheng 	/*
48378275SEric Cheng 	 * If the MAC address is already in use, simply account for the
48388275SEric Cheng 	 * new client.
48398275SEric Cheng 	 */
48408275SEric Cheng 	if (map->ma_nusers++ > 0)
48418275SEric Cheng 		return (0);
48428275SEric Cheng 
48438275SEric Cheng 	/*
48448275SEric Cheng 	 * Activate this MAC address by adding it to the reserved group.
48458275SEric Cheng 	 */
48468275SEric Cheng 	if (group != NULL) {
48478275SEric Cheng 		err = mac_group_addmac(group, (const uint8_t *)mac_addr);
48488275SEric Cheng 		if (err == 0) {
48498275SEric Cheng 			map->ma_type = MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED;
48508275SEric Cheng 			return (0);
48518275SEric Cheng 		}
48528275SEric Cheng 	}
48538275SEric Cheng 
48548275SEric Cheng 	/*
48558400SNicolas.Droux@Sun.COM 	 * The MAC address addition failed. If the client requires a
48568400SNicolas.Droux@Sun.COM 	 * hardware classified MAC address, fail the operation.
48578400SNicolas.Droux@Sun.COM 	 */
48588400SNicolas.Droux@Sun.COM 	if (use_hw) {
48598400SNicolas.Droux@Sun.COM 		err = ENOSPC;
48608400SNicolas.Droux@Sun.COM 		goto bail;
48618400SNicolas.Droux@Sun.COM 	}
48628400SNicolas.Droux@Sun.COM 
48638400SNicolas.Droux@Sun.COM 	/*
48648400SNicolas.Droux@Sun.COM 	 * Try promiscuous mode.
48658400SNicolas.Droux@Sun.COM 	 *
48668400SNicolas.Droux@Sun.COM 	 * For drivers that don't advertise RINGS capability, do
48678400SNicolas.Droux@Sun.COM 	 * nothing for the primary address.
48688275SEric Cheng 	 */
48698400SNicolas.Droux@Sun.COM 	if ((group == NULL) &&
48708400SNicolas.Droux@Sun.COM 	    (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) == 0)) {
48718400SNicolas.Droux@Sun.COM 		map->ma_type = MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED;
48728400SNicolas.Droux@Sun.COM 		return (0);
48738400SNicolas.Droux@Sun.COM 	}
48748400SNicolas.Droux@Sun.COM 
48758400SNicolas.Droux@Sun.COM 	/*
48768400SNicolas.Droux@Sun.COM 	 * Enable promiscuous mode in order to receive traffic
48778400SNicolas.Droux@Sun.COM 	 * to the new MAC address.
48788400SNicolas.Droux@Sun.COM 	 */
48799641SGirish.Moodalbail@Sun.COM 	if ((err = i_mac_promisc_set(mip, B_TRUE)) == 0) {
48808400SNicolas.Droux@Sun.COM 		map->ma_type = MAC_ADDRESS_TYPE_UNICAST_PROMISC;
48818400SNicolas.Droux@Sun.COM 		return (0);
48828275SEric Cheng 	}
48838275SEric Cheng 
48848275SEric Cheng 	/*
48858275SEric Cheng 	 * Free the MAC address that could not be added. Don't free
48868275SEric Cheng 	 * a pre-existing address, it could have been the entry
48878275SEric Cheng 	 * for the primary MAC address which was pre-allocated by
48888275SEric Cheng 	 * mac_init_macaddr(), and which must remain on the list.
48898275SEric Cheng 	 */
48908400SNicolas.Droux@Sun.COM bail:
48918275SEric Cheng 	map->ma_nusers--;
48928275SEric Cheng 	if (allocated_map)
48938275SEric Cheng 		mac_free_macaddr(map);
48948275SEric Cheng 	return (err);
48958275SEric Cheng }
48968275SEric Cheng 
48978275SEric Cheng /*
48988275SEric Cheng  * Remove a reference to a MAC address. This may cause to remove the MAC
48998275SEric Cheng  * address from an associated group or to turn off promiscuous mode.
49008275SEric Cheng  * The caller needs to handle the failure properly.
49018275SEric Cheng  */
49028275SEric Cheng int
mac_remove_macaddr(mac_address_t * map)49038275SEric Cheng mac_remove_macaddr(mac_address_t *map)
49048275SEric Cheng {
49058275SEric Cheng 	mac_impl_t *mip = map->ma_mip;
49068275SEric Cheng 	int err = 0;
49078275SEric Cheng 
49088275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
49098275SEric Cheng 
49108275SEric Cheng 	ASSERT(map == mac_find_macaddr(mip, map->ma_addr));
49118275SEric Cheng 
49128275SEric Cheng 	/*
49138275SEric Cheng 	 * If it's not the last client using this MAC address, only update
49148275SEric Cheng 	 * the MAC clients count.
49158275SEric Cheng 	 */
49168275SEric Cheng 	if (--map->ma_nusers > 0)
49178275SEric Cheng 		return (0);
49188275SEric Cheng 
49198275SEric Cheng 	/*
49208275SEric Cheng 	 * The MAC address is no longer used by any MAC client, so remove
49218275SEric Cheng 	 * it from its associated group, or turn off promiscuous mode
49228275SEric Cheng 	 * if it was enabled for the MAC address.
49238275SEric Cheng 	 */
49248275SEric Cheng 	switch (map->ma_type) {
49258275SEric Cheng 	case MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED:
49268275SEric Cheng 		/*
49278275SEric Cheng 		 * Don't free the preset primary address for drivers that
49288275SEric Cheng 		 * don't advertise RINGS capability.
49298275SEric Cheng 		 */
49308275SEric Cheng 		if (map->ma_group == NULL)
49318275SEric Cheng 			return (0);
49328275SEric Cheng 
49338275SEric Cheng 		err = mac_group_remmac(map->ma_group, map->ma_addr);
493411878SVenu.Iyer@Sun.COM 		if (err == 0)
493511878SVenu.Iyer@Sun.COM 			map->ma_group = NULL;
49368275SEric Cheng 		break;
49378275SEric Cheng 	case MAC_ADDRESS_TYPE_UNICAST_PROMISC:
49389641SGirish.Moodalbail@Sun.COM 		err = i_mac_promisc_set(mip, B_FALSE);
49398275SEric Cheng 		break;
49408275SEric Cheng 	default:
49418275SEric Cheng 		ASSERT(B_FALSE);
49428275SEric Cheng 	}
49438275SEric Cheng 
49448275SEric Cheng 	if (err != 0)
49458275SEric Cheng 		return (err);
49468275SEric Cheng 
49478275SEric Cheng 	/*
49488275SEric Cheng 	 * We created MAC address for the primary one at registration, so we
49498275SEric Cheng 	 * won't free it here. mac_fini_macaddr() will take care of it.
49508275SEric Cheng 	 */
49518275SEric Cheng 	if (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) != 0)
49528275SEric Cheng 		mac_free_macaddr(map);
49538275SEric Cheng 
49548275SEric Cheng 	return (0);
49558275SEric Cheng }
49568275SEric Cheng 
49578275SEric Cheng /*
49588275SEric Cheng  * Update an existing MAC address. The caller need to make sure that the new
49598275SEric Cheng  * value has not been used.
49608275SEric Cheng  */
49618275SEric Cheng int
mac_update_macaddr(mac_address_t * map,uint8_t * mac_addr)49628275SEric Cheng mac_update_macaddr(mac_address_t *map, uint8_t *mac_addr)
49638275SEric Cheng {
49648275SEric Cheng 	mac_impl_t *mip = map->ma_mip;
49658275SEric Cheng 	int err = 0;
49668275SEric Cheng 
49678275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
49688275SEric Cheng 	ASSERT(mac_find_macaddr(mip, mac_addr) == NULL);
49698275SEric Cheng 
49708275SEric Cheng 	switch (map->ma_type) {
49718275SEric Cheng 	case MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED:
49728275SEric Cheng 		/*
49738275SEric Cheng 		 * Update the primary address for drivers that are not
49748275SEric Cheng 		 * RINGS capable.
49758275SEric Cheng 		 */
497611878SVenu.Iyer@Sun.COM 		if (mip->mi_rx_groups == NULL) {
49778275SEric Cheng 			err = mip->mi_unicst(mip->mi_driver, (const uint8_t *)
49788275SEric Cheng 			    mac_addr);
49798275SEric Cheng 			if (err != 0)
49808275SEric Cheng 				return (err);
49818275SEric Cheng 			break;
49828275SEric Cheng 		}
49838275SEric Cheng 
49848275SEric Cheng 		/*
49858275SEric Cheng 		 * If this MAC address is not currently in use,
49868275SEric Cheng 		 * simply break out and update the value.
49878275SEric Cheng 		 */
49888275SEric Cheng 		if (map->ma_nusers == 0)
49898275SEric Cheng 			break;
49908275SEric Cheng 
49918275SEric Cheng 		/*
49928275SEric Cheng 		 * Need to replace the MAC address associated with a group.
49938275SEric Cheng 		 */
49948275SEric Cheng 		err = mac_group_remmac(map->ma_group, map->ma_addr);
49958275SEric Cheng 		if (err != 0)
49968275SEric Cheng 			return (err);
49978275SEric Cheng 
49988275SEric Cheng 		err = mac_group_addmac(map->ma_group, mac_addr);
49998275SEric Cheng 
50008275SEric Cheng 		/*
50018275SEric Cheng 		 * Failure hints hardware error. The MAC layer needs to
50028275SEric Cheng 		 * have error notification facility to handle this.
50038275SEric Cheng 		 * Now, simply try to restore the value.
50048275SEric Cheng 		 */
50058275SEric Cheng 		if (err != 0)
50068275SEric Cheng 			(void) mac_group_addmac(map->ma_group, map->ma_addr);
50078275SEric Cheng 
50088275SEric Cheng 		break;
50098275SEric Cheng 	case MAC_ADDRESS_TYPE_UNICAST_PROMISC:
50108275SEric Cheng 		/*
50118275SEric Cheng 		 * Need to do nothing more if in promiscuous mode.
50128275SEric Cheng 		 */
50138275SEric Cheng 		break;
50148275SEric Cheng 	default:
50158275SEric Cheng 		ASSERT(B_FALSE);
50168275SEric Cheng 	}
50178275SEric Cheng 
50188275SEric Cheng 	/*
50198275SEric Cheng 	 * Successfully replaced the MAC address.
50208275SEric Cheng 	 */
50218275SEric Cheng 	if (err == 0)
50228275SEric Cheng 		bcopy(mac_addr, map->ma_addr, map->ma_len);
50238275SEric Cheng 
50248275SEric Cheng 	return (err);
50258275SEric Cheng }
50268275SEric Cheng 
50278275SEric Cheng /*
50288275SEric Cheng  * Freshen the MAC address with new value. Its caller must have updated the
50298275SEric Cheng  * hardware MAC address before calling this function.
50308275SEric Cheng  * This funcitons is supposed to be used to handle the MAC address change
50318275SEric Cheng  * notification from underlying drivers.
50328275SEric Cheng  */
50338275SEric Cheng void
mac_freshen_macaddr(mac_address_t * map,uint8_t * mac_addr)50348275SEric Cheng mac_freshen_macaddr(mac_address_t *map, uint8_t *mac_addr)
50358275SEric Cheng {
50368275SEric Cheng 	mac_impl_t *mip = map->ma_mip;
50378275SEric Cheng 
50388275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
50398275SEric Cheng 	ASSERT(mac_find_macaddr(mip, mac_addr) == NULL);
50408275SEric Cheng 
50418275SEric Cheng 	/*
50428275SEric Cheng 	 * Freshen the MAC address with new value.
50438275SEric Cheng 	 */
50448275SEric Cheng 	bcopy(mac_addr, map->ma_addr, map->ma_len);
50458275SEric Cheng 	bcopy(mac_addr, mip->mi_addr, map->ma_len);
50468275SEric Cheng 
50478275SEric Cheng 	/*
50488275SEric Cheng 	 * Update all MAC clients that share this MAC address.
50498275SEric Cheng 	 */
50508275SEric Cheng 	mac_unicast_update_clients(mip, map);
50518275SEric Cheng }
50528275SEric Cheng 
50538275SEric Cheng /*
50548275SEric Cheng  * Set up the primary MAC address.
50558275SEric Cheng  */
50568275SEric Cheng void
mac_init_macaddr(mac_impl_t * mip)50578275SEric Cheng mac_init_macaddr(mac_impl_t *mip)
50588275SEric Cheng {
50598275SEric Cheng 	mac_address_t *map;
50608275SEric Cheng 
50618275SEric Cheng 	/*
50628275SEric Cheng 	 * The reference count is initialized to zero, until it's really
50638275SEric Cheng 	 * activated.
50648275SEric Cheng 	 */
50658275SEric Cheng 	map = kmem_zalloc(sizeof (mac_address_t), KM_SLEEP);
50668275SEric Cheng 	map->ma_len = mip->mi_type->mt_addr_length;
50678275SEric Cheng 	bcopy(mip->mi_addr, map->ma_addr, map->ma_len);
50688275SEric Cheng 
50698275SEric Cheng 	/*
50708275SEric Cheng 	 * If driver advertises RINGS capability, it shouldn't have initialized
50718275SEric Cheng 	 * its primary MAC address. For other drivers, including VNIC, the
50728275SEric Cheng 	 * primary address must work after registration.
50738275SEric Cheng 	 */
50748275SEric Cheng 	if (mip->mi_rx_groups == NULL)
50758275SEric Cheng 		map->ma_type = MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED;
50768275SEric Cheng 
50778275SEric Cheng 	map->ma_mip = mip;
50788275SEric Cheng 
50798275SEric Cheng 	mip->mi_addresses = map;
50808275SEric Cheng }
50818275SEric Cheng 
50828275SEric Cheng /*
50838275SEric Cheng  * Clean up the primary MAC address. Note, only one primary MAC address
50848275SEric Cheng  * is allowed. All other MAC addresses must have been freed appropriately.
50858275SEric Cheng  */
50868275SEric Cheng void
mac_fini_macaddr(mac_impl_t * mip)50878275SEric Cheng mac_fini_macaddr(mac_impl_t *mip)
50888275SEric Cheng {
50898275SEric Cheng 	mac_address_t *map = mip->mi_addresses;
50908275SEric Cheng 
50918833SVenu.Iyer@Sun.COM 	if (map == NULL)
50928833SVenu.Iyer@Sun.COM 		return;
50938833SVenu.Iyer@Sun.COM 
50948833SVenu.Iyer@Sun.COM 	/*
50958833SVenu.Iyer@Sun.COM 	 * If mi_addresses is initialized, there should be exactly one
50968833SVenu.Iyer@Sun.COM 	 * entry left on the list with no users.
50978833SVenu.Iyer@Sun.COM 	 */
50988275SEric Cheng 	ASSERT(map->ma_nusers == 0);
50998275SEric Cheng 	ASSERT(map->ma_next == NULL);
51008275SEric Cheng 
51018275SEric Cheng 	kmem_free(map, sizeof (mac_address_t));
51028275SEric Cheng 	mip->mi_addresses = NULL;
51038275SEric Cheng }
51048275SEric Cheng 
51058275SEric Cheng /*
51068275SEric Cheng  * Logging related functions.
510711878SVenu.Iyer@Sun.COM  *
510811878SVenu.Iyer@Sun.COM  * Note that Kernel statistics have been extended to maintain fine
510911878SVenu.Iyer@Sun.COM  * granularity of statistics viz. hardware lane, software lane, fanout
511011878SVenu.Iyer@Sun.COM  * stats etc. However, extended accounting continues to support only
511111878SVenu.Iyer@Sun.COM  * aggregate statistics like before.
51128275SEric Cheng  */
51138275SEric Cheng 
511413109Smichael.l.lim@oracle.com /* Write the flow description to a netinfo_t record */
511513109Smichael.l.lim@oracle.com static netinfo_t *
mac_write_flow_desc(flow_entry_t * flent,mac_client_impl_t * mcip)51168275SEric Cheng mac_write_flow_desc(flow_entry_t *flent, mac_client_impl_t *mcip)
51178275SEric Cheng {
511813109Smichael.l.lim@oracle.com 	netinfo_t		*ninfo;
511913109Smichael.l.lim@oracle.com 	net_desc_t		*ndesc;
51208275SEric Cheng 	flow_desc_t		*fdesc;
51218275SEric Cheng 	mac_resource_props_t	*mrp;
512213109Smichael.l.lim@oracle.com 
512313109Smichael.l.lim@oracle.com 	ninfo = kmem_zalloc(sizeof (netinfo_t), KM_NOSLEEP);
512413109Smichael.l.lim@oracle.com 	if (ninfo == NULL)
512513109Smichael.l.lim@oracle.com 		return (NULL);
512613109Smichael.l.lim@oracle.com 	ndesc = kmem_zalloc(sizeof (net_desc_t), KM_NOSLEEP);
512713109Smichael.l.lim@oracle.com 	if (ndesc == NULL) {
512813109Smichael.l.lim@oracle.com 		kmem_free(ninfo, sizeof (netinfo_t));
512913109Smichael.l.lim@oracle.com 		return (NULL);
513013109Smichael.l.lim@oracle.com 	}
51318275SEric Cheng 
51328275SEric Cheng 	/*
51338275SEric Cheng 	 * Grab the fe_lock to see a self-consistent fe_flow_desc.
51348275SEric Cheng 	 * Updates to the fe_flow_desc are done under the fe_lock
51358275SEric Cheng 	 */
51368275SEric Cheng 	mutex_enter(&flent->fe_lock);
51378275SEric Cheng 	fdesc = &flent->fe_flow_desc;
51388275SEric Cheng 	mrp = &flent->fe_resource_props;
51398275SEric Cheng 
514013109Smichael.l.lim@oracle.com 	ndesc->nd_name = flent->fe_flow_name;
514113109Smichael.l.lim@oracle.com 	ndesc->nd_devname = mcip->mci_name;
514213109Smichael.l.lim@oracle.com 	bcopy(fdesc->fd_src_mac, ndesc->nd_ehost, ETHERADDRL);
514313109Smichael.l.lim@oracle.com 	bcopy(fdesc->fd_dst_mac, ndesc->nd_edest, ETHERADDRL);
514413109Smichael.l.lim@oracle.com 	ndesc->nd_sap = htonl(fdesc->fd_sap);
514513109Smichael.l.lim@oracle.com 	ndesc->nd_isv4 = (uint8_t)fdesc->fd_ipversion == IPV4_VERSION;
514613109Smichael.l.lim@oracle.com 	ndesc->nd_bw_limit = mrp->mrp_maxbw;
514713109Smichael.l.lim@oracle.com 	if (ndesc->nd_isv4) {
514813109Smichael.l.lim@oracle.com 		ndesc->nd_saddr[3] = htonl(fdesc->fd_local_addr.s6_addr32[3]);
514913109Smichael.l.lim@oracle.com 		ndesc->nd_daddr[3] = htonl(fdesc->fd_remote_addr.s6_addr32[3]);
51508275SEric Cheng 	} else {
515113109Smichael.l.lim@oracle.com 		bcopy(&fdesc->fd_local_addr, ndesc->nd_saddr, IPV6_ADDR_LEN);
515213109Smichael.l.lim@oracle.com 		bcopy(&fdesc->fd_remote_addr, ndesc->nd_daddr, IPV6_ADDR_LEN);
515313109Smichael.l.lim@oracle.com 	}
515413109Smichael.l.lim@oracle.com 	ndesc->nd_sport = htons(fdesc->fd_local_port);
515513109Smichael.l.lim@oracle.com 	ndesc->nd_dport = htons(fdesc->fd_remote_port);
515613109Smichael.l.lim@oracle.com 	ndesc->nd_protocol = (uint8_t)fdesc->fd_protocol;
51578275SEric Cheng 	mutex_exit(&flent->fe_lock);
51588275SEric Cheng 
515913109Smichael.l.lim@oracle.com 	ninfo->ni_record = ndesc;
516013109Smichael.l.lim@oracle.com 	ninfo->ni_size = sizeof (net_desc_t);
516113109Smichael.l.lim@oracle.com 	ninfo->ni_type = EX_NET_FLDESC_REC;
516213109Smichael.l.lim@oracle.com 
516313109Smichael.l.lim@oracle.com 	return (ninfo);
516413109Smichael.l.lim@oracle.com }
516513109Smichael.l.lim@oracle.com 
516613109Smichael.l.lim@oracle.com /* Write the flow statistics to a netinfo_t record */
516713109Smichael.l.lim@oracle.com static netinfo_t *
mac_write_flow_stats(flow_entry_t * flent)51688275SEric Cheng mac_write_flow_stats(flow_entry_t *flent)
51698275SEric Cheng {
517013109Smichael.l.lim@oracle.com 	netinfo_t		*ninfo;
517113109Smichael.l.lim@oracle.com 	net_stat_t		*nstat;
517211878SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t	*mac_srs;
517311878SVenu.Iyer@Sun.COM 	mac_rx_stats_t		*mac_rx_stat;
517411878SVenu.Iyer@Sun.COM 	mac_tx_stats_t		*mac_tx_stat;
517511878SVenu.Iyer@Sun.COM 	int			i;
517611878SVenu.Iyer@Sun.COM 
517713109Smichael.l.lim@oracle.com 	ninfo = kmem_zalloc(sizeof (netinfo_t), KM_NOSLEEP);
517813109Smichael.l.lim@oracle.com 	if (ninfo == NULL)
517913109Smichael.l.lim@oracle.com 		return (NULL);
518013109Smichael.l.lim@oracle.com 	nstat = kmem_zalloc(sizeof (net_stat_t), KM_NOSLEEP);
518113109Smichael.l.lim@oracle.com 	if (nstat == NULL) {
518213109Smichael.l.lim@oracle.com 		kmem_free(ninfo, sizeof (netinfo_t));
518313109Smichael.l.lim@oracle.com 		return (NULL);
518413109Smichael.l.lim@oracle.com 	}
518513109Smichael.l.lim@oracle.com 
518613109Smichael.l.lim@oracle.com 	nstat->ns_name = flent->fe_flow_name;
518711878SVenu.Iyer@Sun.COM 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
518811878SVenu.Iyer@Sun.COM 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
518911878SVenu.Iyer@Sun.COM 		mac_rx_stat = &mac_srs->srs_rx.sr_stat;
519011878SVenu.Iyer@Sun.COM 
519113109Smichael.l.lim@oracle.com 		nstat->ns_ibytes += mac_rx_stat->mrs_intrbytes +
519211878SVenu.Iyer@Sun.COM 		    mac_rx_stat->mrs_pollbytes + mac_rx_stat->mrs_lclbytes;
519313109Smichael.l.lim@oracle.com 		nstat->ns_ipackets += mac_rx_stat->mrs_intrcnt +
519411878SVenu.Iyer@Sun.COM 		    mac_rx_stat->mrs_pollcnt + mac_rx_stat->mrs_lclcnt;
519513109Smichael.l.lim@oracle.com 		nstat->ns_oerrors += mac_rx_stat->mrs_ierrors;
519611878SVenu.Iyer@Sun.COM 	}
519711878SVenu.Iyer@Sun.COM 
519811878SVenu.Iyer@Sun.COM 	mac_srs = (mac_soft_ring_set_t *)(flent->fe_tx_srs);
519911878SVenu.Iyer@Sun.COM 	if (mac_srs != NULL) {
520011878SVenu.Iyer@Sun.COM 		mac_tx_stat = &mac_srs->srs_tx.st_stat;
520111878SVenu.Iyer@Sun.COM 
520213109Smichael.l.lim@oracle.com 		nstat->ns_obytes = mac_tx_stat->mts_obytes;
520313109Smichael.l.lim@oracle.com 		nstat->ns_opackets = mac_tx_stat->mts_opackets;
520413109Smichael.l.lim@oracle.com 		nstat->ns_oerrors = mac_tx_stat->mts_oerrors;
520513109Smichael.l.lim@oracle.com 	}
520613109Smichael.l.lim@oracle.com 
520713109Smichael.l.lim@oracle.com 	ninfo->ni_record = nstat;
520813109Smichael.l.lim@oracle.com 	ninfo->ni_size = sizeof (net_stat_t);
520913109Smichael.l.lim@oracle.com 	ninfo->ni_type = EX_NET_FLSTAT_REC;
521013109Smichael.l.lim@oracle.com 
521113109Smichael.l.lim@oracle.com 	return (ninfo);
521213109Smichael.l.lim@oracle.com }
521313109Smichael.l.lim@oracle.com 
521413109Smichael.l.lim@oracle.com /* Write the link description to a netinfo_t record */
521513109Smichael.l.lim@oracle.com static netinfo_t *
mac_write_link_desc(mac_client_impl_t * mcip)52168275SEric Cheng mac_write_link_desc(mac_client_impl_t *mcip)
52178275SEric Cheng {
521813109Smichael.l.lim@oracle.com 	netinfo_t		*ninfo;
521913109Smichael.l.lim@oracle.com 	net_desc_t		*ndesc;
52208275SEric Cheng 	flow_entry_t		*flent = mcip->mci_flent;
52218275SEric Cheng 
522213109Smichael.l.lim@oracle.com 	ninfo = kmem_zalloc(sizeof (netinfo_t), KM_NOSLEEP);
522313109Smichael.l.lim@oracle.com 	if (ninfo == NULL)
522413109Smichael.l.lim@oracle.com 		return (NULL);
522513109Smichael.l.lim@oracle.com 	ndesc = kmem_zalloc(sizeof (net_desc_t), KM_NOSLEEP);
522613109Smichael.l.lim@oracle.com 	if (ndesc == NULL) {
522713109Smichael.l.lim@oracle.com 		kmem_free(ninfo, sizeof (netinfo_t));
522813109Smichael.l.lim@oracle.com 		return (NULL);
522913109Smichael.l.lim@oracle.com 	}
523013109Smichael.l.lim@oracle.com 
523113109Smichael.l.lim@oracle.com 	ndesc->nd_name = mcip->mci_name;
523213109Smichael.l.lim@oracle.com 	ndesc->nd_devname = mcip->mci_name;
523313109Smichael.l.lim@oracle.com 	ndesc->nd_isv4 = B_TRUE;
52348275SEric Cheng 	/*
52358275SEric Cheng 	 * Grab the fe_lock to see a self-consistent fe_flow_desc.
52368275SEric Cheng 	 * Updates to the fe_flow_desc are done under the fe_lock
52378275SEric Cheng 	 * after removing the flent from the flow table.
52388275SEric Cheng 	 */
52398275SEric Cheng 	mutex_enter(&flent->fe_lock);
524013109Smichael.l.lim@oracle.com 	bcopy(flent->fe_flow_desc.fd_src_mac, ndesc->nd_ehost, ETHERADDRL);
52418275SEric Cheng 	mutex_exit(&flent->fe_lock);
52428275SEric Cheng 
524313109Smichael.l.lim@oracle.com 	ninfo->ni_record = ndesc;
524413109Smichael.l.lim@oracle.com 	ninfo->ni_size = sizeof (net_desc_t);
524513109Smichael.l.lim@oracle.com 	ninfo->ni_type = EX_NET_LNDESC_REC;
524613109Smichael.l.lim@oracle.com 
524713109Smichael.l.lim@oracle.com 	return (ninfo);
524813109Smichael.l.lim@oracle.com }
524913109Smichael.l.lim@oracle.com 
525013109Smichael.l.lim@oracle.com /* Write the link statistics to a netinfo_t record */
525113109Smichael.l.lim@oracle.com static netinfo_t *
mac_write_link_stats(mac_client_impl_t * mcip)52528275SEric Cheng mac_write_link_stats(mac_client_impl_t *mcip)
52538275SEric Cheng {
525413109Smichael.l.lim@oracle.com 	netinfo_t		*ninfo;
525513109Smichael.l.lim@oracle.com 	net_stat_t		*nstat;
525611878SVenu.Iyer@Sun.COM 	flow_entry_t		*flent;
525711878SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t	*mac_srs;
525811878SVenu.Iyer@Sun.COM 	mac_rx_stats_t		*mac_rx_stat;
525911878SVenu.Iyer@Sun.COM 	mac_tx_stats_t		*mac_tx_stat;
526011878SVenu.Iyer@Sun.COM 	int			i;
526111878SVenu.Iyer@Sun.COM 
526213109Smichael.l.lim@oracle.com 	ninfo = kmem_zalloc(sizeof (netinfo_t), KM_NOSLEEP);
526313109Smichael.l.lim@oracle.com 	if (ninfo == NULL)
526413109Smichael.l.lim@oracle.com 		return (NULL);
526513109Smichael.l.lim@oracle.com 	nstat = kmem_zalloc(sizeof (net_stat_t), KM_NOSLEEP);
526613109Smichael.l.lim@oracle.com 	if (nstat == NULL) {
526713109Smichael.l.lim@oracle.com 		kmem_free(ninfo, sizeof (netinfo_t));
526813109Smichael.l.lim@oracle.com 		return (NULL);
526913109Smichael.l.lim@oracle.com 	}
527013109Smichael.l.lim@oracle.com 
527113109Smichael.l.lim@oracle.com 	nstat->ns_name = mcip->mci_name;
527211878SVenu.Iyer@Sun.COM 	flent = mcip->mci_flent;
527311878SVenu.Iyer@Sun.COM 	if (flent != NULL)  {
527411878SVenu.Iyer@Sun.COM 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
527511878SVenu.Iyer@Sun.COM 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
527611878SVenu.Iyer@Sun.COM 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
527711878SVenu.Iyer@Sun.COM 
527813109Smichael.l.lim@oracle.com 			nstat->ns_ibytes += mac_rx_stat->mrs_intrbytes +
527911878SVenu.Iyer@Sun.COM 			    mac_rx_stat->mrs_pollbytes +
528011878SVenu.Iyer@Sun.COM 			    mac_rx_stat->mrs_lclbytes;
528113109Smichael.l.lim@oracle.com 			nstat->ns_ipackets += mac_rx_stat->mrs_intrcnt +
528211878SVenu.Iyer@Sun.COM 			    mac_rx_stat->mrs_pollcnt + mac_rx_stat->mrs_lclcnt;
528313109Smichael.l.lim@oracle.com 			nstat->ns_oerrors += mac_rx_stat->mrs_ierrors;
528411878SVenu.Iyer@Sun.COM 		}
528511878SVenu.Iyer@Sun.COM 	}
528611878SVenu.Iyer@Sun.COM 
528711878SVenu.Iyer@Sun.COM 	mac_srs = (mac_soft_ring_set_t *)(mcip->mci_flent->fe_tx_srs);
528811878SVenu.Iyer@Sun.COM 	if (mac_srs != NULL) {
528911878SVenu.Iyer@Sun.COM 		mac_tx_stat = &mac_srs->srs_tx.st_stat;
529011878SVenu.Iyer@Sun.COM 
529113109Smichael.l.lim@oracle.com 		nstat->ns_obytes = mac_tx_stat->mts_obytes;
529213109Smichael.l.lim@oracle.com 		nstat->ns_opackets = mac_tx_stat->mts_opackets;
529313109Smichael.l.lim@oracle.com 		nstat->ns_oerrors = mac_tx_stat->mts_oerrors;
529413109Smichael.l.lim@oracle.com 	}
529513109Smichael.l.lim@oracle.com 
529613109Smichael.l.lim@oracle.com 	ninfo->ni_record = nstat;
529713109Smichael.l.lim@oracle.com 	ninfo->ni_size = sizeof (net_stat_t);
529813109Smichael.l.lim@oracle.com 	ninfo->ni_type = EX_NET_LNSTAT_REC;
529913109Smichael.l.lim@oracle.com 
530013109Smichael.l.lim@oracle.com 	return (ninfo);
530113109Smichael.l.lim@oracle.com }
530213109Smichael.l.lim@oracle.com 
530313109Smichael.l.lim@oracle.com typedef struct i_mac_log_state_s {
530413109Smichael.l.lim@oracle.com 	boolean_t	mi_last;
530513109Smichael.l.lim@oracle.com 	int		mi_fenable;
530613109Smichael.l.lim@oracle.com 	int		mi_lenable;
530713109Smichael.l.lim@oracle.com 	list_t		*mi_list;
530813109Smichael.l.lim@oracle.com } i_mac_log_state_t;
53098275SEric Cheng 
53108275SEric Cheng /*
531113109Smichael.l.lim@oracle.com  * For a given flow, if the description has not been logged before, do it now.
53128275SEric Cheng  * If it is a VNIC, then we have collected information about it from the MAC
53138275SEric Cheng  * table, so skip it.
531413109Smichael.l.lim@oracle.com  *
531513109Smichael.l.lim@oracle.com  * Called through mac_flow_walk_nolock()
531613109Smichael.l.lim@oracle.com  *
531713109Smichael.l.lim@oracle.com  * Return 0 if successful.
53188275SEric Cheng  */
53198275SEric Cheng static int
mac_log_flowinfo(flow_entry_t * flent,void * arg)532013109Smichael.l.lim@oracle.com mac_log_flowinfo(flow_entry_t *flent, void *arg)
53218275SEric Cheng {
53228275SEric Cheng 	mac_client_impl_t	*mcip = flent->fe_mcip;
532313109Smichael.l.lim@oracle.com 	i_mac_log_state_t	*lstate = arg;
532413109Smichael.l.lim@oracle.com 	netinfo_t		*ninfo;
53258275SEric Cheng 
53268275SEric Cheng 	if (mcip == NULL)
53278275SEric Cheng 		return (0);
53288275SEric Cheng 
53298275SEric Cheng 	/*
53308275SEric Cheng 	 * If the name starts with "vnic", and fe_user_generated is true (to
53318275SEric Cheng 	 * exclude the mcast and active flow entries created implicitly for
53328275SEric Cheng 	 * a vnic, it is a VNIC flow.  i.e. vnic1 is a vnic flow,
53338275SEric Cheng 	 * vnic/bge1/mcast1 is not and neither is vnic/bge1/active.
53348275SEric Cheng 	 */
53358275SEric Cheng 	if (strncasecmp(flent->fe_flow_name, "vnic", 4) == 0 &&
53368275SEric Cheng 	    (flent->fe_type & FLOW_USER) != 0) {
53378275SEric Cheng 		return (0);
53388275SEric Cheng 	}
53398275SEric Cheng 
53408275SEric Cheng 	if (!flent->fe_desc_logged) {
53418275SEric Cheng 		/*
534213109Smichael.l.lim@oracle.com 		 * We don't return error because we want to continue the
53438275SEric Cheng 		 * walk in case this is the last walk which means we
53448275SEric Cheng 		 * need to reset fe_desc_logged in all the flows.
53458275SEric Cheng 		 */
534613109Smichael.l.lim@oracle.com 		if ((ninfo = mac_write_flow_desc(flent, mcip)) == NULL)
53478275SEric Cheng 			return (0);
534813109Smichael.l.lim@oracle.com 		list_insert_tail(lstate->mi_list, ninfo);
53498275SEric Cheng 		flent->fe_desc_logged = B_TRUE;
53508275SEric Cheng 	}
53518275SEric Cheng 
53528275SEric Cheng 	/*
53538275SEric Cheng 	 * Regardless of the error, we want to proceed in case we have to
53548275SEric Cheng 	 * reset fe_desc_logged.
53558275SEric Cheng 	 */
535613109Smichael.l.lim@oracle.com 	ninfo = mac_write_flow_stats(flent);
535713109Smichael.l.lim@oracle.com 	if (ninfo == NULL)
535813109Smichael.l.lim@oracle.com 		return (-1);
535913109Smichael.l.lim@oracle.com 
536013109Smichael.l.lim@oracle.com 	list_insert_tail(lstate->mi_list, ninfo);
53618275SEric Cheng 
53628275SEric Cheng 	if (mcip != NULL && !(mcip->mci_state_flags & MCIS_DESC_LOGGED))
53638275SEric Cheng 		flent->fe_desc_logged = B_FALSE;
53648275SEric Cheng 
53658275SEric Cheng 	return (0);
53668275SEric Cheng }
53678275SEric Cheng 
53688275SEric Cheng /*
536913109Smichael.l.lim@oracle.com  * Log the description for each mac client of this mac_impl_t, if it
537013109Smichael.l.lim@oracle.com  * hasn't already been done. Additionally, log statistics for the link as
53718275SEric Cheng  * well. Walk the flow table and log information for each flow as well.
53728275SEric Cheng  * If it is the last walk (mci_last), then we turn off mci_desc_logged (and
53738275SEric Cheng  * also fe_desc_logged, if flow logging is on) since we want to log the
53748275SEric Cheng  * description if and when logging is restarted.
537513109Smichael.l.lim@oracle.com  *
537613109Smichael.l.lim@oracle.com  * Return 0 upon success or -1 upon failure
53778275SEric Cheng  */
537813109Smichael.l.lim@oracle.com static int
i_mac_impl_log(mac_impl_t * mip,i_mac_log_state_t * lstate)537913109Smichael.l.lim@oracle.com i_mac_impl_log(mac_impl_t *mip, i_mac_log_state_t *lstate)
538013109Smichael.l.lim@oracle.com {
53818275SEric Cheng 	mac_client_impl_t	*mcip;
538213109Smichael.l.lim@oracle.com 	netinfo_t		*ninfo;
538313109Smichael.l.lim@oracle.com 
538413109Smichael.l.lim@oracle.com 	i_mac_perim_enter(mip);
53858275SEric Cheng 	/*
53868275SEric Cheng 	 * Only walk the client list for NIC and etherstub
53878275SEric Cheng 	 */
53888275SEric Cheng 	if ((mip->mi_state_flags & MIS_DISABLED) ||
53898275SEric Cheng 	    ((mip->mi_state_flags & MIS_IS_VNIC) &&
539013109Smichael.l.lim@oracle.com 	    (mac_get_lower_mac_handle((mac_handle_t)mip) != NULL))) {
539113109Smichael.l.lim@oracle.com 		i_mac_perim_exit(mip);
539213109Smichael.l.lim@oracle.com 		return (0);
539313109Smichael.l.lim@oracle.com 	}
53948275SEric Cheng 
53958275SEric Cheng 	for (mcip = mip->mi_clients_list; mcip != NULL;
53968275SEric Cheng 	    mcip = mcip->mci_client_next) {
53978275SEric Cheng 		if (!MCIP_DATAPATH_SETUP(mcip))
53988275SEric Cheng 			continue;
53998275SEric Cheng 		if (lstate->mi_lenable) {
54008275SEric Cheng 			if (!(mcip->mci_state_flags & MCIS_DESC_LOGGED)) {
540113109Smichael.l.lim@oracle.com 				ninfo = mac_write_link_desc(mcip);
540213109Smichael.l.lim@oracle.com 				if (ninfo == NULL) {
54038275SEric Cheng 				/*
54048275SEric Cheng 				 * We can't terminate it if this is the last
54058275SEric Cheng 				 * walk, else there might be some links with
54068275SEric Cheng 				 * mi_desc_logged set to true, which means
54078275SEric Cheng 				 * their description won't be logged the next
54088275SEric Cheng 				 * time logging is started (similarly for the
54098275SEric Cheng 				 * flows within such links). We can continue
54108275SEric Cheng 				 * without walking the flow table (i.e. to
54118275SEric Cheng 				 * set fe_desc_logged to false) because we
54128275SEric Cheng 				 * won't have written any flow stuff for this
54138275SEric Cheng 				 * link as we haven't logged the link itself.
54148275SEric Cheng 				 */
541513109Smichael.l.lim@oracle.com 					i_mac_perim_exit(mip);
54168275SEric Cheng 					if (lstate->mi_last)
541713109Smichael.l.lim@oracle.com 						return (0);
54188275SEric Cheng 					else
541913109Smichael.l.lim@oracle.com 						return (-1);
54208275SEric Cheng 				}
54218275SEric Cheng 				mcip->mci_state_flags |= MCIS_DESC_LOGGED;
542213109Smichael.l.lim@oracle.com 				list_insert_tail(lstate->mi_list, ninfo);
54238275SEric Cheng 			}
54248275SEric Cheng 		}
54258275SEric Cheng 
542613109Smichael.l.lim@oracle.com 		ninfo = mac_write_link_stats(mcip);
542713109Smichael.l.lim@oracle.com 		if (ninfo == NULL && !lstate->mi_last) {
542813109Smichael.l.lim@oracle.com 			i_mac_perim_exit(mip);
542913109Smichael.l.lim@oracle.com 			return (-1);
543013109Smichael.l.lim@oracle.com 		}
543113109Smichael.l.lim@oracle.com 		list_insert_tail(lstate->mi_list, ninfo);
54328275SEric Cheng 
54338275SEric Cheng 		if (lstate->mi_last)
54348275SEric Cheng 			mcip->mci_state_flags &= ~MCIS_DESC_LOGGED;
54358275SEric Cheng 
54368275SEric Cheng 		if (lstate->mi_fenable) {
54378275SEric Cheng 			if (mcip->mci_subflow_tab != NULL) {
543813109Smichael.l.lim@oracle.com 				(void) mac_flow_walk_nolock(
543913109Smichael.l.lim@oracle.com 				    mcip->mci_subflow_tab, mac_log_flowinfo,
544013109Smichael.l.lim@oracle.com 				    lstate);
54418275SEric Cheng 			}
54428275SEric Cheng 		}
54438275SEric Cheng 	}
544413109Smichael.l.lim@oracle.com 	i_mac_perim_exit(mip);
544513109Smichael.l.lim@oracle.com 	return (0);
544613109Smichael.l.lim@oracle.com }
544713109Smichael.l.lim@oracle.com 
544813109Smichael.l.lim@oracle.com /*
544913109Smichael.l.lim@oracle.com  * modhash walker function to add a mac_impl_t to a list
545013109Smichael.l.lim@oracle.com  */
545113109Smichael.l.lim@oracle.com /*ARGSUSED*/
545213109Smichael.l.lim@oracle.com static uint_t
i_mac_impl_list_walker(mod_hash_key_t key,mod_hash_val_t * val,void * arg)545313109Smichael.l.lim@oracle.com i_mac_impl_list_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
545413109Smichael.l.lim@oracle.com {
545513109Smichael.l.lim@oracle.com 	list_t			*list = (list_t *)arg;
545613109Smichael.l.lim@oracle.com 	mac_impl_t		*mip = (mac_impl_t *)val;
545713109Smichael.l.lim@oracle.com 
545813109Smichael.l.lim@oracle.com 	if ((mip->mi_state_flags & MIS_DISABLED) == 0) {
545913109Smichael.l.lim@oracle.com 		list_insert_tail(list, mip);
546013109Smichael.l.lim@oracle.com 		mip->mi_ref++;
546113109Smichael.l.lim@oracle.com 	}
546213109Smichael.l.lim@oracle.com 
54638275SEric Cheng 	return (MH_WALK_CONTINUE);
54648275SEric Cheng }
54658275SEric Cheng 
546613109Smichael.l.lim@oracle.com void
i_mac_log_info(list_t * net_log_list,i_mac_log_state_t * lstate)546713109Smichael.l.lim@oracle.com i_mac_log_info(list_t *net_log_list, i_mac_log_state_t *lstate)
546813109Smichael.l.lim@oracle.com {
546913109Smichael.l.lim@oracle.com 	list_t			mac_impl_list;
547013109Smichael.l.lim@oracle.com 	mac_impl_t		*mip;
547113109Smichael.l.lim@oracle.com 	netinfo_t		*ninfo;
547213109Smichael.l.lim@oracle.com 
547313109Smichael.l.lim@oracle.com 	/* Create list of mac_impls */
547413109Smichael.l.lim@oracle.com 	ASSERT(RW_LOCK_HELD(&i_mac_impl_lock));
547513109Smichael.l.lim@oracle.com 	list_create(&mac_impl_list, sizeof (mac_impl_t), offsetof(mac_impl_t,
547613109Smichael.l.lim@oracle.com 	    mi_node));
547713109Smichael.l.lim@oracle.com 	mod_hash_walk(i_mac_impl_hash, i_mac_impl_list_walker, &mac_impl_list);
547813109Smichael.l.lim@oracle.com 	rw_exit(&i_mac_impl_lock);
547913109Smichael.l.lim@oracle.com 
548013109Smichael.l.lim@oracle.com 	/* Create log entries for each mac_impl */
548113109Smichael.l.lim@oracle.com 	for (mip = list_head(&mac_impl_list); mip != NULL;
548213109Smichael.l.lim@oracle.com 	    mip = list_next(&mac_impl_list, mip)) {
548313109Smichael.l.lim@oracle.com 		if (i_mac_impl_log(mip, lstate) != 0)
548413109Smichael.l.lim@oracle.com 			continue;
548513109Smichael.l.lim@oracle.com 	}
548613109Smichael.l.lim@oracle.com 
548713109Smichael.l.lim@oracle.com 	/* Remove elements and destroy list of mac_impls */
548813109Smichael.l.lim@oracle.com 	rw_enter(&i_mac_impl_lock, RW_WRITER);
548913109Smichael.l.lim@oracle.com 	while ((mip = list_remove_tail(&mac_impl_list)) != NULL) {
549013109Smichael.l.lim@oracle.com 		mip->mi_ref--;
549113109Smichael.l.lim@oracle.com 	}
549213109Smichael.l.lim@oracle.com 	rw_exit(&i_mac_impl_lock);
549313109Smichael.l.lim@oracle.com 	list_destroy(&mac_impl_list);
549413109Smichael.l.lim@oracle.com 
549513109Smichael.l.lim@oracle.com 	/*
549613109Smichael.l.lim@oracle.com 	 * Write log entries to files outside of locks, free associated
549713109Smichael.l.lim@oracle.com 	 * structures, and remove entries from the list.
549813109Smichael.l.lim@oracle.com 	 */
549913109Smichael.l.lim@oracle.com 	while ((ninfo = list_head(net_log_list)) != NULL) {
550013109Smichael.l.lim@oracle.com 		(void) exacct_commit_netinfo(ninfo->ni_record, ninfo->ni_type);
550113109Smichael.l.lim@oracle.com 		list_remove(net_log_list, ninfo);
550213109Smichael.l.lim@oracle.com 		kmem_free(ninfo->ni_record, ninfo->ni_size);
550313109Smichael.l.lim@oracle.com 		kmem_free(ninfo, sizeof (*ninfo));
550413109Smichael.l.lim@oracle.com 	}
550513109Smichael.l.lim@oracle.com 	list_destroy(net_log_list);
550613109Smichael.l.lim@oracle.com }
550713109Smichael.l.lim@oracle.com 
55088275SEric Cheng /*
55098275SEric Cheng  * The timer thread that runs every mac_logging_interval seconds and logs
55108275SEric Cheng  * link and/or flow information.
55118275SEric Cheng  */
55128275SEric Cheng /* ARGSUSED */
55138275SEric Cheng void
mac_log_linkinfo(void * arg)55148275SEric Cheng mac_log_linkinfo(void *arg)
55158275SEric Cheng {
55168275SEric Cheng 	i_mac_log_state_t	lstate;
551713109Smichael.l.lim@oracle.com 	list_t			net_log_list;
551813109Smichael.l.lim@oracle.com 
551913109Smichael.l.lim@oracle.com 	list_create(&net_log_list, sizeof (netinfo_t),
552013109Smichael.l.lim@oracle.com 	    offsetof(netinfo_t, ni_link));
55218275SEric Cheng 
55228275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_READER);
55238275SEric Cheng 	if (!mac_flow_log_enable && !mac_link_log_enable) {
55248275SEric Cheng 		rw_exit(&i_mac_impl_lock);
55258275SEric Cheng 		return;
55268275SEric Cheng 	}
55278275SEric Cheng 	lstate.mi_fenable = mac_flow_log_enable;
55288275SEric Cheng 	lstate.mi_lenable = mac_link_log_enable;
55298275SEric Cheng 	lstate.mi_last = B_FALSE;
553013109Smichael.l.lim@oracle.com 	lstate.mi_list = &net_log_list;
553113109Smichael.l.lim@oracle.com 
553213109Smichael.l.lim@oracle.com 	/* Write log entries for each mac_impl in the list */
553313109Smichael.l.lim@oracle.com 	i_mac_log_info(&net_log_list, &lstate);
553413109Smichael.l.lim@oracle.com 
55358275SEric Cheng 	if (mac_flow_log_enable || mac_link_log_enable) {
55368275SEric Cheng 		mac_logging_timer = timeout(mac_log_linkinfo, NULL,
55378275SEric Cheng 		    SEC_TO_TICK(mac_logging_interval));
55388275SEric Cheng 	}
55398275SEric Cheng }
55408275SEric Cheng 
55419073SCathy.Zhou@Sun.COM typedef struct i_mac_fastpath_state_s {
55429073SCathy.Zhou@Sun.COM 	boolean_t	mf_disable;
55439073SCathy.Zhou@Sun.COM 	int		mf_err;
55449073SCathy.Zhou@Sun.COM } i_mac_fastpath_state_t;
55459073SCathy.Zhou@Sun.COM 
554613109Smichael.l.lim@oracle.com /* modhash walker function to enable or disable fastpath */
55479073SCathy.Zhou@Sun.COM /*ARGSUSED*/
55489073SCathy.Zhou@Sun.COM static uint_t
i_mac_fastpath_walker(mod_hash_key_t key,mod_hash_val_t * val,void * arg)554913109Smichael.l.lim@oracle.com i_mac_fastpath_walker(mod_hash_key_t key, mod_hash_val_t *val,
55509073SCathy.Zhou@Sun.COM     void *arg)
55519073SCathy.Zhou@Sun.COM {
55529073SCathy.Zhou@Sun.COM 	i_mac_fastpath_state_t	*state = arg;
55539073SCathy.Zhou@Sun.COM 	mac_handle_t		mh = (mac_handle_t)val;
55549073SCathy.Zhou@Sun.COM 
55559073SCathy.Zhou@Sun.COM 	if (state->mf_disable)
55569073SCathy.Zhou@Sun.COM 		state->mf_err = mac_fastpath_disable(mh);
55579073SCathy.Zhou@Sun.COM 	else
55589073SCathy.Zhou@Sun.COM 		mac_fastpath_enable(mh);
55599073SCathy.Zhou@Sun.COM 
55609073SCathy.Zhou@Sun.COM 	return (state->mf_err == 0 ? MH_WALK_CONTINUE : MH_WALK_TERMINATE);
55619073SCathy.Zhou@Sun.COM }
55629073SCathy.Zhou@Sun.COM 
55638275SEric Cheng /*
55648275SEric Cheng  * Start the logging timer.
55658275SEric Cheng  */
55669073SCathy.Zhou@Sun.COM int
mac_start_logusage(mac_logtype_t type,uint_t interval)55678275SEric Cheng mac_start_logusage(mac_logtype_t type, uint_t interval)
55688275SEric Cheng {
556913109Smichael.l.lim@oracle.com 	i_mac_fastpath_state_t	dstate = {B_TRUE, 0};
557013109Smichael.l.lim@oracle.com 	i_mac_fastpath_state_t	estate = {B_FALSE, 0};
557113109Smichael.l.lim@oracle.com 	int			err;
55729073SCathy.Zhou@Sun.COM 
55738275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
55748275SEric Cheng 	switch (type) {
55758275SEric Cheng 	case MAC_LOGTYPE_FLOW:
55768275SEric Cheng 		if (mac_flow_log_enable) {
55778275SEric Cheng 			rw_exit(&i_mac_impl_lock);
55789073SCathy.Zhou@Sun.COM 			return (0);
55798275SEric Cheng 		}
55808275SEric Cheng 		/* FALLTHRU */
55818275SEric Cheng 	case MAC_LOGTYPE_LINK:
55828275SEric Cheng 		if (mac_link_log_enable) {
55838275SEric Cheng 			rw_exit(&i_mac_impl_lock);
55849073SCathy.Zhou@Sun.COM 			return (0);
55858275SEric Cheng 		}
55868275SEric Cheng 		break;
55878275SEric Cheng 	default:
55888275SEric Cheng 		ASSERT(0);
55898275SEric Cheng 	}
55909073SCathy.Zhou@Sun.COM 
55919073SCathy.Zhou@Sun.COM 	/* Disable fastpath */
559213109Smichael.l.lim@oracle.com 	mod_hash_walk(i_mac_impl_hash, i_mac_fastpath_walker, &dstate);
559313109Smichael.l.lim@oracle.com 	if ((err = dstate.mf_err) != 0) {
55949073SCathy.Zhou@Sun.COM 		/* Reenable fastpath  */
559513109Smichael.l.lim@oracle.com 		mod_hash_walk(i_mac_impl_hash, i_mac_fastpath_walker, &estate);
55969073SCathy.Zhou@Sun.COM 		rw_exit(&i_mac_impl_lock);
55979073SCathy.Zhou@Sun.COM 		return (err);
55989073SCathy.Zhou@Sun.COM 	}
55999073SCathy.Zhou@Sun.COM 
56009073SCathy.Zhou@Sun.COM 	switch (type) {
56019073SCathy.Zhou@Sun.COM 	case MAC_LOGTYPE_FLOW:
56029073SCathy.Zhou@Sun.COM 		mac_flow_log_enable = B_TRUE;
56039073SCathy.Zhou@Sun.COM 		/* FALLTHRU */
56049073SCathy.Zhou@Sun.COM 	case MAC_LOGTYPE_LINK:
56059073SCathy.Zhou@Sun.COM 		mac_link_log_enable = B_TRUE;
56069073SCathy.Zhou@Sun.COM 		break;
56079073SCathy.Zhou@Sun.COM 	}
56089073SCathy.Zhou@Sun.COM 
56098275SEric Cheng 	mac_logging_interval = interval;
56108275SEric Cheng 	rw_exit(&i_mac_impl_lock);
56118275SEric Cheng 	mac_log_linkinfo(NULL);
56129073SCathy.Zhou@Sun.COM 	return (0);
56138275SEric Cheng }
56148275SEric Cheng 
56158275SEric Cheng /*
561613109Smichael.l.lim@oracle.com  * Stop the logging timer if both link and flow logging are turned off.
56178275SEric Cheng  */
56188275SEric Cheng void
mac_stop_logusage(mac_logtype_t type)56198275SEric Cheng mac_stop_logusage(mac_logtype_t type)
56208275SEric Cheng {
56218275SEric Cheng 	i_mac_log_state_t	lstate;
562213109Smichael.l.lim@oracle.com 	i_mac_fastpath_state_t	estate = {B_FALSE, 0};
562313109Smichael.l.lim@oracle.com 	list_t			net_log_list;
562413109Smichael.l.lim@oracle.com 
562513109Smichael.l.lim@oracle.com 	list_create(&net_log_list, sizeof (netinfo_t),
562613109Smichael.l.lim@oracle.com 	    offsetof(netinfo_t, ni_link));
56278275SEric Cheng 
56288275SEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
562913109Smichael.l.lim@oracle.com 
56308275SEric Cheng 	lstate.mi_fenable = mac_flow_log_enable;
56318275SEric Cheng 	lstate.mi_lenable = mac_link_log_enable;
563213109Smichael.l.lim@oracle.com 	lstate.mi_list = &net_log_list;
56338275SEric Cheng 
56348275SEric Cheng 	/* Last walk */
56358275SEric Cheng 	lstate.mi_last = B_TRUE;
56368275SEric Cheng 
56378275SEric Cheng 	switch (type) {
56388275SEric Cheng 	case MAC_LOGTYPE_FLOW:
56398275SEric Cheng 		if (lstate.mi_fenable) {
56408275SEric Cheng 			ASSERT(mac_link_log_enable);
56418275SEric Cheng 			mac_flow_log_enable = B_FALSE;
56428275SEric Cheng 			mac_link_log_enable = B_FALSE;
56438275SEric Cheng 			break;
56448275SEric Cheng 		}
56458275SEric Cheng 		/* FALLTHRU */
56468275SEric Cheng 	case MAC_LOGTYPE_LINK:
56478275SEric Cheng 		if (!lstate.mi_lenable || mac_flow_log_enable) {
56488275SEric Cheng 			rw_exit(&i_mac_impl_lock);
56498275SEric Cheng 			return;
56508275SEric Cheng 		}
56518275SEric Cheng 		mac_link_log_enable = B_FALSE;
56528275SEric Cheng 		break;
56538275SEric Cheng 	default:
56548275SEric Cheng 		ASSERT(0);
56558275SEric Cheng 	}
56569073SCathy.Zhou@Sun.COM 
56579073SCathy.Zhou@Sun.COM 	/* Reenable fastpath */
565813109Smichael.l.lim@oracle.com 	mod_hash_walk(i_mac_impl_hash, i_mac_fastpath_walker, &estate);
565913109Smichael.l.lim@oracle.com 
56608275SEric Cheng 	(void) untimeout(mac_logging_timer);
56618275SEric Cheng 	mac_logging_timer = 0;
56628275SEric Cheng 
566313109Smichael.l.lim@oracle.com 	/* Write log entries for each mac_impl in the list */
566413109Smichael.l.lim@oracle.com 	i_mac_log_info(&net_log_list, &lstate);
56658275SEric Cheng }
56668275SEric Cheng 
56678275SEric Cheng /*
56688275SEric Cheng  * Walk the rx and tx SRS/SRs for a flow and update the priority value.
56698275SEric Cheng  */
56708275SEric Cheng void
mac_flow_update_priority(mac_client_impl_t * mcip,flow_entry_t * flent)56718275SEric Cheng mac_flow_update_priority(mac_client_impl_t *mcip, flow_entry_t *flent)
56728275SEric Cheng {
56738275SEric Cheng 	pri_t			pri;
56748275SEric Cheng 	int			count;
56758275SEric Cheng 	mac_soft_ring_set_t	*mac_srs;
56768275SEric Cheng 
56778275SEric Cheng 	if (flent->fe_rx_srs_cnt <= 0)
56788275SEric Cheng 		return;
56798275SEric Cheng 
56808275SEric Cheng 	if (((mac_soft_ring_set_t *)flent->fe_rx_srs[0])->srs_type ==
56818275SEric Cheng 	    SRST_FLOW) {
56828275SEric Cheng 		pri = FLOW_PRIORITY(mcip->mci_min_pri,
56838275SEric Cheng 		    mcip->mci_max_pri,
56848275SEric Cheng 		    flent->fe_resource_props.mrp_priority);
56858275SEric Cheng 	} else {
56868275SEric Cheng 		pri = mcip->mci_max_pri;
56878275SEric Cheng 	}
56888275SEric Cheng 
56898275SEric Cheng 	for (count = 0; count < flent->fe_rx_srs_cnt; count++) {
56908275SEric Cheng 		mac_srs = flent->fe_rx_srs[count];
56918275SEric Cheng 		mac_update_srs_priority(mac_srs, pri);
56928275SEric Cheng 	}
56938275SEric Cheng 	/*
56948275SEric Cheng 	 * If we have a Tx SRS, we need to modify all the threads associated
56958275SEric Cheng 	 * with it.
56968275SEric Cheng 	 */
56978275SEric Cheng 	if (flent->fe_tx_srs != NULL)
56988275SEric Cheng 		mac_update_srs_priority(flent->fe_tx_srs, pri);
56998275SEric Cheng }
57008275SEric Cheng 
57018275SEric Cheng /*
57028275SEric Cheng  * RX and TX rings are reserved according to different semantics depending
57038275SEric Cheng  * on the requests from the MAC clients and type of rings:
57048275SEric Cheng  *
57058275SEric Cheng  * On the Tx side, by default we reserve individual rings, independently from
57068275SEric Cheng  * the groups.
57078275SEric Cheng  *
57088275SEric Cheng  * On the Rx side, the reservation is at the granularity of the group
57098275SEric Cheng  * of rings, and used for v12n level 1 only. It has a special case for the
57108275SEric Cheng  * primary client.
57118275SEric Cheng  *
57128275SEric Cheng  * If a share is allocated to a MAC client, we allocate a TX group and an
57138275SEric Cheng  * RX group to the client, and assign TX rings and RX rings to these
57148275SEric Cheng  * groups according to information gathered from the driver through
57158275SEric Cheng  * the share capability.
57168275SEric Cheng  *
57178275SEric Cheng  * The foreseable evolution of Rx rings will handle v12n level 2 and higher
57188275SEric Cheng  * to allocate individual rings out of a group and program the hw classifier
57198275SEric Cheng  * based on IP address or higher level criteria.
57208275SEric Cheng  */
57218275SEric Cheng 
57228275SEric Cheng /*
57238275SEric Cheng  * mac_reserve_tx_ring()
57248275SEric Cheng  * Reserve a unused ring by marking it with MR_INUSE state.
57258275SEric Cheng  * As reserved, the ring is ready to function.
57268275SEric Cheng  *
57278275SEric Cheng  * Notes for Hybrid I/O:
57288275SEric Cheng  *
57298275SEric Cheng  * If a specific ring is needed, it is specified through the desired_ring
57308275SEric Cheng  * argument. Otherwise that argument is set to NULL.
57318275SEric Cheng  * If the desired ring was previous allocated to another client, this
57328275SEric Cheng  * function swaps it with a new ring from the group of unassigned rings.
57338275SEric Cheng  */
57348275SEric Cheng mac_ring_t *
mac_reserve_tx_ring(mac_impl_t * mip,mac_ring_t * desired_ring)57358275SEric Cheng mac_reserve_tx_ring(mac_impl_t *mip, mac_ring_t *desired_ring)
57368275SEric Cheng {
573711878SVenu.Iyer@Sun.COM 	mac_group_t		*group;
573811878SVenu.Iyer@Sun.COM 	mac_grp_client_t	*mgcp;
573911878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip;
574011878SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t	*srs;
57418275SEric Cheng 
57428275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
57438275SEric Cheng 
57448275SEric Cheng 	/*
57458275SEric Cheng 	 * Find an available ring and start it before changing its status.
57468275SEric Cheng 	 * The unassigned rings are at the end of the mi_tx_groups
57478275SEric Cheng 	 * array.
57488275SEric Cheng 	 */
574911878SVenu.Iyer@Sun.COM 	group = MAC_DEFAULT_TX_GROUP(mip);
575011878SVenu.Iyer@Sun.COM 
575111878SVenu.Iyer@Sun.COM 	/* Can't take the default ring out of the default group */
575211878SVenu.Iyer@Sun.COM 	ASSERT(desired_ring != (mac_ring_t *)mip->mi_default_tx_ring);
575311878SVenu.Iyer@Sun.COM 
575411878SVenu.Iyer@Sun.COM 	if (desired_ring->mr_state == MR_FREE) {
575511878SVenu.Iyer@Sun.COM 		ASSERT(MAC_GROUP_NO_CLIENT(group));
575611878SVenu.Iyer@Sun.COM 		if (mac_start_ring(desired_ring) != 0)
575711878SVenu.Iyer@Sun.COM 			return (NULL);
575811878SVenu.Iyer@Sun.COM 		return (desired_ring);
575911878SVenu.Iyer@Sun.COM 	}
576011878SVenu.Iyer@Sun.COM 	/*
576111878SVenu.Iyer@Sun.COM 	 * There are clients using this ring, so let's move the clients
576211878SVenu.Iyer@Sun.COM 	 * away from using this ring.
576311878SVenu.Iyer@Sun.COM 	 */
576411878SVenu.Iyer@Sun.COM 	for (mgcp = group->mrg_clients; mgcp != NULL; mgcp = mgcp->mgc_next) {
576511878SVenu.Iyer@Sun.COM 		mcip = mgcp->mgc_client;
576611878SVenu.Iyer@Sun.COM 		mac_tx_client_quiesce((mac_client_handle_t)mcip);
576711878SVenu.Iyer@Sun.COM 		srs = MCIP_TX_SRS(mcip);
576811878SVenu.Iyer@Sun.COM 		ASSERT(mac_tx_srs_ring_present(srs, desired_ring));
576911878SVenu.Iyer@Sun.COM 		mac_tx_invoke_callbacks(mcip,
577011878SVenu.Iyer@Sun.COM 		    (mac_tx_cookie_t)mac_tx_srs_get_soft_ring(srs,
577111878SVenu.Iyer@Sun.COM 		    desired_ring));
577211878SVenu.Iyer@Sun.COM 		mac_tx_srs_del_ring(srs, desired_ring);
577311878SVenu.Iyer@Sun.COM 		mac_tx_client_restart((mac_client_handle_t)mcip);
577411878SVenu.Iyer@Sun.COM 	}
577511878SVenu.Iyer@Sun.COM 	return (desired_ring);
577611878SVenu.Iyer@Sun.COM }
577711878SVenu.Iyer@Sun.COM 
577811878SVenu.Iyer@Sun.COM /*
577911878SVenu.Iyer@Sun.COM  * For a reserved group with multiple clients, return the primary client.
578011878SVenu.Iyer@Sun.COM  */
578111878SVenu.Iyer@Sun.COM static mac_client_impl_t *
mac_get_grp_primary(mac_group_t * grp)578211878SVenu.Iyer@Sun.COM mac_get_grp_primary(mac_group_t *grp)
578311878SVenu.Iyer@Sun.COM {
578411878SVenu.Iyer@Sun.COM 	mac_grp_client_t	*mgcp = grp->mrg_clients;
578511878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip;
578611878SVenu.Iyer@Sun.COM 
578711878SVenu.Iyer@Sun.COM 	while (mgcp != NULL) {
578811878SVenu.Iyer@Sun.COM 		mcip = mgcp->mgc_client;
578911878SVenu.Iyer@Sun.COM 		if (mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC)
579011878SVenu.Iyer@Sun.COM 			return (mcip);
579111878SVenu.Iyer@Sun.COM 		mgcp = mgcp->mgc_next;
579211878SVenu.Iyer@Sun.COM 	}
579311878SVenu.Iyer@Sun.COM 	return (NULL);
579411878SVenu.Iyer@Sun.COM }
579511878SVenu.Iyer@Sun.COM 
579611878SVenu.Iyer@Sun.COM /*
579711878SVenu.Iyer@Sun.COM  * Hybrid I/O specifies the ring that should be given to a share.
579811878SVenu.Iyer@Sun.COM  * If the ring is already used by clients, then we need to release
579911878SVenu.Iyer@Sun.COM  * the ring back to the default group so that we can give it to
580011878SVenu.Iyer@Sun.COM  * the share. This means the clients using this ring now get a
580111878SVenu.Iyer@Sun.COM  * replacement ring. If there aren't any replacement rings, this
580211878SVenu.Iyer@Sun.COM  * function returns a failure.
580311878SVenu.Iyer@Sun.COM  */
580411878SVenu.Iyer@Sun.COM static int
mac_reclaim_ring_from_grp(mac_impl_t * mip,mac_ring_type_t ring_type,mac_ring_t * ring,mac_ring_t ** rings,int nrings)580511878SVenu.Iyer@Sun.COM mac_reclaim_ring_from_grp(mac_impl_t *mip, mac_ring_type_t ring_type,
580611878SVenu.Iyer@Sun.COM     mac_ring_t *ring, mac_ring_t **rings, int nrings)
580711878SVenu.Iyer@Sun.COM {
580811878SVenu.Iyer@Sun.COM 	mac_group_t		*group = (mac_group_t *)ring->mr_gh;
580911878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mrp;
581011878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip;
581111878SVenu.Iyer@Sun.COM 	mac_group_t		*defgrp;
581211878SVenu.Iyer@Sun.COM 	mac_ring_t		*tring;
581311878SVenu.Iyer@Sun.COM 	mac_group_t		*tgrp;
581411878SVenu.Iyer@Sun.COM 	int			i;
581511878SVenu.Iyer@Sun.COM 	int			j;
581611878SVenu.Iyer@Sun.COM 
581711878SVenu.Iyer@Sun.COM 	mcip = MAC_GROUP_ONLY_CLIENT(group);
581811878SVenu.Iyer@Sun.COM 	if (mcip == NULL)
581911878SVenu.Iyer@Sun.COM 		mcip = mac_get_grp_primary(group);
582011878SVenu.Iyer@Sun.COM 	ASSERT(mcip != NULL);
582111878SVenu.Iyer@Sun.COM 	ASSERT(mcip->mci_share == NULL);
582211878SVenu.Iyer@Sun.COM 
582311878SVenu.Iyer@Sun.COM 	mrp = MCIP_RESOURCE_PROPS(mcip);
582411878SVenu.Iyer@Sun.COM 	if (ring_type == MAC_RING_TYPE_RX) {
582511878SVenu.Iyer@Sun.COM 		defgrp = mip->mi_rx_donor_grp;
582611878SVenu.Iyer@Sun.COM 		if ((mrp->mrp_mask & MRP_RX_RINGS) == 0) {
582711878SVenu.Iyer@Sun.COM 			/* Need to put this mac client in the default group */
582811878SVenu.Iyer@Sun.COM 			if (mac_rx_switch_group(mcip, group, defgrp) != 0)
582911878SVenu.Iyer@Sun.COM 				return (ENOSPC);
58308275SEric Cheng 		} else {
58318275SEric Cheng 			/*
583211878SVenu.Iyer@Sun.COM 			 * Switch this ring with some other ring from
583311878SVenu.Iyer@Sun.COM 			 * the default group.
58348275SEric Cheng 			 */
583511878SVenu.Iyer@Sun.COM 			for (tring = defgrp->mrg_rings; tring != NULL;
583611878SVenu.Iyer@Sun.COM 			    tring = tring->mr_next) {
583711878SVenu.Iyer@Sun.COM 				if (tring->mr_index == 0)
583811878SVenu.Iyer@Sun.COM 					continue;
583911878SVenu.Iyer@Sun.COM 				for (j = 0; j < nrings; j++) {
584011878SVenu.Iyer@Sun.COM 					if (rings[j] == tring)
584111878SVenu.Iyer@Sun.COM 						break;
584211878SVenu.Iyer@Sun.COM 				}
584311878SVenu.Iyer@Sun.COM 				if (j >= nrings)
584411878SVenu.Iyer@Sun.COM 					break;
584511878SVenu.Iyer@Sun.COM 			}
584611878SVenu.Iyer@Sun.COM 			if (tring == NULL)
584711878SVenu.Iyer@Sun.COM 				return (ENOSPC);
584811878SVenu.Iyer@Sun.COM 			if (mac_group_mov_ring(mip, group, tring) != 0)
584911878SVenu.Iyer@Sun.COM 				return (ENOSPC);
585011878SVenu.Iyer@Sun.COM 			if (mac_group_mov_ring(mip, defgrp, ring) != 0) {
585111878SVenu.Iyer@Sun.COM 				(void) mac_group_mov_ring(mip, defgrp, tring);
585211878SVenu.Iyer@Sun.COM 				return (ENOSPC);
585311878SVenu.Iyer@Sun.COM 			}
585411878SVenu.Iyer@Sun.COM 		}
585511878SVenu.Iyer@Sun.COM 		ASSERT(ring->mr_gh == (mac_group_handle_t)defgrp);
585611878SVenu.Iyer@Sun.COM 		return (0);
585711878SVenu.Iyer@Sun.COM 	}
585811878SVenu.Iyer@Sun.COM 
585911878SVenu.Iyer@Sun.COM 	defgrp = MAC_DEFAULT_TX_GROUP(mip);
586011878SVenu.Iyer@Sun.COM 	if (ring == (mac_ring_t *)mip->mi_default_tx_ring) {
586111878SVenu.Iyer@Sun.COM 		/*
586211878SVenu.Iyer@Sun.COM 		 * See if we can get a spare ring to replace the default
586311878SVenu.Iyer@Sun.COM 		 * ring.
586411878SVenu.Iyer@Sun.COM 		 */
586511878SVenu.Iyer@Sun.COM 		if (defgrp->mrg_cur_count == 1) {
586611878SVenu.Iyer@Sun.COM 			/*
586711878SVenu.Iyer@Sun.COM 			 * Need to get a ring from another client, see if
586811878SVenu.Iyer@Sun.COM 			 * there are any clients that can be moved to
586911878SVenu.Iyer@Sun.COM 			 * the default group, thereby freeing some rings.
587011878SVenu.Iyer@Sun.COM 			 */
587111878SVenu.Iyer@Sun.COM 			for (i = 0; i < mip->mi_tx_group_count; i++) {
587211878SVenu.Iyer@Sun.COM 				tgrp = &mip->mi_tx_groups[i];
587311878SVenu.Iyer@Sun.COM 				if (tgrp->mrg_state ==
587411878SVenu.Iyer@Sun.COM 				    MAC_GROUP_STATE_REGISTERED) {
587511878SVenu.Iyer@Sun.COM 					continue;
587611878SVenu.Iyer@Sun.COM 				}
587711878SVenu.Iyer@Sun.COM 				mcip = MAC_GROUP_ONLY_CLIENT(tgrp);
587811878SVenu.Iyer@Sun.COM 				if (mcip == NULL)
587911878SVenu.Iyer@Sun.COM 					mcip = mac_get_grp_primary(tgrp);
588011878SVenu.Iyer@Sun.COM 				ASSERT(mcip != NULL);
588111878SVenu.Iyer@Sun.COM 				mrp = MCIP_RESOURCE_PROPS(mcip);
588211878SVenu.Iyer@Sun.COM 				if ((mrp->mrp_mask & MRP_TX_RINGS) == 0) {
588311878SVenu.Iyer@Sun.COM 					ASSERT(tgrp->mrg_cur_count == 1);
588411878SVenu.Iyer@Sun.COM 					/*
588511878SVenu.Iyer@Sun.COM 					 * If this ring is part of the
588611878SVenu.Iyer@Sun.COM 					 * rings asked by the share we cannot
588711878SVenu.Iyer@Sun.COM 					 * use it as the default ring.
588811878SVenu.Iyer@Sun.COM 					 */
588911878SVenu.Iyer@Sun.COM 					for (j = 0; j < nrings; j++) {
589011878SVenu.Iyer@Sun.COM 						if (rings[j] == tgrp->mrg_rings)
589111878SVenu.Iyer@Sun.COM 							break;
589211878SVenu.Iyer@Sun.COM 					}
589311878SVenu.Iyer@Sun.COM 					if (j < nrings)
589411878SVenu.Iyer@Sun.COM 						continue;
589511878SVenu.Iyer@Sun.COM 					mac_tx_client_quiesce(
589611878SVenu.Iyer@Sun.COM 					    (mac_client_handle_t)mcip);
589711878SVenu.Iyer@Sun.COM 					mac_tx_switch_group(mcip, tgrp,
589811878SVenu.Iyer@Sun.COM 					    defgrp);
589911878SVenu.Iyer@Sun.COM 					mac_tx_client_restart(
590011878SVenu.Iyer@Sun.COM 					    (mac_client_handle_t)mcip);
59018275SEric Cheng 					break;
59028275SEric Cheng 				}
59038275SEric Cheng 			}
59048275SEric Cheng 			/*
590511878SVenu.Iyer@Sun.COM 			 * All the rings are reserved, can't give up the
590611878SVenu.Iyer@Sun.COM 			 * default ring.
59078275SEric Cheng 			 */
590811878SVenu.Iyer@Sun.COM 			if (defgrp->mrg_cur_count <= 1)
590911878SVenu.Iyer@Sun.COM 				return (ENOSPC);
591011878SVenu.Iyer@Sun.COM 		}
591111878SVenu.Iyer@Sun.COM 		/*
591211878SVenu.Iyer@Sun.COM 		 * Swap the default ring with another.
591311878SVenu.Iyer@Sun.COM 		 */
591411878SVenu.Iyer@Sun.COM 		for (tring = defgrp->mrg_rings; tring != NULL;
591511878SVenu.Iyer@Sun.COM 		    tring = tring->mr_next) {
591611878SVenu.Iyer@Sun.COM 			/*
591711878SVenu.Iyer@Sun.COM 			 * If this ring is part of the rings asked by the
591811878SVenu.Iyer@Sun.COM 			 * share we cannot use it as the default ring.
591911878SVenu.Iyer@Sun.COM 			 */
592011878SVenu.Iyer@Sun.COM 			for (j = 0; j < nrings; j++) {
592111878SVenu.Iyer@Sun.COM 				if (rings[j] == tring)
592211878SVenu.Iyer@Sun.COM 					break;
59238275SEric Cheng 			}
592411878SVenu.Iyer@Sun.COM 			if (j >= nrings)
592511878SVenu.Iyer@Sun.COM 				break;
592611878SVenu.Iyer@Sun.COM 		}
592711878SVenu.Iyer@Sun.COM 		ASSERT(tring != NULL);
592811878SVenu.Iyer@Sun.COM 		mip->mi_default_tx_ring = (mac_ring_handle_t)tring;
592911878SVenu.Iyer@Sun.COM 		return (0);
593011878SVenu.Iyer@Sun.COM 	}
593111878SVenu.Iyer@Sun.COM 	/*
593211878SVenu.Iyer@Sun.COM 	 * The Tx ring is with a group reserved by a MAC client. See if
593311878SVenu.Iyer@Sun.COM 	 * we can swap it.
593411878SVenu.Iyer@Sun.COM 	 */
593511878SVenu.Iyer@Sun.COM 	ASSERT(group->mrg_state == MAC_GROUP_STATE_RESERVED);
593611878SVenu.Iyer@Sun.COM 	mcip = MAC_GROUP_ONLY_CLIENT(group);
593711878SVenu.Iyer@Sun.COM 	if (mcip == NULL)
593811878SVenu.Iyer@Sun.COM 		mcip = mac_get_grp_primary(group);
593911878SVenu.Iyer@Sun.COM 	ASSERT(mcip !=  NULL);
594011878SVenu.Iyer@Sun.COM 	mrp = MCIP_RESOURCE_PROPS(mcip);
594111878SVenu.Iyer@Sun.COM 	mac_tx_client_quiesce((mac_client_handle_t)mcip);
594211878SVenu.Iyer@Sun.COM 	if ((mrp->mrp_mask & MRP_TX_RINGS) == 0) {
594311878SVenu.Iyer@Sun.COM 		ASSERT(group->mrg_cur_count == 1);
594411878SVenu.Iyer@Sun.COM 		/* Put this mac client in the default group */
594511878SVenu.Iyer@Sun.COM 		mac_tx_switch_group(mcip, group, defgrp);
594611878SVenu.Iyer@Sun.COM 	} else {
594711878SVenu.Iyer@Sun.COM 		/*
594811878SVenu.Iyer@Sun.COM 		 * Switch this ring with some other ring from
594911878SVenu.Iyer@Sun.COM 		 * the default group.
595011878SVenu.Iyer@Sun.COM 		 */
595111878SVenu.Iyer@Sun.COM 		for (tring = defgrp->mrg_rings; tring != NULL;
595211878SVenu.Iyer@Sun.COM 		    tring = tring->mr_next) {
595311878SVenu.Iyer@Sun.COM 			if (tring == (mac_ring_t *)mip->mi_default_tx_ring)
595411878SVenu.Iyer@Sun.COM 				continue;
595511878SVenu.Iyer@Sun.COM 			/*
595611878SVenu.Iyer@Sun.COM 			 * If this ring is part of the rings asked by the
595711878SVenu.Iyer@Sun.COM 			 * share we cannot use it for swapping.
595811878SVenu.Iyer@Sun.COM 			 */
595911878SVenu.Iyer@Sun.COM 			for (j = 0; j < nrings; j++) {
596011878SVenu.Iyer@Sun.COM 				if (rings[j] == tring)
596111878SVenu.Iyer@Sun.COM 					break;
59628400SNicolas.Droux@Sun.COM 			}
596311878SVenu.Iyer@Sun.COM 			if (j >= nrings)
596411878SVenu.Iyer@Sun.COM 				break;
596511878SVenu.Iyer@Sun.COM 		}
596611878SVenu.Iyer@Sun.COM 		if (tring == NULL) {
596711878SVenu.Iyer@Sun.COM 			mac_tx_client_restart((mac_client_handle_t)mcip);
596811878SVenu.Iyer@Sun.COM 			return (ENOSPC);
596911878SVenu.Iyer@Sun.COM 		}
597011878SVenu.Iyer@Sun.COM 		if (mac_group_mov_ring(mip, group, tring) != 0) {
597111878SVenu.Iyer@Sun.COM 			mac_tx_client_restart((mac_client_handle_t)mcip);
597211878SVenu.Iyer@Sun.COM 			return (ENOSPC);
59738275SEric Cheng 		}
597411878SVenu.Iyer@Sun.COM 		if (mac_group_mov_ring(mip, defgrp, ring) != 0) {
597511878SVenu.Iyer@Sun.COM 			(void) mac_group_mov_ring(mip, defgrp, tring);
597611878SVenu.Iyer@Sun.COM 			mac_tx_client_restart((mac_client_handle_t)mcip);
597711878SVenu.Iyer@Sun.COM 			return (ENOSPC);
597811878SVenu.Iyer@Sun.COM 		}
597911878SVenu.Iyer@Sun.COM 	}
598011878SVenu.Iyer@Sun.COM 	mac_tx_client_restart((mac_client_handle_t)mcip);
598111878SVenu.Iyer@Sun.COM 	ASSERT(ring->mr_gh == (mac_group_handle_t)defgrp);
598211878SVenu.Iyer@Sun.COM 	return (0);
598311878SVenu.Iyer@Sun.COM }
59848275SEric Cheng 
59858275SEric Cheng /*
59868275SEric Cheng  * Populate a zero-ring group with rings. If the share is non-NULL,
59878275SEric Cheng  * the rings are chosen according to that share.
59888275SEric Cheng  * Invoked after allocating a new RX or TX group through
59898275SEric Cheng  * mac_reserve_rx_group() or mac_reserve_tx_group(), respectively.
59908275SEric Cheng  * Returns zero on success, an errno otherwise.
59918275SEric Cheng  */
59928275SEric Cheng int
i_mac_group_allocate_rings(mac_impl_t * mip,mac_ring_type_t ring_type,mac_group_t * src_group,mac_group_t * new_group,mac_share_handle_t share,uint32_t ringcnt)59938275SEric Cheng i_mac_group_allocate_rings(mac_impl_t *mip, mac_ring_type_t ring_type,
599411878SVenu.Iyer@Sun.COM     mac_group_t *src_group, mac_group_t *new_group, mac_share_handle_t share,
599511878SVenu.Iyer@Sun.COM     uint32_t ringcnt)
599611878SVenu.Iyer@Sun.COM {
599711878SVenu.Iyer@Sun.COM 	mac_ring_t **rings, *ring;
59988275SEric Cheng 	uint_t nrings;
599911878SVenu.Iyer@Sun.COM 	int rv = 0, i = 0, j;
600011878SVenu.Iyer@Sun.COM 
600111878SVenu.Iyer@Sun.COM 	ASSERT((ring_type == MAC_RING_TYPE_RX &&
600211878SVenu.Iyer@Sun.COM 	    mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) ||
600311878SVenu.Iyer@Sun.COM 	    (ring_type == MAC_RING_TYPE_TX &&
600411878SVenu.Iyer@Sun.COM 	    mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC));
60058275SEric Cheng 
60068275SEric Cheng 	/*
60078275SEric Cheng 	 * First find the rings to allocate to the group.
60088275SEric Cheng 	 */
60098275SEric Cheng 	if (share != NULL) {
60108275SEric Cheng 		/* get rings through ms_squery() */
60118275SEric Cheng 		mip->mi_share_capab.ms_squery(share, ring_type, NULL, &nrings);
60128275SEric Cheng 		ASSERT(nrings != 0);
60138275SEric Cheng 		rings = kmem_alloc(nrings * sizeof (mac_ring_handle_t),
60148275SEric Cheng 		    KM_SLEEP);
60158275SEric Cheng 		mip->mi_share_capab.ms_squery(share, ring_type,
60168275SEric Cheng 		    (mac_ring_handle_t *)rings, &nrings);
601711878SVenu.Iyer@Sun.COM 		for (i = 0; i < nrings; i++) {
601811878SVenu.Iyer@Sun.COM 			/*
601911878SVenu.Iyer@Sun.COM 			 * If we have given this ring to a non-default
602011878SVenu.Iyer@Sun.COM 			 * group, we need to check if we can get this
602111878SVenu.Iyer@Sun.COM 			 * ring.
602211878SVenu.Iyer@Sun.COM 			 */
602311878SVenu.Iyer@Sun.COM 			ring = rings[i];
602411878SVenu.Iyer@Sun.COM 			if (ring->mr_gh != (mac_group_handle_t)src_group ||
602511878SVenu.Iyer@Sun.COM 			    ring == (mac_ring_t *)mip->mi_default_tx_ring) {
602611878SVenu.Iyer@Sun.COM 				if (mac_reclaim_ring_from_grp(mip, ring_type,
602711878SVenu.Iyer@Sun.COM 				    ring, rings, nrings) != 0) {
602811878SVenu.Iyer@Sun.COM 					rv = ENOSPC;
602911878SVenu.Iyer@Sun.COM 					goto bail;
603011878SVenu.Iyer@Sun.COM 				}
603111878SVenu.Iyer@Sun.COM 			}
603211878SVenu.Iyer@Sun.COM 		}
60338275SEric Cheng 	} else {
60348275SEric Cheng 		/*
60358275SEric Cheng 		 * Pick one ring from default group.
60368275SEric Cheng 		 *
60378275SEric Cheng 		 * for now pick the second ring which requires the first ring
60388275SEric Cheng 		 * at index 0 to stay in the default group, since it is the
60398275SEric Cheng 		 * ring which carries the multicast traffic.
60408275SEric Cheng 		 * We need a better way for a driver to indicate this,
60418275SEric Cheng 		 * for example a per-ring flag.
60428275SEric Cheng 		 */
604311878SVenu.Iyer@Sun.COM 		rings = kmem_alloc(ringcnt * sizeof (mac_ring_handle_t),
604411878SVenu.Iyer@Sun.COM 		    KM_SLEEP);
60458275SEric Cheng 		for (ring = src_group->mrg_rings; ring != NULL;
60468275SEric Cheng 		    ring = ring->mr_next) {
604711878SVenu.Iyer@Sun.COM 			if (ring_type == MAC_RING_TYPE_RX &&
604811878SVenu.Iyer@Sun.COM 			    ring->mr_index == 0) {
604911878SVenu.Iyer@Sun.COM 				continue;
605011878SVenu.Iyer@Sun.COM 			}
605111878SVenu.Iyer@Sun.COM 			if (ring_type == MAC_RING_TYPE_TX &&
605211878SVenu.Iyer@Sun.COM 			    ring == (mac_ring_t *)mip->mi_default_tx_ring) {
605311878SVenu.Iyer@Sun.COM 				continue;
605411878SVenu.Iyer@Sun.COM 			}
605511878SVenu.Iyer@Sun.COM 			rings[i++] = ring;
605611878SVenu.Iyer@Sun.COM 			if (i == ringcnt)
60578275SEric Cheng 				break;
60588275SEric Cheng 		}
60598275SEric Cheng 		ASSERT(ring != NULL);
606011878SVenu.Iyer@Sun.COM 		nrings = i;
606111878SVenu.Iyer@Sun.COM 		/* Not enough rings as required */
606211878SVenu.Iyer@Sun.COM 		if (nrings != ringcnt) {
606311878SVenu.Iyer@Sun.COM 			rv = ENOSPC;
606411878SVenu.Iyer@Sun.COM 			goto bail;
606511878SVenu.Iyer@Sun.COM 		}
60668275SEric Cheng 	}
60678275SEric Cheng 
60688275SEric Cheng 	switch (ring_type) {
60698275SEric Cheng 	case MAC_RING_TYPE_RX:
607011878SVenu.Iyer@Sun.COM 		if (src_group->mrg_cur_count - nrings < 1) {
60718275SEric Cheng 			/* we ran out of rings */
607211878SVenu.Iyer@Sun.COM 			rv = ENOSPC;
607311878SVenu.Iyer@Sun.COM 			goto bail;
60748275SEric Cheng 		}
60758275SEric Cheng 
60768275SEric Cheng 		/* move receive rings to new group */
60778275SEric Cheng 		for (i = 0; i < nrings; i++) {
60788275SEric Cheng 			rv = mac_group_mov_ring(mip, new_group, rings[i]);
60798275SEric Cheng 			if (rv != 0) {
60808275SEric Cheng 				/* move rings back on failure */
60818275SEric Cheng 				for (j = 0; j < i; j++) {
60828275SEric Cheng 					(void) mac_group_mov_ring(mip,
60838275SEric Cheng 					    src_group, rings[j]);
60848275SEric Cheng 				}
608511878SVenu.Iyer@Sun.COM 				goto bail;
60868275SEric Cheng 			}
60878275SEric Cheng 		}
60888275SEric Cheng 		break;
60898275SEric Cheng 
60908275SEric Cheng 	case MAC_RING_TYPE_TX: {
60918275SEric Cheng 		mac_ring_t *tmp_ring;
60928275SEric Cheng 
60938275SEric Cheng 		/* move the TX rings to the new group */
60948275SEric Cheng 		for (i = 0; i < nrings; i++) {
60958275SEric Cheng 			/* get the desired ring */
60968275SEric Cheng 			tmp_ring = mac_reserve_tx_ring(mip, rings[i]);
609711878SVenu.Iyer@Sun.COM 			if (tmp_ring == NULL) {
609811878SVenu.Iyer@Sun.COM 				rv = ENOSPC;
609911878SVenu.Iyer@Sun.COM 				goto bail;
610011878SVenu.Iyer@Sun.COM 			}
61018275SEric Cheng 			ASSERT(tmp_ring == rings[i]);
61028275SEric Cheng 			rv = mac_group_mov_ring(mip, new_group, rings[i]);
61038275SEric Cheng 			if (rv != 0) {
61048275SEric Cheng 				/* cleanup on failure */
61058275SEric Cheng 				for (j = 0; j < i; j++) {
61068275SEric Cheng 					(void) mac_group_mov_ring(mip,
610711878SVenu.Iyer@Sun.COM 					    MAC_DEFAULT_TX_GROUP(mip),
610811878SVenu.Iyer@Sun.COM 					    rings[j]);
61098275SEric Cheng 				}
611011878SVenu.Iyer@Sun.COM 				goto bail;
61118275SEric Cheng 			}
61128275SEric Cheng 		}
61138275SEric Cheng 		break;
61148275SEric Cheng 	}
61158275SEric Cheng 	}
61168275SEric Cheng 
611711878SVenu.Iyer@Sun.COM 	/* add group to share */
611811878SVenu.Iyer@Sun.COM 	if (share != NULL)
61198275SEric Cheng 		mip->mi_share_capab.ms_sadd(share, new_group->mrg_driver);
612011878SVenu.Iyer@Sun.COM 
612111878SVenu.Iyer@Sun.COM bail:
612211878SVenu.Iyer@Sun.COM 	/* free temporary array of rings */
612311878SVenu.Iyer@Sun.COM 	kmem_free(rings, nrings * sizeof (mac_ring_handle_t));
612411878SVenu.Iyer@Sun.COM 
612511878SVenu.Iyer@Sun.COM 	return (rv);
61268275SEric Cheng }
61278275SEric Cheng 
61288275SEric Cheng void
mac_group_add_client(mac_group_t * grp,mac_client_impl_t * mcip)612911878SVenu.Iyer@Sun.COM mac_group_add_client(mac_group_t *grp, mac_client_impl_t *mcip)
61308275SEric Cheng {
61318275SEric Cheng 	mac_grp_client_t *mgcp;
61328275SEric Cheng 
61338275SEric Cheng 	for (mgcp = grp->mrg_clients; mgcp != NULL; mgcp = mgcp->mgc_next) {
61348275SEric Cheng 		if (mgcp->mgc_client == mcip)
61358275SEric Cheng 			break;
61368275SEric Cheng 	}
61378275SEric Cheng 
61388275SEric Cheng 	VERIFY(mgcp == NULL);
61398275SEric Cheng 
61408275SEric Cheng 	mgcp = kmem_zalloc(sizeof (mac_grp_client_t), KM_SLEEP);
61418275SEric Cheng 	mgcp->mgc_client = mcip;
61428275SEric Cheng 	mgcp->mgc_next = grp->mrg_clients;
61438275SEric Cheng 	grp->mrg_clients = mgcp;
61448275SEric Cheng 
61458275SEric Cheng }
61468275SEric Cheng 
61478275SEric Cheng void
mac_group_remove_client(mac_group_t * grp,mac_client_impl_t * mcip)614811878SVenu.Iyer@Sun.COM mac_group_remove_client(mac_group_t *grp, mac_client_impl_t *mcip)
61498275SEric Cheng {
61508275SEric Cheng 	mac_grp_client_t *mgcp, **pprev;
61518275SEric Cheng 
61528275SEric Cheng 	for (pprev = &grp->mrg_clients, mgcp = *pprev; mgcp != NULL;
61538275SEric Cheng 	    pprev = &mgcp->mgc_next, mgcp = *pprev) {
61548275SEric Cheng 		if (mgcp->mgc_client == mcip)
61558275SEric Cheng 			break;
61568275SEric Cheng 	}
61578275SEric Cheng 
61588275SEric Cheng 	ASSERT(mgcp != NULL);
61598275SEric Cheng 
61608275SEric Cheng 	*pprev = mgcp->mgc_next;
61618275SEric Cheng 	kmem_free(mgcp, sizeof (mac_grp_client_t));
61628275SEric Cheng }
61638275SEric Cheng 
61648275SEric Cheng /*
61658275SEric Cheng  * mac_reserve_rx_group()
61668275SEric Cheng  *
61678275SEric Cheng  * Finds an available group and exclusively reserves it for a client.
61688275SEric Cheng  * The group is chosen to suit the flow's resource controls (bandwidth and
61698275SEric Cheng  * fanout requirements) and the address type.
61708275SEric Cheng  * If the requestor is the pimary MAC then return the group with the
61718275SEric Cheng  * largest number of rings, otherwise the default ring when available.
61728275SEric Cheng  */
61738275SEric Cheng mac_group_t *
mac_reserve_rx_group(mac_client_impl_t * mcip,uint8_t * mac_addr,boolean_t move)617411878SVenu.Iyer@Sun.COM mac_reserve_rx_group(mac_client_impl_t *mcip, uint8_t *mac_addr, boolean_t move)
61758275SEric Cheng {
61768275SEric Cheng 	mac_share_handle_t	share = mcip->mci_share;
61778275SEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
61788275SEric Cheng 	mac_group_t		*grp = NULL;
617911878SVenu.Iyer@Sun.COM 	int			i;
618011878SVenu.Iyer@Sun.COM 	int			err = 0;
61818275SEric Cheng 	mac_address_t		*map;
618211878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
618311878SVenu.Iyer@Sun.COM 	int			nrings;
618411878SVenu.Iyer@Sun.COM 	int			donor_grp_rcnt;
618511878SVenu.Iyer@Sun.COM 	boolean_t		need_exclgrp = B_FALSE;
618611878SVenu.Iyer@Sun.COM 	int			need_rings = 0;
618711878SVenu.Iyer@Sun.COM 	mac_group_t		*candidate_grp = NULL;
618811878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*gclient;
618911878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*gmrp;
619011878SVenu.Iyer@Sun.COM 	mac_group_t		*donorgrp = NULL;
619111878SVenu.Iyer@Sun.COM 	boolean_t		rxhw = mrp->mrp_mask & MRP_RX_RINGS;
619211878SVenu.Iyer@Sun.COM 	boolean_t		unspec = mrp->mrp_mask & MRP_RXRINGS_UNSPEC;
619311878SVenu.Iyer@Sun.COM 	boolean_t		isprimary;
61948275SEric Cheng 
61958275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
61968275SEric Cheng 
619711878SVenu.Iyer@Sun.COM 	isprimary = mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC;
619811878SVenu.Iyer@Sun.COM 
619911878SVenu.Iyer@Sun.COM 	/*
620011878SVenu.Iyer@Sun.COM 	 * Check if a group already has this mac address (case of VLANs)
620111878SVenu.Iyer@Sun.COM 	 * unless we are moving this MAC client from one group to another.
620211878SVenu.Iyer@Sun.COM 	 */
620311878SVenu.Iyer@Sun.COM 	if (!move && (map = mac_find_macaddr(mip, mac_addr)) != NULL) {
620411878SVenu.Iyer@Sun.COM 		if (map->ma_group != NULL)
620511878SVenu.Iyer@Sun.COM 			return (map->ma_group);
620611878SVenu.Iyer@Sun.COM 	}
620711878SVenu.Iyer@Sun.COM 	if (mip->mi_rx_groups == NULL || mip->mi_rx_group_count == 0)
620811878SVenu.Iyer@Sun.COM 		return (NULL);
620911878SVenu.Iyer@Sun.COM 	/*
621011878SVenu.Iyer@Sun.COM 	 * If exclusive open, return NULL which will enable the
621111878SVenu.Iyer@Sun.COM 	 * caller to use the default group.
621211878SVenu.Iyer@Sun.COM 	 */
621311878SVenu.Iyer@Sun.COM 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE)
621411878SVenu.Iyer@Sun.COM 		return (NULL);
621511878SVenu.Iyer@Sun.COM 
621611878SVenu.Iyer@Sun.COM 	/* For dynamic groups default unspecified to 1 */
621711878SVenu.Iyer@Sun.COM 	if (rxhw && unspec &&
621811878SVenu.Iyer@Sun.COM 	    mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
621911878SVenu.Iyer@Sun.COM 		mrp->mrp_nrxrings = 1;
622011878SVenu.Iyer@Sun.COM 	}
622111878SVenu.Iyer@Sun.COM 	/*
622211878SVenu.Iyer@Sun.COM 	 * For static grouping we allow only specifying rings=0 and
622311878SVenu.Iyer@Sun.COM 	 * unspecified
622411878SVenu.Iyer@Sun.COM 	 */
622511878SVenu.Iyer@Sun.COM 	if (rxhw && mrp->mrp_nrxrings > 0 &&
622611878SVenu.Iyer@Sun.COM 	    mip->mi_rx_group_type == MAC_GROUP_TYPE_STATIC) {
62278275SEric Cheng 		return (NULL);
622811878SVenu.Iyer@Sun.COM 	}
622911878SVenu.Iyer@Sun.COM 	if (rxhw) {
623011878SVenu.Iyer@Sun.COM 		/*
623111878SVenu.Iyer@Sun.COM 		 * We have explicitly asked for a group (with nrxrings,
623211878SVenu.Iyer@Sun.COM 		 * if unspec).
623311878SVenu.Iyer@Sun.COM 		 */
623411878SVenu.Iyer@Sun.COM 		if (unspec || mrp->mrp_nrxrings > 0) {
623511878SVenu.Iyer@Sun.COM 			need_exclgrp = B_TRUE;
623611878SVenu.Iyer@Sun.COM 			need_rings = mrp->mrp_nrxrings;
623711878SVenu.Iyer@Sun.COM 		} else if (mrp->mrp_nrxrings == 0) {
623811878SVenu.Iyer@Sun.COM 			/*
623911878SVenu.Iyer@Sun.COM 			 * We have asked for a software group.
624011878SVenu.Iyer@Sun.COM 			 */
624111878SVenu.Iyer@Sun.COM 			return (NULL);
624211878SVenu.Iyer@Sun.COM 		}
624311878SVenu.Iyer@Sun.COM 	} else if (isprimary && mip->mi_nactiveclients == 1 &&
624411878SVenu.Iyer@Sun.COM 	    mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
624511878SVenu.Iyer@Sun.COM 		/*
624611878SVenu.Iyer@Sun.COM 		 * If the primary is the only active client on this
624711878SVenu.Iyer@Sun.COM 		 * mip and we have not asked for any rings, we give
624811878SVenu.Iyer@Sun.COM 		 * it the default group so that the primary gets to
624911878SVenu.Iyer@Sun.COM 		 * use all the rings.
625011878SVenu.Iyer@Sun.COM 		 */
625111878SVenu.Iyer@Sun.COM 		return (NULL);
625211878SVenu.Iyer@Sun.COM 	}
625311878SVenu.Iyer@Sun.COM 
625411878SVenu.Iyer@Sun.COM 	/* The group that can donate rings */
625511878SVenu.Iyer@Sun.COM 	donorgrp = mip->mi_rx_donor_grp;
625611878SVenu.Iyer@Sun.COM 
625711878SVenu.Iyer@Sun.COM 	/*
625811878SVenu.Iyer@Sun.COM 	 * The number of rings that the default group can donate.
625911878SVenu.Iyer@Sun.COM 	 * We need to leave at least one ring.
626011878SVenu.Iyer@Sun.COM 	 */
626111878SVenu.Iyer@Sun.COM 	donor_grp_rcnt = donorgrp->mrg_cur_count - 1;
62628275SEric Cheng 
62638275SEric Cheng 	/*
62648275SEric Cheng 	 * Try to exclusively reserve a RX group.
62658275SEric Cheng 	 *
626611878SVenu.Iyer@Sun.COM 	 * For flows requiring HW_DEFAULT_RING (unicast flow of the primary
626711878SVenu.Iyer@Sun.COM 	 * client), try to reserve the a non-default RX group and give
626811878SVenu.Iyer@Sun.COM 	 * it all the rings from the donor group, except the default ring
62698275SEric Cheng 	 *
627011878SVenu.Iyer@Sun.COM 	 * For flows requiring HW_RING (unicast flow of other clients), try
627111878SVenu.Iyer@Sun.COM 	 * to reserve non-default RX group with the specified number of
627211878SVenu.Iyer@Sun.COM 	 * rings, if available.
62738275SEric Cheng 	 *
627411878SVenu.Iyer@Sun.COM 	 * For flows that have not asked for software or hardware ring,
627511878SVenu.Iyer@Sun.COM 	 * try to reserve a non-default group with 1 ring, if available.
62768275SEric Cheng 	 */
627711878SVenu.Iyer@Sun.COM 	for (i = 1; i < mip->mi_rx_group_count; i++) {
627811878SVenu.Iyer@Sun.COM 		grp = &mip->mi_rx_groups[i];
62798275SEric Cheng 
62808275SEric Cheng 		DTRACE_PROBE3(rx__group__trying, char *, mip->mi_name,
62818275SEric Cheng 		    int, grp->mrg_index, mac_group_state_t, grp->mrg_state);
62828275SEric Cheng 
62838275SEric Cheng 		/*
628411878SVenu.Iyer@Sun.COM 		 * Check if this group could be a candidate group for
628511878SVenu.Iyer@Sun.COM 		 * eviction if we need a group for this MAC client,
628611878SVenu.Iyer@Sun.COM 		 * but there aren't any. A candidate group is one
628711878SVenu.Iyer@Sun.COM 		 * that didn't ask for an exclusive group, but got
628811878SVenu.Iyer@Sun.COM 		 * one and it has enough rings (combined with what
628911878SVenu.Iyer@Sun.COM 		 * the donor group can donate) for the new MAC
629011878SVenu.Iyer@Sun.COM 		 * client
62918275SEric Cheng 		 */
629211878SVenu.Iyer@Sun.COM 		if (grp->mrg_state >= MAC_GROUP_STATE_RESERVED) {
629311878SVenu.Iyer@Sun.COM 			/*
629411878SVenu.Iyer@Sun.COM 			 * If the primary/donor group is not the default
629511878SVenu.Iyer@Sun.COM 			 * group, don't bother looking for a candidate group.
629611878SVenu.Iyer@Sun.COM 			 * If we don't have enough rings we will check
629711878SVenu.Iyer@Sun.COM 			 * if the primary group can be vacated.
629811878SVenu.Iyer@Sun.COM 			 */
629911878SVenu.Iyer@Sun.COM 			if (candidate_grp == NULL &&
630011878SVenu.Iyer@Sun.COM 			    donorgrp == MAC_DEFAULT_RX_GROUP(mip)) {
630111878SVenu.Iyer@Sun.COM 				ASSERT(!MAC_GROUP_NO_CLIENT(grp));
630211878SVenu.Iyer@Sun.COM 				gclient = MAC_GROUP_ONLY_CLIENT(grp);
630311878SVenu.Iyer@Sun.COM 				if (gclient == NULL)
630411878SVenu.Iyer@Sun.COM 					gclient = mac_get_grp_primary(grp);
630511878SVenu.Iyer@Sun.COM 				ASSERT(gclient != NULL);
630611878SVenu.Iyer@Sun.COM 				gmrp = MCIP_RESOURCE_PROPS(gclient);
630711878SVenu.Iyer@Sun.COM 				if (gclient->mci_share == NULL &&
630811878SVenu.Iyer@Sun.COM 				    (gmrp->mrp_mask & MRP_RX_RINGS) == 0 &&
630911878SVenu.Iyer@Sun.COM 				    (unspec ||
631011878SVenu.Iyer@Sun.COM 				    (grp->mrg_cur_count + donor_grp_rcnt >=
631111878SVenu.Iyer@Sun.COM 				    need_rings))) {
631211878SVenu.Iyer@Sun.COM 					candidate_grp = grp;
631311878SVenu.Iyer@Sun.COM 				}
631411878SVenu.Iyer@Sun.COM 			}
63158275SEric Cheng 			continue;
63168275SEric Cheng 		}
63178275SEric Cheng 		/*
63188275SEric Cheng 		 * This group could already be SHARED by other multicast
63198275SEric Cheng 		 * flows on this client. In that case, the group would
63208275SEric Cheng 		 * be shared and has already been started.
63218275SEric Cheng 		 */
63228275SEric Cheng 		ASSERT(grp->mrg_state != MAC_GROUP_STATE_UNINIT);
63238275SEric Cheng 
63248275SEric Cheng 		if ((grp->mrg_state == MAC_GROUP_STATE_REGISTERED) &&
63258275SEric Cheng 		    (mac_start_group(grp) != 0)) {
63268275SEric Cheng 			continue;
63278275SEric Cheng 		}
63288275SEric Cheng 
632911878SVenu.Iyer@Sun.COM 		if (mip->mi_rx_group_type != MAC_GROUP_TYPE_DYNAMIC)
63308275SEric Cheng 			break;
63318275SEric Cheng 		ASSERT(grp->mrg_cur_count == 0);
63328275SEric Cheng 
63338275SEric Cheng 		/*
63348275SEric Cheng 		 * Populate the group. Rings should be taken
633511878SVenu.Iyer@Sun.COM 		 * from the donor group.
633611878SVenu.Iyer@Sun.COM 		 */
633711878SVenu.Iyer@Sun.COM 		nrings = rxhw ? need_rings : isprimary ? donor_grp_rcnt: 1;
633811878SVenu.Iyer@Sun.COM 
633911878SVenu.Iyer@Sun.COM 		/*
634011878SVenu.Iyer@Sun.COM 		 * If the donor group can't donate, let's just walk and
634111878SVenu.Iyer@Sun.COM 		 * see if someone can vacate a group, so that we have
634211878SVenu.Iyer@Sun.COM 		 * enough rings for this, unless we already have
634311878SVenu.Iyer@Sun.COM 		 * identified a candiate group..
63448275SEric Cheng 		 */
634511878SVenu.Iyer@Sun.COM 		if (nrings <= donor_grp_rcnt) {
634611878SVenu.Iyer@Sun.COM 			err = i_mac_group_allocate_rings(mip, MAC_RING_TYPE_RX,
634711878SVenu.Iyer@Sun.COM 			    donorgrp, grp, share, nrings);
634811878SVenu.Iyer@Sun.COM 			if (err == 0) {
634911878SVenu.Iyer@Sun.COM 				/*
635011878SVenu.Iyer@Sun.COM 				 * For a share i_mac_group_allocate_rings gets
635111878SVenu.Iyer@Sun.COM 				 * the rings from the driver, let's populate
635211878SVenu.Iyer@Sun.COM 				 * the property for the client now.
635311878SVenu.Iyer@Sun.COM 				 */
635411878SVenu.Iyer@Sun.COM 				if (share != NULL) {
635511878SVenu.Iyer@Sun.COM 					mac_client_set_rings(
635611878SVenu.Iyer@Sun.COM 					    (mac_client_handle_t)mcip,
635711878SVenu.Iyer@Sun.COM 					    grp->mrg_cur_count, -1);
635811878SVenu.Iyer@Sun.COM 				}
635911878SVenu.Iyer@Sun.COM 				if (mac_is_primary_client(mcip) && !rxhw)
636011878SVenu.Iyer@Sun.COM 					mip->mi_rx_donor_grp = grp;
636111878SVenu.Iyer@Sun.COM 				break;
636211878SVenu.Iyer@Sun.COM 			}
636311878SVenu.Iyer@Sun.COM 		}
63648275SEric Cheng 
63658275SEric Cheng 		DTRACE_PROBE3(rx__group__reserve__alloc__rings, char *,
63668275SEric Cheng 		    mip->mi_name, int, grp->mrg_index, int, err);
63678275SEric Cheng 
63688275SEric Cheng 		/*
636911878SVenu.Iyer@Sun.COM 		 * It's a dynamic group but the grouping operation
637011878SVenu.Iyer@Sun.COM 		 * failed.
63718275SEric Cheng 		 */
63728275SEric Cheng 		mac_stop_group(grp);
63738275SEric Cheng 	}
637411878SVenu.Iyer@Sun.COM 	/* We didn't find an exclusive group for this MAC client */
637511878SVenu.Iyer@Sun.COM 	if (i >= mip->mi_rx_group_count) {
637611878SVenu.Iyer@Sun.COM 
637711878SVenu.Iyer@Sun.COM 		if (!need_exclgrp)
637811878SVenu.Iyer@Sun.COM 			return (NULL);
637911878SVenu.Iyer@Sun.COM 
638011878SVenu.Iyer@Sun.COM 		/*
638111878SVenu.Iyer@Sun.COM 		 * If we found a candidate group then we switch the
638211878SVenu.Iyer@Sun.COM 		 * MAC client from the candidate_group to the default
638311878SVenu.Iyer@Sun.COM 		 * group and give the group to this MAC client. If
638411878SVenu.Iyer@Sun.COM 		 * we didn't find a candidate_group, check if the
638511878SVenu.Iyer@Sun.COM 		 * primary is in its own group and if it can make way
638611878SVenu.Iyer@Sun.COM 		 * for this MAC client.
638711878SVenu.Iyer@Sun.COM 		 */
638811878SVenu.Iyer@Sun.COM 		if (candidate_grp == NULL &&
638911878SVenu.Iyer@Sun.COM 		    donorgrp != MAC_DEFAULT_RX_GROUP(mip) &&
639011878SVenu.Iyer@Sun.COM 		    donorgrp->mrg_cur_count >= need_rings) {
639111878SVenu.Iyer@Sun.COM 			candidate_grp = donorgrp;
639211878SVenu.Iyer@Sun.COM 		}
639311878SVenu.Iyer@Sun.COM 		if (candidate_grp != NULL) {
639411878SVenu.Iyer@Sun.COM 			boolean_t	prim_grp = B_FALSE;
639511878SVenu.Iyer@Sun.COM 
639611878SVenu.Iyer@Sun.COM 			/*
639711878SVenu.Iyer@Sun.COM 			 * Switch the MAC client from the candidate group
639811878SVenu.Iyer@Sun.COM 			 * to the default group.. If this group was the
639911878SVenu.Iyer@Sun.COM 			 * donor group, then after the switch we need
640011878SVenu.Iyer@Sun.COM 			 * to update the donor group too.
640111878SVenu.Iyer@Sun.COM 			 */
640211878SVenu.Iyer@Sun.COM 			grp = candidate_grp;
640311878SVenu.Iyer@Sun.COM 			gclient = MAC_GROUP_ONLY_CLIENT(grp);
640411878SVenu.Iyer@Sun.COM 			if (gclient == NULL)
640511878SVenu.Iyer@Sun.COM 				gclient = mac_get_grp_primary(grp);
640611878SVenu.Iyer@Sun.COM 			if (grp == mip->mi_rx_donor_grp)
640711878SVenu.Iyer@Sun.COM 				prim_grp = B_TRUE;
640811878SVenu.Iyer@Sun.COM 			if (mac_rx_switch_group(gclient, grp,
640911878SVenu.Iyer@Sun.COM 			    MAC_DEFAULT_RX_GROUP(mip)) != 0) {
641011878SVenu.Iyer@Sun.COM 				return (NULL);
641111878SVenu.Iyer@Sun.COM 			}
641211878SVenu.Iyer@Sun.COM 			if (prim_grp) {
641311878SVenu.Iyer@Sun.COM 				mip->mi_rx_donor_grp =
641411878SVenu.Iyer@Sun.COM 				    MAC_DEFAULT_RX_GROUP(mip);
641511878SVenu.Iyer@Sun.COM 				donorgrp = MAC_DEFAULT_RX_GROUP(mip);
641611878SVenu.Iyer@Sun.COM 			}
641711878SVenu.Iyer@Sun.COM 
641811878SVenu.Iyer@Sun.COM 
641911878SVenu.Iyer@Sun.COM 			/*
642011878SVenu.Iyer@Sun.COM 			 * Now give this group with the required rings
642111878SVenu.Iyer@Sun.COM 			 * to this MAC client.
642211878SVenu.Iyer@Sun.COM 			 */
642311878SVenu.Iyer@Sun.COM 			ASSERT(grp->mrg_state == MAC_GROUP_STATE_REGISTERED);
642411878SVenu.Iyer@Sun.COM 			if (mac_start_group(grp) != 0)
642511878SVenu.Iyer@Sun.COM 				return (NULL);
642611878SVenu.Iyer@Sun.COM 
642711878SVenu.Iyer@Sun.COM 			if (mip->mi_rx_group_type != MAC_GROUP_TYPE_DYNAMIC)
642811878SVenu.Iyer@Sun.COM 				return (grp);
642911878SVenu.Iyer@Sun.COM 
643011878SVenu.Iyer@Sun.COM 			donor_grp_rcnt = donorgrp->mrg_cur_count - 1;
643111878SVenu.Iyer@Sun.COM 			ASSERT(grp->mrg_cur_count == 0);
643211878SVenu.Iyer@Sun.COM 			ASSERT(donor_grp_rcnt >= need_rings);
643311878SVenu.Iyer@Sun.COM 			err = i_mac_group_allocate_rings(mip, MAC_RING_TYPE_RX,
643411878SVenu.Iyer@Sun.COM 			    donorgrp, grp, share, need_rings);
643511878SVenu.Iyer@Sun.COM 			if (err == 0) {
643611878SVenu.Iyer@Sun.COM 				/*
643711878SVenu.Iyer@Sun.COM 				 * For a share i_mac_group_allocate_rings gets
643811878SVenu.Iyer@Sun.COM 				 * the rings from the driver, let's populate
643911878SVenu.Iyer@Sun.COM 				 * the property for the client now.
644011878SVenu.Iyer@Sun.COM 				 */
644111878SVenu.Iyer@Sun.COM 				if (share != NULL) {
644211878SVenu.Iyer@Sun.COM 					mac_client_set_rings(
644311878SVenu.Iyer@Sun.COM 					    (mac_client_handle_t)mcip,
644411878SVenu.Iyer@Sun.COM 					    grp->mrg_cur_count, -1);
644511878SVenu.Iyer@Sun.COM 				}
644611878SVenu.Iyer@Sun.COM 				DTRACE_PROBE2(rx__group__reserved,
644711878SVenu.Iyer@Sun.COM 				    char *, mip->mi_name, int, grp->mrg_index);
644811878SVenu.Iyer@Sun.COM 				return (grp);
644911878SVenu.Iyer@Sun.COM 			}
645011878SVenu.Iyer@Sun.COM 			DTRACE_PROBE3(rx__group__reserve__alloc__rings, char *,
645111878SVenu.Iyer@Sun.COM 			    mip->mi_name, int, grp->mrg_index, int, err);
645211878SVenu.Iyer@Sun.COM 			mac_stop_group(grp);
645311878SVenu.Iyer@Sun.COM 		}
64548275SEric Cheng 		return (NULL);
645511878SVenu.Iyer@Sun.COM 	}
64568275SEric Cheng 	ASSERT(grp != NULL);
64578275SEric Cheng 
64588275SEric Cheng 	DTRACE_PROBE2(rx__group__reserved,
64598275SEric Cheng 	    char *, mip->mi_name, int, grp->mrg_index);
64608275SEric Cheng 	return (grp);
64618275SEric Cheng }
64628275SEric Cheng 
64638275SEric Cheng /*
64648275SEric Cheng  * mac_rx_release_group()
64658275SEric Cheng  *
64668275SEric Cheng  * This is called when there are no clients left for the group.
64678275SEric Cheng  * The group is stopped and marked MAC_GROUP_STATE_REGISTERED,
64688275SEric Cheng  * and if it is a non default group, the shares are removed and
64698275SEric Cheng  * all rings are assigned back to default group.
64708275SEric Cheng  */
64718275SEric Cheng void
mac_release_rx_group(mac_client_impl_t * mcip,mac_group_t * group)64728275SEric Cheng mac_release_rx_group(mac_client_impl_t *mcip, mac_group_t *group)
64738275SEric Cheng {
647411878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
647511878SVenu.Iyer@Sun.COM 	mac_ring_t		*ring;
647611878SVenu.Iyer@Sun.COM 
647711878SVenu.Iyer@Sun.COM 	ASSERT(group != MAC_DEFAULT_RX_GROUP(mip));
647811878SVenu.Iyer@Sun.COM 
647911878SVenu.Iyer@Sun.COM 	if (mip->mi_rx_donor_grp == group)
648011878SVenu.Iyer@Sun.COM 		mip->mi_rx_donor_grp = MAC_DEFAULT_RX_GROUP(mip);
64818275SEric Cheng 
64828275SEric Cheng 	/*
64838275SEric Cheng 	 * This is the case where there are no clients left. Any
64848275SEric Cheng 	 * SRS etc on this group have also be quiesced.
64858275SEric Cheng 	 */
64868275SEric Cheng 	for (ring = group->mrg_rings; ring != NULL; ring = ring->mr_next) {
64878275SEric Cheng 		if (ring->mr_classify_type == MAC_HW_CLASSIFIER) {
64888275SEric Cheng 			ASSERT(group->mrg_state == MAC_GROUP_STATE_RESERVED);
64898275SEric Cheng 			/*
64908275SEric Cheng 			 * Remove the SRS associated with the HW ring.
64918275SEric Cheng 			 * As a result, polling will be disabled.
64928275SEric Cheng 			 */
64938275SEric Cheng 			ring->mr_srs = NULL;
64948275SEric Cheng 		}
649511878SVenu.Iyer@Sun.COM 		ASSERT(group->mrg_state < MAC_GROUP_STATE_RESERVED ||
649611878SVenu.Iyer@Sun.COM 		    ring->mr_state == MR_INUSE);
649711878SVenu.Iyer@Sun.COM 		if (ring->mr_state == MR_INUSE) {
649811878SVenu.Iyer@Sun.COM 			mac_stop_ring(ring);
649911878SVenu.Iyer@Sun.COM 			ring->mr_flag = 0;
650011878SVenu.Iyer@Sun.COM 		}
65018275SEric Cheng 	}
65028275SEric Cheng 
65038275SEric Cheng 	/* remove group from share */
65048275SEric Cheng 	if (mcip->mci_share != NULL) {
65058275SEric Cheng 		mip->mi_share_capab.ms_sremove(mcip->mci_share,
65068275SEric Cheng 		    group->mrg_driver);
65078275SEric Cheng 	}
65088275SEric Cheng 
65098275SEric Cheng 	if (mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
65108275SEric Cheng 		mac_ring_t *ring;
65118275SEric Cheng 
65128275SEric Cheng 		/*
65138275SEric Cheng 		 * Rings were dynamically allocated to group.
65148275SEric Cheng 		 * Move rings back to default group.
65158275SEric Cheng 		 */
65168275SEric Cheng 		while ((ring = group->mrg_rings) != NULL) {
651711878SVenu.Iyer@Sun.COM 			(void) mac_group_mov_ring(mip, mip->mi_rx_donor_grp,
651811878SVenu.Iyer@Sun.COM 			    ring);
65198275SEric Cheng 		}
65208275SEric Cheng 	}
65218275SEric Cheng 	mac_stop_group(group);
65228275SEric Cheng 	/*
65238275SEric Cheng 	 * Possible improvement: See if we can assign the group just released
65248275SEric Cheng 	 * to a another client of the mip
65258275SEric Cheng 	 */
65268275SEric Cheng }
65278275SEric Cheng 
65288275SEric Cheng /*
652911878SVenu.Iyer@Sun.COM  * When we move the primary's mac address between groups, we need to also
653011878SVenu.Iyer@Sun.COM  * take all the clients sharing the same mac address along with it (VLANs)
653111878SVenu.Iyer@Sun.COM  * We remove the mac address for such clients from the group after quiescing
653211878SVenu.Iyer@Sun.COM  * them. When we add the mac address we restart the client. Note that
653311878SVenu.Iyer@Sun.COM  * the primary's mac address is removed from the group after all the
653411878SVenu.Iyer@Sun.COM  * other clients sharing the address are removed. Similarly, the primary's
653511878SVenu.Iyer@Sun.COM  * mac address is added before all the other client's mac address are
653611878SVenu.Iyer@Sun.COM  * added. While grp is the group where the clients reside, tgrp is
653711878SVenu.Iyer@Sun.COM  * the group where the addresses have to be added.
653811878SVenu.Iyer@Sun.COM  */
653911878SVenu.Iyer@Sun.COM static void
mac_rx_move_macaddr_prim(mac_client_impl_t * mcip,mac_group_t * grp,mac_group_t * tgrp,uint8_t * maddr,boolean_t add)654011878SVenu.Iyer@Sun.COM mac_rx_move_macaddr_prim(mac_client_impl_t *mcip, mac_group_t *grp,
654111878SVenu.Iyer@Sun.COM     mac_group_t *tgrp, uint8_t *maddr, boolean_t add)
654211878SVenu.Iyer@Sun.COM {
654311878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
654411878SVenu.Iyer@Sun.COM 	mac_grp_client_t	*mgcp = grp->mrg_clients;
654511878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*gmcip;
654611878SVenu.Iyer@Sun.COM 	boolean_t		prim;
654711878SVenu.Iyer@Sun.COM 
654811878SVenu.Iyer@Sun.COM 	prim = (mcip->mci_state_flags & MCIS_UNICAST_HW) != 0;
654911878SVenu.Iyer@Sun.COM 
655011878SVenu.Iyer@Sun.COM 	/*
655111878SVenu.Iyer@Sun.COM 	 * If the clients are in a non-default group, we just have to
655211878SVenu.Iyer@Sun.COM 	 * walk the group's client list. If it is in the default group
655311878SVenu.Iyer@Sun.COM 	 * (which will be shared by other clients as well, we need to
655411878SVenu.Iyer@Sun.COM 	 * check if the unicast address matches mcip's unicast.
655511878SVenu.Iyer@Sun.COM 	 */
655611878SVenu.Iyer@Sun.COM 	while (mgcp != NULL) {
655711878SVenu.Iyer@Sun.COM 		gmcip = mgcp->mgc_client;
655811878SVenu.Iyer@Sun.COM 		if (gmcip != mcip &&
655911878SVenu.Iyer@Sun.COM 		    (grp != MAC_DEFAULT_RX_GROUP(mip) ||
656011878SVenu.Iyer@Sun.COM 		    mcip->mci_unicast == gmcip->mci_unicast)) {
656111878SVenu.Iyer@Sun.COM 			if (!add) {
656211878SVenu.Iyer@Sun.COM 				mac_rx_client_quiesce(
656311878SVenu.Iyer@Sun.COM 				    (mac_client_handle_t)gmcip);
656411878SVenu.Iyer@Sun.COM 				(void) mac_remove_macaddr(mcip->mci_unicast);
656511878SVenu.Iyer@Sun.COM 			} else {
656611878SVenu.Iyer@Sun.COM 				(void) mac_add_macaddr(mip, tgrp, maddr, prim);
656711878SVenu.Iyer@Sun.COM 				mac_rx_client_restart(
656811878SVenu.Iyer@Sun.COM 				    (mac_client_handle_t)gmcip);
656911878SVenu.Iyer@Sun.COM 			}
657011878SVenu.Iyer@Sun.COM 		}
657111878SVenu.Iyer@Sun.COM 		mgcp = mgcp->mgc_next;
657211878SVenu.Iyer@Sun.COM 	}
657311878SVenu.Iyer@Sun.COM }
657411878SVenu.Iyer@Sun.COM 
657511878SVenu.Iyer@Sun.COM 
657611878SVenu.Iyer@Sun.COM /*
657711878SVenu.Iyer@Sun.COM  * Move the MAC address from fgrp to tgrp. If this is the primary client,
657811878SVenu.Iyer@Sun.COM  * we need to take any VLANs etc. together too.
657911878SVenu.Iyer@Sun.COM  */
658011878SVenu.Iyer@Sun.COM static int
mac_rx_move_macaddr(mac_client_impl_t * mcip,mac_group_t * fgrp,mac_group_t * tgrp)658111878SVenu.Iyer@Sun.COM mac_rx_move_macaddr(mac_client_impl_t *mcip, mac_group_t *fgrp,
658211878SVenu.Iyer@Sun.COM     mac_group_t *tgrp)
658311878SVenu.Iyer@Sun.COM {
658411878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
658511878SVenu.Iyer@Sun.COM 	uint8_t			maddr[MAXMACADDRLEN];
658611878SVenu.Iyer@Sun.COM 	int			err = 0;
658711878SVenu.Iyer@Sun.COM 	boolean_t		prim;
658811878SVenu.Iyer@Sun.COM 	boolean_t		multiclnt = B_FALSE;
658911878SVenu.Iyer@Sun.COM 
659011878SVenu.Iyer@Sun.COM 	mac_rx_client_quiesce((mac_client_handle_t)mcip);
659111878SVenu.Iyer@Sun.COM 	ASSERT(mcip->mci_unicast != NULL);
659211878SVenu.Iyer@Sun.COM 	bcopy(mcip->mci_unicast->ma_addr, maddr, mcip->mci_unicast->ma_len);
659311878SVenu.Iyer@Sun.COM 
659411878SVenu.Iyer@Sun.COM 	prim = (mcip->mci_state_flags & MCIS_UNICAST_HW) != 0;
659511878SVenu.Iyer@Sun.COM 	if (mcip->mci_unicast->ma_nusers > 1) {
659611878SVenu.Iyer@Sun.COM 		mac_rx_move_macaddr_prim(mcip, fgrp, NULL, maddr, B_FALSE);
659711878SVenu.Iyer@Sun.COM 		multiclnt = B_TRUE;
659811878SVenu.Iyer@Sun.COM 	}
659911878SVenu.Iyer@Sun.COM 	ASSERT(mcip->mci_unicast->ma_nusers == 1);
660011878SVenu.Iyer@Sun.COM 	err = mac_remove_macaddr(mcip->mci_unicast);
660111878SVenu.Iyer@Sun.COM 	if (err != 0) {
660211878SVenu.Iyer@Sun.COM 		mac_rx_client_restart((mac_client_handle_t)mcip);
660311878SVenu.Iyer@Sun.COM 		if (multiclnt) {
660411878SVenu.Iyer@Sun.COM 			mac_rx_move_macaddr_prim(mcip, fgrp, fgrp, maddr,
660511878SVenu.Iyer@Sun.COM 			    B_TRUE);
660611878SVenu.Iyer@Sun.COM 		}
660711878SVenu.Iyer@Sun.COM 		return (err);
660811878SVenu.Iyer@Sun.COM 	}
660911878SVenu.Iyer@Sun.COM 	/*
661011878SVenu.Iyer@Sun.COM 	 * Program the H/W Classifier first, if this fails we need
661111878SVenu.Iyer@Sun.COM 	 * not proceed with the other stuff.
661211878SVenu.Iyer@Sun.COM 	 */
661311878SVenu.Iyer@Sun.COM 	if ((err = mac_add_macaddr(mip, tgrp, maddr, prim)) != 0) {
661411878SVenu.Iyer@Sun.COM 		/* Revert back the H/W Classifier */
661511878SVenu.Iyer@Sun.COM 		if ((err = mac_add_macaddr(mip, fgrp, maddr, prim)) != 0) {
661611878SVenu.Iyer@Sun.COM 			/*
661711878SVenu.Iyer@Sun.COM 			 * This should not fail now since it worked earlier,
661811878SVenu.Iyer@Sun.COM 			 * should we panic?
661911878SVenu.Iyer@Sun.COM 			 */
662011878SVenu.Iyer@Sun.COM 			cmn_err(CE_WARN,
662111878SVenu.Iyer@Sun.COM 			    "mac_rx_switch_group: switching %p back"
662211878SVenu.Iyer@Sun.COM 			    " to group %p failed!!", (void *)mcip,
662311878SVenu.Iyer@Sun.COM 			    (void *)fgrp);
662411878SVenu.Iyer@Sun.COM 		}
662511878SVenu.Iyer@Sun.COM 		mac_rx_client_restart((mac_client_handle_t)mcip);
662611878SVenu.Iyer@Sun.COM 		if (multiclnt) {
662711878SVenu.Iyer@Sun.COM 			mac_rx_move_macaddr_prim(mcip, fgrp, fgrp, maddr,
662811878SVenu.Iyer@Sun.COM 			    B_TRUE);
662911878SVenu.Iyer@Sun.COM 		}
663011878SVenu.Iyer@Sun.COM 		return (err);
663111878SVenu.Iyer@Sun.COM 	}
663211878SVenu.Iyer@Sun.COM 	mcip->mci_unicast = mac_find_macaddr(mip, maddr);
663311878SVenu.Iyer@Sun.COM 	mac_rx_client_restart((mac_client_handle_t)mcip);
663411878SVenu.Iyer@Sun.COM 	if (multiclnt)
663511878SVenu.Iyer@Sun.COM 		mac_rx_move_macaddr_prim(mcip, fgrp, tgrp, maddr, B_TRUE);
663611878SVenu.Iyer@Sun.COM 	return (err);
663711878SVenu.Iyer@Sun.COM }
663811878SVenu.Iyer@Sun.COM 
663911878SVenu.Iyer@Sun.COM /*
664011878SVenu.Iyer@Sun.COM  * Switch the MAC client from one group to another. This means we need
664111878SVenu.Iyer@Sun.COM  * to remove the MAC address from the group, remove the MAC client,
664211878SVenu.Iyer@Sun.COM  * teardown the SRSs and revert the group state. Then, we add the client
664311878SVenu.Iyer@Sun.COM  * to the destination group, set the SRSs, and add the MAC address to the
664411878SVenu.Iyer@Sun.COM  * group.
664511878SVenu.Iyer@Sun.COM  */
664611878SVenu.Iyer@Sun.COM int
mac_rx_switch_group(mac_client_impl_t * mcip,mac_group_t * fgrp,mac_group_t * tgrp)664711878SVenu.Iyer@Sun.COM mac_rx_switch_group(mac_client_impl_t *mcip, mac_group_t *fgrp,
664811878SVenu.Iyer@Sun.COM     mac_group_t *tgrp)
664911878SVenu.Iyer@Sun.COM {
665011878SVenu.Iyer@Sun.COM 	int			err;
665111878SVenu.Iyer@Sun.COM 	mac_group_state_t	next_state;
665211878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*group_only_mcip;
665311878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*gmcip;
665411878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
665511878SVenu.Iyer@Sun.COM 	mac_grp_client_t	*mgcp;
665611878SVenu.Iyer@Sun.COM 
665711878SVenu.Iyer@Sun.COM 	ASSERT(fgrp == mcip->mci_flent->fe_rx_ring_group);
665811878SVenu.Iyer@Sun.COM 
665911878SVenu.Iyer@Sun.COM 	if ((err = mac_rx_move_macaddr(mcip, fgrp, tgrp)) != 0)
666011878SVenu.Iyer@Sun.COM 		return (err);
666111878SVenu.Iyer@Sun.COM 
666211878SVenu.Iyer@Sun.COM 	/*
666311878SVenu.Iyer@Sun.COM 	 * The group might be reserved, but SRSs may not be set up, e.g.
666411878SVenu.Iyer@Sun.COM 	 * primary and its vlans using a reserved group.
666511878SVenu.Iyer@Sun.COM 	 */
666611878SVenu.Iyer@Sun.COM 	if (fgrp->mrg_state == MAC_GROUP_STATE_RESERVED &&
666711878SVenu.Iyer@Sun.COM 	    MAC_GROUP_ONLY_CLIENT(fgrp) != NULL) {
666811878SVenu.Iyer@Sun.COM 		mac_rx_srs_group_teardown(mcip->mci_flent, B_TRUE);
666911878SVenu.Iyer@Sun.COM 	}
667011878SVenu.Iyer@Sun.COM 	if (fgrp != MAC_DEFAULT_RX_GROUP(mip)) {
667111878SVenu.Iyer@Sun.COM 		mgcp = fgrp->mrg_clients;
667211878SVenu.Iyer@Sun.COM 		while (mgcp != NULL) {
667311878SVenu.Iyer@Sun.COM 			gmcip = mgcp->mgc_client;
667411878SVenu.Iyer@Sun.COM 			mgcp = mgcp->mgc_next;
667511878SVenu.Iyer@Sun.COM 			mac_group_remove_client(fgrp, gmcip);
667611878SVenu.Iyer@Sun.COM 			mac_group_add_client(tgrp, gmcip);
667711878SVenu.Iyer@Sun.COM 			gmcip->mci_flent->fe_rx_ring_group = tgrp;
667811878SVenu.Iyer@Sun.COM 		}
667911878SVenu.Iyer@Sun.COM 		mac_release_rx_group(mcip, fgrp);
668011878SVenu.Iyer@Sun.COM 		ASSERT(MAC_GROUP_NO_CLIENT(fgrp));
668111878SVenu.Iyer@Sun.COM 		mac_set_group_state(fgrp, MAC_GROUP_STATE_REGISTERED);
668211878SVenu.Iyer@Sun.COM 	} else {
668311878SVenu.Iyer@Sun.COM 		mac_group_remove_client(fgrp, mcip);
668411878SVenu.Iyer@Sun.COM 		mac_group_add_client(tgrp, mcip);
668511878SVenu.Iyer@Sun.COM 		mcip->mci_flent->fe_rx_ring_group = tgrp;
668611878SVenu.Iyer@Sun.COM 		/*
668711878SVenu.Iyer@Sun.COM 		 * If there are other clients (VLANs) sharing this address
668811878SVenu.Iyer@Sun.COM 		 * we should be here only for the primary.
668911878SVenu.Iyer@Sun.COM 		 */
669011878SVenu.Iyer@Sun.COM 		if (mcip->mci_unicast->ma_nusers > 1) {
669111878SVenu.Iyer@Sun.COM 			/*
669211878SVenu.Iyer@Sun.COM 			 * We need to move all the clients that are using
669311878SVenu.Iyer@Sun.COM 			 * this h/w address.
669411878SVenu.Iyer@Sun.COM 			 */
669511878SVenu.Iyer@Sun.COM 			mgcp = fgrp->mrg_clients;
669611878SVenu.Iyer@Sun.COM 			while (mgcp != NULL) {
669711878SVenu.Iyer@Sun.COM 				gmcip = mgcp->mgc_client;
669811878SVenu.Iyer@Sun.COM 				mgcp = mgcp->mgc_next;
669911878SVenu.Iyer@Sun.COM 				if (mcip->mci_unicast == gmcip->mci_unicast) {
670011878SVenu.Iyer@Sun.COM 					mac_group_remove_client(fgrp, gmcip);
670111878SVenu.Iyer@Sun.COM 					mac_group_add_client(tgrp, gmcip);
670211878SVenu.Iyer@Sun.COM 					gmcip->mci_flent->fe_rx_ring_group =
670311878SVenu.Iyer@Sun.COM 					    tgrp;
670411878SVenu.Iyer@Sun.COM 				}
670511878SVenu.Iyer@Sun.COM 			}
670611878SVenu.Iyer@Sun.COM 		}
670711878SVenu.Iyer@Sun.COM 		/*
670811878SVenu.Iyer@Sun.COM 		 * The default group will still take the multicast,
670911878SVenu.Iyer@Sun.COM 		 * broadcast traffic etc., so it won't go to
671011878SVenu.Iyer@Sun.COM 		 * MAC_GROUP_STATE_REGISTERED.
671111878SVenu.Iyer@Sun.COM 		 */
671211878SVenu.Iyer@Sun.COM 		if (fgrp->mrg_state == MAC_GROUP_STATE_RESERVED)
671311878SVenu.Iyer@Sun.COM 			mac_rx_group_unmark(fgrp, MR_CONDEMNED);
671411878SVenu.Iyer@Sun.COM 		mac_set_group_state(fgrp, MAC_GROUP_STATE_SHARED);
671511878SVenu.Iyer@Sun.COM 	}
671611878SVenu.Iyer@Sun.COM 	next_state = mac_group_next_state(tgrp, &group_only_mcip,
671711878SVenu.Iyer@Sun.COM 	    MAC_DEFAULT_RX_GROUP(mip), B_TRUE);
671811878SVenu.Iyer@Sun.COM 	mac_set_group_state(tgrp, next_state);
671911878SVenu.Iyer@Sun.COM 	/*
672011878SVenu.Iyer@Sun.COM 	 * If the destination group is reserved, setup the SRSs etc.
672111878SVenu.Iyer@Sun.COM 	 */
672211878SVenu.Iyer@Sun.COM 	if (tgrp->mrg_state == MAC_GROUP_STATE_RESERVED) {
672311878SVenu.Iyer@Sun.COM 		mac_rx_srs_group_setup(mcip, mcip->mci_flent, SRST_LINK);
672411878SVenu.Iyer@Sun.COM 		mac_fanout_setup(mcip, mcip->mci_flent,
672511878SVenu.Iyer@Sun.COM 		    MCIP_RESOURCE_PROPS(mcip), mac_rx_deliver, mcip, NULL,
672611878SVenu.Iyer@Sun.COM 		    NULL);
672711878SVenu.Iyer@Sun.COM 		mac_rx_group_unmark(tgrp, MR_INCIPIENT);
672811878SVenu.Iyer@Sun.COM 	} else {
672911878SVenu.Iyer@Sun.COM 		mac_rx_switch_grp_to_sw(tgrp);
673011878SVenu.Iyer@Sun.COM 	}
673111878SVenu.Iyer@Sun.COM 	return (0);
673211878SVenu.Iyer@Sun.COM }
673311878SVenu.Iyer@Sun.COM 
673411878SVenu.Iyer@Sun.COM /*
67358275SEric Cheng  * Reserves a TX group for the specified share. Invoked by mac_tx_srs_setup()
67368275SEric Cheng  * when a share was allocated to the client.
67378275SEric Cheng  */
67388275SEric Cheng mac_group_t *
mac_reserve_tx_group(mac_client_impl_t * mcip,boolean_t move)673911878SVenu.Iyer@Sun.COM mac_reserve_tx_group(mac_client_impl_t *mcip, boolean_t move)
674011878SVenu.Iyer@Sun.COM {
674111878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
674211878SVenu.Iyer@Sun.COM 	mac_group_t		*grp = NULL;
674311878SVenu.Iyer@Sun.COM 	int			rv;
674411878SVenu.Iyer@Sun.COM 	int			i;
674511878SVenu.Iyer@Sun.COM 	int			err;
674611878SVenu.Iyer@Sun.COM 	mac_group_t		*defgrp;
674711878SVenu.Iyer@Sun.COM 	mac_share_handle_t	share = mcip->mci_share;
674811878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
674911878SVenu.Iyer@Sun.COM 	int			nrings;
675011878SVenu.Iyer@Sun.COM 	int			defnrings;
675111878SVenu.Iyer@Sun.COM 	boolean_t		need_exclgrp = B_FALSE;
675211878SVenu.Iyer@Sun.COM 	int			need_rings = 0;
675311878SVenu.Iyer@Sun.COM 	mac_group_t		*candidate_grp = NULL;
675411878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*gclient;
675511878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*gmrp;
675611878SVenu.Iyer@Sun.COM 	boolean_t		txhw = mrp->mrp_mask & MRP_TX_RINGS;
675711878SVenu.Iyer@Sun.COM 	boolean_t		unspec = mrp->mrp_mask & MRP_TXRINGS_UNSPEC;
675811878SVenu.Iyer@Sun.COM 	boolean_t		isprimary;
675911878SVenu.Iyer@Sun.COM 
676011878SVenu.Iyer@Sun.COM 	isprimary = mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC;
676111878SVenu.Iyer@Sun.COM 	/*
676211878SVenu.Iyer@Sun.COM 	 * When we come here for a VLAN on the primary (dladm create-vlan),
676311878SVenu.Iyer@Sun.COM 	 * we need to pair it along with the primary (to keep it consistent
676411878SVenu.Iyer@Sun.COM 	 * with the RX side). So, we check if the primary is already assigned
676511878SVenu.Iyer@Sun.COM 	 * to a group and return the group if so. The other way is also
676611878SVenu.Iyer@Sun.COM 	 * true, i.e. the VLAN is already created and now we are plumbing
676711878SVenu.Iyer@Sun.COM 	 * the primary.
676811878SVenu.Iyer@Sun.COM 	 */
676911878SVenu.Iyer@Sun.COM 	if (!move && isprimary) {
677011878SVenu.Iyer@Sun.COM 		for (gclient = mip->mi_clients_list; gclient != NULL;
677111878SVenu.Iyer@Sun.COM 		    gclient = gclient->mci_client_next) {
677211878SVenu.Iyer@Sun.COM 			if (gclient->mci_flent->fe_type & FLOW_PRIMARY_MAC &&
677311878SVenu.Iyer@Sun.COM 			    gclient->mci_flent->fe_tx_ring_group != NULL) {
677411878SVenu.Iyer@Sun.COM 				return (gclient->mci_flent->fe_tx_ring_group);
677511878SVenu.Iyer@Sun.COM 			}
677611878SVenu.Iyer@Sun.COM 		}
677711878SVenu.Iyer@Sun.COM 	}
677811878SVenu.Iyer@Sun.COM 
677911878SVenu.Iyer@Sun.COM 	if (mip->mi_tx_groups == NULL || mip->mi_tx_group_count == 0)
678011878SVenu.Iyer@Sun.COM 		return (NULL);
678111878SVenu.Iyer@Sun.COM 
678211878SVenu.Iyer@Sun.COM 	/* For dynamic groups, default unspec to 1 */
678311878SVenu.Iyer@Sun.COM 	if (txhw && unspec &&
678411878SVenu.Iyer@Sun.COM 	    mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
678511878SVenu.Iyer@Sun.COM 		mrp->mrp_ntxrings = 1;
678611878SVenu.Iyer@Sun.COM 	}
678711878SVenu.Iyer@Sun.COM 	/*
678811878SVenu.Iyer@Sun.COM 	 * For static grouping we allow only specifying rings=0 and
678911878SVenu.Iyer@Sun.COM 	 * unspecified
679011878SVenu.Iyer@Sun.COM 	 */
679111878SVenu.Iyer@Sun.COM 	if (txhw && mrp->mrp_ntxrings > 0 &&
679211878SVenu.Iyer@Sun.COM 	    mip->mi_tx_group_type == MAC_GROUP_TYPE_STATIC) {
679311878SVenu.Iyer@Sun.COM 		return (NULL);
679411878SVenu.Iyer@Sun.COM 	}
679511878SVenu.Iyer@Sun.COM 
679611878SVenu.Iyer@Sun.COM 	if (txhw) {
679711878SVenu.Iyer@Sun.COM 		/*
679811878SVenu.Iyer@Sun.COM 		 * We have explicitly asked for a group (with ntxrings,
679911878SVenu.Iyer@Sun.COM 		 * if unspec).
680011878SVenu.Iyer@Sun.COM 		 */
680111878SVenu.Iyer@Sun.COM 		if (unspec || mrp->mrp_ntxrings > 0) {
680211878SVenu.Iyer@Sun.COM 			need_exclgrp = B_TRUE;
680311878SVenu.Iyer@Sun.COM 			need_rings = mrp->mrp_ntxrings;
680411878SVenu.Iyer@Sun.COM 		} else if (mrp->mrp_ntxrings == 0) {
680511878SVenu.Iyer@Sun.COM 			/*
680611878SVenu.Iyer@Sun.COM 			 * We have asked for a software group.
680711878SVenu.Iyer@Sun.COM 			 */
680811878SVenu.Iyer@Sun.COM 			return (NULL);
680911878SVenu.Iyer@Sun.COM 		}
681011878SVenu.Iyer@Sun.COM 	}
681111878SVenu.Iyer@Sun.COM 	defgrp = MAC_DEFAULT_TX_GROUP(mip);
681211878SVenu.Iyer@Sun.COM 	/*
681311878SVenu.Iyer@Sun.COM 	 * The number of rings that the default group can donate.
681411878SVenu.Iyer@Sun.COM 	 * We need to leave at least one ring - the default ring - in
681511878SVenu.Iyer@Sun.COM 	 * this group.
681611878SVenu.Iyer@Sun.COM 	 */
681711878SVenu.Iyer@Sun.COM 	defnrings = defgrp->mrg_cur_count - 1;
68188275SEric Cheng 
68198275SEric Cheng 	/*
682011878SVenu.Iyer@Sun.COM 	 * Primary gets default group unless explicitly told not
682111878SVenu.Iyer@Sun.COM 	 * to  (i.e. rings > 0).
68228275SEric Cheng 	 */
682311878SVenu.Iyer@Sun.COM 	if (isprimary && !need_exclgrp)
682411878SVenu.Iyer@Sun.COM 		return (NULL);
682511878SVenu.Iyer@Sun.COM 
682611878SVenu.Iyer@Sun.COM 	nrings = (mrp->mrp_mask & MRP_TX_RINGS) != 0 ? mrp->mrp_ntxrings : 1;
68278275SEric Cheng 	for (i = 0; i <  mip->mi_tx_group_count; i++) {
68288275SEric Cheng 		grp = &mip->mi_tx_groups[i];
68298275SEric Cheng 		if ((grp->mrg_state == MAC_GROUP_STATE_RESERVED) ||
683011878SVenu.Iyer@Sun.COM 		    (grp->mrg_state == MAC_GROUP_STATE_UNINIT)) {
683111878SVenu.Iyer@Sun.COM 			/*
683211878SVenu.Iyer@Sun.COM 			 * Select a candidate for replacement if we don't
683311878SVenu.Iyer@Sun.COM 			 * get an exclusive group. A candidate group is one
683411878SVenu.Iyer@Sun.COM 			 * that didn't ask for an exclusive group, but got
683511878SVenu.Iyer@Sun.COM 			 * one and it has enough rings (combined with what
683611878SVenu.Iyer@Sun.COM 			 * the default group can donate) for the new MAC
683711878SVenu.Iyer@Sun.COM 			 * client.
683811878SVenu.Iyer@Sun.COM 			 */
683911878SVenu.Iyer@Sun.COM 			if (grp->mrg_state == MAC_GROUP_STATE_RESERVED &&
684011878SVenu.Iyer@Sun.COM 			    candidate_grp == NULL) {
684111878SVenu.Iyer@Sun.COM 				gclient = MAC_GROUP_ONLY_CLIENT(grp);
684211878SVenu.Iyer@Sun.COM 				if (gclient == NULL)
684311878SVenu.Iyer@Sun.COM 					gclient = mac_get_grp_primary(grp);
684411878SVenu.Iyer@Sun.COM 				gmrp = MCIP_RESOURCE_PROPS(gclient);
684511878SVenu.Iyer@Sun.COM 				if (gclient->mci_share == NULL &&
684611878SVenu.Iyer@Sun.COM 				    (gmrp->mrp_mask & MRP_TX_RINGS) == 0 &&
684711878SVenu.Iyer@Sun.COM 				    (unspec ||
684811878SVenu.Iyer@Sun.COM 				    (grp->mrg_cur_count + defnrings) >=
684911878SVenu.Iyer@Sun.COM 				    need_rings)) {
685011878SVenu.Iyer@Sun.COM 					candidate_grp = grp;
685111878SVenu.Iyer@Sun.COM 				}
685211878SVenu.Iyer@Sun.COM 			}
68538275SEric Cheng 			continue;
685411878SVenu.Iyer@Sun.COM 		}
685511878SVenu.Iyer@Sun.COM 		/*
685611878SVenu.Iyer@Sun.COM 		 * If the default can't donate let's just walk and
685711878SVenu.Iyer@Sun.COM 		 * see if someone can vacate a group, so that we have
685811878SVenu.Iyer@Sun.COM 		 * enough rings for this.
685911878SVenu.Iyer@Sun.COM 		 */
686011878SVenu.Iyer@Sun.COM 		if (mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC ||
686111878SVenu.Iyer@Sun.COM 		    nrings <= defnrings) {
686211878SVenu.Iyer@Sun.COM 			if (grp->mrg_state == MAC_GROUP_STATE_REGISTERED) {
686311878SVenu.Iyer@Sun.COM 				rv = mac_start_group(grp);
686411878SVenu.Iyer@Sun.COM 				ASSERT(rv == 0);
686511878SVenu.Iyer@Sun.COM 			}
686611878SVenu.Iyer@Sun.COM 			break;
686711878SVenu.Iyer@Sun.COM 		}
686811878SVenu.Iyer@Sun.COM 	}
686911878SVenu.Iyer@Sun.COM 
687011878SVenu.Iyer@Sun.COM 	/* The default group */
687111878SVenu.Iyer@Sun.COM 	if (i >= mip->mi_tx_group_count) {
687211878SVenu.Iyer@Sun.COM 		/*
687311878SVenu.Iyer@Sun.COM 		 * If we need an exclusive group and have identified a
687411878SVenu.Iyer@Sun.COM 		 * candidate group we switch the MAC client from the
687511878SVenu.Iyer@Sun.COM 		 * candidate group to the default group and give the
687611878SVenu.Iyer@Sun.COM 		 * candidate group to this client.
687711878SVenu.Iyer@Sun.COM 		 */
687811878SVenu.Iyer@Sun.COM 		if (need_exclgrp && candidate_grp != NULL) {
687911878SVenu.Iyer@Sun.COM 			/*
688011878SVenu.Iyer@Sun.COM 			 * Switch the MAC client from the candidate group
688111878SVenu.Iyer@Sun.COM 			 * to the default group.
688211878SVenu.Iyer@Sun.COM 			 */
688311878SVenu.Iyer@Sun.COM 			grp = candidate_grp;
688411878SVenu.Iyer@Sun.COM 			gclient = MAC_GROUP_ONLY_CLIENT(grp);
688511878SVenu.Iyer@Sun.COM 			if (gclient == NULL)
688611878SVenu.Iyer@Sun.COM 				gclient = mac_get_grp_primary(grp);
688711878SVenu.Iyer@Sun.COM 			mac_tx_client_quiesce((mac_client_handle_t)gclient);
688811878SVenu.Iyer@Sun.COM 			mac_tx_switch_group(gclient, grp, defgrp);
688911878SVenu.Iyer@Sun.COM 			mac_tx_client_restart((mac_client_handle_t)gclient);
689011878SVenu.Iyer@Sun.COM 
689111878SVenu.Iyer@Sun.COM 			/*
689211878SVenu.Iyer@Sun.COM 			 * Give the candidate group with the specified number
689311878SVenu.Iyer@Sun.COM 			 * of rings to this MAC client.
689411878SVenu.Iyer@Sun.COM 			 */
689511878SVenu.Iyer@Sun.COM 			ASSERT(grp->mrg_state == MAC_GROUP_STATE_REGISTERED);
689611878SVenu.Iyer@Sun.COM 			rv = mac_start_group(grp);
689711878SVenu.Iyer@Sun.COM 			ASSERT(rv == 0);
689811878SVenu.Iyer@Sun.COM 
689911878SVenu.Iyer@Sun.COM 			if (mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC)
690011878SVenu.Iyer@Sun.COM 				return (grp);
690111878SVenu.Iyer@Sun.COM 
690211878SVenu.Iyer@Sun.COM 			ASSERT(grp->mrg_cur_count == 0);
690311878SVenu.Iyer@Sun.COM 			ASSERT(defgrp->mrg_cur_count > need_rings);
690411878SVenu.Iyer@Sun.COM 
690511878SVenu.Iyer@Sun.COM 			err = i_mac_group_allocate_rings(mip, MAC_RING_TYPE_TX,
690611878SVenu.Iyer@Sun.COM 			    defgrp, grp, share, need_rings);
690711878SVenu.Iyer@Sun.COM 			if (err == 0) {
690811878SVenu.Iyer@Sun.COM 				/*
690911878SVenu.Iyer@Sun.COM 				 * For a share i_mac_group_allocate_rings gets
691011878SVenu.Iyer@Sun.COM 				 * the rings from the driver, let's populate
691111878SVenu.Iyer@Sun.COM 				 * the property for the client now.
691211878SVenu.Iyer@Sun.COM 				 */
691311878SVenu.Iyer@Sun.COM 				if (share != NULL) {
691411878SVenu.Iyer@Sun.COM 					mac_client_set_rings(
691511878SVenu.Iyer@Sun.COM 					    (mac_client_handle_t)mcip, -1,
691611878SVenu.Iyer@Sun.COM 					    grp->mrg_cur_count);
691711878SVenu.Iyer@Sun.COM 				}
691811878SVenu.Iyer@Sun.COM 				mip->mi_tx_group_free--;
691911878SVenu.Iyer@Sun.COM 				return (grp);
692011878SVenu.Iyer@Sun.COM 			}
692111878SVenu.Iyer@Sun.COM 			DTRACE_PROBE3(tx__group__reserve__alloc__rings, char *,
692211878SVenu.Iyer@Sun.COM 			    mip->mi_name, int, grp->mrg_index, int, err);
692311878SVenu.Iyer@Sun.COM 			mac_stop_group(grp);
692411878SVenu.Iyer@Sun.COM 		}
692511878SVenu.Iyer@Sun.COM 		return (NULL);
692611878SVenu.Iyer@Sun.COM 	}
69278275SEric Cheng 	/*
692811878SVenu.Iyer@Sun.COM 	 * We got an exclusive group, but it is not dynamic.
69298275SEric Cheng 	 */
693011878SVenu.Iyer@Sun.COM 	if (mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC) {
693111878SVenu.Iyer@Sun.COM 		mip->mi_tx_group_free--;
693211878SVenu.Iyer@Sun.COM 		return (grp);
693311878SVenu.Iyer@Sun.COM 	}
693411878SVenu.Iyer@Sun.COM 
693511878SVenu.Iyer@Sun.COM 	rv = i_mac_group_allocate_rings(mip, MAC_RING_TYPE_TX, defgrp, grp,
693611878SVenu.Iyer@Sun.COM 	    share, nrings);
69378275SEric Cheng 	if (rv != 0) {
69388275SEric Cheng 		DTRACE_PROBE3(tx__group__reserve__alloc__rings,
69398275SEric Cheng 		    char *, mip->mi_name, int, grp->mrg_index, int, rv);
69408275SEric Cheng 		mac_stop_group(grp);
69418275SEric Cheng 		return (NULL);
69428275SEric Cheng 	}
694311878SVenu.Iyer@Sun.COM 	/*
694411878SVenu.Iyer@Sun.COM 	 * For a share i_mac_group_allocate_rings gets the rings from the
694511878SVenu.Iyer@Sun.COM 	 * driver, let's populate the property for the client now.
694611878SVenu.Iyer@Sun.COM 	 */
694711878SVenu.Iyer@Sun.COM 	if (share != NULL) {
694811878SVenu.Iyer@Sun.COM 		mac_client_set_rings((mac_client_handle_t)mcip, -1,
694911878SVenu.Iyer@Sun.COM 		    grp->mrg_cur_count);
695011878SVenu.Iyer@Sun.COM 	}
69518275SEric Cheng 	mip->mi_tx_group_free--;
69528275SEric Cheng 	return (grp);
69538275SEric Cheng }
69548275SEric Cheng 
69558275SEric Cheng void
mac_release_tx_group(mac_client_impl_t * mcip,mac_group_t * grp)695611878SVenu.Iyer@Sun.COM mac_release_tx_group(mac_client_impl_t *mcip, mac_group_t *grp)
695711878SVenu.Iyer@Sun.COM {
695811878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
695911878SVenu.Iyer@Sun.COM 	mac_share_handle_t	share = mcip->mci_share;
696011878SVenu.Iyer@Sun.COM 	mac_ring_t		*ring;
696111878SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t	*srs = MCIP_TX_SRS(mcip);
696211878SVenu.Iyer@Sun.COM 	mac_group_t		*defgrp;
696311878SVenu.Iyer@Sun.COM 
696411878SVenu.Iyer@Sun.COM 	defgrp = MAC_DEFAULT_TX_GROUP(mip);
696511878SVenu.Iyer@Sun.COM 	if (srs != NULL) {
696611878SVenu.Iyer@Sun.COM 		if (srs->srs_soft_ring_count > 0) {
696711878SVenu.Iyer@Sun.COM 			for (ring = grp->mrg_rings; ring != NULL;
696811878SVenu.Iyer@Sun.COM 			    ring = ring->mr_next) {
696911878SVenu.Iyer@Sun.COM 				ASSERT(mac_tx_srs_ring_present(srs, ring));
697011878SVenu.Iyer@Sun.COM 				mac_tx_invoke_callbacks(mcip,
697111878SVenu.Iyer@Sun.COM 				    (mac_tx_cookie_t)
697211878SVenu.Iyer@Sun.COM 				    mac_tx_srs_get_soft_ring(srs, ring));
697311878SVenu.Iyer@Sun.COM 				mac_tx_srs_del_ring(srs, ring);
697411878SVenu.Iyer@Sun.COM 			}
697511878SVenu.Iyer@Sun.COM 		} else {
697611878SVenu.Iyer@Sun.COM 			ASSERT(srs->srs_tx.st_arg2 != NULL);
697711878SVenu.Iyer@Sun.COM 			srs->srs_tx.st_arg2 = NULL;
697811878SVenu.Iyer@Sun.COM 			mac_srs_stat_delete(srs);
697911878SVenu.Iyer@Sun.COM 		}
698011878SVenu.Iyer@Sun.COM 	}
698111878SVenu.Iyer@Sun.COM 	if (share != NULL)
698211878SVenu.Iyer@Sun.COM 		mip->mi_share_capab.ms_sremove(share, grp->mrg_driver);
698311878SVenu.Iyer@Sun.COM 
698411878SVenu.Iyer@Sun.COM 	/* move the ring back to the pool */
698511878SVenu.Iyer@Sun.COM 	if (mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC) {
698611878SVenu.Iyer@Sun.COM 		while ((ring = grp->mrg_rings) != NULL)
698711878SVenu.Iyer@Sun.COM 			(void) mac_group_mov_ring(mip, defgrp, ring);
69888275SEric Cheng 	}
69898275SEric Cheng 	mac_stop_group(grp);
69908275SEric Cheng 	mip->mi_tx_group_free++;
69918275SEric Cheng }
69928275SEric Cheng 
69938275SEric Cheng /*
699411878SVenu.Iyer@Sun.COM  * Disassociate a MAC client from a group, i.e go through the rings in the
699511878SVenu.Iyer@Sun.COM  * group and delete all the soft rings tied to them.
699611878SVenu.Iyer@Sun.COM  */
699711878SVenu.Iyer@Sun.COM static void
mac_tx_dismantle_soft_rings(mac_group_t * fgrp,flow_entry_t * flent)699811878SVenu.Iyer@Sun.COM mac_tx_dismantle_soft_rings(mac_group_t *fgrp, flow_entry_t *flent)
699911878SVenu.Iyer@Sun.COM {
700011878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip = flent->fe_mcip;
700111878SVenu.Iyer@Sun.COM 	mac_soft_ring_set_t	*tx_srs;
700211878SVenu.Iyer@Sun.COM 	mac_srs_tx_t		*tx;
700311878SVenu.Iyer@Sun.COM 	mac_ring_t		*ring;
700411878SVenu.Iyer@Sun.COM 
700511878SVenu.Iyer@Sun.COM 	tx_srs = flent->fe_tx_srs;
700611878SVenu.Iyer@Sun.COM 	tx = &tx_srs->srs_tx;
700711878SVenu.Iyer@Sun.COM 
700811878SVenu.Iyer@Sun.COM 	/* Single ring case we haven't created any soft rings */
700911878SVenu.Iyer@Sun.COM 	if (tx->st_mode == SRS_TX_BW || tx->st_mode == SRS_TX_SERIALIZE ||
701011878SVenu.Iyer@Sun.COM 	    tx->st_mode == SRS_TX_DEFAULT) {
701111878SVenu.Iyer@Sun.COM 		tx->st_arg2 = NULL;
701211878SVenu.Iyer@Sun.COM 		mac_srs_stat_delete(tx_srs);
701311878SVenu.Iyer@Sun.COM 	/* Fanout case, where we have to dismantle the soft rings */
701411878SVenu.Iyer@Sun.COM 	} else {
701511878SVenu.Iyer@Sun.COM 		for (ring = fgrp->mrg_rings; ring != NULL;
701611878SVenu.Iyer@Sun.COM 		    ring = ring->mr_next) {
701711878SVenu.Iyer@Sun.COM 			ASSERT(mac_tx_srs_ring_present(tx_srs, ring));
701811878SVenu.Iyer@Sun.COM 			mac_tx_invoke_callbacks(mcip,
701911878SVenu.Iyer@Sun.COM 			    (mac_tx_cookie_t)mac_tx_srs_get_soft_ring(tx_srs,
702011878SVenu.Iyer@Sun.COM 			    ring));
702111878SVenu.Iyer@Sun.COM 			mac_tx_srs_del_ring(tx_srs, ring);
702211878SVenu.Iyer@Sun.COM 		}
702311878SVenu.Iyer@Sun.COM 		ASSERT(tx->st_arg2 == NULL);
702411878SVenu.Iyer@Sun.COM 	}
702511878SVenu.Iyer@Sun.COM }
702611878SVenu.Iyer@Sun.COM 
702711878SVenu.Iyer@Sun.COM /*
702811878SVenu.Iyer@Sun.COM  * Switch the MAC client from one group to another. This means we need
702911878SVenu.Iyer@Sun.COM  * to remove the MAC client, teardown the SRSs and revert the group state.
703011878SVenu.Iyer@Sun.COM  * Then, we add the client to the destination roup, set the SRSs etc.
703111878SVenu.Iyer@Sun.COM  */
703211878SVenu.Iyer@Sun.COM void
mac_tx_switch_group(mac_client_impl_t * mcip,mac_group_t * fgrp,mac_group_t * tgrp)703311878SVenu.Iyer@Sun.COM mac_tx_switch_group(mac_client_impl_t *mcip, mac_group_t *fgrp,
703411878SVenu.Iyer@Sun.COM     mac_group_t *tgrp)
703511878SVenu.Iyer@Sun.COM {
703611878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*group_only_mcip;
703711878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
703811878SVenu.Iyer@Sun.COM 	flow_entry_t		*flent = mcip->mci_flent;
703911878SVenu.Iyer@Sun.COM 	mac_group_t		*defgrp;
704011878SVenu.Iyer@Sun.COM 	mac_grp_client_t	*mgcp;
704111878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*gmcip;
704211878SVenu.Iyer@Sun.COM 	flow_entry_t		*gflent;
704311878SVenu.Iyer@Sun.COM 
704411878SVenu.Iyer@Sun.COM 	defgrp = MAC_DEFAULT_TX_GROUP(mip);
704511878SVenu.Iyer@Sun.COM 	ASSERT(fgrp == flent->fe_tx_ring_group);
704611878SVenu.Iyer@Sun.COM 
704711878SVenu.Iyer@Sun.COM 	if (fgrp == defgrp) {
704811878SVenu.Iyer@Sun.COM 		/*
704911878SVenu.Iyer@Sun.COM 		 * If this is the primary we need to find any VLANs on
705011878SVenu.Iyer@Sun.COM 		 * the primary and move them too.
705111878SVenu.Iyer@Sun.COM 		 */
705211878SVenu.Iyer@Sun.COM 		mac_group_remove_client(fgrp, mcip);
705311878SVenu.Iyer@Sun.COM 		mac_tx_dismantle_soft_rings(fgrp, flent);
705411878SVenu.Iyer@Sun.COM 		if (mcip->mci_unicast->ma_nusers > 1) {
705511878SVenu.Iyer@Sun.COM 			mgcp = fgrp->mrg_clients;
705611878SVenu.Iyer@Sun.COM 			while (mgcp != NULL) {
705711878SVenu.Iyer@Sun.COM 				gmcip = mgcp->mgc_client;
705811878SVenu.Iyer@Sun.COM 				mgcp = mgcp->mgc_next;
705911878SVenu.Iyer@Sun.COM 				if (mcip->mci_unicast != gmcip->mci_unicast)
706011878SVenu.Iyer@Sun.COM 					continue;
706111878SVenu.Iyer@Sun.COM 				mac_tx_client_quiesce(
706211878SVenu.Iyer@Sun.COM 				    (mac_client_handle_t)gmcip);
706311878SVenu.Iyer@Sun.COM 
706411878SVenu.Iyer@Sun.COM 				gflent = gmcip->mci_flent;
706511878SVenu.Iyer@Sun.COM 				mac_group_remove_client(fgrp, gmcip);
706611878SVenu.Iyer@Sun.COM 				mac_tx_dismantle_soft_rings(fgrp, gflent);
706711878SVenu.Iyer@Sun.COM 
706811878SVenu.Iyer@Sun.COM 				mac_group_add_client(tgrp, gmcip);
706911878SVenu.Iyer@Sun.COM 				gflent->fe_tx_ring_group = tgrp;
707011878SVenu.Iyer@Sun.COM 				/* We could directly set this to SHARED */
707111878SVenu.Iyer@Sun.COM 				tgrp->mrg_state = mac_group_next_state(tgrp,
707211878SVenu.Iyer@Sun.COM 				    &group_only_mcip, defgrp, B_FALSE);
707311878SVenu.Iyer@Sun.COM 
707411878SVenu.Iyer@Sun.COM 				mac_tx_srs_group_setup(gmcip, gflent,
707511878SVenu.Iyer@Sun.COM 				    SRST_LINK);
707611878SVenu.Iyer@Sun.COM 				mac_fanout_setup(gmcip, gflent,
707711878SVenu.Iyer@Sun.COM 				    MCIP_RESOURCE_PROPS(gmcip), mac_rx_deliver,
707811878SVenu.Iyer@Sun.COM 				    gmcip, NULL, NULL);
707911878SVenu.Iyer@Sun.COM 
708011878SVenu.Iyer@Sun.COM 				mac_tx_client_restart(
708111878SVenu.Iyer@Sun.COM 				    (mac_client_handle_t)gmcip);
708211878SVenu.Iyer@Sun.COM 			}
708311878SVenu.Iyer@Sun.COM 		}
708411878SVenu.Iyer@Sun.COM 		if (MAC_GROUP_NO_CLIENT(fgrp)) {
708511878SVenu.Iyer@Sun.COM 			mac_ring_t	*ring;
708611878SVenu.Iyer@Sun.COM 			int		cnt;
708711878SVenu.Iyer@Sun.COM 			int		ringcnt;
708811878SVenu.Iyer@Sun.COM 
708911878SVenu.Iyer@Sun.COM 			fgrp->mrg_state = MAC_GROUP_STATE_REGISTERED;
709011878SVenu.Iyer@Sun.COM 			/*
709111878SVenu.Iyer@Sun.COM 			 * Additionally, we also need to stop all
709211878SVenu.Iyer@Sun.COM 			 * the rings in the default group, except
709311878SVenu.Iyer@Sun.COM 			 * the default ring. The reason being
709411878SVenu.Iyer@Sun.COM 			 * this group won't be released since it is
709511878SVenu.Iyer@Sun.COM 			 * the default group, so the rings won't
709611878SVenu.Iyer@Sun.COM 			 * be stopped otherwise.
709711878SVenu.Iyer@Sun.COM 			 */
709811878SVenu.Iyer@Sun.COM 			ringcnt = fgrp->mrg_cur_count;
709911878SVenu.Iyer@Sun.COM 			ring = fgrp->mrg_rings;
710011878SVenu.Iyer@Sun.COM 			for (cnt = 0; cnt < ringcnt; cnt++) {
710111878SVenu.Iyer@Sun.COM 				if (ring->mr_state == MR_INUSE &&
710211878SVenu.Iyer@Sun.COM 				    ring !=
710311878SVenu.Iyer@Sun.COM 				    (mac_ring_t *)mip->mi_default_tx_ring) {
710411878SVenu.Iyer@Sun.COM 					mac_stop_ring(ring);
710511878SVenu.Iyer@Sun.COM 					ring->mr_flag = 0;
710611878SVenu.Iyer@Sun.COM 				}
710711878SVenu.Iyer@Sun.COM 				ring = ring->mr_next;
710811878SVenu.Iyer@Sun.COM 			}
710911878SVenu.Iyer@Sun.COM 		} else if (MAC_GROUP_ONLY_CLIENT(fgrp) != NULL) {
711011878SVenu.Iyer@Sun.COM 			fgrp->mrg_state = MAC_GROUP_STATE_RESERVED;
711111878SVenu.Iyer@Sun.COM 		} else {
711211878SVenu.Iyer@Sun.COM 			ASSERT(fgrp->mrg_state == MAC_GROUP_STATE_SHARED);
711311878SVenu.Iyer@Sun.COM 		}
711411878SVenu.Iyer@Sun.COM 	} else {
711511878SVenu.Iyer@Sun.COM 		/*
711611878SVenu.Iyer@Sun.COM 		 * We could have VLANs sharing the non-default group with
711711878SVenu.Iyer@Sun.COM 		 * the primary.
711811878SVenu.Iyer@Sun.COM 		 */
711911878SVenu.Iyer@Sun.COM 		mgcp = fgrp->mrg_clients;
712011878SVenu.Iyer@Sun.COM 		while (mgcp != NULL) {
712111878SVenu.Iyer@Sun.COM 			gmcip = mgcp->mgc_client;
712211878SVenu.Iyer@Sun.COM 			mgcp = mgcp->mgc_next;
712311878SVenu.Iyer@Sun.COM 			if (gmcip == mcip)
712411878SVenu.Iyer@Sun.COM 				continue;
712511878SVenu.Iyer@Sun.COM 			mac_tx_client_quiesce((mac_client_handle_t)gmcip);
712611878SVenu.Iyer@Sun.COM 			gflent = gmcip->mci_flent;
712711878SVenu.Iyer@Sun.COM 
712811878SVenu.Iyer@Sun.COM 			mac_group_remove_client(fgrp, gmcip);
712911878SVenu.Iyer@Sun.COM 			mac_tx_dismantle_soft_rings(fgrp, gflent);
713011878SVenu.Iyer@Sun.COM 
713111878SVenu.Iyer@Sun.COM 			mac_group_add_client(tgrp, gmcip);
713211878SVenu.Iyer@Sun.COM 			gflent->fe_tx_ring_group = tgrp;
713311878SVenu.Iyer@Sun.COM 			/* We could directly set this to SHARED */
713411878SVenu.Iyer@Sun.COM 			tgrp->mrg_state = mac_group_next_state(tgrp,
713511878SVenu.Iyer@Sun.COM 			    &group_only_mcip, defgrp, B_FALSE);
713611878SVenu.Iyer@Sun.COM 			mac_tx_srs_group_setup(gmcip, gflent, SRST_LINK);
713711878SVenu.Iyer@Sun.COM 			mac_fanout_setup(gmcip, gflent,
713811878SVenu.Iyer@Sun.COM 			    MCIP_RESOURCE_PROPS(gmcip), mac_rx_deliver,
713911878SVenu.Iyer@Sun.COM 			    gmcip, NULL, NULL);
714011878SVenu.Iyer@Sun.COM 
714111878SVenu.Iyer@Sun.COM 			mac_tx_client_restart((mac_client_handle_t)gmcip);
714211878SVenu.Iyer@Sun.COM 		}
714311878SVenu.Iyer@Sun.COM 		mac_group_remove_client(fgrp, mcip);
714411878SVenu.Iyer@Sun.COM 		mac_release_tx_group(mcip, fgrp);
714511878SVenu.Iyer@Sun.COM 		fgrp->mrg_state = MAC_GROUP_STATE_REGISTERED;
714611878SVenu.Iyer@Sun.COM 	}
714711878SVenu.Iyer@Sun.COM 
714811878SVenu.Iyer@Sun.COM 	/* Add it to the tgroup */
714911878SVenu.Iyer@Sun.COM 	mac_group_add_client(tgrp, mcip);
715011878SVenu.Iyer@Sun.COM 	flent->fe_tx_ring_group = tgrp;
715111878SVenu.Iyer@Sun.COM 	tgrp->mrg_state = mac_group_next_state(tgrp, &group_only_mcip,
715211878SVenu.Iyer@Sun.COM 	    defgrp, B_FALSE);
715311878SVenu.Iyer@Sun.COM 
715411878SVenu.Iyer@Sun.COM 	mac_tx_srs_group_setup(mcip, flent, SRST_LINK);
715511878SVenu.Iyer@Sun.COM 	mac_fanout_setup(mcip, flent, MCIP_RESOURCE_PROPS(mcip),
715611878SVenu.Iyer@Sun.COM 	    mac_rx_deliver, mcip, NULL, NULL);
715711878SVenu.Iyer@Sun.COM }
715811878SVenu.Iyer@Sun.COM 
715911878SVenu.Iyer@Sun.COM /*
71608275SEric Cheng  * This is a 1-time control path activity initiated by the client (IP).
71618275SEric Cheng  * The mac perimeter protects against other simultaneous control activities,
71628275SEric Cheng  * for example an ioctl that attempts to change the degree of fanout and
71638275SEric Cheng  * increase or decrease the number of softrings associated with this Tx SRS.
71648275SEric Cheng  */
71658275SEric Cheng static mac_tx_notify_cb_t *
mac_client_tx_notify_add(mac_client_impl_t * mcip,mac_tx_notify_t notify,void * arg)71668275SEric Cheng mac_client_tx_notify_add(mac_client_impl_t *mcip,
71678275SEric Cheng     mac_tx_notify_t notify, void *arg)
71688275SEric Cheng {
71698275SEric Cheng 	mac_cb_info_t *mcbi;
71708275SEric Cheng 	mac_tx_notify_cb_t *mtnfp;
71718275SEric Cheng 
71728275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
71738275SEric Cheng 
71748275SEric Cheng 	mtnfp = kmem_zalloc(sizeof (mac_tx_notify_cb_t), KM_SLEEP);
71758275SEric Cheng 	mtnfp->mtnf_fn = notify;
71768275SEric Cheng 	mtnfp->mtnf_arg = arg;
71778275SEric Cheng 	mtnfp->mtnf_link.mcb_objp = mtnfp;
71788275SEric Cheng 	mtnfp->mtnf_link.mcb_objsize = sizeof (mac_tx_notify_cb_t);
71798275SEric Cheng 	mtnfp->mtnf_link.mcb_flags = MCB_TX_NOTIFY_CB_T;
71808275SEric Cheng 
71818275SEric Cheng 	mcbi = &mcip->mci_tx_notify_cb_info;
71828275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
71838275SEric Cheng 	mac_callback_add(mcbi, &mcip->mci_tx_notify_cb_list, &mtnfp->mtnf_link);
71848275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
71858275SEric Cheng 	return (mtnfp);
71868275SEric Cheng }
71878275SEric Cheng 
71888275SEric Cheng static void
mac_client_tx_notify_remove(mac_client_impl_t * mcip,mac_tx_notify_cb_t * mtnfp)71898275SEric Cheng mac_client_tx_notify_remove(mac_client_impl_t *mcip, mac_tx_notify_cb_t *mtnfp)
71908275SEric Cheng {
71918275SEric Cheng 	mac_cb_info_t	*mcbi;
71928275SEric Cheng 	mac_cb_t	**cblist;
71938275SEric Cheng 
71948275SEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
71958275SEric Cheng 
71968275SEric Cheng 	if (!mac_callback_find(&mcip->mci_tx_notify_cb_info,
71978275SEric Cheng 	    &mcip->mci_tx_notify_cb_list, &mtnfp->mtnf_link)) {
71988275SEric Cheng 		cmn_err(CE_WARN,
71998275SEric Cheng 		    "mac_client_tx_notify_remove: callback not "
72008275SEric Cheng 		    "found, mcip 0x%p mtnfp 0x%p", (void *)mcip, (void *)mtnfp);
72018275SEric Cheng 		return;
72028275SEric Cheng 	}
72038275SEric Cheng 
72048275SEric Cheng 	mcbi = &mcip->mci_tx_notify_cb_info;
72058275SEric Cheng 	cblist = &mcip->mci_tx_notify_cb_list;
72068275SEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
72078275SEric Cheng 	if (mac_callback_remove(mcbi, cblist, &mtnfp->mtnf_link))
72088275SEric Cheng 		kmem_free(mtnfp, sizeof (mac_tx_notify_cb_t));
72098275SEric Cheng 	else
72108275SEric Cheng 		mac_callback_remove_wait(&mcip->mci_tx_notify_cb_info);
72118275SEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
72128275SEric Cheng }
72138275SEric Cheng 
72148275SEric Cheng /*
72158275SEric Cheng  * mac_client_tx_notify():
72168275SEric Cheng  * call to add and remove flow control callback routine.
72178275SEric Cheng  */
72188275SEric Cheng mac_tx_notify_handle_t
mac_client_tx_notify(mac_client_handle_t mch,mac_tx_notify_t callb_func,void * ptr)72198275SEric Cheng mac_client_tx_notify(mac_client_handle_t mch, mac_tx_notify_t callb_func,
72208275SEric Cheng     void *ptr)
72218275SEric Cheng {
72228275SEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
72238275SEric Cheng 	mac_tx_notify_cb_t	*mtnfp = NULL;
72248275SEric Cheng 
72258275SEric Cheng 	i_mac_perim_enter(mcip->mci_mip);
72268275SEric Cheng 
72278275SEric Cheng 	if (callb_func != NULL) {
72288275SEric Cheng 		/* Add a notify callback */
72298275SEric Cheng 		mtnfp = mac_client_tx_notify_add(mcip, callb_func, ptr);
72308275SEric Cheng 	} else {
72318275SEric Cheng 		mac_client_tx_notify_remove(mcip, (mac_tx_notify_cb_t *)ptr);
72328275SEric Cheng 	}
72338275SEric Cheng 	i_mac_perim_exit(mcip->mci_mip);
72348275SEric Cheng 
72358275SEric Cheng 	return ((mac_tx_notify_handle_t)mtnfp);
72368275SEric Cheng }
723710491SRishi.Srivatsavai@Sun.COM 
723810491SRishi.Srivatsavai@Sun.COM void
mac_bridge_vectors(mac_bridge_tx_t txf,mac_bridge_rx_t rxf,mac_bridge_ref_t reff,mac_bridge_ls_t lsf)723910491SRishi.Srivatsavai@Sun.COM mac_bridge_vectors(mac_bridge_tx_t txf, mac_bridge_rx_t rxf,
724010491SRishi.Srivatsavai@Sun.COM     mac_bridge_ref_t reff, mac_bridge_ls_t lsf)
724110491SRishi.Srivatsavai@Sun.COM {
724210491SRishi.Srivatsavai@Sun.COM 	mac_bridge_tx_cb = txf;
724310491SRishi.Srivatsavai@Sun.COM 	mac_bridge_rx_cb = rxf;
724410491SRishi.Srivatsavai@Sun.COM 	mac_bridge_ref_cb = reff;
724510491SRishi.Srivatsavai@Sun.COM 	mac_bridge_ls_cb = lsf;
724610491SRishi.Srivatsavai@Sun.COM }
724710491SRishi.Srivatsavai@Sun.COM 
724810491SRishi.Srivatsavai@Sun.COM int
mac_bridge_set(mac_handle_t mh,mac_handle_t link)724910491SRishi.Srivatsavai@Sun.COM mac_bridge_set(mac_handle_t mh, mac_handle_t link)
725010491SRishi.Srivatsavai@Sun.COM {
725110491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
725210491SRishi.Srivatsavai@Sun.COM 	int retv;
725310491SRishi.Srivatsavai@Sun.COM 
725410491SRishi.Srivatsavai@Sun.COM 	mutex_enter(&mip->mi_bridge_lock);
725510491SRishi.Srivatsavai@Sun.COM 	if (mip->mi_bridge_link == NULL) {
725610491SRishi.Srivatsavai@Sun.COM 		mip->mi_bridge_link = link;
725710491SRishi.Srivatsavai@Sun.COM 		retv = 0;
725810491SRishi.Srivatsavai@Sun.COM 	} else {
725910491SRishi.Srivatsavai@Sun.COM 		retv = EBUSY;
726010491SRishi.Srivatsavai@Sun.COM 	}
726110491SRishi.Srivatsavai@Sun.COM 	mutex_exit(&mip->mi_bridge_lock);
726210491SRishi.Srivatsavai@Sun.COM 	if (retv == 0) {
726310491SRishi.Srivatsavai@Sun.COM 		mac_poll_state_change(mh, B_FALSE);
726410491SRishi.Srivatsavai@Sun.COM 		mac_capab_update(mh);
726510491SRishi.Srivatsavai@Sun.COM 	}
726610491SRishi.Srivatsavai@Sun.COM 	return (retv);
726710491SRishi.Srivatsavai@Sun.COM }
726810491SRishi.Srivatsavai@Sun.COM 
726910491SRishi.Srivatsavai@Sun.COM /*
727010491SRishi.Srivatsavai@Sun.COM  * Disable bridging on the indicated link.
727110491SRishi.Srivatsavai@Sun.COM  */
727210491SRishi.Srivatsavai@Sun.COM void
mac_bridge_clear(mac_handle_t mh,mac_handle_t link)727310491SRishi.Srivatsavai@Sun.COM mac_bridge_clear(mac_handle_t mh, mac_handle_t link)
727410491SRishi.Srivatsavai@Sun.COM {
727510491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
727610491SRishi.Srivatsavai@Sun.COM 
727710491SRishi.Srivatsavai@Sun.COM 	mutex_enter(&mip->mi_bridge_lock);
727810491SRishi.Srivatsavai@Sun.COM 	ASSERT(mip->mi_bridge_link == link);
727910491SRishi.Srivatsavai@Sun.COM 	mip->mi_bridge_link = NULL;
728010491SRishi.Srivatsavai@Sun.COM 	mutex_exit(&mip->mi_bridge_lock);
728110491SRishi.Srivatsavai@Sun.COM 	mac_poll_state_change(mh, B_TRUE);
728210491SRishi.Srivatsavai@Sun.COM 	mac_capab_update(mh);
728310491SRishi.Srivatsavai@Sun.COM }
728410491SRishi.Srivatsavai@Sun.COM 
728510491SRishi.Srivatsavai@Sun.COM void
mac_no_active(mac_handle_t mh)728610491SRishi.Srivatsavai@Sun.COM mac_no_active(mac_handle_t mh)
728710491SRishi.Srivatsavai@Sun.COM {
728810491SRishi.Srivatsavai@Sun.COM 	mac_impl_t *mip = (mac_impl_t *)mh;
728910491SRishi.Srivatsavai@Sun.COM 
729010491SRishi.Srivatsavai@Sun.COM 	i_mac_perim_enter(mip);
729110491SRishi.Srivatsavai@Sun.COM 	mip->mi_state_flags |= MIS_NO_ACTIVE;
729210491SRishi.Srivatsavai@Sun.COM 	i_mac_perim_exit(mip);
729310491SRishi.Srivatsavai@Sun.COM }
729411878SVenu.Iyer@Sun.COM 
729511878SVenu.Iyer@Sun.COM /*
729611878SVenu.Iyer@Sun.COM  * Walk the primary VLAN clients whenever the primary's rings property
729711878SVenu.Iyer@Sun.COM  * changes and update the mac_resource_props_t for the VLAN's client.
729811878SVenu.Iyer@Sun.COM  * We need to do this since we don't support setting these properties
729911878SVenu.Iyer@Sun.COM  * on the primary's VLAN clients, but the VLAN clients have to
730011878SVenu.Iyer@Sun.COM  * follow the primary w.r.t the rings property;
730111878SVenu.Iyer@Sun.COM  */
730211878SVenu.Iyer@Sun.COM void
mac_set_prim_vlan_rings(mac_impl_t * mip,mac_resource_props_t * mrp)730311878SVenu.Iyer@Sun.COM mac_set_prim_vlan_rings(mac_impl_t  *mip, mac_resource_props_t *mrp)
730411878SVenu.Iyer@Sun.COM {
730511878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*vmcip;
730611878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*vmrp;
730711878SVenu.Iyer@Sun.COM 
730811878SVenu.Iyer@Sun.COM 	for (vmcip = mip->mi_clients_list; vmcip != NULL;
730911878SVenu.Iyer@Sun.COM 	    vmcip = vmcip->mci_client_next) {
731011878SVenu.Iyer@Sun.COM 		if (!(vmcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) ||
731111878SVenu.Iyer@Sun.COM 		    mac_client_vid((mac_client_handle_t)vmcip) ==
731211878SVenu.Iyer@Sun.COM 		    VLAN_ID_NONE) {
731311878SVenu.Iyer@Sun.COM 			continue;
731411878SVenu.Iyer@Sun.COM 		}
731511878SVenu.Iyer@Sun.COM 		vmrp = MCIP_RESOURCE_PROPS(vmcip);
731611878SVenu.Iyer@Sun.COM 
731711878SVenu.Iyer@Sun.COM 		vmrp->mrp_nrxrings =  mrp->mrp_nrxrings;
731811878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_RX_RINGS)
731911878SVenu.Iyer@Sun.COM 			vmrp->mrp_mask |= MRP_RX_RINGS;
732011878SVenu.Iyer@Sun.COM 		else if (vmrp->mrp_mask & MRP_RX_RINGS)
732111878SVenu.Iyer@Sun.COM 			vmrp->mrp_mask &= ~MRP_RX_RINGS;
732211878SVenu.Iyer@Sun.COM 
732311878SVenu.Iyer@Sun.COM 		vmrp->mrp_ntxrings =  mrp->mrp_ntxrings;
732411878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_TX_RINGS)
732511878SVenu.Iyer@Sun.COM 			vmrp->mrp_mask |= MRP_TX_RINGS;
732611878SVenu.Iyer@Sun.COM 		else if (vmrp->mrp_mask & MRP_TX_RINGS)
732711878SVenu.Iyer@Sun.COM 			vmrp->mrp_mask &= ~MRP_TX_RINGS;
732811878SVenu.Iyer@Sun.COM 
732911878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_RXRINGS_UNSPEC)
733011878SVenu.Iyer@Sun.COM 			vmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
733111878SVenu.Iyer@Sun.COM 		else
733211878SVenu.Iyer@Sun.COM 			vmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
733311878SVenu.Iyer@Sun.COM 
733411878SVenu.Iyer@Sun.COM 		if (mrp->mrp_mask & MRP_TXRINGS_UNSPEC)
733511878SVenu.Iyer@Sun.COM 			vmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
733611878SVenu.Iyer@Sun.COM 		else
733711878SVenu.Iyer@Sun.COM 			vmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
733811878SVenu.Iyer@Sun.COM 	}
733911878SVenu.Iyer@Sun.COM }
734011878SVenu.Iyer@Sun.COM 
734111878SVenu.Iyer@Sun.COM /*
734211878SVenu.Iyer@Sun.COM  * We are adding or removing ring(s) from a group. The source for taking
734311878SVenu.Iyer@Sun.COM  * rings is the default group. The destination for giving rings back is
734411878SVenu.Iyer@Sun.COM  * the default group.
734511878SVenu.Iyer@Sun.COM  */
734611878SVenu.Iyer@Sun.COM int
mac_group_ring_modify(mac_client_impl_t * mcip,mac_group_t * group,mac_group_t * defgrp)734711878SVenu.Iyer@Sun.COM mac_group_ring_modify(mac_client_impl_t *mcip, mac_group_t *group,
734811878SVenu.Iyer@Sun.COM     mac_group_t *defgrp)
734911878SVenu.Iyer@Sun.COM {
735011878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
735111878SVenu.Iyer@Sun.COM 	uint_t			modify;
735211878SVenu.Iyer@Sun.COM 	int			count;
735311878SVenu.Iyer@Sun.COM 	mac_ring_t		*ring;
735411878SVenu.Iyer@Sun.COM 	mac_ring_t		*next;
735511878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
735611878SVenu.Iyer@Sun.COM 	mac_ring_t		**rings;
735711878SVenu.Iyer@Sun.COM 	uint_t			ringcnt;
735811878SVenu.Iyer@Sun.COM 	int			i = 0;
735911878SVenu.Iyer@Sun.COM 	boolean_t		rx_group = group->mrg_type == MAC_RING_TYPE_RX;
736011878SVenu.Iyer@Sun.COM 	int			start;
736111878SVenu.Iyer@Sun.COM 	int			end;
736211878SVenu.Iyer@Sun.COM 	mac_group_t		*tgrp;
736311878SVenu.Iyer@Sun.COM 	int			j;
736411878SVenu.Iyer@Sun.COM 	int			rv = 0;
736511878SVenu.Iyer@Sun.COM 
736611878SVenu.Iyer@Sun.COM 	/*
736711878SVenu.Iyer@Sun.COM 	 * If we are asked for just a group, we give 1 ring, else
736811878SVenu.Iyer@Sun.COM 	 * the specified number of rings.
736911878SVenu.Iyer@Sun.COM 	 */
737011878SVenu.Iyer@Sun.COM 	if (rx_group) {
737111878SVenu.Iyer@Sun.COM 		ringcnt = (mrp->mrp_mask & MRP_RXRINGS_UNSPEC) ? 1:
737211878SVenu.Iyer@Sun.COM 		    mrp->mrp_nrxrings;
737311878SVenu.Iyer@Sun.COM 	} else {
737411878SVenu.Iyer@Sun.COM 		ringcnt = (mrp->mrp_mask & MRP_TXRINGS_UNSPEC) ? 1:
737511878SVenu.Iyer@Sun.COM 		    mrp->mrp_ntxrings;
737611878SVenu.Iyer@Sun.COM 	}
737711878SVenu.Iyer@Sun.COM 
737811878SVenu.Iyer@Sun.COM 	/* don't allow modifying rings for a share for now. */
737911878SVenu.Iyer@Sun.COM 	ASSERT(mcip->mci_share == NULL);
738011878SVenu.Iyer@Sun.COM 
738111878SVenu.Iyer@Sun.COM 	if (ringcnt == group->mrg_cur_count)
738211878SVenu.Iyer@Sun.COM 		return (0);
738311878SVenu.Iyer@Sun.COM 
738411878SVenu.Iyer@Sun.COM 	if (group->mrg_cur_count > ringcnt) {
738511878SVenu.Iyer@Sun.COM 		modify = group->mrg_cur_count - ringcnt;
738611878SVenu.Iyer@Sun.COM 		if (rx_group) {
738711878SVenu.Iyer@Sun.COM 			if (mip->mi_rx_donor_grp == group) {
738811878SVenu.Iyer@Sun.COM 				ASSERT(mac_is_primary_client(mcip));
738911878SVenu.Iyer@Sun.COM 				mip->mi_rx_donor_grp = defgrp;
739011878SVenu.Iyer@Sun.COM 			} else {
739111878SVenu.Iyer@Sun.COM 				defgrp = mip->mi_rx_donor_grp;
739211878SVenu.Iyer@Sun.COM 			}
739311878SVenu.Iyer@Sun.COM 		}
739411878SVenu.Iyer@Sun.COM 		ring = group->mrg_rings;
739511878SVenu.Iyer@Sun.COM 		rings = kmem_alloc(modify * sizeof (mac_ring_handle_t),
739611878SVenu.Iyer@Sun.COM 		    KM_SLEEP);
739711878SVenu.Iyer@Sun.COM 		j = 0;
739811878SVenu.Iyer@Sun.COM 		for (count = 0; count < modify; count++) {
739911878SVenu.Iyer@Sun.COM 			next = ring->mr_next;
740011878SVenu.Iyer@Sun.COM 			rv = mac_group_mov_ring(mip, defgrp, ring);
740111878SVenu.Iyer@Sun.COM 			if (rv != 0) {
740211878SVenu.Iyer@Sun.COM 				/* cleanup on failure */
740311878SVenu.Iyer@Sun.COM 				for (j = 0; j < count; j++) {
740411878SVenu.Iyer@Sun.COM 					(void) mac_group_mov_ring(mip, group,
740511878SVenu.Iyer@Sun.COM 					    rings[j]);
740611878SVenu.Iyer@Sun.COM 				}
740711878SVenu.Iyer@Sun.COM 				break;
740811878SVenu.Iyer@Sun.COM 			}
740911878SVenu.Iyer@Sun.COM 			rings[j++] = ring;
741011878SVenu.Iyer@Sun.COM 			ring = next;
741111878SVenu.Iyer@Sun.COM 		}
741211878SVenu.Iyer@Sun.COM 		kmem_free(rings, modify * sizeof (mac_ring_handle_t));
741311878SVenu.Iyer@Sun.COM 		return (rv);
741411878SVenu.Iyer@Sun.COM 	}
741511878SVenu.Iyer@Sun.COM 	if (ringcnt >= MAX_RINGS_PER_GROUP)
741611878SVenu.Iyer@Sun.COM 		return (EINVAL);
741711878SVenu.Iyer@Sun.COM 
741811878SVenu.Iyer@Sun.COM 	modify = ringcnt - group->mrg_cur_count;
741911878SVenu.Iyer@Sun.COM 
742011878SVenu.Iyer@Sun.COM 	if (rx_group) {
742111878SVenu.Iyer@Sun.COM 		if (group != mip->mi_rx_donor_grp)
742211878SVenu.Iyer@Sun.COM 			defgrp = mip->mi_rx_donor_grp;
742311878SVenu.Iyer@Sun.COM 		else
742411878SVenu.Iyer@Sun.COM 			/*
742511878SVenu.Iyer@Sun.COM 			 * This is the donor group with all the remaining
742611878SVenu.Iyer@Sun.COM 			 * rings. Default group now gets to be the donor
742711878SVenu.Iyer@Sun.COM 			 */
742811878SVenu.Iyer@Sun.COM 			mip->mi_rx_donor_grp = defgrp;
742911878SVenu.Iyer@Sun.COM 		start = 1;
743011878SVenu.Iyer@Sun.COM 		end = mip->mi_rx_group_count;
743111878SVenu.Iyer@Sun.COM 	} else {
743211878SVenu.Iyer@Sun.COM 		start = 0;
743311878SVenu.Iyer@Sun.COM 		end = mip->mi_tx_group_count - 1;
743411878SVenu.Iyer@Sun.COM 	}
743511878SVenu.Iyer@Sun.COM 	/*
743611878SVenu.Iyer@Sun.COM 	 * If the default doesn't have any rings, lets see if we can
743711878SVenu.Iyer@Sun.COM 	 * take rings given to an h/w client that doesn't need it.
743811878SVenu.Iyer@Sun.COM 	 * For now, we just see if there is  any one client that can donate
743911878SVenu.Iyer@Sun.COM 	 * all the required rings.
744011878SVenu.Iyer@Sun.COM 	 */
744111878SVenu.Iyer@Sun.COM 	if (defgrp->mrg_cur_count < (modify + 1)) {
744211878SVenu.Iyer@Sun.COM 		for (i = start; i < end; i++) {
744311878SVenu.Iyer@Sun.COM 			if (rx_group) {
744411878SVenu.Iyer@Sun.COM 				tgrp = &mip->mi_rx_groups[i];
744511878SVenu.Iyer@Sun.COM 				if (tgrp == group || tgrp->mrg_state <
744611878SVenu.Iyer@Sun.COM 				    MAC_GROUP_STATE_RESERVED) {
744711878SVenu.Iyer@Sun.COM 					continue;
744811878SVenu.Iyer@Sun.COM 				}
744911878SVenu.Iyer@Sun.COM 				mcip = MAC_GROUP_ONLY_CLIENT(tgrp);
745011878SVenu.Iyer@Sun.COM 				if (mcip == NULL)
745111878SVenu.Iyer@Sun.COM 					mcip = mac_get_grp_primary(tgrp);
745211878SVenu.Iyer@Sun.COM 				ASSERT(mcip != NULL);
745311878SVenu.Iyer@Sun.COM 				mrp = MCIP_RESOURCE_PROPS(mcip);
745411878SVenu.Iyer@Sun.COM 				if ((mrp->mrp_mask & MRP_RX_RINGS) != 0)
745511878SVenu.Iyer@Sun.COM 					continue;
745611878SVenu.Iyer@Sun.COM 				if ((tgrp->mrg_cur_count +
745711878SVenu.Iyer@Sun.COM 				    defgrp->mrg_cur_count) < (modify + 1)) {
745811878SVenu.Iyer@Sun.COM 					continue;
745911878SVenu.Iyer@Sun.COM 				}
746011878SVenu.Iyer@Sun.COM 				if (mac_rx_switch_group(mcip, tgrp,
746111878SVenu.Iyer@Sun.COM 				    defgrp) != 0) {
746211878SVenu.Iyer@Sun.COM 					return (ENOSPC);
746311878SVenu.Iyer@Sun.COM 				}
746411878SVenu.Iyer@Sun.COM 			} else {
746511878SVenu.Iyer@Sun.COM 				tgrp = &mip->mi_tx_groups[i];
746611878SVenu.Iyer@Sun.COM 				if (tgrp == group || tgrp->mrg_state <
746711878SVenu.Iyer@Sun.COM 				    MAC_GROUP_STATE_RESERVED) {
746811878SVenu.Iyer@Sun.COM 					continue;
746911878SVenu.Iyer@Sun.COM 				}
747011878SVenu.Iyer@Sun.COM 				mcip = MAC_GROUP_ONLY_CLIENT(tgrp);
747111878SVenu.Iyer@Sun.COM 				if (mcip == NULL)
747211878SVenu.Iyer@Sun.COM 					mcip = mac_get_grp_primary(tgrp);
747311878SVenu.Iyer@Sun.COM 				mrp = MCIP_RESOURCE_PROPS(mcip);
747411878SVenu.Iyer@Sun.COM 				if ((mrp->mrp_mask & MRP_TX_RINGS) != 0)
747511878SVenu.Iyer@Sun.COM 					continue;
747611878SVenu.Iyer@Sun.COM 				if ((tgrp->mrg_cur_count +
747711878SVenu.Iyer@Sun.COM 				    defgrp->mrg_cur_count) < (modify + 1)) {
747811878SVenu.Iyer@Sun.COM 					continue;
747911878SVenu.Iyer@Sun.COM 				}
748011878SVenu.Iyer@Sun.COM 				/* OK, we can switch this to s/w */
748111878SVenu.Iyer@Sun.COM 				mac_tx_client_quiesce(
748211878SVenu.Iyer@Sun.COM 				    (mac_client_handle_t)mcip);
748311878SVenu.Iyer@Sun.COM 				mac_tx_switch_group(mcip, tgrp, defgrp);
748411878SVenu.Iyer@Sun.COM 				mac_tx_client_restart(
748511878SVenu.Iyer@Sun.COM 				    (mac_client_handle_t)mcip);
748611878SVenu.Iyer@Sun.COM 			}
748711878SVenu.Iyer@Sun.COM 		}
748811878SVenu.Iyer@Sun.COM 		if (defgrp->mrg_cur_count < (modify + 1))
748911878SVenu.Iyer@Sun.COM 			return (ENOSPC);
749011878SVenu.Iyer@Sun.COM 	}
749111878SVenu.Iyer@Sun.COM 	if ((rv = i_mac_group_allocate_rings(mip, group->mrg_type, defgrp,
749211878SVenu.Iyer@Sun.COM 	    group, mcip->mci_share, modify)) != 0) {
749311878SVenu.Iyer@Sun.COM 		return (rv);
749411878SVenu.Iyer@Sun.COM 	}
749511878SVenu.Iyer@Sun.COM 	return (0);
749611878SVenu.Iyer@Sun.COM }
749711878SVenu.Iyer@Sun.COM 
749811878SVenu.Iyer@Sun.COM /*
749911878SVenu.Iyer@Sun.COM  * Given the poolname in mac_resource_props, find the cpupart
750011878SVenu.Iyer@Sun.COM  * that is associated with this pool.  The cpupart will be used
750111878SVenu.Iyer@Sun.COM  * later for finding the cpus to be bound to the networking threads.
750211878SVenu.Iyer@Sun.COM  *
750311878SVenu.Iyer@Sun.COM  * use_default is set B_TRUE if pools are enabled and pool_default
750411878SVenu.Iyer@Sun.COM  * is returned.  This avoids a 2nd lookup to set the poolname
750511878SVenu.Iyer@Sun.COM  * for pool-effective.
750611878SVenu.Iyer@Sun.COM  *
750711878SVenu.Iyer@Sun.COM  * returns:
750811878SVenu.Iyer@Sun.COM  *
750911878SVenu.Iyer@Sun.COM  *    NULL -   pools are disabled or if the 'cpus' property is set.
751011878SVenu.Iyer@Sun.COM  *    cpupart of pool_default  - pools are enabled and the pool
751111878SVenu.Iyer@Sun.COM  *             is not available or poolname is blank
751211878SVenu.Iyer@Sun.COM  *    cpupart of named pool    - pools are enabled and the pool
751311878SVenu.Iyer@Sun.COM  *             is available.
751411878SVenu.Iyer@Sun.COM  */
751511878SVenu.Iyer@Sun.COM cpupart_t *
mac_pset_find(mac_resource_props_t * mrp,boolean_t * use_default)751611878SVenu.Iyer@Sun.COM mac_pset_find(mac_resource_props_t *mrp, boolean_t *use_default)
751711878SVenu.Iyer@Sun.COM {
751811878SVenu.Iyer@Sun.COM 	pool_t		*pool;
751911878SVenu.Iyer@Sun.COM 	cpupart_t	*cpupart;
752011878SVenu.Iyer@Sun.COM 
752111878SVenu.Iyer@Sun.COM 	*use_default = B_FALSE;
752211878SVenu.Iyer@Sun.COM 
752311878SVenu.Iyer@Sun.COM 	/* CPUs property is set */
752411878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & MRP_CPUS)
752511878SVenu.Iyer@Sun.COM 		return (NULL);
752611878SVenu.Iyer@Sun.COM 
752711878SVenu.Iyer@Sun.COM 	ASSERT(pool_lock_held());
752811878SVenu.Iyer@Sun.COM 
752911878SVenu.Iyer@Sun.COM 	/* Pools are disabled, no pset */
753011878SVenu.Iyer@Sun.COM 	if (pool_state == POOL_DISABLED)
753111878SVenu.Iyer@Sun.COM 		return (NULL);
753211878SVenu.Iyer@Sun.COM 
753311878SVenu.Iyer@Sun.COM 	/* Pools property is set */
753411878SVenu.Iyer@Sun.COM 	if (mrp->mrp_mask & MRP_POOL) {
753511878SVenu.Iyer@Sun.COM 		if ((pool = pool_lookup_pool_by_name(mrp->mrp_pool)) == NULL) {
753611878SVenu.Iyer@Sun.COM 			/* Pool not found */
753711878SVenu.Iyer@Sun.COM 			DTRACE_PROBE1(mac_pset_find_no_pool, char *,
753811878SVenu.Iyer@Sun.COM 			    mrp->mrp_pool);
753911878SVenu.Iyer@Sun.COM 			*use_default = B_TRUE;
754011878SVenu.Iyer@Sun.COM 			pool = pool_default;
754111878SVenu.Iyer@Sun.COM 		}
754211878SVenu.Iyer@Sun.COM 	/* Pools property is not set */
754311878SVenu.Iyer@Sun.COM 	} else {
754411878SVenu.Iyer@Sun.COM 		*use_default = B_TRUE;
754511878SVenu.Iyer@Sun.COM 		pool = pool_default;
754611878SVenu.Iyer@Sun.COM 	}
754711878SVenu.Iyer@Sun.COM 
754811878SVenu.Iyer@Sun.COM 	/* Find the CPU pset that corresponds to the pool */
754911878SVenu.Iyer@Sun.COM 	mutex_enter(&cpu_lock);
755011878SVenu.Iyer@Sun.COM 	if ((cpupart = cpupart_find(pool->pool_pset->pset_id)) == NULL) {
755111878SVenu.Iyer@Sun.COM 		DTRACE_PROBE1(mac_find_pset_no_pset, psetid_t,
755211878SVenu.Iyer@Sun.COM 		    pool->pool_pset->pset_id);
755311878SVenu.Iyer@Sun.COM 	}
755411878SVenu.Iyer@Sun.COM 	mutex_exit(&cpu_lock);
755511878SVenu.Iyer@Sun.COM 
755611878SVenu.Iyer@Sun.COM 	return (cpupart);
755711878SVenu.Iyer@Sun.COM }
755811878SVenu.Iyer@Sun.COM 
755911878SVenu.Iyer@Sun.COM void
mac_set_pool_effective(boolean_t use_default,cpupart_t * cpupart,mac_resource_props_t * mrp,mac_resource_props_t * emrp)756011878SVenu.Iyer@Sun.COM mac_set_pool_effective(boolean_t use_default, cpupart_t *cpupart,
756111878SVenu.Iyer@Sun.COM     mac_resource_props_t *mrp, mac_resource_props_t *emrp)
756211878SVenu.Iyer@Sun.COM {
756311878SVenu.Iyer@Sun.COM 	ASSERT(pool_lock_held());
756411878SVenu.Iyer@Sun.COM 
756511878SVenu.Iyer@Sun.COM 	if (cpupart != NULL) {
756611878SVenu.Iyer@Sun.COM 		emrp->mrp_mask |= MRP_POOL;
756711878SVenu.Iyer@Sun.COM 		if (use_default) {
756811878SVenu.Iyer@Sun.COM 			(void) strcpy(emrp->mrp_pool,
756911878SVenu.Iyer@Sun.COM 			    "pool_default");
757011878SVenu.Iyer@Sun.COM 		} else {
757111878SVenu.Iyer@Sun.COM 			ASSERT(strlen(mrp->mrp_pool) != 0);
757211878SVenu.Iyer@Sun.COM 			(void) strcpy(emrp->mrp_pool,
757311878SVenu.Iyer@Sun.COM 			    mrp->mrp_pool);
757411878SVenu.Iyer@Sun.COM 		}
757511878SVenu.Iyer@Sun.COM 	} else {
757611878SVenu.Iyer@Sun.COM 		emrp->mrp_mask &= ~MRP_POOL;
757711878SVenu.Iyer@Sun.COM 		bzero(emrp->mrp_pool, MAXPATHLEN);
757811878SVenu.Iyer@Sun.COM 	}
757911878SVenu.Iyer@Sun.COM }
758011878SVenu.Iyer@Sun.COM 
758111878SVenu.Iyer@Sun.COM struct mac_pool_arg {
758211878SVenu.Iyer@Sun.COM 	char		mpa_poolname[MAXPATHLEN];
758311878SVenu.Iyer@Sun.COM 	pool_event_t	mpa_what;
758411878SVenu.Iyer@Sun.COM };
758511878SVenu.Iyer@Sun.COM 
758611878SVenu.Iyer@Sun.COM /*ARGSUSED*/
758711878SVenu.Iyer@Sun.COM static uint_t
mac_pool_link_update(mod_hash_key_t key,mod_hash_val_t * val,void * arg)758811878SVenu.Iyer@Sun.COM mac_pool_link_update(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
758911878SVenu.Iyer@Sun.COM {
759011878SVenu.Iyer@Sun.COM 	struct mac_pool_arg	*mpa = arg;
759111878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = (mac_impl_t *)val;
759211878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*mcip;
759311878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mrp, *emrp;
759411878SVenu.Iyer@Sun.COM 	boolean_t		pool_update = B_FALSE;
759511878SVenu.Iyer@Sun.COM 	boolean_t		pool_clear = B_FALSE;
759611878SVenu.Iyer@Sun.COM 	boolean_t		use_default = B_FALSE;
759711878SVenu.Iyer@Sun.COM 	cpupart_t		*cpupart = NULL;
759811878SVenu.Iyer@Sun.COM 
759911878SVenu.Iyer@Sun.COM 	mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
760011878SVenu.Iyer@Sun.COM 	i_mac_perim_enter(mip);
760111878SVenu.Iyer@Sun.COM 	for (mcip = mip->mi_clients_list; mcip != NULL;
760211878SVenu.Iyer@Sun.COM 	    mcip = mcip->mci_client_next) {
760311878SVenu.Iyer@Sun.COM 		pool_update = B_FALSE;
760411878SVenu.Iyer@Sun.COM 		pool_clear = B_FALSE;
760511878SVenu.Iyer@Sun.COM 		use_default = B_FALSE;
760611878SVenu.Iyer@Sun.COM 		mac_client_get_resources((mac_client_handle_t)mcip, mrp);
760711878SVenu.Iyer@Sun.COM 		emrp = MCIP_EFFECTIVE_PROPS(mcip);
760811878SVenu.Iyer@Sun.COM 
760911878SVenu.Iyer@Sun.COM 		/*
761011878SVenu.Iyer@Sun.COM 		 * When pools are enabled
761111878SVenu.Iyer@Sun.COM 		 */
761211878SVenu.Iyer@Sun.COM 		if ((mpa->mpa_what == POOL_E_ENABLE) &&
761311878SVenu.Iyer@Sun.COM 		    ((mrp->mrp_mask & MRP_CPUS) == 0)) {
761411878SVenu.Iyer@Sun.COM 			mrp->mrp_mask |= MRP_POOL;
761511878SVenu.Iyer@Sun.COM 			pool_update = B_TRUE;
761611878SVenu.Iyer@Sun.COM 		}
761711878SVenu.Iyer@Sun.COM 
761811878SVenu.Iyer@Sun.COM 		/*
761911878SVenu.Iyer@Sun.COM 		 * When pools are disabled
762011878SVenu.Iyer@Sun.COM 		 */
762111878SVenu.Iyer@Sun.COM 		if ((mpa->mpa_what == POOL_E_DISABLE) &&
762211878SVenu.Iyer@Sun.COM 		    ((mrp->mrp_mask & MRP_CPUS) == 0)) {
762311878SVenu.Iyer@Sun.COM 			mrp->mrp_mask |= MRP_POOL;
762411878SVenu.Iyer@Sun.COM 			pool_clear = B_TRUE;
762511878SVenu.Iyer@Sun.COM 		}
762611878SVenu.Iyer@Sun.COM 
762711878SVenu.Iyer@Sun.COM 		/*
762811878SVenu.Iyer@Sun.COM 		 * Look for links with the pool property set and the poolname
762911878SVenu.Iyer@Sun.COM 		 * matching the one which is changing.
763011878SVenu.Iyer@Sun.COM 		 */
763111878SVenu.Iyer@Sun.COM 		if (strcmp(mrp->mrp_pool, mpa->mpa_poolname) == 0) {
763211878SVenu.Iyer@Sun.COM 			/*
763311878SVenu.Iyer@Sun.COM 			 * The pool associated with the link has changed.
763411878SVenu.Iyer@Sun.COM 			 */
763511878SVenu.Iyer@Sun.COM 			if (mpa->mpa_what == POOL_E_CHANGE) {
763611878SVenu.Iyer@Sun.COM 				mrp->mrp_mask |= MRP_POOL;
763711878SVenu.Iyer@Sun.COM 				pool_update = B_TRUE;
763811878SVenu.Iyer@Sun.COM 			}
763911878SVenu.Iyer@Sun.COM 		}
764011878SVenu.Iyer@Sun.COM 
764111878SVenu.Iyer@Sun.COM 		/*
764211878SVenu.Iyer@Sun.COM 		 * This link is associated with pool_default and
764311878SVenu.Iyer@Sun.COM 		 * pool_default has changed.
764411878SVenu.Iyer@Sun.COM 		 */
764511878SVenu.Iyer@Sun.COM 		if ((mpa->mpa_what == POOL_E_CHANGE) &&
764611878SVenu.Iyer@Sun.COM 		    (strcmp(emrp->mrp_pool, "pool_default") == 0) &&
764711878SVenu.Iyer@Sun.COM 		    (strcmp(mpa->mpa_poolname, "pool_default") == 0)) {
764811878SVenu.Iyer@Sun.COM 			mrp->mrp_mask |= MRP_POOL;
764911878SVenu.Iyer@Sun.COM 			pool_update = B_TRUE;
765011878SVenu.Iyer@Sun.COM 		}
765111878SVenu.Iyer@Sun.COM 
765211878SVenu.Iyer@Sun.COM 		/*
765311878SVenu.Iyer@Sun.COM 		 * Get new list of cpus for the pool, bind network
765411878SVenu.Iyer@Sun.COM 		 * threads to new list of cpus and update resources.
765511878SVenu.Iyer@Sun.COM 		 */
765611878SVenu.Iyer@Sun.COM 		if (pool_update) {
765711878SVenu.Iyer@Sun.COM 			if (MCIP_DATAPATH_SETUP(mcip)) {
765811878SVenu.Iyer@Sun.COM 				pool_lock();
765911878SVenu.Iyer@Sun.COM 				cpupart = mac_pset_find(mrp, &use_default);
766011878SVenu.Iyer@Sun.COM 				mac_fanout_setup(mcip, mcip->mci_flent, mrp,
766111878SVenu.Iyer@Sun.COM 				    mac_rx_deliver, mcip, NULL, cpupart);
766211878SVenu.Iyer@Sun.COM 				mac_set_pool_effective(use_default, cpupart,
766311878SVenu.Iyer@Sun.COM 				    mrp, emrp);
766411878SVenu.Iyer@Sun.COM 				pool_unlock();
766511878SVenu.Iyer@Sun.COM 			}
766611878SVenu.Iyer@Sun.COM 			mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip),
766711878SVenu.Iyer@Sun.COM 			    B_FALSE);
766811878SVenu.Iyer@Sun.COM 		}
766911878SVenu.Iyer@Sun.COM 
767011878SVenu.Iyer@Sun.COM 		/*
767111878SVenu.Iyer@Sun.COM 		 * Clear the effective pool and bind network threads
767211878SVenu.Iyer@Sun.COM 		 * to any available CPU.
767311878SVenu.Iyer@Sun.COM 		 */
767411878SVenu.Iyer@Sun.COM 		if (pool_clear) {
767511878SVenu.Iyer@Sun.COM 			if (MCIP_DATAPATH_SETUP(mcip)) {
767611878SVenu.Iyer@Sun.COM 				emrp->mrp_mask &= ~MRP_POOL;
767711878SVenu.Iyer@Sun.COM 				bzero(emrp->mrp_pool, MAXPATHLEN);
767811878SVenu.Iyer@Sun.COM 				mac_fanout_setup(mcip, mcip->mci_flent, mrp,
767911878SVenu.Iyer@Sun.COM 				    mac_rx_deliver, mcip, NULL, NULL);
768011878SVenu.Iyer@Sun.COM 			}
768111878SVenu.Iyer@Sun.COM 			mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip),
768211878SVenu.Iyer@Sun.COM 			    B_FALSE);
768311878SVenu.Iyer@Sun.COM 		}
768411878SVenu.Iyer@Sun.COM 	}
768511878SVenu.Iyer@Sun.COM 	i_mac_perim_exit(mip);
768611878SVenu.Iyer@Sun.COM 	kmem_free(mrp, sizeof (*mrp));
768711878SVenu.Iyer@Sun.COM 	return (MH_WALK_CONTINUE);
768811878SVenu.Iyer@Sun.COM }
768911878SVenu.Iyer@Sun.COM 
769011878SVenu.Iyer@Sun.COM static void
mac_pool_update(void * arg)769111878SVenu.Iyer@Sun.COM mac_pool_update(void *arg)
769211878SVenu.Iyer@Sun.COM {
769311878SVenu.Iyer@Sun.COM 	mod_hash_walk(i_mac_impl_hash, mac_pool_link_update, arg);
769411878SVenu.Iyer@Sun.COM 	kmem_free(arg, sizeof (struct mac_pool_arg));
769511878SVenu.Iyer@Sun.COM }
769611878SVenu.Iyer@Sun.COM 
769711878SVenu.Iyer@Sun.COM /*
769811878SVenu.Iyer@Sun.COM  * Callback function to be executed when a noteworthy pool event
769911878SVenu.Iyer@Sun.COM  * takes place.
770011878SVenu.Iyer@Sun.COM  */
770111878SVenu.Iyer@Sun.COM /* ARGSUSED */
770211878SVenu.Iyer@Sun.COM static void
mac_pool_event_cb(pool_event_t what,poolid_t id,void * arg)770311878SVenu.Iyer@Sun.COM mac_pool_event_cb(pool_event_t what, poolid_t id, void *arg)
770411878SVenu.Iyer@Sun.COM {
770511878SVenu.Iyer@Sun.COM 	pool_t			*pool;
770611878SVenu.Iyer@Sun.COM 	char			*poolname = NULL;
770711878SVenu.Iyer@Sun.COM 	struct mac_pool_arg	*mpa;
770811878SVenu.Iyer@Sun.COM 
770911878SVenu.Iyer@Sun.COM 	pool_lock();
771011878SVenu.Iyer@Sun.COM 	mpa = kmem_zalloc(sizeof (struct mac_pool_arg), KM_SLEEP);
771111878SVenu.Iyer@Sun.COM 
771211878SVenu.Iyer@Sun.COM 	switch (what) {
771311878SVenu.Iyer@Sun.COM 	case POOL_E_ENABLE:
771411878SVenu.Iyer@Sun.COM 	case POOL_E_DISABLE:
771511878SVenu.Iyer@Sun.COM 		break;
771611878SVenu.Iyer@Sun.COM 
771711878SVenu.Iyer@Sun.COM 	case POOL_E_CHANGE:
771811878SVenu.Iyer@Sun.COM 		pool = pool_lookup_pool_by_id(id);
771911878SVenu.Iyer@Sun.COM 		if (pool == NULL) {
772011878SVenu.Iyer@Sun.COM 			kmem_free(mpa, sizeof (struct mac_pool_arg));
772111878SVenu.Iyer@Sun.COM 			pool_unlock();
772211878SVenu.Iyer@Sun.COM 			return;
772311878SVenu.Iyer@Sun.COM 		}
772411878SVenu.Iyer@Sun.COM 		pool_get_name(pool, &poolname);
772511878SVenu.Iyer@Sun.COM 		(void) strlcpy(mpa->mpa_poolname, poolname,
772611878SVenu.Iyer@Sun.COM 		    sizeof (mpa->mpa_poolname));
772711878SVenu.Iyer@Sun.COM 		break;
772811878SVenu.Iyer@Sun.COM 
772911878SVenu.Iyer@Sun.COM 	default:
773011878SVenu.Iyer@Sun.COM 		kmem_free(mpa, sizeof (struct mac_pool_arg));
773111878SVenu.Iyer@Sun.COM 		pool_unlock();
773211878SVenu.Iyer@Sun.COM 		return;
773311878SVenu.Iyer@Sun.COM 	}
773411878SVenu.Iyer@Sun.COM 	pool_unlock();
773511878SVenu.Iyer@Sun.COM 
773611878SVenu.Iyer@Sun.COM 	mpa->mpa_what = what;
773711878SVenu.Iyer@Sun.COM 
773811878SVenu.Iyer@Sun.COM 	mac_pool_update(mpa);
773911878SVenu.Iyer@Sun.COM }
774011878SVenu.Iyer@Sun.COM 
774111878SVenu.Iyer@Sun.COM /*
774211878SVenu.Iyer@Sun.COM  * Set effective rings property. This could be called from datapath_setup/
774311878SVenu.Iyer@Sun.COM  * datapath_teardown or set-linkprop.
774411878SVenu.Iyer@Sun.COM  * If the group is reserved we just go ahead and set the effective rings.
774511878SVenu.Iyer@Sun.COM  * Additionally, for TX this could mean the default  group has lost/gained
774611878SVenu.Iyer@Sun.COM  * some rings, so if the default group is reserved, we need to adjust the
774711878SVenu.Iyer@Sun.COM  * effective rings for the default group clients. For RX, if we are working
774811878SVenu.Iyer@Sun.COM  * with the non-default group, we just need * to reset the effective props
774911878SVenu.Iyer@Sun.COM  * for the default group clients.
775011878SVenu.Iyer@Sun.COM  */
775111878SVenu.Iyer@Sun.COM void
mac_set_rings_effective(mac_client_impl_t * mcip)775211878SVenu.Iyer@Sun.COM mac_set_rings_effective(mac_client_impl_t *mcip)
775311878SVenu.Iyer@Sun.COM {
775411878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
775511878SVenu.Iyer@Sun.COM 	mac_group_t		*grp;
775611878SVenu.Iyer@Sun.COM 	mac_group_t		*defgrp;
775711878SVenu.Iyer@Sun.COM 	flow_entry_t		*flent = mcip->mci_flent;
775811878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*emrp = MCIP_EFFECTIVE_PROPS(mcip);
775911878SVenu.Iyer@Sun.COM 	mac_grp_client_t	*mgcp;
776011878SVenu.Iyer@Sun.COM 	mac_client_impl_t	*gmcip;
776111878SVenu.Iyer@Sun.COM 
776211878SVenu.Iyer@Sun.COM 	grp = flent->fe_rx_ring_group;
776311878SVenu.Iyer@Sun.COM 	if (grp != NULL) {
776411878SVenu.Iyer@Sun.COM 		defgrp = MAC_DEFAULT_RX_GROUP(mip);
776511878SVenu.Iyer@Sun.COM 		/*
776611878SVenu.Iyer@Sun.COM 		 * If we have reserved a group, set the effective rings
776711878SVenu.Iyer@Sun.COM 		 * to the ring count in the group.
776811878SVenu.Iyer@Sun.COM 		 */
776911878SVenu.Iyer@Sun.COM 		if (grp->mrg_state == MAC_GROUP_STATE_RESERVED) {
777011878SVenu.Iyer@Sun.COM 			emrp->mrp_mask |= MRP_RX_RINGS;
777111878SVenu.Iyer@Sun.COM 			emrp->mrp_nrxrings = grp->mrg_cur_count;
777211878SVenu.Iyer@Sun.COM 		}
777311878SVenu.Iyer@Sun.COM 
777411878SVenu.Iyer@Sun.COM 		/*
777511878SVenu.Iyer@Sun.COM 		 * We go through the clients in the shared group and
777611878SVenu.Iyer@Sun.COM 		 * reset the effective properties. It is possible this
777711878SVenu.Iyer@Sun.COM 		 * might have already been done for some client (i.e.
777811878SVenu.Iyer@Sun.COM 		 * if some client is being moved to a group that is
777911878SVenu.Iyer@Sun.COM 		 * already shared). The case where the default group is
778011878SVenu.Iyer@Sun.COM 		 * RESERVED is taken care of above (note in the RX side if
778111878SVenu.Iyer@Sun.COM 		 * there is a non-default group, the default group is always
778211878SVenu.Iyer@Sun.COM 		 * SHARED).
778311878SVenu.Iyer@Sun.COM 		 */
778411878SVenu.Iyer@Sun.COM 		if (grp != defgrp || grp->mrg_state == MAC_GROUP_STATE_SHARED) {
778511878SVenu.Iyer@Sun.COM 			if (grp->mrg_state == MAC_GROUP_STATE_SHARED)
778611878SVenu.Iyer@Sun.COM 				mgcp = grp->mrg_clients;
778711878SVenu.Iyer@Sun.COM 			else
778811878SVenu.Iyer@Sun.COM 				mgcp = defgrp->mrg_clients;
778911878SVenu.Iyer@Sun.COM 			while (mgcp != NULL) {
779011878SVenu.Iyer@Sun.COM 				gmcip = mgcp->mgc_client;
779111878SVenu.Iyer@Sun.COM 				emrp = MCIP_EFFECTIVE_PROPS(gmcip);
779211878SVenu.Iyer@Sun.COM 				if (emrp->mrp_mask & MRP_RX_RINGS) {
779311878SVenu.Iyer@Sun.COM 					emrp->mrp_mask &= ~MRP_RX_RINGS;
779411878SVenu.Iyer@Sun.COM 					emrp->mrp_nrxrings = 0;
779511878SVenu.Iyer@Sun.COM 				}
779611878SVenu.Iyer@Sun.COM 				mgcp = mgcp->mgc_next;
779711878SVenu.Iyer@Sun.COM 			}
779811878SVenu.Iyer@Sun.COM 		}
779911878SVenu.Iyer@Sun.COM 	}
780011878SVenu.Iyer@Sun.COM 
780111878SVenu.Iyer@Sun.COM 	/* Now the TX side */
780211878SVenu.Iyer@Sun.COM 	grp = flent->fe_tx_ring_group;
780311878SVenu.Iyer@Sun.COM 	if (grp != NULL) {
780411878SVenu.Iyer@Sun.COM 		defgrp = MAC_DEFAULT_TX_GROUP(mip);
780511878SVenu.Iyer@Sun.COM 
780611878SVenu.Iyer@Sun.COM 		if (grp->mrg_state == MAC_GROUP_STATE_RESERVED) {
780711878SVenu.Iyer@Sun.COM 			emrp->mrp_mask |= MRP_TX_RINGS;
780811878SVenu.Iyer@Sun.COM 			emrp->mrp_ntxrings = grp->mrg_cur_count;
780911878SVenu.Iyer@Sun.COM 		} else if (grp->mrg_state == MAC_GROUP_STATE_SHARED) {
781011878SVenu.Iyer@Sun.COM 			mgcp = grp->mrg_clients;
781111878SVenu.Iyer@Sun.COM 			while (mgcp != NULL) {
781211878SVenu.Iyer@Sun.COM 				gmcip = mgcp->mgc_client;
781311878SVenu.Iyer@Sun.COM 				emrp = MCIP_EFFECTIVE_PROPS(gmcip);
781411878SVenu.Iyer@Sun.COM 				if (emrp->mrp_mask & MRP_TX_RINGS) {
781511878SVenu.Iyer@Sun.COM 					emrp->mrp_mask &= ~MRP_TX_RINGS;
781611878SVenu.Iyer@Sun.COM 					emrp->mrp_ntxrings = 0;
781711878SVenu.Iyer@Sun.COM 				}
781811878SVenu.Iyer@Sun.COM 				mgcp = mgcp->mgc_next;
781911878SVenu.Iyer@Sun.COM 			}
782011878SVenu.Iyer@Sun.COM 		}
782111878SVenu.Iyer@Sun.COM 
782211878SVenu.Iyer@Sun.COM 		/*
782311878SVenu.Iyer@Sun.COM 		 * If the group is not the default group and the default
782411878SVenu.Iyer@Sun.COM 		 * group is reserved, the ring count in the default group
782511878SVenu.Iyer@Sun.COM 		 * might have changed, update it.
782611878SVenu.Iyer@Sun.COM 		 */
782711878SVenu.Iyer@Sun.COM 		if (grp != defgrp &&
782811878SVenu.Iyer@Sun.COM 		    defgrp->mrg_state == MAC_GROUP_STATE_RESERVED) {
782911878SVenu.Iyer@Sun.COM 			gmcip = MAC_GROUP_ONLY_CLIENT(defgrp);
783011878SVenu.Iyer@Sun.COM 			emrp = MCIP_EFFECTIVE_PROPS(gmcip);
783111878SVenu.Iyer@Sun.COM 			emrp->mrp_ntxrings = defgrp->mrg_cur_count;
783211878SVenu.Iyer@Sun.COM 		}
783311878SVenu.Iyer@Sun.COM 	}
783411878SVenu.Iyer@Sun.COM 	emrp = MCIP_EFFECTIVE_PROPS(mcip);
783511878SVenu.Iyer@Sun.COM }
783611878SVenu.Iyer@Sun.COM 
783711878SVenu.Iyer@Sun.COM /*
783811878SVenu.Iyer@Sun.COM  * Check if the primary is in the default group. If so, see if we
783911878SVenu.Iyer@Sun.COM  * can give it a an exclusive group now that another client is
784011878SVenu.Iyer@Sun.COM  * being configured. We take the primary out of the default group
784111878SVenu.Iyer@Sun.COM  * because the multicast/broadcast packets for the all the clients
784211878SVenu.Iyer@Sun.COM  * will land in the default ring in the default group which means
784311878SVenu.Iyer@Sun.COM  * any client in the default group, even if it is the only on in
784411878SVenu.Iyer@Sun.COM  * the group, will lose exclusive access to the rings, hence
784511878SVenu.Iyer@Sun.COM  * polling.
784611878SVenu.Iyer@Sun.COM  */
784711878SVenu.Iyer@Sun.COM mac_client_impl_t *
mac_check_primary_relocation(mac_client_impl_t * mcip,boolean_t rxhw)784811878SVenu.Iyer@Sun.COM mac_check_primary_relocation(mac_client_impl_t *mcip, boolean_t rxhw)
784911878SVenu.Iyer@Sun.COM {
785011878SVenu.Iyer@Sun.COM 	mac_impl_t		*mip = mcip->mci_mip;
785111878SVenu.Iyer@Sun.COM 	mac_group_t		*defgrp = MAC_DEFAULT_RX_GROUP(mip);
785211878SVenu.Iyer@Sun.COM 	flow_entry_t		*flent = mcip->mci_flent;
785311878SVenu.Iyer@Sun.COM 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
785411878SVenu.Iyer@Sun.COM 	uint8_t			*mac_addr;
785511878SVenu.Iyer@Sun.COM 	mac_group_t		*ngrp;
785611878SVenu.Iyer@Sun.COM 
785711878SVenu.Iyer@Sun.COM 	/*
785811878SVenu.Iyer@Sun.COM 	 * Check if the primary is in the default group, if not
785911878SVenu.Iyer@Sun.COM 	 * or if it is explicitly configured to be in the default
786011878SVenu.Iyer@Sun.COM 	 * group OR set the RX rings property, return.
786111878SVenu.Iyer@Sun.COM 	 */
786211878SVenu.Iyer@Sun.COM 	if (flent->fe_rx_ring_group != defgrp || mrp->mrp_mask & MRP_RX_RINGS)
786311878SVenu.Iyer@Sun.COM 		return (NULL);
786411878SVenu.Iyer@Sun.COM 
786511878SVenu.Iyer@Sun.COM 	/*
786611878SVenu.Iyer@Sun.COM 	 * If the new client needs an exclusive group and we
786711878SVenu.Iyer@Sun.COM 	 * don't have another for the primary, return.
786811878SVenu.Iyer@Sun.COM 	 */
786911878SVenu.Iyer@Sun.COM 	if (rxhw && mip->mi_rxhwclnt_avail < 2)
787011878SVenu.Iyer@Sun.COM 		return (NULL);
787111878SVenu.Iyer@Sun.COM 
787211878SVenu.Iyer@Sun.COM 	mac_addr = flent->fe_flow_desc.fd_dst_mac;
787311878SVenu.Iyer@Sun.COM 	/*
787411878SVenu.Iyer@Sun.COM 	 * We call this when we are setting up the datapath for
787511878SVenu.Iyer@Sun.COM 	 * the first non-primary.
787611878SVenu.Iyer@Sun.COM 	 */
787711878SVenu.Iyer@Sun.COM 	ASSERT(mip->mi_nactiveclients == 2);
787811878SVenu.Iyer@Sun.COM 	/*
787911878SVenu.Iyer@Sun.COM 	 * OK, now we have the primary that needs to be relocated.
788011878SVenu.Iyer@Sun.COM 	 */
788111878SVenu.Iyer@Sun.COM 	ngrp =  mac_reserve_rx_group(mcip, mac_addr, B_TRUE);
788211878SVenu.Iyer@Sun.COM 	if (ngrp == NULL)
788311878SVenu.Iyer@Sun.COM 		return (NULL);
788411878SVenu.Iyer@Sun.COM 	if (mac_rx_switch_group(mcip, defgrp, ngrp) != 0) {
788511878SVenu.Iyer@Sun.COM 		mac_stop_group(ngrp);
788611878SVenu.Iyer@Sun.COM 		return (NULL);
788711878SVenu.Iyer@Sun.COM 	}
788811878SVenu.Iyer@Sun.COM 	return (mcip);
788911878SVenu.Iyer@Sun.COM }
7890