ldpaa_eth.c 26 KB

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