1 // SPDX-License-Identifier: GPL-2.0+ 2 3 #include <linux/types.h> 4 #include <linux/clk.h> 5 #include <linux/platform_device.h> 6 #include <linux/pm_runtime.h> 7 #include <linux/acpi.h> 8 #include <linux/of_mdio.h> 9 #include <linux/etherdevice.h> 10 #include <linux/interrupt.h> 11 #include <linux/io.h> 12 #include <linux/netlink.h> 13 #include <linux/bpf.h> 14 #include <linux/bpf_trace.h> 15 16 #include <net/tcp.h> 17 #include <net/page_pool.h> 18 #include <net/ip6_checksum.h> 19 20 #define NETSEC_REG_SOFT_RST 0x104 21 #define NETSEC_REG_COM_INIT 0x120 22 23 #define NETSEC_REG_TOP_STATUS 0x200 24 #define NETSEC_IRQ_RX BIT(1) 25 #define NETSEC_IRQ_TX BIT(0) 26 27 #define NETSEC_REG_TOP_INTEN 0x204 28 #define NETSEC_REG_INTEN_SET 0x234 29 #define NETSEC_REG_INTEN_CLR 0x238 30 31 #define NETSEC_REG_NRM_TX_STATUS 0x400 32 #define NETSEC_REG_NRM_TX_INTEN 0x404 33 #define NETSEC_REG_NRM_TX_INTEN_SET 0x428 34 #define NETSEC_REG_NRM_TX_INTEN_CLR 0x42c 35 #define NRM_TX_ST_NTOWNR BIT(17) 36 #define NRM_TX_ST_TR_ERR BIT(16) 37 #define NRM_TX_ST_TXDONE BIT(15) 38 #define NRM_TX_ST_TMREXP BIT(14) 39 40 #define NETSEC_REG_NRM_RX_STATUS 0x440 41 #define NETSEC_REG_NRM_RX_INTEN 0x444 42 #define NETSEC_REG_NRM_RX_INTEN_SET 0x468 43 #define NETSEC_REG_NRM_RX_INTEN_CLR 0x46c 44 #define NRM_RX_ST_RC_ERR BIT(16) 45 #define NRM_RX_ST_PKTCNT BIT(15) 46 #define NRM_RX_ST_TMREXP BIT(14) 47 48 #define NETSEC_REG_PKT_CMD_BUF 0xd0 49 50 #define NETSEC_REG_CLK_EN 0x100 51 52 #define NETSEC_REG_PKT_CTRL 0x140 53 54 #define NETSEC_REG_DMA_TMR_CTRL 0x20c 55 #define NETSEC_REG_F_TAIKI_MC_VER 0x22c 56 #define NETSEC_REG_F_TAIKI_VER 0x230 57 #define NETSEC_REG_DMA_HM_CTRL 0x214 58 #define NETSEC_REG_DMA_MH_CTRL 0x220 59 #define NETSEC_REG_ADDR_DIS_CORE 0x218 60 #define NETSEC_REG_DMAC_HM_CMD_BUF 0x210 61 #define NETSEC_REG_DMAC_MH_CMD_BUF 0x21c 62 63 #define NETSEC_REG_NRM_TX_PKTCNT 0x410 64 65 #define NETSEC_REG_NRM_TX_DONE_PKTCNT 0x414 66 #define NETSEC_REG_NRM_TX_DONE_TXINT_PKTCNT 0x418 67 68 #define NETSEC_REG_NRM_TX_TMR 0x41c 69 70 #define NETSEC_REG_NRM_RX_PKTCNT 0x454 71 #define NETSEC_REG_NRM_RX_RXINT_PKTCNT 0x458 72 #define NETSEC_REG_NRM_TX_TXINT_TMR 0x420 73 #define NETSEC_REG_NRM_RX_RXINT_TMR 0x460 74 75 #define NETSEC_REG_NRM_RX_TMR 0x45c 76 77 #define NETSEC_REG_NRM_TX_DESC_START_UP 0x434 78 #define NETSEC_REG_NRM_TX_DESC_START_LW 0x408 79 #define NETSEC_REG_NRM_RX_DESC_START_UP 0x474 80 #define NETSEC_REG_NRM_RX_DESC_START_LW 0x448 81 82 #define NETSEC_REG_NRM_TX_CONFIG 0x430 83 #define NETSEC_REG_NRM_RX_CONFIG 0x470 84 85 #define MAC_REG_STATUS 0x1024 86 #define MAC_REG_DATA 0x11c0 87 #define MAC_REG_CMD 0x11c4 88 #define MAC_REG_FLOW_TH 0x11cc 89 #define MAC_REG_INTF_SEL 0x11d4 90 #define MAC_REG_DESC_INIT 0x11fc 91 #define MAC_REG_DESC_SOFT_RST 0x1204 92 #define NETSEC_REG_MODE_TRANS_COMP_STATUS 0x500 93 94 #define GMAC_REG_MCR 0x0000 95 #define GMAC_REG_MFFR 0x0004 96 #define GMAC_REG_GAR 0x0010 97 #define GMAC_REG_GDR 0x0014 98 #define GMAC_REG_FCR 0x0018 99 #define GMAC_REG_BMR 0x1000 100 #define GMAC_REG_RDLAR 0x100c 101 #define GMAC_REG_TDLAR 0x1010 102 #define GMAC_REG_OMR 0x1018 103 104 #define MHZ(n) ((n) * 1000 * 1000) 105 106 #define NETSEC_TX_SHIFT_OWN_FIELD 31 107 #define NETSEC_TX_SHIFT_LD_FIELD 30 108 #define NETSEC_TX_SHIFT_DRID_FIELD 24 109 #define NETSEC_TX_SHIFT_PT_FIELD 21 110 #define NETSEC_TX_SHIFT_TDRID_FIELD 16 111 #define NETSEC_TX_SHIFT_CC_FIELD 15 112 #define NETSEC_TX_SHIFT_FS_FIELD 9 113 #define NETSEC_TX_LAST 8 114 #define NETSEC_TX_SHIFT_CO 7 115 #define NETSEC_TX_SHIFT_SO 6 116 #define NETSEC_TX_SHIFT_TRS_FIELD 4 117 118 #define NETSEC_RX_PKT_OWN_FIELD 31 119 #define NETSEC_RX_PKT_LD_FIELD 30 120 #define NETSEC_RX_PKT_SDRID_FIELD 24 121 #define NETSEC_RX_PKT_FR_FIELD 23 122 #define NETSEC_RX_PKT_ER_FIELD 21 123 #define NETSEC_RX_PKT_ERR_FIELD 16 124 #define NETSEC_RX_PKT_TDRID_FIELD 12 125 #define NETSEC_RX_PKT_FS_FIELD 9 126 #define NETSEC_RX_PKT_LS_FIELD 8 127 #define NETSEC_RX_PKT_CO_FIELD 6 128 129 #define NETSEC_RX_PKT_ERR_MASK 3 130 131 #define NETSEC_MAX_TX_PKT_LEN 1518 132 #define NETSEC_MAX_TX_JUMBO_PKT_LEN 9018 133 134 #define NETSEC_RING_GMAC 15 135 #define NETSEC_RING_MAX 2 136 137 #define NETSEC_TCP_SEG_LEN_MAX 1460 138 #define NETSEC_TCP_JUMBO_SEG_LEN_MAX 8960 139 140 #define NETSEC_RX_CKSUM_NOTAVAIL 0 141 #define NETSEC_RX_CKSUM_OK 1 142 #define NETSEC_RX_CKSUM_NG 2 143 144 #define NETSEC_TOP_IRQ_REG_CODE_LOAD_END BIT(20) 145 #define NETSEC_IRQ_TRANSITION_COMPLETE BIT(4) 146 147 #define NETSEC_MODE_TRANS_COMP_IRQ_N2T BIT(20) 148 #define NETSEC_MODE_TRANS_COMP_IRQ_T2N BIT(19) 149 150 #define NETSEC_INT_PKTCNT_MAX 2047 151 152 #define NETSEC_FLOW_START_TH_MAX 95 153 #define NETSEC_FLOW_STOP_TH_MAX 95 154 #define NETSEC_FLOW_PAUSE_TIME_MIN 5 155 156 #define NETSEC_CLK_EN_REG_DOM_ALL 0x3f 157 158 #define NETSEC_PKT_CTRL_REG_MODE_NRM BIT(28) 159 #define NETSEC_PKT_CTRL_REG_EN_JUMBO BIT(27) 160 #define NETSEC_PKT_CTRL_REG_LOG_CHKSUM_ER BIT(3) 161 #define NETSEC_PKT_CTRL_REG_LOG_HD_INCOMPLETE BIT(2) 162 #define NETSEC_PKT_CTRL_REG_LOG_HD_ER BIT(1) 163 #define NETSEC_PKT_CTRL_REG_DRP_NO_MATCH BIT(0) 164 165 #define NETSEC_CLK_EN_REG_DOM_G BIT(5) 166 #define NETSEC_CLK_EN_REG_DOM_C BIT(1) 167 #define NETSEC_CLK_EN_REG_DOM_D BIT(0) 168 169 #define NETSEC_COM_INIT_REG_DB BIT(2) 170 #define NETSEC_COM_INIT_REG_CLS BIT(1) 171 #define NETSEC_COM_INIT_REG_ALL (NETSEC_COM_INIT_REG_CLS | \ 172 NETSEC_COM_INIT_REG_DB) 173 174 #define NETSEC_SOFT_RST_REG_RESET 0 175 #define NETSEC_SOFT_RST_REG_RUN BIT(31) 176 177 #define NETSEC_DMA_CTRL_REG_STOP 1 178 #define MH_CTRL__MODE_TRANS BIT(20) 179 180 #define NETSEC_GMAC_CMD_ST_READ 0 181 #define NETSEC_GMAC_CMD_ST_WRITE BIT(28) 182 #define NETSEC_GMAC_CMD_ST_BUSY BIT(31) 183 184 #define NETSEC_GMAC_BMR_REG_COMMON 0x00412080 185 #define NETSEC_GMAC_BMR_REG_RESET 0x00020181 186 #define NETSEC_GMAC_BMR_REG_SWR 0x00000001 187 188 #define NETSEC_GMAC_OMR_REG_ST BIT(13) 189 #define NETSEC_GMAC_OMR_REG_SR BIT(1) 190 191 #define NETSEC_GMAC_MCR_REG_IBN BIT(30) 192 #define NETSEC_GMAC_MCR_REG_CST BIT(25) 193 #define NETSEC_GMAC_MCR_REG_JE BIT(20) 194 #define NETSEC_MCR_PS BIT(15) 195 #define NETSEC_GMAC_MCR_REG_FES BIT(14) 196 #define NETSEC_GMAC_MCR_REG_FULL_DUPLEX_COMMON 0x0000280c 197 #define NETSEC_GMAC_MCR_REG_HALF_DUPLEX_COMMON 0x0001a00c 198 199 #define NETSEC_FCR_RFE BIT(2) 200 #define NETSEC_FCR_TFE BIT(1) 201 202 #define NETSEC_GMAC_GAR_REG_GW BIT(1) 203 #define NETSEC_GMAC_GAR_REG_GB BIT(0) 204 205 #define NETSEC_GMAC_GAR_REG_SHIFT_PA 11 206 #define NETSEC_GMAC_GAR_REG_SHIFT_GR 6 207 #define GMAC_REG_SHIFT_CR_GAR 2 208 209 #define NETSEC_GMAC_GAR_REG_CR_25_35_MHZ 2 210 #define NETSEC_GMAC_GAR_REG_CR_35_60_MHZ 3 211 #define NETSEC_GMAC_GAR_REG_CR_60_100_MHZ 0 212 #define NETSEC_GMAC_GAR_REG_CR_100_150_MHZ 1 213 #define NETSEC_GMAC_GAR_REG_CR_150_250_MHZ 4 214 #define NETSEC_GMAC_GAR_REG_CR_250_300_MHZ 5 215 216 #define NETSEC_GMAC_RDLAR_REG_COMMON 0x18000 217 #define NETSEC_GMAC_TDLAR_REG_COMMON 0x1c000 218 219 #define NETSEC_REG_NETSEC_VER_F_TAIKI 0x50000 220 221 #define NETSEC_REG_DESC_RING_CONFIG_CFG_UP BIT(31) 222 #define NETSEC_REG_DESC_RING_CONFIG_CH_RST BIT(30) 223 #define NETSEC_REG_DESC_TMR_MODE 4 224 #define NETSEC_REG_DESC_ENDIAN 0 225 226 #define NETSEC_MAC_DESC_SOFT_RST_SOFT_RST 1 227 #define NETSEC_MAC_DESC_INIT_REG_INIT 1 228 229 #define NETSEC_EEPROM_MAC_ADDRESS 0x00 230 #define NETSEC_EEPROM_HM_ME_ADDRESS_H 0x08 231 #define NETSEC_EEPROM_HM_ME_ADDRESS_L 0x0C 232 #define NETSEC_EEPROM_HM_ME_SIZE 0x10 233 #define NETSEC_EEPROM_MH_ME_ADDRESS_H 0x14 234 #define NETSEC_EEPROM_MH_ME_ADDRESS_L 0x18 235 #define NETSEC_EEPROM_MH_ME_SIZE 0x1C 236 #define NETSEC_EEPROM_PKT_ME_ADDRESS 0x20 237 #define NETSEC_EEPROM_PKT_ME_SIZE 0x24 238 239 #define DESC_NUM 256 240 241 #define NETSEC_SKB_PAD (NET_SKB_PAD + NET_IP_ALIGN) 242 #define NETSEC_RXBUF_HEADROOM (max(XDP_PACKET_HEADROOM, NET_SKB_PAD) + \ 243 NET_IP_ALIGN) 244 #define NETSEC_RX_BUF_NON_DATA (NETSEC_RXBUF_HEADROOM + \ 245 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) 246 #define NETSEC_RX_BUF_SIZE (PAGE_SIZE - NETSEC_RX_BUF_NON_DATA) 247 248 #define DESC_SZ sizeof(struct netsec_de) 249 250 #define NETSEC_F_NETSEC_VER_MAJOR_NUM(x) ((x) & 0xffff0000) 251 252 #define NETSEC_XDP_PASS 0 253 #define NETSEC_XDP_CONSUMED BIT(0) 254 #define NETSEC_XDP_TX BIT(1) 255 #define NETSEC_XDP_REDIR BIT(2) 256 257 enum ring_id { 258 NETSEC_RING_TX = 0, 259 NETSEC_RING_RX 260 }; 261 262 enum buf_type { 263 TYPE_NETSEC_SKB = 0, 264 TYPE_NETSEC_XDP_TX, 265 TYPE_NETSEC_XDP_NDO, 266 }; 267 268 struct netsec_desc { 269 union { 270 struct sk_buff *skb; 271 struct xdp_frame *xdpf; 272 }; 273 dma_addr_t dma_addr; 274 void *addr; 275 u16 len; 276 u8 buf_type; 277 }; 278 279 struct netsec_desc_ring { 280 dma_addr_t desc_dma; 281 struct netsec_desc *desc; 282 void *vaddr; 283 u16 head, tail; 284 u16 xdp_xmit; /* netsec_xdp_xmit packets */ 285 struct page_pool *page_pool; 286 struct xdp_rxq_info xdp_rxq; 287 spinlock_t lock; /* XDP tx queue locking */ 288 }; 289 290 struct netsec_priv { 291 struct netsec_desc_ring desc_ring[NETSEC_RING_MAX]; 292 struct ethtool_coalesce et_coalesce; 293 struct bpf_prog *xdp_prog; 294 spinlock_t reglock; /* protect reg access */ 295 struct napi_struct napi; 296 phy_interface_t phy_interface; 297 struct net_device *ndev; 298 struct device_node *phy_np; 299 struct phy_device *phydev; 300 struct mii_bus *mii_bus; 301 void __iomem *ioaddr; 302 void __iomem *eeprom_base; 303 struct device *dev; 304 struct clk *clk; 305 u32 msg_enable; 306 u32 freq; 307 u32 phy_addr; 308 bool rx_cksum_offload_flag; 309 }; 310 311 struct netsec_de { /* Netsec Descriptor layout */ 312 u32 attr; 313 u32 data_buf_addr_up; 314 u32 data_buf_addr_lw; 315 u32 buf_len_info; 316 }; 317 318 struct netsec_tx_pkt_ctrl { 319 u16 tcp_seg_len; 320 bool tcp_seg_offload_flag; 321 bool cksum_offload_flag; 322 }; 323 324 struct netsec_rx_pkt_info { 325 int rx_cksum_result; 326 int err_code; 327 bool err_flag; 328 }; 329 330 static void netsec_write(struct netsec_priv *priv, u32 reg_addr, u32 val) 331 { 332 writel(val, priv->ioaddr + reg_addr); 333 } 334 335 static u32 netsec_read(struct netsec_priv *priv, u32 reg_addr) 336 { 337 return readl(priv->ioaddr + reg_addr); 338 } 339 340 /************* MDIO BUS OPS FOLLOW *************/ 341 342 #define TIMEOUT_SPINS_MAC 1000 343 #define TIMEOUT_SECONDARY_MS_MAC 100 344 345 static u32 netsec_clk_type(u32 freq) 346 { 347 if (freq < MHZ(35)) 348 return NETSEC_GMAC_GAR_REG_CR_25_35_MHZ; 349 if (freq < MHZ(60)) 350 return NETSEC_GMAC_GAR_REG_CR_35_60_MHZ; 351 if (freq < MHZ(100)) 352 return NETSEC_GMAC_GAR_REG_CR_60_100_MHZ; 353 if (freq < MHZ(150)) 354 return NETSEC_GMAC_GAR_REG_CR_100_150_MHZ; 355 if (freq < MHZ(250)) 356 return NETSEC_GMAC_GAR_REG_CR_150_250_MHZ; 357 358 return NETSEC_GMAC_GAR_REG_CR_250_300_MHZ; 359 } 360 361 static int netsec_wait_while_busy(struct netsec_priv *priv, u32 addr, u32 mask) 362 { 363 u32 timeout = TIMEOUT_SPINS_MAC; 364 365 while (--timeout && netsec_read(priv, addr) & mask) 366 cpu_relax(); 367 if (timeout) 368 return 0; 369 370 timeout = TIMEOUT_SECONDARY_MS_MAC; 371 while (--timeout && netsec_read(priv, addr) & mask) 372 usleep_range(1000, 2000); 373 374 if (timeout) 375 return 0; 376 377 netdev_WARN(priv->ndev, "%s: timeout\n", __func__); 378 379 return -ETIMEDOUT; 380 } 381 382 static int netsec_mac_write(struct netsec_priv *priv, u32 addr, u32 value) 383 { 384 netsec_write(priv, MAC_REG_DATA, value); 385 netsec_write(priv, MAC_REG_CMD, addr | NETSEC_GMAC_CMD_ST_WRITE); 386 return netsec_wait_while_busy(priv, 387 MAC_REG_CMD, NETSEC_GMAC_CMD_ST_BUSY); 388 } 389 390 static int netsec_mac_read(struct netsec_priv *priv, u32 addr, u32 *read) 391 { 392 int ret; 393 394 netsec_write(priv, MAC_REG_CMD, addr | NETSEC_GMAC_CMD_ST_READ); 395 ret = netsec_wait_while_busy(priv, 396 MAC_REG_CMD, NETSEC_GMAC_CMD_ST_BUSY); 397 if (ret) 398 return ret; 399 400 *read = netsec_read(priv, MAC_REG_DATA); 401 402 return 0; 403 } 404 405 static int netsec_mac_wait_while_busy(struct netsec_priv *priv, 406 u32 addr, u32 mask) 407 { 408 u32 timeout = TIMEOUT_SPINS_MAC; 409 int ret, data; 410 411 do { 412 ret = netsec_mac_read(priv, addr, &data); 413 if (ret) 414 break; 415 cpu_relax(); 416 } while (--timeout && (data & mask)); 417 418 if (timeout) 419 return 0; 420 421 timeout = TIMEOUT_SECONDARY_MS_MAC; 422 do { 423 usleep_range(1000, 2000); 424 425 ret = netsec_mac_read(priv, addr, &data); 426 if (ret) 427 break; 428 cpu_relax(); 429 } while (--timeout && (data & mask)); 430 431 if (timeout && !ret) 432 return 0; 433 434 netdev_WARN(priv->ndev, "%s: timeout\n", __func__); 435 436 return -ETIMEDOUT; 437 } 438 439 static int netsec_mac_update_to_phy_state(struct netsec_priv *priv) 440 { 441 struct phy_device *phydev = priv->ndev->phydev; 442 u32 value = 0; 443 444 value = phydev->duplex ? NETSEC_GMAC_MCR_REG_FULL_DUPLEX_COMMON : 445 NETSEC_GMAC_MCR_REG_HALF_DUPLEX_COMMON; 446 447 if (phydev->speed != SPEED_1000) 448 value |= NETSEC_MCR_PS; 449 450 if (priv->phy_interface != PHY_INTERFACE_MODE_GMII && 451 phydev->speed == SPEED_100) 452 value |= NETSEC_GMAC_MCR_REG_FES; 453 454 value |= NETSEC_GMAC_MCR_REG_CST | NETSEC_GMAC_MCR_REG_JE; 455 456 if (phy_interface_mode_is_rgmii(priv->phy_interface)) 457 value |= NETSEC_GMAC_MCR_REG_IBN; 458 459 if (netsec_mac_write(priv, GMAC_REG_MCR, value)) 460 return -ETIMEDOUT; 461 462 return 0; 463 } 464 465 static int netsec_phy_read(struct mii_bus *bus, int phy_addr, int reg_addr); 466 467 static int netsec_phy_write(struct mii_bus *bus, 468 int phy_addr, int reg, u16 val) 469 { 470 int status; 471 struct netsec_priv *priv = bus->priv; 472 473 if (netsec_mac_write(priv, GMAC_REG_GDR, val)) 474 return -ETIMEDOUT; 475 if (netsec_mac_write(priv, GMAC_REG_GAR, 476 phy_addr << NETSEC_GMAC_GAR_REG_SHIFT_PA | 477 reg << NETSEC_GMAC_GAR_REG_SHIFT_GR | 478 NETSEC_GMAC_GAR_REG_GW | NETSEC_GMAC_GAR_REG_GB | 479 (netsec_clk_type(priv->freq) << 480 GMAC_REG_SHIFT_CR_GAR))) 481 return -ETIMEDOUT; 482 483 status = netsec_mac_wait_while_busy(priv, GMAC_REG_GAR, 484 NETSEC_GMAC_GAR_REG_GB); 485 486 /* Developerbox implements RTL8211E PHY and there is 487 * a compatibility problem with F_GMAC4. 488 * RTL8211E expects MDC clock must be kept toggling for several 489 * clock cycle with MDIO high before entering the IDLE state. 490 * To meet this requirement, netsec driver needs to issue dummy 491 * read(e.g. read PHYID1(offset 0x2) register) right after write. 492 */ 493 netsec_phy_read(bus, phy_addr, MII_PHYSID1); 494 495 return status; 496 } 497 498 static int netsec_phy_read(struct mii_bus *bus, int phy_addr, int reg_addr) 499 { 500 struct netsec_priv *priv = bus->priv; 501 u32 data; 502 int ret; 503 504 if (netsec_mac_write(priv, GMAC_REG_GAR, NETSEC_GMAC_GAR_REG_GB | 505 phy_addr << NETSEC_GMAC_GAR_REG_SHIFT_PA | 506 reg_addr << NETSEC_GMAC_GAR_REG_SHIFT_GR | 507 (netsec_clk_type(priv->freq) << 508 GMAC_REG_SHIFT_CR_GAR))) 509 return -ETIMEDOUT; 510 511 ret = netsec_mac_wait_while_busy(priv, GMAC_REG_GAR, 512 NETSEC_GMAC_GAR_REG_GB); 513 if (ret) 514 return ret; 515 516 ret = netsec_mac_read(priv, GMAC_REG_GDR, &data); 517 if (ret) 518 return ret; 519 520 return data; 521 } 522 523 /************* ETHTOOL_OPS FOLLOW *************/ 524 525 static void netsec_et_get_drvinfo(struct net_device *net_device, 526 struct ethtool_drvinfo *info) 527 { 528 strlcpy(info->driver, "netsec", sizeof(info->driver)); 529 strlcpy(info->bus_info, dev_name(net_device->dev.parent), 530 sizeof(info->bus_info)); 531 } 532 533 static int netsec_et_get_coalesce(struct net_device *net_device, 534 struct ethtool_coalesce *et_coalesce) 535 { 536 struct netsec_priv *priv = netdev_priv(net_device); 537 538 *et_coalesce = priv->et_coalesce; 539 540 return 0; 541 } 542 543 static int netsec_et_set_coalesce(struct net_device *net_device, 544 struct ethtool_coalesce *et_coalesce) 545 { 546 struct netsec_priv *priv = netdev_priv(net_device); 547 548 priv->et_coalesce = *et_coalesce; 549 550 if (priv->et_coalesce.tx_coalesce_usecs < 50) 551 priv->et_coalesce.tx_coalesce_usecs = 50; 552 if (priv->et_coalesce.tx_max_coalesced_frames < 1) 553 priv->et_coalesce.tx_max_coalesced_frames = 1; 554 555 netsec_write(priv, NETSEC_REG_NRM_TX_DONE_TXINT_PKTCNT, 556 priv->et_coalesce.tx_max_coalesced_frames); 557 netsec_write(priv, NETSEC_REG_NRM_TX_TXINT_TMR, 558 priv->et_coalesce.tx_coalesce_usecs); 559 netsec_write(priv, NETSEC_REG_NRM_TX_INTEN_SET, NRM_TX_ST_TXDONE); 560 netsec_write(priv, NETSEC_REG_NRM_TX_INTEN_SET, NRM_TX_ST_TMREXP); 561 562 if (priv->et_coalesce.rx_coalesce_usecs < 50) 563 priv->et_coalesce.rx_coalesce_usecs = 50; 564 if (priv->et_coalesce.rx_max_coalesced_frames < 1) 565 priv->et_coalesce.rx_max_coalesced_frames = 1; 566 567 netsec_write(priv, NETSEC_REG_NRM_RX_RXINT_PKTCNT, 568 priv->et_coalesce.rx_max_coalesced_frames); 569 netsec_write(priv, NETSEC_REG_NRM_RX_RXINT_TMR, 570 priv->et_coalesce.rx_coalesce_usecs); 571 netsec_write(priv, NETSEC_REG_NRM_RX_INTEN_SET, NRM_RX_ST_PKTCNT); 572 netsec_write(priv, NETSEC_REG_NRM_RX_INTEN_SET, NRM_RX_ST_TMREXP); 573 574 return 0; 575 } 576 577 static u32 netsec_et_get_msglevel(struct net_device *dev) 578 { 579 struct netsec_priv *priv = netdev_priv(dev); 580 581 return priv->msg_enable; 582 } 583 584 static void netsec_et_set_msglevel(struct net_device *dev, u32 datum) 585 { 586 struct netsec_priv *priv = netdev_priv(dev); 587 588 priv->msg_enable = datum; 589 } 590 591 static const struct ethtool_ops netsec_ethtool_ops = { 592 .get_drvinfo = netsec_et_get_drvinfo, 593 .get_link_ksettings = phy_ethtool_get_link_ksettings, 594 .set_link_ksettings = phy_ethtool_set_link_ksettings, 595 .get_link = ethtool_op_get_link, 596 .get_coalesce = netsec_et_get_coalesce, 597 .set_coalesce = netsec_et_set_coalesce, 598 .get_msglevel = netsec_et_get_msglevel, 599 .set_msglevel = netsec_et_set_msglevel, 600 }; 601 602 /************* NETDEV_OPS FOLLOW *************/ 603 604 605 static void netsec_set_rx_de(struct netsec_priv *priv, 606 struct netsec_desc_ring *dring, u16 idx, 607 const struct netsec_desc *desc) 608 { 609 struct netsec_de *de = dring->vaddr + DESC_SZ * idx; 610 u32 attr = (1 << NETSEC_RX_PKT_OWN_FIELD) | 611 (1 << NETSEC_RX_PKT_FS_FIELD) | 612 (1 << NETSEC_RX_PKT_LS_FIELD); 613 614 if (idx == DESC_NUM - 1) 615 attr |= (1 << NETSEC_RX_PKT_LD_FIELD); 616 617 de->data_buf_addr_up = upper_32_bits(desc->dma_addr); 618 de->data_buf_addr_lw = lower_32_bits(desc->dma_addr); 619 de->buf_len_info = desc->len; 620 de->attr = attr; 621 dma_wmb(); 622 623 dring->desc[idx].dma_addr = desc->dma_addr; 624 dring->desc[idx].addr = desc->addr; 625 dring->desc[idx].len = desc->len; 626 } 627 628 static bool netsec_clean_tx_dring(struct netsec_priv *priv) 629 { 630 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_TX]; 631 struct netsec_de *entry; 632 int tail = dring->tail; 633 unsigned int bytes; 634 int cnt = 0; 635 636 spin_lock(&dring->lock); 637 638 bytes = 0; 639 entry = dring->vaddr + DESC_SZ * tail; 640 641 while (!(entry->attr & (1U << NETSEC_TX_SHIFT_OWN_FIELD)) && 642 cnt < DESC_NUM) { 643 struct netsec_desc *desc; 644 int eop; 645 646 desc = &dring->desc[tail]; 647 eop = (entry->attr >> NETSEC_TX_LAST) & 1; 648 dma_rmb(); 649 650 /* if buf_type is either TYPE_NETSEC_SKB or 651 * TYPE_NETSEC_XDP_NDO we mapped it 652 */ 653 if (desc->buf_type != TYPE_NETSEC_XDP_TX) 654 dma_unmap_single(priv->dev, desc->dma_addr, desc->len, 655 DMA_TO_DEVICE); 656 657 if (!eop) 658 goto next; 659 660 if (desc->buf_type == TYPE_NETSEC_SKB) { 661 bytes += desc->skb->len; 662 dev_kfree_skb(desc->skb); 663 } else { 664 bytes += desc->xdpf->len; 665 xdp_return_frame(desc->xdpf); 666 } 667 next: 668 /* clean up so netsec_uninit_pkt_dring() won't free the skb 669 * again 670 */ 671 *desc = (struct netsec_desc){}; 672 673 /* entry->attr is not going to be accessed by the NIC until 674 * netsec_set_tx_de() is called. No need for a dma_wmb() here 675 */ 676 entry->attr = 1U << NETSEC_TX_SHIFT_OWN_FIELD; 677 /* move tail ahead */ 678 dring->tail = (tail + 1) % DESC_NUM; 679 680 tail = dring->tail; 681 entry = dring->vaddr + DESC_SZ * tail; 682 cnt++; 683 } 684 685 spin_unlock(&dring->lock); 686 687 if (!cnt) 688 return false; 689 690 /* reading the register clears the irq */ 691 netsec_read(priv, NETSEC_REG_NRM_TX_DONE_PKTCNT); 692 693 priv->ndev->stats.tx_packets += cnt; 694 priv->ndev->stats.tx_bytes += bytes; 695 696 netdev_completed_queue(priv->ndev, cnt, bytes); 697 698 return true; 699 } 700 701 static void netsec_process_tx(struct netsec_priv *priv) 702 { 703 struct net_device *ndev = priv->ndev; 704 bool cleaned; 705 706 cleaned = netsec_clean_tx_dring(priv); 707 708 if (cleaned && netif_queue_stopped(ndev)) { 709 /* Make sure we update the value, anyone stopping the queue 710 * after this will read the proper consumer idx 711 */ 712 smp_wmb(); 713 netif_wake_queue(ndev); 714 } 715 } 716 717 static void *netsec_alloc_rx_data(struct netsec_priv *priv, 718 dma_addr_t *dma_handle, u16 *desc_len) 719 720 { 721 722 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX]; 723 struct page *page; 724 725 page = page_pool_dev_alloc_pages(dring->page_pool); 726 if (!page) 727 return NULL; 728 729 /* We allocate the same buffer length for XDP and non-XDP cases. 730 * page_pool API will map the whole page, skip what's needed for 731 * network payloads and/or XDP 732 */ 733 *dma_handle = page_pool_get_dma_addr(page) + NETSEC_RXBUF_HEADROOM; 734 /* Make sure the incoming payload fits in the page for XDP and non-XDP 735 * cases and reserve enough space for headroom + skb_shared_info 736 */ 737 *desc_len = NETSEC_RX_BUF_SIZE; 738 739 return page_address(page); 740 } 741 742 static void netsec_rx_fill(struct netsec_priv *priv, u16 from, u16 num) 743 { 744 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX]; 745 u16 idx = from; 746 747 while (num) { 748 netsec_set_rx_de(priv, dring, idx, &dring->desc[idx]); 749 idx++; 750 if (idx >= DESC_NUM) 751 idx = 0; 752 num--; 753 } 754 } 755 756 static void netsec_xdp_ring_tx_db(struct netsec_priv *priv, u16 pkts) 757 { 758 if (likely(pkts)) 759 netsec_write(priv, NETSEC_REG_NRM_TX_PKTCNT, pkts); 760 } 761 762 static void netsec_finalize_xdp_rx(struct netsec_priv *priv, u32 xdp_res, 763 u16 pkts) 764 { 765 if (xdp_res & NETSEC_XDP_REDIR) 766 xdp_do_flush_map(); 767 768 if (xdp_res & NETSEC_XDP_TX) 769 netsec_xdp_ring_tx_db(priv, pkts); 770 } 771 772 static void netsec_set_tx_de(struct netsec_priv *priv, 773 struct netsec_desc_ring *dring, 774 const struct netsec_tx_pkt_ctrl *tx_ctrl, 775 const struct netsec_desc *desc, void *buf) 776 { 777 int idx = dring->head; 778 struct netsec_de *de; 779 u32 attr; 780 781 de = dring->vaddr + (DESC_SZ * idx); 782 783 attr = (1 << NETSEC_TX_SHIFT_OWN_FIELD) | 784 (1 << NETSEC_TX_SHIFT_PT_FIELD) | 785 (NETSEC_RING_GMAC << NETSEC_TX_SHIFT_TDRID_FIELD) | 786 (1 << NETSEC_TX_SHIFT_FS_FIELD) | 787 (1 << NETSEC_TX_LAST) | 788 (tx_ctrl->cksum_offload_flag << NETSEC_TX_SHIFT_CO) | 789 (tx_ctrl->tcp_seg_offload_flag << NETSEC_TX_SHIFT_SO) | 790 (1 << NETSEC_TX_SHIFT_TRS_FIELD); 791 if (idx == DESC_NUM - 1) 792 attr |= (1 << NETSEC_TX_SHIFT_LD_FIELD); 793 794 de->data_buf_addr_up = upper_32_bits(desc->dma_addr); 795 de->data_buf_addr_lw = lower_32_bits(desc->dma_addr); 796 de->buf_len_info = (tx_ctrl->tcp_seg_len << 16) | desc->len; 797 de->attr = attr; 798 799 dring->desc[idx] = *desc; 800 if (desc->buf_type == TYPE_NETSEC_SKB) 801 dring->desc[idx].skb = buf; 802 else if (desc->buf_type == TYPE_NETSEC_XDP_TX || 803 desc->buf_type == TYPE_NETSEC_XDP_NDO) 804 dring->desc[idx].xdpf = buf; 805 806 /* move head ahead */ 807 dring->head = (dring->head + 1) % DESC_NUM; 808 } 809 810 /* The current driver only supports 1 Txq, this should run under spin_lock() */ 811 static u32 netsec_xdp_queue_one(struct netsec_priv *priv, 812 struct xdp_frame *xdpf, bool is_ndo) 813 814 { 815 struct netsec_desc_ring *tx_ring = &priv->desc_ring[NETSEC_RING_TX]; 816 struct page *page = virt_to_page(xdpf->data); 817 struct netsec_tx_pkt_ctrl tx_ctrl = {}; 818 struct netsec_desc tx_desc; 819 dma_addr_t dma_handle; 820 u16 filled; 821 822 if (tx_ring->head >= tx_ring->tail) 823 filled = tx_ring->head - tx_ring->tail; 824 else 825 filled = tx_ring->head + DESC_NUM - tx_ring->tail; 826 827 if (DESC_NUM - filled <= 1) 828 return NETSEC_XDP_CONSUMED; 829 830 if (is_ndo) { 831 /* this is for ndo_xdp_xmit, the buffer needs mapping before 832 * sending 833 */ 834 dma_handle = dma_map_single(priv->dev, xdpf->data, xdpf->len, 835 DMA_TO_DEVICE); 836 if (dma_mapping_error(priv->dev, dma_handle)) 837 return NETSEC_XDP_CONSUMED; 838 tx_desc.buf_type = TYPE_NETSEC_XDP_NDO; 839 } else { 840 /* This is the device Rx buffer from page_pool. No need to remap 841 * just sync and send it 842 */ 843 struct netsec_desc_ring *rx_ring = 844 &priv->desc_ring[NETSEC_RING_RX]; 845 enum dma_data_direction dma_dir = 846 page_pool_get_dma_dir(rx_ring->page_pool); 847 848 dma_handle = page_pool_get_dma_addr(page) + xdpf->headroom + 849 sizeof(*xdpf); 850 dma_sync_single_for_device(priv->dev, dma_handle, xdpf->len, 851 dma_dir); 852 tx_desc.buf_type = TYPE_NETSEC_XDP_TX; 853 } 854 855 tx_desc.dma_addr = dma_handle; 856 tx_desc.addr = xdpf->data; 857 tx_desc.len = xdpf->len; 858 859 netdev_sent_queue(priv->ndev, xdpf->len); 860 netsec_set_tx_de(priv, tx_ring, &tx_ctrl, &tx_desc, xdpf); 861 862 return NETSEC_XDP_TX; 863 } 864 865 static u32 netsec_xdp_xmit_back(struct netsec_priv *priv, struct xdp_buff *xdp) 866 { 867 struct netsec_desc_ring *tx_ring = &priv->desc_ring[NETSEC_RING_TX]; 868 struct xdp_frame *xdpf = convert_to_xdp_frame(xdp); 869 u32 ret; 870 871 if (unlikely(!xdpf)) 872 return NETSEC_XDP_CONSUMED; 873 874 spin_lock(&tx_ring->lock); 875 ret = netsec_xdp_queue_one(priv, xdpf, false); 876 spin_unlock(&tx_ring->lock); 877 878 return ret; 879 } 880 881 static u32 netsec_run_xdp(struct netsec_priv *priv, struct bpf_prog *prog, 882 struct xdp_buff *xdp) 883 { 884 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX]; 885 unsigned int len = xdp->data_end - xdp->data; 886 u32 ret = NETSEC_XDP_PASS; 887 int err; 888 u32 act; 889 890 act = bpf_prog_run_xdp(prog, xdp); 891 892 switch (act) { 893 case XDP_PASS: 894 ret = NETSEC_XDP_PASS; 895 break; 896 case XDP_TX: 897 ret = netsec_xdp_xmit_back(priv, xdp); 898 if (ret != NETSEC_XDP_TX) 899 page_pool_put_page(dring->page_pool, 900 virt_to_head_page(xdp->data), len, 901 true); 902 break; 903 case XDP_REDIRECT: 904 err = xdp_do_redirect(priv->ndev, xdp, prog); 905 if (!err) { 906 ret = NETSEC_XDP_REDIR; 907 } else { 908 ret = NETSEC_XDP_CONSUMED; 909 page_pool_put_page(dring->page_pool, 910 virt_to_head_page(xdp->data), len, 911 true); 912 } 913 break; 914 default: 915 bpf_warn_invalid_xdp_action(act); 916 /* fall through */ 917 case XDP_ABORTED: 918 trace_xdp_exception(priv->ndev, prog, act); 919 /* fall through -- handle aborts by dropping packet */ 920 case XDP_DROP: 921 ret = NETSEC_XDP_CONSUMED; 922 page_pool_put_page(dring->page_pool, 923 virt_to_head_page(xdp->data), len, true); 924 break; 925 } 926 927 return ret; 928 } 929 930 static int netsec_process_rx(struct netsec_priv *priv, int budget) 931 { 932 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX]; 933 struct net_device *ndev = priv->ndev; 934 struct netsec_rx_pkt_info rx_info; 935 enum dma_data_direction dma_dir; 936 struct bpf_prog *xdp_prog; 937 u16 xdp_xmit = 0; 938 u32 xdp_act = 0; 939 int done = 0; 940 941 rcu_read_lock(); 942 xdp_prog = READ_ONCE(priv->xdp_prog); 943 dma_dir = page_pool_get_dma_dir(dring->page_pool); 944 945 while (done < budget) { 946 u16 idx = dring->tail; 947 struct netsec_de *de = dring->vaddr + (DESC_SZ * idx); 948 struct netsec_desc *desc = &dring->desc[idx]; 949 struct page *page = virt_to_page(desc->addr); 950 u32 xdp_result = NETSEC_XDP_PASS; 951 struct sk_buff *skb = NULL; 952 u16 pkt_len, desc_len; 953 dma_addr_t dma_handle; 954 struct xdp_buff xdp; 955 void *buf_addr; 956 957 if (de->attr & (1U << NETSEC_RX_PKT_OWN_FIELD)) { 958 /* reading the register clears the irq */ 959 netsec_read(priv, NETSEC_REG_NRM_RX_PKTCNT); 960 break; 961 } 962 963 /* This barrier is needed to keep us from reading 964 * any other fields out of the netsec_de until we have 965 * verified the descriptor has been written back 966 */ 967 dma_rmb(); 968 done++; 969 970 pkt_len = de->buf_len_info >> 16; 971 rx_info.err_code = (de->attr >> NETSEC_RX_PKT_ERR_FIELD) & 972 NETSEC_RX_PKT_ERR_MASK; 973 rx_info.err_flag = (de->attr >> NETSEC_RX_PKT_ER_FIELD) & 1; 974 if (rx_info.err_flag) { 975 netif_err(priv, drv, priv->ndev, 976 "%s: rx fail err(%d)\n", __func__, 977 rx_info.err_code); 978 ndev->stats.rx_dropped++; 979 dring->tail = (dring->tail + 1) % DESC_NUM; 980 /* reuse buffer page frag */ 981 netsec_rx_fill(priv, idx, 1); 982 continue; 983 } 984 rx_info.rx_cksum_result = 985 (de->attr >> NETSEC_RX_PKT_CO_FIELD) & 3; 986 987 /* allocate a fresh buffer and map it to the hardware. 988 * This will eventually replace the old buffer in the hardware 989 */ 990 buf_addr = netsec_alloc_rx_data(priv, &dma_handle, &desc_len); 991 992 if (unlikely(!buf_addr)) 993 break; 994 995 dma_sync_single_for_cpu(priv->dev, desc->dma_addr, pkt_len, 996 dma_dir); 997 prefetch(desc->addr); 998 999 xdp.data_hard_start = desc->addr; 1000 xdp.data = desc->addr + NETSEC_RXBUF_HEADROOM; 1001 xdp_set_data_meta_invalid(&xdp); 1002 xdp.data_end = xdp.data + pkt_len; 1003 xdp.rxq = &dring->xdp_rxq; 1004 1005 if (xdp_prog) { 1006 xdp_result = netsec_run_xdp(priv, xdp_prog, &xdp); 1007 if (xdp_result != NETSEC_XDP_PASS) { 1008 xdp_act |= xdp_result; 1009 if (xdp_result == NETSEC_XDP_TX) 1010 xdp_xmit++; 1011 goto next; 1012 } 1013 } 1014 skb = build_skb(desc->addr, desc->len + NETSEC_RX_BUF_NON_DATA); 1015 1016 if (unlikely(!skb)) { 1017 /* If skb fails recycle_direct will either unmap and 1018 * free the page or refill the cache depending on the 1019 * cache state. Since we paid the allocation cost if 1020 * building an skb fails try to put the page into cache 1021 */ 1022 page_pool_put_page(dring->page_pool, page, pkt_len, 1023 true); 1024 netif_err(priv, drv, priv->ndev, 1025 "rx failed to build skb\n"); 1026 break; 1027 } 1028 page_pool_release_page(dring->page_pool, page); 1029 1030 skb_reserve(skb, xdp.data - xdp.data_hard_start); 1031 skb_put(skb, xdp.data_end - xdp.data); 1032 skb->protocol = eth_type_trans(skb, priv->ndev); 1033 1034 if (priv->rx_cksum_offload_flag && 1035 rx_info.rx_cksum_result == NETSEC_RX_CKSUM_OK) 1036 skb->ip_summed = CHECKSUM_UNNECESSARY; 1037 1038 next: 1039 if ((skb && napi_gro_receive(&priv->napi, skb) != GRO_DROP) || 1040 xdp_result) { 1041 ndev->stats.rx_packets++; 1042 ndev->stats.rx_bytes += xdp.data_end - xdp.data; 1043 } 1044 1045 /* Update the descriptor with fresh buffers */ 1046 desc->len = desc_len; 1047 desc->dma_addr = dma_handle; 1048 desc->addr = buf_addr; 1049 1050 netsec_rx_fill(priv, idx, 1); 1051 dring->tail = (dring->tail + 1) % DESC_NUM; 1052 } 1053 netsec_finalize_xdp_rx(priv, xdp_act, xdp_xmit); 1054 1055 rcu_read_unlock(); 1056 1057 return done; 1058 } 1059 1060 static int netsec_napi_poll(struct napi_struct *napi, int budget) 1061 { 1062 struct netsec_priv *priv; 1063 int done; 1064 1065 priv = container_of(napi, struct netsec_priv, napi); 1066 1067 netsec_process_tx(priv); 1068 done = netsec_process_rx(priv, budget); 1069 1070 if (done < budget && napi_complete_done(napi, done)) { 1071 unsigned long flags; 1072 1073 spin_lock_irqsave(&priv->reglock, flags); 1074 netsec_write(priv, NETSEC_REG_INTEN_SET, 1075 NETSEC_IRQ_RX | NETSEC_IRQ_TX); 1076 spin_unlock_irqrestore(&priv->reglock, flags); 1077 } 1078 1079 return done; 1080 } 1081 1082 1083 static int netsec_desc_used(struct netsec_desc_ring *dring) 1084 { 1085 int used; 1086 1087 if (dring->head >= dring->tail) 1088 used = dring->head - dring->tail; 1089 else 1090 used = dring->head + DESC_NUM - dring->tail; 1091 1092 return used; 1093 } 1094 1095 static int netsec_check_stop_tx(struct netsec_priv *priv, int used) 1096 { 1097 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_TX]; 1098 1099 /* keep tail from touching the queue */ 1100 if (DESC_NUM - used < 2) { 1101 netif_stop_queue(priv->ndev); 1102 1103 /* Make sure we read the updated value in case 1104 * descriptors got freed 1105 */ 1106 smp_rmb(); 1107 1108 used = netsec_desc_used(dring); 1109 if (DESC_NUM - used < 2) 1110 return NETDEV_TX_BUSY; 1111 1112 netif_wake_queue(priv->ndev); 1113 } 1114 1115 return 0; 1116 } 1117 1118 static netdev_tx_t netsec_netdev_start_xmit(struct sk_buff *skb, 1119 struct net_device *ndev) 1120 { 1121 struct netsec_priv *priv = netdev_priv(ndev); 1122 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_TX]; 1123 struct netsec_tx_pkt_ctrl tx_ctrl = {}; 1124 struct netsec_desc tx_desc; 1125 u16 tso_seg_len = 0; 1126 int filled; 1127 1128 spin_lock_bh(&dring->lock); 1129 filled = netsec_desc_used(dring); 1130 if (netsec_check_stop_tx(priv, filled)) { 1131 spin_unlock_bh(&dring->lock); 1132 net_warn_ratelimited("%s %s Tx queue full\n", 1133 dev_name(priv->dev), ndev->name); 1134 return NETDEV_TX_BUSY; 1135 } 1136 1137 if (skb->ip_summed == CHECKSUM_PARTIAL) 1138 tx_ctrl.cksum_offload_flag = true; 1139 1140 if (skb_is_gso(skb)) 1141 tso_seg_len = skb_shinfo(skb)->gso_size; 1142 1143 if (tso_seg_len > 0) { 1144 if (skb->protocol == htons(ETH_P_IP)) { 1145 ip_hdr(skb)->tot_len = 0; 1146 tcp_hdr(skb)->check = 1147 ~tcp_v4_check(0, ip_hdr(skb)->saddr, 1148 ip_hdr(skb)->daddr, 0); 1149 } else { 1150 tcp_v6_gso_csum_prep(skb); 1151 } 1152 1153 tx_ctrl.tcp_seg_offload_flag = true; 1154 tx_ctrl.tcp_seg_len = tso_seg_len; 1155 } 1156 1157 tx_desc.dma_addr = dma_map_single(priv->dev, skb->data, 1158 skb_headlen(skb), DMA_TO_DEVICE); 1159 if (dma_mapping_error(priv->dev, tx_desc.dma_addr)) { 1160 spin_unlock_bh(&dring->lock); 1161 netif_err(priv, drv, priv->ndev, 1162 "%s: DMA mapping failed\n", __func__); 1163 ndev->stats.tx_dropped++; 1164 dev_kfree_skb_any(skb); 1165 return NETDEV_TX_OK; 1166 } 1167 tx_desc.addr = skb->data; 1168 tx_desc.len = skb_headlen(skb); 1169 tx_desc.buf_type = TYPE_NETSEC_SKB; 1170 1171 skb_tx_timestamp(skb); 1172 netdev_sent_queue(priv->ndev, skb->len); 1173 1174 netsec_set_tx_de(priv, dring, &tx_ctrl, &tx_desc, skb); 1175 spin_unlock_bh(&dring->lock); 1176 netsec_write(priv, NETSEC_REG_NRM_TX_PKTCNT, 1); /* submit another tx */ 1177 1178 return NETDEV_TX_OK; 1179 } 1180 1181 static void netsec_uninit_pkt_dring(struct netsec_priv *priv, int id) 1182 { 1183 struct netsec_desc_ring *dring = &priv->desc_ring[id]; 1184 struct netsec_desc *desc; 1185 u16 idx; 1186 1187 if (!dring->vaddr || !dring->desc) 1188 return; 1189 for (idx = 0; idx < DESC_NUM; idx++) { 1190 desc = &dring->desc[idx]; 1191 if (!desc->addr) 1192 continue; 1193 1194 if (id == NETSEC_RING_RX) { 1195 struct page *page = virt_to_page(desc->addr); 1196 1197 page_pool_put_full_page(dring->page_pool, page, false); 1198 } else if (id == NETSEC_RING_TX) { 1199 dma_unmap_single(priv->dev, desc->dma_addr, desc->len, 1200 DMA_TO_DEVICE); 1201 dev_kfree_skb(desc->skb); 1202 } 1203 } 1204 1205 /* Rx is currently using page_pool */ 1206 if (id == NETSEC_RING_RX) { 1207 if (xdp_rxq_info_is_reg(&dring->xdp_rxq)) 1208 xdp_rxq_info_unreg(&dring->xdp_rxq); 1209 page_pool_destroy(dring->page_pool); 1210 } 1211 1212 memset(dring->desc, 0, sizeof(struct netsec_desc) * DESC_NUM); 1213 memset(dring->vaddr, 0, DESC_SZ * DESC_NUM); 1214 1215 dring->head = 0; 1216 dring->tail = 0; 1217 1218 if (id == NETSEC_RING_TX) 1219 netdev_reset_queue(priv->ndev); 1220 } 1221 1222 static void netsec_free_dring(struct netsec_priv *priv, int id) 1223 { 1224 struct netsec_desc_ring *dring = &priv->desc_ring[id]; 1225 1226 if (dring->vaddr) { 1227 dma_free_coherent(priv->dev, DESC_SZ * DESC_NUM, 1228 dring->vaddr, dring->desc_dma); 1229 dring->vaddr = NULL; 1230 } 1231 1232 kfree(dring->desc); 1233 dring->desc = NULL; 1234 } 1235 1236 static int netsec_alloc_dring(struct netsec_priv *priv, enum ring_id id) 1237 { 1238 struct netsec_desc_ring *dring = &priv->desc_ring[id]; 1239 1240 dring->vaddr = dma_alloc_coherent(priv->dev, DESC_SZ * DESC_NUM, 1241 &dring->desc_dma, GFP_KERNEL); 1242 if (!dring->vaddr) 1243 goto err; 1244 1245 dring->desc = kcalloc(DESC_NUM, sizeof(*dring->desc), GFP_KERNEL); 1246 if (!dring->desc) 1247 goto err; 1248 1249 return 0; 1250 err: 1251 netsec_free_dring(priv, id); 1252 1253 return -ENOMEM; 1254 } 1255 1256 static void netsec_setup_tx_dring(struct netsec_priv *priv) 1257 { 1258 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_TX]; 1259 int i; 1260 1261 for (i = 0; i < DESC_NUM; i++) { 1262 struct netsec_de *de; 1263 1264 de = dring->vaddr + (DESC_SZ * i); 1265 /* de->attr is not going to be accessed by the NIC 1266 * until netsec_set_tx_de() is called. 1267 * No need for a dma_wmb() here 1268 */ 1269 de->attr = 1U << NETSEC_TX_SHIFT_OWN_FIELD; 1270 } 1271 } 1272 1273 static int netsec_setup_rx_dring(struct netsec_priv *priv) 1274 { 1275 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX]; 1276 struct bpf_prog *xdp_prog = READ_ONCE(priv->xdp_prog); 1277 struct page_pool_params pp_params = { 1278 .order = 0, 1279 /* internal DMA mapping in page_pool */ 1280 .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV, 1281 .pool_size = DESC_NUM, 1282 .nid = NUMA_NO_NODE, 1283 .dev = priv->dev, 1284 .dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE, 1285 .offset = NETSEC_RXBUF_HEADROOM, 1286 .max_len = NETSEC_RX_BUF_SIZE, 1287 }; 1288 int i, err; 1289 1290 dring->page_pool = page_pool_create(&pp_params); 1291 if (IS_ERR(dring->page_pool)) { 1292 err = PTR_ERR(dring->page_pool); 1293 dring->page_pool = NULL; 1294 goto err_out; 1295 } 1296 1297 err = xdp_rxq_info_reg(&dring->xdp_rxq, priv->ndev, 0); 1298 if (err) 1299 goto err_out; 1300 1301 err = xdp_rxq_info_reg_mem_model(&dring->xdp_rxq, MEM_TYPE_PAGE_POOL, 1302 dring->page_pool); 1303 if (err) 1304 goto err_out; 1305 1306 for (i = 0; i < DESC_NUM; i++) { 1307 struct netsec_desc *desc = &dring->desc[i]; 1308 dma_addr_t dma_handle; 1309 void *buf; 1310 u16 len; 1311 1312 buf = netsec_alloc_rx_data(priv, &dma_handle, &len); 1313 1314 if (!buf) { 1315 err = -ENOMEM; 1316 goto err_out; 1317 } 1318 desc->dma_addr = dma_handle; 1319 desc->addr = buf; 1320 desc->len = len; 1321 } 1322 1323 netsec_rx_fill(priv, 0, DESC_NUM); 1324 1325 return 0; 1326 1327 err_out: 1328 netsec_uninit_pkt_dring(priv, NETSEC_RING_RX); 1329 return err; 1330 } 1331 1332 static int netsec_netdev_load_ucode_region(struct netsec_priv *priv, u32 reg, 1333 u32 addr_h, u32 addr_l, u32 size) 1334 { 1335 u64 base = (u64)addr_h << 32 | addr_l; 1336 void __iomem *ucode; 1337 u32 i; 1338 1339 ucode = ioremap(base, size * sizeof(u32)); 1340 if (!ucode) 1341 return -ENOMEM; 1342 1343 for (i = 0; i < size; i++) 1344 netsec_write(priv, reg, readl(ucode + i * 4)); 1345 1346 iounmap(ucode); 1347 return 0; 1348 } 1349 1350 static int netsec_netdev_load_microcode(struct netsec_priv *priv) 1351 { 1352 u32 addr_h, addr_l, size; 1353 int err; 1354 1355 addr_h = readl(priv->eeprom_base + NETSEC_EEPROM_HM_ME_ADDRESS_H); 1356 addr_l = readl(priv->eeprom_base + NETSEC_EEPROM_HM_ME_ADDRESS_L); 1357 size = readl(priv->eeprom_base + NETSEC_EEPROM_HM_ME_SIZE); 1358 err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_DMAC_HM_CMD_BUF, 1359 addr_h, addr_l, size); 1360 if (err) 1361 return err; 1362 1363 addr_h = readl(priv->eeprom_base + NETSEC_EEPROM_MH_ME_ADDRESS_H); 1364 addr_l = readl(priv->eeprom_base + NETSEC_EEPROM_MH_ME_ADDRESS_L); 1365 size = readl(priv->eeprom_base + NETSEC_EEPROM_MH_ME_SIZE); 1366 err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_DMAC_MH_CMD_BUF, 1367 addr_h, addr_l, size); 1368 if (err) 1369 return err; 1370 1371 addr_h = 0; 1372 addr_l = readl(priv->eeprom_base + NETSEC_EEPROM_PKT_ME_ADDRESS); 1373 size = readl(priv->eeprom_base + NETSEC_EEPROM_PKT_ME_SIZE); 1374 err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_PKT_CMD_BUF, 1375 addr_h, addr_l, size); 1376 if (err) 1377 return err; 1378 1379 return 0; 1380 } 1381 1382 static int netsec_reset_hardware(struct netsec_priv *priv, 1383 bool load_ucode) 1384 { 1385 u32 value; 1386 int err; 1387 1388 /* stop DMA engines */ 1389 if (!netsec_read(priv, NETSEC_REG_ADDR_DIS_CORE)) { 1390 netsec_write(priv, NETSEC_REG_DMA_HM_CTRL, 1391 NETSEC_DMA_CTRL_REG_STOP); 1392 netsec_write(priv, NETSEC_REG_DMA_MH_CTRL, 1393 NETSEC_DMA_CTRL_REG_STOP); 1394 1395 while (netsec_read(priv, NETSEC_REG_DMA_HM_CTRL) & 1396 NETSEC_DMA_CTRL_REG_STOP) 1397 cpu_relax(); 1398 1399 while (netsec_read(priv, NETSEC_REG_DMA_MH_CTRL) & 1400 NETSEC_DMA_CTRL_REG_STOP) 1401 cpu_relax(); 1402 } 1403 1404 netsec_write(priv, NETSEC_REG_SOFT_RST, NETSEC_SOFT_RST_REG_RESET); 1405 netsec_write(priv, NETSEC_REG_SOFT_RST, NETSEC_SOFT_RST_REG_RUN); 1406 netsec_write(priv, NETSEC_REG_COM_INIT, NETSEC_COM_INIT_REG_ALL); 1407 1408 while (netsec_read(priv, NETSEC_REG_COM_INIT) != 0) 1409 cpu_relax(); 1410 1411 /* set desc_start addr */ 1412 netsec_write(priv, NETSEC_REG_NRM_RX_DESC_START_UP, 1413 upper_32_bits(priv->desc_ring[NETSEC_RING_RX].desc_dma)); 1414 netsec_write(priv, NETSEC_REG_NRM_RX_DESC_START_LW, 1415 lower_32_bits(priv->desc_ring[NETSEC_RING_RX].desc_dma)); 1416 1417 netsec_write(priv, NETSEC_REG_NRM_TX_DESC_START_UP, 1418 upper_32_bits(priv->desc_ring[NETSEC_RING_TX].desc_dma)); 1419 netsec_write(priv, NETSEC_REG_NRM_TX_DESC_START_LW, 1420 lower_32_bits(priv->desc_ring[NETSEC_RING_TX].desc_dma)); 1421 1422 /* set normal tx dring ring config */ 1423 netsec_write(priv, NETSEC_REG_NRM_TX_CONFIG, 1424 1 << NETSEC_REG_DESC_ENDIAN); 1425 netsec_write(priv, NETSEC_REG_NRM_RX_CONFIG, 1426 1 << NETSEC_REG_DESC_ENDIAN); 1427 1428 if (load_ucode) { 1429 err = netsec_netdev_load_microcode(priv); 1430 if (err) { 1431 netif_err(priv, probe, priv->ndev, 1432 "%s: failed to load microcode (%d)\n", 1433 __func__, err); 1434 return err; 1435 } 1436 } 1437 1438 /* start DMA engines */ 1439 netsec_write(priv, NETSEC_REG_DMA_TMR_CTRL, priv->freq / 1000000 - 1); 1440 netsec_write(priv, NETSEC_REG_ADDR_DIS_CORE, 0); 1441 1442 usleep_range(1000, 2000); 1443 1444 if (!(netsec_read(priv, NETSEC_REG_TOP_STATUS) & 1445 NETSEC_TOP_IRQ_REG_CODE_LOAD_END)) { 1446 netif_err(priv, probe, priv->ndev, 1447 "microengine start failed\n"); 1448 return -ENXIO; 1449 } 1450 netsec_write(priv, NETSEC_REG_TOP_STATUS, 1451 NETSEC_TOP_IRQ_REG_CODE_LOAD_END); 1452 1453 value = NETSEC_PKT_CTRL_REG_MODE_NRM; 1454 if (priv->ndev->mtu > ETH_DATA_LEN) 1455 value |= NETSEC_PKT_CTRL_REG_EN_JUMBO; 1456 1457 /* change to normal mode */ 1458 netsec_write(priv, NETSEC_REG_DMA_MH_CTRL, MH_CTRL__MODE_TRANS); 1459 netsec_write(priv, NETSEC_REG_PKT_CTRL, value); 1460 1461 while ((netsec_read(priv, NETSEC_REG_MODE_TRANS_COMP_STATUS) & 1462 NETSEC_MODE_TRANS_COMP_IRQ_T2N) == 0) 1463 cpu_relax(); 1464 1465 /* clear any pending EMPTY/ERR irq status */ 1466 netsec_write(priv, NETSEC_REG_NRM_TX_STATUS, ~0); 1467 1468 /* Disable TX & RX intr */ 1469 netsec_write(priv, NETSEC_REG_INTEN_CLR, ~0); 1470 1471 return 0; 1472 } 1473 1474 static int netsec_start_gmac(struct netsec_priv *priv) 1475 { 1476 struct phy_device *phydev = priv->ndev->phydev; 1477 u32 value = 0; 1478 int ret; 1479 1480 if (phydev->speed != SPEED_1000) 1481 value = (NETSEC_GMAC_MCR_REG_CST | 1482 NETSEC_GMAC_MCR_REG_HALF_DUPLEX_COMMON); 1483 1484 if (netsec_mac_write(priv, GMAC_REG_MCR, value)) 1485 return -ETIMEDOUT; 1486 if (netsec_mac_write(priv, GMAC_REG_BMR, 1487 NETSEC_GMAC_BMR_REG_RESET)) 1488 return -ETIMEDOUT; 1489 1490 /* Wait soft reset */ 1491 usleep_range(1000, 5000); 1492 1493 ret = netsec_mac_read(priv, GMAC_REG_BMR, &value); 1494 if (ret) 1495 return ret; 1496 if (value & NETSEC_GMAC_BMR_REG_SWR) 1497 return -EAGAIN; 1498 1499 netsec_write(priv, MAC_REG_DESC_SOFT_RST, 1); 1500 if (netsec_wait_while_busy(priv, MAC_REG_DESC_SOFT_RST, 1)) 1501 return -ETIMEDOUT; 1502 1503 netsec_write(priv, MAC_REG_DESC_INIT, 1); 1504 if (netsec_wait_while_busy(priv, MAC_REG_DESC_INIT, 1)) 1505 return -ETIMEDOUT; 1506 1507 if (netsec_mac_write(priv, GMAC_REG_BMR, 1508 NETSEC_GMAC_BMR_REG_COMMON)) 1509 return -ETIMEDOUT; 1510 if (netsec_mac_write(priv, GMAC_REG_RDLAR, 1511 NETSEC_GMAC_RDLAR_REG_COMMON)) 1512 return -ETIMEDOUT; 1513 if (netsec_mac_write(priv, GMAC_REG_TDLAR, 1514 NETSEC_GMAC_TDLAR_REG_COMMON)) 1515 return -ETIMEDOUT; 1516 if (netsec_mac_write(priv, GMAC_REG_MFFR, 0x80000001)) 1517 return -ETIMEDOUT; 1518 1519 ret = netsec_mac_update_to_phy_state(priv); 1520 if (ret) 1521 return ret; 1522 1523 ret = netsec_mac_read(priv, GMAC_REG_OMR, &value); 1524 if (ret) 1525 return ret; 1526 1527 value |= NETSEC_GMAC_OMR_REG_SR; 1528 value |= NETSEC_GMAC_OMR_REG_ST; 1529 1530 netsec_write(priv, NETSEC_REG_NRM_RX_INTEN_CLR, ~0); 1531 netsec_write(priv, NETSEC_REG_NRM_TX_INTEN_CLR, ~0); 1532 1533 netsec_et_set_coalesce(priv->ndev, &priv->et_coalesce); 1534 1535 if (netsec_mac_write(priv, GMAC_REG_OMR, value)) 1536 return -ETIMEDOUT; 1537 1538 return 0; 1539 } 1540 1541 static int netsec_stop_gmac(struct netsec_priv *priv) 1542 { 1543 u32 value; 1544 int ret; 1545 1546 ret = netsec_mac_read(priv, GMAC_REG_OMR, &value); 1547 if (ret) 1548 return ret; 1549 value &= ~NETSEC_GMAC_OMR_REG_SR; 1550 value &= ~NETSEC_GMAC_OMR_REG_ST; 1551 1552 /* disable all interrupts */ 1553 netsec_write(priv, NETSEC_REG_NRM_RX_INTEN_CLR, ~0); 1554 netsec_write(priv, NETSEC_REG_NRM_TX_INTEN_CLR, ~0); 1555 1556 return netsec_mac_write(priv, GMAC_REG_OMR, value); 1557 } 1558 1559 static void netsec_phy_adjust_link(struct net_device *ndev) 1560 { 1561 struct netsec_priv *priv = netdev_priv(ndev); 1562 1563 if (ndev->phydev->link) 1564 netsec_start_gmac(priv); 1565 else 1566 netsec_stop_gmac(priv); 1567 1568 phy_print_status(ndev->phydev); 1569 } 1570 1571 static irqreturn_t netsec_irq_handler(int irq, void *dev_id) 1572 { 1573 struct netsec_priv *priv = dev_id; 1574 u32 val, status = netsec_read(priv, NETSEC_REG_TOP_STATUS); 1575 unsigned long flags; 1576 1577 /* Disable interrupts */ 1578 if (status & NETSEC_IRQ_TX) { 1579 val = netsec_read(priv, NETSEC_REG_NRM_TX_STATUS); 1580 netsec_write(priv, NETSEC_REG_NRM_TX_STATUS, val); 1581 } 1582 if (status & NETSEC_IRQ_RX) { 1583 val = netsec_read(priv, NETSEC_REG_NRM_RX_STATUS); 1584 netsec_write(priv, NETSEC_REG_NRM_RX_STATUS, val); 1585 } 1586 1587 spin_lock_irqsave(&priv->reglock, flags); 1588 netsec_write(priv, NETSEC_REG_INTEN_CLR, NETSEC_IRQ_RX | NETSEC_IRQ_TX); 1589 spin_unlock_irqrestore(&priv->reglock, flags); 1590 1591 napi_schedule(&priv->napi); 1592 1593 return IRQ_HANDLED; 1594 } 1595 1596 static int netsec_netdev_open(struct net_device *ndev) 1597 { 1598 struct netsec_priv *priv = netdev_priv(ndev); 1599 int ret; 1600 1601 pm_runtime_get_sync(priv->dev); 1602 1603 netsec_setup_tx_dring(priv); 1604 ret = netsec_setup_rx_dring(priv); 1605 if (ret) { 1606 netif_err(priv, probe, priv->ndev, 1607 "%s: fail setup ring\n", __func__); 1608 goto err1; 1609 } 1610 1611 ret = request_irq(priv->ndev->irq, netsec_irq_handler, 1612 IRQF_SHARED, "netsec", priv); 1613 if (ret) { 1614 netif_err(priv, drv, priv->ndev, "request_irq failed\n"); 1615 goto err2; 1616 } 1617 1618 if (dev_of_node(priv->dev)) { 1619 if (!of_phy_connect(priv->ndev, priv->phy_np, 1620 netsec_phy_adjust_link, 0, 1621 priv->phy_interface)) { 1622 netif_err(priv, link, priv->ndev, "missing PHY\n"); 1623 ret = -ENODEV; 1624 goto err3; 1625 } 1626 } else { 1627 ret = phy_connect_direct(priv->ndev, priv->phydev, 1628 netsec_phy_adjust_link, 1629 priv->phy_interface); 1630 if (ret) { 1631 netif_err(priv, link, priv->ndev, 1632 "phy_connect_direct() failed (%d)\n", ret); 1633 goto err3; 1634 } 1635 } 1636 1637 phy_start(ndev->phydev); 1638 1639 netsec_start_gmac(priv); 1640 napi_enable(&priv->napi); 1641 netif_start_queue(ndev); 1642 1643 /* Enable TX+RX intr. */ 1644 netsec_write(priv, NETSEC_REG_INTEN_SET, NETSEC_IRQ_RX | NETSEC_IRQ_TX); 1645 1646 return 0; 1647 err3: 1648 free_irq(priv->ndev->irq, priv); 1649 err2: 1650 netsec_uninit_pkt_dring(priv, NETSEC_RING_RX); 1651 err1: 1652 pm_runtime_put_sync(priv->dev); 1653 return ret; 1654 } 1655 1656 static int netsec_netdev_stop(struct net_device *ndev) 1657 { 1658 int ret; 1659 struct netsec_priv *priv = netdev_priv(ndev); 1660 1661 netif_stop_queue(priv->ndev); 1662 dma_wmb(); 1663 1664 napi_disable(&priv->napi); 1665 1666 netsec_write(priv, NETSEC_REG_INTEN_CLR, ~0); 1667 netsec_stop_gmac(priv); 1668 1669 free_irq(priv->ndev->irq, priv); 1670 1671 netsec_uninit_pkt_dring(priv, NETSEC_RING_TX); 1672 netsec_uninit_pkt_dring(priv, NETSEC_RING_RX); 1673 1674 phy_stop(ndev->phydev); 1675 phy_disconnect(ndev->phydev); 1676 1677 ret = netsec_reset_hardware(priv, false); 1678 1679 pm_runtime_put_sync(priv->dev); 1680 1681 return ret; 1682 } 1683 1684 static int netsec_netdev_init(struct net_device *ndev) 1685 { 1686 struct netsec_priv *priv = netdev_priv(ndev); 1687 int ret; 1688 u16 data; 1689 1690 BUILD_BUG_ON_NOT_POWER_OF_2(DESC_NUM); 1691 1692 ret = netsec_alloc_dring(priv, NETSEC_RING_TX); 1693 if (ret) 1694 return ret; 1695 1696 ret = netsec_alloc_dring(priv, NETSEC_RING_RX); 1697 if (ret) 1698 goto err1; 1699 1700 /* set phy power down */ 1701 data = netsec_phy_read(priv->mii_bus, priv->phy_addr, MII_BMCR) | 1702 BMCR_PDOWN; 1703 netsec_phy_write(priv->mii_bus, priv->phy_addr, MII_BMCR, data); 1704 1705 ret = netsec_reset_hardware(priv, true); 1706 if (ret) 1707 goto err2; 1708 1709 spin_lock_init(&priv->desc_ring[NETSEC_RING_TX].lock); 1710 spin_lock_init(&priv->desc_ring[NETSEC_RING_RX].lock); 1711 1712 return 0; 1713 err2: 1714 netsec_free_dring(priv, NETSEC_RING_RX); 1715 err1: 1716 netsec_free_dring(priv, NETSEC_RING_TX); 1717 return ret; 1718 } 1719 1720 static void netsec_netdev_uninit(struct net_device *ndev) 1721 { 1722 struct netsec_priv *priv = netdev_priv(ndev); 1723 1724 netsec_free_dring(priv, NETSEC_RING_RX); 1725 netsec_free_dring(priv, NETSEC_RING_TX); 1726 } 1727 1728 static int netsec_netdev_set_features(struct net_device *ndev, 1729 netdev_features_t features) 1730 { 1731 struct netsec_priv *priv = netdev_priv(ndev); 1732 1733 priv->rx_cksum_offload_flag = !!(features & NETIF_F_RXCSUM); 1734 1735 return 0; 1736 } 1737 1738 static int netsec_xdp_xmit(struct net_device *ndev, int n, 1739 struct xdp_frame **frames, u32 flags) 1740 { 1741 struct netsec_priv *priv = netdev_priv(ndev); 1742 struct netsec_desc_ring *tx_ring = &priv->desc_ring[NETSEC_RING_TX]; 1743 int drops = 0; 1744 int i; 1745 1746 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 1747 return -EINVAL; 1748 1749 spin_lock(&tx_ring->lock); 1750 for (i = 0; i < n; i++) { 1751 struct xdp_frame *xdpf = frames[i]; 1752 int err; 1753 1754 err = netsec_xdp_queue_one(priv, xdpf, true); 1755 if (err != NETSEC_XDP_TX) { 1756 xdp_return_frame_rx_napi(xdpf); 1757 drops++; 1758 } else { 1759 tx_ring->xdp_xmit++; 1760 } 1761 } 1762 spin_unlock(&tx_ring->lock); 1763 1764 if (unlikely(flags & XDP_XMIT_FLUSH)) { 1765 netsec_xdp_ring_tx_db(priv, tx_ring->xdp_xmit); 1766 tx_ring->xdp_xmit = 0; 1767 } 1768 1769 return n - drops; 1770 } 1771 1772 static int netsec_xdp_setup(struct netsec_priv *priv, struct bpf_prog *prog, 1773 struct netlink_ext_ack *extack) 1774 { 1775 struct net_device *dev = priv->ndev; 1776 struct bpf_prog *old_prog; 1777 1778 /* For now just support only the usual MTU sized frames */ 1779 if (prog && dev->mtu > 1500) { 1780 NL_SET_ERR_MSG_MOD(extack, "Jumbo frames not supported on XDP"); 1781 return -EOPNOTSUPP; 1782 } 1783 1784 if (netif_running(dev)) 1785 netsec_netdev_stop(dev); 1786 1787 /* Detach old prog, if any */ 1788 old_prog = xchg(&priv->xdp_prog, prog); 1789 if (old_prog) 1790 bpf_prog_put(old_prog); 1791 1792 if (netif_running(dev)) 1793 netsec_netdev_open(dev); 1794 1795 return 0; 1796 } 1797 1798 static int netsec_xdp(struct net_device *ndev, struct netdev_bpf *xdp) 1799 { 1800 struct netsec_priv *priv = netdev_priv(ndev); 1801 1802 switch (xdp->command) { 1803 case XDP_SETUP_PROG: 1804 return netsec_xdp_setup(priv, xdp->prog, xdp->extack); 1805 case XDP_QUERY_PROG: 1806 xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id : 0; 1807 return 0; 1808 default: 1809 return -EINVAL; 1810 } 1811 } 1812 1813 static const struct net_device_ops netsec_netdev_ops = { 1814 .ndo_init = netsec_netdev_init, 1815 .ndo_uninit = netsec_netdev_uninit, 1816 .ndo_open = netsec_netdev_open, 1817 .ndo_stop = netsec_netdev_stop, 1818 .ndo_start_xmit = netsec_netdev_start_xmit, 1819 .ndo_set_features = netsec_netdev_set_features, 1820 .ndo_set_mac_address = eth_mac_addr, 1821 .ndo_validate_addr = eth_validate_addr, 1822 .ndo_do_ioctl = phy_do_ioctl, 1823 .ndo_xdp_xmit = netsec_xdp_xmit, 1824 .ndo_bpf = netsec_xdp, 1825 }; 1826 1827 static int netsec_of_probe(struct platform_device *pdev, 1828 struct netsec_priv *priv, u32 *phy_addr) 1829 { 1830 priv->phy_np = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0); 1831 if (!priv->phy_np) { 1832 dev_err(&pdev->dev, "missing required property 'phy-handle'\n"); 1833 return -EINVAL; 1834 } 1835 1836 *phy_addr = of_mdio_parse_addr(&pdev->dev, priv->phy_np); 1837 1838 priv->clk = devm_clk_get(&pdev->dev, NULL); /* get by 'phy_ref_clk' */ 1839 if (IS_ERR(priv->clk)) { 1840 dev_err(&pdev->dev, "phy_ref_clk not found\n"); 1841 return PTR_ERR(priv->clk); 1842 } 1843 priv->freq = clk_get_rate(priv->clk); 1844 1845 return 0; 1846 } 1847 1848 static int netsec_acpi_probe(struct platform_device *pdev, 1849 struct netsec_priv *priv, u32 *phy_addr) 1850 { 1851 int ret; 1852 1853 if (!IS_ENABLED(CONFIG_ACPI)) 1854 return -ENODEV; 1855 1856 ret = device_property_read_u32(&pdev->dev, "phy-channel", phy_addr); 1857 if (ret) { 1858 dev_err(&pdev->dev, 1859 "missing required property 'phy-channel'\n"); 1860 return ret; 1861 } 1862 1863 ret = device_property_read_u32(&pdev->dev, 1864 "socionext,phy-clock-frequency", 1865 &priv->freq); 1866 if (ret) 1867 dev_err(&pdev->dev, 1868 "missing required property 'socionext,phy-clock-frequency'\n"); 1869 return ret; 1870 } 1871 1872 static void netsec_unregister_mdio(struct netsec_priv *priv) 1873 { 1874 struct phy_device *phydev = priv->phydev; 1875 1876 if (!dev_of_node(priv->dev) && phydev) { 1877 phy_device_remove(phydev); 1878 phy_device_free(phydev); 1879 } 1880 1881 mdiobus_unregister(priv->mii_bus); 1882 } 1883 1884 static int netsec_register_mdio(struct netsec_priv *priv, u32 phy_addr) 1885 { 1886 struct mii_bus *bus; 1887 int ret; 1888 1889 bus = devm_mdiobus_alloc(priv->dev); 1890 if (!bus) 1891 return -ENOMEM; 1892 1893 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", dev_name(priv->dev)); 1894 bus->priv = priv; 1895 bus->name = "SNI NETSEC MDIO"; 1896 bus->read = netsec_phy_read; 1897 bus->write = netsec_phy_write; 1898 bus->parent = priv->dev; 1899 priv->mii_bus = bus; 1900 1901 if (dev_of_node(priv->dev)) { 1902 struct device_node *mdio_node, *parent = dev_of_node(priv->dev); 1903 1904 mdio_node = of_get_child_by_name(parent, "mdio"); 1905 if (mdio_node) { 1906 parent = mdio_node; 1907 } else { 1908 /* older f/w doesn't populate the mdio subnode, 1909 * allow relaxed upgrade of f/w in due time. 1910 */ 1911 dev_info(priv->dev, "Upgrade f/w for mdio subnode!\n"); 1912 } 1913 1914 ret = of_mdiobus_register(bus, parent); 1915 of_node_put(mdio_node); 1916 1917 if (ret) { 1918 dev_err(priv->dev, "mdiobus register err(%d)\n", ret); 1919 return ret; 1920 } 1921 } else { 1922 /* Mask out all PHYs from auto probing. */ 1923 bus->phy_mask = ~0; 1924 ret = mdiobus_register(bus); 1925 if (ret) { 1926 dev_err(priv->dev, "mdiobus register err(%d)\n", ret); 1927 return ret; 1928 } 1929 1930 priv->phydev = get_phy_device(bus, phy_addr, false); 1931 if (IS_ERR(priv->phydev)) { 1932 ret = PTR_ERR(priv->phydev); 1933 dev_err(priv->dev, "get_phy_device err(%d)\n", ret); 1934 priv->phydev = NULL; 1935 return -ENODEV; 1936 } 1937 1938 ret = phy_device_register(priv->phydev); 1939 if (ret) { 1940 mdiobus_unregister(bus); 1941 dev_err(priv->dev, 1942 "phy_device_register err(%d)\n", ret); 1943 } 1944 } 1945 1946 return ret; 1947 } 1948 1949 static int netsec_probe(struct platform_device *pdev) 1950 { 1951 struct resource *mmio_res, *eeprom_res, *irq_res; 1952 u8 *mac, macbuf[ETH_ALEN]; 1953 struct netsec_priv *priv; 1954 u32 hw_ver, phy_addr = 0; 1955 struct net_device *ndev; 1956 int ret; 1957 1958 mmio_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1959 if (!mmio_res) { 1960 dev_err(&pdev->dev, "No MMIO resource found.\n"); 1961 return -ENODEV; 1962 } 1963 1964 eeprom_res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1965 if (!eeprom_res) { 1966 dev_info(&pdev->dev, "No EEPROM resource found.\n"); 1967 return -ENODEV; 1968 } 1969 1970 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 1971 if (!irq_res) { 1972 dev_err(&pdev->dev, "No IRQ resource found.\n"); 1973 return -ENODEV; 1974 } 1975 1976 ndev = alloc_etherdev(sizeof(*priv)); 1977 if (!ndev) 1978 return -ENOMEM; 1979 1980 priv = netdev_priv(ndev); 1981 1982 spin_lock_init(&priv->reglock); 1983 SET_NETDEV_DEV(ndev, &pdev->dev); 1984 platform_set_drvdata(pdev, priv); 1985 ndev->irq = irq_res->start; 1986 priv->dev = &pdev->dev; 1987 priv->ndev = ndev; 1988 1989 priv->msg_enable = NETIF_MSG_TX_ERR | NETIF_MSG_HW | NETIF_MSG_DRV | 1990 NETIF_MSG_LINK | NETIF_MSG_PROBE; 1991 1992 priv->phy_interface = device_get_phy_mode(&pdev->dev); 1993 if ((int)priv->phy_interface < 0) { 1994 dev_err(&pdev->dev, "missing required property 'phy-mode'\n"); 1995 ret = -ENODEV; 1996 goto free_ndev; 1997 } 1998 1999 priv->ioaddr = devm_ioremap(&pdev->dev, mmio_res->start, 2000 resource_size(mmio_res)); 2001 if (!priv->ioaddr) { 2002 dev_err(&pdev->dev, "devm_ioremap() failed\n"); 2003 ret = -ENXIO; 2004 goto free_ndev; 2005 } 2006 2007 priv->eeprom_base = devm_ioremap(&pdev->dev, eeprom_res->start, 2008 resource_size(eeprom_res)); 2009 if (!priv->eeprom_base) { 2010 dev_err(&pdev->dev, "devm_ioremap() failed for EEPROM\n"); 2011 ret = -ENXIO; 2012 goto free_ndev; 2013 } 2014 2015 mac = device_get_mac_address(&pdev->dev, macbuf, sizeof(macbuf)); 2016 if (mac) 2017 ether_addr_copy(ndev->dev_addr, mac); 2018 2019 if (priv->eeprom_base && 2020 (!mac || !is_valid_ether_addr(ndev->dev_addr))) { 2021 void __iomem *macp = priv->eeprom_base + 2022 NETSEC_EEPROM_MAC_ADDRESS; 2023 2024 ndev->dev_addr[0] = readb(macp + 3); 2025 ndev->dev_addr[1] = readb(macp + 2); 2026 ndev->dev_addr[2] = readb(macp + 1); 2027 ndev->dev_addr[3] = readb(macp + 0); 2028 ndev->dev_addr[4] = readb(macp + 7); 2029 ndev->dev_addr[5] = readb(macp + 6); 2030 } 2031 2032 if (!is_valid_ether_addr(ndev->dev_addr)) { 2033 dev_warn(&pdev->dev, "No MAC address found, using random\n"); 2034 eth_hw_addr_random(ndev); 2035 } 2036 2037 if (dev_of_node(&pdev->dev)) 2038 ret = netsec_of_probe(pdev, priv, &phy_addr); 2039 else 2040 ret = netsec_acpi_probe(pdev, priv, &phy_addr); 2041 if (ret) 2042 goto free_ndev; 2043 2044 priv->phy_addr = phy_addr; 2045 2046 if (!priv->freq) { 2047 dev_err(&pdev->dev, "missing PHY reference clock frequency\n"); 2048 ret = -ENODEV; 2049 goto free_ndev; 2050 } 2051 2052 /* default for throughput */ 2053 priv->et_coalesce.rx_coalesce_usecs = 500; 2054 priv->et_coalesce.rx_max_coalesced_frames = 8; 2055 priv->et_coalesce.tx_coalesce_usecs = 500; 2056 priv->et_coalesce.tx_max_coalesced_frames = 8; 2057 2058 ret = device_property_read_u32(&pdev->dev, "max-frame-size", 2059 &ndev->max_mtu); 2060 if (ret < 0) 2061 ndev->max_mtu = ETH_DATA_LEN; 2062 2063 /* runtime_pm coverage just for probe, open/close also cover it */ 2064 pm_runtime_enable(&pdev->dev); 2065 pm_runtime_get_sync(&pdev->dev); 2066 2067 hw_ver = netsec_read(priv, NETSEC_REG_F_TAIKI_VER); 2068 /* this driver only supports F_TAIKI style NETSEC */ 2069 if (NETSEC_F_NETSEC_VER_MAJOR_NUM(hw_ver) != 2070 NETSEC_F_NETSEC_VER_MAJOR_NUM(NETSEC_REG_NETSEC_VER_F_TAIKI)) { 2071 ret = -ENODEV; 2072 goto pm_disable; 2073 } 2074 2075 dev_info(&pdev->dev, "hardware revision %d.%d\n", 2076 hw_ver >> 16, hw_ver & 0xffff); 2077 2078 netif_napi_add(ndev, &priv->napi, netsec_napi_poll, NAPI_POLL_WEIGHT); 2079 2080 ndev->netdev_ops = &netsec_netdev_ops; 2081 ndev->ethtool_ops = &netsec_ethtool_ops; 2082 2083 ndev->features |= NETIF_F_HIGHDMA | NETIF_F_RXCSUM | NETIF_F_GSO | 2084 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM; 2085 ndev->hw_features = ndev->features; 2086 2087 priv->rx_cksum_offload_flag = true; 2088 2089 ret = netsec_register_mdio(priv, phy_addr); 2090 if (ret) 2091 goto unreg_napi; 2092 2093 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40))) 2094 dev_warn(&pdev->dev, "Failed to set DMA mask\n"); 2095 2096 ret = register_netdev(ndev); 2097 if (ret) { 2098 netif_err(priv, probe, ndev, "register_netdev() failed\n"); 2099 goto unreg_mii; 2100 } 2101 2102 pm_runtime_put_sync(&pdev->dev); 2103 return 0; 2104 2105 unreg_mii: 2106 netsec_unregister_mdio(priv); 2107 unreg_napi: 2108 netif_napi_del(&priv->napi); 2109 pm_disable: 2110 pm_runtime_put_sync(&pdev->dev); 2111 pm_runtime_disable(&pdev->dev); 2112 free_ndev: 2113 free_netdev(ndev); 2114 dev_err(&pdev->dev, "init failed\n"); 2115 2116 return ret; 2117 } 2118 2119 static int netsec_remove(struct platform_device *pdev) 2120 { 2121 struct netsec_priv *priv = platform_get_drvdata(pdev); 2122 2123 unregister_netdev(priv->ndev); 2124 2125 netsec_unregister_mdio(priv); 2126 2127 netif_napi_del(&priv->napi); 2128 2129 pm_runtime_disable(&pdev->dev); 2130 free_netdev(priv->ndev); 2131 2132 return 0; 2133 } 2134 2135 #ifdef CONFIG_PM 2136 static int netsec_runtime_suspend(struct device *dev) 2137 { 2138 struct netsec_priv *priv = dev_get_drvdata(dev); 2139 2140 netsec_write(priv, NETSEC_REG_CLK_EN, 0); 2141 2142 clk_disable_unprepare(priv->clk); 2143 2144 return 0; 2145 } 2146 2147 static int netsec_runtime_resume(struct device *dev) 2148 { 2149 struct netsec_priv *priv = dev_get_drvdata(dev); 2150 2151 clk_prepare_enable(priv->clk); 2152 2153 netsec_write(priv, NETSEC_REG_CLK_EN, NETSEC_CLK_EN_REG_DOM_D | 2154 NETSEC_CLK_EN_REG_DOM_C | 2155 NETSEC_CLK_EN_REG_DOM_G); 2156 return 0; 2157 } 2158 #endif 2159 2160 static const struct dev_pm_ops netsec_pm_ops = { 2161 SET_RUNTIME_PM_OPS(netsec_runtime_suspend, netsec_runtime_resume, NULL) 2162 }; 2163 2164 static const struct of_device_id netsec_dt_ids[] = { 2165 { .compatible = "socionext,synquacer-netsec" }, 2166 { } 2167 }; 2168 MODULE_DEVICE_TABLE(of, netsec_dt_ids); 2169 2170 #ifdef CONFIG_ACPI 2171 static const struct acpi_device_id netsec_acpi_ids[] = { 2172 { "SCX0001" }, 2173 { } 2174 }; 2175 MODULE_DEVICE_TABLE(acpi, netsec_acpi_ids); 2176 #endif 2177 2178 static struct platform_driver netsec_driver = { 2179 .probe = netsec_probe, 2180 .remove = netsec_remove, 2181 .driver = { 2182 .name = "netsec", 2183 .pm = &netsec_pm_ops, 2184 .of_match_table = netsec_dt_ids, 2185 .acpi_match_table = ACPI_PTR(netsec_acpi_ids), 2186 }, 2187 }; 2188 module_platform_driver(netsec_driver); 2189 2190 MODULE_AUTHOR("Jassi Brar <jaswinder.singh@linaro.org>"); 2191 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); 2192 MODULE_DESCRIPTION("NETSEC Ethernet driver"); 2193 MODULE_LICENSE("GPL"); 2194