Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / uts / common / io / rtls / rtls.c
blobc005375be2071db3a1a252fd121a25068094567f
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
28 * rtls -- REALTEK 8139-serials PCI Fast Ethernet Driver, Depends on the
29 * Generic LAN Driver utility functions in /kernel/misc/mac
31 * This product is covered by one or more of the following patents:
32 * US5,307,459, US5,434,872, US5,732,094, US6,570,884, US6,115,776, and
33 * US6,327,625.
35 * Currently supports:
36 * RTL8139
39 #include <sys/types.h>
40 #include <sys/debug.h>
41 #include <sys/errno.h>
43 #include <sys/stropts.h>
44 #include <sys/stream.h>
45 #include <sys/kmem.h>
46 #include <sys/conf.h>
47 #include <sys/ddi.h>
48 #include <sys/devops.h>
49 #include <sys/ksynch.h>
50 #include <sys/stat.h>
51 #include <sys/conf.h>
52 #include <sys/modctl.h>
53 #include <sys/dlpi.h>
54 #include <sys/ethernet.h>
55 #include <sys/vlan.h>
56 #include <sys/strsun.h>
57 #include <sys/pci.h>
58 #include <sys/sunddi.h>
59 #include <sys/mii.h>
60 #include <sys/miiregs.h>
61 #include <sys/mac_provider.h>
62 #include <sys/mac_ether.h>
64 #include "rtls.h"
67 * Declarations and Module Linkage
71 * This is the string displayed by modinfo, etc.
73 static char rtls_ident[] = "RealTek 8139 Ethernet driver";
75 #ifdef RTLS_DEBUG
76 int rtls_debug = 0;
77 #endif
80 * Required system entry points
82 static int rtls_attach(dev_info_t *, ddi_attach_cmd_t);
83 static int rtls_detach(dev_info_t *, ddi_detach_cmd_t);
84 static int rtls_quiesce(dev_info_t *);
87 * Required driver entry points for MAC
89 static int rtls_m_start(void *);
90 static void rtls_m_stop(void *);
91 static int rtls_m_unicst(void *, const uint8_t *);
92 static int rtls_m_multicst(void *, boolean_t, const uint8_t *);
93 static int rtls_m_promisc(void *, boolean_t);
94 static mblk_t *rtls_m_tx(void *, mblk_t *);
95 static int rtls_m_getprop(void *, const char *, mac_prop_id_t, uint_t,
96 void *);
97 static int rtls_m_setprop(void *, const char *, mac_prop_id_t, uint_t,
98 const void *);
99 static void rtls_m_propinfo(void *, const char *, mac_prop_id_t,
100 mac_prop_info_handle_t);
101 static int rtls_m_stat(void *, uint_t, uint64_t *);
103 static uint_t rtls_intr(caddr_t);
106 * MII entry points
108 static uint16_t rtls_mii_read(void *, uint8_t, uint8_t);
109 static void rtls_mii_write(void *, uint8_t, uint8_t, uint16_t);
110 static void rtls_mii_notify(void *, link_state_t);
113 * Internal functions used by the above entry points
115 static int rtls_chip_reset(rtls_t *, boolean_t);
116 static void rtls_chip_init(rtls_t *);
117 static void rtls_chip_stop(rtls_t *rtlsp);
118 static void rtls_chip_start(rtls_t *rtlsp);
119 static void rtls_chip_restart(rtls_t *rtlsp);
120 static void rtls_get_mac_addr(rtls_t *, uint8_t *);
121 static void rtls_set_mac_addr(rtls_t *, const uint8_t *);
122 static uint_t rtls_hash_index(const uint8_t *);
123 static boolean_t rtls_send(rtls_t *, mblk_t *);
124 static void rtls_receive(rtls_t *);
127 * Buffer Management Routines
129 static int rtls_alloc_bufs(rtls_t *);
130 static void rtls_free_bufs(rtls_t *);
131 static int rtls_alloc_dma_mem(rtls_t *, size_t, ddi_device_acc_attr_t *,
132 uint_t, dma_area_t *);
133 static void rtls_free_dma_mem(dma_area_t *);
135 #ifdef RTLS_DEBUG
136 static void rtls_reg_print(rtls_t *); /* debug routine */
137 #endif
139 #define RTLS_DRIVER_NAME "rtls"
142 * Used for buffers allocated by ddi_dma_mem_alloc()
144 static ddi_dma_attr_t dma_attr = {
145 DMA_ATTR_V0, /* dma_attr version */
146 0, /* dma_attr_addr_lo */
147 (uint_t)0xFFFFFFFF, /* dma_attr_addr_hi */
148 0x7FFFFFFF, /* dma_attr_count_max */
149 4, /* dma_attr_align */
150 0x3F, /* dma_attr_burstsizes */
151 1, /* dma_attr_minxfer */
152 (uint_t)0xFFFFFFFF, /* dma_attr_maxxfer */
153 (uint_t)0xFFFFFFFF, /* dma_attr_seg */
154 1, /* dma_attr_sgllen */
155 1, /* dma_attr_granular */
156 0, /* dma_attr_flags */
160 * PIO access attributes for registers
162 static ddi_device_acc_attr_t rtls_reg_accattr = {
163 DDI_DEVICE_ATTR_V0,
164 DDI_STRUCTURE_LE_ACC,
165 DDI_STRICTORDER_ACC
169 * DMA access attributes for data
171 static ddi_device_acc_attr_t rtls_buf_accattr = {
172 DDI_DEVICE_ATTR_V0,
173 DDI_NEVERSWAP_ACC,
174 DDI_STRICTORDER_ACC
177 uchar_t rtls_broadcastaddr[] = {
178 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
181 static mac_callbacks_t rtls_m_callbacks = {
182 MC_PROPERTIES,
183 rtls_m_stat,
184 rtls_m_start,
185 rtls_m_stop,
186 rtls_m_promisc,
187 rtls_m_multicst,
188 rtls_m_unicst,
189 rtls_m_tx,
190 NULL,
191 NULL, /* mc_ioctl */
192 NULL, /* mc_getcapab */
193 NULL, /* mc_open */
194 NULL, /* mc_close */
195 rtls_m_setprop,
196 rtls_m_getprop,
197 rtls_m_propinfo
200 static mii_ops_t rtls_mii_ops = {
201 MII_OPS_VERSION,
202 rtls_mii_read,
203 rtls_mii_write,
204 rtls_mii_notify, /* notify */
205 NULL, /* reset */
208 DDI_DEFINE_STREAM_OPS(rtls_dev_ops, nulldev, nulldev, rtls_attach, rtls_detach,
209 nodev, NULL, D_MP, NULL, rtls_quiesce);
212 * Standard module linkage initialization for a MAC driver
214 static struct modldrv rtls_modldrv = {
215 &mod_driverops, /* type of module. This one is a driver */
216 rtls_ident, /* short description */
217 &rtls_dev_ops /* driver specific ops */
220 static struct modlinkage modlinkage = {
221 MODREV_1, { (void *)&rtls_modldrv, NULL }
225 * ========== RealTek chip register access Routines ==========
227 static uint8_t rtls_reg_get8(rtls_t *rtlsp, uint32_t reg);
228 #pragma inline(rtls_reg_get8)
229 static uint8_t
230 rtls_reg_get8(rtls_t *rtlsp, uint32_t reg)
232 uint8_t *addr;
234 addr = REG8(rtlsp->io_reg, reg);
235 return (ddi_get8(rtlsp->io_handle, addr));
238 static uint16_t rtls_reg_get16(rtls_t *rtlsp, uint32_t reg);
239 #pragma inline(rtls_reg_get16)
240 static uint16_t
241 rtls_reg_get16(rtls_t *rtlsp, uint32_t reg)
243 uint16_t *addr;
245 addr = REG16(rtlsp->io_reg, reg);
246 return (ddi_get16(rtlsp->io_handle, addr));
249 static uint32_t rtls_reg_get32(rtls_t *rtlsp, uint32_t reg);
250 #pragma inline(rtls_reg_get32)
251 static uint32_t
252 rtls_reg_get32(rtls_t *rtlsp, uint32_t reg)
254 uint32_t *addr;
256 addr = REG32(rtlsp->io_reg, reg);
257 return (ddi_get32(rtlsp->io_handle, addr));
260 static void rtls_reg_set8(rtls_t *rtlsp, uint32_t reg, uint8_t value);
261 #pragma inline(rtls_reg_set8)
262 static void
263 rtls_reg_set8(rtls_t *rtlsp, uint32_t reg, uint8_t value)
265 uint8_t *addr;
267 addr = REG8(rtlsp->io_reg, reg);
268 ddi_put8(rtlsp->io_handle, addr, value);
271 static void rtls_reg_set16(rtls_t *rtlsp, uint32_t reg, uint16_t value);
272 #pragma inline(rtls_reg_set16)
273 static void
274 rtls_reg_set16(rtls_t *rtlsp, uint32_t reg, uint16_t value)
276 uint16_t *addr;
278 addr = REG16(rtlsp->io_reg, reg);
279 ddi_put16(rtlsp->io_handle, addr, value);
282 static void rtls_reg_set32(rtls_t *rtlsp, uint32_t reg, uint32_t value);
283 #pragma inline(rtls_reg_set32)
284 static void
285 rtls_reg_set32(rtls_t *rtlsp, uint32_t reg, uint32_t value)
287 uint32_t *addr;
289 addr = REG32(rtlsp->io_reg, reg);
290 ddi_put32(rtlsp->io_handle, addr, value);
294 * ========== Module Loading Entry Points ==========
297 _init(void)
299 int rv;
301 mac_init_ops(&rtls_dev_ops, RTLS_DRIVER_NAME);
302 if ((rv = mod_install(&modlinkage)) != DDI_SUCCESS) {
303 mac_fini_ops(&rtls_dev_ops);
305 return (rv);
309 _fini(void)
311 int rv;
313 if ((rv = mod_remove(&modlinkage)) == DDI_SUCCESS) {
314 mac_fini_ops(&rtls_dev_ops);
316 return (rv);
320 _info(struct modinfo *modinfop)
322 return (mod_info(&modlinkage, modinfop));
327 * ========== DDI Entry Points ==========
331 * attach(9E) -- Attach a device to the system
333 * Called once for each board successfully probed.
335 static int
336 rtls_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
338 rtls_t *rtlsp; /* Our private device info */
339 ddi_acc_handle_t pci_handle;
340 uint16_t pci_commond;
341 uint16_t vendorid;
342 uint16_t deviceid;
343 uint32_t device;
344 mac_register_t *macp;
345 int err;
347 switch (cmd) {
348 case DDI_ATTACH:
349 break;
350 case DDI_RESUME:
351 if ((rtlsp = ddi_get_driver_private(devinfo)) == NULL) {
352 return (DDI_FAILURE);
354 mutex_enter(&rtlsp->rtls_io_lock);
355 mutex_enter(&rtlsp->rtls_rx_lock);
356 mutex_enter(&rtlsp->rtls_tx_lock);
358 * Turn on Master Enable (DMA) and IO Enable bits.
359 * Enable PCI Memory Space accesses
360 * Disable Memory Write/Invalidate
362 if (pci_config_setup(devinfo, &pci_handle) != DDI_SUCCESS) {
363 mutex_exit(&rtlsp->rtls_tx_lock);
364 mutex_exit(&rtlsp->rtls_rx_lock);
365 mutex_exit(&rtlsp->rtls_io_lock);
366 return (DDI_FAILURE);
368 pci_commond = pci_config_get16(pci_handle, PCI_CONF_COMM);
369 pci_commond &= ~PCI_COMM_MEMWR_INVAL;
370 pci_commond |= PCI_COMM_ME | PCI_COMM_MAE | PCI_COMM_IO;
371 pci_config_put32(pci_handle, PCI_CONF_COMM, pci_commond);
372 pci_config_teardown(&pci_handle);
374 rtls_chip_restart(rtlsp);
375 rtlsp->chip_error = B_FALSE;
376 rtlsp->tx_retry = 0;
377 rtlsp->rtls_suspended = B_FALSE;
378 mutex_exit(&rtlsp->rtls_tx_lock);
379 mutex_exit(&rtlsp->rtls_rx_lock);
380 mutex_exit(&rtlsp->rtls_io_lock);
382 mii_resume(rtlsp->mii);
384 mac_tx_update(rtlsp->mh);
385 return (DDI_SUCCESS);
386 default:
387 return (DDI_FAILURE);
391 * we don't support high level interrupts in the driver
393 if (ddi_intr_hilevel(devinfo, 0) != 0) {
394 cmn_err(CE_WARN, "unsupported high level interrupt");
395 return (DDI_FAILURE);
399 * Get handle to access pci configuration space
401 if (pci_config_setup(devinfo, &pci_handle) != DDI_SUCCESS) {
402 cmn_err(CE_WARN, "pci_config_setup fail.");
403 return (DDI_FAILURE);
407 * Make sure we support this particular vendor/device
409 vendorid = pci_config_get16(pci_handle, PCI_CONF_VENID);
410 deviceid = pci_config_get16(pci_handle, PCI_CONF_DEVID);
411 device = vendorid;
412 device = (device << 16) | deviceid; /* combine two id together */
415 * See if we support this device
416 * We do not return for wrong device id. It's user risk.
418 switch (device) {
419 default:
420 cmn_err(CE_WARN,
421 "RTLS doesn't support this device: "
422 "vendorID = 0x%x, deviceID = 0x%x",
423 vendorid, deviceid);
424 break;
425 case RTLS_SUPPORT_DEVICE_1:
426 case RTLS_SUPPORT_DEVICE_2:
427 case RTLS_SUPPORT_DEVICE_3:
428 case RTLS_SUPPORT_DEVICE_4:
429 break;
433 * Turn on Master Enable (DMA) and IO Enable bits.
434 * Enable PCI Memory Space accesses
435 * Disable Memory Write/Invalidate
437 pci_commond = pci_config_get16(pci_handle, PCI_CONF_COMM);
438 pci_commond &= ~PCI_COMM_MEMWR_INVAL;
439 pci_commond |= PCI_COMM_ME | PCI_COMM_MAE | PCI_COMM_IO;
440 pci_config_put32(pci_handle, PCI_CONF_COMM, pci_commond);
443 * Free handle to access pci configuration space
445 pci_config_teardown(&pci_handle);
447 rtlsp = kmem_zalloc(sizeof (rtls_t), KM_SLEEP);
449 ddi_set_driver_private(devinfo, rtlsp);
450 rtlsp->devinfo = devinfo;
451 rtlsp->instance = ddi_get_instance(devinfo);
454 * Map operating register
456 err = ddi_regs_map_setup(devinfo, 1, &rtlsp->io_reg,
457 0, 0, &rtls_reg_accattr, &rtlsp->io_handle);
458 if (err != DDI_SUCCESS) {
459 kmem_free((caddr_t)rtlsp, sizeof (rtls_t));
460 cmn_err(CE_WARN, "ddi_regs_map_setup fail.");
461 return (DDI_FAILURE);
465 * Allocate the TX and RX descriptors/buffers
467 if (rtls_alloc_bufs(rtlsp) == DDI_FAILURE) {
468 cmn_err(CE_WARN, "DMA buffer allocation fail.");
469 goto fail;
473 * Reset the chip
475 err = rtls_chip_reset(rtlsp, B_FALSE);
476 if (err != DDI_SUCCESS)
477 goto fail;
480 * Init rtls_t structure
482 rtls_get_mac_addr(rtlsp, rtlsp->netaddr);
485 * Add the interrupt handler
487 * This will prevent receiving interrupts before device is ready, as
488 * we are initializing device after setting the interrupts. So we
489 * will not get our interrupt handler invoked by OS while our device
490 * is still coming up or timer routines will not start till we are
491 * all set to process...
494 if (ddi_add_intr(devinfo, 0, &rtlsp->iblk, NULL, rtls_intr,
495 (caddr_t)rtlsp) != DDI_SUCCESS) {
496 cmn_err(CE_WARN, "ddi_add_intr fail.");
497 goto late_fail;
500 if ((rtlsp->mii = mii_alloc(rtlsp, devinfo, &rtls_mii_ops)) == NULL) {
501 ddi_remove_intr(devinfo, 0, rtlsp->iblk);
502 goto late_fail;
505 * Note: Some models of 8139 can support pause, but we have
506 * not implemented support for it at this time. This might be
507 * an interesting feature to add later.
509 mii_set_pauseable(rtlsp->mii, B_FALSE, B_FALSE);
511 if ((macp = mac_alloc(MAC_VERSION)) == NULL) {
512 cmn_err(CE_WARN, "mac_alloc fail.");
513 ddi_remove_intr(devinfo, 0, rtlsp->iblk);
514 goto late_fail;
518 * Init mutex
520 mutex_init(&rtlsp->rtls_io_lock, NULL, MUTEX_DRIVER, rtlsp->iblk);
521 mutex_init(&rtlsp->rtls_tx_lock, NULL, MUTEX_DRIVER, rtlsp->iblk);
522 mutex_init(&rtlsp->rtls_rx_lock, NULL, MUTEX_DRIVER, rtlsp->iblk);
525 * Initialize pointers to device specific functions which will be
526 * used by the generic layer.
528 macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
529 macp->m_driver = rtlsp;
530 macp->m_dip = devinfo;
531 macp->m_src_addr = rtlsp->netaddr;
532 macp->m_callbacks = &rtls_m_callbacks;
533 macp->m_min_sdu = 0;
534 macp->m_max_sdu = ETHERMTU;
535 macp->m_margin = VLAN_TAGSZ;
537 if (mac_register(macp, &rtlsp->mh) != 0) {
538 ddi_remove_intr(devinfo, 0, rtlsp->iblk);
539 mutex_destroy(&rtlsp->rtls_io_lock);
540 mutex_destroy(&rtlsp->rtls_tx_lock);
541 mutex_destroy(&rtlsp->rtls_rx_lock);
542 goto late_fail;
545 mac_free(macp);
547 return (DDI_SUCCESS);
549 late_fail:
550 if (macp)
551 mac_free(macp);
552 if (rtlsp->mii)
553 mii_free(rtlsp->mii);
555 fail:
556 ddi_regs_map_free(&rtlsp->io_handle);
557 rtls_free_bufs(rtlsp);
558 kmem_free(rtlsp, sizeof (rtls_t));
560 return (DDI_FAILURE);
564 * detach(9E) -- Detach a device from the system
566 static int
567 rtls_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
569 rtls_t *rtlsp; /* our private device info */
572 * Get the driver private structure
574 if ((rtlsp = ddi_get_driver_private(devinfo)) == NULL) {
575 return (DDI_FAILURE);
578 switch (cmd) {
579 case DDI_DETACH:
580 break;
582 case DDI_SUSPEND:
583 mii_suspend(rtlsp->mii);
585 mutex_enter(&rtlsp->rtls_io_lock);
586 mutex_enter(&rtlsp->rtls_rx_lock);
587 mutex_enter(&rtlsp->rtls_tx_lock);
589 rtlsp->rtls_suspended = B_TRUE;
590 rtls_chip_stop(rtlsp);
592 mutex_exit(&rtlsp->rtls_tx_lock);
593 mutex_exit(&rtlsp->rtls_rx_lock);
594 mutex_exit(&rtlsp->rtls_io_lock);
595 return (DDI_SUCCESS);
597 default:
598 return (DDI_FAILURE);
601 if (mac_unregister(rtlsp->mh) != 0) {
602 /* device busy */
603 return (DDI_FAILURE);
606 ddi_remove_intr(devinfo, 0, rtlsp->iblk);
608 mii_free(rtlsp->mii);
610 mutex_destroy(&rtlsp->rtls_io_lock);
611 mutex_destroy(&rtlsp->rtls_tx_lock);
612 mutex_destroy(&rtlsp->rtls_rx_lock);
614 ddi_regs_map_free(&rtlsp->io_handle);
615 rtls_free_bufs(rtlsp);
616 kmem_free(rtlsp, sizeof (rtls_t));
618 return (DDI_SUCCESS);
622 * quiesce(9E) entry point.
624 * This function is called when the system is single-threaded at high
625 * PIL with preemption disabled. Therefore, this function must not be
626 * blocked.
628 * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
629 * DDI_FAILURE indicates an error condition and should almost never happen.
631 static int
632 rtls_quiesce(dev_info_t *devinfo)
634 rtls_t *rtlsp; /* our private device info */
637 * Get the driver private structure
639 if ((rtlsp = ddi_get_driver_private(devinfo)) == NULL) {
640 return (DDI_FAILURE);
642 return (rtls_chip_reset(rtlsp, B_TRUE));
646 * ========== MAC Entry Points ==========
650 * rtls_m_start() -- start the board receiving and allow transmits
652 static int
653 rtls_m_start(void *arg)
655 rtls_t *rtlsp = (rtls_t *)arg;
657 mutex_enter(&rtlsp->rtls_io_lock);
658 mutex_enter(&rtlsp->rtls_rx_lock);
659 mutex_enter(&rtlsp->rtls_tx_lock);
661 if (!rtlsp->rtls_suspended)
662 rtls_chip_restart(rtlsp);
664 rtlsp->rtls_running = B_TRUE;
666 mutex_exit(&rtlsp->rtls_tx_lock);
667 mutex_exit(&rtlsp->rtls_rx_lock);
668 mutex_exit(&rtlsp->rtls_io_lock);
670 drv_usecwait(100);
672 mii_start(rtlsp->mii);
674 return (0);
678 * rtls_m_stop() -- stop board receiving and transmits
680 static void
681 rtls_m_stop(void *arg)
683 rtls_t *rtlsp = (rtls_t *)arg;
685 mii_stop(rtlsp->mii);
687 mutex_enter(&rtlsp->rtls_io_lock);
689 if (!rtlsp->rtls_suspended)
690 rtls_chip_stop(rtlsp);
691 rtlsp->rtls_running = B_FALSE;
693 mutex_exit(&rtlsp->rtls_io_lock);
697 * rtls_m_unicst() -- set the physical network address
698 * on the board
700 static int
701 rtls_m_unicst(void *arg, const uint8_t *macaddr)
703 rtls_t *rtlsp = arg;
705 mutex_enter(&rtlsp->rtls_io_lock);
706 bcopy(macaddr, rtlsp->netaddr, ETHERADDRL);
707 if (!rtlsp->rtls_suspended)
708 rtls_set_mac_addr(rtlsp, rtlsp->netaddr);
709 mutex_exit(&rtlsp->rtls_io_lock);
710 return (0);
714 * rtls_m_multicst() -- set(enable) or disable a multicast address
716 * Program the hardware to enable/disable the multicast address in "mcast".
718 static int
719 rtls_m_multicst(void *arg, boolean_t enable, const uint8_t *mcast)
721 rtls_t *rtlsp = (rtls_t *)arg;
722 uint_t index;
723 uint32_t *hashp;
725 mutex_enter(&rtlsp->rtls_io_lock);
726 hashp = rtlsp->multi_hash;
727 index = rtls_hash_index(mcast);
728 /* index value is between 0 and 63 */
730 if (enable) {
731 if (rtlsp->multicast_cnt[index]++) {
732 mutex_exit(&rtlsp->rtls_io_lock);
733 return (0);
735 hashp[index/32] |= 1<< (index % 32);
736 } else {
737 if (--rtlsp->multicast_cnt[index]) {
738 mutex_exit(&rtlsp->rtls_io_lock);
739 return (0);
741 hashp[index/32] &= ~(1<< (index % 32));
745 * Set multicast register
747 if (!rtlsp->rtls_suspended) {
748 rtls_reg_set32(rtlsp, MULTICAST_0_REG, hashp[0]);
749 rtls_reg_set32(rtlsp, MULTICAST_4_REG, hashp[1]);
752 mutex_exit(&rtlsp->rtls_io_lock);
754 return (0);
758 * rtls_hash_index() -- a hashing function used for setting the
759 * node address or a multicast address
761 static uint_t
762 rtls_hash_index(const uint8_t *address)
764 uint32_t crc = (ulong_t)RTLS_HASH_CRC;
765 uint32_t const POLY = RTLS_HASH_POLY;
766 uint32_t msb;
767 int bytes;
768 uchar_t currentbyte;
769 uint_t index;
770 int bit;
772 for (bytes = 0; bytes < ETHERADDRL; bytes++) {
773 currentbyte = address[bytes];
774 for (bit = 0; bit < 8; bit++) {
775 msb = crc >> 31;
776 crc <<= 1;
777 if (msb ^ (currentbyte & 1)) {
778 crc ^= POLY;
779 crc |= 0x00000001;
781 currentbyte >>= 1;
785 index = crc >> 26;
787 return (index);
791 * rtls_m_promisc() -- set or reset promiscuous mode on the board
793 static int
794 rtls_m_promisc(void *arg, boolean_t on)
796 rtls_t *rtlsp = arg;
798 mutex_enter(&rtlsp->rtls_io_lock);
800 rtlsp->promisc = on;
801 if (!rtlsp->rtls_suspended) {
802 uint32_t val32 = rtls_reg_get32(rtlsp, RX_CONFIG_REG);
803 if (on) {
804 val32 |= RX_ACCEPT_ALL_PACKET;
805 } else {
806 val32 &= ~RX_ACCEPT_ALL_PACKET;
808 rtls_reg_set32(rtlsp, RX_CONFIG_REG, val32);
810 mutex_exit(&rtlsp->rtls_io_lock);
812 return (0);
816 * rtls_m_stat() -- retrieve statistic
818 * MAC calls this routine just before it reads the driver's statistics
819 * structure. If your board maintains statistics, this is the time to
820 * read them in and update the values in the structure. If the driver
821 * maintains statistics continuously, this routine need do nothing.
823 static int
824 rtls_m_stat(void *arg, uint_t stat, uint64_t *val)
826 rtls_t *rtlsp = arg;
828 if (mii_m_getstat(rtlsp->mii, stat, val) == 0) {
829 return (0);
832 switch (stat) {
833 case MAC_STAT_IPACKETS:
834 *val = rtlsp->stats.ipackets;
835 break;
836 case MAC_STAT_RBYTES:
837 *val = rtlsp->stats.rbytes;
838 break;
839 case MAC_STAT_OPACKETS:
840 *val = rtlsp->stats.opackets;
841 break;
842 case MAC_STAT_OBYTES:
843 *val = rtlsp->stats.obytes;
844 break;
845 case MAC_STAT_IERRORS:
846 *val = rtlsp->stats.rcv_err;
847 break;
848 case MAC_STAT_OERRORS:
849 *val = rtlsp->stats.xmt_err;
850 break;
851 case MAC_STAT_MULTIRCV:
852 *val = rtlsp->stats.multi_rcv;
853 break;
854 case MAC_STAT_BRDCSTRCV:
855 *val = rtlsp->stats.brdcst_rcv;
856 break;
857 case MAC_STAT_MULTIXMT:
858 *val = rtlsp->stats.multi_xmt;
859 break;
860 case MAC_STAT_BRDCSTXMT:
861 *val = rtlsp->stats.brdcst_xmt;
862 break;
863 case MAC_STAT_UNDERFLOWS:
864 *val = rtlsp->stats.underflow;
865 break;
866 case MAC_STAT_OVERFLOWS:
867 *val = rtlsp->stats.overflow;
868 break;
869 case MAC_STAT_NORCVBUF:
870 *val = rtlsp->stats.no_rcvbuf;
871 break;
872 case MAC_STAT_COLLISIONS:
873 *val = rtlsp->stats.collisions;
874 break;
875 case ETHER_STAT_FCS_ERRORS:
876 *val = rtlsp->stats.crc_err;
877 break;
878 case ETHER_STAT_ALIGN_ERRORS:
879 *val = rtlsp->stats.frame_err;
880 break;
881 case ETHER_STAT_DEFER_XMTS:
882 *val = rtlsp->stats.defer;
883 break;
884 case ETHER_STAT_TX_LATE_COLLISIONS:
885 *val = rtlsp->stats.xmt_latecoll;
886 break;
887 case ETHER_STAT_TOOLONG_ERRORS:
888 *val = rtlsp->stats.too_long;
889 break;
890 case ETHER_STAT_TOOSHORT_ERRORS:
891 *val = rtlsp->stats.in_short;
892 break;
893 case ETHER_STAT_CARRIER_ERRORS:
894 *val = rtlsp->stats.no_carrier;
895 break;
896 case ETHER_STAT_FIRST_COLLISIONS:
897 *val = rtlsp->stats.firstcol;
898 break;
899 case ETHER_STAT_MULTI_COLLISIONS:
900 *val = rtlsp->stats.multicol;
901 break;
902 default:
903 return (ENOTSUP);
907 * RTL8139 don't support MII statistics,
908 * these values are maintained by the driver software.
911 #ifdef RTLS_DEBUG
912 if (rtls_debug & RTLS_TRACE)
913 rtls_reg_print(rtlsp);
914 #endif
916 return (0);
920 rtls_m_getprop(void *arg, const char *name, mac_prop_id_t num, uint_t sz,
921 void *val)
923 rtls_t *rtlsp = arg;
925 return (mii_m_getprop(rtlsp->mii, name, num, sz, val));
929 rtls_m_setprop(void *arg, const char *name, mac_prop_id_t num, uint_t sz,
930 const void *val)
932 rtls_t *rtlsp = arg;
934 return (mii_m_setprop(rtlsp->mii, name, num, sz, val));
937 static void
938 rtls_m_propinfo(void *arg, const char *name, mac_prop_id_t num,
939 mac_prop_info_handle_t prh)
941 rtls_t *rtlsp = arg;
943 mii_m_propinfo(rtlsp->mii, name, num, prh);
947 * rtls_send() -- send a packet
949 * Called when a packet is ready to be transmitted. A pointer to an
950 * M_DATA message that contains the packet is passed to this routine.
951 * The complete LLC header is contained in the message's first message
952 * block, and the remainder of the packet is contained within
953 * additional M_DATA message blocks linked to the first message block.
955 * Returns B_TRUE if the packet was properly disposed of, or B_FALSE if
956 * if the packet is being deferred and should be tried again later.
959 static boolean_t
960 rtls_send(rtls_t *rtlsp, mblk_t *mp)
962 int totlen;
963 int ncc;
964 uint16_t cur_desc;
965 uint32_t tx_status;
967 ASSERT(mp != NULL);
968 ASSERT(rtlsp->rtls_running);
970 mutex_enter(&rtlsp->rtls_tx_lock);
972 if (rtlsp->rtls_suspended) {
973 mutex_exit(&rtlsp->rtls_tx_lock);
974 return (B_FALSE);
978 * If chip error ...
980 if (rtlsp->chip_error) {
981 #ifdef RTLS_DEBUG
982 cmn_err(CE_WARN,
983 "%s: send fail--CHIP ERROR!",
984 mac_name(rtlsp->mh));
985 #endif
986 mutex_exit(&rtlsp->rtls_tx_lock);
987 freemsg(mp);
988 return (B_TRUE);
992 * If chip link down ... Note that experimentation shows that
993 * the device seems not to care about whether or not we have
994 * this check, but if we don't add the check here, it might
995 * not be properly reported as a carrier error.
997 if (rtls_reg_get8(rtlsp, MEDIA_STATUS_REG) & MEDIA_STATUS_LINK) {
998 #ifdef RTLS_DEBUG
999 cmn_err(CE_WARN,
1000 "%s: send fail--LINK DOWN!",
1001 mac_name(rtlsp->mh));
1002 #endif
1003 rtlsp->stats.no_carrier++;
1004 mutex_exit(&rtlsp->rtls_tx_lock);
1005 freemsg(mp);
1006 return (B_TRUE);
1010 * Current transmit descriptor
1012 cur_desc = rtlsp->tx_current_desc;
1013 ASSERT(cur_desc < RTLS_MAX_TX_DESC);
1016 * RealTek 8139 has 4 tx descriptor for transmit. In the first tx loop
1017 * of transmit,we needn't judge transmit status.
1019 if (rtlsp->tx_first_loop < RTLS_MAX_TX_DESC) {
1020 rtlsp->tx_first_loop++;
1021 goto tx_ready;
1025 * If it's not the first tx loop, we need judge whether the chip is
1026 * busy or not. Otherwise, we have to reschedule send and wait...
1028 tx_status = rtls_reg_get32(rtlsp, TX_STATUS_DESC0_REG + 4 * cur_desc);
1031 * H/W doesn't complete packet transmit
1033 if (!(tx_status & TX_COMPLETE_FLAG)) {
1034 #ifdef RTLS_DEBUG
1035 if (rtls_debug & RTLS_SEND) {
1036 cmn_err(CE_NOTE,
1037 "%s: rtls_send: need_sched", mac_name(rtlsp->mh));
1039 #endif
1041 * Through test, we find RTL8139 tx status might be
1042 * not-completing all along. We have to reset chip
1043 * to make RTL8139 tansmit re-work.
1045 if (rtlsp->tx_retry++ > RTLS_TX_RETRY_NUM) {
1048 * Wait transmit h/w more time...
1050 RTLS_TX_WAIT_TIMEOUT; /* 100 ms */
1053 * Judge tx status again, if it remains not-completing,
1054 * we can confirm RTL8139 is in chip error state
1055 * and must reset it.
1057 tx_status = rtls_reg_get32(rtlsp,
1058 TX_STATUS_DESC0_REG + 4 * cur_desc);
1059 if (!(tx_status & TX_COMPLETE_FLAG)) {
1060 #ifdef RTLS_DEBUG
1061 cmn_err(CE_NOTE, "%s: tx chip_error = 0x%x",
1062 mac_name(rtlsp->mh), tx_status);
1063 #endif
1064 rtlsp->tx_retry = 0;
1065 rtlsp->chip_error = B_TRUE;
1066 rtlsp->stats.xmt_err++;
1067 rtlsp->stats.mac_xmt_err++;
1068 mutex_exit(&rtlsp->rtls_tx_lock);
1069 freemsg(mp);
1070 return (B_TRUE);
1072 } else {
1073 rtlsp->stats.defer++;
1074 rtlsp->need_sched = B_TRUE;
1075 mutex_exit(&rtlsp->rtls_tx_lock);
1076 return (B_FALSE);
1081 * Transmit error?
1083 if (tx_status & TX_ERR_FLAG) {
1084 #ifdef RTLS_DEBUG
1085 if (rtls_debug & RTLS_SEND) {
1086 cmn_err(CE_NOTE, "%s: transmit error, status = 0x%x",
1087 mac_name(rtlsp->mh), tx_status);
1089 #endif
1090 rtlsp->stats.xmt_err++;
1091 if (tx_status & TX_STATUS_TX_UNDERRUN)
1092 rtlsp->stats.underflow++;
1093 if (tx_status & TX_STATUS_CS_LOST)
1094 rtlsp->stats.no_carrier++;
1095 if (tx_status & TX_STATUS_OWC)
1096 rtlsp->stats.xmt_latecoll++;
1098 ncc = ((tx_status & TX_STATUS_NCC) >> TX_STATUS_NCC_SHIFT);
1099 if (ncc != 0) {
1100 rtlsp->stats.collisions += ncc;
1101 rtlsp->stats.firstcol++;
1102 rtlsp->stats.multicol += ncc - 1;
1105 tx_ready:
1107 * Initialize variable
1109 rtlsp->tx_retry = 0;
1110 totlen = 0;
1113 * Copy packet to tx descriptor buffer
1115 totlen = msgsize(mp);
1116 if (totlen > (ETHERMAX + 4)) { /* 4 bytes for VLAN header */
1117 cmn_err(CE_NOTE,
1118 "%s: rtls_send: try to send large %d packet",
1119 mac_name(rtlsp->mh), totlen);
1120 rtlsp->stats.mac_xmt_err++;
1121 rtlsp->stats.xmt_err++;
1122 freemsg(mp);
1123 mutex_exit(&rtlsp->rtls_tx_lock);
1124 return (B_TRUE);
1127 /* this will free the mblk */
1128 mcopymsg(mp, rtlsp->tx_buf[cur_desc]);
1130 /* update stats */
1131 if (*rtlsp->tx_buf[cur_desc] & 0x1) {
1132 uint16_t *ptr = (void *)rtlsp->tx_buf[cur_desc];
1133 if ((ptr[0] == 0xffff) &&
1134 (ptr[1] == 0xffff) &&
1135 (ptr[2] == 0xffff)) {
1136 rtlsp->stats.brdcst_xmt++;
1137 } else {
1138 rtlsp->stats.multi_xmt++;
1141 rtlsp->stats.opackets++;
1142 rtlsp->stats.obytes += totlen;
1144 if (totlen < ETHERMIN) {
1145 bzero(rtlsp->tx_buf[cur_desc] + totlen, ETHERMIN - totlen);
1146 totlen = ETHERMIN;
1149 /* make sure caches are flushed */
1150 (void) ddi_dma_sync(rtlsp->dma_area_tx[cur_desc].dma_hdl, 0, totlen,
1151 DDI_DMA_SYNC_FORDEV);
1154 * Start transmit
1155 * set transmit FIFO threshhold to 0x30*32 = 1536 bytes
1156 * to avoid tx underrun.
1158 rtls_reg_set32(rtlsp, TX_STATUS_DESC0_REG + 4 * cur_desc,
1159 totlen | (0x30 << TX_STATUS_TX_THRESHOLD_SHIFT));
1162 * Update the value of current tx descriptor
1164 cur_desc++;
1165 cur_desc %= RTLS_MAX_TX_DESC;
1166 rtlsp->tx_current_desc = cur_desc;
1168 mutex_exit(&rtlsp->rtls_tx_lock);
1170 return (B_TRUE);
1174 * rtls_m_tx() -- send a chain of packets, linked by mp->b_next.
1176 static mblk_t *
1177 rtls_m_tx(void *arg, mblk_t *mp)
1179 rtls_t *rtlsp = arg;
1180 mblk_t *next;
1182 while (mp != NULL) {
1183 next = mp->b_next;
1184 mp->b_next = NULL;
1185 if (!rtls_send(rtlsp, mp)) {
1186 mp->b_next = next;
1187 break;
1189 mp = next;
1191 return (mp);
1195 * rtls_receive() -- receive packets
1197 * Called when receive interrupts detected
1199 static void
1200 rtls_receive(rtls_t *rtlsp)
1202 mblk_t *head = NULL;
1203 mblk_t **mpp;
1204 mblk_t *mp;
1205 uint16_t rx_status;
1206 uint16_t packet_len;
1207 int wrap_size;
1208 uint32_t cur_rx;
1209 uint8_t *rx_ptr;
1211 mpp = &head;
1213 mutex_enter(&rtlsp->rtls_rx_lock);
1215 if (rtlsp->rtls_suspended) {
1216 mutex_exit(&rtlsp->rtls_rx_lock);
1217 return;
1220 while ((rtls_reg_get8(rtlsp, RT_COMMAND_REG)
1221 & RT_COMMAND_BUFF_EMPTY) == 0) {
1224 * Chip error state
1226 if (rtlsp->chip_error) {
1227 #ifdef RTLS_DEBUG
1228 cmn_err(CE_WARN,
1229 "%s: receive fail--CHIP ERROR!",
1230 mac_name(rtlsp->mh));
1231 #endif
1232 break;
1235 cur_rx = rtlsp->cur_rx;
1236 rx_ptr = rtlsp->rx_ring + cur_rx;
1237 packet_len = (rx_ptr[3] << 8) | (rx_ptr[2]);
1238 rx_status = rx_ptr[0];
1241 * DMA still in progress
1243 if (packet_len == RX_STATUS_DMA_BUSY) {
1244 cmn_err(CE_NOTE, "%s: Rx DMA still in progress",
1245 mac_name(rtlsp->mh));
1246 break;
1250 * Check receive status
1252 if ((rx_status & RX_ERR_FLAGS) ||
1253 (!(rx_status & RX_HEADER_STATUS_ROK)) ||
1254 (packet_len < (ETHERMIN + ETHERFCSL)) ||
1255 (packet_len > (ETHERMAX + ETHERFCSL + 4))) {
1256 #ifdef RTLS_DEBUG
1257 cmn_err(CE_NOTE,
1258 "%s: receive error, status = 0x%x, length = %d",
1259 mac_name(rtlsp->mh), rx_status, packet_len);
1260 #endif
1262 * Rx error statistics
1264 if ((rx_status & RX_HEADER_STATUS_RUNT) ||
1265 (packet_len < (ETHERMIN + ETHERFCSL)))
1266 rtlsp->stats.in_short++;
1267 else if (packet_len > (ETHERMAX + ETHERFCSL + 4))
1268 rtlsp->stats.too_long++;
1269 else if (rx_status & RX_HEADER_STATUS_CRC)
1270 rtlsp->stats.crc_err++;
1271 else if (rx_status & RX_HEADER_STATUS_FAE)
1272 rtlsp->stats.frame_err++;
1275 * Set chip_error flag to reset chip:
1276 * (suggested in RealTek programming guide.)
1278 rtlsp->chip_error = B_TRUE;
1279 mutex_exit(&rtlsp->rtls_rx_lock);
1280 return;
1284 * We need not up-send ETHERFCSL bytes of receive packet
1286 packet_len -= ETHERFCSL;
1289 * Allocate buffer to receive this good packet
1291 mp = allocb(packet_len, 0);
1294 * Copy the data found into the new cluster, we have (+4)
1295 * to get us past the packet head data that the rtl chip
1296 * places at the start of the message
1298 if ((cur_rx + packet_len + RX_HEADER_SIZE)
1299 > RTLS_RX_BUF_RING) {
1300 wrap_size = cur_rx + packet_len + RX_HEADER_SIZE
1301 - RTLS_RX_BUF_RING;
1302 #ifdef RTLS_DEBUG
1303 if (rtls_debug & RTLS_RECV) {
1304 cmn_err(CE_NOTE,
1305 "%s: Rx: packet_len = %d, wrap_size = %d",
1306 mac_name(rtlsp->mh), packet_len, wrap_size);
1308 #endif
1310 if (mp != NULL) {
1311 /* Flush caches */
1312 (void) ddi_dma_sync(rtlsp->dma_area_rx.dma_hdl,
1313 cur_rx + RX_HEADER_SIZE,
1314 packet_len - wrap_size,
1315 DDI_DMA_SYNC_FORKERNEL);
1316 (void) ddi_dma_sync(rtlsp->dma_area_rx.dma_hdl,
1317 0, wrap_size,
1318 DDI_DMA_SYNC_FORKERNEL);
1321 * Copy in first section of message as stored
1322 * at the end of the ring buffer
1324 bcopy(rx_ptr + RX_HEADER_SIZE,
1325 mp->b_wptr, packet_len - wrap_size);
1326 mp->b_wptr += packet_len - wrap_size;
1327 bcopy(rtlsp->rx_ring, mp->b_wptr, wrap_size);
1328 mp->b_wptr += wrap_size;
1329 *mpp = mp;
1330 mpp = &mp->b_next;
1332 rtlsp->stats.ipackets++;
1333 if (rx_status & RX_HEADER_STATUS_BCAST)
1334 rtlsp->stats.brdcst_rcv++;
1335 if (rx_status & RX_HEADER_STATUS_MULTI)
1336 rtlsp->stats.multi_rcv++;
1337 rtlsp->stats.rbytes += packet_len;
1338 } else {
1339 rtlsp->stats.no_rcvbuf++;
1342 cur_rx = RTLS_RX_ADDR_ALIGNED(wrap_size + ETHERFCSL);
1343 /* 4-byte aligned */
1344 } else {
1346 if (mp != NULL) {
1347 /* Flush caches */
1348 (void) ddi_dma_sync(rtlsp->dma_area_rx.dma_hdl,
1349 cur_rx + RX_HEADER_SIZE, packet_len,
1350 DDI_DMA_SYNC_FORKERNEL);
1351 bcopy(rx_ptr + RX_HEADER_SIZE, mp->b_wptr,
1352 packet_len);
1353 mp->b_wptr += packet_len;
1354 *mpp = mp;
1355 mpp = &mp->b_next;
1357 rtlsp->stats.ipackets++;
1358 if (rx_status & RX_HEADER_STATUS_BCAST)
1359 rtlsp->stats.brdcst_rcv++;
1360 if (rx_status & RX_HEADER_STATUS_MULTI)
1361 rtlsp->stats.multi_rcv++;
1362 rtlsp->stats.rbytes += packet_len;
1363 } else {
1364 rtlsp->stats.no_rcvbuf++;
1366 cur_rx += packet_len + RX_HEADER_SIZE + ETHERFCSL;
1368 cur_rx = RTLS_RX_ADDR_ALIGNED(cur_rx);
1369 /* 4-byte aligned */
1373 * Update rx buffer ring read pointer:
1374 * give us a little leeway to ensure no overflow
1376 rtlsp->cur_rx = cur_rx;
1377 rtls_reg_set16(rtlsp, RX_CURRENT_READ_ADDR_REG,
1378 cur_rx - READ_ADDR_GAP);
1380 mutex_exit(&rtlsp->rtls_rx_lock);
1383 * Upsend packet
1385 if (head) {
1386 mac_rx(rtlsp->mh, NULL, head);
1391 * rtls_intr() -- interrupt from board to inform us that a receive or
1392 * link change.
1394 static uint_t
1395 rtls_intr(caddr_t arg)
1397 rtls_t *rtlsp = (void *)arg;
1398 uint32_t int_status;
1399 uint32_t val32;
1400 boolean_t resched = B_FALSE;
1402 mutex_enter(&rtlsp->rtls_io_lock);
1403 if (rtlsp->rtls_suspended) {
1404 mutex_exit(&rtlsp->rtls_io_lock);
1405 return (DDI_INTR_UNCLAIMED);
1409 * Was this interrupt caused by our device...
1411 int_status = rtls_reg_get16(rtlsp, RT_INT_STATUS_REG);
1412 if (!(int_status & rtlsp->int_mask)) {
1413 mutex_exit(&rtlsp->rtls_io_lock);
1414 return (DDI_INTR_UNCLAIMED);
1415 /* indicate it wasn't our interrupt */
1419 * Clear interrupt
1421 rtls_reg_set16(rtlsp, RT_INT_STATUS_REG, int_status);
1424 * If chip error, restart chip...
1426 if (rtlsp->chip_error) {
1427 mutex_enter(&rtlsp->rtls_rx_lock);
1428 mutex_enter(&rtlsp->rtls_tx_lock);
1429 rtls_chip_restart(rtlsp);
1430 rtlsp->chip_error = B_FALSE;
1431 rtlsp->tx_retry = 0;
1432 mutex_exit(&rtlsp->rtls_tx_lock);
1433 mutex_exit(&rtlsp->rtls_rx_lock);
1434 mutex_exit(&rtlsp->rtls_io_lock);
1435 return (DDI_INTR_CLAIMED);
1436 /* no need to hand other interrupts */
1440 * Transmit error interrupt
1442 if (int_status & TX_ERR_INT) {
1443 val32 = rtls_reg_get32(rtlsp, TX_CONFIG_REG);
1444 val32 |= TX_CLEAR_ABORT;
1445 rtls_reg_set32(rtlsp, TX_CONFIG_REG, val32);
1446 cmn_err(CE_WARN, "%s: transmit abort!!!", mac_name(rtlsp->mh));
1450 * Trigger mac_tx_update
1452 if (rtlsp->need_sched) {
1453 rtlsp->need_sched = B_FALSE;
1454 resched = B_TRUE;
1457 mutex_exit(&rtlsp->rtls_io_lock);
1460 * Receive interrupt
1462 if (int_status & RTLS_RX_INT) {
1463 if (int_status & RX_OVERFLOW_INT) {
1464 rtlsp->stats.overflow++;
1465 rtlsp->stats.rcv_err++;
1467 rtls_receive(rtlsp);
1471 * Link change interrupt.
1473 if (int_status & LINK_CHANGE_INT) {
1474 mii_check(rtlsp->mii);
1477 if (resched) {
1478 mac_tx_update(rtlsp->mh);
1481 return (DDI_INTR_CLAIMED); /* indicate it was our interrupt */
1485 * ========== Buffer Management Routines ==========
1489 * rtls_alloc_dma_mem() -- allocate an area of memory and a DMA handle
1490 * for accessing it
1492 static int
1493 rtls_alloc_dma_mem(rtls_t *rtlsp, size_t memsize,
1494 ddi_device_acc_attr_t *attr_p, uint_t dma_flags, dma_area_t *dma_p)
1496 caddr_t vaddr;
1497 int err;
1500 * Allocate handle
1502 err = ddi_dma_alloc_handle(rtlsp->devinfo, &dma_attr,
1503 DDI_DMA_SLEEP, NULL, &dma_p->dma_hdl);
1504 if (err != DDI_SUCCESS) {
1505 cmn_err(CE_WARN,
1506 "%s: rtls_alloc_dma_mem: ddi_dma_alloc_handle failed: %d",
1507 mac_name(rtlsp->mh), err);
1508 dma_p->dma_hdl = NULL;
1509 return (DDI_FAILURE);
1513 * Allocate memory
1515 err = ddi_dma_mem_alloc(dma_p->dma_hdl, memsize, attr_p,
1516 dma_flags & (DDI_DMA_CONSISTENT | DDI_DMA_STREAMING),
1517 DDI_DMA_SLEEP, NULL, &vaddr, &dma_p->alength, &dma_p->acc_hdl);
1518 if (err != DDI_SUCCESS) {
1519 cmn_err(CE_WARN,
1520 "%s: rtls_alloc_dma_mem: ddi_dma_mem_alloc failed: %d",
1521 mac_name(rtlsp->mh), err);
1522 ddi_dma_free_handle(&dma_p->dma_hdl);
1523 dma_p->dma_hdl = NULL;
1524 dma_p->acc_hdl = NULL;
1525 return (DDI_FAILURE);
1529 * Bind the two together
1531 dma_p->mem_va = vaddr;
1532 err = ddi_dma_addr_bind_handle(dma_p->dma_hdl, NULL,
1533 vaddr, dma_p->alength, dma_flags, DDI_DMA_SLEEP, NULL,
1534 &dma_p->cookie, &dma_p->ncookies);
1535 if (err != DDI_DMA_MAPPED || dma_p->ncookies != 1) {
1536 cmn_err(CE_WARN,
1537 "%s: rtls_alloc_dma_mem: "
1538 "ddi_dma_addr_bind_handle failed: %d",
1539 mac_name(rtlsp->mh), err);
1540 ddi_dma_mem_free(&dma_p->acc_hdl);
1541 ddi_dma_free_handle(&dma_p->dma_hdl);
1542 dma_p->acc_hdl = NULL;
1543 dma_p->dma_hdl = NULL;
1544 return (DDI_FAILURE);
1547 return (DDI_SUCCESS);
1551 * rtls_free_dma_mem() -- free one allocated area of DMAable memory
1553 static void
1554 rtls_free_dma_mem(dma_area_t *dma_p)
1556 if (dma_p->dma_hdl != NULL) {
1557 if (dma_p->ncookies) {
1558 (void) ddi_dma_unbind_handle(dma_p->dma_hdl);
1559 dma_p->ncookies = 0;
1561 ddi_dma_free_handle(&dma_p->dma_hdl);
1562 dma_p->dma_hdl = NULL;
1565 if (dma_p->acc_hdl != NULL) {
1566 ddi_dma_mem_free(&dma_p->acc_hdl);
1567 dma_p->acc_hdl = NULL;
1572 * rtls_alloc_bufs() -- allocate descriptors/buffers for this device instance
1574 static int
1575 rtls_alloc_bufs(rtls_t *rtlsp)
1577 int i;
1578 int err;
1581 * Allocate memory & handle for Tx buffers
1583 for (i = 0; i < RTLS_MAX_TX_DESC; i++) {
1584 err = rtls_alloc_dma_mem(rtlsp,
1585 RTLS_TX_BUF_SIZE,
1586 &rtls_buf_accattr,
1587 DDI_DMA_WRITE | DDI_DMA_STREAMING,
1588 &rtlsp->dma_area_tx[i]);
1590 if (err != DDI_SUCCESS)
1591 return (DDI_FAILURE);
1593 rtlsp->tx_buf[i] = (uint8_t *)rtlsp->dma_area_tx[i].mem_va;
1597 * Allocate memory & handle for Rx buffers
1599 err = rtls_alloc_dma_mem(rtlsp,
1600 RTLS_RX_BUF_SIZE,
1601 &rtls_buf_accattr,
1602 DDI_DMA_READ | DDI_DMA_STREAMING,
1603 &rtlsp->dma_area_rx);
1605 if (err != DDI_SUCCESS)
1606 return (DDI_FAILURE);
1608 rtlsp->rx_ring = (uint8_t *)rtlsp->dma_area_rx.mem_va;
1610 return (DDI_SUCCESS);
1614 * rtls_free_bufs() -- free descriptors/buffers allocated for this
1615 * device instance.
1617 static void
1618 rtls_free_bufs(rtls_t *rtlsp)
1620 int i;
1622 for (i = 0; i < RTLS_MAX_TX_DESC; i++) {
1623 rtls_free_dma_mem(&rtlsp->dma_area_tx[i]);
1624 rtlsp->tx_buf[i] = NULL;
1627 rtls_free_dma_mem(&rtlsp->dma_area_rx);
1628 rtlsp->rx_ring = NULL;
1632 * ========== Chip H/W Operation Routines ==========
1636 * rtls_chip_reset() -- reset chip
1638 static int
1639 rtls_chip_reset(rtls_t *rtlsp, boolean_t quiesce)
1641 int i;
1642 uint16_t val16;
1643 uint8_t val8;
1646 * Chip should be in STOP state
1648 val8 = rtls_reg_get8(rtlsp, RT_COMMAND_REG);
1649 val8 &= ~(RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
1650 rtls_reg_set8(rtlsp, RT_COMMAND_REG, val8);
1653 * Disable interrupt
1655 val16 = rtls_reg_get16(rtlsp, RT_INT_MASK_REG);
1656 rtls_reg_set16(rtlsp, RT_INT_MASK_REG, val16 & (~RTLS_INT_MASK_ALL));
1657 rtlsp->int_mask = RTLS_INT_MASK_NONE;
1660 * Clear pended interrupt
1662 val16 = rtls_reg_get16(rtlsp, RT_INT_STATUS_REG);
1663 rtls_reg_set16(rtlsp, RT_INT_STATUS_REG, val16);
1666 * Reset chip
1668 val8 = rtls_reg_get8(rtlsp, RT_COMMAND_REG);
1669 rtls_reg_set8(rtlsp, RT_COMMAND_REG, val8 | RT_COMMAND_RESET);
1672 * Wait for reset success
1674 i = 0;
1675 while (rtls_reg_get8(rtlsp, RT_COMMAND_REG) & RT_COMMAND_RESET) {
1676 if (++i > RTLS_RESET_WAIT_NUM) {
1678 * At quiesce path we can't call cmn_err(), as
1679 * it might block
1681 if (!quiesce)
1682 cmn_err(CE_WARN,
1683 "%s: chip reset fail.",
1684 mac_name(rtlsp->mh));
1685 return (DDI_FAILURE);
1687 RTLS_RESET_WAIT_INTERVAL;
1690 return (DDI_SUCCESS);
1694 * rtls_chip_init() -- initialize the specified network board short of
1695 * actually starting the board. Call after rtls_chip_reset().
1697 static void
1698 rtls_chip_init(rtls_t *rtlsp)
1700 uint32_t val32;
1701 uint16_t val16;
1702 uint8_t val8;
1705 * Initialize internal data structures
1707 rtlsp->cur_rx = 0;
1708 rtlsp->tx_current_desc = 0;
1709 rtlsp->tx_first_loop = 0;
1712 * Set DMA physical rx/tx buffer address to register
1714 rtls_reg_set32(rtlsp, RX_BUFF_ADDR_REG,
1715 (ulong_t)rtlsp->dma_area_rx.cookie.dmac_address);
1716 rtls_reg_set32(rtlsp, TX_ADDR_DESC0_REG,
1717 (ulong_t)rtlsp->dma_area_tx[0].cookie.dmac_address);
1718 rtls_reg_set32(rtlsp, TX_ADDR_DESC1_REG,
1719 (ulong_t)rtlsp->dma_area_tx[1].cookie.dmac_address);
1720 rtls_reg_set32(rtlsp, TX_ADDR_DESC2_REG,
1721 (ulong_t)rtlsp->dma_area_tx[2].cookie.dmac_address);
1722 rtls_reg_set32(rtlsp, TX_ADDR_DESC3_REG,
1723 (ulong_t)rtlsp->dma_area_tx[3].cookie.dmac_address);
1726 * Start transmit/receive before set tx/rx configuration register
1728 val8 = rtls_reg_get8(rtlsp, RT_COMMAND_REG);
1729 rtls_reg_set8(rtlsp, RT_COMMAND_REG,
1730 val8 | RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
1733 * Set transmit configuration register
1735 val32 = rtls_reg_get32(rtlsp, TX_CONFIG_REG);
1736 val32 &= TX_CONSIG_REG_RESERVE;
1737 rtls_reg_set32(rtlsp, TX_CONFIG_REG, val32 | TX_CONFIG_DEFAULT);
1740 * Set receive configuration register
1742 val32 = rtls_reg_get32(rtlsp, RX_CONFIG_REG);
1743 val32 &= RX_CONSIG_REG_RESERVE;
1744 if (rtlsp->promisc)
1745 val32 |= RX_ACCEPT_ALL_PACKET;
1746 rtls_reg_set32(rtlsp, RX_CONFIG_REG, val32 | RX_CONFIG_DEFAULT);
1749 * Set multicast register
1751 rtls_reg_set32(rtlsp, MULTICAST_0_REG, rtlsp->multi_hash[0]);
1752 rtls_reg_set32(rtlsp, MULTICAST_4_REG, rtlsp->multi_hash[1]);
1755 * Set unicast address
1757 rtls_set_mac_addr(rtlsp, rtlsp->netaddr);
1760 * Set current address of packet read
1762 rtls_reg_set16(rtlsp, RX_CURRENT_READ_ADDR_REG, RX_READ_RESET_VAL);
1765 * No early-rx interrupts
1767 val16 = rtls_reg_get16(rtlsp, RT_MUL_INTSEL_REG);
1768 val16 &= ~RT_MUL_INTSEL_BITS;
1769 rtls_reg_set16(rtlsp, RT_MUL_INTSEL_REG, val16);
1773 * rtls_chip_start() -- start chip
1775 static void
1776 rtls_chip_start(rtls_t *rtlsp)
1778 uint16_t val16;
1779 uint8_t val8;
1782 * Start transmit/receive
1784 val8 = rtls_reg_get8(rtlsp, RT_COMMAND_REG);
1785 rtls_reg_set8(rtlsp, RT_COMMAND_REG,
1786 val8 | RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
1789 * Enable interrupt
1791 val16 = rtls_reg_get16(rtlsp, RT_INT_MASK_REG);
1792 rtls_reg_set16(rtlsp, RT_INT_MASK_REG, val16 | RTLS_INT_MASK);
1793 rtlsp->int_mask = RTLS_INT_MASK;
1797 * rtls_chip_restart() -- restart chip
1799 static void
1800 rtls_chip_restart(rtls_t *rtlsp)
1802 (void) rtls_chip_reset(rtlsp, B_FALSE);
1803 rtls_chip_init(rtlsp);
1804 rtls_chip_start(rtlsp);
1808 * rtls_chip_stop() -- stop board receiving
1810 static void
1811 rtls_chip_stop(rtls_t *rtlsp)
1813 uint16_t val16;
1814 uint8_t val8;
1817 * Disable interrupt
1819 val16 = rtls_reg_get16(rtlsp, RT_INT_MASK_REG);
1820 rtls_reg_set16(rtlsp, RT_INT_MASK_REG, val16 & (~RTLS_INT_MASK_ALL));
1821 rtlsp->int_mask = RTLS_INT_MASK_NONE;
1824 * Clear pended interrupt
1826 val16 = rtls_reg_get16(rtlsp, RT_INT_STATUS_REG);
1827 rtls_reg_set16(rtlsp, RT_INT_STATUS_REG, val16);
1830 * Stop the board and disable transmit/receive
1832 val8 = rtls_reg_get8(rtlsp, RT_COMMAND_REG);
1833 val8 &= ~(RT_COMMAND_RX_ENABLE | RT_COMMAND_TX_ENABLE);
1834 rtls_reg_set8(rtlsp, RT_COMMAND_REG, val8);
1838 * rtls_get_mac_addr() -- get the physical network address on the board
1840 static void
1841 rtls_get_mac_addr(rtls_t *rtlsp, uint8_t *macaddr)
1843 uint32_t val32;
1846 * Read first 4-byte of mac address
1848 val32 = rtls_reg_get32(rtlsp, ID_0_REG);
1849 macaddr[0] = val32 & 0xff;
1850 val32 = val32 >> 8;
1851 macaddr[1] = val32 & 0xff;
1852 val32 = val32 >> 8;
1853 macaddr[2] = val32 & 0xff;
1854 val32 = val32 >> 8;
1855 macaddr[3] = val32 & 0xff;
1858 * Read last 2-byte of mac address
1860 val32 = rtls_reg_get32(rtlsp, ID_4_REG);
1861 macaddr[4] = val32 & 0xff;
1862 val32 = val32 >> 8;
1863 macaddr[5] = val32 & 0xff;
1866 static void
1867 rtls_set_mac_addr(rtls_t *rtlsp, const uint8_t *macaddr)
1869 uint32_t val32;
1870 uint8_t val8;
1873 * Change to config register write enable mode
1875 val8 = rtls_reg_get8(rtlsp, RT_93c46_COMMAND_REG);
1876 val8 |= RT_93c46_MODE_CONFIG;
1877 rtls_reg_set8(rtlsp, RT_93c46_COMMAND_REG, val8);
1880 * Get first 4 bytes of mac address
1882 val32 = macaddr[3];
1883 val32 = val32 << 8;
1884 val32 |= macaddr[2];
1885 val32 = val32 << 8;
1886 val32 |= macaddr[1];
1887 val32 = val32 << 8;
1888 val32 |= macaddr[0];
1891 * Set first 4 bytes of mac address
1893 rtls_reg_set32(rtlsp, ID_0_REG, val32);
1896 * Get last 2 bytes of mac address
1898 val32 = macaddr[5];
1899 val32 = val32 << 8;
1900 val32 |= macaddr[4];
1903 * Set last 2 bytes of mac address
1905 val32 |= rtls_reg_get32(rtlsp, ID_4_REG) & ~0xffff;
1906 rtls_reg_set32(rtlsp, ID_4_REG, val32);
1909 * Return to normal network/host communication mode
1911 val8 &= ~RT_93c46_MODE_CONFIG;
1912 rtls_reg_set8(rtlsp, RT_93c46_COMMAND_REG, val8);
1915 static uint16_t
1916 rtls_mii_read(void *arg, uint8_t phy, uint8_t reg)
1918 rtls_t *rtlsp = arg;
1919 uint16_t val;
1921 if (phy != 1) {
1922 return (0xffff);
1924 switch (reg) {
1925 case MII_CONTROL:
1926 val = rtls_reg_get16(rtlsp, BASIC_MODE_CONTROL_REG);
1927 break;
1928 case MII_STATUS:
1929 val = rtls_reg_get16(rtlsp, BASIC_MODE_STATUS_REG);
1930 break;
1931 case MII_AN_ADVERT:
1932 val = rtls_reg_get16(rtlsp, AUTO_NEGO_AD_REG);
1933 break;
1934 case MII_AN_LPABLE:
1935 val = rtls_reg_get16(rtlsp, AUTO_NEGO_LP_REG);
1936 break;
1937 case MII_AN_EXPANSION:
1938 val = rtls_reg_get16(rtlsp, AUTO_NEGO_EXP_REG);
1939 break;
1940 case MII_VENDOR(0):
1942 * We "simulate" a vendor private register so that the
1943 * PHY layer can access it to determine detected link
1944 * speed/duplex.
1946 val = rtls_reg_get8(rtlsp, MEDIA_STATUS_REG);
1947 break;
1948 case MII_PHYIDH:
1949 case MII_PHYIDL:
1950 default:
1951 val = 0;
1952 break;
1954 return (val);
1957 void
1958 rtls_mii_write(void *arg, uint8_t phy, uint8_t reg, uint16_t val)
1960 rtls_t *rtlsp = arg;
1961 uint8_t val8;
1963 if (phy != 1) {
1964 return;
1966 switch (reg) {
1967 case MII_CONTROL:
1968 /* Enable writes to all bits of BMCR */
1969 val8 = rtls_reg_get8(rtlsp, RT_93c46_COMMAND_REG);
1970 val8 |= RT_93c46_MODE_CONFIG;
1971 rtls_reg_set8(rtlsp, RT_93c46_COMMAND_REG, val8);
1972 /* write out the value */
1973 rtls_reg_set16(rtlsp, BASIC_MODE_CONTROL_REG, val);
1975 /* Return to normal network/host communication mode */
1976 val8 &= ~RT_93c46_MODE_CONFIG;
1977 rtls_reg_set8(rtlsp, RT_93c46_COMMAND_REG, val8);
1978 return;
1980 case MII_STATUS:
1981 rtls_reg_set16(rtlsp, BASIC_MODE_STATUS_REG, val);
1982 break;
1983 case MII_AN_ADVERT:
1984 rtls_reg_set16(rtlsp, AUTO_NEGO_AD_REG, val);
1985 break;
1986 case MII_AN_LPABLE:
1987 rtls_reg_set16(rtlsp, AUTO_NEGO_LP_REG, val);
1988 break;
1989 case MII_AN_EXPANSION:
1990 rtls_reg_set16(rtlsp, AUTO_NEGO_EXP_REG, val);
1991 break;
1992 case MII_PHYIDH:
1993 case MII_PHYIDL:
1994 default:
1995 /* these are not writable */
1996 break;
2000 void
2001 rtls_mii_notify(void *arg, link_state_t link)
2003 rtls_t *rtlsp = arg;
2005 mac_link_update(rtlsp->mh, link);
2008 #ifdef RTLS_DEBUG
2010 * rtls_reg_print() -- print out reg value(for debug use only)
2012 static void
2013 rtls_reg_print(rtls_t *rtlsp)
2015 uint8_t val8;
2016 uint16_t val16;
2017 uint32_t val32;
2019 val8 = rtls_reg_get8(rtlsp, RT_COMMAND_REG);
2020 cmn_err(CE_NOTE, "%s: RT_COMMAND_REG = 0x%x",
2021 mac_name(rtlsp->mh), val8);
2022 ddi_msleep(1);
2024 val16 = rtls_reg_get16(rtlsp, RT_INT_STATUS_REG);
2025 cmn_err(CE_NOTE, "%s: RT_INT_STATUS_REG = 0x%x",
2026 mac_name(rtlsp->mh), val16);
2027 ddi_msleep(1);
2029 val16 = rtls_reg_get16(rtlsp, RT_INT_MASK_REG);
2030 cmn_err(CE_NOTE, "%s: RT_INT_MASK_REG = 0x%x",
2031 mac_name(rtlsp->mh), val16);
2032 ddi_msleep(1);
2034 val32 = rtls_reg_get32(rtlsp, RX_CONFIG_REG);
2035 cmn_err(CE_NOTE, "%s: RX_CONFIG_REG = 0x%x",
2036 mac_name(rtlsp->mh), val32);
2037 ddi_msleep(1);
2039 val16 = rtls_reg_get16(rtlsp, TX_DESC_STAUS_REG);
2040 cmn_err(CE_NOTE, "%s: TX_DESC_STAUS_REG = 0x%x, cur_desc = %d",
2041 mac_name(rtlsp->mh), val16, rtlsp->tx_current_desc);
2042 ddi_msleep(1);
2044 val32 = rtls_reg_get32(rtlsp, TX_STATUS_DESC0_REG);
2045 cmn_err(CE_NOTE, "%s: TX_STATUS_DESC0_REG = 0x%x",
2046 mac_name(rtlsp->mh), val32);
2047 ddi_msleep(1);
2049 val32 = rtls_reg_get32(rtlsp, TX_STATUS_DESC1_REG);
2050 cmn_err(CE_NOTE, "%s: TX_STATUS_DESC1_REG = 0x%x",
2051 mac_name(rtlsp->mh), val32);
2052 ddi_msleep(1);
2054 val32 = rtls_reg_get32(rtlsp, TX_STATUS_DESC2_REG);
2055 cmn_err(CE_NOTE, "%s: TX_STATUS_DESC2_REG = 0x%x",
2056 mac_name(rtlsp->mh), val32);
2057 ddi_msleep(1);
2059 val32 = rtls_reg_get32(rtlsp, TX_STATUS_DESC3_REG);
2060 cmn_err(CE_NOTE, "%s: TX_STATUS_DESC3_REG = 0x%x",
2061 mac_name(rtlsp->mh), val32);
2062 ddi_msleep(1);
2064 cmn_err(CE_NOTE, "%s: in = %llu, multicast = %llu, broadcast = %llu",
2065 mac_name(rtlsp->mh),
2066 (unsigned long long)rtlsp->stats.ipackets,
2067 (unsigned long long)rtlsp->stats.multi_rcv,
2068 (unsigned long long)rtlsp->stats.brdcst_rcv);
2069 ddi_msleep(1);
2071 #endif