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