xref: /titanic_51/usr/src/uts/common/io/dld/dld_str.c (revision d48713b83f032afcef6785303e68f293eacd5671)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Data-Link Driver
30  */
31 
32 #include	<sys/stropts.h>
33 #include	<sys/strsun.h>
34 #include	<sys/strsubr.h>
35 #include	<sys/atomic.h>
36 #include	<sys/mkdev.h>
37 #include	<sys/vlan.h>
38 #include	<sys/dld.h>
39 #include	<sys/dld_impl.h>
40 #include	<sys/dls_impl.h>
41 #include	<inet/common.h>
42 
43 static int	str_constructor(void *, void *, int);
44 static void	str_destructor(void *, void *);
45 static mblk_t	*str_unitdata_ind(dld_str_t *, mblk_t *);
46 static void	str_notify_promisc_on_phys(dld_str_t *);
47 static void	str_notify_promisc_off_phys(dld_str_t *);
48 static void	str_notify_phys_addr(dld_str_t *, const uint8_t *);
49 static void	str_notify_link_up(dld_str_t *);
50 static void	str_notify_link_down(dld_str_t *);
51 static void	str_notify_capab_reneg(dld_str_t *);
52 static void	str_notify_speed(dld_str_t *, uint32_t);
53 static void	str_notify(void *, mac_notify_type_t);
54 
55 static void	ioc_raw(dld_str_t *, mblk_t *);
56 static void	ioc_fast(dld_str_t *,  mblk_t *);
57 static void	ioc(dld_str_t *, mblk_t *);
58 static void	dld_ioc(dld_str_t *, mblk_t *);
59 static minor_t	dld_minor_hold(boolean_t);
60 static void	dld_minor_rele(minor_t);
61 
62 static uint32_t		str_count;
63 static kmem_cache_t	*str_cachep;
64 static vmem_t		*minor_arenap;
65 static uint32_t		minor_count;
66 static mod_hash_t	*str_hashp;
67 
68 #define	MINOR_TO_PTR(minor)	((void *)(uintptr_t)(minor))
69 #define	PTR_TO_MINOR(ptr)	((minor_t)(uintptr_t)(ptr))
70 
71 #define	STR_HASHSZ		64
72 #define	STR_HASH_KEY(key)	((mod_hash_key_t)(uintptr_t)(key))
73 
74 /*
75  * Some notes on entry points, flow-control, queueing and locking:
76  *
77  * This driver exports the traditional STREAMS put entry point as well as
78  * the non-STREAMS fast-path transmit routine which is provided to IP via
79  * the DL_CAPAB_POLL negotiation.  The put procedure handles all control
80  * and data operations, while the fast-path routine deals only with M_DATA
81  * fast-path packets.  Regardless of the entry point, all outbound packets
82  * will end up in str_mdata_fastpath_put(), where they will be delivered to
83  * the MAC driver.
84  *
85  * The transmit logic operates in two modes: a "not busy" mode where the
86  * packets will be delivered to the MAC for a send attempt, or "busy" mode
87  * where they will be enqueued in the internal queue because of flow-control.
88  * Flow-control happens when the MAC driver indicates the packets couldn't
89  * be transmitted due to lack of resources (e.g. running out of descriptors).
90  * In such case, the driver will place a dummy message on its write-side
91  * STREAMS queue so that the queue is marked as "full".  Any subsequent
92  * packets arriving at the driver will be enqueued in the internal queue,
93  * which is drained in the context of the service thread that gets scheduled
94  * whenever the driver is in the "busy" mode.  When all packets have been
95  * successfully delivered by MAC and the internal queue is empty, it will
96  * transition to the "not busy" mode by removing the dummy message from the
97  * write-side STREAMS queue; in effect this will trigger backenabling.
98  * The sizes of q_hiwat and q_lowat are set to 1 and 0, respectively, due
99  * to the above reasons.
100  *
101  * The driver implements an internal transmit queue independent of STREAMS.
102  * This allows for flexibility and provides a fast enqueue/dequeue mechanism
103  * compared to the putq() and get() STREAMS interfaces.  The only putq() and
104  * getq() operations done by the driver are those related to placing and
105  * removing the dummy message to/from the write-side STREAMS queue for flow-
106  * control purposes.
107  *
108  * Locking is done independent of STREAMS due to the driver being fully MT.
109  * Threads entering the driver (either from put or service entry points)
110  * will most likely be readers, with the exception of a few writer cases
111  * such those handling DLPI attach/detach/bind/unbind/etc. or any of the
112  * DLD-related ioctl requests.  The DLPI detach case is special, because
113  * it involves freeing resources and therefore must be single-threaded.
114  * Unfortunately the readers/writers lock can't be used to protect against
115  * it, because the lock is dropped prior to the driver calling places where
116  * putnext() may be invoked, and such places may depend on those resources
117  * to exist.  Because of this, the driver always completes the DLPI detach
118  * process when there are no other threads running in the driver.  This is
119  * done by keeping track of the number of threads, such that the the last
120  * thread leaving the driver will finish the pending DLPI detach operation.
121  */
122 
123 /*
124  * dld_max_q_count is the queue depth threshold used to limit the number of
125  * outstanding packets or bytes allowed in the queue; once this limit is
126  * reached the driver will free any incoming ones until the queue depth
127  * drops below the threshold.
128  *
129  * This buffering is provided to accomodate clients which do not employ
130  * their own buffering scheme, and to handle occasional packet bursts.
131  * Clients which handle their own buffering will receive positive feedback
132  * from this driver as soon as it transitions into the "busy" state, i.e.
133  * when the queue is initially filled up; they will get backenabled once
134  * the queue is empty.
135  *
136  * The value chosen here is rather arbitrary; in future some intelligent
137  * heuristics may be involved which could take into account the hardware's
138  * transmit ring size, etc.
139  */
140 uint_t dld_max_q_count = (16 * 1024 *1024);
141 
142 /*
143  * dld_finddevinfo() returns the dev_info_t * corresponding to a particular
144  * dev_t. It searches str_hashp (a table of dld_str_t's) for streams that
145  * match dev_t. If a stream is found and it is attached, its dev_info_t *
146  * is returned.
147  */
148 typedef struct i_dld_str_state_s {
149 	major_t		ds_major;
150 	minor_t		ds_minor;
151 	dev_info_t	*ds_dip;
152 } i_dld_str_state_t;
153 
154 /* ARGSUSED */
155 static uint_t
156 i_dld_str_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
157 {
158 	i_dld_str_state_t	*statep = arg;
159 	dld_str_t		*dsp = (dld_str_t *)val;
160 
161 	if (statep->ds_major != dsp->ds_major)
162 		return (MH_WALK_CONTINUE);
163 
164 	ASSERT(statep->ds_minor != 0);
165 
166 	/*
167 	 * Access to ds_ppa and ds_mh need to be protected by ds_lock.
168 	 */
169 	rw_enter(&dsp->ds_lock, RW_READER);
170 	if (statep->ds_minor <= DLD_MAX_MINOR) {
171 		/*
172 		 * Style 1: minor can be derived from the ppa. we
173 		 * continue to walk until we find a matching stream
174 		 * in attached state.
175 		 */
176 		if (statep->ds_minor == DLS_PPA2MINOR(dsp->ds_ppa) &&
177 		    dsp->ds_mh != NULL) {
178 			statep->ds_dip = mac_devinfo_get(dsp->ds_mh);
179 			rw_exit(&dsp->ds_lock);
180 			return (MH_WALK_TERMINATE);
181 		}
182 	} else {
183 		/*
184 		 * Clone: a clone minor is unique. we can terminate the
185 		 * walk if we find a matching stream -- even if we fail
186 		 * to obtain the devinfo.
187 		 */
188 		if (statep->ds_minor == dsp->ds_minor) {
189 			if (dsp->ds_mh != NULL)
190 				statep->ds_dip = mac_devinfo_get(dsp->ds_mh);
191 			rw_exit(&dsp->ds_lock);
192 			return (MH_WALK_TERMINATE);
193 		}
194 	}
195 	rw_exit(&dsp->ds_lock);
196 	return (MH_WALK_CONTINUE);
197 }
198 
199 static dev_info_t *
200 dld_finddevinfo(dev_t dev)
201 {
202 	i_dld_str_state_t	state;
203 
204 	state.ds_minor = getminor(dev);
205 	state.ds_major = getmajor(dev);
206 	state.ds_dip = NULL;
207 
208 	if (state.ds_minor == 0)
209 		return (NULL);
210 
211 	mod_hash_walk(str_hashp, i_dld_str_walker, &state);
212 	return (state.ds_dip);
213 }
214 
215 
216 /*
217  * devo_getinfo: getinfo(9e)
218  */
219 /*ARGSUSED*/
220 int
221 dld_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resp)
222 {
223 	dev_info_t	*devinfo;
224 	minor_t		minor = getminor((dev_t)arg);
225 	int		rc = DDI_FAILURE;
226 
227 	switch (cmd) {
228 	case DDI_INFO_DEVT2DEVINFO:
229 		if ((devinfo = dld_finddevinfo((dev_t)arg)) != NULL) {
230 			*(dev_info_t **)resp = devinfo;
231 			rc = DDI_SUCCESS;
232 		}
233 		break;
234 	case DDI_INFO_DEVT2INSTANCE:
235 		if (minor > 0 && minor <= DLD_MAX_MINOR) {
236 			*resp = (void *)(uintptr_t)DLS_MINOR2INST(minor);
237 			rc = DDI_SUCCESS;
238 		} else if (minor > DLD_MAX_MINOR &&
239 		    (devinfo = dld_finddevinfo((dev_t)arg)) != NULL) {
240 			*resp = (void *)(uintptr_t)ddi_get_instance(devinfo);
241 			rc = DDI_SUCCESS;
242 		}
243 		break;
244 	}
245 	return (rc);
246 }
247 
248 /*
249  * qi_qopen: open(9e)
250  */
251 /*ARGSUSED*/
252 int
253 dld_open(queue_t *rq, dev_t *devp, int flag, int sflag, cred_t *credp)
254 {
255 	dld_str_t	*dsp;
256 	major_t		major;
257 	minor_t		minor;
258 	int		err;
259 
260 	if (sflag == MODOPEN)
261 		return (ENOTSUP);
262 
263 	/*
264 	 * This is a cloning driver and therefore each queue should only
265 	 * ever get opened once.
266 	 */
267 	if (rq->q_ptr != NULL)
268 		return (EBUSY);
269 
270 	major = getmajor(*devp);
271 	minor = getminor(*devp);
272 	if (minor > DLD_MAX_MINOR)
273 		return (ENODEV);
274 
275 	/*
276 	 * Create a new dld_str_t for the stream. This will grab a new minor
277 	 * number that will be handed back in the cloned dev_t.  Creation may
278 	 * fail if we can't allocate the dummy mblk used for flow-control.
279 	 */
280 	dsp = dld_str_create(rq, DLD_DLPI, major,
281 	    ((minor == 0) ? DL_STYLE2 : DL_STYLE1));
282 	if (dsp == NULL)
283 		return (ENOSR);
284 
285 	ASSERT(dsp->ds_dlstate == DL_UNATTACHED);
286 	if (minor != 0) {
287 		/*
288 		 * Style 1 open
289 		 */
290 
291 		if ((err = dld_str_attach(dsp, (t_uscalar_t)minor - 1)) != 0)
292 			goto failed;
293 		ASSERT(dsp->ds_dlstate == DL_UNBOUND);
294 	} else {
295 		(void) qassociate(rq, -1);
296 	}
297 
298 	/*
299 	 * Enable the queue srv(9e) routine.
300 	 */
301 	qprocson(rq);
302 
303 	/*
304 	 * Construct a cloned dev_t to hand back.
305 	 */
306 	*devp = makedevice(getmajor(*devp), dsp->ds_minor);
307 	return (0);
308 
309 failed:
310 	dld_str_destroy(dsp);
311 	return (err);
312 }
313 
314 /*
315  * qi_qclose: close(9e)
316  */
317 int
318 dld_close(queue_t *rq)
319 {
320 	dld_str_t	*dsp = rq->q_ptr;
321 
322 	/*
323 	 * Wait until pending requests are processed.
324 	 */
325 	mutex_enter(&dsp->ds_thr_lock);
326 	while (dsp->ds_pending_cnt > 0)
327 		cv_wait(&dsp->ds_pending_cv, &dsp->ds_thr_lock);
328 	mutex_exit(&dsp->ds_thr_lock);
329 
330 	/*
331 	 * Disable the queue srv(9e) routine.
332 	 */
333 	qprocsoff(rq);
334 
335 	/*
336 	 * At this point we can not be entered by any threads via STREAMS
337 	 * or the direct call interface, which is available only to IP.
338 	 * After the interface is unplumbed, IP wouldn't have any reference
339 	 * to this instance, and therefore we are now effectively single
340 	 * threaded and don't require any lock protection.  Flush all
341 	 * pending packets which are sitting in the transmit queue.
342 	 */
343 	ASSERT(dsp->ds_thr == 0);
344 	dld_tx_flush(dsp);
345 
346 	/*
347 	 * This stream was open to a provider node. Check to see
348 	 * if it has been cleanly shut down.
349 	 */
350 	if (dsp->ds_dlstate != DL_UNATTACHED) {
351 		/*
352 		 * The stream is either open to a style 1 provider or
353 		 * this is not clean shutdown. Detach from the PPA.
354 		 * (This is still ok even in the style 1 case).
355 		 */
356 		dld_str_detach(dsp);
357 	}
358 
359 	dld_str_destroy(dsp);
360 	return (0);
361 }
362 
363 /*
364  * qi_qputp: put(9e)
365  */
366 void
367 dld_wput(queue_t *wq, mblk_t *mp)
368 {
369 	dld_str_t *dsp = (dld_str_t *)wq->q_ptr;
370 
371 	DLD_ENTER(dsp);
372 
373 	switch (DB_TYPE(mp)) {
374 	case M_DATA:
375 		rw_enter(&dsp->ds_lock, RW_READER);
376 		if (dsp->ds_dlstate != DL_IDLE ||
377 		    dsp->ds_mode == DLD_UNITDATA) {
378 			freemsg(mp);
379 		} else if (dsp->ds_mode == DLD_FASTPATH) {
380 			str_mdata_fastpath_put(dsp, mp);
381 		} else if (dsp->ds_mode == DLD_RAW) {
382 			str_mdata_raw_put(dsp, mp);
383 		}
384 		rw_exit(&dsp->ds_lock);
385 		break;
386 	case M_PROTO:
387 	case M_PCPROTO:
388 		dld_proto(dsp, mp);
389 		break;
390 	case M_IOCTL:
391 		dld_ioc(dsp, mp);
392 		break;
393 	case M_FLUSH:
394 		if (*mp->b_rptr & FLUSHW) {
395 			dld_tx_flush(dsp);
396 			*mp->b_rptr &= ~FLUSHW;
397 		}
398 
399 		if (*mp->b_rptr & FLUSHR) {
400 			qreply(wq, mp);
401 		} else {
402 			freemsg(mp);
403 		}
404 		break;
405 	default:
406 		freemsg(mp);
407 		break;
408 	}
409 
410 	DLD_EXIT(dsp);
411 }
412 
413 /*
414  * qi_srvp: srv(9e)
415  */
416 void
417 dld_wsrv(queue_t *wq)
418 {
419 	mblk_t		*mp;
420 	dld_str_t	*dsp = wq->q_ptr;
421 
422 	DLD_ENTER(dsp);
423 	rw_enter(&dsp->ds_lock, RW_READER);
424 	/*
425 	 * Grab all packets (chained via b_next) off our transmit queue
426 	 * and try to send them all to the MAC layer.  Since the queue
427 	 * is independent of streams, we are able to dequeue all messages
428 	 * at once without looping through getq() and manually chaining
429 	 * them.  Note that the queue size parameters (byte and message
430 	 * counts) are cleared as well, but we postpone the backenabling
431 	 * until after the MAC transmit since some packets may end up
432 	 * back at our transmit queue.
433 	 */
434 	mutex_enter(&dsp->ds_tx_list_lock);
435 	if ((mp = dsp->ds_tx_list_head) == NULL) {
436 		ASSERT(!dsp->ds_tx_qbusy);
437 		ASSERT(dsp->ds_tx_flow_mp != NULL);
438 		ASSERT(dsp->ds_tx_list_head == NULL);
439 		ASSERT(dsp->ds_tx_list_tail == NULL);
440 		ASSERT(dsp->ds_tx_cnt == 0);
441 		ASSERT(dsp->ds_tx_msgcnt == 0);
442 		mutex_exit(&dsp->ds_tx_list_lock);
443 		goto done;
444 	}
445 	dsp->ds_tx_list_head = dsp->ds_tx_list_tail = NULL;
446 	dsp->ds_tx_cnt = dsp->ds_tx_msgcnt = 0;
447 	mutex_exit(&dsp->ds_tx_list_lock);
448 
449 	/*
450 	 * Discard packets unless we are attached and bound; note that
451 	 * the driver mode (fastpath/raw/unitdata) is irrelevant here,
452 	 * because regardless of the mode all transmit will end up in
453 	 * str_mdata_fastpath_put() where the packets may be queued.
454 	 */
455 	ASSERT(DB_TYPE(mp) == M_DATA);
456 	if (dsp->ds_dlstate != DL_IDLE) {
457 		freemsgchain(mp);
458 		goto done;
459 	}
460 
461 	/*
462 	 * Attempt to transmit one or more packets.  If the MAC can't
463 	 * send them all, re-queue the packet(s) at the beginning of
464 	 * the transmit queue to avoid any re-ordering.
465 	 */
466 	if ((mp = dls_tx(dsp->ds_dc, mp)) != NULL)
467 		dld_tx_enqueue(dsp, mp, B_TRUE);
468 
469 	/*
470 	 * Grab the list lock again and check if the transmit queue is
471 	 * really empty; if so, lift up flow-control and backenable any
472 	 * writer queues.  If the queue is not empty, schedule service
473 	 * thread to drain it.
474 	 */
475 	mutex_enter(&dsp->ds_tx_list_lock);
476 	if (dsp->ds_tx_list_head == NULL) {
477 		dsp->ds_tx_flow_mp = getq(wq);
478 		ASSERT(dsp->ds_tx_flow_mp != NULL);
479 		dsp->ds_tx_qbusy = B_FALSE;
480 	}
481 	mutex_exit(&dsp->ds_tx_list_lock);
482 done:
483 	rw_exit(&dsp->ds_lock);
484 	DLD_EXIT(dsp);
485 }
486 
487 void
488 dld_init_ops(struct dev_ops *ops, const char *name)
489 {
490 	struct streamtab *stream;
491 	struct qinit *rq, *wq;
492 	struct module_info *modinfo;
493 
494 	modinfo = kmem_zalloc(sizeof (struct module_info), KM_SLEEP);
495 	modinfo->mi_idname = kmem_zalloc(FMNAMESZ, KM_SLEEP);
496 	(void) snprintf(modinfo->mi_idname, FMNAMESZ, "%s", name);
497 	modinfo->mi_minpsz = 0;
498 	modinfo->mi_maxpsz = 64*1024;
499 	modinfo->mi_hiwat  = 1;
500 	modinfo->mi_lowat = 0;
501 
502 	rq = kmem_zalloc(sizeof (struct qinit), KM_SLEEP);
503 	rq->qi_qopen = dld_open;
504 	rq->qi_qclose = dld_close;
505 	rq->qi_minfo = modinfo;
506 
507 	wq = kmem_zalloc(sizeof (struct qinit), KM_SLEEP);
508 	wq->qi_putp = (pfi_t)dld_wput;
509 	wq->qi_srvp = (pfi_t)dld_wsrv;
510 	wq->qi_minfo = modinfo;
511 
512 	stream = kmem_zalloc(sizeof (struct streamtab), KM_SLEEP);
513 	stream->st_rdinit = rq;
514 	stream->st_wrinit = wq;
515 	ops->devo_cb_ops->cb_str = stream;
516 
517 	ops->devo_getinfo = &dld_getinfo;
518 }
519 
520 void
521 dld_fini_ops(struct dev_ops *ops)
522 {
523 	struct streamtab *stream;
524 	struct qinit *rq, *wq;
525 	struct module_info *modinfo;
526 
527 	stream = ops->devo_cb_ops->cb_str;
528 	rq = stream->st_rdinit;
529 	wq = stream->st_wrinit;
530 	modinfo = rq->qi_minfo;
531 	ASSERT(wq->qi_minfo == modinfo);
532 
533 	kmem_free(stream, sizeof (struct streamtab));
534 	kmem_free(wq, sizeof (struct qinit));
535 	kmem_free(rq, sizeof (struct qinit));
536 	kmem_free(modinfo->mi_idname, FMNAMESZ);
537 	kmem_free(modinfo, sizeof (struct module_info));
538 }
539 
540 /*
541  * Initialize this module's data structures.
542  */
543 void
544 dld_str_init(void)
545 {
546 	/*
547 	 * Create dld_str_t object cache.
548 	 */
549 	str_cachep = kmem_cache_create("dld_str_cache", sizeof (dld_str_t),
550 	    0, str_constructor, str_destructor, NULL, NULL, NULL, 0);
551 	ASSERT(str_cachep != NULL);
552 
553 	/*
554 	 * Allocate a vmem arena to manage minor numbers. The range of the
555 	 * arena will be from DLD_MAX_MINOR + 1 to MAXMIN (maximum legal
556 	 * minor number).
557 	 */
558 	minor_arenap = vmem_create("dld_minor_arena",
559 	    MINOR_TO_PTR(DLD_MAX_MINOR + 1), MAXMIN, 1, NULL, NULL, NULL, 0,
560 	    VM_SLEEP | VMC_IDENTIFIER);
561 	ASSERT(minor_arenap != NULL);
562 
563 	/*
564 	 * Create a hash table for maintaining dld_str_t's.
565 	 * The ds_minor field (the clone minor number) of a dld_str_t
566 	 * is used as a key for this hash table because this number is
567 	 * globally unique (allocated from "dld_minor_arena").
568 	 */
569 	str_hashp = mod_hash_create_idhash("dld_str_hash", STR_HASHSZ,
570 	    mod_hash_null_valdtor);
571 }
572 
573 /*
574  * Tear down this module's data structures.
575  */
576 int
577 dld_str_fini(void)
578 {
579 	/*
580 	 * Make sure that there are no objects in use.
581 	 */
582 	if (str_count != 0)
583 		return (EBUSY);
584 
585 	/*
586 	 * Check to see if there are any minor numbers still in use.
587 	 */
588 	if (minor_count != 0)
589 		return (EBUSY);
590 
591 	/*
592 	 * Destroy object cache.
593 	 */
594 	kmem_cache_destroy(str_cachep);
595 	vmem_destroy(minor_arenap);
596 	mod_hash_destroy_idhash(str_hashp);
597 	return (0);
598 }
599 
600 /*
601  * Create a new dld_str_t object.
602  */
603 dld_str_t *
604 dld_str_create(queue_t *rq, uint_t type, major_t major, t_uscalar_t style)
605 {
606 	dld_str_t	*dsp;
607 	int		err;
608 
609 	/*
610 	 * Allocate an object from the cache.
611 	 */
612 	atomic_add_32(&str_count, 1);
613 	dsp = kmem_cache_alloc(str_cachep, KM_SLEEP);
614 
615 	/*
616 	 * Allocate the dummy mblk for flow-control.
617 	 */
618 	dsp->ds_tx_flow_mp = allocb(1, BPRI_HI);
619 	if (dsp->ds_tx_flow_mp == NULL) {
620 		kmem_cache_free(str_cachep, dsp);
621 		atomic_add_32(&str_count, -1);
622 		return (NULL);
623 	}
624 	dsp->ds_type = type;
625 	dsp->ds_major = major;
626 	dsp->ds_style = style;
627 
628 	/*
629 	 * Initialize the queue pointers.
630 	 */
631 	ASSERT(RD(rq) == rq);
632 	dsp->ds_rq = rq;
633 	dsp->ds_wq = WR(rq);
634 	rq->q_ptr = WR(rq)->q_ptr = (void *)dsp;
635 
636 	/*
637 	 * We want explicit control over our write-side STREAMS queue
638 	 * where the dummy mblk gets added/removed for flow-control.
639 	 */
640 	noenable(WR(rq));
641 
642 	err = mod_hash_insert(str_hashp, STR_HASH_KEY(dsp->ds_minor),
643 	    (mod_hash_val_t)dsp);
644 	ASSERT(err == 0);
645 	return (dsp);
646 }
647 
648 /*
649  * Destroy a dld_str_t object.
650  */
651 void
652 dld_str_destroy(dld_str_t *dsp)
653 {
654 	queue_t		*rq;
655 	queue_t		*wq;
656 	mod_hash_val_t	val;
657 	/*
658 	 * Clear the queue pointers.
659 	 */
660 	rq = dsp->ds_rq;
661 	wq = dsp->ds_wq;
662 	ASSERT(wq == WR(rq));
663 
664 	rq->q_ptr = wq->q_ptr = NULL;
665 	dsp->ds_rq = dsp->ds_wq = NULL;
666 
667 	ASSERT(!RW_LOCK_HELD(&dsp->ds_lock));
668 	ASSERT(MUTEX_NOT_HELD(&dsp->ds_tx_list_lock));
669 	ASSERT(dsp->ds_tx_list_head == NULL);
670 	ASSERT(dsp->ds_tx_list_tail == NULL);
671 	ASSERT(dsp->ds_tx_cnt == 0);
672 	ASSERT(dsp->ds_tx_msgcnt == 0);
673 	ASSERT(!dsp->ds_tx_qbusy);
674 
675 	ASSERT(MUTEX_NOT_HELD(&dsp->ds_thr_lock));
676 	ASSERT(dsp->ds_thr == 0);
677 	ASSERT(dsp->ds_pending_req == NULL);
678 
679 	/*
680 	 * Reinitialize all the flags.
681 	 */
682 	dsp->ds_notifications = 0;
683 	dsp->ds_passivestate = DLD_UNINITIALIZED;
684 	dsp->ds_mode = DLD_UNITDATA;
685 
686 	/*
687 	 * Free the dummy mblk if exists.
688 	 */
689 	if (dsp->ds_tx_flow_mp != NULL) {
690 		freeb(dsp->ds_tx_flow_mp);
691 		dsp->ds_tx_flow_mp = NULL;
692 	}
693 
694 	(void) mod_hash_remove(str_hashp, STR_HASH_KEY(dsp->ds_minor), &val);
695 	ASSERT(dsp == (dld_str_t *)val);
696 
697 	/*
698 	 * Free the object back to the cache.
699 	 */
700 	kmem_cache_free(str_cachep, dsp);
701 	atomic_add_32(&str_count, -1);
702 }
703 
704 /*
705  * kmem_cache contructor function: see kmem_cache_create(9f).
706  */
707 /*ARGSUSED*/
708 static int
709 str_constructor(void *buf, void *cdrarg, int kmflags)
710 {
711 	dld_str_t	*dsp = buf;
712 
713 	bzero(buf, sizeof (dld_str_t));
714 
715 	/*
716 	 * Allocate a new minor number.
717 	 */
718 	if ((dsp->ds_minor = dld_minor_hold(kmflags == KM_SLEEP)) == 0)
719 		return (-1);
720 
721 	/*
722 	 * Initialize the DLPI state machine.
723 	 */
724 	dsp->ds_dlstate = DL_UNATTACHED;
725 	dsp->ds_ppa = (t_uscalar_t)-1;
726 
727 	mutex_init(&dsp->ds_thr_lock, NULL, MUTEX_DRIVER, NULL);
728 	rw_init(&dsp->ds_lock, NULL, RW_DRIVER, NULL);
729 	mutex_init(&dsp->ds_tx_list_lock, NULL, MUTEX_DRIVER, NULL);
730 	cv_init(&dsp->ds_pending_cv, NULL, CV_DRIVER, NULL);
731 
732 	return (0);
733 }
734 
735 /*
736  * kmem_cache destructor function.
737  */
738 /*ARGSUSED*/
739 static void
740 str_destructor(void *buf, void *cdrarg)
741 {
742 	dld_str_t	*dsp = buf;
743 
744 	/*
745 	 * Make sure the DLPI state machine was reset.
746 	 */
747 	ASSERT(dsp->ds_dlstate == DL_UNATTACHED);
748 
749 	/*
750 	 * Make sure the data-link interface was closed.
751 	 */
752 	ASSERT(dsp->ds_mh == NULL);
753 	ASSERT(dsp->ds_dc == NULL);
754 
755 	/*
756 	 * Make sure enabled notifications are cleared.
757 	 */
758 	ASSERT(dsp->ds_notifications == 0);
759 
760 	/*
761 	 * Make sure polling is disabled.
762 	 */
763 	ASSERT(!dsp->ds_polling);
764 
765 	/*
766 	 * Release the minor number.
767 	 */
768 	dld_minor_rele(dsp->ds_minor);
769 
770 	ASSERT(!RW_LOCK_HELD(&dsp->ds_lock));
771 	rw_destroy(&dsp->ds_lock);
772 
773 	ASSERT(MUTEX_NOT_HELD(&dsp->ds_tx_list_lock));
774 	mutex_destroy(&dsp->ds_tx_list_lock);
775 	ASSERT(dsp->ds_tx_flow_mp == NULL);
776 
777 	ASSERT(MUTEX_NOT_HELD(&dsp->ds_thr_lock));
778 	mutex_destroy(&dsp->ds_thr_lock);
779 	ASSERT(dsp->ds_pending_req == NULL);
780 	ASSERT(dsp->ds_pending_op == NULL);
781 	ASSERT(dsp->ds_pending_cnt == 0);
782 	cv_destroy(&dsp->ds_pending_cv);
783 }
784 
785 /*
786  * M_DATA put (IP fast-path mode)
787  */
788 void
789 str_mdata_fastpath_put(dld_str_t *dsp, mblk_t *mp)
790 {
791 	/*
792 	 * This function can be called from within dld or from an upper
793 	 * layer protocol (currently only tcp). If we are in the busy
794 	 * mode enqueue the packet(s) and return.  Otherwise hand them
795 	 * over to the MAC driver for transmission; any remaining one(s)
796 	 * which didn't get sent will be queued.
797 	 *
798 	 * Note here that we don't grab the list lock prior to checking
799 	 * the busy flag.  This is okay, because a missed transition
800 	 * will not cause any packet reordering for any particular TCP
801 	 * connection (which is single-threaded).  The enqueue routine
802 	 * will atomically set the busy flag and schedule the service
803 	 * thread to run; the flag is only cleared by the service thread
804 	 * when there is no more packet to be transmitted.
805 	 */
806 	if (dsp->ds_tx_qbusy || (mp = dls_tx(dsp->ds_dc, mp)) != NULL)
807 		dld_tx_enqueue(dsp, mp, B_FALSE);
808 }
809 
810 /*
811  * M_DATA put (raw mode)
812  */
813 void
814 str_mdata_raw_put(dld_str_t *dsp, mblk_t *mp)
815 {
816 	struct ether_header	*ehp;
817 	mblk_t			*bp;
818 	size_t			size;
819 	size_t			hdrlen;
820 
821 	size = MBLKL(mp);
822 	if (size < sizeof (struct ether_header))
823 		goto discard;
824 
825 	hdrlen = sizeof (struct ether_header);
826 
827 	ehp = (struct ether_header *)mp->b_rptr;
828 	if (ntohs(ehp->ether_type) == VLAN_TPID) {
829 		struct ether_vlan_header	*evhp;
830 
831 		if (size < sizeof (struct ether_vlan_header))
832 			goto discard;
833 
834 		/*
835 		 * Replace vtag with our own
836 		 */
837 		evhp = (struct ether_vlan_header *)ehp;
838 		evhp->ether_tci = htons(VLAN_TCI(dsp->ds_pri,
839 		    ETHER_CFI, dsp->ds_vid));
840 		hdrlen = sizeof (struct ether_vlan_header);
841 	}
842 
843 	/*
844 	 * Check the packet is not too big and that any remaining
845 	 * fragment list is composed entirely of M_DATA messages. (We
846 	 * know the first fragment was M_DATA otherwise we could not
847 	 * have got here).
848 	 */
849 	for (bp = mp->b_cont; bp != NULL; bp = bp->b_cont) {
850 		if (DB_TYPE(bp) != M_DATA)
851 			goto discard;
852 		size += MBLKL(bp);
853 	}
854 
855 	if (size > dsp->ds_mip->mi_sdu_max + hdrlen)
856 		goto discard;
857 
858 	str_mdata_fastpath_put(dsp, mp);
859 	return;
860 
861 discard:
862 	freemsg(mp);
863 }
864 
865 /*
866  * Process DL_ATTACH_REQ (style 2) or open(2) (style 1).
867  */
868 int
869 dld_str_attach(dld_str_t *dsp, t_uscalar_t ppa)
870 {
871 	int			err;
872 	const char		*drvname;
873 	char			name[MAXNAMELEN];
874 	dls_channel_t		dc;
875 	uint_t			addr_length;
876 
877 	ASSERT(dsp->ds_dc == NULL);
878 
879 	if ((drvname = ddi_major_to_name(dsp->ds_major)) == NULL)
880 		return (EINVAL);
881 
882 	(void) snprintf(name, MAXNAMELEN, "%s%u", drvname, ppa);
883 
884 	if (strcmp(drvname, "aggr") != 0 &&
885 	    qassociate(dsp->ds_wq, DLS_PPA2INST(ppa)) != 0)
886 		return (EINVAL);
887 
888 	/*
889 	 * Open a channel.
890 	 */
891 	if ((err = dls_open(name, &dc)) != 0) {
892 		(void) qassociate(dsp->ds_wq, -1);
893 		return (err);
894 	}
895 
896 	/*
897 	 * Cache the MAC interface handle, a pointer to the immutable MAC
898 	 * information and the current and 'factory' MAC address.
899 	 */
900 	dsp->ds_mh = dls_mac(dc);
901 	dsp->ds_mip = mac_info(dsp->ds_mh);
902 
903 	mac_unicst_get(dsp->ds_mh, dsp->ds_curr_addr);
904 
905 	addr_length = dsp->ds_mip->mi_addr_length;
906 	bcopy(dsp->ds_mip->mi_unicst_addr, dsp->ds_fact_addr, addr_length);
907 
908 	/*
909 	 * Cache the interface VLAN identifier. (This will be VLAN_ID_NONE for
910 	 * a non-VLAN interface).
911 	 */
912 	dsp->ds_vid = dls_vid(dc);
913 
914 	/*
915 	 * Set the default packet priority.
916 	 */
917 	dsp->ds_pri = 0;
918 
919 	/*
920 	 * Add a notify function so that the we get updates from the MAC.
921 	 */
922 	dsp->ds_mnh = mac_notify_add(dsp->ds_mh, str_notify, (void *)dsp);
923 
924 	dsp->ds_ppa = ppa;
925 	dsp->ds_dc = dc;
926 	dsp->ds_dlstate = DL_UNBOUND;
927 
928 	return (0);
929 }
930 
931 /*
932  * Process DL_DETACH_REQ (style 2) or close(2) (style 1). Can also be called
933  * from close(2) for style 2.
934  */
935 void
936 dld_str_detach(dld_str_t *dsp)
937 {
938 	ASSERT(dsp->ds_thr == 0);
939 
940 	/*
941 	 * Remove the notify function.
942 	 */
943 	mac_notify_remove(dsp->ds_mh, dsp->ds_mnh);
944 
945 	/*
946 	 * Clear the polling and promisc flags.
947 	 */
948 	dsp->ds_polling = B_FALSE;
949 	dsp->ds_soft_ring = B_FALSE;
950 	dsp->ds_promisc = 0;
951 
952 	/*
953 	 * Close the channel.
954 	 */
955 	dls_close(dsp->ds_dc);
956 	dsp->ds_ppa = (t_uscalar_t)-1;
957 	dsp->ds_dc = NULL;
958 	dsp->ds_mh = NULL;
959 
960 	(void) qassociate(dsp->ds_wq, -1);
961 
962 	/*
963 	 * Re-initialize the DLPI state machine.
964 	 */
965 	dsp->ds_dlstate = DL_UNATTACHED;
966 
967 }
968 
969 /*
970  * Raw mode receive function.
971  */
972 /*ARGSUSED*/
973 void
974 dld_str_rx_raw(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
975     size_t header_length)
976 {
977 	dld_str_t		*dsp = (dld_str_t *)arg;
978 	mblk_t			*next;
979 
980 	ASSERT(mp != NULL);
981 	do {
982 		/*
983 		 * Get the pointer to the next packet in the chain and then
984 		 * clear b_next before the packet gets passed on.
985 		 */
986 		next = mp->b_next;
987 		mp->b_next = NULL;
988 
989 		/*
990 		 * Wind back b_rptr to point at the MAC header.
991 		 */
992 		ASSERT(mp->b_rptr >= DB_BASE(mp) + header_length);
993 		mp->b_rptr -= header_length;
994 		if (header_length == sizeof (struct ether_vlan_header)) {
995 			/*
996 			 * Strip off the vtag
997 			 */
998 			ovbcopy(mp->b_rptr, mp->b_rptr + VLAN_TAGSZ,
999 			    2 * ETHERADDRL);
1000 			mp->b_rptr += VLAN_TAGSZ;
1001 		}
1002 
1003 		/*
1004 		 * Pass the packet on.
1005 		 */
1006 		if (canputnext(dsp->ds_rq))
1007 			putnext(dsp->ds_rq, mp);
1008 		else
1009 			freemsg(mp);
1010 
1011 		/*
1012 		 * Move on to the next packet in the chain.
1013 		 */
1014 		mp = next;
1015 	} while (mp != NULL);
1016 }
1017 
1018 /*
1019  * Fast-path receive function.
1020  */
1021 /*ARGSUSED*/
1022 void
1023 dld_str_rx_fastpath(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
1024     size_t header_length)
1025 {
1026 	dld_str_t		*dsp = (dld_str_t *)arg;
1027 	mblk_t			*next;
1028 
1029 	ASSERT(mp != NULL);
1030 	do {
1031 		/*
1032 		 * Get the pointer to the next packet in the chain and then
1033 		 * clear b_next before the packet gets passed on.
1034 		 */
1035 		next = mp->b_next;
1036 		mp->b_next = NULL;
1037 
1038 		/*
1039 		 * Pass the packet on.
1040 		 */
1041 		if (canputnext(dsp->ds_rq))
1042 			putnext(dsp->ds_rq, mp);
1043 		else
1044 			freemsg(mp);
1045 		/*
1046 		 * Move on to the next packet in the chain.
1047 		 */
1048 		mp = next;
1049 	} while (mp != NULL);
1050 }
1051 
1052 /*
1053  * Default receive function (send DL_UNITDATA_IND messages).
1054  */
1055 /*ARGSUSED*/
1056 void
1057 dld_str_rx_unitdata(void *arg, mac_resource_handle_t mrh, mblk_t *mp,
1058     size_t header_length)
1059 {
1060 	dld_str_t		*dsp = (dld_str_t *)arg;
1061 	mblk_t			*ud_mp;
1062 	mblk_t			*next;
1063 
1064 	ASSERT(mp != NULL);
1065 	do {
1066 		/*
1067 		 * Get the pointer to the next packet in the chain and then
1068 		 * clear b_next before the packet gets passed on.
1069 		 */
1070 		next = mp->b_next;
1071 		mp->b_next = NULL;
1072 
1073 		/*
1074 		 * Wind back b_rptr to point at the MAC header.
1075 		 */
1076 		ASSERT(mp->b_rptr >= DB_BASE(mp) + header_length);
1077 		mp->b_rptr -= header_length;
1078 
1079 		/*
1080 		 * Create the DL_UNITDATA_IND M_PROTO.
1081 		 */
1082 		if ((ud_mp = str_unitdata_ind(dsp, mp)) == NULL) {
1083 			freemsgchain(mp);
1084 			return;
1085 		}
1086 
1087 		/*
1088 		 * Advance b_rptr to point at the payload again.
1089 		 */
1090 		mp->b_rptr += header_length;
1091 
1092 		/*
1093 		 * Prepend the DL_UNITDATA_IND.
1094 		 */
1095 		ud_mp->b_cont = mp;
1096 
1097 		/*
1098 		 * Send the message.
1099 		 */
1100 		if (canputnext(dsp->ds_rq))
1101 			putnext(dsp->ds_rq, ud_mp);
1102 		else
1103 			freemsg(ud_mp);
1104 
1105 		/*
1106 		 * Move on to the next packet in the chain.
1107 		 */
1108 		mp = next;
1109 	} while (mp != NULL);
1110 }
1111 
1112 /*
1113  * Generate DL_NOTIFY_IND messages to notify the DLPI consumer of the
1114  * current state of the interface.
1115  */
1116 void
1117 dld_str_notify_ind(dld_str_t *dsp)
1118 {
1119 	mac_notify_type_t	type;
1120 
1121 	for (type = 0; type < MAC_NNOTE; type++)
1122 		str_notify(dsp, type);
1123 }
1124 
1125 typedef struct dl_unitdata_ind_wrapper {
1126 	dl_unitdata_ind_t	dl_unitdata;
1127 	uint8_t			dl_dest_addr[MAXADDRLEN + sizeof (uint16_t)];
1128 	uint8_t			dl_src_addr[MAXADDRLEN + sizeof (uint16_t)];
1129 } dl_unitdata_ind_wrapper_t;
1130 
1131 /*
1132  * Create a DL_UNITDATA_IND M_PROTO message.
1133  */
1134 static mblk_t *
1135 str_unitdata_ind(dld_str_t *dsp, mblk_t *mp)
1136 {
1137 	mblk_t				*nmp;
1138 	dl_unitdata_ind_wrapper_t	*dlwp;
1139 	dl_unitdata_ind_t		*dlp;
1140 	dls_header_info_t		dhi;
1141 	uint_t				addr_length;
1142 	uint8_t				*daddr;
1143 	uint8_t				*saddr;
1144 
1145 	/*
1146 	 * Get the packet header information.
1147 	 */
1148 	dls_header_info(dsp->ds_dc, mp, &dhi);
1149 
1150 	/*
1151 	 * Allocate a message large enough to contain the wrapper structure
1152 	 * defined above.
1153 	 */
1154 	if ((nmp = mexchange(dsp->ds_wq, NULL,
1155 	    sizeof (dl_unitdata_ind_wrapper_t), M_PROTO,
1156 	    DL_UNITDATA_IND)) == NULL)
1157 		return (NULL);
1158 
1159 	dlwp = (dl_unitdata_ind_wrapper_t *)nmp->b_rptr;
1160 
1161 	dlp = &(dlwp->dl_unitdata);
1162 	ASSERT(dlp == (dl_unitdata_ind_t *)nmp->b_rptr);
1163 	ASSERT(dlp->dl_primitive == DL_UNITDATA_IND);
1164 
1165 	/*
1166 	 * Copy in the destination address.
1167 	 */
1168 	addr_length = dsp->ds_mip->mi_addr_length;
1169 	daddr = dlwp->dl_dest_addr;
1170 	dlp->dl_dest_addr_offset = (uintptr_t)daddr - (uintptr_t)dlp;
1171 	bcopy(dhi.dhi_daddr, daddr, addr_length);
1172 
1173 	/*
1174 	 * Set the destination DLSAP to our bound DLSAP value.
1175 	 */
1176 	*(uint16_t *)(daddr + addr_length) = dsp->ds_sap;
1177 	dlp->dl_dest_addr_length = addr_length + sizeof (uint16_t);
1178 
1179 	/*
1180 	 * If the destination address was a group address then
1181 	 * dl_group_address field should be non-zero.
1182 	 */
1183 	dlp->dl_group_address = dhi.dhi_isgroup;
1184 
1185 	/*
1186 	 * Copy in the source address.
1187 	 */
1188 	saddr = dlwp->dl_src_addr;
1189 	dlp->dl_src_addr_offset = (uintptr_t)saddr - (uintptr_t)dlp;
1190 	bcopy(dhi.dhi_saddr, saddr, addr_length);
1191 
1192 	/*
1193 	 * Set the source DLSAP to the packet ethertype.
1194 	 */
1195 	*(uint16_t *)(saddr + addr_length) = dhi.dhi_ethertype;
1196 	dlp->dl_src_addr_length = addr_length + sizeof (uint16_t);
1197 
1198 	return (nmp);
1199 }
1200 
1201 /*
1202  * DL_NOTIFY_IND: DL_NOTE_PROMISC_ON_PHYS
1203  */
1204 static void
1205 str_notify_promisc_on_phys(dld_str_t *dsp)
1206 {
1207 	mblk_t		*mp;
1208 	dl_notify_ind_t	*dlip;
1209 
1210 	if (!(dsp->ds_notifications & DL_NOTE_PROMISC_ON_PHYS))
1211 		return;
1212 
1213 	if ((mp = mexchange(dsp->ds_wq, NULL, sizeof (dl_notify_ind_t),
1214 	    M_PROTO, 0)) == NULL)
1215 		return;
1216 
1217 	bzero(mp->b_rptr, sizeof (dl_notify_ind_t));
1218 	dlip = (dl_notify_ind_t *)mp->b_rptr;
1219 	dlip->dl_primitive = DL_NOTIFY_IND;
1220 	dlip->dl_notification = DL_NOTE_PROMISC_ON_PHYS;
1221 
1222 	qreply(dsp->ds_wq, mp);
1223 }
1224 
1225 /*
1226  * DL_NOTIFY_IND: DL_NOTE_PROMISC_OFF_PHYS
1227  */
1228 static void
1229 str_notify_promisc_off_phys(dld_str_t *dsp)
1230 {
1231 	mblk_t		*mp;
1232 	dl_notify_ind_t	*dlip;
1233 
1234 	if (!(dsp->ds_notifications & DL_NOTE_PROMISC_OFF_PHYS))
1235 		return;
1236 
1237 	if ((mp = mexchange(dsp->ds_wq, NULL, sizeof (dl_notify_ind_t),
1238 	    M_PROTO, 0)) == NULL)
1239 		return;
1240 
1241 	bzero(mp->b_rptr, sizeof (dl_notify_ind_t));
1242 	dlip = (dl_notify_ind_t *)mp->b_rptr;
1243 	dlip->dl_primitive = DL_NOTIFY_IND;
1244 	dlip->dl_notification = DL_NOTE_PROMISC_OFF_PHYS;
1245 
1246 	qreply(dsp->ds_wq, mp);
1247 }
1248 
1249 /*
1250  * DL_NOTIFY_IND: DL_NOTE_PHYS_ADDR
1251  */
1252 static void
1253 str_notify_phys_addr(dld_str_t *dsp, const uint8_t *addr)
1254 {
1255 	mblk_t		*mp;
1256 	dl_notify_ind_t	*dlip;
1257 	uint_t		addr_length;
1258 	uint16_t	ethertype;
1259 
1260 	if (!(dsp->ds_notifications & DL_NOTE_PHYS_ADDR))
1261 		return;
1262 
1263 	addr_length = dsp->ds_mip->mi_addr_length;
1264 	if ((mp = mexchange(dsp->ds_wq, NULL,
1265 	    sizeof (dl_notify_ind_t) + addr_length + sizeof (uint16_t),
1266 	    M_PROTO, 0)) == NULL)
1267 		return;
1268 
1269 	bzero(mp->b_rptr, sizeof (dl_notify_ind_t));
1270 	dlip = (dl_notify_ind_t *)mp->b_rptr;
1271 	dlip->dl_primitive = DL_NOTIFY_IND;
1272 	dlip->dl_notification = DL_NOTE_PHYS_ADDR;
1273 	dlip->dl_data = DL_CURR_PHYS_ADDR;
1274 	dlip->dl_addr_offset = sizeof (dl_notify_ind_t);
1275 	dlip->dl_addr_length = addr_length + sizeof (uint16_t);
1276 
1277 	bcopy(addr, &dlip[1], addr_length);
1278 
1279 	ethertype = (dsp->ds_sap < ETHERTYPE_802_MIN) ? 0 : dsp->ds_sap;
1280 	*(uint16_t *)((uchar_t *)(dlip + 1) + addr_length) =
1281 		ethertype;
1282 
1283 	qreply(dsp->ds_wq, mp);
1284 }
1285 
1286 /*
1287  * DL_NOTIFY_IND: DL_NOTE_LINK_UP
1288  */
1289 static void
1290 str_notify_link_up(dld_str_t *dsp)
1291 {
1292 	mblk_t		*mp;
1293 	dl_notify_ind_t	*dlip;
1294 
1295 	if (!(dsp->ds_notifications & DL_NOTE_LINK_UP))
1296 		return;
1297 
1298 	if ((mp = mexchange(dsp->ds_wq, NULL, sizeof (dl_notify_ind_t),
1299 	    M_PROTO, 0)) == NULL)
1300 		return;
1301 
1302 	bzero(mp->b_rptr, sizeof (dl_notify_ind_t));
1303 	dlip = (dl_notify_ind_t *)mp->b_rptr;
1304 	dlip->dl_primitive = DL_NOTIFY_IND;
1305 	dlip->dl_notification = DL_NOTE_LINK_UP;
1306 
1307 	qreply(dsp->ds_wq, mp);
1308 }
1309 
1310 /*
1311  * DL_NOTIFY_IND: DL_NOTE_LINK_DOWN
1312  */
1313 static void
1314 str_notify_link_down(dld_str_t *dsp)
1315 {
1316 	mblk_t		*mp;
1317 	dl_notify_ind_t	*dlip;
1318 
1319 	if (!(dsp->ds_notifications & DL_NOTE_LINK_DOWN))
1320 		return;
1321 
1322 	if ((mp = mexchange(dsp->ds_wq, NULL, sizeof (dl_notify_ind_t),
1323 	    M_PROTO, 0)) == NULL)
1324 		return;
1325 
1326 	bzero(mp->b_rptr, sizeof (dl_notify_ind_t));
1327 	dlip = (dl_notify_ind_t *)mp->b_rptr;
1328 	dlip->dl_primitive = DL_NOTIFY_IND;
1329 	dlip->dl_notification = DL_NOTE_LINK_DOWN;
1330 
1331 	qreply(dsp->ds_wq, mp);
1332 }
1333 
1334 /*
1335  * DL_NOTIFY_IND: DL_NOTE_SPEED
1336  */
1337 static void
1338 str_notify_speed(dld_str_t *dsp, uint32_t speed)
1339 {
1340 	mblk_t		*mp;
1341 	dl_notify_ind_t	*dlip;
1342 
1343 	if (!(dsp->ds_notifications & DL_NOTE_SPEED))
1344 		return;
1345 
1346 	if ((mp = mexchange(dsp->ds_wq, NULL, sizeof (dl_notify_ind_t),
1347 	    M_PROTO, 0)) == NULL)
1348 		return;
1349 
1350 	bzero(mp->b_rptr, sizeof (dl_notify_ind_t));
1351 	dlip = (dl_notify_ind_t *)mp->b_rptr;
1352 	dlip->dl_primitive = DL_NOTIFY_IND;
1353 	dlip->dl_notification = DL_NOTE_SPEED;
1354 	dlip->dl_data = speed;
1355 
1356 	qreply(dsp->ds_wq, mp);
1357 }
1358 
1359 /*
1360  * DL_NOTIFY_IND: DL_NOTE_CAPAB_RENEG
1361  */
1362 static void
1363 str_notify_capab_reneg(dld_str_t *dsp)
1364 {
1365 	mblk_t		*mp;
1366 	dl_notify_ind_t	*dlip;
1367 
1368 	if (!(dsp->ds_notifications & DL_NOTE_CAPAB_RENEG))
1369 		return;
1370 
1371 	if ((mp = mexchange(dsp->ds_wq, NULL, sizeof (dl_notify_ind_t),
1372 	    M_PROTO, 0)) == NULL)
1373 		return;
1374 
1375 	bzero(mp->b_rptr, sizeof (dl_notify_ind_t));
1376 	dlip = (dl_notify_ind_t *)mp->b_rptr;
1377 	dlip->dl_primitive = DL_NOTIFY_IND;
1378 	dlip->dl_notification = DL_NOTE_CAPAB_RENEG;
1379 
1380 	qreply(dsp->ds_wq, mp);
1381 }
1382 
1383 /*
1384  * MAC notification callback.
1385  */
1386 static void
1387 str_notify(void *arg, mac_notify_type_t type)
1388 {
1389 	dld_str_t		*dsp = (dld_str_t *)arg;
1390 	queue_t			*q = dsp->ds_wq;
1391 
1392 	switch (type) {
1393 	case MAC_NOTE_TX:
1394 		qenable(q);
1395 		break;
1396 
1397 	case MAC_NOTE_DEVPROMISC:
1398 		/*
1399 		 * Send the appropriate DL_NOTIFY_IND.
1400 		 */
1401 		if (mac_promisc_get(dsp->ds_mh, MAC_DEVPROMISC))
1402 			str_notify_promisc_on_phys(dsp);
1403 		else
1404 			str_notify_promisc_off_phys(dsp);
1405 		break;
1406 
1407 	case MAC_NOTE_PROMISC:
1408 		break;
1409 
1410 	case MAC_NOTE_UNICST:
1411 		/*
1412 		 * This notification is sent whenever the MAC unicast address
1413 		 * changes. We need to re-cache the address.
1414 		 */
1415 		mac_unicst_get(dsp->ds_mh, dsp->ds_curr_addr);
1416 
1417 		/*
1418 		 * Send the appropriate DL_NOTIFY_IND.
1419 		 */
1420 		str_notify_phys_addr(dsp, dsp->ds_curr_addr);
1421 		break;
1422 
1423 	case MAC_NOTE_LINK:
1424 		/*
1425 		 * This notification is sent every time the MAC driver
1426 		 * updates the link state.
1427 		 */
1428 		switch (mac_link_get(dsp->ds_mh)) {
1429 		case LINK_STATE_UP:
1430 			/*
1431 			 * The link is up so send the appropriate
1432 			 * DL_NOTIFY_IND.
1433 			 */
1434 			str_notify_link_up(dsp);
1435 
1436 			/*
1437 			 * If we can find the link speed then send a
1438 			 * DL_NOTIFY_IND for that too.
1439 			 */
1440 			if (dsp->ds_mip->mi_stat[MAC_STAT_IFSPEED]) {
1441 				uint64_t	val;
1442 
1443 				val = mac_stat_get(dsp->ds_mh,
1444 				    MAC_STAT_IFSPEED);
1445 				str_notify_speed(dsp,
1446 				    (uint32_t)(val / 1000ull));
1447 			}
1448 			break;
1449 
1450 		case LINK_STATE_DOWN:
1451 			/*
1452 			 * The link is down so send the appropriate
1453 			 * DL_NOTIFY_IND.
1454 			 */
1455 			str_notify_link_down(dsp);
1456 			break;
1457 
1458 		default:
1459 			break;
1460 		}
1461 		break;
1462 
1463 	case MAC_NOTE_RESOURCE:
1464 		/*
1465 		 * This notification is sent whenever the MAC resources
1466 		 * change. We need to renegotiate the capabilities.
1467 		 * Send the appropriate DL_NOTIFY_IND.
1468 		 */
1469 		str_notify_capab_reneg(dsp);
1470 		break;
1471 
1472 	default:
1473 		ASSERT(B_FALSE);
1474 		break;
1475 	}
1476 }
1477 
1478 /*
1479  * Enqueue one or more messages to the transmit queue.
1480  * Caller specifies the insertion position (head/tail).
1481  */
1482 void
1483 dld_tx_enqueue(dld_str_t *dsp, mblk_t *mp, boolean_t head_insert)
1484 {
1485 	mblk_t	*tail;
1486 	queue_t *q = dsp->ds_wq;
1487 	uint_t	cnt, msgcnt;
1488 	uint_t	tot_cnt, tot_msgcnt;
1489 
1490 	ASSERT(DB_TYPE(mp) == M_DATA);
1491 	/* Calculate total size and count of the packet(s) */
1492 	for (tail = mp, cnt = msgdsize(mp), msgcnt = 1;
1493 	    tail->b_next != NULL; tail = tail->b_next) {
1494 		ASSERT(DB_TYPE(tail) == M_DATA);
1495 		cnt += msgdsize(tail);
1496 		msgcnt++;
1497 	}
1498 
1499 	mutex_enter(&dsp->ds_tx_list_lock);
1500 	/*
1501 	 * If the queue depth would exceed the allowed threshold, drop
1502 	 * new packet(s) and drain those already in the queue.
1503 	 */
1504 	tot_cnt = dsp->ds_tx_cnt + cnt;
1505 	tot_msgcnt = dsp->ds_tx_msgcnt + msgcnt;
1506 
1507 	if (!head_insert &&
1508 	    (tot_cnt >= dld_max_q_count || tot_msgcnt >= dld_max_q_count)) {
1509 		ASSERT(dsp->ds_tx_qbusy);
1510 		mutex_exit(&dsp->ds_tx_list_lock);
1511 		freemsgchain(mp);
1512 		goto done;
1513 	}
1514 
1515 	/* Update the queue size parameters */
1516 	dsp->ds_tx_cnt = tot_cnt;
1517 	dsp->ds_tx_msgcnt = tot_msgcnt;
1518 
1519 	/*
1520 	 * If the transmit queue is currently empty and we are
1521 	 * about to deposit the packet(s) there, switch mode to
1522 	 * "busy" and raise flow-control condition.
1523 	 */
1524 	if (!dsp->ds_tx_qbusy) {
1525 		dsp->ds_tx_qbusy = B_TRUE;
1526 		ASSERT(dsp->ds_tx_flow_mp != NULL);
1527 		(void) putq(q, dsp->ds_tx_flow_mp);
1528 		dsp->ds_tx_flow_mp = NULL;
1529 	}
1530 
1531 	if (!head_insert) {
1532 		/* Tail insertion */
1533 		if (dsp->ds_tx_list_head == NULL)
1534 			dsp->ds_tx_list_head = mp;
1535 		else
1536 			dsp->ds_tx_list_tail->b_next = mp;
1537 		dsp->ds_tx_list_tail = tail;
1538 	} else {
1539 		/* Head insertion */
1540 		tail->b_next = dsp->ds_tx_list_head;
1541 		if (dsp->ds_tx_list_head == NULL)
1542 			dsp->ds_tx_list_tail = tail;
1543 		dsp->ds_tx_list_head = mp;
1544 	}
1545 	mutex_exit(&dsp->ds_tx_list_lock);
1546 done:
1547 	/* Schedule service thread to drain the transmit queue */
1548 	qenable(q);
1549 }
1550 
1551 void
1552 dld_tx_flush(dld_str_t *dsp)
1553 {
1554 	mutex_enter(&dsp->ds_tx_list_lock);
1555 	if (dsp->ds_tx_list_head != NULL) {
1556 		freemsgchain(dsp->ds_tx_list_head);
1557 		dsp->ds_tx_list_head = dsp->ds_tx_list_tail = NULL;
1558 		dsp->ds_tx_cnt = dsp->ds_tx_msgcnt = 0;
1559 		if (dsp->ds_tx_qbusy) {
1560 			dsp->ds_tx_flow_mp = getq(dsp->ds_wq);
1561 			ASSERT(dsp->ds_tx_flow_mp != NULL);
1562 			dsp->ds_tx_qbusy = B_FALSE;
1563 		}
1564 	}
1565 	mutex_exit(&dsp->ds_tx_list_lock);
1566 }
1567 
1568 /*
1569  * Process an M_IOCTL message.
1570  */
1571 static void
1572 dld_ioc(dld_str_t *dsp, mblk_t *mp)
1573 {
1574 	uint_t			cmd;
1575 
1576 	cmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
1577 	ASSERT(dsp->ds_type == DLD_DLPI);
1578 
1579 	switch (cmd) {
1580 	case DLIOCRAW:
1581 		ioc_raw(dsp, mp);
1582 		break;
1583 	case DLIOCHDRINFO:
1584 		ioc_fast(dsp, mp);
1585 		break;
1586 	default:
1587 		ioc(dsp, mp);
1588 	}
1589 }
1590 
1591 /*
1592  * DLIOCRAW
1593  */
1594 static void
1595 ioc_raw(dld_str_t *dsp, mblk_t *mp)
1596 {
1597 	queue_t *q = dsp->ds_wq;
1598 
1599 	rw_enter(&dsp->ds_lock, RW_WRITER);
1600 	if (dsp->ds_polling || dsp->ds_soft_ring) {
1601 		rw_exit(&dsp->ds_lock);
1602 		miocnak(q, mp, 0, EPROTO);
1603 		return;
1604 	}
1605 
1606 	if (dsp->ds_mode != DLD_RAW && dsp->ds_dlstate == DL_IDLE) {
1607 		/*
1608 		 * Set the receive callback.
1609 		 */
1610 		dls_rx_set(dsp->ds_dc, dld_str_rx_raw, (void *)dsp);
1611 
1612 		/*
1613 		 * Note that raw mode is enabled.
1614 		 */
1615 		dsp->ds_mode = DLD_RAW;
1616 	}
1617 
1618 	rw_exit(&dsp->ds_lock);
1619 	miocack(q, mp, 0, 0);
1620 }
1621 
1622 /*
1623  * DLIOCHDRINFO
1624  */
1625 static void
1626 ioc_fast(dld_str_t *dsp, mblk_t *mp)
1627 {
1628 	dl_unitdata_req_t *dlp;
1629 	off_t		off;
1630 	size_t		len;
1631 	const uint8_t	*addr;
1632 	uint16_t	sap;
1633 	mblk_t		*nmp;
1634 	mblk_t		*hmp;
1635 	uint_t		addr_length;
1636 	queue_t		*q = dsp->ds_wq;
1637 	int		err;
1638 	dls_channel_t	dc;
1639 
1640 	if (dld_opt & DLD_OPT_NO_FASTPATH) {
1641 		err = ENOTSUP;
1642 		goto failed;
1643 	}
1644 
1645 	nmp = mp->b_cont;
1646 	if (nmp == NULL || MBLKL(nmp) < sizeof (dl_unitdata_req_t) ||
1647 	    (dlp = (dl_unitdata_req_t *)nmp->b_rptr,
1648 	    dlp->dl_primitive != DL_UNITDATA_REQ)) {
1649 		err = EINVAL;
1650 		goto failed;
1651 	}
1652 
1653 	off = dlp->dl_dest_addr_offset;
1654 	len = dlp->dl_dest_addr_length;
1655 
1656 	if (!MBLKIN(nmp, off, len)) {
1657 		err = EINVAL;
1658 		goto failed;
1659 	}
1660 
1661 	rw_enter(&dsp->ds_lock, RW_READER);
1662 	if (dsp->ds_dlstate != DL_IDLE) {
1663 		rw_exit(&dsp->ds_lock);
1664 		err = ENOTSUP;
1665 		goto failed;
1666 	}
1667 
1668 	addr_length = dsp->ds_mip->mi_addr_length;
1669 	if (len != addr_length + sizeof (uint16_t)) {
1670 		rw_exit(&dsp->ds_lock);
1671 		err = EINVAL;
1672 		goto failed;
1673 	}
1674 
1675 	addr = nmp->b_rptr + off;
1676 	sap = *(uint16_t *)(nmp->b_rptr + off + addr_length);
1677 	dc = dsp->ds_dc;
1678 
1679 	if ((hmp = dls_header(dc, addr, sap, dsp->ds_pri)) == NULL) {
1680 		rw_exit(&dsp->ds_lock);
1681 		err = ENOMEM;
1682 		goto failed;
1683 	}
1684 
1685 	/*
1686 	 * This is a performance optimization.  We originally entered
1687 	 * as reader and only become writer upon transitioning into
1688 	 * the DLD_FASTPATH mode for the first time.  Otherwise we
1689 	 * stay as reader and return the fast-path header to IP.
1690 	 */
1691 	if (dsp->ds_mode != DLD_FASTPATH) {
1692 		if (!rw_tryupgrade(&dsp->ds_lock)) {
1693 			rw_exit(&dsp->ds_lock);
1694 			rw_enter(&dsp->ds_lock, RW_WRITER);
1695 
1696 			/*
1697 			 * State may have changed before we re-acquired
1698 			 * the writer lock in case the upgrade failed.
1699 			 */
1700 			if (dsp->ds_dlstate != DL_IDLE) {
1701 				rw_exit(&dsp->ds_lock);
1702 				err = ENOTSUP;
1703 				goto failed;
1704 			}
1705 		}
1706 
1707 		/*
1708 		 * Set the receive callback (unless polling is enabled).
1709 		 */
1710 		if (!dsp->ds_polling && !dsp->ds_soft_ring)
1711 			dls_rx_set(dc, dld_str_rx_fastpath, (void *)dsp);
1712 
1713 		/*
1714 		 * Note that fast-path mode is enabled.
1715 		 */
1716 		dsp->ds_mode = DLD_FASTPATH;
1717 	}
1718 	rw_exit(&dsp->ds_lock);
1719 
1720 	freemsg(nmp->b_cont);
1721 	nmp->b_cont = hmp;
1722 
1723 	miocack(q, mp, MBLKL(nmp) + MBLKL(hmp), 0);
1724 	return;
1725 failed:
1726 	miocnak(q, mp, 0, err);
1727 }
1728 
1729 /*
1730  * Catch-all handler.
1731  */
1732 static void
1733 ioc(dld_str_t *dsp, mblk_t *mp)
1734 {
1735 	queue_t	*q = dsp->ds_wq;
1736 	mac_handle_t mh;
1737 
1738 	rw_enter(&dsp->ds_lock, RW_READER);
1739 	if (dsp->ds_dlstate == DL_UNATTACHED) {
1740 		rw_exit(&dsp->ds_lock);
1741 		miocnak(q, mp, 0, EINVAL);
1742 		return;
1743 	}
1744 	mh = dsp->ds_mh;
1745 	ASSERT(mh != NULL);
1746 	rw_exit(&dsp->ds_lock);
1747 	mac_ioctl(mh, q, mp);
1748 }
1749 
1750 /*
1751  * Allocate a new minor number.
1752  */
1753 static minor_t
1754 dld_minor_hold(boolean_t sleep)
1755 {
1756 	minor_t		minor;
1757 
1758 	/*
1759 	 * Grab a value from the arena.
1760 	 */
1761 	atomic_add_32(&minor_count, 1);
1762 	if ((minor = PTR_TO_MINOR(vmem_alloc(minor_arenap, 1,
1763 	    (sleep) ? VM_SLEEP : VM_NOSLEEP))) == 0) {
1764 		atomic_add_32(&minor_count, -1);
1765 		return (0);
1766 	}
1767 
1768 	return (minor);
1769 }
1770 
1771 /*
1772  * Release a previously allocated minor number.
1773  */
1774 static void
1775 dld_minor_rele(minor_t minor)
1776 {
1777 	/*
1778 	 * Return the value to the arena.
1779 	 */
1780 	vmem_free(minor_arenap, MINOR_TO_PTR(minor), 1);
1781 
1782 	atomic_add_32(&minor_count, -1);
1783 }
1784