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