1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2014-2016 Freescale Semiconductor, Inc.
14 #include <linux/compat.h>
15 #include <fsl-mc/fsl_dpmac.h>
17 #include <fsl-mc/ldpaa_wriop.h>
18 #include "ldpaa_eth.h"
21 static int init_phy(struct eth_device *dev)
23 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)dev->priv;
24 struct phy_device *phydev = NULL;
27 bus = wriop_get_mdio(priv->dpmac_id);
31 phydev = phy_connect(bus, wriop_get_phy_address(priv->dpmac_id),
32 dev, wriop_get_enet_if(priv->dpmac_id));
34 printf("Failed to connect\n");
38 wriop_set_phy_dev(priv->dpmac_id, phydev);
40 return phy_config(phydev);
46 #define DPNI_STATS_PER_PAGE 6
48 static const char *dpni_statistics[][DPNI_STATS_PER_PAGE] = {
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",
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",
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",
73 static void print_dpni_stats(const char *strings[],
74 struct dpni_statistics dpni_stats)
79 stat = (uint64_t *)&dpni_stats;
80 for (i = 0; i < DPNI_STATS_PER_PAGE; i++) {
81 if (strcmp(strings[i], "\0") == 0)
83 printf("%s= %llu\n", strings[i], *stat);
88 static void ldpaa_eth_get_dpni_counter(void)
91 unsigned int page = 0;
92 struct dpni_statistics dpni_stats;
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,
100 printf("dpni_get_statistics: failed:");
101 printf("%d for page[%d]\n", err, page);
104 print_dpni_stats(dpni_statistics[page], dpni_stats);
108 static void ldpaa_eth_get_dpmac_counter(struct eth_device *net_dev)
110 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
114 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
119 printf("dpmac_get_counter: DPMAC_CNT_ING_BYTE failed\n");
122 printf("\nDPMAC counters ..\n");
123 printf("DPMAC_CNT_ING_BYTE=%lld\n", value);
125 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
127 DPMAC_CNT_ING_FRAME_DISCARD,
130 printf("dpmac_get_counter: DPMAC_CNT_ING_FRAME_DISCARD failed\n");
133 printf("DPMAC_CNT_ING_FRAME_DISCARD=%lld\n", value);
135 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
137 DPMAC_CNT_ING_ALIGN_ERR,
140 printf("dpmac_get_counter: DPMAC_CNT_ING_ALIGN_ERR failed\n");
143 printf("DPMAC_CNT_ING_ALIGN_ERR =%lld\n", value);
145 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
150 printf("dpmac_get_counter: DPMAC_CNT_ING_BYTE failed\n");
153 printf("DPMAC_CNT_ING_BYTE=%lld\n", value);
155 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
157 DPMAC_CNT_ING_ERR_FRAME,
160 printf("dpmac_get_counter: DPMAC_CNT_ING_ERR_FRAME failed\n");
163 printf("DPMAC_CNT_ING_ERR_FRAME=%lld\n", value);
165 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
170 printf("dpmac_get_counter: DPMAC_CNT_EGR_BYTE failed\n");
173 printf("DPMAC_CNT_EGR_BYTE =%lld\n", value);
175 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
177 DPMAC_CNT_EGR_ERR_FRAME ,
180 printf("dpmac_get_counter: DPMAC_CNT_EGR_ERR_FRAME failed\n");
183 printf("DPMAC_CNT_EGR_ERR_FRAME =%lld\n", value);
187 static void ldpaa_eth_rx(struct ldpaa_eth_priv *priv,
188 const struct dpaa_fd *fd)
193 struct ldpaa_fas *fas;
194 uint32_t status, err;
195 u32 timeo = (CONFIG_SYS_HZ * 2) / 1000;
197 struct qbman_release_desc releasedesc;
198 struct qbman_swp *swp = dflt_dpio->sw_portal;
200 fd_addr = ldpaa_fd_get_addr(fd);
201 fd_offset = ldpaa_fd_get_offset(fd);
202 fd_length = ldpaa_fd_get_len(fd);
204 debug("Rx frame:data addr=0x%p size=0x%x\n", (u64 *)fd_addr, fd_length);
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);
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);
223 debug("Rx frame: To Upper layer\n");
224 net_process_received_packet((uint8_t *)(fd_addr) + fd_offset,
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);
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);
238 printf("Rx frame: QBMAN buffer release fails\n");
243 static int ldpaa_eth_pull_dequeue_rx(struct eth_device *dev)
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;
251 static struct qbman_pull_desc pulldesc;
252 struct qbman_swp *swp = dflt_dpio->sw_portal;
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);
259 err = qbman_swp_pull(swp, &pulldesc);
261 printf("Dequeue frames error:0x%08x\n", err);
265 time_start = get_timer(0);
268 dq = qbman_swp_dqrr_next(swp);
269 } while (get_timer(time_start) < timeo && !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
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");
282 qbman_swp_dqrr_consume(swp, dq);
286 fd = ldpaa_dq_fd(dq);
288 /* Obtain FD and process it */
289 ldpaa_eth_rx(priv, fd);
290 qbman_swp_dqrr_consume(swp, dq);
294 debug("No DQRR entries\n");
302 static int ldpaa_eth_tx(struct eth_device *net_dev, void *buf, int len)
304 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
307 int data_offset, err;
308 u32 timeo = (CONFIG_SYS_HZ * 10) / 1000;
310 struct qbman_swp *swp = dflt_dpio->sw_portal;
311 struct qbman_eq_desc ed;
312 struct qbman_release_desc releasedesc;
314 /* Setup the FD fields */
315 memset(&fd, 0, sizeof(fd));
317 data_offset = priv->tx_data_offset;
320 err = qbman_swp_acquire(dflt_dpio->sw_portal,
321 dflt_dpbp->dpbp_attr.bpid,
323 } while (err == -EBUSY);
326 printf("qbman_swp_acquire() failed\n");
330 debug("TX data: malloc buffer start=0x%p\n", (u64 *)buffer_start);
332 memcpy(((uint8_t *)(buffer_start) + data_offset), buf, len);
334 flush_dcache_range(buffer_start, buffer_start +
335 LDPAA_ETH_RX_BUFFER_SIZE);
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);
342 fd.simple.ctrl = LDPAA_FD_CTRL_ASAL | LDPAA_FD_CTRL_PTA |
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);
349 time_start = get_timer(0);
351 while (get_timer(time_start) < timeo) {
352 err = qbman_swp_enqueue(swp, &ed,
353 (const struct qbman_fd *)(&fd));
359 printf("error enqueueing Tx frame\n");
366 qbman_release_desc_clear(&releasedesc);
367 qbman_release_desc_set_bpid(&releasedesc, dflt_dpbp->dpbp_attr.bpid);
368 time_start = get_timer(0);
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);
375 printf("TX data: QBMAN buffer release fails\n");
380 static int ldpaa_eth_open(struct eth_device *net_dev, bd_t *bd)
382 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
383 struct dpmac_link_state dpmac_link_state = { 0 };
385 struct dpni_link_state link_state;
389 phy_interface_t enet_if;
390 struct dpni_queue d_queue;
391 struct phy_device *phydev = NULL;
393 if (net_dev->state == ETH_STATE_ACTIVE)
396 if (get_mc_boot_status() != 0) {
397 printf("ERROR (MC is not booted)\n");
401 if (get_dpl_apply_status() == 0) {
402 printf("ERROR (DPL is deployed. No device available)\n");
406 /* DPMAC initialization */
407 err = ldpaa_dpmac_setup(priv);
409 goto err_dpmac_setup;
412 phydev = wriop_get_phy_dev(priv->dpmac_id);
414 err = phy_startup(phydev);
416 printf("%s: Could not initialize\n",
422 phydev = (struct phy_device *)malloc(sizeof(struct phy_device));
423 memset(phydev, 0, sizeof(struct phy_device));
424 wriop_set_phy_dev(priv->dpmac_id, phydev);
426 phydev->speed = SPEED_1000;
428 phydev->duplex = DUPLEX_FULL;
431 bus = wriop_get_mdio(priv->dpmac_id);
432 enet_if = wriop_get_enet_if(priv->dpmac_id);
434 (enet_if == PHY_INTERFACE_MODE_XGMII)) {
435 phydev = (struct phy_device *)
436 malloc(sizeof(struct phy_device));
437 memset(phydev, 0, sizeof(struct phy_device));
438 wriop_set_phy_dev(priv->dpmac_id, phydev);
440 phydev->speed = SPEED_10000;
442 phydev->duplex = DUPLEX_FULL;
446 printf("%s: No link.\n", phydev->dev->name);
451 /* DPMAC binding DPNI */
452 err = ldpaa_dpmac_bind(priv);
456 /* DPNI initialization */
457 err = ldpaa_dpni_setup(priv);
461 err = ldpaa_dpbp_setup();
465 /* DPNI binding DPBP */
466 err = ldpaa_dpni_bind(priv);
470 err = dpni_add_mac_addr(dflt_mc_io, MC_CMD_NO_FLAGS,
471 dflt_dpni->dpni_handle, net_dev->enetaddr);
473 printf("dpni_add_mac_addr() failed\n");
477 err = dpni_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
479 printf("dpni_enable() failed\n");
483 dpmac_link_state.rate = phydev->speed;
485 if (phydev->autoneg == AUTONEG_DISABLE)
486 dpmac_link_state.options &= ~DPMAC_LINK_OPT_AUTONEG;
488 dpmac_link_state.options |= DPMAC_LINK_OPT_AUTONEG;
490 if (phydev->duplex == DUPLEX_HALF)
491 dpmac_link_state.options |= DPMAC_LINK_OPT_HALF_DUPLEX;
493 dpmac_link_state.up = phydev->link;
495 err = dpmac_set_link_state(dflt_mc_io, MC_CMD_NO_FLAGS,
496 priv->dpmac_handle, &dpmac_link_state);
498 printf("dpmac_set_link_state() failed\n");
503 printf("DPMAC link status: %d - ", dpmac_link_state.up);
504 dpmac_link_state.up == 0 ? printf("down\n") :
505 dpmac_link_state.up == 1 ? printf("up\n") : printf("error state\n");
507 err = dpni_get_link_state(dflt_mc_io, MC_CMD_NO_FLAGS,
508 dflt_dpni->dpni_handle, &link_state);
510 printf("dpni_get_link_state() failed\n");
514 printf("DPNI link status: %d - ", link_state.up);
515 link_state.up == 0 ? printf("down\n") :
516 link_state.up == 1 ? printf("up\n") : printf("error state\n");
519 memset(&d_queue, 0, sizeof(struct dpni_queue));
520 err = dpni_get_queue(dflt_mc_io, MC_CMD_NO_FLAGS,
521 dflt_dpni->dpni_handle, DPNI_QUEUE_RX,
524 printf("dpni_get_queue failed\n");
528 priv->rx_dflt_fqid = d_queue.fqid;
530 err = dpni_get_qdid(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle,
533 printf("dpni_get_qdid() failed\n");
541 dpni_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
545 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
548 dpmac_close(dflt_mc_io, MC_CMD_NO_FLAGS, priv->dpmac_handle);
549 dpmac_destroy(dflt_mc_io,
551 MC_CMD_NO_FLAGS, priv->dpmac_id);
556 static void ldpaa_eth_stop(struct eth_device *net_dev)
558 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
561 struct mii_dev *bus = wriop_get_mdio(priv->dpmac_id);
563 struct phy_device *phydev = NULL;
565 if ((net_dev->state == ETH_STATE_PASSIVE) ||
566 (net_dev->state == ETH_STATE_INIT))
570 ldpaa_eth_get_dpni_counter();
571 ldpaa_eth_get_dpmac_counter(net_dev);
574 err = dprc_disconnect(dflt_mc_io, MC_CMD_NO_FLAGS,
575 dflt_dprc_handle, &dpmac_endpoint);
577 printf("dprc_disconnect() failed dpmac_endpoint\n");
579 err = dpmac_close(dflt_mc_io, MC_CMD_NO_FLAGS, priv->dpmac_handle);
581 printf("dpmac_close() failed\n");
583 err = dpmac_destroy(dflt_mc_io,
588 printf("dpmac_destroy() failed\n");
590 /* Stop Tx and Rx traffic */
591 err = dpni_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
593 printf("dpni_disable() failed\n");
596 phydev = wriop_get_phy_dev(priv->dpmac_id);
598 phy_shutdown(phydev);
601 wriop_set_phy_dev(priv->dpmac_id, NULL);
605 /* Free DPBP handle and reset. */
608 dpni_reset(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
610 printf("dpni_reset() failed\n");
612 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
614 printf("dpni_close() failed\n");
617 static void ldpaa_dpbp_drain_cnt(int count)
619 uint64_t buf_array[7];
626 ret = qbman_swp_acquire(dflt_dpio->sw_portal,
627 dflt_dpbp->dpbp_attr.bpid,
630 printf("qbman_swp_acquire() failed\n");
633 for (i = 0; i < ret; i++) {
634 addr = (void *)buf_array[i];
635 debug("Free: buffer addr =0x%p\n", addr);
641 static void ldpaa_dpbp_drain(void)
644 for (i = 0; i < LDPAA_ETH_NUM_BUFS; i += 7)
645 ldpaa_dpbp_drain_cnt(7);
648 static int ldpaa_bp_add_7(uint16_t bpid)
650 uint64_t buf_array[7];
653 struct qbman_release_desc rd;
655 for (i = 0; i < 7; i++) {
656 addr = memalign(LDPAA_ETH_BUF_ALIGN, LDPAA_ETH_RX_BUFFER_SIZE);
658 printf("addr allocation failed\n");
661 memset(addr, 0x00, LDPAA_ETH_RX_BUFFER_SIZE);
662 flush_dcache_range((u64)addr,
663 (u64)(addr + LDPAA_ETH_RX_BUFFER_SIZE));
665 buf_array[i] = (uint64_t)addr;
666 debug("Release: buffer addr =0x%p\n", addr);
670 /* In case the portal is busy, retry until successful.
671 * This function is guaranteed to succeed in a reasonable amount
677 qbman_release_desc_clear(&rd);
678 qbman_release_desc_set_bpid(&rd, bpid);
679 } while (qbman_swp_release(dflt_dpio->sw_portal, &rd, buf_array, i));
690 static int ldpaa_dpbp_seed(uint16_t bpid)
695 for (i = 0; i < LDPAA_ETH_NUM_BUFS; i += 7) {
696 count = ldpaa_bp_add_7(bpid);
698 printf("Buffer Seed= %d\n", count);
704 static int ldpaa_dpbp_setup(void)
708 err = dpbp_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_attr.id,
709 &dflt_dpbp->dpbp_handle);
711 printf("dpbp_open() failed\n");
715 err = dpbp_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
717 printf("dpbp_enable() failed\n");
721 err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
722 dflt_dpbp->dpbp_handle,
723 &dflt_dpbp->dpbp_attr);
725 printf("dpbp_get_attributes() failed\n");
729 err = ldpaa_dpbp_seed(dflt_dpbp->dpbp_attr.bpid);
732 printf("Buffer seeding failed for DPBP %d (bpid=%d)\n",
733 dflt_dpbp->dpbp_attr.id, dflt_dpbp->dpbp_attr.bpid);
741 dpbp_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
743 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
748 static void ldpaa_dpbp_free(void)
751 dpbp_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
752 dpbp_reset(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
753 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
756 static int ldpaa_dpmac_version_check(struct fsl_mc_io *mc_io,
757 struct ldpaa_eth_priv *priv)
760 uint16_t major_ver, minor_ver;
762 error = dpmac_get_api_version(dflt_mc_io, 0,
765 if ((major_ver < DPMAC_VER_MAJOR) ||
766 (major_ver == DPMAC_VER_MAJOR && minor_ver < DPMAC_VER_MINOR)) {
767 printf("DPMAC version mismatch found %u.%u,",
768 major_ver, minor_ver);
769 printf("supported version is %u.%u\n",
770 DPMAC_VER_MAJOR, DPMAC_VER_MINOR);
777 static int ldpaa_dpmac_setup(struct ldpaa_eth_priv *priv)
780 struct dpmac_cfg dpmac_cfg;
782 dpmac_cfg.mac_id = priv->dpmac_id;
784 err = dpmac_create(dflt_mc_io,
786 MC_CMD_NO_FLAGS, &dpmac_cfg,
789 printf("dpmac_create() failed\n");
791 err = ldpaa_dpmac_version_check(dflt_mc_io, priv);
793 printf("ldpaa_dpmac_version_check() failed: %d\n", err);
794 goto err_version_check;
797 err = dpmac_open(dflt_mc_io,
800 &priv->dpmac_handle);
802 printf("dpmac_open() failed: %d\n", err);
810 dpmac_destroy(dflt_mc_io,
812 MC_CMD_NO_FLAGS, priv->dpmac_id);
817 static int ldpaa_dpmac_bind(struct ldpaa_eth_priv *priv)
820 struct dprc_connection_cfg dprc_connection_cfg = {
821 /* If both rates are zero the connection */
822 /* will be configured in "best effort" mode. */
828 struct dprc_endpoint dbg_endpoint;
832 memset(&dpmac_endpoint, 0, sizeof(struct dprc_endpoint));
833 strcpy(dpmac_endpoint.type, "dpmac");
834 dpmac_endpoint.id = priv->dpmac_id;
836 memset(&dpni_endpoint, 0, sizeof(struct dprc_endpoint));
837 strcpy(dpni_endpoint.type, "dpni");
838 dpni_endpoint.id = dflt_dpni->dpni_id;
840 err = dprc_connect(dflt_mc_io, MC_CMD_NO_FLAGS,
844 &dprc_connection_cfg);
846 printf("dprc_connect() failed\n");
849 err = dprc_get_connection(dflt_mc_io, MC_CMD_NO_FLAGS,
850 dflt_dprc_handle, &dpni_endpoint,
851 &dbg_endpoint, &state);
852 printf("%s, DPMAC Type= %s\n", __func__, dbg_endpoint.type);
853 printf("%s, DPMAC ID= %d\n", __func__, dbg_endpoint.id);
854 printf("%s, DPMAC State= %d\n", __func__, state);
856 memset(&dbg_endpoint, 0, sizeof(struct dprc_endpoint));
857 err = dprc_get_connection(dflt_mc_io, MC_CMD_NO_FLAGS,
858 dflt_dprc_handle, &dpmac_endpoint,
859 &dbg_endpoint, &state);
860 printf("%s, DPNI Type= %s\n", __func__, dbg_endpoint.type);
861 printf("%s, DPNI ID= %d\n", __func__, dbg_endpoint.id);
862 printf("%s, DPNI State= %d\n", __func__, state);
867 static int ldpaa_dpni_setup(struct ldpaa_eth_priv *priv)
871 /* and get a handle for the DPNI this interface is associate with */
872 err = dpni_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_id,
873 &dflt_dpni->dpni_handle);
875 printf("dpni_open() failed\n");
878 err = dpni_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
879 dflt_dpni->dpni_handle,
880 &dflt_dpni->dpni_attrs);
882 printf("dpni_get_attributes() failed (err=%d)\n", err);
886 /* Configure our buffers' layout */
887 dflt_dpni->buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
888 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
889 DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
890 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN;
891 dflt_dpni->buf_layout.pass_parser_result = true;
892 dflt_dpni->buf_layout.pass_frame_status = true;
893 dflt_dpni->buf_layout.private_data_size = LDPAA_ETH_SWA_SIZE;
894 /* HW erratum mandates data alignment in multiples of 256 */
895 dflt_dpni->buf_layout.data_align = LDPAA_ETH_BUF_ALIGN;
898 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
899 dflt_dpni->dpni_handle,
900 &dflt_dpni->buf_layout, DPNI_QUEUE_RX);
902 printf("dpni_set_buffer_layout() failed");
907 /* remove Rx-only options */
908 dflt_dpni->buf_layout.options &= ~(DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
909 DPNI_BUF_LAYOUT_OPT_PARSER_RESULT);
910 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
911 dflt_dpni->dpni_handle,
912 &dflt_dpni->buf_layout, DPNI_QUEUE_TX);
914 printf("dpni_set_buffer_layout() failed");
918 /* ... tx-confirm. */
919 dflt_dpni->buf_layout.options &= ~DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE;
920 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
921 dflt_dpni->dpni_handle,
922 &dflt_dpni->buf_layout,
923 DPNI_QUEUE_TX_CONFIRM);
925 printf("dpni_set_buffer_layout() failed");
929 /* Now that we've set our tx buffer layout, retrieve the minimum
930 * required tx data offset.
932 err = dpni_get_tx_data_offset(dflt_mc_io, MC_CMD_NO_FLAGS,
933 dflt_dpni->dpni_handle,
934 &priv->tx_data_offset);
936 printf("dpni_get_tx_data_offset() failed\n");
937 goto err_data_offset;
940 /* Warn in case TX data offset is not multiple of 64 bytes. */
941 WARN_ON(priv->tx_data_offset % 64);
943 /* Accomodate SWA space. */
944 priv->tx_data_offset += LDPAA_ETH_SWA_SIZE;
945 debug("priv->tx_data_offset=%d\n", priv->tx_data_offset);
952 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
957 static int ldpaa_dpni_bind(struct ldpaa_eth_priv *priv)
959 struct dpni_pools_cfg pools_params;
960 struct dpni_queue tx_queue;
963 memset(&pools_params, 0, sizeof(pools_params));
964 pools_params.num_dpbp = 1;
965 pools_params.pools[0].dpbp_id = (uint16_t)dflt_dpbp->dpbp_attr.id;
966 pools_params.pools[0].buffer_size = LDPAA_ETH_RX_BUFFER_SIZE;
967 err = dpni_set_pools(dflt_mc_io, MC_CMD_NO_FLAGS,
968 dflt_dpni->dpni_handle, &pools_params);
970 printf("dpni_set_pools() failed\n");
974 memset(&tx_queue, 0, sizeof(struct dpni_queue));
976 err = dpni_set_queue(dflt_mc_io, MC_CMD_NO_FLAGS,
977 dflt_dpni->dpni_handle,
978 DPNI_QUEUE_TX, 0, 0, &tx_queue);
981 printf("dpni_set_queue() failed\n");
985 err = dpni_set_tx_confirmation_mode(dflt_mc_io, MC_CMD_NO_FLAGS,
986 dflt_dpni->dpni_handle,
989 printf("dpni_set_tx_confirmation_mode() failed\n");
996 static int ldpaa_eth_netdev_init(struct eth_device *net_dev,
997 phy_interface_t enet_if)
1000 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
1002 snprintf(net_dev->name, ETH_NAME_LEN, "DPMAC%d@%s", priv->dpmac_id,
1003 phy_interface_strings[enet_if]);
1005 net_dev->iobase = 0;
1006 net_dev->init = ldpaa_eth_open;
1007 net_dev->halt = ldpaa_eth_stop;
1008 net_dev->send = ldpaa_eth_tx;
1009 net_dev->recv = ldpaa_eth_pull_dequeue_rx;
1011 #ifdef CONFIG_PHYLIB
1012 err = init_phy(net_dev);
1017 err = eth_register(net_dev);
1019 printf("eth_register() = %d\n", err);
1026 int ldpaa_eth_init(int dpmac_id, phy_interface_t enet_if)
1028 struct eth_device *net_dev = NULL;
1029 struct ldpaa_eth_priv *priv = NULL;
1033 net_dev = (struct eth_device *)malloc(sizeof(struct eth_device));
1035 printf("eth_device malloc() failed\n");
1038 memset(net_dev, 0, sizeof(struct eth_device));
1040 /* alloc the ldpaa ethernet private struct */
1041 priv = (struct ldpaa_eth_priv *)malloc(sizeof(struct ldpaa_eth_priv));
1043 printf("ldpaa_eth_priv malloc() failed\n");
1046 memset(priv, 0, sizeof(struct ldpaa_eth_priv));
1048 net_dev->priv = (void *)priv;
1049 priv->net_dev = (struct eth_device *)net_dev;
1050 priv->dpmac_id = dpmac_id;
1051 debug("%s dpmac_id=%d\n", __func__, dpmac_id);
1053 err = ldpaa_eth_netdev_init(net_dev, enet_if);
1055 goto err_netdev_init;
1057 debug("ldpaa ethernet: Probed interface %s\n", net_dev->name);
1062 net_dev->priv = NULL;