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