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