1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright 2014-2016 Freescale Semiconductor, Inc. 4 * Copyright 2017 NXP 5 */ 6 7 #include <common.h> 8 #include <asm/io.h> 9 #include <asm/types.h> 10 #include <malloc.h> 11 #include <net.h> 12 #include <hwconfig.h> 13 #include <phy.h> 14 #include <linux/compat.h> 15 #include <fsl-mc/fsl_dpmac.h> 16 17 #include <fsl-mc/ldpaa_wriop.h> 18 #include "ldpaa_eth.h" 19 20 #ifdef CONFIG_PHYLIB 21 static int init_phy(struct eth_device *dev) 22 { 23 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)dev->priv; 24 struct phy_device *phydev = NULL; 25 struct mii_dev *bus; 26 27 bus = wriop_get_mdio(priv->dpmac_id); 28 if (bus == NULL) 29 return 0; 30 31 phydev = phy_connect(bus, wriop_get_phy_address(priv->dpmac_id), 32 dev, wriop_get_enet_if(priv->dpmac_id)); 33 if (!phydev) { 34 printf("Failed to connect\n"); 35 return -1; 36 } 37 38 priv->phydev = phydev; 39 40 return phy_config(phydev); 41 } 42 #endif 43 44 #ifdef DEBUG 45 46 #define DPNI_STATS_PER_PAGE 6 47 48 static const char *dpni_statistics[][DPNI_STATS_PER_PAGE] = { 49 { 50 "DPNI_CNT_ING_ALL_FRAMES", 51 "DPNI_CNT_ING_ALL_BYTES", 52 "DPNI_CNT_ING_MCAST_FRAMES", 53 "DPNI_CNT_ING_MCAST_BYTES", 54 "DPNI_CNT_ING_BCAST_FRAMES", 55 "DPNI_CNT_ING_BCAST_BYTES", 56 }, { 57 "DPNI_CNT_EGR_ALL_FRAMES", 58 "DPNI_CNT_EGR_ALL_BYTES", 59 "DPNI_CNT_EGR_MCAST_FRAMES", 60 "DPNI_CNT_EGR_MCAST_BYTES", 61 "DPNI_CNT_EGR_BCAST_FRAMES", 62 "DPNI_CNT_EGR_BCAST_BYTES", 63 }, { 64 "DPNI_CNT_ING_FILTERED_FRAMES", 65 "DPNI_CNT_ING_DISCARDED_FRAMES", 66 "DPNI_CNT_ING_NOBUFFER_DISCARDS", 67 "DPNI_CNT_EGR_DISCARDED_FRAMES", 68 "DPNI_CNT_EGR_CNF_FRAMES", 69 "" 70 }, 71 }; 72 73 static void print_dpni_stats(const char *strings[], 74 struct dpni_statistics dpni_stats) 75 { 76 uint64_t *stat; 77 int i; 78 79 stat = (uint64_t *)&dpni_stats; 80 for (i = 0; i < DPNI_STATS_PER_PAGE; i++) { 81 if (strcmp(strings[i], "\0") == 0) 82 break; 83 printf("%s= %llu\n", strings[i], *stat); 84 stat++; 85 } 86 } 87 88 static void ldpaa_eth_get_dpni_counter(void) 89 { 90 int err = 0; 91 unsigned int page = 0; 92 struct dpni_statistics dpni_stats; 93 94 printf("DPNI counters ..\n"); 95 for (page = 0; page < 3; page++) { 96 err = dpni_get_statistics(dflt_mc_io, MC_CMD_NO_FLAGS, 97 dflt_dpni->dpni_handle, page, 98 &dpni_stats); 99 if (err < 0) { 100 printf("dpni_get_statistics: failed:"); 101 printf("%d for page[%d]\n", err, page); 102 return; 103 } 104 print_dpni_stats(dpni_statistics[page], dpni_stats); 105 } 106 } 107 108 static void ldpaa_eth_get_dpmac_counter(struct eth_device *net_dev) 109 { 110 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv; 111 int err = 0; 112 u64 value; 113 114 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS, 115 priv->dpmac_handle, 116 DPMAC_CNT_ING_BYTE, 117 &value); 118 if (err < 0) { 119 printf("dpmac_get_counter: DPMAC_CNT_ING_BYTE failed\n"); 120 return; 121 } 122 printf("\nDPMAC counters ..\n"); 123 printf("DPMAC_CNT_ING_BYTE=%lld\n", value); 124 125 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS, 126 priv->dpmac_handle, 127 DPMAC_CNT_ING_FRAME_DISCARD, 128 &value); 129 if (err < 0) { 130 printf("dpmac_get_counter: DPMAC_CNT_ING_FRAME_DISCARD failed\n"); 131 return; 132 } 133 printf("DPMAC_CNT_ING_FRAME_DISCARD=%lld\n", value); 134 135 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS, 136 priv->dpmac_handle, 137 DPMAC_CNT_ING_ALIGN_ERR, 138 &value); 139 if (err < 0) { 140 printf("dpmac_get_counter: DPMAC_CNT_ING_ALIGN_ERR failed\n"); 141 return; 142 } 143 printf("DPMAC_CNT_ING_ALIGN_ERR =%lld\n", value); 144 145 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS, 146 priv->dpmac_handle, 147 DPMAC_CNT_ING_BYTE, 148 &value); 149 if (err < 0) { 150 printf("dpmac_get_counter: DPMAC_CNT_ING_BYTE failed\n"); 151 return; 152 } 153 printf("DPMAC_CNT_ING_BYTE=%lld\n", value); 154 155 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS, 156 priv->dpmac_handle, 157 DPMAC_CNT_ING_ERR_FRAME, 158 &value); 159 if (err < 0) { 160 printf("dpmac_get_counter: DPMAC_CNT_ING_ERR_FRAME failed\n"); 161 return; 162 } 163 printf("DPMAC_CNT_ING_ERR_FRAME=%lld\n", value); 164 165 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS, 166 priv->dpmac_handle, 167 DPMAC_CNT_EGR_BYTE , 168 &value); 169 if (err < 0) { 170 printf("dpmac_get_counter: DPMAC_CNT_EGR_BYTE failed\n"); 171 return; 172 } 173 printf("DPMAC_CNT_EGR_BYTE =%lld\n", value); 174 175 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS, 176 priv->dpmac_handle, 177 DPMAC_CNT_EGR_ERR_FRAME , 178 &value); 179 if (err < 0) { 180 printf("dpmac_get_counter: DPMAC_CNT_EGR_ERR_FRAME failed\n"); 181 return; 182 } 183 printf("DPMAC_CNT_EGR_ERR_FRAME =%lld\n", value); 184 } 185 #endif 186 187 static void ldpaa_eth_rx(struct ldpaa_eth_priv *priv, 188 const struct dpaa_fd *fd) 189 { 190 u64 fd_addr; 191 uint16_t fd_offset; 192 uint32_t fd_length; 193 struct ldpaa_fas *fas; 194 uint32_t status, err; 195 u32 timeo = (CONFIG_SYS_HZ * 2) / 1000; 196 u32 time_start; 197 struct qbman_release_desc releasedesc; 198 struct qbman_swp *swp = dflt_dpio->sw_portal; 199 200 fd_addr = ldpaa_fd_get_addr(fd); 201 fd_offset = ldpaa_fd_get_offset(fd); 202 fd_length = ldpaa_fd_get_len(fd); 203 204 debug("Rx frame:data addr=0x%p size=0x%x\n", (u64 *)fd_addr, fd_length); 205 206 if (fd->simple.frc & LDPAA_FD_FRC_FASV) { 207 /* Read the frame annotation status word and check for errors */ 208 fas = (struct ldpaa_fas *) 209 ((uint8_t *)(fd_addr) + 210 dflt_dpni->buf_layout.private_data_size); 211 status = le32_to_cpu(fas->status); 212 if (status & LDPAA_ETH_RX_ERR_MASK) { 213 printf("Rx frame error(s): 0x%08x\n", 214 status & LDPAA_ETH_RX_ERR_MASK); 215 goto error; 216 } else if (status & LDPAA_ETH_RX_UNSUPP_MASK) { 217 printf("Unsupported feature in bitmask: 0x%08x\n", 218 status & LDPAA_ETH_RX_UNSUPP_MASK); 219 goto error; 220 } 221 } 222 223 debug("Rx frame: To Upper layer\n"); 224 net_process_received_packet((uint8_t *)(fd_addr) + fd_offset, 225 fd_length); 226 227 error: 228 flush_dcache_range(fd_addr, fd_addr + LDPAA_ETH_RX_BUFFER_SIZE); 229 qbman_release_desc_clear(&releasedesc); 230 qbman_release_desc_set_bpid(&releasedesc, dflt_dpbp->dpbp_attr.bpid); 231 time_start = get_timer(0); 232 do { 233 /* Release buffer into the QBMAN */ 234 err = qbman_swp_release(swp, &releasedesc, &fd_addr, 1); 235 } while (get_timer(time_start) < timeo && err == -EBUSY); 236 237 if (err == -EBUSY) 238 printf("Rx frame: QBMAN buffer release fails\n"); 239 240 return; 241 } 242 243 static int ldpaa_eth_pull_dequeue_rx(struct eth_device *dev) 244 { 245 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)dev->priv; 246 const struct ldpaa_dq *dq; 247 const struct dpaa_fd *fd; 248 int i = 5, err = 0, status; 249 u32 timeo = (CONFIG_SYS_HZ * 2) / 1000; 250 u32 time_start; 251 static struct qbman_pull_desc pulldesc; 252 struct qbman_swp *swp = dflt_dpio->sw_portal; 253 254 while (--i) { 255 qbman_pull_desc_clear(&pulldesc); 256 qbman_pull_desc_set_numframes(&pulldesc, 1); 257 qbman_pull_desc_set_fq(&pulldesc, priv->rx_dflt_fqid); 258 259 err = qbman_swp_pull(swp, &pulldesc); 260 if (err < 0) { 261 printf("Dequeue frames error:0x%08x\n", err); 262 continue; 263 } 264 265 time_start = get_timer(0); 266 267 do { 268 dq = qbman_swp_dqrr_next(swp); 269 } while (get_timer(time_start) < timeo && !dq); 270 271 if (dq) { 272 /* Check for valid frame. If not sent a consume 273 * confirmation to QBMAN otherwise give it to NADK 274 * application and then send consume confirmation to 275 * QBMAN. 276 */ 277 status = (uint8_t)ldpaa_dq_flags(dq); 278 if ((status & LDPAA_DQ_STAT_VALIDFRAME) == 0) { 279 debug("Dequeue RX frames:"); 280 debug("No frame delivered\n"); 281 282 qbman_swp_dqrr_consume(swp, dq); 283 continue; 284 } 285 286 fd = ldpaa_dq_fd(dq); 287 288 /* Obtain FD and process it */ 289 ldpaa_eth_rx(priv, fd); 290 qbman_swp_dqrr_consume(swp, dq); 291 break; 292 } else { 293 err = -ENODATA; 294 debug("No DQRR entries\n"); 295 break; 296 } 297 } 298 299 return err; 300 } 301 302 static int ldpaa_eth_tx(struct eth_device *net_dev, void *buf, int len) 303 { 304 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv; 305 struct dpaa_fd fd; 306 u64 buffer_start; 307 int data_offset, err; 308 u32 timeo = (CONFIG_SYS_HZ * 10) / 1000; 309 u32 time_start; 310 struct qbman_swp *swp = dflt_dpio->sw_portal; 311 struct qbman_eq_desc ed; 312 struct qbman_release_desc releasedesc; 313 314 /* Setup the FD fields */ 315 memset(&fd, 0, sizeof(fd)); 316 317 data_offset = priv->tx_data_offset; 318 319 do { 320 err = qbman_swp_acquire(dflt_dpio->sw_portal, 321 dflt_dpbp->dpbp_attr.bpid, 322 &buffer_start, 1); 323 } while (err == -EBUSY); 324 325 if (err <= 0) { 326 printf("qbman_swp_acquire() failed\n"); 327 return -ENOMEM; 328 } 329 330 debug("TX data: malloc buffer start=0x%p\n", (u64 *)buffer_start); 331 332 memcpy(((uint8_t *)(buffer_start) + data_offset), buf, len); 333 334 flush_dcache_range(buffer_start, buffer_start + 335 LDPAA_ETH_RX_BUFFER_SIZE); 336 337 ldpaa_fd_set_addr(&fd, (u64)buffer_start); 338 ldpaa_fd_set_offset(&fd, (uint16_t)(data_offset)); 339 ldpaa_fd_set_bpid(&fd, dflt_dpbp->dpbp_attr.bpid); 340 ldpaa_fd_set_len(&fd, len); 341 342 fd.simple.ctrl = LDPAA_FD_CTRL_ASAL | LDPAA_FD_CTRL_PTA | 343 LDPAA_FD_CTRL_PTV1; 344 345 qbman_eq_desc_clear(&ed); 346 qbman_eq_desc_set_no_orp(&ed, 0); 347 qbman_eq_desc_set_qd(&ed, priv->tx_qdid, priv->tx_flow_id, 0); 348 349 time_start = get_timer(0); 350 351 while (get_timer(time_start) < timeo) { 352 err = qbman_swp_enqueue(swp, &ed, 353 (const struct qbman_fd *)(&fd)); 354 if (err != -EBUSY) 355 break; 356 } 357 358 if (err < 0) { 359 printf("error enqueueing Tx frame\n"); 360 goto error; 361 } 362 363 return err; 364 365 error: 366 qbman_release_desc_clear(&releasedesc); 367 qbman_release_desc_set_bpid(&releasedesc, dflt_dpbp->dpbp_attr.bpid); 368 time_start = get_timer(0); 369 do { 370 /* Release buffer into the QBMAN */ 371 err = qbman_swp_release(swp, &releasedesc, &buffer_start, 1); 372 } while (get_timer(time_start) < timeo && err == -EBUSY); 373 374 if (err == -EBUSY) 375 printf("TX data: QBMAN buffer release fails\n"); 376 377 return err; 378 } 379 380 static int ldpaa_eth_open(struct eth_device *net_dev, bd_t *bd) 381 { 382 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv; 383 struct dpmac_link_state dpmac_link_state = { 0 }; 384 #ifdef DEBUG 385 struct dpni_link_state link_state; 386 #endif 387 int err = 0; 388 struct mii_dev *bus; 389 phy_interface_t enet_if; 390 struct dpni_queue d_queue; 391 392 if (net_dev->state == ETH_STATE_ACTIVE) 393 return 0; 394 395 if (get_mc_boot_status() != 0) { 396 printf("ERROR (MC is not booted)\n"); 397 return -ENODEV; 398 } 399 400 if (get_dpl_apply_status() == 0) { 401 printf("ERROR (DPL is deployed. No device available)\n"); 402 return -ENODEV; 403 } 404 405 /* DPMAC initialization */ 406 err = ldpaa_dpmac_setup(priv); 407 if (err < 0) 408 goto err_dpmac_setup; 409 410 #ifdef CONFIG_PHYLIB 411 if (priv->phydev) { 412 err = phy_startup(priv->phydev); 413 if (err) { 414 printf("%s: Could not initialize\n", 415 priv->phydev->dev->name); 416 goto err_dpamc_bind; 417 } 418 } 419 #else 420 priv->phydev = (struct phy_device *)malloc(sizeof(struct phy_device)); 421 memset(priv->phydev, 0, sizeof(struct phy_device)); 422 423 priv->phydev->speed = SPEED_1000; 424 priv->phydev->link = 1; 425 priv->phydev->duplex = DUPLEX_FULL; 426 #endif 427 428 bus = wriop_get_mdio(priv->dpmac_id); 429 enet_if = wriop_get_enet_if(priv->dpmac_id); 430 if ((bus == NULL) && 431 (enet_if == PHY_INTERFACE_MODE_XGMII)) { 432 priv->phydev = (struct phy_device *) 433 malloc(sizeof(struct phy_device)); 434 memset(priv->phydev, 0, sizeof(struct phy_device)); 435 436 priv->phydev->speed = SPEED_10000; 437 priv->phydev->link = 1; 438 priv->phydev->duplex = DUPLEX_FULL; 439 } 440 441 if (!priv->phydev->link) { 442 printf("%s: No link.\n", priv->phydev->dev->name); 443 err = -1; 444 goto err_dpamc_bind; 445 } 446 447 /* DPMAC binding DPNI */ 448 err = ldpaa_dpmac_bind(priv); 449 if (err) 450 goto err_dpamc_bind; 451 452 /* DPNI initialization */ 453 err = ldpaa_dpni_setup(priv); 454 if (err < 0) 455 goto err_dpni_setup; 456 457 err = ldpaa_dpbp_setup(); 458 if (err < 0) 459 goto err_dpbp_setup; 460 461 /* DPNI binding DPBP */ 462 err = ldpaa_dpni_bind(priv); 463 if (err) 464 goto err_dpni_bind; 465 466 err = dpni_add_mac_addr(dflt_mc_io, MC_CMD_NO_FLAGS, 467 dflt_dpni->dpni_handle, net_dev->enetaddr); 468 if (err) { 469 printf("dpni_add_mac_addr() failed\n"); 470 return err; 471 } 472 473 err = dpni_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle); 474 if (err < 0) { 475 printf("dpni_enable() failed\n"); 476 return err; 477 } 478 479 dpmac_link_state.rate = priv->phydev->speed; 480 481 if (priv->phydev->autoneg == AUTONEG_DISABLE) 482 dpmac_link_state.options &= ~DPMAC_LINK_OPT_AUTONEG; 483 else 484 dpmac_link_state.options |= DPMAC_LINK_OPT_AUTONEG; 485 486 if (priv->phydev->duplex == DUPLEX_HALF) 487 dpmac_link_state.options |= DPMAC_LINK_OPT_HALF_DUPLEX; 488 489 dpmac_link_state.up = priv->phydev->link; 490 491 err = dpmac_set_link_state(dflt_mc_io, MC_CMD_NO_FLAGS, 492 priv->dpmac_handle, &dpmac_link_state); 493 if (err < 0) { 494 printf("dpmac_set_link_state() failed\n"); 495 return err; 496 } 497 498 #ifdef DEBUG 499 printf("DPMAC link status: %d - ", dpmac_link_state.up); 500 dpmac_link_state.up == 0 ? printf("down\n") : 501 dpmac_link_state.up == 1 ? printf("up\n") : printf("error state\n"); 502 503 err = dpni_get_link_state(dflt_mc_io, MC_CMD_NO_FLAGS, 504 dflt_dpni->dpni_handle, &link_state); 505 if (err < 0) { 506 printf("dpni_get_link_state() failed\n"); 507 return err; 508 } 509 510 printf("DPNI link status: %d - ", link_state.up); 511 link_state.up == 0 ? printf("down\n") : 512 link_state.up == 1 ? printf("up\n") : printf("error state\n"); 513 #endif 514 515 memset(&d_queue, 0, sizeof(struct dpni_queue)); 516 err = dpni_get_queue(dflt_mc_io, MC_CMD_NO_FLAGS, 517 dflt_dpni->dpni_handle, DPNI_QUEUE_RX, 518 0, 0, &d_queue); 519 if (err) { 520 printf("dpni_get_queue failed\n"); 521 goto err_get_queue; 522 } 523 524 priv->rx_dflt_fqid = d_queue.fqid; 525 526 err = dpni_get_qdid(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle, 527 &priv->tx_qdid); 528 if (err) { 529 printf("dpni_get_qdid() failed\n"); 530 goto err_qdid; 531 } 532 533 return priv->phydev->link; 534 535 err_qdid: 536 err_get_queue: 537 dpni_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle); 538 err_dpni_bind: 539 ldpaa_dpbp_free(); 540 err_dpbp_setup: 541 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle); 542 err_dpni_setup: 543 err_dpamc_bind: 544 dpmac_close(dflt_mc_io, MC_CMD_NO_FLAGS, priv->dpmac_handle); 545 dpmac_destroy(dflt_mc_io, 546 dflt_dprc_handle, 547 MC_CMD_NO_FLAGS, priv->dpmac_id); 548 err_dpmac_setup: 549 return err; 550 } 551 552 static void ldpaa_eth_stop(struct eth_device *net_dev) 553 { 554 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv; 555 int err = 0; 556 #ifdef CONFIG_PHYLIB 557 struct mii_dev *bus = wriop_get_mdio(priv->dpmac_id); 558 #endif 559 560 if ((net_dev->state == ETH_STATE_PASSIVE) || 561 (net_dev->state == ETH_STATE_INIT)) 562 return; 563 564 #ifdef DEBUG 565 ldpaa_eth_get_dpni_counter(); 566 ldpaa_eth_get_dpmac_counter(net_dev); 567 #endif 568 569 err = dprc_disconnect(dflt_mc_io, MC_CMD_NO_FLAGS, 570 dflt_dprc_handle, &dpmac_endpoint); 571 if (err < 0) 572 printf("dprc_disconnect() failed dpmac_endpoint\n"); 573 574 err = dpmac_close(dflt_mc_io, MC_CMD_NO_FLAGS, priv->dpmac_handle); 575 if (err < 0) 576 printf("dpmac_close() failed\n"); 577 578 err = dpmac_destroy(dflt_mc_io, 579 dflt_dprc_handle, 580 MC_CMD_NO_FLAGS, 581 priv->dpmac_id); 582 if (err < 0) 583 printf("dpmac_destroy() failed\n"); 584 585 /* Stop Tx and Rx traffic */ 586 err = dpni_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle); 587 if (err < 0) 588 printf("dpni_disable() failed\n"); 589 590 #ifdef CONFIG_PHYLIB 591 if (priv->phydev && bus != NULL) 592 phy_shutdown(priv->phydev); 593 else { 594 free(priv->phydev); 595 priv->phydev = NULL; 596 } 597 #endif 598 599 /* Free DPBP handle and reset. */ 600 ldpaa_dpbp_free(); 601 602 dpni_reset(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle); 603 if (err < 0) 604 printf("dpni_reset() failed\n"); 605 606 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle); 607 if (err < 0) 608 printf("dpni_close() failed\n"); 609 } 610 611 static void ldpaa_dpbp_drain_cnt(int count) 612 { 613 uint64_t buf_array[7]; 614 void *addr; 615 int ret, i; 616 617 BUG_ON(count > 7); 618 619 do { 620 ret = qbman_swp_acquire(dflt_dpio->sw_portal, 621 dflt_dpbp->dpbp_attr.bpid, 622 buf_array, count); 623 if (ret < 0) { 624 printf("qbman_swp_acquire() failed\n"); 625 return; 626 } 627 for (i = 0; i < ret; i++) { 628 addr = (void *)buf_array[i]; 629 debug("Free: buffer addr =0x%p\n", addr); 630 free(addr); 631 } 632 } while (ret); 633 } 634 635 static void ldpaa_dpbp_drain(void) 636 { 637 int i; 638 for (i = 0; i < LDPAA_ETH_NUM_BUFS; i += 7) 639 ldpaa_dpbp_drain_cnt(7); 640 } 641 642 static int ldpaa_bp_add_7(uint16_t bpid) 643 { 644 uint64_t buf_array[7]; 645 u8 *addr; 646 int i; 647 struct qbman_release_desc rd; 648 649 for (i = 0; i < 7; i++) { 650 addr = memalign(LDPAA_ETH_BUF_ALIGN, LDPAA_ETH_RX_BUFFER_SIZE); 651 if (!addr) { 652 printf("addr allocation failed\n"); 653 goto err_alloc; 654 } 655 memset(addr, 0x00, LDPAA_ETH_RX_BUFFER_SIZE); 656 flush_dcache_range((u64)addr, 657 (u64)(addr + LDPAA_ETH_RX_BUFFER_SIZE)); 658 659 buf_array[i] = (uint64_t)addr; 660 debug("Release: buffer addr =0x%p\n", addr); 661 } 662 663 release_bufs: 664 /* In case the portal is busy, retry until successful. 665 * This function is guaranteed to succeed in a reasonable amount 666 * of time. 667 */ 668 669 do { 670 mdelay(1); 671 qbman_release_desc_clear(&rd); 672 qbman_release_desc_set_bpid(&rd, bpid); 673 } while (qbman_swp_release(dflt_dpio->sw_portal, &rd, buf_array, i)); 674 675 return i; 676 677 err_alloc: 678 if (i) 679 goto release_bufs; 680 681 return 0; 682 } 683 684 static int ldpaa_dpbp_seed(uint16_t bpid) 685 { 686 int i; 687 int count; 688 689 for (i = 0; i < LDPAA_ETH_NUM_BUFS; i += 7) { 690 count = ldpaa_bp_add_7(bpid); 691 if (count < 7) 692 printf("Buffer Seed= %d\n", count); 693 } 694 695 return 0; 696 } 697 698 static int ldpaa_dpbp_setup(void) 699 { 700 int err; 701 702 err = dpbp_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_attr.id, 703 &dflt_dpbp->dpbp_handle); 704 if (err) { 705 printf("dpbp_open() failed\n"); 706 goto err_open; 707 } 708 709 err = dpbp_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle); 710 if (err) { 711 printf("dpbp_enable() failed\n"); 712 goto err_enable; 713 } 714 715 err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS, 716 dflt_dpbp->dpbp_handle, 717 &dflt_dpbp->dpbp_attr); 718 if (err) { 719 printf("dpbp_get_attributes() failed\n"); 720 goto err_get_attr; 721 } 722 723 err = ldpaa_dpbp_seed(dflt_dpbp->dpbp_attr.bpid); 724 725 if (err) { 726 printf("Buffer seeding failed for DPBP %d (bpid=%d)\n", 727 dflt_dpbp->dpbp_attr.id, dflt_dpbp->dpbp_attr.bpid); 728 goto err_seed; 729 } 730 731 return 0; 732 733 err_seed: 734 err_get_attr: 735 dpbp_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle); 736 err_enable: 737 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle); 738 err_open: 739 return err; 740 } 741 742 static void ldpaa_dpbp_free(void) 743 { 744 ldpaa_dpbp_drain(); 745 dpbp_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle); 746 dpbp_reset(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle); 747 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle); 748 } 749 750 static int ldpaa_dpmac_version_check(struct fsl_mc_io *mc_io, 751 struct ldpaa_eth_priv *priv) 752 { 753 int error; 754 uint16_t major_ver, minor_ver; 755 756 error = dpmac_get_api_version(dflt_mc_io, 0, 757 &major_ver, 758 &minor_ver); 759 if ((major_ver < DPMAC_VER_MAJOR) || 760 (major_ver == DPMAC_VER_MAJOR && minor_ver < DPMAC_VER_MINOR)) { 761 printf("DPMAC version mismatch found %u.%u,", 762 major_ver, minor_ver); 763 printf("supported version is %u.%u\n", 764 DPMAC_VER_MAJOR, DPMAC_VER_MINOR); 765 return error; 766 } 767 768 return error; 769 } 770 771 static int ldpaa_dpmac_setup(struct ldpaa_eth_priv *priv) 772 { 773 int err = 0; 774 struct dpmac_cfg dpmac_cfg; 775 776 dpmac_cfg.mac_id = priv->dpmac_id; 777 778 err = dpmac_create(dflt_mc_io, 779 dflt_dprc_handle, 780 MC_CMD_NO_FLAGS, &dpmac_cfg, 781 &priv->dpmac_id); 782 if (err) 783 printf("dpmac_create() failed\n"); 784 785 err = ldpaa_dpmac_version_check(dflt_mc_io, priv); 786 if (err < 0) { 787 printf("ldpaa_dpmac_version_check() failed: %d\n", err); 788 goto err_version_check; 789 } 790 791 err = dpmac_open(dflt_mc_io, 792 MC_CMD_NO_FLAGS, 793 priv->dpmac_id, 794 &priv->dpmac_handle); 795 if (err < 0) { 796 printf("dpmac_open() failed: %d\n", err); 797 goto err_open; 798 } 799 800 return err; 801 802 err_open: 803 err_version_check: 804 dpmac_destroy(dflt_mc_io, 805 dflt_dprc_handle, 806 MC_CMD_NO_FLAGS, priv->dpmac_id); 807 808 return err; 809 } 810 811 static int ldpaa_dpmac_bind(struct ldpaa_eth_priv *priv) 812 { 813 int err = 0; 814 struct dprc_connection_cfg dprc_connection_cfg = { 815 /* If both rates are zero the connection */ 816 /* will be configured in "best effort" mode. */ 817 .committed_rate = 0, 818 .max_rate = 0 819 }; 820 821 #ifdef DEBUG 822 struct dprc_endpoint dbg_endpoint; 823 int state = 0; 824 #endif 825 826 memset(&dpmac_endpoint, 0, sizeof(struct dprc_endpoint)); 827 strcpy(dpmac_endpoint.type, "dpmac"); 828 dpmac_endpoint.id = priv->dpmac_id; 829 830 memset(&dpni_endpoint, 0, sizeof(struct dprc_endpoint)); 831 strcpy(dpni_endpoint.type, "dpni"); 832 dpni_endpoint.id = dflt_dpni->dpni_id; 833 834 err = dprc_connect(dflt_mc_io, MC_CMD_NO_FLAGS, 835 dflt_dprc_handle, 836 &dpmac_endpoint, 837 &dpni_endpoint, 838 &dprc_connection_cfg); 839 if (err) 840 printf("dprc_connect() failed\n"); 841 842 #ifdef DEBUG 843 err = dprc_get_connection(dflt_mc_io, MC_CMD_NO_FLAGS, 844 dflt_dprc_handle, &dpni_endpoint, 845 &dbg_endpoint, &state); 846 printf("%s, DPMAC Type= %s\n", __func__, dbg_endpoint.type); 847 printf("%s, DPMAC ID= %d\n", __func__, dbg_endpoint.id); 848 printf("%s, DPMAC State= %d\n", __func__, state); 849 850 memset(&dbg_endpoint, 0, sizeof(struct dprc_endpoint)); 851 err = dprc_get_connection(dflt_mc_io, MC_CMD_NO_FLAGS, 852 dflt_dprc_handle, &dpmac_endpoint, 853 &dbg_endpoint, &state); 854 printf("%s, DPNI Type= %s\n", __func__, dbg_endpoint.type); 855 printf("%s, DPNI ID= %d\n", __func__, dbg_endpoint.id); 856 printf("%s, DPNI State= %d\n", __func__, state); 857 #endif 858 return err; 859 } 860 861 static int ldpaa_dpni_setup(struct ldpaa_eth_priv *priv) 862 { 863 int err; 864 865 /* and get a handle for the DPNI this interface is associate with */ 866 err = dpni_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_id, 867 &dflt_dpni->dpni_handle); 868 if (err) { 869 printf("dpni_open() failed\n"); 870 goto err_open; 871 } 872 err = dpni_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS, 873 dflt_dpni->dpni_handle, 874 &dflt_dpni->dpni_attrs); 875 if (err) { 876 printf("dpni_get_attributes() failed (err=%d)\n", err); 877 goto err_get_attr; 878 } 879 880 /* Configure our buffers' layout */ 881 dflt_dpni->buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT | 882 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS | 883 DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE | 884 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN; 885 dflt_dpni->buf_layout.pass_parser_result = true; 886 dflt_dpni->buf_layout.pass_frame_status = true; 887 dflt_dpni->buf_layout.private_data_size = LDPAA_ETH_SWA_SIZE; 888 /* HW erratum mandates data alignment in multiples of 256 */ 889 dflt_dpni->buf_layout.data_align = LDPAA_ETH_BUF_ALIGN; 890 891 /* ...rx, ... */ 892 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS, 893 dflt_dpni->dpni_handle, 894 &dflt_dpni->buf_layout, DPNI_QUEUE_RX); 895 if (err) { 896 printf("dpni_set_buffer_layout() failed"); 897 goto err_buf_layout; 898 } 899 900 /* ... tx, ... */ 901 /* remove Rx-only options */ 902 dflt_dpni->buf_layout.options &= ~(DPNI_BUF_LAYOUT_OPT_DATA_ALIGN | 903 DPNI_BUF_LAYOUT_OPT_PARSER_RESULT); 904 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS, 905 dflt_dpni->dpni_handle, 906 &dflt_dpni->buf_layout, DPNI_QUEUE_TX); 907 if (err) { 908 printf("dpni_set_buffer_layout() failed"); 909 goto err_buf_layout; 910 } 911 912 /* ... tx-confirm. */ 913 dflt_dpni->buf_layout.options &= ~DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE; 914 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS, 915 dflt_dpni->dpni_handle, 916 &dflt_dpni->buf_layout, 917 DPNI_QUEUE_TX_CONFIRM); 918 if (err) { 919 printf("dpni_set_buffer_layout() failed"); 920 goto err_buf_layout; 921 } 922 923 /* Now that we've set our tx buffer layout, retrieve the minimum 924 * required tx data offset. 925 */ 926 err = dpni_get_tx_data_offset(dflt_mc_io, MC_CMD_NO_FLAGS, 927 dflt_dpni->dpni_handle, 928 &priv->tx_data_offset); 929 if (err) { 930 printf("dpni_get_tx_data_offset() failed\n"); 931 goto err_data_offset; 932 } 933 934 /* Warn in case TX data offset is not multiple of 64 bytes. */ 935 WARN_ON(priv->tx_data_offset % 64); 936 937 /* Accomodate SWA space. */ 938 priv->tx_data_offset += LDPAA_ETH_SWA_SIZE; 939 debug("priv->tx_data_offset=%d\n", priv->tx_data_offset); 940 941 return 0; 942 943 err_data_offset: 944 err_buf_layout: 945 err_get_attr: 946 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle); 947 err_open: 948 return err; 949 } 950 951 static int ldpaa_dpni_bind(struct ldpaa_eth_priv *priv) 952 { 953 struct dpni_pools_cfg pools_params; 954 struct dpni_queue tx_queue; 955 int err = 0; 956 957 memset(&pools_params, 0, sizeof(pools_params)); 958 pools_params.num_dpbp = 1; 959 pools_params.pools[0].dpbp_id = (uint16_t)dflt_dpbp->dpbp_attr.id; 960 pools_params.pools[0].buffer_size = LDPAA_ETH_RX_BUFFER_SIZE; 961 err = dpni_set_pools(dflt_mc_io, MC_CMD_NO_FLAGS, 962 dflt_dpni->dpni_handle, &pools_params); 963 if (err) { 964 printf("dpni_set_pools() failed\n"); 965 return err; 966 } 967 968 memset(&tx_queue, 0, sizeof(struct dpni_queue)); 969 970 err = dpni_set_queue(dflt_mc_io, MC_CMD_NO_FLAGS, 971 dflt_dpni->dpni_handle, 972 DPNI_QUEUE_TX, 0, 0, &tx_queue); 973 974 if (err) { 975 printf("dpni_set_queue() failed\n"); 976 return err; 977 } 978 979 err = dpni_set_tx_confirmation_mode(dflt_mc_io, MC_CMD_NO_FLAGS, 980 dflt_dpni->dpni_handle, 981 DPNI_CONF_DISABLE); 982 if (err) { 983 printf("dpni_set_tx_confirmation_mode() failed\n"); 984 return err; 985 } 986 987 return 0; 988 } 989 990 static int ldpaa_eth_netdev_init(struct eth_device *net_dev, 991 phy_interface_t enet_if) 992 { 993 int err; 994 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv; 995 996 snprintf(net_dev->name, ETH_NAME_LEN, "DPMAC%d@%s", priv->dpmac_id, 997 phy_interface_strings[enet_if]); 998 999 net_dev->iobase = 0; 1000 net_dev->init = ldpaa_eth_open; 1001 net_dev->halt = ldpaa_eth_stop; 1002 net_dev->send = ldpaa_eth_tx; 1003 net_dev->recv = ldpaa_eth_pull_dequeue_rx; 1004 1005 #ifdef CONFIG_PHYLIB 1006 err = init_phy(net_dev); 1007 if (err < 0) 1008 return err; 1009 #endif 1010 1011 err = eth_register(net_dev); 1012 if (err < 0) { 1013 printf("eth_register() = %d\n", err); 1014 return err; 1015 } 1016 1017 return 0; 1018 } 1019 1020 int ldpaa_eth_init(int dpmac_id, phy_interface_t enet_if) 1021 { 1022 struct eth_device *net_dev = NULL; 1023 struct ldpaa_eth_priv *priv = NULL; 1024 int err = 0; 1025 1026 /* Net device */ 1027 net_dev = (struct eth_device *)malloc(sizeof(struct eth_device)); 1028 if (!net_dev) { 1029 printf("eth_device malloc() failed\n"); 1030 return -ENOMEM; 1031 } 1032 memset(net_dev, 0, sizeof(struct eth_device)); 1033 1034 /* alloc the ldpaa ethernet private struct */ 1035 priv = (struct ldpaa_eth_priv *)malloc(sizeof(struct ldpaa_eth_priv)); 1036 if (!priv) { 1037 printf("ldpaa_eth_priv malloc() failed\n"); 1038 return -ENOMEM; 1039 } 1040 memset(priv, 0, sizeof(struct ldpaa_eth_priv)); 1041 1042 net_dev->priv = (void *)priv; 1043 priv->net_dev = (struct eth_device *)net_dev; 1044 priv->dpmac_id = dpmac_id; 1045 debug("%s dpmac_id=%d\n", __func__, dpmac_id); 1046 1047 err = ldpaa_eth_netdev_init(net_dev, enet_if); 1048 if (err) 1049 goto err_netdev_init; 1050 1051 debug("ldpaa ethernet: Probed interface %s\n", net_dev->name); 1052 return 0; 1053 1054 err_netdev_init: 1055 free(priv); 1056 net_dev->priv = NULL; 1057 free(net_dev); 1058 1059 return err; 1060 } 1061