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