xref: /illumos-gate/usr/src/uts/common/io/aggr/aggr_grp.c (revision 07a48826732249fcd3aa8dd53c8389595e9f1fbc)
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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * IEEE 802.3ad Link Aggregation -- Link Aggregation Groups.
28  *
29  * An instance of the structure aggr_grp_t is allocated for each
30  * link aggregation group. When created, aggr_grp_t objects are
31  * entered into the aggr_grp_hash hash table maintained by the modhash
32  * module. The hash key is the linkid associated with the link
33  * aggregation group.
34  *
35  * A set of MAC ports are associated with each association group.
36  */
37 
38 #include <sys/types.h>
39 #include <sys/sysmacros.h>
40 #include <sys/conf.h>
41 #include <sys/cmn_err.h>
42 #include <sys/disp.h>
43 #include <sys/list.h>
44 #include <sys/ksynch.h>
45 #include <sys/kmem.h>
46 #include <sys/stream.h>
47 #include <sys/modctl.h>
48 #include <sys/ddi.h>
49 #include <sys/sunddi.h>
50 #include <sys/atomic.h>
51 #include <sys/stat.h>
52 #include <sys/modhash.h>
53 #include <sys/id_space.h>
54 #include <sys/strsun.h>
55 #include <sys/dlpi.h>
56 #include <sys/mac_provider.h>
57 #include <sys/dls.h>
58 #include <sys/vlan.h>
59 #include <sys/aggr.h>
60 #include <sys/aggr_impl.h>
61 
62 static int aggr_m_start(void *);
63 static void aggr_m_stop(void *);
64 static int aggr_m_promisc(void *, boolean_t);
65 static int aggr_m_multicst(void *, boolean_t, const uint8_t *);
66 static int aggr_m_unicst(void *, const uint8_t *);
67 static int aggr_m_stat(void *, uint_t, uint64_t *);
68 static void aggr_m_ioctl(void *, queue_t *, mblk_t *);
69 static boolean_t aggr_m_capab_get(void *, mac_capab_t, void *);
70 static int aggr_m_setprop(void *, const char *, mac_prop_id_t, uint_t,
71     const void *);
72 static int aggr_m_getprop(void *, const char *, mac_prop_id_t, uint_t,
73     uint_t, void *, uint_t *);
74 
75 
76 static aggr_port_t *aggr_grp_port_lookup(aggr_grp_t *, datalink_id_t);
77 static int aggr_grp_rem_port(aggr_grp_t *, aggr_port_t *, boolean_t *,
78     boolean_t *);
79 
80 static void aggr_grp_capab_set(aggr_grp_t *);
81 static boolean_t aggr_grp_capab_check(aggr_grp_t *, aggr_port_t *);
82 static uint_t aggr_grp_max_sdu(aggr_grp_t *);
83 static uint32_t aggr_grp_max_margin(aggr_grp_t *);
84 static boolean_t aggr_grp_sdu_check(aggr_grp_t *, aggr_port_t *);
85 static boolean_t aggr_grp_margin_check(aggr_grp_t *, aggr_port_t *);
86 
87 static int aggr_add_pseudo_rx_group(aggr_port_t *, aggr_pseudo_rx_group_t *);
88 static void aggr_rem_pseudo_rx_group(aggr_port_t *, aggr_pseudo_rx_group_t *);
89 static int aggr_pseudo_disable_intr(mac_intr_handle_t);
90 static int aggr_pseudo_enable_intr(mac_intr_handle_t);
91 static int aggr_pseudo_start_ring(mac_ring_driver_t, uint64_t);
92 static void aggr_pseudo_stop_ring(mac_ring_driver_t);
93 static int aggr_addmac(void *, const uint8_t *);
94 static int aggr_remmac(void *, const uint8_t *);
95 static mblk_t *aggr_rx_poll(void *, int);
96 static void aggr_fill_ring(void *, mac_ring_type_t, const int,
97     const int, mac_ring_info_t *, mac_ring_handle_t);
98 static void aggr_fill_group(void *, mac_ring_type_t, const int,
99     mac_group_info_t *, mac_group_handle_t);
100 
101 static kmem_cache_t	*aggr_grp_cache;
102 static mod_hash_t	*aggr_grp_hash;
103 static krwlock_t	aggr_grp_lock;
104 static uint_t		aggr_grp_cnt;
105 static id_space_t	*key_ids;
106 
107 #define	GRP_HASHSZ		64
108 #define	GRP_HASH_KEY(linkid)	((mod_hash_key_t)(uintptr_t)linkid)
109 #define	AGGR_PORT_NAME_DELIMIT '-'
110 
111 static uchar_t aggr_zero_mac[] = {0, 0, 0, 0, 0, 0};
112 
113 #define	AGGR_M_CALLBACK_FLAGS	\
114 	(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
115 
116 static mac_callbacks_t aggr_m_callbacks = {
117 	AGGR_M_CALLBACK_FLAGS,
118 	aggr_m_stat,
119 	aggr_m_start,
120 	aggr_m_stop,
121 	aggr_m_promisc,
122 	aggr_m_multicst,
123 	NULL,
124 	aggr_m_tx,
125 	aggr_m_ioctl,
126 	aggr_m_capab_get,
127 	NULL,
128 	NULL,
129 	aggr_m_setprop,
130 	aggr_m_getprop
131 };
132 
133 /*ARGSUSED*/
134 static int
135 aggr_grp_constructor(void *buf, void *arg, int kmflag)
136 {
137 	aggr_grp_t *grp = buf;
138 
139 	bzero(grp, sizeof (*grp));
140 	mutex_init(&grp->lg_lacp_lock, NULL, MUTEX_DEFAULT, NULL);
141 	cv_init(&grp->lg_lacp_cv, NULL, CV_DEFAULT, NULL);
142 	rw_init(&grp->lg_tx_lock, NULL, RW_DRIVER, NULL);
143 	mutex_init(&grp->lg_port_lock, NULL, MUTEX_DEFAULT, NULL);
144 	cv_init(&grp->lg_port_cv, NULL, CV_DEFAULT, NULL);
145 	grp->lg_link_state = LINK_STATE_UNKNOWN;
146 	return (0);
147 }
148 
149 /*ARGSUSED*/
150 static void
151 aggr_grp_destructor(void *buf, void *arg)
152 {
153 	aggr_grp_t *grp = buf;
154 
155 	if (grp->lg_tx_ports != NULL) {
156 		kmem_free(grp->lg_tx_ports,
157 		    grp->lg_tx_ports_size * sizeof (aggr_port_t *));
158 	}
159 
160 	mutex_destroy(&grp->lg_lacp_lock);
161 	cv_destroy(&grp->lg_lacp_cv);
162 	mutex_destroy(&grp->lg_port_lock);
163 	cv_destroy(&grp->lg_port_cv);
164 	rw_destroy(&grp->lg_tx_lock);
165 }
166 
167 void
168 aggr_grp_init(void)
169 {
170 	aggr_grp_cache = kmem_cache_create("aggr_grp_cache",
171 	    sizeof (aggr_grp_t), 0, aggr_grp_constructor,
172 	    aggr_grp_destructor, NULL, NULL, NULL, 0);
173 
174 	aggr_grp_hash = mod_hash_create_idhash("aggr_grp_hash",
175 	    GRP_HASHSZ, mod_hash_null_valdtor);
176 	rw_init(&aggr_grp_lock, NULL, RW_DEFAULT, NULL);
177 	aggr_grp_cnt = 0;
178 
179 	/*
180 	 * Allocate an id space to manage key values (when key is not
181 	 * specified). The range of the id space will be from
182 	 * (AGGR_MAX_KEY + 1) to UINT16_MAX, because the LACP protocol
183 	 * uses a 16-bit key.
184 	 */
185 	key_ids = id_space_create("aggr_key_ids", AGGR_MAX_KEY + 1, UINT16_MAX);
186 	ASSERT(key_ids != NULL);
187 }
188 
189 void
190 aggr_grp_fini(void)
191 {
192 	id_space_destroy(key_ids);
193 	rw_destroy(&aggr_grp_lock);
194 	mod_hash_destroy_idhash(aggr_grp_hash);
195 	kmem_cache_destroy(aggr_grp_cache);
196 }
197 
198 uint_t
199 aggr_grp_count(void)
200 {
201 	uint_t	count;
202 
203 	rw_enter(&aggr_grp_lock, RW_READER);
204 	count = aggr_grp_cnt;
205 	rw_exit(&aggr_grp_lock);
206 	return (count);
207 }
208 
209 /*
210  * Since both aggr_port_notify_cb() and aggr_port_timer_thread() functions
211  * requires the mac perimeter, this function holds a reference of the aggr
212  * and aggr won't call mac_unregister() until this reference drops to 0.
213  */
214 void
215 aggr_grp_port_hold(aggr_port_t *port)
216 {
217 	aggr_grp_t	*grp = port->lp_grp;
218 
219 	AGGR_PORT_REFHOLD(port);
220 	mutex_enter(&grp->lg_port_lock);
221 	grp->lg_port_ref++;
222 	mutex_exit(&grp->lg_port_lock);
223 }
224 
225 /*
226  * Release the reference of the grp and inform aggr_grp_delete() calling
227  * mac_unregister() is now safe.
228  */
229 void
230 aggr_grp_port_rele(aggr_port_t *port)
231 {
232 	aggr_grp_t	*grp = port->lp_grp;
233 
234 	mutex_enter(&grp->lg_port_lock);
235 	if (--grp->lg_port_ref == 0)
236 		cv_signal(&grp->lg_port_cv);
237 	mutex_exit(&grp->lg_port_lock);
238 	AGGR_PORT_REFRELE(port);
239 }
240 
241 /*
242  * Wait for the port's lacp timer thread and the port's notification callback
243  * to exit.
244  */
245 void
246 aggr_grp_port_wait(aggr_grp_t *grp)
247 {
248 	mutex_enter(&grp->lg_port_lock);
249 	if (grp->lg_port_ref != 0)
250 		cv_wait(&grp->lg_port_cv, &grp->lg_port_lock);
251 	mutex_exit(&grp->lg_port_lock);
252 }
253 
254 /*
255  * Attach a port to a link aggregation group.
256  *
257  * A port is attached to a link aggregation group once its speed
258  * and link state have been verified.
259  *
260  * Returns B_TRUE if the group link state or speed has changed. If
261  * it's the case, the caller must notify the MAC layer via a call
262  * to mac_link().
263  */
264 boolean_t
265 aggr_grp_attach_port(aggr_grp_t *grp, aggr_port_t *port)
266 {
267 	boolean_t link_state_changed = B_FALSE;
268 
269 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
270 	ASSERT(MAC_PERIM_HELD(port->lp_mh));
271 
272 	if (port->lp_state == AGGR_PORT_STATE_ATTACHED)
273 		return (B_FALSE);
274 
275 	/*
276 	 * Validate the MAC port link speed and update the group
277 	 * link speed if needed.
278 	 */
279 	if (port->lp_ifspeed == 0 ||
280 	    port->lp_link_state != LINK_STATE_UP ||
281 	    port->lp_link_duplex != LINK_DUPLEX_FULL) {
282 		/*
283 		 * Can't attach a MAC port with unknown link speed,
284 		 * down link, or not in full duplex mode.
285 		 */
286 		return (B_FALSE);
287 	}
288 
289 	if (grp->lg_ifspeed == 0) {
290 		/*
291 		 * The group inherits the speed of the first link being
292 		 * attached.
293 		 */
294 		grp->lg_ifspeed = port->lp_ifspeed;
295 		link_state_changed = B_TRUE;
296 	} else if (grp->lg_ifspeed != port->lp_ifspeed) {
297 		/*
298 		 * The link speed of the MAC port must be the same as
299 		 * the group link speed, as per 802.3ad. Since it is
300 		 * not, the attach is cancelled.
301 		 */
302 		return (B_FALSE);
303 	}
304 
305 	grp->lg_nattached_ports++;
306 
307 	/*
308 	 * Update the group link state.
309 	 */
310 	if (grp->lg_link_state != LINK_STATE_UP) {
311 		grp->lg_link_state = LINK_STATE_UP;
312 		grp->lg_link_duplex = LINK_DUPLEX_FULL;
313 		link_state_changed = B_TRUE;
314 	}
315 
316 	/*
317 	 * Update port's state.
318 	 */
319 	port->lp_state = AGGR_PORT_STATE_ATTACHED;
320 
321 	aggr_grp_multicst_port(port, B_TRUE);
322 
323 	/*
324 	 * Set port's receive callback
325 	 */
326 	mac_rx_set(port->lp_mch, aggr_recv_cb, port);
327 
328 	/*
329 	 * If LACP is OFF, the port can be used to send data as soon
330 	 * as its link is up and verified to be compatible with the
331 	 * aggregation.
332 	 *
333 	 * If LACP is active or passive, notify the LACP subsystem, which
334 	 * will enable sending on the port following the LACP protocol.
335 	 */
336 	if (grp->lg_lacp_mode == AGGR_LACP_OFF)
337 		aggr_send_port_enable(port);
338 	else
339 		aggr_lacp_port_attached(port);
340 
341 	return (link_state_changed);
342 }
343 
344 boolean_t
345 aggr_grp_detach_port(aggr_grp_t *grp, aggr_port_t *port)
346 {
347 	boolean_t link_state_changed = B_FALSE;
348 
349 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
350 	ASSERT(MAC_PERIM_HELD(port->lp_mh));
351 
352 	/* update state */
353 	if (port->lp_state != AGGR_PORT_STATE_ATTACHED)
354 		return (B_FALSE);
355 
356 	mac_rx_clear(port->lp_mch);
357 
358 	aggr_grp_multicst_port(port, B_FALSE);
359 
360 	if (grp->lg_lacp_mode == AGGR_LACP_OFF)
361 		aggr_send_port_disable(port);
362 	else
363 		aggr_lacp_port_detached(port);
364 
365 	port->lp_state = AGGR_PORT_STATE_STANDBY;
366 
367 	grp->lg_nattached_ports--;
368 	if (grp->lg_nattached_ports == 0) {
369 		/* the last attached MAC port of the group is being detached */
370 		grp->lg_ifspeed = 0;
371 		grp->lg_link_state = LINK_STATE_DOWN;
372 		grp->lg_link_duplex = LINK_DUPLEX_UNKNOWN;
373 		link_state_changed = B_TRUE;
374 	}
375 
376 	return (link_state_changed);
377 }
378 
379 /*
380  * Update the MAC addresses of the constituent ports of the specified
381  * group. This function is invoked:
382  * - after creating a new aggregation group.
383  * - after adding new ports to an aggregation group.
384  * - after removing a port from a group when the MAC address of
385  *   that port was used for the MAC address of the group.
386  * - after the MAC address of a port changed when the MAC address
387  *   of that port was used for the MAC address of the group.
388  *
389  * Return true if the link state of the aggregation changed, for example
390  * as a result of a failure changing the MAC address of one of the
391  * constituent ports.
392  */
393 boolean_t
394 aggr_grp_update_ports_mac(aggr_grp_t *grp)
395 {
396 	aggr_port_t *cport;
397 	boolean_t link_state_changed = B_FALSE;
398 	mac_perim_handle_t mph;
399 
400 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
401 
402 	for (cport = grp->lg_ports; cport != NULL;
403 	    cport = cport->lp_next) {
404 		mac_perim_enter_by_mh(cport->lp_mh, &mph);
405 		if (aggr_port_unicst(cport) != 0) {
406 			if (aggr_grp_detach_port(grp, cport))
407 				link_state_changed = B_TRUE;
408 		} else {
409 			/*
410 			 * If a port was detached because of a previous
411 			 * failure changing the MAC address, the port is
412 			 * reattached when it successfully changes the MAC
413 			 * address now, and this might cause the link state
414 			 * of the aggregation to change.
415 			 */
416 			if (aggr_grp_attach_port(grp, cport))
417 				link_state_changed = B_TRUE;
418 		}
419 		mac_perim_exit(mph);
420 	}
421 	return (link_state_changed);
422 }
423 
424 /*
425  * Invoked when the MAC address of a port has changed. If the port's
426  * MAC address was used for the group MAC address, set mac_addr_changedp
427  * to B_TRUE to indicate to the caller that it should send a MAC_NOTE_UNICST
428  * notification. If the link state changes due to detach/attach of
429  * the constituent port, set link_state_changedp to B_TRUE to indicate
430  * to the caller that it should send a MAC_NOTE_LINK notification. In both
431  * cases, it is the responsibility of the caller to invoke notification
432  * functions after releasing the the port lock.
433  */
434 void
435 aggr_grp_port_mac_changed(aggr_grp_t *grp, aggr_port_t *port,
436     boolean_t *mac_addr_changedp, boolean_t *link_state_changedp)
437 {
438 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
439 	ASSERT(MAC_PERIM_HELD(port->lp_mh));
440 	ASSERT(mac_addr_changedp != NULL);
441 	ASSERT(link_state_changedp != NULL);
442 
443 	*mac_addr_changedp = B_FALSE;
444 	*link_state_changedp = B_FALSE;
445 
446 	if (grp->lg_addr_fixed) {
447 		/*
448 		 * The group is using a fixed MAC address or an automatic
449 		 * MAC address has not been set.
450 		 */
451 		return;
452 	}
453 
454 	if (grp->lg_mac_addr_port == port) {
455 		/*
456 		 * The MAC address of the port was assigned to the group
457 		 * MAC address. Update the group MAC address.
458 		 */
459 		bcopy(port->lp_addr, grp->lg_addr, ETHERADDRL);
460 		*mac_addr_changedp = B_TRUE;
461 	} else {
462 		/*
463 		 * Update the actual port MAC address to the MAC address
464 		 * of the group.
465 		 */
466 		if (aggr_port_unicst(port) != 0) {
467 			*link_state_changedp = aggr_grp_detach_port(grp, port);
468 		} else {
469 			/*
470 			 * If a port was detached because of a previous
471 			 * failure changing the MAC address, the port is
472 			 * reattached when it successfully changes the MAC
473 			 * address now, and this might cause the link state
474 			 * of the aggregation to change.
475 			 */
476 			*link_state_changedp = aggr_grp_attach_port(grp, port);
477 		}
478 	}
479 }
480 
481 /*
482  * Add a port to a link aggregation group.
483  */
484 static int
485 aggr_grp_add_port(aggr_grp_t *grp, datalink_id_t port_linkid, boolean_t force,
486     aggr_port_t **pp)
487 {
488 	aggr_port_t *port, **cport;
489 	mac_perim_handle_t mph;
490 	int err;
491 
492 	/*
493 	 * lg_mh could be NULL when the function is called during the creation
494 	 * of the aggregation.
495 	 */
496 	ASSERT(grp->lg_mh == NULL || MAC_PERIM_HELD(grp->lg_mh));
497 
498 	/* create new port */
499 	err = aggr_port_create(grp, port_linkid, force, &port);
500 	if (err != 0)
501 		return (err);
502 
503 	mac_perim_enter_by_mh(port->lp_mh, &mph);
504 
505 	/* add port to list of group constituent ports */
506 	cport = &grp->lg_ports;
507 	while (*cport != NULL)
508 		cport = &((*cport)->lp_next);
509 	*cport = port;
510 
511 	/*
512 	 * Back reference to the group it is member of. A port always
513 	 * holds a reference to its group to ensure that the back
514 	 * reference is always valid.
515 	 */
516 	port->lp_grp = grp;
517 	AGGR_GRP_REFHOLD(grp);
518 	grp->lg_nports++;
519 
520 	aggr_lacp_init_port(port);
521 	mac_perim_exit(mph);
522 
523 	if (pp != NULL)
524 		*pp = port;
525 
526 	return (0);
527 }
528 
529 /*
530  * Add a pseudo Rx ring for the given HW ring handle.
531  */
532 static int
533 aggr_add_pseudo_rx_ring(aggr_port_t *port,
534     aggr_pseudo_rx_group_t *rx_grp, mac_ring_handle_t hw_rh)
535 {
536 	aggr_pseudo_rx_ring_t	*ring;
537 	int			err;
538 	int			j;
539 
540 	for (j = 0; j < MAX_RINGS_PER_GROUP; j++) {
541 		ring = rx_grp->arg_rings + j;
542 		if (!(ring->arr_flags & MAC_PSEUDO_RING_INUSE))
543 			break;
544 	}
545 
546 	/*
547 	 * No slot for this new Rx ring.
548 	 */
549 	if (j == MAX_RINGS_PER_GROUP)
550 		return (EIO);
551 
552 	ring->arr_flags |= MAC_PSEUDO_RING_INUSE;
553 	ring->arr_hw_rh = hw_rh;
554 	ring->arr_port = port;
555 	rx_grp->arg_ring_cnt++;
556 
557 	/*
558 	 * The group is already registered, dynamically add a new ring to the
559 	 * mac group.
560 	 */
561 	mac_hwring_setup(hw_rh, (mac_resource_handle_t)ring);
562 	if ((err = mac_group_add_ring(rx_grp->arg_gh, j)) != 0) {
563 		ring->arr_flags &= ~MAC_PSEUDO_RING_INUSE;
564 		ring->arr_hw_rh = NULL;
565 		ring->arr_port = NULL;
566 		rx_grp->arg_ring_cnt--;
567 		mac_hwring_teardown(hw_rh);
568 	}
569 	return (err);
570 }
571 
572 /*
573  * Remove the pseudo Rx ring of the given HW ring handle.
574  */
575 static void
576 aggr_rem_pseudo_rx_ring(aggr_pseudo_rx_group_t *rx_grp, mac_ring_handle_t hw_rh)
577 {
578 	aggr_pseudo_rx_ring_t	*ring;
579 	int			j;
580 
581 	for (j = 0; j < MAX_RINGS_PER_GROUP; j++) {
582 		ring = rx_grp->arg_rings + j;
583 		if (!(ring->arr_flags & MAC_PSEUDO_RING_INUSE) ||
584 		    ring->arr_hw_rh != hw_rh) {
585 			continue;
586 		}
587 
588 		mac_group_rem_ring(rx_grp->arg_gh, ring->arr_rh);
589 
590 		ring->arr_flags &= ~MAC_PSEUDO_RING_INUSE;
591 		ring->arr_hw_rh = NULL;
592 		ring->arr_port = NULL;
593 		rx_grp->arg_ring_cnt--;
594 		mac_hwring_teardown(hw_rh);
595 		break;
596 	}
597 }
598 
599 /*
600  * This function is called to create pseudo rings over the hardware rings of
601  * the underlying device. Note that there is a 1:1 mapping between the pseudo
602  * RX rings of the aggr and the hardware rings of the underlying port.
603  */
604 static int
605 aggr_add_pseudo_rx_group(aggr_port_t *port, aggr_pseudo_rx_group_t *rx_grp)
606 {
607 	aggr_grp_t		*grp = port->lp_grp;
608 	mac_ring_handle_t	hw_rh[MAX_RINGS_PER_GROUP];
609 	aggr_unicst_addr_t	*addr, *a;
610 	mac_perim_handle_t	pmph;
611 	int			hw_rh_cnt, i = 0, j;
612 	int			err = 0;
613 
614 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
615 	mac_perim_enter_by_mh(port->lp_mh, &pmph);
616 
617 	/*
618 	 * This function must be called after the aggr registers its mac
619 	 * and its RX group has been initialized.
620 	 */
621 	ASSERT(rx_grp->arg_gh != NULL);
622 
623 	/*
624 	 * Get the list the the underlying HW rings.
625 	 */
626 	hw_rh_cnt = mac_hwrings_get(port->lp_mch, &port->lp_hwgh, hw_rh);
627 
628 	if (port->lp_hwgh != NULL) {
629 		/*
630 		 * Quiesce the HW ring and the mac srs on the ring. Note
631 		 * that the HW ring will be restarted when the pseudo ring
632 		 * is started. At that time all the packets will be
633 		 * directly passed up to the pseudo RX ring and handled
634 		 * by mac srs created over the pseudo RX ring.
635 		 */
636 		mac_rx_client_quiesce(port->lp_mch);
637 		mac_srs_perm_quiesce(port->lp_mch, B_TRUE);
638 	}
639 
640 	/*
641 	 * Add all the unicast addresses to the newly added port.
642 	 */
643 	for (addr = rx_grp->arg_macaddr; addr != NULL; addr = addr->aua_next) {
644 		if ((err = aggr_port_addmac(port, addr->aua_addr)) != 0)
645 			break;
646 	}
647 
648 	for (i = 0; err == 0 && i < hw_rh_cnt; i++)
649 		err = aggr_add_pseudo_rx_ring(port, rx_grp, hw_rh[i]);
650 
651 	if (err != 0) {
652 		for (j = 0; j < i; j++)
653 			aggr_rem_pseudo_rx_ring(rx_grp, hw_rh[j]);
654 
655 		for (a = rx_grp->arg_macaddr; a != addr; a = a->aua_next)
656 			aggr_port_remmac(port, a->aua_addr);
657 
658 		if (port->lp_hwgh != NULL) {
659 			mac_srs_perm_quiesce(port->lp_mch, B_FALSE);
660 			mac_rx_client_restart(port->lp_mch);
661 			port->lp_hwgh = NULL;
662 		}
663 	} else {
664 		port->lp_grp_added = B_TRUE;
665 	}
666 done:
667 	mac_perim_exit(pmph);
668 	return (err);
669 }
670 
671 /*
672  * This function is called by aggr to remove pseudo RX rings over the
673  * HW rings of the underlying port.
674  */
675 static void
676 aggr_rem_pseudo_rx_group(aggr_port_t *port, aggr_pseudo_rx_group_t *rx_grp)
677 {
678 	aggr_grp_t		*grp = port->lp_grp;
679 	mac_ring_handle_t	hw_rh[MAX_RINGS_PER_GROUP];
680 	aggr_unicst_addr_t	*addr;
681 	mac_group_handle_t	hwgh;
682 	mac_perim_handle_t	pmph;
683 	int			hw_rh_cnt, i;
684 
685 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
686 	mac_perim_enter_by_mh(port->lp_mh, &pmph);
687 
688 	if (!port->lp_grp_added)
689 		goto done;
690 
691 	ASSERT(rx_grp->arg_gh != NULL);
692 	hw_rh_cnt = mac_hwrings_get(port->lp_mch, &hwgh, hw_rh);
693 
694 	/*
695 	 * If hw_rh_cnt is 0, it means that the underlying port does not
696 	 * support RX rings. Directly return in this case.
697 	 */
698 	for (i = 0; i < hw_rh_cnt; i++)
699 		aggr_rem_pseudo_rx_ring(rx_grp, hw_rh[i]);
700 
701 	for (addr = rx_grp->arg_macaddr; addr != NULL; addr = addr->aua_next)
702 		aggr_port_remmac(port, addr->aua_addr);
703 
704 	if (port->lp_hwgh != NULL) {
705 		port->lp_hwgh = NULL;
706 
707 		/*
708 		 * First clear the permanent-quiesced flag of the RX srs then
709 		 * restart the HW ring and the mac srs on the ring. Note that
710 		 * the HW ring and associated SRS will soon been removed when
711 		 * the port is removed from the aggr.
712 		 */
713 		mac_srs_perm_quiesce(port->lp_mch, B_FALSE);
714 		mac_rx_client_restart(port->lp_mch);
715 	}
716 
717 	port->lp_grp_added = B_FALSE;
718 done:
719 	mac_perim_exit(pmph);
720 }
721 
722 static int
723 aggr_pseudo_disable_intr(mac_intr_handle_t ih)
724 {
725 	aggr_pseudo_rx_ring_t *rr_ring = (aggr_pseudo_rx_ring_t *)ih;
726 	return (mac_hwring_disable_intr(rr_ring->arr_hw_rh));
727 }
728 
729 static int
730 aggr_pseudo_enable_intr(mac_intr_handle_t ih)
731 {
732 	aggr_pseudo_rx_ring_t *rr_ring = (aggr_pseudo_rx_ring_t *)ih;
733 	return (mac_hwring_enable_intr(rr_ring->arr_hw_rh));
734 }
735 
736 static int
737 aggr_pseudo_start_ring(mac_ring_driver_t arg, uint64_t mr_gen)
738 {
739 	aggr_pseudo_rx_ring_t *rr_ring = (aggr_pseudo_rx_ring_t *)arg;
740 	int err;
741 
742 	err = mac_hwring_start(rr_ring->arr_hw_rh);
743 	if (err == 0)
744 		rr_ring->arr_gen = mr_gen;
745 	return (err);
746 }
747 
748 static void
749 aggr_pseudo_stop_ring(mac_ring_driver_t arg)
750 {
751 	aggr_pseudo_rx_ring_t *rr_ring = (aggr_pseudo_rx_ring_t *)arg;
752 	mac_hwring_stop(rr_ring->arr_hw_rh);
753 }
754 
755 /*
756  * Add one or more ports to an existing link aggregation group.
757  */
758 int
759 aggr_grp_add_ports(datalink_id_t linkid, uint_t nports, boolean_t force,
760     laioc_port_t *ports)
761 {
762 	int rc, i, nadded = 0;
763 	aggr_grp_t *grp = NULL;
764 	aggr_port_t *port;
765 	boolean_t link_state_changed = B_FALSE;
766 	mac_perim_handle_t mph, pmph;
767 
768 	/* get group corresponding to linkid */
769 	rw_enter(&aggr_grp_lock, RW_READER);
770 	if (mod_hash_find(aggr_grp_hash, GRP_HASH_KEY(linkid),
771 	    (mod_hash_val_t *)&grp) != 0) {
772 		rw_exit(&aggr_grp_lock);
773 		return (ENOENT);
774 	}
775 	AGGR_GRP_REFHOLD(grp);
776 
777 	/*
778 	 * Hold the perimeter so that the aggregation won't be destroyed.
779 	 */
780 	mac_perim_enter_by_mh(grp->lg_mh, &mph);
781 	rw_exit(&aggr_grp_lock);
782 
783 	/* add the specified ports to group */
784 	for (i = 0; i < nports; i++) {
785 		/* add port to group */
786 		if ((rc = aggr_grp_add_port(grp, ports[i].lp_linkid,
787 		    force, &port)) != 0) {
788 			goto bail;
789 		}
790 		ASSERT(port != NULL);
791 		nadded++;
792 
793 		/* check capabilities */
794 		if (!aggr_grp_capab_check(grp, port) ||
795 		    !aggr_grp_sdu_check(grp, port) ||
796 		    !aggr_grp_margin_check(grp, port)) {
797 			rc = ENOTSUP;
798 			goto bail;
799 		}
800 
801 		/*
802 		 * Create the pseudo ring for each HW ring of the underlying
803 		 * port.
804 		 */
805 		rc = aggr_add_pseudo_rx_group(port, &grp->lg_rx_group);
806 		if (rc != 0)
807 			goto bail;
808 
809 		mac_perim_enter_by_mh(port->lp_mh, &pmph);
810 
811 		/* set LACP mode */
812 		aggr_port_lacp_set_mode(grp, port);
813 
814 		/* start port if group has already been started */
815 		if (grp->lg_started) {
816 			rc = aggr_port_start(port);
817 			if (rc != 0) {
818 				mac_perim_exit(pmph);
819 				goto bail;
820 			}
821 
822 			/*
823 			 * Turn on the promiscuous mode over the port when it
824 			 * is requested to be turned on to receive the
825 			 * non-primary address over a port, or the promiscous
826 			 * mode is enabled over the aggr.
827 			 */
828 			if (grp->lg_promisc || port->lp_prom_addr != NULL) {
829 				rc = aggr_port_promisc(port, B_TRUE);
830 				if (rc != 0) {
831 					mac_perim_exit(pmph);
832 					goto bail;
833 				}
834 			}
835 		}
836 		mac_perim_exit(pmph);
837 
838 		/*
839 		 * Attach each port if necessary.
840 		 */
841 		if (aggr_port_notify_link(grp, port))
842 			link_state_changed = B_TRUE;
843 
844 		/*
845 		 * Initialize the callback functions for this port.
846 		 */
847 		aggr_port_init_callbacks(port);
848 	}
849 
850 	/* update the MAC address of the constituent ports */
851 	if (aggr_grp_update_ports_mac(grp))
852 		link_state_changed = B_TRUE;
853 
854 	if (link_state_changed)
855 		mac_link_update(grp->lg_mh, grp->lg_link_state);
856 
857 bail:
858 	if (rc != 0) {
859 		/* stop and remove ports that have been added */
860 		for (i = 0; i < nadded; i++) {
861 			port = aggr_grp_port_lookup(grp, ports[i].lp_linkid);
862 			ASSERT(port != NULL);
863 			if (grp->lg_started) {
864 				mac_perim_enter_by_mh(port->lp_mh, &pmph);
865 				(void) aggr_port_promisc(port, B_FALSE);
866 				aggr_port_stop(port);
867 				mac_perim_exit(pmph);
868 			}
869 			aggr_rem_pseudo_rx_group(port, &grp->lg_rx_group);
870 			(void) aggr_grp_rem_port(grp, port, NULL, NULL);
871 		}
872 	}
873 
874 	mac_perim_exit(mph);
875 	AGGR_GRP_REFRELE(grp);
876 	return (rc);
877 }
878 
879 static int
880 aggr_grp_modify_common(aggr_grp_t *grp, uint8_t update_mask, uint32_t policy,
881     boolean_t mac_fixed, const uchar_t *mac_addr, aggr_lacp_mode_t lacp_mode,
882     aggr_lacp_timer_t lacp_timer)
883 {
884 	boolean_t mac_addr_changed = B_FALSE;
885 	boolean_t link_state_changed = B_FALSE;
886 	mac_perim_handle_t pmph;
887 
888 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
889 
890 	/* validate fixed address if specified */
891 	if ((update_mask & AGGR_MODIFY_MAC) && mac_fixed &&
892 	    ((bcmp(aggr_zero_mac, mac_addr, ETHERADDRL) == 0) ||
893 	    (mac_addr[0] & 0x01))) {
894 		return (EINVAL);
895 	}
896 
897 	/* update policy if requested */
898 	if (update_mask & AGGR_MODIFY_POLICY)
899 		aggr_send_update_policy(grp, policy);
900 
901 	/* update unicast MAC address if requested */
902 	if (update_mask & AGGR_MODIFY_MAC) {
903 		if (mac_fixed) {
904 			/* user-supplied MAC address */
905 			grp->lg_mac_addr_port = NULL;
906 			if (bcmp(mac_addr, grp->lg_addr, ETHERADDRL) != 0) {
907 				bcopy(mac_addr, grp->lg_addr, ETHERADDRL);
908 				mac_addr_changed = B_TRUE;
909 			}
910 		} else if (grp->lg_addr_fixed) {
911 			/* switch from user-supplied to automatic */
912 			aggr_port_t *port = grp->lg_ports;
913 
914 			mac_perim_enter_by_mh(port->lp_mh, &pmph);
915 			bcopy(port->lp_addr, grp->lg_addr, ETHERADDRL);
916 			grp->lg_mac_addr_port = port;
917 			mac_addr_changed = B_TRUE;
918 			mac_perim_exit(pmph);
919 		}
920 		grp->lg_addr_fixed = mac_fixed;
921 	}
922 
923 	if (mac_addr_changed)
924 		link_state_changed = aggr_grp_update_ports_mac(grp);
925 
926 	if (update_mask & AGGR_MODIFY_LACP_MODE)
927 		aggr_lacp_update_mode(grp, lacp_mode);
928 
929 	if (update_mask & AGGR_MODIFY_LACP_TIMER)
930 		aggr_lacp_update_timer(grp, lacp_timer);
931 
932 	if (link_state_changed)
933 		mac_link_update(grp->lg_mh, grp->lg_link_state);
934 
935 	if (mac_addr_changed)
936 		mac_unicst_update(grp->lg_mh, grp->lg_addr);
937 
938 	return (0);
939 }
940 
941 /*
942  * Update properties of an existing link aggregation group.
943  */
944 int
945 aggr_grp_modify(datalink_id_t linkid, uint8_t update_mask, uint32_t policy,
946     boolean_t mac_fixed, const uchar_t *mac_addr, aggr_lacp_mode_t lacp_mode,
947     aggr_lacp_timer_t lacp_timer)
948 {
949 	aggr_grp_t *grp = NULL;
950 	mac_perim_handle_t mph;
951 	int err;
952 
953 	/* get group corresponding to linkid */
954 	rw_enter(&aggr_grp_lock, RW_READER);
955 	if (mod_hash_find(aggr_grp_hash, GRP_HASH_KEY(linkid),
956 	    (mod_hash_val_t *)&grp) != 0) {
957 		rw_exit(&aggr_grp_lock);
958 		return (ENOENT);
959 	}
960 	AGGR_GRP_REFHOLD(grp);
961 
962 	/*
963 	 * Hold the perimeter so that the aggregation won't be destroyed.
964 	 */
965 	mac_perim_enter_by_mh(grp->lg_mh, &mph);
966 	rw_exit(&aggr_grp_lock);
967 
968 	err = aggr_grp_modify_common(grp, update_mask, policy, mac_fixed,
969 	    mac_addr, lacp_mode, lacp_timer);
970 
971 	mac_perim_exit(mph);
972 	AGGR_GRP_REFRELE(grp);
973 	return (err);
974 }
975 
976 /*
977  * Create a new link aggregation group upon request from administrator.
978  * Returns 0 on success, an errno on failure.
979  */
980 int
981 aggr_grp_create(datalink_id_t linkid, uint32_t key, uint_t nports,
982     laioc_port_t *ports, uint32_t policy, boolean_t mac_fixed, boolean_t force,
983     uchar_t *mac_addr, aggr_lacp_mode_t lacp_mode, aggr_lacp_timer_t lacp_timer)
984 {
985 	aggr_grp_t *grp = NULL;
986 	aggr_port_t *port;
987 	mac_register_t *mac;
988 	boolean_t link_state_changed;
989 	mac_perim_handle_t mph;
990 	int err;
991 	int i;
992 
993 	/* need at least one port */
994 	if (nports == 0)
995 		return (EINVAL);
996 
997 	rw_enter(&aggr_grp_lock, RW_WRITER);
998 
999 	/* does a group with the same linkid already exist? */
1000 	err = mod_hash_find(aggr_grp_hash, GRP_HASH_KEY(linkid),
1001 	    (mod_hash_val_t *)&grp);
1002 	if (err == 0) {
1003 		rw_exit(&aggr_grp_lock);
1004 		return (EEXIST);
1005 	}
1006 
1007 	grp = kmem_cache_alloc(aggr_grp_cache, KM_SLEEP);
1008 
1009 	grp->lg_refs = 1;
1010 	grp->lg_closing = B_FALSE;
1011 	grp->lg_force = force;
1012 	grp->lg_linkid = linkid;
1013 	grp->lg_ifspeed = 0;
1014 	grp->lg_link_state = LINK_STATE_UNKNOWN;
1015 	grp->lg_link_duplex = LINK_DUPLEX_UNKNOWN;
1016 	grp->lg_started = B_FALSE;
1017 	grp->lg_promisc = B_FALSE;
1018 	grp->lg_lacp_done = B_FALSE;
1019 	grp->lg_lacp_head = grp->lg_lacp_tail = NULL;
1020 	grp->lg_lacp_rx_thread = thread_create(NULL, 0,
1021 	    aggr_lacp_rx_thread, grp, 0, &p0, TS_RUN, minclsyspri);
1022 	bzero(&grp->lg_rx_group, sizeof (aggr_pseudo_rx_group_t));
1023 	aggr_lacp_init_grp(grp);
1024 
1025 	/* add MAC ports to group */
1026 	grp->lg_ports = NULL;
1027 	grp->lg_nports = 0;
1028 	grp->lg_nattached_ports = 0;
1029 	grp->lg_ntx_ports = 0;
1030 
1031 	/*
1032 	 * If key is not specified by the user, allocate the key.
1033 	 */
1034 	if ((key == 0) && ((key = (uint32_t)id_alloc(key_ids)) == 0)) {
1035 		err = ENOMEM;
1036 		goto bail;
1037 	}
1038 	grp->lg_key = key;
1039 
1040 	for (i = 0; i < nports; i++) {
1041 		err = aggr_grp_add_port(grp, ports[i].lp_linkid, force, NULL);
1042 		if (err != 0)
1043 			goto bail;
1044 	}
1045 
1046 	/*
1047 	 * If no explicit MAC address was specified by the administrator,
1048 	 * set it to the MAC address of the first port.
1049 	 */
1050 	grp->lg_addr_fixed = mac_fixed;
1051 	if (grp->lg_addr_fixed) {
1052 		/* validate specified address */
1053 		if (bcmp(aggr_zero_mac, mac_addr, ETHERADDRL) == 0) {
1054 			err = EINVAL;
1055 			goto bail;
1056 		}
1057 		bcopy(mac_addr, grp->lg_addr, ETHERADDRL);
1058 	} else {
1059 		bcopy(grp->lg_ports->lp_addr, grp->lg_addr, ETHERADDRL);
1060 		grp->lg_mac_addr_port = grp->lg_ports;
1061 	}
1062 
1063 	/* set the initial group capabilities */
1064 	aggr_grp_capab_set(grp);
1065 
1066 	if ((mac = mac_alloc(MAC_VERSION)) == NULL) {
1067 		err = ENOMEM;
1068 		goto bail;
1069 	}
1070 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
1071 	mac->m_driver = grp;
1072 	mac->m_dip = aggr_dip;
1073 	mac->m_instance = grp->lg_key > AGGR_MAX_KEY ? (uint_t)-1 : grp->lg_key;
1074 	mac->m_src_addr = grp->lg_addr;
1075 	mac->m_callbacks = &aggr_m_callbacks;
1076 	mac->m_min_sdu = 0;
1077 	mac->m_max_sdu = grp->lg_max_sdu = aggr_grp_max_sdu(grp);
1078 	mac->m_margin = aggr_grp_max_margin(grp);
1079 	mac->m_v12n = MAC_VIRT_LEVEL1;
1080 	err = mac_register(mac, &grp->lg_mh);
1081 	mac_free(mac);
1082 	if (err != 0)
1083 		goto bail;
1084 
1085 	if ((err = dls_devnet_create(grp->lg_mh, grp->lg_linkid)) != 0) {
1086 		(void) mac_unregister(grp->lg_mh);
1087 		grp->lg_mh = NULL;
1088 		goto bail;
1089 	}
1090 
1091 	mac_perim_enter_by_mh(grp->lg_mh, &mph);
1092 
1093 	/*
1094 	 * Update the MAC address of the constituent ports.
1095 	 * None of the port is attached at this time, the link state of the
1096 	 * aggregation will not change.
1097 	 */
1098 	link_state_changed = aggr_grp_update_ports_mac(grp);
1099 	ASSERT(!link_state_changed);
1100 
1101 	/* update outbound load balancing policy */
1102 	aggr_send_update_policy(grp, policy);
1103 
1104 	/* set LACP mode */
1105 	aggr_lacp_set_mode(grp, lacp_mode, lacp_timer);
1106 
1107 	/*
1108 	 * Attach each port if necessary.
1109 	 */
1110 	for (port = grp->lg_ports; port != NULL; port = port->lp_next) {
1111 		/*
1112 		 * Create the pseudo ring for each HW ring of the underlying
1113 		 * port. Note that this is done after the aggr registers the
1114 		 * mac.
1115 		 */
1116 		VERIFY(aggr_add_pseudo_rx_group(port, &grp->lg_rx_group) == 0);
1117 		if (aggr_port_notify_link(grp, port))
1118 			link_state_changed = B_TRUE;
1119 
1120 		/*
1121 		 * Initialize the callback functions for this port.
1122 		 */
1123 		aggr_port_init_callbacks(port);
1124 	}
1125 
1126 	if (link_state_changed)
1127 		mac_link_update(grp->lg_mh, grp->lg_link_state);
1128 
1129 	/* add new group to hash table */
1130 	err = mod_hash_insert(aggr_grp_hash, GRP_HASH_KEY(linkid),
1131 	    (mod_hash_val_t)grp);
1132 	ASSERT(err == 0);
1133 	aggr_grp_cnt++;
1134 
1135 	mac_perim_exit(mph);
1136 	rw_exit(&aggr_grp_lock);
1137 	return (0);
1138 
1139 bail:
1140 
1141 	grp->lg_closing = B_TRUE;
1142 
1143 	port = grp->lg_ports;
1144 	while (port != NULL) {
1145 		aggr_port_t *cport;
1146 
1147 		cport = port->lp_next;
1148 		aggr_port_delete(port);
1149 		port = cport;
1150 	}
1151 
1152 	/*
1153 	 * Inform the lacp_rx thread to exit.
1154 	 */
1155 	mutex_enter(&grp->lg_lacp_lock);
1156 	grp->lg_lacp_done = B_TRUE;
1157 	cv_signal(&grp->lg_lacp_cv);
1158 	while (grp->lg_lacp_rx_thread != NULL)
1159 		cv_wait(&grp->lg_lacp_cv, &grp->lg_lacp_lock);
1160 	mutex_exit(&grp->lg_lacp_lock);
1161 
1162 	rw_exit(&aggr_grp_lock);
1163 	AGGR_GRP_REFRELE(grp);
1164 	return (err);
1165 }
1166 
1167 /*
1168  * Return a pointer to the member of a group with specified linkid.
1169  */
1170 static aggr_port_t *
1171 aggr_grp_port_lookup(aggr_grp_t *grp, datalink_id_t linkid)
1172 {
1173 	aggr_port_t *port;
1174 
1175 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
1176 
1177 	for (port = grp->lg_ports; port != NULL; port = port->lp_next) {
1178 		if (port->lp_linkid == linkid)
1179 			break;
1180 	}
1181 
1182 	return (port);
1183 }
1184 
1185 /*
1186  * Stop, detach and remove a port from a link aggregation group.
1187  */
1188 static int
1189 aggr_grp_rem_port(aggr_grp_t *grp, aggr_port_t *port,
1190     boolean_t *mac_addr_changedp, boolean_t *link_state_changedp)
1191 {
1192 	int rc = 0;
1193 	aggr_port_t **pport;
1194 	boolean_t mac_addr_changed = B_FALSE;
1195 	boolean_t link_state_changed = B_FALSE;
1196 	mac_perim_handle_t mph;
1197 	uint64_t val;
1198 	uint_t i;
1199 	uint_t stat;
1200 
1201 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
1202 	ASSERT(grp->lg_nports > 1);
1203 	ASSERT(!grp->lg_closing);
1204 
1205 	/* unlink port */
1206 	for (pport = &grp->lg_ports; *pport != port;
1207 	    pport = &(*pport)->lp_next) {
1208 		if (*pport == NULL) {
1209 			rc = ENOENT;
1210 			goto done;
1211 		}
1212 	}
1213 	*pport = port->lp_next;
1214 
1215 	mac_perim_enter_by_mh(port->lp_mh, &mph);
1216 
1217 	/*
1218 	 * If the MAC address of the port being removed was assigned
1219 	 * to the group, update the group MAC address
1220 	 * using the MAC address of a different port.
1221 	 */
1222 	if (!grp->lg_addr_fixed && grp->lg_mac_addr_port == port) {
1223 		/*
1224 		 * Set the MAC address of the group to the
1225 		 * MAC address of its first port.
1226 		 */
1227 		bcopy(grp->lg_ports->lp_addr, grp->lg_addr, ETHERADDRL);
1228 		grp->lg_mac_addr_port = grp->lg_ports;
1229 		mac_addr_changed = B_TRUE;
1230 	}
1231 
1232 	link_state_changed = aggr_grp_detach_port(grp, port);
1233 
1234 	/*
1235 	 * Add the counter statistics of the ports while it was aggregated
1236 	 * to the group's residual statistics.  This is done by obtaining
1237 	 * the current counter from the underlying MAC then subtracting the
1238 	 * value of the counter at the moment it was added to the
1239 	 * aggregation.
1240 	 */
1241 	for (i = 0; i < MAC_NSTAT; i++) {
1242 		stat = i + MAC_STAT_MIN;
1243 		if (!MAC_STAT_ISACOUNTER(stat))
1244 			continue;
1245 		val = aggr_port_stat(port, stat);
1246 		val -= port->lp_stat[i];
1247 		grp->lg_stat[i] += val;
1248 	}
1249 	for (i = 0; i < ETHER_NSTAT; i++) {
1250 		stat = i + MACTYPE_STAT_MIN;
1251 		if (!ETHER_STAT_ISACOUNTER(stat))
1252 			continue;
1253 		val = aggr_port_stat(port, stat);
1254 		val -= port->lp_ether_stat[i];
1255 		grp->lg_ether_stat[i] += val;
1256 	}
1257 
1258 	grp->lg_nports--;
1259 	mac_perim_exit(mph);
1260 
1261 	aggr_port_delete(port);
1262 
1263 	/*
1264 	 * If the group MAC address has changed, update the MAC address of
1265 	 * the remaining constituent ports according to the new MAC
1266 	 * address of the group.
1267 	 */
1268 	if (mac_addr_changed && aggr_grp_update_ports_mac(grp))
1269 		link_state_changed = B_TRUE;
1270 
1271 done:
1272 	if (mac_addr_changedp != NULL)
1273 		*mac_addr_changedp = mac_addr_changed;
1274 	if (link_state_changedp != NULL)
1275 		*link_state_changedp = link_state_changed;
1276 
1277 	return (rc);
1278 }
1279 
1280 /*
1281  * Remove one or more ports from an existing link aggregation group.
1282  */
1283 int
1284 aggr_grp_rem_ports(datalink_id_t linkid, uint_t nports, laioc_port_t *ports)
1285 {
1286 	int rc = 0, i;
1287 	aggr_grp_t *grp = NULL;
1288 	aggr_port_t *port;
1289 	boolean_t mac_addr_update = B_FALSE, mac_addr_changed;
1290 	boolean_t link_state_update = B_FALSE, link_state_changed;
1291 	mac_perim_handle_t mph, pmph;
1292 
1293 	/* get group corresponding to linkid */
1294 	rw_enter(&aggr_grp_lock, RW_READER);
1295 	if (mod_hash_find(aggr_grp_hash, GRP_HASH_KEY(linkid),
1296 	    (mod_hash_val_t *)&grp) != 0) {
1297 		rw_exit(&aggr_grp_lock);
1298 		return (ENOENT);
1299 	}
1300 	AGGR_GRP_REFHOLD(grp);
1301 
1302 	/*
1303 	 * Hold the perimeter so that the aggregation won't be destroyed.
1304 	 */
1305 	mac_perim_enter_by_mh(grp->lg_mh, &mph);
1306 	rw_exit(&aggr_grp_lock);
1307 
1308 	/* we need to keep at least one port per group */
1309 	if (nports >= grp->lg_nports) {
1310 		rc = EINVAL;
1311 		goto bail;
1312 	}
1313 
1314 	/* first verify that all the groups are valid */
1315 	for (i = 0; i < nports; i++) {
1316 		if (aggr_grp_port_lookup(grp, ports[i].lp_linkid) == NULL) {
1317 			/* port not found */
1318 			rc = ENOENT;
1319 			goto bail;
1320 		}
1321 	}
1322 
1323 	/* clear the promiscous mode for the specified ports */
1324 	for (i = 0; i < nports && rc == 0; i++) {
1325 		/* lookup port */
1326 		port = aggr_grp_port_lookup(grp, ports[i].lp_linkid);
1327 		ASSERT(port != NULL);
1328 
1329 		mac_perim_enter_by_mh(port->lp_mh, &pmph);
1330 		rc = aggr_port_promisc(port, B_FALSE);
1331 		mac_perim_exit(pmph);
1332 	}
1333 	if (rc != 0) {
1334 		for (i = 0; i < nports; i++) {
1335 			port = aggr_grp_port_lookup(grp,
1336 			    ports[i].lp_linkid);
1337 			ASSERT(port != NULL);
1338 
1339 			/*
1340 			 * Turn the promiscuous mode back on if it is required
1341 			 * to receive the non-primary address over a port, or
1342 			 * the promiscous mode is enabled over the aggr.
1343 			 */
1344 			mac_perim_enter_by_mh(port->lp_mh, &pmph);
1345 			if (port->lp_started && (grp->lg_promisc ||
1346 			    port->lp_prom_addr != NULL)) {
1347 				(void) aggr_port_promisc(port, B_TRUE);
1348 			}
1349 			mac_perim_exit(pmph);
1350 		}
1351 		goto bail;
1352 	}
1353 
1354 	/* remove the specified ports from group */
1355 	for (i = 0; i < nports; i++) {
1356 		/* lookup port */
1357 		port = aggr_grp_port_lookup(grp, ports[i].lp_linkid);
1358 		ASSERT(port != NULL);
1359 
1360 		/* stop port if group has already been started */
1361 		if (grp->lg_started) {
1362 			mac_perim_enter_by_mh(port->lp_mh, &pmph);
1363 			aggr_port_stop(port);
1364 			mac_perim_exit(pmph);
1365 		}
1366 
1367 		aggr_rem_pseudo_rx_group(port, &grp->lg_rx_group);
1368 		/* remove port from group */
1369 		rc = aggr_grp_rem_port(grp, port, &mac_addr_changed,
1370 		    &link_state_changed);
1371 		ASSERT(rc == 0);
1372 		mac_addr_update = mac_addr_update || mac_addr_changed;
1373 		link_state_update = link_state_update || link_state_changed;
1374 	}
1375 
1376 bail:
1377 	if (mac_addr_update)
1378 		mac_unicst_update(grp->lg_mh, grp->lg_addr);
1379 	if (link_state_update)
1380 		mac_link_update(grp->lg_mh, grp->lg_link_state);
1381 
1382 	mac_perim_exit(mph);
1383 	AGGR_GRP_REFRELE(grp);
1384 
1385 	return (rc);
1386 }
1387 
1388 int
1389 aggr_grp_delete(datalink_id_t linkid)
1390 {
1391 	aggr_grp_t *grp = NULL;
1392 	aggr_port_t *port, *cport;
1393 	datalink_id_t tmpid;
1394 	mod_hash_val_t val;
1395 	mac_perim_handle_t mph, pmph;
1396 	int err;
1397 
1398 	rw_enter(&aggr_grp_lock, RW_WRITER);
1399 
1400 	if (mod_hash_find(aggr_grp_hash, GRP_HASH_KEY(linkid),
1401 	    (mod_hash_val_t *)&grp) != 0) {
1402 		rw_exit(&aggr_grp_lock);
1403 		return (ENOENT);
1404 	}
1405 
1406 	/*
1407 	 * Note that dls_devnet_destroy() must be called before lg_lock is
1408 	 * held. Otherwise, it will deadlock if another thread is in
1409 	 * aggr_m_stat() and thus has a kstat_hold() on the kstats that
1410 	 * dls_devnet_destroy() needs to delete.
1411 	 */
1412 	if ((err = dls_devnet_destroy(grp->lg_mh, &tmpid, B_TRUE)) != 0) {
1413 		rw_exit(&aggr_grp_lock);
1414 		return (err);
1415 	}
1416 	ASSERT(linkid == tmpid);
1417 
1418 	/*
1419 	 * Unregister from the MAC service module. Since this can
1420 	 * fail if a client hasn't closed the MAC port, we gracefully
1421 	 * fail the operation.
1422 	 */
1423 	if ((err = mac_disable(grp->lg_mh)) != 0) {
1424 		(void) dls_devnet_create(grp->lg_mh, linkid);
1425 		rw_exit(&aggr_grp_lock);
1426 		return (err);
1427 	}
1428 	(void) mod_hash_remove(aggr_grp_hash, GRP_HASH_KEY(linkid), &val);
1429 	ASSERT(grp == (aggr_grp_t *)val);
1430 
1431 	ASSERT(aggr_grp_cnt > 0);
1432 	aggr_grp_cnt--;
1433 	rw_exit(&aggr_grp_lock);
1434 
1435 	/*
1436 	 * Inform the lacp_rx thread to exit.
1437 	 */
1438 	mutex_enter(&grp->lg_lacp_lock);
1439 	grp->lg_lacp_done = B_TRUE;
1440 	cv_signal(&grp->lg_lacp_cv);
1441 	while (grp->lg_lacp_rx_thread != NULL)
1442 		cv_wait(&grp->lg_lacp_cv, &grp->lg_lacp_lock);
1443 	mutex_exit(&grp->lg_lacp_lock);
1444 
1445 	mac_perim_enter_by_mh(grp->lg_mh, &mph);
1446 
1447 	grp->lg_closing = B_TRUE;
1448 	/* detach and free MAC ports associated with group */
1449 	port = grp->lg_ports;
1450 	while (port != NULL) {
1451 		cport = port->lp_next;
1452 		mac_perim_enter_by_mh(port->lp_mh, &pmph);
1453 		if (grp->lg_started)
1454 			aggr_port_stop(port);
1455 		(void) aggr_grp_detach_port(grp, port);
1456 		mac_perim_exit(pmph);
1457 		aggr_rem_pseudo_rx_group(port, &grp->lg_rx_group);
1458 		aggr_port_delete(port);
1459 		port = cport;
1460 	}
1461 
1462 	mac_perim_exit(mph);
1463 
1464 	/*
1465 	 * Wait for the port's lacp timer thread and its notification callback
1466 	 * to exit before calling mac_unregister() since both needs to access
1467 	 * the mac perimeter of the grp.
1468 	 */
1469 	aggr_grp_port_wait(grp);
1470 
1471 	VERIFY(mac_unregister(grp->lg_mh) == 0);
1472 	grp->lg_mh = NULL;
1473 
1474 	AGGR_GRP_REFRELE(grp);
1475 	return (0);
1476 }
1477 
1478 void
1479 aggr_grp_free(aggr_grp_t *grp)
1480 {
1481 	ASSERT(grp->lg_refs == 0);
1482 	ASSERT(grp->lg_port_ref == 0);
1483 	if (grp->lg_key > AGGR_MAX_KEY) {
1484 		id_free(key_ids, grp->lg_key);
1485 		grp->lg_key = 0;
1486 	}
1487 	kmem_cache_free(aggr_grp_cache, grp);
1488 }
1489 
1490 int
1491 aggr_grp_info(datalink_id_t linkid, void *fn_arg,
1492     aggr_grp_info_new_grp_fn_t new_grp_fn,
1493     aggr_grp_info_new_port_fn_t new_port_fn)
1494 {
1495 	aggr_grp_t	*grp;
1496 	aggr_port_t	*port;
1497 	mac_perim_handle_t mph, pmph;
1498 	int		rc = 0;
1499 
1500 	rw_enter(&aggr_grp_lock, RW_READER);
1501 
1502 	if (mod_hash_find(aggr_grp_hash, GRP_HASH_KEY(linkid),
1503 	    (mod_hash_val_t *)&grp) != 0) {
1504 		rw_exit(&aggr_grp_lock);
1505 		return (ENOENT);
1506 	}
1507 	AGGR_GRP_REFHOLD(grp);
1508 
1509 	mac_perim_enter_by_mh(grp->lg_mh, &mph);
1510 	rw_exit(&aggr_grp_lock);
1511 
1512 	rc = new_grp_fn(fn_arg, grp->lg_linkid,
1513 	    (grp->lg_key > AGGR_MAX_KEY) ? 0 : grp->lg_key, grp->lg_addr,
1514 	    grp->lg_addr_fixed, grp->lg_force, grp->lg_tx_policy,
1515 	    grp->lg_nports, grp->lg_lacp_mode, grp->aggr.PeriodicTimer);
1516 
1517 	if (rc != 0)
1518 		goto bail;
1519 
1520 	for (port = grp->lg_ports; port != NULL; port = port->lp_next) {
1521 		mac_perim_enter_by_mh(port->lp_mh, &pmph);
1522 		rc = new_port_fn(fn_arg, port->lp_linkid, port->lp_addr,
1523 		    port->lp_state, &port->lp_lacp.ActorOperPortState);
1524 		mac_perim_exit(pmph);
1525 
1526 		if (rc != 0)
1527 			goto bail;
1528 	}
1529 
1530 bail:
1531 	mac_perim_exit(mph);
1532 	AGGR_GRP_REFRELE(grp);
1533 	return (rc);
1534 }
1535 
1536 /*ARGSUSED*/
1537 static void
1538 aggr_m_ioctl(void *arg, queue_t *q, mblk_t *mp)
1539 {
1540 	miocnak(q, mp, 0, ENOTSUP);
1541 }
1542 
1543 static int
1544 aggr_grp_stat(aggr_grp_t *grp, uint_t stat, uint64_t *val)
1545 {
1546 	aggr_port_t	*port;
1547 	uint_t		stat_index;
1548 
1549 	/* We only aggregate counter statistics. */
1550 	if (IS_MAC_STAT(stat) && !MAC_STAT_ISACOUNTER(stat) ||
1551 	    IS_MACTYPE_STAT(stat) && !ETHER_STAT_ISACOUNTER(stat)) {
1552 		return (ENOTSUP);
1553 	}
1554 
1555 	/*
1556 	 * Counter statistics for a group are computed by aggregating the
1557 	 * counters of the members MACs while they were aggregated, plus
1558 	 * the residual counter of the group itself, which is updated each
1559 	 * time a MAC is removed from the group.
1560 	 */
1561 	*val = 0;
1562 	for (port = grp->lg_ports; port != NULL; port = port->lp_next) {
1563 		/* actual port statistic */
1564 		*val += aggr_port_stat(port, stat);
1565 		/*
1566 		 * minus the port stat when it was added, plus any residual
1567 		 * amount for the group.
1568 		 */
1569 		if (IS_MAC_STAT(stat)) {
1570 			stat_index = stat - MAC_STAT_MIN;
1571 			*val -= port->lp_stat[stat_index];
1572 			*val += grp->lg_stat[stat_index];
1573 		} else if (IS_MACTYPE_STAT(stat)) {
1574 			stat_index = stat - MACTYPE_STAT_MIN;
1575 			*val -= port->lp_ether_stat[stat_index];
1576 			*val += grp->lg_ether_stat[stat_index];
1577 		}
1578 	}
1579 	return (0);
1580 }
1581 
1582 static int
1583 aggr_m_stat(void *arg, uint_t stat, uint64_t *val)
1584 {
1585 	aggr_grp_t		*grp = arg;
1586 	mac_perim_handle_t	mph;
1587 	int			rval = 0;
1588 
1589 	mac_perim_enter_by_mh(grp->lg_mh, &mph);
1590 
1591 	switch (stat) {
1592 	case MAC_STAT_IFSPEED:
1593 		*val = grp->lg_ifspeed;
1594 		break;
1595 
1596 	case ETHER_STAT_LINK_DUPLEX:
1597 		*val = grp->lg_link_duplex;
1598 		break;
1599 
1600 	default:
1601 		/*
1602 		 * For all other statistics, we return the aggregated stat
1603 		 * from the underlying ports.  aggr_grp_stat() will set
1604 		 * rval appropriately if the statistic isn't a counter.
1605 		 */
1606 		rval = aggr_grp_stat(grp, stat, val);
1607 	}
1608 
1609 	mac_perim_exit(mph);
1610 	return (rval);
1611 }
1612 
1613 static int
1614 aggr_m_start(void *arg)
1615 {
1616 	aggr_grp_t *grp = arg;
1617 	aggr_port_t *port;
1618 	mac_perim_handle_t mph, pmph;
1619 
1620 	mac_perim_enter_by_mh(grp->lg_mh, &mph);
1621 
1622 	/*
1623 	 * Attempts to start all configured members of the group.
1624 	 * Group members will be attached when their link-up notification
1625 	 * is received.
1626 	 */
1627 	for (port = grp->lg_ports; port != NULL; port = port->lp_next) {
1628 		mac_perim_enter_by_mh(port->lp_mh, &pmph);
1629 		if (aggr_port_start(port) != 0) {
1630 			mac_perim_exit(pmph);
1631 			continue;
1632 		}
1633 
1634 		/*
1635 		 * Turn on the promiscuous mode if it is required to receive
1636 		 * the non-primary address over a port, or the promiscous
1637 		 * mode is enabled over the aggr.
1638 		 */
1639 		if (grp->lg_promisc || port->lp_prom_addr != NULL) {
1640 			if (aggr_port_promisc(port, B_TRUE) != 0)
1641 				aggr_port_stop(port);
1642 		}
1643 		mac_perim_exit(pmph);
1644 	}
1645 
1646 	grp->lg_started = B_TRUE;
1647 
1648 	mac_perim_exit(mph);
1649 	return (0);
1650 }
1651 
1652 static void
1653 aggr_m_stop(void *arg)
1654 {
1655 	aggr_grp_t *grp = arg;
1656 	aggr_port_t *port;
1657 	mac_perim_handle_t mph, pmph;
1658 
1659 	mac_perim_enter_by_mh(grp->lg_mh, &mph);
1660 
1661 	for (port = grp->lg_ports; port != NULL; port = port->lp_next) {
1662 		mac_perim_enter_by_mh(port->lp_mh, &pmph);
1663 
1664 		/* reset port promiscuous mode */
1665 		(void) aggr_port_promisc(port, B_FALSE);
1666 
1667 		aggr_port_stop(port);
1668 		mac_perim_exit(pmph);
1669 	}
1670 
1671 	grp->lg_started = B_FALSE;
1672 	mac_perim_exit(mph);
1673 }
1674 
1675 static int
1676 aggr_m_promisc(void *arg, boolean_t on)
1677 {
1678 	aggr_grp_t *grp = arg;
1679 	aggr_port_t *port;
1680 	boolean_t link_state_changed = B_FALSE;
1681 	mac_perim_handle_t mph, pmph;
1682 
1683 	AGGR_GRP_REFHOLD(grp);
1684 	mac_perim_enter_by_mh(grp->lg_mh, &mph);
1685 
1686 	ASSERT(!grp->lg_closing);
1687 
1688 	if (on == grp->lg_promisc)
1689 		goto bail;
1690 
1691 	for (port = grp->lg_ports; port != NULL; port = port->lp_next) {
1692 		int	err = 0;
1693 
1694 		mac_perim_enter_by_mh(port->lp_mh, &pmph);
1695 		AGGR_PORT_REFHOLD(port);
1696 		if (!on && (port->lp_prom_addr == NULL))
1697 			err = aggr_port_promisc(port, B_FALSE);
1698 		else if (on && port->lp_started)
1699 			err = aggr_port_promisc(port, B_TRUE);
1700 
1701 		if (err != 0) {
1702 			if (aggr_grp_detach_port(grp, port))
1703 				link_state_changed = B_TRUE;
1704 		} else {
1705 			/*
1706 			 * If a port was detached because of a previous
1707 			 * failure changing the promiscuity, the port
1708 			 * is reattached when it successfully changes
1709 			 * the promiscuity now, and this might cause
1710 			 * the link state of the aggregation to change.
1711 			 */
1712 			if (aggr_grp_attach_port(grp, port))
1713 				link_state_changed = B_TRUE;
1714 		}
1715 		mac_perim_exit(pmph);
1716 		AGGR_PORT_REFRELE(port);
1717 	}
1718 
1719 	grp->lg_promisc = on;
1720 
1721 	if (link_state_changed)
1722 		mac_link_update(grp->lg_mh, grp->lg_link_state);
1723 
1724 bail:
1725 	mac_perim_exit(mph);
1726 	AGGR_GRP_REFRELE(grp);
1727 
1728 	return (0);
1729 }
1730 
1731 static void
1732 aggr_grp_port_rename(const char *new_name, void *arg)
1733 {
1734 	/*
1735 	 * aggr port's mac client name is the format of "aggr link name" plus
1736 	 * AGGR_PORT_NAME_DELIMIT plus "underneath link name".
1737 	 */
1738 	int aggr_len, link_len, clnt_name_len, i;
1739 	char *str_end, *str_st, *str_del;
1740 	char aggr_name[MAXNAMELEN];
1741 	char link_name[MAXNAMELEN];
1742 	char *clnt_name;
1743 	aggr_grp_t *aggr_grp = arg;
1744 	aggr_port_t *aggr_port = aggr_grp->lg_ports;
1745 
1746 	for (i = 0; i < aggr_grp->lg_nports; i++) {
1747 		clnt_name = mac_client_name(aggr_port->lp_mch);
1748 		clnt_name_len = strlen(clnt_name);
1749 		str_st = clnt_name;
1750 		str_end = &(clnt_name[clnt_name_len]);
1751 		str_del = strchr(str_st, AGGR_PORT_NAME_DELIMIT);
1752 		ASSERT(str_del != NULL);
1753 		aggr_len = (intptr_t)((uintptr_t)str_del - (uintptr_t)str_st);
1754 		link_len = (intptr_t)((uintptr_t)str_end - (uintptr_t)str_del);
1755 		bzero(aggr_name, MAXNAMELEN);
1756 		bzero(link_name, MAXNAMELEN);
1757 		bcopy(clnt_name, aggr_name, aggr_len);
1758 		bcopy(str_del, link_name, link_len + 1);
1759 		bzero(clnt_name, MAXNAMELEN);
1760 		(void) snprintf(clnt_name, MAXNAMELEN, "%s%s", new_name,
1761 		    link_name);
1762 
1763 		(void) mac_rename_primary(aggr_port->lp_mh, NULL);
1764 		aggr_port = aggr_port->lp_next;
1765 	}
1766 }
1767 
1768 /*
1769  * Initialize the capabilities that are advertised for the group
1770  * according to the capabilities of the constituent ports.
1771  */
1772 static boolean_t
1773 aggr_m_capab_get(void *arg, mac_capab_t cap, void *cap_data)
1774 {
1775 	aggr_grp_t *grp = arg;
1776 
1777 	switch (cap) {
1778 	case MAC_CAPAB_HCKSUM: {
1779 		uint32_t *hcksum_txflags = cap_data;
1780 		*hcksum_txflags = grp->lg_hcksum_txflags;
1781 		break;
1782 	}
1783 	case MAC_CAPAB_LSO: {
1784 		mac_capab_lso_t *cap_lso = cap_data;
1785 
1786 		if (grp->lg_lso) {
1787 			*cap_lso = grp->lg_cap_lso;
1788 			break;
1789 		} else {
1790 			return (B_FALSE);
1791 		}
1792 	}
1793 	case MAC_CAPAB_NO_NATIVEVLAN:
1794 		return (!grp->lg_vlan);
1795 	case MAC_CAPAB_NO_ZCOPY:
1796 		return (!grp->lg_zcopy);
1797 	case MAC_CAPAB_RINGS: {
1798 		mac_capab_rings_t *cap_rings = cap_data;
1799 
1800 		if (cap_rings->mr_type == MAC_RING_TYPE_RX) {
1801 			cap_rings->mr_group_type = MAC_GROUP_TYPE_STATIC;
1802 			cap_rings->mr_rnum = grp->lg_rx_group.arg_ring_cnt;
1803 			cap_rings->mr_rget = aggr_fill_ring;
1804 
1805 			/*
1806 			 * An aggregation advertises only one (pseudo) RX
1807 			 * group, which virtualizes the main/primary group of
1808 			 * the underlying devices.
1809 			 */
1810 			cap_rings->mr_gnum = 1;
1811 			cap_rings->mr_gget = aggr_fill_group;
1812 			cap_rings->mr_gaddring = NULL;
1813 			cap_rings->mr_gremring = NULL;
1814 		} else {
1815 			return (B_FALSE);
1816 		}
1817 		break;
1818 	}
1819 	case MAC_CAPAB_AGGR:
1820 	{
1821 		mac_capab_aggr_t *aggr_cap;
1822 
1823 		if (cap_data != NULL) {
1824 			aggr_cap = cap_data;
1825 			aggr_cap->mca_rename_fn = aggr_grp_port_rename;
1826 			aggr_cap->mca_unicst = aggr_m_unicst;
1827 		}
1828 		return (B_TRUE);
1829 	}
1830 	default:
1831 		return (B_FALSE);
1832 	}
1833 	return (B_TRUE);
1834 }
1835 
1836 /*
1837  * Callback funtion for MAC layer to register groups.
1838  */
1839 static void
1840 aggr_fill_group(void *arg, mac_ring_type_t rtype, const int index,
1841     mac_group_info_t *infop, mac_group_handle_t gh)
1842 {
1843 	aggr_grp_t *grp = arg;
1844 	aggr_pseudo_rx_group_t *rx_group;
1845 
1846 	ASSERT(rtype == MAC_RING_TYPE_RX && index == 0);
1847 	rx_group = &grp->lg_rx_group;
1848 	rx_group->arg_gh = gh;
1849 	rx_group->arg_grp = grp;
1850 
1851 	infop->mgi_driver = (mac_group_driver_t)rx_group;
1852 	infop->mgi_start = NULL;
1853 	infop->mgi_stop = NULL;
1854 	infop->mgi_addmac = aggr_addmac;
1855 	infop->mgi_remmac = aggr_remmac;
1856 	infop->mgi_count = rx_group->arg_ring_cnt;
1857 }
1858 
1859 /*
1860  * Callback funtion for MAC layer to register all rings.
1861  */
1862 static void
1863 aggr_fill_ring(void *arg, mac_ring_type_t rtype, const int rg_index,
1864     const int index, mac_ring_info_t *infop, mac_ring_handle_t rh)
1865 {
1866 	aggr_grp_t	*grp = arg;
1867 
1868 	switch (rtype) {
1869 	case MAC_RING_TYPE_RX: {
1870 		aggr_pseudo_rx_group_t	*rx_group = &grp->lg_rx_group;
1871 		aggr_pseudo_rx_ring_t	*rx_ring;
1872 		mac_intr_t		aggr_mac_intr;
1873 
1874 		ASSERT(rg_index == 0);
1875 
1876 		ASSERT((index >= 0) && (index < rx_group->arg_ring_cnt));
1877 		rx_ring = rx_group->arg_rings + index;
1878 		rx_ring->arr_rh = rh;
1879 
1880 		/*
1881 		 * Entrypoint to enable interrupt (disable poll) and
1882 		 * disable interrupt (enable poll).
1883 		 */
1884 		aggr_mac_intr.mi_handle = (mac_intr_handle_t)rx_ring;
1885 		aggr_mac_intr.mi_enable = aggr_pseudo_enable_intr;
1886 		aggr_mac_intr.mi_disable = aggr_pseudo_disable_intr;
1887 
1888 		infop->mri_driver = (mac_ring_driver_t)rx_ring;
1889 		infop->mri_start = aggr_pseudo_start_ring;
1890 		infop->mri_stop = aggr_pseudo_stop_ring;
1891 
1892 		infop->mri_intr = aggr_mac_intr;
1893 		infop->mri_poll = aggr_rx_poll;
1894 		break;
1895 	}
1896 	default:
1897 		break;
1898 	}
1899 }
1900 
1901 static mblk_t *
1902 aggr_rx_poll(void *arg, int bytes_to_pickup)
1903 {
1904 	aggr_pseudo_rx_ring_t *rr_ring = arg;
1905 	aggr_port_t *port = rr_ring->arr_port;
1906 	aggr_grp_t *grp = port->lp_grp;
1907 	mblk_t *mp_chain, *mp, **mpp;
1908 
1909 	mp_chain = mac_hwring_poll(rr_ring->arr_hw_rh, bytes_to_pickup);
1910 
1911 	if (grp->lg_lacp_mode == AGGR_LACP_OFF)
1912 		return (mp_chain);
1913 
1914 	mpp = &mp_chain;
1915 	while ((mp = *mpp) != NULL) {
1916 		if (MBLKL(mp) >= sizeof (struct ether_header)) {
1917 			struct ether_header *ehp;
1918 
1919 			ehp = (struct ether_header *)mp->b_rptr;
1920 			if (ntohs(ehp->ether_type) == ETHERTYPE_SLOW) {
1921 				*mpp = mp->b_next;
1922 				mp->b_next = NULL;
1923 				aggr_recv_lacp(port,
1924 				    (mac_resource_handle_t)rr_ring, mp);
1925 				continue;
1926 			}
1927 		}
1928 
1929 		if (!port->lp_collector_enabled) {
1930 			*mpp = mp->b_next;
1931 			mp->b_next = NULL;
1932 			freemsg(mp);
1933 			continue;
1934 		}
1935 		mpp = &mp->b_next;
1936 	}
1937 	return (mp_chain);
1938 }
1939 
1940 static int
1941 aggr_addmac(void *arg, const uint8_t *mac_addr)
1942 {
1943 	aggr_pseudo_rx_group_t	*rx_group = (aggr_pseudo_rx_group_t *)arg;
1944 	aggr_unicst_addr_t	*addr, **pprev;
1945 	aggr_grp_t		*grp = rx_group->arg_grp;
1946 	aggr_port_t		*port, *p;
1947 	mac_perim_handle_t	mph;
1948 	int			err = 0;
1949 
1950 	mac_perim_enter_by_mh(grp->lg_mh, &mph);
1951 
1952 	if (bcmp(mac_addr, grp->lg_addr, ETHERADDRL) == 0) {
1953 		mac_perim_exit(mph);
1954 		return (0);
1955 	}
1956 
1957 	/*
1958 	 * Insert this mac address into the list of mac addresses owned by
1959 	 * the aggregation pseudo group.
1960 	 */
1961 	pprev = &rx_group->arg_macaddr;
1962 	while ((addr = *pprev) != NULL) {
1963 		if (bcmp(mac_addr, addr->aua_addr, ETHERADDRL) == 0) {
1964 			mac_perim_exit(mph);
1965 			return (EEXIST);
1966 		}
1967 		pprev = &addr->aua_next;
1968 	}
1969 	addr = kmem_alloc(sizeof (aggr_unicst_addr_t), KM_SLEEP);
1970 	bcopy(mac_addr, addr->aua_addr, ETHERADDRL);
1971 	addr->aua_next = NULL;
1972 	*pprev = addr;
1973 
1974 	for (port = grp->lg_ports; port != NULL; port = port->lp_next)
1975 		if ((err = aggr_port_addmac(port, mac_addr)) != 0)
1976 			break;
1977 
1978 	if (err != 0) {
1979 		for (p = grp->lg_ports; p != port; p = p->lp_next)
1980 			aggr_port_remmac(p, mac_addr);
1981 
1982 		*pprev = NULL;
1983 		kmem_free(addr, sizeof (aggr_unicst_addr_t));
1984 	}
1985 
1986 	mac_perim_exit(mph);
1987 	return (err);
1988 }
1989 
1990 static int
1991 aggr_remmac(void *arg, const uint8_t *mac_addr)
1992 {
1993 	aggr_pseudo_rx_group_t	*rx_group = (aggr_pseudo_rx_group_t *)arg;
1994 	aggr_unicst_addr_t	*addr, **pprev;
1995 	aggr_grp_t		*grp = rx_group->arg_grp;
1996 	aggr_port_t		*port;
1997 	mac_perim_handle_t	mph;
1998 	int			err = 0;
1999 
2000 	mac_perim_enter_by_mh(grp->lg_mh, &mph);
2001 
2002 	if (bcmp(mac_addr, grp->lg_addr, ETHERADDRL) == 0) {
2003 		mac_perim_exit(mph);
2004 		return (0);
2005 	}
2006 
2007 	/*
2008 	 * Insert this mac address into the list of mac addresses owned by
2009 	 * the aggregation pseudo group.
2010 	 */
2011 	pprev = &rx_group->arg_macaddr;
2012 	while ((addr = *pprev) != NULL) {
2013 		if (bcmp(mac_addr, addr->aua_addr, ETHERADDRL) != 0) {
2014 			pprev = &addr->aua_next;
2015 			continue;
2016 		}
2017 		break;
2018 	}
2019 	if (addr == NULL) {
2020 		mac_perim_exit(mph);
2021 		return (EINVAL);
2022 	}
2023 
2024 	for (port = grp->lg_ports; port != NULL; port = port->lp_next)
2025 		aggr_port_remmac(port, mac_addr);
2026 
2027 	*pprev = addr->aua_next;
2028 	kmem_free(addr, sizeof (aggr_unicst_addr_t));
2029 
2030 	mac_perim_exit(mph);
2031 	return (err);
2032 }
2033 
2034 /*
2035  * Add or remove the multicast addresses that are defined for the group
2036  * to or from the specified port.
2037  *
2038  * Note that aggr_grp_multicst_port(..., B_TRUE) is called when the port
2039  * is started and attached, and aggr_grp_multicst_port(..., B_FALSE) is
2040  * called when the port is either stopped or detached.
2041  */
2042 void
2043 aggr_grp_multicst_port(aggr_port_t *port, boolean_t add)
2044 {
2045 	aggr_grp_t *grp = port->lp_grp;
2046 
2047 	ASSERT(MAC_PERIM_HELD(port->lp_mh));
2048 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
2049 
2050 	if (!port->lp_started || port->lp_state != AGGR_PORT_STATE_ATTACHED)
2051 		return;
2052 
2053 	mac_multicast_refresh(grp->lg_mh, aggr_port_multicst, port, add);
2054 }
2055 
2056 static int
2057 aggr_m_multicst(void *arg, boolean_t add, const uint8_t *addrp)
2058 {
2059 	aggr_grp_t *grp = arg;
2060 	aggr_port_t *port = NULL;
2061 	mac_perim_handle_t mph;
2062 	int err = 0, cerr;
2063 
2064 	mac_perim_enter_by_mh(grp->lg_mh, &mph);
2065 	for (port = grp->lg_ports; port != NULL; port = port->lp_next) {
2066 		if (port->lp_state != AGGR_PORT_STATE_ATTACHED ||
2067 		    !port->lp_started) {
2068 			continue;
2069 		}
2070 		cerr = aggr_port_multicst(port, add, addrp);
2071 		if (cerr != 0 && err == 0)
2072 			err = cerr;
2073 	}
2074 	mac_perim_exit(mph);
2075 	return (err);
2076 }
2077 
2078 static int
2079 aggr_m_unicst(void *arg, const uint8_t *macaddr)
2080 {
2081 	aggr_grp_t *grp = arg;
2082 	mac_perim_handle_t mph;
2083 	int err;
2084 
2085 	mac_perim_enter_by_mh(grp->lg_mh, &mph);
2086 	err = aggr_grp_modify_common(grp, AGGR_MODIFY_MAC, 0, B_TRUE, macaddr,
2087 	    0, 0);
2088 	mac_perim_exit(mph);
2089 	return (err);
2090 }
2091 
2092 /*
2093  * Initialize the capabilities that are advertised for the group
2094  * according to the capabilities of the constituent ports.
2095  */
2096 static void
2097 aggr_grp_capab_set(aggr_grp_t *grp)
2098 {
2099 	uint32_t cksum;
2100 	aggr_port_t *port;
2101 	mac_capab_lso_t cap_lso;
2102 
2103 	ASSERT(grp->lg_mh == NULL);
2104 	ASSERT(grp->lg_ports != NULL);
2105 
2106 	grp->lg_hcksum_txflags = (uint32_t)-1;
2107 	grp->lg_zcopy = B_TRUE;
2108 	grp->lg_vlan = B_TRUE;
2109 
2110 	grp->lg_lso = B_TRUE;
2111 	grp->lg_cap_lso.lso_flags = (t_uscalar_t)-1;
2112 	grp->lg_cap_lso.lso_basic_tcp_ipv4.lso_max = (t_uscalar_t)-1;
2113 
2114 	for (port = grp->lg_ports; port != NULL; port = port->lp_next) {
2115 		if (!mac_capab_get(port->lp_mh, MAC_CAPAB_HCKSUM, &cksum))
2116 			cksum = 0;
2117 		grp->lg_hcksum_txflags &= cksum;
2118 
2119 		grp->lg_vlan &=
2120 		    !mac_capab_get(port->lp_mh, MAC_CAPAB_NO_NATIVEVLAN, NULL);
2121 
2122 		grp->lg_zcopy &=
2123 		    !mac_capab_get(port->lp_mh, MAC_CAPAB_NO_ZCOPY, NULL);
2124 
2125 		grp->lg_lso &=
2126 		    mac_capab_get(port->lp_mh, MAC_CAPAB_LSO, &cap_lso);
2127 		if (grp->lg_lso) {
2128 			grp->lg_cap_lso.lso_flags &= cap_lso.lso_flags;
2129 			if (grp->lg_cap_lso.lso_basic_tcp_ipv4.lso_max >
2130 			    cap_lso.lso_basic_tcp_ipv4.lso_max)
2131 				grp->lg_cap_lso.lso_basic_tcp_ipv4.lso_max =
2132 				    cap_lso.lso_basic_tcp_ipv4.lso_max;
2133 		}
2134 	}
2135 }
2136 
2137 /*
2138  * Checks whether the capabilities of the port being added are compatible
2139  * with the current capabilities of the aggregation.
2140  */
2141 static boolean_t
2142 aggr_grp_capab_check(aggr_grp_t *grp, aggr_port_t *port)
2143 {
2144 	uint32_t hcksum_txflags;
2145 
2146 	ASSERT(grp->lg_ports != NULL);
2147 
2148 	if (((!mac_capab_get(port->lp_mh, MAC_CAPAB_NO_NATIVEVLAN, NULL)) &
2149 	    grp->lg_vlan) != grp->lg_vlan) {
2150 		return (B_FALSE);
2151 	}
2152 
2153 	if (((!mac_capab_get(port->lp_mh, MAC_CAPAB_NO_ZCOPY, NULL)) &
2154 	    grp->lg_zcopy) != grp->lg_zcopy) {
2155 		return (B_FALSE);
2156 	}
2157 
2158 	if (!mac_capab_get(port->lp_mh, MAC_CAPAB_HCKSUM, &hcksum_txflags)) {
2159 		if (grp->lg_hcksum_txflags != 0)
2160 			return (B_FALSE);
2161 	} else if ((hcksum_txflags & grp->lg_hcksum_txflags) !=
2162 	    grp->lg_hcksum_txflags) {
2163 		return (B_FALSE);
2164 	}
2165 
2166 	if (grp->lg_lso) {
2167 		mac_capab_lso_t cap_lso;
2168 
2169 		if (mac_capab_get(port->lp_mh, MAC_CAPAB_LSO, &cap_lso)) {
2170 			if ((grp->lg_cap_lso.lso_flags & cap_lso.lso_flags) !=
2171 			    grp->lg_cap_lso.lso_flags)
2172 				return (B_FALSE);
2173 			if (grp->lg_cap_lso.lso_basic_tcp_ipv4.lso_max >
2174 			    cap_lso.lso_basic_tcp_ipv4.lso_max)
2175 				return (B_FALSE);
2176 		} else {
2177 			return (B_FALSE);
2178 		}
2179 	}
2180 
2181 	return (B_TRUE);
2182 }
2183 
2184 /*
2185  * Returns the maximum SDU according to the SDU of the constituent ports.
2186  */
2187 static uint_t
2188 aggr_grp_max_sdu(aggr_grp_t *grp)
2189 {
2190 	uint_t max_sdu = (uint_t)-1;
2191 	aggr_port_t *port;
2192 
2193 	ASSERT(grp->lg_ports != NULL);
2194 
2195 	for (port = grp->lg_ports; port != NULL; port = port->lp_next) {
2196 		uint_t port_sdu_max;
2197 
2198 		mac_sdu_get(port->lp_mh, NULL, &port_sdu_max);
2199 		if (max_sdu > port_sdu_max)
2200 			max_sdu = port_sdu_max;
2201 	}
2202 
2203 	return (max_sdu);
2204 }
2205 
2206 /*
2207  * Checks if the maximum SDU of the specified port is compatible
2208  * with the maximum SDU of the specified aggregation group, returns
2209  * B_TRUE if it is, B_FALSE otherwise.
2210  */
2211 static boolean_t
2212 aggr_grp_sdu_check(aggr_grp_t *grp, aggr_port_t *port)
2213 {
2214 	uint_t port_sdu_max;
2215 
2216 	mac_sdu_get(port->lp_mh, NULL, &port_sdu_max);
2217 	return (port_sdu_max >= grp->lg_max_sdu);
2218 }
2219 
2220 /*
2221  * Returns the maximum margin according to the margin of the constituent ports.
2222  */
2223 static uint32_t
2224 aggr_grp_max_margin(aggr_grp_t *grp)
2225 {
2226 	uint32_t margin = UINT32_MAX;
2227 	aggr_port_t *port;
2228 
2229 	ASSERT(grp->lg_mh == NULL);
2230 	ASSERT(grp->lg_ports != NULL);
2231 
2232 	for (port = grp->lg_ports; port != NULL; port = port->lp_next) {
2233 		if (margin > port->lp_margin)
2234 			margin = port->lp_margin;
2235 	}
2236 
2237 	grp->lg_margin = margin;
2238 	return (margin);
2239 }
2240 
2241 /*
2242  * Checks if the maximum margin of the specified port is compatible
2243  * with the maximum margin of the specified aggregation group, returns
2244  * B_TRUE if it is, B_FALSE otherwise.
2245  */
2246 static boolean_t
2247 aggr_grp_margin_check(aggr_grp_t *grp, aggr_port_t *port)
2248 {
2249 	if (port->lp_margin >= grp->lg_margin)
2250 		return (B_TRUE);
2251 
2252 	/*
2253 	 * See whether the current margin value is allowed to be changed to
2254 	 * the new value.
2255 	 */
2256 	if (!mac_margin_update(grp->lg_mh, port->lp_margin))
2257 		return (B_FALSE);
2258 
2259 	grp->lg_margin = port->lp_margin;
2260 	return (B_TRUE);
2261 }
2262 
2263 /*
2264  * Set MTU on individual ports of an aggregation group
2265  */
2266 static int
2267 aggr_set_port_sdu(aggr_grp_t *grp, aggr_port_t *port, uint32_t sdu,
2268     uint32_t *old_mtu)
2269 {
2270 	boolean_t 		removed = B_FALSE;
2271 	mac_perim_handle_t	mph;
2272 	mac_diag_t		diag;
2273 	int			err, rv, retry = 0;
2274 
2275 	if (port->lp_mah != NULL) {
2276 		(void) mac_unicast_remove(port->lp_mch, port->lp_mah);
2277 		port->lp_mah = NULL;
2278 		removed = B_TRUE;
2279 	}
2280 	err = mac_set_mtu(port->lp_mh, sdu, old_mtu);
2281 try_again:
2282 	if (removed && (rv = mac_unicast_add(port->lp_mch, NULL,
2283 	    MAC_UNICAST_PRIMARY | MAC_UNICAST_DISABLE_TX_VID_CHECK,
2284 	    &port->lp_mah, 0, &diag)) != 0) {
2285 		/*
2286 		 * following is a workaround for a bug in 'bge' driver.
2287 		 * See CR 6794654 for more information and this work around
2288 		 * will be removed once the CR is fixed.
2289 		 */
2290 		if (rv == EIO && retry++ < 3) {
2291 			delay(2 * hz);
2292 			goto try_again;
2293 		}
2294 		/*
2295 		 * if mac_unicast_add() failed while setting the MTU,
2296 		 * detach the port from the group.
2297 		 */
2298 		mac_perim_enter_by_mh(port->lp_mh, &mph);
2299 		(void) aggr_grp_detach_port(grp, port);
2300 		mac_perim_exit(mph);
2301 		cmn_err(CE_WARN, "Unable to restart the port %s while "
2302 		    "setting MTU. Detaching the port from the aggregation.",
2303 		    mac_client_name(port->lp_mch));
2304 	}
2305 	return (err);
2306 }
2307 
2308 static int
2309 aggr_sdu_update(aggr_grp_t *grp, uint32_t sdu)
2310 {
2311 	int			err = 0, i, rv;
2312 	aggr_port_t		*port;
2313 	uint32_t		*mtu;
2314 
2315 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
2316 
2317 	/*
2318 	 * If the MTU being set is equal to aggr group's maximum
2319 	 * allowable value, then there is nothing to change
2320 	 */
2321 	if (sdu == grp->lg_max_sdu)
2322 		return (0);
2323 
2324 	/* 0 is aggr group's min sdu */
2325 	if (sdu == 0)
2326 		return (EINVAL);
2327 
2328 	mtu = kmem_alloc(sizeof (uint32_t) * grp->lg_nports, KM_SLEEP);
2329 	for (port = grp->lg_ports, i = 0; port != NULL && err == 0;
2330 	    port = port->lp_next, i++) {
2331 		err = aggr_set_port_sdu(grp, port, sdu, mtu + i);
2332 	}
2333 	if (err != 0) {
2334 		/* recover from error: reset the mtus of the ports */
2335 		aggr_port_t *tmp;
2336 
2337 		for (tmp = grp->lg_ports, i = 0; tmp != port;
2338 		    tmp = tmp->lp_next, i++) {
2339 			(void) aggr_set_port_sdu(grp, tmp, *(mtu + i), NULL);
2340 		}
2341 		goto bail;
2342 	}
2343 	grp->lg_max_sdu = aggr_grp_max_sdu(grp);
2344 	rv = mac_maxsdu_update(grp->lg_mh, grp->lg_max_sdu);
2345 	ASSERT(rv == 0);
2346 bail:
2347 	kmem_free(mtu, sizeof (uint32_t) * grp->lg_nports);
2348 	return (err);
2349 }
2350 
2351 /*
2352  * Callback functions for set/get of properties
2353  */
2354 /*ARGSUSED*/
2355 static int
2356 aggr_m_setprop(void *m_driver, const char *pr_name, mac_prop_id_t pr_num,
2357     uint_t pr_valsize, const void *pr_val)
2358 {
2359 	int 		err = ENOTSUP;
2360 	aggr_grp_t 	*grp = m_driver;
2361 
2362 	switch (pr_num) {
2363 	case MAC_PROP_MTU: {
2364 		uint32_t 	mtu;
2365 
2366 		if (pr_valsize < sizeof (mtu)) {
2367 			err = EINVAL;
2368 			break;
2369 		}
2370 		bcopy(pr_val, &mtu, sizeof (mtu));
2371 		err = aggr_sdu_update(grp, mtu);
2372 		break;
2373 	}
2374 	default:
2375 		break;
2376 	}
2377 	return (err);
2378 }
2379 
2380 int
2381 aggr_grp_possible_mtu_range(aggr_grp_t *grp, mac_propval_range_t *range)
2382 {
2383 	mac_propval_range_t		*vals;
2384 	mac_propval_uint32_range_t	*ur;
2385 	aggr_port_t			*port;
2386 	mac_perim_handle_t		mph;
2387 	mac_prop_t 			macprop;
2388 	uint_t 				perm, i;
2389 	uint32_t 			min = 0, max = (uint32_t)-1;
2390 	int 				err = 0;
2391 
2392 	ASSERT(MAC_PERIM_HELD(grp->lg_mh));
2393 
2394 	vals = kmem_alloc(sizeof (mac_propval_range_t) * grp->lg_nports,
2395 	    KM_SLEEP);
2396 	macprop.mp_id = MAC_PROP_MTU;
2397 	macprop.mp_name = "mtu";
2398 	macprop.mp_flags = MAC_PROP_POSSIBLE;
2399 
2400 	for (port = grp->lg_ports, i = 0; port != NULL;
2401 	    port = port->lp_next, i++) {
2402 		mac_perim_enter_by_mh(port->lp_mh, &mph);
2403 		err = mac_get_prop(port->lp_mh, &macprop, vals + i,
2404 		    sizeof (mac_propval_range_t), &perm);
2405 		mac_perim_exit(mph);
2406 		if (err != 0)
2407 			break;
2408 	}
2409 	/*
2410 	 * if any of the underlying ports does not support changing MTU then
2411 	 * just return ENOTSUP
2412 	 */
2413 	if (port != NULL) {
2414 		ASSERT(err != 0);
2415 		goto done;
2416 	}
2417 	range->mpr_count = 1;
2418 	range->mpr_type = MAC_PROPVAL_UINT32;
2419 	for (i = 0; i < grp->lg_nports; i++) {
2420 		ur = &((vals + i)->range_uint32[0]);
2421 		/*
2422 		 * Take max of the min, for range_min; that is the minimum
2423 		 * MTU value for an aggregation is the maximum of the
2424 		 * minimum values of all the underlying ports
2425 		 */
2426 		if (ur->mpur_min > min)
2427 			min = ur->mpur_min;
2428 		/* Take min of the max, for range_max */
2429 		if (ur->mpur_max < max)
2430 			max = ur->mpur_max;
2431 	}
2432 	range->range_uint32[0].mpur_min = min;
2433 	range->range_uint32[0].mpur_max = max;
2434 done:
2435 	kmem_free(vals, sizeof (mac_propval_range_t) * grp->lg_nports);
2436 	return (err);
2437 }
2438 
2439 /*ARGSUSED*/
2440 static int
2441 aggr_m_getprop(void *m_driver, const char *pr_name, mac_prop_id_t pr_num,
2442     uint_t pr_flags, uint_t pr_valsize, void *pr_val, uint_t *perm)
2443 {
2444 	mac_propval_range_t 	range;
2445 	int 			err = ENOTSUP;
2446 	aggr_grp_t		*grp = m_driver;
2447 
2448 	switch (pr_num) {
2449 	case MAC_PROP_MTU:
2450 		if (!(pr_flags & MAC_PROP_POSSIBLE))
2451 			return (ENOTSUP);
2452 		if (pr_valsize < sizeof (mac_propval_range_t))
2453 			return (EINVAL);
2454 		if ((err = aggr_grp_possible_mtu_range(grp, &range)) != 0)
2455 			return (err);
2456 		bcopy(&range, pr_val, sizeof (range));
2457 		return (0);
2458 	}
2459 	return (err);
2460 }
2461