xref: /illumos-gate/usr/src/uts/common/io/mac/mac.c (revision 99ad48a4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2020 Joyent, Inc.
25  * Copyright 2015 Garrett D'Amore <garrett@damore.org>
26  * Copyright 2020 RackTop Systems, Inc.
27  */
28 
29 /*
30  * MAC Services Module
31  *
32  * The GLDv3 framework locking -  The MAC layer
33  * --------------------------------------------
34  *
35  * The MAC layer is central to the GLD framework and can provide the locking
36  * framework needed for itself and for the use of MAC clients. MAC end points
37  * are fairly disjoint and don't share a lot of state. So a coarse grained
38  * multi-threading scheme is to single thread all create/modify/delete or set
39  * type of control operations on a per mac end point while allowing data threads
40  * concurrently.
41  *
42  * Control operations (set) that modify a mac end point are always serialized on
43  * a per mac end point basis, We have at most 1 such thread per mac end point
44  * at a time.
45  *
46  * All other operations that are not serialized are essentially multi-threaded.
47  * For example a control operation (get) like getting statistics which may not
48  * care about reading values atomically or data threads sending or receiving
49  * data. Mostly these type of operations don't modify the control state. Any
50  * state these operations care about are protected using traditional locks.
51  *
52  * The perimeter only serializes serial operations. It does not imply there
53  * aren't any other concurrent operations. However a serialized operation may
54  * sometimes need to make sure it is the only thread. In this case it needs
55  * to use reference counting mechanisms to cv_wait until any current data
56  * threads are done.
57  *
58  * The mac layer itself does not hold any locks across a call to another layer.
59  * The perimeter is however held across a down call to the driver to make the
60  * whole control operation atomic with respect to other control operations.
61  * Also the data path and get type control operations may proceed concurrently.
62  * These operations synchronize with the single serial operation on a given mac
63  * end point using regular locks. The perimeter ensures that conflicting
64  * operations like say a mac_multicast_add and a mac_multicast_remove on the
65  * same mac end point don't interfere with each other and also ensures that the
66  * changes in the mac layer and the call to the underlying driver to say add a
67  * multicast address are done atomically without interference from a thread
68  * trying to delete the same address.
69  *
70  * For example, consider
71  * mac_multicst_add()
72  * {
73  *	mac_perimeter_enter();	serialize all control operations
74  *
75  *	grab list lock		protect against access by data threads
76  *	add to list
77  *	drop list lock
78  *
79  *	call driver's mi_multicst
80  *
81  *	mac_perimeter_exit();
82  * }
83  *
84  * To lessen the number of serialization locks and simplify the lock hierarchy,
85  * we serialize all the control operations on a per mac end point by using a
86  * single serialization lock called the perimeter. We allow recursive entry into
87  * the perimeter to facilitate use of this mechanism by both the mac client and
88  * the MAC layer itself.
89  *
90  * MAC client means an entity that does an operation on a mac handle
91  * obtained from a mac_open/mac_client_open. Similarly MAC driver means
92  * an entity that does an operation on a mac handle obtained from a
93  * mac_register. An entity could be both client and driver but on different
94  * handles eg. aggr. and should only make the corresponding mac interface calls
95  * i.e. mac driver interface or mac client interface as appropriate for that
96  * mac handle.
97  *
98  * General rules.
99  * -------------
100  *
101  * R1. The lock order of upcall threads is natually opposite to downcall
102  * threads. Hence upcalls must not hold any locks across layers for fear of
103  * recursive lock enter and lock order violation. This applies to all layers.
104  *
105  * R2. The perimeter is just another lock. Since it is held in the down
106  * direction, acquiring the perimeter in an upcall is prohibited as it would
107  * cause a deadlock. This applies to all layers.
108  *
109  * Note that upcalls that need to grab the mac perimeter (for example
110  * mac_notify upcalls) can still achieve that by posting the request to a
111  * thread, which can then grab all the required perimeters and locks in the
112  * right global order. Note that in the above example the mac layer iself
113  * won't grab the mac perimeter in the mac_notify upcall, instead the upcall
114  * to the client must do that. Please see the aggr code for an example.
115  *
116  * MAC client rules
117  * ----------------
118  *
119  * R3. A MAC client may use the MAC provided perimeter facility to serialize
120  * control operations on a per mac end point. It does this by by acquring
121  * and holding the perimeter across a sequence of calls to the mac layer.
122  * This ensures atomicity across the entire block of mac calls. In this
123  * model the MAC client must not hold any client locks across the calls to
124  * the mac layer. This model is the preferred solution.
125  *
126  * R4. However if a MAC client has a lot of global state across all mac end
127  * points the per mac end point serialization may not be sufficient. In this
128  * case the client may choose to use global locks or use its own serialization.
129  * To avoid deadlocks, these client layer locks held across the mac calls
130  * in the control path must never be acquired by the data path for the reason
131  * mentioned below.
132  *
133  * (Assume that a control operation that holds a client lock blocks in the
134  * mac layer waiting for upcall reference counts to drop to zero. If an upcall
135  * data thread that holds this reference count, tries to acquire the same
136  * client lock subsequently it will deadlock).
137  *
138  * A MAC client may follow either the R3 model or the R4 model, but can't
139  * mix both. In the former, the hierarchy is Perim -> client locks, but in
140  * the latter it is client locks -> Perim.
141  *
142  * R5. MAC clients must make MAC calls (excluding data calls) in a cv_wait'able
143  * context since they may block while trying to acquire the perimeter.
144  * In addition some calls may block waiting for upcall refcnts to come down to
145  * zero.
146  *
147  * R6. MAC clients must make sure that they are single threaded and all threads
148  * from the top (in particular data threads) have finished before calling
149  * mac_client_close. The MAC framework does not track the number of client
150  * threads using the mac client handle. Also mac clients must make sure
151  * they have undone all the control operations before calling mac_client_close.
152  * For example mac_unicast_remove/mac_multicast_remove to undo the corresponding
153  * mac_unicast_add/mac_multicast_add.
154  *
155  * MAC framework rules
156  * -------------------
157  *
158  * R7. The mac layer itself must not hold any mac layer locks (except the mac
159  * perimeter) across a call to any other layer from the mac layer. The call to
160  * any other layer could be via mi_* entry points, classifier entry points into
161  * the driver or via upcall pointers into layers above. The mac perimeter may
162  * be acquired or held only in the down direction, for e.g. when calling into
163  * a mi_* driver enty point to provide atomicity of the operation.
164  *
165  * R8. Since it is not guaranteed (see R14) that drivers won't hold locks across
166  * mac driver interfaces, the MAC layer must provide a cut out for control
167  * interfaces like upcall notifications and start them in a separate thread.
168  *
169  * R9. Note that locking order also implies a plumbing order. For example
170  * VNICs are allowed to be created over aggrs, but not vice-versa. An attempt
171  * to plumb in any other order must be failed at mac_open time, otherwise it
172  * could lead to deadlocks due to inverse locking order.
173  *
174  * R10. MAC driver interfaces must not block since the driver could call them
175  * in interrupt context.
176  *
177  * R11. Walkers must preferably not hold any locks while calling walker
178  * callbacks. Instead these can operate on reference counts. In simple
179  * callbacks it may be ok to hold a lock and call the callbacks, but this is
180  * harder to maintain in the general case of arbitrary callbacks.
181  *
182  * R12. The MAC layer must protect upcall notification callbacks using reference
183  * counts rather than holding locks across the callbacks.
184  *
185  * R13. Given the variety of drivers, it is preferable if the MAC layer can make
186  * sure that any pointers (such as mac ring pointers) it passes to the driver
187  * remain valid until mac unregister time. Currently the mac layer achieves
188  * this by using generation numbers for rings and freeing the mac rings only
189  * at unregister time.  The MAC layer must provide a layer of indirection and
190  * must not expose underlying driver rings or driver data structures/pointers
191  * directly to MAC clients.
192  *
193  * MAC driver rules
194  * ----------------
195  *
196  * R14. It would be preferable if MAC drivers don't hold any locks across any
197  * mac call. However at a minimum they must not hold any locks across data
198  * upcalls. They must also make sure that all references to mac data structures
199  * are cleaned up and that it is single threaded at mac_unregister time.
200  *
201  * R15. MAC driver interfaces don't block and so the action may be done
202  * asynchronously in a separate thread as for example handling notifications.
203  * The driver must not assume that the action is complete when the call
204  * returns.
205  *
206  * R16. Drivers must maintain a generation number per Rx ring, and pass it
207  * back to mac_rx_ring(); They are expected to increment the generation
208  * number whenever the ring's stop routine is invoked.
209  * See comments in mac_rx_ring();
210  *
211  * R17 Similarly mi_stop is another synchronization point and the driver must
212  * ensure that all upcalls are done and there won't be any future upcall
213  * before returning from mi_stop.
214  *
215  * R18. The driver may assume that all set/modify control operations via
216  * the mi_* entry points are single threaded on a per mac end point.
217  *
218  * Lock and Perimeter hierarchy scenarios
219  * ---------------------------------------
220  *
221  * i_mac_impl_lock -> mi_rw_lock -> srs_lock -> s_ring_lock[i_mac_tx_srs_notify]
222  *
223  * ft_lock -> fe_lock [mac_flow_lookup]
224  *
225  * mi_rw_lock -> fe_lock [mac_bcast_send]
226  *
227  * srs_lock -> mac_bw_lock [mac_rx_srs_drain_bw]
228  *
229  * cpu_lock -> mac_srs_g_lock -> srs_lock -> s_ring_lock [mac_walk_srs_and_bind]
230  *
231  * i_dls_devnet_lock -> mac layer locks [dls_devnet_rename]
232  *
233  * Perimeters are ordered P1 -> P2 -> P3 from top to bottom in order of mac
234  * client to driver. In the case of clients that explictly use the mac provided
235  * perimeter mechanism for its serialization, the hierarchy is
236  * Perimeter -> mac layer locks, since the client never holds any locks across
237  * the mac calls. In the case of clients that use its own locks the hierarchy
238  * is Client locks -> Mac Perim -> Mac layer locks. The client never explicitly
239  * calls mac_perim_enter/exit in this case.
240  *
241  * Subflow creation rules
242  * ---------------------------
243  * o In case of a user specified cpulist present on underlying link and flows,
244  * the flows cpulist must be a subset of the underlying link.
245  * o In case of a user specified fanout mode present on link and flow, the
246  * subflow fanout count has to be less than or equal to that of the
247  * underlying link. The cpu-bindings for the subflows will be a subset of
248  * the underlying link.
249  * o In case if no cpulist specified on both underlying link and flow, the
250  * underlying link relies on a  MAC tunable to provide out of box fanout.
251  * The subflow will have no cpulist (the subflow will be unbound)
252  * o In case if no cpulist is specified on the underlying link, a subflow can
253  * carry  either a user-specified cpulist or fanout count. The cpu-bindings
254  * for the subflow will not adhere to restriction that they need to be subset
255  * of the underlying link.
256  * o In case where the underlying link is carrying either a user specified
257  * cpulist or fanout mode and for a unspecified subflow, the subflow will be
258  * created unbound.
259  * o While creating unbound subflows, bandwidth mode changes attempt to
260  * figure a right fanout count. In such cases the fanout count will override
261  * the unbound cpu-binding behavior.
262  * o In addition to this, while cycling between flow and link properties, we
263  * impose a restriction that if a link property has a subflow with
264  * user-specified attributes, we will not allow changing the link property.
265  * The administrator needs to reset all the user specified properties for the
266  * subflows before attempting a link property change.
267  * Some of the above rules can be overridden by specifying additional command
268  * line options while creating or modifying link or subflow properties.
269  *
270  * Datapath
271  * --------
272  *
273  * For information on the datapath, the world of soft rings, hardware rings, how
274  * it is structured, and the path of an mblk_t between a driver and a mac
275  * client, see mac_sched.c.
276  */
277 
278 #include <sys/types.h>
279 #include <sys/conf.h>
280 #include <sys/id_space.h>
281 #include <sys/esunddi.h>
282 #include <sys/stat.h>
283 #include <sys/mkdev.h>
284 #include <sys/stream.h>
285 #include <sys/strsun.h>
286 #include <sys/strsubr.h>
287 #include <sys/dlpi.h>
288 #include <sys/list.h>
289 #include <sys/modhash.h>
290 #include <sys/mac_provider.h>
291 #include <sys/mac_client_impl.h>
292 #include <sys/mac_soft_ring.h>
293 #include <sys/mac_stat.h>
294 #include <sys/mac_impl.h>
295 #include <sys/mac.h>
296 #include <sys/dls.h>
297 #include <sys/dld.h>
298 #include <sys/modctl.h>
299 #include <sys/fs/dv_node.h>
300 #include <sys/thread.h>
301 #include <sys/proc.h>
302 #include <sys/callb.h>
303 #include <sys/cpuvar.h>
304 #include <sys/atomic.h>
305 #include <sys/bitmap.h>
306 #include <sys/sdt.h>
307 #include <sys/mac_flow.h>
308 #include <sys/ddi_intr_impl.h>
309 #include <sys/disp.h>
310 #include <sys/sdt.h>
311 #include <sys/vnic.h>
312 #include <sys/vnic_impl.h>
313 #include <sys/vlan.h>
314 #include <inet/ip.h>
315 #include <inet/ip6.h>
316 #include <sys/exacct.h>
317 #include <sys/exacct_impl.h>
318 #include <inet/nd.h>
319 #include <sys/ethernet.h>
320 #include <sys/pool.h>
321 #include <sys/pool_pset.h>
322 #include <sys/cpupart.h>
323 #include <inet/wifi_ioctl.h>
324 #include <net/wpa.h>
325 
326 #define	IMPL_HASHSZ	67	/* prime */
327 
328 kmem_cache_t		*i_mac_impl_cachep;
329 mod_hash_t		*i_mac_impl_hash;
330 krwlock_t		i_mac_impl_lock;
331 uint_t			i_mac_impl_count;
332 static kmem_cache_t	*mac_ring_cache;
333 static id_space_t	*minor_ids;
334 static uint32_t		minor_count;
335 static pool_event_cb_t	mac_pool_event_reg;
336 
337 /*
338  * Logging stuff. Perhaps mac_logging_interval could be broken into
339  * mac_flow_log_interval and mac_link_log_interval if we want to be
340  * able to schedule them differently.
341  */
342 uint_t			mac_logging_interval;
343 boolean_t		mac_flow_log_enable;
344 boolean_t		mac_link_log_enable;
345 timeout_id_t		mac_logging_timer;
346 
347 #define	MACTYPE_KMODDIR	"mac"
348 #define	MACTYPE_HASHSZ	67
349 static mod_hash_t	*i_mactype_hash;
350 /*
351  * i_mactype_lock synchronizes threads that obtain references to mactype_t
352  * structures through i_mactype_getplugin().
353  */
354 static kmutex_t		i_mactype_lock;
355 
356 /*
357  * mac_tx_percpu_cnt
358  *
359  * Number of per cpu locks per mac_client_impl_t. Used by the transmit side
360  * in mac_tx to reduce lock contention. This is sized at boot time in mac_init.
361  * mac_tx_percpu_cnt_max is settable in /etc/system and must be a power of 2.
362  * Per cpu locks may be disabled by setting mac_tx_percpu_cnt_max to 1.
363  */
364 int mac_tx_percpu_cnt;
365 int mac_tx_percpu_cnt_max = 128;
366 
367 /*
368  * Call back functions for the bridge module.  These are guaranteed to be valid
369  * when holding a reference on a link or when holding mip->mi_bridge_lock and
370  * mi_bridge_link is non-NULL.
371  */
372 mac_bridge_tx_t mac_bridge_tx_cb;
373 mac_bridge_rx_t mac_bridge_rx_cb;
374 mac_bridge_ref_t mac_bridge_ref_cb;
375 mac_bridge_ls_t mac_bridge_ls_cb;
376 
377 static int i_mac_constructor(void *, void *, int);
378 static void i_mac_destructor(void *, void *);
379 static int i_mac_ring_ctor(void *, void *, int);
380 static void i_mac_ring_dtor(void *, void *);
381 static mblk_t *mac_rx_classify(mac_impl_t *, mac_resource_handle_t, mblk_t *);
382 void mac_tx_client_flush(mac_client_impl_t *);
383 void mac_tx_client_block(mac_client_impl_t *);
384 static void mac_rx_ring_quiesce(mac_ring_t *, uint_t);
385 static int mac_start_group_and_rings(mac_group_t *);
386 static void mac_stop_group_and_rings(mac_group_t *);
387 static void mac_pool_event_cb(pool_event_t, int, void *);
388 
389 typedef struct netinfo_s {
390 	list_node_t	ni_link;
391 	void		*ni_record;
392 	int		ni_size;
393 	int		ni_type;
394 } netinfo_t;
395 
396 /*
397  * Module initialization functions.
398  */
399 
400 void
mac_init(void)401 mac_init(void)
402 {
403 	mac_tx_percpu_cnt = ((boot_max_ncpus == -1) ? max_ncpus :
404 	    boot_max_ncpus);
405 
406 	/* Upper bound is mac_tx_percpu_cnt_max */
407 	if (mac_tx_percpu_cnt > mac_tx_percpu_cnt_max)
408 		mac_tx_percpu_cnt = mac_tx_percpu_cnt_max;
409 
410 	if (mac_tx_percpu_cnt < 1) {
411 		/* Someone set max_tx_percpu_cnt_max to 0 or less */
412 		mac_tx_percpu_cnt = 1;
413 	}
414 
415 	ASSERT(mac_tx_percpu_cnt >= 1);
416 	mac_tx_percpu_cnt = (1 << highbit(mac_tx_percpu_cnt - 1));
417 	/*
418 	 * Make it of the form 2**N - 1 in the range
419 	 * [0 .. mac_tx_percpu_cnt_max - 1]
420 	 */
421 	mac_tx_percpu_cnt--;
422 
423 	i_mac_impl_cachep = kmem_cache_create("mac_impl_cache",
424 	    sizeof (mac_impl_t), 0, i_mac_constructor, i_mac_destructor,
425 	    NULL, NULL, NULL, 0);
426 	ASSERT(i_mac_impl_cachep != NULL);
427 
428 	mac_ring_cache = kmem_cache_create("mac_ring_cache",
429 	    sizeof (mac_ring_t), 0, i_mac_ring_ctor, i_mac_ring_dtor, NULL,
430 	    NULL, NULL, 0);
431 	ASSERT(mac_ring_cache != NULL);
432 
433 	i_mac_impl_hash = mod_hash_create_extended("mac_impl_hash",
434 	    IMPL_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
435 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
436 	rw_init(&i_mac_impl_lock, NULL, RW_DEFAULT, NULL);
437 
438 	mac_flow_init();
439 	mac_soft_ring_init();
440 	mac_bcast_init();
441 	mac_client_init();
442 
443 	i_mac_impl_count = 0;
444 
445 	i_mactype_hash = mod_hash_create_extended("mactype_hash",
446 	    MACTYPE_HASHSZ,
447 	    mod_hash_null_keydtor, mod_hash_null_valdtor,
448 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
449 
450 	/*
451 	 * Allocate an id space to manage minor numbers. The range of the
452 	 * space will be from MAC_MAX_MINOR+1 to MAC_PRIVATE_MINOR-1.  This
453 	 * leaves half of the 32-bit minors available for driver private use.
454 	 */
455 	minor_ids = id_space_create("mac_minor_ids", MAC_MAX_MINOR+1,
456 	    MAC_PRIVATE_MINOR-1);
457 	ASSERT(minor_ids != NULL);
458 	minor_count = 0;
459 
460 	/* Let's default to 20 seconds */
461 	mac_logging_interval = 20;
462 	mac_flow_log_enable = B_FALSE;
463 	mac_link_log_enable = B_FALSE;
464 	mac_logging_timer = NULL;
465 
466 	/* Register to be notified of noteworthy pools events */
467 	mac_pool_event_reg.pec_func =  mac_pool_event_cb;
468 	mac_pool_event_reg.pec_arg = NULL;
469 	pool_event_cb_register(&mac_pool_event_reg);
470 }
471 
472 int
mac_fini(void)473 mac_fini(void)
474 {
475 
476 	if (i_mac_impl_count > 0 || minor_count > 0)
477 		return (EBUSY);
478 
479 	pool_event_cb_unregister(&mac_pool_event_reg);
480 
481 	id_space_destroy(minor_ids);
482 	mac_flow_fini();
483 
484 	mod_hash_destroy_hash(i_mac_impl_hash);
485 	rw_destroy(&i_mac_impl_lock);
486 
487 	mac_client_fini();
488 	kmem_cache_destroy(mac_ring_cache);
489 
490 	mod_hash_destroy_hash(i_mactype_hash);
491 	mac_soft_ring_finish();
492 
493 
494 	return (0);
495 }
496 
497 /*
498  * Initialize a GLDv3 driver's device ops.  A driver that manages its own ops
499  * (e.g. softmac) may pass in a NULL ops argument.
500  */
501 void
mac_init_ops(struct dev_ops * ops,const char * name)502 mac_init_ops(struct dev_ops *ops, const char *name)
503 {
504 	major_t major = ddi_name_to_major((char *)name);
505 
506 	/*
507 	 * By returning on error below, we are not letting the driver continue
508 	 * in an undefined context.  The mac_register() function will faill if
509 	 * DN_GLDV3_DRIVER isn't set.
510 	 */
511 	if (major == DDI_MAJOR_T_NONE)
512 		return;
513 	LOCK_DEV_OPS(&devnamesp[major].dn_lock);
514 	devnamesp[major].dn_flags |= (DN_GLDV3_DRIVER | DN_NETWORK_DRIVER);
515 	UNLOCK_DEV_OPS(&devnamesp[major].dn_lock);
516 	if (ops != NULL)
517 		dld_init_ops(ops, name);
518 }
519 
520 void
mac_fini_ops(struct dev_ops * ops)521 mac_fini_ops(struct dev_ops *ops)
522 {
523 	dld_fini_ops(ops);
524 }
525 
526 /*ARGSUSED*/
527 static int
i_mac_constructor(void * buf,void * arg,int kmflag)528 i_mac_constructor(void *buf, void *arg, int kmflag)
529 {
530 	mac_impl_t	*mip = buf;
531 
532 	bzero(buf, sizeof (mac_impl_t));
533 
534 	mip->mi_linkstate = LINK_STATE_UNKNOWN;
535 
536 	rw_init(&mip->mi_rw_lock, NULL, RW_DRIVER, NULL);
537 	mutex_init(&mip->mi_notify_lock, NULL, MUTEX_DRIVER, NULL);
538 	mutex_init(&mip->mi_promisc_lock, NULL, MUTEX_DRIVER, NULL);
539 	mutex_init(&mip->mi_ring_lock, NULL, MUTEX_DEFAULT, NULL);
540 
541 	mip->mi_notify_cb_info.mcbi_lockp = &mip->mi_notify_lock;
542 	cv_init(&mip->mi_notify_cb_info.mcbi_cv, NULL, CV_DRIVER, NULL);
543 	mip->mi_promisc_cb_info.mcbi_lockp = &mip->mi_promisc_lock;
544 	cv_init(&mip->mi_promisc_cb_info.mcbi_cv, NULL, CV_DRIVER, NULL);
545 
546 	mutex_init(&mip->mi_bridge_lock, NULL, MUTEX_DEFAULT, NULL);
547 
548 	return (0);
549 }
550 
551 /*ARGSUSED*/
552 static void
i_mac_destructor(void * buf,void * arg)553 i_mac_destructor(void *buf, void *arg)
554 {
555 	mac_impl_t	*mip = buf;
556 	mac_cb_info_t	*mcbi;
557 
558 	ASSERT(mip->mi_ref == 0);
559 	ASSERT(mip->mi_active == 0);
560 	ASSERT(mip->mi_linkstate == LINK_STATE_UNKNOWN);
561 	ASSERT(mip->mi_devpromisc == 0);
562 	ASSERT(mip->mi_ksp == NULL);
563 	ASSERT(mip->mi_kstat_count == 0);
564 	ASSERT(mip->mi_nclients == 0);
565 	ASSERT(mip->mi_nactiveclients == 0);
566 	ASSERT(mip->mi_single_active_client == NULL);
567 	ASSERT(mip->mi_state_flags == 0);
568 	ASSERT(mip->mi_factory_addr == NULL);
569 	ASSERT(mip->mi_factory_addr_num == 0);
570 	ASSERT(mip->mi_default_tx_ring == NULL);
571 
572 	mcbi = &mip->mi_notify_cb_info;
573 	ASSERT(mcbi->mcbi_del_cnt == 0 && mcbi->mcbi_walker_cnt == 0);
574 	ASSERT(mip->mi_notify_bits == 0);
575 	ASSERT(mip->mi_notify_thread == NULL);
576 	ASSERT(mcbi->mcbi_lockp == &mip->mi_notify_lock);
577 	mcbi->mcbi_lockp = NULL;
578 
579 	mcbi = &mip->mi_promisc_cb_info;
580 	ASSERT(mcbi->mcbi_del_cnt == 0 && mip->mi_promisc_list == NULL);
581 	ASSERT(mip->mi_promisc_list == NULL);
582 	ASSERT(mcbi->mcbi_lockp == &mip->mi_promisc_lock);
583 	mcbi->mcbi_lockp = NULL;
584 
585 	ASSERT(mip->mi_bcast_ngrps == 0 && mip->mi_bcast_grp == NULL);
586 	ASSERT(mip->mi_perim_owner == NULL && mip->mi_perim_ocnt == 0);
587 
588 	rw_destroy(&mip->mi_rw_lock);
589 
590 	mutex_destroy(&mip->mi_promisc_lock);
591 	cv_destroy(&mip->mi_promisc_cb_info.mcbi_cv);
592 	mutex_destroy(&mip->mi_notify_lock);
593 	cv_destroy(&mip->mi_notify_cb_info.mcbi_cv);
594 	mutex_destroy(&mip->mi_ring_lock);
595 
596 	ASSERT(mip->mi_bridge_link == NULL);
597 }
598 
599 /* ARGSUSED */
600 static int
i_mac_ring_ctor(void * buf,void * arg,int kmflag)601 i_mac_ring_ctor(void *buf, void *arg, int kmflag)
602 {
603 	mac_ring_t *ring = (mac_ring_t *)buf;
604 
605 	bzero(ring, sizeof (mac_ring_t));
606 	cv_init(&ring->mr_cv, NULL, CV_DEFAULT, NULL);
607 	mutex_init(&ring->mr_lock, NULL, MUTEX_DEFAULT, NULL);
608 	ring->mr_state = MR_FREE;
609 	return (0);
610 }
611 
612 /* ARGSUSED */
613 static void
i_mac_ring_dtor(void * buf,void * arg)614 i_mac_ring_dtor(void *buf, void *arg)
615 {
616 	mac_ring_t *ring = (mac_ring_t *)buf;
617 
618 	cv_destroy(&ring->mr_cv);
619 	mutex_destroy(&ring->mr_lock);
620 }
621 
622 /*
623  * Common functions to do mac callback addition and deletion. Currently this is
624  * used by promisc callbacks and notify callbacks. List addition and deletion
625  * need to take care of list walkers. List walkers in general, can't hold list
626  * locks and make upcall callbacks due to potential lock order and recursive
627  * reentry issues. Instead list walkers increment the list walker count to mark
628  * the presence of a walker thread. Addition can be carefully done to ensure
629  * that the list walker always sees either the old list or the new list.
630  * However the deletion can't be done while the walker is active, instead the
631  * deleting thread simply marks the entry as logically deleted. The last walker
632  * physically deletes and frees up the logically deleted entries when the walk
633  * is complete.
634  */
635 void
mac_callback_add(mac_cb_info_t * mcbi,mac_cb_t ** mcb_head,mac_cb_t * mcb_elem)636 mac_callback_add(mac_cb_info_t *mcbi, mac_cb_t **mcb_head,
637     mac_cb_t *mcb_elem)
638 {
639 	mac_cb_t	*p;
640 	mac_cb_t	**pp;
641 
642 	/* Verify it is not already in the list */
643 	for (pp = mcb_head; (p = *pp) != NULL; pp = &p->mcb_nextp) {
644 		if (p == mcb_elem)
645 			break;
646 	}
647 	VERIFY(p == NULL);
648 
649 	/*
650 	 * Add it to the head of the callback list. The membar ensures that
651 	 * the following list pointer manipulations reach global visibility
652 	 * in exactly the program order below.
653 	 */
654 	ASSERT(MUTEX_HELD(mcbi->mcbi_lockp));
655 
656 	mcb_elem->mcb_nextp = *mcb_head;
657 	membar_producer();
658 	*mcb_head = mcb_elem;
659 }
660 
661 /*
662  * Mark the entry as logically deleted. If there aren't any walkers unlink
663  * from the list. In either case return the corresponding status.
664  */
665 boolean_t
mac_callback_remove(mac_cb_info_t * mcbi,mac_cb_t ** mcb_head,mac_cb_t * mcb_elem)666 mac_callback_remove(mac_cb_info_t *mcbi, mac_cb_t **mcb_head,
667     mac_cb_t *mcb_elem)
668 {
669 	mac_cb_t	*p;
670 	mac_cb_t	**pp;
671 
672 	ASSERT(MUTEX_HELD(mcbi->mcbi_lockp));
673 	/*
674 	 * Search the callback list for the entry to be removed
675 	 */
676 	for (pp = mcb_head; (p = *pp) != NULL; pp = &p->mcb_nextp) {
677 		if (p == mcb_elem)
678 			break;
679 	}
680 	VERIFY(p != NULL);
681 
682 	/*
683 	 * If there are walkers just mark it as deleted and the last walker
684 	 * will remove from the list and free it.
685 	 */
686 	if (mcbi->mcbi_walker_cnt != 0) {
687 		p->mcb_flags |= MCB_CONDEMNED;
688 		mcbi->mcbi_del_cnt++;
689 		return (B_FALSE);
690 	}
691 
692 	ASSERT(mcbi->mcbi_del_cnt == 0);
693 	*pp = p->mcb_nextp;
694 	p->mcb_nextp = NULL;
695 	return (B_TRUE);
696 }
697 
698 /*
699  * Wait for all pending callback removals to be completed
700  */
701 void
mac_callback_remove_wait(mac_cb_info_t * mcbi)702 mac_callback_remove_wait(mac_cb_info_t *mcbi)
703 {
704 	ASSERT(MUTEX_HELD(mcbi->mcbi_lockp));
705 	while (mcbi->mcbi_del_cnt != 0) {
706 		DTRACE_PROBE1(need_wait, mac_cb_info_t *, mcbi);
707 		cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
708 	}
709 }
710 
711 void
mac_callback_barrier(mac_cb_info_t * mcbi)712 mac_callback_barrier(mac_cb_info_t *mcbi)
713 {
714 	ASSERT(MUTEX_HELD(mcbi->mcbi_lockp));
715 	ASSERT3U(mcbi->mcbi_barrier_cnt, <, UINT_MAX);
716 
717 	if (mcbi->mcbi_walker_cnt == 0) {
718 		return;
719 	}
720 
721 	mcbi->mcbi_barrier_cnt++;
722 	do {
723 		cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
724 	} while (mcbi->mcbi_walker_cnt > 0);
725 	mcbi->mcbi_barrier_cnt--;
726 	cv_broadcast(&mcbi->mcbi_cv);
727 }
728 
729 void
mac_callback_walker_enter(mac_cb_info_t * mcbi)730 mac_callback_walker_enter(mac_cb_info_t *mcbi)
731 {
732 	mutex_enter(mcbi->mcbi_lockp);
733 	/*
734 	 * Incoming walkers should give precedence to timely clean-up of
735 	 * deleted callback entries and requested barriers.
736 	 */
737 	while (mcbi->mcbi_del_cnt > 0 || mcbi->mcbi_barrier_cnt > 0) {
738 		cv_wait(&mcbi->mcbi_cv, mcbi->mcbi_lockp);
739 	}
740 	mcbi->mcbi_walker_cnt++;
741 	mutex_exit(mcbi->mcbi_lockp);
742 }
743 
744 /*
745  * The last mac callback walker does the cleanup. Walk the list and unlik
746  * all the logically deleted entries and construct a temporary list of
747  * removed entries. Return the list of removed entries to the caller.
748  */
749 static mac_cb_t *
mac_callback_walker_cleanup(mac_cb_info_t * mcbi,mac_cb_t ** mcb_head)750 mac_callback_walker_cleanup(mac_cb_info_t *mcbi, mac_cb_t **mcb_head)
751 {
752 	mac_cb_t	*p;
753 	mac_cb_t	**pp;
754 	mac_cb_t	*rmlist = NULL;		/* List of removed elements */
755 	int	cnt = 0;
756 
757 	ASSERT(MUTEX_HELD(mcbi->mcbi_lockp));
758 	ASSERT(mcbi->mcbi_del_cnt != 0 && mcbi->mcbi_walker_cnt == 0);
759 
760 	pp = mcb_head;
761 	while (*pp != NULL) {
762 		if ((*pp)->mcb_flags & MCB_CONDEMNED) {
763 			p = *pp;
764 			*pp = p->mcb_nextp;
765 			p->mcb_nextp = rmlist;
766 			rmlist = p;
767 			cnt++;
768 			continue;
769 		}
770 		pp = &(*pp)->mcb_nextp;
771 	}
772 
773 	ASSERT(mcbi->mcbi_del_cnt == cnt);
774 	mcbi->mcbi_del_cnt = 0;
775 	return (rmlist);
776 }
777 
778 void
mac_callback_walker_exit(mac_cb_info_t * mcbi,mac_cb_t ** headp,boolean_t is_promisc)779 mac_callback_walker_exit(mac_cb_info_t *mcbi, mac_cb_t **headp,
780     boolean_t is_promisc)
781 {
782 	boolean_t do_wake = B_FALSE;
783 
784 	mutex_enter(mcbi->mcbi_lockp);
785 
786 	/* If walkers remain, nothing more can be done for now */
787 	if (--mcbi->mcbi_walker_cnt != 0) {
788 		mutex_exit(mcbi->mcbi_lockp);
789 		return;
790 	}
791 
792 	if (mcbi->mcbi_del_cnt != 0) {
793 		mac_cb_t *rmlist;
794 
795 		rmlist = mac_callback_walker_cleanup(mcbi, headp);
796 
797 		if (!is_promisc) {
798 			/* The "normal" non-promisc callback clean-up */
799 			mac_callback_free(rmlist);
800 		} else {
801 			mac_cb_t *mcb, *mcb_next;
802 
803 			/*
804 			 * The promisc callbacks are in 2 lists, one off the
805 			 * 'mip' and another off the 'mcip' threaded by
806 			 * mpi_mi_link and mpi_mci_link respectively.  There
807 			 * is, however, only a single shared total walker
808 			 * count, and an entry cannot be physically unlinked if
809 			 * a walker is active on either list. The last walker
810 			 * does this cleanup of logically deleted entries.
811 			 *
812 			 * With a list of callbacks deleted from above from
813 			 * mi_promisc_list (headp), remove the corresponding
814 			 * entry from mci_promisc_list (headp_pair) and free
815 			 * the structure.
816 			 */
817 			for (mcb = rmlist; mcb != NULL; mcb = mcb_next) {
818 				mac_promisc_impl_t *mpip;
819 				mac_client_impl_t *mcip;
820 
821 				mcb_next = mcb->mcb_nextp;
822 				mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
823 				mcip = mpip->mpi_mcip;
824 
825 				ASSERT3P(&mcip->mci_mip->mi_promisc_cb_info,
826 				    ==, mcbi);
827 				ASSERT3P(&mcip->mci_mip->mi_promisc_list,
828 				    ==, headp);
829 
830 				VERIFY(mac_callback_remove(mcbi,
831 				    &mcip->mci_promisc_list,
832 				    &mpip->mpi_mci_link));
833 				mcb->mcb_flags = 0;
834 				mcb->mcb_nextp = NULL;
835 				kmem_cache_free(mac_promisc_impl_cache, mpip);
836 			}
837 		}
838 
839 		/*
840 		 * Wake any walker threads that could be waiting in
841 		 * mac_callback_walker_enter() until deleted items have been
842 		 * cleaned from the list.
843 		 */
844 		do_wake = B_TRUE;
845 	}
846 
847 	if (mcbi->mcbi_barrier_cnt != 0) {
848 		/*
849 		 * One or more threads are waiting for all walkers to exit the
850 		 * callback list.  Notify them, now that the list is clear.
851 		 */
852 		do_wake = B_TRUE;
853 	}
854 
855 	if (do_wake) {
856 		cv_broadcast(&mcbi->mcbi_cv);
857 	}
858 	mutex_exit(mcbi->mcbi_lockp);
859 }
860 
861 static boolean_t
mac_callback_lookup(mac_cb_t ** mcb_headp,mac_cb_t * mcb_elem)862 mac_callback_lookup(mac_cb_t **mcb_headp, mac_cb_t *mcb_elem)
863 {
864 	mac_cb_t	*mcb;
865 
866 	/* Verify it is not already in the list */
867 	for (mcb = *mcb_headp; mcb != NULL; mcb = mcb->mcb_nextp) {
868 		if (mcb == mcb_elem)
869 			return (B_TRUE);
870 	}
871 
872 	return (B_FALSE);
873 }
874 
875 static boolean_t
mac_callback_find(mac_cb_info_t * mcbi,mac_cb_t ** mcb_headp,mac_cb_t * mcb_elem)876 mac_callback_find(mac_cb_info_t *mcbi, mac_cb_t **mcb_headp, mac_cb_t *mcb_elem)
877 {
878 	boolean_t	found;
879 
880 	mutex_enter(mcbi->mcbi_lockp);
881 	found = mac_callback_lookup(mcb_headp, mcb_elem);
882 	mutex_exit(mcbi->mcbi_lockp);
883 
884 	return (found);
885 }
886 
887 /* Free the list of removed callbacks */
888 void
mac_callback_free(mac_cb_t * rmlist)889 mac_callback_free(mac_cb_t *rmlist)
890 {
891 	mac_cb_t	*mcb;
892 	mac_cb_t	*mcb_next;
893 
894 	for (mcb = rmlist; mcb != NULL; mcb = mcb_next) {
895 		mcb_next = mcb->mcb_nextp;
896 		kmem_free(mcb->mcb_objp, mcb->mcb_objsize);
897 	}
898 }
899 
900 void
i_mac_notify(mac_impl_t * mip,mac_notify_type_t type)901 i_mac_notify(mac_impl_t *mip, mac_notify_type_t type)
902 {
903 	mac_cb_info_t	*mcbi;
904 
905 	/*
906 	 * Signal the notify thread even after mi_ref has become zero and
907 	 * mi_disabled is set. The synchronization with the notify thread
908 	 * happens in mac_unregister and that implies the driver must make
909 	 * sure it is single-threaded (with respect to mac calls) and that
910 	 * all pending mac calls have returned before it calls mac_unregister
911 	 */
912 	rw_enter(&i_mac_impl_lock, RW_READER);
913 	if (mip->mi_state_flags & MIS_DISABLED)
914 		goto exit;
915 
916 	/*
917 	 * Guard against incorrect notifications.  (Running a newer
918 	 * mac client against an older implementation?)
919 	 */
920 	if (type >= MAC_NNOTE)
921 		goto exit;
922 
923 	mcbi = &mip->mi_notify_cb_info;
924 	mutex_enter(mcbi->mcbi_lockp);
925 	mip->mi_notify_bits |= (1 << type);
926 	cv_broadcast(&mcbi->mcbi_cv);
927 	mutex_exit(mcbi->mcbi_lockp);
928 
929 exit:
930 	rw_exit(&i_mac_impl_lock);
931 }
932 
933 /*
934  * Mac serialization primitives. Please see the block comment at the
935  * top of the file.
936  */
937 void
i_mac_perim_enter(mac_impl_t * mip)938 i_mac_perim_enter(mac_impl_t *mip)
939 {
940 	mac_client_impl_t	*mcip;
941 
942 	if (mip->mi_state_flags & MIS_IS_VNIC) {
943 		/*
944 		 * This is a VNIC. Return the lower mac since that is what
945 		 * we want to serialize on.
946 		 */
947 		mcip = mac_vnic_lower(mip);
948 		mip = mcip->mci_mip;
949 	}
950 
951 	mutex_enter(&mip->mi_perim_lock);
952 	if (mip->mi_perim_owner == curthread) {
953 		mip->mi_perim_ocnt++;
954 		mutex_exit(&mip->mi_perim_lock);
955 		return;
956 	}
957 
958 	while (mip->mi_perim_owner != NULL)
959 		cv_wait(&mip->mi_perim_cv, &mip->mi_perim_lock);
960 
961 	mip->mi_perim_owner = curthread;
962 	ASSERT(mip->mi_perim_ocnt == 0);
963 	mip->mi_perim_ocnt++;
964 #ifdef DEBUG
965 	mip->mi_perim_stack_depth = getpcstack(mip->mi_perim_stack,
966 	    MAC_PERIM_STACK_DEPTH);
967 #endif
968 	mutex_exit(&mip->mi_perim_lock);
969 }
970 
971 int
i_mac_perim_enter_nowait(mac_impl_t * mip)972 i_mac_perim_enter_nowait(mac_impl_t *mip)
973 {
974 	/*
975 	 * The vnic is a special case, since the serialization is done based
976 	 * on the lower mac. If the lower mac is busy, it does not imply the
977 	 * vnic can't be unregistered. But in the case of other drivers,
978 	 * a busy perimeter or open mac handles implies that the mac is busy
979 	 * and can't be unregistered.
980 	 */
981 	if (mip->mi_state_flags & MIS_IS_VNIC) {
982 		i_mac_perim_enter(mip);
983 		return (0);
984 	}
985 
986 	mutex_enter(&mip->mi_perim_lock);
987 	if (mip->mi_perim_owner != NULL) {
988 		mutex_exit(&mip->mi_perim_lock);
989 		return (EBUSY);
990 	}
991 	ASSERT(mip->mi_perim_ocnt == 0);
992 	mip->mi_perim_owner = curthread;
993 	mip->mi_perim_ocnt++;
994 	mutex_exit(&mip->mi_perim_lock);
995 
996 	return (0);
997 }
998 
999 void
i_mac_perim_exit(mac_impl_t * mip)1000 i_mac_perim_exit(mac_impl_t *mip)
1001 {
1002 	mac_client_impl_t *mcip;
1003 
1004 	if (mip->mi_state_flags & MIS_IS_VNIC) {
1005 		/*
1006 		 * This is a VNIC. Return the lower mac since that is what
1007 		 * we want to serialize on.
1008 		 */
1009 		mcip = mac_vnic_lower(mip);
1010 		mip = mcip->mci_mip;
1011 	}
1012 
1013 	ASSERT(mip->mi_perim_owner == curthread && mip->mi_perim_ocnt != 0);
1014 
1015 	mutex_enter(&mip->mi_perim_lock);
1016 	if (--mip->mi_perim_ocnt == 0) {
1017 		mip->mi_perim_owner = NULL;
1018 		cv_signal(&mip->mi_perim_cv);
1019 	}
1020 	mutex_exit(&mip->mi_perim_lock);
1021 }
1022 
1023 /*
1024  * Returns whether the current thread holds the mac perimeter. Used in making
1025  * assertions.
1026  */
1027 boolean_t
mac_perim_held(mac_handle_t mh)1028 mac_perim_held(mac_handle_t mh)
1029 {
1030 	mac_impl_t	*mip = (mac_impl_t *)mh;
1031 	mac_client_impl_t *mcip;
1032 
1033 	if (mip->mi_state_flags & MIS_IS_VNIC) {
1034 		/*
1035 		 * This is a VNIC. Return the lower mac since that is what
1036 		 * we want to serialize on.
1037 		 */
1038 		mcip = mac_vnic_lower(mip);
1039 		mip = mcip->mci_mip;
1040 	}
1041 	return (mip->mi_perim_owner == curthread);
1042 }
1043 
1044 /*
1045  * mac client interfaces to enter the mac perimeter of a mac end point, given
1046  * its mac handle, or macname or linkid.
1047  */
1048 void
mac_perim_enter_by_mh(mac_handle_t mh,mac_perim_handle_t * mphp)1049 mac_perim_enter_by_mh(mac_handle_t mh, mac_perim_handle_t *mphp)
1050 {
1051 	mac_impl_t	*mip = (mac_impl_t *)mh;
1052 
1053 	i_mac_perim_enter(mip);
1054 	/*
1055 	 * The mac_perim_handle_t returned encodes the 'mip' and whether a
1056 	 * mac_open has been done internally while entering the perimeter.
1057 	 * This information is used in mac_perim_exit
1058 	 */
1059 	MAC_ENCODE_MPH(*mphp, mip, 0);
1060 }
1061 
1062 int
mac_perim_enter_by_macname(const char * name,mac_perim_handle_t * mphp)1063 mac_perim_enter_by_macname(const char *name, mac_perim_handle_t *mphp)
1064 {
1065 	int	err;
1066 	mac_handle_t	mh;
1067 
1068 	if ((err = mac_open(name, &mh)) != 0)
1069 		return (err);
1070 
1071 	mac_perim_enter_by_mh(mh, mphp);
1072 	MAC_ENCODE_MPH(*mphp, mh, 1);
1073 	return (0);
1074 }
1075 
1076 int
mac_perim_enter_by_linkid(datalink_id_t linkid,mac_perim_handle_t * mphp)1077 mac_perim_enter_by_linkid(datalink_id_t linkid, mac_perim_handle_t *mphp)
1078 {
1079 	int	err;
1080 	mac_handle_t	mh;
1081 
1082 	if ((err = mac_open_by_linkid(linkid, &mh)) != 0)
1083 		return (err);
1084 
1085 	mac_perim_enter_by_mh(mh, mphp);
1086 	MAC_ENCODE_MPH(*mphp, mh, 1);
1087 	return (0);
1088 }
1089 
1090 void
mac_perim_exit(mac_perim_handle_t mph)1091 mac_perim_exit(mac_perim_handle_t mph)
1092 {
1093 	mac_impl_t	*mip;
1094 	boolean_t	need_close;
1095 
1096 	MAC_DECODE_MPH(mph, mip, need_close);
1097 	i_mac_perim_exit(mip);
1098 	if (need_close)
1099 		mac_close((mac_handle_t)mip);
1100 }
1101 
1102 int
mac_hold(const char * macname,mac_impl_t ** pmip)1103 mac_hold(const char *macname, mac_impl_t **pmip)
1104 {
1105 	mac_impl_t	*mip;
1106 	int		err;
1107 
1108 	/*
1109 	 * Check the device name length to make sure it won't overflow our
1110 	 * buffer.
1111 	 */
1112 	if (strlen(macname) >= MAXNAMELEN)
1113 		return (EINVAL);
1114 
1115 	/*
1116 	 * Look up its entry in the global hash table.
1117 	 */
1118 	rw_enter(&i_mac_impl_lock, RW_WRITER);
1119 	err = mod_hash_find(i_mac_impl_hash, (mod_hash_key_t)macname,
1120 	    (mod_hash_val_t *)&mip);
1121 
1122 	if (err != 0) {
1123 		rw_exit(&i_mac_impl_lock);
1124 		return (ENOENT);
1125 	}
1126 
1127 	if (mip->mi_state_flags & MIS_DISABLED) {
1128 		rw_exit(&i_mac_impl_lock);
1129 		return (ENOENT);
1130 	}
1131 
1132 	if (mip->mi_state_flags & MIS_EXCLUSIVE_HELD) {
1133 		rw_exit(&i_mac_impl_lock);
1134 		return (EBUSY);
1135 	}
1136 
1137 	mip->mi_ref++;
1138 	rw_exit(&i_mac_impl_lock);
1139 
1140 	*pmip = mip;
1141 	return (0);
1142 }
1143 
1144 void
mac_rele(mac_impl_t * mip)1145 mac_rele(mac_impl_t *mip)
1146 {
1147 	rw_enter(&i_mac_impl_lock, RW_WRITER);
1148 	ASSERT(mip->mi_ref != 0);
1149 	if (--mip->mi_ref == 0) {
1150 		ASSERT(mip->mi_nactiveclients == 0 &&
1151 		    !(mip->mi_state_flags & MIS_EXCLUSIVE));
1152 	}
1153 	rw_exit(&i_mac_impl_lock);
1154 }
1155 
1156 /*
1157  * Private GLDv3 function to start a MAC instance.
1158  */
1159 int
mac_start(mac_handle_t mh)1160 mac_start(mac_handle_t mh)
1161 {
1162 	mac_impl_t	*mip = (mac_impl_t *)mh;
1163 	int		err = 0;
1164 	mac_group_t	*defgrp;
1165 
1166 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1167 	ASSERT(mip->mi_start != NULL);
1168 
1169 	/*
1170 	 * Check whether the device is already started.
1171 	 */
1172 	if (mip->mi_active++ == 0) {
1173 		mac_ring_t *ring = NULL;
1174 
1175 		/*
1176 		 * Start the device.
1177 		 */
1178 		err = mip->mi_start(mip->mi_driver);
1179 		if (err != 0) {
1180 			mip->mi_active--;
1181 			return (err);
1182 		}
1183 
1184 		/*
1185 		 * Start the default tx ring.
1186 		 */
1187 		if (mip->mi_default_tx_ring != NULL) {
1188 
1189 			ring = (mac_ring_t *)mip->mi_default_tx_ring;
1190 			if (ring->mr_state != MR_INUSE) {
1191 				err = mac_start_ring(ring);
1192 				if (err != 0) {
1193 					mip->mi_active--;
1194 					return (err);
1195 				}
1196 			}
1197 		}
1198 
1199 		if ((defgrp = MAC_DEFAULT_RX_GROUP(mip)) != NULL) {
1200 			/*
1201 			 * Start the default group which is responsible
1202 			 * for receiving broadcast and multicast
1203 			 * traffic for both primary and non-primary
1204 			 * MAC clients.
1205 			 */
1206 			ASSERT(defgrp->mrg_state == MAC_GROUP_STATE_REGISTERED);
1207 			err = mac_start_group_and_rings(defgrp);
1208 			if (err != 0) {
1209 				mip->mi_active--;
1210 				if ((ring != NULL) &&
1211 				    (ring->mr_state == MR_INUSE))
1212 					mac_stop_ring(ring);
1213 				return (err);
1214 			}
1215 			mac_set_group_state(defgrp, MAC_GROUP_STATE_SHARED);
1216 		}
1217 	}
1218 
1219 	return (err);
1220 }
1221 
1222 /*
1223  * Private GLDv3 function to stop a MAC instance.
1224  */
1225 void
mac_stop(mac_handle_t mh)1226 mac_stop(mac_handle_t mh)
1227 {
1228 	mac_impl_t	*mip = (mac_impl_t *)mh;
1229 	mac_group_t	*grp;
1230 
1231 	ASSERT(mip->mi_stop != NULL);
1232 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1233 
1234 	/*
1235 	 * Check whether the device is still needed.
1236 	 */
1237 	ASSERT(mip->mi_active != 0);
1238 	if (--mip->mi_active == 0) {
1239 		if ((grp = MAC_DEFAULT_RX_GROUP(mip)) != NULL) {
1240 			/*
1241 			 * There should be no more active clients since the
1242 			 * MAC is being stopped. Stop the default RX group
1243 			 * and transition it back to registered state.
1244 			 *
1245 			 * When clients are torn down, the groups
1246 			 * are release via mac_release_rx_group which
1247 			 * knows the the default group is always in
1248 			 * started mode since broadcast uses it. So
1249 			 * we can assert that their are no clients
1250 			 * (since mac_bcast_add doesn't register itself
1251 			 * as a client) and group is in SHARED state.
1252 			 */
1253 			ASSERT(grp->mrg_state == MAC_GROUP_STATE_SHARED);
1254 			ASSERT(MAC_GROUP_NO_CLIENT(grp) &&
1255 			    mip->mi_nactiveclients == 0);
1256 			mac_stop_group_and_rings(grp);
1257 			mac_set_group_state(grp, MAC_GROUP_STATE_REGISTERED);
1258 		}
1259 
1260 		if (mip->mi_default_tx_ring != NULL) {
1261 			mac_ring_t *ring;
1262 
1263 			ring = (mac_ring_t *)mip->mi_default_tx_ring;
1264 			if (ring->mr_state == MR_INUSE) {
1265 				mac_stop_ring(ring);
1266 				ring->mr_flag = 0;
1267 			}
1268 		}
1269 
1270 		/*
1271 		 * Stop the device.
1272 		 */
1273 		mip->mi_stop(mip->mi_driver);
1274 	}
1275 }
1276 
1277 int
i_mac_promisc_set(mac_impl_t * mip,boolean_t on)1278 i_mac_promisc_set(mac_impl_t *mip, boolean_t on)
1279 {
1280 	int		err = 0;
1281 
1282 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1283 	ASSERT(mip->mi_setpromisc != NULL);
1284 
1285 	if (on) {
1286 		/*
1287 		 * Enable promiscuous mode on the device if not yet enabled.
1288 		 */
1289 		if (mip->mi_devpromisc++ == 0) {
1290 			err = mip->mi_setpromisc(mip->mi_driver, B_TRUE);
1291 			if (err != 0) {
1292 				mip->mi_devpromisc--;
1293 				return (err);
1294 			}
1295 			i_mac_notify(mip, MAC_NOTE_DEVPROMISC);
1296 		}
1297 	} else {
1298 		if (mip->mi_devpromisc == 0)
1299 			return (EPROTO);
1300 
1301 		/*
1302 		 * Disable promiscuous mode on the device if this is the last
1303 		 * enabling.
1304 		 */
1305 		if (--mip->mi_devpromisc == 0) {
1306 			err = mip->mi_setpromisc(mip->mi_driver, B_FALSE);
1307 			if (err != 0) {
1308 				mip->mi_devpromisc++;
1309 				return (err);
1310 			}
1311 			i_mac_notify(mip, MAC_NOTE_DEVPROMISC);
1312 		}
1313 	}
1314 
1315 	return (0);
1316 }
1317 
1318 /*
1319  * The promiscuity state can change any time. If the caller needs to take
1320  * actions that are atomic with the promiscuity state, then the caller needs
1321  * to bracket the entire sequence with mac_perim_enter/exit
1322  */
1323 boolean_t
mac_promisc_get(mac_handle_t mh)1324 mac_promisc_get(mac_handle_t mh)
1325 {
1326 	mac_impl_t		*mip = (mac_impl_t *)mh;
1327 
1328 	/*
1329 	 * Return the current promiscuity.
1330 	 */
1331 	return (mip->mi_devpromisc != 0);
1332 }
1333 
1334 /*
1335  * Invoked at MAC instance attach time to initialize the list
1336  * of factory MAC addresses supported by a MAC instance. This function
1337  * builds a local cache in the mac_impl_t for the MAC addresses
1338  * supported by the underlying hardware. The MAC clients themselves
1339  * use the mac_addr_factory*() functions to query and reserve
1340  * factory MAC addresses.
1341  */
1342 void
mac_addr_factory_init(mac_impl_t * mip)1343 mac_addr_factory_init(mac_impl_t *mip)
1344 {
1345 	mac_capab_multifactaddr_t capab;
1346 	uint8_t *addr;
1347 	int i;
1348 
1349 	/*
1350 	 * First round to see how many factory MAC addresses are available.
1351 	 */
1352 	bzero(&capab, sizeof (capab));
1353 	if (!i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_MULTIFACTADDR,
1354 	    &capab) || (capab.mcm_naddr == 0)) {
1355 		/*
1356 		 * The MAC instance doesn't support multiple factory
1357 		 * MAC addresses, we're done here.
1358 		 */
1359 		return;
1360 	}
1361 
1362 	/*
1363 	 * Allocate the space and get all the factory addresses.
1364 	 */
1365 	addr = kmem_alloc(capab.mcm_naddr * MAXMACADDRLEN, KM_SLEEP);
1366 	capab.mcm_getaddr(mip->mi_driver, capab.mcm_naddr, addr);
1367 
1368 	mip->mi_factory_addr_num = capab.mcm_naddr;
1369 	mip->mi_factory_addr = kmem_zalloc(mip->mi_factory_addr_num *
1370 	    sizeof (mac_factory_addr_t), KM_SLEEP);
1371 
1372 	for (i = 0; i < capab.mcm_naddr; i++) {
1373 		bcopy(addr + i * MAXMACADDRLEN,
1374 		    mip->mi_factory_addr[i].mfa_addr,
1375 		    mip->mi_type->mt_addr_length);
1376 		mip->mi_factory_addr[i].mfa_in_use = B_FALSE;
1377 	}
1378 
1379 	kmem_free(addr, capab.mcm_naddr * MAXMACADDRLEN);
1380 }
1381 
1382 void
mac_addr_factory_fini(mac_impl_t * mip)1383 mac_addr_factory_fini(mac_impl_t *mip)
1384 {
1385 	if (mip->mi_factory_addr == NULL) {
1386 		ASSERT(mip->mi_factory_addr_num == 0);
1387 		return;
1388 	}
1389 
1390 	kmem_free(mip->mi_factory_addr, mip->mi_factory_addr_num *
1391 	    sizeof (mac_factory_addr_t));
1392 
1393 	mip->mi_factory_addr = NULL;
1394 	mip->mi_factory_addr_num = 0;
1395 }
1396 
1397 /*
1398  * Reserve a factory MAC address. If *slot is set to -1, the function
1399  * attempts to reserve any of the available factory MAC addresses and
1400  * returns the reserved slot id. If no slots are available, the function
1401  * returns ENOSPC. If *slot is not set to -1, the function reserves
1402  * the specified slot if it is available, or returns EBUSY is the slot
1403  * is already used. Returns ENOTSUP if the underlying MAC does not
1404  * support multiple factory addresses. If the slot number is not -1 but
1405  * is invalid, returns EINVAL.
1406  */
1407 int
mac_addr_factory_reserve(mac_client_handle_t mch,int * slot)1408 mac_addr_factory_reserve(mac_client_handle_t mch, int *slot)
1409 {
1410 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1411 	mac_impl_t *mip = mcip->mci_mip;
1412 	int i, ret = 0;
1413 
1414 	i_mac_perim_enter(mip);
1415 	/*
1416 	 * Protect against concurrent readers that may need a self-consistent
1417 	 * view of the factory addresses
1418 	 */
1419 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
1420 
1421 	if (mip->mi_factory_addr_num == 0) {
1422 		ret = ENOTSUP;
1423 		goto bail;
1424 	}
1425 
1426 	if (*slot != -1) {
1427 		/* check the specified slot */
1428 		if (*slot < 1 || *slot > mip->mi_factory_addr_num) {
1429 			ret = EINVAL;
1430 			goto bail;
1431 		}
1432 		if (mip->mi_factory_addr[*slot-1].mfa_in_use) {
1433 			ret = EBUSY;
1434 			goto bail;
1435 		}
1436 	} else {
1437 		/* pick the next available slot */
1438 		for (i = 0; i < mip->mi_factory_addr_num; i++) {
1439 			if (!mip->mi_factory_addr[i].mfa_in_use)
1440 				break;
1441 		}
1442 
1443 		if (i == mip->mi_factory_addr_num) {
1444 			ret = ENOSPC;
1445 			goto bail;
1446 		}
1447 		*slot = i+1;
1448 	}
1449 
1450 	mip->mi_factory_addr[*slot-1].mfa_in_use = B_TRUE;
1451 	mip->mi_factory_addr[*slot-1].mfa_client = mcip;
1452 
1453 bail:
1454 	rw_exit(&mip->mi_rw_lock);
1455 	i_mac_perim_exit(mip);
1456 	return (ret);
1457 }
1458 
1459 /*
1460  * Release the specified factory MAC address slot.
1461  */
1462 void
mac_addr_factory_release(mac_client_handle_t mch,uint_t slot)1463 mac_addr_factory_release(mac_client_handle_t mch, uint_t slot)
1464 {
1465 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1466 	mac_impl_t *mip = mcip->mci_mip;
1467 
1468 	i_mac_perim_enter(mip);
1469 	/*
1470 	 * Protect against concurrent readers that may need a self-consistent
1471 	 * view of the factory addresses
1472 	 */
1473 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
1474 
1475 	ASSERT(slot > 0 && slot <= mip->mi_factory_addr_num);
1476 	ASSERT(mip->mi_factory_addr[slot-1].mfa_in_use);
1477 
1478 	mip->mi_factory_addr[slot-1].mfa_in_use = B_FALSE;
1479 
1480 	rw_exit(&mip->mi_rw_lock);
1481 	i_mac_perim_exit(mip);
1482 }
1483 
1484 /*
1485  * Stores in mac_addr the value of the specified MAC address. Returns
1486  * 0 on success, or EINVAL if the slot number is not valid for the MAC.
1487  * The caller must provide a string of at least MAXNAMELEN bytes.
1488  */
1489 void
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)1490 mac_addr_factory_value(mac_handle_t mh, int slot, uchar_t *mac_addr,
1491     uint_t *addr_len, char *client_name, boolean_t *in_use_arg)
1492 {
1493 	mac_impl_t *mip = (mac_impl_t *)mh;
1494 	boolean_t in_use;
1495 
1496 	ASSERT(slot > 0 && slot <= mip->mi_factory_addr_num);
1497 
1498 	/*
1499 	 * Readers need to hold mi_rw_lock. Writers need to hold mac perimeter
1500 	 * and mi_rw_lock
1501 	 */
1502 	rw_enter(&mip->mi_rw_lock, RW_READER);
1503 	bcopy(mip->mi_factory_addr[slot-1].mfa_addr, mac_addr, MAXMACADDRLEN);
1504 	*addr_len = mip->mi_type->mt_addr_length;
1505 	in_use = mip->mi_factory_addr[slot-1].mfa_in_use;
1506 	if (in_use && client_name != NULL) {
1507 		bcopy(mip->mi_factory_addr[slot-1].mfa_client->mci_name,
1508 		    client_name, MAXNAMELEN);
1509 	}
1510 	if (in_use_arg != NULL)
1511 		*in_use_arg = in_use;
1512 	rw_exit(&mip->mi_rw_lock);
1513 }
1514 
1515 /*
1516  * Returns the number of factory MAC addresses (in addition to the
1517  * primary MAC address), 0 if the underlying MAC doesn't support
1518  * that feature.
1519  */
1520 uint_t
mac_addr_factory_num(mac_handle_t mh)1521 mac_addr_factory_num(mac_handle_t mh)
1522 {
1523 	mac_impl_t *mip = (mac_impl_t *)mh;
1524 
1525 	return (mip->mi_factory_addr_num);
1526 }
1527 
1528 
1529 void
mac_rx_group_unmark(mac_group_t * grp,uint_t flag)1530 mac_rx_group_unmark(mac_group_t *grp, uint_t flag)
1531 {
1532 	mac_ring_t	*ring;
1533 
1534 	for (ring = grp->mrg_rings; ring != NULL; ring = ring->mr_next)
1535 		ring->mr_flag &= ~flag;
1536 }
1537 
1538 /*
1539  * The following mac_hwrings_xxx() functions are private mac client functions
1540  * used by the aggr driver to access and control the underlying HW Rx group
1541  * and rings. In this case, the aggr driver has exclusive control of the
1542  * underlying HW Rx group/rings, it calls the following functions to
1543  * start/stop the HW Rx rings, disable/enable polling, add/remove MAC
1544  * addresses, or set up the Rx callback.
1545  */
1546 /* ARGSUSED */
1547 static void
mac_hwrings_rx_process(void * arg,mac_resource_handle_t srs,mblk_t * mp_chain,boolean_t loopback)1548 mac_hwrings_rx_process(void *arg, mac_resource_handle_t srs,
1549     mblk_t *mp_chain, boolean_t loopback)
1550 {
1551 	mac_soft_ring_set_t	*mac_srs = (mac_soft_ring_set_t *)srs;
1552 	mac_srs_rx_t		*srs_rx = &mac_srs->srs_rx;
1553 	mac_direct_rx_t		proc;
1554 	void			*arg1;
1555 	mac_resource_handle_t	arg2;
1556 
1557 	proc = srs_rx->sr_func;
1558 	arg1 = srs_rx->sr_arg1;
1559 	arg2 = mac_srs->srs_mrh;
1560 
1561 	proc(arg1, arg2, mp_chain, NULL);
1562 }
1563 
1564 /*
1565  * This function is called to get the list of HW rings that are reserved by
1566  * an exclusive mac client.
1567  *
1568  * Return value: the number of HW rings.
1569  */
1570 int
mac_hwrings_get(mac_client_handle_t mch,mac_group_handle_t * hwgh,mac_ring_handle_t * hwrh,mac_ring_type_t rtype)1571 mac_hwrings_get(mac_client_handle_t mch, mac_group_handle_t *hwgh,
1572     mac_ring_handle_t *hwrh, mac_ring_type_t rtype)
1573 {
1574 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
1575 	flow_entry_t		*flent = mcip->mci_flent;
1576 	mac_group_t		*grp;
1577 	mac_ring_t		*ring;
1578 	int			cnt = 0;
1579 
1580 	if (rtype == MAC_RING_TYPE_RX) {
1581 		grp = flent->fe_rx_ring_group;
1582 	} else if (rtype == MAC_RING_TYPE_TX) {
1583 		grp = flent->fe_tx_ring_group;
1584 	} else {
1585 		ASSERT(B_FALSE);
1586 		return (-1);
1587 	}
1588 
1589 	/*
1590 	 * The MAC client did not reserve an Rx group, return directly.
1591 	 * This is probably because the underlying MAC does not support
1592 	 * any groups.
1593 	 */
1594 	if (hwgh != NULL)
1595 		*hwgh = NULL;
1596 	if (grp == NULL)
1597 		return (0);
1598 	/*
1599 	 * This group must be reserved by this MAC client.
1600 	 */
1601 	ASSERT((grp->mrg_state == MAC_GROUP_STATE_RESERVED) &&
1602 	    (mcip == MAC_GROUP_ONLY_CLIENT(grp)));
1603 
1604 	for (ring = grp->mrg_rings; ring != NULL; ring = ring->mr_next, cnt++) {
1605 		ASSERT(cnt < MAX_RINGS_PER_GROUP);
1606 		hwrh[cnt] = (mac_ring_handle_t)ring;
1607 	}
1608 	if (hwgh != NULL)
1609 		*hwgh = (mac_group_handle_t)grp;
1610 
1611 	return (cnt);
1612 }
1613 
1614 /*
1615  * Get the HW ring handles of the given group index. If the MAC
1616  * doesn't have a group at this index, or any groups at all, then 0 is
1617  * returned and hwgh is set to NULL. This is a private client API. The
1618  * MAC perimeter must be held when calling this function.
1619  *
1620  * mh: A handle to the MAC that owns the group.
1621  *
1622  * idx: The index of the HW group to be read.
1623  *
1624  * hwgh: If non-NULL, contains a handle to the HW group on return.
1625  *
1626  * hwrh: An array of ring handles pointing to the HW rings in the
1627  * group. The array must be large enough to hold a handle to each ring
1628  * in the group. To be safe, this array should be of size MAX_RINGS_PER_GROUP.
1629  *
1630  * rtype: Used to determine if we are fetching Rx or Tx rings.
1631  *
1632  * Returns the number of rings in the group.
1633  */
1634 uint_t
mac_hwrings_idx_get(mac_handle_t mh,uint_t idx,mac_group_handle_t * hwgh,mac_ring_handle_t * hwrh,mac_ring_type_t rtype)1635 mac_hwrings_idx_get(mac_handle_t mh, uint_t idx, mac_group_handle_t *hwgh,
1636     mac_ring_handle_t *hwrh, mac_ring_type_t rtype)
1637 {
1638 	mac_impl_t		*mip = (mac_impl_t *)mh;
1639 	mac_group_t		*grp;
1640 	mac_ring_t		*ring;
1641 	uint_t			cnt = 0;
1642 
1643 	/*
1644 	 * The MAC perimeter must be held when accessing the
1645 	 * mi_{rx,tx}_groups fields.
1646 	 */
1647 	ASSERT(MAC_PERIM_HELD(mh));
1648 	ASSERT(rtype == MAC_RING_TYPE_RX || rtype == MAC_RING_TYPE_TX);
1649 
1650 	if (rtype == MAC_RING_TYPE_RX) {
1651 		grp = mip->mi_rx_groups;
1652 	} else {
1653 		ASSERT(rtype == MAC_RING_TYPE_TX);
1654 		grp = mip->mi_tx_groups;
1655 	}
1656 
1657 	while (grp != NULL && grp->mrg_index != idx)
1658 		grp = grp->mrg_next;
1659 
1660 	/*
1661 	 * If the MAC doesn't have a group at this index or doesn't
1662 	 * impelement RINGS capab, then set hwgh to NULL and return 0.
1663 	 */
1664 	if (hwgh != NULL)
1665 		*hwgh = NULL;
1666 
1667 	if (grp == NULL)
1668 		return (0);
1669 
1670 	ASSERT3U(idx, ==, grp->mrg_index);
1671 
1672 	for (ring = grp->mrg_rings; ring != NULL; ring = ring->mr_next, cnt++) {
1673 		ASSERT3U(cnt, <, MAX_RINGS_PER_GROUP);
1674 		hwrh[cnt] = (mac_ring_handle_t)ring;
1675 	}
1676 
1677 	/* A group should always have at least one ring. */
1678 	ASSERT3U(cnt, >, 0);
1679 
1680 	if (hwgh != NULL)
1681 		*hwgh = (mac_group_handle_t)grp;
1682 
1683 	return (cnt);
1684 }
1685 
1686 /*
1687  * This function is called to get info about Tx/Rx rings.
1688  *
1689  * Return value: returns uint_t which will have various bits set
1690  * that indicates different properties of the ring.
1691  */
1692 uint_t
mac_hwring_getinfo(mac_ring_handle_t rh)1693 mac_hwring_getinfo(mac_ring_handle_t rh)
1694 {
1695 	mac_ring_t *ring = (mac_ring_t *)rh;
1696 	mac_ring_info_t *info = &ring->mr_info;
1697 
1698 	return (info->mri_flags);
1699 }
1700 
1701 /*
1702  * Set the passthru callback on the hardware ring.
1703  */
1704 void
mac_hwring_set_passthru(mac_ring_handle_t hwrh,mac_rx_t fn,void * arg1,mac_resource_handle_t arg2)1705 mac_hwring_set_passthru(mac_ring_handle_t hwrh, mac_rx_t fn, void *arg1,
1706     mac_resource_handle_t arg2)
1707 {
1708 	mac_ring_t *hwring = (mac_ring_t *)hwrh;
1709 
1710 	ASSERT3S(hwring->mr_type, ==, MAC_RING_TYPE_RX);
1711 
1712 	hwring->mr_classify_type = MAC_PASSTHRU_CLASSIFIER;
1713 
1714 	hwring->mr_pt_fn = fn;
1715 	hwring->mr_pt_arg1 = arg1;
1716 	hwring->mr_pt_arg2 = arg2;
1717 }
1718 
1719 /*
1720  * Clear the passthru callback on the hardware ring.
1721  */
1722 void
mac_hwring_clear_passthru(mac_ring_handle_t hwrh)1723 mac_hwring_clear_passthru(mac_ring_handle_t hwrh)
1724 {
1725 	mac_ring_t *hwring = (mac_ring_t *)hwrh;
1726 
1727 	ASSERT3S(hwring->mr_type, ==, MAC_RING_TYPE_RX);
1728 
1729 	hwring->mr_classify_type = MAC_NO_CLASSIFIER;
1730 
1731 	hwring->mr_pt_fn = NULL;
1732 	hwring->mr_pt_arg1 = NULL;
1733 	hwring->mr_pt_arg2 = NULL;
1734 }
1735 
1736 void
mac_client_set_flow_cb(mac_client_handle_t mch,mac_rx_t func,void * arg1)1737 mac_client_set_flow_cb(mac_client_handle_t mch, mac_rx_t func, void *arg1)
1738 {
1739 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
1740 	flow_entry_t		*flent = mcip->mci_flent;
1741 
1742 	mutex_enter(&flent->fe_lock);
1743 	flent->fe_cb_fn = (flow_fn_t)func;
1744 	flent->fe_cb_arg1 = arg1;
1745 	flent->fe_cb_arg2 = NULL;
1746 	flent->fe_flags &= ~FE_MC_NO_DATAPATH;
1747 	mutex_exit(&flent->fe_lock);
1748 }
1749 
1750 void
mac_client_clear_flow_cb(mac_client_handle_t mch)1751 mac_client_clear_flow_cb(mac_client_handle_t mch)
1752 {
1753 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
1754 	flow_entry_t		*flent = mcip->mci_flent;
1755 
1756 	mutex_enter(&flent->fe_lock);
1757 	flent->fe_cb_fn = (flow_fn_t)mac_rx_def;
1758 	flent->fe_cb_arg1 = NULL;
1759 	flent->fe_cb_arg2 = NULL;
1760 	flent->fe_flags |= FE_MC_NO_DATAPATH;
1761 	mutex_exit(&flent->fe_lock);
1762 }
1763 
1764 /*
1765  * Export ddi interrupt handles from the HW ring to the pseudo ring and
1766  * setup the RX callback of the mac client which exclusively controls
1767  * HW ring.
1768  */
1769 void
mac_hwring_setup(mac_ring_handle_t hwrh,mac_resource_handle_t prh,mac_ring_handle_t pseudo_rh)1770 mac_hwring_setup(mac_ring_handle_t hwrh, mac_resource_handle_t prh,
1771     mac_ring_handle_t pseudo_rh)
1772 {
1773 	mac_ring_t		*hw_ring = (mac_ring_t *)hwrh;
1774 	mac_ring_t		*pseudo_ring;
1775 	mac_soft_ring_set_t	*mac_srs = hw_ring->mr_srs;
1776 
1777 	if (pseudo_rh != NULL) {
1778 		pseudo_ring = (mac_ring_t *)pseudo_rh;
1779 		/* Export the ddi handles to pseudo ring */
1780 		pseudo_ring->mr_info.mri_intr.mi_ddi_handle =
1781 		    hw_ring->mr_info.mri_intr.mi_ddi_handle;
1782 		pseudo_ring->mr_info.mri_intr.mi_ddi_shared =
1783 		    hw_ring->mr_info.mri_intr.mi_ddi_shared;
1784 		/*
1785 		 * Save a pointer to pseudo ring in the hw ring. If
1786 		 * interrupt handle changes, the hw ring will be
1787 		 * notified of the change (see mac_ring_intr_set())
1788 		 * and the appropriate change has to be made to
1789 		 * the pseudo ring that has exported the ddi handle.
1790 		 */
1791 		hw_ring->mr_prh = pseudo_rh;
1792 	}
1793 
1794 	if (hw_ring->mr_type == MAC_RING_TYPE_RX) {
1795 		ASSERT(!(mac_srs->srs_type & SRST_TX));
1796 		mac_srs->srs_mrh = prh;
1797 		mac_srs->srs_rx.sr_lower_proc = mac_hwrings_rx_process;
1798 	}
1799 }
1800 
1801 void
mac_hwring_teardown(mac_ring_handle_t hwrh)1802 mac_hwring_teardown(mac_ring_handle_t hwrh)
1803 {
1804 	mac_ring_t		*hw_ring = (mac_ring_t *)hwrh;
1805 	mac_soft_ring_set_t	*mac_srs;
1806 
1807 	if (hw_ring == NULL)
1808 		return;
1809 	hw_ring->mr_prh = NULL;
1810 	if (hw_ring->mr_type == MAC_RING_TYPE_RX) {
1811 		mac_srs = hw_ring->mr_srs;
1812 		ASSERT(!(mac_srs->srs_type & SRST_TX));
1813 		mac_srs->srs_rx.sr_lower_proc = mac_rx_srs_process;
1814 		mac_srs->srs_mrh = NULL;
1815 	}
1816 }
1817 
1818 int
mac_hwring_disable_intr(mac_ring_handle_t rh)1819 mac_hwring_disable_intr(mac_ring_handle_t rh)
1820 {
1821 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
1822 	mac_intr_t *intr = &rr_ring->mr_info.mri_intr;
1823 
1824 	return (intr->mi_disable(intr->mi_handle));
1825 }
1826 
1827 int
mac_hwring_enable_intr(mac_ring_handle_t rh)1828 mac_hwring_enable_intr(mac_ring_handle_t rh)
1829 {
1830 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
1831 	mac_intr_t *intr = &rr_ring->mr_info.mri_intr;
1832 
1833 	return (intr->mi_enable(intr->mi_handle));
1834 }
1835 
1836 /*
1837  * Start the HW ring pointed to by rh.
1838  *
1839  * This is used by special MAC clients that are MAC themselves and
1840  * need to exert control over the underlying HW rings of the NIC.
1841  */
1842 int
mac_hwring_start(mac_ring_handle_t rh)1843 mac_hwring_start(mac_ring_handle_t rh)
1844 {
1845 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
1846 	int rv = 0;
1847 
1848 	if (rr_ring->mr_state != MR_INUSE)
1849 		rv = mac_start_ring(rr_ring);
1850 
1851 	return (rv);
1852 }
1853 
1854 /*
1855  * Stop the HW ring pointed to by rh. Also see mac_hwring_start().
1856  */
1857 void
mac_hwring_stop(mac_ring_handle_t rh)1858 mac_hwring_stop(mac_ring_handle_t rh)
1859 {
1860 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
1861 
1862 	if (rr_ring->mr_state != MR_FREE)
1863 		mac_stop_ring(rr_ring);
1864 }
1865 
1866 /*
1867  * Remove the quiesced flag from the HW ring pointed to by rh.
1868  *
1869  * This is used by special MAC clients that are MAC themselves and
1870  * need to exert control over the underlying HW rings of the NIC.
1871  */
1872 int
mac_hwring_activate(mac_ring_handle_t rh)1873 mac_hwring_activate(mac_ring_handle_t rh)
1874 {
1875 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
1876 
1877 	MAC_RING_UNMARK(rr_ring, MR_QUIESCE);
1878 	return (0);
1879 }
1880 
1881 /*
1882  * Quiesce the HW ring pointed to by rh. Also see mac_hwring_activate().
1883  */
1884 void
mac_hwring_quiesce(mac_ring_handle_t rh)1885 mac_hwring_quiesce(mac_ring_handle_t rh)
1886 {
1887 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
1888 
1889 	mac_rx_ring_quiesce(rr_ring, MR_QUIESCE);
1890 }
1891 
1892 mblk_t *
mac_hwring_poll(mac_ring_handle_t rh,int bytes_to_pickup)1893 mac_hwring_poll(mac_ring_handle_t rh, int bytes_to_pickup)
1894 {
1895 	mac_ring_t *rr_ring = (mac_ring_t *)rh;
1896 	mac_ring_info_t *info = &rr_ring->mr_info;
1897 
1898 	return (info->mri_poll(info->mri_driver, bytes_to_pickup));
1899 }
1900 
1901 /*
1902  * Send packets through a selected tx ring.
1903  */
1904 mblk_t *
mac_hwring_tx(mac_ring_handle_t rh,mblk_t * mp)1905 mac_hwring_tx(mac_ring_handle_t rh, mblk_t *mp)
1906 {
1907 	mac_ring_t *ring = (mac_ring_t *)rh;
1908 	mac_ring_info_t *info = &ring->mr_info;
1909 
1910 	ASSERT(ring->mr_type == MAC_RING_TYPE_TX &&
1911 	    ring->mr_state >= MR_INUSE);
1912 	return (info->mri_tx(info->mri_driver, mp));
1913 }
1914 
1915 /*
1916  * Query stats for a particular rx/tx ring
1917  */
1918 int
mac_hwring_getstat(mac_ring_handle_t rh,uint_t stat,uint64_t * val)1919 mac_hwring_getstat(mac_ring_handle_t rh, uint_t stat, uint64_t *val)
1920 {
1921 	mac_ring_t	*ring = (mac_ring_t *)rh;
1922 	mac_ring_info_t *info = &ring->mr_info;
1923 
1924 	return (info->mri_stat(info->mri_driver, stat, val));
1925 }
1926 
1927 /*
1928  * Private function that is only used by aggr to send packets through
1929  * a port/Tx ring. Since aggr exposes a pseudo Tx ring even for ports
1930  * that does not expose Tx rings, aggr_ring_tx() entry point needs
1931  * access to mac_impl_t to send packets through m_tx() entry point.
1932  * It accomplishes this by calling mac_hwring_send_priv() function.
1933  */
1934 mblk_t *
mac_hwring_send_priv(mac_client_handle_t mch,mac_ring_handle_t rh,mblk_t * mp)1935 mac_hwring_send_priv(mac_client_handle_t mch, mac_ring_handle_t rh, mblk_t *mp)
1936 {
1937 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1938 	mac_impl_t *mip = mcip->mci_mip;
1939 
1940 	return (mac_provider_tx(mip, rh, mp, mcip));
1941 }
1942 
1943 /*
1944  * Private function that is only used by aggr to update the default transmission
1945  * ring. Because aggr exposes a pseudo Tx ring even for ports that may
1946  * temporarily be down, it may need to update the default ring that is used by
1947  * MAC such that it refers to a link that can actively be used to send traffic.
1948  * Note that this is different from the case where the port has been removed
1949  * from the group. In those cases, all of the rings will be torn down because
1950  * the ring will no longer exist. It's important to give aggr a case where the
1951  * rings can still exist such that it may be able to continue to send LACP PDUs
1952  * to potentially restore the link.
1953  */
1954 void
mac_hwring_set_default(mac_handle_t mh,mac_ring_handle_t rh)1955 mac_hwring_set_default(mac_handle_t mh, mac_ring_handle_t rh)
1956 {
1957 	mac_impl_t *mip = (mac_impl_t *)mh;
1958 	mac_ring_t *ring = (mac_ring_t *)rh;
1959 
1960 	ASSERT(MAC_PERIM_HELD(mh));
1961 	VERIFY(mip->mi_state_flags & MIS_IS_AGGR);
1962 
1963 	/*
1964 	 * We used to condition this assignment on the ring's
1965 	 * 'mr_state' being one of 'MR_INUSE'. However, there are
1966 	 * cases where this is called before the ring has any active
1967 	 * clients, and therefore is not marked as in use. Since the
1968 	 * sole purpose of this function is for aggr to make sure
1969 	 * 'mi_default_tx_ring' matches 'lg_tx_ports[0]', its
1970 	 * imperative that we update its value regardless of ring
1971 	 * state. Otherwise, we can end up in a state where
1972 	 * 'mi_default_tx_ring' points to a pseudo ring of a downed
1973 	 * port, even when 'lg_tx_ports[0]' points to a port that is
1974 	 * up.
1975 	 */
1976 	mip->mi_default_tx_ring = rh;
1977 }
1978 
1979 int
mac_hwgroup_addmac(mac_group_handle_t gh,const uint8_t * addr)1980 mac_hwgroup_addmac(mac_group_handle_t gh, const uint8_t *addr)
1981 {
1982 	mac_group_t *group = (mac_group_t *)gh;
1983 
1984 	return (mac_group_addmac(group, addr));
1985 }
1986 
1987 int
mac_hwgroup_remmac(mac_group_handle_t gh,const uint8_t * addr)1988 mac_hwgroup_remmac(mac_group_handle_t gh, const uint8_t *addr)
1989 {
1990 	mac_group_t *group = (mac_group_t *)gh;
1991 
1992 	return (mac_group_remmac(group, addr));
1993 }
1994 
1995 /*
1996  * Program the group's HW VLAN filter if it has such support.
1997  * Otherwise, the group will implicitly accept tagged traffic and
1998  * there is nothing to do.
1999  */
2000 int
mac_hwgroup_addvlan(mac_group_handle_t gh,uint16_t vid)2001 mac_hwgroup_addvlan(mac_group_handle_t gh, uint16_t vid)
2002 {
2003 	mac_group_t *group = (mac_group_t *)gh;
2004 
2005 	if (!MAC_GROUP_HW_VLAN(group))
2006 		return (0);
2007 
2008 	return (mac_group_addvlan(group, vid));
2009 }
2010 
2011 int
mac_hwgroup_remvlan(mac_group_handle_t gh,uint16_t vid)2012 mac_hwgroup_remvlan(mac_group_handle_t gh, uint16_t vid)
2013 {
2014 	mac_group_t *group = (mac_group_t *)gh;
2015 
2016 	if (!MAC_GROUP_HW_VLAN(group))
2017 		return (0);
2018 
2019 	return (mac_group_remvlan(group, vid));
2020 }
2021 
2022 /*
2023  * Determine if a MAC has HW VLAN support. This is a private API
2024  * consumed by aggr. In the future it might be nice to have a bitfield
2025  * in mac_capab_rings_t to track which forms of HW filtering are
2026  * supported by the MAC.
2027  */
2028 boolean_t
mac_has_hw_vlan(mac_handle_t mh)2029 mac_has_hw_vlan(mac_handle_t mh)
2030 {
2031 	mac_impl_t *mip = (mac_impl_t *)mh;
2032 
2033 	return (MAC_GROUP_HW_VLAN(mip->mi_rx_groups));
2034 }
2035 
2036 /*
2037  * Get the number of Rx HW groups on this MAC.
2038  */
2039 uint_t
mac_get_num_rx_groups(mac_handle_t mh)2040 mac_get_num_rx_groups(mac_handle_t mh)
2041 {
2042 	mac_impl_t *mip = (mac_impl_t *)mh;
2043 
2044 	ASSERT(MAC_PERIM_HELD(mh));
2045 	return (mip->mi_rx_group_count);
2046 }
2047 
2048 int
mac_set_promisc(mac_handle_t mh,boolean_t value)2049 mac_set_promisc(mac_handle_t mh, boolean_t value)
2050 {
2051 	mac_impl_t *mip = (mac_impl_t *)mh;
2052 
2053 	ASSERT(MAC_PERIM_HELD(mh));
2054 	return (i_mac_promisc_set(mip, value));
2055 }
2056 
2057 /*
2058  * Set the RX group to be shared/reserved. Note that the group must be
2059  * started/stopped outside of this function.
2060  */
2061 void
mac_set_group_state(mac_group_t * grp,mac_group_state_t state)2062 mac_set_group_state(mac_group_t *grp, mac_group_state_t state)
2063 {
2064 	/*
2065 	 * If there is no change in the group state, just return.
2066 	 */
2067 	if (grp->mrg_state == state)
2068 		return;
2069 
2070 	switch (state) {
2071 	case MAC_GROUP_STATE_RESERVED:
2072 		/*
2073 		 * Successfully reserved the group.
2074 		 *
2075 		 * Given that there is an exclusive client controlling this
2076 		 * group, we enable the group level polling when available,
2077 		 * so that SRSs get to turn on/off individual rings they's
2078 		 * assigned to.
2079 		 */
2080 		ASSERT(MAC_PERIM_HELD(grp->mrg_mh));
2081 
2082 		if (grp->mrg_type == MAC_RING_TYPE_RX &&
2083 		    GROUP_INTR_DISABLE_FUNC(grp) != NULL) {
2084 			GROUP_INTR_DISABLE_FUNC(grp)(GROUP_INTR_HANDLE(grp));
2085 		}
2086 		break;
2087 
2088 	case MAC_GROUP_STATE_SHARED:
2089 		/*
2090 		 * Set all rings of this group to software classified.
2091 		 * If the group has an overriding interrupt, then re-enable it.
2092 		 */
2093 		ASSERT(MAC_PERIM_HELD(grp->mrg_mh));
2094 
2095 		if (grp->mrg_type == MAC_RING_TYPE_RX &&
2096 		    GROUP_INTR_ENABLE_FUNC(grp) != NULL) {
2097 			GROUP_INTR_ENABLE_FUNC(grp)(GROUP_INTR_HANDLE(grp));
2098 		}
2099 		/* The ring is not available for reservations any more */
2100 		break;
2101 
2102 	case MAC_GROUP_STATE_REGISTERED:
2103 		/* Also callable from mac_register, perim is not held */
2104 		break;
2105 
2106 	default:
2107 		ASSERT(B_FALSE);
2108 		break;
2109 	}
2110 
2111 	grp->mrg_state = state;
2112 }
2113 
2114 /*
2115  * Quiesce future hardware classified packets for the specified Rx ring
2116  */
2117 static void
mac_rx_ring_quiesce(mac_ring_t * rx_ring,uint_t ring_flag)2118 mac_rx_ring_quiesce(mac_ring_t *rx_ring, uint_t ring_flag)
2119 {
2120 	ASSERT(rx_ring->mr_classify_type == MAC_HW_CLASSIFIER);
2121 	ASSERT(ring_flag == MR_CONDEMNED || ring_flag  == MR_QUIESCE);
2122 
2123 	mutex_enter(&rx_ring->mr_lock);
2124 	rx_ring->mr_flag |= ring_flag;
2125 	while (rx_ring->mr_refcnt != 0)
2126 		cv_wait(&rx_ring->mr_cv, &rx_ring->mr_lock);
2127 	mutex_exit(&rx_ring->mr_lock);
2128 }
2129 
2130 /*
2131  * Please see mac_tx for details about the per cpu locking scheme
2132  */
2133 static void
mac_tx_lock_all(mac_client_impl_t * mcip)2134 mac_tx_lock_all(mac_client_impl_t *mcip)
2135 {
2136 	int	i;
2137 
2138 	for (i = 0; i <= mac_tx_percpu_cnt; i++)
2139 		mutex_enter(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
2140 }
2141 
2142 static void
mac_tx_unlock_all(mac_client_impl_t * mcip)2143 mac_tx_unlock_all(mac_client_impl_t *mcip)
2144 {
2145 	int	i;
2146 
2147 	for (i = mac_tx_percpu_cnt; i >= 0; i--)
2148 		mutex_exit(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
2149 }
2150 
2151 static void
mac_tx_unlock_allbutzero(mac_client_impl_t * mcip)2152 mac_tx_unlock_allbutzero(mac_client_impl_t *mcip)
2153 {
2154 	int	i;
2155 
2156 	for (i = mac_tx_percpu_cnt; i > 0; i--)
2157 		mutex_exit(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
2158 }
2159 
2160 static int
mac_tx_sum_refcnt(mac_client_impl_t * mcip)2161 mac_tx_sum_refcnt(mac_client_impl_t *mcip)
2162 {
2163 	int	i;
2164 	int	refcnt = 0;
2165 
2166 	for (i = 0; i <= mac_tx_percpu_cnt; i++)
2167 		refcnt += mcip->mci_tx_pcpu[i].pcpu_tx_refcnt;
2168 
2169 	return (refcnt);
2170 }
2171 
2172 /*
2173  * Stop future Tx packets coming down from the client in preparation for
2174  * quiescing the Tx side. This is needed for dynamic reclaim and reassignment
2175  * of rings between clients
2176  */
2177 void
mac_tx_client_block(mac_client_impl_t * mcip)2178 mac_tx_client_block(mac_client_impl_t *mcip)
2179 {
2180 	mac_tx_lock_all(mcip);
2181 	mcip->mci_tx_flag |= MCI_TX_QUIESCE;
2182 	while (mac_tx_sum_refcnt(mcip) != 0) {
2183 		mac_tx_unlock_allbutzero(mcip);
2184 		cv_wait(&mcip->mci_tx_cv, &mcip->mci_tx_pcpu[0].pcpu_tx_lock);
2185 		mutex_exit(&mcip->mci_tx_pcpu[0].pcpu_tx_lock);
2186 		mac_tx_lock_all(mcip);
2187 	}
2188 	mac_tx_unlock_all(mcip);
2189 }
2190 
2191 void
mac_tx_client_unblock(mac_client_impl_t * mcip)2192 mac_tx_client_unblock(mac_client_impl_t *mcip)
2193 {
2194 	mac_tx_lock_all(mcip);
2195 	mcip->mci_tx_flag &= ~MCI_TX_QUIESCE;
2196 	mac_tx_unlock_all(mcip);
2197 	/*
2198 	 * We may fail to disable flow control for the last MAC_NOTE_TX
2199 	 * notification because the MAC client is quiesced. Send the
2200 	 * notification again.
2201 	 */
2202 	i_mac_notify(mcip->mci_mip, MAC_NOTE_TX);
2203 }
2204 
2205 /*
2206  * Wait for an SRS to quiesce. The SRS worker will signal us when the
2207  * quiesce is done.
2208  */
2209 static void
mac_srs_quiesce_wait(mac_soft_ring_set_t * srs,uint_t srs_flag)2210 mac_srs_quiesce_wait(mac_soft_ring_set_t *srs, uint_t srs_flag)
2211 {
2212 	mutex_enter(&srs->srs_lock);
2213 	while (!(srs->srs_state & srs_flag))
2214 		cv_wait(&srs->srs_quiesce_done_cv, &srs->srs_lock);
2215 	mutex_exit(&srs->srs_lock);
2216 }
2217 
2218 /*
2219  * Quiescing an Rx SRS is achieved by the following sequence. The protocol
2220  * works bottom up by cutting off packet flow from the bottommost point in the
2221  * mac, then the SRS, and then the soft rings. There are 2 use cases of this
2222  * mechanism. One is a temporary quiesce of the SRS, such as say while changing
2223  * the Rx callbacks. Another use case is Rx SRS teardown. In the former case
2224  * the QUIESCE prefix/suffix is used and in the latter the CONDEMNED is used
2225  * for the SRS and MR flags. In the former case the threads pause waiting for
2226  * a restart, while in the latter case the threads exit. The Tx SRS teardown
2227  * is also mostly similar to the above.
2228  *
2229  * 1. Stop future hardware classified packets at the lowest level in the mac.
2230  *    Remove any hardware classification rule (CONDEMNED case) and mark the
2231  *    rings as CONDEMNED or QUIESCE as appropriate. This prevents the mr_refcnt
2232  *    from increasing. Upcalls from the driver that come through hardware
2233  *    classification will be dropped in mac_rx from now on. Then we wait for
2234  *    the mr_refcnt to drop to zero. When the mr_refcnt reaches zero we are
2235  *    sure there aren't any upcall threads from the driver through hardware
2236  *    classification. In the case of SRS teardown we also remove the
2237  *    classification rule in the driver.
2238  *
2239  * 2. Stop future software classified packets by marking the flow entry with
2240  *    FE_QUIESCE or FE_CONDEMNED as appropriate which prevents the refcnt from
2241  *    increasing. We also remove the flow entry from the table in the latter
2242  *    case. Then wait for the fe_refcnt to reach an appropriate quiescent value
2243  *    that indicates there aren't any active threads using that flow entry.
2244  *
2245  * 3. Quiesce the SRS and softrings by signaling the SRS. The SRS poll thread,
2246  *    SRS worker thread, and the soft ring threads are quiesced in sequence
2247  *    with the SRS worker thread serving as a master controller. This
2248  *    mechansim is explained in mac_srs_worker_quiesce().
2249  *
2250  * The restart mechanism to reactivate the SRS and softrings is explained
2251  * in mac_srs_worker_restart(). Here we just signal the SRS worker to start the
2252  * restart sequence.
2253  */
2254 void
mac_rx_srs_quiesce(mac_soft_ring_set_t * srs,uint_t srs_quiesce_flag)2255 mac_rx_srs_quiesce(mac_soft_ring_set_t *srs, uint_t srs_quiesce_flag)
2256 {
2257 	flow_entry_t	*flent = srs->srs_flent;
2258 	uint_t	mr_flag, srs_done_flag;
2259 
2260 	ASSERT(MAC_PERIM_HELD((mac_handle_t)FLENT_TO_MIP(flent)));
2261 	ASSERT(!(srs->srs_type & SRST_TX));
2262 
2263 	if (srs_quiesce_flag == SRS_CONDEMNED) {
2264 		mr_flag = MR_CONDEMNED;
2265 		srs_done_flag = SRS_CONDEMNED_DONE;
2266 		if (srs->srs_type & SRST_CLIENT_POLL_ENABLED)
2267 			mac_srs_client_poll_disable(srs->srs_mcip, srs);
2268 	} else {
2269 		ASSERT(srs_quiesce_flag == SRS_QUIESCE);
2270 		mr_flag = MR_QUIESCE;
2271 		srs_done_flag = SRS_QUIESCE_DONE;
2272 		if (srs->srs_type & SRST_CLIENT_POLL_ENABLED)
2273 			mac_srs_client_poll_quiesce(srs->srs_mcip, srs);
2274 	}
2275 
2276 	if (srs->srs_ring != NULL) {
2277 		mac_rx_ring_quiesce(srs->srs_ring, mr_flag);
2278 	} else {
2279 		/*
2280 		 * SRS is driven by software classification. In case
2281 		 * of CONDEMNED, the top level teardown functions will
2282 		 * deal with flow removal.
2283 		 */
2284 		if (srs_quiesce_flag != SRS_CONDEMNED) {
2285 			FLOW_MARK(flent, FE_QUIESCE);
2286 			mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
2287 		}
2288 	}
2289 
2290 	/*
2291 	 * Signal the SRS to quiesce itself, and then cv_wait for the
2292 	 * SRS quiesce to complete. The SRS worker thread will wake us
2293 	 * up when the quiesce is complete
2294 	 */
2295 	mac_srs_signal(srs, srs_quiesce_flag);
2296 	mac_srs_quiesce_wait(srs, srs_done_flag);
2297 }
2298 
2299 /*
2300  * Remove an SRS.
2301  */
2302 void
mac_rx_srs_remove(mac_soft_ring_set_t * srs)2303 mac_rx_srs_remove(mac_soft_ring_set_t *srs)
2304 {
2305 	flow_entry_t *flent = srs->srs_flent;
2306 	int i;
2307 
2308 	mac_rx_srs_quiesce(srs, SRS_CONDEMNED);
2309 	/*
2310 	 * Locate and remove our entry in the fe_rx_srs[] array, and
2311 	 * adjust the fe_rx_srs array entries and array count by
2312 	 * moving the last entry into the vacated spot.
2313 	 */
2314 	mutex_enter(&flent->fe_lock);
2315 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
2316 		if (flent->fe_rx_srs[i] == srs)
2317 			break;
2318 	}
2319 
2320 	ASSERT(i != 0 && i < flent->fe_rx_srs_cnt);
2321 	if (i != flent->fe_rx_srs_cnt - 1) {
2322 		flent->fe_rx_srs[i] =
2323 		    flent->fe_rx_srs[flent->fe_rx_srs_cnt - 1];
2324 		i = flent->fe_rx_srs_cnt - 1;
2325 	}
2326 
2327 	flent->fe_rx_srs[i] = NULL;
2328 	flent->fe_rx_srs_cnt--;
2329 	mutex_exit(&flent->fe_lock);
2330 
2331 	mac_srs_free(srs);
2332 }
2333 
2334 static void
mac_srs_clear_flag(mac_soft_ring_set_t * srs,uint_t flag)2335 mac_srs_clear_flag(mac_soft_ring_set_t *srs, uint_t flag)
2336 {
2337 	mutex_enter(&srs->srs_lock);
2338 	srs->srs_state &= ~flag;
2339 	mutex_exit(&srs->srs_lock);
2340 }
2341 
2342 void
mac_rx_srs_restart(mac_soft_ring_set_t * srs)2343 mac_rx_srs_restart(mac_soft_ring_set_t *srs)
2344 {
2345 	flow_entry_t	*flent = srs->srs_flent;
2346 	mac_ring_t	*mr;
2347 
2348 	ASSERT(MAC_PERIM_HELD((mac_handle_t)FLENT_TO_MIP(flent)));
2349 	ASSERT((srs->srs_type & SRST_TX) == 0);
2350 
2351 	/*
2352 	 * This handles a change in the number of SRSs between the quiesce and
2353 	 * and restart operation of a flow.
2354 	 */
2355 	if (!SRS_QUIESCED(srs))
2356 		return;
2357 
2358 	/*
2359 	 * Signal the SRS to restart itself. Wait for the restart to complete
2360 	 * Note that we only restart the SRS if it is not marked as
2361 	 * permanently quiesced.
2362 	 */
2363 	if (!SRS_QUIESCED_PERMANENT(srs)) {
2364 		mac_srs_signal(srs, SRS_RESTART);
2365 		mac_srs_quiesce_wait(srs, SRS_RESTART_DONE);
2366 		mac_srs_clear_flag(srs, SRS_RESTART_DONE);
2367 
2368 		mac_srs_client_poll_restart(srs->srs_mcip, srs);
2369 	}
2370 
2371 	/* Finally clear the flags to let the packets in */
2372 	mr = srs->srs_ring;
2373 	if (mr != NULL) {
2374 		MAC_RING_UNMARK(mr, MR_QUIESCE);
2375 		/* In case the ring was stopped, safely restart it */
2376 		if (mr->mr_state != MR_INUSE)
2377 			(void) mac_start_ring(mr);
2378 	} else {
2379 		FLOW_UNMARK(flent, FE_QUIESCE);
2380 	}
2381 }
2382 
2383 /*
2384  * Temporary quiesce of a flow and associated Rx SRS.
2385  * Please see block comment above mac_rx_classify_flow_rem.
2386  */
2387 /* ARGSUSED */
2388 int
mac_rx_classify_flow_quiesce(flow_entry_t * flent,void * arg)2389 mac_rx_classify_flow_quiesce(flow_entry_t *flent, void *arg)
2390 {
2391 	int		i;
2392 
2393 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
2394 		mac_rx_srs_quiesce((mac_soft_ring_set_t *)flent->fe_rx_srs[i],
2395 		    SRS_QUIESCE);
2396 	}
2397 	return (0);
2398 }
2399 
2400 /*
2401  * Restart a flow and associated Rx SRS that has been quiesced temporarily
2402  * Please see block comment above mac_rx_classify_flow_rem
2403  */
2404 /* ARGSUSED */
2405 int
mac_rx_classify_flow_restart(flow_entry_t * flent,void * arg)2406 mac_rx_classify_flow_restart(flow_entry_t *flent, void *arg)
2407 {
2408 	int		i;
2409 
2410 	for (i = 0; i < flent->fe_rx_srs_cnt; i++)
2411 		mac_rx_srs_restart((mac_soft_ring_set_t *)flent->fe_rx_srs[i]);
2412 
2413 	return (0);
2414 }
2415 
2416 void
mac_srs_perm_quiesce(mac_client_handle_t mch,boolean_t on)2417 mac_srs_perm_quiesce(mac_client_handle_t mch, boolean_t on)
2418 {
2419 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2420 	flow_entry_t		*flent = mcip->mci_flent;
2421 	mac_impl_t		*mip = mcip->mci_mip;
2422 	mac_soft_ring_set_t	*mac_srs;
2423 	int			i;
2424 
2425 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2426 
2427 	if (flent == NULL)
2428 		return;
2429 
2430 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
2431 		mac_srs = flent->fe_rx_srs[i];
2432 		mutex_enter(&mac_srs->srs_lock);
2433 		if (on)
2434 			mac_srs->srs_state |= SRS_QUIESCE_PERM;
2435 		else
2436 			mac_srs->srs_state &= ~SRS_QUIESCE_PERM;
2437 		mutex_exit(&mac_srs->srs_lock);
2438 	}
2439 }
2440 
2441 void
mac_rx_client_quiesce(mac_client_handle_t mch)2442 mac_rx_client_quiesce(mac_client_handle_t mch)
2443 {
2444 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2445 	mac_impl_t		*mip = mcip->mci_mip;
2446 
2447 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2448 
2449 	if (MCIP_DATAPATH_SETUP(mcip)) {
2450 		(void) mac_rx_classify_flow_quiesce(mcip->mci_flent,
2451 		    NULL);
2452 		(void) mac_flow_walk_nolock(mcip->mci_subflow_tab,
2453 		    mac_rx_classify_flow_quiesce, NULL);
2454 	}
2455 }
2456 
2457 void
mac_rx_client_restart(mac_client_handle_t mch)2458 mac_rx_client_restart(mac_client_handle_t mch)
2459 {
2460 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2461 	mac_impl_t		*mip = mcip->mci_mip;
2462 
2463 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2464 
2465 	if (MCIP_DATAPATH_SETUP(mcip)) {
2466 		(void) mac_rx_classify_flow_restart(mcip->mci_flent, NULL);
2467 		(void) mac_flow_walk_nolock(mcip->mci_subflow_tab,
2468 		    mac_rx_classify_flow_restart, NULL);
2469 	}
2470 }
2471 
2472 /*
2473  * This function only quiesces the Tx SRS and softring worker threads. Callers
2474  * need to make sure that there aren't any mac client threads doing current or
2475  * future transmits in the mac before calling this function.
2476  */
2477 void
mac_tx_srs_quiesce(mac_soft_ring_set_t * srs,uint_t srs_quiesce_flag)2478 mac_tx_srs_quiesce(mac_soft_ring_set_t *srs, uint_t srs_quiesce_flag)
2479 {
2480 	mac_client_impl_t	*mcip = srs->srs_mcip;
2481 
2482 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
2483 
2484 	ASSERT(srs->srs_type & SRST_TX);
2485 	ASSERT(srs_quiesce_flag == SRS_CONDEMNED ||
2486 	    srs_quiesce_flag == SRS_QUIESCE);
2487 
2488 	/*
2489 	 * Signal the SRS to quiesce itself, and then cv_wait for the
2490 	 * SRS quiesce to complete. The SRS worker thread will wake us
2491 	 * up when the quiesce is complete
2492 	 */
2493 	mac_srs_signal(srs, srs_quiesce_flag);
2494 	mac_srs_quiesce_wait(srs, srs_quiesce_flag == SRS_QUIESCE ?
2495 	    SRS_QUIESCE_DONE : SRS_CONDEMNED_DONE);
2496 }
2497 
2498 void
mac_tx_srs_restart(mac_soft_ring_set_t * srs)2499 mac_tx_srs_restart(mac_soft_ring_set_t *srs)
2500 {
2501 	/*
2502 	 * Resizing the fanout could result in creation of new SRSs.
2503 	 * They may not necessarily be in the quiesced state in which
2504 	 * case it need be restarted
2505 	 */
2506 	if (!SRS_QUIESCED(srs))
2507 		return;
2508 
2509 	mac_srs_signal(srs, SRS_RESTART);
2510 	mac_srs_quiesce_wait(srs, SRS_RESTART_DONE);
2511 	mac_srs_clear_flag(srs, SRS_RESTART_DONE);
2512 }
2513 
2514 /*
2515  * Temporary quiesce of a flow and associated Rx SRS.
2516  * Please see block comment above mac_rx_srs_quiesce
2517  */
2518 /* ARGSUSED */
2519 int
mac_tx_flow_quiesce(flow_entry_t * flent,void * arg)2520 mac_tx_flow_quiesce(flow_entry_t *flent, void *arg)
2521 {
2522 	/*
2523 	 * The fe_tx_srs is null for a subflow on an interface that is
2524 	 * not plumbed
2525 	 */
2526 	if (flent->fe_tx_srs != NULL)
2527 		mac_tx_srs_quiesce(flent->fe_tx_srs, SRS_QUIESCE);
2528 	return (0);
2529 }
2530 
2531 /* ARGSUSED */
2532 int
mac_tx_flow_restart(flow_entry_t * flent,void * arg)2533 mac_tx_flow_restart(flow_entry_t *flent, void *arg)
2534 {
2535 	/*
2536 	 * The fe_tx_srs is null for a subflow on an interface that is
2537 	 * not plumbed
2538 	 */
2539 	if (flent->fe_tx_srs != NULL)
2540 		mac_tx_srs_restart(flent->fe_tx_srs);
2541 	return (0);
2542 }
2543 
2544 static void
i_mac_tx_client_quiesce(mac_client_handle_t mch,uint_t srs_quiesce_flag)2545 i_mac_tx_client_quiesce(mac_client_handle_t mch, uint_t srs_quiesce_flag)
2546 {
2547 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2548 
2549 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
2550 
2551 	mac_tx_client_block(mcip);
2552 	if (MCIP_TX_SRS(mcip) != NULL) {
2553 		mac_tx_srs_quiesce(MCIP_TX_SRS(mcip), srs_quiesce_flag);
2554 		(void) mac_flow_walk_nolock(mcip->mci_subflow_tab,
2555 		    mac_tx_flow_quiesce, NULL);
2556 	}
2557 }
2558 
2559 void
mac_tx_client_quiesce(mac_client_handle_t mch)2560 mac_tx_client_quiesce(mac_client_handle_t mch)
2561 {
2562 	i_mac_tx_client_quiesce(mch, SRS_QUIESCE);
2563 }
2564 
2565 void
mac_tx_client_condemn(mac_client_handle_t mch)2566 mac_tx_client_condemn(mac_client_handle_t mch)
2567 {
2568 	i_mac_tx_client_quiesce(mch, SRS_CONDEMNED);
2569 }
2570 
2571 void
mac_tx_client_restart(mac_client_handle_t mch)2572 mac_tx_client_restart(mac_client_handle_t mch)
2573 {
2574 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
2575 
2576 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
2577 
2578 	mac_tx_client_unblock(mcip);
2579 	if (MCIP_TX_SRS(mcip) != NULL) {
2580 		mac_tx_srs_restart(MCIP_TX_SRS(mcip));
2581 		(void) mac_flow_walk_nolock(mcip->mci_subflow_tab,
2582 		    mac_tx_flow_restart, NULL);
2583 	}
2584 }
2585 
2586 void
mac_tx_client_flush(mac_client_impl_t * mcip)2587 mac_tx_client_flush(mac_client_impl_t *mcip)
2588 {
2589 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
2590 
2591 	mac_tx_client_quiesce((mac_client_handle_t)mcip);
2592 	mac_tx_client_restart((mac_client_handle_t)mcip);
2593 }
2594 
2595 void
mac_client_quiesce(mac_client_impl_t * mcip)2596 mac_client_quiesce(mac_client_impl_t *mcip)
2597 {
2598 	mac_rx_client_quiesce((mac_client_handle_t)mcip);
2599 	mac_tx_client_quiesce((mac_client_handle_t)mcip);
2600 }
2601 
2602 void
mac_client_restart(mac_client_impl_t * mcip)2603 mac_client_restart(mac_client_impl_t *mcip)
2604 {
2605 	mac_rx_client_restart((mac_client_handle_t)mcip);
2606 	mac_tx_client_restart((mac_client_handle_t)mcip);
2607 }
2608 
2609 /*
2610  * Allocate a minor number.
2611  */
2612 minor_t
mac_minor_hold(boolean_t sleep)2613 mac_minor_hold(boolean_t sleep)
2614 {
2615 	id_t id;
2616 
2617 	/*
2618 	 * Grab a value from the arena.
2619 	 */
2620 	atomic_inc_32(&minor_count);
2621 
2622 	if (sleep)
2623 		return ((uint_t)id_alloc(minor_ids));
2624 
2625 	if ((id = id_alloc_nosleep(minor_ids)) == -1) {
2626 		atomic_dec_32(&minor_count);
2627 		return (0);
2628 	}
2629 
2630 	return ((uint_t)id);
2631 }
2632 
2633 /*
2634  * Release a previously allocated minor number.
2635  */
2636 void
mac_minor_rele(minor_t minor)2637 mac_minor_rele(minor_t minor)
2638 {
2639 	/*
2640 	 * Return the value to the arena.
2641 	 */
2642 	id_free(minor_ids, minor);
2643 	atomic_dec_32(&minor_count);
2644 }
2645 
2646 uint32_t
mac_no_notification(mac_handle_t mh)2647 mac_no_notification(mac_handle_t mh)
2648 {
2649 	mac_impl_t *mip = (mac_impl_t *)mh;
2650 
2651 	return (((mip->mi_state_flags & MIS_LEGACY) != 0) ?
2652 	    mip->mi_capab_legacy.ml_unsup_note : 0);
2653 }
2654 
2655 /*
2656  * Prevent any new opens of this mac in preparation for unregister
2657  */
2658 int
i_mac_disable(mac_impl_t * mip)2659 i_mac_disable(mac_impl_t *mip)
2660 {
2661 	mac_client_impl_t	*mcip;
2662 
2663 	rw_enter(&i_mac_impl_lock, RW_WRITER);
2664 	if (mip->mi_state_flags & MIS_DISABLED) {
2665 		/* Already disabled, return success */
2666 		rw_exit(&i_mac_impl_lock);
2667 		return (0);
2668 	}
2669 	/*
2670 	 * See if there are any other references to this mac_t (e.g., VLAN's).
2671 	 * If so return failure. If all the other checks below pass, then
2672 	 * set mi_disabled atomically under the i_mac_impl_lock to prevent
2673 	 * any new VLAN's from being created or new mac client opens of this
2674 	 * mac end point.
2675 	 */
2676 	if (mip->mi_ref > 0) {
2677 		rw_exit(&i_mac_impl_lock);
2678 		return (EBUSY);
2679 	}
2680 
2681 	/*
2682 	 * mac clients must delete all multicast groups they join before
2683 	 * closing. bcast groups are reference counted, the last client
2684 	 * to delete the group will wait till the group is physically
2685 	 * deleted. Since all clients have closed this mac end point
2686 	 * mi_bcast_ngrps must be zero at this point
2687 	 */
2688 	ASSERT(mip->mi_bcast_ngrps == 0);
2689 
2690 	/*
2691 	 * Don't let go of this if it has some flows.
2692 	 * All other code guarantees no flows are added to a disabled
2693 	 * mac, therefore it is sufficient to check for the flow table
2694 	 * only here.
2695 	 */
2696 	mcip = mac_primary_client_handle(mip);
2697 	if ((mcip != NULL) && mac_link_has_flows((mac_client_handle_t)mcip)) {
2698 		rw_exit(&i_mac_impl_lock);
2699 		return (ENOTEMPTY);
2700 	}
2701 
2702 	mip->mi_state_flags |= MIS_DISABLED;
2703 	rw_exit(&i_mac_impl_lock);
2704 	return (0);
2705 }
2706 
2707 int
mac_disable_nowait(mac_handle_t mh)2708 mac_disable_nowait(mac_handle_t mh)
2709 {
2710 	mac_impl_t	*mip = (mac_impl_t *)mh;
2711 	int err;
2712 
2713 	if ((err = i_mac_perim_enter_nowait(mip)) != 0)
2714 		return (err);
2715 	err = i_mac_disable(mip);
2716 	i_mac_perim_exit(mip);
2717 	return (err);
2718 }
2719 
2720 int
mac_disable(mac_handle_t mh)2721 mac_disable(mac_handle_t mh)
2722 {
2723 	mac_impl_t	*mip = (mac_impl_t *)mh;
2724 	int err;
2725 
2726 	i_mac_perim_enter(mip);
2727 	err = i_mac_disable(mip);
2728 	i_mac_perim_exit(mip);
2729 
2730 	/*
2731 	 * Clean up notification thread and wait for it to exit.
2732 	 */
2733 	if (err == 0)
2734 		i_mac_notify_exit(mip);
2735 
2736 	return (err);
2737 }
2738 
2739 /*
2740  * Called when the MAC instance has a non empty flow table, to de-multiplex
2741  * incoming packets to the right flow.
2742  */
2743 /* ARGSUSED */
2744 static mblk_t *
mac_rx_classify(mac_impl_t * mip,mac_resource_handle_t mrh,mblk_t * mp)2745 mac_rx_classify(mac_impl_t *mip, mac_resource_handle_t mrh, mblk_t *mp)
2746 {
2747 	flow_entry_t	*flent = NULL;
2748 	uint_t		flags = FLOW_INBOUND;
2749 	int		err;
2750 
2751 	err = mac_flow_lookup(mip->mi_flow_tab, mp, flags, &flent);
2752 	if (err != 0) {
2753 		/* no registered receive function */
2754 		return (mp);
2755 	} else {
2756 		mac_client_impl_t	*mcip;
2757 
2758 		/*
2759 		 * This flent might just be an additional one on the MAC client,
2760 		 * i.e. for classification purposes (different fdesc), however
2761 		 * the resources, SRS et. al., are in the mci_flent, so if
2762 		 * this isn't the mci_flent, we need to get it.
2763 		 */
2764 		if ((mcip = flent->fe_mcip) != NULL &&
2765 		    mcip->mci_flent != flent) {
2766 			FLOW_REFRELE(flent);
2767 			flent = mcip->mci_flent;
2768 			FLOW_TRY_REFHOLD(flent, err);
2769 			if (err != 0)
2770 				return (mp);
2771 		}
2772 		(flent->fe_cb_fn)(flent->fe_cb_arg1, flent->fe_cb_arg2, mp,
2773 		    B_FALSE);
2774 		FLOW_REFRELE(flent);
2775 	}
2776 	return (NULL);
2777 }
2778 
2779 mblk_t *
mac_rx_flow(mac_handle_t mh,mac_resource_handle_t mrh,mblk_t * mp_chain)2780 mac_rx_flow(mac_handle_t mh, mac_resource_handle_t mrh, mblk_t *mp_chain)
2781 {
2782 	mac_impl_t	*mip = (mac_impl_t *)mh;
2783 	mblk_t		*bp, *bp1, **bpp, *list = NULL;
2784 
2785 	/*
2786 	 * We walk the chain and attempt to classify each packet.
2787 	 * The packets that couldn't be classified will be returned
2788 	 * back to the caller.
2789 	 */
2790 	bp = mp_chain;
2791 	bpp = &list;
2792 	while (bp != NULL) {
2793 		bp1 = bp;
2794 		bp = bp->b_next;
2795 		bp1->b_next = NULL;
2796 
2797 		if (mac_rx_classify(mip, mrh, bp1) != NULL) {
2798 			*bpp = bp1;
2799 			bpp = &bp1->b_next;
2800 		}
2801 	}
2802 	return (list);
2803 }
2804 
2805 static int
mac_tx_flow_srs_wakeup(flow_entry_t * flent,void * arg)2806 mac_tx_flow_srs_wakeup(flow_entry_t *flent, void *arg)
2807 {
2808 	mac_ring_handle_t ring = arg;
2809 
2810 	if (flent->fe_tx_srs)
2811 		mac_tx_srs_wakeup(flent->fe_tx_srs, ring);
2812 	return (0);
2813 }
2814 
2815 void
i_mac_tx_srs_notify(mac_impl_t * mip,mac_ring_handle_t ring)2816 i_mac_tx_srs_notify(mac_impl_t *mip, mac_ring_handle_t ring)
2817 {
2818 	mac_client_impl_t	*cclient;
2819 	mac_soft_ring_set_t	*mac_srs;
2820 
2821 	/*
2822 	 * After grabbing the mi_rw_lock, the list of clients can't change.
2823 	 * If there are any clients mi_disabled must be B_FALSE and can't
2824 	 * get set since there are clients. If there aren't any clients we
2825 	 * don't do anything. In any case the mip has to be valid. The driver
2826 	 * must make sure that it goes single threaded (with respect to mac
2827 	 * calls) and wait for all pending mac calls to finish before calling
2828 	 * mac_unregister.
2829 	 */
2830 	rw_enter(&i_mac_impl_lock, RW_READER);
2831 	if (mip->mi_state_flags & MIS_DISABLED) {
2832 		rw_exit(&i_mac_impl_lock);
2833 		return;
2834 	}
2835 
2836 	/*
2837 	 * Get MAC tx srs from walking mac_client_handle list.
2838 	 */
2839 	rw_enter(&mip->mi_rw_lock, RW_READER);
2840 	for (cclient = mip->mi_clients_list; cclient != NULL;
2841 	    cclient = cclient->mci_client_next) {
2842 		if ((mac_srs = MCIP_TX_SRS(cclient)) != NULL) {
2843 			mac_tx_srs_wakeup(mac_srs, ring);
2844 		} else {
2845 			/*
2846 			 * Aggr opens underlying ports in exclusive mode
2847 			 * and registers flow control callbacks using
2848 			 * mac_tx_client_notify(). When opened in
2849 			 * exclusive mode, Tx SRS won't be created
2850 			 * during mac_unicast_add().
2851 			 */
2852 			if (cclient->mci_state_flags & MCIS_EXCLUSIVE) {
2853 				mac_tx_invoke_callbacks(cclient,
2854 				    (mac_tx_cookie_t)ring);
2855 			}
2856 		}
2857 		(void) mac_flow_walk(cclient->mci_subflow_tab,
2858 		    mac_tx_flow_srs_wakeup, ring);
2859 	}
2860 	rw_exit(&mip->mi_rw_lock);
2861 	rw_exit(&i_mac_impl_lock);
2862 }
2863 
2864 /* ARGSUSED */
2865 void
mac_multicast_refresh(mac_handle_t mh,mac_multicst_t refresh,void * arg,boolean_t add)2866 mac_multicast_refresh(mac_handle_t mh, mac_multicst_t refresh, void *arg,
2867     boolean_t add)
2868 {
2869 	mac_impl_t *mip = (mac_impl_t *)mh;
2870 
2871 	i_mac_perim_enter((mac_impl_t *)mh);
2872 	/*
2873 	 * If no specific refresh function was given then default to the
2874 	 * driver's m_multicst entry point.
2875 	 */
2876 	if (refresh == NULL) {
2877 		refresh = mip->mi_multicst;
2878 		arg = mip->mi_driver;
2879 	}
2880 
2881 	mac_bcast_refresh(mip, refresh, arg, add);
2882 	i_mac_perim_exit((mac_impl_t *)mh);
2883 }
2884 
2885 void
mac_promisc_refresh(mac_handle_t mh,mac_setpromisc_t refresh,void * arg)2886 mac_promisc_refresh(mac_handle_t mh, mac_setpromisc_t refresh, void *arg)
2887 {
2888 	mac_impl_t	*mip = (mac_impl_t *)mh;
2889 
2890 	/*
2891 	 * If no specific refresh function was given then default to the
2892 	 * driver's m_promisc entry point.
2893 	 */
2894 	if (refresh == NULL) {
2895 		refresh = mip->mi_setpromisc;
2896 		arg = mip->mi_driver;
2897 	}
2898 	ASSERT(refresh != NULL);
2899 
2900 	/*
2901 	 * Call the refresh function with the current promiscuity.
2902 	 */
2903 	refresh(arg, (mip->mi_devpromisc != 0));
2904 }
2905 
2906 /*
2907  * The mac client requests that the mac not to change its margin size to
2908  * be less than the specified value.  If "current" is B_TRUE, then the client
2909  * requests the mac not to change its margin size to be smaller than the
2910  * current size. Further, return the current margin size value in this case.
2911  *
2912  * We keep every requested size in an ordered list from largest to smallest.
2913  */
2914 int
mac_margin_add(mac_handle_t mh,uint32_t * marginp,boolean_t current)2915 mac_margin_add(mac_handle_t mh, uint32_t *marginp, boolean_t current)
2916 {
2917 	mac_impl_t		*mip = (mac_impl_t *)mh;
2918 	mac_margin_req_t	**pp, *p;
2919 	int			err = 0;
2920 
2921 	rw_enter(&(mip->mi_rw_lock), RW_WRITER);
2922 	if (current)
2923 		*marginp = mip->mi_margin;
2924 
2925 	/*
2926 	 * If the current margin value cannot satisfy the margin requested,
2927 	 * return ENOTSUP directly.
2928 	 */
2929 	if (*marginp > mip->mi_margin) {
2930 		err = ENOTSUP;
2931 		goto done;
2932 	}
2933 
2934 	/*
2935 	 * Check whether the given margin is already in the list. If so,
2936 	 * bump the reference count.
2937 	 */
2938 	for (pp = &mip->mi_mmrp; (p = *pp) != NULL; pp = &p->mmr_nextp) {
2939 		if (p->mmr_margin == *marginp) {
2940 			/*
2941 			 * The margin requested is already in the list,
2942 			 * so just bump the reference count.
2943 			 */
2944 			p->mmr_ref++;
2945 			goto done;
2946 		}
2947 		if (p->mmr_margin < *marginp)
2948 			break;
2949 	}
2950 
2951 
2952 	p = kmem_zalloc(sizeof (mac_margin_req_t), KM_SLEEP);
2953 	p->mmr_margin = *marginp;
2954 	p->mmr_ref++;
2955 	p->mmr_nextp = *pp;
2956 	*pp = p;
2957 
2958 done:
2959 	rw_exit(&(mip->mi_rw_lock));
2960 	return (err);
2961 }
2962 
2963 /*
2964  * The mac client requests to cancel its previous mac_margin_add() request.
2965  * We remove the requested margin size from the list.
2966  */
2967 int
mac_margin_remove(mac_handle_t mh,uint32_t margin)2968 mac_margin_remove(mac_handle_t mh, uint32_t margin)
2969 {
2970 	mac_impl_t		*mip = (mac_impl_t *)mh;
2971 	mac_margin_req_t	**pp, *p;
2972 	int			err = 0;
2973 
2974 	rw_enter(&(mip->mi_rw_lock), RW_WRITER);
2975 	/*
2976 	 * Find the entry in the list for the given margin.
2977 	 */
2978 	for (pp = &(mip->mi_mmrp); (p = *pp) != NULL; pp = &(p->mmr_nextp)) {
2979 		if (p->mmr_margin == margin) {
2980 			if (--p->mmr_ref == 0)
2981 				break;
2982 
2983 			/*
2984 			 * There is still a reference to this address so
2985 			 * there's nothing more to do.
2986 			 */
2987 			goto done;
2988 		}
2989 	}
2990 
2991 	/*
2992 	 * We did not find an entry for the given margin.
2993 	 */
2994 	if (p == NULL) {
2995 		err = ENOENT;
2996 		goto done;
2997 	}
2998 
2999 	ASSERT(p->mmr_ref == 0);
3000 
3001 	/*
3002 	 * Remove it from the list.
3003 	 */
3004 	*pp = p->mmr_nextp;
3005 	kmem_free(p, sizeof (mac_margin_req_t));
3006 done:
3007 	rw_exit(&(mip->mi_rw_lock));
3008 	return (err);
3009 }
3010 
3011 boolean_t
mac_margin_update(mac_handle_t mh,uint32_t margin)3012 mac_margin_update(mac_handle_t mh, uint32_t margin)
3013 {
3014 	mac_impl_t	*mip = (mac_impl_t *)mh;
3015 	uint32_t	margin_needed = 0;
3016 
3017 	rw_enter(&(mip->mi_rw_lock), RW_WRITER);
3018 
3019 	if (mip->mi_mmrp != NULL)
3020 		margin_needed = mip->mi_mmrp->mmr_margin;
3021 
3022 	if (margin_needed <= margin)
3023 		mip->mi_margin = margin;
3024 
3025 	rw_exit(&(mip->mi_rw_lock));
3026 
3027 	if (margin_needed <= margin)
3028 		i_mac_notify(mip, MAC_NOTE_MARGIN);
3029 
3030 	return (margin_needed <= margin);
3031 }
3032 
3033 /*
3034  * MAC clients use this interface to request that a MAC device not change its
3035  * MTU below the specified amount. At this time, that amount must be within the
3036  * range of the device's current minimum and the device's current maximum. eg. a
3037  * client cannot request a 3000 byte MTU when the device's MTU is currently
3038  * 2000.
3039  *
3040  * If "current" is set to B_TRUE, then the request is to simply to reserve the
3041  * current underlying mac's maximum for this mac client and return it in mtup.
3042  */
3043 int
mac_mtu_add(mac_handle_t mh,uint32_t * mtup,boolean_t current)3044 mac_mtu_add(mac_handle_t mh, uint32_t *mtup, boolean_t current)
3045 {
3046 	mac_impl_t		*mip = (mac_impl_t *)mh;
3047 	mac_mtu_req_t		*prev, *cur;
3048 	mac_propval_range_t	mpr;
3049 	int			err;
3050 
3051 	i_mac_perim_enter(mip);
3052 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
3053 
3054 	if (current == B_TRUE)
3055 		*mtup = mip->mi_sdu_max;
3056 	mpr.mpr_count = 1;
3057 	err = mac_prop_info(mh, MAC_PROP_MTU, "mtu", NULL, 0, &mpr, NULL);
3058 	if (err != 0) {
3059 		rw_exit(&mip->mi_rw_lock);
3060 		i_mac_perim_exit(mip);
3061 		return (err);
3062 	}
3063 
3064 	if (*mtup > mip->mi_sdu_max ||
3065 	    *mtup < mpr.mpr_range_uint32[0].mpur_min) {
3066 		rw_exit(&mip->mi_rw_lock);
3067 		i_mac_perim_exit(mip);
3068 		return (ENOTSUP);
3069 	}
3070 
3071 	prev = NULL;
3072 	for (cur = mip->mi_mtrp; cur != NULL; cur = cur->mtr_nextp) {
3073 		if (*mtup == cur->mtr_mtu) {
3074 			cur->mtr_ref++;
3075 			rw_exit(&mip->mi_rw_lock);
3076 			i_mac_perim_exit(mip);
3077 			return (0);
3078 		}
3079 
3080 		if (*mtup > cur->mtr_mtu)
3081 			break;
3082 
3083 		prev = cur;
3084 	}
3085 
3086 	cur = kmem_alloc(sizeof (mac_mtu_req_t), KM_SLEEP);
3087 	cur->mtr_mtu = *mtup;
3088 	cur->mtr_ref = 1;
3089 	if (prev != NULL) {
3090 		cur->mtr_nextp = prev->mtr_nextp;
3091 		prev->mtr_nextp = cur;
3092 	} else {
3093 		cur->mtr_nextp = mip->mi_mtrp;
3094 		mip->mi_mtrp = cur;
3095 	}
3096 
3097 	rw_exit(&mip->mi_rw_lock);
3098 	i_mac_perim_exit(mip);
3099 	return (0);
3100 }
3101 
3102 int
mac_mtu_remove(mac_handle_t mh,uint32_t mtu)3103 mac_mtu_remove(mac_handle_t mh, uint32_t mtu)
3104 {
3105 	mac_impl_t *mip = (mac_impl_t *)mh;
3106 	mac_mtu_req_t *cur, *prev;
3107 
3108 	i_mac_perim_enter(mip);
3109 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
3110 
3111 	prev = NULL;
3112 	for (cur = mip->mi_mtrp; cur != NULL; cur = cur->mtr_nextp) {
3113 		if (cur->mtr_mtu == mtu) {
3114 			ASSERT(cur->mtr_ref > 0);
3115 			cur->mtr_ref--;
3116 			if (cur->mtr_ref == 0) {
3117 				if (prev == NULL) {
3118 					mip->mi_mtrp = cur->mtr_nextp;
3119 				} else {
3120 					prev->mtr_nextp = cur->mtr_nextp;
3121 				}
3122 				kmem_free(cur, sizeof (mac_mtu_req_t));
3123 			}
3124 			rw_exit(&mip->mi_rw_lock);
3125 			i_mac_perim_exit(mip);
3126 			return (0);
3127 		}
3128 
3129 		prev = cur;
3130 	}
3131 
3132 	rw_exit(&mip->mi_rw_lock);
3133 	i_mac_perim_exit(mip);
3134 	return (ENOENT);
3135 }
3136 
3137 /*
3138  * MAC Type Plugin functions.
3139  */
3140 
3141 mactype_t *
mactype_getplugin(const char * pname)3142 mactype_getplugin(const char *pname)
3143 {
3144 	mactype_t	*mtype = NULL;
3145 	boolean_t	tried_modload = B_FALSE;
3146 
3147 	mutex_enter(&i_mactype_lock);
3148 
3149 find_registered_mactype:
3150 	if (mod_hash_find(i_mactype_hash, (mod_hash_key_t)pname,
3151 	    (mod_hash_val_t *)&mtype) != 0) {
3152 		if (!tried_modload) {
3153 			/*
3154 			 * If the plugin has not yet been loaded, then
3155 			 * attempt to load it now.  If modload() succeeds,
3156 			 * the plugin should have registered using
3157 			 * mactype_register(), in which case we can go back
3158 			 * and attempt to find it again.
3159 			 */
3160 			if (modload(MACTYPE_KMODDIR, (char *)pname) != -1) {
3161 				tried_modload = B_TRUE;
3162 				goto find_registered_mactype;
3163 			}
3164 		}
3165 	} else {
3166 		/*
3167 		 * Note that there's no danger that the plugin we've loaded
3168 		 * could be unloaded between the modload() step and the
3169 		 * reference count bump here, as we're holding
3170 		 * i_mactype_lock, which mactype_unregister() also holds.
3171 		 */
3172 		atomic_inc_32(&mtype->mt_ref);
3173 	}
3174 
3175 	mutex_exit(&i_mactype_lock);
3176 	return (mtype);
3177 }
3178 
3179 mactype_register_t *
mactype_alloc(uint_t mactype_version)3180 mactype_alloc(uint_t mactype_version)
3181 {
3182 	mactype_register_t *mtrp;
3183 
3184 	/*
3185 	 * Make sure there isn't a version mismatch between the plugin and
3186 	 * the framework.  In the future, if multiple versions are
3187 	 * supported, this check could become more sophisticated.
3188 	 */
3189 	if (mactype_version != MACTYPE_VERSION)
3190 		return (NULL);
3191 
3192 	mtrp = kmem_zalloc(sizeof (mactype_register_t), KM_SLEEP);
3193 	mtrp->mtr_version = mactype_version;
3194 	return (mtrp);
3195 }
3196 
3197 void
mactype_free(mactype_register_t * mtrp)3198 mactype_free(mactype_register_t *mtrp)
3199 {
3200 	kmem_free(mtrp, sizeof (mactype_register_t));
3201 }
3202 
3203 int
mactype_register(mactype_register_t * mtrp)3204 mactype_register(mactype_register_t *mtrp)
3205 {
3206 	mactype_t	*mtp;
3207 	mactype_ops_t	*ops = mtrp->mtr_ops;
3208 
3209 	/* Do some sanity checking before we register this MAC type. */
3210 	if (mtrp->mtr_ident == NULL || ops == NULL)
3211 		return (EINVAL);
3212 
3213 	/*
3214 	 * Verify that all mandatory callbacks are set in the ops
3215 	 * vector.
3216 	 */
3217 	if (ops->mtops_unicst_verify == NULL ||
3218 	    ops->mtops_multicst_verify == NULL ||
3219 	    ops->mtops_sap_verify == NULL ||
3220 	    ops->mtops_header == NULL ||
3221 	    ops->mtops_header_info == NULL) {
3222 		return (EINVAL);
3223 	}
3224 
3225 	mtp = kmem_zalloc(sizeof (*mtp), KM_SLEEP);
3226 	mtp->mt_ident = mtrp->mtr_ident;
3227 	mtp->mt_ops = *ops;
3228 	mtp->mt_type = mtrp->mtr_mactype;
3229 	mtp->mt_nativetype = mtrp->mtr_nativetype;
3230 	mtp->mt_addr_length = mtrp->mtr_addrlen;
3231 	if (mtrp->mtr_brdcst_addr != NULL) {
3232 		mtp->mt_brdcst_addr = kmem_alloc(mtrp->mtr_addrlen, KM_SLEEP);
3233 		bcopy(mtrp->mtr_brdcst_addr, mtp->mt_brdcst_addr,
3234 		    mtrp->mtr_addrlen);
3235 	}
3236 
3237 	mtp->mt_stats = mtrp->mtr_stats;
3238 	mtp->mt_statcount = mtrp->mtr_statcount;
3239 
3240 	mtp->mt_mapping = mtrp->mtr_mapping;
3241 	mtp->mt_mappingcount = mtrp->mtr_mappingcount;
3242 
3243 	if (mod_hash_insert(i_mactype_hash,
3244 	    (mod_hash_key_t)mtp->mt_ident, (mod_hash_val_t)mtp) != 0) {
3245 		kmem_free(mtp->mt_brdcst_addr, mtp->mt_addr_length);
3246 		kmem_free(mtp, sizeof (*mtp));
3247 		return (EEXIST);
3248 	}
3249 	return (0);
3250 }
3251 
3252 int
mactype_unregister(const char * ident)3253 mactype_unregister(const char *ident)
3254 {
3255 	mactype_t	*mtp;
3256 	mod_hash_val_t	val;
3257 	int		err;
3258 
3259 	/*
3260 	 * Let's not allow MAC drivers to use this plugin while we're
3261 	 * trying to unregister it.  Holding i_mactype_lock also prevents a
3262 	 * plugin from unregistering while a MAC driver is attempting to
3263 	 * hold a reference to it in i_mactype_getplugin().
3264 	 */
3265 	mutex_enter(&i_mactype_lock);
3266 
3267 	if ((err = mod_hash_find(i_mactype_hash, (mod_hash_key_t)ident,
3268 	    (mod_hash_val_t *)&mtp)) != 0) {
3269 		/* A plugin is trying to unregister, but it never registered. */
3270 		err = ENXIO;
3271 		goto done;
3272 	}
3273 
3274 	if (mtp->mt_ref != 0) {
3275 		err = EBUSY;
3276 		goto done;
3277 	}
3278 
3279 	err = mod_hash_remove(i_mactype_hash, (mod_hash_key_t)ident, &val);
3280 	ASSERT(err == 0);
3281 	if (err != 0) {
3282 		/* This should never happen, thus the ASSERT() above. */
3283 		err = EINVAL;
3284 		goto done;
3285 	}
3286 	ASSERT(mtp == (mactype_t *)val);
3287 
3288 	if (mtp->mt_brdcst_addr != NULL)
3289 		kmem_free(mtp->mt_brdcst_addr, mtp->mt_addr_length);
3290 	kmem_free(mtp, sizeof (mactype_t));
3291 done:
3292 	mutex_exit(&i_mactype_lock);
3293 	return (err);
3294 }
3295 
3296 /*
3297  * Checks the size of the value size specified for a property as
3298  * part of a property operation. Returns B_TRUE if the size is
3299  * correct, B_FALSE otherwise.
3300  */
3301 boolean_t
mac_prop_check_size(mac_prop_id_t id,uint_t valsize,boolean_t is_range)3302 mac_prop_check_size(mac_prop_id_t id, uint_t valsize, boolean_t is_range)
3303 {
3304 	uint_t minsize = 0;
3305 
3306 	if (is_range)
3307 		return (valsize >= sizeof (mac_propval_range_t));
3308 
3309 	switch (id) {
3310 	case MAC_PROP_ZONE:
3311 		minsize = sizeof (dld_ioc_zid_t);
3312 		break;
3313 	case MAC_PROP_AUTOPUSH:
3314 		if (valsize != 0)
3315 			minsize = sizeof (struct dlautopush);
3316 		break;
3317 	case MAC_PROP_TAGMODE:
3318 		minsize = sizeof (link_tagmode_t);
3319 		break;
3320 	case MAC_PROP_RESOURCE:
3321 	case MAC_PROP_RESOURCE_EFF:
3322 		minsize = sizeof (mac_resource_props_t);
3323 		break;
3324 	case MAC_PROP_DUPLEX:
3325 		minsize = sizeof (link_duplex_t);
3326 		break;
3327 	case MAC_PROP_SPEED:
3328 		minsize = sizeof (uint64_t);
3329 		break;
3330 	case MAC_PROP_STATUS:
3331 		minsize = sizeof (link_state_t);
3332 		break;
3333 	case MAC_PROP_AUTONEG:
3334 	case MAC_PROP_EN_AUTONEG:
3335 		minsize = sizeof (uint8_t);
3336 		break;
3337 	case MAC_PROP_MTU:
3338 	case MAC_PROP_LLIMIT:
3339 	case MAC_PROP_LDECAY:
3340 		minsize = sizeof (uint32_t);
3341 		break;
3342 	case MAC_PROP_FLOWCTRL:
3343 		minsize = sizeof (link_flowctrl_t);
3344 		break;
3345 	case MAC_PROP_ADV_FEC_CAP:
3346 	case MAC_PROP_EN_FEC_CAP:
3347 		minsize = sizeof (link_fec_t);
3348 		break;
3349 	case MAC_PROP_ADV_5000FDX_CAP:
3350 	case MAC_PROP_EN_5000FDX_CAP:
3351 	case MAC_PROP_ADV_2500FDX_CAP:
3352 	case MAC_PROP_EN_2500FDX_CAP:
3353 	case MAC_PROP_ADV_100GFDX_CAP:
3354 	case MAC_PROP_EN_100GFDX_CAP:
3355 	case MAC_PROP_ADV_50GFDX_CAP:
3356 	case MAC_PROP_EN_50GFDX_CAP:
3357 	case MAC_PROP_ADV_40GFDX_CAP:
3358 	case MAC_PROP_EN_40GFDX_CAP:
3359 	case MAC_PROP_ADV_25GFDX_CAP:
3360 	case MAC_PROP_EN_25GFDX_CAP:
3361 	case MAC_PROP_ADV_10GFDX_CAP:
3362 	case MAC_PROP_EN_10GFDX_CAP:
3363 	case MAC_PROP_ADV_1000HDX_CAP:
3364 	case MAC_PROP_EN_1000HDX_CAP:
3365 	case MAC_PROP_ADV_100FDX_CAP:
3366 	case MAC_PROP_EN_100FDX_CAP:
3367 	case MAC_PROP_ADV_100HDX_CAP:
3368 	case MAC_PROP_EN_100HDX_CAP:
3369 	case MAC_PROP_ADV_10FDX_CAP:
3370 	case MAC_PROP_EN_10FDX_CAP:
3371 	case MAC_PROP_ADV_10HDX_CAP:
3372 	case MAC_PROP_EN_10HDX_CAP:
3373 	case MAC_PROP_ADV_100T4_CAP:
3374 	case MAC_PROP_EN_100T4_CAP:
3375 		minsize = sizeof (uint8_t);
3376 		break;
3377 	case MAC_PROP_PVID:
3378 		minsize = sizeof (uint16_t);
3379 		break;
3380 	case MAC_PROP_IPTUN_HOPLIMIT:
3381 		minsize = sizeof (uint32_t);
3382 		break;
3383 	case MAC_PROP_IPTUN_ENCAPLIMIT:
3384 		minsize = sizeof (uint32_t);
3385 		break;
3386 	case MAC_PROP_MAX_TX_RINGS_AVAIL:
3387 	case MAC_PROP_MAX_RX_RINGS_AVAIL:
3388 	case MAC_PROP_MAX_RXHWCLNT_AVAIL:
3389 	case MAC_PROP_MAX_TXHWCLNT_AVAIL:
3390 		minsize = sizeof (uint_t);
3391 		break;
3392 	case MAC_PROP_WL_ESSID:
3393 		minsize = sizeof (wl_linkstatus_t);
3394 		break;
3395 	case MAC_PROP_WL_BSSID:
3396 		minsize = sizeof (wl_bssid_t);
3397 		break;
3398 	case MAC_PROP_WL_BSSTYPE:
3399 		minsize = sizeof (wl_bss_type_t);
3400 		break;
3401 	case MAC_PROP_WL_LINKSTATUS:
3402 		minsize = sizeof (wl_linkstatus_t);
3403 		break;
3404 	case MAC_PROP_WL_DESIRED_RATES:
3405 		minsize = sizeof (wl_rates_t);
3406 		break;
3407 	case MAC_PROP_WL_SUPPORTED_RATES:
3408 		minsize = sizeof (wl_rates_t);
3409 		break;
3410 	case MAC_PROP_WL_AUTH_MODE:
3411 		minsize = sizeof (wl_authmode_t);
3412 		break;
3413 	case MAC_PROP_WL_ENCRYPTION:
3414 		minsize = sizeof (wl_encryption_t);
3415 		break;
3416 	case MAC_PROP_WL_RSSI:
3417 		minsize = sizeof (wl_rssi_t);
3418 		break;
3419 	case MAC_PROP_WL_PHY_CONFIG:
3420 		minsize = sizeof (wl_phy_conf_t);
3421 		break;
3422 	case MAC_PROP_WL_CAPABILITY:
3423 		minsize = sizeof (wl_capability_t);
3424 		break;
3425 	case MAC_PROP_WL_WPA:
3426 		minsize = sizeof (wl_wpa_t);
3427 		break;
3428 	case MAC_PROP_WL_SCANRESULTS:
3429 		minsize = sizeof (wl_wpa_ess_t);
3430 		break;
3431 	case MAC_PROP_WL_POWER_MODE:
3432 		minsize = sizeof (wl_ps_mode_t);
3433 		break;
3434 	case MAC_PROP_WL_RADIO:
3435 		minsize = sizeof (wl_radio_t);
3436 		break;
3437 	case MAC_PROP_WL_ESS_LIST:
3438 		minsize = sizeof (wl_ess_list_t);
3439 		break;
3440 	case MAC_PROP_WL_KEY_TAB:
3441 		minsize = sizeof (wl_wep_key_tab_t);
3442 		break;
3443 	case MAC_PROP_WL_CREATE_IBSS:
3444 		minsize = sizeof (wl_create_ibss_t);
3445 		break;
3446 	case MAC_PROP_WL_SETOPTIE:
3447 		minsize = sizeof (wl_wpa_ie_t);
3448 		break;
3449 	case MAC_PROP_WL_DELKEY:
3450 		minsize = sizeof (wl_del_key_t);
3451 		break;
3452 	case MAC_PROP_WL_KEY:
3453 		minsize = sizeof (wl_key_t);
3454 		break;
3455 	case MAC_PROP_WL_MLME:
3456 		minsize = sizeof (wl_mlme_t);
3457 		break;
3458 	case MAC_PROP_VN_PROMISC_FILTERED:
3459 		minsize = sizeof (boolean_t);
3460 		break;
3461 	}
3462 
3463 	return (valsize >= minsize);
3464 }
3465 
3466 /*
3467  * mac_set_prop() sets MAC or hardware driver properties:
3468  *
3469  * - MAC-managed properties such as resource properties include maxbw,
3470  *   priority, and cpu binding list, as well as the default port VID
3471  *   used by bridging. These properties are consumed by the MAC layer
3472  *   itself and not passed down to the driver. For resource control
3473  *   properties, this function invokes mac_set_resources() which will
3474  *   cache the property value in mac_impl_t and may call
3475  *   mac_client_set_resource() to update property value of the primary
3476  *   mac client, if it exists.
3477  *
3478  * - Properties which act on the hardware and must be passed to the
3479  *   driver, such as MTU, through the driver's mc_setprop() entry point.
3480  */
3481 int
mac_set_prop(mac_handle_t mh,mac_prop_id_t id,char * name,void * val,uint_t valsize)3482 mac_set_prop(mac_handle_t mh, mac_prop_id_t id, char *name, void *val,
3483     uint_t valsize)
3484 {
3485 	int err = ENOTSUP;
3486 	mac_impl_t *mip = (mac_impl_t *)mh;
3487 
3488 	ASSERT(MAC_PERIM_HELD(mh));
3489 
3490 	switch (id) {
3491 	case MAC_PROP_RESOURCE: {
3492 		mac_resource_props_t *mrp;
3493 
3494 		/* call mac_set_resources() for MAC properties */
3495 		ASSERT(valsize >= sizeof (mac_resource_props_t));
3496 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
3497 		bcopy(val, mrp, sizeof (*mrp));
3498 		err = mac_set_resources(mh, mrp);
3499 		kmem_free(mrp, sizeof (*mrp));
3500 		break;
3501 	}
3502 
3503 	case MAC_PROP_PVID:
3504 		ASSERT(valsize >= sizeof (uint16_t));
3505 		if (mip->mi_state_flags & MIS_IS_VNIC)
3506 			return (EINVAL);
3507 		err = mac_set_pvid(mh, *(uint16_t *)val);
3508 		break;
3509 
3510 	case MAC_PROP_MTU: {
3511 		uint32_t mtu;
3512 
3513 		ASSERT(valsize >= sizeof (uint32_t));
3514 		bcopy(val, &mtu, sizeof (mtu));
3515 		err = mac_set_mtu(mh, mtu, NULL);
3516 		break;
3517 	}
3518 
3519 	case MAC_PROP_LLIMIT:
3520 	case MAC_PROP_LDECAY: {
3521 		uint32_t learnval;
3522 
3523 		if (valsize < sizeof (learnval) ||
3524 		    (mip->mi_state_flags & MIS_IS_VNIC))
3525 			return (EINVAL);
3526 		bcopy(val, &learnval, sizeof (learnval));
3527 		if (learnval == 0 && id == MAC_PROP_LDECAY)
3528 			return (EINVAL);
3529 		if (id == MAC_PROP_LLIMIT)
3530 			mip->mi_llimit = learnval;
3531 		else
3532 			mip->mi_ldecay = learnval;
3533 		err = 0;
3534 		break;
3535 	}
3536 
3537 	case MAC_PROP_ADV_FEC_CAP:
3538 	case MAC_PROP_EN_FEC_CAP: {
3539 		link_fec_t fec;
3540 
3541 		ASSERT(valsize >= sizeof (link_fec_t));
3542 
3543 		/*
3544 		 * fec cannot be zero, and auto must be set exclusively.
3545 		 */
3546 		bcopy(val, &fec, sizeof (link_fec_t));
3547 		if (fec == 0)
3548 			return (EINVAL);
3549 		if ((fec & LINK_FEC_AUTO) != 0 && (fec & ~LINK_FEC_AUTO) != 0)
3550 			return (EINVAL);
3551 
3552 		if (mip->mi_callbacks->mc_callbacks & MC_SETPROP) {
3553 			err = mip->mi_callbacks->mc_setprop(mip->mi_driver,
3554 			    name, id, valsize, val);
3555 		}
3556 		break;
3557 	}
3558 
3559 	default:
3560 		/* For other driver properties, call driver's callback */
3561 		if (mip->mi_callbacks->mc_callbacks & MC_SETPROP) {
3562 			err = mip->mi_callbacks->mc_setprop(mip->mi_driver,
3563 			    name, id, valsize, val);
3564 		}
3565 	}
3566 	return (err);
3567 }
3568 
3569 /*
3570  * mac_get_prop() gets MAC or device driver properties.
3571  *
3572  * If the property is a driver property, mac_get_prop() calls driver's callback
3573  * entry point to get it.
3574  * If the property is a MAC property, mac_get_prop() invokes mac_get_resources()
3575  * which returns the cached value in mac_impl_t.
3576  */
3577 int
mac_get_prop(mac_handle_t mh,mac_prop_id_t id,char * name,void * val,uint_t valsize)3578 mac_get_prop(mac_handle_t mh, mac_prop_id_t id, char *name, void *val,
3579     uint_t valsize)
3580 {
3581 	int err = ENOTSUP;
3582 	mac_impl_t *mip = (mac_impl_t *)mh;
3583 	uint_t	rings;
3584 	uint_t	vlinks;
3585 
3586 	bzero(val, valsize);
3587 
3588 	switch (id) {
3589 	case MAC_PROP_RESOURCE: {
3590 		mac_resource_props_t *mrp;
3591 
3592 		/* If mac property, read from cache */
3593 		ASSERT(valsize >= sizeof (mac_resource_props_t));
3594 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
3595 		mac_get_resources(mh, mrp);
3596 		bcopy(mrp, val, sizeof (*mrp));
3597 		kmem_free(mrp, sizeof (*mrp));
3598 		return (0);
3599 	}
3600 	case MAC_PROP_RESOURCE_EFF: {
3601 		mac_resource_props_t *mrp;
3602 
3603 		/* If mac effective property, read from client */
3604 		ASSERT(valsize >= sizeof (mac_resource_props_t));
3605 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
3606 		mac_get_effective_resources(mh, mrp);
3607 		bcopy(mrp, val, sizeof (*mrp));
3608 		kmem_free(mrp, sizeof (*mrp));
3609 		return (0);
3610 	}
3611 
3612 	case MAC_PROP_PVID:
3613 		ASSERT(valsize >= sizeof (uint16_t));
3614 		if (mip->mi_state_flags & MIS_IS_VNIC)
3615 			return (EINVAL);
3616 		*(uint16_t *)val = mac_get_pvid(mh);
3617 		return (0);
3618 
3619 	case MAC_PROP_LLIMIT:
3620 	case MAC_PROP_LDECAY:
3621 		ASSERT(valsize >= sizeof (uint32_t));
3622 		if (mip->mi_state_flags & MIS_IS_VNIC)
3623 			return (EINVAL);
3624 		if (id == MAC_PROP_LLIMIT)
3625 			bcopy(&mip->mi_llimit, val, sizeof (mip->mi_llimit));
3626 		else
3627 			bcopy(&mip->mi_ldecay, val, sizeof (mip->mi_ldecay));
3628 		return (0);
3629 
3630 	case MAC_PROP_MTU: {
3631 		uint32_t sdu;
3632 
3633 		ASSERT(valsize >= sizeof (uint32_t));
3634 		mac_sdu_get2(mh, NULL, &sdu, NULL);
3635 		bcopy(&sdu, val, sizeof (sdu));
3636 
3637 		return (0);
3638 	}
3639 	case MAC_PROP_STATUS: {
3640 		link_state_t link_state;
3641 
3642 		if (valsize < sizeof (link_state))
3643 			return (EINVAL);
3644 		link_state = mac_link_get(mh);
3645 		bcopy(&link_state, val, sizeof (link_state));
3646 
3647 		return (0);
3648 	}
3649 
3650 	case MAC_PROP_MAX_RX_RINGS_AVAIL: