/*1* AMD 10Gb Ethernet driver2*3* Copyright (c) 2014-2016,2020 Advanced Micro Devices, Inc.4*5* This file is available to you under your choice of the following two6* licenses:7*8* License 1: GPLv29*10* This file is free software; you may copy, redistribute and/or modify11* it under the terms of the GNU General Public License as published by12* the Free Software Foundation, either version 2 of the License, or (at13* your option) any later version.14*15* This file is distributed in the hope that it will be useful, but16* WITHOUT ANY WARRANTY; without even the implied warranty of17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU18* General Public License for more details.19*20* You should have received a copy of the GNU General Public License21* along with this program. If not, see <http://www.gnu.org/licenses/>.22*23* This file incorporates work covered by the following copyright and24* permission notice:25* The Synopsys DWC ETHER XGMAC Software Driver and documentation26* (hereinafter "Software") is an unsupported proprietary work of Synopsys,27* Inc. unless otherwise expressly agreed to in writing between Synopsys28* and you.29*30* The Software IS NOT an item of Licensed Software or Licensed Product31* under any End User Software License Agreement or Agreement for Licensed32* Product with Synopsys or any supplement thereto. Permission is hereby33* granted, free of charge, to any person obtaining a copy of this software34* annotated with this license and the Software, to deal in the Software35* without restriction, including without limitation the rights to use,36* copy, modify, merge, publish, distribute, sublicense, and/or sell copies37* of the Software, and to permit persons to whom the Software is furnished38* to do so, subject to the following conditions:39*40* The above copyright notice and this permission notice shall be included41* in all copies or substantial portions of the Software.42*43* THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"44* BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED45* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A46* PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS47* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR48* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF49* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS50* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN51* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)52* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF53* THE POSSIBILITY OF SUCH DAMAGE.54*55*56* License 2: Modified BSD57*58* Redistribution and use in source and binary forms, with or without59* modification, are permitted provided that the following conditions are met:60* * Redistributions of source code must retain the above copyright61* notice, this list of conditions and the following disclaimer.62* * Redistributions in binary form must reproduce the above copyright63* notice, this list of conditions and the following disclaimer in the64* documentation and/or other materials provided with the distribution.65* * Neither the name of Advanced Micro Devices, Inc. nor the66* names of its contributors may be used to endorse or promote products67* derived from this software without specific prior written permission.68*69* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"70* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE71* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE72* ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY73* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES74* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;75* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND76* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT77* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF78* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.79*80* This file incorporates work covered by the following copyright and81* permission notice:82* The Synopsys DWC ETHER XGMAC Software Driver and documentation83* (hereinafter "Software") is an unsupported proprietary work of Synopsys,84* Inc. unless otherwise expressly agreed to in writing between Synopsys85* and you.86*87* The Software IS NOT an item of Licensed Software or Licensed Product88* under any End User Software License Agreement or Agreement for Licensed89* Product with Synopsys or any supplement thereto. Permission is hereby90* granted, free of charge, to any person obtaining a copy of this software91* annotated with this license and the Software, to deal in the Software92* without restriction, including without limitation the rights to use,93* copy, modify, merge, publish, distribute, sublicense, and/or sell copies94* of the Software, and to permit persons to whom the Software is furnished95* to do so, subject to the following conditions:96*97* The above copyright notice and this permission notice shall be included98* in all copies or substantial portions of the Software.99*100* THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"101* BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED102* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A103* PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS104* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR105* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF106* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS107* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN108* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)109* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF110* THE POSSIBILITY OF SUCH DAMAGE.111*/112113#include <sys/cdefs.h>114#include "xgbe.h"115#include "xgbe-common.h"116117int118xgbe_calc_rx_buf_size(if_t netdev, unsigned int mtu)119{120unsigned int rx_buf_size;121122if (mtu > XGMAC_JUMBO_PACKET_MTU)123return (-EINVAL);124125rx_buf_size = mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;126rx_buf_size = min(max(rx_buf_size, XGBE_RX_MIN_BUF_SIZE), PAGE_SIZE);127rx_buf_size = (rx_buf_size + XGBE_RX_BUF_ALIGN - 1) &128~(XGBE_RX_BUF_ALIGN - 1);129130return (rx_buf_size);131}132133void134xgbe_get_all_hw_features(struct xgbe_prv_data *pdata)135{136unsigned int mac_hfr0, mac_hfr1, mac_hfr2;137struct xgbe_hw_features *hw_feat = &pdata->hw_feat;138139DBGPR("-->xgbe_get_all_hw_features\n");140141mac_hfr0 = XGMAC_IOREAD(pdata, MAC_HWF0R);142mac_hfr1 = XGMAC_IOREAD(pdata, MAC_HWF1R);143mac_hfr2 = XGMAC_IOREAD(pdata, MAC_HWF2R);144145memset(hw_feat, 0, sizeof(*hw_feat));146147hw_feat->version = XGMAC_IOREAD(pdata, MAC_VR);148149/* Hardware feature register 0 */150hw_feat->gmii = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);151hw_feat->vlhash = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);152hw_feat->sma = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);153hw_feat->rwk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);154hw_feat->mgk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);155hw_feat->mmc = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);156hw_feat->aoe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);157hw_feat->ts = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);158hw_feat->eee = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);159hw_feat->tx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);160hw_feat->rx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);161hw_feat->addn_mac = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,162ADDMACADRSEL);163hw_feat->ts_src = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);164hw_feat->sa_vlan_ins = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);165hw_feat->vxn = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VXN);166167/* Hardware feature register 1 */168hw_feat->rx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,169RXFIFOSIZE);170hw_feat->tx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,171TXFIFOSIZE);172hw_feat->adv_ts_hi = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADVTHWORD);173hw_feat->dma_width = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADDR64);174hw_feat->dcb = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);175hw_feat->sph = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);176hw_feat->tso = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);177hw_feat->dma_debug = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);178hw_feat->rss = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, RSSEN);179hw_feat->tc_cnt = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);180hw_feat->hash_table_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,181HASHTBLSZ);182hw_feat->l3l4_filter_num = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,183L3L4FNUM);184185/* Hardware feature register 2 */186hw_feat->rx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);187hw_feat->tx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);188hw_feat->rx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);189hw_feat->tx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);190hw_feat->pps_out_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);191hw_feat->aux_snap_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, AUXSNAPNUM);192193/* Translate the Hash Table size into actual number */194switch (hw_feat->hash_table_size) {195case 0:196break;197case 1:198hw_feat->hash_table_size = 64;199break;200case 2:201hw_feat->hash_table_size = 128;202break;203case 3:204hw_feat->hash_table_size = 256;205break;206}207208/* Translate the address width setting into actual number */209switch (hw_feat->dma_width) {210case 0:211hw_feat->dma_width = 32;212break;213case 1:214hw_feat->dma_width = 40;215break;216case 2:217hw_feat->dma_width = 48;218break;219default:220hw_feat->dma_width = 32;221}222223/* The Queue, Channel and TC counts are zero based so increment them224* to get the actual number225*/226hw_feat->rx_q_cnt++;227hw_feat->tx_q_cnt++;228hw_feat->rx_ch_cnt++;229hw_feat->tx_ch_cnt++;230hw_feat->tc_cnt++;231232/* Translate the fifo sizes into actual numbers */233hw_feat->rx_fifo_size = 1 << (hw_feat->rx_fifo_size + 7);234hw_feat->tx_fifo_size = 1 << (hw_feat->tx_fifo_size + 7);235DBGPR("%s: Tx fifo 0x%x Rx fifo 0x%x\n", __func__,236hw_feat->tx_fifo_size, hw_feat->rx_fifo_size);237238DBGPR("Hardware features:\n");239240/* Hardware feature register 0 */241DBGPR(" 1GbE support : %s\n",242hw_feat->gmii ? "yes" : "no");243DBGPR(" VLAN hash filter : %s\n",244hw_feat->vlhash ? "yes" : "no");245DBGPR(" MDIO interface : %s\n",246hw_feat->sma ? "yes" : "no");247DBGPR(" Wake-up packet support : %s\n",248hw_feat->rwk ? "yes" : "no");249DBGPR(" Magic packet support : %s\n",250hw_feat->mgk ? "yes" : "no");251DBGPR(" Management counters : %s\n",252hw_feat->mmc ? "yes" : "no");253DBGPR(" ARP offload : %s\n",254hw_feat->aoe ? "yes" : "no");255DBGPR(" IEEE 1588-2008 Timestamp : %s\n",256hw_feat->ts ? "yes" : "no");257DBGPR(" Energy Efficient Ethernet : %s\n",258hw_feat->eee ? "yes" : "no");259DBGPR(" TX checksum offload : %s\n",260hw_feat->tx_coe ? "yes" : "no");261DBGPR(" RX checksum offload : %s\n",262hw_feat->rx_coe ? "yes" : "no");263DBGPR(" Additional MAC addresses : %u\n",264hw_feat->addn_mac);265DBGPR(" Timestamp source : %s\n",266(hw_feat->ts_src == 1) ? "internal" :267(hw_feat->ts_src == 2) ? "external" :268(hw_feat->ts_src == 3) ? "internal/external" : "n/a");269DBGPR(" SA/VLAN insertion : %s\n",270hw_feat->sa_vlan_ins ? "yes" : "no");271272/* Hardware feature register 1 */273DBGPR(" RX fifo size : %u\n",274hw_feat->rx_fifo_size);275DBGPR(" TX fifo size : %u\n",276hw_feat->tx_fifo_size);277DBGPR(" IEEE 1588 high word : %s\n",278hw_feat->adv_ts_hi ? "yes" : "no");279DBGPR(" DMA width : %u\n",280hw_feat->dma_width);281DBGPR(" Data Center Bridging : %s\n",282hw_feat->dcb ? "yes" : "no");283DBGPR(" Split header : %s\n",284hw_feat->sph ? "yes" : "no");285DBGPR(" TCP Segmentation Offload : %s\n",286hw_feat->tso ? "yes" : "no");287DBGPR(" Debug memory interface : %s\n",288hw_feat->dma_debug ? "yes" : "no");289DBGPR(" Receive Side Scaling : %s\n",290hw_feat->rss ? "yes" : "no");291DBGPR(" Traffic Class count : %u\n",292hw_feat->tc_cnt);293DBGPR(" Hash table size : %u\n",294hw_feat->hash_table_size);295DBGPR(" L3/L4 Filters : %u\n",296hw_feat->l3l4_filter_num);297298/* Hardware feature register 2 */299DBGPR(" RX queue count : %u\n",300hw_feat->rx_q_cnt);301DBGPR(" TX queue count : %u\n",302hw_feat->tx_q_cnt);303DBGPR(" RX DMA channel count : %u\n",304hw_feat->rx_ch_cnt);305DBGPR(" TX DMA channel count : %u\n",306hw_feat->rx_ch_cnt);307DBGPR(" PPS outputs : %u\n",308hw_feat->pps_out_num);309DBGPR(" Auxiliary snapshot inputs : %u\n",310hw_feat->aux_snap_num);311312DBGPR("<--xgbe_get_all_hw_features\n");313}314315void316xgbe_init_tx_coalesce(struct xgbe_prv_data *pdata)317{318struct xgbe_hw_if *hw_if = &pdata->hw_if;319320DBGPR("-->xgbe_init_tx_coalesce\n");321322pdata->tx_usecs = XGMAC_INIT_DMA_TX_USECS;323pdata->tx_frames = XGMAC_INIT_DMA_TX_FRAMES;324325hw_if->config_tx_coalesce(pdata);326327DBGPR("<--xgbe_init_tx_coalesce\n");328}329330void331xgbe_init_rx_coalesce(struct xgbe_prv_data *pdata)332{333struct xgbe_hw_if *hw_if = &pdata->hw_if;334335DBGPR("-->xgbe_init_rx_coalesce\n");336337pdata->rx_riwt = hw_if->usec_to_riwt(pdata, XGMAC_INIT_DMA_RX_USECS);338pdata->rx_usecs = XGMAC_INIT_DMA_RX_USECS;339pdata->rx_frames = XGMAC_INIT_DMA_RX_FRAMES;340341hw_if->config_rx_coalesce(pdata);342343DBGPR("<--xgbe_init_rx_coalesce\n");344}345346347