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 rw_exit(&dsp->ds_lock); 444 DLD_EXIT(dsp); 445 return; 446 } 447 dsp->ds_tx_list_head = dsp->ds_tx_list_tail = NULL; 448 dsp->ds_tx_cnt = dsp->ds_tx_msgcnt = 0; 449 mutex_exit(&dsp->ds_tx_list_lock); 450 451 /* 452 * Discard packets unless we are attached and bound; note that 453 * the driver mode (fastpath/raw/unitdata) is irrelevant here, 454 * because regardless of the mode all transmit will end up in 455 * str_mdata_fastpath_put() where the packets may be queued. 456 */ 457 ASSERT(DB_TYPE(mp) == M_DATA); 458 if (dsp->ds_dlstate != DL_IDLE) { 459 freemsgchain(mp); 460 goto done; 461 } 462 463 /* 464 * Attempt to transmit one or more packets. If the MAC can't 465 * send them all, re-queue the packet(s) at the beginning of 466 * the transmit queue to avoid any re-ordering. 467 */ 468 if ((mp = dls_tx(dsp->ds_dc, mp)) != NULL) 469 dld_tx_enqueue(dsp, mp, B_TRUE); 470 471 done: 472 /* 473 * Grab the list lock again and check if the transmit queue is 474 * really empty; if so, lift up flow-control and backenable any 475 * writer queues. If the queue is not empty, schedule service 476 * thread to drain it. 477 */ 478 mutex_enter(&dsp->ds_tx_list_lock); 479 if (dsp->ds_tx_list_head == NULL) { 480 dsp->ds_tx_flow_mp = getq(wq); 481 ASSERT(dsp->ds_tx_flow_mp != NULL); 482 dsp->ds_tx_qbusy = B_FALSE; 483 } 484 mutex_exit(&dsp->ds_tx_list_lock); 485 486 rw_exit(&dsp->ds_lock); 487 DLD_EXIT(dsp); 488 } 489 490 void 491 dld_init_ops(struct dev_ops *ops, const char *name) 492 { 493 struct streamtab *stream; 494 struct qinit *rq, *wq; 495 struct module_info *modinfo; 496 497 modinfo = kmem_zalloc(sizeof (struct module_info), KM_SLEEP); 498 modinfo->mi_idname = kmem_zalloc(FMNAMESZ, KM_SLEEP); 499 (void) snprintf(modinfo->mi_idname, FMNAMESZ, "%s", name); 500 modinfo->mi_minpsz = 0; 501 modinfo->mi_maxpsz = 64*1024; 502 modinfo->mi_hiwat = 1; 503 modinfo->mi_lowat = 0; 504 505 rq = kmem_zalloc(sizeof (struct qinit), KM_SLEEP); 506 rq->qi_qopen = dld_open; 507 rq->qi_qclose = dld_close; 508 rq->qi_minfo = modinfo; 509 510 wq = kmem_zalloc(sizeof (struct qinit), KM_SLEEP); 511 wq->qi_putp = (pfi_t)dld_wput; 512 wq->qi_srvp = (pfi_t)dld_wsrv; 513 wq->qi_minfo = modinfo; 514 515 stream = kmem_zalloc(sizeof (struct streamtab), KM_SLEEP); 516 stream->st_rdinit = rq; 517 stream->st_wrinit = wq; 518 ops->devo_cb_ops->cb_str = stream; 519 520 ops->devo_getinfo = &dld_getinfo; 521 } 522 523 void 524 dld_fini_ops(struct dev_ops *ops) 525 { 526 struct streamtab *stream; 527 struct qinit *rq, *wq; 528 struct module_info *modinfo; 529 530 stream = ops->devo_cb_ops->cb_str; 531 rq = stream->st_rdinit; 532 wq = stream->st_wrinit; 533 modinfo = rq->qi_minfo; 534 ASSERT(wq->qi_minfo == modinfo); 535 536 kmem_free(stream, sizeof (struct streamtab)); 537 kmem_free(wq, sizeof (struct qinit)); 538 kmem_free(rq, sizeof (struct qinit)); 539 kmem_free(modinfo->mi_idname, FMNAMESZ); 540 kmem_free(modinfo, sizeof (struct module_info)); 541 } 542 543 /* 544 * Initialize this module's data structures. 545 */ 546 void 547 dld_str_init(void) 548 { 549 /* 550 * Create dld_str_t object cache. 551 */ 552 str_cachep = kmem_cache_create("dld_str_cache", sizeof (dld_str_t), 553 0, str_constructor, str_destructor, NULL, NULL, NULL, 0); 554 ASSERT(str_cachep != NULL); 555 556 /* 557 * Allocate a vmem arena to manage minor numbers. The range of the 558 * arena will be from DLD_MAX_MINOR + 1 to MAXMIN (maximum legal 559 * minor number). 560 */ 561 minor_arenap = vmem_create("dld_minor_arena", 562 MINOR_TO_PTR(DLD_MAX_MINOR + 1), MAXMIN, 1, NULL, NULL, NULL, 0, 563 VM_SLEEP | VMC_IDENTIFIER); 564 ASSERT(minor_arenap != NULL); 565 566 /* 567 * Create a hash table for maintaining dld_str_t's. 568 * The ds_minor field (the clone minor number) of a dld_str_t 569 * is used as a key for this hash table because this number is 570 * globally unique (allocated from "dld_minor_arena"). 571 */ 572 str_hashp = mod_hash_create_idhash("dld_str_hash", STR_HASHSZ, 573 mod_hash_null_valdtor); 574 } 575 576 /* 577 * Tear down this module's data structures. 578 */ 579 int 580 dld_str_fini(void) 581 { 582 /* 583 * Make sure that there are no objects in use. 584 */ 585 if (str_count != 0) 586 return (EBUSY); 587 588 /* 589 * Check to see if there are any minor numbers still in use. 590 */ 591 if (minor_count != 0) 592 return (EBUSY); 593 594 /* 595 * Destroy object cache. 596 */ 597 kmem_cache_destroy(str_cachep); 598 vmem_destroy(minor_arenap); 599 mod_hash_destroy_idhash(str_hashp); 600 return (0); 601 } 602 603 /* 604 * Create a new dld_str_t object. 605 */ 606 dld_str_t * 607 dld_str_create(queue_t *rq, uint_t type, major_t major, t_uscalar_t style) 608 { 609 dld_str_t *dsp; 610 int err; 611 612 /* 613 * Allocate an object from the cache. 614 */ 615 atomic_add_32(&str_count, 1); 616 dsp = kmem_cache_alloc(str_cachep, KM_SLEEP); 617 618 /* 619 * Allocate the dummy mblk for flow-control. 620 */ 621 dsp->ds_tx_flow_mp = allocb(1, BPRI_HI); 622 if (dsp->ds_tx_flow_mp == NULL) { 623 kmem_cache_free(str_cachep, dsp); 624 atomic_add_32(&str_count, -1); 625 return (NULL); 626 } 627 dsp->ds_type = type; 628 dsp->ds_major = major; 629 dsp->ds_style = style; 630 631 /* 632 * Initialize the queue pointers. 633 */ 634 ASSERT(RD(rq) == rq); 635 dsp->ds_rq = rq; 636 dsp->ds_wq = WR(rq); 637 rq->q_ptr = WR(rq)->q_ptr = (void *)dsp; 638 639 /* 640 * We want explicit control over our write-side STREAMS queue 641 * where the dummy mblk gets added/removed for flow-control. 642 */ 643 noenable(WR(rq)); 644 645 err = mod_hash_insert(str_hashp, STR_HASH_KEY(dsp->ds_minor), 646 (mod_hash_val_t)dsp); 647 ASSERT(err == 0); 648 return (dsp); 649 } 650 651 /* 652 * Destroy a dld_str_t object. 653 */ 654 void 655 dld_str_destroy(dld_str_t *dsp) 656 { 657 queue_t *rq; 658 queue_t *wq; 659 mod_hash_val_t val; 660 /* 661 * Clear the queue pointers. 662 */ 663 rq = dsp->ds_rq; 664 wq = dsp->ds_wq; 665 ASSERT(wq == WR(rq)); 666 667 rq->q_ptr = wq->q_ptr = NULL; 668 dsp->ds_rq = dsp->ds_wq = NULL; 669 670 ASSERT(!RW_LOCK_HELD(&dsp->ds_lock)); 671 ASSERT(MUTEX_NOT_HELD(&dsp->ds_tx_list_lock)); 672 ASSERT(dsp->ds_tx_list_head == NULL); 673 ASSERT(dsp->ds_tx_list_tail == NULL); 674 ASSERT(dsp->ds_tx_cnt == 0); 675 ASSERT(dsp->ds_tx_msgcnt == 0); 676 ASSERT(!dsp->ds_tx_qbusy); 677 678 ASSERT(MUTEX_NOT_HELD(&dsp->ds_thr_lock)); 679 ASSERT(dsp->ds_thr == 0); 680 ASSERT(dsp->ds_pending_req == NULL); 681 682 /* 683 * Reinitialize all the flags. 684 */ 685 dsp->ds_notifications = 0; 686 dsp->ds_passivestate = DLD_UNINITIALIZED; 687 dsp->ds_mode = DLD_UNITDATA; 688 689 /* 690 * Free the dummy mblk if exists. 691 */ 692 if (dsp->ds_tx_flow_mp != NULL) { 693 freeb(dsp->ds_tx_flow_mp); 694 dsp->ds_tx_flow_mp = NULL; 695 } 696 697 (void) mod_hash_remove(str_hashp, STR_HASH_KEY(dsp->ds_minor), &val); 698 ASSERT(dsp == (dld_str_t *)val); 699 700 /* 701 * Free the object back to the cache. 702 */ 703 kmem_cache_free(str_cachep, dsp); 704 atomic_add_32(&str_count, -1); 705 } 706 707 /* 708 * kmem_cache contructor function: see kmem_cache_create(9f). 709 */ 710 /*ARGSUSED*/ 711 static int 712 str_constructor(void *buf, void *cdrarg, int kmflags) 713 { 714 dld_str_t *dsp = buf; 715 716 bzero(buf, sizeof (dld_str_t)); 717 718 /* 719 * Allocate a new minor number. 720 */ 721 if ((dsp->ds_minor = dld_minor_hold(kmflags == KM_SLEEP)) == 0) 722 return (-1); 723 724 /* 725 * Initialize the DLPI state machine. 726 */ 727 dsp->ds_dlstate = DL_UNATTACHED; 728 dsp->ds_ppa = (t_uscalar_t)-1; 729 730 mutex_init(&dsp->ds_thr_lock, NULL, MUTEX_DRIVER, NULL); 731 rw_init(&dsp->ds_lock, NULL, RW_DRIVER, NULL); 732 mutex_init(&dsp->ds_tx_list_lock, NULL, MUTEX_DRIVER, NULL); 733 cv_init(&dsp->ds_pending_cv, NULL, CV_DRIVER, NULL); 734 735 return (0); 736 } 737 738 /* 739 * kmem_cache destructor function. 740 */ 741 /*ARGSUSED*/ 742 static void 743 str_destructor(void *buf, void *cdrarg) 744 { 745 dld_str_t *dsp = buf; 746 747 /* 748 * Make sure the DLPI state machine was reset. 749 */ 750 ASSERT(dsp->ds_dlstate == DL_UNATTACHED); 751 752 /* 753 * Make sure the data-link interface was closed. 754 */ 755 ASSERT(dsp->ds_mh == NULL); 756 ASSERT(dsp->ds_dc == NULL); 757 758 /* 759 * Make sure enabled notifications are cleared. 760 */ 761 ASSERT(dsp->ds_notifications == 0); 762 763 /* 764 * Make sure polling is disabled. 765 */ 766 ASSERT(!dsp->ds_polling); 767 768 /* 769 * Release the minor number. 770 */ 771 dld_minor_rele(dsp->ds_minor); 772 773 ASSERT(!RW_LOCK_HELD(&dsp->ds_lock)); 774 rw_destroy(&dsp->ds_lock); 775 776 ASSERT(MUTEX_NOT_HELD(&dsp->ds_tx_list_lock)); 777 mutex_destroy(&dsp->ds_tx_list_lock); 778 ASSERT(dsp->ds_tx_flow_mp == NULL); 779 780 ASSERT(MUTEX_NOT_HELD(&dsp->ds_thr_lock)); 781 mutex_destroy(&dsp->ds_thr_lock); 782 ASSERT(dsp->ds_pending_req == NULL); 783 ASSERT(dsp->ds_pending_op == NULL); 784 ASSERT(dsp->ds_pending_cnt == 0); 785 cv_destroy(&dsp->ds_pending_cv); 786 } 787 788 /* 789 * M_DATA put (IP fast-path mode) 790 */ 791 void 792 str_mdata_fastpath_put(dld_str_t *dsp, mblk_t *mp) 793 { 794 /* 795 * This function can be called from within dld or from an upper 796 * layer protocol (currently only tcp). If we are in the busy 797 * mode enqueue the packet(s) and return. Otherwise hand them 798 * over to the MAC driver for transmission; any remaining one(s) 799 * which didn't get sent will be queued. 800 * 801 * Note here that we don't grab the list lock prior to checking 802 * the busy flag. This is okay, because a missed transition 803 * will not cause any packet reordering for any particular TCP 804 * connection (which is single-threaded). The enqueue routine 805 * will atomically set the busy flag and schedule the service 806 * thread to run; the flag is only cleared by the service thread 807 * when there is no more packet to be transmitted. 808 */ 809 if (dsp->ds_tx_qbusy || (mp = dls_tx(dsp->ds_dc, mp)) != NULL) 810 dld_tx_enqueue(dsp, mp, B_FALSE); 811 } 812 813 /* 814 * M_DATA put (raw mode) 815 */ 816 void 817 str_mdata_raw_put(dld_str_t *dsp, mblk_t *mp) 818 { 819 struct ether_header *ehp; 820 mblk_t *bp; 821 size_t size; 822 size_t hdrlen; 823 824 size = MBLKL(mp); 825 if (size < sizeof (struct ether_header)) 826 goto discard; 827 828 hdrlen = sizeof (struct ether_header); 829 830 ehp = (struct ether_header *)mp->b_rptr; 831 if (ntohs(ehp->ether_type) == VLAN_TPID) { 832 struct ether_vlan_header *evhp; 833 834 if (size < sizeof (struct ether_vlan_header)) 835 goto discard; 836 837 /* 838 * Replace vtag with our own 839 */ 840 evhp = (struct ether_vlan_header *)ehp; 841 evhp->ether_tci = htons(VLAN_TCI(dsp->ds_pri, 842 ETHER_CFI, dsp->ds_vid)); 843 hdrlen = sizeof (struct ether_vlan_header); 844 } 845 846 /* 847 * Check the packet is not too big and that any remaining 848 * fragment list is composed entirely of M_DATA messages. (We 849 * know the first fragment was M_DATA otherwise we could not 850 * have got here). 851 */ 852 for (bp = mp->b_cont; bp != NULL; bp = bp->b_cont) { 853 if (DB_TYPE(bp) != M_DATA) 854 goto discard; 855 size += MBLKL(bp); 856 } 857 858 if (size > dsp->ds_mip->mi_sdu_max + hdrlen) 859 goto discard; 860 861 str_mdata_fastpath_put(dsp, mp); 862 return; 863 864 discard: 865 freemsg(mp); 866 } 867 868 /* 869 * Process DL_ATTACH_REQ (style 2) or open(2) (style 1). 870 */ 871 int 872 dld_str_attach(dld_str_t *dsp, t_uscalar_t ppa) 873 { 874 int err; 875 const char *drvname; 876 char name[MAXNAMELEN]; 877 dls_channel_t dc; 878 uint_t addr_length; 879 880 ASSERT(dsp->ds_dc == NULL); 881 882 if ((drvname = ddi_major_to_name(dsp->ds_major)) == NULL) 883 return (EINVAL); 884 885 (void) snprintf(name, MAXNAMELEN, "%s%u", drvname, ppa); 886 887 if (strcmp(drvname, "aggr") != 0 && 888 qassociate(dsp->ds_wq, DLS_PPA2INST(ppa)) != 0) 889 return (EINVAL); 890 891 /* 892 * Open a channel. 893 */ 894 if ((err = dls_open(name, &dc)) != 0) { 895 (void) qassociate(dsp->ds_wq, -1); 896 return (err); 897 } 898 899 /* 900 * Cache the MAC interface handle, a pointer to the immutable MAC 901 * information and the current and 'factory' MAC address. 902 */ 903 dsp->ds_mh = dls_mac(dc); 904 dsp->ds_mip = mac_info(dsp->ds_mh); 905 906 mac_unicst_get(dsp->ds_mh, dsp->ds_curr_addr); 907 908 addr_length = dsp->ds_mip->mi_addr_length; 909 bcopy(dsp->ds_mip->mi_unicst_addr, dsp->ds_fact_addr, addr_length); 910 911 /* 912 * Cache the interface VLAN identifier. (This will be VLAN_ID_NONE for 913 * a non-VLAN interface). 914 */ 915 dsp->ds_vid = dls_vid(dc); 916 917 /* 918 * Set the default packet priority. 919 */ 920 dsp->ds_pri = 0; 921 922 /* 923 * Add a notify function so that the we get updates from the MAC. 924 */ 925 dsp->ds_mnh = mac_notify_add(dsp->ds_mh, str_notify, (void *)dsp); 926 927 dsp->ds_ppa = ppa; 928 dsp->ds_dc = dc; 929 dsp->ds_dlstate = DL_UNBOUND; 930 931 return (0); 932 } 933 934 /* 935 * Process DL_DETACH_REQ (style 2) or close(2) (style 1). Can also be called 936 * from close(2) for style 2. 937 */ 938 void 939 dld_str_detach(dld_str_t *dsp) 940 { 941 ASSERT(dsp->ds_thr == 0); 942 943 /* 944 * Remove the notify function. 945 */ 946 mac_notify_remove(dsp->ds_mh, dsp->ds_mnh); 947 948 /* 949 * Clear the polling and promisc flags. 950 */ 951 dsp->ds_polling = B_FALSE; 952 dsp->ds_soft_ring = B_FALSE; 953 dsp->ds_promisc = 0; 954 955 /* 956 * Close the channel. 957 */ 958 dls_close(dsp->ds_dc); 959 dsp->ds_ppa = (t_uscalar_t)-1; 960 dsp->ds_dc = NULL; 961 dsp->ds_mh = NULL; 962 963 (void) qassociate(dsp->ds_wq, -1); 964 965 /* 966 * Re-initialize the DLPI state machine. 967 */ 968 dsp->ds_dlstate = DL_UNATTACHED; 969 970 } 971 972 /* 973 * Raw mode receive function. 974 */ 975 /*ARGSUSED*/ 976 void 977 dld_str_rx_raw(void *arg, mac_resource_handle_t mrh, mblk_t *mp, 978 size_t header_length) 979 { 980 dld_str_t *dsp = (dld_str_t *)arg; 981 mblk_t *next; 982 983 ASSERT(mp != NULL); 984 do { 985 /* 986 * Get the pointer to the next packet in the chain and then 987 * clear b_next before the packet gets passed on. 988 */ 989 next = mp->b_next; 990 mp->b_next = NULL; 991 992 /* 993 * Wind back b_rptr to point at the MAC header. 994 */ 995 ASSERT(mp->b_rptr >= DB_BASE(mp) + header_length); 996 mp->b_rptr -= header_length; 997 if (header_length == sizeof (struct ether_vlan_header)) { 998 /* 999 * Strip off the vtag 1000 */ 1001 ovbcopy(mp->b_rptr, mp->b_rptr + VLAN_TAGSZ, 1002 2 * ETHERADDRL); 1003 mp->b_rptr += VLAN_TAGSZ; 1004 } 1005 1006 /* 1007 * Pass the packet on. 1008 */ 1009 if (canputnext(dsp->ds_rq)) 1010 putnext(dsp->ds_rq, mp); 1011 else 1012 freemsg(mp); 1013 1014 /* 1015 * Move on to the next packet in the chain. 1016 */ 1017 mp = next; 1018 } while (mp != NULL); 1019 } 1020 1021 /* 1022 * Fast-path receive function. 1023 */ 1024 /*ARGSUSED*/ 1025 void 1026 dld_str_rx_fastpath(void *arg, mac_resource_handle_t mrh, mblk_t *mp, 1027 size_t header_length) 1028 { 1029 dld_str_t *dsp = (dld_str_t *)arg; 1030 mblk_t *next; 1031 1032 ASSERT(mp != NULL); 1033 do { 1034 /* 1035 * Get the pointer to the next packet in the chain and then 1036 * clear b_next before the packet gets passed on. 1037 */ 1038 next = mp->b_next; 1039 mp->b_next = NULL; 1040 1041 /* 1042 * Pass the packet on. 1043 */ 1044 if (canputnext(dsp->ds_rq)) 1045 putnext(dsp->ds_rq, mp); 1046 else 1047 freemsg(mp); 1048 /* 1049 * Move on to the next packet in the chain. 1050 */ 1051 mp = next; 1052 } while (mp != NULL); 1053 } 1054 1055 /* 1056 * Default receive function (send DL_UNITDATA_IND messages). 1057 */ 1058 /*ARGSUSED*/ 1059 void 1060 dld_str_rx_unitdata(void *arg, mac_resource_handle_t mrh, mblk_t *mp, 1061 size_t header_length) 1062 { 1063 dld_str_t *dsp = (dld_str_t *)arg; 1064 mblk_t *ud_mp; 1065 mblk_t *next; 1066 1067 ASSERT(mp != NULL); 1068 do { 1069 /* 1070 * Get the pointer to the next packet in the chain and then 1071 * clear b_next before the packet gets passed on. 1072 */ 1073 next = mp->b_next; 1074 mp->b_next = NULL; 1075 1076 /* 1077 * Wind back b_rptr to point at the MAC header. 1078 */ 1079 ASSERT(mp->b_rptr >= DB_BASE(mp) + header_length); 1080 mp->b_rptr -= header_length; 1081 1082 /* 1083 * Create the DL_UNITDATA_IND M_PROTO. 1084 */ 1085 if ((ud_mp = str_unitdata_ind(dsp, mp)) == NULL) { 1086 freemsgchain(mp); 1087 return; 1088 } 1089 1090 /* 1091 * Advance b_rptr to point at the payload again. 1092 */ 1093 mp->b_rptr += header_length; 1094 1095 /* 1096 * Prepend the DL_UNITDATA_IND. 1097 */ 1098 ud_mp->b_cont = mp; 1099 1100 /* 1101 * Send the message. 1102 */ 1103 if (canputnext(dsp->ds_rq)) 1104 putnext(dsp->ds_rq, ud_mp); 1105 else 1106 freemsg(ud_mp); 1107 1108 /* 1109 * Move on to the next packet in the chain. 1110 */ 1111 mp = next; 1112 } while (mp != NULL); 1113 } 1114 1115 /* 1116 * Generate DL_NOTIFY_IND messages to notify the DLPI consumer of the 1117 * current state of the interface. 1118 */ 1119 void 1120 dld_str_notify_ind(dld_str_t *dsp) 1121 { 1122 mac_notify_type_t type; 1123 1124 for (type = 0; type < MAC_NNOTE; type++) 1125 str_notify(dsp, type); 1126 } 1127 1128 typedef struct dl_unitdata_ind_wrapper { 1129 dl_unitdata_ind_t dl_unitdata; 1130 uint8_t dl_dest_addr[MAXADDRLEN + sizeof (uint16_t)]; 1131 uint8_t dl_src_addr[MAXADDRLEN + sizeof (uint16_t)]; 1132 } dl_unitdata_ind_wrapper_t; 1133 1134 /* 1135 * Create a DL_UNITDATA_IND M_PROTO message. 1136 */ 1137 static mblk_t * 1138 str_unitdata_ind(dld_str_t *dsp, mblk_t *mp) 1139 { 1140 mblk_t *nmp; 1141 dl_unitdata_ind_wrapper_t *dlwp; 1142 dl_unitdata_ind_t *dlp; 1143 dls_header_info_t dhi; 1144 uint_t addr_length; 1145 uint8_t *daddr; 1146 uint8_t *saddr; 1147 1148 /* 1149 * Get the packet header information. 1150 */ 1151 dls_header_info(dsp->ds_dc, mp, &dhi); 1152 1153 /* 1154 * Allocate a message large enough to contain the wrapper structure 1155 * defined above. 1156 */ 1157 if ((nmp = mexchange(dsp->ds_wq, NULL, 1158 sizeof (dl_unitdata_ind_wrapper_t), M_PROTO, 1159 DL_UNITDATA_IND)) == NULL) 1160 return (NULL); 1161 1162 dlwp = (dl_unitdata_ind_wrapper_t *)nmp->b_rptr; 1163 1164 dlp = &(dlwp->dl_unitdata); 1165 ASSERT(dlp == (dl_unitdata_ind_t *)nmp->b_rptr); 1166 ASSERT(dlp->dl_primitive == DL_UNITDATA_IND); 1167 1168 /* 1169 * Copy in the destination address. 1170 */ 1171 addr_length = dsp->ds_mip->mi_addr_length; 1172 daddr = dlwp->dl_dest_addr; 1173 dlp->dl_dest_addr_offset = (uintptr_t)daddr - (uintptr_t)dlp; 1174 bcopy(dhi.dhi_daddr, daddr, addr_length); 1175 1176 /* 1177 * Set the destination DLSAP to our bound DLSAP value. 1178 */ 1179 *(uint16_t *)(daddr + addr_length) = dsp->ds_sap; 1180 dlp->dl_dest_addr_length = addr_length + sizeof (uint16_t); 1181 1182 /* 1183 * If the destination address was a group address then 1184 * dl_group_address field should be non-zero. 1185 */ 1186 dlp->dl_group_address = dhi.dhi_isgroup; 1187 1188 /* 1189 * Copy in the source address. 1190 */ 1191 saddr = dlwp->dl_src_addr; 1192 dlp->dl_src_addr_offset = (uintptr_t)saddr - (uintptr_t)dlp; 1193 bcopy(dhi.dhi_saddr, saddr, addr_length); 1194 1195 /* 1196 * Set the source DLSAP to the packet ethertype. 1197 */ 1198 *(uint16_t *)(saddr + addr_length) = dhi.dhi_ethertype; 1199 dlp->dl_src_addr_length = addr_length + sizeof (uint16_t); 1200 1201 return (nmp); 1202 } 1203 1204 /* 1205 * DL_NOTIFY_IND: DL_NOTE_PROMISC_ON_PHYS 1206 */ 1207 static void 1208 str_notify_promisc_on_phys(dld_str_t *dsp) 1209 { 1210 mblk_t *mp; 1211 dl_notify_ind_t *dlip; 1212 1213 if (!(dsp->ds_notifications & DL_NOTE_PROMISC_ON_PHYS)) 1214 return; 1215 1216 if ((mp = mexchange(dsp->ds_wq, NULL, sizeof (dl_notify_ind_t), 1217 M_PROTO, 0)) == NULL) 1218 return; 1219 1220 bzero(mp->b_rptr, sizeof (dl_notify_ind_t)); 1221 dlip = (dl_notify_ind_t *)mp->b_rptr; 1222 dlip->dl_primitive = DL_NOTIFY_IND; 1223 dlip->dl_notification = DL_NOTE_PROMISC_ON_PHYS; 1224 1225 qreply(dsp->ds_wq, mp); 1226 } 1227 1228 /* 1229 * DL_NOTIFY_IND: DL_NOTE_PROMISC_OFF_PHYS 1230 */ 1231 static void 1232 str_notify_promisc_off_phys(dld_str_t *dsp) 1233 { 1234 mblk_t *mp; 1235 dl_notify_ind_t *dlip; 1236 1237 if (!(dsp->ds_notifications & DL_NOTE_PROMISC_OFF_PHYS)) 1238 return; 1239 1240 if ((mp = mexchange(dsp->ds_wq, NULL, sizeof (dl_notify_ind_t), 1241 M_PROTO, 0)) == NULL) 1242 return; 1243 1244 bzero(mp->b_rptr, sizeof (dl_notify_ind_t)); 1245 dlip = (dl_notify_ind_t *)mp->b_rptr; 1246 dlip->dl_primitive = DL_NOTIFY_IND; 1247 dlip->dl_notification = DL_NOTE_PROMISC_OFF_PHYS; 1248 1249 qreply(dsp->ds_wq, mp); 1250 } 1251 1252 /* 1253 * DL_NOTIFY_IND: DL_NOTE_PHYS_ADDR 1254 */ 1255 static void 1256 str_notify_phys_addr(dld_str_t *dsp, const uint8_t *addr) 1257 { 1258 mblk_t *mp; 1259 dl_notify_ind_t *dlip; 1260 uint_t addr_length; 1261 uint16_t ethertype; 1262 1263 if (!(dsp->ds_notifications & DL_NOTE_PHYS_ADDR)) 1264 return; 1265 1266 addr_length = dsp->ds_mip->mi_addr_length; 1267 if ((mp = mexchange(dsp->ds_wq, NULL, 1268 sizeof (dl_notify_ind_t) + addr_length + sizeof (uint16_t), 1269 M_PROTO, 0)) == NULL) 1270 return; 1271 1272 bzero(mp->b_rptr, sizeof (dl_notify_ind_t)); 1273 dlip = (dl_notify_ind_t *)mp->b_rptr; 1274 dlip->dl_primitive = DL_NOTIFY_IND; 1275 dlip->dl_notification = DL_NOTE_PHYS_ADDR; 1276 dlip->dl_data = DL_CURR_PHYS_ADDR; 1277 dlip->dl_addr_offset = sizeof (dl_notify_ind_t); 1278 dlip->dl_addr_length = addr_length + sizeof (uint16_t); 1279 1280 bcopy(addr, &dlip[1], addr_length); 1281 1282 ethertype = (dsp->ds_sap < ETHERTYPE_802_MIN) ? 0 : dsp->ds_sap; 1283 *(uint16_t *)((uchar_t *)(dlip + 1) + addr_length) = 1284 ethertype; 1285 1286 qreply(dsp->ds_wq, mp); 1287 } 1288 1289 /* 1290 * DL_NOTIFY_IND: DL_NOTE_LINK_UP 1291 */ 1292 static void 1293 str_notify_link_up(dld_str_t *dsp) 1294 { 1295 mblk_t *mp; 1296 dl_notify_ind_t *dlip; 1297 1298 if (!(dsp->ds_notifications & DL_NOTE_LINK_UP)) 1299 return; 1300 1301 if ((mp = mexchange(dsp->ds_wq, NULL, sizeof (dl_notify_ind_t), 1302 M_PROTO, 0)) == NULL) 1303 return; 1304 1305 bzero(mp->b_rptr, sizeof (dl_notify_ind_t)); 1306 dlip = (dl_notify_ind_t *)mp->b_rptr; 1307 dlip->dl_primitive = DL_NOTIFY_IND; 1308 dlip->dl_notification = DL_NOTE_LINK_UP; 1309 1310 qreply(dsp->ds_wq, mp); 1311 } 1312 1313 /* 1314 * DL_NOTIFY_IND: DL_NOTE_LINK_DOWN 1315 */ 1316 static void 1317 str_notify_link_down(dld_str_t *dsp) 1318 { 1319 mblk_t *mp; 1320 dl_notify_ind_t *dlip; 1321 1322 if (!(dsp->ds_notifications & DL_NOTE_LINK_DOWN)) 1323 return; 1324 1325 if ((mp = mexchange(dsp->ds_wq, NULL, sizeof (dl_notify_ind_t), 1326 M_PROTO, 0)) == NULL) 1327 return; 1328 1329 bzero(mp->b_rptr, sizeof (dl_notify_ind_t)); 1330 dlip = (dl_notify_ind_t *)mp->b_rptr; 1331 dlip->dl_primitive = DL_NOTIFY_IND; 1332 dlip->dl_notification = DL_NOTE_LINK_DOWN; 1333 1334 qreply(dsp->ds_wq, mp); 1335 } 1336 1337 /* 1338 * DL_NOTIFY_IND: DL_NOTE_SPEED 1339 */ 1340 static void 1341 str_notify_speed(dld_str_t *dsp, uint32_t speed) 1342 { 1343 mblk_t *mp; 1344 dl_notify_ind_t *dlip; 1345 1346 if (!(dsp->ds_notifications & DL_NOTE_SPEED)) 1347 return; 1348 1349 if ((mp = mexchange(dsp->ds_wq, NULL, sizeof (dl_notify_ind_t), 1350 M_PROTO, 0)) == NULL) 1351 return; 1352 1353 bzero(mp->b_rptr, sizeof (dl_notify_ind_t)); 1354 dlip = (dl_notify_ind_t *)mp->b_rptr; 1355 dlip->dl_primitive = DL_NOTIFY_IND; 1356 dlip->dl_notification = DL_NOTE_SPEED; 1357 dlip->dl_data = speed; 1358 1359 qreply(dsp->ds_wq, mp); 1360 } 1361 1362 /* 1363 * DL_NOTIFY_IND: DL_NOTE_CAPAB_RENEG 1364 */ 1365 static void 1366 str_notify_capab_reneg(dld_str_t *dsp) 1367 { 1368 mblk_t *mp; 1369 dl_notify_ind_t *dlip; 1370 1371 if (!(dsp->ds_notifications & DL_NOTE_CAPAB_RENEG)) 1372 return; 1373 1374 if ((mp = mexchange(dsp->ds_wq, NULL, sizeof (dl_notify_ind_t), 1375 M_PROTO, 0)) == NULL) 1376 return; 1377 1378 bzero(mp->b_rptr, sizeof (dl_notify_ind_t)); 1379 dlip = (dl_notify_ind_t *)mp->b_rptr; 1380 dlip->dl_primitive = DL_NOTIFY_IND; 1381 dlip->dl_notification = DL_NOTE_CAPAB_RENEG; 1382 1383 qreply(dsp->ds_wq, mp); 1384 } 1385 1386 /* 1387 * MAC notification callback. 1388 */ 1389 static void 1390 str_notify(void *arg, mac_notify_type_t type) 1391 { 1392 dld_str_t *dsp = (dld_str_t *)arg; 1393 queue_t *q = dsp->ds_wq; 1394 1395 switch (type) { 1396 case MAC_NOTE_TX: 1397 qenable(q); 1398 break; 1399 1400 case MAC_NOTE_DEVPROMISC: 1401 /* 1402 * Send the appropriate DL_NOTIFY_IND. 1403 */ 1404 if (mac_promisc_get(dsp->ds_mh, MAC_DEVPROMISC)) 1405 str_notify_promisc_on_phys(dsp); 1406 else 1407 str_notify_promisc_off_phys(dsp); 1408 break; 1409 1410 case MAC_NOTE_PROMISC: 1411 break; 1412 1413 case MAC_NOTE_UNICST: 1414 /* 1415 * This notification is sent whenever the MAC unicast address 1416 * changes. We need to re-cache the address. 1417 */ 1418 mac_unicst_get(dsp->ds_mh, dsp->ds_curr_addr); 1419 1420 /* 1421 * Send the appropriate DL_NOTIFY_IND. 1422 */ 1423 str_notify_phys_addr(dsp, dsp->ds_curr_addr); 1424 break; 1425 1426 case MAC_NOTE_LINK: 1427 /* 1428 * This notification is sent every time the MAC driver 1429 * updates the link state. 1430 */ 1431 switch (mac_link_get(dsp->ds_mh)) { 1432 case LINK_STATE_UP: 1433 /* 1434 * The link is up so send the appropriate 1435 * DL_NOTIFY_IND. 1436 */ 1437 str_notify_link_up(dsp); 1438 1439 /* 1440 * If we can find the link speed then send a 1441 * DL_NOTIFY_IND for that too. 1442 */ 1443 if (dsp->ds_mip->mi_stat[MAC_STAT_IFSPEED]) { 1444 uint64_t val; 1445 1446 val = mac_stat_get(dsp->ds_mh, 1447 MAC_STAT_IFSPEED); 1448 str_notify_speed(dsp, 1449 (uint32_t)(val / 1000ull)); 1450 } 1451 break; 1452 1453 case LINK_STATE_DOWN: 1454 /* 1455 * The link is down so send the appropriate 1456 * DL_NOTIFY_IND. 1457 */ 1458 str_notify_link_down(dsp); 1459 break; 1460 1461 default: 1462 break; 1463 } 1464 break; 1465 1466 case MAC_NOTE_RESOURCE: 1467 /* 1468 * This notification is sent whenever the MAC resources 1469 * change. We need to renegotiate the capabilities. 1470 * Send the appropriate DL_NOTIFY_IND. 1471 */ 1472 str_notify_capab_reneg(dsp); 1473 break; 1474 1475 default: 1476 ASSERT(B_FALSE); 1477 break; 1478 } 1479 } 1480 1481 /* 1482 * Enqueue one or more messages to the transmit queue. 1483 * Caller specifies the insertion position (head/tail). 1484 */ 1485 void 1486 dld_tx_enqueue(dld_str_t *dsp, mblk_t *mp, boolean_t head_insert) 1487 { 1488 mblk_t *tail; 1489 queue_t *q = dsp->ds_wq; 1490 uint_t cnt, msgcnt; 1491 uint_t tot_cnt, tot_msgcnt; 1492 1493 ASSERT(DB_TYPE(mp) == M_DATA); 1494 /* Calculate total size and count of the packet(s) */ 1495 for (tail = mp, cnt = msgdsize(mp), msgcnt = 1; 1496 tail->b_next != NULL; tail = tail->b_next) { 1497 ASSERT(DB_TYPE(tail->b_next) == M_DATA); 1498 cnt += msgdsize(tail->b_next); 1499 msgcnt++; 1500 } 1501 1502 mutex_enter(&dsp->ds_tx_list_lock); 1503 /* 1504 * If the queue depth would exceed the allowed threshold, drop 1505 * new packet(s) and drain those already in the queue. 1506 */ 1507 tot_cnt = dsp->ds_tx_cnt + cnt; 1508 tot_msgcnt = dsp->ds_tx_msgcnt + msgcnt; 1509 1510 if (!head_insert && 1511 (tot_cnt >= dld_max_q_count || tot_msgcnt >= dld_max_q_count)) { 1512 ASSERT(dsp->ds_tx_qbusy); 1513 mutex_exit(&dsp->ds_tx_list_lock); 1514 freemsgchain(mp); 1515 goto done; 1516 } 1517 1518 /* Update the queue size parameters */ 1519 dsp->ds_tx_cnt = tot_cnt; 1520 dsp->ds_tx_msgcnt = tot_msgcnt; 1521 1522 /* 1523 * If the transmit queue is currently empty and we are 1524 * about to deposit the packet(s) there, switch mode to 1525 * "busy" and raise flow-control condition. 1526 */ 1527 if (!dsp->ds_tx_qbusy) { 1528 dsp->ds_tx_qbusy = B_TRUE; 1529 ASSERT(dsp->ds_tx_flow_mp != NULL); 1530 (void) putq(q, dsp->ds_tx_flow_mp); 1531 dsp->ds_tx_flow_mp = NULL; 1532 } 1533 1534 if (!head_insert) { 1535 /* Tail insertion */ 1536 if (dsp->ds_tx_list_head == NULL) 1537 dsp->ds_tx_list_head = mp; 1538 else 1539 dsp->ds_tx_list_tail->b_next = mp; 1540 dsp->ds_tx_list_tail = tail; 1541 } else { 1542 /* Head insertion */ 1543 tail->b_next = dsp->ds_tx_list_head; 1544 if (dsp->ds_tx_list_head == NULL) 1545 dsp->ds_tx_list_tail = tail; 1546 dsp->ds_tx_list_head = mp; 1547 } 1548 mutex_exit(&dsp->ds_tx_list_lock); 1549 done: 1550 /* Schedule service thread to drain the transmit queue */ 1551 qenable(q); 1552 } 1553 1554 void 1555 dld_tx_flush(dld_str_t *dsp) 1556 { 1557 mutex_enter(&dsp->ds_tx_list_lock); 1558 if (dsp->ds_tx_list_head != NULL) { 1559 freemsgchain(dsp->ds_tx_list_head); 1560 dsp->ds_tx_list_head = dsp->ds_tx_list_tail = NULL; 1561 dsp->ds_tx_cnt = dsp->ds_tx_msgcnt = 0; 1562 if (dsp->ds_tx_qbusy) { 1563 dsp->ds_tx_flow_mp = getq(dsp->ds_wq); 1564 ASSERT(dsp->ds_tx_flow_mp != NULL); 1565 dsp->ds_tx_qbusy = B_FALSE; 1566 } 1567 } 1568 mutex_exit(&dsp->ds_tx_list_lock); 1569 } 1570 1571 /* 1572 * Process an M_IOCTL message. 1573 */ 1574 static void 1575 dld_ioc(dld_str_t *dsp, mblk_t *mp) 1576 { 1577 uint_t cmd; 1578 1579 cmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd; 1580 ASSERT(dsp->ds_type == DLD_DLPI); 1581 1582 switch (cmd) { 1583 case DLIOCRAW: 1584 ioc_raw(dsp, mp); 1585 break; 1586 case DLIOCHDRINFO: 1587 ioc_fast(dsp, mp); 1588 break; 1589 default: 1590 ioc(dsp, mp); 1591 } 1592 } 1593 1594 /* 1595 * DLIOCRAW 1596 */ 1597 static void 1598 ioc_raw(dld_str_t *dsp, mblk_t *mp) 1599 { 1600 queue_t *q = dsp->ds_wq; 1601 1602 rw_enter(&dsp->ds_lock, RW_WRITER); 1603 if (dsp->ds_polling || dsp->ds_soft_ring) { 1604 rw_exit(&dsp->ds_lock); 1605 miocnak(q, mp, 0, EPROTO); 1606 return; 1607 } 1608 1609 if (dsp->ds_mode != DLD_RAW && dsp->ds_dlstate == DL_IDLE) { 1610 /* 1611 * Set the receive callback. 1612 */ 1613 dls_rx_set(dsp->ds_dc, dld_str_rx_raw, (void *)dsp); 1614 } 1615 1616 /* 1617 * Note that raw mode is enabled. 1618 */ 1619 dsp->ds_mode = DLD_RAW; 1620 1621 rw_exit(&dsp->ds_lock); 1622 miocack(q, mp, 0, 0); 1623 } 1624 1625 /* 1626 * DLIOCHDRINFO 1627 */ 1628 static void 1629 ioc_fast(dld_str_t *dsp, mblk_t *mp) 1630 { 1631 dl_unitdata_req_t *dlp; 1632 off_t off; 1633 size_t len; 1634 const uint8_t *addr; 1635 uint16_t sap; 1636 mblk_t *nmp; 1637 mblk_t *hmp; 1638 uint_t addr_length; 1639 queue_t *q = dsp->ds_wq; 1640 int err; 1641 dls_channel_t dc; 1642 1643 if (dld_opt & DLD_OPT_NO_FASTPATH) { 1644 err = ENOTSUP; 1645 goto failed; 1646 } 1647 1648 nmp = mp->b_cont; 1649 if (nmp == NULL || MBLKL(nmp) < sizeof (dl_unitdata_req_t) || 1650 (dlp = (dl_unitdata_req_t *)nmp->b_rptr, 1651 dlp->dl_primitive != DL_UNITDATA_REQ)) { 1652 err = EINVAL; 1653 goto failed; 1654 } 1655 1656 off = dlp->dl_dest_addr_offset; 1657 len = dlp->dl_dest_addr_length; 1658 1659 if (!MBLKIN(nmp, off, len)) { 1660 err = EINVAL; 1661 goto failed; 1662 } 1663 1664 rw_enter(&dsp->ds_lock, RW_READER); 1665 if (dsp->ds_dlstate != DL_IDLE) { 1666 rw_exit(&dsp->ds_lock); 1667 err = ENOTSUP; 1668 goto failed; 1669 } 1670 1671 addr_length = dsp->ds_mip->mi_addr_length; 1672 if (len != addr_length + sizeof (uint16_t)) { 1673 rw_exit(&dsp->ds_lock); 1674 err = EINVAL; 1675 goto failed; 1676 } 1677 1678 addr = nmp->b_rptr + off; 1679 sap = *(uint16_t *)(nmp->b_rptr + off + addr_length); 1680 dc = dsp->ds_dc; 1681 1682 if ((hmp = dls_header(dc, addr, sap, dsp->ds_pri)) == NULL) { 1683 rw_exit(&dsp->ds_lock); 1684 err = ENOMEM; 1685 goto failed; 1686 } 1687 1688 /* 1689 * This is a performance optimization. We originally entered 1690 * as reader and only become writer upon transitioning into 1691 * the DLD_FASTPATH mode for the first time. Otherwise we 1692 * stay as reader and return the fast-path header to IP. 1693 */ 1694 if (dsp->ds_mode != DLD_FASTPATH) { 1695 if (!rw_tryupgrade(&dsp->ds_lock)) { 1696 rw_exit(&dsp->ds_lock); 1697 rw_enter(&dsp->ds_lock, RW_WRITER); 1698 1699 /* 1700 * State may have changed before we re-acquired 1701 * the writer lock in case the upgrade failed. 1702 */ 1703 if (dsp->ds_dlstate != DL_IDLE) { 1704 rw_exit(&dsp->ds_lock); 1705 err = ENOTSUP; 1706 goto failed; 1707 } 1708 } 1709 1710 /* 1711 * Set the receive callback (unless polling is enabled). 1712 */ 1713 if (!dsp->ds_polling && !dsp->ds_soft_ring) 1714 dls_rx_set(dc, dld_str_rx_fastpath, (void *)dsp); 1715 1716 /* 1717 * Note that fast-path mode is enabled. 1718 */ 1719 dsp->ds_mode = DLD_FASTPATH; 1720 } 1721 rw_exit(&dsp->ds_lock); 1722 1723 freemsg(nmp->b_cont); 1724 nmp->b_cont = hmp; 1725 1726 miocack(q, mp, MBLKL(nmp) + MBLKL(hmp), 0); 1727 return; 1728 failed: 1729 miocnak(q, mp, 0, err); 1730 } 1731 1732 /* 1733 * Catch-all handler. 1734 */ 1735 static void 1736 ioc(dld_str_t *dsp, mblk_t *mp) 1737 { 1738 queue_t *q = dsp->ds_wq; 1739 mac_handle_t mh; 1740 1741 rw_enter(&dsp->ds_lock, RW_READER); 1742 if (dsp->ds_dlstate == DL_UNATTACHED) { 1743 rw_exit(&dsp->ds_lock); 1744 miocnak(q, mp, 0, EINVAL); 1745 return; 1746 } 1747 mh = dsp->ds_mh; 1748 ASSERT(mh != NULL); 1749 rw_exit(&dsp->ds_lock); 1750 mac_ioctl(mh, q, mp); 1751 } 1752 1753 /* 1754 * Allocate a new minor number. 1755 */ 1756 static minor_t 1757 dld_minor_hold(boolean_t sleep) 1758 { 1759 minor_t minor; 1760 1761 /* 1762 * Grab a value from the arena. 1763 */ 1764 atomic_add_32(&minor_count, 1); 1765 if ((minor = PTR_TO_MINOR(vmem_alloc(minor_arenap, 1, 1766 (sleep) ? VM_SLEEP : VM_NOSLEEP))) == 0) { 1767 atomic_add_32(&minor_count, -1); 1768 return (0); 1769 } 1770 1771 return (minor); 1772 } 1773 1774 /* 1775 * Release a previously allocated minor number. 1776 */ 1777 static void 1778 dld_minor_rele(minor_t minor) 1779 { 1780 /* 1781 * Return the value to the arena. 1782 */ 1783 vmem_free(minor_arenap, MINOR_TO_PTR(minor), 1); 1784 1785 atomic_add_32(&minor_count, -1); 1786 } 1787