ldpaa_eth.c 22 KB

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