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 | |
Jisheng Zhang | 0e03f56 | 2016-01-20 19:27:22 +0800 | [diff] [blame] | 14 | #include <linux/clk.h> |
| 15 | #include <linux/cpu.h> |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 16 | #include <linux/etherdevice.h> |
Jisheng Zhang | 0e03f56 | 2016-01-20 19:27:22 +0800 | [diff] [blame] | 17 | #include <linux/if_vlan.h> |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 18 | #include <linux/inetdevice.h> |
Jisheng Zhang | 0e03f56 | 2016-01-20 19:27:22 +0800 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/kernel.h> |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 22 | #include <linux/mbus.h> |
| 23 | #include <linux/module.h> |
Jisheng Zhang | 0e03f56 | 2016-01-20 19:27:22 +0800 | [diff] [blame] | 24 | #include <linux/netdevice.h> |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 25 | #include <linux/of.h> |
Jisheng Zhang | 0e03f56 | 2016-01-20 19:27:22 +0800 | [diff] [blame] | 26 | #include <linux/of_address.h> |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 27 | #include <linux/of_irq.h> |
| 28 | #include <linux/of_mdio.h> |
| 29 | #include <linux/of_net.h> |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 30 | #include <linux/phy.h> |
Jisheng Zhang | 0e03f56 | 2016-01-20 19:27:22 +0800 | [diff] [blame] | 31 | #include <linux/platform_device.h> |
| 32 | #include <linux/skbuff.h> |
Gregory CLEMENT | baa11eb | 2016-03-14 09:39:05 +0100 | [diff] [blame] | 33 | #include <net/hwbm.h> |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 34 | #include "mvneta_bm.h" |
Jisheng Zhang | 0e03f56 | 2016-01-20 19:27:22 +0800 | [diff] [blame] | 35 | #include <net/ip.h> |
| 36 | #include <net/ipv6.h> |
| 37 | #include <net/tso.h> |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 38 | |
| 39 | /* Registers */ |
| 40 | #define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2)) |
Marcin Wojtas | e5bdf68 | 2015-11-30 13:27:42 +0100 | [diff] [blame] | 41 | #define MVNETA_RXQ_HW_BUF_ALLOC BIT(0) |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 42 | #define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4 |
| 43 | #define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30 |
| 44 | #define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6 |
| 45 | #define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 46 | #define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8) |
| 47 | #define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8) |
| 48 | #define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2)) |
| 49 | #define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16) |
| 50 | #define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2)) |
| 51 | #define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2)) |
| 52 | #define MVNETA_RXQ_BUF_SIZE_SHIFT 19 |
| 53 | #define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19) |
| 54 | #define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2)) |
| 55 | #define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff |
| 56 | #define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2)) |
| 57 | #define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16 |
| 58 | #define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255 |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 59 | #define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2)) |
| 60 | #define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3 |
| 61 | #define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 62 | #define MVNETA_PORT_RX_RESET 0x1cc0 |
| 63 | #define MVNETA_PORT_RX_DMA_RESET BIT(0) |
| 64 | #define MVNETA_PHY_ADDR 0x2000 |
| 65 | #define MVNETA_PHY_ADDR_MASK 0x1f |
| 66 | #define MVNETA_MBUS_RETRY 0x2010 |
| 67 | #define MVNETA_UNIT_INTR_CAUSE 0x2080 |
| 68 | #define MVNETA_UNIT_CONTROL 0x20B0 |
| 69 | #define MVNETA_PHY_POLLING_ENABLE BIT(1) |
| 70 | #define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3)) |
| 71 | #define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3)) |
| 72 | #define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2)) |
| 73 | #define MVNETA_BASE_ADDR_ENABLE 0x2290 |
Marcin Wojtas | db6ba9a | 2015-11-30 13:27:41 +0100 | [diff] [blame] | 74 | #define MVNETA_ACCESS_PROTECT_ENABLE 0x2294 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 75 | #define MVNETA_PORT_CONFIG 0x2400 |
| 76 | #define MVNETA_UNI_PROMISC_MODE BIT(0) |
| 77 | #define MVNETA_DEF_RXQ(q) ((q) << 1) |
| 78 | #define MVNETA_DEF_RXQ_ARP(q) ((q) << 4) |
| 79 | #define MVNETA_TX_UNSET_ERR_SUM BIT(12) |
| 80 | #define MVNETA_DEF_RXQ_TCP(q) ((q) << 16) |
| 81 | #define MVNETA_DEF_RXQ_UDP(q) ((q) << 19) |
| 82 | #define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22) |
| 83 | #define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25) |
| 84 | #define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \ |
| 85 | MVNETA_DEF_RXQ_ARP(q) | \ |
| 86 | MVNETA_DEF_RXQ_TCP(q) | \ |
| 87 | MVNETA_DEF_RXQ_UDP(q) | \ |
| 88 | MVNETA_DEF_RXQ_BPDU(q) | \ |
| 89 | MVNETA_TX_UNSET_ERR_SUM | \ |
| 90 | MVNETA_RX_CSUM_WITH_PSEUDO_HDR) |
| 91 | #define MVNETA_PORT_CONFIG_EXTEND 0x2404 |
| 92 | #define MVNETA_MAC_ADDR_LOW 0x2414 |
| 93 | #define MVNETA_MAC_ADDR_HIGH 0x2418 |
| 94 | #define MVNETA_SDMA_CONFIG 0x241c |
| 95 | #define MVNETA_SDMA_BRST_SIZE_16 4 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 96 | #define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1) |
| 97 | #define MVNETA_RX_NO_DATA_SWAP BIT(4) |
| 98 | #define MVNETA_TX_NO_DATA_SWAP BIT(5) |
Thomas Petazzoni | 9ad8fef | 2013-07-29 15:21:28 +0200 | [diff] [blame] | 99 | #define MVNETA_DESC_SWAP BIT(6) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 100 | #define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22) |
| 101 | #define MVNETA_PORT_STATUS 0x2444 |
| 102 | #define MVNETA_TX_IN_PRGRS BIT(1) |
| 103 | #define MVNETA_TX_FIFO_EMPTY BIT(8) |
| 104 | #define MVNETA_RX_MIN_FRAME_SIZE 0x247c |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 105 | #define MVNETA_SERDES_CFG 0x24A0 |
Arnaud Patard \(Rtp\) | 5445eaf | 2013-07-29 21:56:48 +0200 | [diff] [blame] | 106 | #define MVNETA_SGMII_SERDES_PROTO 0x0cc7 |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 107 | #define MVNETA_QSGMII_SERDES_PROTO 0x0667 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 108 | #define MVNETA_TYPE_PRIO 0x24bc |
| 109 | #define MVNETA_FORCE_UNI BIT(21) |
| 110 | #define MVNETA_TXQ_CMD_1 0x24e4 |
| 111 | #define MVNETA_TXQ_CMD 0x2448 |
| 112 | #define MVNETA_TXQ_DISABLE_SHIFT 8 |
| 113 | #define MVNETA_TXQ_ENABLE_MASK 0x000000ff |
Andrew Lunn | e483911 | 2015-10-22 18:37:36 +0100 | [diff] [blame] | 114 | #define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484 |
| 115 | #define MVNETA_OVERRUN_FRAME_COUNT 0x2488 |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 116 | #define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4 |
| 117 | #define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 118 | #define MVNETA_ACC_MODE 0x2500 |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 119 | #define MVNETA_BM_ADDRESS 0x2504 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 120 | #define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2)) |
| 121 | #define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff |
| 122 | #define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00 |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 123 | #define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq) |
Gregory CLEMENT | 50bf8cb | 2015-12-09 18:23:51 +0100 | [diff] [blame] | 124 | #define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 125 | #define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2)) |
willy tarreau | 40ba35e | 2014-01-16 08:20:10 +0100 | [diff] [blame] | 126 | |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 127 | /* Exception Interrupt Port/Queue Cause register |
| 128 | * |
| 129 | * Their behavior depend of the mapping done using the PCPX2Q |
| 130 | * registers. For a given CPU if the bit associated to a queue is not |
| 131 | * set, then for the register a read from this CPU will always return |
| 132 | * 0 and a write won't do anything |
| 133 | */ |
willy tarreau | 40ba35e | 2014-01-16 08:20:10 +0100 | [diff] [blame] | 134 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 135 | #define MVNETA_INTR_NEW_CAUSE 0x25a0 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 136 | #define MVNETA_INTR_NEW_MASK 0x25a4 |
willy tarreau | 40ba35e | 2014-01-16 08:20:10 +0100 | [diff] [blame] | 137 | |
| 138 | /* bits 0..7 = TXQ SENT, one bit per queue. |
| 139 | * bits 8..15 = RXQ OCCUP, one bit per queue. |
| 140 | * bits 16..23 = RXQ FREE, one bit per queue. |
| 141 | * bit 29 = OLD_REG_SUM, see old reg ? |
| 142 | * bit 30 = TX_ERR_SUM, one bit for 4 ports |
| 143 | * bit 31 = MISC_SUM, one bit for 4 ports |
| 144 | */ |
| 145 | #define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0) |
| 146 | #define MVNETA_TX_INTR_MASK_ALL (0xff << 0) |
| 147 | #define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8) |
| 148 | #define MVNETA_RX_INTR_MASK_ALL (0xff << 8) |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 149 | #define MVNETA_MISCINTR_INTR_MASK BIT(31) |
willy tarreau | 40ba35e | 2014-01-16 08:20:10 +0100 | [diff] [blame] | 150 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 151 | #define MVNETA_INTR_OLD_CAUSE 0x25a8 |
| 152 | #define MVNETA_INTR_OLD_MASK 0x25ac |
willy tarreau | 40ba35e | 2014-01-16 08:20:10 +0100 | [diff] [blame] | 153 | |
| 154 | /* Data Path Port/Queue Cause Register */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 155 | #define MVNETA_INTR_MISC_CAUSE 0x25b0 |
| 156 | #define MVNETA_INTR_MISC_MASK 0x25b4 |
willy tarreau | 40ba35e | 2014-01-16 08:20:10 +0100 | [diff] [blame] | 157 | |
| 158 | #define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0) |
| 159 | #define MVNETA_CAUSE_LINK_CHANGE BIT(1) |
| 160 | #define MVNETA_CAUSE_PTP BIT(4) |
| 161 | |
| 162 | #define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7) |
| 163 | #define MVNETA_CAUSE_RX_OVERRUN BIT(8) |
| 164 | #define MVNETA_CAUSE_RX_CRC_ERROR BIT(9) |
| 165 | #define MVNETA_CAUSE_RX_LARGE_PKT BIT(10) |
| 166 | #define MVNETA_CAUSE_TX_UNDERUN BIT(11) |
| 167 | #define MVNETA_CAUSE_PRBS_ERR BIT(12) |
| 168 | #define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13) |
| 169 | #define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14) |
| 170 | |
| 171 | #define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16 |
| 172 | #define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT) |
| 173 | #define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool))) |
| 174 | |
| 175 | #define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24 |
| 176 | #define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT) |
| 177 | #define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q))) |
| 178 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 179 | #define MVNETA_INTR_ENABLE 0x25b8 |
| 180 | #define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00 |
Marcin Wojtas | dc1aadf | 2015-11-30 13:27:43 +0100 | [diff] [blame] | 181 | #define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff |
willy tarreau | 40ba35e | 2014-01-16 08:20:10 +0100 | [diff] [blame] | 182 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 183 | #define MVNETA_RXQ_CMD 0x2680 |
| 184 | #define MVNETA_RXQ_DISABLE_SHIFT 8 |
| 185 | #define MVNETA_RXQ_ENABLE_MASK 0x000000ff |
| 186 | #define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4)) |
| 187 | #define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4)) |
| 188 | #define MVNETA_GMAC_CTRL_0 0x2c00 |
| 189 | #define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2 |
| 190 | #define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc |
| 191 | #define MVNETA_GMAC0_PORT_ENABLE BIT(0) |
| 192 | #define MVNETA_GMAC_CTRL_2 0x2c08 |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 193 | #define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0) |
Thomas Petazzoni | a79121d | 2014-03-26 00:25:41 +0100 | [diff] [blame] | 194 | #define MVNETA_GMAC2_PCS_ENABLE BIT(3) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 195 | #define MVNETA_GMAC2_PORT_RGMII BIT(4) |
| 196 | #define MVNETA_GMAC2_PORT_RESET BIT(6) |
| 197 | #define MVNETA_GMAC_STATUS 0x2c10 |
| 198 | #define MVNETA_GMAC_LINK_UP BIT(0) |
| 199 | #define MVNETA_GMAC_SPEED_1000 BIT(1) |
| 200 | #define MVNETA_GMAC_SPEED_100 BIT(2) |
| 201 | #define MVNETA_GMAC_FULL_DUPLEX BIT(3) |
| 202 | #define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4) |
| 203 | #define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5) |
| 204 | #define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6) |
| 205 | #define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7) |
| 206 | #define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c |
| 207 | #define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0) |
| 208 | #define MVNETA_GMAC_FORCE_LINK_PASS BIT(1) |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 209 | #define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 210 | #define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5) |
| 211 | #define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6) |
Thomas Petazzoni | 7140860 | 2013-09-04 16:21:18 +0200 | [diff] [blame] | 212 | #define MVNETA_GMAC_AN_SPEED_EN BIT(7) |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 213 | #define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 214 | #define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12) |
Thomas Petazzoni | 7140860 | 2013-09-04 16:21:18 +0200 | [diff] [blame] | 215 | #define MVNETA_GMAC_AN_DUPLEX_EN BIT(13) |
Andrew Lunn | e483911 | 2015-10-22 18:37:36 +0100 | [diff] [blame] | 216 | #define MVNETA_MIB_COUNTERS_BASE 0x3000 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 217 | #define MVNETA_MIB_LATE_COLLISION 0x7c |
| 218 | #define MVNETA_DA_FILT_SPEC_MCAST 0x3400 |
| 219 | #define MVNETA_DA_FILT_OTH_MCAST 0x3500 |
| 220 | #define MVNETA_DA_FILT_UCAST_BASE 0x3600 |
| 221 | #define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2)) |
| 222 | #define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2)) |
| 223 | #define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000 |
| 224 | #define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16) |
| 225 | #define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2)) |
| 226 | #define MVNETA_TXQ_DEC_SENT_SHIFT 16 |
| 227 | #define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2)) |
| 228 | #define MVNETA_TXQ_SENT_DESC_SHIFT 16 |
| 229 | #define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000 |
| 230 | #define MVNETA_PORT_TX_RESET 0x3cf0 |
| 231 | #define MVNETA_PORT_TX_DMA_RESET BIT(0) |
| 232 | #define MVNETA_TX_MTU 0x3e0c |
| 233 | #define MVNETA_TX_TOKEN_SIZE 0x3e14 |
| 234 | #define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff |
| 235 | #define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2)) |
| 236 | #define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff |
| 237 | |
| 238 | #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff |
| 239 | |
| 240 | /* Descriptor ring Macros */ |
| 241 | #define MVNETA_QUEUE_NEXT_DESC(q, index) \ |
| 242 | (((index) < (q)->last_desc) ? ((index) + 1) : 0) |
| 243 | |
| 244 | /* Various constants */ |
| 245 | |
| 246 | /* Coalescing */ |
Dmitri Epshtein | 06708f8 | 2016-07-06 04:18:58 +0200 | [diff] [blame] | 247 | #define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 248 | #define MVNETA_RX_COAL_PKTS 32 |
| 249 | #define MVNETA_RX_COAL_USEC 100 |
| 250 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 251 | /* The two bytes Marvell header. Either contains a special value used |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 252 | * by Marvell switches when a specific hardware mode is enabled (not |
| 253 | * supported by this driver) or is filled automatically by zeroes on |
| 254 | * the RX side. Those two bytes being at the front of the Ethernet |
| 255 | * header, they allow to have the IP header aligned on a 4 bytes |
| 256 | * boundary automatically: the hardware skips those two bytes on its |
| 257 | * own. |
| 258 | */ |
| 259 | #define MVNETA_MH_SIZE 2 |
| 260 | |
| 261 | #define MVNETA_VLAN_TAG_LEN 4 |
| 262 | |
Marcin Wojtas | 9110ee0 | 2015-11-30 13:27:45 +0100 | [diff] [blame] | 263 | #define MVNETA_TX_CSUM_DEF_SIZE 1600 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 264 | #define MVNETA_TX_CSUM_MAX_SIZE 9800 |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 265 | #define MVNETA_ACC_MODE_EXT1 1 |
| 266 | #define MVNETA_ACC_MODE_EXT2 2 |
| 267 | |
| 268 | #define MVNETA_MAX_DECODE_WIN 6 |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 269 | |
| 270 | /* Timeout constants */ |
| 271 | #define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000 |
| 272 | #define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000 |
| 273 | #define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000 |
| 274 | |
| 275 | #define MVNETA_TX_MTU_MAX 0x3ffff |
| 276 | |
Gregory CLEMENT | 9a401de | 2015-12-09 18:23:50 +0100 | [diff] [blame] | 277 | /* The RSS lookup table actually has 256 entries but we do not use |
| 278 | * them yet |
| 279 | */ |
| 280 | #define MVNETA_RSS_LU_TABLE_SIZE 1 |
| 281 | |
Ezequiel Garcia | 2adb719d | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 282 | /* TSO header size */ |
| 283 | #define TSO_HEADER_SIZE 128 |
| 284 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 285 | /* Max number of Rx descriptors */ |
| 286 | #define MVNETA_MAX_RXD 128 |
| 287 | |
| 288 | /* Max number of Tx descriptors */ |
| 289 | #define MVNETA_MAX_TXD 532 |
| 290 | |
Ezequiel Garcia | 8eef5f9 | 2014-05-30 13:40:05 -0300 | [diff] [blame] | 291 | /* Max number of allowed TCP segments for software TSO */ |
| 292 | #define MVNETA_MAX_TSO_SEGS 100 |
| 293 | |
| 294 | #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS) |
| 295 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 296 | /* descriptor aligned size */ |
| 297 | #define MVNETA_DESC_ALIGNED_SIZE 32 |
| 298 | |
Marcin Wojtas | 8d5047c | 2016-12-01 18:03:07 +0100 | [diff] [blame] | 299 | /* Number of bytes to be taken into account by HW when putting incoming data |
| 300 | * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet |
| 301 | * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers. |
| 302 | */ |
| 303 | #define MVNETA_RX_PKT_OFFSET_CORRECTION 64 |
| 304 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 305 | #define MVNETA_RX_PKT_SIZE(mtu) \ |
| 306 | ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \ |
| 307 | ETH_HLEN + ETH_FCS_LEN, \ |
Jisheng Zhang | c66e98c | 2016-04-01 17:12:49 +0800 | [diff] [blame] | 308 | cache_line_size()) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 309 | |
Ezequiel Garcia | 2e3173a | 2014-05-30 13:40:07 -0300 | [diff] [blame] | 310 | #define IS_TSO_HEADER(txq, addr) \ |
| 311 | ((addr >= txq->tso_hdrs_phys) && \ |
| 312 | (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE)) |
| 313 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 314 | #define MVNETA_RX_GET_BM_POOL_ID(rxd) \ |
| 315 | (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 316 | |
Russell King | 9b0cdef | 2015-10-22 18:37:30 +0100 | [diff] [blame] | 317 | struct mvneta_statistic { |
| 318 | unsigned short offset; |
| 319 | unsigned short type; |
| 320 | const char name[ETH_GSTRING_LEN]; |
| 321 | }; |
| 322 | |
| 323 | #define T_REG_32 32 |
| 324 | #define T_REG_64 64 |
| 325 | |
| 326 | static const struct mvneta_statistic mvneta_statistics[] = { |
| 327 | { 0x3000, T_REG_64, "good_octets_received", }, |
| 328 | { 0x3010, T_REG_32, "good_frames_received", }, |
| 329 | { 0x3008, T_REG_32, "bad_octets_received", }, |
| 330 | { 0x3014, T_REG_32, "bad_frames_received", }, |
| 331 | { 0x3018, T_REG_32, "broadcast_frames_received", }, |
| 332 | { 0x301c, T_REG_32, "multicast_frames_received", }, |
| 333 | { 0x3050, T_REG_32, "unrec_mac_control_received", }, |
| 334 | { 0x3058, T_REG_32, "good_fc_received", }, |
| 335 | { 0x305c, T_REG_32, "bad_fc_received", }, |
| 336 | { 0x3060, T_REG_32, "undersize_received", }, |
| 337 | { 0x3064, T_REG_32, "fragments_received", }, |
| 338 | { 0x3068, T_REG_32, "oversize_received", }, |
| 339 | { 0x306c, T_REG_32, "jabber_received", }, |
| 340 | { 0x3070, T_REG_32, "mac_receive_error", }, |
| 341 | { 0x3074, T_REG_32, "bad_crc_event", }, |
| 342 | { 0x3078, T_REG_32, "collision", }, |
| 343 | { 0x307c, T_REG_32, "late_collision", }, |
| 344 | { 0x2484, T_REG_32, "rx_discard", }, |
| 345 | { 0x2488, T_REG_32, "rx_overrun", }, |
| 346 | { 0x3020, T_REG_32, "frames_64_octets", }, |
| 347 | { 0x3024, T_REG_32, "frames_65_to_127_octets", }, |
| 348 | { 0x3028, T_REG_32, "frames_128_to_255_octets", }, |
| 349 | { 0x302c, T_REG_32, "frames_256_to_511_octets", }, |
| 350 | { 0x3030, T_REG_32, "frames_512_to_1023_octets", }, |
| 351 | { 0x3034, T_REG_32, "frames_1024_to_max_octets", }, |
| 352 | { 0x3038, T_REG_64, "good_octets_sent", }, |
| 353 | { 0x3040, T_REG_32, "good_frames_sent", }, |
| 354 | { 0x3044, T_REG_32, "excessive_collision", }, |
| 355 | { 0x3048, T_REG_32, "multicast_frames_sent", }, |
| 356 | { 0x304c, T_REG_32, "broadcast_frames_sent", }, |
| 357 | { 0x3054, T_REG_32, "fc_sent", }, |
| 358 | { 0x300c, T_REG_32, "internal_mac_transmit_err", }, |
| 359 | }; |
| 360 | |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 361 | struct mvneta_pcpu_stats { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 362 | struct u64_stats_sync syncp; |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 363 | u64 rx_packets; |
| 364 | u64 rx_bytes; |
| 365 | u64 tx_packets; |
| 366 | u64 tx_bytes; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 367 | }; |
| 368 | |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 369 | struct mvneta_pcpu_port { |
| 370 | /* Pointer to the shared port */ |
| 371 | struct mvneta_port *pp; |
| 372 | |
| 373 | /* Pointer to the CPU-local NAPI struct */ |
| 374 | struct napi_struct napi; |
| 375 | |
| 376 | /* Cause of the previous interrupt */ |
| 377 | u32 cause_rx_tx; |
| 378 | }; |
| 379 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 380 | struct mvneta_port { |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 381 | u8 id; |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 382 | struct mvneta_pcpu_port __percpu *ports; |
| 383 | struct mvneta_pcpu_stats __percpu *stats; |
| 384 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 385 | int pkt_size; |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 386 | unsigned int frag_size; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 387 | void __iomem *base; |
| 388 | struct mvneta_rx_queue *rxqs; |
| 389 | struct mvneta_tx_queue *txqs; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 390 | struct net_device *dev; |
Sebastian Andrzej Siewior | 84a3f4d | 2016-08-18 14:57:23 +0200 | [diff] [blame] | 391 | struct hlist_node node_online; |
| 392 | struct hlist_node node_dead; |
Gregory CLEMENT | 90b74c0 | 2015-12-09 18:23:48 +0100 | [diff] [blame] | 393 | int rxq_def; |
Gregory CLEMENT | 5888511 | 2016-02-04 22:09:28 +0100 | [diff] [blame] | 394 | /* Protect the access to the percpu interrupt registers, |
| 395 | * ensuring that the configuration remains coherent. |
| 396 | */ |
| 397 | spinlock_t lock; |
Gregory CLEMENT | 120cfa5 | 2016-02-04 22:09:29 +0100 | [diff] [blame] | 398 | bool is_stopped; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 399 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 400 | u32 cause_rx_tx; |
| 401 | struct napi_struct napi; |
| 402 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 403 | /* Core clock */ |
Thomas Petazzoni | 189dd62 | 2012-11-19 14:15:25 +0100 | [diff] [blame] | 404 | struct clk *clk; |
Jisheng Zhang | 15cc4a4 | 2016-01-20 19:27:24 +0800 | [diff] [blame] | 405 | /* AXI clock */ |
| 406 | struct clk *clk_bus; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 407 | u8 mcast_count[256]; |
| 408 | u16 tx_ring_size; |
| 409 | u16 rx_ring_size; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 410 | |
| 411 | struct mii_bus *mii_bus; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 412 | phy_interface_t phy_interface; |
| 413 | struct device_node *phy_node; |
| 414 | unsigned int link; |
| 415 | unsigned int duplex; |
| 416 | unsigned int speed; |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 417 | unsigned int tx_csum_limit; |
Stas Sergeev | 0c0744f | 2015-12-02 20:35:11 +0300 | [diff] [blame] | 418 | unsigned int use_inband_status:1; |
Russell King | 9b0cdef | 2015-10-22 18:37:30 +0100 | [diff] [blame] | 419 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 420 | struct mvneta_bm *bm_priv; |
| 421 | struct mvneta_bm_pool *pool_long; |
| 422 | struct mvneta_bm_pool *pool_short; |
| 423 | int bm_win_id; |
| 424 | |
Russell King | 9b0cdef | 2015-10-22 18:37:30 +0100 | [diff] [blame] | 425 | u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)]; |
Gregory CLEMENT | 9a401de | 2015-12-09 18:23:50 +0100 | [diff] [blame] | 426 | |
| 427 | u32 indir[MVNETA_RSS_LU_TABLE_SIZE]; |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 428 | |
| 429 | /* Flags for special SoC configurations */ |
| 430 | bool neta_armada3700; |
Marcin Wojtas | 8d5047c | 2016-12-01 18:03:07 +0100 | [diff] [blame] | 431 | u16 rx_offset_correction; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 432 | }; |
| 433 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 434 | /* The mvneta_tx_desc and mvneta_rx_desc structures describe the |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 435 | * layout of the transmit and reception DMA descriptors, and their |
| 436 | * layout is therefore defined by the hardware design |
| 437 | */ |
Thomas Petazzoni | 6083ed4 | 2013-07-29 15:21:27 +0200 | [diff] [blame] | 438 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 439 | #define MVNETA_TX_L3_OFF_SHIFT 0 |
| 440 | #define MVNETA_TX_IP_HLEN_SHIFT 8 |
| 441 | #define MVNETA_TX_L4_UDP BIT(16) |
| 442 | #define MVNETA_TX_L3_IP6 BIT(17) |
| 443 | #define MVNETA_TXD_IP_CSUM BIT(18) |
| 444 | #define MVNETA_TXD_Z_PAD BIT(19) |
| 445 | #define MVNETA_TXD_L_DESC BIT(20) |
| 446 | #define MVNETA_TXD_F_DESC BIT(21) |
| 447 | #define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \ |
| 448 | MVNETA_TXD_L_DESC | \ |
| 449 | MVNETA_TXD_F_DESC) |
| 450 | #define MVNETA_TX_L4_CSUM_FULL BIT(30) |
| 451 | #define MVNETA_TX_L4_CSUM_NOT BIT(31) |
| 452 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 453 | #define MVNETA_RXD_ERR_CRC 0x0 |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 454 | #define MVNETA_RXD_BM_POOL_SHIFT 13 |
| 455 | #define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14)) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 456 | #define MVNETA_RXD_ERR_SUMMARY BIT(16) |
| 457 | #define MVNETA_RXD_ERR_OVERRUN BIT(17) |
| 458 | #define MVNETA_RXD_ERR_LEN BIT(18) |
| 459 | #define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18)) |
| 460 | #define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18)) |
| 461 | #define MVNETA_RXD_L3_IP4 BIT(25) |
| 462 | #define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27)) |
| 463 | #define MVNETA_RXD_L4_CSUM_OK BIT(30) |
| 464 | |
Thomas Petazzoni | 9ad8fef | 2013-07-29 15:21:28 +0200 | [diff] [blame] | 465 | #if defined(__LITTLE_ENDIAN) |
Thomas Petazzoni | 6083ed4 | 2013-07-29 15:21:27 +0200 | [diff] [blame] | 466 | struct mvneta_tx_desc { |
| 467 | u32 command; /* Options used by HW for packet transmitting.*/ |
| 468 | u16 reserverd1; /* csum_l4 (for future use) */ |
| 469 | u16 data_size; /* Data size of transmitted packet in bytes */ |
| 470 | u32 buf_phys_addr; /* Physical addr of transmitted buffer */ |
| 471 | u32 reserved2; /* hw_cmd - (for future use, PMT) */ |
| 472 | u32 reserved3[4]; /* Reserved - (for future use) */ |
| 473 | }; |
| 474 | |
| 475 | struct mvneta_rx_desc { |
| 476 | u32 status; /* Info about received packet */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 477 | u16 reserved1; /* pnc_info - (for future use, PnC) */ |
| 478 | u16 data_size; /* Size of received packet in bytes */ |
Thomas Petazzoni | 6083ed4 | 2013-07-29 15:21:27 +0200 | [diff] [blame] | 479 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 480 | u32 buf_phys_addr; /* Physical address of the buffer */ |
| 481 | u32 reserved2; /* pnc_flow_id (for future use, PnC) */ |
Thomas Petazzoni | 6083ed4 | 2013-07-29 15:21:27 +0200 | [diff] [blame] | 482 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 483 | u32 buf_cookie; /* cookie for access to RX buffer in rx path */ |
| 484 | u16 reserved3; /* prefetch_cmd, for future use */ |
| 485 | u16 reserved4; /* csum_l4 - (for future use, PnC) */ |
Thomas Petazzoni | 6083ed4 | 2013-07-29 15:21:27 +0200 | [diff] [blame] | 486 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 487 | u32 reserved5; /* pnc_extra PnC (for future use, PnC) */ |
| 488 | u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */ |
| 489 | }; |
Thomas Petazzoni | 9ad8fef | 2013-07-29 15:21:28 +0200 | [diff] [blame] | 490 | #else |
| 491 | struct mvneta_tx_desc { |
| 492 | u16 data_size; /* Data size of transmitted packet in bytes */ |
| 493 | u16 reserverd1; /* csum_l4 (for future use) */ |
| 494 | u32 command; /* Options used by HW for packet transmitting.*/ |
| 495 | u32 reserved2; /* hw_cmd - (for future use, PMT) */ |
| 496 | u32 buf_phys_addr; /* Physical addr of transmitted buffer */ |
| 497 | u32 reserved3[4]; /* Reserved - (for future use) */ |
| 498 | }; |
| 499 | |
| 500 | struct mvneta_rx_desc { |
| 501 | u16 data_size; /* Size of received packet in bytes */ |
| 502 | u16 reserved1; /* pnc_info - (for future use, PnC) */ |
| 503 | u32 status; /* Info about received packet */ |
| 504 | |
| 505 | u32 reserved2; /* pnc_flow_id (for future use, PnC) */ |
| 506 | u32 buf_phys_addr; /* Physical address of the buffer */ |
| 507 | |
| 508 | u16 reserved4; /* csum_l4 - (for future use, PnC) */ |
| 509 | u16 reserved3; /* prefetch_cmd, for future use */ |
| 510 | u32 buf_cookie; /* cookie for access to RX buffer in rx path */ |
| 511 | |
| 512 | u32 reserved5; /* pnc_extra PnC (for future use, PnC) */ |
| 513 | u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */ |
| 514 | }; |
| 515 | #endif |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 516 | |
| 517 | struct mvneta_tx_queue { |
| 518 | /* Number of this TX queue, in the range 0-7 */ |
| 519 | u8 id; |
| 520 | |
| 521 | /* Number of TX DMA descriptors in the descriptor ring */ |
| 522 | int size; |
| 523 | |
| 524 | /* Number of currently used TX DMA descriptor in the |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 525 | * descriptor ring |
| 526 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 527 | int count; |
Ezequiel Garcia | 8eef5f9 | 2014-05-30 13:40:05 -0300 | [diff] [blame] | 528 | int tx_stop_threshold; |
| 529 | int tx_wake_threshold; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 530 | |
| 531 | /* Array of transmitted skb */ |
| 532 | struct sk_buff **tx_skb; |
| 533 | |
| 534 | /* Index of last TX DMA descriptor that was inserted */ |
| 535 | int txq_put_index; |
| 536 | |
| 537 | /* Index of the TX DMA descriptor to be cleaned up */ |
| 538 | int txq_get_index; |
| 539 | |
| 540 | u32 done_pkts_coal; |
| 541 | |
| 542 | /* Virtual address of the TX DMA descriptors array */ |
| 543 | struct mvneta_tx_desc *descs; |
| 544 | |
| 545 | /* DMA address of the TX DMA descriptors array */ |
| 546 | dma_addr_t descs_phys; |
| 547 | |
| 548 | /* Index of the last TX DMA descriptor */ |
| 549 | int last_desc; |
| 550 | |
| 551 | /* Index of the next TX DMA descriptor to process */ |
| 552 | int next_desc_to_proc; |
Ezequiel Garcia | 2adb719d | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 553 | |
| 554 | /* DMA buffers for TSO headers */ |
| 555 | char *tso_hdrs; |
| 556 | |
| 557 | /* DMA address of TSO headers */ |
| 558 | dma_addr_t tso_hdrs_phys; |
Gregory CLEMENT | 50bf8cb | 2015-12-09 18:23:51 +0100 | [diff] [blame] | 559 | |
| 560 | /* Affinity mask for CPUs*/ |
| 561 | cpumask_t affinity_mask; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 562 | }; |
| 563 | |
| 564 | struct mvneta_rx_queue { |
| 565 | /* rx queue number, in the range 0-7 */ |
| 566 | u8 id; |
| 567 | |
| 568 | /* num of rx descriptors in the rx descriptor ring */ |
| 569 | int size; |
| 570 | |
| 571 | /* counter of times when mvneta_refill() failed */ |
| 572 | int missed; |
| 573 | |
| 574 | u32 pkts_coal; |
| 575 | u32 time_coal; |
| 576 | |
Gregory CLEMENT | f88bee1 | 2016-12-01 18:03:06 +0100 | [diff] [blame] | 577 | /* Virtual address of the RX buffer */ |
| 578 | void **buf_virt_addr; |
| 579 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 580 | /* Virtual address of the RX DMA descriptors array */ |
| 581 | struct mvneta_rx_desc *descs; |
| 582 | |
| 583 | /* DMA address of the RX DMA descriptors array */ |
| 584 | dma_addr_t descs_phys; |
| 585 | |
| 586 | /* Index of the last RX DMA descriptor */ |
| 587 | int last_desc; |
| 588 | |
| 589 | /* Index of the next RX DMA descriptor to process */ |
| 590 | int next_desc_to_proc; |
| 591 | }; |
| 592 | |
Sebastian Andrzej Siewior | 84a3f4d | 2016-08-18 14:57:23 +0200 | [diff] [blame] | 593 | static enum cpuhp_state online_hpstate; |
Ezequiel Garcia | edadb7f | 2014-05-22 20:07:01 -0300 | [diff] [blame] | 594 | /* The hardware supports eight (8) rx queues, but we are only allowing |
| 595 | * the first one to be used. Therefore, let's just allocate one queue. |
| 596 | */ |
Maxime Ripard | d893665 | 2015-09-25 18:09:37 +0200 | [diff] [blame] | 597 | static int rxq_number = 8; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 598 | static int txq_number = 8; |
| 599 | |
| 600 | static int rxq_def; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 601 | |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 602 | static int rx_copybreak __read_mostly = 256; |
| 603 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 604 | /* HW BM need that each port be identify by a unique ID */ |
| 605 | static int global_port_id; |
| 606 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 607 | #define MVNETA_DRIVER_NAME "mvneta" |
| 608 | #define MVNETA_DRIVER_VERSION "1.0" |
| 609 | |
| 610 | /* Utility/helper methods */ |
| 611 | |
| 612 | /* Write helper method */ |
| 613 | static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data) |
| 614 | { |
| 615 | writel(data, pp->base + offset); |
| 616 | } |
| 617 | |
| 618 | /* Read helper method */ |
| 619 | static u32 mvreg_read(struct mvneta_port *pp, u32 offset) |
| 620 | { |
| 621 | return readl(pp->base + offset); |
| 622 | } |
| 623 | |
| 624 | /* Increment txq get counter */ |
| 625 | static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq) |
| 626 | { |
| 627 | txq->txq_get_index++; |
| 628 | if (txq->txq_get_index == txq->size) |
| 629 | txq->txq_get_index = 0; |
| 630 | } |
| 631 | |
| 632 | /* Increment txq put counter */ |
| 633 | static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq) |
| 634 | { |
| 635 | txq->txq_put_index++; |
| 636 | if (txq->txq_put_index == txq->size) |
| 637 | txq->txq_put_index = 0; |
| 638 | } |
| 639 | |
| 640 | |
| 641 | /* Clear all MIB counters */ |
| 642 | static void mvneta_mib_counters_clear(struct mvneta_port *pp) |
| 643 | { |
| 644 | int i; |
| 645 | u32 dummy; |
| 646 | |
| 647 | /* Perform dummy reads from MIB counters */ |
| 648 | for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4) |
| 649 | dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i)); |
Andrew Lunn | e483911 | 2015-10-22 18:37:36 +0100 | [diff] [blame] | 650 | dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT); |
| 651 | dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /* Get System Network Statistics */ |
stephen hemminger | bc1f447 | 2017-01-06 19:12:52 -0800 | [diff] [blame^] | 655 | static void |
Baoyou Xie | 2dc0d2b | 2016-09-25 17:20:41 +0800 | [diff] [blame] | 656 | mvneta_get_stats64(struct net_device *dev, |
| 657 | struct rtnl_link_stats64 *stats) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 658 | { |
| 659 | struct mvneta_port *pp = netdev_priv(dev); |
| 660 | unsigned int start; |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 661 | int cpu; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 662 | |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 663 | for_each_possible_cpu(cpu) { |
| 664 | struct mvneta_pcpu_stats *cpu_stats; |
| 665 | u64 rx_packets; |
| 666 | u64 rx_bytes; |
| 667 | u64 tx_packets; |
| 668 | u64 tx_bytes; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 669 | |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 670 | cpu_stats = per_cpu_ptr(pp->stats, cpu); |
| 671 | do { |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 672 | start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 673 | rx_packets = cpu_stats->rx_packets; |
| 674 | rx_bytes = cpu_stats->rx_bytes; |
| 675 | tx_packets = cpu_stats->tx_packets; |
| 676 | tx_bytes = cpu_stats->tx_bytes; |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 677 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 678 | |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 679 | stats->rx_packets += rx_packets; |
| 680 | stats->rx_bytes += rx_bytes; |
| 681 | stats->tx_packets += tx_packets; |
| 682 | stats->tx_bytes += tx_bytes; |
| 683 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 684 | |
| 685 | stats->rx_errors = dev->stats.rx_errors; |
| 686 | stats->rx_dropped = dev->stats.rx_dropped; |
| 687 | |
| 688 | stats->tx_dropped = dev->stats.tx_dropped; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* Rx descriptors helper methods */ |
| 692 | |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 693 | /* Checks whether the RX descriptor having this status is both the first |
| 694 | * 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] | 695 | * received through a single RX descriptor, so not having each RX |
| 696 | * descriptor with its first and last bits set is an error |
| 697 | */ |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 698 | static int mvneta_rxq_desc_is_first_last(u32 status) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 699 | { |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 700 | return (status & MVNETA_RXD_FIRST_LAST_DESC) == |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 701 | MVNETA_RXD_FIRST_LAST_DESC; |
| 702 | } |
| 703 | |
| 704 | /* Add number of descriptors ready to receive new packets */ |
| 705 | static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp, |
| 706 | struct mvneta_rx_queue *rxq, |
| 707 | int ndescs) |
| 708 | { |
| 709 | /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 710 | * be added at once |
| 711 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 712 | while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) { |
| 713 | mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), |
| 714 | (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX << |
| 715 | MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT)); |
| 716 | ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX; |
| 717 | } |
| 718 | |
| 719 | mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), |
| 720 | (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT)); |
| 721 | } |
| 722 | |
| 723 | /* Get number of RX descriptors occupied by received packets */ |
| 724 | static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp, |
| 725 | struct mvneta_rx_queue *rxq) |
| 726 | { |
| 727 | u32 val; |
| 728 | |
| 729 | val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id)); |
| 730 | return val & MVNETA_RXQ_OCCUPIED_ALL_MASK; |
| 731 | } |
| 732 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 733 | /* Update num of rx desc called upon return from rx path or |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 734 | * from mvneta_rxq_drop_pkts(). |
| 735 | */ |
| 736 | static void mvneta_rxq_desc_num_update(struct mvneta_port *pp, |
| 737 | struct mvneta_rx_queue *rxq, |
| 738 | int rx_done, int rx_filled) |
| 739 | { |
| 740 | u32 val; |
| 741 | |
| 742 | if ((rx_done <= 0xff) && (rx_filled <= 0xff)) { |
| 743 | val = rx_done | |
| 744 | (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT); |
| 745 | mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val); |
| 746 | return; |
| 747 | } |
| 748 | |
| 749 | /* Only 255 descriptors can be added at once */ |
| 750 | while ((rx_done > 0) || (rx_filled > 0)) { |
| 751 | if (rx_done <= 0xff) { |
| 752 | val = rx_done; |
| 753 | rx_done = 0; |
| 754 | } else { |
| 755 | val = 0xff; |
| 756 | rx_done -= 0xff; |
| 757 | } |
| 758 | if (rx_filled <= 0xff) { |
| 759 | val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT; |
| 760 | rx_filled = 0; |
| 761 | } else { |
| 762 | val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT; |
| 763 | rx_filled -= 0xff; |
| 764 | } |
| 765 | mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | /* Get pointer to next RX descriptor to be processed by SW */ |
| 770 | static struct mvneta_rx_desc * |
| 771 | mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq) |
| 772 | { |
| 773 | int rx_desc = rxq->next_desc_to_proc; |
| 774 | |
| 775 | rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc); |
willy tarreau | 34e4179 | 2014-01-16 08:20:15 +0100 | [diff] [blame] | 776 | prefetch(rxq->descs + rxq->next_desc_to_proc); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 777 | return rxq->descs + rx_desc; |
| 778 | } |
| 779 | |
| 780 | /* Change maximum receive size of the port. */ |
| 781 | static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size) |
| 782 | { |
| 783 | u32 val; |
| 784 | |
| 785 | val = mvreg_read(pp, MVNETA_GMAC_CTRL_0); |
| 786 | val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK; |
| 787 | val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) << |
| 788 | MVNETA_GMAC_MAX_RX_SIZE_SHIFT; |
| 789 | mvreg_write(pp, MVNETA_GMAC_CTRL_0, val); |
| 790 | } |
| 791 | |
| 792 | |
| 793 | /* Set rx queue offset */ |
| 794 | static void mvneta_rxq_offset_set(struct mvneta_port *pp, |
| 795 | struct mvneta_rx_queue *rxq, |
| 796 | int offset) |
| 797 | { |
| 798 | u32 val; |
| 799 | |
| 800 | val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id)); |
| 801 | val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK; |
| 802 | |
| 803 | /* Offset is in */ |
| 804 | val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3); |
| 805 | mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val); |
| 806 | } |
| 807 | |
| 808 | |
| 809 | /* Tx descriptors helper methods */ |
| 810 | |
| 811 | /* Update HW with number of TX descriptors to be sent */ |
| 812 | static void mvneta_txq_pend_desc_add(struct mvneta_port *pp, |
| 813 | struct mvneta_tx_queue *txq, |
| 814 | int pend_desc) |
| 815 | { |
| 816 | u32 val; |
| 817 | |
| 818 | /* Only 255 descriptors can be added at once ; Assume caller |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 819 | * process TX desriptors in quanta less than 256 |
| 820 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 821 | val = pend_desc; |
| 822 | mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val); |
| 823 | } |
| 824 | |
| 825 | /* Get pointer to next TX descriptor to be processed (send) by HW */ |
| 826 | static struct mvneta_tx_desc * |
| 827 | mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq) |
| 828 | { |
| 829 | int tx_desc = txq->next_desc_to_proc; |
| 830 | |
| 831 | txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc); |
| 832 | return txq->descs + tx_desc; |
| 833 | } |
| 834 | |
| 835 | /* Release the last allocated TX descriptor. Useful to handle DMA |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 836 | * mapping failures in the TX path. |
| 837 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 838 | static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq) |
| 839 | { |
| 840 | if (txq->next_desc_to_proc == 0) |
| 841 | txq->next_desc_to_proc = txq->last_desc - 1; |
| 842 | else |
| 843 | txq->next_desc_to_proc--; |
| 844 | } |
| 845 | |
| 846 | /* Set rxq buf size */ |
| 847 | static void mvneta_rxq_buf_size_set(struct mvneta_port *pp, |
| 848 | struct mvneta_rx_queue *rxq, |
| 849 | int buf_size) |
| 850 | { |
| 851 | u32 val; |
| 852 | |
| 853 | val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id)); |
| 854 | |
| 855 | val &= ~MVNETA_RXQ_BUF_SIZE_MASK; |
| 856 | val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT); |
| 857 | |
| 858 | mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val); |
| 859 | } |
| 860 | |
| 861 | /* Disable buffer management (BM) */ |
| 862 | static void mvneta_rxq_bm_disable(struct mvneta_port *pp, |
| 863 | struct mvneta_rx_queue *rxq) |
| 864 | { |
| 865 | u32 val; |
| 866 | |
| 867 | val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id)); |
| 868 | val &= ~MVNETA_RXQ_HW_BUF_ALLOC; |
| 869 | mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val); |
| 870 | } |
| 871 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 872 | /* Enable buffer management (BM) */ |
| 873 | static void mvneta_rxq_bm_enable(struct mvneta_port *pp, |
| 874 | struct mvneta_rx_queue *rxq) |
| 875 | { |
| 876 | u32 val; |
| 877 | |
| 878 | val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id)); |
| 879 | val |= MVNETA_RXQ_HW_BUF_ALLOC; |
| 880 | mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val); |
| 881 | } |
| 882 | |
| 883 | /* Notify HW about port's assignment of pool for bigger packets */ |
| 884 | static void mvneta_rxq_long_pool_set(struct mvneta_port *pp, |
| 885 | struct mvneta_rx_queue *rxq) |
| 886 | { |
| 887 | u32 val; |
| 888 | |
| 889 | val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id)); |
| 890 | val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK; |
| 891 | val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT); |
| 892 | |
| 893 | mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val); |
| 894 | } |
| 895 | |
| 896 | /* Notify HW about port's assignment of pool for smaller packets */ |
| 897 | static void mvneta_rxq_short_pool_set(struct mvneta_port *pp, |
| 898 | struct mvneta_rx_queue *rxq) |
| 899 | { |
| 900 | u32 val; |
| 901 | |
| 902 | val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id)); |
| 903 | val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK; |
| 904 | val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT); |
| 905 | |
| 906 | mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val); |
| 907 | } |
| 908 | |
| 909 | /* Set port's receive buffer size for assigned BM pool */ |
| 910 | static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp, |
| 911 | int buf_size, |
| 912 | u8 pool_id) |
| 913 | { |
| 914 | u32 val; |
| 915 | |
| 916 | if (!IS_ALIGNED(buf_size, 8)) { |
| 917 | dev_warn(pp->dev->dev.parent, |
| 918 | "illegal buf_size value %d, round to %d\n", |
| 919 | buf_size, ALIGN(buf_size, 8)); |
| 920 | buf_size = ALIGN(buf_size, 8); |
| 921 | } |
| 922 | |
| 923 | val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id)); |
| 924 | val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK; |
| 925 | mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val); |
| 926 | } |
| 927 | |
| 928 | /* Configure MBUS window in order to enable access BM internal SRAM */ |
| 929 | static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize, |
| 930 | u8 target, u8 attr) |
| 931 | { |
| 932 | u32 win_enable, win_protect; |
| 933 | int i; |
| 934 | |
| 935 | win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE); |
| 936 | |
| 937 | if (pp->bm_win_id < 0) { |
| 938 | /* Find first not occupied window */ |
| 939 | for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) { |
| 940 | if (win_enable & (1 << i)) { |
| 941 | pp->bm_win_id = i; |
| 942 | break; |
| 943 | } |
| 944 | } |
| 945 | if (i == MVNETA_MAX_DECODE_WIN) |
| 946 | return -ENOMEM; |
| 947 | } else { |
| 948 | i = pp->bm_win_id; |
| 949 | } |
| 950 | |
| 951 | mvreg_write(pp, MVNETA_WIN_BASE(i), 0); |
| 952 | mvreg_write(pp, MVNETA_WIN_SIZE(i), 0); |
| 953 | |
| 954 | if (i < 4) |
| 955 | mvreg_write(pp, MVNETA_WIN_REMAP(i), 0); |
| 956 | |
| 957 | mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) | |
| 958 | (attr << 8) | target); |
| 959 | |
| 960 | mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000); |
| 961 | |
| 962 | win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE); |
| 963 | win_protect |= 3 << (2 * i); |
| 964 | mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect); |
| 965 | |
| 966 | win_enable &= ~(1 << i); |
| 967 | mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable); |
| 968 | |
| 969 | return 0; |
| 970 | } |
| 971 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 972 | static int mvneta_bm_port_mbus_init(struct mvneta_port *pp) |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 973 | { |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 974 | u32 wsize; |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 975 | u8 target, attr; |
| 976 | int err; |
| 977 | |
| 978 | /* Get BM window information */ |
| 979 | err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize, |
| 980 | &target, &attr); |
| 981 | if (err < 0) |
| 982 | return err; |
| 983 | |
| 984 | pp->bm_win_id = -1; |
| 985 | |
| 986 | /* Open NETA -> BM window */ |
| 987 | err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize, |
| 988 | target, attr); |
| 989 | if (err < 0) { |
| 990 | netdev_info(pp->dev, "fail to configure mbus window to BM\n"); |
| 991 | return err; |
| 992 | } |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | /* Assign and initialize pools for port. In case of fail |
| 997 | * buffer manager will remain disabled for current port. |
| 998 | */ |
| 999 | static int mvneta_bm_port_init(struct platform_device *pdev, |
| 1000 | struct mvneta_port *pp) |
| 1001 | { |
| 1002 | struct device_node *dn = pdev->dev.of_node; |
| 1003 | u32 long_pool_id, short_pool_id; |
| 1004 | |
| 1005 | if (!pp->neta_armada3700) { |
| 1006 | int ret; |
| 1007 | |
| 1008 | ret = mvneta_bm_port_mbus_init(pp); |
| 1009 | if (ret) |
| 1010 | return ret; |
| 1011 | } |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1012 | |
| 1013 | if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) { |
| 1014 | netdev_info(pp->dev, "missing long pool id\n"); |
| 1015 | return -EINVAL; |
| 1016 | } |
| 1017 | |
| 1018 | /* Create port's long pool depending on mtu */ |
| 1019 | pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id, |
| 1020 | MVNETA_BM_LONG, pp->id, |
| 1021 | MVNETA_RX_PKT_SIZE(pp->dev->mtu)); |
| 1022 | if (!pp->pool_long) { |
| 1023 | netdev_info(pp->dev, "fail to obtain long pool for port\n"); |
| 1024 | return -ENOMEM; |
| 1025 | } |
| 1026 | |
| 1027 | pp->pool_long->port_map |= 1 << pp->id; |
| 1028 | |
| 1029 | mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size, |
| 1030 | pp->pool_long->id); |
| 1031 | |
| 1032 | /* If short pool id is not defined, assume using single pool */ |
| 1033 | if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id)) |
| 1034 | short_pool_id = long_pool_id; |
| 1035 | |
| 1036 | /* Create port's short pool */ |
| 1037 | pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id, |
| 1038 | MVNETA_BM_SHORT, pp->id, |
| 1039 | MVNETA_BM_SHORT_PKT_SIZE); |
| 1040 | if (!pp->pool_short) { |
| 1041 | netdev_info(pp->dev, "fail to obtain short pool for port\n"); |
| 1042 | mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id); |
| 1043 | return -ENOMEM; |
| 1044 | } |
| 1045 | |
| 1046 | if (short_pool_id != long_pool_id) { |
| 1047 | pp->pool_short->port_map |= 1 << pp->id; |
| 1048 | mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size, |
| 1049 | pp->pool_short->id); |
| 1050 | } |
| 1051 | |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | /* Update settings of a pool for bigger packets */ |
| 1056 | static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu) |
| 1057 | { |
| 1058 | struct mvneta_bm_pool *bm_pool = pp->pool_long; |
Gregory CLEMENT | baa11eb | 2016-03-14 09:39:05 +0100 | [diff] [blame] | 1059 | struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool; |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1060 | int num; |
| 1061 | |
| 1062 | /* Release all buffers from long pool */ |
| 1063 | mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id); |
Gregory CLEMENT | baa11eb | 2016-03-14 09:39:05 +0100 | [diff] [blame] | 1064 | if (hwbm_pool->buf_num) { |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1065 | WARN(1, "cannot free all buffers in pool %d\n", |
| 1066 | bm_pool->id); |
| 1067 | goto bm_mtu_err; |
| 1068 | } |
| 1069 | |
| 1070 | bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu); |
| 1071 | bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size); |
Gregory CLEMENT | baa11eb | 2016-03-14 09:39:05 +0100 | [diff] [blame] | 1072 | hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) + |
| 1073 | SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size)); |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1074 | |
| 1075 | /* Fill entire long pool */ |
Gregory CLEMENT | baa11eb | 2016-03-14 09:39:05 +0100 | [diff] [blame] | 1076 | num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC); |
| 1077 | if (num != hwbm_pool->size) { |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1078 | WARN(1, "pool %d: %d of %d allocated\n", |
Gregory CLEMENT | baa11eb | 2016-03-14 09:39:05 +0100 | [diff] [blame] | 1079 | bm_pool->id, num, hwbm_pool->size); |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1080 | goto bm_mtu_err; |
| 1081 | } |
| 1082 | mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id); |
| 1083 | |
| 1084 | return; |
| 1085 | |
| 1086 | bm_mtu_err: |
| 1087 | mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id); |
| 1088 | mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id); |
| 1089 | |
| 1090 | pp->bm_priv = NULL; |
| 1091 | mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1); |
| 1092 | netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n"); |
| 1093 | } |
| 1094 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1095 | /* Start the Ethernet port RX and TX activity */ |
| 1096 | static void mvneta_port_up(struct mvneta_port *pp) |
| 1097 | { |
| 1098 | int queue; |
| 1099 | u32 q_map; |
| 1100 | |
| 1101 | /* Enable all initialized TXs. */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1102 | q_map = 0; |
| 1103 | for (queue = 0; queue < txq_number; queue++) { |
| 1104 | struct mvneta_tx_queue *txq = &pp->txqs[queue]; |
| 1105 | if (txq->descs != NULL) |
| 1106 | q_map |= (1 << queue); |
| 1107 | } |
| 1108 | mvreg_write(pp, MVNETA_TXQ_CMD, q_map); |
| 1109 | |
| 1110 | /* Enable all initialized RXQs. */ |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 1111 | for (queue = 0; queue < rxq_number; queue++) { |
| 1112 | struct mvneta_rx_queue *rxq = &pp->rxqs[queue]; |
| 1113 | |
| 1114 | if (rxq->descs != NULL) |
| 1115 | q_map |= (1 << queue); |
| 1116 | } |
| 1117 | mvreg_write(pp, MVNETA_RXQ_CMD, q_map); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | /* Stop the Ethernet port activity */ |
| 1121 | static void mvneta_port_down(struct mvneta_port *pp) |
| 1122 | { |
| 1123 | u32 val; |
| 1124 | int count; |
| 1125 | |
| 1126 | /* Stop Rx port activity. Check port Rx activity. */ |
| 1127 | val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK; |
| 1128 | |
| 1129 | /* Issue stop command for active channels only */ |
| 1130 | if (val != 0) |
| 1131 | mvreg_write(pp, MVNETA_RXQ_CMD, |
| 1132 | val << MVNETA_RXQ_DISABLE_SHIFT); |
| 1133 | |
| 1134 | /* Wait for all Rx activity to terminate. */ |
| 1135 | count = 0; |
| 1136 | do { |
| 1137 | if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) { |
| 1138 | netdev_warn(pp->dev, |
Dmitri Epshtein | 0838abb3 | 2016-03-12 18:44:19 +0100 | [diff] [blame] | 1139 | "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n", |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1140 | val); |
| 1141 | break; |
| 1142 | } |
| 1143 | mdelay(1); |
| 1144 | |
| 1145 | val = mvreg_read(pp, MVNETA_RXQ_CMD); |
Dmitri Epshtein | a3703fb | 2016-03-12 18:44:20 +0100 | [diff] [blame] | 1146 | } while (val & MVNETA_RXQ_ENABLE_MASK); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1147 | |
| 1148 | /* Stop Tx port activity. Check port Tx activity. Issue stop |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 1149 | * command for active channels only |
| 1150 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1151 | val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK; |
| 1152 | |
| 1153 | if (val != 0) |
| 1154 | mvreg_write(pp, MVNETA_TXQ_CMD, |
| 1155 | (val << MVNETA_TXQ_DISABLE_SHIFT)); |
| 1156 | |
| 1157 | /* Wait for all Tx activity to terminate. */ |
| 1158 | count = 0; |
| 1159 | do { |
| 1160 | if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) { |
| 1161 | netdev_warn(pp->dev, |
| 1162 | "TIMEOUT for TX stopped status=0x%08x\n", |
| 1163 | val); |
| 1164 | break; |
| 1165 | } |
| 1166 | mdelay(1); |
| 1167 | |
| 1168 | /* Check TX Command reg that all Txqs are stopped */ |
| 1169 | val = mvreg_read(pp, MVNETA_TXQ_CMD); |
| 1170 | |
Dmitri Epshtein | a3703fb | 2016-03-12 18:44:20 +0100 | [diff] [blame] | 1171 | } while (val & MVNETA_TXQ_ENABLE_MASK); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1172 | |
| 1173 | /* Double check to verify that TX FIFO is empty */ |
| 1174 | count = 0; |
| 1175 | do { |
| 1176 | if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) { |
| 1177 | netdev_warn(pp->dev, |
Dmitri Epshtein | 0838abb3 | 2016-03-12 18:44:19 +0100 | [diff] [blame] | 1178 | "TX FIFO empty timeout status=0x%08x\n", |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1179 | val); |
| 1180 | break; |
| 1181 | } |
| 1182 | mdelay(1); |
| 1183 | |
| 1184 | val = mvreg_read(pp, MVNETA_PORT_STATUS); |
| 1185 | } while (!(val & MVNETA_TX_FIFO_EMPTY) && |
| 1186 | (val & MVNETA_TX_IN_PRGRS)); |
| 1187 | |
| 1188 | udelay(200); |
| 1189 | } |
| 1190 | |
| 1191 | /* Enable the port by setting the port enable bit of the MAC control register */ |
| 1192 | static void mvneta_port_enable(struct mvneta_port *pp) |
| 1193 | { |
| 1194 | u32 val; |
| 1195 | |
| 1196 | /* Enable port */ |
| 1197 | val = mvreg_read(pp, MVNETA_GMAC_CTRL_0); |
| 1198 | val |= MVNETA_GMAC0_PORT_ENABLE; |
| 1199 | mvreg_write(pp, MVNETA_GMAC_CTRL_0, val); |
| 1200 | } |
| 1201 | |
| 1202 | /* Disable the port and wait for about 200 usec before retuning */ |
| 1203 | static void mvneta_port_disable(struct mvneta_port *pp) |
| 1204 | { |
| 1205 | u32 val; |
| 1206 | |
| 1207 | /* Reset the Enable bit in the Serial Control Register */ |
| 1208 | val = mvreg_read(pp, MVNETA_GMAC_CTRL_0); |
| 1209 | val &= ~MVNETA_GMAC0_PORT_ENABLE; |
| 1210 | mvreg_write(pp, MVNETA_GMAC_CTRL_0, val); |
| 1211 | |
| 1212 | udelay(200); |
| 1213 | } |
| 1214 | |
| 1215 | /* Multicast tables methods */ |
| 1216 | |
| 1217 | /* Set all entries in Unicast MAC Table; queue==-1 means reject all */ |
| 1218 | static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue) |
| 1219 | { |
| 1220 | int offset; |
| 1221 | u32 val; |
| 1222 | |
| 1223 | if (queue == -1) { |
| 1224 | val = 0; |
| 1225 | } else { |
| 1226 | val = 0x1 | (queue << 1); |
| 1227 | val |= (val << 24) | (val << 16) | (val << 8); |
| 1228 | } |
| 1229 | |
| 1230 | for (offset = 0; offset <= 0xc; offset += 4) |
| 1231 | mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val); |
| 1232 | } |
| 1233 | |
| 1234 | /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */ |
| 1235 | static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue) |
| 1236 | { |
| 1237 | int offset; |
| 1238 | u32 val; |
| 1239 | |
| 1240 | if (queue == -1) { |
| 1241 | val = 0; |
| 1242 | } else { |
| 1243 | val = 0x1 | (queue << 1); |
| 1244 | val |= (val << 24) | (val << 16) | (val << 8); |
| 1245 | } |
| 1246 | |
| 1247 | for (offset = 0; offset <= 0xfc; offset += 4) |
| 1248 | mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val); |
| 1249 | |
| 1250 | } |
| 1251 | |
| 1252 | /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */ |
| 1253 | static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue) |
| 1254 | { |
| 1255 | int offset; |
| 1256 | u32 val; |
| 1257 | |
| 1258 | if (queue == -1) { |
| 1259 | memset(pp->mcast_count, 0, sizeof(pp->mcast_count)); |
| 1260 | val = 0; |
| 1261 | } else { |
| 1262 | memset(pp->mcast_count, 1, sizeof(pp->mcast_count)); |
| 1263 | val = 0x1 | (queue << 1); |
| 1264 | val |= (val << 24) | (val << 16) | (val << 8); |
| 1265 | } |
| 1266 | |
| 1267 | for (offset = 0; offset <= 0xfc; offset += 4) |
| 1268 | mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val); |
| 1269 | } |
| 1270 | |
Stas Sergeev | 0c0744f | 2015-12-02 20:35:11 +0300 | [diff] [blame] | 1271 | static void mvneta_set_autoneg(struct mvneta_port *pp, int enable) |
| 1272 | { |
| 1273 | u32 val; |
| 1274 | |
| 1275 | if (enable) { |
| 1276 | val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG); |
| 1277 | val &= ~(MVNETA_GMAC_FORCE_LINK_PASS | |
| 1278 | MVNETA_GMAC_FORCE_LINK_DOWN | |
| 1279 | MVNETA_GMAC_AN_FLOW_CTRL_EN); |
| 1280 | val |= MVNETA_GMAC_INBAND_AN_ENABLE | |
| 1281 | MVNETA_GMAC_AN_SPEED_EN | |
| 1282 | MVNETA_GMAC_AN_DUPLEX_EN; |
| 1283 | mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val); |
| 1284 | |
| 1285 | val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER); |
| 1286 | val |= MVNETA_GMAC_1MS_CLOCK_ENABLE; |
| 1287 | mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val); |
| 1288 | |
| 1289 | val = mvreg_read(pp, MVNETA_GMAC_CTRL_2); |
| 1290 | val |= MVNETA_GMAC2_INBAND_AN_ENABLE; |
| 1291 | mvreg_write(pp, MVNETA_GMAC_CTRL_2, val); |
| 1292 | } else { |
| 1293 | val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG); |
| 1294 | val &= ~(MVNETA_GMAC_INBAND_AN_ENABLE | |
| 1295 | MVNETA_GMAC_AN_SPEED_EN | |
| 1296 | MVNETA_GMAC_AN_DUPLEX_EN); |
| 1297 | mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val); |
| 1298 | |
| 1299 | val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER); |
| 1300 | val &= ~MVNETA_GMAC_1MS_CLOCK_ENABLE; |
| 1301 | mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val); |
| 1302 | |
| 1303 | val = mvreg_read(pp, MVNETA_GMAC_CTRL_2); |
| 1304 | val &= ~MVNETA_GMAC2_INBAND_AN_ENABLE; |
| 1305 | mvreg_write(pp, MVNETA_GMAC_CTRL_2, val); |
| 1306 | } |
| 1307 | } |
| 1308 | |
Gregory CLEMENT | db488c1 | 2016-02-04 22:09:27 +0100 | [diff] [blame] | 1309 | static void mvneta_percpu_unmask_interrupt(void *arg) |
| 1310 | { |
| 1311 | struct mvneta_port *pp = arg; |
| 1312 | |
| 1313 | /* All the queue are unmasked, but actually only the ones |
| 1314 | * mapped to this CPU will be unmasked |
| 1315 | */ |
| 1316 | mvreg_write(pp, MVNETA_INTR_NEW_MASK, |
| 1317 | MVNETA_RX_INTR_MASK_ALL | |
| 1318 | MVNETA_TX_INTR_MASK_ALL | |
| 1319 | MVNETA_MISCINTR_INTR_MASK); |
| 1320 | } |
| 1321 | |
| 1322 | static void mvneta_percpu_mask_interrupt(void *arg) |
| 1323 | { |
| 1324 | struct mvneta_port *pp = arg; |
| 1325 | |
| 1326 | /* All the queue are masked, but actually only the ones |
| 1327 | * mapped to this CPU will be masked |
| 1328 | */ |
| 1329 | mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0); |
| 1330 | mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0); |
| 1331 | mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0); |
| 1332 | } |
| 1333 | |
| 1334 | static void mvneta_percpu_clear_intr_cause(void *arg) |
| 1335 | { |
| 1336 | struct mvneta_port *pp = arg; |
| 1337 | |
| 1338 | /* All the queue are cleared, but actually only the ones |
| 1339 | * mapped to this CPU will be cleared |
| 1340 | */ |
| 1341 | mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0); |
| 1342 | mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0); |
| 1343 | mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0); |
| 1344 | } |
| 1345 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1346 | /* This method sets defaults to the NETA port: |
| 1347 | * Clears interrupt Cause and Mask registers. |
| 1348 | * Clears all MAC tables. |
| 1349 | * Sets defaults to all registers. |
| 1350 | * Resets RX and TX descriptor rings. |
| 1351 | * Resets PHY. |
| 1352 | * This method can be called after mvneta_port_down() to return the port |
| 1353 | * settings to defaults. |
| 1354 | */ |
| 1355 | static void mvneta_defaults_set(struct mvneta_port *pp) |
| 1356 | { |
| 1357 | int cpu; |
| 1358 | int queue; |
| 1359 | u32 val; |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 1360 | int max_cpu = num_present_cpus(); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1361 | |
| 1362 | /* Clear all Cause registers */ |
Gregory CLEMENT | db488c1 | 2016-02-04 22:09:27 +0100 | [diff] [blame] | 1363 | on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1364 | |
| 1365 | /* Mask all interrupts */ |
Gregory CLEMENT | db488c1 | 2016-02-04 22:09:27 +0100 | [diff] [blame] | 1366 | on_each_cpu(mvneta_percpu_mask_interrupt, pp, true); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1367 | mvreg_write(pp, MVNETA_INTR_ENABLE, 0); |
| 1368 | |
| 1369 | /* Enable MBUS Retry bit16 */ |
| 1370 | mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20); |
| 1371 | |
Gregory CLEMENT | 50bf8cb | 2015-12-09 18:23:51 +0100 | [diff] [blame] | 1372 | /* Set CPU queue access map. CPUs are assigned to the RX and |
| 1373 | * TX queues modulo their number. If there is only one TX |
| 1374 | * queue then it is assigned to the CPU associated to the |
| 1375 | * default RX queue. |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 1376 | */ |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 1377 | for_each_present_cpu(cpu) { |
| 1378 | int rxq_map = 0, txq_map = 0; |
Gregory CLEMENT | 50bf8cb | 2015-12-09 18:23:51 +0100 | [diff] [blame] | 1379 | int rxq, txq; |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 1380 | if (!pp->neta_armada3700) { |
| 1381 | for (rxq = 0; rxq < rxq_number; rxq++) |
| 1382 | if ((rxq % max_cpu) == cpu) |
| 1383 | rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq); |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 1384 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 1385 | for (txq = 0; txq < txq_number; txq++) |
| 1386 | if ((txq % max_cpu) == cpu) |
| 1387 | txq_map |= MVNETA_CPU_TXQ_ACCESS(txq); |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 1388 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 1389 | /* With only one TX queue we configure a special case |
| 1390 | * which will allow to get all the irq on a single |
| 1391 | * CPU |
| 1392 | */ |
| 1393 | if (txq_number == 1) |
| 1394 | txq_map = (cpu == pp->rxq_def) ? |
| 1395 | MVNETA_CPU_TXQ_ACCESS(1) : 0; |
Gregory CLEMENT | 50bf8cb | 2015-12-09 18:23:51 +0100 | [diff] [blame] | 1396 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 1397 | } else { |
| 1398 | txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK; |
| 1399 | rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK; |
| 1400 | } |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 1401 | |
| 1402 | mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map); |
| 1403 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1404 | |
| 1405 | /* Reset RX and TX DMAs */ |
| 1406 | mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET); |
| 1407 | mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET); |
| 1408 | |
| 1409 | /* Disable Legacy WRR, Disable EJP, Release from reset */ |
| 1410 | mvreg_write(pp, MVNETA_TXQ_CMD_1, 0); |
| 1411 | for (queue = 0; queue < txq_number; queue++) { |
| 1412 | mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0); |
| 1413 | mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0); |
| 1414 | } |
| 1415 | |
| 1416 | mvreg_write(pp, MVNETA_PORT_TX_RESET, 0); |
| 1417 | mvreg_write(pp, MVNETA_PORT_RX_RESET, 0); |
| 1418 | |
| 1419 | /* Set Port Acceleration Mode */ |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1420 | if (pp->bm_priv) |
| 1421 | /* HW buffer management + legacy parser */ |
| 1422 | val = MVNETA_ACC_MODE_EXT2; |
| 1423 | else |
| 1424 | /* SW buffer management + legacy parser */ |
| 1425 | val = MVNETA_ACC_MODE_EXT1; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1426 | mvreg_write(pp, MVNETA_ACC_MODE, val); |
| 1427 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1428 | if (pp->bm_priv) |
| 1429 | mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr); |
| 1430 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1431 | /* Update val of portCfg register accordingly with all RxQueue types */ |
Gregory CLEMENT | 90b74c0 | 2015-12-09 18:23:48 +0100 | [diff] [blame] | 1432 | val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1433 | mvreg_write(pp, MVNETA_PORT_CONFIG, val); |
| 1434 | |
| 1435 | val = 0; |
| 1436 | mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val); |
| 1437 | mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64); |
| 1438 | |
| 1439 | /* Build PORT_SDMA_CONFIG_REG */ |
| 1440 | val = 0; |
| 1441 | |
| 1442 | /* Default burst size */ |
| 1443 | val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16); |
| 1444 | val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16); |
Thomas Petazzoni | 9ad8fef | 2013-07-29 15:21:28 +0200 | [diff] [blame] | 1445 | val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1446 | |
Thomas Petazzoni | 9ad8fef | 2013-07-29 15:21:28 +0200 | [diff] [blame] | 1447 | #if defined(__BIG_ENDIAN) |
| 1448 | val |= MVNETA_DESC_SWAP; |
| 1449 | #endif |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1450 | |
| 1451 | /* Assign port SDMA configuration */ |
| 1452 | mvreg_write(pp, MVNETA_SDMA_CONFIG, val); |
| 1453 | |
Thomas Petazzoni | 7140860 | 2013-09-04 16:21:18 +0200 | [diff] [blame] | 1454 | /* Disable PHY polling in hardware, since we're using the |
| 1455 | * kernel phylib to do this. |
| 1456 | */ |
| 1457 | val = mvreg_read(pp, MVNETA_UNIT_CONTROL); |
| 1458 | val &= ~MVNETA_PHY_POLLING_ENABLE; |
| 1459 | mvreg_write(pp, MVNETA_UNIT_CONTROL, val); |
| 1460 | |
Stas Sergeev | 0c0744f | 2015-12-02 20:35:11 +0300 | [diff] [blame] | 1461 | mvneta_set_autoneg(pp, pp->use_inband_status); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1462 | mvneta_set_ucast_table(pp, -1); |
| 1463 | mvneta_set_special_mcast_table(pp, -1); |
| 1464 | mvneta_set_other_mcast_table(pp, -1); |
| 1465 | |
| 1466 | /* Set port interrupt enable register - default enable all */ |
| 1467 | mvreg_write(pp, MVNETA_INTR_ENABLE, |
| 1468 | (MVNETA_RXQ_INTR_ENABLE_ALL_MASK |
| 1469 | | MVNETA_TXQ_INTR_ENABLE_ALL_MASK)); |
Andrew Lunn | e483911 | 2015-10-22 18:37:36 +0100 | [diff] [blame] | 1470 | |
| 1471 | mvneta_mib_counters_clear(pp); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | /* Set max sizes for tx queues */ |
| 1475 | static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size) |
| 1476 | |
| 1477 | { |
| 1478 | u32 val, size, mtu; |
| 1479 | int queue; |
| 1480 | |
| 1481 | mtu = max_tx_size * 8; |
| 1482 | if (mtu > MVNETA_TX_MTU_MAX) |
| 1483 | mtu = MVNETA_TX_MTU_MAX; |
| 1484 | |
| 1485 | /* Set MTU */ |
| 1486 | val = mvreg_read(pp, MVNETA_TX_MTU); |
| 1487 | val &= ~MVNETA_TX_MTU_MAX; |
| 1488 | val |= mtu; |
| 1489 | mvreg_write(pp, MVNETA_TX_MTU, val); |
| 1490 | |
| 1491 | /* TX token size and all TXQs token size must be larger that MTU */ |
| 1492 | val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE); |
| 1493 | |
| 1494 | size = val & MVNETA_TX_TOKEN_SIZE_MAX; |
| 1495 | if (size < mtu) { |
| 1496 | size = mtu; |
| 1497 | val &= ~MVNETA_TX_TOKEN_SIZE_MAX; |
| 1498 | val |= size; |
| 1499 | mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val); |
| 1500 | } |
| 1501 | for (queue = 0; queue < txq_number; queue++) { |
| 1502 | val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue)); |
| 1503 | |
| 1504 | size = val & MVNETA_TXQ_TOKEN_SIZE_MAX; |
| 1505 | if (size < mtu) { |
| 1506 | size = mtu; |
| 1507 | val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX; |
| 1508 | val |= size; |
| 1509 | mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val); |
| 1510 | } |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | /* Set unicast address */ |
| 1515 | static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble, |
| 1516 | int queue) |
| 1517 | { |
| 1518 | unsigned int unicast_reg; |
| 1519 | unsigned int tbl_offset; |
| 1520 | unsigned int reg_offset; |
| 1521 | |
| 1522 | /* Locate the Unicast table entry */ |
| 1523 | last_nibble = (0xf & last_nibble); |
| 1524 | |
| 1525 | /* offset from unicast tbl base */ |
| 1526 | tbl_offset = (last_nibble / 4) * 4; |
| 1527 | |
| 1528 | /* offset within the above reg */ |
| 1529 | reg_offset = last_nibble % 4; |
| 1530 | |
| 1531 | unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset)); |
| 1532 | |
| 1533 | if (queue == -1) { |
| 1534 | /* Clear accepts frame bit at specified unicast DA tbl entry */ |
| 1535 | unicast_reg &= ~(0xff << (8 * reg_offset)); |
| 1536 | } else { |
| 1537 | unicast_reg &= ~(0xff << (8 * reg_offset)); |
| 1538 | unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset)); |
| 1539 | } |
| 1540 | |
| 1541 | mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg); |
| 1542 | } |
| 1543 | |
| 1544 | /* Set mac address */ |
| 1545 | static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr, |
| 1546 | int queue) |
| 1547 | { |
| 1548 | unsigned int mac_h; |
| 1549 | unsigned int mac_l; |
| 1550 | |
| 1551 | if (queue != -1) { |
| 1552 | mac_l = (addr[4] << 8) | (addr[5]); |
| 1553 | mac_h = (addr[0] << 24) | (addr[1] << 16) | |
| 1554 | (addr[2] << 8) | (addr[3] << 0); |
| 1555 | |
| 1556 | mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l); |
| 1557 | mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h); |
| 1558 | } |
| 1559 | |
| 1560 | /* Accept frames of this address */ |
| 1561 | mvneta_set_ucast_addr(pp, addr[5], queue); |
| 1562 | } |
| 1563 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 1564 | /* Set the number of packets that will be received before RX interrupt |
| 1565 | * will be generated by HW. |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1566 | */ |
| 1567 | static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp, |
| 1568 | struct mvneta_rx_queue *rxq, u32 value) |
| 1569 | { |
| 1570 | mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id), |
| 1571 | value | MVNETA_RXQ_NON_OCCUPIED(0)); |
| 1572 | rxq->pkts_coal = value; |
| 1573 | } |
| 1574 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 1575 | /* Set the time delay in usec before RX interrupt will be generated by |
| 1576 | * HW. |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1577 | */ |
| 1578 | static void mvneta_rx_time_coal_set(struct mvneta_port *pp, |
| 1579 | struct mvneta_rx_queue *rxq, u32 value) |
| 1580 | { |
Thomas Petazzoni | 189dd62 | 2012-11-19 14:15:25 +0100 | [diff] [blame] | 1581 | u32 val; |
| 1582 | unsigned long clk_rate; |
| 1583 | |
| 1584 | clk_rate = clk_get_rate(pp->clk); |
| 1585 | val = (clk_rate / 1000000) * value; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1586 | |
| 1587 | mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val); |
| 1588 | rxq->time_coal = value; |
| 1589 | } |
| 1590 | |
| 1591 | /* Set threshold for TX_DONE pkts coalescing */ |
| 1592 | static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp, |
| 1593 | struct mvneta_tx_queue *txq, u32 value) |
| 1594 | { |
| 1595 | u32 val; |
| 1596 | |
| 1597 | val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id)); |
| 1598 | |
| 1599 | val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK; |
| 1600 | val |= MVNETA_TXQ_SENT_THRESH_MASK(value); |
| 1601 | |
| 1602 | mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val); |
| 1603 | |
| 1604 | txq->done_pkts_coal = value; |
| 1605 | } |
| 1606 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1607 | /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */ |
| 1608 | static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc, |
Gregory CLEMENT | f88bee1 | 2016-12-01 18:03:06 +0100 | [diff] [blame] | 1609 | u32 phys_addr, void *virt_addr, |
| 1610 | struct mvneta_rx_queue *rxq) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1611 | { |
Gregory CLEMENT | f88bee1 | 2016-12-01 18:03:06 +0100 | [diff] [blame] | 1612 | int i; |
| 1613 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1614 | rx_desc->buf_phys_addr = phys_addr; |
Gregory CLEMENT | f88bee1 | 2016-12-01 18:03:06 +0100 | [diff] [blame] | 1615 | i = rx_desc - rxq->descs; |
| 1616 | rxq->buf_virt_addr[i] = virt_addr; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | /* Decrement sent descriptors counter */ |
| 1620 | static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp, |
| 1621 | struct mvneta_tx_queue *txq, |
| 1622 | int sent_desc) |
| 1623 | { |
| 1624 | u32 val; |
| 1625 | |
| 1626 | /* Only 255 TX descriptors can be updated at once */ |
| 1627 | while (sent_desc > 0xff) { |
| 1628 | val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT; |
| 1629 | mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val); |
| 1630 | sent_desc = sent_desc - 0xff; |
| 1631 | } |
| 1632 | |
| 1633 | val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT; |
| 1634 | mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val); |
| 1635 | } |
| 1636 | |
| 1637 | /* Get number of TX descriptors already sent by HW */ |
| 1638 | static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp, |
| 1639 | struct mvneta_tx_queue *txq) |
| 1640 | { |
| 1641 | u32 val; |
| 1642 | int sent_desc; |
| 1643 | |
| 1644 | val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id)); |
| 1645 | sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >> |
| 1646 | MVNETA_TXQ_SENT_DESC_SHIFT; |
| 1647 | |
| 1648 | return sent_desc; |
| 1649 | } |
| 1650 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 1651 | /* Get number of sent descriptors and decrement counter. |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1652 | * The number of sent descriptors is returned. |
| 1653 | */ |
| 1654 | static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp, |
| 1655 | struct mvneta_tx_queue *txq) |
| 1656 | { |
| 1657 | int sent_desc; |
| 1658 | |
| 1659 | /* Get number of sent descriptors */ |
| 1660 | sent_desc = mvneta_txq_sent_desc_num_get(pp, txq); |
| 1661 | |
| 1662 | /* Decrement sent descriptors counter */ |
| 1663 | if (sent_desc) |
| 1664 | mvneta_txq_sent_desc_dec(pp, txq, sent_desc); |
| 1665 | |
| 1666 | return sent_desc; |
| 1667 | } |
| 1668 | |
| 1669 | /* Set TXQ descriptors fields relevant for CSUM calculation */ |
| 1670 | static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto, |
| 1671 | int ip_hdr_len, int l4_proto) |
| 1672 | { |
| 1673 | u32 command; |
| 1674 | |
| 1675 | /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk, |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 1676 | * G_L4_chk, L4_type; required only for checksum |
| 1677 | * calculation |
| 1678 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1679 | command = l3_offs << MVNETA_TX_L3_OFF_SHIFT; |
| 1680 | command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT; |
| 1681 | |
Thomas Fitzsimmons | 0a19858 | 2014-07-08 19:44:07 -0400 | [diff] [blame] | 1682 | if (l3_proto == htons(ETH_P_IP)) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1683 | command |= MVNETA_TXD_IP_CSUM; |
| 1684 | else |
| 1685 | command |= MVNETA_TX_L3_IP6; |
| 1686 | |
| 1687 | if (l4_proto == IPPROTO_TCP) |
| 1688 | command |= MVNETA_TX_L4_CSUM_FULL; |
| 1689 | else if (l4_proto == IPPROTO_UDP) |
| 1690 | command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL; |
| 1691 | else |
| 1692 | command |= MVNETA_TX_L4_CSUM_NOT; |
| 1693 | |
| 1694 | return command; |
| 1695 | } |
| 1696 | |
| 1697 | |
| 1698 | /* Display more error info */ |
| 1699 | static void mvneta_rx_error(struct mvneta_port *pp, |
| 1700 | struct mvneta_rx_desc *rx_desc) |
| 1701 | { |
| 1702 | u32 status = rx_desc->status; |
| 1703 | |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 1704 | if (!mvneta_rxq_desc_is_first_last(status)) { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1705 | netdev_err(pp->dev, |
| 1706 | "bad rx status %08x (buffer oversize), size=%d\n", |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 1707 | status, rx_desc->data_size); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1708 | return; |
| 1709 | } |
| 1710 | |
| 1711 | switch (status & MVNETA_RXD_ERR_CODE_MASK) { |
| 1712 | case MVNETA_RXD_ERR_CRC: |
| 1713 | netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n", |
| 1714 | status, rx_desc->data_size); |
| 1715 | break; |
| 1716 | case MVNETA_RXD_ERR_OVERRUN: |
| 1717 | netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n", |
| 1718 | status, rx_desc->data_size); |
| 1719 | break; |
| 1720 | case MVNETA_RXD_ERR_LEN: |
| 1721 | netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n", |
| 1722 | status, rx_desc->data_size); |
| 1723 | break; |
| 1724 | case MVNETA_RXD_ERR_RESOURCE: |
| 1725 | netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n", |
| 1726 | status, rx_desc->data_size); |
| 1727 | break; |
| 1728 | } |
| 1729 | } |
| 1730 | |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 1731 | /* Handle RX checksum offload based on the descriptor's status */ |
| 1732 | static void mvneta_rx_csum(struct mvneta_port *pp, u32 status, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1733 | struct sk_buff *skb) |
| 1734 | { |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 1735 | if ((status & MVNETA_RXD_L3_IP4) && |
| 1736 | (status & MVNETA_RXD_L4_CSUM_OK)) { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1737 | skb->csum = 0; |
| 1738 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 1739 | return; |
| 1740 | } |
| 1741 | |
| 1742 | skb->ip_summed = CHECKSUM_NONE; |
| 1743 | } |
| 1744 | |
willy tarreau | 6c49897 | 2014-01-16 08:20:12 +0100 | [diff] [blame] | 1745 | /* Return tx queue pointer (find last set bit) according to <cause> returned |
| 1746 | * form tx_done reg. <cause> must not be null. The return value is always a |
| 1747 | * valid queue for matching the first one found in <cause>. |
| 1748 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1749 | static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp, |
| 1750 | u32 cause) |
| 1751 | { |
| 1752 | int queue = fls(cause) - 1; |
| 1753 | |
willy tarreau | 6c49897 | 2014-01-16 08:20:12 +0100 | [diff] [blame] | 1754 | return &pp->txqs[queue]; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | /* Free tx queue skbuffs */ |
| 1758 | static void mvneta_txq_bufs_free(struct mvneta_port *pp, |
| 1759 | struct mvneta_tx_queue *txq, int num) |
| 1760 | { |
| 1761 | int i; |
| 1762 | |
| 1763 | for (i = 0; i < num; i++) { |
| 1764 | struct mvneta_tx_desc *tx_desc = txq->descs + |
| 1765 | txq->txq_get_index; |
| 1766 | struct sk_buff *skb = txq->tx_skb[txq->txq_get_index]; |
| 1767 | |
| 1768 | mvneta_txq_inc_get(txq); |
| 1769 | |
Ezequiel Garcia | 2e3173a | 2014-05-30 13:40:07 -0300 | [diff] [blame] | 1770 | if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr)) |
| 1771 | dma_unmap_single(pp->dev->dev.parent, |
| 1772 | tx_desc->buf_phys_addr, |
| 1773 | tx_desc->data_size, DMA_TO_DEVICE); |
Ezequiel Garcia | ba7e46e | 2014-05-30 13:40:06 -0300 | [diff] [blame] | 1774 | if (!skb) |
| 1775 | continue; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1776 | dev_kfree_skb_any(skb); |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | /* Handle end of transmission */ |
Arnaud Ebalard | cd71319 | 2014-01-16 08:20:19 +0100 | [diff] [blame] | 1781 | static void mvneta_txq_done(struct mvneta_port *pp, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1782 | struct mvneta_tx_queue *txq) |
| 1783 | { |
| 1784 | struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id); |
| 1785 | int tx_done; |
| 1786 | |
| 1787 | tx_done = mvneta_txq_sent_desc_proc(pp, txq); |
Arnaud Ebalard | cd71319 | 2014-01-16 08:20:19 +0100 | [diff] [blame] | 1788 | if (!tx_done) |
| 1789 | return; |
| 1790 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1791 | mvneta_txq_bufs_free(pp, txq, tx_done); |
| 1792 | |
| 1793 | txq->count -= tx_done; |
| 1794 | |
| 1795 | if (netif_tx_queue_stopped(nq)) { |
Ezequiel Garcia | 8eef5f9 | 2014-05-30 13:40:05 -0300 | [diff] [blame] | 1796 | if (txq->count <= txq->tx_wake_threshold) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1797 | netif_tx_wake_queue(nq); |
| 1798 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1799 | } |
| 1800 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1801 | void *mvneta_frag_alloc(unsigned int frag_size) |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1802 | { |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1803 | if (likely(frag_size <= PAGE_SIZE)) |
| 1804 | return netdev_alloc_frag(frag_size); |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1805 | else |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1806 | return kmalloc(frag_size, GFP_ATOMIC); |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1807 | } |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1808 | EXPORT_SYMBOL_GPL(mvneta_frag_alloc); |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1809 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1810 | void mvneta_frag_free(unsigned int frag_size, void *data) |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1811 | { |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1812 | if (likely(frag_size <= PAGE_SIZE)) |
Alexander Duyck | 13dc0d2 | 2015-05-06 21:12:14 -0700 | [diff] [blame] | 1813 | skb_free_frag(data); |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1814 | else |
| 1815 | kfree(data); |
| 1816 | } |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1817 | EXPORT_SYMBOL_GPL(mvneta_frag_free); |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1818 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1819 | /* Refill processing for SW buffer management */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1820 | static int mvneta_rx_refill(struct mvneta_port *pp, |
Gregory CLEMENT | f88bee1 | 2016-12-01 18:03:06 +0100 | [diff] [blame] | 1821 | struct mvneta_rx_desc *rx_desc, |
| 1822 | struct mvneta_rx_queue *rxq) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1823 | |
| 1824 | { |
| 1825 | dma_addr_t phys_addr; |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1826 | void *data; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1827 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1828 | data = mvneta_frag_alloc(pp->frag_size); |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1829 | if (!data) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1830 | return -ENOMEM; |
| 1831 | |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1832 | phys_addr = dma_map_single(pp->dev->dev.parent, data, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1833 | MVNETA_RX_BUF_SIZE(pp->pkt_size), |
| 1834 | DMA_FROM_DEVICE); |
| 1835 | if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) { |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1836 | mvneta_frag_free(pp->frag_size, data); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1837 | return -ENOMEM; |
| 1838 | } |
| 1839 | |
Marcin Wojtas | 8d5047c | 2016-12-01 18:03:07 +0100 | [diff] [blame] | 1840 | phys_addr += pp->rx_offset_correction; |
Gregory CLEMENT | f88bee1 | 2016-12-01 18:03:06 +0100 | [diff] [blame] | 1841 | mvneta_rx_desc_fill(rx_desc, phys_addr, data, rxq); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1842 | return 0; |
| 1843 | } |
| 1844 | |
| 1845 | /* Handle tx checksum */ |
| 1846 | static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb) |
| 1847 | { |
| 1848 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 1849 | int ip_hdr_len = 0; |
Vlad Yasevich | 817dbfa | 2014-08-25 10:34:54 -0400 | [diff] [blame] | 1850 | __be16 l3_proto = vlan_get_protocol(skb); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1851 | u8 l4_proto; |
| 1852 | |
Vlad Yasevich | 817dbfa | 2014-08-25 10:34:54 -0400 | [diff] [blame] | 1853 | if (l3_proto == htons(ETH_P_IP)) { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1854 | struct iphdr *ip4h = ip_hdr(skb); |
| 1855 | |
| 1856 | /* Calculate IPv4 checksum and L4 checksum */ |
| 1857 | ip_hdr_len = ip4h->ihl; |
| 1858 | l4_proto = ip4h->protocol; |
Vlad Yasevich | 817dbfa | 2014-08-25 10:34:54 -0400 | [diff] [blame] | 1859 | } else if (l3_proto == htons(ETH_P_IPV6)) { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1860 | struct ipv6hdr *ip6h = ipv6_hdr(skb); |
| 1861 | |
| 1862 | /* Read l4_protocol from one of IPv6 extra headers */ |
| 1863 | if (skb_network_header_len(skb) > 0) |
| 1864 | ip_hdr_len = (skb_network_header_len(skb) >> 2); |
| 1865 | l4_proto = ip6h->nexthdr; |
| 1866 | } else |
| 1867 | return MVNETA_TX_L4_CSUM_NOT; |
| 1868 | |
| 1869 | return mvneta_txq_desc_csum(skb_network_offset(skb), |
Vlad Yasevich | 817dbfa | 2014-08-25 10:34:54 -0400 | [diff] [blame] | 1870 | l3_proto, ip_hdr_len, l4_proto); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | return MVNETA_TX_L4_CSUM_NOT; |
| 1874 | } |
| 1875 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1876 | /* Drop packets received by the RXQ and free buffers */ |
| 1877 | static void mvneta_rxq_drop_pkts(struct mvneta_port *pp, |
| 1878 | struct mvneta_rx_queue *rxq) |
| 1879 | { |
| 1880 | int rx_done, i; |
| 1881 | |
| 1882 | rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq); |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1883 | if (rx_done) |
| 1884 | mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done); |
| 1885 | |
| 1886 | if (pp->bm_priv) { |
| 1887 | for (i = 0; i < rx_done; i++) { |
| 1888 | struct mvneta_rx_desc *rx_desc = |
| 1889 | mvneta_rxq_next_desc_get(rxq); |
| 1890 | u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc); |
| 1891 | struct mvneta_bm_pool *bm_pool; |
| 1892 | |
| 1893 | bm_pool = &pp->bm_priv->bm_pools[pool_id]; |
| 1894 | /* Return dropped buffer to the pool */ |
| 1895 | mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool, |
| 1896 | rx_desc->buf_phys_addr); |
| 1897 | } |
| 1898 | return; |
| 1899 | } |
| 1900 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1901 | for (i = 0; i < rxq->size; i++) { |
| 1902 | struct mvneta_rx_desc *rx_desc = rxq->descs + i; |
Gregory CLEMENT | f88bee1 | 2016-12-01 18:03:06 +0100 | [diff] [blame] | 1903 | void *data = rxq->buf_virt_addr[i]; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1904 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1905 | dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr, |
Ezequiel Garcia | a328f3a | 2013-12-05 13:35:37 -0300 | [diff] [blame] | 1906 | MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE); |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1907 | mvneta_frag_free(pp->frag_size, data); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1908 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1909 | } |
| 1910 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1911 | /* Main rx processing when using software buffer management */ |
| 1912 | static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo, |
| 1913 | struct mvneta_rx_queue *rxq) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1914 | { |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 1915 | struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1916 | struct net_device *dev = pp->dev; |
Simon Guinot | a84e328 | 2015-07-19 13:00:53 +0200 | [diff] [blame] | 1917 | int rx_done; |
willy tarreau | dc4277d | 2014-01-16 08:20:07 +0100 | [diff] [blame] | 1918 | u32 rcvd_pkts = 0; |
| 1919 | u32 rcvd_bytes = 0; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1920 | |
| 1921 | /* Get number of received packets */ |
| 1922 | rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq); |
| 1923 | |
| 1924 | if (rx_todo > rx_done) |
| 1925 | rx_todo = rx_done; |
| 1926 | |
| 1927 | rx_done = 0; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1928 | |
| 1929 | /* Fairness NAPI loop */ |
| 1930 | while (rx_done < rx_todo) { |
| 1931 | struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq); |
| 1932 | struct sk_buff *skb; |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1933 | unsigned char *data; |
Simon Guinot | daf158d | 2015-09-15 22:41:21 +0200 | [diff] [blame] | 1934 | dma_addr_t phys_addr; |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1935 | u32 rx_status, frag_size; |
Gregory CLEMENT | f88bee1 | 2016-12-01 18:03:06 +0100 | [diff] [blame] | 1936 | int rx_bytes, err, index; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1937 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1938 | rx_done++; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1939 | rx_status = rx_desc->status; |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 1940 | rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE); |
Gregory CLEMENT | f88bee1 | 2016-12-01 18:03:06 +0100 | [diff] [blame] | 1941 | index = rx_desc - rxq->descs; |
| 1942 | data = rxq->buf_virt_addr[index]; |
Simon Guinot | daf158d | 2015-09-15 22:41:21 +0200 | [diff] [blame] | 1943 | phys_addr = rx_desc->buf_phys_addr; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1944 | |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 1945 | if (!mvneta_rxq_desc_is_first_last(rx_status) || |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 1946 | (rx_status & MVNETA_RXD_ERR_SUMMARY)) { |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1947 | err_drop_frame: |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1948 | dev->stats.rx_errors++; |
| 1949 | mvneta_rx_error(pp, rx_desc); |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 1950 | /* leave the descriptor untouched */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1951 | continue; |
| 1952 | } |
| 1953 | |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 1954 | if (rx_bytes <= rx_copybreak) { |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1955 | /* better copy a small frame and not unmap the DMA region */ |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 1956 | skb = netdev_alloc_skb_ip_align(dev, rx_bytes); |
| 1957 | if (unlikely(!skb)) |
| 1958 | goto err_drop_frame; |
| 1959 | |
| 1960 | dma_sync_single_range_for_cpu(dev->dev.parent, |
Gregory CLEMENT | ac83b7d | 2016-12-01 18:03:04 +0100 | [diff] [blame] | 1961 | phys_addr, |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1962 | MVNETA_MH_SIZE + NET_SKB_PAD, |
| 1963 | rx_bytes, |
| 1964 | DMA_FROM_DEVICE); |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 1965 | memcpy(skb_put(skb, rx_bytes), |
| 1966 | data + MVNETA_MH_SIZE + NET_SKB_PAD, |
| 1967 | rx_bytes); |
| 1968 | |
| 1969 | skb->protocol = eth_type_trans(skb, dev); |
| 1970 | mvneta_rx_csum(pp, rx_status, skb); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 1971 | napi_gro_receive(&port->napi, skb); |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 1972 | |
| 1973 | rcvd_pkts++; |
| 1974 | rcvd_bytes += rx_bytes; |
| 1975 | |
| 1976 | /* leave the descriptor and buffer untouched */ |
| 1977 | continue; |
| 1978 | } |
| 1979 | |
Simon Guinot | a84e328 | 2015-07-19 13:00:53 +0200 | [diff] [blame] | 1980 | /* Refill processing */ |
Gregory CLEMENT | f88bee1 | 2016-12-01 18:03:06 +0100 | [diff] [blame] | 1981 | err = mvneta_rx_refill(pp, rx_desc, rxq); |
Simon Guinot | a84e328 | 2015-07-19 13:00:53 +0200 | [diff] [blame] | 1982 | if (err) { |
| 1983 | netdev_err(dev, "Linux processing - Can't refill\n"); |
| 1984 | rxq->missed++; |
| 1985 | goto err_drop_frame; |
| 1986 | } |
| 1987 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1988 | frag_size = pp->frag_size; |
| 1989 | |
| 1990 | skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size); |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 1991 | |
Marcin Wojtas | 26c17a17 | 2015-11-30 13:27:44 +0100 | [diff] [blame] | 1992 | /* After refill old buffer has to be unmapped regardless |
| 1993 | * the skb is successfully built or not. |
| 1994 | */ |
Simon Guinot | daf158d | 2015-09-15 22:41:21 +0200 | [diff] [blame] | 1995 | dma_unmap_single(dev->dev.parent, phys_addr, |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 1996 | MVNETA_RX_BUF_SIZE(pp->pkt_size), |
| 1997 | DMA_FROM_DEVICE); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 1998 | |
Marcin Wojtas | 26c17a17 | 2015-11-30 13:27:44 +0100 | [diff] [blame] | 1999 | if (!skb) |
| 2000 | goto err_drop_frame; |
| 2001 | |
willy tarreau | dc4277d | 2014-01-16 08:20:07 +0100 | [diff] [blame] | 2002 | rcvd_pkts++; |
| 2003 | rcvd_bytes += rx_bytes; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2004 | |
| 2005 | /* Linux processing */ |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 2006 | skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2007 | skb_put(skb, rx_bytes); |
| 2008 | |
| 2009 | skb->protocol = eth_type_trans(skb, dev); |
| 2010 | |
willy tarreau | 5428213 | 2014-01-16 08:20:14 +0100 | [diff] [blame] | 2011 | mvneta_rx_csum(pp, rx_status, skb); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2012 | |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2013 | napi_gro_receive(&port->napi, skb); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2014 | } |
| 2015 | |
willy tarreau | dc4277d | 2014-01-16 08:20:07 +0100 | [diff] [blame] | 2016 | if (rcvd_pkts) { |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 2017 | struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats); |
| 2018 | |
| 2019 | u64_stats_update_begin(&stats->syncp); |
| 2020 | stats->rx_packets += rcvd_pkts; |
| 2021 | stats->rx_bytes += rcvd_bytes; |
| 2022 | u64_stats_update_end(&stats->syncp); |
willy tarreau | dc4277d | 2014-01-16 08:20:07 +0100 | [diff] [blame] | 2023 | } |
| 2024 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2025 | /* Update rxq management counters */ |
Simon Guinot | a84e328 | 2015-07-19 13:00:53 +0200 | [diff] [blame] | 2026 | mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2027 | |
| 2028 | return rx_done; |
| 2029 | } |
| 2030 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 2031 | /* Main rx processing when using hardware buffer management */ |
| 2032 | static int mvneta_rx_hwbm(struct mvneta_port *pp, int rx_todo, |
| 2033 | struct mvneta_rx_queue *rxq) |
| 2034 | { |
| 2035 | struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports); |
| 2036 | struct net_device *dev = pp->dev; |
| 2037 | int rx_done; |
| 2038 | u32 rcvd_pkts = 0; |
| 2039 | u32 rcvd_bytes = 0; |
| 2040 | |
| 2041 | /* Get number of received packets */ |
| 2042 | rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq); |
| 2043 | |
| 2044 | if (rx_todo > rx_done) |
| 2045 | rx_todo = rx_done; |
| 2046 | |
| 2047 | rx_done = 0; |
| 2048 | |
| 2049 | /* Fairness NAPI loop */ |
| 2050 | while (rx_done < rx_todo) { |
| 2051 | struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq); |
| 2052 | struct mvneta_bm_pool *bm_pool = NULL; |
| 2053 | struct sk_buff *skb; |
| 2054 | unsigned char *data; |
| 2055 | dma_addr_t phys_addr; |
| 2056 | u32 rx_status, frag_size; |
| 2057 | int rx_bytes, err; |
| 2058 | u8 pool_id; |
| 2059 | |
| 2060 | rx_done++; |
| 2061 | rx_status = rx_desc->status; |
| 2062 | rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE); |
Gregory CLEMENT | f88bee1 | 2016-12-01 18:03:06 +0100 | [diff] [blame] | 2063 | data = (u8 *)(uintptr_t)rx_desc->buf_cookie; |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 2064 | phys_addr = rx_desc->buf_phys_addr; |
| 2065 | pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc); |
| 2066 | bm_pool = &pp->bm_priv->bm_pools[pool_id]; |
| 2067 | |
| 2068 | if (!mvneta_rxq_desc_is_first_last(rx_status) || |
| 2069 | (rx_status & MVNETA_RXD_ERR_SUMMARY)) { |
| 2070 | err_drop_frame_ret_pool: |
| 2071 | /* Return the buffer to the pool */ |
| 2072 | mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool, |
| 2073 | rx_desc->buf_phys_addr); |
| 2074 | err_drop_frame: |
| 2075 | dev->stats.rx_errors++; |
| 2076 | mvneta_rx_error(pp, rx_desc); |
| 2077 | /* leave the descriptor untouched */ |
| 2078 | continue; |
| 2079 | } |
| 2080 | |
| 2081 | if (rx_bytes <= rx_copybreak) { |
| 2082 | /* better copy a small frame and not unmap the DMA region */ |
| 2083 | skb = netdev_alloc_skb_ip_align(dev, rx_bytes); |
| 2084 | if (unlikely(!skb)) |
| 2085 | goto err_drop_frame_ret_pool; |
| 2086 | |
| 2087 | dma_sync_single_range_for_cpu(dev->dev.parent, |
| 2088 | rx_desc->buf_phys_addr, |
| 2089 | MVNETA_MH_SIZE + NET_SKB_PAD, |
| 2090 | rx_bytes, |
| 2091 | DMA_FROM_DEVICE); |
| 2092 | memcpy(skb_put(skb, rx_bytes), |
| 2093 | data + MVNETA_MH_SIZE + NET_SKB_PAD, |
| 2094 | rx_bytes); |
| 2095 | |
| 2096 | skb->protocol = eth_type_trans(skb, dev); |
| 2097 | mvneta_rx_csum(pp, rx_status, skb); |
| 2098 | napi_gro_receive(&port->napi, skb); |
| 2099 | |
| 2100 | rcvd_pkts++; |
| 2101 | rcvd_bytes += rx_bytes; |
| 2102 | |
| 2103 | /* Return the buffer to the pool */ |
| 2104 | mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool, |
| 2105 | rx_desc->buf_phys_addr); |
| 2106 | |
| 2107 | /* leave the descriptor and buffer untouched */ |
| 2108 | continue; |
| 2109 | } |
| 2110 | |
| 2111 | /* Refill processing */ |
Gregory CLEMENT | baa11eb | 2016-03-14 09:39:05 +0100 | [diff] [blame] | 2112 | err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC); |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 2113 | if (err) { |
| 2114 | netdev_err(dev, "Linux processing - Can't refill\n"); |
| 2115 | rxq->missed++; |
| 2116 | goto err_drop_frame_ret_pool; |
| 2117 | } |
| 2118 | |
Gregory CLEMENT | baa11eb | 2016-03-14 09:39:05 +0100 | [diff] [blame] | 2119 | frag_size = bm_pool->hwbm_pool.frag_size; |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 2120 | |
| 2121 | skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size); |
| 2122 | |
| 2123 | /* After refill old buffer has to be unmapped regardless |
| 2124 | * the skb is successfully built or not. |
| 2125 | */ |
| 2126 | dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr, |
| 2127 | bm_pool->buf_size, DMA_FROM_DEVICE); |
| 2128 | if (!skb) |
| 2129 | goto err_drop_frame; |
| 2130 | |
| 2131 | rcvd_pkts++; |
| 2132 | rcvd_bytes += rx_bytes; |
| 2133 | |
| 2134 | /* Linux processing */ |
| 2135 | skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD); |
| 2136 | skb_put(skb, rx_bytes); |
| 2137 | |
| 2138 | skb->protocol = eth_type_trans(skb, dev); |
| 2139 | |
| 2140 | mvneta_rx_csum(pp, rx_status, skb); |
| 2141 | |
| 2142 | napi_gro_receive(&port->napi, skb); |
| 2143 | } |
| 2144 | |
| 2145 | if (rcvd_pkts) { |
| 2146 | struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats); |
| 2147 | |
| 2148 | u64_stats_update_begin(&stats->syncp); |
| 2149 | stats->rx_packets += rcvd_pkts; |
| 2150 | stats->rx_bytes += rcvd_bytes; |
| 2151 | u64_stats_update_end(&stats->syncp); |
| 2152 | } |
| 2153 | |
| 2154 | /* Update rxq management counters */ |
| 2155 | mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done); |
| 2156 | |
| 2157 | return rx_done; |
| 2158 | } |
| 2159 | |
Ezequiel Garcia | 2adb719d | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 2160 | static inline void |
| 2161 | mvneta_tso_put_hdr(struct sk_buff *skb, |
| 2162 | struct mvneta_port *pp, struct mvneta_tx_queue *txq) |
| 2163 | { |
| 2164 | struct mvneta_tx_desc *tx_desc; |
| 2165 | int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); |
| 2166 | |
| 2167 | txq->tx_skb[txq->txq_put_index] = NULL; |
| 2168 | tx_desc = mvneta_txq_next_desc_get(txq); |
| 2169 | tx_desc->data_size = hdr_len; |
| 2170 | tx_desc->command = mvneta_skb_tx_csum(pp, skb); |
| 2171 | tx_desc->command |= MVNETA_TXD_F_DESC; |
| 2172 | tx_desc->buf_phys_addr = txq->tso_hdrs_phys + |
| 2173 | txq->txq_put_index * TSO_HEADER_SIZE; |
| 2174 | mvneta_txq_inc_put(txq); |
| 2175 | } |
| 2176 | |
| 2177 | static inline int |
| 2178 | mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq, |
| 2179 | struct sk_buff *skb, char *data, int size, |
| 2180 | bool last_tcp, bool is_last) |
| 2181 | { |
| 2182 | struct mvneta_tx_desc *tx_desc; |
| 2183 | |
| 2184 | tx_desc = mvneta_txq_next_desc_get(txq); |
| 2185 | tx_desc->data_size = size; |
| 2186 | tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data, |
| 2187 | size, DMA_TO_DEVICE); |
| 2188 | if (unlikely(dma_mapping_error(dev->dev.parent, |
| 2189 | tx_desc->buf_phys_addr))) { |
| 2190 | mvneta_txq_desc_put(txq); |
| 2191 | return -ENOMEM; |
| 2192 | } |
| 2193 | |
| 2194 | tx_desc->command = 0; |
| 2195 | txq->tx_skb[txq->txq_put_index] = NULL; |
| 2196 | |
| 2197 | if (last_tcp) { |
| 2198 | /* last descriptor in the TCP packet */ |
| 2199 | tx_desc->command = MVNETA_TXD_L_DESC; |
| 2200 | |
| 2201 | /* last descriptor in SKB */ |
| 2202 | if (is_last) |
| 2203 | txq->tx_skb[txq->txq_put_index] = skb; |
| 2204 | } |
| 2205 | mvneta_txq_inc_put(txq); |
| 2206 | return 0; |
| 2207 | } |
| 2208 | |
| 2209 | static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev, |
| 2210 | struct mvneta_tx_queue *txq) |
| 2211 | { |
| 2212 | int total_len, data_left; |
| 2213 | int desc_count = 0; |
| 2214 | struct mvneta_port *pp = netdev_priv(dev); |
| 2215 | struct tso_t tso; |
| 2216 | int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); |
| 2217 | int i; |
| 2218 | |
| 2219 | /* Count needed descriptors */ |
| 2220 | if ((txq->count + tso_count_descs(skb)) >= txq->size) |
| 2221 | return 0; |
| 2222 | |
| 2223 | if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) { |
| 2224 | pr_info("*** Is this even possible???!?!?\n"); |
| 2225 | return 0; |
| 2226 | } |
| 2227 | |
| 2228 | /* Initialize the TSO handler, and prepare the first payload */ |
| 2229 | tso_start(skb, &tso); |
| 2230 | |
| 2231 | total_len = skb->len - hdr_len; |
| 2232 | while (total_len > 0) { |
| 2233 | char *hdr; |
| 2234 | |
| 2235 | data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len); |
| 2236 | total_len -= data_left; |
| 2237 | desc_count++; |
| 2238 | |
| 2239 | /* prepare packet headers: MAC + IP + TCP */ |
| 2240 | hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE; |
| 2241 | tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0); |
| 2242 | |
| 2243 | mvneta_tso_put_hdr(skb, pp, txq); |
| 2244 | |
| 2245 | while (data_left > 0) { |
| 2246 | int size; |
| 2247 | desc_count++; |
| 2248 | |
| 2249 | size = min_t(int, tso.size, data_left); |
| 2250 | |
| 2251 | if (mvneta_tso_put_data(dev, txq, skb, |
| 2252 | tso.data, size, |
| 2253 | size == data_left, |
| 2254 | total_len == 0)) |
| 2255 | goto err_release; |
| 2256 | data_left -= size; |
| 2257 | |
| 2258 | tso_build_data(skb, &tso, size); |
| 2259 | } |
| 2260 | } |
| 2261 | |
| 2262 | return desc_count; |
| 2263 | |
| 2264 | err_release: |
| 2265 | /* Release all used data descriptors; header descriptors must not |
| 2266 | * be DMA-unmapped. |
| 2267 | */ |
| 2268 | for (i = desc_count - 1; i >= 0; i--) { |
| 2269 | struct mvneta_tx_desc *tx_desc = txq->descs + i; |
Ezequiel Garcia | 2e3173a | 2014-05-30 13:40:07 -0300 | [diff] [blame] | 2270 | if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr)) |
Ezequiel Garcia | 2adb719d | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 2271 | dma_unmap_single(pp->dev->dev.parent, |
| 2272 | tx_desc->buf_phys_addr, |
| 2273 | tx_desc->data_size, |
| 2274 | DMA_TO_DEVICE); |
| 2275 | mvneta_txq_desc_put(txq); |
| 2276 | } |
| 2277 | return 0; |
| 2278 | } |
| 2279 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2280 | /* Handle tx fragmentation processing */ |
| 2281 | static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb, |
| 2282 | struct mvneta_tx_queue *txq) |
| 2283 | { |
| 2284 | struct mvneta_tx_desc *tx_desc; |
Ezequiel Garcia | 3d4ea02 | 2014-05-22 20:06:57 -0300 | [diff] [blame] | 2285 | int i, nr_frags = skb_shinfo(skb)->nr_frags; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2286 | |
Ezequiel Garcia | 3d4ea02 | 2014-05-22 20:06:57 -0300 | [diff] [blame] | 2287 | for (i = 0; i < nr_frags; i++) { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2288 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 2289 | void *addr = page_address(frag->page.p) + frag->page_offset; |
| 2290 | |
| 2291 | tx_desc = mvneta_txq_next_desc_get(txq); |
| 2292 | tx_desc->data_size = frag->size; |
| 2293 | |
| 2294 | tx_desc->buf_phys_addr = |
| 2295 | dma_map_single(pp->dev->dev.parent, addr, |
| 2296 | tx_desc->data_size, DMA_TO_DEVICE); |
| 2297 | |
| 2298 | if (dma_mapping_error(pp->dev->dev.parent, |
| 2299 | tx_desc->buf_phys_addr)) { |
| 2300 | mvneta_txq_desc_put(txq); |
| 2301 | goto error; |
| 2302 | } |
| 2303 | |
Ezequiel Garcia | 3d4ea02 | 2014-05-22 20:06:57 -0300 | [diff] [blame] | 2304 | if (i == nr_frags - 1) { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2305 | /* Last descriptor */ |
| 2306 | tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2307 | txq->tx_skb[txq->txq_put_index] = skb; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2308 | } else { |
| 2309 | /* Descriptor in the middle: Not First, Not Last */ |
| 2310 | tx_desc->command = 0; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2311 | txq->tx_skb[txq->txq_put_index] = NULL; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2312 | } |
Ezequiel Garcia | 3d4ea02 | 2014-05-22 20:06:57 -0300 | [diff] [blame] | 2313 | mvneta_txq_inc_put(txq); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | return 0; |
| 2317 | |
| 2318 | error: |
| 2319 | /* Release all descriptors that were used to map fragments of |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 2320 | * this packet, as well as the corresponding DMA mappings |
| 2321 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2322 | for (i = i - 1; i >= 0; i--) { |
| 2323 | tx_desc = txq->descs + i; |
| 2324 | dma_unmap_single(pp->dev->dev.parent, |
| 2325 | tx_desc->buf_phys_addr, |
| 2326 | tx_desc->data_size, |
| 2327 | DMA_TO_DEVICE); |
| 2328 | mvneta_txq_desc_put(txq); |
| 2329 | } |
| 2330 | |
| 2331 | return -ENOMEM; |
| 2332 | } |
| 2333 | |
| 2334 | /* Main tx processing */ |
| 2335 | static int mvneta_tx(struct sk_buff *skb, struct net_device *dev) |
| 2336 | { |
| 2337 | struct mvneta_port *pp = netdev_priv(dev); |
Willy Tarreau | ee40a11 | 2013-04-11 23:00:37 +0200 | [diff] [blame] | 2338 | u16 txq_id = skb_get_queue_mapping(skb); |
| 2339 | struct mvneta_tx_queue *txq = &pp->txqs[txq_id]; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2340 | struct mvneta_tx_desc *tx_desc; |
Eric Dumazet | 5f478b4 | 2014-12-02 04:30:59 -0800 | [diff] [blame] | 2341 | int len = skb->len; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2342 | int frags = 0; |
| 2343 | u32 tx_cmd; |
| 2344 | |
| 2345 | if (!netif_running(dev)) |
| 2346 | goto out; |
| 2347 | |
Ezequiel Garcia | 2adb719d | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 2348 | if (skb_is_gso(skb)) { |
| 2349 | frags = mvneta_tx_tso(skb, dev, txq); |
| 2350 | goto out; |
| 2351 | } |
| 2352 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2353 | frags = skb_shinfo(skb)->nr_frags + 1; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2354 | |
| 2355 | /* Get a descriptor for the first part of the packet */ |
| 2356 | tx_desc = mvneta_txq_next_desc_get(txq); |
| 2357 | |
| 2358 | tx_cmd = mvneta_skb_tx_csum(pp, skb); |
| 2359 | |
| 2360 | tx_desc->data_size = skb_headlen(skb); |
| 2361 | |
| 2362 | tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data, |
| 2363 | tx_desc->data_size, |
| 2364 | DMA_TO_DEVICE); |
| 2365 | if (unlikely(dma_mapping_error(dev->dev.parent, |
| 2366 | tx_desc->buf_phys_addr))) { |
| 2367 | mvneta_txq_desc_put(txq); |
| 2368 | frags = 0; |
| 2369 | goto out; |
| 2370 | } |
| 2371 | |
| 2372 | if (frags == 1) { |
| 2373 | /* First and Last descriptor */ |
| 2374 | tx_cmd |= MVNETA_TXD_FLZ_DESC; |
| 2375 | tx_desc->command = tx_cmd; |
| 2376 | txq->tx_skb[txq->txq_put_index] = skb; |
| 2377 | mvneta_txq_inc_put(txq); |
| 2378 | } else { |
| 2379 | /* First but not Last */ |
| 2380 | tx_cmd |= MVNETA_TXD_F_DESC; |
| 2381 | txq->tx_skb[txq->txq_put_index] = NULL; |
| 2382 | mvneta_txq_inc_put(txq); |
| 2383 | tx_desc->command = tx_cmd; |
| 2384 | /* Continue with other skb fragments */ |
| 2385 | if (mvneta_tx_frag_process(pp, skb, txq)) { |
| 2386 | dma_unmap_single(dev->dev.parent, |
| 2387 | tx_desc->buf_phys_addr, |
| 2388 | tx_desc->data_size, |
| 2389 | DMA_TO_DEVICE); |
| 2390 | mvneta_txq_desc_put(txq); |
| 2391 | frags = 0; |
| 2392 | goto out; |
| 2393 | } |
| 2394 | } |
| 2395 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2396 | out: |
| 2397 | if (frags > 0) { |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 2398 | struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats); |
Ezequiel Garcia | e19d2dd | 2014-05-19 13:59:54 -0300 | [diff] [blame] | 2399 | struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id); |
| 2400 | |
| 2401 | txq->count += frags; |
| 2402 | mvneta_txq_pend_desc_add(pp, txq, frags); |
| 2403 | |
Ezequiel Garcia | 8eef5f9 | 2014-05-30 13:40:05 -0300 | [diff] [blame] | 2404 | if (txq->count >= txq->tx_stop_threshold) |
Ezequiel Garcia | e19d2dd | 2014-05-19 13:59:54 -0300 | [diff] [blame] | 2405 | netif_tx_stop_queue(nq); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2406 | |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 2407 | u64_stats_update_begin(&stats->syncp); |
| 2408 | stats->tx_packets++; |
Eric Dumazet | 5f478b4 | 2014-12-02 04:30:59 -0800 | [diff] [blame] | 2409 | stats->tx_bytes += len; |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 2410 | u64_stats_update_end(&stats->syncp); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2411 | } else { |
| 2412 | dev->stats.tx_dropped++; |
| 2413 | dev_kfree_skb_any(skb); |
| 2414 | } |
| 2415 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2416 | return NETDEV_TX_OK; |
| 2417 | } |
| 2418 | |
| 2419 | |
| 2420 | /* Free tx resources, when resetting a port */ |
| 2421 | static void mvneta_txq_done_force(struct mvneta_port *pp, |
| 2422 | struct mvneta_tx_queue *txq) |
| 2423 | |
| 2424 | { |
| 2425 | int tx_done = txq->count; |
| 2426 | |
| 2427 | mvneta_txq_bufs_free(pp, txq, tx_done); |
| 2428 | |
| 2429 | /* reset txq */ |
| 2430 | txq->count = 0; |
| 2431 | txq->txq_put_index = 0; |
| 2432 | txq->txq_get_index = 0; |
| 2433 | } |
| 2434 | |
willy tarreau | 6c49897 | 2014-01-16 08:20:12 +0100 | [diff] [blame] | 2435 | /* Handle tx done - called in softirq context. The <cause_tx_done> argument |
| 2436 | * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL. |
| 2437 | */ |
Arnaud Ebalard | 0713a86 | 2014-01-16 08:20:18 +0100 | [diff] [blame] | 2438 | 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] | 2439 | { |
| 2440 | struct mvneta_tx_queue *txq; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2441 | struct netdev_queue *nq; |
| 2442 | |
willy tarreau | 6c49897 | 2014-01-16 08:20:12 +0100 | [diff] [blame] | 2443 | while (cause_tx_done) { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2444 | txq = mvneta_tx_done_policy(pp, cause_tx_done); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2445 | |
| 2446 | nq = netdev_get_tx_queue(pp->dev, txq->id); |
| 2447 | __netif_tx_lock(nq, smp_processor_id()); |
| 2448 | |
Arnaud Ebalard | 0713a86 | 2014-01-16 08:20:18 +0100 | [diff] [blame] | 2449 | if (txq->count) |
| 2450 | mvneta_txq_done(pp, txq); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2451 | |
| 2452 | __netif_tx_unlock(nq); |
| 2453 | cause_tx_done &= ~((1 << txq->id)); |
| 2454 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2455 | } |
| 2456 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 2457 | /* Compute crc8 of the specified address, using a unique algorithm , |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2458 | * according to hw spec, different than generic crc8 algorithm |
| 2459 | */ |
| 2460 | static int mvneta_addr_crc(unsigned char *addr) |
| 2461 | { |
| 2462 | int crc = 0; |
| 2463 | int i; |
| 2464 | |
| 2465 | for (i = 0; i < ETH_ALEN; i++) { |
| 2466 | int j; |
| 2467 | |
| 2468 | crc = (crc ^ addr[i]) << 8; |
| 2469 | for (j = 7; j >= 0; j--) { |
| 2470 | if (crc & (0x100 << j)) |
| 2471 | crc ^= 0x107 << j; |
| 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | return crc; |
| 2476 | } |
| 2477 | |
| 2478 | /* This method controls the net device special MAC multicast support. |
| 2479 | * The Special Multicast Table for MAC addresses supports MAC of the form |
| 2480 | * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF). |
| 2481 | * The MAC DA[7:0] bits are used as a pointer to the Special Multicast |
| 2482 | * Table entries in the DA-Filter table. This method set the Special |
| 2483 | * Multicast Table appropriate entry. |
| 2484 | */ |
| 2485 | static void mvneta_set_special_mcast_addr(struct mvneta_port *pp, |
| 2486 | unsigned char last_byte, |
| 2487 | int queue) |
| 2488 | { |
| 2489 | unsigned int smc_table_reg; |
| 2490 | unsigned int tbl_offset; |
| 2491 | unsigned int reg_offset; |
| 2492 | |
| 2493 | /* Register offset from SMC table base */ |
| 2494 | tbl_offset = (last_byte / 4); |
| 2495 | /* Entry offset within the above reg */ |
| 2496 | reg_offset = last_byte % 4; |
| 2497 | |
| 2498 | smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST |
| 2499 | + tbl_offset * 4)); |
| 2500 | |
| 2501 | if (queue == -1) |
| 2502 | smc_table_reg &= ~(0xff << (8 * reg_offset)); |
| 2503 | else { |
| 2504 | smc_table_reg &= ~(0xff << (8 * reg_offset)); |
| 2505 | smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset)); |
| 2506 | } |
| 2507 | |
| 2508 | mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4, |
| 2509 | smc_table_reg); |
| 2510 | } |
| 2511 | |
| 2512 | /* This method controls the network device Other MAC multicast support. |
| 2513 | * The Other Multicast Table is used for multicast of another type. |
| 2514 | * A CRC-8 is used as an index to the Other Multicast Table entries |
| 2515 | * in the DA-Filter table. |
| 2516 | * The method gets the CRC-8 value from the calling routine and |
| 2517 | * sets the Other Multicast Table appropriate entry according to the |
| 2518 | * specified CRC-8 . |
| 2519 | */ |
| 2520 | static void mvneta_set_other_mcast_addr(struct mvneta_port *pp, |
| 2521 | unsigned char crc8, |
| 2522 | int queue) |
| 2523 | { |
| 2524 | unsigned int omc_table_reg; |
| 2525 | unsigned int tbl_offset; |
| 2526 | unsigned int reg_offset; |
| 2527 | |
| 2528 | tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */ |
| 2529 | reg_offset = crc8 % 4; /* Entry offset within the above reg */ |
| 2530 | |
| 2531 | omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset); |
| 2532 | |
| 2533 | if (queue == -1) { |
| 2534 | /* Clear accepts frame bit at specified Other DA table entry */ |
| 2535 | omc_table_reg &= ~(0xff << (8 * reg_offset)); |
| 2536 | } else { |
| 2537 | omc_table_reg &= ~(0xff << (8 * reg_offset)); |
| 2538 | omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset)); |
| 2539 | } |
| 2540 | |
| 2541 | mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg); |
| 2542 | } |
| 2543 | |
| 2544 | /* The network device supports multicast using two tables: |
| 2545 | * 1) Special Multicast Table for MAC addresses of the form |
| 2546 | * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF). |
| 2547 | * The MAC DA[7:0] bits are used as a pointer to the Special Multicast |
| 2548 | * Table entries in the DA-Filter table. |
| 2549 | * 2) Other Multicast Table for multicast of another type. A CRC-8 value |
| 2550 | * is used as an index to the Other Multicast Table entries in the |
| 2551 | * DA-Filter table. |
| 2552 | */ |
| 2553 | static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr, |
| 2554 | int queue) |
| 2555 | { |
| 2556 | unsigned char crc_result = 0; |
| 2557 | |
| 2558 | if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) { |
| 2559 | mvneta_set_special_mcast_addr(pp, p_addr[5], queue); |
| 2560 | return 0; |
| 2561 | } |
| 2562 | |
| 2563 | crc_result = mvneta_addr_crc(p_addr); |
| 2564 | if (queue == -1) { |
| 2565 | if (pp->mcast_count[crc_result] == 0) { |
| 2566 | netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n", |
| 2567 | crc_result); |
| 2568 | return -EINVAL; |
| 2569 | } |
| 2570 | |
| 2571 | pp->mcast_count[crc_result]--; |
| 2572 | if (pp->mcast_count[crc_result] != 0) { |
| 2573 | netdev_info(pp->dev, |
| 2574 | "After delete there are %d valid Mcast for crc8=0x%02x\n", |
| 2575 | pp->mcast_count[crc_result], crc_result); |
| 2576 | return -EINVAL; |
| 2577 | } |
| 2578 | } else |
| 2579 | pp->mcast_count[crc_result]++; |
| 2580 | |
| 2581 | mvneta_set_other_mcast_addr(pp, crc_result, queue); |
| 2582 | |
| 2583 | return 0; |
| 2584 | } |
| 2585 | |
| 2586 | /* Configure Fitering mode of Ethernet port */ |
| 2587 | static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp, |
| 2588 | int is_promisc) |
| 2589 | { |
| 2590 | u32 port_cfg_reg, val; |
| 2591 | |
| 2592 | port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG); |
| 2593 | |
| 2594 | val = mvreg_read(pp, MVNETA_TYPE_PRIO); |
| 2595 | |
| 2596 | /* Set / Clear UPM bit in port configuration register */ |
| 2597 | if (is_promisc) { |
| 2598 | /* Accept all Unicast addresses */ |
| 2599 | port_cfg_reg |= MVNETA_UNI_PROMISC_MODE; |
| 2600 | val |= MVNETA_FORCE_UNI; |
| 2601 | mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff); |
| 2602 | mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff); |
| 2603 | } else { |
| 2604 | /* Reject all Unicast addresses */ |
| 2605 | port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE; |
| 2606 | val &= ~MVNETA_FORCE_UNI; |
| 2607 | } |
| 2608 | |
| 2609 | mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg); |
| 2610 | mvreg_write(pp, MVNETA_TYPE_PRIO, val); |
| 2611 | } |
| 2612 | |
| 2613 | /* register unicast and multicast addresses */ |
| 2614 | static void mvneta_set_rx_mode(struct net_device *dev) |
| 2615 | { |
| 2616 | struct mvneta_port *pp = netdev_priv(dev); |
| 2617 | struct netdev_hw_addr *ha; |
| 2618 | |
| 2619 | if (dev->flags & IFF_PROMISC) { |
| 2620 | /* Accept all: Multicast + Unicast */ |
| 2621 | mvneta_rx_unicast_promisc_set(pp, 1); |
Gregory CLEMENT | 90b74c0 | 2015-12-09 18:23:48 +0100 | [diff] [blame] | 2622 | mvneta_set_ucast_table(pp, pp->rxq_def); |
| 2623 | mvneta_set_special_mcast_table(pp, pp->rxq_def); |
| 2624 | mvneta_set_other_mcast_table(pp, pp->rxq_def); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2625 | } else { |
| 2626 | /* Accept single Unicast */ |
| 2627 | mvneta_rx_unicast_promisc_set(pp, 0); |
| 2628 | mvneta_set_ucast_table(pp, -1); |
Gregory CLEMENT | 90b74c0 | 2015-12-09 18:23:48 +0100 | [diff] [blame] | 2629 | mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2630 | |
| 2631 | if (dev->flags & IFF_ALLMULTI) { |
| 2632 | /* Accept all multicast */ |
Gregory CLEMENT | 90b74c0 | 2015-12-09 18:23:48 +0100 | [diff] [blame] | 2633 | mvneta_set_special_mcast_table(pp, pp->rxq_def); |
| 2634 | mvneta_set_other_mcast_table(pp, pp->rxq_def); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2635 | } else { |
| 2636 | /* Accept only initialized multicast */ |
| 2637 | mvneta_set_special_mcast_table(pp, -1); |
| 2638 | mvneta_set_other_mcast_table(pp, -1); |
| 2639 | |
| 2640 | if (!netdev_mc_empty(dev)) { |
| 2641 | netdev_for_each_mc_addr(ha, dev) { |
| 2642 | mvneta_mcast_addr_set(pp, ha->addr, |
Gregory CLEMENT | 90b74c0 | 2015-12-09 18:23:48 +0100 | [diff] [blame] | 2643 | pp->rxq_def); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2644 | } |
| 2645 | } |
| 2646 | } |
| 2647 | } |
| 2648 | } |
| 2649 | |
| 2650 | /* Interrupt handling - the callback for request_irq() */ |
| 2651 | static irqreturn_t mvneta_isr(int irq, void *dev_id) |
| 2652 | { |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 2653 | struct mvneta_port *pp = (struct mvneta_port *)dev_id; |
| 2654 | |
| 2655 | mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0); |
| 2656 | napi_schedule(&pp->napi); |
| 2657 | |
| 2658 | return IRQ_HANDLED; |
| 2659 | } |
| 2660 | |
| 2661 | /* Interrupt handling - the callback for request_percpu_irq() */ |
| 2662 | static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id) |
| 2663 | { |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2664 | struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2665 | |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2666 | disable_percpu_irq(port->pp->dev->irq); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2667 | napi_schedule(&port->napi); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2668 | |
| 2669 | return IRQ_HANDLED; |
| 2670 | } |
| 2671 | |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 2672 | static int mvneta_fixed_link_update(struct mvneta_port *pp, |
| 2673 | struct phy_device *phy) |
| 2674 | { |
| 2675 | struct fixed_phy_status status; |
| 2676 | struct fixed_phy_status changed = {}; |
| 2677 | u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS); |
| 2678 | |
| 2679 | status.link = !!(gmac_stat & MVNETA_GMAC_LINK_UP); |
| 2680 | if (gmac_stat & MVNETA_GMAC_SPEED_1000) |
| 2681 | status.speed = SPEED_1000; |
| 2682 | else if (gmac_stat & MVNETA_GMAC_SPEED_100) |
| 2683 | status.speed = SPEED_100; |
| 2684 | else |
| 2685 | status.speed = SPEED_10; |
| 2686 | status.duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX); |
| 2687 | changed.link = 1; |
| 2688 | changed.speed = 1; |
| 2689 | changed.duplex = 1; |
| 2690 | fixed_phy_update_state(phy, &status, &changed); |
| 2691 | return 0; |
| 2692 | } |
| 2693 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2694 | /* NAPI handler |
| 2695 | * Bits 0 - 7 of the causeRxTx register indicate that are transmitted |
| 2696 | * packets on the corresponding TXQ (Bit 0 is for TX queue 1). |
| 2697 | * Bits 8 -15 of the cause Rx Tx register indicate that are received |
| 2698 | * packets on the corresponding RXQ (Bit 8 is for RX queue 0). |
| 2699 | * Each CPU has its own causeRxTx register |
| 2700 | */ |
| 2701 | static int mvneta_poll(struct napi_struct *napi, int budget) |
| 2702 | { |
| 2703 | int rx_done = 0; |
| 2704 | u32 cause_rx_tx; |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 2705 | int rx_queue; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2706 | struct mvneta_port *pp = netdev_priv(napi->dev); |
Philippe Reynes | c6c022e | 2016-07-30 17:42:11 +0200 | [diff] [blame] | 2707 | struct net_device *ndev = pp->dev; |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 2708 | struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2709 | |
| 2710 | if (!netif_running(pp->dev)) { |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 2711 | napi_complete(napi); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2712 | return rx_done; |
| 2713 | } |
| 2714 | |
| 2715 | /* Read cause register */ |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 2716 | cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE); |
| 2717 | if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) { |
| 2718 | u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE); |
| 2719 | |
| 2720 | mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0); |
| 2721 | if (pp->use_inband_status && (cause_misc & |
| 2722 | (MVNETA_CAUSE_PHY_STATUS_CHANGE | |
| 2723 | MVNETA_CAUSE_LINK_CHANGE | |
| 2724 | MVNETA_CAUSE_PSC_SYNC_CHANGE))) { |
Philippe Reynes | c6c022e | 2016-07-30 17:42:11 +0200 | [diff] [blame] | 2725 | mvneta_fixed_link_update(pp, ndev->phydev); |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 2726 | } |
| 2727 | } |
willy tarreau | 71f6d1b | 2014-01-16 08:20:11 +0100 | [diff] [blame] | 2728 | |
| 2729 | /* Release Tx descriptors */ |
| 2730 | if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) { |
Arnaud Ebalard | 0713a86 | 2014-01-16 08:20:18 +0100 | [diff] [blame] | 2731 | 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] | 2732 | cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL; |
| 2733 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2734 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 2735 | /* For the case where the last mvneta_poll did not process all |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2736 | * RX packets |
| 2737 | */ |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 2738 | rx_queue = fls(((cause_rx_tx >> 8) & 0xff)); |
| 2739 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 2740 | cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx : |
| 2741 | port->cause_rx_tx; |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 2742 | |
| 2743 | if (rx_queue) { |
| 2744 | rx_queue = rx_queue - 1; |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 2745 | if (pp->bm_priv) |
| 2746 | rx_done = mvneta_rx_hwbm(pp, budget, &pp->rxqs[rx_queue]); |
| 2747 | else |
| 2748 | rx_done = mvneta_rx_swbm(pp, budget, &pp->rxqs[rx_queue]); |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 2749 | } |
| 2750 | |
Maxime Ripard | d893665 | 2015-09-25 18:09:37 +0200 | [diff] [blame] | 2751 | budget -= rx_done; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2752 | |
| 2753 | if (budget > 0) { |
| 2754 | cause_rx_tx = 0; |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 2755 | napi_complete(napi); |
| 2756 | |
| 2757 | if (pp->neta_armada3700) { |
| 2758 | unsigned long flags; |
| 2759 | |
| 2760 | local_irq_save(flags); |
| 2761 | mvreg_write(pp, MVNETA_INTR_NEW_MASK, |
| 2762 | MVNETA_RX_INTR_MASK(rxq_number) | |
| 2763 | MVNETA_TX_INTR_MASK(txq_number) | |
| 2764 | MVNETA_MISCINTR_INTR_MASK); |
| 2765 | local_irq_restore(flags); |
| 2766 | } else { |
| 2767 | enable_percpu_irq(pp->dev->irq, 0); |
| 2768 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2769 | } |
| 2770 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 2771 | if (pp->neta_armada3700) |
| 2772 | pp->cause_rx_tx = cause_rx_tx; |
| 2773 | else |
| 2774 | port->cause_rx_tx = cause_rx_tx; |
| 2775 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2776 | return rx_done; |
| 2777 | } |
| 2778 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2779 | /* Handle rxq fill: allocates rxq skbs; called when initializing a port */ |
| 2780 | static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq, |
| 2781 | int num) |
| 2782 | { |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2783 | int i; |
| 2784 | |
| 2785 | for (i = 0; i < num; i++) { |
willy tarreau | a1a65ab | 2014-01-16 08:20:13 +0100 | [diff] [blame] | 2786 | memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc)); |
Gregory CLEMENT | f88bee1 | 2016-12-01 18:03:06 +0100 | [diff] [blame] | 2787 | if (mvneta_rx_refill(pp, rxq->descs + i, rxq) != 0) { |
willy tarreau | a1a65ab | 2014-01-16 08:20:13 +0100 | [diff] [blame] | 2788 | 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] | 2789 | __func__, rxq->id, i, num); |
| 2790 | break; |
| 2791 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | /* Add this number of RX descriptors as non occupied (ready to |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 2795 | * get packets) |
| 2796 | */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2797 | mvneta_rxq_non_occup_desc_add(pp, rxq, i); |
| 2798 | |
| 2799 | return i; |
| 2800 | } |
| 2801 | |
| 2802 | /* Free all packets pending transmit from all TXQs and reset TX port */ |
| 2803 | static void mvneta_tx_reset(struct mvneta_port *pp) |
| 2804 | { |
| 2805 | int queue; |
| 2806 | |
Ezequiel Garcia | 9672850 | 2014-05-22 20:06:59 -0300 | [diff] [blame] | 2807 | /* free the skb's in the tx ring */ |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2808 | for (queue = 0; queue < txq_number; queue++) |
| 2809 | mvneta_txq_done_force(pp, &pp->txqs[queue]); |
| 2810 | |
| 2811 | mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET); |
| 2812 | mvreg_write(pp, MVNETA_PORT_TX_RESET, 0); |
| 2813 | } |
| 2814 | |
| 2815 | static void mvneta_rx_reset(struct mvneta_port *pp) |
| 2816 | { |
| 2817 | mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET); |
| 2818 | mvreg_write(pp, MVNETA_PORT_RX_RESET, 0); |
| 2819 | } |
| 2820 | |
| 2821 | /* Rx/Tx queue initialization/cleanup methods */ |
| 2822 | |
| 2823 | /* Create a specified RX queue */ |
| 2824 | static int mvneta_rxq_init(struct mvneta_port *pp, |
| 2825 | struct mvneta_rx_queue *rxq) |
| 2826 | |
| 2827 | { |
| 2828 | rxq->size = pp->rx_ring_size; |
| 2829 | |
| 2830 | /* Allocate memory for RX descriptors */ |
| 2831 | rxq->descs = dma_alloc_coherent(pp->dev->dev.parent, |
| 2832 | rxq->size * MVNETA_DESC_ALIGNED_SIZE, |
| 2833 | &rxq->descs_phys, GFP_KERNEL); |
Joe Perches | d0320f7 | 2013-03-14 13:07:21 +0000 | [diff] [blame] | 2834 | if (rxq->descs == NULL) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2835 | return -ENOMEM; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2836 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2837 | rxq->last_desc = rxq->size - 1; |
| 2838 | |
| 2839 | /* Set Rx descriptors queue starting address */ |
| 2840 | mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys); |
| 2841 | mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size); |
| 2842 | |
| 2843 | /* Set Offset */ |
Marcin Wojtas | 8d5047c | 2016-12-01 18:03:07 +0100 | [diff] [blame] | 2844 | mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD - pp->rx_offset_correction); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2845 | |
| 2846 | /* Set coalescing pkts and time */ |
| 2847 | mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal); |
| 2848 | mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal); |
| 2849 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 2850 | if (!pp->bm_priv) { |
| 2851 | /* Fill RXQ with buffers from RX pool */ |
| 2852 | mvneta_rxq_buf_size_set(pp, rxq, |
| 2853 | MVNETA_RX_BUF_SIZE(pp->pkt_size)); |
| 2854 | mvneta_rxq_bm_disable(pp, rxq); |
Gregory CLEMENT | e9f6499 | 2016-12-01 18:03:05 +0100 | [diff] [blame] | 2855 | mvneta_rxq_fill(pp, rxq, rxq->size); |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 2856 | } else { |
| 2857 | mvneta_rxq_bm_enable(pp, rxq); |
| 2858 | mvneta_rxq_long_pool_set(pp, rxq); |
| 2859 | mvneta_rxq_short_pool_set(pp, rxq); |
Gregory CLEMENT | e9f6499 | 2016-12-01 18:03:05 +0100 | [diff] [blame] | 2860 | mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size); |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 2861 | } |
| 2862 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2863 | return 0; |
| 2864 | } |
| 2865 | |
| 2866 | /* Cleanup Rx queue */ |
| 2867 | static void mvneta_rxq_deinit(struct mvneta_port *pp, |
| 2868 | struct mvneta_rx_queue *rxq) |
| 2869 | { |
| 2870 | mvneta_rxq_drop_pkts(pp, rxq); |
| 2871 | |
| 2872 | if (rxq->descs) |
| 2873 | dma_free_coherent(pp->dev->dev.parent, |
| 2874 | rxq->size * MVNETA_DESC_ALIGNED_SIZE, |
| 2875 | rxq->descs, |
| 2876 | rxq->descs_phys); |
| 2877 | |
| 2878 | rxq->descs = NULL; |
| 2879 | rxq->last_desc = 0; |
| 2880 | rxq->next_desc_to_proc = 0; |
| 2881 | rxq->descs_phys = 0; |
| 2882 | } |
| 2883 | |
| 2884 | /* Create and initialize a tx queue */ |
| 2885 | static int mvneta_txq_init(struct mvneta_port *pp, |
| 2886 | struct mvneta_tx_queue *txq) |
| 2887 | { |
Gregory CLEMENT | 50bf8cb | 2015-12-09 18:23:51 +0100 | [diff] [blame] | 2888 | int cpu; |
| 2889 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2890 | txq->size = pp->tx_ring_size; |
| 2891 | |
Ezequiel Garcia | 8eef5f9 | 2014-05-30 13:40:05 -0300 | [diff] [blame] | 2892 | /* A queue must always have room for at least one skb. |
| 2893 | * Therefore, stop the queue when the free entries reaches |
| 2894 | * the maximum number of descriptors per skb. |
| 2895 | */ |
| 2896 | txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS; |
| 2897 | txq->tx_wake_threshold = txq->tx_stop_threshold / 2; |
| 2898 | |
| 2899 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2900 | /* Allocate memory for TX descriptors */ |
| 2901 | txq->descs = dma_alloc_coherent(pp->dev->dev.parent, |
| 2902 | txq->size * MVNETA_DESC_ALIGNED_SIZE, |
| 2903 | &txq->descs_phys, GFP_KERNEL); |
Joe Perches | d0320f7 | 2013-03-14 13:07:21 +0000 | [diff] [blame] | 2904 | if (txq->descs == NULL) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2905 | return -ENOMEM; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2906 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2907 | txq->last_desc = txq->size - 1; |
| 2908 | |
| 2909 | /* Set maximum bandwidth for enabled TXQs */ |
| 2910 | mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff); |
| 2911 | mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff); |
| 2912 | |
| 2913 | /* Set Tx descriptors queue starting address */ |
| 2914 | mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys); |
| 2915 | mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size); |
| 2916 | |
| 2917 | txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL); |
| 2918 | if (txq->tx_skb == NULL) { |
| 2919 | dma_free_coherent(pp->dev->dev.parent, |
| 2920 | txq->size * MVNETA_DESC_ALIGNED_SIZE, |
| 2921 | txq->descs, txq->descs_phys); |
| 2922 | return -ENOMEM; |
| 2923 | } |
Ezequiel Garcia | 2adb719d | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 2924 | |
| 2925 | /* Allocate DMA buffers for TSO MAC/IP/TCP headers */ |
| 2926 | txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent, |
| 2927 | txq->size * TSO_HEADER_SIZE, |
| 2928 | &txq->tso_hdrs_phys, GFP_KERNEL); |
| 2929 | if (txq->tso_hdrs == NULL) { |
| 2930 | kfree(txq->tx_skb); |
| 2931 | dma_free_coherent(pp->dev->dev.parent, |
| 2932 | txq->size * MVNETA_DESC_ALIGNED_SIZE, |
| 2933 | txq->descs, txq->descs_phys); |
| 2934 | return -ENOMEM; |
| 2935 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2936 | mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal); |
| 2937 | |
Gregory CLEMENT | 50bf8cb | 2015-12-09 18:23:51 +0100 | [diff] [blame] | 2938 | /* Setup XPS mapping */ |
| 2939 | if (txq_number > 1) |
| 2940 | cpu = txq->id % num_present_cpus(); |
| 2941 | else |
| 2942 | cpu = pp->rxq_def % num_present_cpus(); |
| 2943 | cpumask_set_cpu(cpu, &txq->affinity_mask); |
| 2944 | netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id); |
| 2945 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2946 | return 0; |
| 2947 | } |
| 2948 | |
| 2949 | /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/ |
| 2950 | static void mvneta_txq_deinit(struct mvneta_port *pp, |
| 2951 | struct mvneta_tx_queue *txq) |
| 2952 | { |
| 2953 | kfree(txq->tx_skb); |
| 2954 | |
Ezequiel Garcia | 2adb719d | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 2955 | if (txq->tso_hdrs) |
| 2956 | dma_free_coherent(pp->dev->dev.parent, |
| 2957 | txq->size * TSO_HEADER_SIZE, |
| 2958 | txq->tso_hdrs, txq->tso_hdrs_phys); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2959 | if (txq->descs) |
| 2960 | dma_free_coherent(pp->dev->dev.parent, |
| 2961 | txq->size * MVNETA_DESC_ALIGNED_SIZE, |
| 2962 | txq->descs, txq->descs_phys); |
| 2963 | |
| 2964 | txq->descs = NULL; |
| 2965 | txq->last_desc = 0; |
| 2966 | txq->next_desc_to_proc = 0; |
| 2967 | txq->descs_phys = 0; |
| 2968 | |
| 2969 | /* Set minimum bandwidth for disabled TXQs */ |
| 2970 | mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0); |
| 2971 | mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0); |
| 2972 | |
| 2973 | /* Set Tx descriptors queue starting address and size */ |
| 2974 | mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0); |
| 2975 | mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0); |
| 2976 | } |
| 2977 | |
| 2978 | /* Cleanup all Tx queues */ |
| 2979 | static void mvneta_cleanup_txqs(struct mvneta_port *pp) |
| 2980 | { |
| 2981 | int queue; |
| 2982 | |
| 2983 | for (queue = 0; queue < txq_number; queue++) |
| 2984 | mvneta_txq_deinit(pp, &pp->txqs[queue]); |
| 2985 | } |
| 2986 | |
| 2987 | /* Cleanup all Rx queues */ |
| 2988 | static void mvneta_cleanup_rxqs(struct mvneta_port *pp) |
| 2989 | { |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 2990 | int queue; |
| 2991 | |
| 2992 | for (queue = 0; queue < txq_number; queue++) |
| 2993 | mvneta_rxq_deinit(pp, &pp->rxqs[queue]); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 2994 | } |
| 2995 | |
| 2996 | |
| 2997 | /* Init all Rx queues */ |
| 2998 | static int mvneta_setup_rxqs(struct mvneta_port *pp) |
| 2999 | { |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 3000 | int queue; |
| 3001 | |
| 3002 | for (queue = 0; queue < rxq_number; queue++) { |
| 3003 | int err = mvneta_rxq_init(pp, &pp->rxqs[queue]); |
| 3004 | |
| 3005 | if (err) { |
| 3006 | netdev_err(pp->dev, "%s: can't create rxq=%d\n", |
| 3007 | __func__, queue); |
| 3008 | mvneta_cleanup_rxqs(pp); |
| 3009 | return err; |
| 3010 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | return 0; |
| 3014 | } |
| 3015 | |
| 3016 | /* Init all tx queues */ |
| 3017 | static int mvneta_setup_txqs(struct mvneta_port *pp) |
| 3018 | { |
| 3019 | int queue; |
| 3020 | |
| 3021 | for (queue = 0; queue < txq_number; queue++) { |
| 3022 | int err = mvneta_txq_init(pp, &pp->txqs[queue]); |
| 3023 | if (err) { |
| 3024 | netdev_err(pp->dev, "%s: can't create txq=%d\n", |
| 3025 | __func__, queue); |
| 3026 | mvneta_cleanup_txqs(pp); |
| 3027 | return err; |
| 3028 | } |
| 3029 | } |
| 3030 | |
| 3031 | return 0; |
| 3032 | } |
| 3033 | |
| 3034 | static void mvneta_start_dev(struct mvneta_port *pp) |
| 3035 | { |
Gregory CLEMENT | 6b125d6 | 2016-02-04 22:09:25 +0100 | [diff] [blame] | 3036 | int cpu; |
Philippe Reynes | c6c022e | 2016-07-30 17:42:11 +0200 | [diff] [blame] | 3037 | struct net_device *ndev = pp->dev; |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 3038 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3039 | mvneta_max_rx_size_set(pp, pp->pkt_size); |
| 3040 | mvneta_txq_max_tx_size_set(pp, pp->pkt_size); |
| 3041 | |
| 3042 | /* start the Rx/Tx activity */ |
| 3043 | mvneta_port_enable(pp); |
| 3044 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3045 | if (!pp->neta_armada3700) { |
| 3046 | /* Enable polling on the port */ |
| 3047 | for_each_online_cpu(cpu) { |
| 3048 | struct mvneta_pcpu_port *port = |
| 3049 | per_cpu_ptr(pp->ports, cpu); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 3050 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3051 | napi_enable(&port->napi); |
| 3052 | } |
| 3053 | } else { |
| 3054 | napi_enable(&pp->napi); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 3055 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3056 | |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 3057 | /* Unmask interrupts. It has to be done from each CPU */ |
Gregory CLEMENT | 6b125d6 | 2016-02-04 22:09:25 +0100 | [diff] [blame] | 3058 | on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true); |
| 3059 | |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 3060 | mvreg_write(pp, MVNETA_INTR_MISC_MASK, |
| 3061 | MVNETA_CAUSE_PHY_STATUS_CHANGE | |
| 3062 | MVNETA_CAUSE_LINK_CHANGE | |
| 3063 | MVNETA_CAUSE_PSC_SYNC_CHANGE); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3064 | |
Philippe Reynes | c6c022e | 2016-07-30 17:42:11 +0200 | [diff] [blame] | 3065 | phy_start(ndev->phydev); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3066 | netif_tx_start_all_queues(pp->dev); |
| 3067 | } |
| 3068 | |
| 3069 | static void mvneta_stop_dev(struct mvneta_port *pp) |
| 3070 | { |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 3071 | unsigned int cpu; |
Philippe Reynes | c6c022e | 2016-07-30 17:42:11 +0200 | [diff] [blame] | 3072 | struct net_device *ndev = pp->dev; |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 3073 | |
Philippe Reynes | c6c022e | 2016-07-30 17:42:11 +0200 | [diff] [blame] | 3074 | phy_stop(ndev->phydev); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3075 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3076 | if (!pp->neta_armada3700) { |
| 3077 | for_each_online_cpu(cpu) { |
| 3078 | struct mvneta_pcpu_port *port = |
| 3079 | per_cpu_ptr(pp->ports, cpu); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 3080 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3081 | napi_disable(&port->napi); |
| 3082 | } |
| 3083 | } else { |
| 3084 | napi_disable(&pp->napi); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 3085 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3086 | |
| 3087 | netif_carrier_off(pp->dev); |
| 3088 | |
| 3089 | mvneta_port_down(pp); |
| 3090 | netif_tx_stop_all_queues(pp->dev); |
| 3091 | |
| 3092 | /* Stop the port activity */ |
| 3093 | mvneta_port_disable(pp); |
| 3094 | |
| 3095 | /* Clear all ethernet port interrupts */ |
Gregory CLEMENT | db488c1 | 2016-02-04 22:09:27 +0100 | [diff] [blame] | 3096 | on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3097 | |
| 3098 | /* Mask all ethernet port interrupts */ |
Gregory CLEMENT | db488c1 | 2016-02-04 22:09:27 +0100 | [diff] [blame] | 3099 | on_each_cpu(mvneta_percpu_mask_interrupt, pp, true); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3100 | |
| 3101 | mvneta_tx_reset(pp); |
| 3102 | mvneta_rx_reset(pp); |
| 3103 | } |
| 3104 | |
Marcin Wojtas | db5dd0d | 2016-04-01 15:21:18 +0200 | [diff] [blame] | 3105 | static void mvneta_percpu_enable(void *arg) |
| 3106 | { |
| 3107 | struct mvneta_port *pp = arg; |
| 3108 | |
| 3109 | enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE); |
| 3110 | } |
| 3111 | |
| 3112 | static void mvneta_percpu_disable(void *arg) |
| 3113 | { |
| 3114 | struct mvneta_port *pp = arg; |
| 3115 | |
| 3116 | disable_percpu_irq(pp->dev->irq); |
| 3117 | } |
| 3118 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3119 | /* Change the device mtu */ |
| 3120 | static int mvneta_change_mtu(struct net_device *dev, int mtu) |
| 3121 | { |
| 3122 | struct mvneta_port *pp = netdev_priv(dev); |
| 3123 | int ret; |
| 3124 | |
Jarod Wilson | 5777987 | 2016-10-17 15:54:06 -0400 | [diff] [blame] | 3125 | if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) { |
| 3126 | netdev_info(dev, "Illegal MTU value %d, rounding to %d\n", |
| 3127 | mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8)); |
| 3128 | mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8); |
| 3129 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3130 | |
| 3131 | dev->mtu = mtu; |
| 3132 | |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 3133 | if (!netif_running(dev)) { |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 3134 | if (pp->bm_priv) |
| 3135 | mvneta_bm_update_mtu(pp, mtu); |
| 3136 | |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 3137 | netdev_update_features(dev); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3138 | return 0; |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 3139 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3140 | |
Thomas Petazzoni | 6a20c17 | 2012-11-19 11:41:25 +0100 | [diff] [blame] | 3141 | /* The interface is running, so we have to force a |
Ezequiel Garcia | a92dbd9 | 2014-05-22 20:06:58 -0300 | [diff] [blame] | 3142 | * reallocation of the queues |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3143 | */ |
| 3144 | mvneta_stop_dev(pp); |
Marcin Wojtas | db5dd0d | 2016-04-01 15:21:18 +0200 | [diff] [blame] | 3145 | on_each_cpu(mvneta_percpu_disable, pp, true); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3146 | |
| 3147 | mvneta_cleanup_txqs(pp); |
| 3148 | mvneta_cleanup_rxqs(pp); |
| 3149 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 3150 | if (pp->bm_priv) |
| 3151 | mvneta_bm_update_mtu(pp, mtu); |
| 3152 | |
Ezequiel Garcia | a92dbd9 | 2014-05-22 20:06:58 -0300 | [diff] [blame] | 3153 | pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu); |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 3154 | pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) + |
| 3155 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3156 | |
| 3157 | ret = mvneta_setup_rxqs(pp); |
| 3158 | if (ret) { |
Ezequiel Garcia | a92dbd9 | 2014-05-22 20:06:58 -0300 | [diff] [blame] | 3159 | netdev_err(dev, "unable to setup rxqs after MTU change\n"); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3160 | return ret; |
| 3161 | } |
| 3162 | |
Ezequiel Garcia | a92dbd9 | 2014-05-22 20:06:58 -0300 | [diff] [blame] | 3163 | ret = mvneta_setup_txqs(pp); |
| 3164 | if (ret) { |
| 3165 | netdev_err(dev, "unable to setup txqs after MTU change\n"); |
| 3166 | return ret; |
| 3167 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3168 | |
Marcin Wojtas | db5dd0d | 2016-04-01 15:21:18 +0200 | [diff] [blame] | 3169 | on_each_cpu(mvneta_percpu_enable, pp, true); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3170 | mvneta_start_dev(pp); |
| 3171 | mvneta_port_up(pp); |
| 3172 | |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 3173 | netdev_update_features(dev); |
| 3174 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3175 | return 0; |
| 3176 | } |
| 3177 | |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 3178 | static netdev_features_t mvneta_fix_features(struct net_device *dev, |
| 3179 | netdev_features_t features) |
| 3180 | { |
| 3181 | struct mvneta_port *pp = netdev_priv(dev); |
| 3182 | |
| 3183 | if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) { |
| 3184 | features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO); |
| 3185 | netdev_info(dev, |
| 3186 | "Disable IP checksum for MTU greater than %dB\n", |
| 3187 | pp->tx_csum_limit); |
| 3188 | } |
| 3189 | |
| 3190 | return features; |
| 3191 | } |
| 3192 | |
Thomas Petazzoni | 8cc3e43 | 2013-06-04 04:52:23 +0000 | [diff] [blame] | 3193 | /* Get mac address */ |
| 3194 | static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr) |
| 3195 | { |
| 3196 | u32 mac_addr_l, mac_addr_h; |
| 3197 | |
| 3198 | mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW); |
| 3199 | mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH); |
| 3200 | addr[0] = (mac_addr_h >> 24) & 0xFF; |
| 3201 | addr[1] = (mac_addr_h >> 16) & 0xFF; |
| 3202 | addr[2] = (mac_addr_h >> 8) & 0xFF; |
| 3203 | addr[3] = mac_addr_h & 0xFF; |
| 3204 | addr[4] = (mac_addr_l >> 8) & 0xFF; |
| 3205 | addr[5] = mac_addr_l & 0xFF; |
| 3206 | } |
| 3207 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3208 | /* Handle setting mac address */ |
| 3209 | static int mvneta_set_mac_addr(struct net_device *dev, void *addr) |
| 3210 | { |
| 3211 | struct mvneta_port *pp = netdev_priv(dev); |
Ezequiel Garcia | e68de36 | 2014-05-22 20:07:00 -0300 | [diff] [blame] | 3212 | struct sockaddr *sockaddr = addr; |
| 3213 | int ret; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3214 | |
Ezequiel Garcia | e68de36 | 2014-05-22 20:07:00 -0300 | [diff] [blame] | 3215 | ret = eth_prepare_mac_addr_change(dev, addr); |
| 3216 | if (ret < 0) |
| 3217 | return ret; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3218 | /* Remove previous address table entry */ |
| 3219 | mvneta_mac_addr_set(pp, dev->dev_addr, -1); |
| 3220 | |
| 3221 | /* Set new addr in hw */ |
Gregory CLEMENT | 90b74c0 | 2015-12-09 18:23:48 +0100 | [diff] [blame] | 3222 | mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3223 | |
Ezequiel Garcia | e68de36 | 2014-05-22 20:07:00 -0300 | [diff] [blame] | 3224 | eth_commit_mac_addr_change(dev, addr); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3225 | return 0; |
| 3226 | } |
| 3227 | |
| 3228 | static void mvneta_adjust_link(struct net_device *ndev) |
| 3229 | { |
| 3230 | struct mvneta_port *pp = netdev_priv(ndev); |
Philippe Reynes | c6c022e | 2016-07-30 17:42:11 +0200 | [diff] [blame] | 3231 | struct phy_device *phydev = ndev->phydev; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3232 | int status_change = 0; |
| 3233 | |
| 3234 | if (phydev->link) { |
| 3235 | if ((pp->speed != phydev->speed) || |
| 3236 | (pp->duplex != phydev->duplex)) { |
| 3237 | u32 val; |
| 3238 | |
| 3239 | val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG); |
| 3240 | val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED | |
| 3241 | MVNETA_GMAC_CONFIG_GMII_SPEED | |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 3242 | MVNETA_GMAC_CONFIG_FULL_DUPLEX); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3243 | |
| 3244 | if (phydev->duplex) |
| 3245 | val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX; |
| 3246 | |
| 3247 | if (phydev->speed == SPEED_1000) |
| 3248 | val |= MVNETA_GMAC_CONFIG_GMII_SPEED; |
Thomas Petazzoni | 4d12bc6 | 2014-07-08 10:49:43 +0200 | [diff] [blame] | 3249 | else if (phydev->speed == SPEED_100) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3250 | val |= MVNETA_GMAC_CONFIG_MII_SPEED; |
| 3251 | |
| 3252 | mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val); |
| 3253 | |
| 3254 | pp->duplex = phydev->duplex; |
| 3255 | pp->speed = phydev->speed; |
| 3256 | } |
| 3257 | } |
| 3258 | |
| 3259 | if (phydev->link != pp->link) { |
| 3260 | if (!phydev->link) { |
| 3261 | pp->duplex = -1; |
| 3262 | pp->speed = 0; |
| 3263 | } |
| 3264 | |
| 3265 | pp->link = phydev->link; |
| 3266 | status_change = 1; |
| 3267 | } |
| 3268 | |
| 3269 | if (status_change) { |
| 3270 | if (phydev->link) { |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 3271 | if (!pp->use_inband_status) { |
| 3272 | u32 val = mvreg_read(pp, |
| 3273 | MVNETA_GMAC_AUTONEG_CONFIG); |
| 3274 | val &= ~MVNETA_GMAC_FORCE_LINK_DOWN; |
| 3275 | val |= MVNETA_GMAC_FORCE_LINK_PASS; |
| 3276 | mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, |
| 3277 | val); |
| 3278 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3279 | mvneta_port_up(pp); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3280 | } else { |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 3281 | if (!pp->use_inband_status) { |
| 3282 | u32 val = mvreg_read(pp, |
| 3283 | MVNETA_GMAC_AUTONEG_CONFIG); |
| 3284 | val &= ~MVNETA_GMAC_FORCE_LINK_PASS; |
| 3285 | val |= MVNETA_GMAC_FORCE_LINK_DOWN; |
| 3286 | mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, |
| 3287 | val); |
| 3288 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3289 | mvneta_port_down(pp); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3290 | } |
Ezequiel Garcia | 0089b74 | 2014-10-31 12:57:20 -0300 | [diff] [blame] | 3291 | phy_print_status(phydev); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | static int mvneta_mdio_probe(struct mvneta_port *pp) |
| 3296 | { |
| 3297 | struct phy_device *phy_dev; |
| 3298 | |
| 3299 | phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0, |
| 3300 | pp->phy_interface); |
| 3301 | if (!phy_dev) { |
| 3302 | netdev_err(pp->dev, "could not find the PHY\n"); |
| 3303 | return -ENODEV; |
| 3304 | } |
| 3305 | |
| 3306 | phy_dev->supported &= PHY_GBIT_FEATURES; |
| 3307 | phy_dev->advertising = phy_dev->supported; |
| 3308 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3309 | pp->link = 0; |
| 3310 | pp->duplex = 0; |
| 3311 | pp->speed = 0; |
| 3312 | |
| 3313 | return 0; |
| 3314 | } |
| 3315 | |
| 3316 | static void mvneta_mdio_remove(struct mvneta_port *pp) |
| 3317 | { |
Philippe Reynes | c6c022e | 2016-07-30 17:42:11 +0200 | [diff] [blame] | 3318 | struct net_device *ndev = pp->dev; |
| 3319 | |
| 3320 | phy_disconnect(ndev->phydev); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3321 | } |
| 3322 | |
Gregory CLEMENT | 120cfa5 | 2016-02-04 22:09:29 +0100 | [diff] [blame] | 3323 | /* Electing a CPU must be done in an atomic way: it should be done |
| 3324 | * after or before the removal/insertion of a CPU and this function is |
| 3325 | * not reentrant. |
| 3326 | */ |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 3327 | static void mvneta_percpu_elect(struct mvneta_port *pp) |
| 3328 | { |
Gregory CLEMENT | cad5d84 | 2016-02-04 22:09:24 +0100 | [diff] [blame] | 3329 | int elected_cpu = 0, max_cpu, cpu, i = 0; |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 3330 | |
Gregory CLEMENT | cad5d84 | 2016-02-04 22:09:24 +0100 | [diff] [blame] | 3331 | /* Use the cpu associated to the rxq when it is online, in all |
| 3332 | * the other cases, use the cpu 0 which can't be offline. |
| 3333 | */ |
| 3334 | if (cpu_online(pp->rxq_def)) |
| 3335 | elected_cpu = pp->rxq_def; |
| 3336 | |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 3337 | max_cpu = num_present_cpus(); |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 3338 | |
| 3339 | for_each_online_cpu(cpu) { |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 3340 | int rxq_map = 0, txq_map = 0; |
| 3341 | int rxq; |
| 3342 | |
| 3343 | for (rxq = 0; rxq < rxq_number; rxq++) |
| 3344 | if ((rxq % max_cpu) == cpu) |
| 3345 | rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq); |
| 3346 | |
Gregory CLEMENT | cad5d84 | 2016-02-04 22:09:24 +0100 | [diff] [blame] | 3347 | if (cpu == elected_cpu) |
Gregory CLEMENT | 50bf8cb | 2015-12-09 18:23:51 +0100 | [diff] [blame] | 3348 | /* Map the default receive queue queue to the |
| 3349 | * elected CPU |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 3350 | */ |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 3351 | rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def); |
Gregory CLEMENT | 50bf8cb | 2015-12-09 18:23:51 +0100 | [diff] [blame] | 3352 | |
| 3353 | /* We update the TX queue map only if we have one |
| 3354 | * queue. In this case we associate the TX queue to |
| 3355 | * the CPU bound to the default RX queue |
| 3356 | */ |
| 3357 | if (txq_number == 1) |
Gregory CLEMENT | cad5d84 | 2016-02-04 22:09:24 +0100 | [diff] [blame] | 3358 | txq_map = (cpu == elected_cpu) ? |
Gregory CLEMENT | 50bf8cb | 2015-12-09 18:23:51 +0100 | [diff] [blame] | 3359 | MVNETA_CPU_TXQ_ACCESS(1) : 0; |
| 3360 | else |
| 3361 | txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) & |
| 3362 | MVNETA_CPU_TXQ_ACCESS_ALL_MASK; |
| 3363 | |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 3364 | mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map); |
| 3365 | |
| 3366 | /* Update the interrupt mask on each CPU according the |
| 3367 | * new mapping |
| 3368 | */ |
| 3369 | smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt, |
| 3370 | pp, true); |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 3371 | i++; |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 3372 | |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 3373 | } |
| 3374 | }; |
| 3375 | |
Sebastian Andrzej Siewior | 84a3f4d | 2016-08-18 14:57:23 +0200 | [diff] [blame] | 3376 | static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node) |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 3377 | { |
Sebastian Andrzej Siewior | 84a3f4d | 2016-08-18 14:57:23 +0200 | [diff] [blame] | 3378 | int other_cpu; |
| 3379 | struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port, |
| 3380 | node_online); |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 3381 | struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu); |
| 3382 | |
Sebastian Andrzej Siewior | 84a3f4d | 2016-08-18 14:57:23 +0200 | [diff] [blame] | 3383 | |
| 3384 | spin_lock(&pp->lock); |
| 3385 | /* |
| 3386 | * Configuring the driver for a new CPU while the driver is |
| 3387 | * stopping is racy, so just avoid it. |
| 3388 | */ |
| 3389 | if (pp->is_stopped) { |
| 3390 | spin_unlock(&pp->lock); |
| 3391 | return 0; |
| 3392 | } |
| 3393 | netif_tx_stop_all_queues(pp->dev); |
| 3394 | |
| 3395 | /* |
| 3396 | * We have to synchronise on tha napi of each CPU except the one |
| 3397 | * just being woken up |
| 3398 | */ |
| 3399 | for_each_online_cpu(other_cpu) { |
| 3400 | if (other_cpu != cpu) { |
| 3401 | struct mvneta_pcpu_port *other_port = |
| 3402 | per_cpu_ptr(pp->ports, other_cpu); |
| 3403 | |
| 3404 | napi_synchronize(&other_port->napi); |
Gregory CLEMENT | 120cfa5 | 2016-02-04 22:09:29 +0100 | [diff] [blame] | 3405 | } |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 3406 | } |
| 3407 | |
Sebastian Andrzej Siewior | 84a3f4d | 2016-08-18 14:57:23 +0200 | [diff] [blame] | 3408 | /* Mask all ethernet port interrupts */ |
| 3409 | on_each_cpu(mvneta_percpu_mask_interrupt, pp, true); |
| 3410 | napi_enable(&port->napi); |
| 3411 | |
| 3412 | /* |
| 3413 | * Enable per-CPU interrupts on the CPU that is |
| 3414 | * brought up. |
| 3415 | */ |
| 3416 | mvneta_percpu_enable(pp); |
| 3417 | |
| 3418 | /* |
| 3419 | * Enable per-CPU interrupt on the one CPU we care |
| 3420 | * about. |
| 3421 | */ |
| 3422 | mvneta_percpu_elect(pp); |
| 3423 | |
| 3424 | /* Unmask all ethernet port interrupts */ |
| 3425 | on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true); |
| 3426 | mvreg_write(pp, MVNETA_INTR_MISC_MASK, |
| 3427 | MVNETA_CAUSE_PHY_STATUS_CHANGE | |
| 3428 | MVNETA_CAUSE_LINK_CHANGE | |
| 3429 | MVNETA_CAUSE_PSC_SYNC_CHANGE); |
| 3430 | netif_tx_start_all_queues(pp->dev); |
| 3431 | spin_unlock(&pp->lock); |
| 3432 | return 0; |
| 3433 | } |
| 3434 | |
| 3435 | static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node) |
| 3436 | { |
| 3437 | struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port, |
| 3438 | node_online); |
| 3439 | struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu); |
| 3440 | |
| 3441 | /* |
| 3442 | * Thanks to this lock we are sure that any pending cpu election is |
| 3443 | * done. |
| 3444 | */ |
| 3445 | spin_lock(&pp->lock); |
| 3446 | /* Mask all ethernet port interrupts */ |
| 3447 | on_each_cpu(mvneta_percpu_mask_interrupt, pp, true); |
| 3448 | spin_unlock(&pp->lock); |
| 3449 | |
| 3450 | napi_synchronize(&port->napi); |
| 3451 | napi_disable(&port->napi); |
| 3452 | /* Disable per-CPU interrupts on the CPU that is brought down. */ |
| 3453 | mvneta_percpu_disable(pp); |
| 3454 | return 0; |
| 3455 | } |
| 3456 | |
| 3457 | static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node) |
| 3458 | { |
| 3459 | struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port, |
| 3460 | node_dead); |
| 3461 | |
| 3462 | /* Check if a new CPU must be elected now this on is down */ |
| 3463 | spin_lock(&pp->lock); |
| 3464 | mvneta_percpu_elect(pp); |
| 3465 | spin_unlock(&pp->lock); |
| 3466 | /* Unmask all ethernet port interrupts */ |
| 3467 | on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true); |
| 3468 | mvreg_write(pp, MVNETA_INTR_MISC_MASK, |
| 3469 | MVNETA_CAUSE_PHY_STATUS_CHANGE | |
| 3470 | MVNETA_CAUSE_LINK_CHANGE | |
| 3471 | MVNETA_CAUSE_PSC_SYNC_CHANGE); |
| 3472 | netif_tx_start_all_queues(pp->dev); |
| 3473 | return 0; |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 3474 | } |
| 3475 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3476 | static int mvneta_open(struct net_device *dev) |
| 3477 | { |
| 3478 | struct mvneta_port *pp = netdev_priv(dev); |
Gregory CLEMENT | 6b125d6 | 2016-02-04 22:09:25 +0100 | [diff] [blame] | 3479 | int ret; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3480 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3481 | pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu); |
willy tarreau | 8ec2cd4 | 2014-01-16 08:20:16 +0100 | [diff] [blame] | 3482 | pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) + |
| 3483 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3484 | |
| 3485 | ret = mvneta_setup_rxqs(pp); |
| 3486 | if (ret) |
| 3487 | return ret; |
| 3488 | |
| 3489 | ret = mvneta_setup_txqs(pp); |
| 3490 | if (ret) |
| 3491 | goto err_cleanup_rxqs; |
| 3492 | |
| 3493 | /* Connect to port interrupt line */ |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3494 | if (pp->neta_armada3700) |
| 3495 | ret = request_irq(pp->dev->irq, mvneta_isr, 0, |
| 3496 | dev->name, pp); |
| 3497 | else |
| 3498 | ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr, |
| 3499 | dev->name, pp->ports); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3500 | if (ret) { |
| 3501 | netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq); |
| 3502 | goto err_cleanup_txqs; |
| 3503 | } |
| 3504 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3505 | if (!pp->neta_armada3700) { |
| 3506 | /* Enable per-CPU interrupt on all the CPU to handle our RX |
| 3507 | * queue interrupts |
| 3508 | */ |
| 3509 | on_each_cpu(mvneta_percpu_enable, pp, true); |
Gregory CLEMENT | 2dcf75e | 2015-12-09 18:23:49 +0100 | [diff] [blame] | 3510 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3511 | pp->is_stopped = false; |
| 3512 | /* Register a CPU notifier to handle the case where our CPU |
| 3513 | * might be taken offline. |
| 3514 | */ |
| 3515 | ret = cpuhp_state_add_instance_nocalls(online_hpstate, |
| 3516 | &pp->node_online); |
| 3517 | if (ret) |
| 3518 | goto err_free_irq; |
Sebastian Andrzej Siewior | 84a3f4d | 2016-08-18 14:57:23 +0200 | [diff] [blame] | 3519 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3520 | ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD, |
| 3521 | &pp->node_dead); |
| 3522 | if (ret) |
| 3523 | goto err_free_online_hp; |
| 3524 | } |
Maxime Ripard | f864288 | 2015-09-25 18:09:38 +0200 | [diff] [blame] | 3525 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3526 | /* In default link is down */ |
| 3527 | netif_carrier_off(pp->dev); |
| 3528 | |
| 3529 | ret = mvneta_mdio_probe(pp); |
| 3530 | if (ret < 0) { |
| 3531 | netdev_err(dev, "cannot probe MDIO bus\n"); |
Sebastian Andrzej Siewior | 84a3f4d | 2016-08-18 14:57:23 +0200 | [diff] [blame] | 3532 | goto err_free_dead_hp; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | mvneta_start_dev(pp); |
| 3536 | |
| 3537 | return 0; |
| 3538 | |
Sebastian Andrzej Siewior | 84a3f4d | 2016-08-18 14:57:23 +0200 | [diff] [blame] | 3539 | err_free_dead_hp: |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3540 | if (!pp->neta_armada3700) |
| 3541 | cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD, |
| 3542 | &pp->node_dead); |
Sebastian Andrzej Siewior | 84a3f4d | 2016-08-18 14:57:23 +0200 | [diff] [blame] | 3543 | err_free_online_hp: |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3544 | if (!pp->neta_armada3700) |
| 3545 | cpuhp_state_remove_instance_nocalls(online_hpstate, |
| 3546 | &pp->node_online); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3547 | err_free_irq: |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3548 | if (pp->neta_armada3700) { |
| 3549 | free_irq(pp->dev->irq, pp); |
| 3550 | } else { |
| 3551 | on_each_cpu(mvneta_percpu_disable, pp, true); |
| 3552 | free_percpu_irq(pp->dev->irq, pp->ports); |
| 3553 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3554 | err_cleanup_txqs: |
| 3555 | mvneta_cleanup_txqs(pp); |
| 3556 | err_cleanup_rxqs: |
| 3557 | mvneta_cleanup_rxqs(pp); |
| 3558 | return ret; |
| 3559 | } |
| 3560 | |
| 3561 | /* Stop the port, free port interrupt line */ |
| 3562 | static int mvneta_stop(struct net_device *dev) |
| 3563 | { |
| 3564 | struct mvneta_port *pp = netdev_priv(dev); |
| 3565 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3566 | if (!pp->neta_armada3700) { |
| 3567 | /* Inform that we are stopping so we don't want to setup the |
| 3568 | * driver for new CPUs in the notifiers. The code of the |
| 3569 | * notifier for CPU online is protected by the same spinlock, |
| 3570 | * so when we get the lock, the notifer work is done. |
| 3571 | */ |
| 3572 | spin_lock(&pp->lock); |
| 3573 | pp->is_stopped = true; |
| 3574 | spin_unlock(&pp->lock); |
Gregory CLEMENT | 1c2722a | 2016-03-12 18:44:17 +0100 | [diff] [blame] | 3575 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3576 | mvneta_stop_dev(pp); |
| 3577 | mvneta_mdio_remove(pp); |
Sebastian Andrzej Siewior | 84a3f4d | 2016-08-18 14:57:23 +0200 | [diff] [blame] | 3578 | |
Dan Carpenter | d26aac2 | 2016-12-07 14:32:17 +0300 | [diff] [blame] | 3579 | cpuhp_state_remove_instance_nocalls(online_hpstate, |
| 3580 | &pp->node_online); |
| 3581 | cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD, |
| 3582 | &pp->node_dead); |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3583 | on_each_cpu(mvneta_percpu_disable, pp, true); |
| 3584 | free_percpu_irq(dev->irq, pp->ports); |
| 3585 | } else { |
| 3586 | mvneta_stop_dev(pp); |
| 3587 | mvneta_mdio_remove(pp); |
| 3588 | free_irq(dev->irq, pp); |
| 3589 | } |
| 3590 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3591 | mvneta_cleanup_rxqs(pp); |
| 3592 | mvneta_cleanup_txqs(pp); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3593 | |
| 3594 | return 0; |
| 3595 | } |
| 3596 | |
Thomas Petazzoni | 15f5945 | 2013-09-04 16:26:52 +0200 | [diff] [blame] | 3597 | static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 3598 | { |
Philippe Reynes | c6c022e | 2016-07-30 17:42:11 +0200 | [diff] [blame] | 3599 | if (!dev->phydev) |
Thomas Petazzoni | 15f5945 | 2013-09-04 16:26:52 +0200 | [diff] [blame] | 3600 | return -ENOTSUPP; |
| 3601 | |
Philippe Reynes | c6c022e | 2016-07-30 17:42:11 +0200 | [diff] [blame] | 3602 | return phy_mii_ioctl(dev->phydev, ifr, cmd); |
Thomas Petazzoni | 15f5945 | 2013-09-04 16:26:52 +0200 | [diff] [blame] | 3603 | } |
| 3604 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3605 | /* Ethtool methods */ |
| 3606 | |
Philippe Reynes | 013ad40 | 2016-07-30 17:42:12 +0200 | [diff] [blame] | 3607 | /* Set link ksettings (phy address, speed) for ethtools */ |
Baoyou Xie | 2dc0d2b | 2016-09-25 17:20:41 +0800 | [diff] [blame] | 3608 | static int |
| 3609 | mvneta_ethtool_set_link_ksettings(struct net_device *ndev, |
| 3610 | const struct ethtool_link_ksettings *cmd) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3611 | { |
Philippe Reynes | 013ad40 | 2016-07-30 17:42:12 +0200 | [diff] [blame] | 3612 | struct mvneta_port *pp = netdev_priv(ndev); |
| 3613 | struct phy_device *phydev = ndev->phydev; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3614 | |
Stas Sergeev | 0c0744f | 2015-12-02 20:35:11 +0300 | [diff] [blame] | 3615 | if (!phydev) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3616 | return -ENODEV; |
| 3617 | |
Philippe Reynes | 013ad40 | 2016-07-30 17:42:12 +0200 | [diff] [blame] | 3618 | if ((cmd->base.autoneg == AUTONEG_ENABLE) != pp->use_inband_status) { |
Stas Sergeev | 0c0744f | 2015-12-02 20:35:11 +0300 | [diff] [blame] | 3619 | u32 val; |
| 3620 | |
Philippe Reynes | 013ad40 | 2016-07-30 17:42:12 +0200 | [diff] [blame] | 3621 | mvneta_set_autoneg(pp, cmd->base.autoneg == AUTONEG_ENABLE); |
Stas Sergeev | 0c0744f | 2015-12-02 20:35:11 +0300 | [diff] [blame] | 3622 | |
Philippe Reynes | 013ad40 | 2016-07-30 17:42:12 +0200 | [diff] [blame] | 3623 | if (cmd->base.autoneg == AUTONEG_DISABLE) { |
Stas Sergeev | 0c0744f | 2015-12-02 20:35:11 +0300 | [diff] [blame] | 3624 | val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG); |
| 3625 | val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED | |
| 3626 | MVNETA_GMAC_CONFIG_GMII_SPEED | |
| 3627 | MVNETA_GMAC_CONFIG_FULL_DUPLEX); |
| 3628 | |
| 3629 | if (phydev->duplex) |
| 3630 | val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX; |
| 3631 | |
| 3632 | if (phydev->speed == SPEED_1000) |
| 3633 | val |= MVNETA_GMAC_CONFIG_GMII_SPEED; |
| 3634 | else if (phydev->speed == SPEED_100) |
| 3635 | val |= MVNETA_GMAC_CONFIG_MII_SPEED; |
| 3636 | |
| 3637 | mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val); |
| 3638 | } |
| 3639 | |
Philippe Reynes | 013ad40 | 2016-07-30 17:42:12 +0200 | [diff] [blame] | 3640 | pp->use_inband_status = (cmd->base.autoneg == AUTONEG_ENABLE); |
Stas Sergeev | 0c0744f | 2015-12-02 20:35:11 +0300 | [diff] [blame] | 3641 | netdev_info(pp->dev, "autoneg status set to %i\n", |
| 3642 | pp->use_inband_status); |
| 3643 | |
Philippe Reynes | 013ad40 | 2016-07-30 17:42:12 +0200 | [diff] [blame] | 3644 | if (netif_running(ndev)) { |
Stas Sergeev | 0c0744f | 2015-12-02 20:35:11 +0300 | [diff] [blame] | 3645 | mvneta_port_down(pp); |
| 3646 | mvneta_port_up(pp); |
| 3647 | } |
| 3648 | } |
| 3649 | |
Philippe Reynes | 013ad40 | 2016-07-30 17:42:12 +0200 | [diff] [blame] | 3650 | return phy_ethtool_ksettings_set(ndev->phydev, cmd); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3651 | } |
| 3652 | |
| 3653 | /* Set interrupt coalescing for ethtools */ |
| 3654 | static int mvneta_ethtool_set_coalesce(struct net_device *dev, |
| 3655 | struct ethtool_coalesce *c) |
| 3656 | { |
| 3657 | struct mvneta_port *pp = netdev_priv(dev); |
| 3658 | int queue; |
| 3659 | |
| 3660 | for (queue = 0; queue < rxq_number; queue++) { |
| 3661 | struct mvneta_rx_queue *rxq = &pp->rxqs[queue]; |
| 3662 | rxq->time_coal = c->rx_coalesce_usecs; |
| 3663 | rxq->pkts_coal = c->rx_max_coalesced_frames; |
| 3664 | mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal); |
| 3665 | mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal); |
| 3666 | } |
| 3667 | |
| 3668 | for (queue = 0; queue < txq_number; queue++) { |
| 3669 | struct mvneta_tx_queue *txq = &pp->txqs[queue]; |
| 3670 | txq->done_pkts_coal = c->tx_max_coalesced_frames; |
| 3671 | mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal); |
| 3672 | } |
| 3673 | |
| 3674 | return 0; |
| 3675 | } |
| 3676 | |
| 3677 | /* get coalescing for ethtools */ |
| 3678 | static int mvneta_ethtool_get_coalesce(struct net_device *dev, |
| 3679 | struct ethtool_coalesce *c) |
| 3680 | { |
| 3681 | struct mvneta_port *pp = netdev_priv(dev); |
| 3682 | |
| 3683 | c->rx_coalesce_usecs = pp->rxqs[0].time_coal; |
| 3684 | c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal; |
| 3685 | |
| 3686 | c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal; |
| 3687 | return 0; |
| 3688 | } |
| 3689 | |
| 3690 | |
| 3691 | static void mvneta_ethtool_get_drvinfo(struct net_device *dev, |
| 3692 | struct ethtool_drvinfo *drvinfo) |
| 3693 | { |
| 3694 | strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME, |
| 3695 | sizeof(drvinfo->driver)); |
| 3696 | strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION, |
| 3697 | sizeof(drvinfo->version)); |
| 3698 | strlcpy(drvinfo->bus_info, dev_name(&dev->dev), |
| 3699 | sizeof(drvinfo->bus_info)); |
| 3700 | } |
| 3701 | |
| 3702 | |
| 3703 | static void mvneta_ethtool_get_ringparam(struct net_device *netdev, |
| 3704 | struct ethtool_ringparam *ring) |
| 3705 | { |
| 3706 | struct mvneta_port *pp = netdev_priv(netdev); |
| 3707 | |
| 3708 | ring->rx_max_pending = MVNETA_MAX_RXD; |
| 3709 | ring->tx_max_pending = MVNETA_MAX_TXD; |
| 3710 | ring->rx_pending = pp->rx_ring_size; |
| 3711 | ring->tx_pending = pp->tx_ring_size; |
| 3712 | } |
| 3713 | |
| 3714 | static int mvneta_ethtool_set_ringparam(struct net_device *dev, |
| 3715 | struct ethtool_ringparam *ring) |
| 3716 | { |
| 3717 | struct mvneta_port *pp = netdev_priv(dev); |
| 3718 | |
| 3719 | if ((ring->rx_pending == 0) || (ring->tx_pending == 0)) |
| 3720 | return -EINVAL; |
| 3721 | pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ? |
| 3722 | ring->rx_pending : MVNETA_MAX_RXD; |
Ezequiel Garcia | 8eef5f9 | 2014-05-30 13:40:05 -0300 | [diff] [blame] | 3723 | |
| 3724 | pp->tx_ring_size = clamp_t(u16, ring->tx_pending, |
| 3725 | MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD); |
| 3726 | if (pp->tx_ring_size != ring->tx_pending) |
| 3727 | netdev_warn(dev, "TX queue size set to %u (requested %u)\n", |
| 3728 | pp->tx_ring_size, ring->tx_pending); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3729 | |
| 3730 | if (netif_running(dev)) { |
| 3731 | mvneta_stop(dev); |
| 3732 | if (mvneta_open(dev)) { |
| 3733 | netdev_err(dev, |
| 3734 | "error on opening device after ring param change\n"); |
| 3735 | return -ENOMEM; |
| 3736 | } |
| 3737 | } |
| 3738 | |
| 3739 | return 0; |
| 3740 | } |
| 3741 | |
Russell King | 9b0cdef | 2015-10-22 18:37:30 +0100 | [diff] [blame] | 3742 | static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset, |
| 3743 | u8 *data) |
| 3744 | { |
| 3745 | if (sset == ETH_SS_STATS) { |
| 3746 | int i; |
| 3747 | |
| 3748 | for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++) |
| 3749 | memcpy(data + i * ETH_GSTRING_LEN, |
| 3750 | mvneta_statistics[i].name, ETH_GSTRING_LEN); |
| 3751 | } |
| 3752 | } |
| 3753 | |
| 3754 | static void mvneta_ethtool_update_stats(struct mvneta_port *pp) |
| 3755 | { |
| 3756 | const struct mvneta_statistic *s; |
| 3757 | void __iomem *base = pp->base; |
| 3758 | u32 high, low, val; |
Jisheng Zhang | 2c83229 | 2016-01-20 16:36:25 +0800 | [diff] [blame] | 3759 | u64 val64; |
Russell King | 9b0cdef | 2015-10-22 18:37:30 +0100 | [diff] [blame] | 3760 | int i; |
| 3761 | |
| 3762 | for (i = 0, s = mvneta_statistics; |
| 3763 | s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics); |
| 3764 | s++, i++) { |
Russell King | 9b0cdef | 2015-10-22 18:37:30 +0100 | [diff] [blame] | 3765 | switch (s->type) { |
| 3766 | case T_REG_32: |
| 3767 | val = readl_relaxed(base + s->offset); |
Jisheng Zhang | 2c83229 | 2016-01-20 16:36:25 +0800 | [diff] [blame] | 3768 | pp->ethtool_stats[i] += val; |
Russell King | 9b0cdef | 2015-10-22 18:37:30 +0100 | [diff] [blame] | 3769 | break; |
| 3770 | case T_REG_64: |
| 3771 | /* Docs say to read low 32-bit then high */ |
| 3772 | low = readl_relaxed(base + s->offset); |
| 3773 | high = readl_relaxed(base + s->offset + 4); |
Jisheng Zhang | 2c83229 | 2016-01-20 16:36:25 +0800 | [diff] [blame] | 3774 | val64 = (u64)high << 32 | low; |
| 3775 | pp->ethtool_stats[i] += val64; |
Russell King | 9b0cdef | 2015-10-22 18:37:30 +0100 | [diff] [blame] | 3776 | break; |
| 3777 | } |
Russell King | 9b0cdef | 2015-10-22 18:37:30 +0100 | [diff] [blame] | 3778 | } |
| 3779 | } |
| 3780 | |
| 3781 | static void mvneta_ethtool_get_stats(struct net_device *dev, |
| 3782 | struct ethtool_stats *stats, u64 *data) |
| 3783 | { |
| 3784 | struct mvneta_port *pp = netdev_priv(dev); |
| 3785 | int i; |
| 3786 | |
| 3787 | mvneta_ethtool_update_stats(pp); |
| 3788 | |
| 3789 | for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++) |
| 3790 | *data++ = pp->ethtool_stats[i]; |
| 3791 | } |
| 3792 | |
| 3793 | static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset) |
| 3794 | { |
| 3795 | if (sset == ETH_SS_STATS) |
| 3796 | return ARRAY_SIZE(mvneta_statistics); |
| 3797 | return -EOPNOTSUPP; |
| 3798 | } |
| 3799 | |
Gregory CLEMENT | 9a401de | 2015-12-09 18:23:50 +0100 | [diff] [blame] | 3800 | static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev) |
| 3801 | { |
| 3802 | return MVNETA_RSS_LU_TABLE_SIZE; |
| 3803 | } |
| 3804 | |
| 3805 | static int mvneta_ethtool_get_rxnfc(struct net_device *dev, |
| 3806 | struct ethtool_rxnfc *info, |
| 3807 | u32 *rules __always_unused) |
| 3808 | { |
| 3809 | switch (info->cmd) { |
| 3810 | case ETHTOOL_GRXRINGS: |
| 3811 | info->data = rxq_number; |
| 3812 | return 0; |
| 3813 | case ETHTOOL_GRXFH: |
| 3814 | return -EOPNOTSUPP; |
| 3815 | default: |
| 3816 | return -EOPNOTSUPP; |
| 3817 | } |
| 3818 | } |
| 3819 | |
| 3820 | static int mvneta_config_rss(struct mvneta_port *pp) |
| 3821 | { |
| 3822 | int cpu; |
| 3823 | u32 val; |
| 3824 | |
| 3825 | netif_tx_stop_all_queues(pp->dev); |
| 3826 | |
Gregory CLEMENT | 6b125d6 | 2016-02-04 22:09:25 +0100 | [diff] [blame] | 3827 | on_each_cpu(mvneta_percpu_mask_interrupt, pp, true); |
Gregory CLEMENT | 9a401de | 2015-12-09 18:23:50 +0100 | [diff] [blame] | 3828 | |
| 3829 | /* We have to synchronise on the napi of each CPU */ |
| 3830 | for_each_online_cpu(cpu) { |
| 3831 | struct mvneta_pcpu_port *pcpu_port = |
| 3832 | per_cpu_ptr(pp->ports, cpu); |
| 3833 | |
| 3834 | napi_synchronize(&pcpu_port->napi); |
| 3835 | napi_disable(&pcpu_port->napi); |
| 3836 | } |
| 3837 | |
| 3838 | pp->rxq_def = pp->indir[0]; |
| 3839 | |
| 3840 | /* Update unicast mapping */ |
| 3841 | mvneta_set_rx_mode(pp->dev); |
| 3842 | |
| 3843 | /* Update val of portCfg register accordingly with all RxQueue types */ |
| 3844 | val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def); |
| 3845 | mvreg_write(pp, MVNETA_PORT_CONFIG, val); |
| 3846 | |
| 3847 | /* Update the elected CPU matching the new rxq_def */ |
Gregory CLEMENT | 120cfa5 | 2016-02-04 22:09:29 +0100 | [diff] [blame] | 3848 | spin_lock(&pp->lock); |
Gregory CLEMENT | 9a401de | 2015-12-09 18:23:50 +0100 | [diff] [blame] | 3849 | mvneta_percpu_elect(pp); |
Gregory CLEMENT | 120cfa5 | 2016-02-04 22:09:29 +0100 | [diff] [blame] | 3850 | spin_unlock(&pp->lock); |
Gregory CLEMENT | 9a401de | 2015-12-09 18:23:50 +0100 | [diff] [blame] | 3851 | |
| 3852 | /* We have to synchronise on the napi of each CPU */ |
| 3853 | for_each_online_cpu(cpu) { |
| 3854 | struct mvneta_pcpu_port *pcpu_port = |
| 3855 | per_cpu_ptr(pp->ports, cpu); |
| 3856 | |
| 3857 | napi_enable(&pcpu_port->napi); |
| 3858 | } |
| 3859 | |
| 3860 | netif_tx_start_all_queues(pp->dev); |
| 3861 | |
| 3862 | return 0; |
| 3863 | } |
| 3864 | |
| 3865 | static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir, |
| 3866 | const u8 *key, const u8 hfunc) |
| 3867 | { |
| 3868 | struct mvneta_port *pp = netdev_priv(dev); |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3869 | |
| 3870 | /* Current code for Armada 3700 doesn't support RSS features yet */ |
| 3871 | if (pp->neta_armada3700) |
| 3872 | return -EOPNOTSUPP; |
| 3873 | |
Gregory CLEMENT | 9a401de | 2015-12-09 18:23:50 +0100 | [diff] [blame] | 3874 | /* We require at least one supported parameter to be changed |
| 3875 | * and no change in any of the unsupported parameters |
| 3876 | */ |
| 3877 | if (key || |
| 3878 | (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)) |
| 3879 | return -EOPNOTSUPP; |
| 3880 | |
| 3881 | if (!indir) |
| 3882 | return 0; |
| 3883 | |
| 3884 | memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE); |
| 3885 | |
| 3886 | return mvneta_config_rss(pp); |
| 3887 | } |
| 3888 | |
| 3889 | static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, |
| 3890 | u8 *hfunc) |
| 3891 | { |
| 3892 | struct mvneta_port *pp = netdev_priv(dev); |
| 3893 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 3894 | /* Current code for Armada 3700 doesn't support RSS features yet */ |
| 3895 | if (pp->neta_armada3700) |
| 3896 | return -EOPNOTSUPP; |
| 3897 | |
Gregory CLEMENT | 9a401de | 2015-12-09 18:23:50 +0100 | [diff] [blame] | 3898 | if (hfunc) |
| 3899 | *hfunc = ETH_RSS_HASH_TOP; |
| 3900 | |
| 3901 | if (!indir) |
| 3902 | return 0; |
| 3903 | |
| 3904 | memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE); |
| 3905 | |
| 3906 | return 0; |
| 3907 | } |
| 3908 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3909 | static const struct net_device_ops mvneta_netdev_ops = { |
| 3910 | .ndo_open = mvneta_open, |
| 3911 | .ndo_stop = mvneta_stop, |
| 3912 | .ndo_start_xmit = mvneta_tx, |
| 3913 | .ndo_set_rx_mode = mvneta_set_rx_mode, |
| 3914 | .ndo_set_mac_address = mvneta_set_mac_addr, |
| 3915 | .ndo_change_mtu = mvneta_change_mtu, |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 3916 | .ndo_fix_features = mvneta_fix_features, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3917 | .ndo_get_stats64 = mvneta_get_stats64, |
Thomas Petazzoni | 15f5945 | 2013-09-04 16:26:52 +0200 | [diff] [blame] | 3918 | .ndo_do_ioctl = mvneta_ioctl, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3919 | }; |
| 3920 | |
| 3921 | const struct ethtool_ops mvneta_eth_tool_ops = { |
Florian Fainelli | 5489ee8 | 2016-11-15 11:19:47 -0800 | [diff] [blame] | 3922 | .nway_reset = phy_ethtool_nway_reset, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3923 | .get_link = ethtool_op_get_link, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3924 | .set_coalesce = mvneta_ethtool_set_coalesce, |
| 3925 | .get_coalesce = mvneta_ethtool_get_coalesce, |
| 3926 | .get_drvinfo = mvneta_ethtool_get_drvinfo, |
| 3927 | .get_ringparam = mvneta_ethtool_get_ringparam, |
| 3928 | .set_ringparam = mvneta_ethtool_set_ringparam, |
Russell King | 9b0cdef | 2015-10-22 18:37:30 +0100 | [diff] [blame] | 3929 | .get_strings = mvneta_ethtool_get_strings, |
| 3930 | .get_ethtool_stats = mvneta_ethtool_get_stats, |
| 3931 | .get_sset_count = mvneta_ethtool_get_sset_count, |
Gregory CLEMENT | 9a401de | 2015-12-09 18:23:50 +0100 | [diff] [blame] | 3932 | .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size, |
| 3933 | .get_rxnfc = mvneta_ethtool_get_rxnfc, |
| 3934 | .get_rxfh = mvneta_ethtool_get_rxfh, |
| 3935 | .set_rxfh = mvneta_ethtool_set_rxfh, |
Philippe Reynes | 013ad40 | 2016-07-30 17:42:12 +0200 | [diff] [blame] | 3936 | .get_link_ksettings = phy_ethtool_get_link_ksettings, |
| 3937 | .set_link_ksettings = mvneta_ethtool_set_link_ksettings, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3938 | }; |
| 3939 | |
| 3940 | /* Initialize hw */ |
Ezequiel Garcia | 9672850 | 2014-05-22 20:06:59 -0300 | [diff] [blame] | 3941 | static int mvneta_init(struct device *dev, struct mvneta_port *pp) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3942 | { |
| 3943 | int queue; |
| 3944 | |
| 3945 | /* Disable port */ |
| 3946 | mvneta_port_disable(pp); |
| 3947 | |
| 3948 | /* Set port default values */ |
| 3949 | mvneta_defaults_set(pp); |
| 3950 | |
Ezequiel Garcia | 9672850 | 2014-05-22 20:06:59 -0300 | [diff] [blame] | 3951 | pp->txqs = devm_kcalloc(dev, txq_number, sizeof(struct mvneta_tx_queue), |
| 3952 | GFP_KERNEL); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3953 | if (!pp->txqs) |
| 3954 | return -ENOMEM; |
| 3955 | |
| 3956 | /* Initialize TX descriptor rings */ |
| 3957 | for (queue = 0; queue < txq_number; queue++) { |
| 3958 | struct mvneta_tx_queue *txq = &pp->txqs[queue]; |
| 3959 | txq->id = queue; |
| 3960 | txq->size = pp->tx_ring_size; |
| 3961 | txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS; |
| 3962 | } |
| 3963 | |
Ezequiel Garcia | 9672850 | 2014-05-22 20:06:59 -0300 | [diff] [blame] | 3964 | pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(struct mvneta_rx_queue), |
| 3965 | GFP_KERNEL); |
| 3966 | if (!pp->rxqs) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3967 | return -ENOMEM; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3968 | |
| 3969 | /* Create Rx descriptor rings */ |
| 3970 | for (queue = 0; queue < rxq_number; queue++) { |
| 3971 | struct mvneta_rx_queue *rxq = &pp->rxqs[queue]; |
| 3972 | rxq->id = queue; |
| 3973 | rxq->size = pp->rx_ring_size; |
| 3974 | rxq->pkts_coal = MVNETA_RX_COAL_PKTS; |
| 3975 | rxq->time_coal = MVNETA_RX_COAL_USEC; |
Gregory CLEMENT | f88bee1 | 2016-12-01 18:03:06 +0100 | [diff] [blame] | 3976 | rxq->buf_virt_addr = devm_kmalloc(pp->dev->dev.parent, |
| 3977 | rxq->size * sizeof(void *), |
| 3978 | GFP_KERNEL); |
| 3979 | if (!rxq->buf_virt_addr) |
| 3980 | return -ENOMEM; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3981 | } |
| 3982 | |
| 3983 | return 0; |
| 3984 | } |
| 3985 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3986 | /* platform glue : initialize decoding windows */ |
Greg KH | 03ce758 | 2012-12-21 13:42:15 +0000 | [diff] [blame] | 3987 | static void mvneta_conf_mbus_windows(struct mvneta_port *pp, |
| 3988 | const struct mbus_dram_target_info *dram) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 3989 | { |
| 3990 | u32 win_enable; |
| 3991 | u32 win_protect; |
| 3992 | int i; |
| 3993 | |
| 3994 | for (i = 0; i < 6; i++) { |
| 3995 | mvreg_write(pp, MVNETA_WIN_BASE(i), 0); |
| 3996 | mvreg_write(pp, MVNETA_WIN_SIZE(i), 0); |
| 3997 | |
| 3998 | if (i < 4) |
| 3999 | mvreg_write(pp, MVNETA_WIN_REMAP(i), 0); |
| 4000 | } |
| 4001 | |
| 4002 | win_enable = 0x3f; |
| 4003 | win_protect = 0; |
| 4004 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 4005 | if (dram) { |
| 4006 | for (i = 0; i < dram->num_cs; i++) { |
| 4007 | const struct mbus_dram_window *cs = dram->cs + i; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4008 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 4009 | mvreg_write(pp, MVNETA_WIN_BASE(i), |
| 4010 | (cs->base & 0xffff0000) | |
| 4011 | (cs->mbus_attr << 8) | |
| 4012 | dram->mbus_dram_target_id); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4013 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 4014 | mvreg_write(pp, MVNETA_WIN_SIZE(i), |
| 4015 | (cs->size - 1) & 0xffff0000); |
| 4016 | |
| 4017 | win_enable &= ~(1 << i); |
| 4018 | win_protect |= 3 << (2 * i); |
| 4019 | } |
| 4020 | } else { |
| 4021 | /* For Armada3700 open default 4GB Mbus window, leaving |
| 4022 | * arbitration of target/attribute to a different layer |
| 4023 | * of configuration. |
| 4024 | */ |
| 4025 | mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000); |
| 4026 | win_enable &= ~BIT(0); |
| 4027 | win_protect = 3; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4028 | } |
| 4029 | |
| 4030 | mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable); |
Marcin Wojtas | db6ba9a | 2015-11-30 13:27:41 +0100 | [diff] [blame] | 4031 | mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | /* Power up the port */ |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 4035 | 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] | 4036 | { |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 4037 | u32 ctrl; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4038 | |
| 4039 | /* MAC Cause register should be cleared */ |
| 4040 | mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0); |
| 4041 | |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 4042 | ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4043 | |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 4044 | /* Even though it might look weird, when we're configured in |
| 4045 | * SGMII or QSGMII mode, the RGMII bit needs to be set. |
| 4046 | */ |
| 4047 | switch(phy_mode) { |
| 4048 | case PHY_INTERFACE_MODE_QSGMII: |
| 4049 | mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO); |
| 4050 | ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII; |
| 4051 | break; |
| 4052 | case PHY_INTERFACE_MODE_SGMII: |
| 4053 | mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO); |
| 4054 | ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII; |
| 4055 | break; |
| 4056 | case PHY_INTERFACE_MODE_RGMII: |
| 4057 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 4058 | ctrl |= MVNETA_GMAC2_PORT_RGMII; |
| 4059 | break; |
| 4060 | default: |
| 4061 | return -EINVAL; |
| 4062 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4063 | |
| 4064 | /* Cancel Port Reset */ |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 4065 | ctrl &= ~MVNETA_GMAC2_PORT_RESET; |
| 4066 | mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4067 | |
| 4068 | while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) & |
| 4069 | MVNETA_GMAC2_PORT_RESET) != 0) |
| 4070 | continue; |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 4071 | |
| 4072 | return 0; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | /* Device initialization routine */ |
Greg KH | 03ce758 | 2012-12-21 13:42:15 +0000 | [diff] [blame] | 4076 | static int mvneta_probe(struct platform_device *pdev) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4077 | { |
| 4078 | const struct mbus_dram_target_info *dram_target_info; |
Thomas Petazzoni | c3f0dd3 | 2014-03-27 11:39:29 +0100 | [diff] [blame] | 4079 | struct resource *res; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4080 | struct device_node *dn = pdev->dev.of_node; |
| 4081 | struct device_node *phy_node; |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 4082 | struct device_node *bm_node; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4083 | struct mvneta_port *pp; |
| 4084 | struct net_device *dev; |
Thomas Petazzoni | 8cc3e43 | 2013-06-04 04:52:23 +0000 | [diff] [blame] | 4085 | const char *dt_mac_addr; |
| 4086 | char hw_mac_addr[ETH_ALEN]; |
| 4087 | const char *mac_from; |
Stas Sergeev | f8af8e6 | 2015-07-20 17:49:58 -0700 | [diff] [blame] | 4088 | const char *managed; |
Marcin Wojtas | 9110ee0 | 2015-11-30 13:27:45 +0100 | [diff] [blame] | 4089 | int tx_csum_limit; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4090 | int phy_mode; |
| 4091 | int err; |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 4092 | int cpu; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4093 | |
Willy Tarreau | ee40a11 | 2013-04-11 23:00:37 +0200 | [diff] [blame] | 4094 | dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4095 | if (!dev) |
| 4096 | return -ENOMEM; |
| 4097 | |
| 4098 | dev->irq = irq_of_parse_and_map(dn, 0); |
| 4099 | if (dev->irq == 0) { |
| 4100 | err = -EINVAL; |
| 4101 | goto err_free_netdev; |
| 4102 | } |
| 4103 | |
| 4104 | phy_node = of_parse_phandle(dn, "phy", 0); |
| 4105 | if (!phy_node) { |
Thomas Petazzoni | 83895be | 2014-05-16 16:14:06 +0200 | [diff] [blame] | 4106 | if (!of_phy_is_fixed_link(dn)) { |
| 4107 | dev_err(&pdev->dev, "no PHY specified\n"); |
| 4108 | err = -ENODEV; |
| 4109 | goto err_free_irq; |
| 4110 | } |
| 4111 | |
| 4112 | err = of_phy_register_fixed_link(dn); |
| 4113 | if (err < 0) { |
| 4114 | dev_err(&pdev->dev, "cannot register fixed PHY\n"); |
| 4115 | goto err_free_irq; |
| 4116 | } |
| 4117 | |
| 4118 | /* In the case of a fixed PHY, the DT node associated |
| 4119 | * to the PHY is the Ethernet MAC DT node. |
| 4120 | */ |
Uwe Kleine-König | c891c24 | 2014-08-07 21:58:46 +0200 | [diff] [blame] | 4121 | phy_node = of_node_get(dn); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | phy_mode = of_get_phy_mode(dn); |
| 4125 | if (phy_mode < 0) { |
| 4126 | dev_err(&pdev->dev, "incorrect phy-mode\n"); |
| 4127 | err = -EINVAL; |
Uwe Kleine-König | c891c24 | 2014-08-07 21:58:46 +0200 | [diff] [blame] | 4128 | goto err_put_phy_node; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4129 | } |
| 4130 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4131 | dev->tx_queue_len = MVNETA_MAX_TXD; |
| 4132 | dev->watchdog_timeo = 5 * HZ; |
| 4133 | dev->netdev_ops = &mvneta_netdev_ops; |
| 4134 | |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 4135 | dev->ethtool_ops = &mvneta_eth_tool_ops; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4136 | |
| 4137 | pp = netdev_priv(dev); |
Gregory CLEMENT | 1c2722a | 2016-03-12 18:44:17 +0100 | [diff] [blame] | 4138 | spin_lock_init(&pp->lock); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4139 | pp->phy_node = phy_node; |
| 4140 | pp->phy_interface = phy_mode; |
Stas Sergeev | f8af8e6 | 2015-07-20 17:49:58 -0700 | [diff] [blame] | 4141 | |
| 4142 | err = of_property_read_string(dn, "managed", &managed); |
| 4143 | pp->use_inband_status = (err == 0 && |
| 4144 | strcmp(managed, "in-band-status") == 0); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4145 | |
Gregory CLEMENT | 90b74c0 | 2015-12-09 18:23:48 +0100 | [diff] [blame] | 4146 | pp->rxq_def = rxq_def; |
| 4147 | |
Marcin Wojtas | 8d5047c | 2016-12-01 18:03:07 +0100 | [diff] [blame] | 4148 | /* Set RX packet offset correction for platforms, whose |
| 4149 | * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit |
| 4150 | * platforms and 0B for 32-bit ones. |
| 4151 | */ |
| 4152 | pp->rx_offset_correction = |
| 4153 | max(0, NET_SKB_PAD - MVNETA_RX_PKT_OFFSET_CORRECTION); |
| 4154 | |
Gregory CLEMENT | 9a401de | 2015-12-09 18:23:50 +0100 | [diff] [blame] | 4155 | pp->indir[0] = rxq_def; |
| 4156 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 4157 | /* Get special SoC configurations */ |
| 4158 | if (of_device_is_compatible(dn, "marvell,armada-3700-neta")) |
| 4159 | pp->neta_armada3700 = true; |
| 4160 | |
Jisheng Zhang | 2804ba4 | 2016-01-20 19:27:23 +0800 | [diff] [blame] | 4161 | pp->clk = devm_clk_get(&pdev->dev, "core"); |
| 4162 | if (IS_ERR(pp->clk)) |
| 4163 | pp->clk = devm_clk_get(&pdev->dev, NULL); |
Thomas Petazzoni | 189dd62 | 2012-11-19 14:15:25 +0100 | [diff] [blame] | 4164 | if (IS_ERR(pp->clk)) { |
| 4165 | err = PTR_ERR(pp->clk); |
Uwe Kleine-König | c891c24 | 2014-08-07 21:58:46 +0200 | [diff] [blame] | 4166 | goto err_put_phy_node; |
Thomas Petazzoni | 189dd62 | 2012-11-19 14:15:25 +0100 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | clk_prepare_enable(pp->clk); |
| 4170 | |
Jisheng Zhang | 15cc4a4 | 2016-01-20 19:27:24 +0800 | [diff] [blame] | 4171 | pp->clk_bus = devm_clk_get(&pdev->dev, "bus"); |
| 4172 | if (!IS_ERR(pp->clk_bus)) |
| 4173 | clk_prepare_enable(pp->clk_bus); |
| 4174 | |
Thomas Petazzoni | c3f0dd3 | 2014-03-27 11:39:29 +0100 | [diff] [blame] | 4175 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 4176 | pp->base = devm_ioremap_resource(&pdev->dev, res); |
| 4177 | if (IS_ERR(pp->base)) { |
| 4178 | err = PTR_ERR(pp->base); |
Arnaud Patard \(Rtp\) | 5445eaf | 2013-07-29 21:56:48 +0200 | [diff] [blame] | 4179 | goto err_clk; |
| 4180 | } |
| 4181 | |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 4182 | /* Alloc per-cpu port structure */ |
| 4183 | pp->ports = alloc_percpu(struct mvneta_pcpu_port); |
| 4184 | if (!pp->ports) { |
| 4185 | err = -ENOMEM; |
| 4186 | goto err_clk; |
| 4187 | } |
| 4188 | |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 4189 | /* Alloc per-cpu stats */ |
WANG Cong | 1c213bd | 2014-02-13 11:46:28 -0800 | [diff] [blame] | 4190 | pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats); |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 4191 | if (!pp->stats) { |
| 4192 | err = -ENOMEM; |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 4193 | goto err_free_ports; |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 4194 | } |
| 4195 | |
Thomas Petazzoni | 8cc3e43 | 2013-06-04 04:52:23 +0000 | [diff] [blame] | 4196 | dt_mac_addr = of_get_mac_address(dn); |
Luka Perkov | 6c7a9a3 | 2013-10-30 00:10:01 +0100 | [diff] [blame] | 4197 | if (dt_mac_addr) { |
Thomas Petazzoni | 8cc3e43 | 2013-06-04 04:52:23 +0000 | [diff] [blame] | 4198 | mac_from = "device tree"; |
| 4199 | memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN); |
| 4200 | } else { |
| 4201 | mvneta_get_mac_addr(pp, hw_mac_addr); |
| 4202 | if (is_valid_ether_addr(hw_mac_addr)) { |
| 4203 | mac_from = "hardware"; |
| 4204 | memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN); |
| 4205 | } else { |
| 4206 | mac_from = "random"; |
| 4207 | eth_hw_addr_random(dev); |
| 4208 | } |
| 4209 | } |
| 4210 | |
Marcin Wojtas | 9110ee0 | 2015-11-30 13:27:45 +0100 | [diff] [blame] | 4211 | if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) { |
| 4212 | if (tx_csum_limit < 0 || |
| 4213 | tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) { |
| 4214 | tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE; |
| 4215 | dev_info(&pdev->dev, |
| 4216 | "Wrong TX csum limit in DT, set to %dB\n", |
| 4217 | MVNETA_TX_CSUM_DEF_SIZE); |
| 4218 | } |
| 4219 | } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) { |
| 4220 | tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE; |
| 4221 | } else { |
| 4222 | tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE; |
| 4223 | } |
| 4224 | |
| 4225 | pp->tx_csum_limit = tx_csum_limit; |
Simon Guinot | b65657f | 2015-06-30 16:20:22 +0200 | [diff] [blame] | 4226 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 4227 | dram_target_info = mv_mbus_dram_info(); |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 4228 | /* Armada3700 requires setting default configuration of Mbus |
| 4229 | * windows, however without using filled mbus_dram_target_info |
| 4230 | * structure. |
| 4231 | */ |
| 4232 | if (dram_target_info || pp->neta_armada3700) |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 4233 | mvneta_conf_mbus_windows(pp, dram_target_info); |
| 4234 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4235 | pp->tx_ring_size = MVNETA_MAX_TXD; |
| 4236 | pp->rx_ring_size = MVNETA_MAX_RXD; |
| 4237 | |
| 4238 | pp->dev = dev; |
| 4239 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 4240 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 4241 | pp->id = global_port_id++; |
| 4242 | |
| 4243 | /* Obtain access to BM resources if enabled and already initialized */ |
| 4244 | bm_node = of_parse_phandle(dn, "buffer-manager", 0); |
| 4245 | if (bm_node && bm_node->data) { |
| 4246 | pp->bm_priv = bm_node->data; |
| 4247 | err = mvneta_bm_port_init(pdev, pp); |
| 4248 | if (err < 0) { |
| 4249 | dev_info(&pdev->dev, "use SW buffer management\n"); |
| 4250 | pp->bm_priv = NULL; |
| 4251 | } |
| 4252 | } |
Peter Chen | d4e4da0 | 2016-08-01 15:02:36 +0800 | [diff] [blame] | 4253 | of_node_put(bm_node); |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 4254 | |
Ezequiel Garcia | 9672850 | 2014-05-22 20:06:59 -0300 | [diff] [blame] | 4255 | err = mvneta_init(&pdev->dev, pp); |
| 4256 | if (err < 0) |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 4257 | goto err_netdev; |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 4258 | |
| 4259 | err = mvneta_port_power_up(pp, phy_mode); |
| 4260 | if (err < 0) { |
| 4261 | dev_err(&pdev->dev, "can't power up port\n"); |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 4262 | goto err_netdev; |
Thomas Petazzoni | 3f1dd4b | 2014-04-15 15:50:20 +0200 | [diff] [blame] | 4263 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4264 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 4265 | /* Armada3700 network controller does not support per-cpu |
| 4266 | * operation, so only single NAPI should be initialized. |
| 4267 | */ |
| 4268 | if (pp->neta_armada3700) { |
| 4269 | netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT); |
| 4270 | } else { |
| 4271 | for_each_present_cpu(cpu) { |
| 4272 | struct mvneta_pcpu_port *port = |
| 4273 | per_cpu_ptr(pp->ports, cpu); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 4274 | |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 4275 | netif_napi_add(dev, &port->napi, mvneta_poll, |
| 4276 | NAPI_POLL_WEIGHT); |
| 4277 | port->pp = pp; |
| 4278 | } |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 4279 | } |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4280 | |
Ezequiel Garcia | 2adb719d | 2014-05-19 13:59:55 -0300 | [diff] [blame] | 4281 | dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO; |
Ezequiel Garcia | 01ef26c | 2014-05-19 13:59:53 -0300 | [diff] [blame] | 4282 | dev->hw_features |= dev->features; |
| 4283 | dev->vlan_features |= dev->features; |
Andrew Lunn | 97db8af | 2016-11-24 00:08:13 +0100 | [diff] [blame] | 4284 | dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; |
Ezequiel Garcia | 8eef5f9 | 2014-05-30 13:40:05 -0300 | [diff] [blame] | 4285 | dev->gso_max_segs = MVNETA_MAX_TSO_SEGS; |
willy tarreau | b50b72d | 2013-04-06 08:47:01 +0000 | [diff] [blame] | 4286 | |
Jarod Wilson | 5777987 | 2016-10-17 15:54:06 -0400 | [diff] [blame] | 4287 | /* MTU range: 68 - 9676 */ |
| 4288 | dev->min_mtu = ETH_MIN_MTU; |
| 4289 | /* 9676 == 9700 - 20 and rounding to 8 */ |
| 4290 | dev->max_mtu = 9676; |
| 4291 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4292 | err = register_netdev(dev); |
| 4293 | if (err < 0) { |
| 4294 | dev_err(&pdev->dev, "failed to register\n"); |
Ezequiel Garcia | 9672850 | 2014-05-22 20:06:59 -0300 | [diff] [blame] | 4295 | goto err_free_stats; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4296 | } |
| 4297 | |
Thomas Petazzoni | 8cc3e43 | 2013-06-04 04:52:23 +0000 | [diff] [blame] | 4298 | netdev_info(dev, "Using %s mac address %pM\n", mac_from, |
| 4299 | dev->dev_addr); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4300 | |
| 4301 | platform_set_drvdata(pdev, pp->dev); |
| 4302 | |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 4303 | if (pp->use_inband_status) { |
| 4304 | struct phy_device *phy = of_phy_find_device(dn); |
| 4305 | |
| 4306 | mvneta_fixed_link_update(pp, phy); |
Russell King | 04d53b2 | 2015-09-24 20:36:18 +0100 | [diff] [blame] | 4307 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 4308 | put_device(&phy->mdio.dev); |
Stas Sergeev | 898b2970 | 2015-04-01 20:32:49 +0300 | [diff] [blame] | 4309 | } |
| 4310 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4311 | return 0; |
| 4312 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 4313 | err_netdev: |
| 4314 | unregister_netdev(dev); |
| 4315 | if (pp->bm_priv) { |
| 4316 | mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id); |
| 4317 | mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, |
| 4318 | 1 << pp->id); |
| 4319 | } |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 4320 | err_free_stats: |
| 4321 | free_percpu(pp->stats); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 4322 | err_free_ports: |
| 4323 | free_percpu(pp->ports); |
Arnaud Patard \(Rtp\) | 5445eaf | 2013-07-29 21:56:48 +0200 | [diff] [blame] | 4324 | err_clk: |
Jisheng Zhang | 15cc4a4 | 2016-01-20 19:27:24 +0800 | [diff] [blame] | 4325 | clk_disable_unprepare(pp->clk_bus); |
Arnaud Patard \(Rtp\) | 5445eaf | 2013-07-29 21:56:48 +0200 | [diff] [blame] | 4326 | clk_disable_unprepare(pp->clk); |
Uwe Kleine-König | c891c24 | 2014-08-07 21:58:46 +0200 | [diff] [blame] | 4327 | err_put_phy_node: |
| 4328 | of_node_put(phy_node); |
Johan Hovold | 5a57a30 | 2016-11-28 19:25:04 +0100 | [diff] [blame] | 4329 | if (of_phy_is_fixed_link(dn)) |
| 4330 | of_phy_deregister_fixed_link(dn); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4331 | err_free_irq: |
| 4332 | irq_dispose_mapping(dev->irq); |
| 4333 | err_free_netdev: |
| 4334 | free_netdev(dev); |
| 4335 | return err; |
| 4336 | } |
| 4337 | |
| 4338 | /* Device removal routine */ |
Greg KH | 03ce758 | 2012-12-21 13:42:15 +0000 | [diff] [blame] | 4339 | static int mvneta_remove(struct platform_device *pdev) |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4340 | { |
| 4341 | struct net_device *dev = platform_get_drvdata(pdev); |
Johan Hovold | 5a57a30 | 2016-11-28 19:25:04 +0100 | [diff] [blame] | 4342 | struct device_node *dn = pdev->dev.of_node; |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4343 | struct mvneta_port *pp = netdev_priv(dev); |
| 4344 | |
| 4345 | unregister_netdev(dev); |
Jisheng Zhang | 15cc4a4 | 2016-01-20 19:27:24 +0800 | [diff] [blame] | 4346 | clk_disable_unprepare(pp->clk_bus); |
Thomas Petazzoni | 189dd62 | 2012-11-19 14:15:25 +0100 | [diff] [blame] | 4347 | clk_disable_unprepare(pp->clk); |
Maxime Ripard | 12bb03b | 2015-09-25 18:09:36 +0200 | [diff] [blame] | 4348 | free_percpu(pp->ports); |
willy tarreau | 74c41b0 | 2014-01-16 08:20:08 +0100 | [diff] [blame] | 4349 | free_percpu(pp->stats); |
Johan Hovold | 5a57a30 | 2016-11-28 19:25:04 +0100 | [diff] [blame] | 4350 | if (of_phy_is_fixed_link(dn)) |
| 4351 | of_phy_deregister_fixed_link(dn); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4352 | irq_dispose_mapping(dev->irq); |
Uwe Kleine-König | c891c24 | 2014-08-07 21:58:46 +0200 | [diff] [blame] | 4353 | of_node_put(pp->phy_node); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4354 | free_netdev(dev); |
| 4355 | |
Marcin Wojtas | dc35a10 | 2016-03-14 09:39:03 +0100 | [diff] [blame] | 4356 | if (pp->bm_priv) { |
| 4357 | mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id); |
| 4358 | mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, |
| 4359 | 1 << pp->id); |
| 4360 | } |
| 4361 | |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4362 | return 0; |
| 4363 | } |
| 4364 | |
| 4365 | static const struct of_device_id mvneta_match[] = { |
| 4366 | { .compatible = "marvell,armada-370-neta" }, |
Simon Guinot | f522a97 | 2015-06-30 16:20:20 +0200 | [diff] [blame] | 4367 | { .compatible = "marvell,armada-xp-neta" }, |
Marcin Wojtas | 2636ac3 | 2016-12-01 18:03:09 +0100 | [diff] [blame] | 4368 | { .compatible = "marvell,armada-3700-neta" }, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4369 | { } |
| 4370 | }; |
| 4371 | MODULE_DEVICE_TABLE(of, mvneta_match); |
| 4372 | |
| 4373 | static struct platform_driver mvneta_driver = { |
| 4374 | .probe = mvneta_probe, |
Greg KH | 03ce758 | 2012-12-21 13:42:15 +0000 | [diff] [blame] | 4375 | .remove = mvneta_remove, |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4376 | .driver = { |
| 4377 | .name = MVNETA_DRIVER_NAME, |
| 4378 | .of_match_table = mvneta_match, |
| 4379 | }, |
| 4380 | }; |
| 4381 | |
Sebastian Andrzej Siewior | 84a3f4d | 2016-08-18 14:57:23 +0200 | [diff] [blame] | 4382 | static int __init mvneta_driver_init(void) |
| 4383 | { |
| 4384 | int ret; |
| 4385 | |
| 4386 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online", |
| 4387 | mvneta_cpu_online, |
| 4388 | mvneta_cpu_down_prepare); |
| 4389 | if (ret < 0) |
| 4390 | goto out; |
| 4391 | online_hpstate = ret; |
| 4392 | ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead", |
| 4393 | NULL, mvneta_cpu_dead); |
| 4394 | if (ret) |
| 4395 | goto err_dead; |
| 4396 | |
| 4397 | ret = platform_driver_register(&mvneta_driver); |
| 4398 | if (ret) |
| 4399 | goto err; |
| 4400 | return 0; |
| 4401 | |
| 4402 | err: |
| 4403 | cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD); |
| 4404 | err_dead: |
| 4405 | cpuhp_remove_multi_state(online_hpstate); |
| 4406 | out: |
| 4407 | return ret; |
| 4408 | } |
| 4409 | module_init(mvneta_driver_init); |
| 4410 | |
| 4411 | static void __exit mvneta_driver_exit(void) |
| 4412 | { |
| 4413 | platform_driver_unregister(&mvneta_driver); |
| 4414 | cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD); |
| 4415 | cpuhp_remove_multi_state(online_hpstate); |
| 4416 | } |
| 4417 | module_exit(mvneta_driver_exit); |
Thomas Petazzoni | c5aff18 | 2012-08-17 14:04:28 +0300 | [diff] [blame] | 4418 | |
| 4419 | MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com"); |
| 4420 | MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>"); |
| 4421 | MODULE_LICENSE("GPL"); |
| 4422 | |
| 4423 | module_param(rxq_number, int, S_IRUGO); |
| 4424 | module_param(txq_number, int, S_IRUGO); |
| 4425 | |
| 4426 | module_param(rxq_def, int, S_IRUGO); |
willy tarreau | f19fadf | 2014-01-16 08:20:17 +0100 | [diff] [blame] | 4427 | module_param(rx_copybreak, int, S_IRUGO | S_IWUSR); |