1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later 2 /* 3 * Copyright 2008 - 2016 Freescale Semiconductor Inc. 4 * Copyright 2020 NXP 5 */ 6 7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8 9 #include <linux/init.h> 10 #include <linux/module.h> 11 #include <linux/of_platform.h> 12 #include <linux/of_mdio.h> 13 #include <linux/of_net.h> 14 #include <linux/io.h> 15 #include <linux/if_arp.h> 16 #include <linux/if_vlan.h> 17 #include <linux/icmp.h> 18 #include <linux/ip.h> 19 #include <linux/ipv6.h> 20 #include <linux/udp.h> 21 #include <linux/tcp.h> 22 #include <linux/net.h> 23 #include <linux/skbuff.h> 24 #include <linux/etherdevice.h> 25 #include <linux/if_ether.h> 26 #include <linux/highmem.h> 27 #include <linux/percpu.h> 28 #include <linux/dma-mapping.h> 29 #include <linux/sort.h> 30 #include <linux/phy_fixed.h> 31 #include <linux/bpf.h> 32 #include <linux/bpf_trace.h> 33 #include <soc/fsl/bman.h> 34 #include <soc/fsl/qman.h> 35 #include "fman.h" 36 #include "fman_port.h" 37 #include "mac.h" 38 #include "dpaa_eth.h" 39 40 /* CREATE_TRACE_POINTS only needs to be defined once. Other dpaa files 41 * using trace events only need to #include <trace/events/sched.h> 42 */ 43 #define CREATE_TRACE_POINTS 44 #include "dpaa_eth_trace.h" 45 46 static int debug = -1; 47 module_param(debug, int, 0444); 48 MODULE_PARM_DESC(debug, "Module/Driver verbosity level (0=none,...,16=all)"); 49 50 static u16 tx_timeout = 1000; 51 module_param(tx_timeout, ushort, 0444); 52 MODULE_PARM_DESC(tx_timeout, "The Tx timeout in ms"); 53 54 #define FM_FD_STAT_RX_ERRORS \ 55 (FM_FD_ERR_DMA | FM_FD_ERR_PHYSICAL | \ 56 FM_FD_ERR_SIZE | FM_FD_ERR_CLS_DISCARD | \ 57 FM_FD_ERR_EXTRACTION | FM_FD_ERR_NO_SCHEME | \ 58 FM_FD_ERR_PRS_TIMEOUT | FM_FD_ERR_PRS_ILL_INSTRUCT | \ 59 FM_FD_ERR_PRS_HDR_ERR) 60 61 #define FM_FD_STAT_TX_ERRORS \ 62 (FM_FD_ERR_UNSUPPORTED_FORMAT | \ 63 FM_FD_ERR_LENGTH | FM_FD_ERR_DMA) 64 65 #define DPAA_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \ 66 NETIF_MSG_LINK | NETIF_MSG_IFUP | \ 67 NETIF_MSG_IFDOWN | NETIF_MSG_HW) 68 69 #define DPAA_INGRESS_CS_THRESHOLD 0x10000000 70 /* Ingress congestion threshold on FMan ports 71 * The size in bytes of the ingress tail-drop threshold on FMan ports. 72 * Traffic piling up above this value will be rejected by QMan and discarded 73 * by FMan. 74 */ 75 76 /* Size in bytes of the FQ taildrop threshold */ 77 #define DPAA_FQ_TD 0x200000 78 79 #define DPAA_CS_THRESHOLD_1G 0x06000000 80 /* Egress congestion threshold on 1G ports, range 0x1000 .. 0x10000000 81 * The size in bytes of the egress Congestion State notification threshold on 82 * 1G ports. The 1G dTSECs can quite easily be flooded by cores doing Tx in a 83 * tight loop (e.g. by sending UDP datagrams at "while(1) speed"), 84 * and the larger the frame size, the more acute the problem. 85 * So we have to find a balance between these factors: 86 * - avoiding the device staying congested for a prolonged time (risking 87 * the netdev watchdog to fire - see also the tx_timeout module param); 88 * - affecting performance of protocols such as TCP, which otherwise 89 * behave well under the congestion notification mechanism; 90 * - preventing the Tx cores from tightly-looping (as if the congestion 91 * threshold was too low to be effective); 92 * - running out of memory if the CS threshold is set too high. 93 */ 94 95 #define DPAA_CS_THRESHOLD_10G 0x10000000 96 /* The size in bytes of the egress Congestion State notification threshold on 97 * 10G ports, range 0x1000 .. 0x10000000 98 */ 99 100 /* Largest value that the FQD's OAL field can hold */ 101 #define FSL_QMAN_MAX_OAL 127 102 103 /* Default alignment for start of data in an Rx FD */ 104 #ifdef CONFIG_DPAA_ERRATUM_A050385 105 /* aligning data start to 64 avoids DMA transaction splits, unless the buffer 106 * is crossing a 4k page boundary 107 */ 108 #define DPAA_FD_DATA_ALIGNMENT (fman_has_errata_a050385() ? 64 : 16) 109 /* aligning to 256 avoids DMA transaction splits caused by 4k page boundary 110 * crossings; also, all SG fragments except the last must have a size multiple 111 * of 256 to avoid DMA transaction splits 112 */ 113 #define DPAA_A050385_ALIGN 256 114 #define DPAA_FD_RX_DATA_ALIGNMENT (fman_has_errata_a050385() ? \ 115 DPAA_A050385_ALIGN : 16) 116 #else 117 #define DPAA_FD_DATA_ALIGNMENT 16 118 #define DPAA_FD_RX_DATA_ALIGNMENT DPAA_FD_DATA_ALIGNMENT 119 #endif 120 121 /* The DPAA requires 256 bytes reserved and mapped for the SGT */ 122 #define DPAA_SGT_SIZE 256 123 124 /* Values for the L3R field of the FM Parse Results 125 */ 126 /* L3 Type field: First IP Present IPv4 */ 127 #define FM_L3_PARSE_RESULT_IPV4 0x8000 128 /* L3 Type field: First IP Present IPv6 */ 129 #define FM_L3_PARSE_RESULT_IPV6 0x4000 130 /* Values for the L4R field of the FM Parse Results */ 131 /* L4 Type field: UDP */ 132 #define FM_L4_PARSE_RESULT_UDP 0x40 133 /* L4 Type field: TCP */ 134 #define FM_L4_PARSE_RESULT_TCP 0x20 135 136 /* FD status field indicating whether the FM Parser has attempted to validate 137 * the L4 csum of the frame. 138 * Note that having this bit set doesn't necessarily imply that the checksum 139 * is valid. One would have to check the parse results to find that out. 140 */ 141 #define FM_FD_STAT_L4CV 0x00000004 142 143 #define DPAA_SGT_MAX_ENTRIES 16 /* maximum number of entries in SG Table */ 144 #define DPAA_BUFF_RELEASE_MAX 8 /* maximum number of buffers released at once */ 145 146 #define FSL_DPAA_BPID_INV 0xff 147 #define FSL_DPAA_ETH_MAX_BUF_COUNT 128 148 #define FSL_DPAA_ETH_REFILL_THRESHOLD 80 149 150 #define DPAA_TX_PRIV_DATA_SIZE 16 151 #define DPAA_PARSE_RESULTS_SIZE sizeof(struct fman_prs_result) 152 #define DPAA_TIME_STAMP_SIZE 8 153 #define DPAA_HASH_RESULTS_SIZE 8 154 #define DPAA_HWA_SIZE (DPAA_PARSE_RESULTS_SIZE + DPAA_TIME_STAMP_SIZE \ 155 + DPAA_HASH_RESULTS_SIZE) 156 #define DPAA_RX_PRIV_DATA_DEFAULT_SIZE (DPAA_TX_PRIV_DATA_SIZE + \ 157 XDP_PACKET_HEADROOM - DPAA_HWA_SIZE) 158 #ifdef CONFIG_DPAA_ERRATUM_A050385 159 #define DPAA_RX_PRIV_DATA_A050385_SIZE (DPAA_A050385_ALIGN - DPAA_HWA_SIZE) 160 #define DPAA_RX_PRIV_DATA_SIZE (fman_has_errata_a050385() ? \ 161 DPAA_RX_PRIV_DATA_A050385_SIZE : \ 162 DPAA_RX_PRIV_DATA_DEFAULT_SIZE) 163 #else 164 #define DPAA_RX_PRIV_DATA_SIZE DPAA_RX_PRIV_DATA_DEFAULT_SIZE 165 #endif 166 167 #define DPAA_ETH_PCD_RXQ_NUM 128 168 169 #define DPAA_ENQUEUE_RETRIES 100000 170 171 enum port_type {RX, TX}; 172 173 struct fm_port_fqs { 174 struct dpaa_fq *tx_defq; 175 struct dpaa_fq *tx_errq; 176 struct dpaa_fq *rx_defq; 177 struct dpaa_fq *rx_errq; 178 struct dpaa_fq *rx_pcdq; 179 }; 180 181 /* All the dpa bps in use at any moment */ 182 static struct dpaa_bp *dpaa_bp_array[BM_MAX_NUM_OF_POOLS]; 183 184 #define DPAA_BP_RAW_SIZE 4096 185 186 #ifdef CONFIG_DPAA_ERRATUM_A050385 187 #define dpaa_bp_size(raw_size) (SKB_WITH_OVERHEAD(raw_size) & \ 188 ~(DPAA_A050385_ALIGN - 1)) 189 #else 190 #define dpaa_bp_size(raw_size) SKB_WITH_OVERHEAD(raw_size) 191 #endif 192 193 static int dpaa_max_frm; 194 195 static int dpaa_rx_extra_headroom; 196 197 #define dpaa_get_max_mtu() \ 198 (dpaa_max_frm - (VLAN_ETH_HLEN + ETH_FCS_LEN)) 199 200 static int dpaa_netdev_init(struct net_device *net_dev, 201 const struct net_device_ops *dpaa_ops, 202 u16 tx_timeout) 203 { 204 struct dpaa_priv *priv = netdev_priv(net_dev); 205 struct device *dev = net_dev->dev.parent; 206 struct dpaa_percpu_priv *percpu_priv; 207 const u8 *mac_addr; 208 int i, err; 209 210 /* Although we access another CPU's private data here 211 * we do it at initialization so it is safe 212 */ 213 for_each_possible_cpu(i) { 214 percpu_priv = per_cpu_ptr(priv->percpu_priv, i); 215 percpu_priv->net_dev = net_dev; 216 } 217 218 net_dev->netdev_ops = dpaa_ops; 219 mac_addr = priv->mac_dev->addr; 220 221 net_dev->mem_start = priv->mac_dev->res->start; 222 net_dev->mem_end = priv->mac_dev->res->end; 223 224 net_dev->min_mtu = ETH_MIN_MTU; 225 net_dev->max_mtu = dpaa_get_max_mtu(); 226 227 net_dev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | 228 NETIF_F_LLTX | NETIF_F_RXHASH); 229 230 net_dev->hw_features |= NETIF_F_SG | NETIF_F_HIGHDMA; 231 /* The kernels enables GSO automatically, if we declare NETIF_F_SG. 232 * For conformity, we'll still declare GSO explicitly. 233 */ 234 net_dev->features |= NETIF_F_GSO; 235 net_dev->features |= NETIF_F_RXCSUM; 236 237 net_dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 238 /* we do not want shared skbs on TX */ 239 net_dev->priv_flags &= ~IFF_TX_SKB_SHARING; 240 241 net_dev->features |= net_dev->hw_features; 242 net_dev->vlan_features = net_dev->features; 243 244 if (is_valid_ether_addr(mac_addr)) { 245 memcpy(net_dev->perm_addr, mac_addr, net_dev->addr_len); 246 eth_hw_addr_set(net_dev, mac_addr); 247 } else { 248 eth_hw_addr_random(net_dev); 249 err = priv->mac_dev->change_addr(priv->mac_dev->fman_mac, 250 (const enet_addr_t *)net_dev->dev_addr); 251 if (err) { 252 dev_err(dev, "Failed to set random MAC address\n"); 253 return -EINVAL; 254 } 255 dev_info(dev, "Using random MAC address: %pM\n", 256 net_dev->dev_addr); 257 } 258 259 net_dev->ethtool_ops = &dpaa_ethtool_ops; 260 261 net_dev->needed_headroom = priv->tx_headroom; 262 net_dev->watchdog_timeo = msecs_to_jiffies(tx_timeout); 263 264 /* start without the RUNNING flag, phylib controls it later */ 265 netif_carrier_off(net_dev); 266 267 err = register_netdev(net_dev); 268 if (err < 0) { 269 dev_err(dev, "register_netdev() = %d\n", err); 270 return err; 271 } 272 273 return 0; 274 } 275 276 static int dpaa_stop(struct net_device *net_dev) 277 { 278 struct mac_device *mac_dev; 279 struct dpaa_priv *priv; 280 int i, err, error; 281 282 priv = netdev_priv(net_dev); 283 mac_dev = priv->mac_dev; 284 285 netif_tx_stop_all_queues(net_dev); 286 /* Allow the Fman (Tx) port to process in-flight frames before we 287 * try switching it off. 288 */ 289 msleep(200); 290 291 err = mac_dev->stop(mac_dev); 292 if (err < 0) 293 netif_err(priv, ifdown, net_dev, "mac_dev->stop() = %d\n", 294 err); 295 296 for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) { 297 error = fman_port_disable(mac_dev->port[i]); 298 if (error) 299 err = error; 300 } 301 302 if (net_dev->phydev) 303 phy_disconnect(net_dev->phydev); 304 net_dev->phydev = NULL; 305 306 msleep(200); 307 308 return err; 309 } 310 311 static void dpaa_tx_timeout(struct net_device *net_dev, unsigned int txqueue) 312 { 313 struct dpaa_percpu_priv *percpu_priv; 314 const struct dpaa_priv *priv; 315 316 priv = netdev_priv(net_dev); 317 percpu_priv = this_cpu_ptr(priv->percpu_priv); 318 319 netif_crit(priv, timer, net_dev, "Transmit timeout latency: %u ms\n", 320 jiffies_to_msecs(jiffies - dev_trans_start(net_dev))); 321 322 percpu_priv->stats.tx_errors++; 323 } 324 325 /* Calculates the statistics for the given device by adding the statistics 326 * collected by each CPU. 327 */ 328 static void dpaa_get_stats64(struct net_device *net_dev, 329 struct rtnl_link_stats64 *s) 330 { 331 int numstats = sizeof(struct rtnl_link_stats64) / sizeof(u64); 332 struct dpaa_priv *priv = netdev_priv(net_dev); 333 struct dpaa_percpu_priv *percpu_priv; 334 u64 *netstats = (u64 *)s; 335 u64 *cpustats; 336 int i, j; 337 338 for_each_possible_cpu(i) { 339 percpu_priv = per_cpu_ptr(priv->percpu_priv, i); 340 341 cpustats = (u64 *)&percpu_priv->stats; 342 343 /* add stats from all CPUs */ 344 for (j = 0; j < numstats; j++) 345 netstats[j] += cpustats[j]; 346 } 347 } 348 349 static int dpaa_setup_tc(struct net_device *net_dev, enum tc_setup_type type, 350 void *type_data) 351 { 352 struct dpaa_priv *priv = netdev_priv(net_dev); 353 struct tc_mqprio_qopt *mqprio = type_data; 354 u8 num_tc; 355 int i; 356 357 if (type != TC_SETUP_QDISC_MQPRIO) 358 return -EOPNOTSUPP; 359 360 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 361 num_tc = mqprio->num_tc; 362 363 if (num_tc == priv->num_tc) 364 return 0; 365 366 if (!num_tc) { 367 netdev_reset_tc(net_dev); 368 goto out; 369 } 370 371 if (num_tc > DPAA_TC_NUM) { 372 netdev_err(net_dev, "Too many traffic classes: max %d supported.\n", 373 DPAA_TC_NUM); 374 return -EINVAL; 375 } 376 377 netdev_set_num_tc(net_dev, num_tc); 378 379 for (i = 0; i < num_tc; i++) 380 netdev_set_tc_queue(net_dev, i, DPAA_TC_TXQ_NUM, 381 i * DPAA_TC_TXQ_NUM); 382 383 out: 384 priv->num_tc = num_tc ? : 1; 385 netif_set_real_num_tx_queues(net_dev, priv->num_tc * DPAA_TC_TXQ_NUM); 386 return 0; 387 } 388 389 static struct mac_device *dpaa_mac_dev_get(struct platform_device *pdev) 390 { 391 struct dpaa_eth_data *eth_data; 392 struct device *dpaa_dev; 393 struct mac_device *mac_dev; 394 395 dpaa_dev = &pdev->dev; 396 eth_data = dpaa_dev->platform_data; 397 if (!eth_data) { 398 dev_err(dpaa_dev, "eth_data missing\n"); 399 return ERR_PTR(-ENODEV); 400 } 401 mac_dev = eth_data->mac_dev; 402 if (!mac_dev) { 403 dev_err(dpaa_dev, "mac_dev missing\n"); 404 return ERR_PTR(-EINVAL); 405 } 406 407 return mac_dev; 408 } 409 410 static int dpaa_set_mac_address(struct net_device *net_dev, void *addr) 411 { 412 const struct dpaa_priv *priv; 413 struct mac_device *mac_dev; 414 struct sockaddr old_addr; 415 int err; 416 417 priv = netdev_priv(net_dev); 418 419 memcpy(old_addr.sa_data, net_dev->dev_addr, ETH_ALEN); 420 421 err = eth_mac_addr(net_dev, addr); 422 if (err < 0) { 423 netif_err(priv, drv, net_dev, "eth_mac_addr() = %d\n", err); 424 return err; 425 } 426 427 mac_dev = priv->mac_dev; 428 429 err = mac_dev->change_addr(mac_dev->fman_mac, 430 (const enet_addr_t *)net_dev->dev_addr); 431 if (err < 0) { 432 netif_err(priv, drv, net_dev, "mac_dev->change_addr() = %d\n", 433 err); 434 /* reverting to previous address */ 435 eth_mac_addr(net_dev, &old_addr); 436 437 return err; 438 } 439 440 return 0; 441 } 442 443 static void dpaa_set_rx_mode(struct net_device *net_dev) 444 { 445 const struct dpaa_priv *priv; 446 int err; 447 448 priv = netdev_priv(net_dev); 449 450 if (!!(net_dev->flags & IFF_PROMISC) != priv->mac_dev->promisc) { 451 priv->mac_dev->promisc = !priv->mac_dev->promisc; 452 err = priv->mac_dev->set_promisc(priv->mac_dev->fman_mac, 453 priv->mac_dev->promisc); 454 if (err < 0) 455 netif_err(priv, drv, net_dev, 456 "mac_dev->set_promisc() = %d\n", 457 err); 458 } 459 460 if (!!(net_dev->flags & IFF_ALLMULTI) != priv->mac_dev->allmulti) { 461 priv->mac_dev->allmulti = !priv->mac_dev->allmulti; 462 err = priv->mac_dev->set_allmulti(priv->mac_dev->fman_mac, 463 priv->mac_dev->allmulti); 464 if (err < 0) 465 netif_err(priv, drv, net_dev, 466 "mac_dev->set_allmulti() = %d\n", 467 err); 468 } 469 470 err = priv->mac_dev->set_multi(net_dev, priv->mac_dev); 471 if (err < 0) 472 netif_err(priv, drv, net_dev, "mac_dev->set_multi() = %d\n", 473 err); 474 } 475 476 static struct dpaa_bp *dpaa_bpid2pool(int bpid) 477 { 478 if (WARN_ON(bpid < 0 || bpid >= BM_MAX_NUM_OF_POOLS)) 479 return NULL; 480 481 return dpaa_bp_array[bpid]; 482 } 483 484 /* checks if this bpool is already allocated */ 485 static bool dpaa_bpid2pool_use(int bpid) 486 { 487 if (dpaa_bpid2pool(bpid)) { 488 refcount_inc(&dpaa_bp_array[bpid]->refs); 489 return true; 490 } 491 492 return false; 493 } 494 495 /* called only once per bpid by dpaa_bp_alloc_pool() */ 496 static void dpaa_bpid2pool_map(int bpid, struct dpaa_bp *dpaa_bp) 497 { 498 dpaa_bp_array[bpid] = dpaa_bp; 499 refcount_set(&dpaa_bp->refs, 1); 500 } 501 502 static int dpaa_bp_alloc_pool(struct dpaa_bp *dpaa_bp) 503 { 504 int err; 505 506 if (dpaa_bp->size == 0 || dpaa_bp->config_count == 0) { 507 pr_err("%s: Buffer pool is not properly initialized! Missing size or initial number of buffers\n", 508 __func__); 509 return -EINVAL; 510 } 511 512 /* If the pool is already specified, we only create one per bpid */ 513 if (dpaa_bp->bpid != FSL_DPAA_BPID_INV && 514 dpaa_bpid2pool_use(dpaa_bp->bpid)) 515 return 0; 516 517 if (dpaa_bp->bpid == FSL_DPAA_BPID_INV) { 518 dpaa_bp->pool = bman_new_pool(); 519 if (!dpaa_bp->pool) { 520 pr_err("%s: bman_new_pool() failed\n", 521 __func__); 522 return -ENODEV; 523 } 524 525 dpaa_bp->bpid = (u8)bman_get_bpid(dpaa_bp->pool); 526 } 527 528 if (dpaa_bp->seed_cb) { 529 err = dpaa_bp->seed_cb(dpaa_bp); 530 if (err) 531 goto pool_seed_failed; 532 } 533 534 dpaa_bpid2pool_map(dpaa_bp->bpid, dpaa_bp); 535 536 return 0; 537 538 pool_seed_failed: 539 pr_err("%s: pool seeding failed\n", __func__); 540 bman_free_pool(dpaa_bp->pool); 541 542 return err; 543 } 544 545 /* remove and free all the buffers from the given buffer pool */ 546 static void dpaa_bp_drain(struct dpaa_bp *bp) 547 { 548 u8 num = 8; 549 int ret; 550 551 do { 552 struct bm_buffer bmb[8]; 553 int i; 554 555 ret = bman_acquire(bp->pool, bmb, num); 556 if (ret < 0) { 557 if (num == 8) { 558 /* we have less than 8 buffers left; 559 * drain them one by one 560 */ 561 num = 1; 562 ret = 1; 563 continue; 564 } else { 565 /* Pool is fully drained */ 566 break; 567 } 568 } 569 570 if (bp->free_buf_cb) 571 for (i = 0; i < num; i++) 572 bp->free_buf_cb(bp, &bmb[i]); 573 } while (ret > 0); 574 } 575 576 static void dpaa_bp_free(struct dpaa_bp *dpaa_bp) 577 { 578 struct dpaa_bp *bp = dpaa_bpid2pool(dpaa_bp->bpid); 579 580 /* the mapping between bpid and dpaa_bp is done very late in the 581 * allocation procedure; if something failed before the mapping, the bp 582 * was not configured, therefore we don't need the below instructions 583 */ 584 if (!bp) 585 return; 586 587 if (!refcount_dec_and_test(&bp->refs)) 588 return; 589 590 if (bp->free_buf_cb) 591 dpaa_bp_drain(bp); 592 593 dpaa_bp_array[bp->bpid] = NULL; 594 bman_free_pool(bp->pool); 595 } 596 597 static void dpaa_bps_free(struct dpaa_priv *priv) 598 { 599 dpaa_bp_free(priv->dpaa_bp); 600 } 601 602 /* Use multiple WQs for FQ assignment: 603 * - Tx Confirmation queues go to WQ1. 604 * - Rx Error and Tx Error queues go to WQ5 (giving them a better chance 605 * to be scheduled, in case there are many more FQs in WQ6). 606 * - Rx Default goes to WQ6. 607 * - Tx queues go to different WQs depending on their priority. Equal 608 * chunks of NR_CPUS queues go to WQ6 (lowest priority), WQ2, WQ1 and 609 * WQ0 (highest priority). 610 * This ensures that Tx-confirmed buffers are timely released. In particular, 611 * it avoids congestion on the Tx Confirm FQs, which can pile up PFDRs if they 612 * are greatly outnumbered by other FQs in the system, while 613 * dequeue scheduling is round-robin. 614 */ 615 static inline void dpaa_assign_wq(struct dpaa_fq *fq, int idx) 616 { 617 switch (fq->fq_type) { 618 case FQ_TYPE_TX_CONFIRM: 619 case FQ_TYPE_TX_CONF_MQ: 620 fq->wq = 1; 621 break; 622 case FQ_TYPE_RX_ERROR: 623 case FQ_TYPE_TX_ERROR: 624 fq->wq = 5; 625 break; 626 case FQ_TYPE_RX_DEFAULT: 627 case FQ_TYPE_RX_PCD: 628 fq->wq = 6; 629 break; 630 case FQ_TYPE_TX: 631 switch (idx / DPAA_TC_TXQ_NUM) { 632 case 0: 633 /* Low priority (best effort) */ 634 fq->wq = 6; 635 break; 636 case 1: 637 /* Medium priority */ 638 fq->wq = 2; 639 break; 640 case 2: 641 /* High priority */ 642 fq->wq = 1; 643 break; 644 case 3: 645 /* Very high priority */ 646 fq->wq = 0; 647 break; 648 default: 649 WARN(1, "Too many TX FQs: more than %d!\n", 650 DPAA_ETH_TXQ_NUM); 651 } 652 break; 653 default: 654 WARN(1, "Invalid FQ type %d for FQID %d!\n", 655 fq->fq_type, fq->fqid); 656 } 657 } 658 659 static struct dpaa_fq *dpaa_fq_alloc(struct device *dev, 660 u32 start, u32 count, 661 struct list_head *list, 662 enum dpaa_fq_type fq_type) 663 { 664 struct dpaa_fq *dpaa_fq; 665 int i; 666 667 dpaa_fq = devm_kcalloc(dev, count, sizeof(*dpaa_fq), 668 GFP_KERNEL); 669 if (!dpaa_fq) 670 return NULL; 671 672 for (i = 0; i < count; i++) { 673 dpaa_fq[i].fq_type = fq_type; 674 dpaa_fq[i].fqid = start ? start + i : 0; 675 list_add_tail(&dpaa_fq[i].list, list); 676 } 677 678 for (i = 0; i < count; i++) 679 dpaa_assign_wq(dpaa_fq + i, i); 680 681 return dpaa_fq; 682 } 683 684 static int dpaa_alloc_all_fqs(struct device *dev, struct list_head *list, 685 struct fm_port_fqs *port_fqs) 686 { 687 struct dpaa_fq *dpaa_fq; 688 u32 fq_base, fq_base_aligned, i; 689 690 dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_RX_ERROR); 691 if (!dpaa_fq) 692 goto fq_alloc_failed; 693 694 port_fqs->rx_errq = &dpaa_fq[0]; 695 696 dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_RX_DEFAULT); 697 if (!dpaa_fq) 698 goto fq_alloc_failed; 699 700 port_fqs->rx_defq = &dpaa_fq[0]; 701 702 /* the PCD FQIDs range needs to be aligned for correct operation */ 703 if (qman_alloc_fqid_range(&fq_base, 2 * DPAA_ETH_PCD_RXQ_NUM)) 704 goto fq_alloc_failed; 705 706 fq_base_aligned = ALIGN(fq_base, DPAA_ETH_PCD_RXQ_NUM); 707 708 for (i = fq_base; i < fq_base_aligned; i++) 709 qman_release_fqid(i); 710 711 for (i = fq_base_aligned + DPAA_ETH_PCD_RXQ_NUM; 712 i < (fq_base + 2 * DPAA_ETH_PCD_RXQ_NUM); i++) 713 qman_release_fqid(i); 714 715 dpaa_fq = dpaa_fq_alloc(dev, fq_base_aligned, DPAA_ETH_PCD_RXQ_NUM, 716 list, FQ_TYPE_RX_PCD); 717 if (!dpaa_fq) 718 goto fq_alloc_failed; 719 720 port_fqs->rx_pcdq = &dpaa_fq[0]; 721 722 if (!dpaa_fq_alloc(dev, 0, DPAA_ETH_TXQ_NUM, list, FQ_TYPE_TX_CONF_MQ)) 723 goto fq_alloc_failed; 724 725 dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_TX_ERROR); 726 if (!dpaa_fq) 727 goto fq_alloc_failed; 728 729 port_fqs->tx_errq = &dpaa_fq[0]; 730 731 dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_TX_CONFIRM); 732 if (!dpaa_fq) 733 goto fq_alloc_failed; 734 735 port_fqs->tx_defq = &dpaa_fq[0]; 736 737 if (!dpaa_fq_alloc(dev, 0, DPAA_ETH_TXQ_NUM, list, FQ_TYPE_TX)) 738 goto fq_alloc_failed; 739 740 return 0; 741 742 fq_alloc_failed: 743 dev_err(dev, "dpaa_fq_alloc() failed\n"); 744 return -ENOMEM; 745 } 746 747 static u32 rx_pool_channel; 748 static DEFINE_SPINLOCK(rx_pool_channel_init); 749 750 static int dpaa_get_channel(void) 751 { 752 spin_lock(&rx_pool_channel_init); 753 if (!rx_pool_channel) { 754 u32 pool; 755 int ret; 756 757 ret = qman_alloc_pool(&pool); 758 759 if (!ret) 760 rx_pool_channel = pool; 761 } 762 spin_unlock(&rx_pool_channel_init); 763 if (!rx_pool_channel) 764 return -ENOMEM; 765 return rx_pool_channel; 766 } 767 768 static void dpaa_release_channel(void) 769 { 770 qman_release_pool(rx_pool_channel); 771 } 772 773 static void dpaa_eth_add_channel(u16 channel, struct device *dev) 774 { 775 u32 pool = QM_SDQCR_CHANNELS_POOL_CONV(channel); 776 const cpumask_t *cpus = qman_affine_cpus(); 777 struct qman_portal *portal; 778 int cpu; 779 780 for_each_cpu_and(cpu, cpus, cpu_online_mask) { 781 portal = qman_get_affine_portal(cpu); 782 qman_p_static_dequeue_add(portal, pool); 783 qman_start_using_portal(portal, dev); 784 } 785 } 786 787 /* Congestion group state change notification callback. 788 * Stops the device's egress queues while they are congested and 789 * wakes them upon exiting congested state. 790 * Also updates some CGR-related stats. 791 */ 792 static void dpaa_eth_cgscn(struct qman_portal *qm, struct qman_cgr *cgr, 793 int congested) 794 { 795 struct dpaa_priv *priv = (struct dpaa_priv *)container_of(cgr, 796 struct dpaa_priv, cgr_data.cgr); 797 798 if (congested) { 799 priv->cgr_data.congestion_start_jiffies = jiffies; 800 netif_tx_stop_all_queues(priv->net_dev); 801 priv->cgr_data.cgr_congested_count++; 802 } else { 803 priv->cgr_data.congested_jiffies += 804 (jiffies - priv->cgr_data.congestion_start_jiffies); 805 netif_tx_wake_all_queues(priv->net_dev); 806 } 807 } 808 809 static int dpaa_eth_cgr_init(struct dpaa_priv *priv) 810 { 811 struct qm_mcc_initcgr initcgr; 812 u32 cs_th; 813 int err; 814 815 err = qman_alloc_cgrid(&priv->cgr_data.cgr.cgrid); 816 if (err < 0) { 817 if (netif_msg_drv(priv)) 818 pr_err("%s: Error %d allocating CGR ID\n", 819 __func__, err); 820 goto out_error; 821 } 822 priv->cgr_data.cgr.cb = dpaa_eth_cgscn; 823 824 /* Enable Congestion State Change Notifications and CS taildrop */ 825 memset(&initcgr, 0, sizeof(initcgr)); 826 initcgr.we_mask = cpu_to_be16(QM_CGR_WE_CSCN_EN | QM_CGR_WE_CS_THRES); 827 initcgr.cgr.cscn_en = QM_CGR_EN; 828 829 /* Set different thresholds based on the MAC speed. 830 * This may turn suboptimal if the MAC is reconfigured at a speed 831 * lower than its max, e.g. if a dTSEC later negotiates a 100Mbps link. 832 * In such cases, we ought to reconfigure the threshold, too. 833 */ 834 if (priv->mac_dev->if_support & SUPPORTED_10000baseT_Full) 835 cs_th = DPAA_CS_THRESHOLD_10G; 836 else 837 cs_th = DPAA_CS_THRESHOLD_1G; 838 qm_cgr_cs_thres_set64(&initcgr.cgr.cs_thres, cs_th, 1); 839 840 initcgr.we_mask |= cpu_to_be16(QM_CGR_WE_CSTD_EN); 841 initcgr.cgr.cstd_en = QM_CGR_EN; 842 843 err = qman_create_cgr(&priv->cgr_data.cgr, QMAN_CGR_FLAG_USE_INIT, 844 &initcgr); 845 if (err < 0) { 846 if (netif_msg_drv(priv)) 847 pr_err("%s: Error %d creating CGR with ID %d\n", 848 __func__, err, priv->cgr_data.cgr.cgrid); 849 qman_release_cgrid(priv->cgr_data.cgr.cgrid); 850 goto out_error; 851 } 852 if (netif_msg_drv(priv)) 853 pr_debug("Created CGR %d for netdev with hwaddr %pM on QMan channel %d\n", 854 priv->cgr_data.cgr.cgrid, priv->mac_dev->addr, 855 priv->cgr_data.cgr.chan); 856 857 out_error: 858 return err; 859 } 860 861 static inline void dpaa_setup_ingress(const struct dpaa_priv *priv, 862 struct dpaa_fq *fq, 863 const struct qman_fq *template) 864 { 865 fq->fq_base = *template; 866 fq->net_dev = priv->net_dev; 867 868 fq->flags = QMAN_FQ_FLAG_NO_ENQUEUE; 869 fq->channel = priv->channel; 870 } 871 872 static inline void dpaa_setup_egress(const struct dpaa_priv *priv, 873 struct dpaa_fq *fq, 874 struct fman_port *port, 875 const struct qman_fq *template) 876 { 877 fq->fq_base = *template; 878 fq->net_dev = priv->net_dev; 879 880 if (port) { 881 fq->flags = QMAN_FQ_FLAG_TO_DCPORTAL; 882 fq->channel = (u16)fman_port_get_qman_channel_id(port); 883 } else { 884 fq->flags = QMAN_FQ_FLAG_NO_MODIFY; 885 } 886 } 887 888 static void dpaa_fq_setup(struct dpaa_priv *priv, 889 const struct dpaa_fq_cbs *fq_cbs, 890 struct fman_port *tx_port) 891 { 892 int egress_cnt = 0, conf_cnt = 0, num_portals = 0, portal_cnt = 0, cpu; 893 const cpumask_t *affine_cpus = qman_affine_cpus(); 894 u16 channels[NR_CPUS]; 895 struct dpaa_fq *fq; 896 897 for_each_cpu_and(cpu, affine_cpus, cpu_online_mask) 898 channels[num_portals++] = qman_affine_channel(cpu); 899 900 if (num_portals == 0) 901 dev_err(priv->net_dev->dev.parent, 902 "No Qman software (affine) channels found\n"); 903 904 /* Initialize each FQ in the list */ 905 list_for_each_entry(fq, &priv->dpaa_fq_list, list) { 906 switch (fq->fq_type) { 907 case FQ_TYPE_RX_DEFAULT: 908 dpaa_setup_ingress(priv, fq, &fq_cbs->rx_defq); 909 break; 910 case FQ_TYPE_RX_ERROR: 911 dpaa_setup_ingress(priv, fq, &fq_cbs->rx_errq); 912 break; 913 case FQ_TYPE_RX_PCD: 914 if (!num_portals) 915 continue; 916 dpaa_setup_ingress(priv, fq, &fq_cbs->rx_defq); 917 fq->channel = channels[portal_cnt++ % num_portals]; 918 break; 919 case FQ_TYPE_TX: 920 dpaa_setup_egress(priv, fq, tx_port, 921 &fq_cbs->egress_ern); 922 /* If we have more Tx queues than the number of cores, 923 * just ignore the extra ones. 924 */ 925 if (egress_cnt < DPAA_ETH_TXQ_NUM) 926 priv->egress_fqs[egress_cnt++] = &fq->fq_base; 927 break; 928 case FQ_TYPE_TX_CONF_MQ: 929 priv->conf_fqs[conf_cnt++] = &fq->fq_base; 930 fallthrough; 931 case FQ_TYPE_TX_CONFIRM: 932 dpaa_setup_ingress(priv, fq, &fq_cbs->tx_defq); 933 break; 934 case FQ_TYPE_TX_ERROR: 935 dpaa_setup_ingress(priv, fq, &fq_cbs->tx_errq); 936 break; 937 default: 938 dev_warn(priv->net_dev->dev.parent, 939 "Unknown FQ type detected!\n"); 940 break; 941 } 942 } 943 944 /* Make sure all CPUs receive a corresponding Tx queue. */ 945 while (egress_cnt < DPAA_ETH_TXQ_NUM) { 946 list_for_each_entry(fq, &priv->dpaa_fq_list, list) { 947 if (fq->fq_type != FQ_TYPE_TX) 948 continue; 949 priv->egress_fqs[egress_cnt++] = &fq->fq_base; 950 if (egress_cnt == DPAA_ETH_TXQ_NUM) 951 break; 952 } 953 } 954 } 955 956 static inline int dpaa_tx_fq_to_id(const struct dpaa_priv *priv, 957 struct qman_fq *tx_fq) 958 { 959 int i; 960 961 for (i = 0; i < DPAA_ETH_TXQ_NUM; i++) 962 if (priv->egress_fqs[i] == tx_fq) 963 return i; 964 965 return -EINVAL; 966 } 967 968 static int dpaa_fq_init(struct dpaa_fq *dpaa_fq, bool td_enable) 969 { 970 const struct dpaa_priv *priv; 971 struct qman_fq *confq = NULL; 972 struct qm_mcc_initfq initfq; 973 struct device *dev; 974 struct qman_fq *fq; 975 int queue_id; 976 int err; 977 978 priv = netdev_priv(dpaa_fq->net_dev); 979 dev = dpaa_fq->net_dev->dev.parent; 980 981 if (dpaa_fq->fqid == 0) 982 dpaa_fq->flags |= QMAN_FQ_FLAG_DYNAMIC_FQID; 983 984 dpaa_fq->init = !(dpaa_fq->flags & QMAN_FQ_FLAG_NO_MODIFY); 985 986 err = qman_create_fq(dpaa_fq->fqid, dpaa_fq->flags, &dpaa_fq->fq_base); 987 if (err) { 988 dev_err(dev, "qman_create_fq() failed\n"); 989 return err; 990 } 991 fq = &dpaa_fq->fq_base; 992 993 if (dpaa_fq->init) { 994 memset(&initfq, 0, sizeof(initfq)); 995 996 initfq.we_mask = cpu_to_be16(QM_INITFQ_WE_FQCTRL); 997 /* Note: we may get to keep an empty FQ in cache */ 998 initfq.fqd.fq_ctrl = cpu_to_be16(QM_FQCTRL_PREFERINCACHE); 999 1000 /* Try to reduce the number of portal interrupts for 1001 * Tx Confirmation FQs. 1002 */ 1003 if (dpaa_fq->fq_type == FQ_TYPE_TX_CONFIRM) 1004 initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_AVOIDBLOCK); 1005 1006 /* FQ placement */ 1007 initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_DESTWQ); 1008 1009 qm_fqd_set_destwq(&initfq.fqd, dpaa_fq->channel, dpaa_fq->wq); 1010 1011 /* Put all egress queues in a congestion group of their own. 1012 * Sensu stricto, the Tx confirmation queues are Rx FQs, 1013 * rather than Tx - but they nonetheless account for the 1014 * memory footprint on behalf of egress traffic. We therefore 1015 * place them in the netdev's CGR, along with the Tx FQs. 1016 */ 1017 if (dpaa_fq->fq_type == FQ_TYPE_TX || 1018 dpaa_fq->fq_type == FQ_TYPE_TX_CONFIRM || 1019 dpaa_fq->fq_type == FQ_TYPE_TX_CONF_MQ) { 1020 initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CGID); 1021 initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_CGE); 1022 initfq.fqd.cgid = (u8)priv->cgr_data.cgr.cgrid; 1023 /* Set a fixed overhead accounting, in an attempt to 1024 * reduce the impact of fixed-size skb shells and the 1025 * driver's needed headroom on system memory. This is 1026 * especially the case when the egress traffic is 1027 * composed of small datagrams. 1028 * Unfortunately, QMan's OAL value is capped to an 1029 * insufficient value, but even that is better than 1030 * no overhead accounting at all. 1031 */ 1032 initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_OAC); 1033 qm_fqd_set_oac(&initfq.fqd, QM_OAC_CG); 1034 qm_fqd_set_oal(&initfq.fqd, 1035 min(sizeof(struct sk_buff) + 1036 priv->tx_headroom, 1037 (size_t)FSL_QMAN_MAX_OAL)); 1038 } 1039 1040 if (td_enable) { 1041 initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_TDTHRESH); 1042 qm_fqd_set_taildrop(&initfq.fqd, DPAA_FQ_TD, 1); 1043 initfq.fqd.fq_ctrl = cpu_to_be16(QM_FQCTRL_TDE); 1044 } 1045 1046 if (dpaa_fq->fq_type == FQ_TYPE_TX) { 1047 queue_id = dpaa_tx_fq_to_id(priv, &dpaa_fq->fq_base); 1048 if (queue_id >= 0) 1049 confq = priv->conf_fqs[queue_id]; 1050 if (confq) { 1051 initfq.we_mask |= 1052 cpu_to_be16(QM_INITFQ_WE_CONTEXTA); 1053 /* ContextA: OVOM=1(use contextA2 bits instead of ICAD) 1054 * A2V=1 (contextA A2 field is valid) 1055 * A0V=1 (contextA A0 field is valid) 1056 * B0V=1 (contextB field is valid) 1057 * ContextA A2: EBD=1 (deallocate buffers inside FMan) 1058 * ContextB B0(ASPID): 0 (absolute Virtual Storage ID) 1059 */ 1060 qm_fqd_context_a_set64(&initfq.fqd, 1061 0x1e00000080000000ULL); 1062 } 1063 } 1064 1065 /* Put all the ingress queues in our "ingress CGR". */ 1066 if (priv->use_ingress_cgr && 1067 (dpaa_fq->fq_type == FQ_TYPE_RX_DEFAULT || 1068 dpaa_fq->fq_type == FQ_TYPE_RX_ERROR || 1069 dpaa_fq->fq_type == FQ_TYPE_RX_PCD)) { 1070 initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CGID); 1071 initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_CGE); 1072 initfq.fqd.cgid = (u8)priv->ingress_cgr.cgrid; 1073 /* Set a fixed overhead accounting, just like for the 1074 * egress CGR. 1075 */ 1076 initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_OAC); 1077 qm_fqd_set_oac(&initfq.fqd, QM_OAC_CG); 1078 qm_fqd_set_oal(&initfq.fqd, 1079 min(sizeof(struct sk_buff) + 1080 priv->tx_headroom, 1081 (size_t)FSL_QMAN_MAX_OAL)); 1082 } 1083 1084 /* Initialization common to all ingress queues */ 1085 if (dpaa_fq->flags & QMAN_FQ_FLAG_NO_ENQUEUE) { 1086 initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CONTEXTA); 1087 initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_HOLDACTIVE | 1088 QM_FQCTRL_CTXASTASHING); 1089 initfq.fqd.context_a.stashing.exclusive = 1090 QM_STASHING_EXCL_DATA | QM_STASHING_EXCL_CTX | 1091 QM_STASHING_EXCL_ANNOTATION; 1092 qm_fqd_set_stashing(&initfq.fqd, 1, 2, 1093 DIV_ROUND_UP(sizeof(struct qman_fq), 1094 64)); 1095 } 1096 1097 err = qman_init_fq(fq, QMAN_INITFQ_FLAG_SCHED, &initfq); 1098 if (err < 0) { 1099 dev_err(dev, "qman_init_fq(%u) = %d\n", 1100 qman_fq_fqid(fq), err); 1101 qman_destroy_fq(fq); 1102 return err; 1103 } 1104 } 1105 1106 dpaa_fq->fqid = qman_fq_fqid(fq); 1107 1108 if (dpaa_fq->fq_type == FQ_TYPE_RX_DEFAULT || 1109 dpaa_fq->fq_type == FQ_TYPE_RX_PCD) { 1110 err = xdp_rxq_info_reg(&dpaa_fq->xdp_rxq, dpaa_fq->net_dev, 1111 dpaa_fq->fqid, 0); 1112 if (err) { 1113 dev_err(dev, "xdp_rxq_info_reg() = %d\n", err); 1114 return err; 1115 } 1116 1117 err = xdp_rxq_info_reg_mem_model(&dpaa_fq->xdp_rxq, 1118 MEM_TYPE_PAGE_ORDER0, NULL); 1119 if (err) { 1120 dev_err(dev, "xdp_rxq_info_reg_mem_model() = %d\n", 1121 err); 1122 xdp_rxq_info_unreg(&dpaa_fq->xdp_rxq); 1123 return err; 1124 } 1125 } 1126 1127 return 0; 1128 } 1129 1130 static int dpaa_fq_free_entry(struct device *dev, struct qman_fq *fq) 1131 { 1132 const struct dpaa_priv *priv; 1133 struct dpaa_fq *dpaa_fq; 1134 int err, error; 1135 1136 err = 0; 1137 1138 dpaa_fq = container_of(fq, struct dpaa_fq, fq_base); 1139 priv = netdev_priv(dpaa_fq->net_dev); 1140 1141 if (dpaa_fq->init) { 1142 err = qman_retire_fq(fq, NULL); 1143 if (err < 0 && netif_msg_drv(priv)) 1144 dev_err(dev, "qman_retire_fq(%u) = %d\n", 1145 qman_fq_fqid(fq), err); 1146 1147 error = qman_oos_fq(fq); 1148 if (error < 0 && netif_msg_drv(priv)) { 1149 dev_err(dev, "qman_oos_fq(%u) = %d\n", 1150 qman_fq_fqid(fq), error); 1151 if (err >= 0) 1152 err = error; 1153 } 1154 } 1155 1156 if ((dpaa_fq->fq_type == FQ_TYPE_RX_DEFAULT || 1157 dpaa_fq->fq_type == FQ_TYPE_RX_PCD) && 1158 xdp_rxq_info_is_reg(&dpaa_fq->xdp_rxq)) 1159 xdp_rxq_info_unreg(&dpaa_fq->xdp_rxq); 1160 1161 qman_destroy_fq(fq); 1162 list_del(&dpaa_fq->list); 1163 1164 return err; 1165 } 1166 1167 static int dpaa_fq_free(struct device *dev, struct list_head *list) 1168 { 1169 struct dpaa_fq *dpaa_fq, *tmp; 1170 int err, error; 1171 1172 err = 0; 1173 list_for_each_entry_safe(dpaa_fq, tmp, list, list) { 1174 error = dpaa_fq_free_entry(dev, (struct qman_fq *)dpaa_fq); 1175 if (error < 0 && err >= 0) 1176 err = error; 1177 } 1178 1179 return err; 1180 } 1181 1182 static int dpaa_eth_init_tx_port(struct fman_port *port, struct dpaa_fq *errq, 1183 struct dpaa_fq *defq, 1184 struct dpaa_buffer_layout *buf_layout) 1185 { 1186 struct fman_buffer_prefix_content buf_prefix_content; 1187 struct fman_port_params params; 1188 int err; 1189 1190 memset(¶ms, 0, sizeof(params)); 1191 memset(&buf_prefix_content, 0, sizeof(buf_prefix_content)); 1192 1193 buf_prefix_content.priv_data_size = buf_layout->priv_data_size; 1194 buf_prefix_content.pass_prs_result = true; 1195 buf_prefix_content.pass_hash_result = true; 1196 buf_prefix_content.pass_time_stamp = true; 1197 buf_prefix_content.data_align = DPAA_FD_DATA_ALIGNMENT; 1198 1199 params.specific_params.non_rx_params.err_fqid = errq->fqid; 1200 params.specific_params.non_rx_params.dflt_fqid = defq->fqid; 1201 1202 err = fman_port_config(port, ¶ms); 1203 if (err) { 1204 pr_err("%s: fman_port_config failed\n", __func__); 1205 return err; 1206 } 1207 1208 err = fman_port_cfg_buf_prefix_content(port, &buf_prefix_content); 1209 if (err) { 1210 pr_err("%s: fman_port_cfg_buf_prefix_content failed\n", 1211 __func__); 1212 return err; 1213 } 1214 1215 err = fman_port_init(port); 1216 if (err) 1217 pr_err("%s: fm_port_init failed\n", __func__); 1218 1219 return err; 1220 } 1221 1222 static int dpaa_eth_init_rx_port(struct fman_port *port, struct dpaa_bp *bp, 1223 struct dpaa_fq *errq, 1224 struct dpaa_fq *defq, struct dpaa_fq *pcdq, 1225 struct dpaa_buffer_layout *buf_layout) 1226 { 1227 struct fman_buffer_prefix_content buf_prefix_content; 1228 struct fman_port_rx_params *rx_p; 1229 struct fman_port_params params; 1230 int err; 1231 1232 memset(¶ms, 0, sizeof(params)); 1233 memset(&buf_prefix_content, 0, sizeof(buf_prefix_content)); 1234 1235 buf_prefix_content.priv_data_size = buf_layout->priv_data_size; 1236 buf_prefix_content.pass_prs_result = true; 1237 buf_prefix_content.pass_hash_result = true; 1238 buf_prefix_content.pass_time_stamp = true; 1239 buf_prefix_content.data_align = DPAA_FD_RX_DATA_ALIGNMENT; 1240 1241 rx_p = ¶ms.specific_params.rx_params; 1242 rx_p->err_fqid = errq->fqid; 1243 rx_p->dflt_fqid = defq->fqid; 1244 if (pcdq) { 1245 rx_p->pcd_base_fqid = pcdq->fqid; 1246 rx_p->pcd_fqs_count = DPAA_ETH_PCD_RXQ_NUM; 1247 } 1248 1249 rx_p->ext_buf_pools.num_of_pools_used = 1; 1250 rx_p->ext_buf_pools.ext_buf_pool[0].id = bp->bpid; 1251 rx_p->ext_buf_pools.ext_buf_pool[0].size = (u16)bp->size; 1252 1253 err = fman_port_config(port, ¶ms); 1254 if (err) { 1255 pr_err("%s: fman_port_config failed\n", __func__); 1256 return err; 1257 } 1258 1259 err = fman_port_cfg_buf_prefix_content(port, &buf_prefix_content); 1260 if (err) { 1261 pr_err("%s: fman_port_cfg_buf_prefix_content failed\n", 1262 __func__); 1263 return err; 1264 } 1265 1266 err = fman_port_init(port); 1267 if (err) 1268 pr_err("%s: fm_port_init failed\n", __func__); 1269 1270 return err; 1271 } 1272 1273 static int dpaa_eth_init_ports(struct mac_device *mac_dev, 1274 struct dpaa_bp *bp, 1275 struct fm_port_fqs *port_fqs, 1276 struct dpaa_buffer_layout *buf_layout, 1277 struct device *dev) 1278 { 1279 struct fman_port *rxport = mac_dev->port[RX]; 1280 struct fman_port *txport = mac_dev->port[TX]; 1281 int err; 1282 1283 err = dpaa_eth_init_tx_port(txport, port_fqs->tx_errq, 1284 port_fqs->tx_defq, &buf_layout[TX]); 1285 if (err) 1286 return err; 1287 1288 err = dpaa_eth_init_rx_port(rxport, bp, port_fqs->rx_errq, 1289 port_fqs->rx_defq, port_fqs->rx_pcdq, 1290 &buf_layout[RX]); 1291 1292 return err; 1293 } 1294 1295 static int dpaa_bman_release(const struct dpaa_bp *dpaa_bp, 1296 struct bm_buffer *bmb, int cnt) 1297 { 1298 int err; 1299 1300 err = bman_release(dpaa_bp->pool, bmb, cnt); 1301 /* Should never occur, address anyway to avoid leaking the buffers */ 1302 if (WARN_ON(err) && dpaa_bp->free_buf_cb) 1303 while (cnt-- > 0) 1304 dpaa_bp->free_buf_cb(dpaa_bp, &bmb[cnt]); 1305 1306 return cnt; 1307 } 1308 1309 static void dpaa_release_sgt_members(struct qm_sg_entry *sgt) 1310 { 1311 struct bm_buffer bmb[DPAA_BUFF_RELEASE_MAX]; 1312 struct dpaa_bp *dpaa_bp; 1313 int i = 0, j; 1314 1315 memset(bmb, 0, sizeof(bmb)); 1316 1317 do { 1318 dpaa_bp = dpaa_bpid2pool(sgt[i].bpid); 1319 if (!dpaa_bp) 1320 return; 1321 1322 j = 0; 1323 do { 1324 WARN_ON(qm_sg_entry_is_ext(&sgt[i])); 1325 1326 bm_buffer_set64(&bmb[j], qm_sg_entry_get64(&sgt[i])); 1327 1328 j++; i++; 1329 } while (j < ARRAY_SIZE(bmb) && 1330 !qm_sg_entry_is_final(&sgt[i - 1]) && 1331 sgt[i - 1].bpid == sgt[i].bpid); 1332 1333 dpaa_bman_release(dpaa_bp, bmb, j); 1334 } while (!qm_sg_entry_is_final(&sgt[i - 1])); 1335 } 1336 1337 static void dpaa_fd_release(const struct net_device *net_dev, 1338 const struct qm_fd *fd) 1339 { 1340 struct qm_sg_entry *sgt; 1341 struct dpaa_bp *dpaa_bp; 1342 struct bm_buffer bmb; 1343 dma_addr_t addr; 1344 void *vaddr; 1345 1346 bmb.data = 0; 1347 bm_buffer_set64(&bmb, qm_fd_addr(fd)); 1348 1349 dpaa_bp = dpaa_bpid2pool(fd->bpid); 1350 if (!dpaa_bp) 1351 return; 1352 1353 if (qm_fd_get_format(fd) == qm_fd_sg) { 1354 vaddr = phys_to_virt(qm_fd_addr(fd)); 1355 sgt = vaddr + qm_fd_get_offset(fd); 1356 1357 dma_unmap_page(dpaa_bp->priv->rx_dma_dev, qm_fd_addr(fd), 1358 DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE); 1359 1360 dpaa_release_sgt_members(sgt); 1361 1362 addr = dma_map_page(dpaa_bp->priv->rx_dma_dev, 1363 virt_to_page(vaddr), 0, DPAA_BP_RAW_SIZE, 1364 DMA_FROM_DEVICE); 1365 if (dma_mapping_error(dpaa_bp->priv->rx_dma_dev, addr)) { 1366 netdev_err(net_dev, "DMA mapping failed\n"); 1367 return; 1368 } 1369 bm_buffer_set64(&bmb, addr); 1370 } 1371 1372 dpaa_bman_release(dpaa_bp, &bmb, 1); 1373 } 1374 1375 static void count_ern(struct dpaa_percpu_priv *percpu_priv, 1376 const union qm_mr_entry *msg) 1377 { 1378 switch (msg->ern.rc & QM_MR_RC_MASK) { 1379 case QM_MR_RC_CGR_TAILDROP: 1380 percpu_priv->ern_cnt.cg_tdrop++; 1381 break; 1382 case QM_MR_RC_WRED: 1383 percpu_priv->ern_cnt.wred++; 1384 break; 1385 case QM_MR_RC_ERROR: 1386 percpu_priv->ern_cnt.err_cond++; 1387 break; 1388 case QM_MR_RC_ORPWINDOW_EARLY: 1389 percpu_priv->ern_cnt.early_window++; 1390 break; 1391 case QM_MR_RC_ORPWINDOW_LATE: 1392 percpu_priv->ern_cnt.late_window++; 1393 break; 1394 case QM_MR_RC_FQ_TAILDROP: 1395 percpu_priv->ern_cnt.fq_tdrop++; 1396 break; 1397 case QM_MR_RC_ORPWINDOW_RETIRED: 1398 percpu_priv->ern_cnt.fq_retired++; 1399 break; 1400 case QM_MR_RC_ORP_ZERO: 1401 percpu_priv->ern_cnt.orp_zero++; 1402 break; 1403 } 1404 } 1405 1406 /* Turn on HW checksum computation for this outgoing frame. 1407 * If the current protocol is not something we support in this regard 1408 * (or if the stack has already computed the SW checksum), we do nothing. 1409 * 1410 * Returns 0 if all goes well (or HW csum doesn't apply), and a negative value 1411 * otherwise. 1412 * 1413 * Note that this function may modify the fd->cmd field and the skb data buffer 1414 * (the Parse Results area). 1415 */ 1416 static int dpaa_enable_tx_csum(struct dpaa_priv *priv, 1417 struct sk_buff *skb, 1418 struct qm_fd *fd, 1419 void *parse_results) 1420 { 1421 struct fman_prs_result *parse_result; 1422 u16 ethertype = ntohs(skb->protocol); 1423 struct ipv6hdr *ipv6h = NULL; 1424 struct iphdr *iph; 1425 int retval = 0; 1426 u8 l4_proto; 1427 1428 if (skb->ip_summed != CHECKSUM_PARTIAL) 1429 return 0; 1430 1431 /* Note: L3 csum seems to be already computed in sw, but we can't choose 1432 * L4 alone from the FM configuration anyway. 1433 */ 1434 1435 /* Fill in some fields of the Parse Results array, so the FMan 1436 * can find them as if they came from the FMan Parser. 1437 */ 1438 parse_result = (struct fman_prs_result *)parse_results; 1439 1440 /* If we're dealing with VLAN, get the real Ethernet type */ 1441 if (ethertype == ETH_P_8021Q) { 1442 /* We can't always assume the MAC header is set correctly 1443 * by the stack, so reset to beginning of skb->data 1444 */ 1445 skb_reset_mac_header(skb); 1446 ethertype = ntohs(vlan_eth_hdr(skb)->h_vlan_encapsulated_proto); 1447 } 1448 1449 /* Fill in the relevant L3 parse result fields 1450 * and read the L4 protocol type 1451 */ 1452 switch (ethertype) { 1453 case ETH_P_IP: 1454 parse_result->l3r = cpu_to_be16(FM_L3_PARSE_RESULT_IPV4); 1455 iph = ip_hdr(skb); 1456 WARN_ON(!iph); 1457 l4_proto = iph->protocol; 1458 break; 1459 case ETH_P_IPV6: 1460 parse_result->l3r = cpu_to_be16(FM_L3_PARSE_RESULT_IPV6); 1461 ipv6h = ipv6_hdr(skb); 1462 WARN_ON(!ipv6h); 1463 l4_proto = ipv6h->nexthdr; 1464 break; 1465 default: 1466 /* We shouldn't even be here */ 1467 if (net_ratelimit()) 1468 netif_alert(priv, tx_err, priv->net_dev, 1469 "Can't compute HW csum for L3 proto 0x%x\n", 1470 ntohs(skb->protocol)); 1471 retval = -EIO; 1472 goto return_error; 1473 } 1474 1475 /* Fill in the relevant L4 parse result fields */ 1476 switch (l4_proto) { 1477 case IPPROTO_UDP: 1478 parse_result->l4r = FM_L4_PARSE_RESULT_UDP; 1479 break; 1480 case IPPROTO_TCP: 1481 parse_result->l4r = FM_L4_PARSE_RESULT_TCP; 1482 break; 1483 default: 1484 if (net_ratelimit()) 1485 netif_alert(priv, tx_err, priv->net_dev, 1486 "Can't compute HW csum for L4 proto 0x%x\n", 1487 l4_proto); 1488 retval = -EIO; 1489 goto return_error; 1490 } 1491 1492 /* At index 0 is IPOffset_1 as defined in the Parse Results */ 1493 parse_result->ip_off[0] = (u8)skb_network_offset(skb); 1494 parse_result->l4_off = (u8)skb_transport_offset(skb); 1495 1496 /* Enable L3 (and L4, if TCP or UDP) HW checksum. */ 1497 fd->cmd |= cpu_to_be32(FM_FD_CMD_RPD | FM_FD_CMD_DTC); 1498 1499 /* On P1023 and similar platforms fd->cmd interpretation could 1500 * be disabled by setting CONTEXT_A bit ICMD; currently this bit 1501 * is not set so we do not need to check; in the future, if/when 1502 * using context_a we need to check this bit 1503 */ 1504 1505 return_error: 1506 return retval; 1507 } 1508 1509 static int dpaa_bp_add_8_bufs(const struct dpaa_bp *dpaa_bp) 1510 { 1511 struct net_device *net_dev = dpaa_bp->priv->net_dev; 1512 struct bm_buffer bmb[8]; 1513 dma_addr_t addr; 1514 struct page *p; 1515 u8 i; 1516 1517 for (i = 0; i < 8; i++) { 1518 p = dev_alloc_pages(0); 1519 if (unlikely(!p)) { 1520 netdev_err(net_dev, "dev_alloc_pages() failed\n"); 1521 goto release_previous_buffs; 1522 } 1523 1524 addr = dma_map_page(dpaa_bp->priv->rx_dma_dev, p, 0, 1525 DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE); 1526 if (unlikely(dma_mapping_error(dpaa_bp->priv->rx_dma_dev, 1527 addr))) { 1528 netdev_err(net_dev, "DMA map failed\n"); 1529 goto release_previous_buffs; 1530 } 1531 1532 bmb[i].data = 0; 1533 bm_buffer_set64(&bmb[i], addr); 1534 } 1535 1536 release_bufs: 1537 return dpaa_bman_release(dpaa_bp, bmb, i); 1538 1539 release_previous_buffs: 1540 WARN_ONCE(1, "dpaa_eth: failed to add buffers on Rx\n"); 1541 1542 bm_buffer_set64(&bmb[i], 0); 1543 /* Avoid releasing a completely null buffer; bman_release() requires 1544 * at least one buffer. 1545 */ 1546 if (likely(i)) 1547 goto release_bufs; 1548 1549 return 0; 1550 } 1551 1552 static int dpaa_bp_seed(struct dpaa_bp *dpaa_bp) 1553 { 1554 int i; 1555 1556 /* Give each CPU an allotment of "config_count" buffers */ 1557 for_each_possible_cpu(i) { 1558 int *count_ptr = per_cpu_ptr(dpaa_bp->percpu_count, i); 1559 int j; 1560 1561 /* Although we access another CPU's counters here 1562 * we do it at boot time so it is safe 1563 */ 1564 for (j = 0; j < dpaa_bp->config_count; j += 8) 1565 *count_ptr += dpaa_bp_add_8_bufs(dpaa_bp); 1566 } 1567 return 0; 1568 } 1569 1570 /* Add buffers/(pages) for Rx processing whenever bpool count falls below 1571 * REFILL_THRESHOLD. 1572 */ 1573 static int dpaa_eth_refill_bpool(struct dpaa_bp *dpaa_bp, int *countptr) 1574 { 1575 int count = *countptr; 1576 int new_bufs; 1577 1578 if (unlikely(count < FSL_DPAA_ETH_REFILL_THRESHOLD)) { 1579 do { 1580 new_bufs = dpaa_bp_add_8_bufs(dpaa_bp); 1581 if (unlikely(!new_bufs)) { 1582 /* Avoid looping forever if we've temporarily 1583 * run out of memory. We'll try again at the 1584 * next NAPI cycle. 1585 */ 1586 break; 1587 } 1588 count += new_bufs; 1589 } while (count < FSL_DPAA_ETH_MAX_BUF_COUNT); 1590 1591 *countptr = count; 1592 if (unlikely(count < FSL_DPAA_ETH_MAX_BUF_COUNT)) 1593 return -ENOMEM; 1594 } 1595 1596 return 0; 1597 } 1598 1599 static int dpaa_eth_refill_bpools(struct dpaa_priv *priv) 1600 { 1601 struct dpaa_bp *dpaa_bp; 1602 int *countptr; 1603 1604 dpaa_bp = priv->dpaa_bp; 1605 if (!dpaa_bp) 1606 return -EINVAL; 1607 countptr = this_cpu_ptr(dpaa_bp->percpu_count); 1608 1609 return dpaa_eth_refill_bpool(dpaa_bp, countptr); 1610 } 1611 1612 /* Cleanup function for outgoing frame descriptors that were built on Tx path, 1613 * either contiguous frames or scatter/gather ones. 1614 * Skb freeing is not handled here. 1615 * 1616 * This function may be called on error paths in the Tx function, so guard 1617 * against cases when not all fd relevant fields were filled in. To avoid 1618 * reading the invalid transmission timestamp for the error paths set ts to 1619 * false. 1620 * 1621 * Return the skb backpointer, since for S/G frames the buffer containing it 1622 * gets freed here. 1623 * 1624 * No skb backpointer is set when transmitting XDP frames. Cleanup the buffer 1625 * and return NULL in this case. 1626 */ 1627 static struct sk_buff *dpaa_cleanup_tx_fd(const struct dpaa_priv *priv, 1628 const struct qm_fd *fd, bool ts) 1629 { 1630 const enum dma_data_direction dma_dir = DMA_TO_DEVICE; 1631 struct device *dev = priv->net_dev->dev.parent; 1632 struct skb_shared_hwtstamps shhwtstamps; 1633 dma_addr_t addr = qm_fd_addr(fd); 1634 void *vaddr = phys_to_virt(addr); 1635 const struct qm_sg_entry *sgt; 1636 struct dpaa_eth_swbp *swbp; 1637 struct sk_buff *skb; 1638 u64 ns; 1639 int i; 1640 1641 if (unlikely(qm_fd_get_format(fd) == qm_fd_sg)) { 1642 dma_unmap_page(priv->tx_dma_dev, addr, 1643 qm_fd_get_offset(fd) + DPAA_SGT_SIZE, 1644 dma_dir); 1645 1646 /* The sgt buffer has been allocated with netdev_alloc_frag(), 1647 * it's from lowmem. 1648 */ 1649 sgt = vaddr + qm_fd_get_offset(fd); 1650 1651 /* sgt[0] is from lowmem, was dma_map_single()-ed */ 1652 dma_unmap_single(priv->tx_dma_dev, qm_sg_addr(&sgt[0]), 1653 qm_sg_entry_get_len(&sgt[0]), dma_dir); 1654 1655 /* remaining pages were mapped with skb_frag_dma_map() */ 1656 for (i = 1; (i < DPAA_SGT_MAX_ENTRIES) && 1657 !qm_sg_entry_is_final(&sgt[i - 1]); i++) { 1658 WARN_ON(qm_sg_entry_is_ext(&sgt[i])); 1659 1660 dma_unmap_page(priv->tx_dma_dev, qm_sg_addr(&sgt[i]), 1661 qm_sg_entry_get_len(&sgt[i]), dma_dir); 1662 } 1663 } else { 1664 dma_unmap_single(priv->tx_dma_dev, addr, 1665 qm_fd_get_offset(fd) + qm_fd_get_length(fd), 1666 dma_dir); 1667 } 1668 1669 swbp = (struct dpaa_eth_swbp *)vaddr; 1670 skb = swbp->skb; 1671 1672 /* No skb backpointer is set when running XDP. An xdp_frame 1673 * backpointer is saved instead. 1674 */ 1675 if (!skb) { 1676 xdp_return_frame(swbp->xdpf); 1677 return NULL; 1678 } 1679 1680 /* DMA unmapping is required before accessing the HW provided info */ 1681 if (ts && priv->tx_tstamp && 1682 skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) { 1683 memset(&shhwtstamps, 0, sizeof(shhwtstamps)); 1684 1685 if (!fman_port_get_tstamp(priv->mac_dev->port[TX], vaddr, 1686 &ns)) { 1687 shhwtstamps.hwtstamp = ns_to_ktime(ns); 1688 skb_tstamp_tx(skb, &shhwtstamps); 1689 } else { 1690 dev_warn(dev, "fman_port_get_tstamp failed!\n"); 1691 } 1692 } 1693 1694 if (qm_fd_get_format(fd) == qm_fd_sg) 1695 /* Free the page that we allocated on Tx for the SGT */ 1696 free_pages((unsigned long)vaddr, 0); 1697 1698 return skb; 1699 } 1700 1701 static u8 rx_csum_offload(const struct dpaa_priv *priv, const struct qm_fd *fd) 1702 { 1703 /* The parser has run and performed L4 checksum validation. 1704 * We know there were no parser errors (and implicitly no 1705 * L4 csum error), otherwise we wouldn't be here. 1706 */ 1707 if ((priv->net_dev->features & NETIF_F_RXCSUM) && 1708 (be32_to_cpu(fd->status) & FM_FD_STAT_L4CV)) 1709 return CHECKSUM_UNNECESSARY; 1710 1711 /* We're here because either the parser didn't run or the L4 checksum 1712 * was not verified. This may include the case of a UDP frame with 1713 * checksum zero or an L4 proto other than TCP/UDP 1714 */ 1715 return CHECKSUM_NONE; 1716 } 1717 1718 #define PTR_IS_ALIGNED(x, a) (IS_ALIGNED((unsigned long)(x), (a))) 1719 1720 /* Build a linear skb around the received buffer. 1721 * We are guaranteed there is enough room at the end of the data buffer to 1722 * accommodate the shared info area of the skb. 1723 */ 1724 static struct sk_buff *contig_fd_to_skb(const struct dpaa_priv *priv, 1725 const struct qm_fd *fd) 1726 { 1727 ssize_t fd_off = qm_fd_get_offset(fd); 1728 dma_addr_t addr = qm_fd_addr(fd); 1729 struct dpaa_bp *dpaa_bp; 1730 struct sk_buff *skb; 1731 void *vaddr; 1732 1733 vaddr = phys_to_virt(addr); 1734 WARN_ON(!IS_ALIGNED((unsigned long)vaddr, SMP_CACHE_BYTES)); 1735 1736 dpaa_bp = dpaa_bpid2pool(fd->bpid); 1737 if (!dpaa_bp) 1738 goto free_buffer; 1739 1740 skb = build_skb(vaddr, dpaa_bp->size + 1741 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); 1742 if (WARN_ONCE(!skb, "Build skb failure on Rx\n")) 1743 goto free_buffer; 1744 skb_reserve(skb, fd_off); 1745 skb_put(skb, qm_fd_get_length(fd)); 1746 1747 skb->ip_summed = rx_csum_offload(priv, fd); 1748 1749 return skb; 1750 1751 free_buffer: 1752 free_pages((unsigned long)vaddr, 0); 1753 return NULL; 1754 } 1755 1756 /* Build an skb with the data of the first S/G entry in the linear portion and 1757 * the rest of the frame as skb fragments. 1758 * 1759 * The page fragment holding the S/G Table is recycled here. 1760 */ 1761 static struct sk_buff *sg_fd_to_skb(const struct dpaa_priv *priv, 1762 const struct qm_fd *fd) 1763 { 1764 ssize_t fd_off = qm_fd_get_offset(fd); 1765 dma_addr_t addr = qm_fd_addr(fd); 1766 const struct qm_sg_entry *sgt; 1767 struct page *page, *head_page; 1768 struct dpaa_bp *dpaa_bp; 1769 void *vaddr, *sg_vaddr; 1770 int frag_off, frag_len; 1771 struct sk_buff *skb; 1772 dma_addr_t sg_addr; 1773 int page_offset; 1774 unsigned int sz; 1775 int *count_ptr; 1776 int i, j; 1777 1778 vaddr = phys_to_virt(addr); 1779 WARN_ON(!IS_ALIGNED((unsigned long)vaddr, SMP_CACHE_BYTES)); 1780 1781 /* Iterate through the SGT entries and add data buffers to the skb */ 1782 sgt = vaddr + fd_off; 1783 skb = NULL; 1784 for (i = 0; i < DPAA_SGT_MAX_ENTRIES; i++) { 1785 /* Extension bit is not supported */ 1786 WARN_ON(qm_sg_entry_is_ext(&sgt[i])); 1787 1788 sg_addr = qm_sg_addr(&sgt[i]); 1789 sg_vaddr = phys_to_virt(sg_addr); 1790 WARN_ON(!PTR_IS_ALIGNED(sg_vaddr, SMP_CACHE_BYTES)); 1791 1792 dma_unmap_page(priv->rx_dma_dev, sg_addr, 1793 DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE); 1794 1795 /* We may use multiple Rx pools */ 1796 dpaa_bp = dpaa_bpid2pool(sgt[i].bpid); 1797 if (!dpaa_bp) 1798 goto free_buffers; 1799 1800 if (!skb) { 1801 sz = dpaa_bp->size + 1802 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1803 skb = build_skb(sg_vaddr, sz); 1804 if (WARN_ON(!skb)) 1805 goto free_buffers; 1806 1807 skb->ip_summed = rx_csum_offload(priv, fd); 1808 1809 /* Make sure forwarded skbs will have enough space 1810 * on Tx, if extra headers are added. 1811 */ 1812 WARN_ON(fd_off != priv->rx_headroom); 1813 skb_reserve(skb, fd_off); 1814 skb_put(skb, qm_sg_entry_get_len(&sgt[i])); 1815 } else { 1816 /* Not the first S/G entry; all data from buffer will 1817 * be added in an skb fragment; fragment index is offset 1818 * by one since first S/G entry was incorporated in the 1819 * linear part of the skb. 1820 * 1821 * Caution: 'page' may be a tail page. 1822 */ 1823 page = virt_to_page(sg_vaddr); 1824 head_page = virt_to_head_page(sg_vaddr); 1825 1826 /* Compute offset in (possibly tail) page */ 1827 page_offset = ((unsigned long)sg_vaddr & 1828 (PAGE_SIZE - 1)) + 1829 (page_address(page) - page_address(head_page)); 1830 /* page_offset only refers to the beginning of sgt[i]; 1831 * but the buffer itself may have an internal offset. 1832 */ 1833 frag_off = qm_sg_entry_get_off(&sgt[i]) + page_offset; 1834 frag_len = qm_sg_entry_get_len(&sgt[i]); 1835 /* skb_add_rx_frag() does no checking on the page; if 1836 * we pass it a tail page, we'll end up with 1837 * bad page accounting and eventually with segafults. 1838 */ 1839 skb_add_rx_frag(skb, i - 1, head_page, frag_off, 1840 frag_len, dpaa_bp->size); 1841 } 1842 1843 /* Update the pool count for the current {cpu x bpool} */ 1844 count_ptr = this_cpu_ptr(dpaa_bp->percpu_count); 1845 (*count_ptr)--; 1846 1847 if (qm_sg_entry_is_final(&sgt[i])) 1848 break; 1849 } 1850 WARN_ONCE(i == DPAA_SGT_MAX_ENTRIES, "No final bit on SGT\n"); 1851 1852 /* free the SG table buffer */ 1853 free_pages((unsigned long)vaddr, 0); 1854 1855 return skb; 1856 1857 free_buffers: 1858 /* free all the SG entries */ 1859 for (j = 0; j < DPAA_SGT_MAX_ENTRIES ; j++) { 1860 sg_addr = qm_sg_addr(&sgt[j]); 1861 sg_vaddr = phys_to_virt(sg_addr); 1862 /* all pages 0..i were unmaped */ 1863 if (j > i) 1864 dma_unmap_page(priv->rx_dma_dev, qm_sg_addr(&sgt[j]), 1865 DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE); 1866 free_pages((unsigned long)sg_vaddr, 0); 1867 /* counters 0..i-1 were decremented */ 1868 if (j >= i) { 1869 dpaa_bp = dpaa_bpid2pool(sgt[j].bpid); 1870 if (dpaa_bp) { 1871 count_ptr = this_cpu_ptr(dpaa_bp->percpu_count); 1872 (*count_ptr)--; 1873 } 1874 } 1875 1876 if (qm_sg_entry_is_final(&sgt[j])) 1877 break; 1878 } 1879 /* free the SGT fragment */ 1880 free_pages((unsigned long)vaddr, 0); 1881 1882 return NULL; 1883 } 1884 1885 static int skb_to_contig_fd(struct dpaa_priv *priv, 1886 struct sk_buff *skb, struct qm_fd *fd, 1887 int *offset) 1888 { 1889 struct net_device *net_dev = priv->net_dev; 1890 enum dma_data_direction dma_dir; 1891 struct dpaa_eth_swbp *swbp; 1892 unsigned char *buff_start; 1893 dma_addr_t addr; 1894 int err; 1895 1896 /* We are guaranteed to have at least tx_headroom bytes 1897 * available, so just use that for offset. 1898 */ 1899 fd->bpid = FSL_DPAA_BPID_INV; 1900 buff_start = skb->data - priv->tx_headroom; 1901 dma_dir = DMA_TO_DEVICE; 1902 1903 swbp = (struct dpaa_eth_swbp *)buff_start; 1904 swbp->skb = skb; 1905 1906 /* Enable L3/L4 hardware checksum computation. 1907 * 1908 * We must do this before dma_map_single(DMA_TO_DEVICE), because we may 1909 * need to write into the skb. 1910 */ 1911 err = dpaa_enable_tx_csum(priv, skb, fd, 1912 buff_start + DPAA_TX_PRIV_DATA_SIZE); 1913 if (unlikely(err < 0)) { 1914 if (net_ratelimit()) 1915 netif_err(priv, tx_err, net_dev, "HW csum error: %d\n", 1916 err); 1917 return err; 1918 } 1919 1920 /* Fill in the rest of the FD fields */ 1921 qm_fd_set_contig(fd, priv->tx_headroom, skb->len); 1922 fd->cmd |= cpu_to_be32(FM_FD_CMD_FCO); 1923 1924 /* Map the entire buffer size that may be seen by FMan, but no more */ 1925 addr = dma_map_single(priv->tx_dma_dev, buff_start, 1926 priv->tx_headroom + skb->len, dma_dir); 1927 if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) { 1928 if (net_ratelimit()) 1929 netif_err(priv, tx_err, net_dev, "dma_map_single() failed\n"); 1930 return -EINVAL; 1931 } 1932 qm_fd_addr_set64(fd, addr); 1933 1934 return 0; 1935 } 1936 1937 static int skb_to_sg_fd(struct dpaa_priv *priv, 1938 struct sk_buff *skb, struct qm_fd *fd) 1939 { 1940 const enum dma_data_direction dma_dir = DMA_TO_DEVICE; 1941 const int nr_frags = skb_shinfo(skb)->nr_frags; 1942 struct net_device *net_dev = priv->net_dev; 1943 struct dpaa_eth_swbp *swbp; 1944 struct qm_sg_entry *sgt; 1945 void *buff_start; 1946 skb_frag_t *frag; 1947 dma_addr_t addr; 1948 size_t frag_len; 1949 struct page *p; 1950 int i, j, err; 1951 1952 /* get a page to store the SGTable */ 1953 p = dev_alloc_pages(0); 1954 if (unlikely(!p)) { 1955 netdev_err(net_dev, "dev_alloc_pages() failed\n"); 1956 return -ENOMEM; 1957 } 1958 buff_start = page_address(p); 1959 1960 /* Enable L3/L4 hardware checksum computation. 1961 * 1962 * We must do this before dma_map_single(DMA_TO_DEVICE), because we may 1963 * need to write into the skb. 1964 */ 1965 err = dpaa_enable_tx_csum(priv, skb, fd, 1966 buff_start + DPAA_TX_PRIV_DATA_SIZE); 1967 if (unlikely(err < 0)) { 1968 if (net_ratelimit()) 1969 netif_err(priv, tx_err, net_dev, "HW csum error: %d\n", 1970 err); 1971 goto csum_failed; 1972 } 1973 1974 /* SGT[0] is used by the linear part */ 1975 sgt = (struct qm_sg_entry *)(buff_start + priv->tx_headroom); 1976 frag_len = skb_headlen(skb); 1977 qm_sg_entry_set_len(&sgt[0], frag_len); 1978 sgt[0].bpid = FSL_DPAA_BPID_INV; 1979 sgt[0].offset = 0; 1980 addr = dma_map_single(priv->tx_dma_dev, skb->data, 1981 skb_headlen(skb), dma_dir); 1982 if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) { 1983 netdev_err(priv->net_dev, "DMA mapping failed\n"); 1984 err = -EINVAL; 1985 goto sg0_map_failed; 1986 } 1987 qm_sg_entry_set64(&sgt[0], addr); 1988 1989 /* populate the rest of SGT entries */ 1990 for (i = 0; i < nr_frags; i++) { 1991 frag = &skb_shinfo(skb)->frags[i]; 1992 frag_len = skb_frag_size(frag); 1993 WARN_ON(!skb_frag_page(frag)); 1994 addr = skb_frag_dma_map(priv->tx_dma_dev, frag, 0, 1995 frag_len, dma_dir); 1996 if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) { 1997 netdev_err(priv->net_dev, "DMA mapping failed\n"); 1998 err = -EINVAL; 1999 goto sg_map_failed; 2000 } 2001 2002 qm_sg_entry_set_len(&sgt[i + 1], frag_len); 2003 sgt[i + 1].bpid = FSL_DPAA_BPID_INV; 2004 sgt[i + 1].offset = 0; 2005 2006 /* keep the offset in the address */ 2007 qm_sg_entry_set64(&sgt[i + 1], addr); 2008 } 2009 2010 /* Set the final bit in the last used entry of the SGT */ 2011 qm_sg_entry_set_f(&sgt[nr_frags], frag_len); 2012 2013 /* set fd offset to priv->tx_headroom */ 2014 qm_fd_set_sg(fd, priv->tx_headroom, skb->len); 2015 2016 /* DMA map the SGT page */ 2017 swbp = (struct dpaa_eth_swbp *)buff_start; 2018 swbp->skb = skb; 2019 2020 addr = dma_map_page(priv->tx_dma_dev, p, 0, 2021 priv->tx_headroom + DPAA_SGT_SIZE, dma_dir); 2022 if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) { 2023 netdev_err(priv->net_dev, "DMA mapping failed\n"); 2024 err = -EINVAL; 2025 goto sgt_map_failed; 2026 } 2027 2028 fd->bpid = FSL_DPAA_BPID_INV; 2029 fd->cmd |= cpu_to_be32(FM_FD_CMD_FCO); 2030 qm_fd_addr_set64(fd, addr); 2031 2032 return 0; 2033 2034 sgt_map_failed: 2035 sg_map_failed: 2036 for (j = 0; j < i; j++) 2037 dma_unmap_page(priv->tx_dma_dev, qm_sg_addr(&sgt[j]), 2038 qm_sg_entry_get_len(&sgt[j]), dma_dir); 2039 sg0_map_failed: 2040 csum_failed: 2041 free_pages((unsigned long)buff_start, 0); 2042 2043 return err; 2044 } 2045 2046 static inline int dpaa_xmit(struct dpaa_priv *priv, 2047 struct rtnl_link_stats64 *percpu_stats, 2048 int queue, 2049 struct qm_fd *fd) 2050 { 2051 struct qman_fq *egress_fq; 2052 int err, i; 2053 2054 egress_fq = priv->egress_fqs[queue]; 2055 if (fd->bpid == FSL_DPAA_BPID_INV) 2056 fd->cmd |= cpu_to_be32(qman_fq_fqid(priv->conf_fqs[queue])); 2057 2058 /* Trace this Tx fd */ 2059 trace_dpaa_tx_fd(priv->net_dev, egress_fq, fd); 2060 2061 for (i = 0; i < DPAA_ENQUEUE_RETRIES; i++) { 2062 err = qman_enqueue(egress_fq, fd); 2063 if (err != -EBUSY) 2064 break; 2065 } 2066 2067 if (unlikely(err < 0)) { 2068 percpu_stats->tx_fifo_errors++; 2069 return err; 2070 } 2071 2072 percpu_stats->tx_packets++; 2073 percpu_stats->tx_bytes += qm_fd_get_length(fd); 2074 2075 return 0; 2076 } 2077 2078 #ifdef CONFIG_DPAA_ERRATUM_A050385 2079 static int dpaa_a050385_wa_skb(struct net_device *net_dev, struct sk_buff **s) 2080 { 2081 struct dpaa_priv *priv = netdev_priv(net_dev); 2082 struct sk_buff *new_skb, *skb = *s; 2083 unsigned char *start, i; 2084 2085 /* check linear buffer alignment */ 2086 if (!PTR_IS_ALIGNED(skb->data, DPAA_A050385_ALIGN)) 2087 goto workaround; 2088 2089 /* linear buffers just need to have an aligned start */ 2090 if (!skb_is_nonlinear(skb)) 2091 return 0; 2092 2093 /* linear data size for nonlinear skbs needs to be aligned */ 2094 if (!IS_ALIGNED(skb_headlen(skb), DPAA_A050385_ALIGN)) 2095 goto workaround; 2096 2097 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2098 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2099 2100 /* all fragments need to have aligned start addresses */ 2101 if (!IS_ALIGNED(skb_frag_off(frag), DPAA_A050385_ALIGN)) 2102 goto workaround; 2103 2104 /* all but last fragment need to have aligned sizes */ 2105 if (!IS_ALIGNED(skb_frag_size(frag), DPAA_A050385_ALIGN) && 2106 (i < skb_shinfo(skb)->nr_frags - 1)) 2107 goto workaround; 2108 } 2109 2110 return 0; 2111 2112 workaround: 2113 /* copy all the skb content into a new linear buffer */ 2114 new_skb = netdev_alloc_skb(net_dev, skb->len + DPAA_A050385_ALIGN - 1 + 2115 priv->tx_headroom); 2116 if (!new_skb) 2117 return -ENOMEM; 2118 2119 /* NET_SKB_PAD bytes already reserved, adding up to tx_headroom */ 2120 skb_reserve(new_skb, priv->tx_headroom - NET_SKB_PAD); 2121 2122 /* Workaround for DPAA_A050385 requires data start to be aligned */ 2123 start = PTR_ALIGN(new_skb->data, DPAA_A050385_ALIGN); 2124 if (start - new_skb->data) 2125 skb_reserve(new_skb, start - new_skb->data); 2126 2127 skb_put(new_skb, skb->len); 2128 skb_copy_bits(skb, 0, new_skb->data, skb->len); 2129 skb_copy_header(new_skb, skb); 2130 new_skb->dev = skb->dev; 2131 2132 /* Copy relevant timestamp info from the old skb to the new */ 2133 if (priv->tx_tstamp) { 2134 skb_shinfo(new_skb)->tx_flags = skb_shinfo(skb)->tx_flags; 2135 skb_shinfo(new_skb)->hwtstamps = skb_shinfo(skb)->hwtstamps; 2136 skb_shinfo(new_skb)->tskey = skb_shinfo(skb)->tskey; 2137 if (skb->sk) 2138 skb_set_owner_w(new_skb, skb->sk); 2139 } 2140 2141 /* We move the headroom when we align it so we have to reset the 2142 * network and transport header offsets relative to the new data 2143 * pointer. The checksum offload relies on these offsets. 2144 */ 2145 skb_set_network_header(new_skb, skb_network_offset(skb)); 2146 skb_set_transport_header(new_skb, skb_transport_offset(skb)); 2147 2148 dev_kfree_skb(skb); 2149 *s = new_skb; 2150 2151 return 0; 2152 } 2153 2154 static int dpaa_a050385_wa_xdpf(struct dpaa_priv *priv, 2155 struct xdp_frame **init_xdpf) 2156 { 2157 struct xdp_frame *new_xdpf, *xdpf = *init_xdpf; 2158 void *new_buff, *aligned_data; 2159 struct page *p; 2160 u32 data_shift; 2161 int headroom; 2162 2163 /* Check the data alignment and make sure the headroom is large 2164 * enough to store the xdpf backpointer. Use an aligned headroom 2165 * value. 2166 * 2167 * Due to alignment constraints, we give XDP access to the full 256 2168 * byte frame headroom. If the XDP program uses all of it, copy the 2169 * data to a new buffer and make room for storing the backpointer. 2170 */ 2171 if (PTR_IS_ALIGNED(xdpf->data, DPAA_FD_DATA_ALIGNMENT) && 2172 xdpf->headroom >= priv->tx_headroom) { 2173 xdpf->headroom = priv->tx_headroom; 2174 return 0; 2175 } 2176 2177 /* Try to move the data inside the buffer just enough to align it and 2178 * store the xdpf backpointer. If the available headroom isn't large 2179 * enough, resort to allocating a new buffer and copying the data. 2180 */ 2181 aligned_data = PTR_ALIGN_DOWN(xdpf->data, DPAA_FD_DATA_ALIGNMENT); 2182 data_shift = xdpf->data - aligned_data; 2183 2184 /* The XDP frame's headroom needs to be large enough to accommodate 2185 * shifting the data as well as storing the xdpf backpointer. 2186 */ 2187 if (xdpf->headroom >= data_shift + priv->tx_headroom) { 2188 memmove(aligned_data, xdpf->data, xdpf->len); 2189 xdpf->data = aligned_data; 2190 xdpf->headroom = priv->tx_headroom; 2191 return 0; 2192 } 2193 2194 /* The new xdp_frame is stored in the new buffer. Reserve enough space 2195 * in the headroom for storing it along with the driver's private 2196 * info. The headroom needs to be aligned to DPAA_FD_DATA_ALIGNMENT to 2197 * guarantee the data's alignment in the buffer. 2198 */ 2199 headroom = ALIGN(sizeof(*new_xdpf) + priv->tx_headroom, 2200 DPAA_FD_DATA_ALIGNMENT); 2201 2202 /* Assure the extended headroom and data don't overflow the buffer, 2203 * while maintaining the mandatory tailroom. 2204 */ 2205 if (headroom + xdpf->len > DPAA_BP_RAW_SIZE - 2206 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) 2207 return -ENOMEM; 2208 2209 p = dev_alloc_pages(0); 2210 if (unlikely(!p)) 2211 return -ENOMEM; 2212 2213 /* Copy the data to the new buffer at a properly aligned offset */ 2214 new_buff = page_address(p); 2215 memcpy(new_buff + headroom, xdpf->data, xdpf->len); 2216 2217 /* Create an XDP frame around the new buffer in a similar fashion 2218 * to xdp_convert_buff_to_frame. 2219 */ 2220 new_xdpf = new_buff; 2221 new_xdpf->data = new_buff + headroom; 2222 new_xdpf->len = xdpf->len; 2223 new_xdpf->headroom = priv->tx_headroom; 2224 new_xdpf->frame_sz = DPAA_BP_RAW_SIZE; 2225 new_xdpf->mem.type = MEM_TYPE_PAGE_ORDER0; 2226 2227 /* Release the initial buffer */ 2228 xdp_return_frame_rx_napi(xdpf); 2229 2230 *init_xdpf = new_xdpf; 2231 return 0; 2232 } 2233 #endif 2234 2235 static netdev_tx_t 2236 dpaa_start_xmit(struct sk_buff *skb, struct net_device *net_dev) 2237 { 2238 const int queue_mapping = skb_get_queue_mapping(skb); 2239 bool nonlinear = skb_is_nonlinear(skb); 2240 struct rtnl_link_stats64 *percpu_stats; 2241 struct dpaa_percpu_priv *percpu_priv; 2242 struct netdev_queue *txq; 2243 struct dpaa_priv *priv; 2244 struct qm_fd fd; 2245 int offset = 0; 2246 int err = 0; 2247 2248 priv = netdev_priv(net_dev); 2249 percpu_priv = this_cpu_ptr(priv->percpu_priv); 2250 percpu_stats = &percpu_priv->stats; 2251 2252 qm_fd_clear_fd(&fd); 2253 2254 if (!nonlinear) { 2255 /* We're going to store the skb backpointer at the beginning 2256 * of the data buffer, so we need a privately owned skb 2257 * 2258 * We've made sure skb is not shared in dev->priv_flags, 2259 * we need to verify the skb head is not cloned 2260 */ 2261 if (skb_cow_head(skb, priv->tx_headroom)) 2262 goto enomem; 2263 2264 WARN_ON(skb_is_nonlinear(skb)); 2265 } 2266 2267 /* MAX_SKB_FRAGS is equal or larger than our dpaa_SGT_MAX_ENTRIES; 2268 * make sure we don't feed FMan with more fragments than it supports. 2269 */ 2270 if (unlikely(nonlinear && 2271 (skb_shinfo(skb)->nr_frags >= DPAA_SGT_MAX_ENTRIES))) { 2272 /* If the egress skb contains more fragments than we support 2273 * we have no choice but to linearize it ourselves. 2274 */ 2275 if (__skb_linearize(skb)) 2276 goto enomem; 2277 2278 nonlinear = skb_is_nonlinear(skb); 2279 } 2280 2281 #ifdef CONFIG_DPAA_ERRATUM_A050385 2282 if (unlikely(fman_has_errata_a050385())) { 2283 if (dpaa_a050385_wa_skb(net_dev, &skb)) 2284 goto enomem; 2285 nonlinear = skb_is_nonlinear(skb); 2286 } 2287 #endif 2288 2289 if (nonlinear) { 2290 /* Just create a S/G fd based on the skb */ 2291 err = skb_to_sg_fd(priv, skb, &fd); 2292 percpu_priv->tx_frag_skbuffs++; 2293 } else { 2294 /* Create a contig FD from this skb */ 2295 err = skb_to_contig_fd(priv, skb, &fd, &offset); 2296 } 2297 if (unlikely(err < 0)) 2298 goto skb_to_fd_failed; 2299 2300 txq = netdev_get_tx_queue(net_dev, queue_mapping); 2301 2302 /* LLTX requires to do our own update of trans_start */ 2303 txq_trans_cond_update(txq); 2304 2305 if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) { 2306 fd.cmd |= cpu_to_be32(FM_FD_CMD_UPD); 2307 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 2308 } 2309 2310 if (likely(dpaa_xmit(priv, percpu_stats, queue_mapping, &fd) == 0)) 2311 return NETDEV_TX_OK; 2312 2313 dpaa_cleanup_tx_fd(priv, &fd, false); 2314 skb_to_fd_failed: 2315 enomem: 2316 percpu_stats->tx_errors++; 2317 dev_kfree_skb(skb); 2318 return NETDEV_TX_OK; 2319 } 2320 2321 static void dpaa_rx_error(struct net_device *net_dev, 2322 const struct dpaa_priv *priv, 2323 struct dpaa_percpu_priv *percpu_priv, 2324 const struct qm_fd *fd, 2325 u32 fqid) 2326 { 2327 if (net_ratelimit()) 2328 netif_err(priv, hw, net_dev, "Err FD status = 0x%08x\n", 2329 be32_to_cpu(fd->status) & FM_FD_STAT_RX_ERRORS); 2330 2331 percpu_priv->stats.rx_errors++; 2332 2333 if (be32_to_cpu(fd->status) & FM_FD_ERR_DMA) 2334 percpu_priv->rx_errors.dme++; 2335 if (be32_to_cpu(fd->status) & FM_FD_ERR_PHYSICAL) 2336 percpu_priv->rx_errors.fpe++; 2337 if (be32_to_cpu(fd->status) & FM_FD_ERR_SIZE) 2338 percpu_priv->rx_errors.fse++; 2339 if (be32_to_cpu(fd->status) & FM_FD_ERR_PRS_HDR_ERR) 2340 percpu_priv->rx_errors.phe++; 2341 2342 dpaa_fd_release(net_dev, fd); 2343 } 2344 2345 static void dpaa_tx_error(struct net_device *net_dev, 2346 const struct dpaa_priv *priv, 2347 struct dpaa_percpu_priv *percpu_priv, 2348 const struct qm_fd *fd, 2349 u32 fqid) 2350 { 2351 struct sk_buff *skb; 2352 2353 if (net_ratelimit()) 2354 netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n", 2355 be32_to_cpu(fd->status) & FM_FD_STAT_TX_ERRORS); 2356 2357 percpu_priv->stats.tx_errors++; 2358 2359 skb = dpaa_cleanup_tx_fd(priv, fd, false); 2360 dev_kfree_skb(skb); 2361 } 2362 2363 static int dpaa_eth_poll(struct napi_struct *napi, int budget) 2364 { 2365 struct dpaa_napi_portal *np = 2366 container_of(napi, struct dpaa_napi_portal, napi); 2367 int cleaned; 2368 2369 np->xdp_act = 0; 2370 2371 cleaned = qman_p_poll_dqrr(np->p, budget); 2372 2373 if (cleaned < budget) { 2374 napi_complete_done(napi, cleaned); 2375 qman_p_irqsource_add(np->p, QM_PIRQ_DQRI); 2376 } else if (np->down) { 2377 qman_p_irqsource_add(np->p, QM_PIRQ_DQRI); 2378 } 2379 2380 if (np->xdp_act & XDP_REDIRECT) 2381 xdp_do_flush(); 2382 2383 return cleaned; 2384 } 2385 2386 static void dpaa_tx_conf(struct net_device *net_dev, 2387 const struct dpaa_priv *priv, 2388 struct dpaa_percpu_priv *percpu_priv, 2389 const struct qm_fd *fd, 2390 u32 fqid) 2391 { 2392 struct sk_buff *skb; 2393 2394 if (unlikely(be32_to_cpu(fd->status) & FM_FD_STAT_TX_ERRORS)) { 2395 if (net_ratelimit()) 2396 netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n", 2397 be32_to_cpu(fd->status) & 2398 FM_FD_STAT_TX_ERRORS); 2399 2400 percpu_priv->stats.tx_errors++; 2401 } 2402 2403 percpu_priv->tx_confirm++; 2404 2405 skb = dpaa_cleanup_tx_fd(priv, fd, true); 2406 2407 consume_skb(skb); 2408 } 2409 2410 static inline int dpaa_eth_napi_schedule(struct dpaa_percpu_priv *percpu_priv, 2411 struct qman_portal *portal, bool sched_napi) 2412 { 2413 if (sched_napi) { 2414 /* Disable QMan IRQ and invoke NAPI */ 2415 qman_p_irqsource_remove(portal, QM_PIRQ_DQRI); 2416 2417 percpu_priv->np.p = portal; 2418 napi_schedule(&percpu_priv->np.napi); 2419 percpu_priv->in_interrupt++; 2420 return 1; 2421 } 2422 return 0; 2423 } 2424 2425 static enum qman_cb_dqrr_result rx_error_dqrr(struct qman_portal *portal, 2426 struct qman_fq *fq, 2427 const struct qm_dqrr_entry *dq, 2428 bool sched_napi) 2429 { 2430 struct dpaa_fq *dpaa_fq = container_of(fq, struct dpaa_fq, fq_base); 2431 struct dpaa_percpu_priv *percpu_priv; 2432 struct net_device *net_dev; 2433 struct dpaa_bp *dpaa_bp; 2434 struct dpaa_priv *priv; 2435 2436 net_dev = dpaa_fq->net_dev; 2437 priv = netdev_priv(net_dev); 2438 dpaa_bp = dpaa_bpid2pool(dq->fd.bpid); 2439 if (!dpaa_bp) 2440 return qman_cb_dqrr_consume; 2441 2442 percpu_priv = this_cpu_ptr(priv->percpu_priv); 2443 2444 if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi)) 2445 return qman_cb_dqrr_stop; 2446 2447 dpaa_eth_refill_bpools(priv); 2448 dpaa_rx_error(net_dev, priv, percpu_priv, &dq->fd, fq->fqid); 2449 2450 return qman_cb_dqrr_consume; 2451 } 2452 2453 static int dpaa_xdp_xmit_frame(struct net_device *net_dev, 2454 struct xdp_frame *xdpf) 2455 { 2456 struct dpaa_priv *priv = netdev_priv(net_dev); 2457 struct rtnl_link_stats64 *percpu_stats; 2458 struct dpaa_percpu_priv *percpu_priv; 2459 struct dpaa_eth_swbp *swbp; 2460 struct netdev_queue *txq; 2461 void *buff_start; 2462 struct qm_fd fd; 2463 dma_addr_t addr; 2464 int err; 2465 2466 percpu_priv = this_cpu_ptr(priv->percpu_priv); 2467 percpu_stats = &percpu_priv->stats; 2468 2469 #ifdef CONFIG_DPAA_ERRATUM_A050385 2470 if (unlikely(fman_has_errata_a050385())) { 2471 if (dpaa_a050385_wa_xdpf(priv, &xdpf)) { 2472 err = -ENOMEM; 2473 goto out_error; 2474 } 2475 } 2476 #endif 2477 2478 if (xdpf->headroom < DPAA_TX_PRIV_DATA_SIZE) { 2479 err = -EINVAL; 2480 goto out_error; 2481 } 2482 2483 buff_start = xdpf->data - xdpf->headroom; 2484 2485 /* Leave empty the skb backpointer at the start of the buffer. 2486 * Save the XDP frame for easy cleanup on confirmation. 2487 */ 2488 swbp = (struct dpaa_eth_swbp *)buff_start; 2489 swbp->skb = NULL; 2490 swbp->xdpf = xdpf; 2491 2492 qm_fd_clear_fd(&fd); 2493 fd.bpid = FSL_DPAA_BPID_INV; 2494 fd.cmd |= cpu_to_be32(FM_FD_CMD_FCO); 2495 qm_fd_set_contig(&fd, xdpf->headroom, xdpf->len); 2496 2497 addr = dma_map_single(priv->tx_dma_dev, buff_start, 2498 xdpf->headroom + xdpf->len, 2499 DMA_TO_DEVICE); 2500 if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) { 2501 err = -EINVAL; 2502 goto out_error; 2503 } 2504 2505 qm_fd_addr_set64(&fd, addr); 2506 2507 /* Bump the trans_start */ 2508 txq = netdev_get_tx_queue(net_dev, smp_processor_id()); 2509 txq_trans_cond_update(txq); 2510 2511 err = dpaa_xmit(priv, percpu_stats, smp_processor_id(), &fd); 2512 if (err) { 2513 dma_unmap_single(priv->tx_dma_dev, addr, 2514 qm_fd_get_offset(&fd) + qm_fd_get_length(&fd), 2515 DMA_TO_DEVICE); 2516 goto out_error; 2517 } 2518 2519 return 0; 2520 2521 out_error: 2522 percpu_stats->tx_errors++; 2523 return err; 2524 } 2525 2526 static u32 dpaa_run_xdp(struct dpaa_priv *priv, struct qm_fd *fd, void *vaddr, 2527 struct dpaa_fq *dpaa_fq, unsigned int *xdp_meta_len) 2528 { 2529 ssize_t fd_off = qm_fd_get_offset(fd); 2530 struct bpf_prog *xdp_prog; 2531 struct xdp_frame *xdpf; 2532 struct xdp_buff xdp; 2533 u32 xdp_act; 2534 int err; 2535 2536 xdp_prog = READ_ONCE(priv->xdp_prog); 2537 if (!xdp_prog) 2538 return XDP_PASS; 2539 2540 xdp_init_buff(&xdp, DPAA_BP_RAW_SIZE - DPAA_TX_PRIV_DATA_SIZE, 2541 &dpaa_fq->xdp_rxq); 2542 xdp_prepare_buff(&xdp, vaddr + fd_off - XDP_PACKET_HEADROOM, 2543 XDP_PACKET_HEADROOM, qm_fd_get_length(fd), true); 2544 2545 /* We reserve a fixed headroom of 256 bytes under the erratum and we 2546 * offer it all to XDP programs to use. If no room is left for the 2547 * xdpf backpointer on TX, we will need to copy the data. 2548 * Disable metadata support since data realignments might be required 2549 * and the information can be lost. 2550 */ 2551 #ifdef CONFIG_DPAA_ERRATUM_A050385 2552 if (unlikely(fman_has_errata_a050385())) { 2553 xdp_set_data_meta_invalid(&xdp); 2554 xdp.data_hard_start = vaddr; 2555 xdp.frame_sz = DPAA_BP_RAW_SIZE; 2556 } 2557 #endif 2558 2559 xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp); 2560 2561 /* Update the length and the offset of the FD */ 2562 qm_fd_set_contig(fd, xdp.data - vaddr, xdp.data_end - xdp.data); 2563 2564 switch (xdp_act) { 2565 case XDP_PASS: 2566 #ifdef CONFIG_DPAA_ERRATUM_A050385 2567 *xdp_meta_len = xdp_data_meta_unsupported(&xdp) ? 0 : 2568 xdp.data - xdp.data_meta; 2569 #else 2570 *xdp_meta_len = xdp.data - xdp.data_meta; 2571 #endif 2572 break; 2573 case XDP_TX: 2574 /* We can access the full headroom when sending the frame 2575 * back out 2576 */ 2577 xdp.data_hard_start = vaddr; 2578 xdp.frame_sz = DPAA_BP_RAW_SIZE; 2579 xdpf = xdp_convert_buff_to_frame(&xdp); 2580 if (unlikely(!xdpf)) { 2581 free_pages((unsigned long)vaddr, 0); 2582 break; 2583 } 2584 2585 if (dpaa_xdp_xmit_frame(priv->net_dev, xdpf)) 2586 xdp_return_frame_rx_napi(xdpf); 2587 2588 break; 2589 case XDP_REDIRECT: 2590 /* Allow redirect to use the full headroom */ 2591 xdp.data_hard_start = vaddr; 2592 xdp.frame_sz = DPAA_BP_RAW_SIZE; 2593 2594 err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog); 2595 if (err) { 2596 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act); 2597 free_pages((unsigned long)vaddr, 0); 2598 } 2599 break; 2600 default: 2601 bpf_warn_invalid_xdp_action(priv->net_dev, xdp_prog, xdp_act); 2602 fallthrough; 2603 case XDP_ABORTED: 2604 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act); 2605 fallthrough; 2606 case XDP_DROP: 2607 /* Free the buffer */ 2608 free_pages((unsigned long)vaddr, 0); 2609 break; 2610 } 2611 2612 return xdp_act; 2613 } 2614 2615 static enum qman_cb_dqrr_result rx_default_dqrr(struct qman_portal *portal, 2616 struct qman_fq *fq, 2617 const struct qm_dqrr_entry *dq, 2618 bool sched_napi) 2619 { 2620 bool ts_valid = false, hash_valid = false; 2621 struct skb_shared_hwtstamps *shhwtstamps; 2622 unsigned int skb_len, xdp_meta_len = 0; 2623 struct rtnl_link_stats64 *percpu_stats; 2624 struct dpaa_percpu_priv *percpu_priv; 2625 const struct qm_fd *fd = &dq->fd; 2626 dma_addr_t addr = qm_fd_addr(fd); 2627 struct dpaa_napi_portal *np; 2628 enum qm_fd_format fd_format; 2629 struct net_device *net_dev; 2630 u32 fd_status, hash_offset; 2631 struct qm_sg_entry *sgt; 2632 struct dpaa_bp *dpaa_bp; 2633 struct dpaa_fq *dpaa_fq; 2634 struct dpaa_priv *priv; 2635 struct sk_buff *skb; 2636 int *count_ptr; 2637 u32 xdp_act; 2638 void *vaddr; 2639 u32 hash; 2640 u64 ns; 2641 2642 dpaa_fq = container_of(fq, struct dpaa_fq, fq_base); 2643 fd_status = be32_to_cpu(fd->status); 2644 fd_format = qm_fd_get_format(fd); 2645 net_dev = dpaa_fq->net_dev; 2646 priv = netdev_priv(net_dev); 2647 dpaa_bp = dpaa_bpid2pool(dq->fd.bpid); 2648 if (!dpaa_bp) 2649 return qman_cb_dqrr_consume; 2650 2651 /* Trace the Rx fd */ 2652 trace_dpaa_rx_fd(net_dev, fq, &dq->fd); 2653 2654 percpu_priv = this_cpu_ptr(priv->percpu_priv); 2655 percpu_stats = &percpu_priv->stats; 2656 np = &percpu_priv->np; 2657 2658 if (unlikely(dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi))) 2659 return qman_cb_dqrr_stop; 2660 2661 /* Make sure we didn't run out of buffers */ 2662 if (unlikely(dpaa_eth_refill_bpools(priv))) { 2663 /* Unable to refill the buffer pool due to insufficient 2664 * system memory. Just release the frame back into the pool, 2665 * otherwise we'll soon end up with an empty buffer pool. 2666 */ 2667 dpaa_fd_release(net_dev, &dq->fd); 2668 return qman_cb_dqrr_consume; 2669 } 2670 2671 if (unlikely(fd_status & FM_FD_STAT_RX_ERRORS) != 0) { 2672 if (net_ratelimit()) 2673 netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n", 2674 fd_status & FM_FD_STAT_RX_ERRORS); 2675 2676 percpu_stats->rx_errors++; 2677 dpaa_fd_release(net_dev, fd); 2678 return qman_cb_dqrr_consume; 2679 } 2680 2681 dma_unmap_page(dpaa_bp->priv->rx_dma_dev, addr, DPAA_BP_RAW_SIZE, 2682 DMA_FROM_DEVICE); 2683 2684 /* prefetch the first 64 bytes of the frame or the SGT start */ 2685 vaddr = phys_to_virt(addr); 2686 prefetch(vaddr + qm_fd_get_offset(fd)); 2687 2688 /* The only FD types that we may receive are contig and S/G */ 2689 WARN_ON((fd_format != qm_fd_contig) && (fd_format != qm_fd_sg)); 2690 2691 /* Account for either the contig buffer or the SGT buffer (depending on 2692 * which case we were in) having been removed from the pool. 2693 */ 2694 count_ptr = this_cpu_ptr(dpaa_bp->percpu_count); 2695 (*count_ptr)--; 2696 2697 /* Extract the timestamp stored in the headroom before running XDP */ 2698 if (priv->rx_tstamp) { 2699 if (!fman_port_get_tstamp(priv->mac_dev->port[RX], vaddr, &ns)) 2700 ts_valid = true; 2701 else 2702 WARN_ONCE(1, "fman_port_get_tstamp failed!\n"); 2703 } 2704 2705 /* Extract the hash stored in the headroom before running XDP */ 2706 if (net_dev->features & NETIF_F_RXHASH && priv->keygen_in_use && 2707 !fman_port_get_hash_result_offset(priv->mac_dev->port[RX], 2708 &hash_offset)) { 2709 hash = be32_to_cpu(*(u32 *)(vaddr + hash_offset)); 2710 hash_valid = true; 2711 } 2712 2713 if (likely(fd_format == qm_fd_contig)) { 2714 xdp_act = dpaa_run_xdp(priv, (struct qm_fd *)fd, vaddr, 2715 dpaa_fq, &xdp_meta_len); 2716 np->xdp_act |= xdp_act; 2717 if (xdp_act != XDP_PASS) { 2718 percpu_stats->rx_packets++; 2719 percpu_stats->rx_bytes += qm_fd_get_length(fd); 2720 return qman_cb_dqrr_consume; 2721 } 2722 skb = contig_fd_to_skb(priv, fd); 2723 } else { 2724 /* XDP doesn't support S/G frames. Return the fragments to the 2725 * buffer pool and release the SGT. 2726 */ 2727 if (READ_ONCE(priv->xdp_prog)) { 2728 WARN_ONCE(1, "S/G frames not supported under XDP\n"); 2729 sgt = vaddr + qm_fd_get_offset(fd); 2730 dpaa_release_sgt_members(sgt); 2731 free_pages((unsigned long)vaddr, 0); 2732 return qman_cb_dqrr_consume; 2733 } 2734 skb = sg_fd_to_skb(priv, fd); 2735 } 2736 if (!skb) 2737 return qman_cb_dqrr_consume; 2738 2739 if (xdp_meta_len) 2740 skb_metadata_set(skb, xdp_meta_len); 2741 2742 /* Set the previously extracted timestamp */ 2743 if (ts_valid) { 2744 shhwtstamps = skb_hwtstamps(skb); 2745 memset(shhwtstamps, 0, sizeof(*shhwtstamps)); 2746 shhwtstamps->hwtstamp = ns_to_ktime(ns); 2747 } 2748 2749 skb->protocol = eth_type_trans(skb, net_dev); 2750 2751 /* Set the previously extracted hash */ 2752 if (hash_valid) { 2753 enum pkt_hash_types type; 2754 2755 /* if L4 exists, it was used in the hash generation */ 2756 type = be32_to_cpu(fd->status) & FM_FD_STAT_L4CV ? 2757 PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3; 2758 skb_set_hash(skb, hash, type); 2759 } 2760 2761 skb_len = skb->len; 2762 2763 if (unlikely(netif_receive_skb(skb) == NET_RX_DROP)) { 2764 percpu_stats->rx_dropped++; 2765 return qman_cb_dqrr_consume; 2766 } 2767 2768 percpu_stats->rx_packets++; 2769 percpu_stats->rx_bytes += skb_len; 2770 2771 return qman_cb_dqrr_consume; 2772 } 2773 2774 static enum qman_cb_dqrr_result conf_error_dqrr(struct qman_portal *portal, 2775 struct qman_fq *fq, 2776 const struct qm_dqrr_entry *dq, 2777 bool sched_napi) 2778 { 2779 struct dpaa_percpu_priv *percpu_priv; 2780 struct net_device *net_dev; 2781 struct dpaa_priv *priv; 2782 2783 net_dev = ((struct dpaa_fq *)fq)->net_dev; 2784 priv = netdev_priv(net_dev); 2785 2786 percpu_priv = this_cpu_ptr(priv->percpu_priv); 2787 2788 if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi)) 2789 return qman_cb_dqrr_stop; 2790 2791 dpaa_tx_error(net_dev, priv, percpu_priv, &dq->fd, fq->fqid); 2792 2793 return qman_cb_dqrr_consume; 2794 } 2795 2796 static enum qman_cb_dqrr_result conf_dflt_dqrr(struct qman_portal *portal, 2797 struct qman_fq *fq, 2798 const struct qm_dqrr_entry *dq, 2799 bool sched_napi) 2800 { 2801 struct dpaa_percpu_priv *percpu_priv; 2802 struct net_device *net_dev; 2803 struct dpaa_priv *priv; 2804 2805 net_dev = ((struct dpaa_fq *)fq)->net_dev; 2806 priv = netdev_priv(net_dev); 2807 2808 /* Trace the fd */ 2809 trace_dpaa_tx_conf_fd(net_dev, fq, &dq->fd); 2810 2811 percpu_priv = this_cpu_ptr(priv->percpu_priv); 2812 2813 if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi)) 2814 return qman_cb_dqrr_stop; 2815 2816 dpaa_tx_conf(net_dev, priv, percpu_priv, &dq->fd, fq->fqid); 2817 2818 return qman_cb_dqrr_consume; 2819 } 2820 2821 static void egress_ern(struct qman_portal *portal, 2822 struct qman_fq *fq, 2823 const union qm_mr_entry *msg) 2824 { 2825 const struct qm_fd *fd = &msg->ern.fd; 2826 struct dpaa_percpu_priv *percpu_priv; 2827 const struct dpaa_priv *priv; 2828 struct net_device *net_dev; 2829 struct sk_buff *skb; 2830 2831 net_dev = ((struct dpaa_fq *)fq)->net_dev; 2832 priv = netdev_priv(net_dev); 2833 percpu_priv = this_cpu_ptr(priv->percpu_priv); 2834 2835 percpu_priv->stats.tx_dropped++; 2836 percpu_priv->stats.tx_fifo_errors++; 2837 count_ern(percpu_priv, msg); 2838 2839 skb = dpaa_cleanup_tx_fd(priv, fd, false); 2840 dev_kfree_skb_any(skb); 2841 } 2842 2843 static const struct dpaa_fq_cbs dpaa_fq_cbs = { 2844 .rx_defq = { .cb = { .dqrr = rx_default_dqrr } }, 2845 .tx_defq = { .cb = { .dqrr = conf_dflt_dqrr } }, 2846 .rx_errq = { .cb = { .dqrr = rx_error_dqrr } }, 2847 .tx_errq = { .cb = { .dqrr = conf_error_dqrr } }, 2848 .egress_ern = { .cb = { .ern = egress_ern } } 2849 }; 2850 2851 static void dpaa_eth_napi_enable(struct dpaa_priv *priv) 2852 { 2853 struct dpaa_percpu_priv *percpu_priv; 2854 int i; 2855 2856 for_each_online_cpu(i) { 2857 percpu_priv = per_cpu_ptr(priv->percpu_priv, i); 2858 2859 percpu_priv->np.down = false; 2860 napi_enable(&percpu_priv->np.napi); 2861 } 2862 } 2863 2864 static void dpaa_eth_napi_disable(struct dpaa_priv *priv) 2865 { 2866 struct dpaa_percpu_priv *percpu_priv; 2867 int i; 2868 2869 for_each_online_cpu(i) { 2870 percpu_priv = per_cpu_ptr(priv->percpu_priv, i); 2871 2872 percpu_priv->np.down = true; 2873 napi_disable(&percpu_priv->np.napi); 2874 } 2875 } 2876 2877 static void dpaa_adjust_link(struct net_device *net_dev) 2878 { 2879 struct mac_device *mac_dev; 2880 struct dpaa_priv *priv; 2881 2882 priv = netdev_priv(net_dev); 2883 mac_dev = priv->mac_dev; 2884 mac_dev->adjust_link(mac_dev); 2885 } 2886 2887 /* The Aquantia PHYs are capable of performing rate adaptation */ 2888 #define PHY_VEND_AQUANTIA 0x03a1b400 2889 #define PHY_VEND_AQUANTIA2 0x31c31c00 2890 2891 static int dpaa_phy_init(struct net_device *net_dev) 2892 { 2893 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 2894 struct mac_device *mac_dev; 2895 struct phy_device *phy_dev; 2896 struct dpaa_priv *priv; 2897 u32 phy_vendor; 2898 2899 priv = netdev_priv(net_dev); 2900 mac_dev = priv->mac_dev; 2901 2902 phy_dev = of_phy_connect(net_dev, mac_dev->phy_node, 2903 &dpaa_adjust_link, 0, 2904 mac_dev->phy_if); 2905 if (!phy_dev) { 2906 netif_err(priv, ifup, net_dev, "init_phy() failed\n"); 2907 return -ENODEV; 2908 } 2909 2910 phy_vendor = phy_dev->drv->phy_id & GENMASK(31, 10); 2911 /* Unless the PHY is capable of rate adaptation */ 2912 if (mac_dev->phy_if != PHY_INTERFACE_MODE_XGMII || 2913 (phy_vendor != PHY_VEND_AQUANTIA && 2914 phy_vendor != PHY_VEND_AQUANTIA2)) { 2915 /* remove any features not supported by the controller */ 2916 ethtool_convert_legacy_u32_to_link_mode(mask, 2917 mac_dev->if_support); 2918 linkmode_and(phy_dev->supported, phy_dev->supported, mask); 2919 } 2920 2921 phy_support_asym_pause(phy_dev); 2922 2923 mac_dev->phy_dev = phy_dev; 2924 net_dev->phydev = phy_dev; 2925 2926 return 0; 2927 } 2928 2929 static int dpaa_open(struct net_device *net_dev) 2930 { 2931 struct mac_device *mac_dev; 2932 struct dpaa_priv *priv; 2933 int err, i; 2934 2935 priv = netdev_priv(net_dev); 2936 mac_dev = priv->mac_dev; 2937 dpaa_eth_napi_enable(priv); 2938 2939 err = dpaa_phy_init(net_dev); 2940 if (err) 2941 goto phy_init_failed; 2942 2943 for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) { 2944 err = fman_port_enable(mac_dev->port[i]); 2945 if (err) 2946 goto mac_start_failed; 2947 } 2948 2949 err = priv->mac_dev->start(mac_dev); 2950 if (err < 0) { 2951 netif_err(priv, ifup, net_dev, "mac_dev->start() = %d\n", err); 2952 goto mac_start_failed; 2953 } 2954 2955 netif_tx_start_all_queues(net_dev); 2956 2957 return 0; 2958 2959 mac_start_failed: 2960 for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) 2961 fman_port_disable(mac_dev->port[i]); 2962 2963 phy_init_failed: 2964 dpaa_eth_napi_disable(priv); 2965 2966 return err; 2967 } 2968 2969 static int dpaa_eth_stop(struct net_device *net_dev) 2970 { 2971 struct dpaa_priv *priv; 2972 int err; 2973 2974 err = dpaa_stop(net_dev); 2975 2976 priv = netdev_priv(net_dev); 2977 dpaa_eth_napi_disable(priv); 2978 2979 return err; 2980 } 2981 2982 static bool xdp_validate_mtu(struct dpaa_priv *priv, int mtu) 2983 { 2984 int max_contig_data = priv->dpaa_bp->size - priv->rx_headroom; 2985 2986 /* We do not support S/G fragments when XDP is enabled. 2987 * Limit the MTU in relation to the buffer size. 2988 */ 2989 if (mtu + VLAN_ETH_HLEN + ETH_FCS_LEN > max_contig_data) { 2990 dev_warn(priv->net_dev->dev.parent, 2991 "The maximum MTU for XDP is %d\n", 2992 max_contig_data - VLAN_ETH_HLEN - ETH_FCS_LEN); 2993 return false; 2994 } 2995 2996 return true; 2997 } 2998 2999 static int dpaa_change_mtu(struct net_device *net_dev, int new_mtu) 3000 { 3001 struct dpaa_priv *priv = netdev_priv(net_dev); 3002 3003 if (priv->xdp_prog && !xdp_validate_mtu(priv, new_mtu)) 3004 return -EINVAL; 3005 3006 net_dev->mtu = new_mtu; 3007 return 0; 3008 } 3009 3010 static int dpaa_setup_xdp(struct net_device *net_dev, struct netdev_bpf *bpf) 3011 { 3012 struct dpaa_priv *priv = netdev_priv(net_dev); 3013 struct bpf_prog *old_prog; 3014 int err; 3015 bool up; 3016 3017 /* S/G fragments are not supported in XDP-mode */ 3018 if (bpf->prog && !xdp_validate_mtu(priv, net_dev->mtu)) { 3019 NL_SET_ERR_MSG_MOD(bpf->extack, "MTU too large for XDP"); 3020 return -EINVAL; 3021 } 3022 3023 up = netif_running(net_dev); 3024 3025 if (up) 3026 dpaa_eth_stop(net_dev); 3027 3028 old_prog = xchg(&priv->xdp_prog, bpf->prog); 3029 if (old_prog) 3030 bpf_prog_put(old_prog); 3031 3032 if (up) { 3033 err = dpaa_open(net_dev); 3034 if (err) { 3035 NL_SET_ERR_MSG_MOD(bpf->extack, "dpaa_open() failed"); 3036 return err; 3037 } 3038 } 3039 3040 return 0; 3041 } 3042 3043 static int dpaa_xdp(struct net_device *net_dev, struct netdev_bpf *xdp) 3044 { 3045 switch (xdp->command) { 3046 case XDP_SETUP_PROG: 3047 return dpaa_setup_xdp(net_dev, xdp); 3048 default: 3049 return -EINVAL; 3050 } 3051 } 3052 3053 static int dpaa_xdp_xmit(struct net_device *net_dev, int n, 3054 struct xdp_frame **frames, u32 flags) 3055 { 3056 struct xdp_frame *xdpf; 3057 int i, nxmit = 0; 3058 3059 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 3060 return -EINVAL; 3061 3062 if (!netif_running(net_dev)) 3063 return -ENETDOWN; 3064 3065 for (i = 0; i < n; i++) { 3066 xdpf = frames[i]; 3067 if (dpaa_xdp_xmit_frame(net_dev, xdpf)) 3068 break; 3069 nxmit++; 3070 } 3071 3072 return nxmit; 3073 } 3074 3075 static int dpaa_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 3076 { 3077 struct dpaa_priv *priv = netdev_priv(dev); 3078 struct hwtstamp_config config; 3079 3080 if (copy_from_user(&config, rq->ifr_data, sizeof(config))) 3081 return -EFAULT; 3082 3083 switch (config.tx_type) { 3084 case HWTSTAMP_TX_OFF: 3085 /* Couldn't disable rx/tx timestamping separately. 3086 * Do nothing here. 3087 */ 3088 priv->tx_tstamp = false; 3089 break; 3090 case HWTSTAMP_TX_ON: 3091 priv->mac_dev->set_tstamp(priv->mac_dev->fman_mac, true); 3092 priv->tx_tstamp = true; 3093 break; 3094 default: 3095 return -ERANGE; 3096 } 3097 3098 if (config.rx_filter == HWTSTAMP_FILTER_NONE) { 3099 /* Couldn't disable rx/tx timestamping separately. 3100 * Do nothing here. 3101 */ 3102 priv->rx_tstamp = false; 3103 } else { 3104 priv->mac_dev->set_tstamp(priv->mac_dev->fman_mac, true); 3105 priv->rx_tstamp = true; 3106 /* TS is set for all frame types, not only those requested */ 3107 config.rx_filter = HWTSTAMP_FILTER_ALL; 3108 } 3109 3110 return copy_to_user(rq->ifr_data, &config, sizeof(config)) ? 3111 -EFAULT : 0; 3112 } 3113 3114 static int dpaa_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd) 3115 { 3116 int ret = -EINVAL; 3117 3118 if (cmd == SIOCGMIIREG) { 3119 if (net_dev->phydev) 3120 return phy_mii_ioctl(net_dev->phydev, rq, cmd); 3121 } 3122 3123 if (cmd == SIOCSHWTSTAMP) 3124 return dpaa_ts_ioctl(net_dev, rq, cmd); 3125 3126 return ret; 3127 } 3128 3129 static const struct net_device_ops dpaa_ops = { 3130 .ndo_open = dpaa_open, 3131 .ndo_start_xmit = dpaa_start_xmit, 3132 .ndo_stop = dpaa_eth_stop, 3133 .ndo_tx_timeout = dpaa_tx_timeout, 3134 .ndo_get_stats64 = dpaa_get_stats64, 3135 .ndo_change_carrier = fixed_phy_change_carrier, 3136 .ndo_set_mac_address = dpaa_set_mac_address, 3137 .ndo_validate_addr = eth_validate_addr, 3138 .ndo_set_rx_mode = dpaa_set_rx_mode, 3139 .ndo_eth_ioctl = dpaa_ioctl, 3140 .ndo_setup_tc = dpaa_setup_tc, 3141 .ndo_change_mtu = dpaa_change_mtu, 3142 .ndo_bpf = dpaa_xdp, 3143 .ndo_xdp_xmit = dpaa_xdp_xmit, 3144 }; 3145 3146 static int dpaa_napi_add(struct net_device *net_dev) 3147 { 3148 struct dpaa_priv *priv = netdev_priv(net_dev); 3149 struct dpaa_percpu_priv *percpu_priv; 3150 int cpu; 3151 3152 for_each_possible_cpu(cpu) { 3153 percpu_priv = per_cpu_ptr(priv->percpu_priv, cpu); 3154 3155 netif_napi_add(net_dev, &percpu_priv->np.napi, 3156 dpaa_eth_poll, NAPI_POLL_WEIGHT); 3157 } 3158 3159 return 0; 3160 } 3161 3162 static void dpaa_napi_del(struct net_device *net_dev) 3163 { 3164 struct dpaa_priv *priv = netdev_priv(net_dev); 3165 struct dpaa_percpu_priv *percpu_priv; 3166 int cpu; 3167 3168 for_each_possible_cpu(cpu) { 3169 percpu_priv = per_cpu_ptr(priv->percpu_priv, cpu); 3170 3171 netif_napi_del(&percpu_priv->np.napi); 3172 } 3173 } 3174 3175 static inline void dpaa_bp_free_pf(const struct dpaa_bp *bp, 3176 struct bm_buffer *bmb) 3177 { 3178 dma_addr_t addr = bm_buf_addr(bmb); 3179 3180 dma_unmap_page(bp->priv->rx_dma_dev, addr, DPAA_BP_RAW_SIZE, 3181 DMA_FROM_DEVICE); 3182 3183 skb_free_frag(phys_to_virt(addr)); 3184 } 3185 3186 /* Alloc the dpaa_bp struct and configure default values */ 3187 static struct dpaa_bp *dpaa_bp_alloc(struct device *dev) 3188 { 3189 struct dpaa_bp *dpaa_bp; 3190 3191 dpaa_bp = devm_kzalloc(dev, sizeof(*dpaa_bp), GFP_KERNEL); 3192 if (!dpaa_bp) 3193 return ERR_PTR(-ENOMEM); 3194 3195 dpaa_bp->bpid = FSL_DPAA_BPID_INV; 3196 dpaa_bp->percpu_count = devm_alloc_percpu(dev, *dpaa_bp->percpu_count); 3197 if (!dpaa_bp->percpu_count) 3198 return ERR_PTR(-ENOMEM); 3199 3200 dpaa_bp->config_count = FSL_DPAA_ETH_MAX_BUF_COUNT; 3201 3202 dpaa_bp->seed_cb = dpaa_bp_seed; 3203 dpaa_bp->free_buf_cb = dpaa_bp_free_pf; 3204 3205 return dpaa_bp; 3206 } 3207 3208 /* Place all ingress FQs (Rx Default, Rx Error) in a dedicated CGR. 3209 * We won't be sending congestion notifications to FMan; for now, we just use 3210 * this CGR to generate enqueue rejections to FMan in order to drop the frames 3211 * before they reach our ingress queues and eat up memory. 3212 */ 3213 static int dpaa_ingress_cgr_init(struct dpaa_priv *priv) 3214 { 3215 struct qm_mcc_initcgr initcgr; 3216 u32 cs_th; 3217 int err; 3218 3219 err = qman_alloc_cgrid(&priv->ingress_cgr.cgrid); 3220 if (err < 0) { 3221 if (netif_msg_drv(priv)) 3222 pr_err("Error %d allocating CGR ID\n", err); 3223 goto out_error; 3224 } 3225 3226 /* Enable CS TD, but disable Congestion State Change Notifications. */ 3227 memset(&initcgr, 0, sizeof(initcgr)); 3228 initcgr.we_mask = cpu_to_be16(QM_CGR_WE_CS_THRES); 3229 initcgr.cgr.cscn_en = QM_CGR_EN; 3230 cs_th = DPAA_INGRESS_CS_THRESHOLD; 3231 qm_cgr_cs_thres_set64(&initcgr.cgr.cs_thres, cs_th, 1); 3232 3233 initcgr.we_mask |= cpu_to_be16(QM_CGR_WE_CSTD_EN); 3234 initcgr.cgr.cstd_en = QM_CGR_EN; 3235 3236 /* This CGR will be associated with the SWP affined to the current CPU. 3237 * However, we'll place all our ingress FQs in it. 3238 */ 3239 err = qman_create_cgr(&priv->ingress_cgr, QMAN_CGR_FLAG_USE_INIT, 3240 &initcgr); 3241 if (err < 0) { 3242 if (netif_msg_drv(priv)) 3243 pr_err("Error %d creating ingress CGR with ID %d\n", 3244 err, priv->ingress_cgr.cgrid); 3245 qman_release_cgrid(priv->ingress_cgr.cgrid); 3246 goto out_error; 3247 } 3248 if (netif_msg_drv(priv)) 3249 pr_debug("Created ingress CGR %d for netdev with hwaddr %pM\n", 3250 priv->ingress_cgr.cgrid, priv->mac_dev->addr); 3251 3252 priv->use_ingress_cgr = true; 3253 3254 out_error: 3255 return err; 3256 } 3257 3258 static u16 dpaa_get_headroom(struct dpaa_buffer_layout *bl, 3259 enum port_type port) 3260 { 3261 u16 headroom; 3262 3263 /* The frame headroom must accommodate: 3264 * - the driver private data area 3265 * - parse results, hash results, timestamp if selected 3266 * If either hash results or time stamp are selected, both will 3267 * be copied to/from the frame headroom, as TS is located between PR and 3268 * HR in the IC and IC copy size has a granularity of 16bytes 3269 * (see description of FMBM_RICP and FMBM_TICP registers in DPAARM) 3270 * 3271 * Also make sure the headroom is a multiple of data_align bytes 3272 */ 3273 headroom = (u16)(bl[port].priv_data_size + DPAA_HWA_SIZE); 3274 3275 if (port == RX) { 3276 #ifdef CONFIG_DPAA_ERRATUM_A050385 3277 if (unlikely(fman_has_errata_a050385())) 3278 headroom = XDP_PACKET_HEADROOM; 3279 #endif 3280 3281 return ALIGN(headroom, DPAA_FD_RX_DATA_ALIGNMENT); 3282 } else { 3283 return ALIGN(headroom, DPAA_FD_DATA_ALIGNMENT); 3284 } 3285 } 3286 3287 static int dpaa_eth_probe(struct platform_device *pdev) 3288 { 3289 struct net_device *net_dev = NULL; 3290 struct dpaa_bp *dpaa_bp = NULL; 3291 struct dpaa_fq *dpaa_fq, *tmp; 3292 struct dpaa_priv *priv = NULL; 3293 struct fm_port_fqs port_fqs; 3294 struct mac_device *mac_dev; 3295 int err = 0, channel; 3296 struct device *dev; 3297 3298 dev = &pdev->dev; 3299 3300 err = bman_is_probed(); 3301 if (!err) 3302 return -EPROBE_DEFER; 3303 if (err < 0) { 3304 dev_err(dev, "failing probe due to bman probe error\n"); 3305 return -ENODEV; 3306 } 3307 err = qman_is_probed(); 3308 if (!err) 3309 return -EPROBE_DEFER; 3310 if (err < 0) { 3311 dev_err(dev, "failing probe due to qman probe error\n"); 3312 return -ENODEV; 3313 } 3314 err = bman_portals_probed(); 3315 if (!err) 3316 return -EPROBE_DEFER; 3317 if (err < 0) { 3318 dev_err(dev, 3319 "failing probe due to bman portals probe error\n"); 3320 return -ENODEV; 3321 } 3322 err = qman_portals_probed(); 3323 if (!err) 3324 return -EPROBE_DEFER; 3325 if (err < 0) { 3326 dev_err(dev, 3327 "failing probe due to qman portals probe error\n"); 3328 return -ENODEV; 3329 } 3330 3331 /* Allocate this early, so we can store relevant information in 3332 * the private area 3333 */ 3334 net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA_ETH_TXQ_NUM); 3335 if (!net_dev) { 3336 dev_err(dev, "alloc_etherdev_mq() failed\n"); 3337 return -ENOMEM; 3338 } 3339 3340 /* Do this here, so we can be verbose early */ 3341 SET_NETDEV_DEV(net_dev, dev->parent); 3342 dev_set_drvdata(dev, net_dev); 3343 3344 priv = netdev_priv(net_dev); 3345 priv->net_dev = net_dev; 3346 3347 priv->msg_enable = netif_msg_init(debug, DPAA_MSG_DEFAULT); 3348 3349 mac_dev = dpaa_mac_dev_get(pdev); 3350 if (IS_ERR(mac_dev)) { 3351 netdev_err(net_dev, "dpaa_mac_dev_get() failed\n"); 3352 err = PTR_ERR(mac_dev); 3353 goto free_netdev; 3354 } 3355 3356 /* Devices used for DMA mapping */ 3357 priv->rx_dma_dev = fman_port_get_device(mac_dev->port[RX]); 3358 priv->tx_dma_dev = fman_port_get_device(mac_dev->port[TX]); 3359 err = dma_coerce_mask_and_coherent(priv->rx_dma_dev, DMA_BIT_MASK(40)); 3360 if (!err) 3361 err = dma_coerce_mask_and_coherent(priv->tx_dma_dev, 3362 DMA_BIT_MASK(40)); 3363 if (err) { 3364 netdev_err(net_dev, "dma_coerce_mask_and_coherent() failed\n"); 3365 goto free_netdev; 3366 } 3367 3368 /* If fsl_fm_max_frm is set to a higher value than the all-common 1500, 3369 * we choose conservatively and let the user explicitly set a higher 3370 * MTU via ifconfig. Otherwise, the user may end up with different MTUs 3371 * in the same LAN. 3372 * If on the other hand fsl_fm_max_frm has been chosen below 1500, 3373 * start with the maximum allowed. 3374 */ 3375 net_dev->mtu = min(dpaa_get_max_mtu(), ETH_DATA_LEN); 3376 3377 netdev_dbg(net_dev, "Setting initial MTU on net device: %d\n", 3378 net_dev->mtu); 3379 3380 priv->buf_layout[RX].priv_data_size = DPAA_RX_PRIV_DATA_SIZE; /* Rx */ 3381 priv->buf_layout[TX].priv_data_size = DPAA_TX_PRIV_DATA_SIZE; /* Tx */ 3382 3383 /* bp init */ 3384 dpaa_bp = dpaa_bp_alloc(dev); 3385 if (IS_ERR(dpaa_bp)) { 3386 err = PTR_ERR(dpaa_bp); 3387 goto free_dpaa_bps; 3388 } 3389 /* the raw size of the buffers used for reception */ 3390 dpaa_bp->raw_size = DPAA_BP_RAW_SIZE; 3391 /* avoid runtime computations by keeping the usable size here */ 3392 dpaa_bp->size = dpaa_bp_size(dpaa_bp->raw_size); 3393 dpaa_bp->priv = priv; 3394 3395 err = dpaa_bp_alloc_pool(dpaa_bp); 3396 if (err < 0) 3397 goto free_dpaa_bps; 3398 priv->dpaa_bp = dpaa_bp; 3399 3400 INIT_LIST_HEAD(&priv->dpaa_fq_list); 3401 3402 memset(&port_fqs, 0, sizeof(port_fqs)); 3403 3404 err = dpaa_alloc_all_fqs(dev, &priv->dpaa_fq_list, &port_fqs); 3405 if (err < 0) { 3406 dev_err(dev, "dpaa_alloc_all_fqs() failed\n"); 3407 goto free_dpaa_bps; 3408 } 3409 3410 priv->mac_dev = mac_dev; 3411 3412 channel = dpaa_get_channel(); 3413 if (channel < 0) { 3414 dev_err(dev, "dpaa_get_channel() failed\n"); 3415 err = channel; 3416 goto free_dpaa_bps; 3417 } 3418 3419 priv->channel = (u16)channel; 3420 3421 /* Walk the CPUs with affine portals 3422 * and add this pool channel to each's dequeue mask. 3423 */ 3424 dpaa_eth_add_channel(priv->channel, &pdev->dev); 3425 3426 dpaa_fq_setup(priv, &dpaa_fq_cbs, priv->mac_dev->port[TX]); 3427 3428 /* Create a congestion group for this netdev, with 3429 * dynamically-allocated CGR ID. 3430 * Must be executed after probing the MAC, but before 3431 * assigning the egress FQs to the CGRs. 3432 */ 3433 err = dpaa_eth_cgr_init(priv); 3434 if (err < 0) { 3435 dev_err(dev, "Error initializing CGR\n"); 3436 goto free_dpaa_bps; 3437 } 3438 3439 err = dpaa_ingress_cgr_init(priv); 3440 if (err < 0) { 3441 dev_err(dev, "Error initializing ingress CGR\n"); 3442 goto delete_egress_cgr; 3443 } 3444 3445 /* Add the FQs to the interface, and make them active */ 3446 list_for_each_entry_safe(dpaa_fq, tmp, &priv->dpaa_fq_list, list) { 3447 err = dpaa_fq_init(dpaa_fq, false); 3448 if (err < 0) 3449 goto free_dpaa_fqs; 3450 } 3451 3452 priv->tx_headroom = dpaa_get_headroom(priv->buf_layout, TX); 3453 priv->rx_headroom = dpaa_get_headroom(priv->buf_layout, RX); 3454 3455 /* All real interfaces need their ports initialized */ 3456 err = dpaa_eth_init_ports(mac_dev, dpaa_bp, &port_fqs, 3457 &priv->buf_layout[0], dev); 3458 if (err) 3459 goto free_dpaa_fqs; 3460 3461 /* Rx traffic distribution based on keygen hashing defaults to on */ 3462 priv->keygen_in_use = true; 3463 3464 priv->percpu_priv = devm_alloc_percpu(dev, *priv->percpu_priv); 3465 if (!priv->percpu_priv) { 3466 dev_err(dev, "devm_alloc_percpu() failed\n"); 3467 err = -ENOMEM; 3468 goto free_dpaa_fqs; 3469 } 3470 3471 priv->num_tc = 1; 3472 netif_set_real_num_tx_queues(net_dev, priv->num_tc * DPAA_TC_TXQ_NUM); 3473 3474 /* Initialize NAPI */ 3475 err = dpaa_napi_add(net_dev); 3476 if (err < 0) 3477 goto delete_dpaa_napi; 3478 3479 err = dpaa_netdev_init(net_dev, &dpaa_ops, tx_timeout); 3480 if (err < 0) 3481 goto delete_dpaa_napi; 3482 3483 dpaa_eth_sysfs_init(&net_dev->dev); 3484 3485 netif_info(priv, probe, net_dev, "Probed interface %s\n", 3486 net_dev->name); 3487 3488 return 0; 3489 3490 delete_dpaa_napi: 3491 dpaa_napi_del(net_dev); 3492 free_dpaa_fqs: 3493 dpaa_fq_free(dev, &priv->dpaa_fq_list); 3494 qman_delete_cgr_safe(&priv->ingress_cgr); 3495 qman_release_cgrid(priv->ingress_cgr.cgrid); 3496 delete_egress_cgr: 3497 qman_delete_cgr_safe(&priv->cgr_data.cgr); 3498 qman_release_cgrid(priv->cgr_data.cgr.cgrid); 3499 free_dpaa_bps: 3500 dpaa_bps_free(priv); 3501 free_netdev: 3502 dev_set_drvdata(dev, NULL); 3503 free_netdev(net_dev); 3504 3505 return err; 3506 } 3507 3508 static int dpaa_remove(struct platform_device *pdev) 3509 { 3510 struct net_device *net_dev; 3511 struct dpaa_priv *priv; 3512 struct device *dev; 3513 int err; 3514 3515 dev = &pdev->dev; 3516 net_dev = dev_get_drvdata(dev); 3517 3518 priv = netdev_priv(net_dev); 3519 3520 dpaa_eth_sysfs_remove(dev); 3521 3522 dev_set_drvdata(dev, NULL); 3523 unregister_netdev(net_dev); 3524 3525 err = dpaa_fq_free(dev, &priv->dpaa_fq_list); 3526 3527 qman_delete_cgr_safe(&priv->ingress_cgr); 3528 qman_release_cgrid(priv->ingress_cgr.cgrid); 3529 qman_delete_cgr_safe(&priv->cgr_data.cgr); 3530 qman_release_cgrid(priv->cgr_data.cgr.cgrid); 3531 3532 dpaa_napi_del(net_dev); 3533 3534 dpaa_bps_free(priv); 3535 3536 free_netdev(net_dev); 3537 3538 return err; 3539 } 3540 3541 static const struct platform_device_id dpaa_devtype[] = { 3542 { 3543 .name = "dpaa-ethernet", 3544 .driver_data = 0, 3545 }, { 3546 } 3547 }; 3548 MODULE_DEVICE_TABLE(platform, dpaa_devtype); 3549 3550 static struct platform_driver dpaa_driver = { 3551 .driver = { 3552 .name = KBUILD_MODNAME, 3553 }, 3554 .id_table = dpaa_devtype, 3555 .probe = dpaa_eth_probe, 3556 .remove = dpaa_remove 3557 }; 3558 3559 static int __init dpaa_load(void) 3560 { 3561 int err; 3562 3563 pr_debug("FSL DPAA Ethernet driver\n"); 3564 3565 /* initialize dpaa_eth mirror values */ 3566 dpaa_rx_extra_headroom = fman_get_rx_extra_headroom(); 3567 dpaa_max_frm = fman_get_max_frm(); 3568 3569 err = platform_driver_register(&dpaa_driver); 3570 if (err < 0) 3571 pr_err("Error, platform_driver_register() = %d\n", err); 3572 3573 return err; 3574 } 3575 module_init(dpaa_load); 3576 3577 static void __exit dpaa_unload(void) 3578 { 3579 platform_driver_unregister(&dpaa_driver); 3580 3581 /* Only one channel is used and needs to be released after all 3582 * interfaces are removed 3583 */ 3584 dpaa_release_channel(); 3585 } 3586 module_exit(dpaa_unload); 3587 3588 MODULE_LICENSE("Dual BSD/GPL"); 3589 MODULE_DESCRIPTION("FSL DPAA Ethernet driver"); 3590