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 .supported_coalesce_params = ETHTOOL_COALESCE_USECS | 593 ETHTOOL_COALESCE_MAX_FRAMES, 594 .get_drvinfo = netsec_et_get_drvinfo, 595 .get_link_ksettings = phy_ethtool_get_link_ksettings, 596 .set_link_ksettings = phy_ethtool_set_link_ksettings, 597 .get_link = ethtool_op_get_link, 598 .get_coalesce = netsec_et_get_coalesce, 599 .set_coalesce = netsec_et_set_coalesce, 600 .get_msglevel = netsec_et_get_msglevel, 601 .set_msglevel = netsec_et_set_msglevel, 602 }; 603 604 /************* NETDEV_OPS FOLLOW *************/ 605 606 607 static void netsec_set_rx_de(struct netsec_priv *priv, 608 struct netsec_desc_ring *dring, u16 idx, 609 const struct netsec_desc *desc) 610 { 611 struct netsec_de *de = dring->vaddr + DESC_SZ * idx; 612 u32 attr = (1 << NETSEC_RX_PKT_OWN_FIELD) | 613 (1 << NETSEC_RX_PKT_FS_FIELD) | 614 (1 << NETSEC_RX_PKT_LS_FIELD); 615 616 if (idx == DESC_NUM - 1) 617 attr |= (1 << NETSEC_RX_PKT_LD_FIELD); 618 619 de->data_buf_addr_up = upper_32_bits(desc->dma_addr); 620 de->data_buf_addr_lw = lower_32_bits(desc->dma_addr); 621 de->buf_len_info = desc->len; 622 de->attr = attr; 623 dma_wmb(); 624 625 dring->desc[idx].dma_addr = desc->dma_addr; 626 dring->desc[idx].addr = desc->addr; 627 dring->desc[idx].len = desc->len; 628 } 629 630 static bool netsec_clean_tx_dring(struct netsec_priv *priv) 631 { 632 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_TX]; 633 struct netsec_de *entry; 634 int tail = dring->tail; 635 unsigned int bytes; 636 int cnt = 0; 637 638 spin_lock(&dring->lock); 639 640 bytes = 0; 641 entry = dring->vaddr + DESC_SZ * tail; 642 643 while (!(entry->attr & (1U << NETSEC_TX_SHIFT_OWN_FIELD)) && 644 cnt < DESC_NUM) { 645 struct netsec_desc *desc; 646 int eop; 647 648 desc = &dring->desc[tail]; 649 eop = (entry->attr >> NETSEC_TX_LAST) & 1; 650 dma_rmb(); 651 652 /* if buf_type is either TYPE_NETSEC_SKB or 653 * TYPE_NETSEC_XDP_NDO we mapped it 654 */ 655 if (desc->buf_type != TYPE_NETSEC_XDP_TX) 656 dma_unmap_single(priv->dev, desc->dma_addr, desc->len, 657 DMA_TO_DEVICE); 658 659 if (!eop) 660 goto next; 661 662 if (desc->buf_type == TYPE_NETSEC_SKB) { 663 bytes += desc->skb->len; 664 dev_kfree_skb(desc->skb); 665 } else { 666 bytes += desc->xdpf->len; 667 xdp_return_frame(desc->xdpf); 668 } 669 next: 670 /* clean up so netsec_uninit_pkt_dring() won't free the skb 671 * again 672 */ 673 *desc = (struct netsec_desc){}; 674 675 /* entry->attr is not going to be accessed by the NIC until 676 * netsec_set_tx_de() is called. No need for a dma_wmb() here 677 */ 678 entry->attr = 1U << NETSEC_TX_SHIFT_OWN_FIELD; 679 /* move tail ahead */ 680 dring->tail = (tail + 1) % DESC_NUM; 681 682 tail = dring->tail; 683 entry = dring->vaddr + DESC_SZ * tail; 684 cnt++; 685 } 686 687 spin_unlock(&dring->lock); 688 689 if (!cnt) 690 return false; 691 692 /* reading the register clears the irq */ 693 netsec_read(priv, NETSEC_REG_NRM_TX_DONE_PKTCNT); 694 695 priv->ndev->stats.tx_packets += cnt; 696 priv->ndev->stats.tx_bytes += bytes; 697 698 netdev_completed_queue(priv->ndev, cnt, bytes); 699 700 return true; 701 } 702 703 static void netsec_process_tx(struct netsec_priv *priv) 704 { 705 struct net_device *ndev = priv->ndev; 706 bool cleaned; 707 708 cleaned = netsec_clean_tx_dring(priv); 709 710 if (cleaned && netif_queue_stopped(ndev)) { 711 /* Make sure we update the value, anyone stopping the queue 712 * after this will read the proper consumer idx 713 */ 714 smp_wmb(); 715 netif_wake_queue(ndev); 716 } 717 } 718 719 static void *netsec_alloc_rx_data(struct netsec_priv *priv, 720 dma_addr_t *dma_handle, u16 *desc_len) 721 722 { 723 724 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX]; 725 struct page *page; 726 727 page = page_pool_dev_alloc_pages(dring->page_pool); 728 if (!page) 729 return NULL; 730 731 /* We allocate the same buffer length for XDP and non-XDP cases. 732 * page_pool API will map the whole page, skip what's needed for 733 * network payloads and/or XDP 734 */ 735 *dma_handle = page_pool_get_dma_addr(page) + NETSEC_RXBUF_HEADROOM; 736 /* Make sure the incoming payload fits in the page for XDP and non-XDP 737 * cases and reserve enough space for headroom + skb_shared_info 738 */ 739 *desc_len = NETSEC_RX_BUF_SIZE; 740 741 return page_address(page); 742 } 743 744 static void netsec_rx_fill(struct netsec_priv *priv, u16 from, u16 num) 745 { 746 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX]; 747 u16 idx = from; 748 749 while (num) { 750 netsec_set_rx_de(priv, dring, idx, &dring->desc[idx]); 751 idx++; 752 if (idx >= DESC_NUM) 753 idx = 0; 754 num--; 755 } 756 } 757 758 static void netsec_xdp_ring_tx_db(struct netsec_priv *priv, u16 pkts) 759 { 760 if (likely(pkts)) 761 netsec_write(priv, NETSEC_REG_NRM_TX_PKTCNT, pkts); 762 } 763 764 static void netsec_finalize_xdp_rx(struct netsec_priv *priv, u32 xdp_res, 765 u16 pkts) 766 { 767 if (xdp_res & NETSEC_XDP_REDIR) 768 xdp_do_flush_map(); 769 770 if (xdp_res & NETSEC_XDP_TX) 771 netsec_xdp_ring_tx_db(priv, pkts); 772 } 773 774 static void netsec_set_tx_de(struct netsec_priv *priv, 775 struct netsec_desc_ring *dring, 776 const struct netsec_tx_pkt_ctrl *tx_ctrl, 777 const struct netsec_desc *desc, void *buf) 778 { 779 int idx = dring->head; 780 struct netsec_de *de; 781 u32 attr; 782 783 de = dring->vaddr + (DESC_SZ * idx); 784 785 attr = (1 << NETSEC_TX_SHIFT_OWN_FIELD) | 786 (1 << NETSEC_TX_SHIFT_PT_FIELD) | 787 (NETSEC_RING_GMAC << NETSEC_TX_SHIFT_TDRID_FIELD) | 788 (1 << NETSEC_TX_SHIFT_FS_FIELD) | 789 (1 << NETSEC_TX_LAST) | 790 (tx_ctrl->cksum_offload_flag << NETSEC_TX_SHIFT_CO) | 791 (tx_ctrl->tcp_seg_offload_flag << NETSEC_TX_SHIFT_SO) | 792 (1 << NETSEC_TX_SHIFT_TRS_FIELD); 793 if (idx == DESC_NUM - 1) 794 attr |= (1 << NETSEC_TX_SHIFT_LD_FIELD); 795 796 de->data_buf_addr_up = upper_32_bits(desc->dma_addr); 797 de->data_buf_addr_lw = lower_32_bits(desc->dma_addr); 798 de->buf_len_info = (tx_ctrl->tcp_seg_len << 16) | desc->len; 799 de->attr = attr; 800 801 dring->desc[idx] = *desc; 802 if (desc->buf_type == TYPE_NETSEC_SKB) 803 dring->desc[idx].skb = buf; 804 else if (desc->buf_type == TYPE_NETSEC_XDP_TX || 805 desc->buf_type == TYPE_NETSEC_XDP_NDO) 806 dring->desc[idx].xdpf = buf; 807 808 /* move head ahead */ 809 dring->head = (dring->head + 1) % DESC_NUM; 810 } 811 812 /* The current driver only supports 1 Txq, this should run under spin_lock() */ 813 static u32 netsec_xdp_queue_one(struct netsec_priv *priv, 814 struct xdp_frame *xdpf, bool is_ndo) 815 816 { 817 struct netsec_desc_ring *tx_ring = &priv->desc_ring[NETSEC_RING_TX]; 818 struct page *page = virt_to_page(xdpf->data); 819 struct netsec_tx_pkt_ctrl tx_ctrl = {}; 820 struct netsec_desc tx_desc; 821 dma_addr_t dma_handle; 822 u16 filled; 823 824 if (tx_ring->head >= tx_ring->tail) 825 filled = tx_ring->head - tx_ring->tail; 826 else 827 filled = tx_ring->head + DESC_NUM - tx_ring->tail; 828 829 if (DESC_NUM - filled <= 1) 830 return NETSEC_XDP_CONSUMED; 831 832 if (is_ndo) { 833 /* this is for ndo_xdp_xmit, the buffer needs mapping before 834 * sending 835 */ 836 dma_handle = dma_map_single(priv->dev, xdpf->data, xdpf->len, 837 DMA_TO_DEVICE); 838 if (dma_mapping_error(priv->dev, dma_handle)) 839 return NETSEC_XDP_CONSUMED; 840 tx_desc.buf_type = TYPE_NETSEC_XDP_NDO; 841 } else { 842 /* This is the device Rx buffer from page_pool. No need to remap 843 * just sync and send it 844 */ 845 struct netsec_desc_ring *rx_ring = 846 &priv->desc_ring[NETSEC_RING_RX]; 847 enum dma_data_direction dma_dir = 848 page_pool_get_dma_dir(rx_ring->page_pool); 849 850 dma_handle = page_pool_get_dma_addr(page) + xdpf->headroom + 851 sizeof(*xdpf); 852 dma_sync_single_for_device(priv->dev, dma_handle, xdpf->len, 853 dma_dir); 854 tx_desc.buf_type = TYPE_NETSEC_XDP_TX; 855 } 856 857 tx_desc.dma_addr = dma_handle; 858 tx_desc.addr = xdpf->data; 859 tx_desc.len = xdpf->len; 860 861 netdev_sent_queue(priv->ndev, xdpf->len); 862 netsec_set_tx_de(priv, tx_ring, &tx_ctrl, &tx_desc, xdpf); 863 864 return NETSEC_XDP_TX; 865 } 866 867 static u32 netsec_xdp_xmit_back(struct netsec_priv *priv, struct xdp_buff *xdp) 868 { 869 struct netsec_desc_ring *tx_ring = &priv->desc_ring[NETSEC_RING_TX]; 870 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp); 871 u32 ret; 872 873 if (unlikely(!xdpf)) 874 return NETSEC_XDP_CONSUMED; 875 876 spin_lock(&tx_ring->lock); 877 ret = netsec_xdp_queue_one(priv, xdpf, false); 878 spin_unlock(&tx_ring->lock); 879 880 return ret; 881 } 882 883 static u32 netsec_run_xdp(struct netsec_priv *priv, struct bpf_prog *prog, 884 struct xdp_buff *xdp) 885 { 886 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX]; 887 unsigned int sync, len = xdp->data_end - xdp->data; 888 u32 ret = NETSEC_XDP_PASS; 889 struct page *page; 890 int err; 891 u32 act; 892 893 act = bpf_prog_run_xdp(prog, xdp); 894 895 /* Due xdp_adjust_tail: DMA sync for_device cover max len CPU touch */ 896 sync = xdp->data_end - xdp->data_hard_start - NETSEC_RXBUF_HEADROOM; 897 sync = max(sync, len); 898 899 switch (act) { 900 case XDP_PASS: 901 ret = NETSEC_XDP_PASS; 902 break; 903 case XDP_TX: 904 ret = netsec_xdp_xmit_back(priv, xdp); 905 if (ret != NETSEC_XDP_TX) { 906 page = virt_to_head_page(xdp->data); 907 page_pool_put_page(dring->page_pool, page, sync, true); 908 } 909 break; 910 case XDP_REDIRECT: 911 err = xdp_do_redirect(priv->ndev, xdp, prog); 912 if (!err) { 913 ret = NETSEC_XDP_REDIR; 914 } else { 915 ret = NETSEC_XDP_CONSUMED; 916 page = virt_to_head_page(xdp->data); 917 page_pool_put_page(dring->page_pool, page, sync, true); 918 } 919 break; 920 default: 921 bpf_warn_invalid_xdp_action(act); 922 /* fall through */ 923 case XDP_ABORTED: 924 trace_xdp_exception(priv->ndev, prog, act); 925 /* fall through -- handle aborts by dropping packet */ 926 case XDP_DROP: 927 ret = NETSEC_XDP_CONSUMED; 928 page = virt_to_head_page(xdp->data); 929 page_pool_put_page(dring->page_pool, page, sync, true); 930 break; 931 } 932 933 return ret; 934 } 935 936 static int netsec_process_rx(struct netsec_priv *priv, int budget) 937 { 938 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX]; 939 struct net_device *ndev = priv->ndev; 940 struct netsec_rx_pkt_info rx_info; 941 enum dma_data_direction dma_dir; 942 struct bpf_prog *xdp_prog; 943 struct xdp_buff xdp; 944 u16 xdp_xmit = 0; 945 u32 xdp_act = 0; 946 int done = 0; 947 948 xdp.rxq = &dring->xdp_rxq; 949 xdp.frame_sz = PAGE_SIZE; 950 951 rcu_read_lock(); 952 xdp_prog = READ_ONCE(priv->xdp_prog); 953 dma_dir = page_pool_get_dma_dir(dring->page_pool); 954 955 while (done < budget) { 956 u16 idx = dring->tail; 957 struct netsec_de *de = dring->vaddr + (DESC_SZ * idx); 958 struct netsec_desc *desc = &dring->desc[idx]; 959 struct page *page = virt_to_page(desc->addr); 960 u32 xdp_result = NETSEC_XDP_PASS; 961 struct sk_buff *skb = NULL; 962 u16 pkt_len, desc_len; 963 dma_addr_t dma_handle; 964 void *buf_addr; 965 966 if (de->attr & (1U << NETSEC_RX_PKT_OWN_FIELD)) { 967 /* reading the register clears the irq */ 968 netsec_read(priv, NETSEC_REG_NRM_RX_PKTCNT); 969 break; 970 } 971 972 /* This barrier is needed to keep us from reading 973 * any other fields out of the netsec_de until we have 974 * verified the descriptor has been written back 975 */ 976 dma_rmb(); 977 done++; 978 979 pkt_len = de->buf_len_info >> 16; 980 rx_info.err_code = (de->attr >> NETSEC_RX_PKT_ERR_FIELD) & 981 NETSEC_RX_PKT_ERR_MASK; 982 rx_info.err_flag = (de->attr >> NETSEC_RX_PKT_ER_FIELD) & 1; 983 if (rx_info.err_flag) { 984 netif_err(priv, drv, priv->ndev, 985 "%s: rx fail err(%d)\n", __func__, 986 rx_info.err_code); 987 ndev->stats.rx_dropped++; 988 dring->tail = (dring->tail + 1) % DESC_NUM; 989 /* reuse buffer page frag */ 990 netsec_rx_fill(priv, idx, 1); 991 continue; 992 } 993 rx_info.rx_cksum_result = 994 (de->attr >> NETSEC_RX_PKT_CO_FIELD) & 3; 995 996 /* allocate a fresh buffer and map it to the hardware. 997 * This will eventually replace the old buffer in the hardware 998 */ 999 buf_addr = netsec_alloc_rx_data(priv, &dma_handle, &desc_len); 1000 1001 if (unlikely(!buf_addr)) 1002 break; 1003 1004 dma_sync_single_for_cpu(priv->dev, desc->dma_addr, pkt_len, 1005 dma_dir); 1006 prefetch(desc->addr); 1007 1008 xdp.data_hard_start = desc->addr; 1009 xdp.data = desc->addr + NETSEC_RXBUF_HEADROOM; 1010 xdp_set_data_meta_invalid(&xdp); 1011 xdp.data_end = xdp.data + pkt_len; 1012 1013 if (xdp_prog) { 1014 xdp_result = netsec_run_xdp(priv, xdp_prog, &xdp); 1015 if (xdp_result != NETSEC_XDP_PASS) { 1016 xdp_act |= xdp_result; 1017 if (xdp_result == NETSEC_XDP_TX) 1018 xdp_xmit++; 1019 goto next; 1020 } 1021 } 1022 skb = build_skb(desc->addr, desc->len + NETSEC_RX_BUF_NON_DATA); 1023 1024 if (unlikely(!skb)) { 1025 /* If skb fails recycle_direct will either unmap and 1026 * free the page or refill the cache depending on the 1027 * cache state. Since we paid the allocation cost if 1028 * building an skb fails try to put the page into cache 1029 */ 1030 page_pool_put_page(dring->page_pool, page, pkt_len, 1031 true); 1032 netif_err(priv, drv, priv->ndev, 1033 "rx failed to build skb\n"); 1034 break; 1035 } 1036 page_pool_release_page(dring->page_pool, page); 1037 1038 skb_reserve(skb, xdp.data - xdp.data_hard_start); 1039 skb_put(skb, xdp.data_end - xdp.data); 1040 skb->protocol = eth_type_trans(skb, priv->ndev); 1041 1042 if (priv->rx_cksum_offload_flag && 1043 rx_info.rx_cksum_result == NETSEC_RX_CKSUM_OK) 1044 skb->ip_summed = CHECKSUM_UNNECESSARY; 1045 1046 next: 1047 if ((skb && napi_gro_receive(&priv->napi, skb) != GRO_DROP) || 1048 xdp_result) { 1049 ndev->stats.rx_packets++; 1050 ndev->stats.rx_bytes += xdp.data_end - xdp.data; 1051 } 1052 1053 /* Update the descriptor with fresh buffers */ 1054 desc->len = desc_len; 1055 desc->dma_addr = dma_handle; 1056 desc->addr = buf_addr; 1057 1058 netsec_rx_fill(priv, idx, 1); 1059 dring->tail = (dring->tail + 1) % DESC_NUM; 1060 } 1061 netsec_finalize_xdp_rx(priv, xdp_act, xdp_xmit); 1062 1063 rcu_read_unlock(); 1064 1065 return done; 1066 } 1067 1068 static int netsec_napi_poll(struct napi_struct *napi, int budget) 1069 { 1070 struct netsec_priv *priv; 1071 int done; 1072 1073 priv = container_of(napi, struct netsec_priv, napi); 1074 1075 netsec_process_tx(priv); 1076 done = netsec_process_rx(priv, budget); 1077 1078 if (done < budget && napi_complete_done(napi, done)) { 1079 unsigned long flags; 1080 1081 spin_lock_irqsave(&priv->reglock, flags); 1082 netsec_write(priv, NETSEC_REG_INTEN_SET, 1083 NETSEC_IRQ_RX | NETSEC_IRQ_TX); 1084 spin_unlock_irqrestore(&priv->reglock, flags); 1085 } 1086 1087 return done; 1088 } 1089 1090 1091 static int netsec_desc_used(struct netsec_desc_ring *dring) 1092 { 1093 int used; 1094 1095 if (dring->head >= dring->tail) 1096 used = dring->head - dring->tail; 1097 else 1098 used = dring->head + DESC_NUM - dring->tail; 1099 1100 return used; 1101 } 1102 1103 static int netsec_check_stop_tx(struct netsec_priv *priv, int used) 1104 { 1105 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_TX]; 1106 1107 /* keep tail from touching the queue */ 1108 if (DESC_NUM - used < 2) { 1109 netif_stop_queue(priv->ndev); 1110 1111 /* Make sure we read the updated value in case 1112 * descriptors got freed 1113 */ 1114 smp_rmb(); 1115 1116 used = netsec_desc_used(dring); 1117 if (DESC_NUM - used < 2) 1118 return NETDEV_TX_BUSY; 1119 1120 netif_wake_queue(priv->ndev); 1121 } 1122 1123 return 0; 1124 } 1125 1126 static netdev_tx_t netsec_netdev_start_xmit(struct sk_buff *skb, 1127 struct net_device *ndev) 1128 { 1129 struct netsec_priv *priv = netdev_priv(ndev); 1130 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_TX]; 1131 struct netsec_tx_pkt_ctrl tx_ctrl = {}; 1132 struct netsec_desc tx_desc; 1133 u16 tso_seg_len = 0; 1134 int filled; 1135 1136 spin_lock_bh(&dring->lock); 1137 filled = netsec_desc_used(dring); 1138 if (netsec_check_stop_tx(priv, filled)) { 1139 spin_unlock_bh(&dring->lock); 1140 net_warn_ratelimited("%s %s Tx queue full\n", 1141 dev_name(priv->dev), ndev->name); 1142 return NETDEV_TX_BUSY; 1143 } 1144 1145 if (skb->ip_summed == CHECKSUM_PARTIAL) 1146 tx_ctrl.cksum_offload_flag = true; 1147 1148 if (skb_is_gso(skb)) 1149 tso_seg_len = skb_shinfo(skb)->gso_size; 1150 1151 if (tso_seg_len > 0) { 1152 if (skb->protocol == htons(ETH_P_IP)) { 1153 ip_hdr(skb)->tot_len = 0; 1154 tcp_hdr(skb)->check = 1155 ~tcp_v4_check(0, ip_hdr(skb)->saddr, 1156 ip_hdr(skb)->daddr, 0); 1157 } else { 1158 tcp_v6_gso_csum_prep(skb); 1159 } 1160 1161 tx_ctrl.tcp_seg_offload_flag = true; 1162 tx_ctrl.tcp_seg_len = tso_seg_len; 1163 } 1164 1165 tx_desc.dma_addr = dma_map_single(priv->dev, skb->data, 1166 skb_headlen(skb), DMA_TO_DEVICE); 1167 if (dma_mapping_error(priv->dev, tx_desc.dma_addr)) { 1168 spin_unlock_bh(&dring->lock); 1169 netif_err(priv, drv, priv->ndev, 1170 "%s: DMA mapping failed\n", __func__); 1171 ndev->stats.tx_dropped++; 1172 dev_kfree_skb_any(skb); 1173 return NETDEV_TX_OK; 1174 } 1175 tx_desc.addr = skb->data; 1176 tx_desc.len = skb_headlen(skb); 1177 tx_desc.buf_type = TYPE_NETSEC_SKB; 1178 1179 skb_tx_timestamp(skb); 1180 netdev_sent_queue(priv->ndev, skb->len); 1181 1182 netsec_set_tx_de(priv, dring, &tx_ctrl, &tx_desc, skb); 1183 spin_unlock_bh(&dring->lock); 1184 netsec_write(priv, NETSEC_REG_NRM_TX_PKTCNT, 1); /* submit another tx */ 1185 1186 return NETDEV_TX_OK; 1187 } 1188 1189 static void netsec_uninit_pkt_dring(struct netsec_priv *priv, int id) 1190 { 1191 struct netsec_desc_ring *dring = &priv->desc_ring[id]; 1192 struct netsec_desc *desc; 1193 u16 idx; 1194 1195 if (!dring->vaddr || !dring->desc) 1196 return; 1197 for (idx = 0; idx < DESC_NUM; idx++) { 1198 desc = &dring->desc[idx]; 1199 if (!desc->addr) 1200 continue; 1201 1202 if (id == NETSEC_RING_RX) { 1203 struct page *page = virt_to_page(desc->addr); 1204 1205 page_pool_put_full_page(dring->page_pool, page, false); 1206 } else if (id == NETSEC_RING_TX) { 1207 dma_unmap_single(priv->dev, desc->dma_addr, desc->len, 1208 DMA_TO_DEVICE); 1209 dev_kfree_skb(desc->skb); 1210 } 1211 } 1212 1213 /* Rx is currently using page_pool */ 1214 if (id == NETSEC_RING_RX) { 1215 if (xdp_rxq_info_is_reg(&dring->xdp_rxq)) 1216 xdp_rxq_info_unreg(&dring->xdp_rxq); 1217 page_pool_destroy(dring->page_pool); 1218 } 1219 1220 memset(dring->desc, 0, sizeof(struct netsec_desc) * DESC_NUM); 1221 memset(dring->vaddr, 0, DESC_SZ * DESC_NUM); 1222 1223 dring->head = 0; 1224 dring->tail = 0; 1225 1226 if (id == NETSEC_RING_TX) 1227 netdev_reset_queue(priv->ndev); 1228 } 1229 1230 static void netsec_free_dring(struct netsec_priv *priv, int id) 1231 { 1232 struct netsec_desc_ring *dring = &priv->desc_ring[id]; 1233 1234 if (dring->vaddr) { 1235 dma_free_coherent(priv->dev, DESC_SZ * DESC_NUM, 1236 dring->vaddr, dring->desc_dma); 1237 dring->vaddr = NULL; 1238 } 1239 1240 kfree(dring->desc); 1241 dring->desc = NULL; 1242 } 1243 1244 static int netsec_alloc_dring(struct netsec_priv *priv, enum ring_id id) 1245 { 1246 struct netsec_desc_ring *dring = &priv->desc_ring[id]; 1247 1248 dring->vaddr = dma_alloc_coherent(priv->dev, DESC_SZ * DESC_NUM, 1249 &dring->desc_dma, GFP_KERNEL); 1250 if (!dring->vaddr) 1251 goto err; 1252 1253 dring->desc = kcalloc(DESC_NUM, sizeof(*dring->desc), GFP_KERNEL); 1254 if (!dring->desc) 1255 goto err; 1256 1257 return 0; 1258 err: 1259 netsec_free_dring(priv, id); 1260 1261 return -ENOMEM; 1262 } 1263 1264 static void netsec_setup_tx_dring(struct netsec_priv *priv) 1265 { 1266 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_TX]; 1267 int i; 1268 1269 for (i = 0; i < DESC_NUM; i++) { 1270 struct netsec_de *de; 1271 1272 de = dring->vaddr + (DESC_SZ * i); 1273 /* de->attr is not going to be accessed by the NIC 1274 * until netsec_set_tx_de() is called. 1275 * No need for a dma_wmb() here 1276 */ 1277 de->attr = 1U << NETSEC_TX_SHIFT_OWN_FIELD; 1278 } 1279 } 1280 1281 static int netsec_setup_rx_dring(struct netsec_priv *priv) 1282 { 1283 struct netsec_desc_ring *dring = &priv->desc_ring[NETSEC_RING_RX]; 1284 struct bpf_prog *xdp_prog = READ_ONCE(priv->xdp_prog); 1285 struct page_pool_params pp_params = { 1286 .order = 0, 1287 /* internal DMA mapping in page_pool */ 1288 .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV, 1289 .pool_size = DESC_NUM, 1290 .nid = NUMA_NO_NODE, 1291 .dev = priv->dev, 1292 .dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE, 1293 .offset = NETSEC_RXBUF_HEADROOM, 1294 .max_len = NETSEC_RX_BUF_SIZE, 1295 }; 1296 int i, err; 1297 1298 dring->page_pool = page_pool_create(&pp_params); 1299 if (IS_ERR(dring->page_pool)) { 1300 err = PTR_ERR(dring->page_pool); 1301 dring->page_pool = NULL; 1302 goto err_out; 1303 } 1304 1305 err = xdp_rxq_info_reg(&dring->xdp_rxq, priv->ndev, 0); 1306 if (err) 1307 goto err_out; 1308 1309 err = xdp_rxq_info_reg_mem_model(&dring->xdp_rxq, MEM_TYPE_PAGE_POOL, 1310 dring->page_pool); 1311 if (err) 1312 goto err_out; 1313 1314 for (i = 0; i < DESC_NUM; i++) { 1315 struct netsec_desc *desc = &dring->desc[i]; 1316 dma_addr_t dma_handle; 1317 void *buf; 1318 u16 len; 1319 1320 buf = netsec_alloc_rx_data(priv, &dma_handle, &len); 1321 1322 if (!buf) { 1323 err = -ENOMEM; 1324 goto err_out; 1325 } 1326 desc->dma_addr = dma_handle; 1327 desc->addr = buf; 1328 desc->len = len; 1329 } 1330 1331 netsec_rx_fill(priv, 0, DESC_NUM); 1332 1333 return 0; 1334 1335 err_out: 1336 netsec_uninit_pkt_dring(priv, NETSEC_RING_RX); 1337 return err; 1338 } 1339 1340 static int netsec_netdev_load_ucode_region(struct netsec_priv *priv, u32 reg, 1341 u32 addr_h, u32 addr_l, u32 size) 1342 { 1343 u64 base = (u64)addr_h << 32 | addr_l; 1344 void __iomem *ucode; 1345 u32 i; 1346 1347 ucode = ioremap(base, size * sizeof(u32)); 1348 if (!ucode) 1349 return -ENOMEM; 1350 1351 for (i = 0; i < size; i++) 1352 netsec_write(priv, reg, readl(ucode + i * 4)); 1353 1354 iounmap(ucode); 1355 return 0; 1356 } 1357 1358 static int netsec_netdev_load_microcode(struct netsec_priv *priv) 1359 { 1360 u32 addr_h, addr_l, size; 1361 int err; 1362 1363 addr_h = readl(priv->eeprom_base + NETSEC_EEPROM_HM_ME_ADDRESS_H); 1364 addr_l = readl(priv->eeprom_base + NETSEC_EEPROM_HM_ME_ADDRESS_L); 1365 size = readl(priv->eeprom_base + NETSEC_EEPROM_HM_ME_SIZE); 1366 err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_DMAC_HM_CMD_BUF, 1367 addr_h, addr_l, size); 1368 if (err) 1369 return err; 1370 1371 addr_h = readl(priv->eeprom_base + NETSEC_EEPROM_MH_ME_ADDRESS_H); 1372 addr_l = readl(priv->eeprom_base + NETSEC_EEPROM_MH_ME_ADDRESS_L); 1373 size = readl(priv->eeprom_base + NETSEC_EEPROM_MH_ME_SIZE); 1374 err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_DMAC_MH_CMD_BUF, 1375 addr_h, addr_l, size); 1376 if (err) 1377 return err; 1378 1379 addr_h = 0; 1380 addr_l = readl(priv->eeprom_base + NETSEC_EEPROM_PKT_ME_ADDRESS); 1381 size = readl(priv->eeprom_base + NETSEC_EEPROM_PKT_ME_SIZE); 1382 err = netsec_netdev_load_ucode_region(priv, NETSEC_REG_PKT_CMD_BUF, 1383 addr_h, addr_l, size); 1384 if (err) 1385 return err; 1386 1387 return 0; 1388 } 1389 1390 static int netsec_reset_hardware(struct netsec_priv *priv, 1391 bool load_ucode) 1392 { 1393 u32 value; 1394 int err; 1395 1396 /* stop DMA engines */ 1397 if (!netsec_read(priv, NETSEC_REG_ADDR_DIS_CORE)) { 1398 netsec_write(priv, NETSEC_REG_DMA_HM_CTRL, 1399 NETSEC_DMA_CTRL_REG_STOP); 1400 netsec_write(priv, NETSEC_REG_DMA_MH_CTRL, 1401 NETSEC_DMA_CTRL_REG_STOP); 1402 1403 while (netsec_read(priv, NETSEC_REG_DMA_HM_CTRL) & 1404 NETSEC_DMA_CTRL_REG_STOP) 1405 cpu_relax(); 1406 1407 while (netsec_read(priv, NETSEC_REG_DMA_MH_CTRL) & 1408 NETSEC_DMA_CTRL_REG_STOP) 1409 cpu_relax(); 1410 } 1411 1412 netsec_write(priv, NETSEC_REG_SOFT_RST, NETSEC_SOFT_RST_REG_RESET); 1413 netsec_write(priv, NETSEC_REG_SOFT_RST, NETSEC_SOFT_RST_REG_RUN); 1414 netsec_write(priv, NETSEC_REG_COM_INIT, NETSEC_COM_INIT_REG_ALL); 1415 1416 while (netsec_read(priv, NETSEC_REG_COM_INIT) != 0) 1417 cpu_relax(); 1418 1419 /* set desc_start addr */ 1420 netsec_write(priv, NETSEC_REG_NRM_RX_DESC_START_UP, 1421 upper_32_bits(priv->desc_ring[NETSEC_RING_RX].desc_dma)); 1422 netsec_write(priv, NETSEC_REG_NRM_RX_DESC_START_LW, 1423 lower_32_bits(priv->desc_ring[NETSEC_RING_RX].desc_dma)); 1424 1425 netsec_write(priv, NETSEC_REG_NRM_TX_DESC_START_UP, 1426 upper_32_bits(priv->desc_ring[NETSEC_RING_TX].desc_dma)); 1427 netsec_write(priv, NETSEC_REG_NRM_TX_DESC_START_LW, 1428 lower_32_bits(priv->desc_ring[NETSEC_RING_TX].desc_dma)); 1429 1430 /* set normal tx dring ring config */ 1431 netsec_write(priv, NETSEC_REG_NRM_TX_CONFIG, 1432 1 << NETSEC_REG_DESC_ENDIAN); 1433 netsec_write(priv, NETSEC_REG_NRM_RX_CONFIG, 1434 1 << NETSEC_REG_DESC_ENDIAN); 1435 1436 if (load_ucode) { 1437 err = netsec_netdev_load_microcode(priv); 1438 if (err) { 1439 netif_err(priv, probe, priv->ndev, 1440 "%s: failed to load microcode (%d)\n", 1441 __func__, err); 1442 return err; 1443 } 1444 } 1445 1446 /* start DMA engines */ 1447 netsec_write(priv, NETSEC_REG_DMA_TMR_CTRL, priv->freq / 1000000 - 1); 1448 netsec_write(priv, NETSEC_REG_ADDR_DIS_CORE, 0); 1449 1450 usleep_range(1000, 2000); 1451 1452 if (!(netsec_read(priv, NETSEC_REG_TOP_STATUS) & 1453 NETSEC_TOP_IRQ_REG_CODE_LOAD_END)) { 1454 netif_err(priv, probe, priv->ndev, 1455 "microengine start failed\n"); 1456 return -ENXIO; 1457 } 1458 netsec_write(priv, NETSEC_REG_TOP_STATUS, 1459 NETSEC_TOP_IRQ_REG_CODE_LOAD_END); 1460 1461 value = NETSEC_PKT_CTRL_REG_MODE_NRM; 1462 if (priv->ndev->mtu > ETH_DATA_LEN) 1463 value |= NETSEC_PKT_CTRL_REG_EN_JUMBO; 1464 1465 /* change to normal mode */ 1466 netsec_write(priv, NETSEC_REG_DMA_MH_CTRL, MH_CTRL__MODE_TRANS); 1467 netsec_write(priv, NETSEC_REG_PKT_CTRL, value); 1468 1469 while ((netsec_read(priv, NETSEC_REG_MODE_TRANS_COMP_STATUS) & 1470 NETSEC_MODE_TRANS_COMP_IRQ_T2N) == 0) 1471 cpu_relax(); 1472 1473 /* clear any pending EMPTY/ERR irq status */ 1474 netsec_write(priv, NETSEC_REG_NRM_TX_STATUS, ~0); 1475 1476 /* Disable TX & RX intr */ 1477 netsec_write(priv, NETSEC_REG_INTEN_CLR, ~0); 1478 1479 return 0; 1480 } 1481 1482 static int netsec_start_gmac(struct netsec_priv *priv) 1483 { 1484 struct phy_device *phydev = priv->ndev->phydev; 1485 u32 value = 0; 1486 int ret; 1487 1488 if (phydev->speed != SPEED_1000) 1489 value = (NETSEC_GMAC_MCR_REG_CST | 1490 NETSEC_GMAC_MCR_REG_HALF_DUPLEX_COMMON); 1491 1492 if (netsec_mac_write(priv, GMAC_REG_MCR, value)) 1493 return -ETIMEDOUT; 1494 if (netsec_mac_write(priv, GMAC_REG_BMR, 1495 NETSEC_GMAC_BMR_REG_RESET)) 1496 return -ETIMEDOUT; 1497 1498 /* Wait soft reset */ 1499 usleep_range(1000, 5000); 1500 1501 ret = netsec_mac_read(priv, GMAC_REG_BMR, &value); 1502 if (ret) 1503 return ret; 1504 if (value & NETSEC_GMAC_BMR_REG_SWR) 1505 return -EAGAIN; 1506 1507 netsec_write(priv, MAC_REG_DESC_SOFT_RST, 1); 1508 if (netsec_wait_while_busy(priv, MAC_REG_DESC_SOFT_RST, 1)) 1509 return -ETIMEDOUT; 1510 1511 netsec_write(priv, MAC_REG_DESC_INIT, 1); 1512 if (netsec_wait_while_busy(priv, MAC_REG_DESC_INIT, 1)) 1513 return -ETIMEDOUT; 1514 1515 if (netsec_mac_write(priv, GMAC_REG_BMR, 1516 NETSEC_GMAC_BMR_REG_COMMON)) 1517 return -ETIMEDOUT; 1518 if (netsec_mac_write(priv, GMAC_REG_RDLAR, 1519 NETSEC_GMAC_RDLAR_REG_COMMON)) 1520 return -ETIMEDOUT; 1521 if (netsec_mac_write(priv, GMAC_REG_TDLAR, 1522 NETSEC_GMAC_TDLAR_REG_COMMON)) 1523 return -ETIMEDOUT; 1524 if (netsec_mac_write(priv, GMAC_REG_MFFR, 0x80000001)) 1525 return -ETIMEDOUT; 1526 1527 ret = netsec_mac_update_to_phy_state(priv); 1528 if (ret) 1529 return ret; 1530 1531 ret = netsec_mac_read(priv, GMAC_REG_OMR, &value); 1532 if (ret) 1533 return ret; 1534 1535 value |= NETSEC_GMAC_OMR_REG_SR; 1536 value |= NETSEC_GMAC_OMR_REG_ST; 1537 1538 netsec_write(priv, NETSEC_REG_NRM_RX_INTEN_CLR, ~0); 1539 netsec_write(priv, NETSEC_REG_NRM_TX_INTEN_CLR, ~0); 1540 1541 netsec_et_set_coalesce(priv->ndev, &priv->et_coalesce); 1542 1543 if (netsec_mac_write(priv, GMAC_REG_OMR, value)) 1544 return -ETIMEDOUT; 1545 1546 return 0; 1547 } 1548 1549 static int netsec_stop_gmac(struct netsec_priv *priv) 1550 { 1551 u32 value; 1552 int ret; 1553 1554 ret = netsec_mac_read(priv, GMAC_REG_OMR, &value); 1555 if (ret) 1556 return ret; 1557 value &= ~NETSEC_GMAC_OMR_REG_SR; 1558 value &= ~NETSEC_GMAC_OMR_REG_ST; 1559 1560 /* disable all interrupts */ 1561 netsec_write(priv, NETSEC_REG_NRM_RX_INTEN_CLR, ~0); 1562 netsec_write(priv, NETSEC_REG_NRM_TX_INTEN_CLR, ~0); 1563 1564 return netsec_mac_write(priv, GMAC_REG_OMR, value); 1565 } 1566 1567 static void netsec_phy_adjust_link(struct net_device *ndev) 1568 { 1569 struct netsec_priv *priv = netdev_priv(ndev); 1570 1571 if (ndev->phydev->link) 1572 netsec_start_gmac(priv); 1573 else 1574 netsec_stop_gmac(priv); 1575 1576 phy_print_status(ndev->phydev); 1577 } 1578 1579 static irqreturn_t netsec_irq_handler(int irq, void *dev_id) 1580 { 1581 struct netsec_priv *priv = dev_id; 1582 u32 val, status = netsec_read(priv, NETSEC_REG_TOP_STATUS); 1583 unsigned long flags; 1584 1585 /* Disable interrupts */ 1586 if (status & NETSEC_IRQ_TX) { 1587 val = netsec_read(priv, NETSEC_REG_NRM_TX_STATUS); 1588 netsec_write(priv, NETSEC_REG_NRM_TX_STATUS, val); 1589 } 1590 if (status & NETSEC_IRQ_RX) { 1591 val = netsec_read(priv, NETSEC_REG_NRM_RX_STATUS); 1592 netsec_write(priv, NETSEC_REG_NRM_RX_STATUS, val); 1593 } 1594 1595 spin_lock_irqsave(&priv->reglock, flags); 1596 netsec_write(priv, NETSEC_REG_INTEN_CLR, NETSEC_IRQ_RX | NETSEC_IRQ_TX); 1597 spin_unlock_irqrestore(&priv->reglock, flags); 1598 1599 napi_schedule(&priv->napi); 1600 1601 return IRQ_HANDLED; 1602 } 1603 1604 static int netsec_netdev_open(struct net_device *ndev) 1605 { 1606 struct netsec_priv *priv = netdev_priv(ndev); 1607 int ret; 1608 1609 pm_runtime_get_sync(priv->dev); 1610 1611 netsec_setup_tx_dring(priv); 1612 ret = netsec_setup_rx_dring(priv); 1613 if (ret) { 1614 netif_err(priv, probe, priv->ndev, 1615 "%s: fail setup ring\n", __func__); 1616 goto err1; 1617 } 1618 1619 ret = request_irq(priv->ndev->irq, netsec_irq_handler, 1620 IRQF_SHARED, "netsec", priv); 1621 if (ret) { 1622 netif_err(priv, drv, priv->ndev, "request_irq failed\n"); 1623 goto err2; 1624 } 1625 1626 if (dev_of_node(priv->dev)) { 1627 if (!of_phy_connect(priv->ndev, priv->phy_np, 1628 netsec_phy_adjust_link, 0, 1629 priv->phy_interface)) { 1630 netif_err(priv, link, priv->ndev, "missing PHY\n"); 1631 ret = -ENODEV; 1632 goto err3; 1633 } 1634 } else { 1635 ret = phy_connect_direct(priv->ndev, priv->phydev, 1636 netsec_phy_adjust_link, 1637 priv->phy_interface); 1638 if (ret) { 1639 netif_err(priv, link, priv->ndev, 1640 "phy_connect_direct() failed (%d)\n", ret); 1641 goto err3; 1642 } 1643 } 1644 1645 phy_start(ndev->phydev); 1646 1647 netsec_start_gmac(priv); 1648 napi_enable(&priv->napi); 1649 netif_start_queue(ndev); 1650 1651 /* Enable TX+RX intr. */ 1652 netsec_write(priv, NETSEC_REG_INTEN_SET, NETSEC_IRQ_RX | NETSEC_IRQ_TX); 1653 1654 return 0; 1655 err3: 1656 free_irq(priv->ndev->irq, priv); 1657 err2: 1658 netsec_uninit_pkt_dring(priv, NETSEC_RING_RX); 1659 err1: 1660 pm_runtime_put_sync(priv->dev); 1661 return ret; 1662 } 1663 1664 static int netsec_netdev_stop(struct net_device *ndev) 1665 { 1666 int ret; 1667 struct netsec_priv *priv = netdev_priv(ndev); 1668 1669 netif_stop_queue(priv->ndev); 1670 dma_wmb(); 1671 1672 napi_disable(&priv->napi); 1673 1674 netsec_write(priv, NETSEC_REG_INTEN_CLR, ~0); 1675 netsec_stop_gmac(priv); 1676 1677 free_irq(priv->ndev->irq, priv); 1678 1679 netsec_uninit_pkt_dring(priv, NETSEC_RING_TX); 1680 netsec_uninit_pkt_dring(priv, NETSEC_RING_RX); 1681 1682 phy_stop(ndev->phydev); 1683 phy_disconnect(ndev->phydev); 1684 1685 ret = netsec_reset_hardware(priv, false); 1686 1687 pm_runtime_put_sync(priv->dev); 1688 1689 return ret; 1690 } 1691 1692 static int netsec_netdev_init(struct net_device *ndev) 1693 { 1694 struct netsec_priv *priv = netdev_priv(ndev); 1695 int ret; 1696 u16 data; 1697 1698 BUILD_BUG_ON_NOT_POWER_OF_2(DESC_NUM); 1699 1700 ret = netsec_alloc_dring(priv, NETSEC_RING_TX); 1701 if (ret) 1702 return ret; 1703 1704 ret = netsec_alloc_dring(priv, NETSEC_RING_RX); 1705 if (ret) 1706 goto err1; 1707 1708 /* set phy power down */ 1709 data = netsec_phy_read(priv->mii_bus, priv->phy_addr, MII_BMCR) | 1710 BMCR_PDOWN; 1711 netsec_phy_write(priv->mii_bus, priv->phy_addr, MII_BMCR, data); 1712 1713 ret = netsec_reset_hardware(priv, true); 1714 if (ret) 1715 goto err2; 1716 1717 spin_lock_init(&priv->desc_ring[NETSEC_RING_TX].lock); 1718 spin_lock_init(&priv->desc_ring[NETSEC_RING_RX].lock); 1719 1720 return 0; 1721 err2: 1722 netsec_free_dring(priv, NETSEC_RING_RX); 1723 err1: 1724 netsec_free_dring(priv, NETSEC_RING_TX); 1725 return ret; 1726 } 1727 1728 static void netsec_netdev_uninit(struct net_device *ndev) 1729 { 1730 struct netsec_priv *priv = netdev_priv(ndev); 1731 1732 netsec_free_dring(priv, NETSEC_RING_RX); 1733 netsec_free_dring(priv, NETSEC_RING_TX); 1734 } 1735 1736 static int netsec_netdev_set_features(struct net_device *ndev, 1737 netdev_features_t features) 1738 { 1739 struct netsec_priv *priv = netdev_priv(ndev); 1740 1741 priv->rx_cksum_offload_flag = !!(features & NETIF_F_RXCSUM); 1742 1743 return 0; 1744 } 1745 1746 static int netsec_xdp_xmit(struct net_device *ndev, int n, 1747 struct xdp_frame **frames, u32 flags) 1748 { 1749 struct netsec_priv *priv = netdev_priv(ndev); 1750 struct netsec_desc_ring *tx_ring = &priv->desc_ring[NETSEC_RING_TX]; 1751 int drops = 0; 1752 int i; 1753 1754 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 1755 return -EINVAL; 1756 1757 spin_lock(&tx_ring->lock); 1758 for (i = 0; i < n; i++) { 1759 struct xdp_frame *xdpf = frames[i]; 1760 int err; 1761 1762 err = netsec_xdp_queue_one(priv, xdpf, true); 1763 if (err != NETSEC_XDP_TX) { 1764 xdp_return_frame_rx_napi(xdpf); 1765 drops++; 1766 } else { 1767 tx_ring->xdp_xmit++; 1768 } 1769 } 1770 spin_unlock(&tx_ring->lock); 1771 1772 if (unlikely(flags & XDP_XMIT_FLUSH)) { 1773 netsec_xdp_ring_tx_db(priv, tx_ring->xdp_xmit); 1774 tx_ring->xdp_xmit = 0; 1775 } 1776 1777 return n - drops; 1778 } 1779 1780 static int netsec_xdp_setup(struct netsec_priv *priv, struct bpf_prog *prog, 1781 struct netlink_ext_ack *extack) 1782 { 1783 struct net_device *dev = priv->ndev; 1784 struct bpf_prog *old_prog; 1785 1786 /* For now just support only the usual MTU sized frames */ 1787 if (prog && dev->mtu > 1500) { 1788 NL_SET_ERR_MSG_MOD(extack, "Jumbo frames not supported on XDP"); 1789 return -EOPNOTSUPP; 1790 } 1791 1792 if (netif_running(dev)) 1793 netsec_netdev_stop(dev); 1794 1795 /* Detach old prog, if any */ 1796 old_prog = xchg(&priv->xdp_prog, prog); 1797 if (old_prog) 1798 bpf_prog_put(old_prog); 1799 1800 if (netif_running(dev)) 1801 netsec_netdev_open(dev); 1802 1803 return 0; 1804 } 1805 1806 static int netsec_xdp(struct net_device *ndev, struct netdev_bpf *xdp) 1807 { 1808 struct netsec_priv *priv = netdev_priv(ndev); 1809 1810 switch (xdp->command) { 1811 case XDP_SETUP_PROG: 1812 return netsec_xdp_setup(priv, xdp->prog, xdp->extack); 1813 case XDP_QUERY_PROG: 1814 xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id : 0; 1815 return 0; 1816 default: 1817 return -EINVAL; 1818 } 1819 } 1820 1821 static const struct net_device_ops netsec_netdev_ops = { 1822 .ndo_init = netsec_netdev_init, 1823 .ndo_uninit = netsec_netdev_uninit, 1824 .ndo_open = netsec_netdev_open, 1825 .ndo_stop = netsec_netdev_stop, 1826 .ndo_start_xmit = netsec_netdev_start_xmit, 1827 .ndo_set_features = netsec_netdev_set_features, 1828 .ndo_set_mac_address = eth_mac_addr, 1829 .ndo_validate_addr = eth_validate_addr, 1830 .ndo_do_ioctl = phy_do_ioctl, 1831 .ndo_xdp_xmit = netsec_xdp_xmit, 1832 .ndo_bpf = netsec_xdp, 1833 }; 1834 1835 static int netsec_of_probe(struct platform_device *pdev, 1836 struct netsec_priv *priv, u32 *phy_addr) 1837 { 1838 priv->phy_np = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0); 1839 if (!priv->phy_np) { 1840 dev_err(&pdev->dev, "missing required property 'phy-handle'\n"); 1841 return -EINVAL; 1842 } 1843 1844 *phy_addr = of_mdio_parse_addr(&pdev->dev, priv->phy_np); 1845 1846 priv->clk = devm_clk_get(&pdev->dev, NULL); /* get by 'phy_ref_clk' */ 1847 if (IS_ERR(priv->clk)) { 1848 dev_err(&pdev->dev, "phy_ref_clk not found\n"); 1849 return PTR_ERR(priv->clk); 1850 } 1851 priv->freq = clk_get_rate(priv->clk); 1852 1853 return 0; 1854 } 1855 1856 static int netsec_acpi_probe(struct platform_device *pdev, 1857 struct netsec_priv *priv, u32 *phy_addr) 1858 { 1859 int ret; 1860 1861 if (!IS_ENABLED(CONFIG_ACPI)) 1862 return -ENODEV; 1863 1864 ret = device_property_read_u32(&pdev->dev, "phy-channel", phy_addr); 1865 if (ret) { 1866 dev_err(&pdev->dev, 1867 "missing required property 'phy-channel'\n"); 1868 return ret; 1869 } 1870 1871 ret = device_property_read_u32(&pdev->dev, 1872 "socionext,phy-clock-frequency", 1873 &priv->freq); 1874 if (ret) 1875 dev_err(&pdev->dev, 1876 "missing required property 'socionext,phy-clock-frequency'\n"); 1877 return ret; 1878 } 1879 1880 static void netsec_unregister_mdio(struct netsec_priv *priv) 1881 { 1882 struct phy_device *phydev = priv->phydev; 1883 1884 if (!dev_of_node(priv->dev) && phydev) { 1885 phy_device_remove(phydev); 1886 phy_device_free(phydev); 1887 } 1888 1889 mdiobus_unregister(priv->mii_bus); 1890 } 1891 1892 static int netsec_register_mdio(struct netsec_priv *priv, u32 phy_addr) 1893 { 1894 struct mii_bus *bus; 1895 int ret; 1896 1897 bus = devm_mdiobus_alloc(priv->dev); 1898 if (!bus) 1899 return -ENOMEM; 1900 1901 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", dev_name(priv->dev)); 1902 bus->priv = priv; 1903 bus->name = "SNI NETSEC MDIO"; 1904 bus->read = netsec_phy_read; 1905 bus->write = netsec_phy_write; 1906 bus->parent = priv->dev; 1907 priv->mii_bus = bus; 1908 1909 if (dev_of_node(priv->dev)) { 1910 struct device_node *mdio_node, *parent = dev_of_node(priv->dev); 1911 1912 mdio_node = of_get_child_by_name(parent, "mdio"); 1913 if (mdio_node) { 1914 parent = mdio_node; 1915 } else { 1916 /* older f/w doesn't populate the mdio subnode, 1917 * allow relaxed upgrade of f/w in due time. 1918 */ 1919 dev_info(priv->dev, "Upgrade f/w for mdio subnode!\n"); 1920 } 1921 1922 ret = of_mdiobus_register(bus, parent); 1923 of_node_put(mdio_node); 1924 1925 if (ret) { 1926 dev_err(priv->dev, "mdiobus register err(%d)\n", ret); 1927 return ret; 1928 } 1929 } else { 1930 /* Mask out all PHYs from auto probing. */ 1931 bus->phy_mask = ~0; 1932 ret = mdiobus_register(bus); 1933 if (ret) { 1934 dev_err(priv->dev, "mdiobus register err(%d)\n", ret); 1935 return ret; 1936 } 1937 1938 priv->phydev = get_phy_device(bus, phy_addr, false); 1939 if (IS_ERR(priv->phydev)) { 1940 ret = PTR_ERR(priv->phydev); 1941 dev_err(priv->dev, "get_phy_device err(%d)\n", ret); 1942 priv->phydev = NULL; 1943 return -ENODEV; 1944 } 1945 1946 ret = phy_device_register(priv->phydev); 1947 if (ret) { 1948 mdiobus_unregister(bus); 1949 dev_err(priv->dev, 1950 "phy_device_register err(%d)\n", ret); 1951 } 1952 } 1953 1954 return ret; 1955 } 1956 1957 static int netsec_probe(struct platform_device *pdev) 1958 { 1959 struct resource *mmio_res, *eeprom_res, *irq_res; 1960 u8 *mac, macbuf[ETH_ALEN]; 1961 struct netsec_priv *priv; 1962 u32 hw_ver, phy_addr = 0; 1963 struct net_device *ndev; 1964 int ret; 1965 1966 mmio_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1967 if (!mmio_res) { 1968 dev_err(&pdev->dev, "No MMIO resource found.\n"); 1969 return -ENODEV; 1970 } 1971 1972 eeprom_res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1973 if (!eeprom_res) { 1974 dev_info(&pdev->dev, "No EEPROM resource found.\n"); 1975 return -ENODEV; 1976 } 1977 1978 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 1979 if (!irq_res) { 1980 dev_err(&pdev->dev, "No IRQ resource found.\n"); 1981 return -ENODEV; 1982 } 1983 1984 ndev = alloc_etherdev(sizeof(*priv)); 1985 if (!ndev) 1986 return -ENOMEM; 1987 1988 priv = netdev_priv(ndev); 1989 1990 spin_lock_init(&priv->reglock); 1991 SET_NETDEV_DEV(ndev, &pdev->dev); 1992 platform_set_drvdata(pdev, priv); 1993 ndev->irq = irq_res->start; 1994 priv->dev = &pdev->dev; 1995 priv->ndev = ndev; 1996 1997 priv->msg_enable = NETIF_MSG_TX_ERR | NETIF_MSG_HW | NETIF_MSG_DRV | 1998 NETIF_MSG_LINK | NETIF_MSG_PROBE; 1999 2000 priv->phy_interface = device_get_phy_mode(&pdev->dev); 2001 if ((int)priv->phy_interface < 0) { 2002 dev_err(&pdev->dev, "missing required property 'phy-mode'\n"); 2003 ret = -ENODEV; 2004 goto free_ndev; 2005 } 2006 2007 priv->ioaddr = devm_ioremap(&pdev->dev, mmio_res->start, 2008 resource_size(mmio_res)); 2009 if (!priv->ioaddr) { 2010 dev_err(&pdev->dev, "devm_ioremap() failed\n"); 2011 ret = -ENXIO; 2012 goto free_ndev; 2013 } 2014 2015 priv->eeprom_base = devm_ioremap(&pdev->dev, eeprom_res->start, 2016 resource_size(eeprom_res)); 2017 if (!priv->eeprom_base) { 2018 dev_err(&pdev->dev, "devm_ioremap() failed for EEPROM\n"); 2019 ret = -ENXIO; 2020 goto free_ndev; 2021 } 2022 2023 mac = device_get_mac_address(&pdev->dev, macbuf, sizeof(macbuf)); 2024 if (mac) 2025 ether_addr_copy(ndev->dev_addr, mac); 2026 2027 if (priv->eeprom_base && 2028 (!mac || !is_valid_ether_addr(ndev->dev_addr))) { 2029 void __iomem *macp = priv->eeprom_base + 2030 NETSEC_EEPROM_MAC_ADDRESS; 2031 2032 ndev->dev_addr[0] = readb(macp + 3); 2033 ndev->dev_addr[1] = readb(macp + 2); 2034 ndev->dev_addr[2] = readb(macp + 1); 2035 ndev->dev_addr[3] = readb(macp + 0); 2036 ndev->dev_addr[4] = readb(macp + 7); 2037 ndev->dev_addr[5] = readb(macp + 6); 2038 } 2039 2040 if (!is_valid_ether_addr(ndev->dev_addr)) { 2041 dev_warn(&pdev->dev, "No MAC address found, using random\n"); 2042 eth_hw_addr_random(ndev); 2043 } 2044 2045 if (dev_of_node(&pdev->dev)) 2046 ret = netsec_of_probe(pdev, priv, &phy_addr); 2047 else 2048 ret = netsec_acpi_probe(pdev, priv, &phy_addr); 2049 if (ret) 2050 goto free_ndev; 2051 2052 priv->phy_addr = phy_addr; 2053 2054 if (!priv->freq) { 2055 dev_err(&pdev->dev, "missing PHY reference clock frequency\n"); 2056 ret = -ENODEV; 2057 goto free_ndev; 2058 } 2059 2060 /* default for throughput */ 2061 priv->et_coalesce.rx_coalesce_usecs = 500; 2062 priv->et_coalesce.rx_max_coalesced_frames = 8; 2063 priv->et_coalesce.tx_coalesce_usecs = 500; 2064 priv->et_coalesce.tx_max_coalesced_frames = 8; 2065 2066 ret = device_property_read_u32(&pdev->dev, "max-frame-size", 2067 &ndev->max_mtu); 2068 if (ret < 0) 2069 ndev->max_mtu = ETH_DATA_LEN; 2070 2071 /* runtime_pm coverage just for probe, open/close also cover it */ 2072 pm_runtime_enable(&pdev->dev); 2073 pm_runtime_get_sync(&pdev->dev); 2074 2075 hw_ver = netsec_read(priv, NETSEC_REG_F_TAIKI_VER); 2076 /* this driver only supports F_TAIKI style NETSEC */ 2077 if (NETSEC_F_NETSEC_VER_MAJOR_NUM(hw_ver) != 2078 NETSEC_F_NETSEC_VER_MAJOR_NUM(NETSEC_REG_NETSEC_VER_F_TAIKI)) { 2079 ret = -ENODEV; 2080 goto pm_disable; 2081 } 2082 2083 dev_info(&pdev->dev, "hardware revision %d.%d\n", 2084 hw_ver >> 16, hw_ver & 0xffff); 2085 2086 netif_napi_add(ndev, &priv->napi, netsec_napi_poll, NAPI_POLL_WEIGHT); 2087 2088 ndev->netdev_ops = &netsec_netdev_ops; 2089 ndev->ethtool_ops = &netsec_ethtool_ops; 2090 2091 ndev->features |= NETIF_F_HIGHDMA | NETIF_F_RXCSUM | NETIF_F_GSO | 2092 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM; 2093 ndev->hw_features = ndev->features; 2094 2095 priv->rx_cksum_offload_flag = true; 2096 2097 ret = netsec_register_mdio(priv, phy_addr); 2098 if (ret) 2099 goto unreg_napi; 2100 2101 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40))) 2102 dev_warn(&pdev->dev, "Failed to set DMA mask\n"); 2103 2104 ret = register_netdev(ndev); 2105 if (ret) { 2106 netif_err(priv, probe, ndev, "register_netdev() failed\n"); 2107 goto unreg_mii; 2108 } 2109 2110 pm_runtime_put_sync(&pdev->dev); 2111 return 0; 2112 2113 unreg_mii: 2114 netsec_unregister_mdio(priv); 2115 unreg_napi: 2116 netif_napi_del(&priv->napi); 2117 pm_disable: 2118 pm_runtime_put_sync(&pdev->dev); 2119 pm_runtime_disable(&pdev->dev); 2120 free_ndev: 2121 free_netdev(ndev); 2122 dev_err(&pdev->dev, "init failed\n"); 2123 2124 return ret; 2125 } 2126 2127 static int netsec_remove(struct platform_device *pdev) 2128 { 2129 struct netsec_priv *priv = platform_get_drvdata(pdev); 2130 2131 unregister_netdev(priv->ndev); 2132 2133 netsec_unregister_mdio(priv); 2134 2135 netif_napi_del(&priv->napi); 2136 2137 pm_runtime_disable(&pdev->dev); 2138 free_netdev(priv->ndev); 2139 2140 return 0; 2141 } 2142 2143 #ifdef CONFIG_PM 2144 static int netsec_runtime_suspend(struct device *dev) 2145 { 2146 struct netsec_priv *priv = dev_get_drvdata(dev); 2147 2148 netsec_write(priv, NETSEC_REG_CLK_EN, 0); 2149 2150 clk_disable_unprepare(priv->clk); 2151 2152 return 0; 2153 } 2154 2155 static int netsec_runtime_resume(struct device *dev) 2156 { 2157 struct netsec_priv *priv = dev_get_drvdata(dev); 2158 2159 clk_prepare_enable(priv->clk); 2160 2161 netsec_write(priv, NETSEC_REG_CLK_EN, NETSEC_CLK_EN_REG_DOM_D | 2162 NETSEC_CLK_EN_REG_DOM_C | 2163 NETSEC_CLK_EN_REG_DOM_G); 2164 return 0; 2165 } 2166 #endif 2167 2168 static const struct dev_pm_ops netsec_pm_ops = { 2169 SET_RUNTIME_PM_OPS(netsec_runtime_suspend, netsec_runtime_resume, NULL) 2170 }; 2171 2172 static const struct of_device_id netsec_dt_ids[] = { 2173 { .compatible = "socionext,synquacer-netsec" }, 2174 { } 2175 }; 2176 MODULE_DEVICE_TABLE(of, netsec_dt_ids); 2177 2178 #ifdef CONFIG_ACPI 2179 static const struct acpi_device_id netsec_acpi_ids[] = { 2180 { "SCX0001" }, 2181 { } 2182 }; 2183 MODULE_DEVICE_TABLE(acpi, netsec_acpi_ids); 2184 #endif 2185 2186 static struct platform_driver netsec_driver = { 2187 .probe = netsec_probe, 2188 .remove = netsec_remove, 2189 .driver = { 2190 .name = "netsec", 2191 .pm = &netsec_pm_ops, 2192 .of_match_table = netsec_dt_ids, 2193 .acpi_match_table = ACPI_PTR(netsec_acpi_ids), 2194 }, 2195 }; 2196 module_platform_driver(netsec_driver); 2197 2198 MODULE_AUTHOR("Jassi Brar <jaswinder.singh@linaro.org>"); 2199 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); 2200 MODULE_DESCRIPTION("NETSEC Ethernet driver"); 2201 MODULE_LICENSE("GPL"); 2202