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