Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs. |
| 3 | * |
| 4 | * Copyright (C) 2012 Marvell |
| 5 | * |
| 6 | * Rami Rosen <rosenr@marvell.com> |
| 7 | * Thomas Petazzoni <thomas.petazzoni@free-electrons.com> |
| 8 | * |
| 9 | * This file is licensed under the terms of the GNU General Public |
| 10 | * License version 2. This program is licensed "as is" without any |
| 11 | * warranty of any kind, whether express or implied. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/etherdevice.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/skbuff.h> |
| 19 | #include <linux/inetdevice.h> |
| 20 | #include <linux/mbus.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/interrupt.h> |
David S. Miller | 2d39d12 | 2014-08-25 20:21:55 -0700 | [diff] [blame] | 23 | #include <linux/if_vlan.h> |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 24 | #include <net/ip.h> |
| 25 | #include <net/ipv6.h> |
Thomas Petazzoni | c3f0dd3 | 2014-03-27 11:39:29 +0100 | [diff] [blame] | 26 | #include <linux/io.h> |
Ezequiel Garcia | 2adb719 | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 27 | #include <net/tso.h> |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 28 | #include <linux/of.h> |
| 29 | #include <linux/of_irq.h> |
| 30 | #include <linux/of_mdio.h> |
| 31 | #include <linux/of_net.h> |
| 32 | #include <linux/of_address.h> |
| 33 | #include <linux/phy.h> |
Thomas Petazzoni | 189dd62 | 2012-11-19 14:15:25 +0100 | [diff] [blame] | 34 | #include <linux/clk.h> |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 35 | #include <linux/cpu.h> |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 36 | |
| 37 | /* Registers */ |
| 38 | #define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2)) |
Marcin Wojtas | e5bdf68 | 2015-11-30 13:27:42 +0100 | [diff] [blame] | 39 | #define MVNETA_RXQ_HW_BUF_ALLOC BIT(0) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 40 | #define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8) |
| 41 | #define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8) |
| 42 | #define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2)) |
| 43 | #define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16) |
| 44 | #define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2)) |
| 45 | #define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2)) |
| 46 | #define MVNETA_RXQ_BUF_SIZE_SHIFT 19 |
| 47 | #define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19) |
| 48 | #define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2)) |
| 49 | #define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff |
| 50 | #define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2)) |
| 51 | #define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16 |
| 52 | #define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255 |
| 53 | #define MVNETA_PORT_RX_RESET 0x1cc0 |
| 54 | #define MVNETA_PORT_RX_DMA_RESET BIT(0) |
| 55 | #define MVNETA_PHY_ADDR 0x2000 |
| 56 | #define MVNETA_PHY_ADDR_MASK 0x1f |
| 57 | #define MVNETA_MBUS_RETRY 0x2010 |
| 58 | #define MVNETA_UNIT_INTR_CAUSE 0x2080 |
| 59 | #define MVNETA_UNIT_CONTROL 0x20B0 |
| 60 | #define MVNETA_PHY_POLLING_ENABLE BIT(1) |
| 61 | #define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3)) |
| 62 | #define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3)) |
| 63 | #define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2)) |
| 64 | #define MVNETA_BASE_ADDR_ENABLE 0x2290 |
Marcin Wojtas | db6ba9a | 2015-11-30 13:27:41 +0100 | [diff] [blame] | 65 | #define MVNETA_ACCESS_PROTECT_ENABLE 0x2294 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 66 | #define MVNETA_PORT_CONFIG 0x2400 |
| 67 | #define MVNETA_UNI_PROMISC_MODE BIT(0) |
| 68 | #define MVNETA_DEF_RXQ(q) ((q) << 1) |
| 69 | #define MVNETA_DEF_RXQ_ARP(q) ((q) << 4) |
| 70 | #define MVNETA_TX_UNSET_ERR_SUM BIT(12) |
| 71 | #define MVNETA_DEF_RXQ_TCP(q) ((q) << 16) |
| 72 | #define MVNETA_DEF_RXQ_UDP(q) ((q) << 19) |
| 73 | #define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22) |
| 74 | #define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25) |
| 75 | #define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \ |
| 76 | MVNETA_DEF_RXQ_ARP(q) | \ |
| 77 | MVNETA_DEF_RXQ_TCP(q) | \ |
| 78 | MVNETA_DEF_RXQ_UDP(q) | \ |
| 79 | MVNETA_DEF_RXQ_BPDU(q) | \ |
| 80 | MVNETA_TX_UNSET_ERR_SUM | \ |
| 81 | MVNETA_RX_CSUM_WITH_PSEUDO_HDR) |
| 82 | #define MVNETA_PORT_CONFIG_EXTEND 0x2404 |
| 83 | #define MVNETA_MAC_ADDR_LOW 0x2414 |
| 84 | #define MVNETA_MAC_ADDR_HIGH 0x2418 |
| 85 | #define MVNETA_SDMA_CONFIG 0x241c |
| 86 | #define MVNETA_SDMA_BRST_SIZE_16 4 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 87 | #define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1) |
| 88 | #define MVNETA_RX_NO_DATA_SWAP BIT(4) |
| 89 | #define MVNETA_TX_NO_DATA_SWAP BIT(5) |
Thomas Petazzoni | 9ad8fef | 2013-07-29 15:21:28 +0200 | [diff] [blame] | 90 | #define MVNETA_DESC_SWAP BIT(6) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 91 | #define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22) |
| 92 | #define MVNETA_PORT_STATUS 0x2444 |
| 93 | #define MVNETA_TX_IN_PRGRS BIT(1) |
| 94 | #define MVNETA_TX_FIFO_EMPTY BIT(8) |
| 95 | #define MVNETA_RX_MIN_FRAME_SIZE 0x247c |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 96 | #define MVNETA_SERDES_CFG 0x24A0 |
Arnaud Patard \(Rtp\) | 5445eaf | 2013-07-29 21:56:48 +0200 | [diff] [blame] | 97 | #define MVNETA_SGMII_SERDES_PROTO 0x0cc7 |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 98 | #define MVNETA_QSGMII_SERDES_PROTO 0x0667 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 99 | #define MVNETA_TYPE_PRIO 0x24bc |
| 100 | #define MVNETA_FORCE_UNI BIT(21) |
| 101 | #define MVNETA_TXQ_CMD_1 0x24e4 |
| 102 | #define MVNETA_TXQ_CMD 0x2448 |
| 103 | #define MVNETA_TXQ_DISABLE_SHIFT 8 |
| 104 | #define MVNETA_TXQ_ENABLE_MASK 0x000000ff |
Andrew Lunn | e483911 | 2015-10-22 18:37:36 +0100 | [diff] [blame] | 105 | #define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484 |
| 106 | #define MVNETA_OVERRUN_FRAME_COUNT 0x2488 |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 107 | #define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4 |
| 108 | #define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 109 | #define MVNETA_ACC_MODE 0x2500 |
| 110 | #define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2)) |
| 111 | #define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff |
| 112 | #define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00 |
| 113 | #define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2)) |
willy tarreau | 40ba35e | 2014-01-16 08:20:10 +0100 | [diff] [blame] | 114 | |
| 115 | /* Exception Interrupt Port/Queue Cause register */ |
| 116 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 117 | #define MVNETA_INTR_NEW_CAUSE 0x25a0 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 118 | #define MVNETA_INTR_NEW_MASK 0x25a4 |
willy tarreau | 40ba35e | 2014-01-16 08:20:10 +0100 | [diff] [blame] | 119 | |
| 120 | /* bits 0..7 = TXQ SENT, one bit per queue. |
| 121 | * bits 8..15 = RXQ OCCUP, one bit per queue. |
| 122 | * bits 16..23 = RXQ FREE, one bit per queue. |
| 123 | * bit 29 = OLD_REG_SUM, see old reg ? |
| 124 | * bit 30 = TX_ERR_SUM, one bit for 4 ports |
| 125 | * bit 31 = MISC_SUM, one bit for 4 ports |
| 126 | */ |
| 127 | #define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0) |
| 128 | #define MVNETA_TX_INTR_MASK_ALL (0xff << 0) |
| 129 | #define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8) |
| 130 | #define MVNETA_RX_INTR_MASK_ALL (0xff << 8) |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 131 | #define MVNETA_MISCINTR_INTR_MASK BIT(31) |
willy tarreau | 40ba35e | 2014-01-16 08:20:10 +0100 | [diff] [blame] | 132 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 133 | #define MVNETA_INTR_OLD_CAUSE 0x25a8 |
| 134 | #define MVNETA_INTR_OLD_MASK 0x25ac |
willy tarreau | 40ba35e | 2014-01-16 08:20:10 +0100 | [diff] [blame] | 135 | |
| 136 | /* Data Path Port/Queue Cause Register */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 137 | #define MVNETA_INTR_MISC_CAUSE 0x25b0 |
| 138 | #define MVNETA_INTR_MISC_MASK 0x25b4 |
willy tarreau | 40ba35e | 2014-01-16 08:20:10 +0100 | [diff] [blame] | 139 | |
| 140 | #define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0) |
| 141 | #define MVNETA_CAUSE_LINK_CHANGE BIT(1) |
| 142 | #define MVNETA_CAUSE_PTP BIT(4) |
| 143 | |
| 144 | #define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7) |
| 145 | #define MVNETA_CAUSE_RX_OVERRUN BIT(8) |
| 146 | #define MVNETA_CAUSE_RX_CRC_ERROR BIT(9) |
| 147 | #define MVNETA_CAUSE_RX_LARGE_PKT BIT(10) |
| 148 | #define MVNETA_CAUSE_TX_UNDERUN BIT(11) |
| 149 | #define MVNETA_CAUSE_PRBS_ERR BIT(12) |
| 150 | #define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13) |
| 151 | #define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14) |
| 152 | |
| 153 | #define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16 |
| 154 | #define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT) |
| 155 | #define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool))) |
| 156 | |
| 157 | #define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24 |
| 158 | #define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT) |
| 159 | #define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q))) |
| 160 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 161 | #define MVNETA_INTR_ENABLE 0x25b8 |
| 162 | #define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00 |
Marcin Wojtas | dc1aadf | 2015-11-30 13:27:43 +0100 | [diff] [blame^] | 163 | #define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff |
willy tarreau | 40ba35e | 2014-01-16 08:20:10 +0100 | [diff] [blame] | 164 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 165 | #define MVNETA_RXQ_CMD 0x2680 |
| 166 | #define MVNETA_RXQ_DISABLE_SHIFT 8 |
| 167 | #define MVNETA_RXQ_ENABLE_MASK 0x000000ff |
| 168 | #define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4)) |
| 169 | #define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4)) |
| 170 | #define MVNETA_GMAC_CTRL_0 0x2c00 |
| 171 | #define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2 |
| 172 | #define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc |
| 173 | #define MVNETA_GMAC0_PORT_ENABLE BIT(0) |
| 174 | #define MVNETA_GMAC_CTRL_2 0x2c08 |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 175 | #define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0) |
Thomas Petazzoni | a79121d | 2014-03-26 00:25:41 +0100 | [diff] [blame] | 176 | #define MVNETA_GMAC2_PCS_ENABLE BIT(3) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 177 | #define MVNETA_GMAC2_PORT_RGMII BIT(4) |
| 178 | #define MVNETA_GMAC2_PORT_RESET BIT(6) |
| 179 | #define MVNETA_GMAC_STATUS 0x2c10 |
| 180 | #define MVNETA_GMAC_LINK_UP BIT(0) |
| 181 | #define MVNETA_GMAC_SPEED_1000 BIT(1) |
| 182 | #define MVNETA_GMAC_SPEED_100 BIT(2) |
| 183 | #define MVNETA_GMAC_FULL_DUPLEX BIT(3) |
| 184 | #define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4) |
| 185 | #define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5) |
| 186 | #define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6) |
| 187 | #define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7) |
| 188 | #define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c |
| 189 | #define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0) |
| 190 | #define MVNETA_GMAC_FORCE_LINK_PASS BIT(1) |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 191 | #define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 192 | #define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5) |
| 193 | #define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6) |
Thomas Petazzoni | 7140860 | 2013-09-04 16:21:18 +0200 | [diff] [blame] | 194 | #define MVNETA_GMAC_AN_SPEED_EN BIT(7) |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 195 | #define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 196 | #define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12) |
Thomas Petazzoni | 7140860 | 2013-09-04 16:21:18 +0200 | [diff] [blame] | 197 | #define MVNETA_GMAC_AN_DUPLEX_EN BIT(13) |
Andrew Lunn | e483911 | 2015-10-22 18:37:36 +0100 | [diff] [blame] | 198 | #define MVNETA_MIB_COUNTERS_BASE 0x3000 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 199 | #define MVNETA_MIB_LATE_COLLISION 0x7c |
| 200 | #define MVNETA_DA_FILT_SPEC_MCAST 0x3400 |
| 201 | #define MVNETA_DA_FILT_OTH_MCAST 0x3500 |
| 202 | #define MVNETA_DA_FILT_UCAST_BASE 0x3600 |
| 203 | #define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2)) |
| 204 | #define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2)) |
| 205 | #define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000 |
| 206 | #define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16) |
| 207 | #define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2)) |
| 208 | #define MVNETA_TXQ_DEC_SENT_SHIFT 16 |
| 209 | #define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2)) |
| 210 | #define MVNETA_TXQ_SENT_DESC_SHIFT 16 |
| 211 | #define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000 |
| 212 | #define MVNETA_PORT_TX_RESET 0x3cf0 |
| 213 | #define MVNETA_PORT_TX_DMA_RESET BIT(0) |
| 214 | #define MVNETA_TX_MTU 0x3e0c |
| 215 | #define MVNETA_TX_TOKEN_SIZE 0x3e14 |
| 216 | #define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff |
| 217 | #define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2)) |
| 218 | #define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff |
| 219 | |
| 220 | #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff |
| 221 | |
| 222 | /* Descriptor ring Macros */ |
| 223 | #define MVNETA_QUEUE_NEXT_DESC(q, index) \ |
| 224 | (((index) < (q)->last_desc) ? ((index) + 1) : 0) |
| 225 | |
| 226 | /* Various constants */ |
| 227 | |
| 228 | /* Coalescing */ |
willy tarreau | aebea2b | 2014-12-02 08:13:04 +0100 | [diff] [blame] | 229 | #define MVNETA_TXDONE_COAL_PKTS 1 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 230 | #define MVNETA_RX_COAL_PKTS 32 |
| 231 | #define MVNETA_RX_COAL_USEC 100 |
| 232 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 233 | /* The two bytes Marvell header. Either contains a special value used |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 234 | * by Marvell switches when a specific hardware mode is enabled (not |
| 235 | * supported by this driver) or is filled automatically by zeroes on |
| 236 | * the RX side. Those two bytes being at the front of the Ethernet |
| 237 | * header, they allow to have the IP header aligned on a 4 bytes |
| 238 | * boundary automatically: the hardware skips those two bytes on its |
| 239 | * own. |
| 240 | */ |
| 241 | #define MVNETA_MH_SIZE 2 |
| 242 | |
| 243 | #define MVNETA_VLAN_TAG_LEN 4 |
| 244 | |
| 245 | #define MVNETA_CPU_D_CACHE_LINE_SIZE 32 |
| 246 | #define MVNETA_TX_CSUM_MAX_SIZE 9800 |
| 247 | #define MVNETA_ACC_MODE_EXT 1 |
| 248 | |
| 249 | /* Timeout constants */ |
| 250 | #define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000 |
| 251 | #define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000 |
| 252 | #define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000 |
| 253 | |
| 254 | #define MVNETA_TX_MTU_MAX 0x3ffff |
| 255 | |
Ezequiel Garcia | 2adb719 | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 256 | /* TSO header size */ |
| 257 | #define TSO_HEADER_SIZE 128 |
| 258 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 259 | /* Max number of Rx descriptors */ |
| 260 | #define MVNETA_MAX_RXD 128 |
| 261 | |
| 262 | /* Max number of Tx descriptors */ |
| 263 | #define MVNETA_MAX_TXD 532 |
| 264 | |
Ezequiel Garcia | 8eef5f9 | 2014-05-30 13:40:05 -0300 | [diff] [blame] | 265 | /* Max number of allowed TCP segments for software TSO */ |
| 266 | #define MVNETA_MAX_TSO_SEGS 100 |
| 267 | |
| 268 | #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS) |
| 269 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 270 | /* descriptor aligned size */ |
| 271 | #define MVNETA_DESC_ALIGNED_SIZE 32 |
| 272 | |
| 273 | #define MVNETA_RX_PKT_SIZE(mtu) \ |
| 274 | ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \ |
| 275 | ETH_HLEN + ETH_FCS_LEN, \ |
| 276 | MVNETA_CPU_D_CACHE_LINE_SIZE) |
| 277 | |
Ezequiel Garcia | 2e3173a | 2014-05-30 13:40:07 -0300 | [diff] [blame] | 278 | #define IS_TSO_HEADER(txq, addr) \ |
| 279 | ((addr >= txq->tso_hdrs_phys) && \ |
| 280 | (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE)) |
| 281 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 282 | #define MVNETA_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD) |
| 283 | |
Russell King | 9b0cdef | 2015-10-22 18:37:30 +0100 | [diff] [blame] | 284 | struct mvneta_statistic { |
| 285 | unsigned short offset; |
| 286 | unsigned short type; |
| 287 | const char name[ETH_GSTRING_LEN]; |
| 288 | }; |
| 289 | |
| 290 | #define T_REG_32 32 |
| 291 | #define T_REG_64 64 |
| 292 | |
| 293 | static const struct mvneta_statistic mvneta_statistics[] = { |
| 294 | { 0x3000, T_REG_64, "good_octets_received", }, |
| 295 | { 0x3010, T_REG_32, "good_frames_received", }, |
| 296 | { 0x3008, T_REG_32, "bad_octets_received", }, |
| 297 | { 0x3014, T_REG_32, "bad_frames_received", }, |
| 298 | { 0x3018, T_REG_32, "broadcast_frames_received", }, |
| 299 | { 0x301c, T_REG_32, "multicast_frames_received", }, |
| 300 | { 0x3050, T_REG_32, "unrec_mac_control_received", }, |
| 301 | { 0x3058, T_REG_32, "good_fc_received", }, |
| 302 | { 0x305c, T_REG_32, "bad_fc_received", }, |
| 303 | { 0x3060, T_REG_32, "undersize_received", }, |
| 304 | { 0x3064, T_REG_32, "fragments_received", }, |
| 305 | { 0x3068, T_REG_32, "oversize_received", }, |
| 306 | { 0x306c, T_REG_32, "jabber_received", }, |
| 307 | { 0x3070, T_REG_32, "mac_receive_error", }, |
| 308 | { 0x3074, T_REG_32, "bad_crc_event", }, |
| 309 | { 0x3078, T_REG_32, "collision", }, |
| 310 | { 0x307c, T_REG_32, "late_collision", }, |
| 311 | { 0x2484, T_REG_32, "rx_discard", }, |
| 312 | { 0x2488, T_REG_32, "rx_overrun", }, |
| 313 | { 0x3020, T_REG_32, "frames_64_octets", }, |
| 314 | { 0x3024, T_REG_32, "frames_65_to_127_octets", }, |
| 315 | { 0x3028, T_REG_32, "frames_128_to_255_octets", }, |
| 316 | { 0x302c, T_REG_32, "frames_256_to_511_octets", }, |
| 317 | { 0x3030, T_REG_32, "frames_512_to_1023_octets", }, |
| 318 | { 0x3034, T_REG_32, "frames_1024_to_max_octets", }, |
| 319 | { 0x3038, T_REG_64, "good_octets_sent", }, |
| 320 | { 0x3040, T_REG_32, "good_frames_sent", }, |
| 321 | { 0x3044, T_REG_32, "excessive_collision", }, |
| 322 | { 0x3048, T_REG_32, "multicast_frames_sent", }, |
| 323 | { 0x304c, T_REG_32, "broadcast_frames_sent", }, |
| 324 | { 0x3054, T_REG_32, "fc_sent", }, |
| 325 | { 0x300c, T_REG_32, "internal_mac_transmit_err", }, |
| 326 | }; |
| 327 | |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 328 | struct mvneta_pcpu_stats { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 329 | struct u64_stats_sync syncp; |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 330 | u64 rx_packets; |
| 331 | u64 rx_bytes; |
| 332 | u64 tx_packets; |
| 333 | u64 tx_bytes; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 334 | }; |
| 335 | |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 336 | struct mvneta_pcpu_port { |
| 337 | /* Pointer to the shared port */ |
| 338 | struct mvneta_port *pp; |
| 339 | |
| 340 | /* Pointer to the CPU-local NAPI struct */ |
| 341 | struct napi_struct napi; |
| 342 | |
| 343 | /* Cause of the previous interrupt */ |
| 344 | u32 cause_rx_tx; |
| 345 | }; |
| 346 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 347 | struct mvneta_port { |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 348 | struct mvneta_pcpu_port __percpu *ports; |
| 349 | struct mvneta_pcpu_stats __percpu *stats; |
| 350 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 351 | int pkt_size; |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 352 | unsigned int frag_size; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 353 | void __iomem *base; |
| 354 | struct mvneta_rx_queue *rxqs; |
| 355 | struct mvneta_tx_queue *txqs; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 356 | struct net_device *dev; |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 357 | struct notifier_block cpu_notifier; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 358 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 359 | /* Core clock */ |
Thomas Petazzoni | 189dd62 | 2012-11-19 14:15:25 +0100 | [diff] [blame] | 360 | struct clk *clk; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 361 | u8 mcast_count[256]; |
| 362 | u16 tx_ring_size; |
| 363 | u16 rx_ring_size; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 364 | |
| 365 | struct mii_bus *mii_bus; |
| 366 | struct phy_device *phy_dev; |
| 367 | phy_interface_t phy_interface; |
| 368 | struct device_node *phy_node; |
| 369 | unsigned int link; |
| 370 | unsigned int duplex; |
| 371 | unsigned int speed; |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 372 | unsigned int tx_csum_limit; |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 373 | int use_inband_status:1; |
Russell King | 9b0cdef | 2015-10-22 18:37:30 +0100 | [diff] [blame] | 374 | |
| 375 | u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)]; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 376 | }; |
| 377 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 378 | /* The mvneta_tx_desc and mvneta_rx_desc structures describe the |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 379 | * layout of the transmit and reception DMA descriptors, and their |
| 380 | * layout is therefore defined by the hardware design |
| 381 | */ |
Thomas Petazzoni | 6083ed4 | 2013-07-29 15:21:27 +0200 | [diff] [blame] | 382 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 383 | #define MVNETA_TX_L3_OFF_SHIFT 0 |
| 384 | #define MVNETA_TX_IP_HLEN_SHIFT 8 |
| 385 | #define MVNETA_TX_L4_UDP BIT(16) |
| 386 | #define MVNETA_TX_L3_IP6 BIT(17) |
| 387 | #define MVNETA_TXD_IP_CSUM BIT(18) |
| 388 | #define MVNETA_TXD_Z_PAD BIT(19) |
| 389 | #define MVNETA_TXD_L_DESC BIT(20) |
| 390 | #define MVNETA_TXD_F_DESC BIT(21) |
| 391 | #define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \ |
| 392 | MVNETA_TXD_L_DESC | \ |
| 393 | MVNETA_TXD_F_DESC) |
| 394 | #define MVNETA_TX_L4_CSUM_FULL BIT(30) |
| 395 | #define MVNETA_TX_L4_CSUM_NOT BIT(31) |
| 396 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 397 | #define MVNETA_RXD_ERR_CRC 0x0 |
| 398 | #define MVNETA_RXD_ERR_SUMMARY BIT(16) |
| 399 | #define MVNETA_RXD_ERR_OVERRUN BIT(17) |
| 400 | #define MVNETA_RXD_ERR_LEN BIT(18) |
| 401 | #define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18)) |
| 402 | #define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18)) |
| 403 | #define MVNETA_RXD_L3_IP4 BIT(25) |
| 404 | #define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27)) |
| 405 | #define MVNETA_RXD_L4_CSUM_OK BIT(30) |
| 406 | |
Thomas Petazzoni | 9ad8fef | 2013-07-29 15:21:28 +0200 | [diff] [blame] | 407 | #if defined(__LITTLE_ENDIAN) |
Thomas Petazzoni | 6083ed4 | 2013-07-29 15:21:27 +0200 | [diff] [blame] | 408 | struct mvneta_tx_desc { |
| 409 | u32 command; /* Options used by HW for packet transmitting.*/ |
| 410 | u16 reserverd1; /* csum_l4 (for future use) */ |
| 411 | u16 data_size; /* Data size of transmitted packet in bytes */ |
| 412 | u32 buf_phys_addr; /* Physical addr of transmitted buffer */ |
| 413 | u32 reserved2; /* hw_cmd - (for future use, PMT) */ |
| 414 | u32 reserved3[4]; /* Reserved - (for future use) */ |
| 415 | }; |
| 416 | |
| 417 | struct mvneta_rx_desc { |
| 418 | u32 status; /* Info about received packet */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 419 | u16 reserved1; /* pnc_info - (for future use, PnC) */ |
| 420 | u16 data_size; /* Size of received packet in bytes */ |
Thomas Petazzoni | 6083ed4 | 2013-07-29 15:21:27 +0200 | [diff] [blame] | 421 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 422 | u32 buf_phys_addr; /* Physical address of the buffer */ |
| 423 | u32 reserved2; /* pnc_flow_id (for future use, PnC) */ |
Thomas Petazzoni | 6083ed4 | 2013-07-29 15:21:27 +0200 | [diff] [blame] | 424 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 425 | u32 buf_cookie; /* cookie for access to RX buffer in rx path */ |
| 426 | u16 reserved3; /* prefetch_cmd, for future use */ |
| 427 | u16 reserved4; /* csum_l4 - (for future use, PnC) */ |
Thomas Petazzoni | 6083ed4 | 2013-07-29 15:21:27 +0200 | [diff] [blame] | 428 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 429 | u32 reserved5; /* pnc_extra PnC (for future use, PnC) */ |
| 430 | u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */ |
| 431 | }; |
Thomas Petazzoni | 9ad8fef | 2013-07-29 15:21:28 +0200 | [diff] [blame] | 432 | #else |
| 433 | struct mvneta_tx_desc { |
| 434 | u16 data_size; /* Data size of transmitted packet in bytes */ |
| 435 | u16 reserverd1; /* csum_l4 (for future use) */ |
| 436 | u32 command; /* Options used by HW for packet transmitting.*/ |
| 437 | u32 reserved2; /* hw_cmd - (for future use, PMT) */ |
| 438 | u32 buf_phys_addr; /* Physical addr of transmitted buffer */ |
| 439 | u32 reserved3[4]; /* Reserved - (for future use) */ |
| 440 | }; |
| 441 | |
| 442 | struct mvneta_rx_desc { |
| 443 | u16 data_size; /* Size of received packet in bytes */ |
| 444 | u16 reserved1; /* pnc_info - (for future use, PnC) */ |
| 445 | u32 status; /* Info about received packet */ |
| 446 | |
| 447 | u32 reserved2; /* pnc_flow_id (for future use, PnC) */ |
| 448 | u32 buf_phys_addr; /* Physical address of the buffer */ |
| 449 | |
| 450 | u16 reserved4; /* csum_l4 - (for future use, PnC) */ |
| 451 | u16 reserved3; /* prefetch_cmd, for future use */ |
| 452 | u32 buf_cookie; /* cookie for access to RX buffer in rx path */ |
| 453 | |
| 454 | u32 reserved5; /* pnc_extra PnC (for future use, PnC) */ |
| 455 | u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */ |
| 456 | }; |
| 457 | #endif |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 458 | |
| 459 | struct mvneta_tx_queue { |
| 460 | /* Number of this TX queue, in the range 0-7 */ |
| 461 | u8 id; |
| 462 | |
| 463 | /* Number of TX DMA descriptors in the descriptor ring */ |
| 464 | int size; |
| 465 | |
| 466 | /* Number of currently used TX DMA descriptor in the |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 467 | * descriptor ring |
| 468 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 469 | int count; |
Ezequiel Garcia | 8eef5f9 | 2014-05-30 13:40:05 -0300 | [diff] [blame] | 470 | int tx_stop_threshold; |
| 471 | int tx_wake_threshold; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 472 | |
| 473 | /* Array of transmitted skb */ |
| 474 | struct sk_buff **tx_skb; |
| 475 | |
| 476 | /* Index of last TX DMA descriptor that was inserted */ |
| 477 | int txq_put_index; |
| 478 | |
| 479 | /* Index of the TX DMA descriptor to be cleaned up */ |
| 480 | int txq_get_index; |
| 481 | |
| 482 | u32 done_pkts_coal; |
| 483 | |
| 484 | /* Virtual address of the TX DMA descriptors array */ |
| 485 | struct mvneta_tx_desc *descs; |
| 486 | |
| 487 | /* DMA address of the TX DMA descriptors array */ |
| 488 | dma_addr_t descs_phys; |
| 489 | |
| 490 | /* Index of the last TX DMA descriptor */ |
| 491 | int last_desc; |
| 492 | |
| 493 | /* Index of the next TX DMA descriptor to process */ |
| 494 | int next_desc_to_proc; |
Ezequiel Garcia | 2adb719 | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 495 | |
| 496 | /* DMA buffers for TSO headers */ |
| 497 | char *tso_hdrs; |
| 498 | |
| 499 | /* DMA address of TSO headers */ |
| 500 | dma_addr_t tso_hdrs_phys; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 501 | }; |
| 502 | |
| 503 | struct mvneta_rx_queue { |
| 504 | /* rx queue number, in the range 0-7 */ |
| 505 | u8 id; |
| 506 | |
| 507 | /* num of rx descriptors in the rx descriptor ring */ |
| 508 | int size; |
| 509 | |
| 510 | /* counter of times when mvneta_refill() failed */ |
| 511 | int missed; |
| 512 | |
| 513 | u32 pkts_coal; |
| 514 | u32 time_coal; |
| 515 | |
| 516 | /* Virtual address of the RX DMA descriptors array */ |
| 517 | struct mvneta_rx_desc *descs; |
| 518 | |
| 519 | /* DMA address of the RX DMA descriptors array */ |
| 520 | dma_addr_t descs_phys; |
| 521 | |
| 522 | /* Index of the last RX DMA descriptor */ |
| 523 | int last_desc; |
| 524 | |
| 525 | /* Index of the next RX DMA descriptor to process */ |
| 526 | int next_desc_to_proc; |
| 527 | }; |
| 528 | |
Ezequiel Garcia | edadb7f | 2014-05-22 20:07:01 -0300 | [diff] [blame] | 529 | /* The hardware supports eight (8) rx queues, but we are only allowing |
| 530 | * the first one to be used. Therefore, let's just allocate one queue. |
| 531 | */ |
Maxime Ripard | d893665 | 2015-09-25 18:09:37 +0200 | [diff] [blame] | 532 | static int rxq_number = 8; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 533 | static int txq_number = 8; |
| 534 | |
| 535 | static int rxq_def; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 536 | |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 537 | static int rx_copybreak __read_mostly = 256; |
| 538 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 539 | #define MVNETA_DRIVER_NAME "mvneta" |
| 540 | #define MVNETA_DRIVER_VERSION "1.0" |
| 541 | |
| 542 | /* Utility/helper methods */ |
| 543 | |
| 544 | /* Write helper method */ |
| 545 | static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data) |
| 546 | { |
| 547 | writel(data, pp->base + offset); |
| 548 | } |
| 549 | |
| 550 | /* Read helper method */ |
| 551 | static u32 mvreg_read(struct mvneta_port *pp, u32 offset) |
| 552 | { |
| 553 | return readl(pp->base + offset); |
| 554 | } |
| 555 | |
| 556 | /* Increment txq get counter */ |
| 557 | static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq) |
| 558 | { |
| 559 | txq->txq_get_index++; |
| 560 | if (txq->txq_get_index == txq->size) |
| 561 | txq->txq_get_index = 0; |
| 562 | } |
| 563 | |
| 564 | /* Increment txq put counter */ |
| 565 | static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq) |
| 566 | { |
| 567 | txq->txq_put_index++; |
| 568 | if (txq->txq_put_index == txq->size) |
| 569 | txq->txq_put_index = 0; |
| 570 | } |
| 571 | |
| 572 | |
| 573 | /* Clear all MIB counters */ |
| 574 | static void mvneta_mib_counters_clear(struct mvneta_port *pp) |
| 575 | { |
| 576 | int i; |
| 577 | u32 dummy; |
| 578 | |
| 579 | /* Perform dummy reads from MIB counters */ |
| 580 | for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4) |
| 581 | dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i)); |
Andrew Lunn | e483911 | 2015-10-22 18:37:36 +0100 | [diff] [blame] | 582 | dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT); |
| 583 | dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* Get System Network Statistics */ |
| 587 | struct rtnl_link_stats64 *mvneta_get_stats64(struct net_device *dev, |
| 588 | struct rtnl_link_stats64 *stats) |
| 589 | { |
| 590 | struct mvneta_port *pp = netdev_priv(dev); |
| 591 | unsigned int start; |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 592 | int cpu; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 593 | |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 594 | for_each_possible_cpu(cpu) { |
| 595 | struct mvneta_pcpu_stats *cpu_stats; |
| 596 | u64 rx_packets; |
| 597 | u64 rx_bytes; |
| 598 | u64 tx_packets; |
| 599 | u64 tx_bytes; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 600 | |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 601 | cpu_stats = per_cpu_ptr(pp->stats, cpu); |
| 602 | do { |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 603 | start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 604 | rx_packets = cpu_stats->rx_packets; |
| 605 | rx_bytes = cpu_stats->rx_bytes; |
| 606 | tx_packets = cpu_stats->tx_packets; |
| 607 | tx_bytes = cpu_stats->tx_bytes; |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 608 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 609 | |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 610 | stats->rx_packets += rx_packets; |
| 611 | stats->rx_bytes += rx_bytes; |
| 612 | stats->tx_packets += tx_packets; |
| 613 | stats->tx_bytes += tx_bytes; |
| 614 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 615 | |
| 616 | stats->rx_errors = dev->stats.rx_errors; |
| 617 | stats->rx_dropped = dev->stats.rx_dropped; |
| 618 | |
| 619 | stats->tx_dropped = dev->stats.tx_dropped; |
| 620 | |
| 621 | return stats; |
| 622 | } |
| 623 | |
| 624 | /* Rx descriptors helper methods */ |
| 625 | |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 626 | /* Checks whether the RX descriptor having this status is both the first |
| 627 | * and the last descriptor for the RX packet. Each RX packet is currently |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 628 | * received through a single RX descriptor, so not having each RX |
| 629 | * descriptor with its first and last bits set is an error |
| 630 | */ |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 631 | static int mvneta_rxq_desc_is_first_last(u32 status) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 632 | { |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 633 | return (status & MVNETA_RXD_FIRST_LAST_DESC) == |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 634 | MVNETA_RXD_FIRST_LAST_DESC; |
| 635 | } |
| 636 | |
| 637 | /* Add number of descriptors ready to receive new packets */ |
| 638 | static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp, |
| 639 | struct mvneta_rx_queue *rxq, |
| 640 | int ndescs) |
| 641 | { |
| 642 | /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 643 | * be added at once |
| 644 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 645 | while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) { |
| 646 | mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), |
| 647 | (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX << |
| 648 | MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT)); |
| 649 | ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX; |
| 650 | } |
| 651 | |
| 652 | mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), |
| 653 | (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT)); |
| 654 | } |
| 655 | |
| 656 | /* Get number of RX descriptors occupied by received packets */ |
| 657 | static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp, |
| 658 | struct mvneta_rx_queue *rxq) |
| 659 | { |
| 660 | u32 val; |
| 661 | |
| 662 | val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id)); |
| 663 | return val & MVNETA_RXQ_OCCUPIED_ALL_MASK; |
| 664 | } |
| 665 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 666 | /* Update num of rx desc called upon return from rx path or |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 667 | * from mvneta_rxq_drop_pkts(). |
| 668 | */ |
| 669 | static void mvneta_rxq_desc_num_update(struct mvneta_port *pp, |
| 670 | struct mvneta_rx_queue *rxq, |
| 671 | int rx_done, int rx_filled) |
| 672 | { |
| 673 | u32 val; |
| 674 | |
| 675 | if ((rx_done <= 0xff) && (rx_filled <= 0xff)) { |
| 676 | val = rx_done | |
| 677 | (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT); |
| 678 | mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val); |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | /* Only 255 descriptors can be added at once */ |
| 683 | while ((rx_done > 0) || (rx_filled > 0)) { |
| 684 | if (rx_done <= 0xff) { |
| 685 | val = rx_done; |
| 686 | rx_done = 0; |
| 687 | } else { |
| 688 | val = 0xff; |
| 689 | rx_done -= 0xff; |
| 690 | } |
| 691 | if (rx_filled <= 0xff) { |
| 692 | val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT; |
| 693 | rx_filled = 0; |
| 694 | } else { |
| 695 | val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT; |
| 696 | rx_filled -= 0xff; |
| 697 | } |
| 698 | mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /* Get pointer to next RX descriptor to be processed by SW */ |
| 703 | static struct mvneta_rx_desc * |
| 704 | mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq) |
| 705 | { |
| 706 | int rx_desc = rxq->next_desc_to_proc; |
| 707 | |
| 708 | rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc); |
willy tarreau | 34e4179 | 2014-01-16 08:20:15 +0100 | [diff] [blame] | 709 | prefetch(rxq->descs + rxq->next_desc_to_proc); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 710 | return rxq->descs + rx_desc; |
| 711 | } |
| 712 | |
| 713 | /* Change maximum receive size of the port. */ |
| 714 | static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size) |
| 715 | { |
| 716 | u32 val; |
| 717 | |
| 718 | val = mvreg_read(pp, MVNETA_GMAC_CTRL_0); |
| 719 | val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK; |
| 720 | val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) << |
| 721 | MVNETA_GMAC_MAX_RX_SIZE_SHIFT; |
| 722 | mvreg_write(pp, MVNETA_GMAC_CTRL_0, val); |
| 723 | } |
| 724 | |
| 725 | |
| 726 | /* Set rx queue offset */ |
| 727 | static void mvneta_rxq_offset_set(struct mvneta_port *pp, |
| 728 | struct mvneta_rx_queue *rxq, |
| 729 | int offset) |
| 730 | { |
| 731 | u32 val; |
| 732 | |
| 733 | val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id)); |
| 734 | val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK; |
| 735 | |
| 736 | /* Offset is in */ |
| 737 | val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3); |
| 738 | mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val); |
| 739 | } |
| 740 | |
| 741 | |
| 742 | /* Tx descriptors helper methods */ |
| 743 | |
| 744 | /* Update HW with number of TX descriptors to be sent */ |
| 745 | static void mvneta_txq_pend_desc_add(struct mvneta_port *pp, |
| 746 | struct mvneta_tx_queue *txq, |
| 747 | int pend_desc) |
| 748 | { |
| 749 | u32 val; |
| 750 | |
| 751 | /* Only 255 descriptors can be added at once ; Assume caller |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 752 | * process TX desriptors in quanta less than 256 |
| 753 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 754 | val = pend_desc; |
| 755 | mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val); |
| 756 | } |
| 757 | |
| 758 | /* Get pointer to next TX descriptor to be processed (send) by HW */ |
| 759 | static struct mvneta_tx_desc * |
| 760 | mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq) |
| 761 | { |
| 762 | int tx_desc = txq->next_desc_to_proc; |
| 763 | |
| 764 | txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc); |
| 765 | return txq->descs + tx_desc; |
| 766 | } |
| 767 | |
| 768 | /* Release the last allocated TX descriptor. Useful to handle DMA |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 769 | * mapping failures in the TX path. |
| 770 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 771 | static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq) |
| 772 | { |
| 773 | if (txq->next_desc_to_proc == 0) |
| 774 | txq->next_desc_to_proc = txq->last_desc - 1; |
| 775 | else |
| 776 | txq->next_desc_to_proc--; |
| 777 | } |
| 778 | |
| 779 | /* Set rxq buf size */ |
| 780 | static void mvneta_rxq_buf_size_set(struct mvneta_port *pp, |
| 781 | struct mvneta_rx_queue *rxq, |
| 782 | int buf_size) |
| 783 | { |
| 784 | u32 val; |
| 785 | |
| 786 | val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id)); |
| 787 | |
| 788 | val &= ~MVNETA_RXQ_BUF_SIZE_MASK; |
| 789 | val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT); |
| 790 | |
| 791 | mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val); |
| 792 | } |
| 793 | |
| 794 | /* Disable buffer management (BM) */ |
| 795 | static void mvneta_rxq_bm_disable(struct mvneta_port *pp, |
| 796 | struct mvneta_rx_queue *rxq) |
| 797 | { |
| 798 | u32 val; |
| 799 | |
| 800 | val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id)); |
| 801 | val &= ~MVNETA_RXQ_HW_BUF_ALLOC; |
| 802 | mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val); |
| 803 | } |
| 804 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 805 | /* Start the Ethernet port RX and TX activity */ |
| 806 | static void mvneta_port_up(struct mvneta_port *pp) |
| 807 | { |
| 808 | int queue; |
| 809 | u32 q_map; |
| 810 | |
| 811 | /* Enable all initialized TXs. */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 812 | q_map = 0; |
| 813 | for (queue = 0; queue < txq_number; queue++) { |
| 814 | struct mvneta_tx_queue *txq = &pp->txqs[queue]; |
| 815 | if (txq->descs != NULL) |
| 816 | q_map |= (1 << queue); |
| 817 | } |
| 818 | mvreg_write(pp, MVNETA_TXQ_CMD, q_map); |
| 819 | |
| 820 | /* Enable all initialized RXQs. */ |
Maxime Ripard | d893665 | 2015-09-25 18:09:37 +0200 | [diff] [blame] | 821 | mvreg_write(pp, MVNETA_RXQ_CMD, BIT(rxq_def)); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /* Stop the Ethernet port activity */ |
| 825 | static void mvneta_port_down(struct mvneta_port *pp) |
| 826 | { |
| 827 | u32 val; |
| 828 | int count; |
| 829 | |
| 830 | /* Stop Rx port activity. Check port Rx activity. */ |
| 831 | val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK; |
| 832 | |
| 833 | /* Issue stop command for active channels only */ |
| 834 | if (val != 0) |
| 835 | mvreg_write(pp, MVNETA_RXQ_CMD, |
| 836 | val << MVNETA_RXQ_DISABLE_SHIFT); |
| 837 | |
| 838 | /* Wait for all Rx activity to terminate. */ |
| 839 | count = 0; |
| 840 | do { |
| 841 | if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) { |
| 842 | netdev_warn(pp->dev, |
| 843 | "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n", |
| 844 | val); |
| 845 | break; |
| 846 | } |
| 847 | mdelay(1); |
| 848 | |
| 849 | val = mvreg_read(pp, MVNETA_RXQ_CMD); |
| 850 | } while (val & 0xff); |
| 851 | |
| 852 | /* Stop Tx port activity. Check port Tx activity. Issue stop |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 853 | * command for active channels only |
| 854 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 855 | val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK; |
| 856 | |
| 857 | if (val != 0) |
| 858 | mvreg_write(pp, MVNETA_TXQ_CMD, |
| 859 | (val << MVNETA_TXQ_DISABLE_SHIFT)); |
| 860 | |
| 861 | /* Wait for all Tx activity to terminate. */ |
| 862 | count = 0; |
| 863 | do { |
| 864 | if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) { |
| 865 | netdev_warn(pp->dev, |
| 866 | "TIMEOUT for TX stopped status=0x%08x\n", |
| 867 | val); |
| 868 | break; |
| 869 | } |
| 870 | mdelay(1); |
| 871 | |
| 872 | /* Check TX Command reg that all Txqs are stopped */ |
| 873 | val = mvreg_read(pp, MVNETA_TXQ_CMD); |
| 874 | |
| 875 | } while (val & 0xff); |
| 876 | |
| 877 | /* Double check to verify that TX FIFO is empty */ |
| 878 | count = 0; |
| 879 | do { |
| 880 | if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) { |
| 881 | netdev_warn(pp->dev, |
| 882 | "TX FIFO empty timeout status=0x08%x\n", |
| 883 | val); |
| 884 | break; |
| 885 | } |
| 886 | mdelay(1); |
| 887 | |
| 888 | val = mvreg_read(pp, MVNETA_PORT_STATUS); |
| 889 | } while (!(val & MVNETA_TX_FIFO_EMPTY) && |
| 890 | (val & MVNETA_TX_IN_PRGRS)); |
| 891 | |
| 892 | udelay(200); |
| 893 | } |
| 894 | |
| 895 | /* Enable the port by setting the port enable bit of the MAC control register */ |
| 896 | static void mvneta_port_enable(struct mvneta_port *pp) |
| 897 | { |
| 898 | u32 val; |
| 899 | |
| 900 | /* Enable port */ |
| 901 | val = mvreg_read(pp, MVNETA_GMAC_CTRL_0); |
| 902 | val |= MVNETA_GMAC0_PORT_ENABLE; |
| 903 | mvreg_write(pp, MVNETA_GMAC_CTRL_0, val); |
| 904 | } |
| 905 | |
| 906 | /* Disable the port and wait for about 200 usec before retuning */ |
| 907 | static void mvneta_port_disable(struct mvneta_port *pp) |
| 908 | { |
| 909 | u32 val; |
| 910 | |
| 911 | /* Reset the Enable bit in the Serial Control Register */ |
| 912 | val = mvreg_read(pp, MVNETA_GMAC_CTRL_0); |
| 913 | val &= ~MVNETA_GMAC0_PORT_ENABLE; |
| 914 | mvreg_write(pp, MVNETA_GMAC_CTRL_0, val); |
| 915 | |
| 916 | udelay(200); |
| 917 | } |
| 918 | |
| 919 | /* Multicast tables methods */ |
| 920 | |
| 921 | /* Set all entries in Unicast MAC Table; queue==-1 means reject all */ |
| 922 | static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue) |
| 923 | { |
| 924 | int offset; |
| 925 | u32 val; |
| 926 | |
| 927 | if (queue == -1) { |
| 928 | val = 0; |
| 929 | } else { |
| 930 | val = 0x1 | (queue << 1); |
| 931 | val |= (val << 24) | (val << 16) | (val << 8); |
| 932 | } |
| 933 | |
| 934 | for (offset = 0; offset <= 0xc; offset += 4) |
| 935 | mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val); |
| 936 | } |
| 937 | |
| 938 | /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */ |
| 939 | static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue) |
| 940 | { |
| 941 | int offset; |
| 942 | u32 val; |
| 943 | |
| 944 | if (queue == -1) { |
| 945 | val = 0; |
| 946 | } else { |
| 947 | val = 0x1 | (queue << 1); |
| 948 | val |= (val << 24) | (val << 16) | (val << 8); |
| 949 | } |
| 950 | |
| 951 | for (offset = 0; offset <= 0xfc; offset += 4) |
| 952 | mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val); |
| 953 | |
| 954 | } |
| 955 | |
| 956 | /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */ |
| 957 | static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue) |
| 958 | { |
| 959 | int offset; |
| 960 | u32 val; |
| 961 | |
| 962 | if (queue == -1) { |
| 963 | memset(pp->mcast_count, 0, sizeof(pp->mcast_count)); |
| 964 | val = 0; |
| 965 | } else { |
| 966 | memset(pp->mcast_count, 1, sizeof(pp->mcast_count)); |
| 967 | val = 0x1 | (queue << 1); |
| 968 | val |= (val << 24) | (val << 16) | (val << 8); |
| 969 | } |
| 970 | |
| 971 | for (offset = 0; offset <= 0xfc; offset += 4) |
| 972 | mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val); |
| 973 | } |
| 974 | |
| 975 | /* This method sets defaults to the NETA port: |
| 976 | * Clears interrupt Cause and Mask registers. |
| 977 | * Clears all MAC tables. |
| 978 | * Sets defaults to all registers. |
| 979 | * Resets RX and TX descriptor rings. |
| 980 | * Resets PHY. |
| 981 | * This method can be called after mvneta_port_down() to return the port |
| 982 | * settings to defaults. |
| 983 | */ |
| 984 | static void mvneta_defaults_set(struct mvneta_port *pp) |
| 985 | { |
| 986 | int cpu; |
| 987 | int queue; |
| 988 | u32 val; |
| 989 | |
| 990 | /* Clear all Cause registers */ |
| 991 | mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0); |
| 992 | mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0); |
| 993 | mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0); |
| 994 | |
| 995 | /* Mask all interrupts */ |
| 996 | mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0); |
| 997 | mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0); |
| 998 | mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0); |
| 999 | mvreg_write(pp, MVNETA_INTR_ENABLE, 0); |
| 1000 | |
| 1001 | /* Enable MBUS Retry bit16 */ |
| 1002 | mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20); |
| 1003 | |
| 1004 | /* Set CPU queue access map - all CPUs have access to all RX |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 1005 | * queues and to all TX queues |
| 1006 | */ |
Maxime Ripard | 2502d0e | 2015-09-25 18:09:35 +0200 | [diff] [blame] | 1007 | for_each_present_cpu(cpu) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1008 | mvreg_write(pp, MVNETA_CPU_MAP(cpu), |
| 1009 | (MVNETA_CPU_RXQ_ACCESS_ALL_MASK | |
| 1010 | MVNETA_CPU_TXQ_ACCESS_ALL_MASK)); |
| 1011 | |
| 1012 | /* Reset RX and TX DMAs */ |
| 1013 | mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET); |
| 1014 | mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET); |
| 1015 | |
| 1016 | /* Disable Legacy WRR, Disable EJP, Release from reset */ |
| 1017 | mvreg_write(pp, MVNETA_TXQ_CMD_1, 0); |
| 1018 | for (queue = 0; queue < txq_number; queue++) { |
| 1019 | mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0); |
| 1020 | mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0); |
| 1021 | } |
| 1022 | |
| 1023 | mvreg_write(pp, MVNETA_PORT_TX_RESET, 0); |
| 1024 | mvreg_write(pp, MVNETA_PORT_RX_RESET, 0); |
| 1025 | |
| 1026 | /* Set Port Acceleration Mode */ |
| 1027 | val = MVNETA_ACC_MODE_EXT; |
| 1028 | mvreg_write(pp, MVNETA_ACC_MODE, val); |
| 1029 | |
| 1030 | /* Update val of portCfg register accordingly with all RxQueue types */ |
| 1031 | val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def); |
| 1032 | mvreg_write(pp, MVNETA_PORT_CONFIG, val); |
| 1033 | |
| 1034 | val = 0; |
| 1035 | mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val); |
| 1036 | mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64); |
| 1037 | |
| 1038 | /* Build PORT_SDMA_CONFIG_REG */ |
| 1039 | val = 0; |
| 1040 | |
| 1041 | /* Default burst size */ |
| 1042 | val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16); |
| 1043 | val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16); |
Thomas Petazzoni | 9ad8fef | 2013-07-29 15:21:28 +0200 | [diff] [blame] | 1044 | val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1045 | |
Thomas Petazzoni | 9ad8fef | 2013-07-29 15:21:28 +0200 | [diff] [blame] | 1046 | #if defined(__BIG_ENDIAN) |
| 1047 | val |= MVNETA_DESC_SWAP; |
| 1048 | #endif |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1049 | |
| 1050 | /* Assign port SDMA configuration */ |
| 1051 | mvreg_write(pp, MVNETA_SDMA_CONFIG, val); |
| 1052 | |
Thomas Petazzoni | 7140860 | 2013-09-04 16:21:18 +0200 | [diff] [blame] | 1053 | /* Disable PHY polling in hardware, since we're using the |
| 1054 | * kernel phylib to do this. |
| 1055 | */ |
| 1056 | val = mvreg_read(pp, MVNETA_UNIT_CONTROL); |
| 1057 | val &= ~MVNETA_PHY_POLLING_ENABLE; |
| 1058 | mvreg_write(pp, MVNETA_UNIT_CONTROL, val); |
| 1059 | |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 1060 | if (pp->use_inband_status) { |
| 1061 | val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG); |
| 1062 | val &= ~(MVNETA_GMAC_FORCE_LINK_PASS | |
| 1063 | MVNETA_GMAC_FORCE_LINK_DOWN | |
| 1064 | MVNETA_GMAC_AN_FLOW_CTRL_EN); |
| 1065 | val |= MVNETA_GMAC_INBAND_AN_ENABLE | |
| 1066 | MVNETA_GMAC_AN_SPEED_EN | |
| 1067 | MVNETA_GMAC_AN_DUPLEX_EN; |
| 1068 | mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val); |
| 1069 | val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER); |
| 1070 | val |= MVNETA_GMAC_1MS_CLOCK_ENABLE; |
| 1071 | mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val); |
Stas Sergeev | 538761b | 2015-06-18 18:36:03 +0300 | [diff] [blame] | 1072 | } else { |
| 1073 | val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG); |
| 1074 | val &= ~(MVNETA_GMAC_INBAND_AN_ENABLE | |
| 1075 | MVNETA_GMAC_AN_SPEED_EN | |
| 1076 | MVNETA_GMAC_AN_DUPLEX_EN); |
| 1077 | mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val); |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 1078 | } |
| 1079 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1080 | mvneta_set_ucast_table(pp, -1); |
| 1081 | mvneta_set_special_mcast_table(pp, -1); |
| 1082 | mvneta_set_other_mcast_table(pp, -1); |
| 1083 | |
| 1084 | /* Set port interrupt enable register - default enable all */ |
| 1085 | mvreg_write(pp, MVNETA_INTR_ENABLE, |
| 1086 | (MVNETA_RXQ_INTR_ENABLE_ALL_MASK |
| 1087 | | MVNETA_TXQ_INTR_ENABLE_ALL_MASK)); |
Andrew Lunn | e483911 | 2015-10-22 18:37:36 +0100 | [diff] [blame] | 1088 | |
| 1089 | mvneta_mib_counters_clear(pp); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /* Set max sizes for tx queues */ |
| 1093 | static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size) |
| 1094 | |
| 1095 | { |
| 1096 | u32 val, size, mtu; |
| 1097 | int queue; |
| 1098 | |
| 1099 | mtu = max_tx_size * 8; |
| 1100 | if (mtu > MVNETA_TX_MTU_MAX) |
| 1101 | mtu = MVNETA_TX_MTU_MAX; |
| 1102 | |
| 1103 | /* Set MTU */ |
| 1104 | val = mvreg_read(pp, MVNETA_TX_MTU); |
| 1105 | val &= ~MVNETA_TX_MTU_MAX; |
| 1106 | val |= mtu; |
| 1107 | mvreg_write(pp, MVNETA_TX_MTU, val); |
| 1108 | |
| 1109 | /* TX token size and all TXQs token size must be larger that MTU */ |
| 1110 | val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE); |
| 1111 | |
| 1112 | size = val & MVNETA_TX_TOKEN_SIZE_MAX; |
| 1113 | if (size < mtu) { |
| 1114 | size = mtu; |
| 1115 | val &= ~MVNETA_TX_TOKEN_SIZE_MAX; |
| 1116 | val |= size; |
| 1117 | mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val); |
| 1118 | } |
| 1119 | for (queue = 0; queue < txq_number; queue++) { |
| 1120 | val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue)); |
| 1121 | |
| 1122 | size = val & MVNETA_TXQ_TOKEN_SIZE_MAX; |
| 1123 | if (size < mtu) { |
| 1124 | size = mtu; |
| 1125 | val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX; |
| 1126 | val |= size; |
| 1127 | mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val); |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | /* Set unicast address */ |
| 1133 | static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble, |
| 1134 | int queue) |
| 1135 | { |
| 1136 | unsigned int unicast_reg; |
| 1137 | unsigned int tbl_offset; |
| 1138 | unsigned int reg_offset; |
| 1139 | |
| 1140 | /* Locate the Unicast table entry */ |
| 1141 | last_nibble = (0xf & last_nibble); |
| 1142 | |
| 1143 | /* offset from unicast tbl base */ |
| 1144 | tbl_offset = (last_nibble / 4) * 4; |
| 1145 | |
| 1146 | /* offset within the above reg */ |
| 1147 | reg_offset = last_nibble % 4; |
| 1148 | |
| 1149 | unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset)); |
| 1150 | |
| 1151 | if (queue == -1) { |
| 1152 | /* Clear accepts frame bit at specified unicast DA tbl entry */ |
| 1153 | unicast_reg &= ~(0xff << (8 * reg_offset)); |
| 1154 | } else { |
| 1155 | unicast_reg &= ~(0xff << (8 * reg_offset)); |
| 1156 | unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset)); |
| 1157 | } |
| 1158 | |
| 1159 | mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg); |
| 1160 | } |
| 1161 | |
| 1162 | /* Set mac address */ |
| 1163 | static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr, |
| 1164 | int queue) |
| 1165 | { |
| 1166 | unsigned int mac_h; |
| 1167 | unsigned int mac_l; |
| 1168 | |
| 1169 | if (queue != -1) { |
| 1170 | mac_l = (addr[4] << 8) | (addr[5]); |
| 1171 | mac_h = (addr[0] << 24) | (addr[1] << 16) | |
| 1172 | (addr[2] << 8) | (addr[3] << 0); |
| 1173 | |
| 1174 | mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l); |
| 1175 | mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h); |
| 1176 | } |
| 1177 | |
| 1178 | /* Accept frames of this address */ |
| 1179 | mvneta_set_ucast_addr(pp, addr[5], queue); |
| 1180 | } |
| 1181 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 1182 | /* Set the number of packets that will be received before RX interrupt |
| 1183 | * will be generated by HW. |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1184 | */ |
| 1185 | static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp, |
| 1186 | struct mvneta_rx_queue *rxq, u32 value) |
| 1187 | { |
| 1188 | mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id), |
| 1189 | value | MVNETA_RXQ_NON_OCCUPIED(0)); |
| 1190 | rxq->pkts_coal = value; |
| 1191 | } |
| 1192 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 1193 | /* Set the time delay in usec before RX interrupt will be generated by |
| 1194 | * HW. |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1195 | */ |
| 1196 | static void mvneta_rx_time_coal_set(struct mvneta_port *pp, |
| 1197 | struct mvneta_rx_queue *rxq, u32 value) |
| 1198 | { |
Thomas Petazzoni | 189dd62 | 2012-11-19 14:15:25 +0100 | [diff] [blame] | 1199 | u32 val; |
| 1200 | unsigned long clk_rate; |
| 1201 | |
| 1202 | clk_rate = clk_get_rate(pp->clk); |
| 1203 | val = (clk_rate / 1000000) * value; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1204 | |
| 1205 | mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val); |
| 1206 | rxq->time_coal = value; |
| 1207 | } |
| 1208 | |
| 1209 | /* Set threshold for TX_DONE pkts coalescing */ |
| 1210 | static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp, |
| 1211 | struct mvneta_tx_queue *txq, u32 value) |
| 1212 | { |
| 1213 | u32 val; |
| 1214 | |
| 1215 | val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id)); |
| 1216 | |
| 1217 | val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK; |
| 1218 | val |= MVNETA_TXQ_SENT_THRESH_MASK(value); |
| 1219 | |
| 1220 | mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val); |
| 1221 | |
| 1222 | txq->done_pkts_coal = value; |
| 1223 | } |
| 1224 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1225 | /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */ |
| 1226 | static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc, |
| 1227 | u32 phys_addr, u32 cookie) |
| 1228 | { |
| 1229 | rx_desc->buf_cookie = cookie; |
| 1230 | rx_desc->buf_phys_addr = phys_addr; |
| 1231 | } |
| 1232 | |
| 1233 | /* Decrement sent descriptors counter */ |
| 1234 | static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp, |
| 1235 | struct mvneta_tx_queue *txq, |
| 1236 | int sent_desc) |
| 1237 | { |
| 1238 | u32 val; |
| 1239 | |
| 1240 | /* Only 255 TX descriptors can be updated at once */ |
| 1241 | while (sent_desc > 0xff) { |
| 1242 | val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT; |
| 1243 | mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val); |
| 1244 | sent_desc = sent_desc - 0xff; |
| 1245 | } |
| 1246 | |
| 1247 | val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT; |
| 1248 | mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val); |
| 1249 | } |
| 1250 | |
| 1251 | /* Get number of TX descriptors already sent by HW */ |
| 1252 | static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp, |
| 1253 | struct mvneta_tx_queue *txq) |
| 1254 | { |
| 1255 | u32 val; |
| 1256 | int sent_desc; |
| 1257 | |
| 1258 | val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id)); |
| 1259 | sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >> |
| 1260 | MVNETA_TXQ_SENT_DESC_SHIFT; |
| 1261 | |
| 1262 | return sent_desc; |
| 1263 | } |
| 1264 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 1265 | /* Get number of sent descriptors and decrement counter. |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1266 | * The number of sent descriptors is returned. |
| 1267 | */ |
| 1268 | static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp, |
| 1269 | struct mvneta_tx_queue *txq) |
| 1270 | { |
| 1271 | int sent_desc; |
| 1272 | |
| 1273 | /* Get number of sent descriptors */ |
| 1274 | sent_desc = mvneta_txq_sent_desc_num_get(pp, txq); |
| 1275 | |
| 1276 | /* Decrement sent descriptors counter */ |
| 1277 | if (sent_desc) |
| 1278 | mvneta_txq_sent_desc_dec(pp, txq, sent_desc); |
| 1279 | |
| 1280 | return sent_desc; |
| 1281 | } |
| 1282 | |
| 1283 | /* Set TXQ descriptors fields relevant for CSUM calculation */ |
| 1284 | static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto, |
| 1285 | int ip_hdr_len, int l4_proto) |
| 1286 | { |
| 1287 | u32 command; |
| 1288 | |
| 1289 | /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk, |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 1290 | * G_L4_chk, L4_type; required only for checksum |
| 1291 | * calculation |
| 1292 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1293 | command = l3_offs << MVNETA_TX_L3_OFF_SHIFT; |
| 1294 | command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT; |
| 1295 | |
Thomas Fitzsimmons | 0a19858 | 2014-07-08 19:44:07 -0400 | [diff] [blame] | 1296 | if (l3_proto == htons(ETH_P_IP)) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1297 | command |= MVNETA_TXD_IP_CSUM; |
| 1298 | else |
| 1299 | command |= MVNETA_TX_L3_IP6; |
| 1300 | |
| 1301 | if (l4_proto == IPPROTO_TCP) |
| 1302 | command |= MVNETA_TX_L4_CSUM_FULL; |
| 1303 | else if (l4_proto == IPPROTO_UDP) |
| 1304 | command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL; |
| 1305 | else |
| 1306 | command |= MVNETA_TX_L4_CSUM_NOT; |
| 1307 | |
| 1308 | return command; |
| 1309 | } |
| 1310 | |
| 1311 | |
| 1312 | /* Display more error info */ |
| 1313 | static void mvneta_rx_error(struct mvneta_port *pp, |
| 1314 | struct mvneta_rx_desc *rx_desc) |
| 1315 | { |
| 1316 | u32 status = rx_desc->status; |
| 1317 | |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 1318 | if (!mvneta_rxq_desc_is_first_last(status)) { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1319 | netdev_err(pp->dev, |
| 1320 | "bad rx status %08x (buffer oversize), size=%d\n", |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 1321 | status, rx_desc->data_size); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1322 | return; |
| 1323 | } |
| 1324 | |
| 1325 | switch (status & MVNETA_RXD_ERR_CODE_MASK) { |
| 1326 | case MVNETA_RXD_ERR_CRC: |
| 1327 | netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n", |
| 1328 | status, rx_desc->data_size); |
| 1329 | break; |
| 1330 | case MVNETA_RXD_ERR_OVERRUN: |
| 1331 | netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n", |
| 1332 | status, rx_desc->data_size); |
| 1333 | break; |
| 1334 | case MVNETA_RXD_ERR_LEN: |
| 1335 | netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n", |
| 1336 | status, rx_desc->data_size); |
| 1337 | break; |
| 1338 | case MVNETA_RXD_ERR_RESOURCE: |
| 1339 | netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n", |
| 1340 | status, rx_desc->data_size); |
| 1341 | break; |
| 1342 | } |
| 1343 | } |
| 1344 | |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 1345 | /* Handle RX checksum offload based on the descriptor's status */ |
| 1346 | static void mvneta_rx_csum(struct mvneta_port *pp, u32 status, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1347 | struct sk_buff *skb) |
| 1348 | { |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 1349 | if ((status & MVNETA_RXD_L3_IP4) && |
| 1350 | (status & MVNETA_RXD_L4_CSUM_OK)) { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1351 | skb->csum = 0; |
| 1352 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 1353 | return; |
| 1354 | } |
| 1355 | |
| 1356 | skb->ip_summed = CHECKSUM_NONE; |
| 1357 | } |
| 1358 | |
willy tarreau | 6c49897 | 2014-01-16 08:20:12 +0100 | [diff] [blame] | 1359 | /* Return tx queue pointer (find last set bit) according to <cause> returned |
| 1360 | * form tx_done reg. <cause> must not be null. The return value is always a |
| 1361 | * valid queue for matching the first one found in <cause>. |
| 1362 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1363 | static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp, |
| 1364 | u32 cause) |
| 1365 | { |
| 1366 | int queue = fls(cause) - 1; |
| 1367 | |
willy tarreau | 6c49897 | 2014-01-16 08:20:12 +0100 | [diff] [blame] | 1368 | return &pp->txqs[queue]; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | /* Free tx queue skbuffs */ |
| 1372 | static void mvneta_txq_bufs_free(struct mvneta_port *pp, |
| 1373 | struct mvneta_tx_queue *txq, int num) |
| 1374 | { |
| 1375 | int i; |
| 1376 | |
| 1377 | for (i = 0; i < num; i++) { |
| 1378 | struct mvneta_tx_desc *tx_desc = txq->descs + |
| 1379 | txq->txq_get_index; |
| 1380 | struct sk_buff *skb = txq->tx_skb[txq->txq_get_index]; |
| 1381 | |
| 1382 | mvneta_txq_inc_get(txq); |
| 1383 | |
Ezequiel Garcia | 2e3173a | 2014-05-30 13:40:07 -0300 | [diff] [blame] | 1384 | if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr)) |
| 1385 | dma_unmap_single(pp->dev->dev.parent, |
| 1386 | tx_desc->buf_phys_addr, |
| 1387 | tx_desc->data_size, DMA_TO_DEVICE); |
Ezequiel Garcia | ba7e46e | 2014-05-30 13:40:06 -0300 | [diff] [blame] | 1388 | if (!skb) |
| 1389 | continue; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1390 | dev_kfree_skb_any(skb); |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | /* Handle end of transmission */ |
Arnaud Ebalard | cd71319 | 2014-01-16 08:20:19 +0100 | [diff] [blame] | 1395 | static void mvneta_txq_done(struct mvneta_port *pp, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1396 | struct mvneta_tx_queue *txq) |
| 1397 | { |
| 1398 | struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id); |
| 1399 | int tx_done; |
| 1400 | |
| 1401 | tx_done = mvneta_txq_sent_desc_proc(pp, txq); |
Arnaud Ebalard | cd71319 | 2014-01-16 08:20:19 +0100 | [diff] [blame] | 1402 | if (!tx_done) |
| 1403 | return; |
| 1404 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1405 | mvneta_txq_bufs_free(pp, txq, tx_done); |
| 1406 | |
| 1407 | txq->count -= tx_done; |
| 1408 | |
| 1409 | if (netif_tx_queue_stopped(nq)) { |
Ezequiel Garcia | 8eef5f9 | 2014-05-30 13:40:05 -0300 | [diff] [blame] | 1410 | if (txq->count <= txq->tx_wake_threshold) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1411 | netif_tx_wake_queue(nq); |
| 1412 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1413 | } |
| 1414 | |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1415 | static void *mvneta_frag_alloc(const struct mvneta_port *pp) |
| 1416 | { |
| 1417 | if (likely(pp->frag_size <= PAGE_SIZE)) |
| 1418 | return netdev_alloc_frag(pp->frag_size); |
| 1419 | else |
| 1420 | return kmalloc(pp->frag_size, GFP_ATOMIC); |
| 1421 | } |
| 1422 | |
| 1423 | static void mvneta_frag_free(const struct mvneta_port *pp, void *data) |
| 1424 | { |
| 1425 | if (likely(pp->frag_size <= PAGE_SIZE)) |
Alexander Duyck | 13dc0d2 | 2015-05-06 21:12:14 -0700 | [diff] [blame] | 1426 | skb_free_frag(data); |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1427 | else |
| 1428 | kfree(data); |
| 1429 | } |
| 1430 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1431 | /* Refill processing */ |
| 1432 | static int mvneta_rx_refill(struct mvneta_port *pp, |
| 1433 | struct mvneta_rx_desc *rx_desc) |
| 1434 | |
| 1435 | { |
| 1436 | dma_addr_t phys_addr; |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1437 | void *data; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1438 | |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1439 | data = mvneta_frag_alloc(pp); |
| 1440 | if (!data) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1441 | return -ENOMEM; |
| 1442 | |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1443 | phys_addr = dma_map_single(pp->dev->dev.parent, data, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1444 | MVNETA_RX_BUF_SIZE(pp->pkt_size), |
| 1445 | DMA_FROM_DEVICE); |
| 1446 | if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) { |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1447 | mvneta_frag_free(pp, data); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1448 | return -ENOMEM; |
| 1449 | } |
| 1450 | |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1451 | mvneta_rx_desc_fill(rx_desc, phys_addr, (u32)data); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1452 | return 0; |
| 1453 | } |
| 1454 | |
| 1455 | /* Handle tx checksum */ |
| 1456 | static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb) |
| 1457 | { |
| 1458 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 1459 | int ip_hdr_len = 0; |
Vlad Yasevich | 817dbfa | 2014-08-25 10:34:54 -0400 | [diff] [blame] | 1460 | __be16 l3_proto = vlan_get_protocol(skb); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1461 | u8 l4_proto; |
| 1462 | |
Vlad Yasevich | 817dbfa | 2014-08-25 10:34:54 -0400 | [diff] [blame] | 1463 | if (l3_proto == htons(ETH_P_IP)) { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1464 | struct iphdr *ip4h = ip_hdr(skb); |
| 1465 | |
| 1466 | /* Calculate IPv4 checksum and L4 checksum */ |
| 1467 | ip_hdr_len = ip4h->ihl; |
| 1468 | l4_proto = ip4h->protocol; |
Vlad Yasevich | 817dbfa | 2014-08-25 10:34:54 -0400 | [diff] [blame] | 1469 | } else if (l3_proto == htons(ETH_P_IPV6)) { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1470 | struct ipv6hdr *ip6h = ipv6_hdr(skb); |
| 1471 | |
| 1472 | /* Read l4_protocol from one of IPv6 extra headers */ |
| 1473 | if (skb_network_header_len(skb) > 0) |
| 1474 | ip_hdr_len = (skb_network_header_len(skb) >> 2); |
| 1475 | l4_proto = ip6h->nexthdr; |
| 1476 | } else |
| 1477 | return MVNETA_TX_L4_CSUM_NOT; |
| 1478 | |
| 1479 | return mvneta_txq_desc_csum(skb_network_offset(skb), |
Vlad Yasevich | 817dbfa | 2014-08-25 10:34:54 -0400 | [diff] [blame] | 1480 | l3_proto, ip_hdr_len, l4_proto); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | return MVNETA_TX_L4_CSUM_NOT; |
| 1484 | } |
| 1485 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1486 | /* Drop packets received by the RXQ and free buffers */ |
| 1487 | static void mvneta_rxq_drop_pkts(struct mvneta_port *pp, |
| 1488 | struct mvneta_rx_queue *rxq) |
| 1489 | { |
| 1490 | int rx_done, i; |
| 1491 | |
| 1492 | rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq); |
| 1493 | for (i = 0; i < rxq->size; i++) { |
| 1494 | struct mvneta_rx_desc *rx_desc = rxq->descs + i; |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1495 | void *data = (void *)rx_desc->buf_cookie; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1496 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1497 | dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr, |
Ezequiel Garcia | a328f3a | 2013-12-05 13:35:37 -0300 | [diff] [blame] | 1498 | MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE); |
Justin Maggard | 8c94ddb | 2015-11-09 17:21:05 -0800 | [diff] [blame] | 1499 | mvneta_frag_free(pp, data); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | if (rx_done) |
| 1503 | mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done); |
| 1504 | } |
| 1505 | |
| 1506 | /* Main rx processing */ |
| 1507 | static int mvneta_rx(struct mvneta_port *pp, int rx_todo, |
| 1508 | struct mvneta_rx_queue *rxq) |
| 1509 | { |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 1510 | struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1511 | struct net_device *dev = pp->dev; |
Simon Guinot | a84e328 | 2015-07-19 13:00:53 +0200 | [diff] [blame] | 1512 | int rx_done; |
willy tarreau | dc4277d | 2014-01-16 08:20:07 +0100 | [diff] [blame] | 1513 | u32 rcvd_pkts = 0; |
| 1514 | u32 rcvd_bytes = 0; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1515 | |
| 1516 | /* Get number of received packets */ |
| 1517 | rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq); |
| 1518 | |
| 1519 | if (rx_todo > rx_done) |
| 1520 | rx_todo = rx_done; |
| 1521 | |
| 1522 | rx_done = 0; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1523 | |
| 1524 | /* Fairness NAPI loop */ |
| 1525 | while (rx_done < rx_todo) { |
| 1526 | struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq); |
| 1527 | struct sk_buff *skb; |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1528 | unsigned char *data; |
Simon Guinot | daf158d | 2015-09-15 22:41:21 +0200 | [diff] [blame] | 1529 | dma_addr_t phys_addr; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1530 | u32 rx_status; |
| 1531 | int rx_bytes, err; |
| 1532 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1533 | rx_done++; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1534 | rx_status = rx_desc->status; |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 1535 | rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE); |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1536 | data = (unsigned char *)rx_desc->buf_cookie; |
Simon Guinot | daf158d | 2015-09-15 22:41:21 +0200 | [diff] [blame] | 1537 | phys_addr = rx_desc->buf_phys_addr; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1538 | |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 1539 | if (!mvneta_rxq_desc_is_first_last(rx_status) || |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 1540 | (rx_status & MVNETA_RXD_ERR_SUMMARY)) { |
| 1541 | err_drop_frame: |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1542 | dev->stats.rx_errors++; |
| 1543 | mvneta_rx_error(pp, rx_desc); |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1544 | /* leave the descriptor untouched */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1545 | continue; |
| 1546 | } |
| 1547 | |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 1548 | if (rx_bytes <= rx_copybreak) { |
| 1549 | /* better copy a small frame and not unmap the DMA region */ |
| 1550 | skb = netdev_alloc_skb_ip_align(dev, rx_bytes); |
| 1551 | if (unlikely(!skb)) |
| 1552 | goto err_drop_frame; |
| 1553 | |
| 1554 | dma_sync_single_range_for_cpu(dev->dev.parent, |
| 1555 | rx_desc->buf_phys_addr, |
| 1556 | MVNETA_MH_SIZE + NET_SKB_PAD, |
| 1557 | rx_bytes, |
| 1558 | DMA_FROM_DEVICE); |
| 1559 | memcpy(skb_put(skb, rx_bytes), |
| 1560 | data + MVNETA_MH_SIZE + NET_SKB_PAD, |
| 1561 | rx_bytes); |
| 1562 | |
| 1563 | skb->protocol = eth_type_trans(skb, dev); |
| 1564 | mvneta_rx_csum(pp, rx_status, skb); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 1565 | napi_gro_receive(&port->napi, skb); |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 1566 | |
| 1567 | rcvd_pkts++; |
| 1568 | rcvd_bytes += rx_bytes; |
| 1569 | |
| 1570 | /* leave the descriptor and buffer untouched */ |
| 1571 | continue; |
| 1572 | } |
| 1573 | |
Simon Guinot | a84e328 | 2015-07-19 13:00:53 +0200 | [diff] [blame] | 1574 | /* Refill processing */ |
| 1575 | err = mvneta_rx_refill(pp, rx_desc); |
| 1576 | if (err) { |
| 1577 | netdev_err(dev, "Linux processing - Can't refill\n"); |
| 1578 | rxq->missed++; |
| 1579 | goto err_drop_frame; |
| 1580 | } |
| 1581 | |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 1582 | skb = build_skb(data, pp->frag_size > PAGE_SIZE ? 0 : pp->frag_size); |
| 1583 | if (!skb) |
| 1584 | goto err_drop_frame; |
| 1585 | |
Simon Guinot | daf158d | 2015-09-15 22:41:21 +0200 | [diff] [blame] | 1586 | dma_unmap_single(dev->dev.parent, phys_addr, |
Ezequiel Garcia | a328f3a | 2013-12-05 13:35:37 -0300 | [diff] [blame] | 1587 | MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1588 | |
willy tarreau | dc4277d | 2014-01-16 08:20:07 +0100 | [diff] [blame] | 1589 | rcvd_pkts++; |
| 1590 | rcvd_bytes += rx_bytes; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1591 | |
| 1592 | /* Linux processing */ |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1593 | skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1594 | skb_put(skb, rx_bytes); |
| 1595 | |
| 1596 | skb->protocol = eth_type_trans(skb, dev); |
| 1597 | |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 1598 | mvneta_rx_csum(pp, rx_status, skb); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1599 | |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 1600 | napi_gro_receive(&port->napi, skb); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1601 | } |
| 1602 | |
willy tarreau | dc4277d | 2014-01-16 08:20:07 +0100 | [diff] [blame] | 1603 | if (rcvd_pkts) { |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 1604 | struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats); |
| 1605 | |
| 1606 | u64_stats_update_begin(&stats->syncp); |
| 1607 | stats->rx_packets += rcvd_pkts; |
| 1608 | stats->rx_bytes += rcvd_bytes; |
| 1609 | u64_stats_update_end(&stats->syncp); |
willy tarreau | dc4277d | 2014-01-16 08:20:07 +0100 | [diff] [blame] | 1610 | } |
| 1611 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1612 | /* Update rxq management counters */ |
Simon Guinot | a84e328 | 2015-07-19 13:00:53 +0200 | [diff] [blame] | 1613 | mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1614 | |
| 1615 | return rx_done; |
| 1616 | } |
| 1617 | |
Ezequiel Garcia | 2adb719 | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 1618 | static inline void |
| 1619 | mvneta_tso_put_hdr(struct sk_buff *skb, |
| 1620 | struct mvneta_port *pp, struct mvneta_tx_queue *txq) |
| 1621 | { |
| 1622 | struct mvneta_tx_desc *tx_desc; |
| 1623 | int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); |
| 1624 | |
| 1625 | txq->tx_skb[txq->txq_put_index] = NULL; |
| 1626 | tx_desc = mvneta_txq_next_desc_get(txq); |
| 1627 | tx_desc->data_size = hdr_len; |
| 1628 | tx_desc->command = mvneta_skb_tx_csum(pp, skb); |
| 1629 | tx_desc->command |= MVNETA_TXD_F_DESC; |
| 1630 | tx_desc->buf_phys_addr = txq->tso_hdrs_phys + |
| 1631 | txq->txq_put_index * TSO_HEADER_SIZE; |
| 1632 | mvneta_txq_inc_put(txq); |
| 1633 | } |
| 1634 | |
| 1635 | static inline int |
| 1636 | mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq, |
| 1637 | struct sk_buff *skb, char *data, int size, |
| 1638 | bool last_tcp, bool is_last) |
| 1639 | { |
| 1640 | struct mvneta_tx_desc *tx_desc; |
| 1641 | |
| 1642 | tx_desc = mvneta_txq_next_desc_get(txq); |
| 1643 | tx_desc->data_size = size; |
| 1644 | tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data, |
| 1645 | size, DMA_TO_DEVICE); |
| 1646 | if (unlikely(dma_mapping_error(dev->dev.parent, |
| 1647 | tx_desc->buf_phys_addr))) { |
| 1648 | mvneta_txq_desc_put(txq); |
| 1649 | return -ENOMEM; |
| 1650 | } |
| 1651 | |
| 1652 | tx_desc->command = 0; |
| 1653 | txq->tx_skb[txq->txq_put_index] = NULL; |
| 1654 | |
| 1655 | if (last_tcp) { |
| 1656 | /* last descriptor in the TCP packet */ |
| 1657 | tx_desc->command = MVNETA_TXD_L_DESC; |
| 1658 | |
| 1659 | /* last descriptor in SKB */ |
| 1660 | if (is_last) |
| 1661 | txq->tx_skb[txq->txq_put_index] = skb; |
| 1662 | } |
| 1663 | mvneta_txq_inc_put(txq); |
| 1664 | return 0; |
| 1665 | } |
| 1666 | |
| 1667 | static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev, |
| 1668 | struct mvneta_tx_queue *txq) |
| 1669 | { |
| 1670 | int total_len, data_left; |
| 1671 | int desc_count = 0; |
| 1672 | struct mvneta_port *pp = netdev_priv(dev); |
| 1673 | struct tso_t tso; |
| 1674 | int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); |
| 1675 | int i; |
| 1676 | |
| 1677 | /* Count needed descriptors */ |
| 1678 | if ((txq->count + tso_count_descs(skb)) >= txq->size) |
| 1679 | return 0; |
| 1680 | |
| 1681 | if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) { |
| 1682 | pr_info("*** Is this even possible???!?!?\n"); |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
| 1686 | /* Initialize the TSO handler, and prepare the first payload */ |
| 1687 | tso_start(skb, &tso); |
| 1688 | |
| 1689 | total_len = skb->len - hdr_len; |
| 1690 | while (total_len > 0) { |
| 1691 | char *hdr; |
| 1692 | |
| 1693 | data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len); |
| 1694 | total_len -= data_left; |
| 1695 | desc_count++; |
| 1696 | |
| 1697 | /* prepare packet headers: MAC + IP + TCP */ |
| 1698 | hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE; |
| 1699 | tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0); |
| 1700 | |
| 1701 | mvneta_tso_put_hdr(skb, pp, txq); |
| 1702 | |
| 1703 | while (data_left > 0) { |
| 1704 | int size; |
| 1705 | desc_count++; |
| 1706 | |
| 1707 | size = min_t(int, tso.size, data_left); |
| 1708 | |
| 1709 | if (mvneta_tso_put_data(dev, txq, skb, |
| 1710 | tso.data, size, |
| 1711 | size == data_left, |
| 1712 | total_len == 0)) |
| 1713 | goto err_release; |
| 1714 | data_left -= size; |
| 1715 | |
| 1716 | tso_build_data(skb, &tso, size); |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | return desc_count; |
| 1721 | |
| 1722 | err_release: |
| 1723 | /* Release all used data descriptors; header descriptors must not |
| 1724 | * be DMA-unmapped. |
| 1725 | */ |
| 1726 | for (i = desc_count - 1; i >= 0; i--) { |
| 1727 | struct mvneta_tx_desc *tx_desc = txq->descs + i; |
Ezequiel Garcia | 2e3173a | 2014-05-30 13:40:07 -0300 | [diff] [blame] | 1728 | if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr)) |
Ezequiel Garcia | 2adb719 | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 1729 | dma_unmap_single(pp->dev->dev.parent, |
| 1730 | tx_desc->buf_phys_addr, |
| 1731 | tx_desc->data_size, |
| 1732 | DMA_TO_DEVICE); |
| 1733 | mvneta_txq_desc_put(txq); |
| 1734 | } |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1738 | /* Handle tx fragmentation processing */ |
| 1739 | static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb, |
| 1740 | struct mvneta_tx_queue *txq) |
| 1741 | { |
| 1742 | struct mvneta_tx_desc *tx_desc; |
Ezequiel Garcia | 3d4ea02 | 2014-05-22 20:06:57 -0300 | [diff] [blame] | 1743 | int i, nr_frags = skb_shinfo(skb)->nr_frags; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1744 | |
Ezequiel Garcia | 3d4ea02 | 2014-05-22 20:06:57 -0300 | [diff] [blame] | 1745 | for (i = 0; i < nr_frags; i++) { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1746 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 1747 | void *addr = page_address(frag->page.p) + frag->page_offset; |
| 1748 | |
| 1749 | tx_desc = mvneta_txq_next_desc_get(txq); |
| 1750 | tx_desc->data_size = frag->size; |
| 1751 | |
| 1752 | tx_desc->buf_phys_addr = |
| 1753 | dma_map_single(pp->dev->dev.parent, addr, |
| 1754 | tx_desc->data_size, DMA_TO_DEVICE); |
| 1755 | |
| 1756 | if (dma_mapping_error(pp->dev->dev.parent, |
| 1757 | tx_desc->buf_phys_addr)) { |
| 1758 | mvneta_txq_desc_put(txq); |
| 1759 | goto error; |
| 1760 | } |
| 1761 | |
Ezequiel Garcia | 3d4ea02 | 2014-05-22 20:06:57 -0300 | [diff] [blame] | 1762 | if (i == nr_frags - 1) { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1763 | /* Last descriptor */ |
| 1764 | tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1765 | txq->tx_skb[txq->txq_put_index] = skb; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1766 | } else { |
| 1767 | /* Descriptor in the middle: Not First, Not Last */ |
| 1768 | tx_desc->command = 0; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1769 | txq->tx_skb[txq->txq_put_index] = NULL; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1770 | } |
Ezequiel Garcia | 3d4ea02 | 2014-05-22 20:06:57 -0300 | [diff] [blame] | 1771 | mvneta_txq_inc_put(txq); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | return 0; |
| 1775 | |
| 1776 | error: |
| 1777 | /* Release all descriptors that were used to map fragments of |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 1778 | * this packet, as well as the corresponding DMA mappings |
| 1779 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1780 | for (i = i - 1; i >= 0; i--) { |
| 1781 | tx_desc = txq->descs + i; |
| 1782 | dma_unmap_single(pp->dev->dev.parent, |
| 1783 | tx_desc->buf_phys_addr, |
| 1784 | tx_desc->data_size, |
| 1785 | DMA_TO_DEVICE); |
| 1786 | mvneta_txq_desc_put(txq); |
| 1787 | } |
| 1788 | |
| 1789 | return -ENOMEM; |
| 1790 | } |
| 1791 | |
| 1792 | /* Main tx processing */ |
| 1793 | static int mvneta_tx(struct sk_buff *skb, struct net_device *dev) |
| 1794 | { |
| 1795 | struct mvneta_port *pp = netdev_priv(dev); |
Willy Tarreau | ee40a11 | 2013-04-11 23:00:37 +0200 | [diff] [blame] | 1796 | u16 txq_id = skb_get_queue_mapping(skb); |
| 1797 | struct mvneta_tx_queue *txq = &pp->txqs[txq_id]; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1798 | struct mvneta_tx_desc *tx_desc; |
Eric Dumazet | 5f478b4 | 2014-12-02 04:30:59 -0800 | [diff] [blame] | 1799 | int len = skb->len; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1800 | int frags = 0; |
| 1801 | u32 tx_cmd; |
| 1802 | |
| 1803 | if (!netif_running(dev)) |
| 1804 | goto out; |
| 1805 | |
Ezequiel Garcia | 2adb719 | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 1806 | if (skb_is_gso(skb)) { |
| 1807 | frags = mvneta_tx_tso(skb, dev, txq); |
| 1808 | goto out; |
| 1809 | } |
| 1810 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1811 | frags = skb_shinfo(skb)->nr_frags + 1; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1812 | |
| 1813 | /* Get a descriptor for the first part of the packet */ |
| 1814 | tx_desc = mvneta_txq_next_desc_get(txq); |
| 1815 | |
| 1816 | tx_cmd = mvneta_skb_tx_csum(pp, skb); |
| 1817 | |
| 1818 | tx_desc->data_size = skb_headlen(skb); |
| 1819 | |
| 1820 | tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data, |
| 1821 | tx_desc->data_size, |
| 1822 | DMA_TO_DEVICE); |
| 1823 | if (unlikely(dma_mapping_error(dev->dev.parent, |
| 1824 | tx_desc->buf_phys_addr))) { |
| 1825 | mvneta_txq_desc_put(txq); |
| 1826 | frags = 0; |
| 1827 | goto out; |
| 1828 | } |
| 1829 | |
| 1830 | if (frags == 1) { |
| 1831 | /* First and Last descriptor */ |
| 1832 | tx_cmd |= MVNETA_TXD_FLZ_DESC; |
| 1833 | tx_desc->command = tx_cmd; |
| 1834 | txq->tx_skb[txq->txq_put_index] = skb; |
| 1835 | mvneta_txq_inc_put(txq); |
| 1836 | } else { |
| 1837 | /* First but not Last */ |
| 1838 | tx_cmd |= MVNETA_TXD_F_DESC; |
| 1839 | txq->tx_skb[txq->txq_put_index] = NULL; |
| 1840 | mvneta_txq_inc_put(txq); |
| 1841 | tx_desc->command = tx_cmd; |
| 1842 | /* Continue with other skb fragments */ |
| 1843 | if (mvneta_tx_frag_process(pp, skb, txq)) { |
| 1844 | dma_unmap_single(dev->dev.parent, |
| 1845 | tx_desc->buf_phys_addr, |
| 1846 | tx_desc->data_size, |
| 1847 | DMA_TO_DEVICE); |
| 1848 | mvneta_txq_desc_put(txq); |
| 1849 | frags = 0; |
| 1850 | goto out; |
| 1851 | } |
| 1852 | } |
| 1853 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1854 | out: |
| 1855 | if (frags > 0) { |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 1856 | struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats); |
Ezequiel Garcia | e19d2dd | 2014-05-19 13:59:54 -0300 | [diff] [blame] | 1857 | struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id); |
| 1858 | |
| 1859 | txq->count += frags; |
| 1860 | mvneta_txq_pend_desc_add(pp, txq, frags); |
| 1861 | |
Ezequiel Garcia | 8eef5f9 | 2014-05-30 13:40:05 -0300 | [diff] [blame] | 1862 | if (txq->count >= txq->tx_stop_threshold) |
Ezequiel Garcia | e19d2dd | 2014-05-19 13:59:54 -0300 | [diff] [blame] | 1863 | netif_tx_stop_queue(nq); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1864 | |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 1865 | u64_stats_update_begin(&stats->syncp); |
| 1866 | stats->tx_packets++; |
Eric Dumazet | 5f478b4 | 2014-12-02 04:30:59 -0800 | [diff] [blame] | 1867 | stats->tx_bytes += len; |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 1868 | u64_stats_update_end(&stats->syncp); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1869 | } else { |
| 1870 | dev->stats.tx_dropped++; |
| 1871 | dev_kfree_skb_any(skb); |
| 1872 | } |
| 1873 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1874 | return NETDEV_TX_OK; |
| 1875 | } |
| 1876 | |
| 1877 | |
| 1878 | /* Free tx resources, when resetting a port */ |
| 1879 | static void mvneta_txq_done_force(struct mvneta_port *pp, |
| 1880 | struct mvneta_tx_queue *txq) |
| 1881 | |
| 1882 | { |
| 1883 | int tx_done = txq->count; |
| 1884 | |
| 1885 | mvneta_txq_bufs_free(pp, txq, tx_done); |
| 1886 | |
| 1887 | /* reset txq */ |
| 1888 | txq->count = 0; |
| 1889 | txq->txq_put_index = 0; |
| 1890 | txq->txq_get_index = 0; |
| 1891 | } |
| 1892 | |
willy tarreau | 6c49897 | 2014-01-16 08:20:12 +0100 | [diff] [blame] | 1893 | /* Handle tx done - called in softirq context. The <cause_tx_done> argument |
| 1894 | * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL. |
| 1895 | */ |
Arnaud Ebalard | 0713a86 | 2014-01-16 08:20:18 +0100 | [diff] [blame] | 1896 | static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1897 | { |
| 1898 | struct mvneta_tx_queue *txq; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1899 | struct netdev_queue *nq; |
| 1900 | |
willy tarreau | 6c49897 | 2014-01-16 08:20:12 +0100 | [diff] [blame] | 1901 | while (cause_tx_done) { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1902 | txq = mvneta_tx_done_policy(pp, cause_tx_done); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1903 | |
| 1904 | nq = netdev_get_tx_queue(pp->dev, txq->id); |
| 1905 | __netif_tx_lock(nq, smp_processor_id()); |
| 1906 | |
Arnaud Ebalard | 0713a86 | 2014-01-16 08:20:18 +0100 | [diff] [blame] | 1907 | if (txq->count) |
| 1908 | mvneta_txq_done(pp, txq); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1909 | |
| 1910 | __netif_tx_unlock(nq); |
| 1911 | cause_tx_done &= ~((1 << txq->id)); |
| 1912 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1913 | } |
| 1914 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 1915 | /* Compute crc8 of the specified address, using a unique algorithm , |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1916 | * according to hw spec, different than generic crc8 algorithm |
| 1917 | */ |
| 1918 | static int mvneta_addr_crc(unsigned char *addr) |
| 1919 | { |
| 1920 | int crc = 0; |
| 1921 | int i; |
| 1922 | |
| 1923 | for (i = 0; i < ETH_ALEN; i++) { |
| 1924 | int j; |
| 1925 | |
| 1926 | crc = (crc ^ addr[i]) << 8; |
| 1927 | for (j = 7; j >= 0; j--) { |
| 1928 | if (crc & (0x100 << j)) |
| 1929 | crc ^= 0x107 << j; |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | return crc; |
| 1934 | } |
| 1935 | |
| 1936 | /* This method controls the net device special MAC multicast support. |
| 1937 | * The Special Multicast Table for MAC addresses supports MAC of the form |
| 1938 | * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF). |
| 1939 | * The MAC DA[7:0] bits are used as a pointer to the Special Multicast |
| 1940 | * Table entries in the DA-Filter table. This method set the Special |
| 1941 | * Multicast Table appropriate entry. |
| 1942 | */ |
| 1943 | static void mvneta_set_special_mcast_addr(struct mvneta_port *pp, |
| 1944 | unsigned char last_byte, |
| 1945 | int queue) |
| 1946 | { |
| 1947 | unsigned int smc_table_reg; |
| 1948 | unsigned int tbl_offset; |
| 1949 | unsigned int reg_offset; |
| 1950 | |
| 1951 | /* Register offset from SMC table base */ |
| 1952 | tbl_offset = (last_byte / 4); |
| 1953 | /* Entry offset within the above reg */ |
| 1954 | reg_offset = last_byte % 4; |
| 1955 | |
| 1956 | smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST |
| 1957 | + tbl_offset * 4)); |
| 1958 | |
| 1959 | if (queue == -1) |
| 1960 | smc_table_reg &= ~(0xff << (8 * reg_offset)); |
| 1961 | else { |
| 1962 | smc_table_reg &= ~(0xff << (8 * reg_offset)); |
| 1963 | smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset)); |
| 1964 | } |
| 1965 | |
| 1966 | mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4, |
| 1967 | smc_table_reg); |
| 1968 | } |
| 1969 | |
| 1970 | /* This method controls the network device Other MAC multicast support. |
| 1971 | * The Other Multicast Table is used for multicast of another type. |
| 1972 | * A CRC-8 is used as an index to the Other Multicast Table entries |
| 1973 | * in the DA-Filter table. |
| 1974 | * The method gets the CRC-8 value from the calling routine and |
| 1975 | * sets the Other Multicast Table appropriate entry according to the |
| 1976 | * specified CRC-8 . |
| 1977 | */ |
| 1978 | static void mvneta_set_other_mcast_addr(struct mvneta_port *pp, |
| 1979 | unsigned char crc8, |
| 1980 | int queue) |
| 1981 | { |
| 1982 | unsigned int omc_table_reg; |
| 1983 | unsigned int tbl_offset; |
| 1984 | unsigned int reg_offset; |
| 1985 | |
| 1986 | tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */ |
| 1987 | reg_offset = crc8 % 4; /* Entry offset within the above reg */ |
| 1988 | |
| 1989 | omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset); |
| 1990 | |
| 1991 | if (queue == -1) { |
| 1992 | /* Clear accepts frame bit at specified Other DA table entry */ |
| 1993 | omc_table_reg &= ~(0xff << (8 * reg_offset)); |
| 1994 | } else { |
| 1995 | omc_table_reg &= ~(0xff << (8 * reg_offset)); |
| 1996 | omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset)); |
| 1997 | } |
| 1998 | |
| 1999 | mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg); |
| 2000 | } |
| 2001 | |
| 2002 | /* The network device supports multicast using two tables: |
| 2003 | * 1) Special Multicast Table for MAC addresses of the form |
| 2004 | * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF). |
| 2005 | * The MAC DA[7:0] bits are used as a pointer to the Special Multicast |
| 2006 | * Table entries in the DA-Filter table. |
| 2007 | * 2) Other Multicast Table for multicast of another type. A CRC-8 value |
| 2008 | * is used as an index to the Other Multicast Table entries in the |
| 2009 | * DA-Filter table. |
| 2010 | */ |
| 2011 | static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr, |
| 2012 | int queue) |
| 2013 | { |
| 2014 | unsigned char crc_result = 0; |
| 2015 | |
| 2016 | if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) { |
| 2017 | mvneta_set_special_mcast_addr(pp, p_addr[5], queue); |
| 2018 | return 0; |
| 2019 | } |
| 2020 | |
| 2021 | crc_result = mvneta_addr_crc(p_addr); |
| 2022 | if (queue == -1) { |
| 2023 | if (pp->mcast_count[crc_result] == 0) { |
| 2024 | netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n", |
| 2025 | crc_result); |
| 2026 | return -EINVAL; |
| 2027 | } |
| 2028 | |
| 2029 | pp->mcast_count[crc_result]--; |
| 2030 | if (pp->mcast_count[crc_result] != 0) { |
| 2031 | netdev_info(pp->dev, |
| 2032 | "After delete there are %d valid Mcast for crc8=0x%02x\n", |
| 2033 | pp->mcast_count[crc_result], crc_result); |
| 2034 | return -EINVAL; |
| 2035 | } |
| 2036 | } else |
| 2037 | pp->mcast_count[crc_result]++; |
| 2038 | |
| 2039 | mvneta_set_other_mcast_addr(pp, crc_result, queue); |
| 2040 | |
| 2041 | return 0; |
| 2042 | } |
| 2043 | |
| 2044 | /* Configure Fitering mode of Ethernet port */ |
| 2045 | static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp, |
| 2046 | int is_promisc) |
| 2047 | { |
| 2048 | u32 port_cfg_reg, val; |
| 2049 | |
| 2050 | port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG); |
| 2051 | |
| 2052 | val = mvreg_read(pp, MVNETA_TYPE_PRIO); |
| 2053 | |
| 2054 | /* Set / Clear UPM bit in port configuration register */ |
| 2055 | if (is_promisc) { |
| 2056 | /* Accept all Unicast addresses */ |
| 2057 | port_cfg_reg |= MVNETA_UNI_PROMISC_MODE; |
| 2058 | val |= MVNETA_FORCE_UNI; |
| 2059 | mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff); |
| 2060 | mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff); |
| 2061 | } else { |
| 2062 | /* Reject all Unicast addresses */ |
| 2063 | port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE; |
| 2064 | val &= ~MVNETA_FORCE_UNI; |
| 2065 | } |
| 2066 | |
| 2067 | mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg); |
| 2068 | mvreg_write(pp, MVNETA_TYPE_PRIO, val); |
| 2069 | } |
| 2070 | |
| 2071 | /* register unicast and multicast addresses */ |
| 2072 | static void mvneta_set_rx_mode(struct net_device *dev) |
| 2073 | { |
| 2074 | struct mvneta_port *pp = netdev_priv(dev); |
| 2075 | struct netdev_hw_addr *ha; |
| 2076 | |
| 2077 | if (dev->flags & IFF_PROMISC) { |
| 2078 | /* Accept all: Multicast + Unicast */ |
| 2079 | mvneta_rx_unicast_promisc_set(pp, 1); |
| 2080 | mvneta_set_ucast_table(pp, rxq_def); |
| 2081 | mvneta_set_special_mcast_table(pp, rxq_def); |
| 2082 | mvneta_set_other_mcast_table(pp, rxq_def); |
| 2083 | } else { |
| 2084 | /* Accept single Unicast */ |
| 2085 | mvneta_rx_unicast_promisc_set(pp, 0); |
| 2086 | mvneta_set_ucast_table(pp, -1); |
| 2087 | mvneta_mac_addr_set(pp, dev->dev_addr, rxq_def); |
| 2088 | |
| 2089 | if (dev->flags & IFF_ALLMULTI) { |
| 2090 | /* Accept all multicast */ |
| 2091 | mvneta_set_special_mcast_table(pp, rxq_def); |
| 2092 | mvneta_set_other_mcast_table(pp, rxq_def); |
| 2093 | } else { |
| 2094 | /* Accept only initialized multicast */ |
| 2095 | mvneta_set_special_mcast_table(pp, -1); |
| 2096 | mvneta_set_other_mcast_table(pp, -1); |
| 2097 | |
| 2098 | if (!netdev_mc_empty(dev)) { |
| 2099 | netdev_for_each_mc_addr(ha, dev) { |
| 2100 | mvneta_mcast_addr_set(pp, ha->addr, |
| 2101 | rxq_def); |
| 2102 | } |
| 2103 | } |
| 2104 | } |
| 2105 | } |
| 2106 | } |
| 2107 | |
| 2108 | /* Interrupt handling - the callback for request_irq() */ |
| 2109 | static irqreturn_t mvneta_isr(int irq, void *dev_id) |
| 2110 | { |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2111 | struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2112 | |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2113 | disable_percpu_irq(port->pp->dev->irq); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2114 | napi_schedule(&port->napi); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2115 | |
| 2116 | return IRQ_HANDLED; |
| 2117 | } |
| 2118 | |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 2119 | static int mvneta_fixed_link_update(struct mvneta_port *pp, |
| 2120 | struct phy_device *phy) |
| 2121 | { |
| 2122 | struct fixed_phy_status status; |
| 2123 | struct fixed_phy_status changed = {}; |
| 2124 | u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS); |
| 2125 | |
| 2126 | status.link = !!(gmac_stat & MVNETA_GMAC_LINK_UP); |
| 2127 | if (gmac_stat & MVNETA_GMAC_SPEED_1000) |
| 2128 | status.speed = SPEED_1000; |
| 2129 | else if (gmac_stat & MVNETA_GMAC_SPEED_100) |
| 2130 | status.speed = SPEED_100; |
| 2131 | else |
| 2132 | status.speed = SPEED_10; |
| 2133 | status.duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX); |
| 2134 | changed.link = 1; |
| 2135 | changed.speed = 1; |
| 2136 | changed.duplex = 1; |
| 2137 | fixed_phy_update_state(phy, &status, &changed); |
| 2138 | return 0; |
| 2139 | } |
| 2140 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2141 | /* NAPI handler |
| 2142 | * Bits 0 - 7 of the causeRxTx register indicate that are transmitted |
| 2143 | * packets on the corresponding TXQ (Bit 0 is for TX queue 1). |
| 2144 | * Bits 8 -15 of the cause Rx Tx register indicate that are received |
| 2145 | * packets on the corresponding RXQ (Bit 8 is for RX queue 0). |
| 2146 | * Each CPU has its own causeRxTx register |
| 2147 | */ |
| 2148 | static int mvneta_poll(struct napi_struct *napi, int budget) |
| 2149 | { |
| 2150 | int rx_done = 0; |
| 2151 | u32 cause_rx_tx; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2152 | struct mvneta_port *pp = netdev_priv(napi->dev); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2153 | struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2154 | |
| 2155 | if (!netif_running(pp->dev)) { |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2156 | napi_complete(&port->napi); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2157 | return rx_done; |
| 2158 | } |
| 2159 | |
| 2160 | /* Read cause register */ |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 2161 | cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE); |
| 2162 | if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) { |
| 2163 | u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE); |
| 2164 | |
| 2165 | mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0); |
| 2166 | if (pp->use_inband_status && (cause_misc & |
| 2167 | (MVNETA_CAUSE_PHY_STATUS_CHANGE | |
| 2168 | MVNETA_CAUSE_LINK_CHANGE | |
| 2169 | MVNETA_CAUSE_PSC_SYNC_CHANGE))) { |
| 2170 | mvneta_fixed_link_update(pp, pp->phy_dev); |
| 2171 | } |
| 2172 | } |
willy tarreau | 71f6d1b | 2014-01-16 08:20:11 +0100 | [diff] [blame] | 2173 | |
| 2174 | /* Release Tx descriptors */ |
| 2175 | if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) { |
Arnaud Ebalard | 0713a86 | 2014-01-16 08:20:18 +0100 | [diff] [blame] | 2176 | mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL)); |
willy tarreau | 71f6d1b | 2014-01-16 08:20:11 +0100 | [diff] [blame] | 2177 | cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL; |
| 2178 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2179 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 2180 | /* For the case where the last mvneta_poll did not process all |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2181 | * RX packets |
| 2182 | */ |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2183 | cause_rx_tx |= port->cause_rx_tx; |
Maxime Ripard | d893665 | 2015-09-25 18:09:37 +0200 | [diff] [blame] | 2184 | rx_done = mvneta_rx(pp, budget, &pp->rxqs[rxq_def]); |
| 2185 | budget -= rx_done; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2186 | |
| 2187 | if (budget > 0) { |
| 2188 | cause_rx_tx = 0; |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2189 | napi_complete(&port->napi); |
| 2190 | enable_percpu_irq(pp->dev->irq, 0); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2191 | } |
| 2192 | |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2193 | port->cause_rx_tx = cause_rx_tx; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2194 | return rx_done; |
| 2195 | } |
| 2196 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2197 | /* Handle rxq fill: allocates rxq skbs; called when initializing a port */ |
| 2198 | static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq, |
| 2199 | int num) |
| 2200 | { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2201 | int i; |
| 2202 | |
| 2203 | for (i = 0; i < num; i++) { |
willy tarreau | a1a65ab | 2014-01-16 08:20:13 +0100 | [diff] [blame] | 2204 | memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc)); |
| 2205 | if (mvneta_rx_refill(pp, rxq->descs + i) != 0) { |
| 2206 | netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n", |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2207 | __func__, rxq->id, i, num); |
| 2208 | break; |
| 2209 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | /* Add this number of RX descriptors as non occupied (ready to |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 2213 | * get packets) |
| 2214 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2215 | mvneta_rxq_non_occup_desc_add(pp, rxq, i); |
| 2216 | |
| 2217 | return i; |
| 2218 | } |
| 2219 | |
| 2220 | /* Free all packets pending transmit from all TXQs and reset TX port */ |
| 2221 | static void mvneta_tx_reset(struct mvneta_port *pp) |
| 2222 | { |
| 2223 | int queue; |
| 2224 | |
Ezequiel Garcia | 9672850 | 2014-05-22 20:06:59 -0300 | [diff] [blame] | 2225 | /* free the skb's in the tx ring */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2226 | for (queue = 0; queue < txq_number; queue++) |
| 2227 | mvneta_txq_done_force(pp, &pp->txqs[queue]); |
| 2228 | |
| 2229 | mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET); |
| 2230 | mvreg_write(pp, MVNETA_PORT_TX_RESET, 0); |
| 2231 | } |
| 2232 | |
| 2233 | static void mvneta_rx_reset(struct mvneta_port *pp) |
| 2234 | { |
| 2235 | mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET); |
| 2236 | mvreg_write(pp, MVNETA_PORT_RX_RESET, 0); |
| 2237 | } |
| 2238 | |
| 2239 | /* Rx/Tx queue initialization/cleanup methods */ |
| 2240 | |
| 2241 | /* Create a specified RX queue */ |
| 2242 | static int mvneta_rxq_init(struct mvneta_port *pp, |
| 2243 | struct mvneta_rx_queue *rxq) |
| 2244 | |
| 2245 | { |
| 2246 | rxq->size = pp->rx_ring_size; |
| 2247 | |
| 2248 | /* Allocate memory for RX descriptors */ |
| 2249 | rxq->descs = dma_alloc_coherent(pp->dev->dev.parent, |
| 2250 | rxq->size * MVNETA_DESC_ALIGNED_SIZE, |
| 2251 | &rxq->descs_phys, GFP_KERNEL); |
Joe Perches | d0320f7 | 2013-03-14 13:07:21 +0000 | [diff] [blame] | 2252 | if (rxq->descs == NULL) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2253 | return -ENOMEM; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2254 | |
| 2255 | BUG_ON(rxq->descs != |
| 2256 | PTR_ALIGN(rxq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE)); |
| 2257 | |
| 2258 | rxq->last_desc = rxq->size - 1; |
| 2259 | |
| 2260 | /* Set Rx descriptors queue starting address */ |
| 2261 | mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys); |
| 2262 | mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size); |
| 2263 | |
| 2264 | /* Set Offset */ |
| 2265 | mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD); |
| 2266 | |
| 2267 | /* Set coalescing pkts and time */ |
| 2268 | mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal); |
| 2269 | mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal); |
| 2270 | |
| 2271 | /* Fill RXQ with buffers from RX pool */ |
| 2272 | mvneta_rxq_buf_size_set(pp, rxq, MVNETA_RX_BUF_SIZE(pp->pkt_size)); |
| 2273 | mvneta_rxq_bm_disable(pp, rxq); |
| 2274 | mvneta_rxq_fill(pp, rxq, rxq->size); |
| 2275 | |
| 2276 | return 0; |
| 2277 | } |
| 2278 | |
| 2279 | /* Cleanup Rx queue */ |
| 2280 | static void mvneta_rxq_deinit(struct mvneta_port *pp, |
| 2281 | struct mvneta_rx_queue *rxq) |
| 2282 | { |
| 2283 | mvneta_rxq_drop_pkts(pp, rxq); |
| 2284 | |
| 2285 | if (rxq->descs) |
| 2286 | dma_free_coherent(pp->dev->dev.parent, |
| 2287 | rxq->size * MVNETA_DESC_ALIGNED_SIZE, |
| 2288 | rxq->descs, |
| 2289 | rxq->descs_phys); |
| 2290 | |
| 2291 | rxq->descs = NULL; |
| 2292 | rxq->last_desc = 0; |
| 2293 | rxq->next_desc_to_proc = 0; |
| 2294 | rxq->descs_phys = 0; |
| 2295 | } |
| 2296 | |
| 2297 | /* Create and initialize a tx queue */ |
| 2298 | static int mvneta_txq_init(struct mvneta_port *pp, |
| 2299 | struct mvneta_tx_queue *txq) |
| 2300 | { |
| 2301 | txq->size = pp->tx_ring_size; |
| 2302 | |
Ezequiel Garcia | 8eef5f9 | 2014-05-30 13:40:05 -0300 | [diff] [blame] | 2303 | /* A queue must always have room for at least one skb. |
| 2304 | * Therefore, stop the queue when the free entries reaches |
| 2305 | * the maximum number of descriptors per skb. |
| 2306 | */ |
| 2307 | txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS; |
| 2308 | txq->tx_wake_threshold = txq->tx_stop_threshold / 2; |
| 2309 | |
| 2310 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2311 | /* Allocate memory for TX descriptors */ |
| 2312 | txq->descs = dma_alloc_coherent(pp->dev->dev.parent, |
| 2313 | txq->size * MVNETA_DESC_ALIGNED_SIZE, |
| 2314 | &txq->descs_phys, GFP_KERNEL); |
Joe Perches | d0320f7 | 2013-03-14 13:07:21 +0000 | [diff] [blame] | 2315 | if (txq->descs == NULL) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2316 | return -ENOMEM; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2317 | |
| 2318 | /* Make sure descriptor address is cache line size aligned */ |
| 2319 | BUG_ON(txq->descs != |
| 2320 | PTR_ALIGN(txq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE)); |
| 2321 | |
| 2322 | txq->last_desc = txq->size - 1; |
| 2323 | |
| 2324 | /* Set maximum bandwidth for enabled TXQs */ |
| 2325 | mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff); |
| 2326 | mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff); |
| 2327 | |
| 2328 | /* Set Tx descriptors queue starting address */ |
| 2329 | mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys); |
| 2330 | mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size); |
| 2331 | |
| 2332 | txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL); |
| 2333 | if (txq->tx_skb == NULL) { |
| 2334 | dma_free_coherent(pp->dev->dev.parent, |
| 2335 | txq->size * MVNETA_DESC_ALIGNED_SIZE, |
| 2336 | txq->descs, txq->descs_phys); |
| 2337 | return -ENOMEM; |
| 2338 | } |
Ezequiel Garcia | 2adb719 | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 2339 | |
| 2340 | /* Allocate DMA buffers for TSO MAC/IP/TCP headers */ |
| 2341 | txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent, |
| 2342 | txq->size * TSO_HEADER_SIZE, |
| 2343 | &txq->tso_hdrs_phys, GFP_KERNEL); |
| 2344 | if (txq->tso_hdrs == NULL) { |
| 2345 | kfree(txq->tx_skb); |
| 2346 | dma_free_coherent(pp->dev->dev.parent, |
| 2347 | txq->size * MVNETA_DESC_ALIGNED_SIZE, |
| 2348 | txq->descs, txq->descs_phys); |
| 2349 | return -ENOMEM; |
| 2350 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2351 | mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal); |
| 2352 | |
| 2353 | return 0; |
| 2354 | } |
| 2355 | |
| 2356 | /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/ |
| 2357 | static void mvneta_txq_deinit(struct mvneta_port *pp, |
| 2358 | struct mvneta_tx_queue *txq) |
| 2359 | { |
| 2360 | kfree(txq->tx_skb); |
| 2361 | |
Ezequiel Garcia | 2adb719 | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 2362 | if (txq->tso_hdrs) |
| 2363 | dma_free_coherent(pp->dev->dev.parent, |
| 2364 | txq->size * TSO_HEADER_SIZE, |
| 2365 | txq->tso_hdrs, txq->tso_hdrs_phys); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2366 | if (txq->descs) |
| 2367 | dma_free_coherent(pp->dev->dev.parent, |
| 2368 | txq->size * MVNETA_DESC_ALIGNED_SIZE, |
| 2369 | txq->descs, txq->descs_phys); |
| 2370 | |
| 2371 | txq->descs = NULL; |
| 2372 | txq->last_desc = 0; |
| 2373 | txq->next_desc_to_proc = 0; |
| 2374 | txq->descs_phys = 0; |
| 2375 | |
| 2376 | /* Set minimum bandwidth for disabled TXQs */ |
| 2377 | mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0); |
| 2378 | mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0); |
| 2379 | |
| 2380 | /* Set Tx descriptors queue starting address and size */ |
| 2381 | mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0); |
| 2382 | mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0); |
| 2383 | } |
| 2384 | |
| 2385 | /* Cleanup all Tx queues */ |
| 2386 | static void mvneta_cleanup_txqs(struct mvneta_port *pp) |
| 2387 | { |
| 2388 | int queue; |
| 2389 | |
| 2390 | for (queue = 0; queue < txq_number; queue++) |
| 2391 | mvneta_txq_deinit(pp, &pp->txqs[queue]); |
| 2392 | } |
| 2393 | |
| 2394 | /* Cleanup all Rx queues */ |
| 2395 | static void mvneta_cleanup_rxqs(struct mvneta_port *pp) |
| 2396 | { |
Maxime Ripard | d893665 | 2015-09-25 18:09:37 +0200 | [diff] [blame] | 2397 | mvneta_rxq_deinit(pp, &pp->rxqs[rxq_def]); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | |
| 2401 | /* Init all Rx queues */ |
| 2402 | static int mvneta_setup_rxqs(struct mvneta_port *pp) |
| 2403 | { |
Maxime Ripard | d893665 | 2015-09-25 18:09:37 +0200 | [diff] [blame] | 2404 | int err = mvneta_rxq_init(pp, &pp->rxqs[rxq_def]); |
| 2405 | if (err) { |
| 2406 | netdev_err(pp->dev, "%s: can't create rxq=%d\n", |
| 2407 | __func__, rxq_def); |
| 2408 | mvneta_cleanup_rxqs(pp); |
| 2409 | return err; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | return 0; |
| 2413 | } |
| 2414 | |
| 2415 | /* Init all tx queues */ |
| 2416 | static int mvneta_setup_txqs(struct mvneta_port *pp) |
| 2417 | { |
| 2418 | int queue; |
| 2419 | |
| 2420 | for (queue = 0; queue < txq_number; queue++) { |
| 2421 | int err = mvneta_txq_init(pp, &pp->txqs[queue]); |
| 2422 | if (err) { |
| 2423 | netdev_err(pp->dev, "%s: can't create txq=%d\n", |
| 2424 | __func__, queue); |
| 2425 | mvneta_cleanup_txqs(pp); |
| 2426 | return err; |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | return 0; |
| 2431 | } |
| 2432 | |
| 2433 | static void mvneta_start_dev(struct mvneta_port *pp) |
| 2434 | { |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2435 | unsigned int cpu; |
| 2436 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2437 | mvneta_max_rx_size_set(pp, pp->pkt_size); |
| 2438 | mvneta_txq_max_tx_size_set(pp, pp->pkt_size); |
| 2439 | |
| 2440 | /* start the Rx/Tx activity */ |
| 2441 | mvneta_port_enable(pp); |
| 2442 | |
| 2443 | /* Enable polling on the port */ |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2444 | for_each_present_cpu(cpu) { |
| 2445 | struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu); |
| 2446 | |
| 2447 | napi_enable(&port->napi); |
| 2448 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2449 | |
| 2450 | /* Unmask interrupts */ |
| 2451 | mvreg_write(pp, MVNETA_INTR_NEW_MASK, |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 2452 | MVNETA_RX_INTR_MASK(rxq_number) | |
| 2453 | MVNETA_TX_INTR_MASK(txq_number) | |
| 2454 | MVNETA_MISCINTR_INTR_MASK); |
| 2455 | mvreg_write(pp, MVNETA_INTR_MISC_MASK, |
| 2456 | MVNETA_CAUSE_PHY_STATUS_CHANGE | |
| 2457 | MVNETA_CAUSE_LINK_CHANGE | |
| 2458 | MVNETA_CAUSE_PSC_SYNC_CHANGE); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2459 | |
| 2460 | phy_start(pp->phy_dev); |
| 2461 | netif_tx_start_all_queues(pp->dev); |
| 2462 | } |
| 2463 | |
| 2464 | static void mvneta_stop_dev(struct mvneta_port *pp) |
| 2465 | { |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2466 | unsigned int cpu; |
| 2467 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2468 | phy_stop(pp->phy_dev); |
| 2469 | |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2470 | for_each_present_cpu(cpu) { |
| 2471 | struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu); |
| 2472 | |
| 2473 | napi_disable(&port->napi); |
| 2474 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2475 | |
| 2476 | netif_carrier_off(pp->dev); |
| 2477 | |
| 2478 | mvneta_port_down(pp); |
| 2479 | netif_tx_stop_all_queues(pp->dev); |
| 2480 | |
| 2481 | /* Stop the port activity */ |
| 2482 | mvneta_port_disable(pp); |
| 2483 | |
| 2484 | /* Clear all ethernet port interrupts */ |
| 2485 | mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0); |
| 2486 | mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0); |
| 2487 | |
| 2488 | /* Mask all ethernet port interrupts */ |
| 2489 | mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0); |
| 2490 | mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0); |
| 2491 | mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0); |
| 2492 | |
| 2493 | mvneta_tx_reset(pp); |
| 2494 | mvneta_rx_reset(pp); |
| 2495 | } |
| 2496 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2497 | /* Return positive if MTU is valid */ |
| 2498 | static int mvneta_check_mtu_valid(struct net_device *dev, int mtu) |
| 2499 | { |
| 2500 | if (mtu < 68) { |
| 2501 | netdev_err(dev, "cannot change mtu to less than 68\n"); |
| 2502 | return -EINVAL; |
| 2503 | } |
| 2504 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 2505 | /* 9676 == 9700 - 20 and rounding to 8 */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2506 | if (mtu > 9676) { |
| 2507 | netdev_info(dev, "Illegal MTU value %d, round to 9676\n", mtu); |
| 2508 | mtu = 9676; |
| 2509 | } |
| 2510 | |
| 2511 | if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) { |
| 2512 | netdev_info(dev, "Illegal MTU value %d, rounding to %d\n", |
| 2513 | mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8)); |
| 2514 | mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8); |
| 2515 | } |
| 2516 | |
| 2517 | return mtu; |
| 2518 | } |
| 2519 | |
| 2520 | /* Change the device mtu */ |
| 2521 | static int mvneta_change_mtu(struct net_device *dev, int mtu) |
| 2522 | { |
| 2523 | struct mvneta_port *pp = netdev_priv(dev); |
| 2524 | int ret; |
| 2525 | |
| 2526 | mtu = mvneta_check_mtu_valid(dev, mtu); |
| 2527 | if (mtu < 0) |
| 2528 | return -EINVAL; |
| 2529 | |
| 2530 | dev->mtu = mtu; |
| 2531 | |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 2532 | if (!netif_running(dev)) { |
| 2533 | netdev_update_features(dev); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2534 | return 0; |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 2535 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2536 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 2537 | /* The interface is running, so we have to force a |
Ezequiel Garcia | a92dbd9 | 2014-05-22 20:06:58 -0300 | [diff] [blame] | 2538 | * reallocation of the queues |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2539 | */ |
| 2540 | mvneta_stop_dev(pp); |
| 2541 | |
| 2542 | mvneta_cleanup_txqs(pp); |
| 2543 | mvneta_cleanup_rxqs(pp); |
| 2544 | |
Ezequiel Garcia | a92dbd9 | 2014-05-22 20:06:58 -0300 | [diff] [blame] | 2545 | pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu); |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 2546 | pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) + |
| 2547 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2548 | |
| 2549 | ret = mvneta_setup_rxqs(pp); |
| 2550 | if (ret) { |
Ezequiel Garcia | a92dbd9 | 2014-05-22 20:06:58 -0300 | [diff] [blame] | 2551 | netdev_err(dev, "unable to setup rxqs after MTU change\n"); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2552 | return ret; |
| 2553 | } |
| 2554 | |
Ezequiel Garcia | a92dbd9 | 2014-05-22 20:06:58 -0300 | [diff] [blame] | 2555 | ret = mvneta_setup_txqs(pp); |
| 2556 | if (ret) { |
| 2557 | netdev_err(dev, "unable to setup txqs after MTU change\n"); |
| 2558 | return ret; |
| 2559 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2560 | |
| 2561 | mvneta_start_dev(pp); |
| 2562 | mvneta_port_up(pp); |
| 2563 | |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 2564 | netdev_update_features(dev); |
| 2565 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2566 | return 0; |
| 2567 | } |
| 2568 | |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 2569 | static netdev_features_t mvneta_fix_features(struct net_device *dev, |
| 2570 | netdev_features_t features) |
| 2571 | { |
| 2572 | struct mvneta_port *pp = netdev_priv(dev); |
| 2573 | |
| 2574 | if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) { |
| 2575 | features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO); |
| 2576 | netdev_info(dev, |
| 2577 | "Disable IP checksum for MTU greater than %dB\n", |
| 2578 | pp->tx_csum_limit); |
| 2579 | } |
| 2580 | |
| 2581 | return features; |
| 2582 | } |
| 2583 | |
Thomas Petazzoni | 8cc3e43 | 2013-06-04 04:52:23 +0000 | [diff] [blame] | 2584 | /* Get mac address */ |
| 2585 | static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr) |
| 2586 | { |
| 2587 | u32 mac_addr_l, mac_addr_h; |
| 2588 | |
| 2589 | mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW); |
| 2590 | mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH); |
| 2591 | addr[0] = (mac_addr_h >> 24) & 0xFF; |
| 2592 | addr[1] = (mac_addr_h >> 16) & 0xFF; |
| 2593 | addr[2] = (mac_addr_h >> 8) & 0xFF; |
| 2594 | addr[3] = mac_addr_h & 0xFF; |
| 2595 | addr[4] = (mac_addr_l >> 8) & 0xFF; |
| 2596 | addr[5] = mac_addr_l & 0xFF; |
| 2597 | } |
| 2598 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2599 | /* Handle setting mac address */ |
| 2600 | static int mvneta_set_mac_addr(struct net_device *dev, void *addr) |
| 2601 | { |
| 2602 | struct mvneta_port *pp = netdev_priv(dev); |
Ezequiel Garcia | e68de36 | 2014-05-22 20:07:00 -0300 | [diff] [blame] | 2603 | struct sockaddr *sockaddr = addr; |
| 2604 | int ret; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2605 | |
Ezequiel Garcia | e68de36 | 2014-05-22 20:07:00 -0300 | [diff] [blame] | 2606 | ret = eth_prepare_mac_addr_change(dev, addr); |
| 2607 | if (ret < 0) |
| 2608 | return ret; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2609 | /* Remove previous address table entry */ |
| 2610 | mvneta_mac_addr_set(pp, dev->dev_addr, -1); |
| 2611 | |
| 2612 | /* Set new addr in hw */ |
Ezequiel Garcia | e68de36 | 2014-05-22 20:07:00 -0300 | [diff] [blame] | 2613 | mvneta_mac_addr_set(pp, sockaddr->sa_data, rxq_def); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2614 | |
Ezequiel Garcia | e68de36 | 2014-05-22 20:07:00 -0300 | [diff] [blame] | 2615 | eth_commit_mac_addr_change(dev, addr); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2616 | return 0; |
| 2617 | } |
| 2618 | |
| 2619 | static void mvneta_adjust_link(struct net_device *ndev) |
| 2620 | { |
| 2621 | struct mvneta_port *pp = netdev_priv(ndev); |
| 2622 | struct phy_device *phydev = pp->phy_dev; |
| 2623 | int status_change = 0; |
| 2624 | |
| 2625 | if (phydev->link) { |
| 2626 | if ((pp->speed != phydev->speed) || |
| 2627 | (pp->duplex != phydev->duplex)) { |
| 2628 | u32 val; |
| 2629 | |
| 2630 | val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG); |
| 2631 | val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED | |
| 2632 | MVNETA_GMAC_CONFIG_GMII_SPEED | |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 2633 | MVNETA_GMAC_CONFIG_FULL_DUPLEX); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2634 | |
| 2635 | if (phydev->duplex) |
| 2636 | val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX; |
| 2637 | |
| 2638 | if (phydev->speed == SPEED_1000) |
| 2639 | val |= MVNETA_GMAC_CONFIG_GMII_SPEED; |
Thomas Petazzoni | 4d12bc6 | 2014-07-08 10:49:43 +0200 | [diff] [blame] | 2640 | else if (phydev->speed == SPEED_100) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2641 | val |= MVNETA_GMAC_CONFIG_MII_SPEED; |
| 2642 | |
| 2643 | mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val); |
| 2644 | |
| 2645 | pp->duplex = phydev->duplex; |
| 2646 | pp->speed = phydev->speed; |
| 2647 | } |
| 2648 | } |
| 2649 | |
| 2650 | if (phydev->link != pp->link) { |
| 2651 | if (!phydev->link) { |
| 2652 | pp->duplex = -1; |
| 2653 | pp->speed = 0; |
| 2654 | } |
| 2655 | |
| 2656 | pp->link = phydev->link; |
| 2657 | status_change = 1; |
| 2658 | } |
| 2659 | |
| 2660 | if (status_change) { |
| 2661 | if (phydev->link) { |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 2662 | if (!pp->use_inband_status) { |
| 2663 | u32 val = mvreg_read(pp, |
| 2664 | MVNETA_GMAC_AUTONEG_CONFIG); |
| 2665 | val &= ~MVNETA_GMAC_FORCE_LINK_DOWN; |
| 2666 | val |= MVNETA_GMAC_FORCE_LINK_PASS; |
| 2667 | mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, |
| 2668 | val); |
| 2669 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2670 | mvneta_port_up(pp); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2671 | } else { |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 2672 | if (!pp->use_inband_status) { |
| 2673 | u32 val = mvreg_read(pp, |
| 2674 | MVNETA_GMAC_AUTONEG_CONFIG); |
| 2675 | val &= ~MVNETA_GMAC_FORCE_LINK_PASS; |
| 2676 | val |= MVNETA_GMAC_FORCE_LINK_DOWN; |
| 2677 | mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, |
| 2678 | val); |
| 2679 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2680 | mvneta_port_down(pp); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2681 | } |
Ezequiel Garcia | 0089b74 | 2014-10-31 12:57:20 -0300 | [diff] [blame] | 2682 | phy_print_status(phydev); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2683 | } |
| 2684 | } |
| 2685 | |
| 2686 | static int mvneta_mdio_probe(struct mvneta_port *pp) |
| 2687 | { |
| 2688 | struct phy_device *phy_dev; |
| 2689 | |
| 2690 | phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0, |
| 2691 | pp->phy_interface); |
| 2692 | if (!phy_dev) { |
| 2693 | netdev_err(pp->dev, "could not find the PHY\n"); |
| 2694 | return -ENODEV; |
| 2695 | } |
| 2696 | |
| 2697 | phy_dev->supported &= PHY_GBIT_FEATURES; |
| 2698 | phy_dev->advertising = phy_dev->supported; |
| 2699 | |
| 2700 | pp->phy_dev = phy_dev; |
| 2701 | pp->link = 0; |
| 2702 | pp->duplex = 0; |
| 2703 | pp->speed = 0; |
| 2704 | |
| 2705 | return 0; |
| 2706 | } |
| 2707 | |
| 2708 | static void mvneta_mdio_remove(struct mvneta_port *pp) |
| 2709 | { |
| 2710 | phy_disconnect(pp->phy_dev); |
| 2711 | pp->phy_dev = NULL; |
| 2712 | } |
| 2713 | |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 2714 | static void mvneta_percpu_enable(void *arg) |
| 2715 | { |
| 2716 | struct mvneta_port *pp = arg; |
| 2717 | |
| 2718 | enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE); |
| 2719 | } |
| 2720 | |
| 2721 | static void mvneta_percpu_disable(void *arg) |
| 2722 | { |
| 2723 | struct mvneta_port *pp = arg; |
| 2724 | |
| 2725 | disable_percpu_irq(pp->dev->irq); |
| 2726 | } |
| 2727 | |
| 2728 | static void mvneta_percpu_elect(struct mvneta_port *pp) |
| 2729 | { |
| 2730 | int online_cpu_idx, cpu, i = 0; |
| 2731 | |
| 2732 | online_cpu_idx = rxq_def % num_online_cpus(); |
| 2733 | |
| 2734 | for_each_online_cpu(cpu) { |
| 2735 | if (i == online_cpu_idx) |
| 2736 | /* Enable per-CPU interrupt on the one CPU we |
| 2737 | * just elected |
| 2738 | */ |
| 2739 | smp_call_function_single(cpu, mvneta_percpu_enable, |
| 2740 | pp, true); |
| 2741 | else |
| 2742 | /* Disable per-CPU interrupt on all the other CPU */ |
| 2743 | smp_call_function_single(cpu, mvneta_percpu_disable, |
| 2744 | pp, true); |
| 2745 | i++; |
| 2746 | } |
| 2747 | }; |
| 2748 | |
| 2749 | static int mvneta_percpu_notifier(struct notifier_block *nfb, |
| 2750 | unsigned long action, void *hcpu) |
| 2751 | { |
| 2752 | struct mvneta_port *pp = container_of(nfb, struct mvneta_port, |
| 2753 | cpu_notifier); |
| 2754 | int cpu = (unsigned long)hcpu, other_cpu; |
| 2755 | struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu); |
| 2756 | |
| 2757 | switch (action) { |
| 2758 | case CPU_ONLINE: |
| 2759 | case CPU_ONLINE_FROZEN: |
| 2760 | netif_tx_stop_all_queues(pp->dev); |
| 2761 | |
| 2762 | /* We have to synchronise on tha napi of each CPU |
| 2763 | * except the one just being waked up |
| 2764 | */ |
| 2765 | for_each_online_cpu(other_cpu) { |
| 2766 | if (other_cpu != cpu) { |
| 2767 | struct mvneta_pcpu_port *other_port = |
| 2768 | per_cpu_ptr(pp->ports, other_cpu); |
| 2769 | |
| 2770 | napi_synchronize(&other_port->napi); |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | /* Mask all ethernet port interrupts */ |
| 2775 | mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0); |
| 2776 | mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0); |
| 2777 | mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0); |
| 2778 | napi_enable(&port->napi); |
| 2779 | |
| 2780 | /* Enable per-CPU interrupt on the one CPU we care |
| 2781 | * about. |
| 2782 | */ |
| 2783 | mvneta_percpu_elect(pp); |
| 2784 | |
| 2785 | /* Unmask all ethernet port interrupts */ |
| 2786 | mvreg_write(pp, MVNETA_INTR_NEW_MASK, |
| 2787 | MVNETA_RX_INTR_MASK(rxq_number) | |
| 2788 | MVNETA_TX_INTR_MASK(txq_number) | |
| 2789 | MVNETA_MISCINTR_INTR_MASK); |
| 2790 | mvreg_write(pp, MVNETA_INTR_MISC_MASK, |
| 2791 | MVNETA_CAUSE_PHY_STATUS_CHANGE | |
| 2792 | MVNETA_CAUSE_LINK_CHANGE | |
| 2793 | MVNETA_CAUSE_PSC_SYNC_CHANGE); |
| 2794 | netif_tx_start_all_queues(pp->dev); |
| 2795 | break; |
| 2796 | case CPU_DOWN_PREPARE: |
| 2797 | case CPU_DOWN_PREPARE_FROZEN: |
| 2798 | netif_tx_stop_all_queues(pp->dev); |
| 2799 | /* Mask all ethernet port interrupts */ |
| 2800 | mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0); |
| 2801 | mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0); |
| 2802 | mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0); |
| 2803 | |
| 2804 | napi_synchronize(&port->napi); |
| 2805 | napi_disable(&port->napi); |
| 2806 | /* Disable per-CPU interrupts on the CPU that is |
| 2807 | * brought down. |
| 2808 | */ |
| 2809 | smp_call_function_single(cpu, mvneta_percpu_disable, |
| 2810 | pp, true); |
| 2811 | |
| 2812 | break; |
| 2813 | case CPU_DEAD: |
| 2814 | case CPU_DEAD_FROZEN: |
| 2815 | /* Check if a new CPU must be elected now this on is down */ |
| 2816 | mvneta_percpu_elect(pp); |
| 2817 | /* Unmask all ethernet port interrupts */ |
| 2818 | mvreg_write(pp, MVNETA_INTR_NEW_MASK, |
| 2819 | MVNETA_RX_INTR_MASK(rxq_number) | |
| 2820 | MVNETA_TX_INTR_MASK(txq_number) | |
| 2821 | MVNETA_MISCINTR_INTR_MASK); |
| 2822 | mvreg_write(pp, MVNETA_INTR_MISC_MASK, |
| 2823 | MVNETA_CAUSE_PHY_STATUS_CHANGE | |
| 2824 | MVNETA_CAUSE_LINK_CHANGE | |
| 2825 | MVNETA_CAUSE_PSC_SYNC_CHANGE); |
| 2826 | netif_tx_start_all_queues(pp->dev); |
| 2827 | break; |
| 2828 | } |
| 2829 | |
| 2830 | return NOTIFY_OK; |
| 2831 | } |
| 2832 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2833 | static int mvneta_open(struct net_device *dev) |
| 2834 | { |
| 2835 | struct mvneta_port *pp = netdev_priv(dev); |
| 2836 | int ret; |
| 2837 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2838 | pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu); |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 2839 | pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) + |
| 2840 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2841 | |
| 2842 | ret = mvneta_setup_rxqs(pp); |
| 2843 | if (ret) |
| 2844 | return ret; |
| 2845 | |
| 2846 | ret = mvneta_setup_txqs(pp); |
| 2847 | if (ret) |
| 2848 | goto err_cleanup_rxqs; |
| 2849 | |
| 2850 | /* Connect to port interrupt line */ |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2851 | ret = request_percpu_irq(pp->dev->irq, mvneta_isr, |
| 2852 | MVNETA_DRIVER_NAME, pp->ports); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2853 | if (ret) { |
| 2854 | netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq); |
| 2855 | goto err_cleanup_txqs; |
| 2856 | } |
| 2857 | |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 2858 | /* Even though the documentation says that request_percpu_irq |
| 2859 | * doesn't enable the interrupts automatically, it actually |
| 2860 | * does so on the local CPU. |
| 2861 | * |
| 2862 | * Make sure it's disabled. |
| 2863 | */ |
| 2864 | mvneta_percpu_disable(pp); |
| 2865 | |
| 2866 | /* Elect a CPU to handle our RX queue interrupt */ |
| 2867 | mvneta_percpu_elect(pp); |
| 2868 | |
| 2869 | /* Register a CPU notifier to handle the case where our CPU |
| 2870 | * might be taken offline. |
| 2871 | */ |
| 2872 | register_cpu_notifier(&pp->cpu_notifier); |
| 2873 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2874 | /* In default link is down */ |
| 2875 | netif_carrier_off(pp->dev); |
| 2876 | |
| 2877 | ret = mvneta_mdio_probe(pp); |
| 2878 | if (ret < 0) { |
| 2879 | netdev_err(dev, "cannot probe MDIO bus\n"); |
| 2880 | goto err_free_irq; |
| 2881 | } |
| 2882 | |
| 2883 | mvneta_start_dev(pp); |
| 2884 | |
| 2885 | return 0; |
| 2886 | |
| 2887 | err_free_irq: |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2888 | free_percpu_irq(pp->dev->irq, pp->ports); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2889 | err_cleanup_txqs: |
| 2890 | mvneta_cleanup_txqs(pp); |
| 2891 | err_cleanup_rxqs: |
| 2892 | mvneta_cleanup_rxqs(pp); |
| 2893 | return ret; |
| 2894 | } |
| 2895 | |
| 2896 | /* Stop the port, free port interrupt line */ |
| 2897 | static int mvneta_stop(struct net_device *dev) |
| 2898 | { |
| 2899 | struct mvneta_port *pp = netdev_priv(dev); |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 2900 | int cpu; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2901 | |
| 2902 | mvneta_stop_dev(pp); |
| 2903 | mvneta_mdio_remove(pp); |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 2904 | unregister_cpu_notifier(&pp->cpu_notifier); |
| 2905 | for_each_present_cpu(cpu) |
| 2906 | smp_call_function_single(cpu, mvneta_percpu_disable, pp, true); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2907 | free_percpu_irq(dev->irq, pp->ports); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2908 | mvneta_cleanup_rxqs(pp); |
| 2909 | mvneta_cleanup_txqs(pp); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2910 | |
| 2911 | return 0; |
| 2912 | } |
| 2913 | |
Thomas Petazzoni | 15f5945 | 2013-09-04 16:26:52 +0200 | [diff] [blame] | 2914 | static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 2915 | { |
| 2916 | struct mvneta_port *pp = netdev_priv(dev); |
Thomas Petazzoni | 15f5945 | 2013-09-04 16:26:52 +0200 | [diff] [blame] | 2917 | |
| 2918 | if (!pp->phy_dev) |
| 2919 | return -ENOTSUPP; |
| 2920 | |
Stas Sergeev | ecf7b36 | 2015-04-01 19:23:29 +0300 | [diff] [blame] | 2921 | return phy_mii_ioctl(pp->phy_dev, ifr, cmd); |
Thomas Petazzoni | 15f5945 | 2013-09-04 16:26:52 +0200 | [diff] [blame] | 2922 | } |
| 2923 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2924 | /* Ethtool methods */ |
| 2925 | |
| 2926 | /* Get settings (phy address, speed) for ethtools */ |
| 2927 | int mvneta_ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 2928 | { |
| 2929 | struct mvneta_port *pp = netdev_priv(dev); |
| 2930 | |
| 2931 | if (!pp->phy_dev) |
| 2932 | return -ENODEV; |
| 2933 | |
| 2934 | return phy_ethtool_gset(pp->phy_dev, cmd); |
| 2935 | } |
| 2936 | |
| 2937 | /* Set settings (phy address, speed) for ethtools */ |
| 2938 | int mvneta_ethtool_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 2939 | { |
| 2940 | struct mvneta_port *pp = netdev_priv(dev); |
| 2941 | |
| 2942 | if (!pp->phy_dev) |
| 2943 | return -ENODEV; |
| 2944 | |
| 2945 | return phy_ethtool_sset(pp->phy_dev, cmd); |
| 2946 | } |
| 2947 | |
| 2948 | /* Set interrupt coalescing for ethtools */ |
| 2949 | static int mvneta_ethtool_set_coalesce(struct net_device *dev, |
| 2950 | struct ethtool_coalesce *c) |
| 2951 | { |
| 2952 | struct mvneta_port *pp = netdev_priv(dev); |
| 2953 | int queue; |
| 2954 | |
| 2955 | for (queue = 0; queue < rxq_number; queue++) { |
| 2956 | struct mvneta_rx_queue *rxq = &pp->rxqs[queue]; |
| 2957 | rxq->time_coal = c->rx_coalesce_usecs; |
| 2958 | rxq->pkts_coal = c->rx_max_coalesced_frames; |
| 2959 | mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal); |
| 2960 | mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal); |
| 2961 | } |
| 2962 | |
| 2963 | for (queue = 0; queue < txq_number; queue++) { |
| 2964 | struct mvneta_tx_queue *txq = &pp->txqs[queue]; |
| 2965 | txq->done_pkts_coal = c->tx_max_coalesced_frames; |
| 2966 | mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal); |
| 2967 | } |
| 2968 | |
| 2969 | return 0; |
| 2970 | } |
| 2971 | |
| 2972 | /* get coalescing for ethtools */ |
| 2973 | static int mvneta_ethtool_get_coalesce(struct net_device *dev, |
| 2974 | struct ethtool_coalesce *c) |
| 2975 | { |
| 2976 | struct mvneta_port *pp = netdev_priv(dev); |
| 2977 | |
| 2978 | c->rx_coalesce_usecs = pp->rxqs[0].time_coal; |
| 2979 | c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal; |
| 2980 | |
| 2981 | c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal; |
| 2982 | return 0; |
| 2983 | } |
| 2984 | |
| 2985 | |
| 2986 | static void mvneta_ethtool_get_drvinfo(struct net_device *dev, |
| 2987 | struct ethtool_drvinfo *drvinfo) |
| 2988 | { |
| 2989 | strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME, |
| 2990 | sizeof(drvinfo->driver)); |
| 2991 | strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION, |
| 2992 | sizeof(drvinfo->version)); |
| 2993 | strlcpy(drvinfo->bus_info, dev_name(&dev->dev), |
| 2994 | sizeof(drvinfo->bus_info)); |
| 2995 | } |
| 2996 | |
| 2997 | |
| 2998 | static void mvneta_ethtool_get_ringparam(struct net_device *netdev, |
| 2999 | struct ethtool_ringparam *ring) |
| 3000 | { |
| 3001 | struct mvneta_port *pp = netdev_priv(netdev); |
| 3002 | |
| 3003 | ring->rx_max_pending = MVNETA_MAX_RXD; |
| 3004 | ring->tx_max_pending = MVNETA_MAX_TXD; |
| 3005 | ring->rx_pending = pp->rx_ring_size; |
| 3006 | ring->tx_pending = pp->tx_ring_size; |
| 3007 | } |
| 3008 | |
| 3009 | static int mvneta_ethtool_set_ringparam(struct net_device *dev, |
| 3010 | struct ethtool_ringparam *ring) |
| 3011 | { |
| 3012 | struct mvneta_port *pp = netdev_priv(dev); |
| 3013 | |
| 3014 | if ((ring->rx_pending == 0) || (ring->tx_pending == 0)) |
| 3015 | return -EINVAL; |
| 3016 | pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ? |
| 3017 | ring->rx_pending : MVNETA_MAX_RXD; |
Ezequiel Garcia | 8eef5f9 | 2014-05-30 13:40:05 -0300 | [diff] [blame] | 3018 | |
| 3019 | pp->tx_ring_size = clamp_t(u16, ring->tx_pending, |
| 3020 | MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD); |
| 3021 | if (pp->tx_ring_size != ring->tx_pending) |
| 3022 | netdev_warn(dev, "TX queue size set to %u (requested %u)\n", |
| 3023 | pp->tx_ring_size, ring->tx_pending); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3024 | |
| 3025 | if (netif_running(dev)) { |
| 3026 | mvneta_stop(dev); |
| 3027 | if (mvneta_open(dev)) { |
| 3028 | netdev_err(dev, |
| 3029 | "error on opening device after ring param change\n"); |
| 3030 | return -ENOMEM; |
| 3031 | } |
| 3032 | } |
| 3033 | |
| 3034 | return 0; |
| 3035 | } |
| 3036 | |
Russell King | 9b0cdef | 2015-10-22 18:37:30 +0100 | [diff] [blame] | 3037 | static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset, |
| 3038 | u8 *data) |
| 3039 | { |
| 3040 | if (sset == ETH_SS_STATS) { |
| 3041 | int i; |
| 3042 | |
| 3043 | for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++) |
| 3044 | memcpy(data + i * ETH_GSTRING_LEN, |
| 3045 | mvneta_statistics[i].name, ETH_GSTRING_LEN); |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | static void mvneta_ethtool_update_stats(struct mvneta_port *pp) |
| 3050 | { |
| 3051 | const struct mvneta_statistic *s; |
| 3052 | void __iomem *base = pp->base; |
| 3053 | u32 high, low, val; |
| 3054 | int i; |
| 3055 | |
| 3056 | for (i = 0, s = mvneta_statistics; |
| 3057 | s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics); |
| 3058 | s++, i++) { |
| 3059 | val = 0; |
| 3060 | |
| 3061 | switch (s->type) { |
| 3062 | case T_REG_32: |
| 3063 | val = readl_relaxed(base + s->offset); |
| 3064 | break; |
| 3065 | case T_REG_64: |
| 3066 | /* Docs say to read low 32-bit then high */ |
| 3067 | low = readl_relaxed(base + s->offset); |
| 3068 | high = readl_relaxed(base + s->offset + 4); |
| 3069 | val = (u64)high << 32 | low; |
| 3070 | break; |
| 3071 | } |
| 3072 | |
| 3073 | pp->ethtool_stats[i] += val; |
| 3074 | } |
| 3075 | } |
| 3076 | |
| 3077 | static void mvneta_ethtool_get_stats(struct net_device *dev, |
| 3078 | struct ethtool_stats *stats, u64 *data) |
| 3079 | { |
| 3080 | struct mvneta_port *pp = netdev_priv(dev); |
| 3081 | int i; |
| 3082 | |
| 3083 | mvneta_ethtool_update_stats(pp); |
| 3084 | |
| 3085 | for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++) |
| 3086 | *data++ = pp->ethtool_stats[i]; |
| 3087 | } |
| 3088 | |
| 3089 | static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset) |
| 3090 | { |
| 3091 | if (sset == ETH_SS_STATS) |
| 3092 | return ARRAY_SIZE(mvneta_statistics); |
| 3093 | return -EOPNOTSUPP; |
| 3094 | } |
| 3095 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3096 | static const struct net_device_ops mvneta_netdev_ops = { |
| 3097 | .ndo_open = mvneta_open, |
| 3098 | .ndo_stop = mvneta_stop, |
| 3099 | .ndo_start_xmit = mvneta_tx, |
| 3100 | .ndo_set_rx_mode = mvneta_set_rx_mode, |
| 3101 | .ndo_set_mac_address = mvneta_set_mac_addr, |
| 3102 | .ndo_change_mtu = mvneta_change_mtu, |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 3103 | .ndo_fix_features = mvneta_fix_features, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3104 | .ndo_get_stats64 = mvneta_get_stats64, |
Thomas Petazzoni | 15f5945 | 2013-09-04 16:26:52 +0200 | [diff] [blame] | 3105 | .ndo_do_ioctl = mvneta_ioctl, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3106 | }; |
| 3107 | |
| 3108 | const struct ethtool_ops mvneta_eth_tool_ops = { |
| 3109 | .get_link = ethtool_op_get_link, |
| 3110 | .get_settings = mvneta_ethtool_get_settings, |
| 3111 | .set_settings = mvneta_ethtool_set_settings, |
| 3112 | .set_coalesce = mvneta_ethtool_set_coalesce, |
| 3113 | .get_coalesce = mvneta_ethtool_get_coalesce, |
| 3114 | .get_drvinfo = mvneta_ethtool_get_drvinfo, |
| 3115 | .get_ringparam = mvneta_ethtool_get_ringparam, |
| 3116 | .set_ringparam = mvneta_ethtool_set_ringparam, |
Russell King | 9b0cdef | 2015-10-22 18:37:30 +0100 | [diff] [blame] | 3117 | .get_strings = mvneta_ethtool_get_strings, |
| 3118 | .get_ethtool_stats = mvneta_ethtool_get_stats, |
| 3119 | .get_sset_count = mvneta_ethtool_get_sset_count, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3120 | }; |
| 3121 | |
| 3122 | /* Initialize hw */ |
Ezequiel Garcia | 9672850 | 2014-05-22 20:06:59 -0300 | [diff] [blame] | 3123 | static int mvneta_init(struct device *dev, struct mvneta_port *pp) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3124 | { |
| 3125 | int queue; |
| 3126 | |
| 3127 | /* Disable port */ |
| 3128 | mvneta_port_disable(pp); |
| 3129 | |
| 3130 | /* Set port default values */ |
| 3131 | mvneta_defaults_set(pp); |
| 3132 | |
Ezequiel Garcia | 9672850 | 2014-05-22 20:06:59 -0300 | [diff] [blame] | 3133 | pp->txqs = devm_kcalloc(dev, txq_number, sizeof(struct mvneta_tx_queue), |
| 3134 | GFP_KERNEL); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3135 | if (!pp->txqs) |
| 3136 | return -ENOMEM; |
| 3137 | |
| 3138 | /* Initialize TX descriptor rings */ |
| 3139 | for (queue = 0; queue < txq_number; queue++) { |
| 3140 | struct mvneta_tx_queue *txq = &pp->txqs[queue]; |
| 3141 | txq->id = queue; |
| 3142 | txq->size = pp->tx_ring_size; |
| 3143 | txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS; |
| 3144 | } |
| 3145 | |
Ezequiel Garcia | 9672850 | 2014-05-22 20:06:59 -0300 | [diff] [blame] | 3146 | pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(struct mvneta_rx_queue), |
| 3147 | GFP_KERNEL); |
| 3148 | if (!pp->rxqs) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3149 | return -ENOMEM; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3150 | |
| 3151 | /* Create Rx descriptor rings */ |
| 3152 | for (queue = 0; queue < rxq_number; queue++) { |
| 3153 | struct mvneta_rx_queue *rxq = &pp->rxqs[queue]; |
| 3154 | rxq->id = queue; |
| 3155 | rxq->size = pp->rx_ring_size; |
| 3156 | rxq->pkts_coal = MVNETA_RX_COAL_PKTS; |
| 3157 | rxq->time_coal = MVNETA_RX_COAL_USEC; |
| 3158 | } |
| 3159 | |
| 3160 | return 0; |
| 3161 | } |
| 3162 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3163 | /* platform glue : initialize decoding windows */ |
Greg KH | 03ce758 | 2012-12-21 13:42:15 +0000 | [diff] [blame] | 3164 | static void mvneta_conf_mbus_windows(struct mvneta_port *pp, |
| 3165 | const struct mbus_dram_target_info *dram) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3166 | { |
| 3167 | u32 win_enable; |
| 3168 | u32 win_protect; |
| 3169 | int i; |
| 3170 | |
| 3171 | for (i = 0; i < 6; i++) { |
| 3172 | mvreg_write(pp, MVNETA_WIN_BASE(i), 0); |
| 3173 | mvreg_write(pp, MVNETA_WIN_SIZE(i), 0); |
| 3174 | |
| 3175 | if (i < 4) |
| 3176 | mvreg_write(pp, MVNETA_WIN_REMAP(i), 0); |
| 3177 | } |
| 3178 | |
| 3179 | win_enable = 0x3f; |
| 3180 | win_protect = 0; |
| 3181 | |
| 3182 | for (i = 0; i < dram->num_cs; i++) { |
| 3183 | const struct mbus_dram_window *cs = dram->cs + i; |
| 3184 | mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) | |
| 3185 | (cs->mbus_attr << 8) | dram->mbus_dram_target_id); |
| 3186 | |
| 3187 | mvreg_write(pp, MVNETA_WIN_SIZE(i), |
| 3188 | (cs->size - 1) & 0xffff0000); |
| 3189 | |
| 3190 | win_enable &= ~(1 << i); |
| 3191 | win_protect |= 3 << (2 * i); |
| 3192 | } |
| 3193 | |
| 3194 | mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable); |
Marcin Wojtas | db6ba9a | 2015-11-30 13:27:41 +0100 | [diff] [blame] | 3195 | mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | /* Power up the port */ |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 3199 | static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3200 | { |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 3201 | u32 ctrl; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3202 | |
| 3203 | /* MAC Cause register should be cleared */ |
| 3204 | mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0); |
| 3205 | |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 3206 | ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3207 | |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 3208 | /* Even though it might look weird, when we're configured in |
| 3209 | * SGMII or QSGMII mode, the RGMII bit needs to be set. |
| 3210 | */ |
| 3211 | switch(phy_mode) { |
| 3212 | case PHY_INTERFACE_MODE_QSGMII: |
| 3213 | mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO); |
| 3214 | ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII; |
| 3215 | break; |
| 3216 | case PHY_INTERFACE_MODE_SGMII: |
| 3217 | mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO); |
| 3218 | ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII; |
| 3219 | break; |
| 3220 | case PHY_INTERFACE_MODE_RGMII: |
| 3221 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 3222 | ctrl |= MVNETA_GMAC2_PORT_RGMII; |
| 3223 | break; |
| 3224 | default: |
| 3225 | return -EINVAL; |
| 3226 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3227 | |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 3228 | if (pp->use_inband_status) |
| 3229 | ctrl |= MVNETA_GMAC2_INBAND_AN_ENABLE; |
| 3230 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3231 | /* Cancel Port Reset */ |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 3232 | ctrl &= ~MVNETA_GMAC2_PORT_RESET; |
| 3233 | mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3234 | |
| 3235 | while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) & |
| 3236 | MVNETA_GMAC2_PORT_RESET) != 0) |
| 3237 | continue; |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 3238 | |
| 3239 | return 0; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | /* Device initialization routine */ |
Greg KH | 03ce758 | 2012-12-21 13:42:15 +0000 | [diff] [blame] | 3243 | static int mvneta_probe(struct platform_device *pdev) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3244 | { |
| 3245 | const struct mbus_dram_target_info *dram_target_info; |
Thomas Petazzoni | c3f0dd3 | 2014-03-27 11:39:29 +0100 | [diff] [blame] | 3246 | struct resource *res; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3247 | struct device_node *dn = pdev->dev.of_node; |
| 3248 | struct device_node *phy_node; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3249 | struct mvneta_port *pp; |
| 3250 | struct net_device *dev; |
Thomas Petazzoni | 8cc3e43 | 2013-06-04 04:52:23 +0000 | [diff] [blame] | 3251 | const char *dt_mac_addr; |
| 3252 | char hw_mac_addr[ETH_ALEN]; |
| 3253 | const char *mac_from; |
Stas Sergeev | f8af8e6 | 2015-07-20 17:49:58 -0700 | [diff] [blame] | 3254 | const char *managed; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3255 | int phy_mode; |
| 3256 | int err; |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 3257 | int cpu; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3258 | |
Willy Tarreau | ee40a11 | 2013-04-11 23:00:37 +0200 | [diff] [blame] | 3259 | dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3260 | if (!dev) |
| 3261 | return -ENOMEM; |
| 3262 | |
| 3263 | dev->irq = irq_of_parse_and_map(dn, 0); |
| 3264 | if (dev->irq == 0) { |
| 3265 | err = -EINVAL; |
| 3266 | goto err_free_netdev; |
| 3267 | } |
| 3268 | |
| 3269 | phy_node = of_parse_phandle(dn, "phy", 0); |
| 3270 | if (!phy_node) { |
Thomas Petazzoni | 83895be | 2014-05-16 16:14:06 +0200 | [diff] [blame] | 3271 | if (!of_phy_is_fixed_link(dn)) { |
| 3272 | dev_err(&pdev->dev, "no PHY specified\n"); |
| 3273 | err = -ENODEV; |
| 3274 | goto err_free_irq; |
| 3275 | } |
| 3276 | |
| 3277 | err = of_phy_register_fixed_link(dn); |
| 3278 | if (err < 0) { |
| 3279 | dev_err(&pdev->dev, "cannot register fixed PHY\n"); |
| 3280 | goto err_free_irq; |
| 3281 | } |
| 3282 | |
| 3283 | /* In the case of a fixed PHY, the DT node associated |
| 3284 | * to the PHY is the Ethernet MAC DT node. |
| 3285 | */ |
Uwe Kleine-König | c891c24 | 2014-08-07 21:58:46 +0200 | [diff] [blame] | 3286 | phy_node = of_node_get(dn); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3287 | } |
| 3288 | |
| 3289 | phy_mode = of_get_phy_mode(dn); |
| 3290 | if (phy_mode < 0) { |
| 3291 | dev_err(&pdev->dev, "incorrect phy-mode\n"); |
| 3292 | err = -EINVAL; |
Uwe Kleine-König | c891c24 | 2014-08-07 21:58:46 +0200 | [diff] [blame] | 3293 | goto err_put_phy_node; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3294 | } |
| 3295 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3296 | dev->tx_queue_len = MVNETA_MAX_TXD; |
| 3297 | dev->watchdog_timeo = 5 * HZ; |
| 3298 | dev->netdev_ops = &mvneta_netdev_ops; |
| 3299 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 3300 | dev->ethtool_ops = &mvneta_eth_tool_ops; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3301 | |
| 3302 | pp = netdev_priv(dev); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3303 | pp->phy_node = phy_node; |
| 3304 | pp->phy_interface = phy_mode; |
Stas Sergeev | f8af8e6 | 2015-07-20 17:49:58 -0700 | [diff] [blame] | 3305 | |
| 3306 | err = of_property_read_string(dn, "managed", &managed); |
| 3307 | pp->use_inband_status = (err == 0 && |
| 3308 | strcmp(managed, "in-band-status") == 0); |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 3309 | pp->cpu_notifier.notifier_call = mvneta_percpu_notifier; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3310 | |
Thomas Petazzoni | 189dd62 | 2012-11-19 14:15:25 +0100 | [diff] [blame] | 3311 | pp->clk = devm_clk_get(&pdev->dev, NULL); |
| 3312 | if (IS_ERR(pp->clk)) { |
| 3313 | err = PTR_ERR(pp->clk); |
Uwe Kleine-König | c891c24 | 2014-08-07 21:58:46 +0200 | [diff] [blame] | 3314 | goto err_put_phy_node; |
Thomas Petazzoni | 189dd62 | 2012-11-19 14:15:25 +0100 | [diff] [blame] | 3315 | } |
| 3316 | |
| 3317 | clk_prepare_enable(pp->clk); |
| 3318 | |
Thomas Petazzoni | c3f0dd3 | 2014-03-27 11:39:29 +0100 | [diff] [blame] | 3319 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 3320 | pp->base = devm_ioremap_resource(&pdev->dev, res); |
| 3321 | if (IS_ERR(pp->base)) { |
| 3322 | err = PTR_ERR(pp->base); |
Arnaud Patard \(Rtp\) | 5445eaf | 2013-07-29 21:56:48 +0200 | [diff] [blame] | 3323 | goto err_clk; |
| 3324 | } |
| 3325 | |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 3326 | /* Alloc per-cpu port structure */ |
| 3327 | pp->ports = alloc_percpu(struct mvneta_pcpu_port); |
| 3328 | if (!pp->ports) { |
| 3329 | err = -ENOMEM; |
| 3330 | goto err_clk; |
| 3331 | } |
| 3332 | |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 3333 | /* Alloc per-cpu stats */ |
WANG Cong | 1c213bd | 2014-02-13 11:46:28 -0800 | [diff] [blame] | 3334 | pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats); |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 3335 | if (!pp->stats) { |
| 3336 | err = -ENOMEM; |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 3337 | goto err_free_ports; |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 3338 | } |
| 3339 | |
Thomas Petazzoni | 8cc3e43 | 2013-06-04 04:52:23 +0000 | [diff] [blame] | 3340 | dt_mac_addr = of_get_mac_address(dn); |
Luka Perkov | 6c7a9a3 | 2013-10-30 00:10:01 +0100 | [diff] [blame] | 3341 | if (dt_mac_addr) { |
Thomas Petazzoni | 8cc3e43 | 2013-06-04 04:52:23 +0000 | [diff] [blame] | 3342 | mac_from = "device tree"; |
| 3343 | memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN); |
| 3344 | } else { |
| 3345 | mvneta_get_mac_addr(pp, hw_mac_addr); |
| 3346 | if (is_valid_ether_addr(hw_mac_addr)) { |
| 3347 | mac_from = "hardware"; |
| 3348 | memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN); |
| 3349 | } else { |
| 3350 | mac_from = "random"; |
| 3351 | eth_hw_addr_random(dev); |
| 3352 | } |
| 3353 | } |
| 3354 | |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 3355 | if (of_device_is_compatible(dn, "marvell,armada-370-neta")) |
| 3356 | pp->tx_csum_limit = 1600; |
| 3357 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3358 | pp->tx_ring_size = MVNETA_MAX_TXD; |
| 3359 | pp->rx_ring_size = MVNETA_MAX_RXD; |
| 3360 | |
| 3361 | pp->dev = dev; |
| 3362 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 3363 | |
Ezequiel Garcia | 9672850 | 2014-05-22 20:06:59 -0300 | [diff] [blame] | 3364 | err = mvneta_init(&pdev->dev, pp); |
| 3365 | if (err < 0) |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 3366 | goto err_free_stats; |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 3367 | |
| 3368 | err = mvneta_port_power_up(pp, phy_mode); |
| 3369 | if (err < 0) { |
| 3370 | dev_err(&pdev->dev, "can't power up port\n"); |
Ezequiel Garcia | 9672850 | 2014-05-22 20:06:59 -0300 | [diff] [blame] | 3371 | goto err_free_stats; |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 3372 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3373 | |
| 3374 | dram_target_info = mv_mbus_dram_info(); |
| 3375 | if (dram_target_info) |
| 3376 | mvneta_conf_mbus_windows(pp, dram_target_info); |
| 3377 | |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 3378 | for_each_present_cpu(cpu) { |
| 3379 | struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu); |
| 3380 | |
| 3381 | netif_napi_add(dev, &port->napi, mvneta_poll, NAPI_POLL_WEIGHT); |
| 3382 | port->pp = pp; |
| 3383 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3384 | |
Ezequiel Garcia | 2adb719 | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 3385 | dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO; |
Ezequiel Garcia | 01ef26c | 2014-05-19 13:59:53 -0300 | [diff] [blame] | 3386 | dev->hw_features |= dev->features; |
| 3387 | dev->vlan_features |= dev->features; |
willy tarreau | b50b72d | 2013-04-06 08:47:01 +0000 | [diff] [blame] | 3388 | dev->priv_flags |= IFF_UNICAST_FLT; |
Ezequiel Garcia | 8eef5f9 | 2014-05-30 13:40:05 -0300 | [diff] [blame] | 3389 | dev->gso_max_segs = MVNETA_MAX_TSO_SEGS; |
willy tarreau | b50b72d | 2013-04-06 08:47:01 +0000 | [diff] [blame] | 3390 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3391 | err = register_netdev(dev); |
| 3392 | if (err < 0) { |
| 3393 | dev_err(&pdev->dev, "failed to register\n"); |
Ezequiel Garcia | 9672850 | 2014-05-22 20:06:59 -0300 | [diff] [blame] | 3394 | goto err_free_stats; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3395 | } |
| 3396 | |
Thomas Petazzoni | 8cc3e43 | 2013-06-04 04:52:23 +0000 | [diff] [blame] | 3397 | netdev_info(dev, "Using %s mac address %pM\n", mac_from, |
| 3398 | dev->dev_addr); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3399 | |
| 3400 | platform_set_drvdata(pdev, pp->dev); |
| 3401 | |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 3402 | if (pp->use_inband_status) { |
| 3403 | struct phy_device *phy = of_phy_find_device(dn); |
| 3404 | |
| 3405 | mvneta_fixed_link_update(pp, phy); |
Russell King | 04d53b2 | 2015-09-24 20:36:18 +0100 | [diff] [blame] | 3406 | |
| 3407 | put_device(&phy->dev); |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 3408 | } |
| 3409 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3410 | return 0; |
| 3411 | |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 3412 | err_free_stats: |
| 3413 | free_percpu(pp->stats); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 3414 | err_free_ports: |
| 3415 | free_percpu(pp->ports); |
Arnaud Patard \(Rtp\) | 5445eaf | 2013-07-29 21:56:48 +0200 | [diff] [blame] | 3416 | err_clk: |
| 3417 | clk_disable_unprepare(pp->clk); |
Uwe Kleine-König | c891c24 | 2014-08-07 21:58:46 +0200 | [diff] [blame] | 3418 | err_put_phy_node: |
| 3419 | of_node_put(phy_node); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3420 | err_free_irq: |
| 3421 | irq_dispose_mapping(dev->irq); |
| 3422 | err_free_netdev: |
| 3423 | free_netdev(dev); |
| 3424 | return err; |
| 3425 | } |
| 3426 | |
| 3427 | /* Device removal routine */ |
Greg KH | 03ce758 | 2012-12-21 13:42:15 +0000 | [diff] [blame] | 3428 | static int mvneta_remove(struct platform_device *pdev) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3429 | { |
| 3430 | struct net_device *dev = platform_get_drvdata(pdev); |
| 3431 | struct mvneta_port *pp = netdev_priv(dev); |
| 3432 | |
| 3433 | unregister_netdev(dev); |
Thomas Petazzoni | 189dd62 | 2012-11-19 14:15:25 +0100 | [diff] [blame] | 3434 | clk_disable_unprepare(pp->clk); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 3435 | free_percpu(pp->ports); |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 3436 | free_percpu(pp->stats); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3437 | irq_dispose_mapping(dev->irq); |
Uwe Kleine-König | c891c24 | 2014-08-07 21:58:46 +0200 | [diff] [blame] | 3438 | of_node_put(pp->phy_node); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3439 | free_netdev(dev); |
| 3440 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3441 | return 0; |
| 3442 | } |
| 3443 | |
| 3444 | static const struct of_device_id mvneta_match[] = { |
| 3445 | { .compatible = "marvell,armada-370-neta" }, |
Simon Guinot | f522a97 | 2015-06-30 16:20:20 +0200 | [diff] [blame] | 3446 | { .compatible = "marvell,armada-xp-neta" }, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3447 | { } |
| 3448 | }; |
| 3449 | MODULE_DEVICE_TABLE(of, mvneta_match); |
| 3450 | |
| 3451 | static struct platform_driver mvneta_driver = { |
| 3452 | .probe = mvneta_probe, |
Greg KH | 03ce758 | 2012-12-21 13:42:15 +0000 | [diff] [blame] | 3453 | .remove = mvneta_remove, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3454 | .driver = { |
| 3455 | .name = MVNETA_DRIVER_NAME, |
| 3456 | .of_match_table = mvneta_match, |
| 3457 | }, |
| 3458 | }; |
| 3459 | |
| 3460 | module_platform_driver(mvneta_driver); |
| 3461 | |
| 3462 | MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com"); |
| 3463 | MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>"); |
| 3464 | MODULE_LICENSE("GPL"); |
| 3465 | |
| 3466 | module_param(rxq_number, int, S_IRUGO); |
| 3467 | module_param(txq_number, int, S_IRUGO); |
| 3468 | |
| 3469 | module_param(rxq_def, int, S_IRUGO); |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 3470 | module_param(rx_copybreak, int, S_IRUGO | S_IWUSR); |