xref: /illumos-gate/usr/src/uts/common/io/mac/mac_client.c (revision 119d61cc)
1da14cebeSEric Cheng /*
2da14cebeSEric Cheng  * CDDL HEADER START
3da14cebeSEric Cheng  *
4da14cebeSEric Cheng  * The contents of this file are subject to the terms of the
5da14cebeSEric Cheng  * Common Development and Distribution License (the "License").
6da14cebeSEric Cheng  * You may not use this file except in compliance with the License.
7da14cebeSEric Cheng  *
8da14cebeSEric Cheng  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da14cebeSEric Cheng  * or http://www.opensolaris.org/os/licensing.
10da14cebeSEric Cheng  * See the License for the specific language governing permissions
11da14cebeSEric Cheng  * and limitations under the License.
12da14cebeSEric Cheng  *
13da14cebeSEric Cheng  * When distributing Covered Code, include this CDDL HEADER in each
14da14cebeSEric Cheng  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da14cebeSEric Cheng  * If applicable, add the following below this CDDL HEADER, with the
16da14cebeSEric Cheng  * fields enclosed by brackets "[]" replaced with your own identifying
17da14cebeSEric Cheng  * information: Portions Copyright [yyyy] [name of copyright owner]
18da14cebeSEric Cheng  *
19da14cebeSEric Cheng  * CDDL HEADER END
20da14cebeSEric Cheng  */
21da14cebeSEric Cheng 
22da14cebeSEric Cheng /*
235adf34bdSRajagopal Kunhappan  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24b237158dSPatrick Mooney  * Copyright 2019 Joyent, Inc.
2576fb654bSAndrew Stormont  * Copyright 2017 RackTop Systems.
26*119d61ccSAndy Fiddaman  * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
27da14cebeSEric Cheng  */
28da14cebeSEric Cheng 
29da14cebeSEric Cheng /*
30da14cebeSEric Cheng  * - General Introduction:
31da14cebeSEric Cheng  *
32da14cebeSEric Cheng  * This file contains the implementation of the MAC client kernel
33da14cebeSEric Cheng  * API and related code. The MAC client API allows a kernel module
34da14cebeSEric Cheng  * to gain access to a MAC instance (physical NIC, link aggregation, etc).
35da14cebeSEric Cheng  * It allows a MAC client to associate itself with a MAC address,
36da14cebeSEric Cheng  * VLANs, callback functions for data traffic and for promiscuous mode.
37da14cebeSEric Cheng  * The MAC client API is also used to specify the properties associated
38da14cebeSEric Cheng  * with a MAC client, such as bandwidth limits, priority, CPUS, etc.
39da14cebeSEric Cheng  * These properties are further used to determine the hardware resources
40da14cebeSEric Cheng  * to allocate to the various MAC clients.
41da14cebeSEric Cheng  *
42da14cebeSEric Cheng  * - Primary MAC clients:
43da14cebeSEric Cheng  *
44da14cebeSEric Cheng  * The MAC client API refers to "primary MAC clients". A primary MAC
45da14cebeSEric Cheng  * client is a client which "owns" the primary MAC address of
46da14cebeSEric Cheng  * the underlying MAC instance. The primary MAC address is called out
47da14cebeSEric Cheng  * since it is associated with specific semantics: the primary MAC
48da14cebeSEric Cheng  * address is the MAC address which is assigned to the IP interface
49da14cebeSEric Cheng  * when it is plumbed, and the primary MAC address is assigned
50da14cebeSEric Cheng  * to VLAN data-links. The primary address of a MAC instance can
51da14cebeSEric Cheng  * also change dynamically from under the MAC client, for example
52da14cebeSEric Cheng  * as a result of a change of state of a link aggregation. In that
53da14cebeSEric Cheng  * case the MAC layer automatically updates all data-structures which
54da14cebeSEric Cheng  * refer to the current value of the primary MAC address. Typical
55da14cebeSEric Cheng  * primary MAC clients are dls, aggr, and xnb. A typical non-primary
56da14cebeSEric Cheng  * MAC client is the vnic driver.
57da14cebeSEric Cheng  *
58da14cebeSEric Cheng  * - Virtual Switching:
59da14cebeSEric Cheng  *
60da14cebeSEric Cheng  * The MAC layer implements a virtual switch between the MAC clients
61da14cebeSEric Cheng  * (primary and non-primary) defined on top of the same underlying
62da14cebeSEric Cheng  * NIC (physical, link aggregation, etc). The virtual switch is
63da14cebeSEric Cheng  * VLAN-aware, i.e. it allows multiple MAC clients to be member
64da14cebeSEric Cheng  * of one or more VLANs, and the virtual switch will distribute
65da14cebeSEric Cheng  * multicast tagged packets only to the member of the corresponding
66da14cebeSEric Cheng  * VLANs.
67da14cebeSEric Cheng  *
68da14cebeSEric Cheng  * - Upper vs Lower MAC:
69da14cebeSEric Cheng  *
70da14cebeSEric Cheng  * Creating a VNIC on top of a MAC instance effectively causes
71da14cebeSEric Cheng  * two MAC instances to be layered on top of each other, one for
72da14cebeSEric Cheng  * the VNIC(s), one for the underlying MAC instance (physical NIC,
73da14cebeSEric Cheng  * link aggregation, etc). In the code below we refer to the
74da14cebeSEric Cheng  * underlying NIC as the "lower MAC", and we refer to VNICs as
75da14cebeSEric Cheng  * the "upper MAC".
76da14cebeSEric Cheng  *
77da14cebeSEric Cheng  * - Pass-through for VNICs:
78da14cebeSEric Cheng  *
79da14cebeSEric Cheng  * When VNICs are created on top of an underlying MAC, this causes
80da14cebeSEric Cheng  * a layering of two MAC instances. Since the lower MAC already
81da14cebeSEric Cheng  * does the switching and demultiplexing to its MAC clients, the
82da14cebeSEric Cheng  * upper MAC would simply have to pass packets to the layer below
83da14cebeSEric Cheng  * or above it, which would introduce overhead. In order to avoid
84da14cebeSEric Cheng  * this overhead, the MAC layer implements a pass-through mechanism
85da14cebeSEric Cheng  * for VNICs. When a VNIC opens the lower MAC instance, it saves
86da14cebeSEric Cheng  * the MAC client handle it optains from the MAC layer. When a MAC
87da14cebeSEric Cheng  * client opens a VNIC (upper MAC), the MAC layer detects that
88da14cebeSEric Cheng  * the MAC being opened is a VNIC, and gets the MAC client handle
89da14cebeSEric Cheng  * that the VNIC driver obtained from the lower MAC. This exchange
901a41ca23SJerry Jelinek  * is done through a private capability between the MAC layer
91da14cebeSEric Cheng  * and the VNIC driver. The upper MAC then returns that handle
92da14cebeSEric Cheng  * directly to its MAC client. Any operation done by the upper
93da14cebeSEric Cheng  * MAC client is now done on the lower MAC client handle, which
94da14cebeSEric Cheng  * allows the VNIC driver to be completely bypassed for the
95da14cebeSEric Cheng  * performance sensitive data-path.
96da14cebeSEric Cheng  *
971a41ca23SJerry Jelinek  * - Secondary MACs for VNICs:
981a41ca23SJerry Jelinek  *
991a41ca23SJerry Jelinek  * VNICs support multiple upper mac clients to enable support for
1001a41ca23SJerry Jelinek  * multiple MAC addresses on the VNIC. When the VNIC is created the
1011a41ca23SJerry Jelinek  * initial mac client is the primary upper mac. Any additional mac
1021a41ca23SJerry Jelinek  * clients are secondary macs. These are kept in sync with the primary
1031a41ca23SJerry Jelinek  * (for things such as the rx function and resource control settings)
1041a41ca23SJerry Jelinek  * using the same private capability interface between the MAC layer
1051a41ca23SJerry Jelinek  * and the VNIC layer.
1061a41ca23SJerry Jelinek  *
107da14cebeSEric Cheng  */
108da14cebeSEric Cheng 
109da14cebeSEric Cheng #include <sys/types.h>
110da14cebeSEric Cheng #include <sys/conf.h>
111da14cebeSEric Cheng #include <sys/id_space.h>
112da14cebeSEric Cheng #include <sys/esunddi.h>
113da14cebeSEric Cheng #include <sys/stat.h>
114da14cebeSEric Cheng #include <sys/mkdev.h>
115da14cebeSEric Cheng #include <sys/stream.h>
116da14cebeSEric Cheng #include <sys/strsun.h>
117da14cebeSEric Cheng #include <sys/strsubr.h>
118c61a1653SRyan Zezeski #include <sys/pattr.h>
119da14cebeSEric Cheng #include <sys/dlpi.h>
120da14cebeSEric Cheng #include <sys/modhash.h>
121da14cebeSEric Cheng #include <sys/mac_impl.h>
122da14cebeSEric Cheng #include <sys/mac_client_impl.h>
123da14cebeSEric Cheng #include <sys/mac_soft_ring.h>
1240dc2366fSVenugopal Iyer #include <sys/mac_stat.h>
125da14cebeSEric Cheng #include <sys/dls.h>
126da14cebeSEric Cheng #include <sys/dld.h>
127da14cebeSEric Cheng #include <sys/modctl.h>
128da14cebeSEric Cheng #include <sys/fs/dv_node.h>
129da14cebeSEric Cheng #include <sys/thread.h>
130da14cebeSEric Cheng #include <sys/proc.h>
131da14cebeSEric Cheng #include <sys/callb.h>
132da14cebeSEric Cheng #include <sys/cpuvar.h>
133da14cebeSEric Cheng #include <sys/atomic.h>
134da14cebeSEric Cheng #include <sys/sdt.h>
135da14cebeSEric Cheng #include <sys/mac_flow.h>
136da14cebeSEric Cheng #include <sys/ddi_intr_impl.h>
137da14cebeSEric Cheng #include <sys/disp.h>
138da14cebeSEric Cheng #include <sys/sdt.h>
139da14cebeSEric Cheng #include <sys/vnic.h>
140da14cebeSEric Cheng #include <sys/vnic_impl.h>
141da14cebeSEric Cheng #include <sys/vlan.h>
142da14cebeSEric Cheng #include <inet/ip.h>
143da14cebeSEric Cheng #include <inet/ip6.h>
144da14cebeSEric Cheng #include <sys/exacct.h>
145da14cebeSEric Cheng #include <sys/exacct_impl.h>
146da14cebeSEric Cheng #include <inet/nd.h>
147da14cebeSEric Cheng #include <sys/ethernet.h>
148da14cebeSEric Cheng 
149da14cebeSEric Cheng kmem_cache_t	*mac_client_impl_cache;
150da14cebeSEric Cheng kmem_cache_t	*mac_promisc_impl_cache;
151da14cebeSEric Cheng 
152da14cebeSEric Cheng static boolean_t mac_client_single_rcvr(mac_client_impl_t *);
153da14cebeSEric Cheng static flow_entry_t *mac_client_swap_mciflent(mac_client_impl_t *);
154da14cebeSEric Cheng static flow_entry_t *mac_client_get_flow(mac_client_impl_t *,
155da14cebeSEric Cheng     mac_unicast_impl_t *);
156da14cebeSEric Cheng static void mac_client_remove_flow_from_list(mac_client_impl_t *,
157da14cebeSEric Cheng     flow_entry_t *);
158da14cebeSEric Cheng static void mac_client_add_to_flow_list(mac_client_impl_t *, flow_entry_t *);
159da14cebeSEric Cheng static void mac_rename_flow_names(mac_client_impl_t *, const char *);
160da14cebeSEric Cheng static void mac_virtual_link_update(mac_impl_t *);
1610dc2366fSVenugopal Iyer static int mac_client_datapath_setup(mac_client_impl_t *, uint16_t,
1620dc2366fSVenugopal Iyer     uint8_t *, mac_resource_props_t *, boolean_t, mac_unicast_impl_t *);
1630dc2366fSVenugopal Iyer static void mac_client_datapath_teardown(mac_client_handle_t,
1640dc2366fSVenugopal Iyer     mac_unicast_impl_t *, flow_entry_t *);
1651a41ca23SJerry Jelinek static int mac_resource_ctl_set(mac_client_handle_t, mac_resource_props_t *);
166da14cebeSEric Cheng 
167da14cebeSEric Cheng /* ARGSUSED */
168da14cebeSEric Cheng static int
i_mac_client_impl_ctor(void * buf,void * arg,int kmflag)169da14cebeSEric Cheng i_mac_client_impl_ctor(void *buf, void *arg, int kmflag)
170da14cebeSEric Cheng {
171da14cebeSEric Cheng 	int	i;
172da14cebeSEric Cheng 	mac_client_impl_t	*mcip = buf;
173da14cebeSEric Cheng 
174da14cebeSEric Cheng 	bzero(buf, MAC_CLIENT_IMPL_SIZE);
175da14cebeSEric Cheng 	mutex_init(&mcip->mci_tx_cb_lock, NULL, MUTEX_DRIVER, NULL);
176da14cebeSEric Cheng 	mcip->mci_tx_notify_cb_info.mcbi_lockp = &mcip->mci_tx_cb_lock;
177da14cebeSEric Cheng 
178da14cebeSEric Cheng 	ASSERT(mac_tx_percpu_cnt >= 0);
179da14cebeSEric Cheng 	for (i = 0; i <= mac_tx_percpu_cnt; i++) {
180da14cebeSEric Cheng 		mutex_init(&mcip->mci_tx_pcpu[i].pcpu_tx_lock, NULL,
181da14cebeSEric Cheng 		    MUTEX_DRIVER, NULL);
182da14cebeSEric Cheng 	}
183da14cebeSEric Cheng 	cv_init(&mcip->mci_tx_cv, NULL, CV_DRIVER, NULL);
184da14cebeSEric Cheng 
185da14cebeSEric Cheng 	return (0);
186da14cebeSEric Cheng }
187da14cebeSEric Cheng 
188da14cebeSEric Cheng /* ARGSUSED */
189da14cebeSEric Cheng static void
i_mac_client_impl_dtor(void * buf,void * arg)190da14cebeSEric Cheng i_mac_client_impl_dtor(void *buf, void *arg)
191da14cebeSEric Cheng {
192da14cebeSEric Cheng 	int	i;
193da14cebeSEric Cheng 	mac_client_impl_t *mcip = buf;
194da14cebeSEric Cheng 
195da14cebeSEric Cheng 	ASSERT(mcip->mci_promisc_list == NULL);
196da14cebeSEric Cheng 	ASSERT(mcip->mci_unicast_list == NULL);
197da14cebeSEric Cheng 	ASSERT(mcip->mci_state_flags == 0);
198da14cebeSEric Cheng 	ASSERT(mcip->mci_tx_flag == 0);
199da14cebeSEric Cheng 
200da14cebeSEric Cheng 	mutex_destroy(&mcip->mci_tx_cb_lock);
201da14cebeSEric Cheng 
202da14cebeSEric Cheng 	ASSERT(mac_tx_percpu_cnt >= 0);
203da14cebeSEric Cheng 	for (i = 0; i <= mac_tx_percpu_cnt; i++) {
204da14cebeSEric Cheng 		ASSERT(mcip->mci_tx_pcpu[i].pcpu_tx_refcnt == 0);
205da14cebeSEric Cheng 		mutex_destroy(&mcip->mci_tx_pcpu[i].pcpu_tx_lock);
206da14cebeSEric Cheng 	}
207da14cebeSEric Cheng 	cv_destroy(&mcip->mci_tx_cv);
208da14cebeSEric Cheng }
209da14cebeSEric Cheng 
210da14cebeSEric Cheng /* ARGSUSED */
211da14cebeSEric Cheng static int
i_mac_promisc_impl_ctor(void * buf,void * arg,int kmflag)212da14cebeSEric Cheng i_mac_promisc_impl_ctor(void *buf, void *arg, int kmflag)
213da14cebeSEric Cheng {
214da14cebeSEric Cheng 	mac_promisc_impl_t	*mpip = buf;
215da14cebeSEric Cheng 
216da14cebeSEric Cheng 	bzero(buf, sizeof (mac_promisc_impl_t));
217da14cebeSEric Cheng 	mpip->mpi_mci_link.mcb_objp = buf;
218da14cebeSEric Cheng 	mpip->mpi_mci_link.mcb_objsize = sizeof (mac_promisc_impl_t);
219da14cebeSEric Cheng 	mpip->mpi_mi_link.mcb_objp = buf;
220da14cebeSEric Cheng 	mpip->mpi_mi_link.mcb_objsize = sizeof (mac_promisc_impl_t);
221da14cebeSEric Cheng 	return (0);
222da14cebeSEric Cheng }
223da14cebeSEric Cheng 
224da14cebeSEric Cheng /* ARGSUSED */
225da14cebeSEric Cheng static void
i_mac_promisc_impl_dtor(void * buf,void * arg)226da14cebeSEric Cheng i_mac_promisc_impl_dtor(void *buf, void *arg)
227da14cebeSEric Cheng {
228da14cebeSEric Cheng 	mac_promisc_impl_t	*mpip = buf;
229da14cebeSEric Cheng 
230da14cebeSEric Cheng 	ASSERT(mpip->mpi_mci_link.mcb_objp != NULL);
231da14cebeSEric Cheng 	ASSERT(mpip->mpi_mci_link.mcb_objsize == sizeof (mac_promisc_impl_t));
232da14cebeSEric Cheng 	ASSERT(mpip->mpi_mi_link.mcb_objp == mpip->mpi_mci_link.mcb_objp);
233da14cebeSEric Cheng 	ASSERT(mpip->mpi_mi_link.mcb_objsize == sizeof (mac_promisc_impl_t));
234da14cebeSEric Cheng 
235da14cebeSEric Cheng 	mpip->mpi_mci_link.mcb_objp = NULL;
236da14cebeSEric Cheng 	mpip->mpi_mci_link.mcb_objsize = 0;
237da14cebeSEric Cheng 	mpip->mpi_mi_link.mcb_objp = NULL;
238da14cebeSEric Cheng 	mpip->mpi_mi_link.mcb_objsize = 0;
239da14cebeSEric Cheng 
240da14cebeSEric Cheng 	ASSERT(mpip->mpi_mci_link.mcb_flags == 0);
241da14cebeSEric Cheng 	mpip->mpi_mci_link.mcb_objsize = 0;
242da14cebeSEric Cheng }
243da14cebeSEric Cheng 
244da14cebeSEric Cheng void
mac_client_init(void)245da14cebeSEric Cheng mac_client_init(void)
246da14cebeSEric Cheng {
247da14cebeSEric Cheng 	ASSERT(mac_tx_percpu_cnt >= 0);
248da14cebeSEric Cheng 
249da14cebeSEric Cheng 	mac_client_impl_cache = kmem_cache_create("mac_client_impl_cache",
250da14cebeSEric Cheng 	    MAC_CLIENT_IMPL_SIZE, 0, i_mac_client_impl_ctor,
251da14cebeSEric Cheng 	    i_mac_client_impl_dtor, NULL, NULL, NULL, 0);
252da14cebeSEric Cheng 	ASSERT(mac_client_impl_cache != NULL);
253da14cebeSEric Cheng 
254da14cebeSEric Cheng 	mac_promisc_impl_cache = kmem_cache_create("mac_promisc_impl_cache",
255da14cebeSEric Cheng 	    sizeof (mac_promisc_impl_t), 0, i_mac_promisc_impl_ctor,
256da14cebeSEric Cheng 	    i_mac_promisc_impl_dtor, NULL, NULL, NULL, 0);
257da14cebeSEric Cheng 	ASSERT(mac_promisc_impl_cache != NULL);
258da14cebeSEric Cheng }
259da14cebeSEric Cheng 
260da14cebeSEric Cheng void
mac_client_fini(void)261da14cebeSEric Cheng mac_client_fini(void)
262da14cebeSEric Cheng {
263da14cebeSEric Cheng 	kmem_cache_destroy(mac_client_impl_cache);
264da14cebeSEric Cheng 	kmem_cache_destroy(mac_promisc_impl_cache);
265da14cebeSEric Cheng }
266da14cebeSEric Cheng 
267da14cebeSEric Cheng /*
268da14cebeSEric Cheng  * Return the lower MAC client handle from the VNIC driver for the
269da14cebeSEric Cheng  * specified VNIC MAC instance.
270da14cebeSEric Cheng  */
271da14cebeSEric Cheng mac_client_impl_t *
mac_vnic_lower(mac_impl_t * mip)272da14cebeSEric Cheng mac_vnic_lower(mac_impl_t *mip)
273da14cebeSEric Cheng {
274da14cebeSEric Cheng 	mac_capab_vnic_t cap;
275da14cebeSEric Cheng 	mac_client_impl_t *mcip;
276da14cebeSEric Cheng 
277da14cebeSEric Cheng 	VERIFY(i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, &cap));
278da14cebeSEric Cheng 	mcip = cap.mcv_mac_client_handle(cap.mcv_arg);
279da14cebeSEric Cheng 
280da14cebeSEric Cheng 	return (mcip);
281da14cebeSEric Cheng }
282da14cebeSEric Cheng 
283da14cebeSEric Cheng /*
2841a41ca23SJerry Jelinek  * Update the secondary macs
2851a41ca23SJerry Jelinek  */
2861a41ca23SJerry Jelinek void
mac_vnic_secondary_update(mac_impl_t * mip)2871a41ca23SJerry Jelinek mac_vnic_secondary_update(mac_impl_t *mip)
2881a41ca23SJerry Jelinek {
2891a41ca23SJerry Jelinek 	mac_capab_vnic_t cap;
2901a41ca23SJerry Jelinek 
2911a41ca23SJerry Jelinek 	VERIFY(i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_VNIC, &cap));
2921a41ca23SJerry Jelinek 	cap.mcv_mac_secondary_update(cap.mcv_arg);
2931a41ca23SJerry Jelinek }
2941a41ca23SJerry Jelinek 
2951a41ca23SJerry Jelinek /*
296da14cebeSEric Cheng  * Return the MAC client handle of the primary MAC client for the
297da14cebeSEric Cheng  * specified MAC instance, or NULL otherwise.
298da14cebeSEric Cheng  */
299da14cebeSEric Cheng mac_client_impl_t *
mac_primary_client_handle(mac_impl_t * mip)300da14cebeSEric Cheng mac_primary_client_handle(mac_impl_t *mip)
301da14cebeSEric Cheng {
302da14cebeSEric Cheng 	mac_client_impl_t *mcip;
303da14cebeSEric Cheng 
304da14cebeSEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC)
305da14cebeSEric Cheng 		return (mac_vnic_lower(mip));
306da14cebeSEric Cheng 
307da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
308da14cebeSEric Cheng 
309da14cebeSEric Cheng 	for (mcip = mip->mi_clients_list; mcip != NULL;
310da14cebeSEric Cheng 	    mcip = mcip->mci_client_next) {
311da14cebeSEric Cheng 		if (MCIP_DATAPATH_SETUP(mcip) && mac_is_primary_client(mcip))
312da14cebeSEric Cheng 			return (mcip);
313da14cebeSEric Cheng 	}
314da14cebeSEric Cheng 	return (NULL);
315da14cebeSEric Cheng }
316da14cebeSEric Cheng 
317da14cebeSEric Cheng /*
318da14cebeSEric Cheng  * Open a MAC specified by its MAC name.
319da14cebeSEric Cheng  */
320da14cebeSEric Cheng int
mac_open(const char * macname,mac_handle_t * mhp)321da14cebeSEric Cheng mac_open(const char *macname, mac_handle_t *mhp)
322da14cebeSEric Cheng {
323da14cebeSEric Cheng 	mac_impl_t	*mip;
324da14cebeSEric Cheng 	int		err;
325da14cebeSEric Cheng 
326da14cebeSEric Cheng 	/*
327da14cebeSEric Cheng 	 * Look up its entry in the global hash table.
328da14cebeSEric Cheng 	 */
329da14cebeSEric Cheng 	if ((err = mac_hold(macname, &mip)) != 0)
330da14cebeSEric Cheng 		return (err);
331da14cebeSEric Cheng 
332da14cebeSEric Cheng 	/*
333da14cebeSEric Cheng 	 * Hold the dip associated to the MAC to prevent it from being
334da14cebeSEric Cheng 	 * detached. For a softmac, its underlying dip is held by the
335da14cebeSEric Cheng 	 * mi_open() callback.
336da14cebeSEric Cheng 	 *
337da14cebeSEric Cheng 	 * This is done to be more tolerant with some defective drivers,
338da14cebeSEric Cheng 	 * which incorrectly handle mac_unregister() failure in their
339da14cebeSEric Cheng 	 * xxx_detach() routine. For example, some drivers ignore the
340da14cebeSEric Cheng 	 * failure of mac_unregister() and free all resources that
341da14cebeSEric Cheng 	 * that are needed for data transmition.
342da14cebeSEric Cheng 	 */
343da14cebeSEric Cheng 	e_ddi_hold_devi(mip->mi_dip);
344da14cebeSEric Cheng 
345da14cebeSEric Cheng 	if (!(mip->mi_callbacks->mc_callbacks & MC_OPEN)) {
346da14cebeSEric Cheng 		*mhp = (mac_handle_t)mip;
347da14cebeSEric Cheng 		return (0);
348da14cebeSEric Cheng 	}
349da14cebeSEric Cheng 
350da14cebeSEric Cheng 	/*
351da14cebeSEric Cheng 	 * The mac perimeter is used in both mac_open and mac_close by the
352da14cebeSEric Cheng 	 * framework to single thread the MC_OPEN/MC_CLOSE of drivers.
353da14cebeSEric Cheng 	 */
354da14cebeSEric Cheng 	i_mac_perim_enter(mip);
355da14cebeSEric Cheng 	mip->mi_oref++;
356da14cebeSEric Cheng 	if (mip->mi_oref != 1 || ((err = mip->mi_open(mip->mi_driver)) == 0)) {
357da14cebeSEric Cheng 		*mhp = (mac_handle_t)mip;
358da14cebeSEric Cheng 		i_mac_perim_exit(mip);
359da14cebeSEric Cheng 		return (0);
360da14cebeSEric Cheng 	}
361da14cebeSEric Cheng 	mip->mi_oref--;
362da14cebeSEric Cheng 	ddi_release_devi(mip->mi_dip);
363da14cebeSEric Cheng 	mac_rele(mip);
364da14cebeSEric Cheng 	i_mac_perim_exit(mip);
365da14cebeSEric Cheng 	return (err);
366da14cebeSEric Cheng }
367da14cebeSEric Cheng 
368da14cebeSEric Cheng /*
369da14cebeSEric Cheng  * Open a MAC specified by its linkid.
370da14cebeSEric Cheng  */
371da14cebeSEric Cheng int
mac_open_by_linkid(datalink_id_t linkid,mac_handle_t * mhp)372da14cebeSEric Cheng mac_open_by_linkid(datalink_id_t linkid, mac_handle_t *mhp)
373da14cebeSEric Cheng {
374da14cebeSEric Cheng 	dls_dl_handle_t	dlh;
375da14cebeSEric Cheng 	int		err;
376da14cebeSEric Cheng 
377da14cebeSEric Cheng 	if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0)
378da14cebeSEric Cheng 		return (err);
379da14cebeSEric Cheng 
380da14cebeSEric Cheng 	dls_devnet_prop_task_wait(dlh);
381da14cebeSEric Cheng 
382da14cebeSEric Cheng 	err = mac_open(dls_devnet_mac(dlh), mhp);
383da14cebeSEric Cheng 
384da14cebeSEric Cheng 	dls_devnet_rele_tmp(dlh);
385da14cebeSEric Cheng 	return (err);
386da14cebeSEric Cheng }
387da14cebeSEric Cheng 
388da14cebeSEric Cheng /*
389da14cebeSEric Cheng  * Open a MAC specified by its link name.
390da14cebeSEric Cheng  */
391da14cebeSEric Cheng int
mac_open_by_linkname(const char * link,mac_handle_t * mhp)392da14cebeSEric Cheng mac_open_by_linkname(const char *link, mac_handle_t *mhp)
393da14cebeSEric Cheng {
394da14cebeSEric Cheng 	datalink_id_t	linkid;
395da14cebeSEric Cheng 	int		err;
396da14cebeSEric Cheng 
397da14cebeSEric Cheng 	if ((err = dls_mgmt_get_linkid(link, &linkid)) != 0)
398da14cebeSEric Cheng 		return (err);
399da14cebeSEric Cheng 	return (mac_open_by_linkid(linkid, mhp));
400da14cebeSEric Cheng }
401da14cebeSEric Cheng 
402da14cebeSEric Cheng /*
403da14cebeSEric Cheng  * Close the specified MAC.
404da14cebeSEric Cheng  */
405da14cebeSEric Cheng void
mac_close(mac_handle_t mh)406da14cebeSEric Cheng mac_close(mac_handle_t mh)
407da14cebeSEric Cheng {
408da14cebeSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
409da14cebeSEric Cheng 
410da14cebeSEric Cheng 	i_mac_perim_enter(mip);
411da14cebeSEric Cheng 	/*
412da14cebeSEric Cheng 	 * The mac perimeter is used in both mac_open and mac_close by the
413da14cebeSEric Cheng 	 * framework to single thread the MC_OPEN/MC_CLOSE of drivers.
414da14cebeSEric Cheng 	 */
415da14cebeSEric Cheng 	if (mip->mi_callbacks->mc_callbacks & MC_OPEN) {
416da14cebeSEric Cheng 		ASSERT(mip->mi_oref != 0);
417da14cebeSEric Cheng 		if (--mip->mi_oref == 0) {
418da14cebeSEric Cheng 			if ((mip->mi_callbacks->mc_callbacks & MC_CLOSE))
419da14cebeSEric Cheng 				mip->mi_close(mip->mi_driver);
420da14cebeSEric Cheng 		}
421da14cebeSEric Cheng 	}
422da14cebeSEric Cheng 	i_mac_perim_exit(mip);
423da14cebeSEric Cheng 	ddi_release_devi(mip->mi_dip);
424da14cebeSEric Cheng 	mac_rele(mip);
425da14cebeSEric Cheng }
426da14cebeSEric Cheng 
427da14cebeSEric Cheng /*
428da14cebeSEric Cheng  * Misc utility functions to retrieve various information about a MAC
429da14cebeSEric Cheng  * instance or a MAC client.
430da14cebeSEric Cheng  */
431da14cebeSEric Cheng 
432da14cebeSEric Cheng const mac_info_t *
mac_info(mac_handle_t mh)433da14cebeSEric Cheng mac_info(mac_handle_t mh)
434da14cebeSEric Cheng {
435da14cebeSEric Cheng 	return (&((mac_impl_t *)mh)->mi_info);
436da14cebeSEric Cheng }
437da14cebeSEric Cheng 
438da14cebeSEric Cheng dev_info_t *
mac_devinfo_get(mac_handle_t mh)439da14cebeSEric Cheng mac_devinfo_get(mac_handle_t mh)
440da14cebeSEric Cheng {
441da14cebeSEric Cheng 	return (((mac_impl_t *)mh)->mi_dip);
442da14cebeSEric Cheng }
443da14cebeSEric Cheng 
4445d460eafSCathy Zhou void *
mac_driver(mac_handle_t mh)4455d460eafSCathy Zhou mac_driver(mac_handle_t mh)
4465d460eafSCathy Zhou {
4475d460eafSCathy Zhou 	return (((mac_impl_t *)mh)->mi_driver);
4485d460eafSCathy Zhou }
4495d460eafSCathy Zhou 
450da14cebeSEric Cheng const char *
mac_name(mac_handle_t mh)451da14cebeSEric Cheng mac_name(mac_handle_t mh)
452da14cebeSEric Cheng {
453da14cebeSEric Cheng 	return (((mac_impl_t *)mh)->mi_name);
454da14cebeSEric Cheng }
455da14cebeSEric Cheng 
4560a0e9771SDarren Reed int
mac_type(mac_handle_t mh)4570a0e9771SDarren Reed mac_type(mac_handle_t mh)
4580a0e9771SDarren Reed {
4590a0e9771SDarren Reed 	return (((mac_impl_t *)mh)->mi_type->mt_type);
4600a0e9771SDarren Reed }
4610a0e9771SDarren Reed 
462f195a053SDarren Reed int
mac_nativetype(mac_handle_t mh)463f195a053SDarren Reed mac_nativetype(mac_handle_t mh)
464f195a053SDarren Reed {
465f195a053SDarren Reed 	return (((mac_impl_t *)mh)->mi_type->mt_nativetype);
466f195a053SDarren Reed }
467f195a053SDarren Reed 
468da14cebeSEric Cheng char *
mac_client_name(mac_client_handle_t mch)469da14cebeSEric Cheng mac_client_name(mac_client_handle_t mch)
470da14cebeSEric Cheng {
471da14cebeSEric Cheng 	return (((mac_client_impl_t *)mch)->mci_name);
472da14cebeSEric Cheng }
473da14cebeSEric Cheng 
474da14cebeSEric Cheng minor_t
mac_minor(mac_handle_t mh)475da14cebeSEric Cheng mac_minor(mac_handle_t mh)
476da14cebeSEric Cheng {
477da14cebeSEric Cheng 	return (((mac_impl_t *)mh)->mi_minor);
478da14cebeSEric Cheng }
479da14cebeSEric Cheng 
480da14cebeSEric Cheng /*
481da14cebeSEric Cheng  * Return the VID associated with a MAC client. This function should
482da14cebeSEric Cheng  * be called for clients which are associated with only one VID.
483da14cebeSEric Cheng  */
484da14cebeSEric Cheng uint16_t
mac_client_vid(mac_client_handle_t mch)485da14cebeSEric Cheng mac_client_vid(mac_client_handle_t mch)
486da14cebeSEric Cheng {
487da14cebeSEric Cheng 	uint16_t		vid = VLAN_ID_NONE;
488da14cebeSEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
489da14cebeSEric Cheng 	flow_desc_t		flow_desc;
490da14cebeSEric Cheng 
491da14cebeSEric Cheng 	if (mcip->mci_nflents == 0)
492da14cebeSEric Cheng 		return (vid);
493da14cebeSEric Cheng 
494da14cebeSEric Cheng 	ASSERT(MCIP_DATAPATH_SETUP(mcip) && mac_client_single_rcvr(mcip));
495da14cebeSEric Cheng 
496da14cebeSEric Cheng 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
497da14cebeSEric Cheng 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
498da14cebeSEric Cheng 		vid = flow_desc.fd_vid;
499da14cebeSEric Cheng 
500da14cebeSEric Cheng 	return (vid);
501da14cebeSEric Cheng }
502da14cebeSEric Cheng 
503da14cebeSEric Cheng /*
50472782355SNicolas Droux  * Return whether the specified MAC client corresponds to a VLAN VNIC.
50572782355SNicolas Droux  */
50672782355SNicolas Droux boolean_t
mac_client_is_vlan_vnic(mac_client_handle_t mch)50772782355SNicolas Droux mac_client_is_vlan_vnic(mac_client_handle_t mch)
50872782355SNicolas Droux {
50972782355SNicolas Droux 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
51072782355SNicolas Droux 
51172782355SNicolas Droux 	return (((mcip->mci_state_flags & MCIS_IS_VNIC) != 0) &&
51272782355SNicolas Droux 	    ((mcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) != 0));
51372782355SNicolas Droux }
51472782355SNicolas Droux 
51572782355SNicolas Droux /*
516da14cebeSEric Cheng  * Return the link speed associated with the specified MAC client.
517da14cebeSEric Cheng  *
518da14cebeSEric Cheng  * The link speed of a MAC client is equal to the smallest value of
519da14cebeSEric Cheng  * 1) the current link speed of the underlying NIC, or
520da14cebeSEric Cheng  * 2) the bandwidth limit set for the MAC client.
521da14cebeSEric Cheng  *
522da14cebeSEric Cheng  * Note that the bandwidth limit can be higher than the speed
523da14cebeSEric Cheng  * of the underlying NIC. This is allowed to avoid spurious
524da14cebeSEric Cheng  * administration action failures or artifically lowering the
525da14cebeSEric Cheng  * bandwidth limit of a link that may  have temporarily lowered
526da14cebeSEric Cheng  * its link speed due to hardware problem or administrator action.
527da14cebeSEric Cheng  */
528da14cebeSEric Cheng static uint64_t
mac_client_ifspeed(mac_client_impl_t * mcip)529da14cebeSEric Cheng mac_client_ifspeed(mac_client_impl_t *mcip)
530da14cebeSEric Cheng {
531da14cebeSEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
532da14cebeSEric Cheng 	uint64_t nic_speed;
533da14cebeSEric Cheng 
534da14cebeSEric Cheng 	nic_speed = mac_stat_get((mac_handle_t)mip, MAC_STAT_IFSPEED);
535da14cebeSEric Cheng 
536da14cebeSEric Cheng 	if (nic_speed == 0) {
537da14cebeSEric Cheng 		return (0);
538da14cebeSEric Cheng 	} else {
539da14cebeSEric Cheng 		uint64_t policy_limit = (uint64_t)-1;
540da14cebeSEric Cheng 
541da14cebeSEric Cheng 		if (MCIP_RESOURCE_PROPS_MASK(mcip) & MRP_MAXBW)
542da14cebeSEric Cheng 			policy_limit = MCIP_RESOURCE_PROPS_MAXBW(mcip);
543da14cebeSEric Cheng 
544da14cebeSEric Cheng 		return (MIN(policy_limit, nic_speed));
545da14cebeSEric Cheng 	}
546da14cebeSEric Cheng }
547da14cebeSEric Cheng 
548da14cebeSEric Cheng /*
549da14cebeSEric Cheng  * Return the link state of the specified client. If here are more
550da14cebeSEric Cheng  * than one clients of the underying mac_impl_t, the link state
551da14cebeSEric Cheng  * will always be UP regardless of the link state of the underlying
552da14cebeSEric Cheng  * mac_impl_t. This is needed to allow the MAC clients to continue
553da14cebeSEric Cheng  * to communicate with each other even when the physical link of
554da14cebeSEric Cheng  * their mac_impl_t is down.
555da14cebeSEric Cheng  */
556da14cebeSEric Cheng static uint64_t
mac_client_link_state(mac_client_impl_t * mcip)557da14cebeSEric Cheng mac_client_link_state(mac_client_impl_t *mcip)
558da14cebeSEric Cheng {
559da14cebeSEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
560da14cebeSEric Cheng 	uint16_t vid;
561da14cebeSEric Cheng 	mac_client_impl_t *mci_list;
562da14cebeSEric Cheng 	mac_unicast_impl_t *mui_list, *oth_mui_list;
563da14cebeSEric Cheng 
564da14cebeSEric Cheng 	/*
565da14cebeSEric Cheng 	 * Returns LINK_STATE_UP if there are other MAC clients defined on
566da14cebeSEric Cheng 	 * mac_impl_t which share same VLAN ID as that of mcip. Note that
567da14cebeSEric Cheng 	 * if 'mcip' has more than one VID's then we match ANY one of the
568da14cebeSEric Cheng 	 * VID's with other MAC client's VID's and return LINK_STATE_UP.
569da14cebeSEric Cheng 	 */
570da14cebeSEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_READER);
571da14cebeSEric Cheng 	for (mui_list = mcip->mci_unicast_list; mui_list != NULL;
572da14cebeSEric Cheng 	    mui_list = mui_list->mui_next) {
573da14cebeSEric Cheng 		vid = mui_list->mui_vid;
574da14cebeSEric Cheng 		for (mci_list = mip->mi_clients_list; mci_list != NULL;
575da14cebeSEric Cheng 		    mci_list = mci_list->mci_client_next) {
576da14cebeSEric Cheng 			if (mci_list == mcip)
577da14cebeSEric Cheng 				continue;
578da14cebeSEric Cheng 			for (oth_mui_list = mci_list->mci_unicast_list;
579da14cebeSEric Cheng 			    oth_mui_list != NULL; oth_mui_list = oth_mui_list->
580da14cebeSEric Cheng 			    mui_next) {
581da14cebeSEric Cheng 				if (vid == oth_mui_list->mui_vid) {
582da14cebeSEric Cheng 					rw_exit(&mcip->mci_rw_lock);
583da14cebeSEric Cheng 					return (LINK_STATE_UP);
584da14cebeSEric Cheng 				}
585da14cebeSEric Cheng 			}
586da14cebeSEric Cheng 		}
587da14cebeSEric Cheng 	}
588da14cebeSEric Cheng 	rw_exit(&mcip->mci_rw_lock);
589da14cebeSEric Cheng 
590da14cebeSEric Cheng 	return (mac_stat_get((mac_handle_t)mip, MAC_STAT_LINK_STATE));
591da14cebeSEric Cheng }
592da14cebeSEric Cheng 
593da14cebeSEric Cheng /*
5940dc2366fSVenugopal Iyer  * These statistics are consumed by dladm show-link -s <vnic>,
5950dc2366fSVenugopal Iyer  * dladm show-vnic -s and netstat. With the introduction of dlstat,
5960dc2366fSVenugopal Iyer  * dladm show-link -s and dladm show-vnic -s witll be EOL'ed while
5970dc2366fSVenugopal Iyer  * netstat will consume from kstats introduced for dlstat. This code
5980dc2366fSVenugopal Iyer  * will be removed at that time.
5990dc2366fSVenugopal Iyer  */
6000dc2366fSVenugopal Iyer 
6010dc2366fSVenugopal Iyer /*
602da14cebeSEric Cheng  * Return the statistics of a MAC client. These statistics are different
603da14cebeSEric Cheng  * then the statistics of the underlying MAC which are returned by
604da14cebeSEric Cheng  * mac_stat_get().
605d47ced1fSRobert Mustacchi  *
606d47ced1fSRobert Mustacchi  * Note that for things based on the tx and rx stats, mac will end up clobbering
607d47ced1fSRobert Mustacchi  * those stats when the underlying set of rings in the srs changes. As such, we
608d47ced1fSRobert Mustacchi  * need to source not only the current set, but also the historical set when
609d47ced1fSRobert Mustacchi  * returning to the client, lest our counters appear to go backwards.
610da14cebeSEric Cheng  */
611da14cebeSEric Cheng uint64_t
mac_client_stat_get(mac_client_handle_t mch,uint_t stat)612da14cebeSEric Cheng mac_client_stat_get(mac_client_handle_t mch, uint_t stat)
613da14cebeSEric Cheng {
614da14cebeSEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
615da14cebeSEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
6160dc2366fSVenugopal Iyer 	flow_entry_t		*flent = mcip->mci_flent;
6170dc2366fSVenugopal Iyer 	mac_soft_ring_set_t	*mac_srs;
618d47ced1fSRobert Mustacchi 	mac_rx_stats_t		*mac_rx_stat, *old_rx_stat;
619d47ced1fSRobert Mustacchi 	mac_tx_stats_t		*mac_tx_stat, *old_tx_stat;
6200dc2366fSVenugopal Iyer 	int i;
6210dc2366fSVenugopal Iyer 	uint64_t val = 0;
6220dc2366fSVenugopal Iyer 
6230dc2366fSVenugopal Iyer 	mac_srs = (mac_soft_ring_set_t *)(flent->fe_tx_srs);
6240dc2366fSVenugopal Iyer 	mac_tx_stat = &mac_srs->srs_tx.st_stat;
625d47ced1fSRobert Mustacchi 	old_rx_stat = &mcip->mci_misc_stat.mms_defunctrxlanestats;
626d47ced1fSRobert Mustacchi 	old_tx_stat = &mcip->mci_misc_stat.mms_defuncttxlanestats;
627da14cebeSEric Cheng 
628da14cebeSEric Cheng 	switch (stat) {
629da14cebeSEric Cheng 	case MAC_STAT_LINK_STATE:
630da14cebeSEric Cheng 		val = mac_client_link_state(mcip);
631da14cebeSEric Cheng 		break;
632da14cebeSEric Cheng 	case MAC_STAT_LINK_UP:
633da14cebeSEric Cheng 		val = (mac_client_link_state(mcip) == LINK_STATE_UP);
634da14cebeSEric Cheng 		break;
635da14cebeSEric Cheng 	case MAC_STAT_PROMISC:
636da14cebeSEric Cheng 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_PROMISC);
637da14cebeSEric Cheng 		break;
6384eaa4710SRishi Srivatsavai 	case MAC_STAT_LOWLINK_STATE:
6394eaa4710SRishi Srivatsavai 		val = mac_stat_get((mac_handle_t)mip, MAC_STAT_LOWLINK_STATE);
6404eaa4710SRishi Srivatsavai 		break;
641da14cebeSEric Cheng 	case MAC_STAT_IFSPEED:
642da14cebeSEric Cheng 		val = mac_client_ifspeed(mcip);
643da14cebeSEric Cheng 		break;
644da14cebeSEric Cheng 	case MAC_STAT_MULTIRCV:
6450dc2366fSVenugopal Iyer 		val = mcip->mci_misc_stat.mms_multircv;
646da14cebeSEric Cheng 		break;
647da14cebeSEric Cheng 	case MAC_STAT_BRDCSTRCV:
6480dc2366fSVenugopal Iyer 		val = mcip->mci_misc_stat.mms_brdcstrcv;
649da14cebeSEric Cheng 		break;
650da14cebeSEric Cheng 	case MAC_STAT_MULTIXMT:
6510dc2366fSVenugopal Iyer 		val = mcip->mci_misc_stat.mms_multixmt;
652da14cebeSEric Cheng 		break;
653da14cebeSEric Cheng 	case MAC_STAT_BRDCSTXMT:
6540dc2366fSVenugopal Iyer 		val = mcip->mci_misc_stat.mms_brdcstxmt;
655da14cebeSEric Cheng 		break;
656da14cebeSEric Cheng 	case MAC_STAT_OBYTES:
6570dc2366fSVenugopal Iyer 		val = mac_tx_stat->mts_obytes;
658d47ced1fSRobert Mustacchi 		val += old_tx_stat->mts_obytes;
659da14cebeSEric Cheng 		break;
660da14cebeSEric Cheng 	case MAC_STAT_OPACKETS:
6610dc2366fSVenugopal Iyer 		val = mac_tx_stat->mts_opackets;
662d47ced1fSRobert Mustacchi 		val += old_tx_stat->mts_opackets;
663da14cebeSEric Cheng 		break;
664da14cebeSEric Cheng 	case MAC_STAT_OERRORS:
6650dc2366fSVenugopal Iyer 		val = mac_tx_stat->mts_oerrors;
666d47ced1fSRobert Mustacchi 		val += old_tx_stat->mts_oerrors;
667da14cebeSEric Cheng 		break;
668da14cebeSEric Cheng 	case MAC_STAT_IPACKETS:
6690dc2366fSVenugopal Iyer 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
6700dc2366fSVenugopal Iyer 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
6710dc2366fSVenugopal Iyer 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
6720dc2366fSVenugopal Iyer 			val += mac_rx_stat->mrs_intrcnt +
6730dc2366fSVenugopal Iyer 			    mac_rx_stat->mrs_pollcnt + mac_rx_stat->mrs_lclcnt;
6740dc2366fSVenugopal Iyer 		}
675d47ced1fSRobert Mustacchi 		val += old_rx_stat->mrs_intrcnt + old_rx_stat->mrs_pollcnt +
676d47ced1fSRobert Mustacchi 		    old_rx_stat->mrs_lclcnt;
677da14cebeSEric Cheng 		break;
678da14cebeSEric Cheng 	case MAC_STAT_RBYTES:
6790dc2366fSVenugopal Iyer 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
6800dc2366fSVenugopal Iyer 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
6810dc2366fSVenugopal Iyer 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
6820dc2366fSVenugopal Iyer 			val += mac_rx_stat->mrs_intrbytes +
6830dc2366fSVenugopal Iyer 			    mac_rx_stat->mrs_pollbytes +
6840dc2366fSVenugopal Iyer 			    mac_rx_stat->mrs_lclbytes;
6850dc2366fSVenugopal Iyer 		}
686d47ced1fSRobert Mustacchi 		val += old_rx_stat->mrs_intrbytes + old_rx_stat->mrs_pollbytes +
687d47ced1fSRobert Mustacchi 		    old_rx_stat->mrs_lclbytes;
688da14cebeSEric Cheng 		break;
689da14cebeSEric Cheng 	case MAC_STAT_IERRORS:
6900dc2366fSVenugopal Iyer 		for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
6910dc2366fSVenugopal Iyer 			mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
6920dc2366fSVenugopal Iyer 			mac_rx_stat = &mac_srs->srs_rx.sr_stat;
6930dc2366fSVenugopal Iyer 			val += mac_rx_stat->mrs_ierrors;
6940dc2366fSVenugopal Iyer 		}
695d47ced1fSRobert Mustacchi 		val += old_rx_stat->mrs_ierrors;
696da14cebeSEric Cheng 		break;
697da14cebeSEric Cheng 	default:
6980dc2366fSVenugopal Iyer 		val = mac_driver_stat_default(mip, stat);
699da14cebeSEric Cheng 		break;
700da14cebeSEric Cheng 	}
701da14cebeSEric Cheng 
702da14cebeSEric Cheng 	return (val);
703da14cebeSEric Cheng }
704da14cebeSEric Cheng 
705da14cebeSEric Cheng /*
706da14cebeSEric Cheng  * Return the statistics of the specified MAC instance.
707da14cebeSEric Cheng  */
708da14cebeSEric Cheng uint64_t
mac_stat_get(mac_handle_t mh,uint_t stat)709da14cebeSEric Cheng mac_stat_get(mac_handle_t mh, uint_t stat)
710da14cebeSEric Cheng {
711da14cebeSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
712da14cebeSEric Cheng 	uint64_t	val;
713da14cebeSEric Cheng 	int		ret;
714da14cebeSEric Cheng 
715da14cebeSEric Cheng 	/*
716da14cebeSEric Cheng 	 * The range of stat determines where it is maintained.  Stat
717da14cebeSEric Cheng 	 * values from 0 up to (but not including) MAC_STAT_MIN are
718da14cebeSEric Cheng 	 * mainteined by the mac module itself.  Everything else is
719da14cebeSEric Cheng 	 * maintained by the driver.
720da14cebeSEric Cheng 	 *
721da14cebeSEric Cheng 	 * If the mac_impl_t being queried corresponds to a VNIC,
722da14cebeSEric Cheng 	 * the stats need to be queried from the lower MAC client
723da14cebeSEric Cheng 	 * corresponding to the VNIC. (The mac_link_update()
724da14cebeSEric Cheng 	 * invoked by the driver to the lower MAC causes the *lower
725da14cebeSEric Cheng 	 * MAC* to update its mi_linkstate, and send a notification
726da14cebeSEric Cheng 	 * to its MAC clients. Due to the VNIC passthrough,
727da14cebeSEric Cheng 	 * these notifications are sent to the upper MAC clients
728da14cebeSEric Cheng 	 * of the VNIC directly, and the upper mac_impl_t of the VNIC
729da14cebeSEric Cheng 	 * does not have a valid mi_linkstate.
730da14cebeSEric Cheng 	 */
731da14cebeSEric Cheng 	if (stat < MAC_STAT_MIN && !(mip->mi_state_flags & MIS_IS_VNIC)) {
732da14cebeSEric Cheng 		/* these stats are maintained by the mac module itself */
733da14cebeSEric Cheng 		switch (stat) {
734da14cebeSEric Cheng 		case MAC_STAT_LINK_STATE:
735da14cebeSEric Cheng 			return (mip->mi_linkstate);
736da14cebeSEric Cheng 		case MAC_STAT_LINK_UP:
737da14cebeSEric Cheng 			return (mip->mi_linkstate == LINK_STATE_UP);
738da14cebeSEric Cheng 		case MAC_STAT_PROMISC:
739da14cebeSEric Cheng 			return (mip->mi_devpromisc != 0);
7404eaa4710SRishi Srivatsavai 		case MAC_STAT_LOWLINK_STATE:
7414eaa4710SRishi Srivatsavai 			return (mip->mi_lowlinkstate);
742da14cebeSEric Cheng 		default:
743da14cebeSEric Cheng 			ASSERT(B_FALSE);
744da14cebeSEric Cheng 		}
745da14cebeSEric Cheng 	}
746da14cebeSEric Cheng 
747da14cebeSEric Cheng 	/*
748da14cebeSEric Cheng 	 * Call the driver to get the given statistic.
749da14cebeSEric Cheng 	 */
750da14cebeSEric Cheng 	ret = mip->mi_getstat(mip->mi_driver, stat, &val);
751da14cebeSEric Cheng 	if (ret != 0) {
752da14cebeSEric Cheng 		/*
753da14cebeSEric Cheng 		 * The driver doesn't support this statistic.  Get the
754da14cebeSEric Cheng 		 * statistic's default value.
755da14cebeSEric Cheng 		 */
7560dc2366fSVenugopal Iyer 		val = mac_driver_stat_default(mip, stat);
757da14cebeSEric Cheng 	}
758da14cebeSEric Cheng 	return (val);
759da14cebeSEric Cheng }
760da14cebeSEric Cheng 
761da14cebeSEric Cheng /*
7620dc2366fSVenugopal Iyer  * Query hardware rx ring corresponding to the pseudo ring.
7630dc2366fSVenugopal Iyer  */
7640dc2366fSVenugopal Iyer uint64_t
mac_pseudo_rx_ring_stat_get(mac_ring_handle_t handle,uint_t stat)7650dc2366fSVenugopal Iyer mac_pseudo_rx_ring_stat_get(mac_ring_handle_t handle, uint_t stat)
7660dc2366fSVenugopal Iyer {
7670dc2366fSVenugopal Iyer 	return (mac_rx_ring_stat_get(handle, stat));
7680dc2366fSVenugopal Iyer }
7690dc2366fSVenugopal Iyer 
7700dc2366fSVenugopal Iyer /*
7710dc2366fSVenugopal Iyer  * Query hardware tx ring corresponding to the pseudo ring.
7720dc2366fSVenugopal Iyer  */
7730dc2366fSVenugopal Iyer uint64_t
mac_pseudo_tx_ring_stat_get(mac_ring_handle_t handle,uint_t stat)7740dc2366fSVenugopal Iyer mac_pseudo_tx_ring_stat_get(mac_ring_handle_t handle, uint_t stat)
7750dc2366fSVenugopal Iyer {
7760dc2366fSVenugopal Iyer 	return (mac_tx_ring_stat_get(handle, stat));
7770dc2366fSVenugopal Iyer }
7780dc2366fSVenugopal Iyer 
7790dc2366fSVenugopal Iyer /*
780da14cebeSEric Cheng  * Utility function which returns the VID associated with a flow entry.
781da14cebeSEric Cheng  */
782da14cebeSEric Cheng uint16_t
i_mac_flow_vid(flow_entry_t * flent)783da14cebeSEric Cheng i_mac_flow_vid(flow_entry_t *flent)
784da14cebeSEric Cheng {
785da14cebeSEric Cheng 	flow_desc_t	flow_desc;
786da14cebeSEric Cheng 
787da14cebeSEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
788da14cebeSEric Cheng 
789da14cebeSEric Cheng 	if ((flow_desc.fd_mask & FLOW_LINK_VID) != 0)
790da14cebeSEric Cheng 		return (flow_desc.fd_vid);
791da14cebeSEric Cheng 	return (VLAN_ID_NONE);
792da14cebeSEric Cheng }
793da14cebeSEric Cheng 
794da14cebeSEric Cheng /*
795da14cebeSEric Cheng  * Verify the validity of the specified unicast MAC address. Returns B_TRUE
796da14cebeSEric Cheng  * if the address is valid, B_FALSE otherwise (multicast address, or incorrect
797da14cebeSEric Cheng  * length.
798da14cebeSEric Cheng  */
799da14cebeSEric Cheng boolean_t
mac_unicst_verify(mac_handle_t mh,const uint8_t * addr,uint_t len)800da14cebeSEric Cheng mac_unicst_verify(mac_handle_t mh, const uint8_t *addr, uint_t len)
801da14cebeSEric Cheng {
802da14cebeSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
803da14cebeSEric Cheng 
804da14cebeSEric Cheng 	/*
805da14cebeSEric Cheng 	 * Verify the address. No lock is needed since mi_type and plugin
806da14cebeSEric Cheng 	 * details don't change after mac_register().
807da14cebeSEric Cheng 	 */
808da14cebeSEric Cheng 	if ((len != mip->mi_type->mt_addr_length) ||
809da14cebeSEric Cheng 	    (mip->mi_type->mt_ops.mtops_unicst_verify(addr,
810da14cebeSEric Cheng 	    mip->mi_pdata)) != 0) {
811da14cebeSEric Cheng 		return (B_FALSE);
812da14cebeSEric Cheng 	} else {
813da14cebeSEric Cheng 		return (B_TRUE);
814da14cebeSEric Cheng 	}
815da14cebeSEric Cheng }
816da14cebeSEric Cheng 
817da14cebeSEric Cheng void
mac_sdu_get(mac_handle_t mh,uint_t * min_sdu,uint_t * max_sdu)818da14cebeSEric Cheng mac_sdu_get(mac_handle_t mh, uint_t *min_sdu, uint_t *max_sdu)
819da14cebeSEric Cheng {
820da14cebeSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
821da14cebeSEric Cheng 
822da14cebeSEric Cheng 	if (min_sdu != NULL)
823da14cebeSEric Cheng 		*min_sdu = mip->mi_sdu_min;
824da14cebeSEric Cheng 	if (max_sdu != NULL)
825da14cebeSEric Cheng 		*max_sdu = mip->mi_sdu_max;
826da14cebeSEric Cheng }
827da14cebeSEric Cheng 
8281eee170aSErik Nordmark void
mac_sdu_get2(mac_handle_t mh,uint_t * min_sdu,uint_t * max_sdu,uint_t * multicast_sdu)8291eee170aSErik Nordmark mac_sdu_get2(mac_handle_t mh, uint_t *min_sdu, uint_t *max_sdu,
8301eee170aSErik Nordmark     uint_t *multicast_sdu)
8311eee170aSErik Nordmark {
8321eee170aSErik Nordmark 	mac_impl_t	*mip = (mac_impl_t *)mh;
8331eee170aSErik Nordmark 
8341eee170aSErik Nordmark 	if (min_sdu != NULL)
8351eee170aSErik Nordmark 		*min_sdu = mip->mi_sdu_min;
8361eee170aSErik Nordmark 	if (max_sdu != NULL)
8371eee170aSErik Nordmark 		*max_sdu = mip->mi_sdu_max;
8381eee170aSErik Nordmark 	if (multicast_sdu != NULL)
8391eee170aSErik Nordmark 		*multicast_sdu = mip->mi_sdu_multicast;
8401eee170aSErik Nordmark }
8411eee170aSErik Nordmark 
842da14cebeSEric Cheng /*
843da14cebeSEric Cheng  * Update the MAC unicast address of the specified client's flows. Currently
844da14cebeSEric Cheng  * only one unicast MAC unicast address is allowed per client.
845da14cebeSEric Cheng  */
846da14cebeSEric Cheng static void
mac_unicast_update_client_flow(mac_client_impl_t * mcip)847da14cebeSEric Cheng mac_unicast_update_client_flow(mac_client_impl_t *mcip)
848da14cebeSEric Cheng {
849da14cebeSEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
850da14cebeSEric Cheng 	flow_entry_t *flent = mcip->mci_flent;
851da14cebeSEric Cheng 	mac_address_t *map = mcip->mci_unicast;
852da14cebeSEric Cheng 	flow_desc_t flow_desc;
853da14cebeSEric Cheng 
854da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
855da14cebeSEric Cheng 	ASSERT(flent != NULL);
856da14cebeSEric Cheng 
857da14cebeSEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
858da14cebeSEric Cheng 	ASSERT(flow_desc.fd_mask & FLOW_LINK_DST);
859da14cebeSEric Cheng 
860da14cebeSEric Cheng 	bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
861da14cebeSEric Cheng 	mac_flow_set_desc(flent, &flow_desc);
862da14cebeSEric Cheng 
863da14cebeSEric Cheng 	/*
864797f979dSCody Peter Mello 	 * The v6 local and SLAAC addrs (used by mac protection) need to be
8650dc2366fSVenugopal Iyer 	 * regenerated because our mac address has changed.
8660dc2366fSVenugopal Iyer 	 */
867797f979dSCody Peter Mello 	mac_protect_update_mac_token(mcip);
8680dc2366fSVenugopal Iyer 
8690dc2366fSVenugopal Iyer 	/*
87084de666eSRyan Zezeski 	 * When there are multiple VLANs sharing the same MAC address,
87184de666eSRyan Zezeski 	 * each gets its own MAC client, except when running on sun4v
87284de666eSRyan Zezeski 	 * vsw. In that case the mci_flent_list is used to place
87384de666eSRyan Zezeski 	 * multiple VLAN flows on one MAC client. If we ever get rid
87484de666eSRyan Zezeski 	 * of vsw then this code can go, but until then we need to
87584de666eSRyan Zezeski 	 * update all flow entries.
876da14cebeSEric Cheng 	 */
877da14cebeSEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
878da14cebeSEric Cheng 	    flent = flent->fe_client_next) {
879da14cebeSEric Cheng 		mac_flow_get_desc(flent, &flow_desc);
880da14cebeSEric Cheng 		if (!(flent->fe_type & FLOW_PRIMARY_MAC ||
881da14cebeSEric Cheng 		    flent->fe_type & FLOW_VNIC_MAC))
882da14cebeSEric Cheng 			continue;
883da14cebeSEric Cheng 
884da14cebeSEric Cheng 		bcopy(map->ma_addr, flow_desc.fd_dst_mac, map->ma_len);
885da14cebeSEric Cheng 		mac_flow_set_desc(flent, &flow_desc);
886da14cebeSEric Cheng 	}
887da14cebeSEric Cheng }
888da14cebeSEric Cheng 
889da14cebeSEric Cheng /*
890da14cebeSEric Cheng  * Update all clients that share the same unicast address.
891da14cebeSEric Cheng  */
892da14cebeSEric Cheng void
mac_unicast_update_clients(mac_impl_t * mip,mac_address_t * map)893da14cebeSEric Cheng mac_unicast_update_clients(mac_impl_t *mip, mac_address_t *map)
894da14cebeSEric Cheng {
895da14cebeSEric Cheng 	mac_client_impl_t *mcip;
896da14cebeSEric Cheng 
897da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
898da14cebeSEric Cheng 
899da14cebeSEric Cheng 	/*
900da14cebeSEric Cheng 	 * Find all clients that share the same unicast MAC address and update
901da14cebeSEric Cheng 	 * them appropriately.
902da14cebeSEric Cheng 	 */
903da14cebeSEric Cheng 	for (mcip = mip->mi_clients_list; mcip != NULL;
904da14cebeSEric Cheng 	    mcip = mcip->mci_client_next) {
905da14cebeSEric Cheng 		/*
906da14cebeSEric Cheng 		 * Ignore clients that don't share this MAC address.
907da14cebeSEric Cheng 		 */
908da14cebeSEric Cheng 		if (map != mcip->mci_unicast)
909da14cebeSEric Cheng 			continue;
910da14cebeSEric Cheng 
911da14cebeSEric Cheng 		/*
912da14cebeSEric Cheng 		 * Update those clients with same old unicast MAC address.
913da14cebeSEric Cheng 		 */
914da14cebeSEric Cheng 		mac_unicast_update_client_flow(mcip);
915da14cebeSEric Cheng 	}
916da14cebeSEric Cheng }
917da14cebeSEric Cheng 
918da14cebeSEric Cheng /*
919da14cebeSEric Cheng  * Update the unicast MAC address of the specified VNIC MAC client.
920da14cebeSEric Cheng  *
921da14cebeSEric Cheng  * Check whether the operation is valid. Any of following cases should fail:
922da14cebeSEric Cheng  *
923da14cebeSEric Cheng  * 1. It's a VLAN type of VNIC.
924da14cebeSEric Cheng  * 2. The new value is current "primary" MAC address.
925da14cebeSEric Cheng  * 3. The current MAC address is shared with other clients.
926da14cebeSEric Cheng  * 4. The new MAC address has been used. This case will be valid when
927da14cebeSEric Cheng  *    client migration is fully supported.
928da14cebeSEric Cheng  */
929da14cebeSEric Cheng int
mac_vnic_unicast_set(mac_client_handle_t mch,const uint8_t * addr)930da14cebeSEric Cheng mac_vnic_unicast_set(mac_client_handle_t mch, const uint8_t *addr)
931da14cebeSEric Cheng {
932da14cebeSEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
933da14cebeSEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
934da14cebeSEric Cheng 	mac_address_t *map = mcip->mci_unicast;
935da14cebeSEric Cheng 	int err;
936da14cebeSEric Cheng 
937da14cebeSEric Cheng 	ASSERT(!(mip->mi_state_flags & MIS_IS_VNIC));
938da14cebeSEric Cheng 	ASSERT(mcip->mci_state_flags & MCIS_IS_VNIC);
939da14cebeSEric Cheng 	ASSERT(mcip->mci_flags != MAC_CLIENT_FLAGS_PRIMARY);
940da14cebeSEric Cheng 
941da14cebeSEric Cheng 	i_mac_perim_enter(mip);
942da14cebeSEric Cheng 
943da14cebeSEric Cheng 	/*
944da14cebeSEric Cheng 	 * If this is a VLAN type of VNIC, it's using "primary" MAC address
945da14cebeSEric Cheng 	 * of the underlying interface. Must fail here. Refer to case 1 above.
946da14cebeSEric Cheng 	 */
947da14cebeSEric Cheng 	if (bcmp(map->ma_addr, mip->mi_addr, map->ma_len) == 0) {
948da14cebeSEric Cheng 		i_mac_perim_exit(mip);
949da14cebeSEric Cheng 		return (ENOTSUP);
950da14cebeSEric Cheng 	}
951da14cebeSEric Cheng 
952da14cebeSEric Cheng 	/*
953da14cebeSEric Cheng 	 * If the new address is the "primary" one, must fail. Refer to
954da14cebeSEric Cheng 	 * case 2 above.
955da14cebeSEric Cheng 	 */
956da14cebeSEric Cheng 	if (bcmp(addr, mip->mi_addr, map->ma_len) == 0) {
957da14cebeSEric Cheng 		i_mac_perim_exit(mip);
958da14cebeSEric Cheng 		return (EACCES);
959da14cebeSEric Cheng 	}
960da14cebeSEric Cheng 
961da14cebeSEric Cheng 	/*
962da14cebeSEric Cheng 	 * If the address is shared by multiple clients, must fail. Refer
963da14cebeSEric Cheng 	 * to case 3 above.
964da14cebeSEric Cheng 	 */
965da14cebeSEric Cheng 	if (mac_check_macaddr_shared(map)) {
966da14cebeSEric Cheng 		i_mac_perim_exit(mip);
967da14cebeSEric Cheng 		return (EBUSY);
968da14cebeSEric Cheng 	}
969da14cebeSEric Cheng 
970da14cebeSEric Cheng 	/*
971da14cebeSEric Cheng 	 * If the new address has been used, must fail for now. Refer to
972da14cebeSEric Cheng 	 * case 4 above.
973da14cebeSEric Cheng 	 */
974da14cebeSEric Cheng 	if (mac_find_macaddr(mip, (uint8_t *)addr) != NULL) {
975da14cebeSEric Cheng 		i_mac_perim_exit(mip);
976da14cebeSEric Cheng 		return (ENOTSUP);
977da14cebeSEric Cheng 	}
978da14cebeSEric Cheng 
979da14cebeSEric Cheng 	/*
980da14cebeSEric Cheng 	 * Update the MAC address.
981da14cebeSEric Cheng 	 */
982da14cebeSEric Cheng 	err = mac_update_macaddr(map, (uint8_t *)addr);
983da14cebeSEric Cheng 
984da14cebeSEric Cheng 	if (err != 0) {
985da14cebeSEric Cheng 		i_mac_perim_exit(mip);
986da14cebeSEric Cheng 		return (err);
987da14cebeSEric Cheng 	}
988da14cebeSEric Cheng 
989da14cebeSEric Cheng 	/*
990da14cebeSEric Cheng 	 * Update all flows of this MAC client.
991da14cebeSEric Cheng 	 */
992da14cebeSEric Cheng 	mac_unicast_update_client_flow(mcip);
993da14cebeSEric Cheng 
994da14cebeSEric Cheng 	i_mac_perim_exit(mip);
995da14cebeSEric Cheng 	return (0);
996da14cebeSEric Cheng }
997da14cebeSEric Cheng 
998da14cebeSEric Cheng /*
999da14cebeSEric Cheng  * Program the new primary unicast address of the specified MAC.
1000da14cebeSEric Cheng  *
1001da14cebeSEric Cheng  * Function mac_update_macaddr() takes care different types of underlying
1002da14cebeSEric Cheng  * MAC. If the underlying MAC is VNIC, the VNIC driver must have registerd
1003da14cebeSEric Cheng  * mi_unicst() entry point, that indirectly calls mac_vnic_unicast_set()
1004da14cebeSEric Cheng  * which will take care of updating the MAC address of the corresponding
1005da14cebeSEric Cheng  * MAC client.
1006da14cebeSEric Cheng  *
1007da14cebeSEric Cheng  * This is the only interface that allow the client to update the "primary"
1008da14cebeSEric Cheng  * MAC address of the underlying MAC. The new value must have not been
1009da14cebeSEric Cheng  * used by other clients.
1010da14cebeSEric Cheng  */
1011da14cebeSEric Cheng int
mac_unicast_primary_set(mac_handle_t mh,const uint8_t * addr)1012da14cebeSEric Cheng mac_unicast_primary_set(mac_handle_t mh, const uint8_t *addr)
1013da14cebeSEric Cheng {
1014da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
1015da14cebeSEric Cheng 	mac_address_t *map;
1016da14cebeSEric Cheng 	int err;
1017da14cebeSEric Cheng 
1018da14cebeSEric Cheng 	/* verify the address validity */
1019da14cebeSEric Cheng 	if (!mac_unicst_verify(mh, addr, mip->mi_type->mt_addr_length))
1020da14cebeSEric Cheng 		return (EINVAL);
1021da14cebeSEric Cheng 
1022da14cebeSEric Cheng 	i_mac_perim_enter(mip);
1023da14cebeSEric Cheng 
1024da14cebeSEric Cheng 	/*
1025da14cebeSEric Cheng 	 * If the new value is the same as the current primary address value,
1026da14cebeSEric Cheng 	 * there's nothing to do.
1027da14cebeSEric Cheng 	 */
1028da14cebeSEric Cheng 	if (bcmp(addr, mip->mi_addr, mip->mi_type->mt_addr_length) == 0) {
1029da14cebeSEric Cheng 		i_mac_perim_exit(mip);
1030da14cebeSEric Cheng 		return (0);
1031da14cebeSEric Cheng 	}
1032da14cebeSEric Cheng 
103384de666eSRyan Zezeski 	if (mac_find_macaddr(mip, (uint8_t *)addr) != NULL) {
1034da14cebeSEric Cheng 		i_mac_perim_exit(mip);
1035da14cebeSEric Cheng 		return (EBUSY);
1036da14cebeSEric Cheng 	}
1037da14cebeSEric Cheng 
1038da14cebeSEric Cheng 	map = mac_find_macaddr(mip, mip->mi_addr);
1039da14cebeSEric Cheng 	ASSERT(map != NULL);
1040da14cebeSEric Cheng 
1041da14cebeSEric Cheng 	/*
1042da14cebeSEric Cheng 	 * Update the MAC address.
1043da14cebeSEric Cheng 	 */
1044da14cebeSEric Cheng 	if (mip->mi_state_flags & MIS_IS_AGGR) {
1045da14cebeSEric Cheng 		mac_capab_aggr_t aggr_cap;
1046da14cebeSEric Cheng 
1047da14cebeSEric Cheng 		/*
104884de666eSRyan Zezeski 		 * If the MAC is an aggregation, other than the unicast
1049da14cebeSEric Cheng 		 * addresses programming, aggr must be informed about this
105084de666eSRyan Zezeski 		 * primary unicst address change to change its MAC address
1051da14cebeSEric Cheng 		 * policy to be user-specified.
1052da14cebeSEric Cheng 		 */
1053da14cebeSEric Cheng 		ASSERT(map->ma_type == MAC_ADDRESS_TYPE_UNICAST_CLASSIFIED);
1054da14cebeSEric Cheng 		VERIFY(i_mac_capab_get(mh, MAC_CAPAB_AGGR, &aggr_cap));
1055da14cebeSEric Cheng 		err = aggr_cap.mca_unicst(mip->mi_driver, addr);
1056da14cebeSEric Cheng 		if (err == 0)
1057da14cebeSEric Cheng 			bcopy(addr, map->ma_addr, map->ma_len);
1058da14cebeSEric Cheng 	} else {
1059da14cebeSEric Cheng 		err = mac_update_macaddr(map, (uint8_t *)addr);
1060da14cebeSEric Cheng 	}
1061da14cebeSEric Cheng 
1062da14cebeSEric Cheng 	if (err != 0) {
1063da14cebeSEric Cheng 		i_mac_perim_exit(mip);
1064da14cebeSEric Cheng 		return (err);
1065da14cebeSEric Cheng 	}
1066da14cebeSEric Cheng 
1067da14cebeSEric Cheng 	mac_unicast_update_clients(mip, map);
1068da14cebeSEric Cheng 
1069da14cebeSEric Cheng 	/*
1070da14cebeSEric Cheng 	 * Save the new primary MAC address in mac_impl_t.
1071da14cebeSEric Cheng 	 */
1072da14cebeSEric Cheng 	bcopy(addr, mip->mi_addr, mip->mi_type->mt_addr_length);
1073da14cebeSEric Cheng 
1074da14cebeSEric Cheng 	i_mac_perim_exit(mip);
1075da14cebeSEric Cheng 
1076da14cebeSEric Cheng 	if (err == 0)
1077da14cebeSEric Cheng 		i_mac_notify(mip, MAC_NOTE_UNICST);
1078da14cebeSEric Cheng 
1079da14cebeSEric Cheng 	return (err);
1080da14cebeSEric Cheng }
1081da14cebeSEric Cheng 
1082da14cebeSEric Cheng /*
1083da14cebeSEric Cheng  * Return the current primary MAC address of the specified MAC.
1084da14cebeSEric Cheng  */
1085da14cebeSEric Cheng void
mac_unicast_primary_get(mac_handle_t mh,uint8_t * addr)1086da14cebeSEric Cheng mac_unicast_primary_get(mac_handle_t mh, uint8_t *addr)
1087da14cebeSEric Cheng {
1088da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
1089da14cebeSEric Cheng 
1090da14cebeSEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
1091da14cebeSEric Cheng 	bcopy(mip->mi_addr, addr, mip->mi_type->mt_addr_length);
1092da14cebeSEric Cheng 	rw_exit(&mip->mi_rw_lock);
1093da14cebeSEric Cheng }
1094da14cebeSEric Cheng 
1095da14cebeSEric Cheng /*
10961a41ca23SJerry Jelinek  * Return the secondary MAC address for the specified handle
10971a41ca23SJerry Jelinek  */
10981a41ca23SJerry Jelinek void
mac_unicast_secondary_get(mac_client_handle_t mh,uint8_t * addr)10991a41ca23SJerry Jelinek mac_unicast_secondary_get(mac_client_handle_t mh, uint8_t *addr)
11001a41ca23SJerry Jelinek {
11011a41ca23SJerry Jelinek 	mac_client_impl_t *mcip = (mac_client_impl_t *)mh;
11021a41ca23SJerry Jelinek 
11031a41ca23SJerry Jelinek 	ASSERT(mcip->mci_unicast != NULL);
11041a41ca23SJerry Jelinek 	bcopy(mcip->mci_unicast->ma_addr, addr, mcip->mci_unicast->ma_len);
11051a41ca23SJerry Jelinek }
11061a41ca23SJerry Jelinek 
11071a41ca23SJerry Jelinek /*
1108da14cebeSEric Cheng  * Return information about the use of the primary MAC address of the
1109da14cebeSEric Cheng  * specified MAC instance:
1110da14cebeSEric Cheng  *
1111da14cebeSEric Cheng  * - if client_name is non-NULL, it must point to a string of at
1112da14cebeSEric Cheng  *   least MAXNAMELEN bytes, and will be set to the name of the MAC
1113da14cebeSEric Cheng  *   client which uses the primary MAC address.
1114da14cebeSEric Cheng  *
1115da14cebeSEric Cheng  * - if in_use is non-NULL, used to return whether the primary MAC
1116da14cebeSEric Cheng  *   address is currently in use.
1117da14cebeSEric Cheng  */
1118da14cebeSEric Cheng void
mac_unicast_primary_info(mac_handle_t mh,char * client_name,boolean_t * in_use)1119da14cebeSEric Cheng mac_unicast_primary_info(mac_handle_t mh, char *client_name, boolean_t *in_use)
1120da14cebeSEric Cheng {
1121da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
1122da14cebeSEric Cheng 	mac_client_impl_t *cur_client;
1123da14cebeSEric Cheng 
1124da14cebeSEric Cheng 	if (in_use != NULL)
1125da14cebeSEric Cheng 		*in_use = B_FALSE;
1126da14cebeSEric Cheng 	if (client_name != NULL)
1127da14cebeSEric Cheng 		bzero(client_name, MAXNAMELEN);
1128da14cebeSEric Cheng 
1129da14cebeSEric Cheng 	/*
1130da14cebeSEric Cheng 	 * The mi_rw_lock is used to protect threads that don't hold the
1131da14cebeSEric Cheng 	 * mac perimeter to get a consistent view of the mi_clients_list.
1132da14cebeSEric Cheng 	 * Threads that modify the list must hold both the mac perimeter and
1133da14cebeSEric Cheng 	 * mi_rw_lock(RW_WRITER)
1134da14cebeSEric Cheng 	 */
1135da14cebeSEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
1136da14cebeSEric Cheng 	for (cur_client = mip->mi_clients_list; cur_client != NULL;
1137da14cebeSEric Cheng 	    cur_client = cur_client->mci_client_next) {
1138da14cebeSEric Cheng 		if (mac_is_primary_client(cur_client) ||
1139da14cebeSEric Cheng 		    (mip->mi_state_flags & MIS_IS_VNIC)) {
1140da14cebeSEric Cheng 			rw_exit(&mip->mi_rw_lock);
1141da14cebeSEric Cheng 			if (in_use != NULL)
1142da14cebeSEric Cheng 				*in_use = B_TRUE;
1143da14cebeSEric Cheng 			if (client_name != NULL) {
1144da14cebeSEric Cheng 				bcopy(cur_client->mci_name, client_name,
1145da14cebeSEric Cheng 				    MAXNAMELEN);
1146da14cebeSEric Cheng 			}
1147da14cebeSEric Cheng 			return;
1148da14cebeSEric Cheng 		}
1149da14cebeSEric Cheng 	}
1150da14cebeSEric Cheng 	rw_exit(&mip->mi_rw_lock);
1151da14cebeSEric Cheng }
1152da14cebeSEric Cheng 
1153da14cebeSEric Cheng /*
11542b24ab6bSSebastien Roy  * Return the current destination MAC address of the specified MAC.
11552b24ab6bSSebastien Roy  */
11562b24ab6bSSebastien Roy boolean_t
mac_dst_get(mac_handle_t mh,uint8_t * addr)11572b24ab6bSSebastien Roy mac_dst_get(mac_handle_t mh, uint8_t *addr)
11582b24ab6bSSebastien Roy {
11592b24ab6bSSebastien Roy 	mac_impl_t *mip = (mac_impl_t *)mh;
11602b24ab6bSSebastien Roy 
11612b24ab6bSSebastien Roy 	rw_enter(&mip->mi_rw_lock, RW_READER);
11622b24ab6bSSebastien Roy 	if (mip->mi_dstaddr_set)
11632b24ab6bSSebastien Roy 		bcopy(mip->mi_dstaddr, addr, mip->mi_type->mt_addr_length);
11642b24ab6bSSebastien Roy 	rw_exit(&mip->mi_rw_lock);
11652b24ab6bSSebastien Roy 	return (mip->mi_dstaddr_set);
11662b24ab6bSSebastien Roy }
11672b24ab6bSSebastien Roy 
11682b24ab6bSSebastien Roy /*
1169da14cebeSEric Cheng  * Add the specified MAC client to the list of clients which opened
1170da14cebeSEric Cheng  * the specified MAC.
1171da14cebeSEric Cheng  */
1172da14cebeSEric Cheng static void
mac_client_add(mac_client_impl_t * mcip)1173da14cebeSEric Cheng mac_client_add(mac_client_impl_t *mcip)
1174da14cebeSEric Cheng {
1175da14cebeSEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
1176da14cebeSEric Cheng 
1177da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1178da14cebeSEric Cheng 
1179da14cebeSEric Cheng 	/* add VNIC to the front of the list */
1180da14cebeSEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
1181da14cebeSEric Cheng 	mcip->mci_client_next = mip->mi_clients_list;
1182da14cebeSEric Cheng 	mip->mi_clients_list = mcip;
1183da14cebeSEric Cheng 	mip->mi_nclients++;
1184da14cebeSEric Cheng 	rw_exit(&mip->mi_rw_lock);
1185da14cebeSEric Cheng }
1186da14cebeSEric Cheng 
1187da14cebeSEric Cheng /*
1188da14cebeSEric Cheng  * Remove the specified MAC client from the list of clients which opened
1189da14cebeSEric Cheng  * the specified MAC.
1190da14cebeSEric Cheng  */
1191da14cebeSEric Cheng static void
mac_client_remove(mac_client_impl_t * mcip)1192da14cebeSEric Cheng mac_client_remove(mac_client_impl_t *mcip)
1193da14cebeSEric Cheng {
1194da14cebeSEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
1195da14cebeSEric Cheng 	mac_client_impl_t **prev, *cclient;
1196da14cebeSEric Cheng 
1197da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1198da14cebeSEric Cheng 
1199da14cebeSEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
1200da14cebeSEric Cheng 	prev = &mip->mi_clients_list;
1201da14cebeSEric Cheng 	cclient = *prev;
1202da14cebeSEric Cheng 	while (cclient != NULL && cclient != mcip) {
1203da14cebeSEric Cheng 		prev = &cclient->mci_client_next;
1204da14cebeSEric Cheng 		cclient = *prev;
1205da14cebeSEric Cheng 	}
1206da14cebeSEric Cheng 	ASSERT(cclient != NULL);
1207da14cebeSEric Cheng 	*prev = cclient->mci_client_next;
1208da14cebeSEric Cheng 	mip->mi_nclients--;
1209da14cebeSEric Cheng 	rw_exit(&mip->mi_rw_lock);
1210da14cebeSEric Cheng }
1211da14cebeSEric Cheng 
1212da14cebeSEric Cheng static mac_unicast_impl_t *
mac_client_find_vid(mac_client_impl_t * mcip,uint16_t vid)1213da14cebeSEric Cheng mac_client_find_vid(mac_client_impl_t *mcip, uint16_t vid)
1214da14cebeSEric Cheng {
1215da14cebeSEric Cheng 	mac_unicast_impl_t *muip = mcip->mci_unicast_list;
1216da14cebeSEric Cheng 
1217da14cebeSEric Cheng 	while ((muip != NULL) && (muip->mui_vid != vid))
1218da14cebeSEric Cheng 		muip = muip->mui_next;
1219da14cebeSEric Cheng 
1220da14cebeSEric Cheng 	return (muip);
1221da14cebeSEric Cheng }
1222da14cebeSEric Cheng 
1223da14cebeSEric Cheng /*
1224da14cebeSEric Cheng  * Return whether the specified (MAC address, VID) tuple is already used by
1225da14cebeSEric Cheng  * one of the MAC clients associated with the specified MAC.
1226da14cebeSEric Cheng  */
1227da14cebeSEric Cheng static boolean_t
mac_addr_in_use(mac_impl_t * mip,uint8_t * mac_addr,uint16_t vid)1228da14cebeSEric Cheng mac_addr_in_use(mac_impl_t *mip, uint8_t *mac_addr, uint16_t vid)
1229da14cebeSEric Cheng {
1230da14cebeSEric Cheng 	mac_client_impl_t *client;
1231da14cebeSEric Cheng 	mac_address_t *map;
1232da14cebeSEric Cheng 
1233da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1234da14cebeSEric Cheng 
1235da14cebeSEric Cheng 	for (client = mip->mi_clients_list; client != NULL;
1236da14cebeSEric Cheng 	    client = client->mci_client_next) {
1237da14cebeSEric Cheng 
1238da14cebeSEric Cheng 		/*
1239da14cebeSEric Cheng 		 * Ignore clients that don't have unicast address.
1240da14cebeSEric Cheng 		 */
1241da14cebeSEric Cheng 		if (client->mci_unicast_list == NULL)
1242da14cebeSEric Cheng 			continue;
1243da14cebeSEric Cheng 
1244da14cebeSEric Cheng 		map = client->mci_unicast;
1245da14cebeSEric Cheng 
1246da14cebeSEric Cheng 		if ((bcmp(mac_addr, map->ma_addr, map->ma_len) == 0) &&
1247da14cebeSEric Cheng 		    (mac_client_find_vid(client, vid) != NULL)) {
1248da14cebeSEric Cheng 			return (B_TRUE);
1249da14cebeSEric Cheng 		}
1250da14cebeSEric Cheng 	}
1251da14cebeSEric Cheng 
1252da14cebeSEric Cheng 	return (B_FALSE);
1253da14cebeSEric Cheng }
1254da14cebeSEric Cheng 
1255da14cebeSEric Cheng /*
1256da14cebeSEric Cheng  * Generate a random MAC address. The MAC address prefix is
1257da14cebeSEric Cheng  * stored in the array pointed to by mac_addr, and its length, in bytes,
1258da14cebeSEric Cheng  * is specified by prefix_len. The least significant bits
1259da14cebeSEric Cheng  * after prefix_len bytes are generated, and stored after the prefix
1260da14cebeSEric Cheng  * in the mac_addr array.
1261da14cebeSEric Cheng  */
1262da14cebeSEric Cheng int
mac_addr_random(mac_client_handle_t mch,uint_t prefix_len,uint8_t * mac_addr,mac_diag_t * diag)1263da14cebeSEric Cheng mac_addr_random(mac_client_handle_t mch, uint_t prefix_len,
1264da14cebeSEric Cheng     uint8_t *mac_addr, mac_diag_t *diag)
1265da14cebeSEric Cheng {
1266da14cebeSEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1267da14cebeSEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
1268da14cebeSEric Cheng 	size_t addr_len = mip->mi_type->mt_addr_length;
1269da14cebeSEric Cheng 
1270da14cebeSEric Cheng 	if (prefix_len >= addr_len) {
1271da14cebeSEric Cheng 		*diag = MAC_DIAG_MACPREFIXLEN_INVALID;
1272da14cebeSEric Cheng 		return (EINVAL);
1273da14cebeSEric Cheng 	}
1274da14cebeSEric Cheng 
1275da14cebeSEric Cheng 	/* check the prefix value */
1276da14cebeSEric Cheng 	if (prefix_len > 0) {
1277da14cebeSEric Cheng 		bzero(mac_addr + prefix_len, addr_len - prefix_len);
1278da14cebeSEric Cheng 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr,
1279da14cebeSEric Cheng 		    addr_len)) {
1280da14cebeSEric Cheng 			*diag = MAC_DIAG_MACPREFIX_INVALID;
1281da14cebeSEric Cheng 			return (EINVAL);
1282da14cebeSEric Cheng 		}
1283da14cebeSEric Cheng 	}
1284da14cebeSEric Cheng 
1285da14cebeSEric Cheng 	/* generate the MAC address */
1286da14cebeSEric Cheng 	if (prefix_len < addr_len) {
1287da14cebeSEric Cheng 		(void) random_get_pseudo_bytes(mac_addr +
1288da14cebeSEric Cheng 		    prefix_len, addr_len - prefix_len);
1289da14cebeSEric Cheng 	}
1290da14cebeSEric Cheng 
1291ea720162SAndy Fiddaman 	*diag = MAC_DIAG_NONE;
1292da14cebeSEric Cheng 	return (0);
1293da14cebeSEric Cheng }
1294da14cebeSEric Cheng 
1295da14cebeSEric Cheng /*
1296da14cebeSEric Cheng  * Set the priority range for this MAC client. This will be used to
1297da14cebeSEric Cheng  * determine the absolute priority for the threads created for this
1298da14cebeSEric Cheng  * MAC client using the specified "low", "medium" and "high" level.
1299da14cebeSEric Cheng  * This will also be used for any subflows on this MAC client.
1300da14cebeSEric Cheng  */
1301da14cebeSEric Cheng #define	MAC_CLIENT_SET_PRIORITY_RANGE(mcip, pri) {			\
1302da14cebeSEric Cheng 	(mcip)->mci_min_pri = FLOW_MIN_PRIORITY(MINCLSYSPRI,	\
1303da14cebeSEric Cheng 	    MAXCLSYSPRI, (pri));					\
1304da14cebeSEric Cheng 	(mcip)->mci_max_pri = FLOW_MAX_PRIORITY(MINCLSYSPRI,	\
1305da14cebeSEric Cheng 	    MAXCLSYSPRI, (mcip)->mci_min_pri);				\
1306da14cebeSEric Cheng 	}
1307da14cebeSEric Cheng 
1308da14cebeSEric Cheng /*
1309da14cebeSEric Cheng  * MAC client open entry point. Return a new MAC client handle. Each
1310da14cebeSEric Cheng  * MAC client is associated with a name, specified through the 'name'
1311da14cebeSEric Cheng  * argument.
1312da14cebeSEric Cheng  */
1313da14cebeSEric Cheng int
mac_client_open(mac_handle_t mh,mac_client_handle_t * mchp,char * name,uint16_t flags)1314da14cebeSEric Cheng mac_client_open(mac_handle_t mh, mac_client_handle_t *mchp, char *name,
1315da14cebeSEric Cheng     uint16_t flags)
1316da14cebeSEric Cheng {
1317da14cebeSEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
1318da14cebeSEric Cheng 	mac_client_impl_t	*mcip;
1319da14cebeSEric Cheng 	int			err = 0;
13200dc2366fSVenugopal Iyer 	boolean_t		share_desired;
1321da14cebeSEric Cheng 	flow_entry_t		*flent = NULL;
1322da14cebeSEric Cheng 
13230dc2366fSVenugopal Iyer 	share_desired = (flags & MAC_OPEN_FLAGS_SHARES_DESIRED) != 0;
1324da14cebeSEric Cheng 	*mchp = NULL;
1325da14cebeSEric Cheng 
1326da14cebeSEric Cheng 	i_mac_perim_enter(mip);
1327da14cebeSEric Cheng 
1328da14cebeSEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
1329da14cebeSEric Cheng 		/*
1330da14cebeSEric Cheng 		 * The underlying MAC is a VNIC. Return the MAC client
1331da14cebeSEric Cheng 		 * handle of the lower MAC which was obtained by
1332da14cebeSEric Cheng 		 * the VNIC driver when it did its mac_client_open().
1333da14cebeSEric Cheng 		 */
1334da14cebeSEric Cheng 
1335da14cebeSEric Cheng 		mcip = mac_vnic_lower(mip);
1336da14cebeSEric Cheng 
1337da14cebeSEric Cheng 		/*
1338da14cebeSEric Cheng 		 * Note that multiple mac clients share the same mcip in
1339da14cebeSEric Cheng 		 * this case.
1340da14cebeSEric Cheng 		 */
1341da14cebeSEric Cheng 		if (flags & MAC_OPEN_FLAGS_EXCLUSIVE)
1342da14cebeSEric Cheng 			mcip->mci_state_flags |= MCIS_EXCLUSIVE;
1343da14cebeSEric Cheng 
1344fc4e975dSVenugopal Iyer 		if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
1345fc4e975dSVenugopal Iyer 			mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
1346fc4e975dSVenugopal Iyer 
1347da14cebeSEric Cheng 		mip->mi_clients_list = mcip;
1348da14cebeSEric Cheng 		i_mac_perim_exit(mip);
1349da14cebeSEric Cheng 		*mchp = (mac_client_handle_t)mcip;
13501a41ca23SJerry Jelinek 
13511a41ca23SJerry Jelinek 		DTRACE_PROBE2(mac__client__open__nonallocated, mac_impl_t *,
13521a41ca23SJerry Jelinek 		    mcip->mci_mip, mac_client_impl_t *, mcip);
13531a41ca23SJerry Jelinek 
1354da14cebeSEric Cheng 		return (err);
1355da14cebeSEric Cheng 	}
1356da14cebeSEric Cheng 
1357da14cebeSEric Cheng 	mcip = kmem_cache_alloc(mac_client_impl_cache, KM_SLEEP);
1358da14cebeSEric Cheng 
1359da14cebeSEric Cheng 	mcip->mci_mip = mip;
1360da14cebeSEric Cheng 	mcip->mci_upper_mip = NULL;
1361c61a1653SRyan Zezeski 	mcip->mci_rx_fn = mac_rx_def;
1362da14cebeSEric Cheng 	mcip->mci_rx_arg = NULL;
1363fc4e975dSVenugopal Iyer 	mcip->mci_rx_p_fn = NULL;
1364fc4e975dSVenugopal Iyer 	mcip->mci_rx_p_arg = NULL;
1365fc4e975dSVenugopal Iyer 	mcip->mci_p_unicast_list = NULL;
1366da14cebeSEric Cheng 	mcip->mci_direct_rx_fn = NULL;
1367da14cebeSEric Cheng 	mcip->mci_direct_rx_arg = NULL;
13683bb0cb70SBryan Cantrill 	mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
1369da14cebeSEric Cheng 
1370fc4e975dSVenugopal Iyer 	mcip->mci_unicast_list = NULL;
1371fc4e975dSVenugopal Iyer 
1372da14cebeSEric Cheng 	if ((flags & MAC_OPEN_FLAGS_IS_VNIC) != 0)
1373da14cebeSEric Cheng 		mcip->mci_state_flags |= MCIS_IS_VNIC;
1374da14cebeSEric Cheng 
1375da14cebeSEric Cheng 	if ((flags & MAC_OPEN_FLAGS_EXCLUSIVE) != 0)
1376da14cebeSEric Cheng 		mcip->mci_state_flags |= MCIS_EXCLUSIVE;
1377da14cebeSEric Cheng 
1378da14cebeSEric Cheng 	if ((flags & MAC_OPEN_FLAGS_IS_AGGR_PORT) != 0)
1379da14cebeSEric Cheng 		mcip->mci_state_flags |= MCIS_IS_AGGR_PORT;
1380da14cebeSEric Cheng 
13810dc2366fSVenugopal Iyer 	if (mip->mi_state_flags & MIS_IS_AGGR)
138284de666eSRyan Zezeski 		mcip->mci_state_flags |= MCIS_IS_AGGR_CLIENT;
13830dc2366fSVenugopal Iyer 
1384da14cebeSEric Cheng 	if ((flags & MAC_OPEN_FLAGS_USE_DATALINK_NAME) != 0) {
1385da14cebeSEric Cheng 		datalink_id_t	linkid;
1386da14cebeSEric Cheng 
1387da14cebeSEric Cheng 		ASSERT(name == NULL);
1388da14cebeSEric Cheng 		if ((err = dls_devnet_macname2linkid(mip->mi_name,
1389da14cebeSEric Cheng 		    &linkid)) != 0) {
1390da14cebeSEric Cheng 			goto done;
1391da14cebeSEric Cheng 		}
1392da14cebeSEric Cheng 		if ((err = dls_mgmt_get_linkinfo(linkid, mcip->mci_name, NULL,
1393da14cebeSEric Cheng 		    NULL, NULL)) != 0) {
1394da14cebeSEric Cheng 			/*
1395da14cebeSEric Cheng 			 * Use mac name if dlmgmtd is not available.
1396da14cebeSEric Cheng 			 */
1397da14cebeSEric Cheng 			if (err == EBADF) {
1398da14cebeSEric Cheng 				(void) strlcpy(mcip->mci_name, mip->mi_name,
1399da14cebeSEric Cheng 				    sizeof (mcip->mci_name));
1400da14cebeSEric Cheng 				err = 0;
1401da14cebeSEric Cheng 			} else {
1402da14cebeSEric Cheng 				goto done;
1403da14cebeSEric Cheng 			}
1404da14cebeSEric Cheng 		}
1405da14cebeSEric Cheng 		mcip->mci_state_flags |= MCIS_USE_DATALINK_NAME;
1406da14cebeSEric Cheng 	} else {
1407da14cebeSEric Cheng 		ASSERT(name != NULL);
1408da14cebeSEric Cheng 		if (strlen(name) > MAXNAMELEN) {
1409da14cebeSEric Cheng 			err = EINVAL;
1410da14cebeSEric Cheng 			goto done;
1411da14cebeSEric Cheng 		}
1412da14cebeSEric Cheng 		(void) strlcpy(mcip->mci_name, name, sizeof (mcip->mci_name));
1413da14cebeSEric Cheng 	}
1414fc4e975dSVenugopal Iyer 
1415fc4e975dSVenugopal Iyer 	if (flags & MAC_OPEN_FLAGS_MULTI_PRIMARY)
1416fc4e975dSVenugopal Iyer 		mcip->mci_flags |= MAC_CLIENT_FLAGS_MULTI_PRIMARY;
1417fc4e975dSVenugopal Iyer 
14180dc2366fSVenugopal Iyer 	if (flags & MAC_OPEN_FLAGS_NO_UNICAST_ADDR)
14190dc2366fSVenugopal Iyer 		mcip->mci_state_flags |= MCIS_NO_UNICAST_ADDR;
14200dc2366fSVenugopal Iyer 
14210dc2366fSVenugopal Iyer 	mac_protect_init(mcip);
14220dc2366fSVenugopal Iyer 
1423da14cebeSEric Cheng 	/* the subflow table will be created dynamically */
1424da14cebeSEric Cheng 	mcip->mci_subflow_tab = NULL;
1425da14cebeSEric Cheng 
14260dc2366fSVenugopal Iyer 	mcip->mci_misc_stat.mms_multircv = 0;
14270dc2366fSVenugopal Iyer 	mcip->mci_misc_stat.mms_brdcstrcv = 0;
14280dc2366fSVenugopal Iyer 	mcip->mci_misc_stat.mms_multixmt = 0;
14290dc2366fSVenugopal Iyer 	mcip->mci_misc_stat.mms_brdcstxmt = 0;
1430da14cebeSEric Cheng 
1431da14cebeSEric Cheng 	/* Create an initial flow */
1432da14cebeSEric Cheng 
1433da14cebeSEric Cheng 	err = mac_flow_create(NULL, NULL, mcip->mci_name, NULL,
1434da14cebeSEric Cheng 	    mcip->mci_state_flags & MCIS_IS_VNIC ? FLOW_VNIC_MAC :
1435da14cebeSEric Cheng 	    FLOW_PRIMARY_MAC, &flent);
1436da14cebeSEric Cheng 	if (err != 0)
1437da14cebeSEric Cheng 		goto done;
1438da14cebeSEric Cheng 	mcip->mci_flent = flent;
1439da14cebeSEric Cheng 	FLOW_MARK(flent, FE_MC_NO_DATAPATH);
1440da14cebeSEric Cheng 	flent->fe_mcip = mcip;
144145948e49SRyan Zezeski 
1442da14cebeSEric Cheng 	/*
1443da14cebeSEric Cheng 	 * Place initial creation reference on the flow. This reference
1444da14cebeSEric Cheng 	 * is released in the corresponding delete action viz.
1445da14cebeSEric Cheng 	 * mac_unicast_remove after waiting for all transient refs to
1446da14cebeSEric Cheng 	 * to go away. The wait happens in mac_flow_wait.
1447da14cebeSEric Cheng 	 */
1448da14cebeSEric Cheng 	FLOW_REFHOLD(flent);
1449da14cebeSEric Cheng 
1450da14cebeSEric Cheng 	/*
1451da14cebeSEric Cheng 	 * Do this ahead of the mac_bcast_add() below so that the mi_nclients
1452da14cebeSEric Cheng 	 * will have the right value for mac_rx_srs_setup().
1453da14cebeSEric Cheng 	 */
1454da14cebeSEric Cheng 	mac_client_add(mcip);
1455da14cebeSEric Cheng 
145636f99a58SToomas Soome 	mcip->mci_share = 0;
14570dc2366fSVenugopal Iyer 	if (share_desired)
1458da14cebeSEric Cheng 		i_mac_share_alloc(mcip);
1459da14cebeSEric Cheng 
14600dc2366fSVenugopal Iyer 	/*
14610dc2366fSVenugopal Iyer 	 * We will do mimimal datapath setup to allow a MAC client to
14620dc2366fSVenugopal Iyer 	 * transmit or receive non-unicast packets without waiting
14630dc2366fSVenugopal Iyer 	 * for mac_unicast_add.
14640dc2366fSVenugopal Iyer 	 */
14650dc2366fSVenugopal Iyer 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR) {
14660dc2366fSVenugopal Iyer 		if ((err = mac_client_datapath_setup(mcip, VLAN_ID_NONE,
14670dc2366fSVenugopal Iyer 		    NULL, NULL, B_TRUE, NULL)) != 0) {
14680dc2366fSVenugopal Iyer 			goto done;
14690dc2366fSVenugopal Iyer 		}
14700dc2366fSVenugopal Iyer 	}
14711a41ca23SJerry Jelinek 
14721a41ca23SJerry Jelinek 	DTRACE_PROBE2(mac__client__open__allocated, mac_impl_t *,
14731a41ca23SJerry Jelinek 	    mcip->mci_mip, mac_client_impl_t *, mcip);
14741a41ca23SJerry Jelinek 
14751a41ca23SJerry Jelinek 	*mchp = (mac_client_handle_t)mcip;
1476da14cebeSEric Cheng 	i_mac_perim_exit(mip);
1477da14cebeSEric Cheng 	return (0);
1478da14cebeSEric Cheng 
1479da14cebeSEric Cheng done:
1480da14cebeSEric Cheng 	i_mac_perim_exit(mip);
1481da14cebeSEric Cheng 	mcip->mci_state_flags = 0;
1482da14cebeSEric Cheng 	mcip->mci_tx_flag = 0;
1483da14cebeSEric Cheng 	kmem_cache_free(mac_client_impl_cache, mcip);
1484da14cebeSEric Cheng 	return (err);
1485da14cebeSEric Cheng }
1486da14cebeSEric Cheng 
1487da14cebeSEric Cheng /*
1488da14cebeSEric Cheng  * Close the specified MAC client handle.
1489da14cebeSEric Cheng  */
1490da14cebeSEric Cheng void
mac_client_close(mac_client_handle_t mch,uint16_t flags)1491da14cebeSEric Cheng mac_client_close(mac_client_handle_t mch, uint16_t flags)
1492da14cebeSEric Cheng {
1493da14cebeSEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
1494da14cebeSEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
1495da14cebeSEric Cheng 	flow_entry_t		*flent;
1496da14cebeSEric Cheng 
1497da14cebeSEric Cheng 	i_mac_perim_enter(mip);
1498da14cebeSEric Cheng 
1499da14cebeSEric Cheng 	if (flags & MAC_CLOSE_FLAGS_EXCLUSIVE)
1500da14cebeSEric Cheng 		mcip->mci_state_flags &= ~MCIS_EXCLUSIVE;
1501da14cebeSEric Cheng 
1502da14cebeSEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
1503da14cebeSEric Cheng 	    !(flags & MAC_CLOSE_FLAGS_IS_VNIC)) {
1504da14cebeSEric Cheng 		/*
1505da14cebeSEric Cheng 		 * This is an upper VNIC client initiated operation.
1506da14cebeSEric Cheng 		 * The lower MAC client will be closed by the VNIC driver
1507da14cebeSEric Cheng 		 * when the VNIC is deleted.
1508da14cebeSEric Cheng 		 */
1509da14cebeSEric Cheng 
1510da14cebeSEric Cheng 		i_mac_perim_exit(mip);
1511da14cebeSEric Cheng 		return;
1512da14cebeSEric Cheng 	}
1513da14cebeSEric Cheng 
15140dc2366fSVenugopal Iyer 	/* If we have only setup up minimal datapth setup, tear it down */
15150dc2366fSVenugopal Iyer 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR) {
15160dc2366fSVenugopal Iyer 		mac_client_datapath_teardown((mac_client_handle_t)mcip, NULL,
15170dc2366fSVenugopal Iyer 		    mcip->mci_flent);
15180dc2366fSVenugopal Iyer 		mcip->mci_state_flags &= ~MCIS_NO_UNICAST_ADDR;
15190dc2366fSVenugopal Iyer 	}
15200dc2366fSVenugopal Iyer 
1521da14cebeSEric Cheng 	/*
1522da14cebeSEric Cheng 	 * Remove the flent associated with the MAC client
1523da14cebeSEric Cheng 	 */
1524da14cebeSEric Cheng 	flent = mcip->mci_flent;
1525da14cebeSEric Cheng 	mcip->mci_flent = NULL;
1526da14cebeSEric Cheng 	FLOW_FINAL_REFRELE(flent);
1527da14cebeSEric Cheng 
1528da14cebeSEric Cheng 	/*
1529da14cebeSEric Cheng 	 * MAC clients must remove the unicast addresses and promisc callbacks
1530da14cebeSEric Cheng 	 * they added before issuing a mac_client_close().
1531da14cebeSEric Cheng 	 */
1532da14cebeSEric Cheng 	ASSERT(mcip->mci_unicast_list == NULL);
1533da14cebeSEric Cheng 	ASSERT(mcip->mci_promisc_list == NULL);
1534da14cebeSEric Cheng 	ASSERT(mcip->mci_tx_notify_cb_list == NULL);
1535da14cebeSEric Cheng 
1536da14cebeSEric Cheng 	i_mac_share_free(mcip);
15370dc2366fSVenugopal Iyer 	mac_protect_fini(mcip);
1538da14cebeSEric Cheng 	mac_client_remove(mcip);
1539da14cebeSEric Cheng 
1540da14cebeSEric Cheng 	i_mac_perim_exit(mip);
1541da14cebeSEric Cheng 	mcip->mci_subflow_tab = NULL;
1542da14cebeSEric Cheng 	mcip->mci_state_flags = 0;
1543da14cebeSEric Cheng 	mcip->mci_tx_flag = 0;
1544da14cebeSEric Cheng 	kmem_cache_free(mac_client_impl_cache, mch);
1545da14cebeSEric Cheng }
1546da14cebeSEric Cheng 
1547da14cebeSEric Cheng /*
154884de666eSRyan Zezeski  * Set the Rx bypass receive callback and return B_TRUE. Return
154984de666eSRyan Zezeski  * B_FALSE if it's not possible to enable bypass.
1550da14cebeSEric Cheng  */
1551da14cebeSEric Cheng boolean_t
mac_rx_bypass_set(mac_client_handle_t mch,mac_direct_rx_t rx_fn,void * arg1)1552da14cebeSEric Cheng mac_rx_bypass_set(mac_client_handle_t mch, mac_direct_rx_t rx_fn, void *arg1)
1553da14cebeSEric Cheng {
1554da14cebeSEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
1555da14cebeSEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
1556da14cebeSEric Cheng 
1557da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
1558da14cebeSEric Cheng 
1559da14cebeSEric Cheng 	/*
156084de666eSRyan Zezeski 	 * If the client has more than one VLAN then process packets
156184de666eSRyan Zezeski 	 * through DLS. This should happen only when sun4v vsw is on
156284de666eSRyan Zezeski 	 * the scene.
1563da14cebeSEric Cheng 	 */
156484de666eSRyan Zezeski 	if (mcip->mci_nvids > 1)
1565da14cebeSEric Cheng 		return (B_FALSE);
1566da14cebeSEric Cheng 
1567da14cebeSEric Cheng 	/*
1568da14cebeSEric Cheng 	 * These are not accessed directly in the data path, and hence
1569da14cebeSEric Cheng 	 * don't need any protection
1570da14cebeSEric Cheng 	 */
1571da14cebeSEric Cheng 	mcip->mci_direct_rx_fn = rx_fn;
1572da14cebeSEric Cheng 	mcip->mci_direct_rx_arg = arg1;
1573da14cebeSEric Cheng 	return (B_TRUE);
1574da14cebeSEric Cheng }
1575da14cebeSEric Cheng 
1576da14cebeSEric Cheng /*
15778d4cf8d8S  * Enable/Disable rx bypass. By default, bypass is assumed to be enabled.
15788d4cf8d8S  */
15798d4cf8d8S void
mac_rx_bypass_enable(mac_client_handle_t mch)15808d4cf8d8S mac_rx_bypass_enable(mac_client_handle_t mch)
15818d4cf8d8S {
15828d4cf8d8S 	((mac_client_impl_t *)mch)->mci_state_flags &= ~MCIS_RX_BYPASS_DISABLE;
15838d4cf8d8S }
15848d4cf8d8S 
15858d4cf8d8S void
mac_rx_bypass_disable(mac_client_handle_t mch)15868d4cf8d8S mac_rx_bypass_disable(mac_client_handle_t mch)
15878d4cf8d8S {
15888d4cf8d8S 	((mac_client_impl_t *)mch)->mci_state_flags |= MCIS_RX_BYPASS_DISABLE;
15898d4cf8d8S }
15908d4cf8d8S 
15918d4cf8d8S /*
1592da14cebeSEric Cheng  * Set the receive callback for the specified MAC client. There can be
1593da14cebeSEric Cheng  * at most one such callback per MAC client.
1594da14cebeSEric Cheng  */
1595da14cebeSEric Cheng void
mac_rx_set(mac_client_handle_t mch,mac_rx_t rx_fn,void * arg)1596da14cebeSEric Cheng mac_rx_set(mac_client_handle_t mch, mac_rx_t rx_fn, void *arg)
1597da14cebeSEric Cheng {
1598da14cebeSEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1599da14cebeSEric Cheng 	mac_impl_t	*mip = mcip->mci_mip;
16001a41ca23SJerry Jelinek 	mac_impl_t	*umip = mcip->mci_upper_mip;
1601da14cebeSEric Cheng 
1602da14cebeSEric Cheng 	/*
1603da14cebeSEric Cheng 	 * Instead of adding an extra set of locks and refcnts in
1604da14cebeSEric Cheng 	 * the datapath at the mac client boundary, we temporarily quiesce
1605da14cebeSEric Cheng 	 * the SRS and related entities. We then change the receive function
1606da14cebeSEric Cheng 	 * without interference from any receive data thread and then reenable
1607da14cebeSEric Cheng 	 * the data flow subsequently.
1608da14cebeSEric Cheng 	 */
1609da14cebeSEric Cheng 	i_mac_perim_enter(mip);
1610da14cebeSEric Cheng 	mac_rx_client_quiesce(mch);
1611da14cebeSEric Cheng 
1612da14cebeSEric Cheng 	mcip->mci_rx_fn = rx_fn;
1613da14cebeSEric Cheng 	mcip->mci_rx_arg = arg;
1614da14cebeSEric Cheng 	mac_rx_client_restart(mch);
16151a41ca23SJerry Jelinek 
16161a41ca23SJerry Jelinek 	/*
161784de666eSRyan Zezeski 	 * If we're changing the Rx function on the primary MAC of a VNIC,
161884de666eSRyan Zezeski 	 * make sure any secondary addresses on the VNIC are updated as well.
16191a41ca23SJerry Jelinek 	 */
16201a41ca23SJerry Jelinek 	if (umip != NULL) {
16211a41ca23SJerry Jelinek 		ASSERT((umip->mi_state_flags & MIS_IS_VNIC) != 0);
16221a41ca23SJerry Jelinek 		mac_vnic_secondary_update(umip);
16231a41ca23SJerry Jelinek 	}
1624*119d61ccSAndy Fiddaman 
1625*119d61ccSAndy Fiddaman 	i_mac_perim_exit(mip);
1626da14cebeSEric Cheng }
1627da14cebeSEric Cheng 
1628da14cebeSEric Cheng /*
1629da14cebeSEric Cheng  * Reset the receive callback for the specified MAC client.
1630da14cebeSEric Cheng  */
1631da14cebeSEric Cheng void
mac_rx_clear(mac_client_handle_t mch)1632da14cebeSEric Cheng mac_rx_clear(mac_client_handle_t mch)
1633da14cebeSEric Cheng {
1634c61a1653SRyan Zezeski 	mac_rx_set(mch, mac_rx_def, NULL);
1635da14cebeSEric Cheng }
1636da14cebeSEric Cheng 
16371a41ca23SJerry Jelinek void
mac_rx_barrier(mac_client_handle_t mch)1638b237158dSPatrick Mooney mac_rx_barrier(mac_client_handle_t mch)
1639b237158dSPatrick Mooney {
1640b237158dSPatrick Mooney 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
1641b237158dSPatrick Mooney 	mac_impl_t *mip = mcip->mci_mip;
1642b237158dSPatrick Mooney 
1643b237158dSPatrick Mooney 	i_mac_perim_enter(mip);
1644b237158dSPatrick Mooney 
1645b237158dSPatrick Mooney 	/* If a RX callback is set, quiesce and restart that datapath */
1646c61a1653SRyan Zezeski 	if (mcip->mci_rx_fn != mac_rx_def) {
1647b237158dSPatrick Mooney 		mac_rx_client_quiesce(mch);
1648b237158dSPatrick Mooney 		mac_rx_client_restart(mch);
1649b237158dSPatrick Mooney 	}
1650b237158dSPatrick Mooney 
1651b237158dSPatrick Mooney 	/* If any promisc callbacks are registered, perform a barrier there */
1652b237158dSPatrick Mooney 	if (mcip->mci_promisc_list != NULL || mip->mi_promisc_list != NULL) {
1653b237158dSPatrick Mooney 		mac_cb_info_t *mcbi =  &mip->mi_promisc_cb_info;
1654b237158dSPatrick Mooney 
1655b237158dSPatrick Mooney 		mutex_enter(mcbi->mcbi_lockp);
1656b237158dSPatrick Mooney 		mac_callback_barrier(mcbi);
1657b237158dSPatrick Mooney 		mutex_exit(mcbi->mcbi_lockp);
1658b237158dSPatrick Mooney 	}
1659b237158dSPatrick Mooney 
1660b237158dSPatrick Mooney 	i_mac_perim_exit(mip);
1661b237158dSPatrick Mooney }
1662b237158dSPatrick Mooney 
1663b237158dSPatrick Mooney void
mac_secondary_dup(mac_client_handle_t smch,mac_client_handle_t dmch)16641a41ca23SJerry Jelinek mac_secondary_dup(mac_client_handle_t smch, mac_client_handle_t dmch)
16651a41ca23SJerry Jelinek {
16661a41ca23SJerry Jelinek 	mac_client_impl_t *smcip = (mac_client_impl_t *)smch;
16671a41ca23SJerry Jelinek 	mac_client_impl_t *dmcip = (mac_client_impl_t *)dmch;
16681a41ca23SJerry Jelinek 	flow_entry_t *flent = dmcip->mci_flent;
16691a41ca23SJerry Jelinek 
16701a41ca23SJerry Jelinek 	/* This should only be called to setup secondary macs */
16711a41ca23SJerry Jelinek 	ASSERT((flent->fe_type & FLOW_PRIMARY_MAC) == 0);
16721a41ca23SJerry Jelinek 
16731a41ca23SJerry Jelinek 	mac_rx_set(dmch, smcip->mci_rx_fn, smcip->mci_rx_arg);
16741a41ca23SJerry Jelinek 	dmcip->mci_promisc_list = smcip->mci_promisc_list;
16751a41ca23SJerry Jelinek 
16761a41ca23SJerry Jelinek 	/*
16771a41ca23SJerry Jelinek 	 * Duplicate the primary mac resources to the secondary.
16781a41ca23SJerry Jelinek 	 * Since we already validated the resource controls when setting
16791a41ca23SJerry Jelinek 	 * them on the primary, we can ignore errors here.
16801a41ca23SJerry Jelinek 	 */
16811a41ca23SJerry Jelinek 	(void) mac_resource_ctl_set(dmch, MCIP_RESOURCE_PROPS(smcip));
16821a41ca23SJerry Jelinek }
16831a41ca23SJerry Jelinek 
16841a41ca23SJerry Jelinek /*
16851a41ca23SJerry Jelinek  * Called when removing a secondary MAC. Currently only clears the promisc_list
16861a41ca23SJerry Jelinek  * since we share the primary mac's promisc_list.
16871a41ca23SJerry Jelinek  */
16881a41ca23SJerry Jelinek void
mac_secondary_cleanup(mac_client_handle_t mch)16891a41ca23SJerry Jelinek mac_secondary_cleanup(mac_client_handle_t mch)
16901a41ca23SJerry Jelinek {
16911a41ca23SJerry Jelinek 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
16921a41ca23SJerry Jelinek 	flow_entry_t *flent = mcip->mci_flent;
16931a41ca23SJerry Jelinek 
16941a41ca23SJerry Jelinek 	/* This should only be called for secondary macs */
16951a41ca23SJerry Jelinek 	ASSERT((flent->fe_type & FLOW_PRIMARY_MAC) == 0);
16961a41ca23SJerry Jelinek 	mcip->mci_promisc_list = NULL;
16971a41ca23SJerry Jelinek }
16981a41ca23SJerry Jelinek 
1699da14cebeSEric Cheng /*
1700da14cebeSEric Cheng  * Walk the MAC client subflow table and updates their priority values.
1701da14cebeSEric Cheng  */
1702da14cebeSEric Cheng static int
mac_update_subflow_priority_cb(flow_entry_t * flent,void * arg)1703da14cebeSEric Cheng mac_update_subflow_priority_cb(flow_entry_t *flent, void *arg)
1704da14cebeSEric Cheng {
1705da14cebeSEric Cheng 	mac_flow_update_priority(arg, flent);
1706da14cebeSEric Cheng 	return (0);
1707da14cebeSEric Cheng }
1708da14cebeSEric Cheng 
1709da14cebeSEric Cheng void
mac_update_subflow_priority(mac_client_impl_t * mcip)1710da14cebeSEric Cheng mac_update_subflow_priority(mac_client_impl_t *mcip)
1711da14cebeSEric Cheng {
1712da14cebeSEric Cheng 	(void) mac_flow_walk(mcip->mci_subflow_tab,
1713da14cebeSEric Cheng 	    mac_update_subflow_priority_cb, mcip);
1714da14cebeSEric Cheng }
1715da14cebeSEric Cheng 
1716da14cebeSEric Cheng /*
17170dc2366fSVenugopal Iyer  * Modify the TX or RX ring properties. We could either just move around
17180dc2366fSVenugopal Iyer  * rings, i.e add/remove rings given to a client. Or this might cause the
17190dc2366fSVenugopal Iyer  * client to move from hardware based to software or the other way around.
17200dc2366fSVenugopal Iyer  * If we want to reset this property, then we clear the mask, additionally
17210dc2366fSVenugopal Iyer  * if the client was given a non-default group we remove all rings except
17220dc2366fSVenugopal Iyer  * for 1 and give it back to the default group.
17230dc2366fSVenugopal Iyer  */
17240dc2366fSVenugopal Iyer int
mac_client_set_rings_prop(mac_client_impl_t * mcip,mac_resource_props_t * mrp,mac_resource_props_t * tmrp)17250dc2366fSVenugopal Iyer mac_client_set_rings_prop(mac_client_impl_t *mcip, mac_resource_props_t *mrp,
17260dc2366fSVenugopal Iyer     mac_resource_props_t *tmrp)
17270dc2366fSVenugopal Iyer {
17280dc2366fSVenugopal Iyer 	mac_impl_t		*mip = mcip->mci_mip;
17290dc2366fSVenugopal Iyer 	flow_entry_t		*flent = mcip->mci_flent;
17300dc2366fSVenugopal Iyer 	uint8_t			*mac_addr;
17310dc2366fSVenugopal Iyer 	int			err = 0;
17320dc2366fSVenugopal Iyer 	mac_group_t		*defgrp;
17330dc2366fSVenugopal Iyer 	mac_group_t		*group;
17340dc2366fSVenugopal Iyer 	mac_group_t		*ngrp;
17350dc2366fSVenugopal Iyer 	mac_resource_props_t	*cmrp = MCIP_RESOURCE_PROPS(mcip);
17360dc2366fSVenugopal Iyer 	uint_t			ringcnt;
17370dc2366fSVenugopal Iyer 	boolean_t		unspec;
17380dc2366fSVenugopal Iyer 
173936f99a58SToomas Soome 	if (mcip->mci_share != 0)
17400dc2366fSVenugopal Iyer 		return (EINVAL);
17410dc2366fSVenugopal Iyer 
17420dc2366fSVenugopal Iyer 	if (mrp->mrp_mask & MRP_RX_RINGS) {
17430dc2366fSVenugopal Iyer 		unspec = mrp->mrp_mask & MRP_RXRINGS_UNSPEC;
17440dc2366fSVenugopal Iyer 		group = flent->fe_rx_ring_group;
17450dc2366fSVenugopal Iyer 		defgrp = MAC_DEFAULT_RX_GROUP(mip);
17460dc2366fSVenugopal Iyer 		mac_addr = flent->fe_flow_desc.fd_dst_mac;
17470dc2366fSVenugopal Iyer 
17480dc2366fSVenugopal Iyer 		/*
17490dc2366fSVenugopal Iyer 		 * No resulting change. If we are resetting on a client on
17500dc2366fSVenugopal Iyer 		 * which there was no rx rings property. For dynamic group
17510dc2366fSVenugopal Iyer 		 * if we are setting the same number of rings already set.
17520dc2366fSVenugopal Iyer 		 * For static group if we are requesting a group again.
17530dc2366fSVenugopal Iyer 		 */
17540dc2366fSVenugopal Iyer 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
17550dc2366fSVenugopal Iyer 			if (!(tmrp->mrp_mask & MRP_RX_RINGS))
17560dc2366fSVenugopal Iyer 				return (0);
17570dc2366fSVenugopal Iyer 		} else {
17580dc2366fSVenugopal Iyer 			if (unspec) {
17590dc2366fSVenugopal Iyer 				if (tmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
17600dc2366fSVenugopal Iyer 					return (0);
17610dc2366fSVenugopal Iyer 			} else if (mip->mi_rx_group_type ==
17620dc2366fSVenugopal Iyer 			    MAC_GROUP_TYPE_DYNAMIC) {
17630dc2366fSVenugopal Iyer 				if ((tmrp->mrp_mask & MRP_RX_RINGS) &&
17640dc2366fSVenugopal Iyer 				    !(tmrp->mrp_mask & MRP_RXRINGS_UNSPEC) &&
17650dc2366fSVenugopal Iyer 				    mrp->mrp_nrxrings == tmrp->mrp_nrxrings) {
17660dc2366fSVenugopal Iyer 					return (0);
17670dc2366fSVenugopal Iyer 				}
17680dc2366fSVenugopal Iyer 			}
17690dc2366fSVenugopal Iyer 		}
17700dc2366fSVenugopal Iyer 		/* Resetting the prop */
17710dc2366fSVenugopal Iyer 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
17720dc2366fSVenugopal Iyer 			/*
17730dc2366fSVenugopal Iyer 			 * We will just keep one ring and give others back if
17740dc2366fSVenugopal Iyer 			 * we are not the primary. For the primary we give
17750dc2366fSVenugopal Iyer 			 * all the rings in the default group except the
17760dc2366fSVenugopal Iyer 			 * default ring. If it is a static group, then
17770dc2366fSVenugopal Iyer 			 * we don't do anything, but clear the MRP_RX_RINGS
17780dc2366fSVenugopal Iyer 			 * flag.
17790dc2366fSVenugopal Iyer 			 */
17800dc2366fSVenugopal Iyer 			if (group != defgrp) {
17810dc2366fSVenugopal Iyer 				if (mip->mi_rx_group_type ==
17820dc2366fSVenugopal Iyer 				    MAC_GROUP_TYPE_DYNAMIC) {
17830dc2366fSVenugopal Iyer 					/*
17840dc2366fSVenugopal Iyer 					 * This group has reserved rings
17850dc2366fSVenugopal Iyer 					 * that need to be released now,
17860dc2366fSVenugopal Iyer 					 * so does the group.
17870dc2366fSVenugopal Iyer 					 */
17880dc2366fSVenugopal Iyer 					MAC_RX_RING_RELEASED(mip,
17890dc2366fSVenugopal Iyer 					    group->mrg_cur_count);
17900dc2366fSVenugopal Iyer 					MAC_RX_GRP_RELEASED(mip);
17910dc2366fSVenugopal Iyer 					if ((flent->fe_type &
17920dc2366fSVenugopal Iyer 					    FLOW_PRIMARY_MAC) != 0) {
17930dc2366fSVenugopal Iyer 						if (mip->mi_nactiveclients ==
17940dc2366fSVenugopal Iyer 						    1) {
17950dc2366fSVenugopal Iyer 							(void)
17960dc2366fSVenugopal Iyer 							    mac_rx_switch_group(
17970dc2366fSVenugopal Iyer 							    mcip, group,
17980dc2366fSVenugopal Iyer 							    defgrp);
17990dc2366fSVenugopal Iyer 							return (0);
18000dc2366fSVenugopal Iyer 						} else {
18010dc2366fSVenugopal Iyer 							cmrp->mrp_nrxrings =
18020dc2366fSVenugopal Iyer 							    group->
18030dc2366fSVenugopal Iyer 							    mrg_cur_count +
18040dc2366fSVenugopal Iyer 							    defgrp->
18050dc2366fSVenugopal Iyer 							    mrg_cur_count - 1;
18060dc2366fSVenugopal Iyer 						}
18070dc2366fSVenugopal Iyer 					} else {
18080dc2366fSVenugopal Iyer 						cmrp->mrp_nrxrings = 1;
18090dc2366fSVenugopal Iyer 					}
18100dc2366fSVenugopal Iyer 					(void) mac_group_ring_modify(mcip,
18110dc2366fSVenugopal Iyer 					    group, defgrp);
18120dc2366fSVenugopal Iyer 				} else {
18130dc2366fSVenugopal Iyer 					/*
18140dc2366fSVenugopal Iyer 					 * If this is a static group, we
18150dc2366fSVenugopal Iyer 					 * need to release the group. The
18160dc2366fSVenugopal Iyer 					 * client will remain in the same
18170dc2366fSVenugopal Iyer 					 * group till some other client
18180dc2366fSVenugopal Iyer 					 * needs this group.
18190dc2366fSVenugopal Iyer 					 */
18200dc2366fSVenugopal Iyer 					MAC_RX_GRP_RELEASED(mip);
18210dc2366fSVenugopal Iyer 				}
18220dc2366fSVenugopal Iyer 			/* Let check if we can give this an excl group */
18230dc2366fSVenugopal Iyer 			} else if (group == defgrp) {
182484de666eSRyan Zezeski 				/*
182584de666eSRyan Zezeski 				 * If multiple clients share an
182684de666eSRyan Zezeski 				 * address then they must stay on the
182784de666eSRyan Zezeski 				 * default group.
182884de666eSRyan Zezeski 				 */
182984de666eSRyan Zezeski 				if (mac_check_macaddr_shared(mcip->mci_unicast))
183084de666eSRyan Zezeski 					return (0);
183184de666eSRyan Zezeski 
18320dc2366fSVenugopal Iyer 				ngrp = mac_reserve_rx_group(mcip, mac_addr,
18330dc2366fSVenugopal Iyer 				    B_TRUE);
18340dc2366fSVenugopal Iyer 				/* Couldn't give it a group, that's fine */
18350dc2366fSVenugopal Iyer 				if (ngrp == NULL)
18360dc2366fSVenugopal Iyer 					return (0);
18370dc2366fSVenugopal Iyer 				/* Switch to H/W */
18380dc2366fSVenugopal Iyer 				if (mac_rx_switch_group(mcip, defgrp, ngrp) !=
18390dc2366fSVenugopal Iyer 				    0) {
18400dc2366fSVenugopal Iyer 					mac_stop_group(ngrp);
18410dc2366fSVenugopal Iyer 					return (0);
18420dc2366fSVenugopal Iyer 				}
18430dc2366fSVenugopal Iyer 			}
18440dc2366fSVenugopal Iyer 			/*
18450dc2366fSVenugopal Iyer 			 * If the client is in the default group, we will
18460dc2366fSVenugopal Iyer 			 * just clear the MRP_RX_RINGS and leave it as
18470dc2366fSVenugopal Iyer 			 * it rather than look for an exclusive group
18480dc2366fSVenugopal Iyer 			 * for it.
18490dc2366fSVenugopal Iyer 			 */
18500dc2366fSVenugopal Iyer 			return (0);
18510dc2366fSVenugopal Iyer 		}
18520dc2366fSVenugopal Iyer 
18530dc2366fSVenugopal Iyer 		if (group == defgrp && ((mrp->mrp_nrxrings > 0) || unspec)) {
185484de666eSRyan Zezeski 			/*
185584de666eSRyan Zezeski 			 * We are requesting Rx rings. Try to reserve
185684de666eSRyan Zezeski 			 * a non-default group.
185784de666eSRyan Zezeski 			 *
185884de666eSRyan Zezeski 			 * If multiple clients share an address then
185984de666eSRyan Zezeski 			 * they must stay on the default group.
186084de666eSRyan Zezeski 			 */
186184de666eSRyan Zezeski 			if (mac_check_macaddr_shared(mcip->mci_unicast))
186284de666eSRyan Zezeski 				return (EINVAL);
186384de666eSRyan Zezeski 
18640dc2366fSVenugopal Iyer 			ngrp = mac_reserve_rx_group(mcip, mac_addr, B_TRUE);
18650dc2366fSVenugopal Iyer 			if (ngrp == NULL)
18660dc2366fSVenugopal Iyer 				return (ENOSPC);
18670dc2366fSVenugopal Iyer 
18680dc2366fSVenugopal Iyer 			/* Switch to H/W */
18690dc2366fSVenugopal Iyer 			if (mac_rx_switch_group(mcip, defgrp, ngrp) != 0) {
18700dc2366fSVenugopal Iyer 				mac_release_rx_group(mcip, ngrp);
18710dc2366fSVenugopal Iyer 				return (ENOSPC);
18720dc2366fSVenugopal Iyer 			}
18730dc2366fSVenugopal Iyer 			MAC_RX_GRP_RESERVED(mip);
18740dc2366fSVenugopal Iyer 			if (mip->mi_rx_group_type == MAC_GROUP_TYPE_DYNAMIC)
18750dc2366fSVenugopal Iyer 				MAC_RX_RING_RESERVED(mip, ngrp->mrg_cur_count);
18760dc2366fSVenugopal Iyer 		} else if (group != defgrp && !unspec &&
18770dc2366fSVenugopal Iyer 		    mrp->mrp_nrxrings == 0) {
18780dc2366fSVenugopal Iyer 			/* Switch to S/W */
18790dc2366fSVenugopal Iyer 			ringcnt = group->mrg_cur_count;
18800dc2366fSVenugopal Iyer 			if (mac_rx_switch_group(mcip, group, defgrp) != 0)
18810dc2366fSVenugopal Iyer 				return (ENOSPC);
18820dc2366fSVenugopal Iyer 			if (tmrp->mrp_mask & MRP_RX_RINGS) {
18830dc2366fSVenugopal Iyer 				MAC_RX_GRP_RELEASED(mip);
18840dc2366fSVenugopal Iyer 				if (mip->mi_rx_group_type ==
18850dc2366fSVenugopal Iyer 				    MAC_GROUP_TYPE_DYNAMIC) {
18860dc2366fSVenugopal Iyer 					MAC_RX_RING_RELEASED(mip, ringcnt);
18870dc2366fSVenugopal Iyer 				}
18880dc2366fSVenugopal Iyer 			}
18890dc2366fSVenugopal Iyer 		} else if (group != defgrp && mip->mi_rx_group_type ==
18900dc2366fSVenugopal Iyer 		    MAC_GROUP_TYPE_DYNAMIC) {
18910dc2366fSVenugopal Iyer 			ringcnt = group->mrg_cur_count;
18920dc2366fSVenugopal Iyer 			err = mac_group_ring_modify(mcip, group, defgrp);
18930dc2366fSVenugopal Iyer 			if (err != 0)
18940dc2366fSVenugopal Iyer 				return (err);
18950dc2366fSVenugopal Iyer 			/*
18960dc2366fSVenugopal Iyer 			 * Update the accounting. If this group
18970dc2366fSVenugopal Iyer 			 * already had explicitly reserved rings,
18980dc2366fSVenugopal Iyer 			 * we need to update the rings based on
18990dc2366fSVenugopal Iyer 			 * the new ring count. If this group
19000dc2366fSVenugopal Iyer 			 * had not explicitly reserved rings,
19010dc2366fSVenugopal Iyer 			 * then we just reserve the rings asked for
19020dc2366fSVenugopal Iyer 			 * and reserve the group.
19030dc2366fSVenugopal Iyer 			 */
19040dc2366fSVenugopal Iyer 			if (tmrp->mrp_mask & MRP_RX_RINGS) {
19050dc2366fSVenugopal Iyer 				if (ringcnt > group->mrg_cur_count) {
19060dc2366fSVenugopal Iyer 					MAC_RX_RING_RELEASED(mip,
19070dc2366fSVenugopal Iyer 					    ringcnt - group->mrg_cur_count);
19080dc2366fSVenugopal Iyer 				} else {
19090dc2366fSVenugopal Iyer 					MAC_RX_RING_RESERVED(mip,
19100dc2366fSVenugopal Iyer 					    group->mrg_cur_count - ringcnt);
19110dc2366fSVenugopal Iyer 				}
19120dc2366fSVenugopal Iyer 			} else {
19130dc2366fSVenugopal Iyer 				MAC_RX_RING_RESERVED(mip, group->mrg_cur_count);
19140dc2366fSVenugopal Iyer 				MAC_RX_GRP_RESERVED(mip);
19150dc2366fSVenugopal Iyer 			}
19160dc2366fSVenugopal Iyer 		}
19170dc2366fSVenugopal Iyer 	}
19180dc2366fSVenugopal Iyer 	if (mrp->mrp_mask & MRP_TX_RINGS) {
19190dc2366fSVenugopal Iyer 		unspec = mrp->mrp_mask & MRP_TXRINGS_UNSPEC;
19200dc2366fSVenugopal Iyer 		group = flent->fe_tx_ring_group;
19210dc2366fSVenugopal Iyer 		defgrp = MAC_DEFAULT_TX_GROUP(mip);
19220dc2366fSVenugopal Iyer 
19230dc2366fSVenugopal Iyer 		/*
19240dc2366fSVenugopal Iyer 		 * For static groups we only allow rings=0 or resetting the
19250dc2366fSVenugopal Iyer 		 * rings property.
19260dc2366fSVenugopal Iyer 		 */
19270dc2366fSVenugopal Iyer 		if (mrp->mrp_ntxrings > 0 &&
19280dc2366fSVenugopal Iyer 		    mip->mi_tx_group_type != MAC_GROUP_TYPE_DYNAMIC) {
19290dc2366fSVenugopal Iyer 			return (ENOTSUP);
19300dc2366fSVenugopal Iyer 		}
19310dc2366fSVenugopal Iyer 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
19320dc2366fSVenugopal Iyer 			if (!(tmrp->mrp_mask & MRP_TX_RINGS))
19330dc2366fSVenugopal Iyer 				return (0);
19340dc2366fSVenugopal Iyer 		} else {
19350dc2366fSVenugopal Iyer 			if (unspec) {
19360dc2366fSVenugopal Iyer 				if (tmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
19370dc2366fSVenugopal Iyer 					return (0);
19380dc2366fSVenugopal Iyer 			} else if (mip->mi_tx_group_type ==
19390dc2366fSVenugopal Iyer 			    MAC_GROUP_TYPE_DYNAMIC) {
19400dc2366fSVenugopal Iyer 				if ((tmrp->mrp_mask & MRP_TX_RINGS) &&
19410dc2366fSVenugopal Iyer 				    !(tmrp->mrp_mask & MRP_TXRINGS_UNSPEC) &&
19420dc2366fSVenugopal Iyer 				    mrp->mrp_ntxrings == tmrp->mrp_ntxrings) {
19430dc2366fSVenugopal Iyer 					return (0);
19440dc2366fSVenugopal Iyer 				}
19450dc2366fSVenugopal Iyer 			}
19460dc2366fSVenugopal Iyer 		}
19470dc2366fSVenugopal Iyer 		/* Resetting the prop */
19480dc2366fSVenugopal Iyer 		if (mrp->mrp_mask & MRP_RINGS_RESET) {
19490dc2366fSVenugopal Iyer 			if (group != defgrp) {
19500dc2366fSVenugopal Iyer 				if (mip->mi_tx_group_type ==
19510dc2366fSVenugopal Iyer 				    MAC_GROUP_TYPE_DYNAMIC) {
19520dc2366fSVenugopal Iyer 					ringcnt = group->mrg_cur_count;
19530dc2366fSVenugopal Iyer 					if ((flent->fe_type &
19540dc2366fSVenugopal Iyer 					    FLOW_PRIMARY_MAC) != 0) {
19550dc2366fSVenugopal Iyer 						mac_tx_client_quiesce(
19560dc2366fSVenugopal Iyer 						    (mac_client_handle_t)
19570dc2366fSVenugopal Iyer 						    mcip);
19580dc2366fSVenugopal Iyer 						mac_tx_switch_group(mcip,
19590dc2366fSVenugopal Iyer 						    group, defgrp);
19600dc2366fSVenugopal Iyer 						mac_tx_client_restart(
19610dc2366fSVenugopal Iyer 						    (mac_client_handle_t)
19620dc2366fSVenugopal Iyer 						    mcip);
19630dc2366fSVenugopal Iyer 						MAC_TX_GRP_RELEASED(mip);
19640dc2366fSVenugopal Iyer 						MAC_TX_RING_RELEASED(mip,
19650dc2366fSVenugopal Iyer 						    ringcnt);
19660dc2366fSVenugopal Iyer 						return (0);
19670dc2366fSVenugopal Iyer 					}
19680dc2366fSVenugopal Iyer 					cmrp->mrp_ntxrings = 1;
19690dc2366fSVenugopal Iyer 					(void) mac_group_ring_modify(mcip,
19700dc2366fSVenugopal Iyer 					    group, defgrp);
19710dc2366fSVenugopal Iyer 					/*
19720dc2366fSVenugopal Iyer 					 * This group has reserved rings
19730dc2366fSVenugopal Iyer 					 * that need to be released now.
19740dc2366fSVenugopal Iyer 					 */
19750dc2366fSVenugopal Iyer 					MAC_TX_RING_RELEASED(mip, ringcnt);
19760dc2366fSVenugopal Iyer 				}
19770dc2366fSVenugopal Iyer 				/*
19780dc2366fSVenugopal Iyer 				 * If this is a static group, we
19790dc2366fSVenugopal Iyer 				 * need to release the group. The
19800dc2366fSVenugopal Iyer 				 * client will remain in the same
19810dc2366fSVenugopal Iyer 				 * group till some other client
19820dc2366fSVenugopal Iyer 				 * needs this group.
19830dc2366fSVenugopal Iyer 				 */
19840dc2366fSVenugopal Iyer 				MAC_TX_GRP_RELEASED(mip);
19850dc2366fSVenugopal Iyer 			} else if (group == defgrp &&
19860dc2366fSVenugopal Iyer 			    (flent->fe_type & FLOW_PRIMARY_MAC) == 0) {
19870dc2366fSVenugopal Iyer 				ngrp = mac_reserve_tx_group(mcip, B_TRUE);
19880dc2366fSVenugopal Iyer 				if (ngrp == NULL)
19890dc2366fSVenugopal Iyer 					return (0);
19900dc2366fSVenugopal Iyer 				mac_tx_client_quiesce(
19910dc2366fSVenugopal Iyer 				    (mac_client_handle_t)mcip);
19920dc2366fSVenugopal Iyer 				mac_tx_switch_group(mcip, defgrp, ngrp);
19930dc2366fSVenugopal Iyer 				mac_tx_client_restart(
19940dc2366fSVenugopal Iyer 				    (mac_client_handle_t)mcip);
19950dc2366fSVenugopal Iyer 			}
19960dc2366fSVenugopal Iyer 			/*
19970dc2366fSVenugopal Iyer 			 * If the client is in the default group, we will
19980dc2366fSVenugopal Iyer 			 * just clear the MRP_TX_RINGS and leave it as
19990dc2366fSVenugopal Iyer 			 * it rather than look for an exclusive group
20000dc2366fSVenugopal Iyer 			 * for it.
20010dc2366fSVenugopal Iyer 			 */
20020dc2366fSVenugopal Iyer 			return (0);
20030dc2366fSVenugopal Iyer 		}
20040dc2366fSVenugopal Iyer 
20050dc2366fSVenugopal Iyer 		/* Switch to H/W */
20060dc2366fSVenugopal Iyer 		if (group == defgrp && ((mrp->mrp_ntxrings > 0) || unspec)) {
20070dc2366fSVenugopal Iyer 			ngrp = mac_reserve_tx_group(mcip, B_TRUE);
20080dc2366fSVenugopal Iyer 			if (ngrp == NULL)
20090dc2366fSVenugopal Iyer 				return (ENOSPC);
20100dc2366fSVenugopal Iyer 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
20110dc2366fSVenugopal Iyer 			mac_tx_switch_group(mcip, defgrp, ngrp);
20120dc2366fSVenugopal Iyer 			mac_tx_client_restart((mac_client_handle_t)mcip);
20130dc2366fSVenugopal Iyer 			MAC_TX_GRP_RESERVED(mip);
20140dc2366fSVenugopal Iyer 			if (mip->mi_tx_group_type == MAC_GROUP_TYPE_DYNAMIC)
20150dc2366fSVenugopal Iyer 				MAC_TX_RING_RESERVED(mip, ngrp->mrg_cur_count);
20160dc2366fSVenugopal Iyer 		/* Switch to S/W */
20170dc2366fSVenugopal Iyer 		} else if (group != defgrp && !unspec &&
20180dc2366fSVenugopal Iyer 		    mrp->mrp_ntxrings == 0) {
20190dc2366fSVenugopal Iyer 			/* Switch to S/W */
20200dc2366fSVenugopal Iyer 			ringcnt = group->mrg_cur_count;
20210dc2366fSVenugopal Iyer 			mac_tx_client_quiesce((mac_client_handle_t)mcip);
20220dc2366fSVenugopal Iyer 			mac_tx_switch_group(mcip, group, defgrp);
20230dc2366fSVenugopal Iyer 			mac_tx_client_restart((mac_client_handle_t)mcip);
20240dc2366fSVenugopal Iyer 			if (tmrp->mrp_mask & MRP_TX_RINGS) {
20250dc2366fSVenugopal Iyer 				MAC_TX_GRP_RELEASED(mip);
20260dc2366fSVenugopal Iyer 				if (mip->mi_tx_group_type ==
20270dc2366fSVenugopal Iyer 				    MAC_GROUP_TYPE_DYNAMIC) {
20280dc2366fSVenugopal Iyer 					MAC_TX_RING_RELEASED(mip, ringcnt);
20290dc2366fSVenugopal Iyer 				}
20300dc2366fSVenugopal Iyer 			}
20310dc2366fSVenugopal Iyer 		} else if (group != defgrp && mip->mi_tx_group_type ==
20320dc2366fSVenugopal Iyer 		    MAC_GROUP_TYPE_DYNAMIC) {
20330dc2366fSVenugopal Iyer 			ringcnt = group->mrg_cur_count;
20340dc2366fSVenugopal Iyer 			err = mac_group_ring_modify(mcip, group, defgrp);
20350dc2366fSVenugopal Iyer 			if (err != 0)
20360dc2366fSVenugopal Iyer 				return (err);
20370dc2366fSVenugopal Iyer 			/*
20380dc2366fSVenugopal Iyer 			 * Update the accounting. If this group
20390dc2366fSVenugopal Iyer 			 * already had explicitly reserved rings,
20400dc2366fSVenugopal Iyer 			 * we need to update the rings based on
20410dc2366fSVenugopal Iyer 			 * the new ring count. If this group
20420dc2366fSVenugopal Iyer 			 * had not explicitly reserved rings,
20430dc2366fSVenugopal Iyer 			 * then we just reserve the rings asked for
20440dc2366fSVenugopal Iyer 			 * and reserve the group.
20450dc2366fSVenugopal Iyer 			 */
20460dc2366fSVenugopal Iyer 			if (tmrp->mrp_mask & MRP_TX_RINGS) {
20470dc2366fSVenugopal Iyer 				if (ringcnt > group->mrg_cur_count) {
20480dc2366fSVenugopal Iyer 					MAC_TX_RING_RELEASED(mip,
20490dc2366fSVenugopal Iyer 					    ringcnt - group->mrg_cur_count);
20500dc2366fSVenugopal Iyer 				} else {
20510dc2366fSVenugopal Iyer 					MAC_TX_RING_RESERVED(mip,
20520dc2366fSVenugopal Iyer 					    group->mrg_cur_count - ringcnt);
20530dc2366fSVenugopal Iyer 				}
20540dc2366fSVenugopal Iyer 			} else {
20550dc2366fSVenugopal Iyer 				MAC_TX_RING_RESERVED(mip, group->mrg_cur_count);
20560dc2366fSVenugopal Iyer 				MAC_TX_GRP_RESERVED(mip);
20570dc2366fSVenugopal Iyer 			}
20580dc2366fSVenugopal Iyer 		}
20590dc2366fSVenugopal Iyer 	}
20600dc2366fSVenugopal Iyer 	return (0);
20610dc2366fSVenugopal Iyer }
20620dc2366fSVenugopal Iyer 
20630dc2366fSVenugopal Iyer /*
2064da14cebeSEric Cheng  * When the MAC client is being brought up (i.e. we do a unicast_add) we need
2065da14cebeSEric Cheng  * to initialize the cpu and resource control structure in the
2066da14cebeSEric Cheng  * mac_client_impl_t from the mac_impl_t (i.e if there are any cached
2067da14cebeSEric Cheng  * properties before the flow entry for the unicast address was created).
2068da14cebeSEric Cheng  */
20691a41ca23SJerry Jelinek static int
mac_resource_ctl_set(mac_client_handle_t mch,mac_resource_props_t * mrp)2070da14cebeSEric Cheng mac_resource_ctl_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
2071da14cebeSEric Cheng {
2072da14cebeSEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2073da14cebeSEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mcip->mci_mip;
20741a41ca23SJerry Jelinek 	mac_impl_t		*umip = mcip->mci_upper_mip;
2075da14cebeSEric Cheng 	int			err = 0;
20760dc2366fSVenugopal Iyer 	flow_entry_t		*flent = mcip->mci_flent;
20770dc2366fSVenugopal Iyer 	mac_resource_props_t	*omrp, *nmrp = MCIP_RESOURCE_PROPS(mcip);
2078da14cebeSEric Cheng 
2079da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2080da14cebeSEric Cheng 
20810dc2366fSVenugopal Iyer 	err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
20820dc2366fSVenugopal Iyer 	    mcip->mci_upper_mip : mip, mrp);
2083da14cebeSEric Cheng 	if (err != 0)
2084da14cebeSEric Cheng 		return (err);
2085da14cebeSEric Cheng 
20860dc2366fSVenugopal Iyer 	/*
20870dc2366fSVenugopal Iyer 	 * Copy over the existing properties since mac_update_resources
20880dc2366fSVenugopal Iyer 	 * will modify the client's mrp. Currently, the saved property
20890dc2366fSVenugopal Iyer 	 * is used to determine the difference between existing and
20900dc2366fSVenugopal Iyer 	 * modified rings property.
20910dc2366fSVenugopal Iyer 	 */
20920dc2366fSVenugopal Iyer 	omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
20930dc2366fSVenugopal Iyer 	bcopy(nmrp, omrp, sizeof (*omrp));
2094da14cebeSEric Cheng 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
2095da14cebeSEric Cheng 	if (MCIP_DATAPATH_SETUP(mcip)) {
2096da14cebeSEric Cheng 		/*
20970dc2366fSVenugopal Iyer 		 * We support rings only for primary client when there are
20980dc2366fSVenugopal Iyer 		 * multiple clients sharing the same MAC address (e.g. VLAN).
20990dc2366fSVenugopal Iyer 		 */
21000dc2366fSVenugopal Iyer 		if (mrp->mrp_mask & MRP_RX_RINGS ||
21010dc2366fSVenugopal Iyer 		    mrp->mrp_mask & MRP_TX_RINGS) {
21020dc2366fSVenugopal Iyer 
21030dc2366fSVenugopal Iyer 			if ((err = mac_client_set_rings_prop(mcip, mrp,
21040dc2366fSVenugopal Iyer 			    omrp)) != 0) {
21050dc2366fSVenugopal Iyer 				if (omrp->mrp_mask & MRP_RX_RINGS) {
21060dc2366fSVenugopal Iyer 					nmrp->mrp_mask |= MRP_RX_RINGS;
21070dc2366fSVenugopal Iyer 					nmrp->mrp_nrxrings = omrp->mrp_nrxrings;
21080dc2366fSVenugopal Iyer 				} else {
21090dc2366fSVenugopal Iyer 					nmrp->mrp_mask &= ~MRP_RX_RINGS;
21100dc2366fSVenugopal Iyer 					nmrp->mrp_nrxrings = 0;
21110dc2366fSVenugopal Iyer 				}
21120dc2366fSVenugopal Iyer 				if (omrp->mrp_mask & MRP_TX_RINGS) {
21130dc2366fSVenugopal Iyer 					nmrp->mrp_mask |= MRP_TX_RINGS;
21140dc2366fSVenugopal Iyer 					nmrp->mrp_ntxrings = omrp->mrp_ntxrings;
21150dc2366fSVenugopal Iyer 				} else {
21160dc2366fSVenugopal Iyer 					nmrp->mrp_mask &= ~MRP_TX_RINGS;
21170dc2366fSVenugopal Iyer 					nmrp->mrp_ntxrings = 0;
21180dc2366fSVenugopal Iyer 				}
21190dc2366fSVenugopal Iyer 				if (omrp->mrp_mask & MRP_RXRINGS_UNSPEC)
21200dc2366fSVenugopal Iyer 					omrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
21210dc2366fSVenugopal Iyer 				else
21220dc2366fSVenugopal Iyer 					omrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
21230dc2366fSVenugopal Iyer 
21240dc2366fSVenugopal Iyer 				if (omrp->mrp_mask & MRP_TXRINGS_UNSPEC)
21250dc2366fSVenugopal Iyer 					omrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
21260dc2366fSVenugopal Iyer 				else
21270dc2366fSVenugopal Iyer 					omrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
21280dc2366fSVenugopal Iyer 				kmem_free(omrp, sizeof (*omrp));
21290dc2366fSVenugopal Iyer 				return (err);
21300dc2366fSVenugopal Iyer 			}
21310dc2366fSVenugopal Iyer 
21320dc2366fSVenugopal Iyer 			/*
21330dc2366fSVenugopal Iyer 			 * If we modified the rings property of the primary
21340dc2366fSVenugopal Iyer 			 * we need to update the property fields of its
21350dc2366fSVenugopal Iyer 			 * VLANs as they inherit the primary's properites.
21360dc2366fSVenugopal Iyer 			 */
21370dc2366fSVenugopal Iyer 			if (mac_is_primary_client(mcip)) {
21380dc2366fSVenugopal Iyer 				mac_set_prim_vlan_rings(mip,
21390dc2366fSVenugopal Iyer 				    MCIP_RESOURCE_PROPS(mcip));
21400dc2366fSVenugopal Iyer 			}
21410dc2366fSVenugopal Iyer 		}
21420dc2366fSVenugopal Iyer 		/*
2143da14cebeSEric Cheng 		 * We have to set this prior to calling mac_flow_modify.
2144da14cebeSEric Cheng 		 */
2145da14cebeSEric Cheng 		if (mrp->mrp_mask & MRP_PRIORITY) {
2146da14cebeSEric Cheng 			if (mrp->mrp_priority == MPL_RESET) {
2147da14cebeSEric Cheng 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
2148da14cebeSEric Cheng 				    MPL_LINK_DEFAULT);
2149da14cebeSEric Cheng 			} else {
2150da14cebeSEric Cheng 				MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
2151da14cebeSEric Cheng 				    mrp->mrp_priority);
2152da14cebeSEric Cheng 			}
2153da14cebeSEric Cheng 		}
2154da14cebeSEric Cheng 
21550dc2366fSVenugopal Iyer 		mac_flow_modify(mip->mi_flow_tab, flent, mrp);
2156da14cebeSEric Cheng 		if (mrp->mrp_mask & MRP_PRIORITY)
2157da14cebeSEric Cheng 			mac_update_subflow_priority(mcip);
21581a41ca23SJerry Jelinek 
21591a41ca23SJerry Jelinek 		/* Apply these resource settings to any secondary macs */
21601a41ca23SJerry Jelinek 		if (umip != NULL) {
21611a41ca23SJerry Jelinek 			ASSERT((umip->mi_state_flags & MIS_IS_VNIC) != 0);
21621a41ca23SJerry Jelinek 			mac_vnic_secondary_update(umip);
21631a41ca23SJerry Jelinek 		}
2164da14cebeSEric Cheng 	}
21650dc2366fSVenugopal Iyer 	kmem_free(omrp, sizeof (*omrp));
2166da14cebeSEric Cheng 	return (0);
2167da14cebeSEric Cheng }
2168da14cebeSEric Cheng 
2169da14cebeSEric Cheng static int
mac_unicast_flow_create(mac_client_impl_t * mcip,uint8_t * mac_addr,uint16_t vid,boolean_t is_primary,boolean_t first_flow,flow_entry_t ** flent,mac_resource_props_t * mrp)2170da14cebeSEric Cheng mac_unicast_flow_create(mac_client_impl_t *mcip, uint8_t *mac_addr,
2171da14cebeSEric Cheng     uint16_t vid, boolean_t is_primary, boolean_t first_flow,
2172da14cebeSEric Cheng     flow_entry_t **flent, mac_resource_props_t *mrp)
2173da14cebeSEric Cheng {
2174da14cebeSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mcip->mci_mip;
2175da14cebeSEric Cheng 	flow_desc_t	flow_desc;
2176da000602SGirish Moodalbail 	char		flowname[MAXFLOWNAMELEN];
2177da14cebeSEric Cheng 	int		err;
2178da14cebeSEric Cheng 	uint_t		flent_flags;
2179da14cebeSEric Cheng 
2180da14cebeSEric Cheng 	/*
2181da14cebeSEric Cheng 	 * First unicast address being added, create a new flow
2182da14cebeSEric Cheng 	 * for that MAC client.
2183da14cebeSEric Cheng 	 */
2184da14cebeSEric Cheng 	bzero(&flow_desc, sizeof (flow_desc));
2185da14cebeSEric Cheng 
21860dc2366fSVenugopal Iyer 	ASSERT(mac_addr != NULL ||
21870dc2366fSVenugopal Iyer 	    (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR));
21880dc2366fSVenugopal Iyer 	if (mac_addr != NULL) {
2189da14cebeSEric Cheng 		flow_desc.fd_mac_len = mip->mi_type->mt_addr_length;
2190da14cebeSEric Cheng 		bcopy(mac_addr, flow_desc.fd_dst_mac, flow_desc.fd_mac_len);
21910dc2366fSVenugopal Iyer 	}
2192da14cebeSEric Cheng 	flow_desc.fd_mask = FLOW_LINK_DST;
2193da14cebeSEric Cheng 	if (vid != 0) {
2194da14cebeSEric Cheng 		flow_desc.fd_vid = vid;
2195da14cebeSEric Cheng 		flow_desc.fd_mask |= FLOW_LINK_VID;
2196da14cebeSEric Cheng 	}
2197da14cebeSEric Cheng 
2198da14cebeSEric Cheng 	/*
2199da14cebeSEric Cheng 	 * XXX-nicolas. For now I'm keeping the FLOW_PRIMARY_MAC
2200da14cebeSEric Cheng 	 * and FLOW_VNIC. Even though they're a hack inherited
2201da14cebeSEric Cheng 	 * from the SRS code, we'll keep them for now. They're currently
2202da14cebeSEric Cheng 	 * consumed by mac_datapath_setup() to create the SRS.
2203da14cebeSEric Cheng 	 * That code should be eventually moved out of
2204da14cebeSEric Cheng 	 * mac_datapath_setup() and moved to a mac_srs_create()
2205da14cebeSEric Cheng 	 * function of some sort to keep things clean.
2206da14cebeSEric Cheng 	 *
2207da14cebeSEric Cheng 	 * Also, there's no reason why the SRS for the primary MAC
2208da14cebeSEric Cheng 	 * client should be different than any other MAC client. Until
2209da14cebeSEric Cheng 	 * this is cleaned-up, we support only one MAC unicast address
2210da14cebeSEric Cheng 	 * per client.
2211da14cebeSEric Cheng 	 *
2212da14cebeSEric Cheng 	 * We set FLOW_PRIMARY_MAC for the primary MAC address,
2213da14cebeSEric Cheng 	 * FLOW_VNIC for everything else.
2214da14cebeSEric Cheng 	 */
2215da14cebeSEric Cheng 	if (is_primary)
2216da14cebeSEric Cheng 		flent_flags = FLOW_PRIMARY_MAC;
2217da14cebeSEric Cheng 	else
2218da14cebeSEric Cheng 		flent_flags = FLOW_VNIC_MAC;
2219da14cebeSEric Cheng 
2220da14cebeSEric Cheng 	/*
222184de666eSRyan Zezeski 	 * For the first flow we use the MAC client's name - mci_name, for
222284de666eSRyan Zezeski 	 * subsequent ones we just create a name with the VID. This is
2223da14cebeSEric Cheng 	 * so that we can add these flows to the same flow table. This is
222484de666eSRyan Zezeski 	 * fine as the flow name (except for the one with the MAC client's
2225da14cebeSEric Cheng 	 * name) is not visible. When the first flow is removed, we just replace
2226da14cebeSEric Cheng 	 * its fdesc with another from the list, so we will still retain the
2227da14cebeSEric Cheng 	 * flent with the MAC client's flow name.
2228da14cebeSEric Cheng 	 */
2229da14cebeSEric Cheng 	if (first_flow) {
2230da000602SGirish Moodalbail 		bcopy(mcip->mci_name, flowname, MAXFLOWNAMELEN);
2231da14cebeSEric Cheng 	} else {
2232da14cebeSEric Cheng 		(void) sprintf(flowname, "%s%u", mcip->mci_name, vid);
2233da14cebeSEric Cheng 		flent_flags = FLOW_NO_STATS;
2234da14cebeSEric Cheng 	}
2235da14cebeSEric Cheng 
2236da14cebeSEric Cheng 	if ((err = mac_flow_create(&flow_desc, mrp, flowname, NULL,
2237da14cebeSEric Cheng 	    flent_flags, flent)) != 0)
2238da14cebeSEric Cheng 		return (err);
2239da14cebeSEric Cheng 
22400dc2366fSVenugopal Iyer 	mac_misc_stat_create(*flent);
2241da14cebeSEric Cheng 	FLOW_MARK(*flent, FE_INCIPIENT);
2242da14cebeSEric Cheng 	(*flent)->fe_mcip = mcip;
2243da14cebeSEric Cheng 
2244da14cebeSEric Cheng 	/*
2245da14cebeSEric Cheng 	 * Place initial creation reference on the flow. This reference
2246da14cebeSEric Cheng 	 * is released in the corresponding delete action viz.
2247da14cebeSEric Cheng 	 * mac_unicast_remove after waiting for all transient refs to
2248da14cebeSEric Cheng 	 * to go away. The wait happens in mac_flow_wait.
2249da14cebeSEric Cheng 	 * We have already held the reference in mac_client_open().
2250da14cebeSEric Cheng 	 */
2251da14cebeSEric Cheng 	if (!first_flow)
2252da14cebeSEric Cheng 		FLOW_REFHOLD(*flent);
2253da14cebeSEric Cheng 	return (0);
2254da14cebeSEric Cheng }
2255da14cebeSEric Cheng 
2256da14cebeSEric Cheng /* Refresh the multicast grouping for this VID. */
2257da14cebeSEric Cheng int
mac_client_update_mcast(void * arg,boolean_t add,const uint8_t * addrp)2258da14cebeSEric Cheng mac_client_update_mcast(void *arg, boolean_t add, const uint8_t *addrp)
2259da14cebeSEric Cheng {
2260da14cebeSEric Cheng 	flow_entry_t		*flent = arg;
2261da14cebeSEric Cheng 	mac_client_impl_t	*mcip = flent->fe_mcip;
2262da14cebeSEric Cheng 	uint16_t		vid;
2263da14cebeSEric Cheng 	flow_desc_t		flow_desc;
2264da14cebeSEric Cheng 
2265da14cebeSEric Cheng 	mac_flow_get_desc(flent, &flow_desc);
2266da14cebeSEric Cheng 	vid = (flow_desc.fd_mask & FLOW_LINK_VID) != 0 ?
2267da14cebeSEric Cheng 	    flow_desc.fd_vid : VLAN_ID_NONE;
2268da14cebeSEric Cheng 
2269da14cebeSEric Cheng 	/*
2270da14cebeSEric Cheng 	 * We don't call mac_multicast_add()/mac_multicast_remove() as
2271da14cebeSEric Cheng 	 * we want to add/remove for this specific vid.
2272da14cebeSEric Cheng 	 */
2273da14cebeSEric Cheng 	if (add) {
2274da14cebeSEric Cheng 		return (mac_bcast_add(mcip, addrp, vid,
2275da14cebeSEric Cheng 		    MAC_ADDRTYPE_MULTICAST));
2276da14cebeSEric Cheng 	} else {
2277da14cebeSEric Cheng 		mac_bcast_delete(mcip, addrp, vid);
2278da14cebeSEric Cheng 		return (0);
2279da14cebeSEric Cheng 	}
2280da14cebeSEric Cheng }
2281da14cebeSEric Cheng 
2282ae6aa22aSVenugopal Iyer static void
mac_update_single_active_client(mac_impl_t * mip)2283ae6aa22aSVenugopal Iyer mac_update_single_active_client(mac_impl_t *mip)
2284ae6aa22aSVenugopal Iyer {
2285ae6aa22aSVenugopal Iyer 	mac_client_impl_t *client = NULL;
2286ae6aa22aSVenugopal Iyer 
2287ae6aa22aSVenugopal Iyer 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
2288ae6aa22aSVenugopal Iyer 
2289ae6aa22aSVenugopal Iyer 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
2290ae6aa22aSVenugopal Iyer 	if (mip->mi_nactiveclients == 1) {
2291ae6aa22aSVenugopal Iyer 		/*
2292ae6aa22aSVenugopal Iyer 		 * Find the one active MAC client from the list of MAC
2293ae6aa22aSVenugopal Iyer 		 * clients. The active MAC client has at least one
2294ae6aa22aSVenugopal Iyer 		 * unicast address.
2295ae6aa22aSVenugopal Iyer 		 */
2296ae6aa22aSVenugopal Iyer 		for (client = mip->mi_clients_list; client != NULL;
2297ae6aa22aSVenugopal Iyer 		    client = client->mci_client_next) {
2298ae6aa22aSVenugopal Iyer 			if (client->mci_unicast_list != NULL)
2299ae6aa22aSVenugopal Iyer 				break;
2300ae6aa22aSVenugopal Iyer 		}
2301ae6aa22aSVenugopal Iyer 		ASSERT(client != NULL);
2302ae6aa22aSVenugopal Iyer 	}
2303ae6aa22aSVenugopal Iyer 
2304ae6aa22aSVenugopal Iyer 	/*
2305ae6aa22aSVenugopal Iyer 	 * mi_single_active_client is protected by the MAC impl's read/writer
2306ae6aa22aSVenugopal Iyer 	 * lock, which allows mac_rx() to check the value of that pointer
2307ae6aa22aSVenugopal Iyer 	 * as a reader.
2308ae6aa22aSVenugopal Iyer 	 */
2309ae6aa22aSVenugopal Iyer 	mip->mi_single_active_client = client;
2310ae6aa22aSVenugopal Iyer 	rw_exit(&mip->mi_rw_lock);
2311ae6aa22aSVenugopal Iyer }
2312ae6aa22aSVenugopal Iyer 
2313da14cebeSEric Cheng /*
2314fc4e975dSVenugopal Iyer  * Set up the data path. Called from i_mac_unicast_add after having
2315fc4e975dSVenugopal Iyer  * done all the validations including making sure this is an active
2316fc4e975dSVenugopal Iyer  * client (i.e that is ready to process packets.)
2317fc4e975dSVenugopal Iyer  */
2318fc4e975dSVenugopal Iyer static int
mac_client_datapath_setup(mac_client_impl_t * mcip,uint16_t vid,uint8_t * mac_addr,mac_resource_props_t * mrp,boolean_t isprimary,mac_unicast_impl_t * muip)2319fc4e975dSVenugopal Iyer mac_client_datapath_setup(mac_client_impl_t *mcip, uint16_t vid,
2320fc4e975dSVenugopal Iyer     uint8_t *mac_addr, mac_resource_props_t *mrp, boolean_t isprimary,
2321fc4e975dSVenugopal Iyer     mac_unicast_impl_t *muip)
2322fc4e975dSVenugopal Iyer {
2323fc4e975dSVenugopal Iyer 	mac_impl_t	*mip = mcip->mci_mip;
2324fc4e975dSVenugopal Iyer 	boolean_t	mac_started = B_FALSE;
2325fc4e975dSVenugopal Iyer 	boolean_t	bcast_added = B_FALSE;
2326fc4e975dSVenugopal Iyer 	boolean_t	nactiveclients_added = B_FALSE;
2327fc4e975dSVenugopal Iyer 	flow_entry_t	*flent;
2328fc4e975dSVenugopal Iyer 	int		err = 0;
23290dc2366fSVenugopal Iyer 	boolean_t	no_unicast;
23300dc2366fSVenugopal Iyer 
23310dc2366fSVenugopal Iyer 	no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
2332fc4e975dSVenugopal Iyer 
2333fc4e975dSVenugopal Iyer 	if ((err = mac_start((mac_handle_t)mip)) != 0)
2334fc4e975dSVenugopal Iyer 		goto bail;
2335fc4e975dSVenugopal Iyer 
2336fc4e975dSVenugopal Iyer 	mac_started = B_TRUE;
2337fc4e975dSVenugopal Iyer 
2338fc4e975dSVenugopal Iyer 	/* add the MAC client to the broadcast address group by default */
2339fc4e975dSVenugopal Iyer 	if (mip->mi_type->mt_brdcst_addr != NULL) {
2340fc4e975dSVenugopal Iyer 		err = mac_bcast_add(mcip, mip->mi_type->mt_brdcst_addr, vid,
2341fc4e975dSVenugopal Iyer 		    MAC_ADDRTYPE_BROADCAST);
2342fc4e975dSVenugopal Iyer 		if (err != 0)
2343fc4e975dSVenugopal Iyer 			goto bail;
2344fc4e975dSVenugopal Iyer 		bcast_added = B_TRUE;
2345fc4e975dSVenugopal Iyer 	}
2346fc4e975dSVenugopal Iyer 
2347fc4e975dSVenugopal Iyer 	/*
2348fc4e975dSVenugopal Iyer 	 * If this is the first unicast address addition for this
2349fc4e975dSVenugopal Iyer 	 * client, reuse the pre-allocated larval flow entry associated with
2350fc4e975dSVenugopal Iyer 	 * the MAC client.
2351fc4e975dSVenugopal Iyer 	 */
2352fc4e975dSVenugopal Iyer 	flent = (mcip->mci_nflents == 0) ? mcip->mci_flent : NULL;
2353fc4e975dSVenugopal Iyer 
2354fc4e975dSVenugopal Iyer 	/* We are configuring the unicast flow now */
2355fc4e975dSVenugopal Iyer 	if (!MCIP_DATAPATH_SETUP(mcip)) {
2356fc4e975dSVenugopal Iyer 
23570dc2366fSVenugopal Iyer 		if (mrp != NULL) {
2358fc4e975dSVenugopal Iyer 			MAC_CLIENT_SET_PRIORITY_RANGE(mcip,
2359fc4e975dSVenugopal Iyer 			    (mrp->mrp_mask & MRP_PRIORITY) ? mrp->mrp_priority :
2360fc4e975dSVenugopal Iyer 			    MPL_LINK_DEFAULT);
23610dc2366fSVenugopal Iyer 		}
2362fc4e975dSVenugopal Iyer 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
2363fc4e975dSVenugopal Iyer 		    isprimary, B_TRUE, &flent, mrp)) != 0)
2364fc4e975dSVenugopal Iyer 			goto bail;
2365fc4e975dSVenugopal Iyer 
2366fc4e975dSVenugopal Iyer 		mip->mi_nactiveclients++;
2367fc4e975dSVenugopal Iyer 		nactiveclients_added = B_TRUE;
2368fc4e975dSVenugopal Iyer 
2369fc4e975dSVenugopal Iyer 		/*
2370fc4e975dSVenugopal Iyer 		 * This will allocate the RX ring group if possible for the
2371fc4e975dSVenugopal Iyer 		 * flow and program the software classifier as needed.
2372fc4e975dSVenugopal Iyer 		 */
2373fc4e975dSVenugopal Iyer 		if ((err = mac_datapath_setup(mcip, flent, SRST_LINK)) != 0)
2374fc4e975dSVenugopal Iyer 			goto bail;
2375fc4e975dSVenugopal Iyer 
23760dc2366fSVenugopal Iyer 		if (no_unicast)
23770dc2366fSVenugopal Iyer 			goto done_setup;
2378fc4e975dSVenugopal Iyer 		/*
2379fc4e975dSVenugopal Iyer 		 * The unicast MAC address must have been added successfully.
2380fc4e975dSVenugopal Iyer 		 */
2381fc4e975dSVenugopal Iyer 		ASSERT(mcip->mci_unicast != NULL);
238284de666eSRyan Zezeski 
2383fc4e975dSVenugopal Iyer 		/*
2384fc4e975dSVenugopal Iyer 		 * Push down the sub-flows that were defined on this link
2385fc4e975dSVenugopal Iyer 		 * hitherto. The flows are added to the active flow table
2386fc4e975dSVenugopal Iyer 		 * and SRS, softrings etc. are created as needed.
2387fc4e975dSVenugopal Iyer 		 */
2388fc4e975dSVenugopal Iyer 		mac_link_init_flows((mac_client_handle_t)mcip);
2389fc4e975dSVenugopal Iyer 	} else {
2390fc4e975dSVenugopal Iyer 		mac_address_t *map = mcip->mci_unicast;
2391fc4e975dSVenugopal Iyer 
23920dc2366fSVenugopal Iyer 		ASSERT(!no_unicast);
2393fc4e975dSVenugopal Iyer 		/*
239484de666eSRyan Zezeski 		 * A unicast flow already exists for that MAC client
239584de666eSRyan Zezeski 		 * so this flow must be the same MAC address but with
239684de666eSRyan Zezeski 		 * a different VID. It has been checked by
239784de666eSRyan Zezeski 		 * mac_addr_in_use().
2398fc4e975dSVenugopal Iyer 		 *
239984de666eSRyan Zezeski 		 * We will use the SRS etc. from the initial
240084de666eSRyan Zezeski 		 * mci_flent. We don't need to create a kstat for
240184de666eSRyan Zezeski 		 * this, as except for the fdesc, everything will be
240284de666eSRyan Zezeski 		 * used from the first flent.
240384de666eSRyan Zezeski 		 *
240484de666eSRyan Zezeski 		 * The only time we should see multiple flents on the
240584de666eSRyan Zezeski 		 * same MAC client is on the sun4v vsw. If we removed
240684de666eSRyan Zezeski 		 * that code we should be able to remove the entire
240784de666eSRyan Zezeski 		 * notion of multiple flents on a MAC client (this
240884de666eSRyan Zezeski 		 * doesn't affect sub/user flows because they have
240984de666eSRyan Zezeski 		 * their own list unrelated to mci_flent_list).
2410fc4e975dSVenugopal Iyer 		 */
2411fc4e975dSVenugopal Iyer 		if (bcmp(mac_addr, map->ma_addr, map->ma_len) != 0) {
2412fc4e975dSVenugopal Iyer 			err = EINVAL;
2413fc4e975dSVenugopal Iyer 			goto bail;
2414fc4e975dSVenugopal Iyer 		}
2415fc4e975dSVenugopal Iyer 
2416fc4e975dSVenugopal Iyer 		if ((err = mac_unicast_flow_create(mcip, mac_addr, vid,
2417fc4e975dSVenugopal Iyer 		    isprimary, B_FALSE, &flent, NULL)) != 0) {
2418fc4e975dSVenugopal Iyer 			goto bail;
2419fc4e975dSVenugopal Iyer 		}
2420fc4e975dSVenugopal Iyer 		if ((err = mac_flow_add(mip->mi_flow_tab, flent)) != 0) {
2421fc4e975dSVenugopal Iyer 			FLOW_FINAL_REFRELE(flent);
2422fc4e975dSVenugopal Iyer 			goto bail;
2423fc4e975dSVenugopal Iyer 		}
2424fc4e975dSVenugopal Iyer 
2425fc4e975dSVenugopal Iyer 		/* update the multicast group for this vid */
2426fc4e975dSVenugopal Iyer 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
2427fc4e975dSVenugopal Iyer 		    (void *)flent, B_TRUE);
2428fc4e975dSVenugopal Iyer 
2429fc4e975dSVenugopal Iyer 	}
2430fc4e975dSVenugopal Iyer 
2431fc4e975dSVenugopal Iyer 	/* populate the shared MAC address */
2432fc4e975dSVenugopal Iyer 	muip->mui_map = mcip->mci_unicast;
2433fc4e975dSVenugopal Iyer 
2434fc4e975dSVenugopal Iyer 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
2435fc4e975dSVenugopal Iyer 	muip->mui_next = mcip->mci_unicast_list;
2436fc4e975dSVenugopal Iyer 	mcip->mci_unicast_list = muip;
2437fc4e975dSVenugopal Iyer 	rw_exit(&mcip->mci_rw_lock);
2438fc4e975dSVenugopal Iyer 
24390dc2366fSVenugopal Iyer done_setup:
2440fc4e975dSVenugopal Iyer 	/*
2441fc4e975dSVenugopal Iyer 	 * First add the flent to the flow list of this mcip. Then set
2442fc4e975dSVenugopal Iyer 	 * the mip's mi_single_active_client if needed. The Rx path assumes
2443fc4e975dSVenugopal Iyer 	 * that mip->mi_single_active_client will always have an associated
2444fc4e975dSVenugopal Iyer 	 * flent.
2445fc4e975dSVenugopal Iyer 	 */
2446fc4e975dSVenugopal Iyer 	mac_client_add_to_flow_list(mcip, flent);
2447fc4e975dSVenugopal Iyer 	if (nactiveclients_added)
2448fc4e975dSVenugopal Iyer 		mac_update_single_active_client(mip);
2449fc4e975dSVenugopal Iyer 	/*
2450fc4e975dSVenugopal Iyer 	 * Trigger a renegotiation of the capabilities when the number of
2451fc4e975dSVenugopal Iyer 	 * active clients changes from 1 to 2, since some of the capabilities
2452fc4e975dSVenugopal Iyer 	 * might have to be disabled. Also send a MAC_NOTE_LINK notification
2453fc4e975dSVenugopal Iyer 	 * to all the MAC clients whenever physical link is DOWN.
2454fc4e975dSVenugopal Iyer 	 */
2455fc4e975dSVenugopal Iyer 	if (mip->mi_nactiveclients == 2) {
2456fc4e975dSVenugopal Iyer 		mac_capab_update((mac_handle_t)mip);
2457fc4e975dSVenugopal Iyer 		mac_virtual_link_update(mip);
2458fc4e975dSVenugopal Iyer 	}
2459fc4e975dSVenugopal Iyer 	/*
2460fc4e975dSVenugopal Iyer 	 * Now that the setup is complete, clear the INCIPIENT flag.
2461fc4e975dSVenugopal Iyer 	 * The flag was set to avoid incoming packets seeing inconsistent
2462fc4e975dSVenugopal Iyer 	 * structures while the setup was in progress. Clear the mci_tx_flag
2463fc4e975dSVenugopal Iyer 	 * by calling mac_tx_client_block. It is possible that
2464fc4e975dSVenugopal Iyer 	 * mac_unicast_remove was called prior to this mac_unicast_add which
2465fc4e975dSVenugopal Iyer 	 * could have set the MCI_TX_QUIESCE flag.
2466fc4e975dSVenugopal Iyer 	 */
2467fc4e975dSVenugopal Iyer 	if (flent->fe_rx_ring_group != NULL)
2468fc4e975dSVenugopal Iyer 		mac_rx_group_unmark(flent->fe_rx_ring_group, MR_INCIPIENT);
2469fc4e975dSVenugopal Iyer 	FLOW_UNMARK(flent, FE_INCIPIENT);
247045948e49SRyan Zezeski 
247145948e49SRyan Zezeski 	/*
247245948e49SRyan Zezeski 	 * If this is an aggr port client, don't enable the flow's
247345948e49SRyan Zezeski 	 * datapath at this stage. Otherwise, bcast traffic could
247445948e49SRyan Zezeski 	 * arrive while the aggr port is in the process of
247545948e49SRyan Zezeski 	 * initializing. Instead, the flow's datapath is started later
247645948e49SRyan Zezeski 	 * when mac_client_set_flow_cb() is called.
247745948e49SRyan Zezeski 	 */
247845948e49SRyan Zezeski 	if ((mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0)
2479fc4e975dSVenugopal Iyer 		FLOW_UNMARK(flent, FE_MC_NO_DATAPATH);
248045948e49SRyan Zezeski 
2481fc4e975dSVenugopal Iyer 	mac_tx_client_unblock(mcip);
2482fc4e975dSVenugopal Iyer 	return (0);
2483fc4e975dSVenugopal Iyer bail:
2484fc4e975dSVenugopal Iyer 	if (bcast_added)
2485fc4e975dSVenugopal Iyer 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr, vid);
2486fc4e975dSVenugopal Iyer 
2487fc4e975dSVenugopal Iyer 	if (nactiveclients_added)
2488fc4e975dSVenugopal Iyer 		mip->mi_nactiveclients--;
2489fc4e975dSVenugopal Iyer 
24904559da71SNitin Hande 	if (mac_started)
24914559da71SNitin Hande 		mac_stop((mac_handle_t)mip);
24924559da71SNitin Hande 
2493fc4e975dSVenugopal Iyer 	return (err);
2494fc4e975dSVenugopal Iyer }
2495fc4e975dSVenugopal Iyer 
2496fc4e975dSVenugopal Iyer /*
2497fc4e975dSVenugopal Iyer  * Return the passive primary MAC client, if present. The passive client is
2498fc4e975dSVenugopal Iyer  * a stand-by client that has the same unicast address as another that is
2499fc4e975dSVenugopal Iyer  * currenly active. Once the active client goes away, the passive client
2500fc4e975dSVenugopal Iyer  * becomes active.
2501fc4e975dSVenugopal Iyer  */
2502fc4e975dSVenugopal Iyer static mac_client_impl_t *
mac_get_passive_primary_client(mac_impl_t * mip)2503fc4e975dSVenugopal Iyer mac_get_passive_primary_client(mac_impl_t *mip)
2504fc4e975dSVenugopal Iyer {
2505fc4e975dSVenugopal Iyer 	mac_client_impl_t	*mcip;
2506fc4e975dSVenugopal Iyer 
2507fc4e975dSVenugopal Iyer 	for (mcip = mip->mi_clients_list; mcip != NULL;
2508fc4e975dSVenugopal Iyer 	    mcip = mcip->mci_client_next) {
2509fc4e975dSVenugopal Iyer 		if (mac_is_primary_client(mcip) &&
2510fc4e975dSVenugopal Iyer 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
2511fc4e975dSVenugopal Iyer 			return (mcip);
2512fc4e975dSVenugopal Iyer 		}
2513fc4e975dSVenugopal Iyer 	}
2514fc4e975dSVenugopal Iyer 	return (NULL);
2515fc4e975dSVenugopal Iyer }
2516fc4e975dSVenugopal Iyer 
2517fc4e975dSVenugopal Iyer /*
2518da14cebeSEric Cheng  * Add a new unicast address to the MAC client.
2519da14cebeSEric Cheng  *
2520da14cebeSEric Cheng  * The MAC address can be specified either by value, or the MAC client
2521da14cebeSEric Cheng  * can specify that it wants to use the primary MAC address of the
2522da14cebeSEric Cheng  * underlying MAC. See the introductory comments at the beginning
2523da14cebeSEric Cheng  * of this file for more more information on primary MAC addresses.
2524da14cebeSEric Cheng  *
2525da14cebeSEric Cheng  * Note also the tuple (MAC address, VID) must be unique
2526da14cebeSEric Cheng  * for the MAC clients defined on top of the same underlying MAC
2527da14cebeSEric Cheng  * instance, unless the MAC_UNICAST_NODUPCHECK is specified.
25284eaa4710SRishi Srivatsavai  *
25294eaa4710SRishi Srivatsavai  * In no case can a client use the PVID for the MAC, if the MAC has one set.
2530da14cebeSEric Cheng  */
2531da14cebeSEric Cheng int
i_mac_unicast_add(mac_client_handle_t mch,uint8_t * mac_addr,uint16_t flags,mac_unicast_handle_t * mah,uint16_t vid,mac_diag_t * diag)2532da14cebeSEric Cheng i_mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
2533da14cebeSEric Cheng     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
2534da14cebeSEric Cheng {
2535da14cebeSEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2536da14cebeSEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
2537da14cebeSEric Cheng 	int			err;
2538da14cebeSEric Cheng 	uint_t			mac_len = mip->mi_type->mt_addr_length;
2539da14cebeSEric Cheng 	boolean_t		check_dups = !(flags & MAC_UNICAST_NODUPCHECK);
25405d460eafSCathy Zhou 	boolean_t		fastpath_disabled = B_FALSE;
2541fc4e975dSVenugopal Iyer 	boolean_t		is_primary = (flags & MAC_UNICAST_PRIMARY);
2542fc4e975dSVenugopal Iyer 	boolean_t		is_unicast_hw = (flags & MAC_UNICAST_HW);
25430dc2366fSVenugopal Iyer 	mac_resource_props_t	*mrp;
2544fc4e975dSVenugopal Iyer 	boolean_t		passive_client = B_FALSE;
2545fc4e975dSVenugopal Iyer 	mac_unicast_impl_t	*muip;
2546fc4e975dSVenugopal Iyer 	boolean_t		is_vnic_primary =
2547fc4e975dSVenugopal Iyer 	    (flags & MAC_UNICAST_VNIC_PRIMARY);
2548da14cebeSEric Cheng 
254984de666eSRyan Zezeski 	/*
255084de666eSRyan Zezeski 	 * When the VID is non-zero the underlying MAC cannot be a
255184de666eSRyan Zezeski 	 * VNIC. I.e., dladm create-vlan cannot take a VNIC as
255284de666eSRyan Zezeski 	 * argument, only the primary MAC client.
255384de666eSRyan Zezeski 	 */
255484de666eSRyan Zezeski 	ASSERT(!((mip->mi_state_flags & MIS_IS_VNIC) && (vid != VLAN_ID_NONE)));
2555da14cebeSEric Cheng 
2556ea720162SAndy Fiddaman 	*diag = MAC_DIAG_NONE;
2557ea720162SAndy Fiddaman 
2558da14cebeSEric Cheng 	/*
25590dc2366fSVenugopal Iyer 	 * Can't unicast add if the client asked only for minimal datapath
25600dc2366fSVenugopal Iyer 	 * setup.
25610dc2366fSVenugopal Iyer 	 */
25620dc2366fSVenugopal Iyer 	if (mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR)
25630dc2366fSVenugopal Iyer 		return (ENOTSUP);
25640dc2366fSVenugopal Iyer 
25650dc2366fSVenugopal Iyer 	/*
25664eaa4710SRishi Srivatsavai 	 * Check for an attempted use of the current Port VLAN ID, if enabled.
25674eaa4710SRishi Srivatsavai 	 * No client may use it.
25684eaa4710SRishi Srivatsavai 	 */
256984de666eSRyan Zezeski 	if (mip->mi_pvid != VLAN_ID_NONE && vid == mip->mi_pvid)
25704eaa4710SRishi Srivatsavai 		return (EBUSY);
25714eaa4710SRishi Srivatsavai 
25724eaa4710SRishi Srivatsavai 	/*
2573da14cebeSEric Cheng 	 * Check whether it's the primary client and flag it.
2574da14cebeSEric Cheng 	 */
257584de666eSRyan Zezeski 	if (!(mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
257684de666eSRyan Zezeski 	    vid == VLAN_ID_NONE)
2577da14cebeSEric Cheng 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PRIMARY;
2578da14cebeSEric Cheng 
2579da14cebeSEric Cheng 	/*
2580da14cebeSEric Cheng 	 * is_vnic_primary is true when we come here as a VLAN VNIC
258184de666eSRyan Zezeski 	 * which uses the primary MAC client's address but with a non-zero
2582da14cebeSEric Cheng 	 * VID. In this case the MAC address is not specified by an upper
2583da14cebeSEric Cheng 	 * MAC client.
2584da14cebeSEric Cheng 	 */
2585da14cebeSEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) && is_primary &&
2586da14cebeSEric Cheng 	    !is_vnic_primary) {
2587da14cebeSEric Cheng 		/*
2588da14cebeSEric Cheng 		 * The address is being set by the upper MAC client
2589da14cebeSEric Cheng 		 * of a VNIC. The MAC address was already set by the
2590da14cebeSEric Cheng 		 * VNIC driver during VNIC creation.
2591da14cebeSEric Cheng 		 *
2592da14cebeSEric Cheng 		 * Note: a VNIC has only one MAC address. We return
2593da14cebeSEric Cheng 		 * the MAC unicast address handle of the lower MAC client
2594da14cebeSEric Cheng 		 * corresponding to the VNIC. We allocate a new entry
2595da14cebeSEric Cheng 		 * which is flagged appropriately, so that mac_unicast_remove()
2596da14cebeSEric Cheng 		 * doesn't attempt to free the original entry that
2597da14cebeSEric Cheng 		 * was allocated by the VNIC driver.
2598da14cebeSEric Cheng 		 */
2599da14cebeSEric Cheng 		ASSERT(mcip->mci_unicast != NULL);
2600da14cebeSEric Cheng 
26014c91d6c6SVenugopal Iyer 		/* Check for VLAN flags, if present */
26024c91d6c6SVenugopal Iyer 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
26034c91d6c6SVenugopal Iyer 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
26044c91d6c6SVenugopal Iyer 
26054c91d6c6SVenugopal Iyer 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
26064c91d6c6SVenugopal Iyer 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
26074c91d6c6SVenugopal Iyer 
26084c91d6c6SVenugopal Iyer 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
26094c91d6c6SVenugopal Iyer 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
26104c91d6c6SVenugopal Iyer 
2611da14cebeSEric Cheng 		/*
2612da14cebeSEric Cheng 		 * Ensure that the primary unicast address of the VNIC
2613fc4e975dSVenugopal Iyer 		 * is added only once unless we have the
2614fc4e975dSVenugopal Iyer 		 * MAC_CLIENT_FLAGS_MULTI_PRIMARY set (and this is not
2615fc4e975dSVenugopal Iyer 		 * a passive MAC client).
2616da14cebeSEric Cheng 		 */
2617fc4e975dSVenugopal Iyer 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) != 0) {
2618fc4e975dSVenugopal Iyer 			if ((mcip->mci_flags &
2619fc4e975dSVenugopal Iyer 			    MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
2620fc4e975dSVenugopal Iyer 			    (mcip->mci_flags &
2621fc4e975dSVenugopal Iyer 			    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
2622da14cebeSEric Cheng 				return (EBUSY);
2623fc4e975dSVenugopal Iyer 			}
2624fc4e975dSVenugopal Iyer 			mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
2625fc4e975dSVenugopal Iyer 			passive_client = B_TRUE;
2626fc4e975dSVenugopal Iyer 		}
2627da14cebeSEric Cheng 
2628da14cebeSEric Cheng 		mcip->mci_flags |= MAC_CLIENT_FLAGS_VNIC_PRIMARY;
2629da14cebeSEric Cheng 
2630da14cebeSEric Cheng 		/*
2631da14cebeSEric Cheng 		 * Create a handle for vid 0.
2632da14cebeSEric Cheng 		 */
263384de666eSRyan Zezeski 		ASSERT(vid == VLAN_ID_NONE);
2634da14cebeSEric Cheng 		muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
2635da14cebeSEric Cheng 		muip->mui_vid = vid;
2636da14cebeSEric Cheng 		*mah = (mac_unicast_handle_t)muip;
2637fc4e975dSVenugopal Iyer 		/*
2638fc4e975dSVenugopal Iyer 		 * This will be used by the caller to defer setting the
2639fc4e975dSVenugopal Iyer 		 * rx functions.
2640fc4e975dSVenugopal Iyer 		 */
2641fc4e975dSVenugopal Iyer 		if (passive_client)
2642fc4e975dSVenugopal Iyer 			return (EAGAIN);
2643da14cebeSEric Cheng 		return (0);
2644da14cebeSEric Cheng 	}
2645da14cebeSEric Cheng 
2646da14cebeSEric Cheng 	/* primary MAC clients cannot be opened on top of anchor VNICs */
2647da14cebeSEric Cheng 	if ((is_vnic_primary || is_primary) &&
2648da14cebeSEric Cheng 	    i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_ANCHOR_VNIC, NULL)) {
2649da14cebeSEric Cheng 		return (ENXIO);
2650da14cebeSEric Cheng 	}
2651da14cebeSEric Cheng 
2652da14cebeSEric Cheng 	/*
265384de666eSRyan Zezeski 	 * If this is a VNIC/VLAN, disable softmac fast-path. This is
265484de666eSRyan Zezeski 	 * only relevant to legacy devices which use softmac to
265584de666eSRyan Zezeski 	 * interface with GLDv3.
2656da14cebeSEric Cheng 	 */
26575d460eafSCathy Zhou 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
26585d460eafSCathy Zhou 		err = mac_fastpath_disable((mac_handle_t)mip);
26595d460eafSCathy Zhou 		if (err != 0)
26605d460eafSCathy Zhou 			return (err);
26615d460eafSCathy Zhou 		fastpath_disabled = B_TRUE;
26625d460eafSCathy Zhou 	}
26635d460eafSCathy Zhou 
26645d460eafSCathy Zhou 	/*
26655d460eafSCathy Zhou 	 * Return EBUSY if:
26665d460eafSCathy Zhou 	 *  - there is an exclusively active mac client exists.
26675d460eafSCathy Zhou 	 *  - this is an exclusive active mac client but
26685d460eafSCathy Zhou 	 *	a. there is already active mac clients exist, or
26695d460eafSCathy Zhou 	 *	b. fastpath streams are already plumbed on this legacy device
26704eaa4710SRishi Srivatsavai 	 *  - the mac creator has disallowed active mac clients.
26715d460eafSCathy Zhou 	 */
26724eaa4710SRishi Srivatsavai 	if (mip->mi_state_flags & (MIS_EXCLUSIVE|MIS_NO_ACTIVE)) {
26735d460eafSCathy Zhou 		if (fastpath_disabled)
26745d460eafSCathy Zhou 			mac_fastpath_enable((mac_handle_t)mip);
2675da14cebeSEric Cheng 		return (EBUSY);
2676da14cebeSEric Cheng 	}
2677da14cebeSEric Cheng 
26785d460eafSCathy Zhou 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
26795d460eafSCathy Zhou 		ASSERT(!fastpath_disabled);
26805d460eafSCathy Zhou 		if (mip->mi_nactiveclients != 0)
26815d460eafSCathy Zhou 			return (EBUSY);
26825d460eafSCathy Zhou 
26835d460eafSCathy Zhou 		if ((mip->mi_state_flags & MIS_LEGACY) &&
26845d460eafSCathy Zhou 		    !(mip->mi_capab_legacy.ml_active_set(mip->mi_driver))) {
26855d460eafSCathy Zhou 			return (EBUSY);
26865d460eafSCathy Zhou 		}
2687da14cebeSEric Cheng 		mip->mi_state_flags |= MIS_EXCLUSIVE;
26885d460eafSCathy Zhou 	}
2689da14cebeSEric Cheng 
26900dc2366fSVenugopal Iyer 	mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
2691ae6aa22aSVenugopal Iyer 	if (is_primary && !(mcip->mci_state_flags & (MCIS_IS_VNIC |
2692ae6aa22aSVenugopal Iyer 	    MCIS_IS_AGGR_PORT))) {
2693da14cebeSEric Cheng 		/*
2694da14cebeSEric Cheng 		 * Apply the property cached in the mac_impl_t to the primary
2695ae6aa22aSVenugopal Iyer 		 * mac client. If the mac client is a VNIC or an aggregation
2696ae6aa22aSVenugopal Iyer 		 * port, its property should be set in the mcip when the
2697ae6aa22aSVenugopal Iyer 		 * VNIC/aggr was created.
2698da14cebeSEric Cheng 		 */
26990dc2366fSVenugopal Iyer 		mac_get_resources((mac_handle_t)mip, mrp);
27000dc2366fSVenugopal Iyer 		(void) mac_client_set_resources(mch, mrp);
2701da14cebeSEric Cheng 	} else if (mcip->mci_state_flags & MCIS_IS_VNIC) {
27020dc2366fSVenugopal Iyer 		/*
270384de666eSRyan Zezeski 		 * This is a VLAN client sharing the address of the
270484de666eSRyan Zezeski 		 * primary MAC client; i.e., one created via dladm
270584de666eSRyan Zezeski 		 * create-vlan. We don't support specifying ring
270684de666eSRyan Zezeski 		 * properties for this type of client as it inherits
270784de666eSRyan Zezeski 		 * these from the primary MAC client.
27080dc2366fSVenugopal Iyer 		 */
27090dc2366fSVenugopal Iyer 		if (is_vnic_primary) {
27100dc2366fSVenugopal Iyer 			mac_resource_props_t	*vmrp;
27110dc2366fSVenugopal Iyer 
27120dc2366fSVenugopal Iyer 			vmrp = MCIP_RESOURCE_PROPS(mcip);
27130dc2366fSVenugopal Iyer 			if (vmrp->mrp_mask & MRP_RX_RINGS ||
27140dc2366fSVenugopal Iyer 			    vmrp->mrp_mask & MRP_TX_RINGS) {
27150dc2366fSVenugopal Iyer 				if (fastpath_disabled)
27160dc2366fSVenugopal Iyer 					mac_fastpath_enable((mac_handle_t)mip);
27170dc2366fSVenugopal Iyer 				kmem_free(mrp, sizeof (*mrp));
27180dc2366fSVenugopal Iyer 				return (ENOTSUP);
27190dc2366fSVenugopal Iyer 			}
27200dc2366fSVenugopal Iyer 			/*
27210dc2366fSVenugopal Iyer 			 * Additionally we also need to inherit any
27220dc2366fSVenugopal Iyer 			 * rings property from the MAC.
27230dc2366fSVenugopal Iyer 			 */
27240dc2366fSVenugopal Iyer 			mac_get_resources((mac_handle_t)mip, mrp);
27250dc2366fSVenugopal Iyer 			if (mrp->mrp_mask & MRP_RX_RINGS) {
27260dc2366fSVenugopal Iyer 				vmrp->mrp_mask |= MRP_RX_RINGS;
27270dc2366fSVenugopal Iyer 				vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
27280dc2366fSVenugopal Iyer 			}
27290dc2366fSVenugopal Iyer 			if (mrp->mrp_mask & MRP_TX_RINGS) {
27300dc2366fSVenugopal Iyer 				vmrp->mrp_mask |= MRP_TX_RINGS;
27310dc2366fSVenugopal Iyer 				vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
27320dc2366fSVenugopal Iyer 			}
27330dc2366fSVenugopal Iyer 		}
27340dc2366fSVenugopal Iyer 		bcopy(MCIP_RESOURCE_PROPS(mcip), mrp, sizeof (*mrp));
2735da14cebeSEric Cheng 	}
2736da14cebeSEric Cheng 
2737da14cebeSEric Cheng 	muip = kmem_zalloc(sizeof (mac_unicast_impl_t), KM_SLEEP);
2738da14cebeSEric Cheng 	muip->mui_vid = vid;
2739da14cebeSEric Cheng 
2740da14cebeSEric Cheng 	if (is_primary || is_vnic_primary) {
2741da14cebeSEric Cheng 		mac_addr = mip->mi_addr;
2742da14cebeSEric Cheng 	} else {
2743da14cebeSEric Cheng 
2744da14cebeSEric Cheng 		/*
2745da14cebeSEric Cheng 		 * Verify the validity of the specified MAC addresses value.
2746da14cebeSEric Cheng 		 */
2747da14cebeSEric Cheng 		if (!mac_unicst_verify((mac_handle_t)mip, mac_addr, mac_len)) {
2748da14cebeSEric Cheng 			*diag = MAC_DIAG_MACADDR_INVALID;
2749da14cebeSEric Cheng 			err = EINVAL;
2750fc4e975dSVenugopal Iyer 			goto bail_out;
2751da14cebeSEric Cheng 		}
2752da14cebeSEric Cheng 
2753da14cebeSEric Cheng 		/*
2754da14cebeSEric Cheng 		 * Make sure that the specified MAC address is different
2755da14cebeSEric Cheng 		 * than the unicast MAC address of the underlying NIC.
2756da14cebeSEric Cheng 		 */
2757da14cebeSEric Cheng 		if (check_dups && bcmp(mip->mi_addr, mac_addr, mac_len) == 0) {
2758da14cebeSEric Cheng 			*diag = MAC_DIAG_MACADDR_NIC;
2759da14cebeSEric Cheng 			err = EINVAL;
2760fc4e975dSVenugopal Iyer 			goto bail_out;
2761da14cebeSEric Cheng 		}
2762da14cebeSEric Cheng 	}
2763da14cebeSEric Cheng 
2764da14cebeSEric Cheng 	/*
2765fc4e975dSVenugopal Iyer 	 * Set the flags here so that if this is a passive client, we
2766fc4e975dSVenugopal Iyer 	 * can return and set it when we call mac_client_datapath_setup
2767fc4e975dSVenugopal Iyer 	 * when this becomes the active client. If we defer to using these
2768fc4e975dSVenugopal Iyer 	 * flags to mac_client_datapath_setup, then for a passive client,
2769fc4e975dSVenugopal Iyer 	 * we'd have to store the flags somewhere (probably fe_flags)
2770fc4e975dSVenugopal Iyer 	 * and then use it.
2771da14cebeSEric Cheng 	 */
2772da14cebeSEric Cheng 	if (!MCIP_DATAPATH_SETUP(mcip)) {
277308ac1c49SNicolas Droux 		if (is_unicast_hw) {
277408ac1c49SNicolas Droux 			/*
277508ac1c49SNicolas Droux 			 * The client requires a hardware MAC address slot
277608ac1c49SNicolas Droux 			 * for that unicast address. Since we support only
277708ac1c49SNicolas Droux 			 * one unicast MAC address per client, flag the
277808ac1c49SNicolas Droux 			 * MAC client itself.
277908ac1c49SNicolas Droux 			 */
278008ac1c49SNicolas Droux 			mcip->mci_state_flags |= MCIS_UNICAST_HW;
278108ac1c49SNicolas Droux 		}
2782da14cebeSEric Cheng 
27834c91d6c6SVenugopal Iyer 		/* Check for VLAN flags, if present */
27844c91d6c6SVenugopal Iyer 		if ((flags & MAC_UNICAST_TAG_DISABLE) != 0)
27854c91d6c6SVenugopal Iyer 			mcip->mci_state_flags |= MCIS_TAG_DISABLE;
27864c91d6c6SVenugopal Iyer 
27874c91d6c6SVenugopal Iyer 		if ((flags & MAC_UNICAST_STRIP_DISABLE) != 0)
27884c91d6c6SVenugopal Iyer 			mcip->mci_state_flags |= MCIS_STRIP_DISABLE;
27894c91d6c6SVenugopal Iyer 
27904c91d6c6SVenugopal Iyer 		if ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0)
27914c91d6c6SVenugopal Iyer 			mcip->mci_state_flags |= MCIS_DISABLE_TX_VID_CHECK;
2792da14cebeSEric Cheng 	} else {
27934c91d6c6SVenugopal Iyer 		/*
27944c91d6c6SVenugopal Iyer 		 * Assert that the specified flags are consistent with the
27954c91d6c6SVenugopal Iyer 		 * flags specified by previous calls to mac_unicast_add().
27964c91d6c6SVenugopal Iyer 		 */
27974c91d6c6SVenugopal Iyer 		ASSERT(((flags & MAC_UNICAST_TAG_DISABLE) != 0 &&
27984c91d6c6SVenugopal Iyer 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) != 0) ||
27994c91d6c6SVenugopal Iyer 		    ((flags & MAC_UNICAST_TAG_DISABLE) == 0 &&
28004c91d6c6SVenugopal Iyer 		    (mcip->mci_state_flags & MCIS_TAG_DISABLE) == 0));
28014c91d6c6SVenugopal Iyer 
28024c91d6c6SVenugopal Iyer 		ASSERT(((flags & MAC_UNICAST_STRIP_DISABLE) != 0 &&
28034c91d6c6SVenugopal Iyer 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) != 0) ||
28044c91d6c6SVenugopal Iyer 		    ((flags & MAC_UNICAST_STRIP_DISABLE) == 0 &&
28054c91d6c6SVenugopal Iyer 		    (mcip->mci_state_flags & MCIS_STRIP_DISABLE) == 0));
28064c91d6c6SVenugopal Iyer 
28074c91d6c6SVenugopal Iyer 		ASSERT(((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) != 0 &&
28084c91d6c6SVenugopal Iyer 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) != 0) ||
28094c91d6c6SVenugopal Iyer 		    ((flags & MAC_UNICAST_DISABLE_TX_VID_CHECK) == 0 &&
28104c91d6c6SVenugopal Iyer 		    (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK) == 0));
28114c91d6c6SVenugopal Iyer 
281208ac1c49SNicolas Droux 		/*
281308ac1c49SNicolas Droux 		 * Make sure the client is consistent about its requests
281408ac1c49SNicolas Droux 		 * for MAC addresses. I.e. all requests from the clients
281508ac1c49SNicolas Droux 		 * must have the MAC_UNICAST_HW flag set or clear.
281608ac1c49SNicolas Droux 		 */
281776fb654bSAndrew Stormont 		if (((mcip->mci_state_flags & MCIS_UNICAST_HW) != 0 &&
281876fb654bSAndrew Stormont 		    !is_unicast_hw) ||
281976fb654bSAndrew Stormont 		    ((mcip->mci_state_flags & MCIS_UNICAST_HW) == 0 &&
282076fb654bSAndrew Stormont 		    is_unicast_hw)) {
282108ac1c49SNicolas Droux 			err = EINVAL;
2822fc4e975dSVenugopal Iyer 			goto bail_out;
282308ac1c49SNicolas Droux 		}
2824da14cebeSEric Cheng 	}
2825fc4e975dSVenugopal Iyer 	/*
2826fc4e975dSVenugopal Iyer 	 * Make sure the MAC address is not already used by
2827fc4e975dSVenugopal Iyer 	 * another MAC client defined on top of the same
2828fc4e975dSVenugopal Iyer 	 * underlying NIC. Unless we have MAC_CLIENT_FLAGS_MULTI_PRIMARY
2829fc4e975dSVenugopal Iyer 	 * set when we allow a passive client to be present which will
2830fc4e975dSVenugopal Iyer 	 * be activated when the currently active client goes away - this
2831fc4e975dSVenugopal Iyer 	 * works only with primary addresses.
2832fc4e975dSVenugopal Iyer 	 */
2833fc4e975dSVenugopal Iyer 	if ((check_dups || is_primary || is_vnic_primary) &&
2834fc4e975dSVenugopal Iyer 	    mac_addr_in_use(mip, mac_addr, vid)) {
2835fc4e975dSVenugopal Iyer 		/*
2836fc4e975dSVenugopal Iyer 		 * Must have set the multiple primary address flag when
2837fc4e975dSVenugopal Iyer 		 * we did a mac_client_open AND this should be a primary
2838fc4e975dSVenugopal Iyer 		 * MAC client AND there should not already be a passive
2839fc4e975dSVenugopal Iyer 		 * primary. If all is true then we let this succeed
2840fc4e975dSVenugopal Iyer 		 * even if the address is a dup.
2841fc4e975dSVenugopal Iyer 		 */
2842fc4e975dSVenugopal Iyer 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_MULTI_PRIMARY) == 0 ||
2843fc4e975dSVenugopal Iyer 		    (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) == 0 ||
2844fc4e975dSVenugopal Iyer 		    mac_get_passive_primary_client(mip) != NULL) {
2845fc4e975dSVenugopal Iyer 			*diag = MAC_DIAG_MACADDR_INUSE;
2846fc4e975dSVenugopal Iyer 			err = EEXIST;
2847fc4e975dSVenugopal Iyer 			goto bail_out;
2848da14cebeSEric Cheng 		}
2849fc4e975dSVenugopal Iyer 		ASSERT((mcip->mci_flags &
2850fc4e975dSVenugopal Iyer 		    MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) == 0);
2851fc4e975dSVenugopal Iyer 		mcip->mci_flags |= MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
28520dc2366fSVenugopal Iyer 		kmem_free(mrp, sizeof (*mrp));
2853da14cebeSEric Cheng 
2854fc4e975dSVenugopal Iyer 		/*
2855fc4e975dSVenugopal Iyer 		 * Stash the unicast address handle, we will use it when
2856fc4e975dSVenugopal Iyer 		 * we set up the passive client.
2857fc4e975dSVenugopal Iyer 		 */
2858fc4e975dSVenugopal Iyer 		mcip->mci_p_unicast_list = muip;
2859da14cebeSEric Cheng 		*mah = (mac_unicast_handle_t)muip;
2860da14cebeSEric Cheng 		return (0);
28615d460eafSCathy Zhou 	}
28625d460eafSCathy Zhou 
28630dc2366fSVenugopal Iyer 	err = mac_client_datapath_setup(mcip, vid, mac_addr, mrp,
2864fc4e975dSVenugopal Iyer 	    is_primary || is_vnic_primary, muip);
2865fc4e975dSVenugopal Iyer 	if (err != 0)
2866fc4e975dSVenugopal Iyer 		goto bail_out;
28670dc2366fSVenugopal Iyer 
28680dc2366fSVenugopal Iyer 	kmem_free(mrp, sizeof (*mrp));
2869fc4e975dSVenugopal Iyer 	*mah = (mac_unicast_handle_t)muip;
2870fc4e975dSVenugopal Iyer 	return (0);
2871fc4e975dSVenugopal Iyer 
2872fc4e975dSVenugopal Iyer bail_out:
28735d460eafSCathy Zhou 	if (fastpath_disabled)
28745d460eafSCathy Zhou 		mac_fastpath_enable((mac_handle_t)mip);
2875fc4e975dSVenugopal Iyer 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
2876fc4e975dSVenugopal Iyer 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
2877fc4e975dSVenugopal Iyer 		if (mip->mi_state_flags & MIS_LEGACY) {
2878fc4e975dSVenugopal Iyer 			mip->mi_capab_legacy.ml_active_clear(
2879fc4e975dSVenugopal Iyer 			    mip->mi_driver);
2880fc4e975dSVenugopal Iyer 		}
2881fc4e975dSVenugopal Iyer 	}
28820dc2366fSVenugopal Iyer 	kmem_free(mrp, sizeof (*mrp));
2883da14cebeSEric Cheng 	kmem_free(muip, sizeof (mac_unicast_impl_t));
2884da14cebeSEric Cheng 	return (err);
2885da14cebeSEric Cheng }
2886da14cebeSEric Cheng 
2887fc4e975dSVenugopal Iyer /*
2888fc4e975dSVenugopal Iyer  * Wrapper function to mac_unicast_add when we want to have the same mac
2889fc4e975dSVenugopal Iyer  * client open for two instances, one that is currently active and another
2890fc4e975dSVenugopal Iyer  * that will become active when the current one is removed. In this case
2891fc4e975dSVenugopal Iyer  * mac_unicast_add will return EGAIN and we will save the rx function and
2892fc4e975dSVenugopal Iyer  * arg which will be used when we activate the passive client in
2893fc4e975dSVenugopal Iyer  * mac_unicast_remove.
2894fc4e975dSVenugopal Iyer  */
2895fc4e975dSVenugopal Iyer int
mac_unicast_add_set_rx(mac_client_handle_t mch,uint8_t * mac_addr,uint16_t flags,mac_unicast_handle_t * mah,uint16_t vid,mac_diag_t * diag,mac_rx_t rx_fn,void * arg)2896fc4e975dSVenugopal Iyer mac_unicast_add_set_rx(mac_client_handle_t mch, uint8_t *mac_addr,
2897fc4e975dSVenugopal Iyer     uint16_t flags, mac_unicast_handle_t *mah,  uint16_t vid, mac_diag_t *diag,
2898fc4e975dSVenugopal Iyer     mac_rx_t rx_fn, void *arg)
2899fc4e975dSVenugopal Iyer {
2900fc4e975dSVenugopal Iyer 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2901fc4e975dSVenugopal Iyer 	uint_t			err;
2902fc4e975dSVenugopal Iyer 
2903fc4e975dSVenugopal Iyer 	err = mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
2904fc4e975dSVenugopal Iyer 	if (err != 0 && err != EAGAIN)
2905fc4e975dSVenugopal Iyer 		return (err);
2906fc4e975dSVenugopal Iyer 	if (err == EAGAIN) {
2907fc4e975dSVenugopal Iyer 		if (rx_fn != NULL) {
2908fc4e975dSVenugopal Iyer 			mcip->mci_rx_p_fn = rx_fn;
2909fc4e975dSVenugopal Iyer 			mcip->mci_rx_p_arg = arg;
2910fc4e975dSVenugopal Iyer 		}
2911fc4e975dSVenugopal Iyer 		return (0);
2912fc4e975dSVenugopal Iyer 	}
2913fc4e975dSVenugopal Iyer 	if (rx_fn != NULL)
2914fc4e975dSVenugopal Iyer 		mac_rx_set(mch, rx_fn, arg);
2915fc4e975dSVenugopal Iyer 	return (err);
2916fc4e975dSVenugopal Iyer }
2917fc4e975dSVenugopal Iyer 
2918da14cebeSEric Cheng int
mac_unicast_add(mac_client_handle_t mch,uint8_t * mac_addr,uint16_t flags,mac_unicast_handle_t * mah,uint16_t vid,mac_diag_t * diag)2919da14cebeSEric Cheng mac_unicast_add(mac_client_handle_t mch, uint8_t *mac_addr, uint16_t flags,
2920da14cebeSEric Cheng     mac_unicast_handle_t *mah, uint16_t vid, mac_diag_t *diag)
2921da14cebeSEric Cheng {
2922da14cebeSEric Cheng 	mac_impl_t *mip = ((mac_client_impl_t *)mch)->mci_mip;
2923da14cebeSEric Cheng 	uint_t err;
2924da14cebeSEric Cheng 
2925da14cebeSEric Cheng 	i_mac_perim_enter(mip);
2926da14cebeSEric Cheng 	err = i_mac_unicast_add(mch, mac_addr, flags, mah, vid, diag);
2927da14cebeSEric Cheng 	i_mac_perim_exit(mip);
2928da14cebeSEric Cheng 
2929da14cebeSEric Cheng 	return (err);
2930da14cebeSEric Cheng }
2931da14cebeSEric Cheng 
29320dc2366fSVenugopal Iyer static void
mac_client_datapath_teardown(mac_client_handle_t mch,mac_unicast_impl_t * muip,flow_entry_t * flent)2933fc4e975dSVenugopal Iyer mac_client_datapath_teardown(mac_client_handle_t mch, mac_unicast_impl_t *muip,
2934fc4e975dSVenugopal Iyer     flow_entry_t *flent)
2935da14cebeSEric Cheng {
2936da14cebeSEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
2937da14cebeSEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
29380dc2366fSVenugopal Iyer 	boolean_t		no_unicast;
2939da14cebeSEric Cheng 
2940ae6aa22aSVenugopal Iyer 	/*
29410dc2366fSVenugopal Iyer 	 * If we have not added a unicast address for this MAC client, just
29420dc2366fSVenugopal Iyer 	 * teardown the datapath.
29430dc2366fSVenugopal Iyer 	 */
29440dc2366fSVenugopal Iyer 	no_unicast = mcip->mci_state_flags & MCIS_NO_UNICAST_ADDR;
29450dc2366fSVenugopal Iyer 
29460dc2366fSVenugopal Iyer 	if (!no_unicast) {
29470dc2366fSVenugopal Iyer 		/*
29480dc2366fSVenugopal Iyer 		 * We would have initialized subflows etc. only if we brought
29490dc2366fSVenugopal Iyer 		 * up the primary client and set the unicast unicast address
29500dc2366fSVenugopal Iyer 		 * etc. Deactivate the flows. The flow entry will be removed
29510dc2366fSVenugopal Iyer 		 * from the active flow tables, and the associated SRS,
29520dc2366fSVenugopal Iyer 		 * softrings etc will be deleted. But the flow entry itself
29530dc2366fSVenugopal Iyer 		 * won't be destroyed, instead it will continue to be archived
29540dc2366fSVenugopal Iyer 		 * off the  the global flow hash list, for a possible future
29550dc2366fSVenugopal Iyer 		 * activation when say IP is plumbed again.
2956ae6aa22aSVenugopal Iyer 		 */
2957ae6aa22aSVenugopal Iyer 		mac_link_release_flows(mch);
29580dc2366fSVenugopal Iyer 	}
2959da14cebeSEric Cheng 	mip->mi_nactiveclients--;
2960ae6aa22aSVenugopal Iyer 	mac_update_single_active_client(mip);
2961da14cebeSEric Cheng 
2962fc4e975dSVenugopal Iyer 	/* Tear down the data path */
2963da14cebeSEric Cheng 	mac_datapath_teardown(mcip, mcip->mci_flent, SRST_LINK);
2964da14cebeSEric Cheng 
2965da14cebeSEric Cheng 	/*
2966da14cebeSEric Cheng 	 * Prevent any future access to the flow entry through the mci_flent
2967da14cebeSEric Cheng 	 * pointer by setting the mci_flent to NULL. Access to mci_flent in
2968da14cebeSEric Cheng 	 * mac_bcast_send is also under mi_rw_lock.
2969da14cebeSEric Cheng 	 */
2970da14cebeSEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
2971da14cebeSEric Cheng 	flent = mcip->mci_flent;
2972da14cebeSEric Cheng 	mac_client_remove_flow_from_list(mcip, flent);
2973da14cebeSEric Cheng 
2974da14cebeSEric Cheng 	if (mcip->mci_state_flags & MCIS_DESC_LOGGED)
2975da14cebeSEric Cheng 		mcip->mci_state_flags &= ~MCIS_DESC_LOGGED;
2976da14cebeSEric Cheng 
2977da14cebeSEric Cheng 	/*
2978da14cebeSEric Cheng 	 * This is the last unicast address being removed and there shouldn't
2979da14cebeSEric Cheng 	 * be any outbound data threads at this point coming down from mac
2980da14cebeSEric Cheng 	 * clients. We have waited for the data threads to finish before
2981da14cebeSEric Cheng 	 * starting dld_str_detach. Non-data threads must access TX SRS
2982da14cebeSEric Cheng 	 * under mi_rw_lock.
2983da14cebeSEric Cheng 	 */
2984da14cebeSEric Cheng 	rw_exit(&mip->mi_rw_lock);
2985da14cebeSEric Cheng 
2986da14cebeSEric Cheng 	/*
2987da14cebeSEric Cheng 	 * Don't use FLOW_MARK with FE_MC_NO_DATAPATH, as the flow might
2988da14cebeSEric Cheng 	 * contain other flags, such as FE_CONDEMNED, which we need to
2989da14cebeSEric Cheng 	 * cleared. We don't call mac_flow_cleanup() for this unicast
2990da14cebeSEric Cheng 	 * flow as we have a already cleaned up SRSs etc. (via the teadown
2991da14cebeSEric Cheng 	 * path). We just clear the stats and reset the initial callback
2992da14cebeSEric Cheng 	 * function, the rest will be set when we call mac_flow_create,
2993da14cebeSEric Cheng 	 * if at all.
2994da14cebeSEric Cheng 	 */
2995da14cebeSEric Cheng 	mutex_enter(&flent->fe_lock);
2996da14cebeSEric Cheng 	ASSERT(flent->fe_refcnt == 1 && flent->fe_mbg == NULL &&
2997da14cebeSEric Cheng 	    flent->fe_tx_srs == NULL && flent->fe_rx_srs_cnt == 0);
2998da14cebeSEric Cheng 	flent->fe_flags = FE_MC_NO_DATAPATH;
2999da14cebeSEric Cheng 	flow_stat_destroy(flent);
30000dc2366fSVenugopal Iyer 	mac_misc_stat_delete(flent);
3001da14cebeSEric Cheng 
3002da14cebeSEric Cheng 	/* Initialize the receiver function to a safe routine */
3003c61a1653SRyan Zezeski 	flent->fe_cb_fn = (flow_fn_t)mac_rx_def;
3004da14cebeSEric Cheng 	flent->fe_cb_arg1 = NULL;
3005da14cebeSEric Cheng 	flent->fe_cb_arg2 = NULL;
3006da14cebeSEric Cheng 
3007da14cebeSEric Cheng 	flent->fe_index = -1;
3008da14cebeSEric Cheng 	mutex_exit(&flent->fe_lock);
3009da14cebeSEric Cheng 
3010da14cebeSEric Cheng 	if (mip->mi_type->mt_brdcst_addr != NULL) {
30110dc2366fSVenugopal Iyer 		ASSERT(muip != NULL || no_unicast);
3012da14cebeSEric Cheng 		mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
30130dc2366fSVenugopal Iyer 		    muip != NULL ? muip->mui_vid : VLAN_ID_NONE);
3014da14cebeSEric Cheng 	}
3015da14cebeSEric Cheng 
3016da14cebeSEric Cheng 	if (mip->mi_nactiveclients == 1) {
3017da14cebeSEric Cheng 		mac_capab_update((mac_handle_t)mip);
3018da14cebeSEric Cheng 		mac_virtual_link_update(mip);
3019da14cebeSEric Cheng 	}
30205d460eafSCathy Zhou 
30215d460eafSCathy Zhou 	if (mcip->mci_state_flags & MCIS_EXCLUSIVE) {
3022da14cebeSEric Cheng 		mip->mi_state_flags &= ~MIS_EXCLUSIVE;
30235d460eafSCathy Zhou 
30245d460eafSCathy Zhou 		if (mip->mi_state_flags & MIS_LEGACY)
30255d460eafSCathy Zhou 			mip->mi_capab_legacy.ml_active_clear(mip->mi_driver);
30265d460eafSCathy Zhou 	}
30275d460eafSCathy Zhou 
302808ac1c49SNicolas Droux 	mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
3029da14cebeSEric Cheng 
30304c91d6c6SVenugopal Iyer 	if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
30314c91d6c6SVenugopal Iyer 		mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
30324c91d6c6SVenugopal Iyer 
30334c91d6c6SVenugopal Iyer 	if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
30344c91d6c6SVenugopal Iyer 		mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
30354c91d6c6SVenugopal Iyer 
30364c91d6c6SVenugopal Iyer 	if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
30374c91d6c6SVenugopal Iyer 		mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
30384c91d6c6SVenugopal Iyer 
30390dc2366fSVenugopal Iyer 	if (muip != NULL)
3040da14cebeSEric Cheng 		kmem_free(muip, sizeof (mac_unicast_impl_t));
30410dc2366fSVenugopal Iyer 	mac_protect_cancel_timer(mcip);
3042797f979dSCody Peter Mello 	mac_protect_flush_dynamic(mcip);
30435d460eafSCathy Zhou 
30440dc2366fSVenugopal Iyer 	bzero(&mcip->mci_misc_stat, sizeof (mcip->mci_misc_stat));
30455d460eafSCathy Zhou 	/*
30465d460eafSCathy Zhou 	 * Disable fastpath if this is a VNIC or a VLAN.
30475d460eafSCathy Zhou 	 */
30485d460eafSCathy Zhou 	if (mcip->mci_state_flags & MCIS_IS_VNIC)
30495d460eafSCathy Zhou 		mac_fastpath_enable((mac_handle_t)mip);
30505d460eafSCathy Zhou 	mac_stop((mac_handle_t)mip);
3051fc4e975dSVenugopal Iyer }
3052fc4e975dSVenugopal Iyer 
3053fc4e975dSVenugopal Iyer /*
3054fc4e975dSVenugopal Iyer  * Remove a MAC address which was previously added by mac_unicast_add().
3055fc4e975dSVenugopal Iyer  */
3056fc4e975dSVenugopal Iyer int
mac_unicast_remove(mac_client_handle_t mch,mac_unicast_handle_t mah)3057fc4e975dSVenugopal Iyer mac_unicast_remove(mac_client_handle_t mch, mac_unicast_handle_t mah)
3058fc4e975dSVenugopal Iyer {
3059fc4e975dSVenugopal Iyer 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3060fc4e975dSVenugopal Iyer 	mac_unicast_impl_t *muip = (mac_unicast_impl_t *)mah;
3061fc4e975dSVenugopal Iyer 	mac_unicast_impl_t *pre;
3062fc4e975dSVenugopal Iyer 	mac_impl_t *mip = mcip->mci_mip;
3063fc4e975dSVenugopal Iyer 	flow_entry_t		*flent;
30640dc2366fSVenugopal Iyer 	uint16_t mui_vid;
3065fc4e975dSVenugopal Iyer 
3066fc4e975dSVenugopal Iyer 	i_mac_perim_enter(mip);
3067fc4e975dSVenugopal Iyer 	if (mcip->mci_flags & MAC_CLIENT_FLAGS_VNIC_PRIMARY) {
3068fc4e975dSVenugopal Iyer 		/*
306984de666eSRyan Zezeski 		 * Call made by the upper MAC client of a VNIC.
3070fc4e975dSVenugopal Iyer 		 * There's nothing much to do, the unicast address will
3071fc4e975dSVenugopal Iyer 		 * be removed by the VNIC driver when the VNIC is deleted,
3072fc4e975dSVenugopal Iyer 		 * but let's ensure that all our transmit is done before
3073fc4e975dSVenugopal Iyer 		 * the client does a mac_client_stop lest it trigger an
3074fc4e975dSVenugopal Iyer 		 * assert in the driver.
3075fc4e975dSVenugopal Iyer 		 */
307684de666eSRyan Zezeski 		ASSERT(muip->mui_vid == VLAN_ID_NONE);
3077fc4e975dSVenugopal Iyer 
3078fc4e975dSVenugopal Iyer 		mac_tx_client_flush(mcip);
3079fc4e975dSVenugopal Iyer 
3080fc4e975dSVenugopal Iyer 		if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
3081fc4e975dSVenugopal Iyer 			mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
3082fc4e975dSVenugopal Iyer 			if (mcip->mci_rx_p_fn != NULL) {
3083fc4e975dSVenugopal Iyer 				mac_rx_set(mch, mcip->mci_rx_p_fn,
3084fc4e975dSVenugopal Iyer 				    mcip->mci_rx_p_arg);
3085fc4e975dSVenugopal Iyer 				mcip->mci_rx_p_fn = NULL;
3086fc4e975dSVenugopal Iyer 				mcip->mci_rx_p_arg = NULL;
3087fc4e975dSVenugopal Iyer 			}
3088fc4e975dSVenugopal Iyer 			kmem_free(muip, sizeof (mac_unicast_impl_t));
3089fc4e975dSVenugopal Iyer 			i_mac_perim_exit(mip);
3090fc4e975dSVenugopal Iyer 			return (0);
3091fc4e975dSVenugopal Iyer 		}
3092fc4e975dSVenugopal Iyer 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_VNIC_PRIMARY;
3093fc4e975dSVenugopal Iyer 
3094fc4e975dSVenugopal Iyer 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
3095fc4e975dSVenugopal Iyer 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
3096fc4e975dSVenugopal Iyer 
3097fc4e975dSVenugopal Iyer 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
3098fc4e975dSVenugopal Iyer 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
3099fc4e975dSVenugopal Iyer 
3100fc4e975dSVenugopal Iyer 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
3101fc4e975dSVenugopal Iyer 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
3102fc4e975dSVenugopal Iyer 
3103fc4e975dSVenugopal Iyer 		kmem_free(muip, sizeof (mac_unicast_impl_t));
3104fc4e975dSVenugopal Iyer 		i_mac_perim_exit(mip);
3105fc4e975dSVenugopal Iyer 		return (0);
3106fc4e975dSVenugopal Iyer 	}
3107fc4e975dSVenugopal Iyer 
3108fc4e975dSVenugopal Iyer 	ASSERT(muip != NULL);
3109fc4e975dSVenugopal Iyer 
3110fc4e975dSVenugopal Iyer 	/*
3111fc4e975dSVenugopal Iyer 	 * We are removing a passive client, we haven't setup the datapath
3112fc4e975dSVenugopal Iyer 	 * for this yet, so nothing much to do.
3113fc4e975dSVenugopal Iyer 	 */
3114b460a6edSVenugopal Iyer 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PASSIVE_PRIMARY) != 0) {
3115fc4e975dSVenugopal Iyer 
3116fc4e975dSVenugopal Iyer 		ASSERT((mcip->mci_flent->fe_flags & FE_MC_NO_DATAPATH) != 0);
3117fc4e975dSVenugopal Iyer 		ASSERT(mcip->mci_p_unicast_list == muip);
3118fc4e975dSVenugopal Iyer 
3119b460a6edSVenugopal Iyer 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
3120b460a6edSVenugopal Iyer 
3121fc4e975dSVenugopal Iyer 		mcip->mci_p_unicast_list = NULL;
3122fc4e975dSVenugopal Iyer 		mcip->mci_rx_p_fn = NULL;
3123fc4e975dSVenugopal Iyer 		mcip->mci_rx_p_arg = NULL;
3124fc4e975dSVenugopal Iyer 
3125fc4e975dSVenugopal Iyer 		mcip->mci_state_flags &= ~MCIS_UNICAST_HW;
3126fc4e975dSVenugopal Iyer 
3127fc4e975dSVenugopal Iyer 		if (mcip->mci_state_flags & MCIS_TAG_DISABLE)
3128fc4e975dSVenugopal Iyer 			mcip->mci_state_flags &= ~MCIS_TAG_DISABLE;
3129fc4e975dSVenugopal Iyer 
3130fc4e975dSVenugopal Iyer 		if (mcip->mci_state_flags & MCIS_STRIP_DISABLE)
3131fc4e975dSVenugopal Iyer 			mcip->mci_state_flags &= ~MCIS_STRIP_DISABLE;
3132fc4e975dSVenugopal Iyer 
3133fc4e975dSVenugopal Iyer 		if (mcip->mci_state_flags & MCIS_DISABLE_TX_VID_CHECK)
3134fc4e975dSVenugopal Iyer 			mcip->mci_state_flags &= ~MCIS_DISABLE_TX_VID_CHECK;
3135fc4e975dSVenugopal Iyer 
3136fc4e975dSVenugopal Iyer 		kmem_free(muip, sizeof (mac_unicast_impl_t));
3137fc4e975dSVenugopal Iyer 		i_mac_perim_exit(mip);
3138fc4e975dSVenugopal Iyer 		return (0);
3139fc4e975dSVenugopal Iyer 	}
314084de666eSRyan Zezeski 
3141fc4e975dSVenugopal Iyer 	/*
3142fc4e975dSVenugopal Iyer 	 * Remove the VID from the list of client's VIDs.
3143fc4e975dSVenugopal Iyer 	 */
3144fc4e975dSVenugopal Iyer 	pre = mcip->mci_unicast_list;
3145fc4e975dSVenugopal Iyer 	if (muip == pre) {
3146fc4e975dSVenugopal Iyer 		mcip->mci_unicast_list = muip->mui_next;
3147fc4e975dSVenugopal Iyer 	} else {
3148fc4e975dSVenugopal Iyer 		while ((pre->mui_next != NULL) && (pre->mui_next != muip))
3149fc4e975dSVenugopal Iyer 			pre = pre->mui_next;
3150fc4e975dSVenugopal Iyer 		ASSERT(pre->mui_next == muip);
3151fc4e975dSVenugopal Iyer 		rw_enter(&mcip->mci_rw_lock, RW_WRITER);
3152fc4e975dSVenugopal Iyer 		pre->mui_next = muip->mui_next;
3153fc4e975dSVenugopal Iyer 		rw_exit(&mcip->mci_rw_lock);
3154fc4e975dSVenugopal Iyer 	}
3155fc4e975dSVenugopal Iyer 
3156fc4e975dSVenugopal Iyer 	if (!mac_client_single_rcvr(mcip)) {
3157fc4e975dSVenugopal Iyer 		/*
3158fc4e975dSVenugopal Iyer 		 * This MAC client is shared by more than one unicast
3159fc4e975dSVenugopal Iyer 		 * addresses, so we will just remove the flent
3160fc4e975dSVenugopal Iyer 		 * corresponding to the address being removed. We don't invoke
3161fc4e975dSVenugopal Iyer 		 * mac_rx_classify_flow_rem() since the additional flow is
3162fc4e975dSVenugopal Iyer 		 * not associated with its own separate set of SRS and rings,
3163fc4e975dSVenugopal Iyer 		 * and these constructs are still needed for the remaining
3164fc4e975dSVenugopal Iyer 		 * flows.
3165fc4e975dSVenugopal Iyer 		 */
3166fc4e975dSVenugopal Iyer 		flent = mac_client_get_flow(mcip, muip);
316784de666eSRyan Zezeski 		VERIFY3P(flent, !=, NULL);
3168fc4e975dSVenugopal Iyer 
3169fc4e975dSVenugopal Iyer 		/*
3170fc4e975dSVenugopal Iyer 		 * The first one is disappearing, need to make sure
3171fc4e975dSVenugopal Iyer 		 * we replace it with another from the list of
3172fc4e975dSVenugopal Iyer 		 * shared clients.
3173fc4e975dSVenugopal Iyer 		 */
3174fc4e975dSVenugopal Iyer 		if (flent == mcip->mci_flent)
3175fc4e975dSVenugopal Iyer 			flent = mac_client_swap_mciflent(mcip);
3176fc4e975dSVenugopal Iyer 		mac_client_remove_flow_from_list(mcip, flent);
3177fc4e975dSVenugopal Iyer 		mac_flow_remove(mip->mi_flow_tab, flent, B_FALSE);
3178fc4e975dSVenugopal Iyer 		mac_flow_wait(flent, FLOW_DRIVER_UPCALL);
3179fc4e975dSVenugopal Iyer 
3180fc4e975dSVenugopal Iyer 		/*
3181fc4e975dSVenugopal Iyer 		 * The multicast groups that were added by the client so
3182fc4e975dSVenugopal Iyer 		 * far must be removed from the brodcast domain corresponding
3183fc4e975dSVenugopal Iyer 		 * to the VID being removed.
3184fc4e975dSVenugopal Iyer 		 */
3185fc4e975dSVenugopal Iyer 		mac_client_bcast_refresh(mcip, mac_client_update_mcast,
3186fc4e975dSVenugopal Iyer 		    (void *)flent, B_FALSE);
3187fc4e975dSVenugopal Iyer 
3188fc4e975dSVenugopal Iyer 		if (mip->mi_type->mt_brdcst_addr != NULL) {
3189fc4e975dSVenugopal Iyer 			mac_bcast_delete(mcip, mip->mi_type->mt_brdcst_addr,
3190fc4e975dSVenugopal Iyer 			    muip->mui_vid);
3191fc4e975dSVenugopal Iyer 		}
3192fc4e975dSVenugopal Iyer 
3193fc4e975dSVenugopal Iyer 		FLOW_FINAL_REFRELE(flent);
3194fc4e975dSVenugopal Iyer 		ASSERT(!(mcip->mci_state_flags & MCIS_EXCLUSIVE));
319584de666eSRyan Zezeski 
3196fc4e975dSVenugopal Iyer 		/*
3197fc4e975dSVenugopal Iyer 		 * Enable fastpath if this is a VNIC or a VLAN.
3198fc4e975dSVenugopal Iyer 		 */
3199fc4e975dSVenugopal Iyer 		if (mcip->mci_state_flags & MCIS_IS_VNIC)
3200fc4e975dSVenugopal Iyer 			mac_fastpath_enable((mac_handle_t)mip);
3201fc4e975dSVenugopal Iyer 		mac_stop((mac_handle_t)mip);
3202fc4e975dSVenugopal Iyer 		i_mac_perim_exit(mip);
3203fc4e975dSVenugopal Iyer 		return (0);
3204fc4e975dSVenugopal Iyer 	}
3205fc4e975dSVenugopal Iyer 
32060dc2366fSVenugopal Iyer 	mui_vid = muip->mui_vid;
3207fc4e975dSVenugopal Iyer 	mac_client_datapath_teardown(mch, muip, flent);
3208fc4e975dSVenugopal Iyer 
320984de666eSRyan Zezeski 	if ((mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY) &&
321084de666eSRyan Zezeski 	    mui_vid == VLAN_ID_NONE) {
32110dc2366fSVenugopal Iyer 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PRIMARY;
32120dc2366fSVenugopal Iyer 	} else {
32130dc2366fSVenugopal Iyer 		i_mac_perim_exit(mip);
32140dc2366fSVenugopal Iyer 		return (0);
32150dc2366fSVenugopal Iyer 	}
32160dc2366fSVenugopal Iyer 
3217fc4e975dSVenugopal Iyer 	/*
3218fc4e975dSVenugopal Iyer 	 * If we are removing the primary, check if we have a passive primary
3219fc4e975dSVenugopal Iyer 	 * client that we need to activate now.
3220fc4e975dSVenugopal Iyer 	 */
3221fc4e975dSVenugopal Iyer 	mcip = mac_get_passive_primary_client(mip);
3222fc4e975dSVenugopal Iyer 	if (mcip != NULL) {
32230dc2366fSVenugopal Iyer 		mac_resource_props_t	*mrp;
3224fc4e975dSVenugopal Iyer 		mac_unicast_impl_t	*muip;
3225fc4e975dSVenugopal Iyer 
3226fc4e975dSVenugopal Iyer 		mcip->mci_flags &= ~MAC_CLIENT_FLAGS_PASSIVE_PRIMARY;
32270dc2366fSVenugopal Iyer 		mrp = kmem_zalloc(sizeof (*mrp), KM_SLEEP);
32280dc2366fSVenugopal Iyer 
3229fc4e975dSVenugopal Iyer 		/*
3230fc4e975dSVenugopal Iyer 		 * Apply the property cached in the mac_impl_t to the
3231fc4e975dSVenugopal Iyer 		 * primary mac client.
3232fc4e975dSVenugopal Iyer 		 */
32330dc2366fSVenugopal Iyer 		mac_get_resources((mac_handle_t)mip, mrp);
32340dc2366fSVenugopal Iyer 		(void) mac_client_set_resources(mch, mrp);
3235fc4e975dSVenugopal Iyer 		ASSERT(mcip->mci_p_unicast_list != NULL);
3236fc4e975dSVenugopal Iyer 		muip = mcip->mci_p_unicast_list;
3237fc4e975dSVenugopal Iyer 		mcip->mci_p_unicast_list = NULL;
3238fc4e975dSVenugopal Iyer 		if (mac_client_datapath_setup(mcip, VLAN_ID_NONE,
32390dc2366fSVenugopal Iyer 		    mip->mi_addr, mrp, B_TRUE, muip) == 0) {
3240fc4e975dSVenugopal Iyer 			if (mcip->mci_rx_p_fn != NULL) {
3241fc4e975dSVenugopal Iyer 				mac_rx_set(mch, mcip->mci_rx_p_fn,
3242fc4e975dSVenugopal Iyer 				    mcip->mci_rx_p_arg);
3243fc4e975dSVenugopal Iyer 				mcip->mci_rx_p_fn = NULL;
3244fc4e975dSVenugopal Iyer 				mcip->mci_rx_p_arg = NULL;
3245fc4e975dSVenugopal Iyer 			}
3246c9c211bcSVenugopal Iyer 		} else {
3247c9c211bcSVenugopal Iyer 			kmem_free(muip, sizeof (mac_unicast_impl_t));
3248fc4e975dSVenugopal Iyer 		}
32490dc2366fSVenugopal Iyer 		kmem_free(mrp, sizeof (*mrp));
3250fc4e975dSVenugopal Iyer 	}
32515d460eafSCathy Zhou 	i_mac_perim_exit(mip);
3252da14cebeSEric Cheng 	return (0);
3253da14cebeSEric Cheng }
3254da14cebeSEric Cheng 
3255da14cebeSEric Cheng /*
3256da14cebeSEric Cheng  * Multicast add function invoked by MAC clients.
3257da14cebeSEric Cheng  */
3258da14cebeSEric Cheng int
mac_multicast_add(mac_client_handle_t mch,const uint8_t * addr)3259da14cebeSEric Cheng mac_multicast_add(mac_client_handle_t mch, const uint8_t *addr)
3260da14cebeSEric Cheng {
3261da14cebeSEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3262da14cebeSEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
3263da14cebeSEric Cheng 	flow_entry_t		*flent = mcip->mci_flent_list;
3264da14cebeSEric Cheng 	flow_entry_t		*prev_fe = NULL;
3265da14cebeSEric Cheng 	uint16_t		vid;
3266da14cebeSEric Cheng 	int			err = 0;
3267da14cebeSEric Cheng 
3268da14cebeSEric Cheng 	/* Verify the address is a valid multicast address */
3269da14cebeSEric Cheng 	if ((err = mip->mi_type->mt_ops.mtops_multicst_verify(addr,
3270da14cebeSEric Cheng 	    mip->mi_pdata)) != 0)
3271da14cebeSEric Cheng 		return (err);
3272da14cebeSEric Cheng 
3273da14cebeSEric Cheng 	i_mac_perim_enter(mip);
3274da14cebeSEric Cheng 	while (flent != NULL) {
3275da14cebeSEric Cheng 		vid = i_mac_flow_vid(flent);
3276da14cebeSEric Cheng 
3277da14cebeSEric Cheng 		err = mac_bcast_add((mac_client_impl_t *)mch, addr, vid,
3278da14cebeSEric Cheng 		    MAC_ADDRTYPE_MULTICAST);
3279da14cebeSEric Cheng 		if (err != 0)
3280da14cebeSEric Cheng 			break;
3281da14cebeSEric Cheng 		prev_fe = flent;
3282da14cebeSEric Cheng 		flent = flent->fe_client_next;
3283da14cebeSEric Cheng 	}
3284da14cebeSEric Cheng 
3285da14cebeSEric Cheng 	/*
3286da14cebeSEric Cheng 	 * If we failed adding, then undo all, rather than partial
3287da14cebeSEric Cheng 	 * success.
3288da14cebeSEric Cheng 	 */
3289da14cebeSEric Cheng 	if (flent != NULL && prev_fe != NULL) {
3290da14cebeSEric Cheng 		flent = mcip->mci_flent_list;
3291da14cebeSEric Cheng 		while (flent != prev_fe->fe_client_next) {
3292da14cebeSEric Cheng 			vid = i_mac_flow_vid(flent);
3293da14cebeSEric Cheng 			mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
3294da14cebeSEric Cheng 			flent = flent->fe_client_next;
3295da14cebeSEric Cheng 		}
3296da14cebeSEric Cheng 	}
3297da14cebeSEric Cheng 	i_mac_perim_exit(mip);
3298da14cebeSEric Cheng 	return (err);
3299da14cebeSEric Cheng }
3300da14cebeSEric Cheng 
3301da14cebeSEric Cheng /*
3302da14cebeSEric Cheng  * Multicast delete function invoked by MAC clients.
3303da14cebeSEric Cheng  */
3304da14cebeSEric Cheng void
mac_multicast_remove(mac_client_handle_t mch,const uint8_t * addr)3305da14cebeSEric Cheng mac_multicast_remove(mac_client_handle_t mch, const uint8_t *addr)
3306da14cebeSEric Cheng {
3307da14cebeSEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3308da14cebeSEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
3309da14cebeSEric Cheng 	flow_entry_t		*flent;
3310da14cebeSEric Cheng 	uint16_t		vid;
3311da14cebeSEric Cheng 
3312da14cebeSEric Cheng 	i_mac_perim_enter(mip);
3313da14cebeSEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
3314da14cebeSEric Cheng 	    flent = flent->fe_client_next) {
3315da14cebeSEric Cheng 		vid = i_mac_flow_vid(flent);
3316da14cebeSEric Cheng 		mac_bcast_delete((mac_client_impl_t *)mch, addr, vid);
3317da14cebeSEric Cheng 	}
3318da14cebeSEric Cheng 	i_mac_perim_exit(mip);
3319da14cebeSEric Cheng }
3320da14cebeSEric Cheng 
3321da14cebeSEric Cheng /*
3322da14cebeSEric Cheng  * When a MAC client desires to capture packets on an interface,
3323da14cebeSEric Cheng  * it registers a promiscuous call back with mac_promisc_add().
3324da14cebeSEric Cheng  * There are three types of promiscuous callbacks:
3325da14cebeSEric Cheng  *
3326da14cebeSEric Cheng  * * MAC_CLIENT_PROMISC_ALL
3327da14cebeSEric Cheng  *   Captures all packets sent and received by the MAC client,
3328da14cebeSEric Cheng  *   the physical interface, as well as all other MAC clients
3329da14cebeSEric Cheng  *   defined on top of the same MAC.
3330da14cebeSEric Cheng  *
3331da14cebeSEric Cheng  * * MAC_CLIENT_PROMISC_FILTERED
3332da14cebeSEric Cheng  *   Captures all packets sent and received by the MAC client,
3333da14cebeSEric Cheng  *   plus all multicast traffic sent and received by the phyisical
3334da14cebeSEric Cheng  *   interface and the other MAC clients.
3335da14cebeSEric Cheng  *
3336da14cebeSEric Cheng  * * MAC_CLIENT_PROMISC_MULTI
3337da14cebeSEric Cheng  *   Captures all broadcast and multicast packets sent and
3338da14cebeSEric Cheng  *   received by the MAC clients as well as the physical interface.
3339da14cebeSEric Cheng  *
3340da14cebeSEric Cheng  * In all cases, the underlying MAC is put in promiscuous mode.
3341da14cebeSEric Cheng  */
3342da14cebeSEric Cheng int
mac_promisc_add(mac_client_handle_t mch,mac_client_promisc_type_t type,mac_rx_t fn,void * arg,mac_promisc_handle_t * mphp,uint16_t flags)3343da14cebeSEric Cheng mac_promisc_add(mac_client_handle_t mch, mac_client_promisc_type_t type,
3344da14cebeSEric Cheng     mac_rx_t fn, void *arg, mac_promisc_handle_t *mphp, uint16_t flags)
3345da14cebeSEric Cheng {
3346da14cebeSEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3347da14cebeSEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
3348da14cebeSEric Cheng 	mac_promisc_impl_t *mpip;
3349da14cebeSEric Cheng 	mac_cb_info_t	*mcbi;
3350da14cebeSEric Cheng 	int rc;
3351da14cebeSEric Cheng 
3352da14cebeSEric Cheng 	i_mac_perim_enter(mip);
3353da14cebeSEric Cheng 
33543bdd2dd4SMichael Lim 	if ((rc = mac_start((mac_handle_t)mip)) != 0) {
3355da14cebeSEric Cheng 		i_mac_perim_exit(mip);
3356da14cebeSEric Cheng 		return (rc);
3357da14cebeSEric Cheng 	}
3358da14cebeSEric Cheng 
3359da14cebeSEric Cheng 	if ((mcip->mci_state_flags & MCIS_IS_VNIC) &&
336010a40492SRobert Mustacchi 	    type == MAC_CLIENT_PROMISC_ALL &&
336110a40492SRobert Mustacchi 	    (mcip->mci_protect_flags & MPT_FLAG_PROMISC_FILTERED)) {
3362da14cebeSEric Cheng 		/*
3363da14cebeSEric Cheng 		 * The function is being invoked by the upper MAC client
3364da14cebeSEric Cheng 		 * of a VNIC. The VNIC should only see the traffic
3365da14cebeSEric Cheng 		 * it is entitled to.
3366da14cebeSEric Cheng 		 */
3367da14cebeSEric Cheng 		type = MAC_CLIENT_PROMISC_FILTERED;
3368da14cebeSEric Cheng 	}
3369da14cebeSEric Cheng 
3370da14cebeSEric Cheng 
3371da14cebeSEric Cheng 	/*
3372da14cebeSEric Cheng 	 * Turn on promiscuous mode for the underlying NIC.
3373da14cebeSEric Cheng 	 * This is needed even for filtered callbacks which
3374da14cebeSEric Cheng 	 * expect to receive all multicast traffic on the wire.
3375da14cebeSEric Cheng 	 *
3376da14cebeSEric Cheng 	 * Physical promiscuous mode should not be turned on if
3377da14cebeSEric Cheng 	 * MAC_PROMISC_FLAGS_NO_PHYS is set.
3378da14cebeSEric Cheng 	 */
3379da14cebeSEric Cheng 	if ((flags & MAC_PROMISC_FLAGS_NO_PHYS) == 0) {
3380d91a22bfSGirish Moodalbail 		if ((rc = i_mac_promisc_set(mip, B_TRUE)) != 0) {
33813bdd2dd4SMichael Lim 			mac_stop((mac_handle_t)mip);
3382da14cebeSEric Cheng 			i_mac_perim_exit(mip);
3383da14cebeSEric Cheng 			return (rc);
3384da14cebeSEric Cheng 		}
3385da14cebeSEric Cheng 	}
3386da14cebeSEric Cheng 
3387da14cebeSEric Cheng 	mpip = kmem_cache_alloc(mac_promisc_impl_cache, KM_SLEEP);
3388da14cebeSEric Cheng 
3389da14cebeSEric Cheng 	mpip->mpi_type = type;
3390da14cebeSEric Cheng 	mpip->mpi_fn = fn;
3391da14cebeSEric Cheng 	mpip->mpi_arg = arg;
3392da14cebeSEric Cheng 	mpip->mpi_mcip = mcip;
3393da14cebeSEric Cheng 	mpip->mpi_no_tx_loop = ((flags & MAC_PROMISC_FLAGS_NO_TX_LOOP) != 0);
3394da14cebeSEric Cheng 	mpip->mpi_no_phys = ((flags & MAC_PROMISC_FLAGS_NO_PHYS) != 0);
3395ae6aa22aSVenugopal Iyer 	mpip->mpi_strip_vlan_tag =
3396ae6aa22aSVenugopal Iyer 	    ((flags & MAC_PROMISC_FLAGS_VLAN_TAG_STRIP) != 0);
33970a0e9771SDarren Reed 	mpip->mpi_no_copy = ((flags & MAC_PROMISC_FLAGS_NO_COPY) != 0);
3398da14cebeSEric Cheng 
3399da14cebeSEric Cheng 	mcbi = &mip->mi_promisc_cb_info;
3400da14cebeSEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
3401da14cebeSEric Cheng 
3402da14cebeSEric Cheng 	mac_callback_add(&mip->mi_promisc_cb_info, &mcip->mci_promisc_list,
3403da14cebeSEric Cheng 	    &mpip->mpi_mci_link);
3404da14cebeSEric Cheng 	mac_callback_add(&mip->mi_promisc_cb_info, &mip->mi_promisc_list,
3405da14cebeSEric Cheng 	    &mpip->mpi_mi_link);
3406da14cebeSEric Cheng 
3407da14cebeSEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
3408da14cebeSEric Cheng 
3409da14cebeSEric Cheng 	*mphp = (mac_promisc_handle_t)mpip;
34101a41ca23SJerry Jelinek 
34111a41ca23SJerry Jelinek 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
34121a41ca23SJerry Jelinek 		mac_impl_t *umip = mcip->mci_upper_mip;
34131a41ca23SJerry Jelinek 
34141a41ca23SJerry Jelinek 		ASSERT(umip != NULL);
34151a41ca23SJerry Jelinek 		mac_vnic_secondary_update(umip);
34161a41ca23SJerry Jelinek 	}
34171a41ca23SJerry Jelinek 
3418da14cebeSEric Cheng 	i_mac_perim_exit(mip);
34191a41ca23SJerry Jelinek 
3420da14cebeSEric Cheng 	return (0);
3421da14cebeSEric Cheng }
3422da14cebeSEric Cheng 
3423da14cebeSEric Cheng /*
3424da14cebeSEric Cheng  * Remove a multicast address previously aded through mac_promisc_add().
3425da14cebeSEric Cheng  */
34265ecc58b1SGirish Moodalbail void
mac_promisc_remove(mac_promisc_handle_t mph)3427da14cebeSEric Cheng mac_promisc_remove(mac_promisc_handle_t mph)
3428da14cebeSEric Cheng {
3429da14cebeSEric Cheng 	mac_promisc_impl_t *mpip = (mac_promisc_impl_t *)mph;
3430da14cebeSEric Cheng 	mac_client_impl_t *mcip = mpip->mpi_mcip;
3431da14cebeSEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
3432da14cebeSEric Cheng 	mac_cb_info_t *mcbi;
3433d91a22bfSGirish Moodalbail 	int rv;
3434da14cebeSEric Cheng 
3435da14cebeSEric Cheng 	i_mac_perim_enter(mip);
3436da14cebeSEric Cheng 
3437da14cebeSEric Cheng 	/*
3438da14cebeSEric Cheng 	 * Even if the device can't be reset into normal mode, we still
3439da14cebeSEric Cheng 	 * need to clear the client promisc callbacks. The client may want
3440da14cebeSEric Cheng 	 * to close the mac end point and we can't have stale callbacks.
3441da14cebeSEric Cheng 	 */
3442da14cebeSEric Cheng 	if (!(mpip->mpi_no_phys)) {
3443d91a22bfSGirish Moodalbail 		if ((rv = i_mac_promisc_set(mip, B_FALSE)) != 0) {
3444d91a22bfSGirish Moodalbail 			cmn_err(CE_WARN, "%s: failed to switch OFF promiscuous"
3445d91a22bfSGirish Moodalbail 			    " mode because of error 0x%x", mip->mi_name, rv);
3446d91a22bfSGirish Moodalbail 		}
3447da14cebeSEric Cheng 	}
3448da14cebeSEric Cheng 	mcbi = &mip->mi_promisc_cb_info;
3449da14cebeSEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
3450da14cebeSEric Cheng 	if (mac_callback_remove(mcbi, &mip->mi_promisc_list,
3451da14cebeSEric Cheng 	    &mpip->mpi_mi_link)) {
3452da14cebeSEric Cheng 		VERIFY(mac_callback_remove(&mip->mi_promisc_cb_info,
3453da14cebeSEric Cheng 		    &mcip->mci_promisc_list, &mpip->mpi_mci_link));
3454da14cebeSEric Cheng 		kmem_cache_free(mac_promisc_impl_cache, mpip);
3455da14cebeSEric Cheng 	} else {
3456da14cebeSEric Cheng 		mac_callback_remove_wait(&mip->mi_promisc_cb_info);
3457da14cebeSEric Cheng 	}
34581a41ca23SJerry Jelinek 
34591a41ca23SJerry Jelinek 	if (mcip->mci_state_flags & MCIS_IS_VNIC) {
34601a41ca23SJerry Jelinek 		mac_impl_t *umip = mcip->mci_upper_mip;
34611a41ca23SJerry Jelinek 
34621a41ca23SJerry Jelinek 		ASSERT(umip != NULL);
34631a41ca23SJerry Jelinek 		mac_vnic_secondary_update(umip);
34641a41ca23SJerry Jelinek 	}
34651a41ca23SJerry Jelinek 
3466da14cebeSEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
34673bdd2dd4SMichael Lim 	mac_stop((mac_handle_t)mip);
3468da14cebeSEric Cheng 
3469da14cebeSEric Cheng 	i_mac_perim_exit(mip);
3470da14cebeSEric Cheng }
3471da14cebeSEric Cheng 
3472da14cebeSEric Cheng /*
3473da14cebeSEric Cheng  * Reference count the number of active Tx threads. MCI_TX_QUIESCE indicates
3474da14cebeSEric Cheng  * that a control operation wants to quiesce the Tx data flow in which case
3475da14cebeSEric Cheng  * we return an error. Holding any of the per cpu locks ensures that the
3476da14cebeSEric Cheng  * mci_tx_flag won't change.
3477da14cebeSEric Cheng  *
3478da14cebeSEric Cheng  * 'CPU' must be accessed just once and used to compute the index into the
3479da14cebeSEric Cheng  * percpu array, and that index must be used for the entire duration of the
3480da14cebeSEric Cheng  * packet send operation. Note that the thread may be preempted and run on
3481da14cebeSEric Cheng  * another cpu any time and so we can't use 'CPU' more than once for the
3482da14cebeSEric Cheng  * operation.
3483da14cebeSEric Cheng  */
3484da14cebeSEric Cheng #define	MAC_TX_TRY_HOLD(mcip, mytx, error)				\
3485da14cebeSEric Cheng {									\
3486da14cebeSEric Cheng 	(error) = 0;							\
3487da14cebeSEric Cheng 	(mytx) = &(mcip)->mci_tx_pcpu[CPU->cpu_seqid & mac_tx_percpu_cnt]; \
3488da14cebeSEric Cheng 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
3489da14cebeSEric Cheng 	if (!((mcip)->mci_tx_flag & MCI_TX_QUIESCE)) {			\
3490da14cebeSEric Cheng 		(mytx)->pcpu_tx_refcnt++;				\
3491da14cebeSEric Cheng 	} else {							\
3492da14cebeSEric Cheng 		(error) = -1;						\
3493da14cebeSEric Cheng 	}								\
3494da14cebeSEric Cheng 	mutex_exit(&(mytx)->pcpu_tx_lock);				\
3495da14cebeSEric Cheng }
3496da14cebeSEric Cheng 
3497da14cebeSEric Cheng /*
3498da14cebeSEric Cheng  * Release the reference. If needed, signal any control operation waiting
3499da14cebeSEric Cheng  * for Tx quiescence. The wait and signal are always done using the
3500da14cebeSEric Cheng  * mci_tx_pcpu[0]'s lock
3501da14cebeSEric Cheng  */
3502da14cebeSEric Cheng #define	MAC_TX_RELE(mcip, mytx) {					\
3503da14cebeSEric Cheng 	mutex_enter(&(mytx)->pcpu_tx_lock);				\
3504da14cebeSEric Cheng 	if (--(mytx)->pcpu_tx_refcnt == 0 &&				\
3505da14cebeSEric Cheng 	    (mcip)->mci_tx_flag & MCI_TX_QUIESCE) {			\
3506da14cebeSEric Cheng 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
3507da14cebeSEric Cheng 		mutex_enter(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
3508da14cebeSEric Cheng 		cv_signal(&(mcip)->mci_tx_cv);				\
3509da14cebeSEric Cheng 		mutex_exit(&(mcip)->mci_tx_pcpu[0].pcpu_tx_lock);	\
3510da14cebeSEric Cheng 	} else {							\
3511da14cebeSEric Cheng 		mutex_exit(&(mytx)->pcpu_tx_lock);			\
3512da14cebeSEric Cheng 	}								\
3513da14cebeSEric Cheng }
3514da14cebeSEric Cheng 
3515da14cebeSEric Cheng /*
3516da14cebeSEric Cheng  * Send function invoked by MAC clients.
3517da14cebeSEric Cheng  */
3518da14cebeSEric Cheng mac_tx_cookie_t
mac_tx(mac_client_handle_t mch,mblk_t * mp_chain,uintptr_t hint,uint16_t flag,mblk_t ** ret_mp)3519da14cebeSEric Cheng mac_tx(mac_client_handle_t mch, mblk_t *mp_chain, uintptr_t hint,
3520da14cebeSEric Cheng     uint16_t flag, mblk_t **ret_mp)
3521da14cebeSEric Cheng {
352236f99a58SToomas Soome 	mac_tx_cookie_t		cookie = 0;
3523da14cebeSEric Cheng 	int			error;
3524da14cebeSEric Cheng 	mac_tx_percpu_t		*mytx;
3525da14cebeSEric Cheng 	mac_soft_ring_set_t	*srs;
3526da14cebeSEric Cheng 	flow_entry_t		*flent;
3527da14cebeSEric Cheng 	boolean_t		is_subflow = B_FALSE;
3528da14cebeSEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3529da14cebeSEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
3530da14cebeSEric Cheng 	mac_srs_tx_t		*srs_tx;
3531da14cebeSEric Cheng 
3532da14cebeSEric Cheng 	/*
3533da14cebeSEric Cheng 	 * Check whether the active Tx threads count is bumped already.
3534da14cebeSEric Cheng 	 */
3535da14cebeSEric Cheng 	if (!(flag & MAC_TX_NO_HOLD)) {
3536da14cebeSEric Cheng 		MAC_TX_TRY_HOLD(mcip, mytx, error);
3537da14cebeSEric Cheng 		if (error != 0) {
3538da14cebeSEric Cheng 			freemsgchain(mp_chain);
353936f99a58SToomas Soome 			return (0);
3540da14cebeSEric Cheng 		}
3541da14cebeSEric Cheng 	}
3542da14cebeSEric Cheng 
354325ec3e3dSEric Cheng 	/*
354425ec3e3dSEric Cheng 	 * If mac protection is enabled, only the permissible packets will be
354525ec3e3dSEric Cheng 	 * returned by mac_protect_check().
354625ec3e3dSEric Cheng 	 */
354725ec3e3dSEric Cheng 	if ((mcip->mci_flent->
354825ec3e3dSEric Cheng 	    fe_resource_props.mrp_mask & MRP_PROTECT) != 0 &&
354925ec3e3dSEric Cheng 	    (mp_chain = mac_protect_check(mch, mp_chain)) == NULL)
355025ec3e3dSEric Cheng 		goto done;
355125ec3e3dSEric Cheng 
3552da14cebeSEric Cheng 	if (mcip->mci_subflow_tab != NULL &&
3553da14cebeSEric Cheng 	    mcip->mci_subflow_tab->ft_flow_count > 0 &&
3554da14cebeSEric Cheng 	    mac_flow_lookup(mcip->mci_subflow_tab, mp_chain,
3555da14cebeSEric Cheng 	    FLOW_OUTBOUND, &flent) == 0) {
3556da14cebeSEric Cheng 		/*
3557da14cebeSEric Cheng 		 * The main assumption here is that if in the event
3558da14cebeSEric Cheng 		 * we get a chain, all the packets will be classified
3559da14cebeSEric Cheng 		 * to the same Flow/SRS. If this changes for any
3560da14cebeSEric Cheng 		 * reason, the following logic should change as well.
3561da14cebeSEric Cheng 		 * I suppose the fanout_hint also assumes this .
3562da14cebeSEric Cheng 		 */
3563da14cebeSEric Cheng 		ASSERT(flent != NULL);
3564da14cebeSEric Cheng 		is_subflow = B_TRUE;
3565da14cebeSEric Cheng 	} else {
3566da14cebeSEric Cheng 		flent = mcip->mci_flent;
3567da14cebeSEric Cheng 	}
3568da14cebeSEric Cheng 
3569da14cebeSEric Cheng 	srs = flent->fe_tx_srs;
35700a0e9771SDarren Reed 	/*
35710a0e9771SDarren Reed 	 * This is to avoid panics with PF_PACKET that can call mac_tx()
35720a0e9771SDarren Reed 	 * against an interface that is not capable of sending. A rewrite
35730a0e9771SDarren Reed 	 * of the mac datapath is required to remove this limitation.
35740a0e9771SDarren Reed 	 */
35750a0e9771SDarren Reed 	if (srs == NULL) {
35760a0e9771SDarren Reed 		freemsgchain(mp_chain);
357725ec3e3dSEric Cheng 		goto done;
35780a0e9771SDarren Reed 	}
357925ec3e3dSEric Cheng 
3580da14cebeSEric Cheng 	srs_tx = &srs->srs_tx;
3581da14cebeSEric Cheng 	if (srs_tx->st_mode == SRS_TX_DEFAULT &&
3582da14cebeSEric Cheng 	    (srs->srs_state & SRS_ENQUEUED) == 0 &&
3583c61a1653SRyan Zezeski 	    mip->mi_nactiveclients == 1 &&
3584c61a1653SRyan Zezeski 	    mp_chain->b_next == NULL &&
3585c61a1653SRyan Zezeski 	    (DB_CKSUMFLAGS(mp_chain) & HW_LSO) == 0) {
3586da14cebeSEric Cheng 		uint64_t	obytes;
3587da14cebeSEric Cheng 
3588da14cebeSEric Cheng 		/*
3589da14cebeSEric Cheng 		 * Since dls always opens the underlying MAC, nclients equals
3590da14cebeSEric Cheng 		 * to 1 means that the only active client is dls itself acting
3591da14cebeSEric Cheng 		 * as a primary client of the MAC instance. Since dls will not
3592da14cebeSEric Cheng 		 * send tagged packets in that case, and dls is trusted to send
3593da14cebeSEric Cheng 		 * packets for its allowed VLAN(s), the VLAN tag insertion and
3594da14cebeSEric Cheng 		 * check is required only if nclients is greater than 1.
3595da14cebeSEric Cheng 		 */
3596da14cebeSEric Cheng 		if (mip->mi_nclients > 1) {
3597da14cebeSEric Cheng 			if (MAC_VID_CHECK_NEEDED(mcip)) {
3598da14cebeSEric Cheng 				int	err = 0;
3599da14cebeSEric Cheng 
3600da14cebeSEric Cheng 				MAC_VID_CHECK(mcip, mp_chain, err);
3601da14cebeSEric Cheng 				if (err != 0) {
3602da14cebeSEric Cheng 					freemsg(mp_chain);
36030dc2366fSVenugopal Iyer 					mcip->mci_misc_stat.mms_txerrors++;
3604da14cebeSEric Cheng 					goto done;
3605da14cebeSEric Cheng 				}
3606da14cebeSEric Cheng 			}
3607da14cebeSEric Cheng 			if (MAC_TAG_NEEDED(mcip)) {
3608da14cebeSEric Cheng 				mp_chain = mac_add_vlan_tag(mp_chain, 0,
3609da14cebeSEric Cheng 				    mac_client_vid(mch));
3610da14cebeSEric Cheng 				if (mp_chain == NULL) {
36110dc2366fSVenugopal Iyer 					mcip->mci_misc_stat.mms_txerrors++;
3612da14cebeSEric Cheng 					goto done;
3613da14cebeSEric Cheng 				}
3614da14cebeSEric Cheng 			}
3615da14cebeSEric Cheng 		}
3616da14cebeSEric Cheng 
3617da14cebeSEric Cheng 		obytes = (mp_chain->b_cont == NULL ? MBLKL(mp_chain) :
3618da14cebeSEric Cheng 		    msgdsize(mp_chain));
3619da14cebeSEric Cheng 
3620c61a1653SRyan Zezeski 		mp_chain = mac_provider_tx(mip, srs_tx->st_arg2, mp_chain,
3621c61a1653SRyan Zezeski 		    mcip);
3622c61a1653SRyan Zezeski 
3623da14cebeSEric Cheng 		if (mp_chain == NULL) {
362436f99a58SToomas Soome 			cookie = 0;
36250dc2366fSVenugopal Iyer 			SRS_TX_STAT_UPDATE(srs, opackets, 1);
36260dc2366fSVenugopal Iyer 			SRS_TX_STAT_UPDATE(srs, obytes, obytes);
3627da14cebeSEric Cheng 		} else {
3628da14cebeSEric Cheng 			mutex_enter(&srs->srs_lock);
3629da14cebeSEric Cheng 			cookie = mac_tx_srs_no_desc(srs, mp_chain,
3630da14cebeSEric Cheng 			    flag, ret_mp);
3631da14cebeSEric Cheng 			mutex_exit(&srs->srs_lock);
3632da14cebeSEric Cheng 		}
3633da14cebeSEric Cheng 	} else {
3634c61a1653SRyan Zezeski 		mblk_t *mp = mp_chain;
3635c61a1653SRyan Zezeski 		mblk_t *new_head = NULL;
3636c61a1653SRyan Zezeski 		mblk_t *new_tail = NULL;
3637c61a1653SRyan Zezeski 
3638c61a1653SRyan Zezeski 		/*
3639c61a1653SRyan Zezeski 		 * There are occasions where the packets arriving here
3640c61a1653SRyan Zezeski 		 * may request hardware offloads that are not
3641c61a1653SRyan Zezeski 		 * available from the underlying MAC provider. This
3642c61a1653SRyan Zezeski 		 * currently only happens when a packet is sent across
3643c61a1653SRyan Zezeski 		 * the MAC-loopback path of one MAC and then forwarded
3644c61a1653SRyan Zezeski 		 * (via IP) to another MAC that lacks one or more of
3645c61a1653SRyan Zezeski 		 * the hardware offloads provided by the first one.
3646c61a1653SRyan Zezeski 		 * However, in the future, we may choose to pretend
3647c61a1653SRyan Zezeski 		 * all MAC providers support all offloads, performing
3648c61a1653SRyan Zezeski 		 * emulation on Tx as needed.
3649c61a1653SRyan Zezeski 		 *
3650c61a1653SRyan Zezeski 		 * We iterate each mblk in-turn, emulating hardware
3651c61a1653SRyan Zezeski 		 * offloads as required. From this process, we create
3652c61a1653SRyan Zezeski 		 * a new chain. The new chain may be the same as the
3653c61a1653SRyan Zezeski 		 * original chain (no hardware emulation needed), a
3654c61a1653SRyan Zezeski 		 * collection of new mblks (hardware emulation
3655c61a1653SRyan Zezeski 		 * needed), or a mix. At this point, the chain is safe
3656c61a1653SRyan Zezeski 		 * for consumption by the underlying MAC provider and
3657c61a1653SRyan Zezeski 		 * is passed down to the SRS.
3658c61a1653SRyan Zezeski 		 */
3659c61a1653SRyan Zezeski 		while (mp != NULL) {
3660c61a1653SRyan Zezeski 			mblk_t *next = mp->b_next;
3661c61a1653SRyan Zezeski 			mblk_t *tail = NULL;
3662c61a1653SRyan Zezeski 			const uint16_t needed =
3663c61a1653SRyan Zezeski 			    (DB_CKSUMFLAGS(mp) ^ mip->mi_tx_cksum_flags) &
3664c61a1653SRyan Zezeski 			    DB_CKSUMFLAGS(mp);
3665c61a1653SRyan Zezeski 
3666c61a1653SRyan Zezeski 			mp->b_next = NULL;
3667c61a1653SRyan Zezeski 
3668c61a1653SRyan Zezeski 			if ((needed & (HCK_TX_FLAGS | HW_LSO_FLAGS)) != 0) {
3669c61a1653SRyan Zezeski 				mac_emul_t emul = 0;
3670c61a1653SRyan Zezeski 
3671c61a1653SRyan Zezeski 				if (needed & HCK_IPV4_HDRCKSUM)
3672c61a1653SRyan Zezeski 					emul |= MAC_IPCKSUM_EMUL;
3673c61a1653SRyan Zezeski 				if (needed & (HCK_PARTIALCKSUM | HCK_FULLCKSUM))
3674c61a1653SRyan Zezeski 					emul |= MAC_HWCKSUM_EMUL;
3675c61a1653SRyan Zezeski 				if (needed & HW_LSO)
3676c61a1653SRyan Zezeski 					emul = MAC_LSO_EMUL;
3677c61a1653SRyan Zezeski 
3678c61a1653SRyan Zezeski 				mac_hw_emul(&mp, &tail, NULL, emul);
3679c61a1653SRyan Zezeski 
3680c61a1653SRyan Zezeski 				if (mp == NULL) {
3681c61a1653SRyan Zezeski 					mp = next;
3682c61a1653SRyan Zezeski 					continue;
3683c61a1653SRyan Zezeski 				}
3684c61a1653SRyan Zezeski 			}
3685c61a1653SRyan Zezeski 
3686c61a1653SRyan Zezeski 			if (new_head == NULL) {
3687c61a1653SRyan Zezeski 				new_head = mp;
3688c61a1653SRyan Zezeski 			} else {
3689c61a1653SRyan Zezeski 				new_tail->b_next = mp;
3690c61a1653SRyan Zezeski 			}
3691c61a1653SRyan Zezeski 
3692c61a1653SRyan Zezeski 			new_tail = (tail == NULL) ? mp : tail;
3693c61a1653SRyan Zezeski 			mp = next;
3694c61a1653SRyan Zezeski 		}
3695c61a1653SRyan Zezeski 
3696c61a1653SRyan Zezeski 		if (new_head == NULL) {
3697c61a1653SRyan Zezeski 			cookie = 0;
3698c61a1653SRyan Zezeski 			goto done;
3699c61a1653SRyan Zezeski 		}
3700c61a1653SRyan Zezeski 
3701c61a1653SRyan Zezeski 		cookie = srs_tx->st_func(srs, new_head, hint, flag, ret_mp);
3702da14cebeSEric Cheng 	}
3703da14cebeSEric Cheng 
3704da14cebeSEric Cheng done:
3705da14cebeSEric Cheng 	if (is_subflow)
3706da14cebeSEric Cheng 		FLOW_REFRELE(flent);
3707da14cebeSEric Cheng 
3708da14cebeSEric Cheng 	if (!(flag & MAC_TX_NO_HOLD))
3709da14cebeSEric Cheng 		MAC_TX_RELE(mcip, mytx);
3710da14cebeSEric Cheng 
3711da14cebeSEric Cheng 	return (cookie);
3712da14cebeSEric Cheng }
3713da14cebeSEric Cheng 
3714da14cebeSEric Cheng /*
3715da14cebeSEric Cheng  * mac_tx_is_blocked
3716da14cebeSEric Cheng  *
3717da14cebeSEric Cheng  * Given a cookie, it returns if the ring identified by the cookie is
3718ae6aa22aSVenugopal Iyer  * flow-controlled or not. If NULL is passed in place of a cookie,
3719ae6aa22aSVenugopal Iyer  * then it finds out if any of the underlying rings belonging to the
3720ae6aa22aSVenugopal Iyer  * SRS is flow controlled or not and returns that status.
3721da14cebeSEric Cheng  */
3722da14cebeSEric Cheng /* ARGSUSED */
3723da14cebeSEric Cheng boolean_t
mac_tx_is_flow_blocked(mac_client_handle_t mch,mac_tx_cookie_t cookie)3724da14cebeSEric Cheng mac_tx_is_flow_blocked(mac_client_handle_t mch, mac_tx_cookie_t cookie)
3725da14cebeSEric Cheng {
3726da14cebeSEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3727ae6aa22aSVenugopal Iyer 	mac_soft_ring_set_t *mac_srs;
3728da14cebeSEric Cheng 	mac_soft_ring_t *sringp;
3729da14cebeSEric Cheng 	boolean_t blocked = B_FALSE;
3730ae6aa22aSVenugopal Iyer 	mac_tx_percpu_t *mytx;
3731ae6aa22aSVenugopal Iyer 	int err;
3732da14cebeSEric Cheng 	int i;
3733da14cebeSEric Cheng 
3734da14cebeSEric Cheng 	/*
3735ae6aa22aSVenugopal Iyer 	 * Bump the reference count so that mac_srs won't be deleted.
3736ae6aa22aSVenugopal Iyer 	 * If the client is currently quiesced and we failed to bump
3737ae6aa22aSVenugopal Iyer 	 * the reference, return B_TRUE so that flow control stays
3738ae6aa22aSVenugopal Iyer 	 * as enabled.
3739ae6aa22aSVenugopal Iyer 	 *
3740ae6aa22aSVenugopal Iyer 	 * Flow control will then be disabled once the client is no
3741ae6aa22aSVenugopal Iyer 	 * longer quiesced.
3742da14cebeSEric Cheng 	 */
3743ae6aa22aSVenugopal Iyer 	MAC_TX_TRY_HOLD(mcip, mytx, err);
3744ae6aa22aSVenugopal Iyer 	if (err != 0)
3745ae6aa22aSVenugopal Iyer 		return (B_TRUE);
3746ae6aa22aSVenugopal Iyer 
3747ae6aa22aSVenugopal Iyer 	if ((mac_srs = MCIP_TX_SRS(mcip)) == NULL) {
3748ae6aa22aSVenugopal Iyer 		MAC_TX_RELE(mcip, mytx);
3749da14cebeSEric Cheng 		return (B_FALSE);
3750ae6aa22aSVenugopal Iyer 	}
3751da14cebeSEric Cheng 
3752da14cebeSEric Cheng 	mutex_enter(&mac_srs->srs_lock);
37530dc2366fSVenugopal Iyer 	/*
37540dc2366fSVenugopal Iyer 	 * Only in the case of TX_FANOUT and TX_AGGR, the underlying
37550dc2366fSVenugopal Iyer 	 * softring (s_ring_state) will have the HIWAT set. This is
37560dc2366fSVenugopal Iyer 	 * the multiple Tx ring flow control case. For all other
37570dc2366fSVenugopal Iyer 	 * case, SRS (srs_state) will store the condition.
37580dc2366fSVenugopal Iyer 	 */
37590dc2366fSVenugopal Iyer 	if (mac_srs->srs_tx.st_mode == SRS_TX_FANOUT ||
37600dc2366fSVenugopal Iyer 	    mac_srs->srs_tx.st_mode == SRS_TX_AGGR) {
376136f99a58SToomas Soome 		if (cookie != 0) {
3762ae6aa22aSVenugopal Iyer 			sringp = (mac_soft_ring_t *)cookie;
3763ae6aa22aSVenugopal Iyer 			mutex_enter(&sringp->s_ring_lock);
3764ae6aa22aSVenugopal Iyer 			if (sringp->s_ring_state & S_RING_TX_HIWAT)
3765ae6aa22aSVenugopal Iyer 				blocked = B_TRUE;
3766ae6aa22aSVenugopal Iyer 			mutex_exit(&sringp->s_ring_lock);
3767ae6aa22aSVenugopal Iyer 		} else {
37680dc2366fSVenugopal Iyer 			for (i = 0; i < mac_srs->srs_tx_ring_count; i++) {
37690dc2366fSVenugopal Iyer 				sringp = mac_srs->srs_tx_soft_rings[i];
3770da14cebeSEric Cheng 				mutex_enter(&sringp->s_ring_lock);
3771da14cebeSEric Cheng 				if (sringp->s_ring_state & S_RING_TX_HIWAT) {
3772da14cebeSEric Cheng 					blocked = B_TRUE;
3773da14cebeSEric Cheng 					mutex_exit(&sringp->s_ring_lock);
3774da14cebeSEric Cheng 					break;
3775da14cebeSEric Cheng 				}
3776da14cebeSEric Cheng 				mutex_exit(&sringp->s_ring_lock);
3777da14cebeSEric Cheng 			}
3778ae6aa22aSVenugopal Iyer 		}
3779da14cebeSEric Cheng 	} else {
3780da14cebeSEric Cheng 		blocked = (mac_srs->srs_state & SRS_TX_HIWAT);
3781da14cebeSEric Cheng 	}
3782da14cebeSEric Cheng 	mutex_exit(&mac_srs->srs_lock);
3783ae6aa22aSVenugopal Iyer 	MAC_TX_RELE(mcip, mytx);
3784da14cebeSEric Cheng 	return (blocked);
3785da14cebeSEric Cheng }
3786da14cebeSEric Cheng 
3787da14cebeSEric Cheng /*
3788da14cebeSEric Cheng  * Check if the MAC client is the primary MAC client.
3789da14cebeSEric Cheng  */
3790da14cebeSEric Cheng boolean_t
mac_is_primary_client(mac_client_impl_t * mcip)3791da14cebeSEric Cheng mac_is_primary_client(mac_client_impl_t *mcip)
3792da14cebeSEric Cheng {
3793da14cebeSEric Cheng 	return (mcip->mci_flags & MAC_CLIENT_FLAGS_PRIMARY);
3794da14cebeSEric Cheng }
3795da14cebeSEric Cheng 
3796da14cebeSEric Cheng void
mac_ioctl(mac_handle_t mh,queue_t * wq,mblk_t * bp)3797da14cebeSEric Cheng mac_ioctl(mac_handle_t mh, queue_t *wq, mblk_t *bp)
3798da14cebeSEric Cheng {
3799da14cebeSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
3800da14cebeSEric Cheng 	int cmd = ((struct iocblk *)bp->b_rptr)->ioc_cmd;
3801da14cebeSEric Cheng 
3802da14cebeSEric Cheng 	if ((cmd == ND_GET && (mip->mi_callbacks->mc_callbacks & MC_GETPROP)) ||
3803da14cebeSEric Cheng 	    (cmd == ND_SET && (mip->mi_callbacks->mc_callbacks & MC_SETPROP))) {
3804da14cebeSEric Cheng 		/*
3805da14cebeSEric Cheng 		 * If ndd props were registered, call them.
3806da14cebeSEric Cheng 		 * Note that ndd ioctls are Obsolete
3807da14cebeSEric Cheng 		 */
3808da14cebeSEric Cheng 		mac_ndd_ioctl(mip, wq, bp);
3809da14cebeSEric Cheng 		return;
3810da14cebeSEric Cheng 	}
3811da14cebeSEric Cheng 
3812da14cebeSEric Cheng 	/*
3813da14cebeSEric Cheng 	 * Call the driver to handle the ioctl.  The driver may not support
3814da14cebeSEric Cheng 	 * any ioctls, in which case we reply with a NAK on its behalf.
3815da14cebeSEric Cheng 	 */
3816da14cebeSEric Cheng 	if (mip->mi_callbacks->mc_callbacks & MC_IOCTL)
3817da14cebeSEric Cheng 		mip->mi_ioctl(mip->mi_driver, wq, bp);
3818da14cebeSEric Cheng 	else
3819da14cebeSEric Cheng 		miocnak(wq, bp, 0, EINVAL);
3820da14cebeSEric Cheng }
3821da14cebeSEric Cheng 
3822da14cebeSEric Cheng /*
3823da14cebeSEric Cheng  * Return the link state of the specified MAC instance.
3824da14cebeSEric Cheng  */
3825da14cebeSEric Cheng link_state_t
mac_link_get(mac_handle_t mh)3826da14cebeSEric Cheng mac_link_get(mac_handle_t mh)
3827da14cebeSEric Cheng {
3828da14cebeSEric Cheng 	return (((mac_impl_t *)mh)->mi_linkstate);
3829da14cebeSEric Cheng }
3830da14cebeSEric Cheng 
3831da14cebeSEric Cheng /*
3832da14cebeSEric Cheng  * Add a mac client specified notification callback. Please see the comments
3833da14cebeSEric Cheng  * above mac_callback_add() for general information about mac callback
3834da14cebeSEric Cheng  * addition/deletion in the presence of mac callback list walkers
3835da14cebeSEric Cheng  */
3836da14cebeSEric Cheng mac_notify_handle_t
mac_notify_add(mac_handle_t mh,mac_notify_t notify_fn,void * arg)3837da14cebeSEric Cheng mac_notify_add(mac_handle_t mh, mac_notify_t notify_fn, void *arg)
3838da14cebeSEric Cheng {
3839da14cebeSEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
3840da14cebeSEric Cheng 	mac_notify_cb_t		*mncb;
3841da14cebeSEric Cheng 	mac_cb_info_t		*mcbi;
3842da14cebeSEric Cheng 
3843da14cebeSEric Cheng 	/*
3844da14cebeSEric Cheng 	 * Allocate a notify callback structure, fill in the details and
3845da14cebeSEric Cheng 	 * use the mac callback list manipulation functions to chain into
3846da14cebeSEric Cheng 	 * the list of callbacks.
3847da14cebeSEric Cheng 	 */
3848da14cebeSEric Cheng 	mncb = kmem_zalloc(sizeof (mac_notify_cb_t), KM_SLEEP);
3849da14cebeSEric Cheng 	mncb->mncb_fn = notify_fn;
3850da14cebeSEric Cheng 	mncb->mncb_arg = arg;
3851da14cebeSEric Cheng 	mncb->mncb_mip = mip;
3852da14cebeSEric Cheng 	mncb->mncb_link.mcb_objp = mncb;
3853da14cebeSEric Cheng 	mncb->mncb_link.mcb_objsize = sizeof (mac_notify_cb_t);
3854da14cebeSEric Cheng 	mncb->mncb_link.mcb_flags = MCB_NOTIFY_CB_T;
3855da14cebeSEric Cheng 
3856da14cebeSEric Cheng 	mcbi = &mip->mi_notify_cb_info;
3857da14cebeSEric Cheng 
3858da14cebeSEric Cheng 	i_mac_perim_enter(mip);
3859da14cebeSEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
3860da14cebeSEric Cheng 
3861da14cebeSEric Cheng 	mac_callback_add(&mip->mi_notify_cb_info, &mip->mi_notify_cb_list,
3862da14cebeSEric Cheng 	    &mncb->mncb_link);
3863da14cebeSEric Cheng 
3864da14cebeSEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
3865da14cebeSEric Cheng 	i_mac_perim_exit(mip);
3866da14cebeSEric Cheng 	return ((mac_notify_handle_t)mncb);
3867da14cebeSEric Cheng }
3868da14cebeSEric Cheng 
3869da14cebeSEric Cheng void
mac_notify_remove_wait(mac_handle_t mh)3870da14cebeSEric Cheng mac_notify_remove_wait(mac_handle_t mh)
3871da14cebeSEric Cheng {
3872da14cebeSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
3873da14cebeSEric Cheng 	mac_cb_info_t	*mcbi = &mip->mi_notify_cb_info;
3874da14cebeSEric Cheng 
3875da14cebeSEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
3876da14cebeSEric Cheng 	mac_callback_remove_wait(&mip->mi_notify_cb_info);
3877da14cebeSEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
3878da14cebeSEric Cheng }
3879da14cebeSEric Cheng 
3880da14cebeSEric Cheng /*
3881da14cebeSEric Cheng  * Remove a mac client specified notification callback
3882da14cebeSEric Cheng  */
3883da14cebeSEric Cheng int
mac_notify_remove(mac_notify_handle_t mnh,boolean_t wait)3884da14cebeSEric Cheng mac_notify_remove(mac_notify_handle_t mnh, boolean_t wait)
3885da14cebeSEric Cheng {
3886da14cebeSEric Cheng 	mac_notify_cb_t	*mncb = (mac_notify_cb_t *)mnh;
3887da14cebeSEric Cheng 	mac_impl_t	*mip = mncb->mncb_mip;
3888da14cebeSEric Cheng 	mac_cb_info_t	*mcbi;
3889da14cebeSEric Cheng 	int		err = 0;
3890da14cebeSEric Cheng 
3891da14cebeSEric Cheng 	mcbi = &mip->mi_notify_cb_info;
3892da14cebeSEric Cheng 
3893da14cebeSEric Cheng 	i_mac_perim_enter(mip);
3894da14cebeSEric Cheng 	mutex_enter(mcbi->mcbi_lockp);
3895da14cebeSEric Cheng 
3896da14cebeSEric Cheng 	ASSERT(mncb->mncb_link.mcb_objp == mncb);
3897da14cebeSEric Cheng 	/*
3898da14cebeSEric Cheng 	 * If there aren't any list walkers, the remove would succeed
3899da14cebeSEric Cheng 	 * inline, else we wait for the deferred remove to complete
3900da14cebeSEric Cheng 	 */
3901da14cebeSEric Cheng 	if (mac_callback_remove(&mip->mi_notify_cb_info,
3902da14cebeSEric Cheng 	    &mip->mi_notify_cb_list, &mncb->mncb_link)) {
3903da14cebeSEric Cheng 		kmem_free(mncb, sizeof (mac_notify_cb_t));
3904da14cebeSEric Cheng 	} else {
3905da14cebeSEric Cheng 		err = EBUSY;
3906da14cebeSEric Cheng 	}
3907da14cebeSEric Cheng 
3908da14cebeSEric Cheng 	mutex_exit(mcbi->mcbi_lockp);
3909da14cebeSEric Cheng 	i_mac_perim_exit(mip);
3910da14cebeSEric Cheng 
3911da14cebeSEric Cheng 	/*
3912da14cebeSEric Cheng 	 * If we failed to remove the notification callback and "wait" is set
3913da14cebeSEric Cheng 	 * to be B_TRUE, wait for the callback to finish after we exit the
3914da14cebeSEric Cheng 	 * mac perimeter.
3915da14cebeSEric Cheng 	 */
3916da14cebeSEric Cheng 	if (err != 0 && wait) {
3917da14cebeSEric Cheng 		mac_notify_remove_wait((mac_handle_t)mip);
3918da14cebeSEric Cheng 		return (0);
3919da14cebeSEric Cheng 	}
3920da14cebeSEric Cheng 
3921da14cebeSEric Cheng 	return (err);
3922da14cebeSEric Cheng }
3923da14cebeSEric Cheng 
3924da14cebeSEric Cheng /*
3925da14cebeSEric Cheng  * Associate resource management callbacks with the specified MAC
3926da14cebeSEric Cheng  * clients.
3927da14cebeSEric Cheng  */
3928da14cebeSEric Cheng 
3929da14cebeSEric Cheng void
mac_resource_set_common(mac_client_handle_t mch,mac_resource_add_t add,mac_resource_remove_t remove,mac_resource_quiesce_t quiesce,mac_resource_restart_t restart,mac_resource_bind_t bind,void * arg)3930da14cebeSEric Cheng mac_resource_set_common(mac_client_handle_t mch, mac_resource_add_t add,
3931da14cebeSEric Cheng     mac_resource_remove_t remove, mac_resource_quiesce_t quiesce,
3932da14cebeSEric Cheng     mac_resource_restart_t restart, mac_resource_bind_t bind,
3933da14cebeSEric Cheng     void *arg)
3934da14cebeSEric Cheng {
3935da14cebeSEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
3936da14cebeSEric Cheng 
3937da14cebeSEric Cheng 	mcip->mci_resource_add = add;
3938da14cebeSEric Cheng 	mcip->mci_resource_remove = remove;
3939da14cebeSEric Cheng 	mcip->mci_resource_quiesce = quiesce;
3940da14cebeSEric Cheng 	mcip->mci_resource_restart = restart;
3941da14cebeSEric Cheng 	mcip->mci_resource_bind = bind;
3942da14cebeSEric Cheng 	mcip->mci_resource_arg = arg;
3943da14cebeSEric Cheng }
3944da14cebeSEric Cheng 
3945da14cebeSEric Cheng void
mac_resource_set(mac_client_handle_t mch,mac_resource_add_t add,void * arg)3946da14cebeSEric Cheng mac_resource_set(mac_client_handle_t mch, mac_resource_add_t add, void *arg)
3947da14cebeSEric Cheng {
3948da14cebeSEric Cheng 	/* update the 'resource_add' callback */
3949da14cebeSEric Cheng 	mac_resource_set_common(mch, add, NULL, NULL, NULL, NULL, arg);
3950da14cebeSEric Cheng }
3951da14cebeSEric Cheng 
3952da14cebeSEric Cheng /*
3953da14cebeSEric Cheng  * Sets up the client resources and enable the polling interface over all the
3954da14cebeSEric Cheng  * SRS's and the soft rings of the client
3955da14cebeSEric Cheng  */
3956da14cebeSEric Cheng void
mac_client_poll_enable(mac_client_handle_t mch)3957da14cebeSEric Cheng mac_client_poll_enable(mac_client_handle_t mch)
3958da14cebeSEric Cheng {
3959da14cebeSEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3960da14cebeSEric Cheng 	mac_soft_ring_set_t	*mac_srs;
3961da14cebeSEric Cheng 	flow_entry_t		*flent;
3962da14cebeSEric Cheng 	int			i;
3963da14cebeSEric Cheng 
3964da14cebeSEric Cheng 	flent = mcip->mci_flent;
3965da14cebeSEric Cheng 	ASSERT(flent != NULL);
3966da14cebeSEric Cheng 
39678d4cf8d8S 	mcip->mci_state_flags |= MCIS_CLIENT_POLL_CAPABLE;
3968da14cebeSEric Cheng 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
3969da14cebeSEric Cheng 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
3970da14cebeSEric Cheng 		ASSERT(mac_srs->srs_mcip == mcip);
3971da14cebeSEric Cheng 		mac_srs_client_poll_enable(mcip, mac_srs);
3972da14cebeSEric Cheng 	}
3973da14cebeSEric Cheng }
3974da14cebeSEric Cheng 
3975da14cebeSEric Cheng /*
3976da14cebeSEric Cheng  * Tears down the client resources and disable the polling interface over all
3977da14cebeSEric Cheng  * the SRS's and the soft rings of the client
3978da14cebeSEric Cheng  */
3979da14cebeSEric Cheng void
mac_client_poll_disable(mac_client_handle_t mch)3980da14cebeSEric Cheng mac_client_poll_disable(mac_client_handle_t mch)
3981da14cebeSEric Cheng {
3982da14cebeSEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
3983da14cebeSEric Cheng 	mac_soft_ring_set_t	*mac_srs;
3984da14cebeSEric Cheng 	flow_entry_t		*flent;
3985da14cebeSEric Cheng 	int			i;
3986da14cebeSEric Cheng 
3987da14cebeSEric Cheng 	flent = mcip->mci_flent;
3988da14cebeSEric Cheng 	ASSERT(flent != NULL);
3989da14cebeSEric Cheng 
39908d4cf8d8S 	mcip->mci_state_flags &= ~MCIS_CLIENT_POLL_CAPABLE;
3991da14cebeSEric Cheng 	for (i = 0; i < flent->fe_rx_srs_cnt; i++) {
3992da14cebeSEric Cheng 		mac_srs = (mac_soft_ring_set_t *)flent->fe_rx_srs[i];
3993da14cebeSEric Cheng 		ASSERT(mac_srs->srs_mcip == mcip);
3994da14cebeSEric Cheng 		mac_srs_client_poll_disable(mcip, mac_srs);
3995da14cebeSEric Cheng 	}
3996da14cebeSEric Cheng }
3997da14cebeSEric Cheng 
3998da14cebeSEric Cheng /*
3999da14cebeSEric Cheng  * Associate the CPUs specified by the given property with a MAC client.
4000da14cebeSEric Cheng  */
4001da14cebeSEric Cheng int
mac_cpu_set(mac_client_handle_t mch,mac_resource_props_t * mrp)4002da14cebeSEric Cheng mac_cpu_set(mac_client_handle_t mch, mac_resource_props_t *mrp)
4003da14cebeSEric Cheng {
4004da14cebeSEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4005da14cebeSEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
4006da14cebeSEric Cheng 	int err = 0;
4007da14cebeSEric Cheng 
4008da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4009da14cebeSEric Cheng 
40100dc2366fSVenugopal Iyer 	if ((err = mac_validate_props(mcip->mci_state_flags & MCIS_IS_VNIC ?
40110dc2366fSVenugopal Iyer 	    mcip->mci_upper_mip : mip, mrp)) != 0) {
4012da14cebeSEric Cheng 		return (err);
40130dc2366fSVenugopal Iyer 	}
4014da14cebeSEric Cheng 	if (MCIP_DATAPATH_SETUP(mcip))
4015da14cebeSEric Cheng 		mac_flow_modify(mip->mi_flow_tab, mcip->mci_flent, mrp);
4016da14cebeSEric Cheng 
4017da14cebeSEric Cheng 	mac_update_resources(mrp, MCIP_RESOURCE_PROPS(mcip), B_FALSE);
4018da14cebeSEric Cheng 	return (0);
4019da14cebeSEric Cheng }
4020da14cebeSEric Cheng 
4021da14cebeSEric Cheng /*
4022da14cebeSEric Cheng  * Apply the specified properties to the specified MAC client.
4023da14cebeSEric Cheng  */
4024da14cebeSEric Cheng int
mac_client_set_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)4025da14cebeSEric Cheng mac_client_set_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
4026da14cebeSEric Cheng {
4027da14cebeSEric Cheng 	mac_client_impl_t *mcip = (mac_client_impl_t *)mch;
4028da14cebeSEric Cheng 	mac_impl_t *mip = mcip->mci_mip;
4029da14cebeSEric Cheng 	int err = 0;
4030da14cebeSEric Cheng 
4031da14cebeSEric Cheng 	i_mac_perim_enter(mip);
4032da14cebeSEric Cheng 
4033da14cebeSEric Cheng 	if ((mrp->mrp_mask & MRP_MAXBW) || (mrp->mrp_mask & MRP_PRIORITY)) {
4034da14cebeSEric Cheng 		err = mac_resource_ctl_set(mch, mrp);
403525ec3e3dSEric Cheng 		if (err != 0)
403625ec3e3dSEric Cheng 			goto done;
4037da14cebeSEric Cheng 	}
4038da14cebeSEric Cheng 
40390dc2366fSVenugopal Iyer 	if (mrp->mrp_mask & (MRP_CPUS|MRP_POOL)) {
4040da14cebeSEric Cheng 		err = mac_cpu_set(mch, mrp);
404125ec3e3dSEric Cheng 		if (err != 0)
404225ec3e3dSEric Cheng 			goto done;
404325ec3e3dSEric Cheng 	}
4044da14cebeSEric Cheng 
40450dc2366fSVenugopal Iyer 	if (mrp->mrp_mask & MRP_PROTECT) {
404625ec3e3dSEric Cheng 		err = mac_protect_set(mch, mrp);
40470dc2366fSVenugopal Iyer 		if (err != 0)
40480dc2366fSVenugopal Iyer 			goto done;
40490dc2366fSVenugopal Iyer 	}
40500dc2366fSVenugopal Iyer 
40510dc2366fSVenugopal Iyer 	if ((mrp->mrp_mask & MRP_RX_RINGS) || (mrp->mrp_mask & MRP_TX_RINGS))
40520dc2366fSVenugopal Iyer 		err = mac_resource_ctl_set(mch, mrp);
405325ec3e3dSEric Cheng 
405425ec3e3dSEric Cheng done:
4055da14cebeSEric Cheng 	i_mac_perim_exit(mip);
4056da14cebeSEric Cheng 	return (err);
4057da14cebeSEric Cheng }
4058da14cebeSEric Cheng 
4059da14cebeSEric Cheng /*
4060da14cebeSEric Cheng  * Return the properties currently associated with the specified MAC client.
4061da14cebeSEric Cheng  */
4062da14cebeSEric Cheng void
mac_client_get_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)4063da14cebeSEric Cheng mac_client_get_resources(mac_client_handle_t mch, mac_resource_props_t *mrp)
4064da14cebeSEric Cheng {
4065da14cebeSEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
4066da14cebeSEric Cheng 	mac_resource_props_t	*mcip_mrp = MCIP_RESOURCE_PROPS(mcip);
4067da14cebeSEric Cheng 
4068da14cebeSEric Cheng 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
4069da14cebeSEric Cheng }
4070da14cebeSEric Cheng 
4071da14cebeSEric Cheng /*
40720dc2366fSVenugopal Iyer  * Return the effective properties currently associated with the specified
40730dc2366fSVenugopal Iyer  * MAC client.
40740dc2366fSVenugopal Iyer  */
40750dc2366fSVenugopal Iyer void
mac_client_get_effective_resources(mac_client_handle_t mch,mac_resource_props_t * mrp)40760dc2366fSVenugopal Iyer mac_client_get_effective_resources(mac_client_handle_t mch,
40770dc2366fSVenugopal Iyer     mac_resource_props_t *mrp)
40780dc2366fSVenugopal Iyer {
40790dc2366fSVenugopal Iyer 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
40800dc2366fSVenugopal Iyer 	mac_resource_props_t	*mcip_mrp = MCIP_EFFECTIVE_PROPS(mcip);
40810dc2366fSVenugopal Iyer 
40820dc2366fSVenugopal Iyer 	bcopy(mcip_mrp, mrp, sizeof (mac_resource_props_t));
40830dc2366fSVenugopal Iyer }
40840dc2366fSVenugopal Iyer 
40850dc2366fSVenugopal Iyer /*
4086da14cebeSEric Cheng  * Pass a copy of the specified packet to the promiscuous callbacks
4087da14cebeSEric Cheng  * of the specified MAC.
4088da14cebeSEric Cheng  *
4089da14cebeSEric Cheng  * If sender is NULL, the function is being invoked for a packet chain
4090da14cebeSEric Cheng  * received from the wire. If sender is non-NULL, it points to
4091da14cebeSEric Cheng  * the MAC client from which the packet is being sent.
4092da14cebeSEric Cheng  *
4093da14cebeSEric Cheng  * The packets are distributed to the promiscuous callbacks as follows:
4094da14cebeSEric Cheng  *
4095da14cebeSEric Cheng  * - all packets are sent to the MAC_CLIENT_PROMISC_ALL callbacks
4096da14cebeSEric Cheng  * - all broadcast and multicast packets are sent to the
4097da14cebeSEric Cheng  *   MAC_CLIENT_PROMISC_FILTER and MAC_CLIENT_PROMISC_MULTI.
4098da14cebeSEric Cheng  *
4099da14cebeSEric Cheng  * The unicast packets of MAC_CLIENT_PROMISC_FILTER callbacks are dispatched
4100da14cebeSEric Cheng  * after classification by mac_rx_deliver().
4101da14cebeSEric Cheng  */
4102da14cebeSEric Cheng static void
mac_promisc_dispatch_one(mac_promisc_impl_t * mpip,mblk_t * mp,boolean_t loopback,boolean_t local)4103da14cebeSEric Cheng mac_promisc_dispatch_one(mac_promisc_impl_t *mpip, mblk_t *mp,
4104c61a1653SRyan Zezeski     boolean_t loopback, boolean_t local)
4105da14cebeSEric Cheng {
4106c61a1653SRyan Zezeski 	mblk_t *mp_next;
4107da14cebeSEric Cheng 
41080a0e9771SDarren Reed 	if (!mpip->mpi_no_copy || mpip->mpi_strip_vlan_tag) {
4109c61a1653SRyan Zezeski 		mblk_t *mp_copy;
4110c61a1653SRyan Zezeski 
4111da14cebeSEric Cheng 		mp_copy = copymsg(mp);
4112da14cebeSEric Cheng 		if (mp_copy == NULL)
4113da14cebeSEric Cheng 			return;
4114da14cebeSEric Cheng 
4115ae6aa22aSVenugopal Iyer 		if (mpip->mpi_strip_vlan_tag) {
41160a0e9771SDarren Reed 			mp_copy = mac_strip_vlan_tag_chain(mp_copy);
41170a0e9771SDarren Reed 			if (mp_copy == NULL)
4118ae6aa22aSVenugopal Iyer 				return;
4119ae6aa22aSVenugopal Iyer 		}
41200a0e9771SDarren Reed 
4121c61a1653SRyan Zezeski 		/*
4122c61a1653SRyan Zezeski 		 * There is code upstack that can't deal with message
4123c61a1653SRyan Zezeski 		 * chains.
4124c61a1653SRyan Zezeski 		 */
4125c61a1653SRyan Zezeski 		for (mblk_t *tmp = mp_copy; tmp != NULL; tmp = mp_next) {
4126c61a1653SRyan Zezeski 			mp_next = tmp->b_next;
4127c61a1653SRyan Zezeski 			tmp->b_next = NULL;
4128c61a1653SRyan Zezeski 			mpip->mpi_fn(mpip->mpi_arg, NULL, tmp, loopback);
4129c61a1653SRyan Zezeski 		}
4130c61a1653SRyan Zezeski 
4131c61a1653SRyan Zezeski 		return;
4132c61a1653SRyan Zezeski 	}
4133c61a1653SRyan Zezeski 
4134c61a1653SRyan Zezeski 	mp_next = mp->b_next;
4135c61a1653SRyan Zezeski 	mp->b_next = NULL;
4136c61a1653SRyan Zezeski 	mpip->mpi_fn(mpip->mpi_arg, NULL, mp, loopback);
41370a0e9771SDarren Reed 	mp->b_next = mp_next;
4138da14cebeSEric Cheng }
4139da14cebeSEric Cheng 
4140da14cebeSEric Cheng /*
4141da14cebeSEric Cheng  * Return the VID of a packet. Zero if the packet is not tagged.
4142da14cebeSEric Cheng  */
4143da14cebeSEric Cheng static uint16_t
mac_ether_vid(mblk_t * mp)4144da14cebeSEric Cheng mac_ether_vid(mblk_t *mp)
4145da14cebeSEric Cheng {
4146da14cebeSEric Cheng 	struct ether_header *eth = (struct ether_header *)mp->b_rptr;
4147da14cebeSEric Cheng 
4148da14cebeSEric Cheng 	if (ntohs(eth->ether_type) == ETHERTYPE_VLAN) {
4149da14cebeSEric Cheng 		struct ether_vlan_header *t_evhp =
4150da14cebeSEric Cheng 		    (struct ether_vlan_header *)mp->b_rptr;
4151da14cebeSEric Cheng 		return (VLAN_ID(ntohs(t_evhp->ether_tci)));
4152da14cebeSEric Cheng 	}
4153da14cebeSEric Cheng 
4154da14cebeSEric Cheng 	return (0);
4155da14cebeSEric Cheng }
4156da14cebeSEric Cheng 
4157da14cebeSEric Cheng /*
4158da14cebeSEric Cheng  * Return whether the specified packet contains a multicast or broadcast
4159da14cebeSEric Cheng  * destination MAC address.
4160da14cebeSEric Cheng  */
4161da14cebeSEric Cheng static boolean_t
mac_is_mcast(mac_impl_t * mip,mblk_t * mp)4162da14cebeSEric Cheng mac_is_mcast(mac_impl_t *mip, mblk_t *mp)
4163da14cebeSEric Cheng {
4164da14cebeSEric Cheng 	mac_header_info_t hdr_info;
4165da14cebeSEric Cheng 
4166da14cebeSEric Cheng 	if (mac_header_info((mac_handle_t)mip, mp, &hdr_info) != 0)
4167da14cebeSEric Cheng 		return (B_FALSE);
4168da14cebeSEric Cheng 	return ((hdr_info.mhi_dsttype == MAC_ADDRTYPE_BROADCAST) ||
4169da14cebeSEric Cheng 	    (hdr_info.mhi_dsttype == MAC_ADDRTYPE_MULTICAST));
4170da14cebeSEric Cheng }
4171da14cebeSEric Cheng 
4172da14cebeSEric Cheng /*
4173da14cebeSEric Cheng  * Send a copy of an mblk chain to the MAC clients of the specified MAC.
4174da14cebeSEric Cheng  * "sender" points to the sender MAC client for outbound packets, and
4175da14cebeSEric Cheng  * is set to NULL for inbound packets.
4176da14cebeSEric Cheng  */
4177da14cebeSEric Cheng void
mac_promisc_dispatch(mac_impl_t * mip,mblk_t * mp_chain,mac_client_impl_t * sender,boolean_t local)4178da14cebeSEric Cheng mac_promisc_dispatch(mac_impl_t *mip, mblk_t *mp_chain,
4179c61a1653SRyan Zezeski     mac_client_impl_t *sender, boolean_t local)
4180da14cebeSEric Cheng {
4181da14cebeSEric Cheng 	mac_promisc_impl_t *mpip;
4182da14cebeSEric Cheng 	mac_cb_t *mcb;
4183da14cebeSEric Cheng 	mblk_t *mp;
4184da14cebeSEric Cheng 	boolean_t is_mcast, is_sender;
4185da14cebeSEric Cheng 
4186da14cebeSEric Cheng 	MAC_PROMISC_WALKER_INC(mip);
4187da14cebeSEric Cheng 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
4188da14cebeSEric Cheng 		is_mcast = mac_is_mcast(mip, mp);
4189da14cebeSEric Cheng 		/* send packet to interested callbacks */
4190da14cebeSEric Cheng 		for (mcb = mip->mi_promisc_list; mcb != NULL;
4191da14cebeSEric Cheng 		    mcb = mcb->mcb_nextp) {
4192da14cebeSEric Cheng 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
4193da14cebeSEric Cheng 			is_sender = (mpip->mpi_mcip == sender);
4194da14cebeSEric Cheng 
4195da14cebeSEric Cheng 			if (is_sender && mpip->mpi_no_tx_loop)
4196da14cebeSEric Cheng 				/*
4197da14cebeSEric Cheng 				 * The sender doesn't want to receive
4198da14cebeSEric Cheng 				 * copies of the packets it sends.
4199da14cebeSEric Cheng 				 */
4200da14cebeSEric Cheng 				continue;
4201da14cebeSEric Cheng 
42024eaa4710SRishi Srivatsavai 			/* this client doesn't need any packets (bridge) */
42034eaa4710SRishi Srivatsavai 			if (mpip->mpi_fn == NULL)
42044eaa4710SRishi Srivatsavai 				continue;
42054eaa4710SRishi Srivatsavai 
4206da14cebeSEric Cheng 			/*
4207da14cebeSEric Cheng 			 * For an ethernet MAC, don't displatch a multicast
4208da14cebeSEric Cheng 			 * packet to a non-PROMISC_ALL callbacks unless the VID
4209da14cebeSEric Cheng 			 * of the packet matches the VID of the client.
4210da14cebeSEric Cheng 			 */
4211da14cebeSEric Cheng 			if (is_mcast &&
4212da14cebeSEric Cheng 			    mpip->mpi_type != MAC_CLIENT_PROMISC_ALL &&
4213da14cebeSEric Cheng 			    !mac_client_check_flow_vid(mpip->mpi_mcip,
4214da14cebeSEric Cheng 			    mac_ether_vid(mp)))
4215da14cebeSEric Cheng 				continue;
4216da14cebeSEric Cheng 
4217da14cebeSEric Cheng 			if (is_sender ||
4218da14cebeSEric Cheng 			    mpip->mpi_type == MAC_CLIENT_PROMISC_ALL ||
4219c61a1653SRyan Zezeski 			    is_mcast) {
4220c61a1653SRyan Zezeski 				mac_promisc_dispatch_one(mpip, mp, is_sender,
4221c61a1653SRyan Zezeski 				    local);
4222c61a1653SRyan Zezeski 			}
4223da14cebeSEric Cheng 		}
4224da14cebeSEric Cheng 	}
4225da14cebeSEric Cheng 	MAC_PROMISC_WALKER_DCR(mip);
4226da14cebeSEric Cheng }
4227da14cebeSEric Cheng 
4228da14cebeSEric Cheng void
mac_promisc_client_dispatch(mac_client_impl_t * mcip,mblk_t * mp_chain)4229da14cebeSEric Cheng mac_promisc_client_dispatch(mac_client_impl_t *mcip, mblk_t *mp_chain)
4230da14cebeSEric Cheng {
4231da14cebeSEric Cheng 	mac_impl_t		*mip = mcip->mci_mip;
4232da14cebeSEric Cheng 	mac_promisc_impl_t	*mpip;
4233da14cebeSEric Cheng 	boolean_t		is_mcast;
4234da14cebeSEric Cheng 	mblk_t			*mp;
4235da14cebeSEric Cheng 	mac_cb_t		*mcb;
4236da14cebeSEric Cheng 
4237da14cebeSEric Cheng 	/*
4238da14cebeSEric Cheng 	 * The unicast packets for the MAC client still
4239da14cebeSEric Cheng 	 * need to be delivered to the MAC_CLIENT_PROMISC_FILTERED
4240da14cebeSEric Cheng 	 * promiscuous callbacks. The broadcast and multicast
4241da14cebeSEric Cheng 	 * packets were delivered from mac_rx().
4242da14cebeSEric Cheng 	 */
4243da14cebeSEric Cheng 	MAC_PROMISC_WALKER_INC(mip);
4244da14cebeSEric Cheng 	for (mp = mp_chain; mp != NULL; mp = mp->b_next) {
4245da14cebeSEric Cheng 		is_mcast = mac_is_mcast(mip, mp);
4246da14cebeSEric Cheng 		for (mcb = mcip->mci_promisc_list; mcb != NULL;
4247da14cebeSEric Cheng 		    mcb = mcb->mcb_nextp) {
4248da14cebeSEric Cheng 			mpip = (mac_promisc_impl_t *)mcb->mcb_objp;
4249da14cebeSEric Cheng 			if (mpip->mpi_type == MAC_CLIENT_PROMISC_FILTERED &&
4250da14cebeSEric Cheng 			    !is_mcast) {
4251c61a1653SRyan Zezeski 				mac_promisc_dispatch_one(mpip, mp, B_FALSE,
4252c61a1653SRyan Zezeski 				    B_FALSE);
4253da14cebeSEric Cheng 			}
4254da14cebeSEric Cheng 		}
4255da14cebeSEric Cheng 	}
4256da14cebeSEric Cheng 	MAC_PROMISC_WALKER_DCR(mip);
4257da14cebeSEric Cheng }
4258da14cebeSEric Cheng 
4259da14cebeSEric Cheng /*
4260da14cebeSEric Cheng  * Return the margin value currently assigned to the specified MAC instance.
4261da14cebeSEric Cheng  */
4262da14cebeSEric Cheng void
mac_margin_get(mac_handle_t mh,uint32_t * marginp)4263da14cebeSEric Cheng mac_margin_get(mac_handle_t mh, uint32_t *marginp)
4264da14cebeSEric Cheng {
4265da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
4266da14cebeSEric Cheng 
4267da14cebeSEric Cheng 	rw_enter(&(mip->mi_rw_lock), RW_READER);
4268da14cebeSEric Cheng 	*marginp = mip->mi_margin;
4269da14cebeSEric Cheng 	rw_exit(&(mip->mi_rw_lock));
4270da14cebeSEric Cheng }
4271da14cebeSEric Cheng 
4272da14cebeSEric Cheng /*
4273da14cebeSEric Cheng  * mac_info_get() is used for retrieving the mac_info when a DL_INFO_REQ is
4274da14cebeSEric Cheng  * issued before a DL_ATTACH_REQ. we walk the i_mac_impl_hash table and find
4275da14cebeSEric Cheng  * the first mac_impl_t with a matching driver name; then we copy its mac_info_t
4276da14cebeSEric Cheng  * to the caller. we do all this with i_mac_impl_lock held so the mac_impl_t
4277da14cebeSEric Cheng  * cannot disappear while we are accessing it.
4278da14cebeSEric Cheng  */
4279da14cebeSEric Cheng typedef struct i_mac_info_state_s {
4280da14cebeSEric Cheng 	const char	*mi_name;
4281da14cebeSEric Cheng 	mac_info_t	*mi_infop;
4282da14cebeSEric Cheng } i_mac_info_state_t;
4283da14cebeSEric Cheng 
4284da14cebeSEric Cheng /*ARGSUSED*/
4285da14cebeSEric Cheng static uint_t
i_mac_info_walker(mod_hash_key_t key,mod_hash_val_t * val,void * arg)4286da14cebeSEric Cheng i_mac_info_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
4287da14cebeSEric Cheng {
4288da14cebeSEric Cheng 	i_mac_info_state_t *statep = arg;
4289da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)val;
4290da14cebeSEric Cheng 
4291da14cebeSEric Cheng 	if (mip->mi_state_flags & MIS_DISABLED)
4292da14cebeSEric Cheng 		return (MH_WALK_CONTINUE);
4293da14cebeSEric Cheng 
4294da14cebeSEric Cheng 	if (strcmp(statep->mi_name,
4295da14cebeSEric Cheng 	    ddi_driver_name(mip->mi_dip)) != 0)
4296da14cebeSEric Cheng 		return (MH_WALK_CONTINUE);
4297da14cebeSEric Cheng 
4298da14cebeSEric Cheng 	statep->mi_infop = &mip->mi_info;
4299da14cebeSEric Cheng 	return (MH_WALK_TERMINATE);
4300da14cebeSEric Cheng }
4301da14cebeSEric Cheng 
4302da14cebeSEric Cheng boolean_t
mac_info_get(const char * name,mac_info_t * minfop)4303da14cebeSEric Cheng mac_info_get(const char *name, mac_info_t *minfop)
4304da14cebeSEric Cheng {
4305da14cebeSEric Cheng 	i_mac_info_state_t state;
4306da14cebeSEric Cheng 
4307da14cebeSEric Cheng 	rw_enter(&i_mac_impl_lock, RW_READER);
4308da14cebeSEric Cheng 	state.mi_name = name;
4309da14cebeSEric Cheng 	state.mi_infop = NULL;
4310da14cebeSEric Cheng 	mod_hash_walk(i_mac_impl_hash, i_mac_info_walker, &state);
4311da14cebeSEric Cheng 	if (state.mi_infop == NULL) {
4312da14cebeSEric Cheng 		rw_exit(&i_mac_impl_lock);
4313da14cebeSEric Cheng 		return (B_FALSE);
4314da14cebeSEric Cheng 	}
4315da14cebeSEric Cheng 	*minfop = *state.mi_infop;
4316da14cebeSEric Cheng 	rw_exit(&i_mac_impl_lock);
4317da14cebeSEric Cheng 	return (B_TRUE);
4318da14cebeSEric Cheng }
4319da14cebeSEric Cheng 
4320da14cebeSEric Cheng /*
4321da14cebeSEric Cheng  * To get the capabilities that MAC layer cares about, such as rings, factory
43224eaa4710SRishi Srivatsavai  * mac address, vnic or not, it should directly invoke this function.  If the
43234eaa4710SRishi Srivatsavai  * link is part of a bridge, then the only "capability" it has is the inability
43244eaa4710SRishi Srivatsavai  * to do zero copy.
4325da14cebeSEric Cheng  */
4326da14cebeSEric Cheng boolean_t
i_mac_capab_get(mac_handle_t mh,mac_capab_t cap,void * cap_data)4327da14cebeSEric Cheng i_mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
4328da14cebeSEric Cheng {
4329da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
4330da14cebeSEric Cheng 
433197f11506SPatrick Mooney 	if (mip->mi_bridge_link != NULL) {
43324eaa4710SRishi Srivatsavai 		return (cap == MAC_CAPAB_NO_ZCOPY);
433397f11506SPatrick Mooney 	} else if (mip->mi_callbacks->mc_callbacks & MC_GETCAPAB) {
433497f11506SPatrick Mooney 		boolean_t res;
433597f11506SPatrick Mooney 
433697f11506SPatrick Mooney 		res = mip->mi_getcapab(mip->mi_driver, cap, cap_data);
433797f11506SPatrick Mooney 		/*
433897f11506SPatrick Mooney 		 * Until we have suppport for TSOv6 emulation in the MAC
433997f11506SPatrick Mooney 		 * loopback path, do not allow the TSOv6 capability to be
434097f11506SPatrick Mooney 		 * advertised to consumers.
434197f11506SPatrick Mooney 		 */
434297f11506SPatrick Mooney 		if (res && cap == MAC_CAPAB_LSO) {
434397f11506SPatrick Mooney 			mac_capab_lso_t *cap_lso = cap_data;
434497f11506SPatrick Mooney 
434597f11506SPatrick Mooney 			cap_lso->lso_flags &= ~LSO_TX_BASIC_TCP_IPV6;
434697f11506SPatrick Mooney 			cap_lso->lso_basic_tcp_ipv6.lso_max = 0;
434797f11506SPatrick Mooney 		}
434897f11506SPatrick Mooney 		return (res);
434997f11506SPatrick Mooney 	} else {
4350da14cebeSEric Cheng 		return (B_FALSE);
4351da14cebeSEric Cheng 	}
435297f11506SPatrick Mooney }
4353da14cebeSEric Cheng 
4354da14cebeSEric Cheng /*
4355da14cebeSEric Cheng  * Capability query function. If number of active mac clients is greater than
4356da14cebeSEric Cheng  * 1, only limited capabilities can be advertised to the caller no matter the
4357da14cebeSEric Cheng  * driver has certain capability or not. Else, we query the driver to get the
4358da14cebeSEric Cheng  * capability.
4359da14cebeSEric Cheng  */
4360da14cebeSEric Cheng boolean_t
mac_capab_get(mac_handle_t mh,mac_capab_t cap,void * cap_data)4361da14cebeSEric Cheng mac_capab_get(mac_handle_t mh, mac_capab_t cap, void *cap_data)
4362da14cebeSEric Cheng {
4363da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
4364da14cebeSEric Cheng 
4365da14cebeSEric Cheng 	/*
4366c61a1653SRyan Zezeski 	 * Some capabilities are restricted when there are more than one active
4367c61a1653SRyan Zezeski 	 * clients on the MAC resource.  The ones noted below are safe,
4368c61a1653SRyan Zezeski 	 * independent of that count.
4369da14cebeSEric Cheng 	 */
4370da14cebeSEric Cheng 	if (mip->mi_nactiveclients > 1) {
4371da14cebeSEric Cheng 		switch (cap) {
4372da14cebeSEric Cheng 		case MAC_CAPAB_NO_ZCOPY:
4373da14cebeSEric Cheng 			return (B_TRUE);
43745d460eafSCathy Zhou 		case MAC_CAPAB_LEGACY:
43755d460eafSCathy Zhou 		case MAC_CAPAB_HCKSUM:
4376c61a1653SRyan Zezeski 		case MAC_CAPAB_LSO:
43779056fcebSCathy Zhou 		case MAC_CAPAB_NO_NATIVEVLAN:
43785d460eafSCathy Zhou 			break;
4379da14cebeSEric Cheng 		default:
4380da14cebeSEric Cheng 			return (B_FALSE);
4381da14cebeSEric Cheng 		}
4382da14cebeSEric Cheng 	}
4383da14cebeSEric Cheng 
4384da14cebeSEric Cheng 	/* else get capab from driver */
4385da14cebeSEric Cheng 	return (i_mac_capab_get(mh, cap, cap_data));
4386da14cebeSEric Cheng }
4387da14cebeSEric Cheng 
4388da14cebeSEric Cheng boolean_t
mac_sap_verify(mac_handle_t mh,uint32_t sap,uint32_t * bind_sap)4389da14cebeSEric Cheng mac_sap_verify(mac_handle_t mh, uint32_t sap, uint32_t *bind_sap)
4390da14cebeSEric Cheng {
4391da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
4392da14cebeSEric Cheng 
4393da14cebeSEric Cheng 	return (mip->mi_type->mt_ops.mtops_sap_verify(sap, bind_sap,
4394da14cebeSEric Cheng 	    mip->mi_pdata));
4395da14cebeSEric Cheng }
4396da14cebeSEric Cheng 
4397da14cebeSEric Cheng mblk_t *
mac_header(mac_handle_t mh,const uint8_t * daddr,uint32_t sap,mblk_t * payload,size_t extra_len)4398da14cebeSEric Cheng mac_header(mac_handle_t mh, const uint8_t *daddr, uint32_t sap, mblk_t *payload,
4399da14cebeSEric Cheng     size_t extra_len)
4400da14cebeSEric Cheng {
4401da14cebeSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
44022b24ab6bSSebastien Roy 	const uint8_t	*hdr_daddr;
4403da14cebeSEric Cheng 
44042b24ab6bSSebastien Roy 	/*
44052b24ab6bSSebastien Roy 	 * If the MAC is point-to-point with a fixed destination address, then
44062b24ab6bSSebastien Roy 	 * we must always use that destination in the MAC header.
44072b24ab6bSSebastien Roy 	 */
44082b24ab6bSSebastien Roy 	hdr_daddr = (mip->mi_dstaddr_set ? mip->mi_dstaddr : daddr);
44092b24ab6bSSebastien Roy 	return (mip->mi_type->mt_ops.mtops_header(mip->mi_addr, hdr_daddr, sap,
4410da14cebeSEric Cheng 	    mip->mi_pdata, payload, extra_len));
4411da14cebeSEric Cheng }
4412da14cebeSEric Cheng 
4413da14cebeSEric Cheng int
mac_header_info(mac_handle_t mh,mblk_t * mp,mac_header_info_t * mhip)4414da14cebeSEric Cheng mac_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
4415da14cebeSEric Cheng {
4416da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
4417da14cebeSEric Cheng 
4418da14cebeSEric Cheng 	return (mip->mi_type->mt_ops.mtops_header_info(mp, mip->mi_pdata,
4419da14cebeSEric Cheng 	    mhip));
4420da14cebeSEric Cheng }
4421da14cebeSEric Cheng 
442225ec3e3dSEric Cheng int
mac_vlan_header_info(mac_handle_t mh,mblk_t * mp,mac_header_info_t * mhip)442325ec3e3dSEric Cheng mac_vlan_header_info(mac_handle_t mh, mblk_t *mp, mac_header_info_t *mhip)
442425ec3e3dSEric Cheng {
442525ec3e3dSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
442625ec3e3dSEric Cheng 	boolean_t	is_ethernet = (mip->mi_info.mi_media == DL_ETHER);
442725ec3e3dSEric Cheng 	int		err = 0;
442825ec3e3dSEric Cheng 
442925ec3e3dSEric Cheng 	/*
443025ec3e3dSEric Cheng 	 * Packets should always be at least 16 bit aligned.
443125ec3e3dSEric Cheng 	 */
443225ec3e3dSEric Cheng 	ASSERT(IS_P2ALIGNED(mp->b_rptr, sizeof (uint16_t)));
443325ec3e3dSEric Cheng 
443425ec3e3dSEric Cheng 	if ((err = mac_header_info(mh, mp, mhip)) != 0)
443525ec3e3dSEric Cheng 		return (err);
443625ec3e3dSEric Cheng 
443725ec3e3dSEric Cheng 	/*
443825ec3e3dSEric Cheng 	 * If this is a VLAN-tagged Ethernet packet, then the SAP in the
443925ec3e3dSEric Cheng 	 * mac_header_info_t as returned by mac_header_info() is
444025ec3e3dSEric Cheng 	 * ETHERTYPE_VLAN. We need to grab the ethertype from the VLAN header.
444125ec3e3dSEric Cheng 	 */
444225ec3e3dSEric Cheng 	if (is_ethernet && (mhip->mhi_bindsap == ETHERTYPE_VLAN)) {
444325ec3e3dSEric Cheng 		struct ether_vlan_header *evhp;
444425ec3e3dSEric Cheng 		uint16_t sap;
444525ec3e3dSEric Cheng 		mblk_t *tmp = NULL;
444625ec3e3dSEric Cheng 		size_t size;
444725ec3e3dSEric Cheng 
444825ec3e3dSEric Cheng 		size = sizeof (struct ether_vlan_header);
444925ec3e3dSEric Cheng 		if (MBLKL(mp) < size) {
445025ec3e3dSEric Cheng 			/*
445125ec3e3dSEric Cheng 			 * Pullup the message in order to get the MAC header
445225ec3e3dSEric Cheng 			 * infomation. Note that this is a read-only function,
445325ec3e3dSEric Cheng 			 * we keep the input packet intact.
445425ec3e3dSEric Cheng 			 */
445525ec3e3dSEric Cheng 			if ((tmp = msgpullup(mp, size)) == NULL)
445625ec3e3dSEric Cheng 				return (EINVAL);
445725ec3e3dSEric Cheng 
445825ec3e3dSEric Cheng 			mp = tmp;
445925ec3e3dSEric Cheng 		}
446025ec3e3dSEric Cheng 		evhp = (struct ether_vlan_header *)mp->b_rptr;
446125ec3e3dSEric Cheng 		sap = ntohs(evhp->ether_type);
446225ec3e3dSEric Cheng 		(void) mac_sap_verify(mh, sap, &mhip->mhi_bindsap);
446325ec3e3dSEric Cheng 		mhip->mhi_hdrsize = sizeof (struct ether_vlan_header);
446425ec3e3dSEric Cheng 		mhip->mhi_tci = ntohs(evhp->ether_tci);
446525ec3e3dSEric Cheng 		mhip->mhi_istagged = B_TRUE;
446625ec3e3dSEric Cheng 		freemsg(tmp);
446725ec3e3dSEric Cheng 
446825ec3e3dSEric Cheng 		if (VLAN_CFI(mhip->mhi_tci) != ETHER_CFI)
446925ec3e3dSEric Cheng 			return (EINVAL);
447025ec3e3dSEric Cheng 	} else {
447125ec3e3dSEric Cheng 		mhip->mhi_istagged = B_FALSE;
447225ec3e3dSEric Cheng 		mhip->mhi_tci = 0;
447325ec3e3dSEric Cheng 	}
447425ec3e3dSEric Cheng 
447525ec3e3dSEric Cheng 	return (0);
447625ec3e3dSEric Cheng }
447725ec3e3dSEric Cheng 
4478da14cebeSEric Cheng mblk_t *
mac_header_cook(mac_handle_t mh,mblk_t * mp)4479da14cebeSEric Cheng mac_header_cook(mac_handle_t mh, mblk_t *mp)
4480da14cebeSEric Cheng {
4481da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
4482da14cebeSEric Cheng 
4483da14cebeSEric Cheng 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_COOK) {
4484da14cebeSEric Cheng 		if (DB_REF(mp) > 1) {
4485da14cebeSEric Cheng 			mblk_t *newmp = copymsg(mp);
4486da14cebeSEric Cheng 			if (newmp == NULL)
4487da14cebeSEric Cheng 				return (NULL);
4488da14cebeSEric Cheng 			freemsg(mp);
4489da14cebeSEric Cheng 			mp = newmp;
4490da14cebeSEric Cheng 		}
4491da14cebeSEric Cheng 		return (mip->mi_type->mt_ops.mtops_header_cook(mp,
4492da14cebeSEric Cheng 		    mip->mi_pdata));
4493da14cebeSEric Cheng 	}
4494da14cebeSEric Cheng 	return (mp);
4495da14cebeSEric Cheng }
4496da14cebeSEric Cheng 
4497da14cebeSEric Cheng mblk_t *
mac_header_uncook(mac_handle_t mh,mblk_t * mp)4498da14cebeSEric Cheng mac_header_uncook(mac_handle_t mh, mblk_t *mp)
4499da14cebeSEric Cheng {
4500da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
4501da14cebeSEric Cheng 
4502da14cebeSEric Cheng 	if (mip->mi_type->mt_ops.mtops_ops & MTOPS_HEADER_UNCOOK) {
4503da14cebeSEric Cheng 		if (DB_REF(mp) > 1) {
4504da14cebeSEric Cheng 			mblk_t *newmp = copymsg(mp);
4505da14cebeSEric Cheng 			if (newmp == NULL)
4506da14cebeSEric Cheng 				return (NULL);
4507da14cebeSEric Cheng 			freemsg(mp);
4508da14cebeSEric Cheng 			mp = newmp;
4509da14cebeSEric Cheng 		}
4510da14cebeSEric Cheng 		return (mip->mi_type->mt_ops.mtops_header_uncook(mp,
4511da14cebeSEric Cheng 		    mip->mi_pdata));
4512da14cebeSEric Cheng 	}
4513da14cebeSEric Cheng 	return (mp);
4514da14cebeSEric Cheng }
4515da14cebeSEric Cheng 
4516da14cebeSEric Cheng uint_t
mac_addr_len(mac_handle_t mh)4517da14cebeSEric Cheng mac_addr_len(mac_handle_t mh)
4518da14cebeSEric Cheng {
4519da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
4520da14cebeSEric Cheng 
4521da14cebeSEric Cheng 	return (mip->mi_type->mt_addr_length);
4522da14cebeSEric Cheng }
4523da14cebeSEric Cheng 
4524da14cebeSEric Cheng /* True if a MAC is a VNIC */
4525da14cebeSEric Cheng boolean_t
mac_is_vnic(mac_handle_t mh)4526da14cebeSEric Cheng mac_is_vnic(mac_handle_t mh)
4527da14cebeSEric Cheng {
452836589d6bSRobert Mustacchi 	return ((((mac_impl_t *)mh)->mi_state_flags & MIS_IS_VNIC) != 0);
452936589d6bSRobert Mustacchi }
453036589d6bSRobert Mustacchi 
453136589d6bSRobert Mustacchi boolean_t
mac_is_overlay(mac_handle_t mh)453236589d6bSRobert Mustacchi mac_is_overlay(mac_handle_t mh)
453336589d6bSRobert Mustacchi {
453436589d6bSRobert Mustacchi 	return ((((mac_impl_t *)mh)->mi_state_flags & MIS_IS_OVERLAY) != 0);
4535da14cebeSEric Cheng }
4536da14cebeSEric Cheng 
4537da14cebeSEric Cheng mac_handle_t
mac_get_lower_mac_handle(mac_handle_t mh)4538da14cebeSEric Cheng mac_get_lower_mac_handle(mac_handle_t mh)
4539da14cebeSEric Cheng {
4540da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
4541da14cebeSEric Cheng 
4542da14cebeSEric Cheng 	ASSERT(mac_is_vnic(mh));
4543da14cebeSEric Cheng 	return (((vnic_t *)mip->mi_driver)->vn_lower_mh);
4544da14cebeSEric Cheng }
4545da14cebeSEric Cheng 
45460dc2366fSVenugopal Iyer boolean_t
mac_is_vnic_primary(mac_handle_t mh)45470dc2366fSVenugopal Iyer mac_is_vnic_primary(mac_handle_t mh)
45480dc2366fSVenugopal Iyer {
45490dc2366fSVenugopal Iyer 	mac_impl_t *mip = (mac_impl_t *)mh;
45500dc2366fSVenugopal Iyer 
45510dc2366fSVenugopal Iyer 	ASSERT(mac_is_vnic(mh));
45520dc2366fSVenugopal Iyer 	return (((vnic_t *)mip->mi_driver)->vn_addr_type ==
45530dc2366fSVenugopal Iyer 	    VNIC_MAC_ADDR_TYPE_PRIMARY);
45540dc2366fSVenugopal Iyer }
45550dc2366fSVenugopal Iyer 
4556da14cebeSEric Cheng void
mac_update_resources(mac_resource_props_t * nmrp,mac_resource_props_t * cmrp,boolean_t is_user_flow)4557da14cebeSEric Cheng mac_update_resources(mac_resource_props_t *nmrp, mac_resource_props_t *cmrp,
4558da14cebeSEric Cheng     boolean_t is_user_flow)
4559da14cebeSEric Cheng {
4560da14cebeSEric Cheng 	if (nmrp != NULL && cmrp != NULL) {
4561da14cebeSEric Cheng 		if (nmrp->mrp_mask & MRP_PRIORITY) {
4562da14cebeSEric Cheng 			if (nmrp->mrp_priority == MPL_RESET) {
4563da14cebeSEric Cheng 				cmrp->mrp_mask &= ~MRP_PRIORITY;
4564da14cebeSEric Cheng 				if (is_user_flow) {
4565da14cebeSEric Cheng 					cmrp->mrp_priority =
4566da14cebeSEric Cheng 					    MPL_SUBFLOW_DEFAULT;
4567da14cebeSEric Cheng 				} else {
4568da14cebeSEric Cheng 					cmrp->mrp_priority = MPL_LINK_DEFAULT;
4569da14cebeSEric Cheng 				}
4570da14cebeSEric Cheng 			} else {
4571da14cebeSEric Cheng 				cmrp->mrp_mask |= MRP_PRIORITY;
4572da14cebeSEric Cheng 				cmrp->mrp_priority = nmrp->mrp_priority;
4573da14cebeSEric Cheng 			}
4574da14cebeSEric Cheng 		}
4575da14cebeSEric Cheng 		if (nmrp->mrp_mask & MRP_MAXBW) {
45760dc2366fSVenugopal Iyer 			if (nmrp->mrp_maxbw == MRP_MAXBW_RESETVAL) {
4577da14cebeSEric Cheng 				cmrp->mrp_mask &= ~MRP_MAXBW;
45780dc2366fSVenugopal Iyer 				cmrp->mrp_maxbw = 0;
45790dc2366fSVenugopal Iyer 			} else {
4580da14cebeSEric Cheng 				cmrp->mrp_mask |= MRP_MAXBW;
45810dc2366fSVenugopal Iyer 				cmrp->mrp_maxbw = nmrp->mrp_maxbw;
45820dc2366fSVenugopal Iyer 			}
4583da14cebeSEric Cheng 		}
4584da14cebeSEric Cheng 		if (nmrp->mrp_mask & MRP_CPUS)
4585da14cebeSEric Cheng 			MAC_COPY_CPUS(nmrp, cmrp);
458625ec3e3dSEric Cheng 
45870dc2366fSVenugopal Iyer 		if (nmrp->mrp_mask & MRP_POOL) {
45880dc2366fSVenugopal Iyer 			if (strlen(nmrp->mrp_pool) == 0) {
45890dc2366fSVenugopal Iyer 				cmrp->mrp_mask &= ~MRP_POOL;
45900dc2366fSVenugopal Iyer 				bzero(cmrp->mrp_pool, sizeof (cmrp->mrp_pool));
45910dc2366fSVenugopal Iyer 			} else {
45920dc2366fSVenugopal Iyer 				cmrp->mrp_mask |= MRP_POOL;
45930dc2366fSVenugopal Iyer 				(void) strncpy(cmrp->mrp_pool, nmrp->mrp_pool,
45940dc2366fSVenugopal Iyer 				    sizeof (cmrp->mrp_pool));
45950dc2366fSVenugopal Iyer 			}
45960dc2366fSVenugopal Iyer 
45970dc2366fSVenugopal Iyer 		}
45980dc2366fSVenugopal Iyer 
459925ec3e3dSEric Cheng 		if (nmrp->mrp_mask & MRP_PROTECT)
460025ec3e3dSEric Cheng 			mac_protect_update(nmrp, cmrp);
46010dc2366fSVenugopal Iyer 
46020dc2366fSVenugopal Iyer 		/*
46030dc2366fSVenugopal Iyer 		 * Update the rings specified.
46040dc2366fSVenugopal Iyer 		 */
46050dc2366fSVenugopal Iyer 		if (nmrp->mrp_mask & MRP_RX_RINGS) {
46060dc2366fSVenugopal Iyer 			if (nmrp->mrp_mask & MRP_RINGS_RESET) {
46070dc2366fSVenugopal Iyer 				cmrp->mrp_mask &= ~MRP_RX_RINGS;
46080dc2366fSVenugopal Iyer 				if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
46090dc2366fSVenugopal Iyer 					cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
46100dc2366fSVenugopal Iyer 				cmrp->mrp_nrxrings = 0;
46110dc2366fSVenugopal Iyer 			} else {
46120dc2366fSVenugopal Iyer 				cmrp->mrp_mask |= MRP_RX_RINGS;
46130dc2366fSVenugopal Iyer 				cmrp->mrp_nrxrings = nmrp->mrp_nrxrings;
46140dc2366fSVenugopal Iyer 			}
46150dc2366fSVenugopal Iyer 		}
46160dc2366fSVenugopal Iyer 		if (nmrp->mrp_mask & MRP_TX_RINGS) {
46170dc2366fSVenugopal Iyer 			if (nmrp->mrp_mask & MRP_RINGS_RESET) {
46180dc2366fSVenugopal Iyer 				cmrp->mrp_mask &= ~MRP_TX_RINGS;
46190dc2366fSVenugopal Iyer 				if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
46200dc2366fSVenugopal Iyer 					cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
46210dc2366fSVenugopal Iyer 				cmrp->mrp_ntxrings = 0;
46220dc2366fSVenugopal Iyer 			} else {
46230dc2366fSVenugopal Iyer 				cmrp->mrp_mask |= MRP_TX_RINGS;
46240dc2366fSVenugopal Iyer 				cmrp->mrp_ntxrings = nmrp->mrp_ntxrings;
46250dc2366fSVenugopal Iyer 			}
46260dc2366fSVenugopal Iyer 		}
46270dc2366fSVenugopal Iyer 		if (nmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
46280dc2366fSVenugopal Iyer 			cmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
46290dc2366fSVenugopal Iyer 		else if (cmrp->mrp_mask & MRP_RXRINGS_UNSPEC)
46300dc2366fSVenugopal Iyer 			cmrp->mrp_mask &= ~MRP_RXRINGS_UNSPEC;
46310dc2366fSVenugopal Iyer 
46320dc2366fSVenugopal Iyer 		if (nmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
46330dc2366fSVenugopal Iyer 			cmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
46340dc2366fSVenugopal Iyer 		else if (cmrp->mrp_mask & MRP_TXRINGS_UNSPEC)
46350dc2366fSVenugopal Iyer 			cmrp->mrp_mask &= ~MRP_TXRINGS_UNSPEC;
4636da14cebeSEric Cheng 	}
4637da14cebeSEric Cheng }
4638da14cebeSEric Cheng 
4639da14cebeSEric Cheng /*
4640da14cebeSEric Cheng  * i_mac_set_resources:
4641da14cebeSEric Cheng  *
4642da14cebeSEric Cheng  * This routine associates properties with the primary MAC client of
4643da14cebeSEric Cheng  * the specified MAC instance.
4644da14cebeSEric Cheng  * - Cache the properties in mac_impl_t
4645da14cebeSEric Cheng  * - Apply the properties to the primary MAC client if exists
4646da14cebeSEric Cheng  */
4647da14cebeSEric Cheng int
i_mac_set_resources(mac_handle_t mh,mac_resource_props_t * mrp)4648da14cebeSEric Cheng i_mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4649da14cebeSEric Cheng {
4650da14cebeSEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
4651da14cebeSEric Cheng 	mac_client_impl_t	*mcip;
4652da14cebeSEric Cheng 	int			err = 0;
46535d460eafSCathy Zhou 	uint32_t		resmask, newresmask;
46540dc2366fSVenugopal Iyer 	mac_resource_props_t	*tmrp, *umrp;
4655da14cebeSEric Cheng 
4656da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4657da14cebeSEric Cheng 
46580dc2366fSVenugopal Iyer 	err = mac_validate_props(mip, mrp);
4659da14cebeSEric Cheng 	if (err != 0)
4660da14cebeSEric Cheng 		return (err);
4661da14cebeSEric Cheng 
46620dc2366fSVenugopal Iyer 	umrp = kmem_zalloc(sizeof (*umrp), KM_SLEEP);
46630dc2366fSVenugopal Iyer 	bcopy(&mip->mi_resource_props, umrp, sizeof (*umrp));
46640dc2366fSVenugopal Iyer 	resmask = umrp->mrp_mask;
46650dc2366fSVenugopal Iyer 	mac_update_resources(mrp, umrp, B_FALSE);
46660dc2366fSVenugopal Iyer 	newresmask = umrp->mrp_mask;
46675d460eafSCathy Zhou 
46685d460eafSCathy Zhou 	if (resmask == 0 && newresmask != 0) {
46695d460eafSCathy Zhou 		/*
46700dc2366fSVenugopal Iyer 		 * Bandwidth, priority, cpu or pool link properties configured,
46715d460eafSCathy Zhou 		 * must disable fastpath.
46725d460eafSCathy Zhou 		 */
46730dc2366fSVenugopal Iyer 		if ((err = mac_fastpath_disable((mac_handle_t)mip)) != 0) {
46740dc2366fSVenugopal Iyer 			kmem_free(umrp, sizeof (*umrp));
46755d460eafSCathy Zhou 			return (err);
46765d460eafSCathy Zhou 		}
46770dc2366fSVenugopal Iyer 	}
46785d460eafSCathy Zhou 
4679da14cebeSEric Cheng 	/*
4680da14cebeSEric Cheng 	 * Since bind_cpu may be modified by mac_client_set_resources()
4681da14cebeSEric Cheng 	 * we use a copy of bind_cpu and finally cache bind_cpu in mip.
4682da14cebeSEric Cheng 	 * This allows us to cache only user edits in mip.
4683da14cebeSEric Cheng 	 */
46840dc2366fSVenugopal Iyer 	tmrp = kmem_zalloc(sizeof (*tmrp), KM_SLEEP);
46850dc2366fSVenugopal Iyer 	bcopy(mrp, tmrp, sizeof (*tmrp));
4686da14cebeSEric Cheng 	mcip = mac_primary_client_handle(mip);
4687ae6aa22aSVenugopal Iyer 	if (mcip != NULL && (mcip->mci_state_flags & MCIS_IS_AGGR_PORT) == 0) {
46880dc2366fSVenugopal Iyer 		err = mac_client_set_resources((mac_client_handle_t)mcip, tmrp);
46890dc2366fSVenugopal Iyer 	} else if ((mrp->mrp_mask & MRP_RX_RINGS ||
46900dc2366fSVenugopal Iyer 	    mrp->mrp_mask & MRP_TX_RINGS)) {
46910dc2366fSVenugopal Iyer 		mac_client_impl_t	*vmcip;
46920dc2366fSVenugopal Iyer 
46930dc2366fSVenugopal Iyer 		/*
46940dc2366fSVenugopal Iyer 		 * If the primary is not up, we need to check if there
46950dc2366fSVenugopal Iyer 		 * are any VLANs on this primary. If there are then
46960dc2366fSVenugopal Iyer 		 * we need to set this property on the VLANs since
46970dc2366fSVenugopal Iyer 		 * VLANs follow the primary they are based on. Just
46980dc2366fSVenugopal Iyer 		 * look for the first VLAN and change its properties,
46990dc2366fSVenugopal Iyer 		 * all the other VLANs should be in the same group.
47000dc2366fSVenugopal Iyer 		 */
47010dc2366fSVenugopal Iyer 		for (vmcip = mip->mi_clients_list; vmcip != NULL;
47020dc2366fSVenugopal Iyer 		    vmcip = vmcip->mci_client_next) {
47030dc2366fSVenugopal Iyer 			if ((vmcip->mci_flent->fe_type & FLOW_PRIMARY_MAC) &&
47040dc2366fSVenugopal Iyer 			    mac_client_vid((mac_client_handle_t)vmcip) !=
47050dc2366fSVenugopal Iyer 			    VLAN_ID_NONE) {
47060dc2366fSVenugopal Iyer 				break;
47070dc2366fSVenugopal Iyer 			}
47080dc2366fSVenugopal Iyer 		}
47090dc2366fSVenugopal Iyer 		if (vmcip != NULL) {
47100dc2366fSVenugopal Iyer 			mac_resource_props_t	*omrp;
47110dc2366fSVenugopal Iyer 			mac_resource_props_t	*vmrp;
47120dc2366fSVenugopal Iyer 
47130dc2366fSVenugopal Iyer 			omrp = kmem_zalloc(sizeof (*omrp), KM_SLEEP);
47140dc2366fSVenugopal Iyer 			bcopy(MCIP_RESOURCE_PROPS(vmcip), omrp, sizeof (*omrp));
47150dc2366fSVenugopal Iyer 			/*
47160dc2366fSVenugopal Iyer 			 * We dont' call mac_update_resources since we
47170dc2366fSVenugopal Iyer 			 * want to take only the ring properties and
47180dc2366fSVenugopal Iyer 			 * not all the properties that may have changed.
47190dc2366fSVenugopal Iyer 			 */
47200dc2366fSVenugopal Iyer 			vmrp = MCIP_RESOURCE_PROPS(vmcip);
47210dc2366fSVenugopal Iyer 			if (mrp->mrp_mask & MRP_RX_RINGS) {
47220dc2366fSVenugopal Iyer 				if (mrp->mrp_mask & MRP_RINGS_RESET) {
47230dc2366fSVenugopal Iyer 					vmrp->mrp_mask &= ~MRP_RX_RINGS;
47240dc2366fSVenugopal Iyer 					if (vmrp->mrp_mask &
47250dc2366fSVenugopal Iyer 					    MRP_RXRINGS_UNSPEC) {
47260dc2366fSVenugopal Iyer 						vmrp->mrp_mask &=
47270dc2366fSVenugopal Iyer 						    ~MRP_RXRINGS_UNSPEC;
47280dc2366fSVenugopal Iyer 					}
47290dc2366fSVenugopal Iyer 					vmrp->mrp_nrxrings = 0;
47300dc2366fSVenugopal Iyer 				} else {
47310dc2366fSVenugopal Iyer 					vmrp->mrp_mask |= MRP_RX_RINGS;
47320dc2366fSVenugopal Iyer 					vmrp->mrp_nrxrings = mrp->mrp_nrxrings;
47330dc2366fSVenugopal Iyer 				}
47340dc2366fSVenugopal Iyer 			}
47350dc2366fSVenugopal Iyer 			if (mrp->mrp_mask & MRP_TX_RINGS) {
47360dc2366fSVenugopal Iyer 				if (mrp->mrp_mask & MRP_RINGS_RESET) {
47370dc2366fSVenugopal Iyer 					vmrp->mrp_mask &= ~MRP_TX_RINGS;
47380dc2366fSVenugopal Iyer 					if (vmrp->mrp_mask &
47390dc2366fSVenugopal Iyer 					    MRP_TXRINGS_UNSPEC) {
47400dc2366fSVenugopal Iyer 						vmrp->mrp_mask &=
47410dc2366fSVenugopal Iyer 						    ~MRP_TXRINGS_UNSPEC;
47420dc2366fSVenugopal Iyer 					}
47430dc2366fSVenugopal Iyer 					vmrp->mrp_ntxrings = 0;
47440dc2366fSVenugopal Iyer 				} else {
47450dc2366fSVenugopal Iyer 					vmrp->mrp_mask |= MRP_TX_RINGS;
47460dc2366fSVenugopal Iyer 					vmrp->mrp_ntxrings = mrp->mrp_ntxrings;
47470dc2366fSVenugopal Iyer 				}
47480dc2366fSVenugopal Iyer 			}
47490dc2366fSVenugopal Iyer 			if (mrp->mrp_mask & MRP_RXRINGS_UNSPEC)
47500dc2366fSVenugopal Iyer 				vmrp->mrp_mask |= MRP_RXRINGS_UNSPEC;
47510dc2366fSVenugopal Iyer 
47520dc2366fSVenugopal Iyer 			if (mrp->mrp_mask & MRP_TXRINGS_UNSPEC)
47530dc2366fSVenugopal Iyer 				vmrp->mrp_mask |= MRP_TXRINGS_UNSPEC;
47540dc2366fSVenugopal Iyer 
47550dc2366fSVenugopal Iyer 			if ((err = mac_client_set_rings_prop(vmcip, mrp,
47560dc2366fSVenugopal Iyer 			    omrp)) != 0) {
47570dc2366fSVenugopal Iyer 				bcopy(omrp, MCIP_RESOURCE_PROPS(vmcip),
47580dc2366fSVenugopal Iyer 				    sizeof (*omrp));
47590dc2366fSVenugopal Iyer 			} else {
47600dc2366fSVenugopal Iyer 				mac_set_prim_vlan_rings(mip, vmrp);
47610dc2366fSVenugopal Iyer 			}
47620dc2366fSVenugopal Iyer 			kmem_free(omrp, sizeof (*omrp));
47630dc2366fSVenugopal Iyer 		}
4764da14cebeSEric Cheng 	}
47655d460eafSCathy Zhou 
47665d460eafSCathy Zhou 	/* Only update the values if mac_client_set_resources succeeded */
47675d460eafSCathy Zhou 	if (err == 0) {
47680dc2366fSVenugopal Iyer 		bcopy(umrp, &mip->mi_resource_props, sizeof (*umrp));
47695d460eafSCathy Zhou 		/*
47700dc2366fSVenugopal Iyer 		 * If bandwidth, priority or cpu link properties cleared,
47715d460eafSCathy Zhou 		 * renable fastpath.
47725d460eafSCathy Zhou 		 */
47735d460eafSCathy Zhou 		if (resmask != 0 && newresmask == 0)
47745d460eafSCathy Zhou 			mac_fastpath_enable((mac_handle_t)mip);
47755d460eafSCathy Zhou 	} else if (resmask == 0 && newresmask != 0) {
47765d460eafSCathy Zhou 		mac_fastpath_enable((mac_handle_t)mip);
47775d460eafSCathy Zhou 	}
47780dc2366fSVenugopal Iyer 	kmem_free(tmrp, sizeof (*tmrp));
47790dc2366fSVenugopal Iyer 	kmem_free(umrp, sizeof (*umrp));
4780da14cebeSEric Cheng 	return (err);
4781da14cebeSEric Cheng }
4782da14cebeSEric Cheng 
4783da14cebeSEric Cheng int
mac_set_resources(mac_handle_t mh,mac_resource_props_t * mrp)4784da14cebeSEric Cheng mac_set_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4785da14cebeSEric Cheng {
4786da14cebeSEric Cheng 	int err;
4787da14cebeSEric Cheng 
4788da14cebeSEric Cheng 	i_mac_perim_enter((mac_impl_t *)mh);
4789da14cebeSEric Cheng 	err = i_mac_set_resources(mh, mrp);
4790da14cebeSEric Cheng 	i_mac_perim_exit((mac_impl_t *)mh);
4791da14cebeSEric Cheng 	return (err);
4792da14cebeSEric Cheng }
4793da14cebeSEric Cheng 
4794da14cebeSEric Cheng /*
4795da14cebeSEric Cheng  * Get the properties cached for the specified MAC instance.
4796da14cebeSEric Cheng  */
4797da14cebeSEric Cheng void
mac_get_resources(mac_handle_t mh,mac_resource_props_t * mrp)4798da14cebeSEric Cheng mac_get_resources(mac_handle_t mh, mac_resource_props_t *mrp)
4799da14cebeSEric Cheng {
4800da14cebeSEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
4801da14cebeSEric Cheng 	mac_client_impl_t	*mcip;
4802da14cebeSEric Cheng 
4803da14cebeSEric Cheng 	mcip = mac_primary_client_handle(mip);
4804da14cebeSEric Cheng 	if (mcip != NULL) {
48050dc2366fSVenugopal Iyer 		mac_client_get_resources((mac_client_handle_t)mcip, mrp);
48060dc2366fSVenugopal Iyer 		return;
48070dc2366fSVenugopal Iyer 	}
48080dc2366fSVenugopal Iyer 	bcopy(&mip->mi_resource_props, mrp, sizeof (mac_resource_props_t));
48090dc2366fSVenugopal Iyer }
48100dc2366fSVenugopal Iyer 
48110dc2366fSVenugopal Iyer /*
48120dc2366fSVenugopal Iyer  * Get the effective properties from the primary client of the
48130dc2366fSVenugopal Iyer  * specified MAC instance.
48140dc2366fSVenugopal Iyer  */
48150dc2366fSVenugopal Iyer void
mac_get_effective_resources(mac_handle_t mh,mac_resource_props_t * mrp)48160dc2366fSVenugopal Iyer mac_get_effective_resources(mac_handle_t mh, mac_resource_props_t *mrp)
48170dc2366fSVenugopal Iyer {
48180dc2366fSVenugopal Iyer 	mac_impl_t		*mip = (mac_impl_t *)mh;
48190dc2366fSVenugopal Iyer 	mac_client_impl_t	*mcip;
48200dc2366fSVenugopal Iyer 
48210dc2366fSVenugopal Iyer 	mcip = mac_primary_client_handle(mip);
48220dc2366fSVenugopal Iyer 	if (mcip != NULL) {
48230dc2366fSVenugopal Iyer 		mac_client_get_effective_resources((mac_client_handle_t)mcip,
4824da14cebeSEric Cheng 		    mrp);
4825da14cebeSEric Cheng 		return;
4826da14cebeSEric Cheng 	}
48270dc2366fSVenugopal Iyer 	bzero(mrp, sizeof (mac_resource_props_t));
4828da14cebeSEric Cheng }
4829da14cebeSEric Cheng 
48304eaa4710SRishi Srivatsavai int
mac_set_pvid(mac_handle_t mh,uint16_t pvid)48314eaa4710SRishi Srivatsavai mac_set_pvid(mac_handle_t mh, uint16_t pvid)
48324eaa4710SRishi Srivatsavai {
48334eaa4710SRishi Srivatsavai 	mac_impl_t *mip = (mac_impl_t *)mh;
48344eaa4710SRishi Srivatsavai 	mac_client_impl_t *mcip;
48354eaa4710SRishi Srivatsavai 	mac_unicast_impl_t *muip;
48364eaa4710SRishi Srivatsavai 
48374eaa4710SRishi Srivatsavai 	i_mac_perim_enter(mip);
48384eaa4710SRishi Srivatsavai 	if (pvid != 0) {
48394eaa4710SRishi Srivatsavai 		for (mcip = mip->mi_clients_list; mcip != NULL;
48404eaa4710SRishi Srivatsavai 		    mcip = mcip->mci_client_next) {
48414eaa4710SRishi Srivatsavai 			for (muip = mcip->mci_unicast_list; muip != NULL;
48424eaa4710SRishi Srivatsavai 			    muip = muip->mui_next) {
48434eaa4710SRishi Srivatsavai 				if (muip->mui_vid == pvid) {
48444eaa4710SRishi Srivatsavai 					i_mac_perim_exit(mip);
48454eaa4710SRishi Srivatsavai 					return (EBUSY);
48464eaa4710SRishi Srivatsavai 				}
48474eaa4710SRishi Srivatsavai 			}
48484eaa4710SRishi Srivatsavai 		}
48494eaa4710SRishi Srivatsavai 	}
48504eaa4710SRishi Srivatsavai 	mip->mi_pvid = pvid;
48514eaa4710SRishi Srivatsavai 	i_mac_perim_exit(mip);
48524eaa4710SRishi Srivatsavai 	return (0);
48534eaa4710SRishi Srivatsavai }
48544eaa4710SRishi Srivatsavai 
48554eaa4710SRishi Srivatsavai uint16_t
mac_get_pvid(mac_handle_t mh)48564eaa4710SRishi Srivatsavai mac_get_pvid(mac_handle_t mh)
48574eaa4710SRishi Srivatsavai {
48584eaa4710SRishi Srivatsavai 	mac_impl_t *mip = (mac_impl_t *)mh;
48594eaa4710SRishi Srivatsavai 
48604eaa4710SRishi Srivatsavai 	return (mip->mi_pvid);
48614eaa4710SRishi Srivatsavai }
48624eaa4710SRishi Srivatsavai 
48634eaa4710SRishi Srivatsavai uint32_t
mac_get_llimit(mac_handle_t mh)48644eaa4710SRishi Srivatsavai mac_get_llimit(mac_handle_t mh)
48654eaa4710SRishi Srivatsavai {
48664eaa4710SRishi Srivatsavai 	mac_impl_t *mip = (mac_impl_t *)mh;
48674eaa4710SRishi Srivatsavai 
48684eaa4710SRishi Srivatsavai 	return (mip->mi_llimit);
48694eaa4710SRishi Srivatsavai }
48704eaa4710SRishi Srivatsavai 
48714eaa4710SRishi Srivatsavai uint32_t
mac_get_ldecay(mac_handle_t mh)48724eaa4710SRishi Srivatsavai mac_get_ldecay(mac_handle_t mh)
48734eaa4710SRishi Srivatsavai {
48744eaa4710SRishi Srivatsavai 	mac_impl_t *mip = (mac_impl_t *)mh;
48754eaa4710SRishi Srivatsavai 
48764eaa4710SRishi Srivatsavai 	return (mip->mi_ldecay);
48774eaa4710SRishi Srivatsavai }
48784eaa4710SRishi Srivatsavai 
4879da14cebeSEric Cheng /*
4880da14cebeSEric Cheng  * Rename a mac client, its flow, and the kstat.
4881da14cebeSEric Cheng  */
4882da14cebeSEric Cheng int
mac_rename_primary(mac_handle_t mh,const char * new_name)4883da14cebeSEric Cheng mac_rename_primary(mac_handle_t mh, const char *new_name)
4884da14cebeSEric Cheng {
4885da14cebeSEric Cheng 	mac_impl_t		*mip = (mac_impl_t *)mh;
4886da14cebeSEric Cheng 	mac_client_impl_t	*cur_clnt = NULL;
4887da14cebeSEric Cheng 	flow_entry_t		*fep;
4888da14cebeSEric Cheng 
4889da14cebeSEric Cheng 	i_mac_perim_enter(mip);
4890da14cebeSEric Cheng 
4891da14cebeSEric Cheng 	/*
4892da14cebeSEric Cheng 	 * VNICs: we need to change the sys flow name and
4893da14cebeSEric Cheng 	 * the associated flow kstat.
4894da14cebeSEric Cheng 	 */
4895da14cebeSEric Cheng 	if (mip->mi_state_flags & MIS_IS_VNIC) {
48960dc2366fSVenugopal Iyer 		mac_client_impl_t *mcip = mac_vnic_lower(mip);
4897da14cebeSEric Cheng 		ASSERT(new_name != NULL);
48980dc2366fSVenugopal Iyer 		mac_rename_flow_names(mcip, new_name);
48990dc2366fSVenugopal Iyer 		mac_stat_rename(mcip);
4900da14cebeSEric Cheng 		goto done;
4901da14cebeSEric Cheng 	}
4902da14cebeSEric Cheng 	/*
4903da14cebeSEric Cheng 	 * This mac may itself be an aggr link, or it may have some client
4904da14cebeSEric Cheng 	 * which is an aggr port. For both cases, we need to change the
4905da14cebeSEric Cheng 	 * aggr port's mac client name, its flow name and the associated flow
4906da14cebeSEric Cheng 	 * kstat.
4907da14cebeSEric Cheng 	 */
4908da14cebeSEric Cheng 	if (mip->mi_state_flags & MIS_IS_AGGR) {
4909da14cebeSEric Cheng 		mac_capab_aggr_t aggr_cap;
4910da14cebeSEric Cheng 		mac_rename_fn_t rename_fn;
4911da14cebeSEric Cheng 		boolean_t ret;
4912da14cebeSEric Cheng 
4913da14cebeSEric Cheng 		ASSERT(new_name != NULL);
4914da14cebeSEric Cheng 		ret = i_mac_capab_get((mac_handle_t)mip, MAC_CAPAB_AGGR,
4915da14cebeSEric Cheng 		    (void *)(&aggr_cap));
4916da14cebeSEric Cheng 		ASSERT(ret == B_TRUE);
4917da14cebeSEric Cheng 		rename_fn = aggr_cap.mca_rename_fn;
4918da14cebeSEric Cheng 		rename_fn(new_name, mip->mi_driver);
4919da14cebeSEric Cheng 		/*
4920da14cebeSEric Cheng 		 * The aggr's client name and kstat flow name will be
4921da14cebeSEric Cheng 		 * updated below, i.e. via mac_rename_flow_names.
4922da14cebeSEric Cheng 		 */
4923da14cebeSEric Cheng 	}
4924da14cebeSEric Cheng 
4925da14cebeSEric Cheng 	for (cur_clnt = mip->mi_clients_list; cur_clnt != NULL;
4926da14cebeSEric Cheng 	    cur_clnt = cur_clnt->mci_client_next) {
4927da14cebeSEric Cheng 		if (cur_clnt->mci_state_flags & MCIS_IS_AGGR_PORT) {
4928da14cebeSEric Cheng 			if (new_name != NULL) {
4929da14cebeSEric Cheng 				char *str_st = cur_clnt->mci_name;
4930da14cebeSEric Cheng 				char *str_del = strchr(str_st, '-');
4931da14cebeSEric Cheng 
4932da14cebeSEric Cheng 				ASSERT(str_del != NULL);
4933da14cebeSEric Cheng 				bzero(str_del + 1, MAXNAMELEN -
4934da14cebeSEric Cheng 				    (str_del - str_st + 1));
4935da14cebeSEric Cheng 				bcopy(new_name, str_del + 1,
4936da14cebeSEric Cheng 				    strlen(new_name));
4937da14cebeSEric Cheng 			}
4938da14cebeSEric Cheng 			fep = cur_clnt->mci_flent;
4939da14cebeSEric Cheng 			mac_rename_flow(fep, cur_clnt->mci_name);
4940da14cebeSEric Cheng 			break;
4941da14cebeSEric Cheng 		} else if (new_name != NULL &&
4942da14cebeSEric Cheng 		    cur_clnt->mci_state_flags & MCIS_USE_DATALINK_NAME) {
4943da14cebeSEric Cheng 			mac_rename_flow_names(cur_clnt, new_name);
4944da14cebeSEric Cheng 			break;
4945da14cebeSEric Cheng 		}
4946da14cebeSEric Cheng 	}
4947da14cebeSEric Cheng 
49480dc2366fSVenugopal Iyer 	/* Recreate kstats associated with aggr pseudo rings */
49490dc2366fSVenugopal Iyer 	if (mip->mi_state_flags & MIS_IS_AGGR)
49500dc2366fSVenugopal Iyer 		mac_pseudo_ring_stat_rename(mip);
49510dc2366fSVenugopal Iyer 
4952da14cebeSEric Cheng done:
4953da14cebeSEric Cheng 	i_mac_perim_exit(mip);
4954da14cebeSEric Cheng 	return (0);
4955da14cebeSEric Cheng }
4956da14cebeSEric Cheng 
4957da14cebeSEric Cheng /*
4958da14cebeSEric Cheng  * Rename the MAC client's flow names
4959da14cebeSEric Cheng  */
4960da14cebeSEric Cheng static void
mac_rename_flow_names(mac_client_impl_t * mcip,const char * new_name)4961da14cebeSEric Cheng mac_rename_flow_names(mac_client_impl_t *mcip, const char *new_name)
4962da14cebeSEric Cheng {
4963da14cebeSEric Cheng 	flow_entry_t	*flent;
4964da14cebeSEric Cheng 	uint16_t	vid;
4965da000602SGirish Moodalbail 	char		flowname[MAXFLOWNAMELEN];
4966da14cebeSEric Cheng 	mac_impl_t	*mip = mcip->mci_mip;
4967da14cebeSEric Cheng 
4968da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mip));
4969da14cebeSEric Cheng 
4970da14cebeSEric Cheng 	/*
4971da14cebeSEric Cheng 	 * Use mi_rw_lock to ensure that threads not in the mac perimeter
4972da14cebeSEric Cheng 	 * see a self-consistent value for mci_name
4973da14cebeSEric Cheng 	 */
4974da14cebeSEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_WRITER);
4975da14cebeSEric Cheng 	(void) strlcpy(mcip->mci_name, new_name, sizeof (mcip->mci_name));
4976da14cebeSEric Cheng 	rw_exit(&mip->mi_rw_lock);
4977da14cebeSEric Cheng 
4978da14cebeSEric Cheng 	mac_rename_flow(mcip->mci_flent, new_name);
4979da14cebeSEric Cheng 
4980da14cebeSEric Cheng 	if (mcip->mci_nflents == 1)
4981da14cebeSEric Cheng 		return;
4982da14cebeSEric Cheng 
4983da14cebeSEric Cheng 	/*
4984da14cebeSEric Cheng 	 * We have to rename all the others too, no stats to destroy for
4985da14cebeSEric Cheng 	 * these.
4986da14cebeSEric Cheng 	 */
4987da14cebeSEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
4988da14cebeSEric Cheng 	    flent = flent->fe_client_next) {
4989da14cebeSEric Cheng 		if (flent != mcip->mci_flent) {
4990da14cebeSEric Cheng 			vid = i_mac_flow_vid(flent);
4991da14cebeSEric Cheng 			(void) sprintf(flowname, "%s%u", new_name, vid);
4992da14cebeSEric Cheng 			mac_flow_set_name(flent, flowname);
4993da14cebeSEric Cheng 		}
4994da14cebeSEric Cheng 	}
4995da14cebeSEric Cheng }
4996da14cebeSEric Cheng 
4997da14cebeSEric Cheng 
4998da14cebeSEric Cheng /*
4999da14cebeSEric Cheng  * Add a flow to the MAC client's flow list - i.e list of MAC/VID tuples
5000da14cebeSEric Cheng  * defined for the specified MAC client.
5001da14cebeSEric Cheng  */
5002da14cebeSEric Cheng static void
mac_client_add_to_flow_list(mac_client_impl_t * mcip,flow_entry_t * flent)5003da14cebeSEric Cheng mac_client_add_to_flow_list(mac_client_impl_t *mcip, flow_entry_t *flent)
5004da14cebeSEric Cheng {
5005da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5006da14cebeSEric Cheng 	/*
5007da14cebeSEric Cheng 	 * The promisc Rx data path walks the mci_flent_list. Protect by
5008da14cebeSEric Cheng 	 * using mi_rw_lock
5009da14cebeSEric Cheng 	 */
5010da14cebeSEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
5011da14cebeSEric Cheng 
50123bb0cb70SBryan Cantrill 	mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
50133bb0cb70SBryan Cantrill 
5014da14cebeSEric Cheng 	/* Add it to the head */
5015da14cebeSEric Cheng 	flent->fe_client_next = mcip->mci_flent_list;
5016da14cebeSEric Cheng 	mcip->mci_flent_list = flent;
5017da14cebeSEric Cheng 	mcip->mci_nflents++;
5018da14cebeSEric Cheng 
5019da14cebeSEric Cheng 	/*
5020da14cebeSEric Cheng 	 * Keep track of the number of non-zero VIDs addresses per MAC
5021da14cebeSEric Cheng 	 * client to avoid figuring it out in the data-path.
5022da14cebeSEric Cheng 	 */
5023da14cebeSEric Cheng 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
5024da14cebeSEric Cheng 		mcip->mci_nvids++;
5025da14cebeSEric Cheng 
5026da14cebeSEric Cheng 	rw_exit(&mcip->mci_rw_lock);
5027da14cebeSEric Cheng }
5028da14cebeSEric Cheng 
5029da14cebeSEric Cheng /*
5030da14cebeSEric Cheng  * Remove a flow entry from the MAC client's list.
5031da14cebeSEric Cheng  */
5032da14cebeSEric Cheng static void
mac_client_remove_flow_from_list(mac_client_impl_t * mcip,flow_entry_t * flent)5033da14cebeSEric Cheng mac_client_remove_flow_from_list(mac_client_impl_t *mcip, flow_entry_t *flent)
5034da14cebeSEric Cheng {
5035da14cebeSEric Cheng 	flow_entry_t	*fe = mcip->mci_flent_list;
5036da14cebeSEric Cheng 	flow_entry_t	*prev_fe = NULL;
5037da14cebeSEric Cheng 
5038da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5039da14cebeSEric Cheng 	/*
5040da14cebeSEric Cheng 	 * The promisc Rx data path walks the mci_flent_list. Protect by
5041da14cebeSEric Cheng 	 * using mci_rw_lock
5042da14cebeSEric Cheng 	 */
5043da14cebeSEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
50443bb0cb70SBryan Cantrill 	mcip->mci_vidcache = MCIP_VIDCACHE_INVALID;
50453bb0cb70SBryan Cantrill 
5046da14cebeSEric Cheng 	while ((fe != NULL) && (fe != flent)) {
5047da14cebeSEric Cheng 		prev_fe = fe;
5048da14cebeSEric Cheng 		fe = fe->fe_client_next;
5049da14cebeSEric Cheng 	}
5050da14cebeSEric Cheng 
5051da000602SGirish Moodalbail 	ASSERT(fe != NULL);
5052da14cebeSEric Cheng 	if (prev_fe == NULL) {
5053da14cebeSEric Cheng 		/* Deleting the first node */
5054da14cebeSEric Cheng 		mcip->mci_flent_list = fe->fe_client_next;
5055da14cebeSEric Cheng 	} else {
5056da14cebeSEric Cheng 		prev_fe->fe_client_next = fe->fe_client_next;
5057da14cebeSEric Cheng 	}
5058da14cebeSEric Cheng 	mcip->mci_nflents--;
5059da14cebeSEric Cheng 
5060da14cebeSEric Cheng 	if (i_mac_flow_vid(flent) != VLAN_ID_NONE)
5061da14cebeSEric Cheng 		mcip->mci_nvids--;
5062da000602SGirish Moodalbail 
5063da14cebeSEric Cheng 	rw_exit(&mcip->mci_rw_lock);
5064da14cebeSEric Cheng }
5065da14cebeSEric Cheng 
5066da14cebeSEric Cheng /*
5067da14cebeSEric Cheng  * Check if the given VID belongs to this MAC client.
5068da14cebeSEric Cheng  */
5069da14cebeSEric Cheng boolean_t
mac_client_check_flow_vid(mac_client_impl_t * mcip,uint16_t vid)5070da14cebeSEric Cheng mac_client_check_flow_vid(mac_client_impl_t *mcip, uint16_t vid)
5071da14cebeSEric Cheng {
5072da14cebeSEric Cheng 	flow_entry_t	*flent;
5073da14cebeSEric Cheng 	uint16_t	mci_vid;
50743bb0cb70SBryan Cantrill 	uint32_t	cache = mcip->mci_vidcache;
50753bb0cb70SBryan Cantrill 
50763bb0cb70SBryan Cantrill 	/*
50773bb0cb70SBryan Cantrill 	 * In hopes of not having to touch the mci_rw_lock, check to see if
50783bb0cb70SBryan Cantrill 	 * this vid matches our cached result.
50793bb0cb70SBryan Cantrill 	 */
50803bb0cb70SBryan Cantrill 	if (MCIP_VIDCACHE_ISVALID(cache) && MCIP_VIDCACHE_VID(cache) == vid)
50813bb0cb70SBryan Cantrill 		return (MCIP_VIDCACHE_BOOL(cache) ? B_TRUE : B_FALSE);
5082da14cebeSEric Cheng 
5083da14cebeSEric Cheng 	/* The mci_flent_list is protected by mci_rw_lock */
5084da14cebeSEric Cheng 	rw_enter(&mcip->mci_rw_lock, RW_WRITER);
5085da14cebeSEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
5086da14cebeSEric Cheng 	    flent = flent->fe_client_next) {
5087da14cebeSEric Cheng 		mci_vid = i_mac_flow_vid(flent);
5088da14cebeSEric Cheng 		if (vid == mci_vid) {
50893bb0cb70SBryan Cantrill 			mcip->mci_vidcache = MCIP_VIDCACHE_CACHE(vid, B_TRUE);
5090da14cebeSEric Cheng 			rw_exit(&mcip->mci_rw_lock);
5091da14cebeSEric Cheng 			return (B_TRUE);
5092da14cebeSEric Cheng 		}
5093da14cebeSEric Cheng 	}
50943bb0cb70SBryan Cantrill 
50953bb0cb70SBryan Cantrill 	mcip->mci_vidcache = MCIP_VIDCACHE_CACHE(vid, B_FALSE);
5096da14cebeSEric Cheng 	rw_exit(&mcip->mci_rw_lock);
5097da14cebeSEric Cheng 	return (B_FALSE);
5098da14cebeSEric Cheng }
5099da14cebeSEric Cheng 
5100da14cebeSEric Cheng /*
5101da14cebeSEric Cheng  * Get the flow entry for the specified <MAC addr, VID> tuple.
5102da14cebeSEric Cheng  */
5103da14cebeSEric Cheng static flow_entry_t *
mac_client_get_flow(mac_client_impl_t * mcip,mac_unicast_impl_t * muip)5104da14cebeSEric Cheng mac_client_get_flow(mac_client_impl_t *mcip, mac_unicast_impl_t *muip)
5105da14cebeSEric Cheng {
5106da14cebeSEric Cheng 	mac_address_t *map = mcip->mci_unicast;
5107da14cebeSEric Cheng 	flow_entry_t *flent;
5108da14cebeSEric Cheng 	uint16_t vid;
5109da14cebeSEric Cheng 	flow_desc_t flow_desc;
5110da14cebeSEric Cheng 
5111da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5112da14cebeSEric Cheng 
5113da14cebeSEric Cheng 	mac_flow_get_desc(mcip->mci_flent, &flow_desc);
5114da14cebeSEric Cheng 	if (bcmp(flow_desc.fd_dst_mac, map->ma_addr, map->ma_len) != 0)
5115da14cebeSEric Cheng 		return (NULL);
5116da14cebeSEric Cheng 
5117da14cebeSEric Cheng 	for (flent = mcip->mci_flent_list; flent != NULL;
5118da14cebeSEric Cheng 	    flent = flent->fe_client_next) {
5119da14cebeSEric Cheng 		vid = i_mac_flow_vid(flent);
5120da14cebeSEric Cheng 		if (vid == muip->mui_vid) {
5121da14cebeSEric Cheng 			return (flent);
5122da14cebeSEric Cheng 		}
5123da14cebeSEric Cheng 	}
5124da14cebeSEric Cheng 
5125da14cebeSEric Cheng 	return (NULL);
5126da14cebeSEric Cheng }
5127da14cebeSEric Cheng 
5128da14cebeSEric Cheng /*
5129da14cebeSEric Cheng  * Since mci_flent has the SRSs, when we want to remove it, we replace
5130da14cebeSEric Cheng  * the flow_desc_t in mci_flent with that of an existing flent and then
5131da14cebeSEric Cheng  * remove that flent instead of mci_flent.
5132da14cebeSEric Cheng  */
5133da14cebeSEric Cheng static flow_entry_t *
mac_client_swap_mciflent(mac_client_impl_t * mcip)5134da14cebeSEric Cheng mac_client_swap_mciflent(mac_client_impl_t *mcip)
5135da14cebeSEric Cheng {
5136da14cebeSEric Cheng 	flow_entry_t	*flent = mcip->mci_flent;
5137da14cebeSEric Cheng 	flow_tab_t	*ft = flent->fe_flow_tab;
5138da14cebeSEric Cheng 	flow_entry_t	*flent1;
5139da14cebeSEric Cheng 	flow_desc_t	fl_desc;
5140da000602SGirish Moodalbail 	char		fl_name[MAXFLOWNAMELEN];
5141da14cebeSEric Cheng 	int		err;
5142da14cebeSEric Cheng 
5143da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
5144da14cebeSEric Cheng 	ASSERT(mcip->mci_nflents > 1);
5145da14cebeSEric Cheng 
5146da14cebeSEric Cheng 	/* get the next flent following the primary flent  */
5147da14cebeSEric Cheng 	flent1 = mcip->mci_flent_list->fe_client_next;
5148da14cebeSEric Cheng 	ASSERT(flent1 != NULL && flent1->fe_flow_tab == ft);
5149da14cebeSEric Cheng 
5150da14cebeSEric Cheng 	/*
5151da14cebeSEric Cheng 	 * Remove the flent from the flow table before updating the
5152da14cebeSEric Cheng 	 * flow descriptor as the hash depends on the flow descriptor.
5153da14cebeSEric Cheng 	 * This also helps incoming packet classification avoid having
5154da14cebeSEric Cheng 	 * to grab fe_lock. Access to fe_flow_desc of a flent not in the
5155da14cebeSEric Cheng 	 * flow table is done under the fe_lock so that log or stat functions
5156da14cebeSEric Cheng 	 * see a self-consistent fe_flow_desc. The name and desc are specific
5157da14cebeSEric Cheng 	 * to a flow, the rest are shared by all the clients, including
5158da14cebeSEric Cheng 	 * resource control etc.
5159da14cebeSEric Cheng 	 */
5160da14cebeSEric Cheng 	mac_flow_remove(ft, flent, B_TRUE);
5161da14cebeSEric Cheng 	mac_flow_remove(ft, flent1, B_TRUE);
5162da14cebeSEric Cheng 
5163da14cebeSEric Cheng 	bcopy(&flent->fe_flow_desc, &fl_desc, sizeof (flow_desc_t));
5164da000602SGirish Moodalbail 	bcopy(flent->fe_flow_name, fl_name, MAXFLOWNAMELEN);
5165da14cebeSEric Cheng 
5166da14cebeSEric Cheng 	/* update the primary flow entry */
5167da14cebeSEric Cheng 	mutex_enter(&flent->fe_lock);
5168da14cebeSEric Cheng 	bcopy(&flent1->fe_flow_desc, &flent->fe_flow_desc,
5169da14cebeSEric Cheng 	    sizeof (flow_desc_t));
5170da000602SGirish Moodalbail 	bcopy(&flent1->fe_flow_name, &flent->fe_flow_name, MAXFLOWNAMELEN);
5171da14cebeSEric Cheng 	mutex_exit(&flent->fe_lock);
5172da14cebeSEric Cheng 
5173da14cebeSEric Cheng 	/* update the flow entry that is to be freed */
5174da14cebeSEric Cheng 	mutex_enter(&flent1->fe_lock);
5175da14cebeSEric Cheng 	bcopy(&fl_desc, &flent1->fe_flow_desc, sizeof (flow_desc_t));
5176da000602SGirish Moodalbail 	bcopy(fl_name, &flent1->fe_flow_name, MAXFLOWNAMELEN);
5177da14cebeSEric Cheng 	mutex_exit(&flent1->fe_lock);
5178da14cebeSEric Cheng 
5179da14cebeSEric Cheng 	/* now reinsert the flow entries in the table */
5180da14cebeSEric Cheng 	err = mac_flow_add(ft, flent);
5181da14cebeSEric Cheng 	ASSERT(err == 0);
5182da14cebeSEric Cheng 
5183da14cebeSEric Cheng 	err = mac_flow_add(ft, flent1);
5184da14cebeSEric Cheng 	ASSERT(err == 0);
5185da14cebeSEric Cheng 
5186da14cebeSEric Cheng 	return (flent1);
5187da14cebeSEric Cheng }
5188da14cebeSEric Cheng 
5189da14cebeSEric Cheng /*
5190da14cebeSEric Cheng  * Return whether there is only one flow entry associated with this
5191da14cebeSEric Cheng  * MAC client.
5192da14cebeSEric Cheng  */
5193da14cebeSEric Cheng static boolean_t
mac_client_single_rcvr(mac_client_impl_t * mcip)5194da14cebeSEric Cheng mac_client_single_rcvr(mac_client_impl_t *mcip)
5195da14cebeSEric Cheng {
5196da14cebeSEric Cheng 	return (mcip->mci_nflents == 1);
5197da14cebeSEric Cheng }
5198da14cebeSEric Cheng 
5199da14cebeSEric Cheng int
mac_validate_props(mac_impl_t * mip,mac_resource_props_t * mrp)52000dc2366fSVenugopal Iyer mac_validate_props(mac_impl_t *mip, mac_resource_props_t *mrp)
5201da14cebeSEric Cheng {
52020dc2366fSVenugopal Iyer 	boolean_t		reset;
52030dc2366fSVenugopal Iyer 	uint32_t		rings_needed;
52040dc2366fSVenugopal Iyer 	uint32_t		rings_avail;
52050dc2366fSVenugopal Iyer 	mac_group_type_t	gtype;
52060dc2366fSVenugopal Iyer 	mac_resource_props_t	*mip_mrp;
52070dc2366fSVenugopal Iyer 
5208da14cebeSEric Cheng 	if (mrp == NULL)
5209da14cebeSEric Cheng 		return (0);
5210da14cebeSEric Cheng 
5211da14cebeSEric Cheng 	if (mrp->mrp_mask & MRP_PRIORITY) {
5212da14cebeSEric Cheng 		mac_priority_level_t	pri = mrp->mrp_priority;
5213da14cebeSEric Cheng 
5214da14cebeSEric Cheng 		if (pri < MPL_LOW || pri > MPL_RESET)
5215da14cebeSEric Cheng 			return (EINVAL);
5216da14cebeSEric Cheng 	}
5217da14cebeSEric Cheng 
5218da14cebeSEric Cheng 	if (mrp->mrp_mask & MRP_MAXBW) {
5219da14cebeSEric Cheng 		uint64_t maxbw = mrp->mrp_maxbw;
5220da14cebeSEric Cheng 
5221da14cebeSEric Cheng 		if (maxbw < MRP_MAXBW_MINVAL && maxbw != 0)
5222da14cebeSEric Cheng 			return (EINVAL);
5223da14cebeSEric Cheng 	}
5224da14cebeSEric Cheng 	if (mrp->mrp_mask & MRP_CPUS) {
5225b6b50fcfSNitin Hande 		int i, j;
5226da14cebeSEric Cheng 		mac_cpu_mode_t	fanout;
5227da14cebeSEric Cheng 
52285adf34bdSRajagopal Kunhappan 		if (mrp->mrp_ncpus > ncpus)
5229da14cebeSEric Cheng 			return (EINVAL);
5230da14cebeSEric Cheng 
5231da14cebeSEric Cheng 		for (i = 0; i < mrp->mrp_ncpus; i++) {
5232b6b50fcfSNitin Hande 			for (j = 0; j < mrp->mrp_ncpus; j++) {
5233b6b50fcfSNitin Hande 				if (i != j &&
5234b6b50fcfSNitin Hande 				    mrp->mrp_cpu[i] == mrp->mrp_cpu[j]) {
5235b6b50fcfSNitin Hande 					return (EINVAL);
5236b6b50fcfSNitin Hande 				}
5237b6b50fcfSNitin Hande 			}
5238b6b50fcfSNitin Hande 		}
5239b6b50fcfSNitin Hande 
5240b6b50fcfSNitin Hande 		for (i = 0; i < mrp->mrp_ncpus; i++) {
5241da14cebeSEric Cheng 			cpu_t *cp;
5242da14cebeSEric Cheng 			int rv;
5243da14cebeSEric Cheng 
5244da14cebeSEric Cheng 			mutex_enter(&cpu_lock);
5245da14cebeSEric Cheng 			cp = cpu_get(mrp->mrp_cpu[i]);
5246da14cebeSEric Cheng 			if (cp != NULL)
5247da14cebeSEric Cheng 				rv = cpu_is_online(cp);
5248da14cebeSEric Cheng 			else
5249da14cebeSEric Cheng 				rv = 0;
5250da14cebeSEric Cheng 			mutex_exit(&cpu_lock);
5251da14cebeSEric Cheng 			if (rv == 0)
5252da14cebeSEric Cheng 				return (EINVAL);
5253da14cebeSEric Cheng 		}
5254da14cebeSEric Cheng 
5255da14cebeSEric Cheng 		fanout = mrp->mrp_fanout_mode;
5256da14cebeSEric Cheng 		if (fanout < 0 || fanout > MCM_CPUS)
5257da14cebeSEric Cheng 			return (EINVAL);
5258da14cebeSEric Cheng 	}
525925ec3e3dSEric Cheng 
526025ec3e3dSEric Cheng 	if (mrp->mrp_mask & MRP_PROTECT) {
526125ec3e3dSEric Cheng 		int err = mac_protect_validate(mrp);
526225ec3e3dSEric Cheng 		if (err != 0)
526325ec3e3dSEric Cheng 			return (err);
526425ec3e3dSEric Cheng 	}
52650dc2366fSVenugopal Iyer 
52660dc2366fSVenugopal Iyer 	if (!(mrp->mrp_mask & MRP_RX_RINGS) &&
52670dc2366fSVenugopal Iyer 	    !(mrp->mrp_mask & MRP_TX_RINGS)) {
52680dc2366fSVenugopal Iyer 		return (0);
52690dc2366fSVenugopal Iyer 	}
52700dc2366fSVenugopal Iyer 
52710dc2366fSVenugopal Iyer 	/*
52720dc2366fSVenugopal Iyer 	 * mip will be null when we come from mac_flow_create or
52730dc2366fSVenugopal Iyer 	 * mac_link_flow_modify. In the latter case it is a user flow,
52740dc2366fSVenugopal Iyer 	 * for which we don't support rings. In the former we would
52750dc2366fSVenugopal Iyer 	 * have validated the props beforehand (i_mac_unicast_add ->
52760dc2366fSVenugopal Iyer 	 * mac_client_set_resources -> validate for the primary and
52770dc2366fSVenugopal Iyer 	 * vnic_dev_create -> mac_client_set_resources -> validate for
52780dc2366fSVenugopal Iyer 	 * a vnic.
52790dc2366fSVenugopal Iyer 	 */
52800dc2366fSVenugopal Iyer 	if (mip == NULL)
52810dc2366fSVenugopal Iyer 		return (0);
52820dc2366fSVenugopal Iyer 
52830dc2366fSVenugopal Iyer 	/*
52840dc2366fSVenugopal Iyer 	 * We don't support setting rings property for a VNIC that is using a
52850dc2366fSVenugopal Iyer 	 * primary address (VLAN)
52860dc2366fSVenugopal Iyer 	 */
52870dc2366fSVenugopal Iyer 	if ((mip->mi_state_flags & MIS_IS_VNIC) &&
52880dc2366fSVenugopal Iyer 	    mac_is_vnic_primary((mac_handle_t)mip)) {
52890dc2366fSVenugopal Iyer 		return (ENOTSUP);
52900dc2366fSVenugopal Iyer 	}
52910dc2366fSVenugopal Iyer 
52920dc2366fSVenugopal Iyer 	mip_mrp = &mip->mi_resource_props;
52930dc2366fSVenugopal Iyer 	/*
52940dc2366fSVenugopal Iyer 	 * The rings property should be validated against the NICs
52950dc2366fSVenugopal Iyer 	 * resources
52960dc2366fSVenugopal Iyer 	 */
52970dc2366fSVenugopal Iyer 	if (mip->mi_state_flags & MIS_IS_VNIC)
52980dc2366fSVenugopal Iyer 		mip = (mac_impl_t *)mac_get_lower_mac_handle((mac_handle_t)mip);
52990dc2366fSVenugopal Iyer 
53000dc2366fSVenugopal Iyer 	reset = mrp->mrp_mask & MRP_RINGS_RESET;
53010dc2366fSVenugopal Iyer 	/*
53020dc2366fSVenugopal Iyer 	 * If groups are not supported, return error.
53030dc2366fSVenugopal Iyer 	 */
53040dc2366fSVenugopal Iyer 	if (((mrp->mrp_mask & MRP_RX_RINGS) && mip->mi_rx_groups == NULL) ||
53050dc2366fSVenugopal Iyer 	    ((mrp->mrp_mask & MRP_TX_RINGS) && mip->mi_tx_groups == NULL)) {
53060dc2366fSVenugopal Iyer 		return (EINVAL);
53070dc2366fSVenugopal Iyer 	}
53080dc2366fSVenugopal Iyer 	/*
53090dc2366fSVenugopal Iyer 	 * If we are just resetting, there is no validation needed.
53100dc2366fSVenugopal Iyer 	 */
53110dc2366fSVenugopal Iyer 	if (reset)
53120dc2366fSVenugopal Iyer 		return (0);
53130dc2366fSVenugopal Iyer 
53140dc2366fSVenugopal Iyer 	if (mrp->mrp_mask & MRP_RX_RINGS) {
53150dc2366fSVenugopal Iyer 		rings_needed = mrp->mrp_nrxrings;
53160dc2366fSVenugopal Iyer 		/*
53170dc2366fSVenugopal Iyer 		 * We just want to check if the number of additional
53180dc2366fSVenugopal Iyer 		 * rings requested is available.
53190dc2366fSVenugopal Iyer 		 */
53200dc2366fSVenugopal Iyer 		if (mip_mrp->mrp_mask & MRP_RX_RINGS) {
53210dc2366fSVenugopal Iyer 			if (mrp->mrp_nrxrings > mip_mrp->mrp_nrxrings)
53220dc2366fSVenugopal Iyer 				/* Just check for the additional rings */
53230dc2366fSVenugopal Iyer 				rings_needed -= mip_mrp->mrp_nrxrings;
53240dc2366fSVenugopal Iyer 			else
53250dc2366fSVenugopal Iyer 				/* We are not asking for additional rings */
53260dc2366fSVenugopal Iyer 				rings_needed = 0;
53270dc2366fSVenugopal Iyer 		}
53280dc2366fSVenugopal Iyer 		rings_avail = mip->mi_rxrings_avail;
53290dc2366fSVenugopal Iyer 		gtype = mip->mi_rx_group_type;
53300dc2366fSVenugopal Iyer 	} else {
53310dc2366fSVenugopal Iyer 		rings_needed = mrp->mrp_ntxrings;
53320dc2366fSVenugopal Iyer 		/* Similarly for the TX rings */
53330dc2366fSVenugopal Iyer 		if (mip_mrp->mrp_mask & MRP_TX_RINGS) {
53340dc2366fSVenugopal Iyer 			if (mrp->mrp_ntxrings > mip_mrp->mrp_ntxrings)
53350dc2366fSVenugopal Iyer 				/* Just check for the additional rings */
53360dc2366fSVenugopal Iyer 				rings_needed -= mip_mrp->mrp_ntxrings;
53370dc2366fSVenugopal Iyer 			else
53380dc2366fSVenugopal Iyer 				/* We are not asking for additional rings */
53390dc2366fSVenugopal Iyer 				rings_needed = 0;
53400dc2366fSVenugopal Iyer 		}
53410dc2366fSVenugopal Iyer 		rings_avail = mip->mi_txrings_avail;
53420dc2366fSVenugopal Iyer 		gtype = mip->mi_tx_group_type;
53430dc2366fSVenugopal Iyer 	}
53440dc2366fSVenugopal Iyer 
53450dc2366fSVenugopal Iyer 	/* Error if the group is dynamic .. */
53460dc2366fSVenugopal Iyer 	if (gtype == MAC_GROUP_TYPE_DYNAMIC) {
53470dc2366fSVenugopal Iyer 		/*
53480dc2366fSVenugopal Iyer 		 * .. and rings specified are more than available.
53490dc2366fSVenugopal Iyer 		 */
53500dc2366fSVenugopal Iyer 		if (rings_needed > rings_avail)
53510dc2366fSVenugopal Iyer 			return (EINVAL);
53520dc2366fSVenugopal Iyer 	} else {
53530dc2366fSVenugopal Iyer 		/*
53540dc2366fSVenugopal Iyer 		 * OR group is static and we have specified some rings.
53550dc2366fSVenugopal Iyer 		 */
53560dc2366fSVenugopal Iyer 		if (rings_needed > 0)
53570dc2366fSVenugopal Iyer 			return (EINVAL);
53580dc2366fSVenugopal Iyer 	}
5359da14cebeSEric Cheng 	return (0);
5360da14cebeSEric Cheng }
5361da14cebeSEric Cheng 
5362da14cebeSEric Cheng /*
5363da14cebeSEric Cheng  * Send a MAC_NOTE_LINK notification to all the MAC clients whenever the
5364da14cebeSEric Cheng  * underlying physical link is down. This is to allow MAC clients to
5365da14cebeSEric Cheng  * communicate with other clients.
5366da14cebeSEric Cheng  */
5367da14cebeSEric Cheng void
mac_virtual_link_update(mac_impl_t * mip)5368da14cebeSEric Cheng mac_virtual_link_update(mac_impl_t *mip)
5369da14cebeSEric Cheng {
5370da14cebeSEric Cheng 	if (mip->mi_linkstate != LINK_STATE_UP)
5371da14cebeSEric Cheng 		i_mac_notify(mip, MAC_NOTE_LINK);
5372da14cebeSEric Cheng }
5373da14cebeSEric Cheng 
5374da14cebeSEric Cheng /*
5375da14cebeSEric Cheng  * For clients that have a pass-thru MAC, e.g. VNIC, we set the VNIC's
5376da14cebeSEric Cheng  * mac handle in the client.
5377da14cebeSEric Cheng  */
5378da14cebeSEric Cheng void
mac_set_upper_mac(mac_client_handle_t mch,mac_handle_t mh,mac_resource_props_t * mrp)53790dc2366fSVenugopal Iyer mac_set_upper_mac(mac_client_handle_t mch, mac_handle_t mh,
53800dc2366fSVenugopal Iyer     mac_resource_props_t *mrp)
5381da14cebeSEric Cheng {
5382da14cebeSEric Cheng 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
53830dc2366fSVenugopal Iyer 	mac_impl_t		*mip = (mac_impl_t *)mh;
5384da14cebeSEric Cheng 
53850dc2366fSVenugopal Iyer 	mcip->mci_upper_mip = mip;
53860dc2366fSVenugopal Iyer 	/* If there are any properties, copy it over too */
53870dc2366fSVenugopal Iyer 	if (mrp != NULL) {
53880dc2366fSVenugopal Iyer 		bcopy(mrp, &mip->mi_resource_props,
53890dc2366fSVenugopal Iyer 		    sizeof (mac_resource_props_t));
53900dc2366fSVenugopal Iyer 	}
5391da14cebeSEric Cheng }
5392da14cebeSEric Cheng 
5393da14cebeSEric Cheng /*
5394da14cebeSEric Cheng  * Mark the mac as being used exclusively by the single mac client that is
5395da14cebeSEric Cheng  * doing some control operation on this mac. No further opens of this mac
5396da14cebeSEric Cheng  * will be allowed until this client calls mac_unmark_exclusive. The mac
5397da14cebeSEric Cheng  * client calling this function must already be in the mac perimeter
5398da14cebeSEric Cheng  */
5399da14cebeSEric Cheng int
mac_mark_exclusive(mac_handle_t mh)5400da14cebeSEric Cheng mac_mark_exclusive(mac_handle_t mh)
5401da14cebeSEric Cheng {
5402da14cebeSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
5403da14cebeSEric Cheng 
5404da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD(mh));
5405da14cebeSEric Cheng 	/*
5406da14cebeSEric Cheng 	 * Look up its entry in the global hash table.
5407da14cebeSEric Cheng 	 */
5408da14cebeSEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
5409da14cebeSEric Cheng 	if (mip->mi_state_flags & MIS_DISABLED) {
5410da14cebeSEric Cheng 		rw_exit(&i_mac_impl_lock);
5411da14cebeSEric Cheng 		return (ENOENT);
5412da14cebeSEric Cheng 	}
5413da14cebeSEric Cheng 
5414da14cebeSEric Cheng 	/*
5415da14cebeSEric Cheng 	 * A reference to mac is held even if the link is not plumbed.
5416da14cebeSEric Cheng 	 * In i_dls_link_create() we open the MAC interface and hold the
5417da14cebeSEric Cheng 	 * reference. There is an additional reference for the mac_open
5418da14cebeSEric Cheng 	 * done in acquiring the mac perimeter
5419da14cebeSEric Cheng 	 */
5420da14cebeSEric Cheng 	if (mip->mi_ref != 2) {
5421da14cebeSEric Cheng 		rw_exit(&i_mac_impl_lock);
5422da14cebeSEric Cheng 		return (EBUSY);
5423da14cebeSEric Cheng 	}
5424da14cebeSEric Cheng 
5425da14cebeSEric Cheng 	ASSERT(!(mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
5426da14cebeSEric Cheng 	mip->mi_state_flags |= MIS_EXCLUSIVE_HELD;
5427da14cebeSEric Cheng 	rw_exit(&i_mac_impl_lock);
5428da14cebeSEric Cheng 	return (0);
5429da14cebeSEric Cheng }
5430da14cebeSEric Cheng 
5431da14cebeSEric Cheng void
mac_unmark_exclusive(mac_handle_t mh)5432da14cebeSEric Cheng mac_unmark_exclusive(mac_handle_t mh)
5433da14cebeSEric Cheng {
5434da14cebeSEric Cheng 	mac_impl_t	*mip = (mac_impl_t *)mh;
5435da14cebeSEric Cheng 
5436da14cebeSEric Cheng 	ASSERT(MAC_PERIM_HELD(mh));
5437da14cebeSEric Cheng 
5438da14cebeSEric Cheng 	rw_enter(&i_mac_impl_lock, RW_WRITER);
5439da14cebeSEric Cheng 	/* 1 for the creation and another for the perimeter */
5440da14cebeSEric Cheng 	ASSERT(mip->mi_ref == 2 && (mip->mi_state_flags & MIS_EXCLUSIVE_HELD));
5441da14cebeSEric Cheng 	mip->mi_state_flags &= ~MIS_EXCLUSIVE_HELD;
5442da14cebeSEric Cheng 	rw_exit(&i_mac_impl_lock);
5443da14cebeSEric Cheng }
5444da14cebeSEric Cheng 
5445da14cebeSEric Cheng /*
54460dc2366fSVenugopal Iyer  * Set the MTU for the specified MAC.
5447da14cebeSEric Cheng  */
5448da14cebeSEric Cheng int
mac_set_mtu(mac_handle_t mh,uint_t new_mtu,uint_t * old_mtu_arg)5449da14cebeSEric Cheng mac_set_mtu(mac_handle_t mh, uint_t new_mtu, uint_t *old_mtu_arg)
5450da14cebeSEric Cheng {
5451da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
5452da14cebeSEric Cheng 	uint_t old_mtu;
5453e3321b89SSebastien Roy 	int rv = 0;
5454da14cebeSEric Cheng 
5455da14cebeSEric Cheng 	i_mac_perim_enter(mip);
5456da14cebeSEric Cheng 
54572b24ab6bSSebastien Roy 	if (!(mip->mi_callbacks->mc_callbacks & (MC_SETPROP|MC_GETPROP))) {
5458da14cebeSEric Cheng 		rv = ENOTSUP;
5459da14cebeSEric Cheng 		goto bail;
5460da14cebeSEric Cheng 	}
5461da14cebeSEric Cheng 
54622b24ab6bSSebastien Roy 	old_mtu = mip->mi_sdu_max;
5463da14cebeSEric Cheng 
54640dc2366fSVenugopal Iyer 	if (new_mtu == 0 || new_mtu < mip->mi_sdu_min) {
54650dc2366fSVenugopal Iyer 		rv = EINVAL;
54660dc2366fSVenugopal Iyer 		goto bail;
54670dc2366fSVenugopal Iyer 	}
54680dc2366fSVenugopal Iyer 
5469a776d98eSRobert Mustacchi 	rw_enter(&mip->mi_rw_lock, RW_READER);
5470a776d98eSRobert Mustacchi 	if (mip->mi_mtrp != NULL && new_mtu < mip->mi_mtrp->mtr_mtu) {
5471a776d98eSRobert Mustacchi 		rv = EBUSY;
5472a776d98eSRobert Mustacchi 		rw_exit(&mip->mi_rw_lock);
5473a776d98eSRobert Mustacchi 		goto bail;
5474a776d98eSRobert Mustacchi 	}
5475a776d98eSRobert Mustacchi 	rw_exit(&mip->mi_rw_lock);
5476a776d98eSRobert Mustacchi 
5477da14cebeSEric Cheng 	if (old_mtu != new_mtu) {
5478da14cebeSEric Cheng 		rv = mip->mi_callbacks->mc_setprop(mip->mi_driver,
5479da14cebeSEric Cheng 		    "mtu", MAC_PROP_MTU, sizeof (uint_t), &new_mtu);
54800dc2366fSVenugopal Iyer 		if (rv != 0)
54810dc2366fSVenugopal Iyer 			goto bail;
54820dc2366fSVenugopal Iyer 		rv = mac_maxsdu_update(mh, new_mtu);
54830dc2366fSVenugopal Iyer 		ASSERT(rv == 0);
5484da14cebeSEric Cheng 	}
5485da14cebeSEric Cheng 
5486da14cebeSEric Cheng bail:
5487da14cebeSEric Cheng 	i_mac_perim_exit(mip);
5488da14cebeSEric Cheng 
5489da14cebeSEric Cheng 	if (rv == 0 && old_mtu_arg != NULL)
5490da14cebeSEric Cheng 		*old_mtu_arg = old_mtu;
5491da14cebeSEric Cheng 	return (rv);
5492da14cebeSEric Cheng }
5493da14cebeSEric Cheng 
54940dc2366fSVenugopal Iyer /*
54950dc2366fSVenugopal Iyer  * Return the RX h/w information for the group indexed by grp_num.
54960dc2366fSVenugopal Iyer  */
5497da14cebeSEric Cheng void
mac_get_hwrxgrp_info(mac_handle_t mh,int grp_index,uint_t * grp_num,uint_t * n_rings,uint_t * rings,uint_t * type,uint_t * n_clnts,char * clnts_name)54980dc2366fSVenugopal Iyer mac_get_hwrxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
54990dc2366fSVenugopal Iyer     uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
55000dc2366fSVenugopal Iyer     char *clnts_name)
5501da14cebeSEric Cheng {
5502da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
5503da14cebeSEric Cheng 	mac_grp_client_t *mcip;
5504da14cebeSEric Cheng 	uint_t i = 0, index = 0;
55050dc2366fSVenugopal Iyer 	mac_ring_t	*ring;
5506da14cebeSEric Cheng 
5507da14cebeSEric Cheng 	/* Revisit when we implement fully dynamic group allocation */
5508da14cebeSEric Cheng 	ASSERT(grp_index >= 0 && grp_index < mip->mi_rx_group_count);
5509da14cebeSEric Cheng 
5510da14cebeSEric Cheng 	rw_enter(&mip->mi_rw_lock, RW_READER);
5511da14cebeSEric Cheng 	*grp_num = mip->mi_rx_groups[grp_index].mrg_index;
5512da14cebeSEric Cheng 	*type = mip->mi_rx_groups[grp_index].mrg_type;
5513da14cebeSEric Cheng 	*n_rings = mip->mi_rx_groups[grp_index].mrg_cur_count;
55140dc2366fSVenugopal Iyer 	ring = mip->mi_rx_groups[grp_index].mrg_rings;
55150dc2366fSVenugopal Iyer 	for (index = 0; index < mip->mi_rx_groups[grp_index].mrg_cur_count;
55160dc2366fSVenugopal Iyer 	    index++) {
55170dc2366fSVenugopal Iyer 		rings[index] = ring->mr_index;
55180dc2366fSVenugopal Iyer 		ring = ring->mr_next;
55190dc2366fSVenugopal Iyer 	}
55200dc2366fSVenugopal Iyer 	/* Assuming the 1st is the default group */
55210dc2366fSVenugopal Iyer 	index = 0;
55220dc2366fSVenugopal Iyer 	if (grp_index == 0) {
55230dc2366fSVenugopal Iyer 		(void) strlcpy(clnts_name, "<default,mcast>,",
55240dc2366fSVenugopal Iyer 		    MAXCLIENTNAMELEN);
55250dc2366fSVenugopal Iyer 		index += strlen("<default,mcast>,");
55260dc2366fSVenugopal Iyer 	}
5527da14cebeSEric Cheng 	for (mcip = mip->mi_rx_groups[grp_index].mrg_clients; mcip != NULL;
5528da14cebeSEric Cheng 	    mcip = mcip->mgc_next) {
5529da14cebeSEric Cheng 		int name_len = strlen(mcip->mgc_client->mci_name);
5530da14cebeSEric Cheng 
5531da14cebeSEric Cheng 		/*
5532da14cebeSEric Cheng 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
5533da14cebeSEric Cheng 		 * names.
5534da14cebeSEric Cheng 		 * XXXX Formating the client name string needs to be moved
5535da14cebeSEric Cheng 		 * to user land when fixing the size of dhi_clnts in
5536da14cebeSEric Cheng 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
5537da14cebeSEric Cheng 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
5538da14cebeSEric Cheng 		 */
5539da14cebeSEric Cheng 		if (index + name_len >= MAXCLIENTNAMELEN) {
5540da14cebeSEric Cheng 			index = MAXCLIENTNAMELEN;
5541da14cebeSEric Cheng 			break;
5542da14cebeSEric Cheng 		}
5543da14cebeSEric Cheng 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
5544da14cebeSEric Cheng 		    name_len);
5545da14cebeSEric Cheng 		index += name_len;
5546da14cebeSEric Cheng 		clnts_name[index++] = ',';
5547da14cebeSEric Cheng 		i++;
5548da14cebeSEric Cheng 	}
5549da14cebeSEric Cheng 
5550da14cebeSEric Cheng 	/* Get rid of the last , */
5551da14cebeSEric Cheng 	if (index > 0)
5552da14cebeSEric Cheng 		clnts_name[index - 1] = '\0';
5553da14cebeSEric Cheng 	*n_clnts = i;
5554da14cebeSEric Cheng 	rw_exit(&mip->mi_rw_lock);
5555da14cebeSEric Cheng }
5556da14cebeSEric Cheng 
55570dc2366fSVenugopal Iyer /*
55580dc2366fSVenugopal Iyer  * Return the TX h/w information for the group indexed by grp_num.
55590dc2366fSVenugopal Iyer  */
55600dc2366fSVenugopal Iyer void
mac_get_hwtxgrp_info(mac_handle_t mh,int grp_index,uint_t * grp_num,uint_t * n_rings,uint_t * rings,uint_t * type,uint_t * n_clnts,char * clnts_name)55610dc2366fSVenugopal Iyer mac_get_hwtxgrp_info(mac_handle_t mh, int grp_index, uint_t *grp_num,
55620dc2366fSVenugopal Iyer     uint_t *n_rings, uint_t *rings, uint_t *type, uint_t *n_clnts,
55630dc2366fSVenugopal Iyer     char *clnts_name)
55640dc2366fSVenugopal Iyer {
55650dc2366fSVenugopal Iyer 	mac_impl_t *mip = (mac_impl_t *)mh;
55660dc2366fSVenugopal Iyer 	mac_grp_client_t *mcip;
55670dc2366fSVenugopal Iyer 	uint_t i = 0, index = 0;
55680dc2366fSVenugopal Iyer 	mac_ring_t	*ring;
55690dc2366fSVenugopal Iyer 
55700dc2366fSVenugopal Iyer 	/* Revisit when we implement fully dynamic group allocation */
55710dc2366fSVenugopal Iyer 	ASSERT(grp_index >= 0 && grp_index <= mip->mi_tx_group_count);
55720dc2366fSVenugopal Iyer 
55730dc2366fSVenugopal Iyer 	rw_enter(&mip->mi_rw_lock, RW_READER);
55740dc2366fSVenugopal Iyer 	*grp_num = mip->mi_tx_groups[grp_index].mrg_index > 0 ?
55750dc2366fSVenugopal Iyer 	    mip->mi_tx_groups[grp_index].mrg_index : grp_index;
55760dc2366fSVenugopal Iyer 	*type = mip->mi_tx_groups[grp_index].mrg_type;
55770dc2366fSVenugopal Iyer 	*n_rings = mip->mi_tx_groups[grp_index].mrg_cur_count;
55780dc2366fSVenugopal Iyer 	ring = mip->mi_tx_groups[grp_index].mrg_rings;
55790dc2366fSVenugopal Iyer 	for (index = 0; index < mip->mi_tx_groups[grp_index].mrg_cur_count;
55800dc2366fSVenugopal Iyer 	    index++) {
55810dc2366fSVenugopal Iyer 		rings[index] = ring->mr_index;
55820dc2366fSVenugopal Iyer 		ring = ring->mr_next;
55830dc2366fSVenugopal Iyer 	}
55840dc2366fSVenugopal Iyer 	index = 0;
55850dc2366fSVenugopal Iyer 	/* Default group has an index of -1 */
55860dc2366fSVenugopal Iyer 	if (mip->mi_tx_groups[grp_index].mrg_index < 0) {
55870dc2366fSVenugopal Iyer 		(void) strlcpy(clnts_name, "<default>,",
55880dc2366fSVenugopal Iyer 		    MAXCLIENTNAMELEN);
55890dc2366fSVenugopal Iyer 		index += strlen("<default>,");
55900dc2366fSVenugopal Iyer 	}
55910dc2366fSVenugopal Iyer 	for (mcip = mip->mi_tx_groups[grp_index].mrg_clients; mcip != NULL;
55920dc2366fSVenugopal Iyer 	    mcip = mcip->mgc_next) {
55930dc2366fSVenugopal Iyer 		int name_len = strlen(mcip->mgc_client->mci_name);
55940dc2366fSVenugopal Iyer 
55950dc2366fSVenugopal Iyer 		/*
55960dc2366fSVenugopal Iyer 		 * MAXCLIENTNAMELEN is the buffer size reserved for client
55970dc2366fSVenugopal Iyer 		 * names.
55980dc2366fSVenugopal Iyer 		 * XXXX Formating the client name string needs to be moved
55990dc2366fSVenugopal Iyer 		 * to user land when fixing the size of dhi_clnts in
56000dc2366fSVenugopal Iyer 		 * dld_hwgrpinfo_t. We should use n_clients * client_name for
56010dc2366fSVenugopal Iyer 		 * dhi_clntsin instead of MAXCLIENTNAMELEN
56020dc2366fSVenugopal Iyer 		 */
56030dc2366fSVenugopal Iyer 		if (index + name_len >= MAXCLIENTNAMELEN) {
56040dc2366fSVenugopal Iyer 			index = MAXCLIENTNAMELEN;
56050dc2366fSVenugopal Iyer 			break;
56060dc2366fSVenugopal Iyer 		}
56070dc2366fSVenugopal Iyer 		bcopy(mcip->mgc_client->mci_name, &(clnts_name[index]),
56080dc2366fSVenugopal Iyer 		    name_len);
56090dc2366fSVenugopal Iyer 		index += name_len;
56100dc2366fSVenugopal Iyer 		clnts_name[index++] = ',';
56110dc2366fSVenugopal Iyer 		i++;
56120dc2366fSVenugopal Iyer 	}
56130dc2366fSVenugopal Iyer 
56140dc2366fSVenugopal Iyer 	/* Get rid of the last , */
56150dc2366fSVenugopal Iyer 	if (index > 0)
56160dc2366fSVenugopal Iyer 		clnts_name[index - 1] = '\0';
56170dc2366fSVenugopal Iyer 	*n_clnts = i;
56180dc2366fSVenugopal Iyer 	rw_exit(&mip->mi_rw_lock);
56190dc2366fSVenugopal Iyer }
56200dc2366fSVenugopal Iyer 
56210dc2366fSVenugopal Iyer /*
56220dc2366fSVenugopal Iyer  * Return the group count for RX or TX.
56230dc2366fSVenugopal Iyer  */
5624da14cebeSEric Cheng uint_t
mac_hwgrp_num(mac_handle_t mh,int type)56250dc2366fSVenugopal Iyer mac_hwgrp_num(mac_handle_t mh, int type)
5626da14cebeSEric Cheng {
5627da14cebeSEric Cheng 	mac_impl_t *mip = (mac_impl_t *)mh;
5628da14cebeSEric Cheng 
56290dc2366fSVenugopal Iyer 	/*
56300dc2366fSVenugopal Iyer 	 * Return the Rx and Tx group count; for the Tx we need to
56310dc2366fSVenugopal Iyer 	 * include the default too.
56320dc2366fSVenugopal Iyer 	 */
56330dc2366fSVenugopal Iyer 	return (type == MAC_RING_TYPE_RX ? mip->mi_rx_group_count :
56340dc2366fSVenugopal Iyer 	    mip->mi_tx_groups != NULL ? mip->mi_tx_group_count + 1 : 0);
56350dc2366fSVenugopal Iyer }
56360dc2366fSVenugopal Iyer 
56370dc2366fSVenugopal Iyer /*
56380dc2366fSVenugopal Iyer  * The total number of free TX rings for this MAC.
56390dc2366fSVenugopal Iyer  */
56400dc2366fSVenugopal Iyer uint_t
mac_txavail_get(mac_handle_t mh)56410dc2366fSVenugopal Iyer mac_txavail_get(mac_handle_t mh)
56420dc2366fSVenugopal Iyer {
56430dc2366fSVenugopal Iyer 	mac_impl_t	*mip = (mac_impl_t *)mh;
56440dc2366fSVenugopal Iyer 
56450dc2366fSVenugopal Iyer 	return (mip->mi_txrings_avail);
56460dc2366fSVenugopal Iyer }
56470dc2366fSVenugopal Iyer 
56480dc2366fSVenugopal Iyer /*
56490dc2366fSVenugopal Iyer  * The total number of free RX rings for this MAC.
56500dc2366fSVenugopal Iyer  */
56510dc2366fSVenugopal Iyer uint_t
mac_rxavail_get(mac_handle_t mh)56520dc2366fSVenugopal Iyer mac_rxavail_get(mac_handle_t mh)
56530dc2366fSVenugopal Iyer {
56540dc2366fSVenugopal Iyer 	mac_impl_t	*mip = (mac_impl_t *)mh;
56550dc2366fSVenugopal Iyer 
56560dc2366fSVenugopal Iyer 	return (mip->mi_rxrings_avail);
56570dc2366fSVenugopal Iyer }
56580dc2366fSVenugopal Iyer 
56590dc2366fSVenugopal Iyer /*
56600dc2366fSVenugopal Iyer  * The total number of reserved RX rings on this MAC.
56610dc2366fSVenugopal Iyer  */
56620dc2366fSVenugopal Iyer uint_t
mac_rxrsvd_get(mac_handle_t mh)56630dc2366fSVenugopal Iyer mac_rxrsvd_get(mac_handle_t mh)
56640dc2366fSVenugopal Iyer {
56650dc2366fSVenugopal Iyer 	mac_impl_t	*mip = (mac_impl_t *)mh;
56660dc2366fSVenugopal Iyer 
56670dc2366fSVenugopal Iyer 	return (mip->mi_rxrings_rsvd);
56680dc2366fSVenugopal Iyer }
56690dc2366fSVenugopal Iyer 
56700dc2366fSVenugopal Iyer /*
56710dc2366fSVenugopal Iyer  * The total number of reserved TX rings on this MAC.
56720dc2366fSVenugopal Iyer  */
56730dc2366fSVenugopal Iyer uint_t
mac_txrsvd_get(mac_handle_t mh)56740dc2366fSVenugopal Iyer mac_txrsvd_get(mac_handle_t mh)
56750dc2366fSVenugopal Iyer {
56760dc2366fSVenugopal Iyer 	mac_impl_t	*mip = (mac_impl_t *)mh;
56770dc2366fSVenugopal Iyer 
56780dc2366fSVenugopal Iyer 	return (mip->mi_txrings_rsvd);
56790dc2366fSVenugopal Iyer }
56800dc2366fSVenugopal Iyer 
56810dc2366fSVenugopal Iyer /*
56820dc2366fSVenugopal Iyer  * Total number of free RX groups on this MAC.
56830dc2366fSVenugopal Iyer  */
56840dc2366fSVenugopal Iyer uint_t
mac_rxhwlnksavail_get(mac_handle_t mh)56850dc2366fSVenugopal Iyer mac_rxhwlnksavail_get(mac_handle_t mh)
56860dc2366fSVenugopal Iyer {
56870dc2366fSVenugopal Iyer 	mac_impl_t	*mip = (mac_impl_t *)mh;
56880dc2366fSVenugopal Iyer 
56890dc2366fSVenugopal Iyer 	return (mip->mi_rxhwclnt_avail);
56900dc2366fSVenugopal Iyer }
56910dc2366fSVenugopal Iyer 
56920dc2366fSVenugopal Iyer /*
56930dc2366fSVenugopal Iyer  * Total number of RX groups reserved on this MAC.
56940dc2366fSVenugopal Iyer  */
56950dc2366fSVenugopal Iyer uint_t
mac_rxhwlnksrsvd_get(mac_handle_t mh)56960dc2366fSVenugopal Iyer mac_rxhwlnksrsvd_get(mac_handle_t mh)
56970dc2366fSVenugopal Iyer {
56980dc2366fSVenugopal Iyer 	mac_impl_t	*mip = (mac_impl_t *)mh;
56990dc2366fSVenugopal Iyer 
57000dc2366fSVenugopal Iyer 	return (mip->mi_rxhwclnt_used);
57010dc2366fSVenugopal Iyer }
57020dc2366fSVenugopal Iyer 
57030dc2366fSVenugopal Iyer /*
57040dc2366fSVenugopal Iyer  * Total number of free TX groups on this MAC.
57050dc2366fSVenugopal Iyer  */
57060dc2366fSVenugopal Iyer uint_t
mac_txhwlnksavail_get(mac_handle_t mh)57070dc2366fSVenugopal Iyer mac_txhwlnksavail_get(mac_handle_t mh)
57080dc2366fSVenugopal Iyer {
57090dc2366fSVenugopal Iyer 	mac_impl_t	*mip = (mac_impl_t *)mh;
57100dc2366fSVenugopal Iyer 
57110dc2366fSVenugopal Iyer 	return (mip->mi_txhwclnt_avail);
57120dc2366fSVenugopal Iyer }
57130dc2366fSVenugopal Iyer 
57140dc2366fSVenugopal Iyer /*
57150dc2366fSVenugopal Iyer  * Total number of TX groups reserved on this MAC.
57160dc2366fSVenugopal Iyer  */
57170dc2366fSVenugopal Iyer uint_t
mac_txhwlnksrsvd_get(mac_handle_t mh)57180dc2366fSVenugopal Iyer mac_txhwlnksrsvd_get(mac_handle_t mh)
57190dc2366fSVenugopal Iyer {
57200dc2366fSVenugopal Iyer 	mac_impl_t	*mip = (mac_impl_t *)mh;
57210dc2366fSVenugopal Iyer 
57220dc2366fSVenugopal Iyer 	return (mip->mi_txhwclnt_used);
57230dc2366fSVenugopal Iyer }
57240dc2366fSVenugopal Iyer 
57250dc2366fSVenugopal Iyer /*
57260dc2366fSVenugopal Iyer  * Initialize the rings property for a mac client. A non-0 value for
57270dc2366fSVenugopal Iyer  * rxring or txring specifies the number of rings required, a value
57280dc2366fSVenugopal Iyer  * of MAC_RXRINGS_NONE/MAC_TXRINGS_NONE specifies that it doesn't need
57290dc2366fSVenugopal Iyer  * any RX/TX rings and a value of MAC_RXRINGS_DONTCARE/MAC_TXRINGS_DONTCARE
57300dc2366fSVenugopal Iyer  * means the system can decide whether it can give any rings or not.
57310dc2366fSVenugopal Iyer  */
57320dc2366fSVenugopal Iyer void
mac_client_set_rings(mac_client_handle_t mch,int rxrings,int txrings)57330dc2366fSVenugopal Iyer mac_client_set_rings(mac_client_handle_t mch, int rxrings, int txrings)
57340dc2366fSVenugopal Iyer {
57350dc2366fSVenugopal Iyer 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
57360dc2366fSVenugopal Iyer 	mac_resource_props_t	*mrp = MCIP_RESOURCE_PROPS(mcip);
57370dc2366fSVenugopal Iyer 
57380dc2366fSVenugopal Iyer 	if (rxrings != MAC_RXRINGS_DONTCARE) {
57390dc2366fSVenugopal Iyer 		mrp->mrp_mask |= MRP_RX_RINGS;
57400dc2366fSVenugopal Iyer 		mrp->mrp_nrxrings = rxrings;
57410dc2366fSVenugopal Iyer 	}
57420dc2366fSVenugopal Iyer 
57430dc2366fSVenugopal Iyer 	if (txrings != MAC_TXRINGS_DONTCARE) {
57440dc2366fSVenugopal Iyer 		mrp->mrp_mask |= MRP_TX_RINGS;
57450dc2366fSVenugopal Iyer 		mrp->mrp_ntxrings = txrings;
57460dc2366fSVenugopal Iyer 	}
5747da14cebeSEric Cheng }
574810a40492SRobert Mustacchi 
574910a40492SRobert Mustacchi boolean_t
mac_get_promisc_filtered(mac_client_handle_t mch)575010a40492SRobert Mustacchi mac_get_promisc_filtered(mac_client_handle_t mch)
575110a40492SRobert Mustacchi {
575210a40492SRobert Mustacchi 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
575310a40492SRobert Mustacchi 
575410a40492SRobert Mustacchi 	return (mcip->mci_protect_flags & MPT_FLAG_PROMISC_FILTERED);
575510a40492SRobert Mustacchi }
575610a40492SRobert Mustacchi 
575710a40492SRobert Mustacchi void
mac_set_promisc_filtered(mac_client_handle_t mch,boolean_t enable)575810a40492SRobert Mustacchi mac_set_promisc_filtered(mac_client_handle_t mch, boolean_t enable)
575910a40492SRobert Mustacchi {
576010a40492SRobert Mustacchi 	mac_client_impl_t	*mcip = (mac_client_impl_t *)mch;
576110a40492SRobert Mustacchi 
576210a40492SRobert Mustacchi 	ASSERT(MAC_PERIM_HELD((mac_handle_t)mcip->mci_mip));
576310a40492SRobert Mustacchi 	if (enable)
576410a40492SRobert Mustacchi 		mcip->mci_protect_flags |= MPT_FLAG_PROMISC_FILTERED;
576510a40492SRobert Mustacchi 	else
576610a40492SRobert Mustacchi 		mcip->mci_protect_flags &= ~MPT_FLAG_PROMISC_FILTERED;
576710a40492SRobert Mustacchi }
5768