blob: 73b7ba29dfdc031ff5fc9e30c11a23f1d599c970 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07002/*
Yogesh Gaur1a0c4ae2018-05-09 10:52:17 +05303 * Copyright 2014-2016 Freescale Semiconductor, Inc.
Yogesh Gaur318c32f2017-11-15 11:59:31 +05304 * Copyright 2017 NXP
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07005 */
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>
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +053015#include <fsl-mc/fsl_dpmac.h>
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -070016
Prabhakar Kushwahaef9329a2016-02-24 17:02:11 +053017#include <fsl-mc/ldpaa_wriop.h>
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -070018#include "ldpaa_eth.h"
19
Prabhakar Kushwahaef9329a2016-02-24 17:02:11 +053020#ifdef CONFIG_PHYLIB
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -070021static int init_phy(struct eth_device *dev)
22{
Prabhakar Kushwahaef9329a2016-02-24 17:02:11 +053023 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)dev->priv;
24 struct phy_device *phydev = NULL;
25 struct mii_dev *bus;
Pankaj Bansal50adb5e2018-10-10 14:08:34 +053026 int phy_addr, phy_num;
27 int ret = 0;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -070028
Prabhakar Kushwahaef9329a2016-02-24 17:02:11 +053029 bus = wriop_get_mdio(priv->dpmac_id);
30 if (bus == NULL)
31 return 0;
32
Pankaj Bansal50adb5e2018-10-10 14:08:34 +053033 for (phy_num = 0; phy_num < WRIOP_MAX_PHY_NUM; phy_num++) {
34 phy_addr = wriop_get_phy_address(priv->dpmac_id, phy_num);
35 if (phy_addr < 0)
36 continue;
Prabhakar Kushwahaef9329a2016-02-24 17:02:11 +053037
Pankaj Bansal50adb5e2018-10-10 14:08:34 +053038 phydev = phy_connect(bus, phy_addr, dev,
39 wriop_get_enet_if(priv->dpmac_id));
40 if (!phydev) {
41 printf("Failed to connect\n");
42 ret = -ENODEV;
43 break;
44 }
45 wriop_set_phy_dev(priv->dpmac_id, phy_num, phydev);
46 ret = phy_config(phydev);
47 if (ret)
48 break;
49 }
Pankaj Bansalff076d72018-10-10 14:08:31 +053050
51 if (ret) {
Pankaj Bansal50adb5e2018-10-10 14:08:34 +053052 for (phy_num = 0; phy_num < WRIOP_MAX_PHY_NUM; phy_num++) {
53 phydev = wriop_get_phy_dev(priv->dpmac_id, phy_num);
54 if (!phydev)
55 continue;
56
57 free(phydev);
58 wriop_set_phy_dev(priv->dpmac_id, phy_num, NULL);
59 }
Pankaj Bansalff076d72018-10-10 14:08:31 +053060 }
61
62 return ret;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -070063}
Prabhakar Kushwahaef9329a2016-02-24 17:02:11 +053064#endif
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -070065
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +053066#ifdef DEBUG
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +053067
Yogesh Gaur318c32f2017-11-15 11:59:31 +053068#define DPNI_STATS_PER_PAGE 6
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +053069
Yogesh Gaur318c32f2017-11-15 11:59:31 +053070static const char *dpni_statistics[][DPNI_STATS_PER_PAGE] = {
71 {
72 "DPNI_CNT_ING_ALL_FRAMES",
73 "DPNI_CNT_ING_ALL_BYTES",
74 "DPNI_CNT_ING_MCAST_FRAMES",
75 "DPNI_CNT_ING_MCAST_BYTES",
76 "DPNI_CNT_ING_BCAST_FRAMES",
77 "DPNI_CNT_ING_BCAST_BYTES",
78 }, {
79 "DPNI_CNT_EGR_ALL_FRAMES",
80 "DPNI_CNT_EGR_ALL_BYTES",
81 "DPNI_CNT_EGR_MCAST_FRAMES",
82 "DPNI_CNT_EGR_MCAST_BYTES",
83 "DPNI_CNT_EGR_BCAST_FRAMES",
84 "DPNI_CNT_EGR_BCAST_BYTES",
85 }, {
86 "DPNI_CNT_ING_FILTERED_FRAMES",
87 "DPNI_CNT_ING_DISCARDED_FRAMES",
88 "DPNI_CNT_ING_NOBUFFER_DISCARDS",
89 "DPNI_CNT_EGR_DISCARDED_FRAMES",
90 "DPNI_CNT_EGR_CNF_FRAMES",
91 ""
92 },
93};
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +053094
Yogesh Gaur318c32f2017-11-15 11:59:31 +053095static void print_dpni_stats(const char *strings[],
96 struct dpni_statistics dpni_stats)
97{
98 uint64_t *stat;
99 int i;
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530100
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530101 stat = (uint64_t *)&dpni_stats;
102 for (i = 0; i < DPNI_STATS_PER_PAGE; i++) {
103 if (strcmp(strings[i], "\0") == 0)
104 break;
105 printf("%s= %llu\n", strings[i], *stat);
106 stat++;
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530107 }
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530108}
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530109
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530110static void ldpaa_eth_get_dpni_counter(void)
111{
112 int err = 0;
113 unsigned int page = 0;
114 struct dpni_statistics dpni_stats;
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530115
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530116 printf("DPNI counters ..\n");
117 for (page = 0; page < 3; page++) {
118 err = dpni_get_statistics(dflt_mc_io, MC_CMD_NO_FLAGS,
119 dflt_dpni->dpni_handle, page,
120 &dpni_stats);
121 if (err < 0) {
122 printf("dpni_get_statistics: failed:");
123 printf("%d for page[%d]\n", err, page);
124 return;
125 }
126 print_dpni_stats(dpni_statistics[page], dpni_stats);
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530127 }
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530128}
Prabhakar Kushwaha314aa552015-12-24 15:33:37 +0530129
130static void ldpaa_eth_get_dpmac_counter(struct eth_device *net_dev)
131{
132 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
133 int err = 0;
134 u64 value;
135
136 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
137 priv->dpmac_handle,
138 DPMAC_CNT_ING_BYTE,
139 &value);
140 if (err < 0) {
141 printf("dpmac_get_counter: DPMAC_CNT_ING_BYTE failed\n");
142 return;
143 }
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530144 printf("\nDPMAC counters ..\n");
Prabhakar Kushwaha314aa552015-12-24 15:33:37 +0530145 printf("DPMAC_CNT_ING_BYTE=%lld\n", value);
146
147 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
148 priv->dpmac_handle,
149 DPMAC_CNT_ING_FRAME_DISCARD,
150 &value);
151 if (err < 0) {
152 printf("dpmac_get_counter: DPMAC_CNT_ING_FRAME_DISCARD failed\n");
153 return;
154 }
155 printf("DPMAC_CNT_ING_FRAME_DISCARD=%lld\n", value);
156
157 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
158 priv->dpmac_handle,
159 DPMAC_CNT_ING_ALIGN_ERR,
160 &value);
161 if (err < 0) {
162 printf("dpmac_get_counter: DPMAC_CNT_ING_ALIGN_ERR failed\n");
163 return;
164 }
165 printf("DPMAC_CNT_ING_ALIGN_ERR =%lld\n", value);
166
167 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
168 priv->dpmac_handle,
169 DPMAC_CNT_ING_BYTE,
170 &value);
171 if (err < 0) {
172 printf("dpmac_get_counter: DPMAC_CNT_ING_BYTE failed\n");
173 return;
174 }
175 printf("DPMAC_CNT_ING_BYTE=%lld\n", value);
176
177 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
178 priv->dpmac_handle,
179 DPMAC_CNT_ING_ERR_FRAME,
180 &value);
181 if (err < 0) {
182 printf("dpmac_get_counter: DPMAC_CNT_ING_ERR_FRAME failed\n");
183 return;
184 }
185 printf("DPMAC_CNT_ING_ERR_FRAME=%lld\n", value);
186
187 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
188 priv->dpmac_handle,
189 DPMAC_CNT_EGR_BYTE ,
190 &value);
191 if (err < 0) {
192 printf("dpmac_get_counter: DPMAC_CNT_EGR_BYTE failed\n");
193 return;
194 }
195 printf("DPMAC_CNT_EGR_BYTE =%lld\n", value);
196
197 err = dpmac_get_counter(dflt_mc_io, MC_CMD_NO_FLAGS,
198 priv->dpmac_handle,
199 DPMAC_CNT_EGR_ERR_FRAME ,
200 &value);
201 if (err < 0) {
202 printf("dpmac_get_counter: DPMAC_CNT_EGR_ERR_FRAME failed\n");
203 return;
204 }
205 printf("DPMAC_CNT_EGR_ERR_FRAME =%lld\n", value);
206}
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530207#endif
208
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700209static void ldpaa_eth_rx(struct ldpaa_eth_priv *priv,
210 const struct dpaa_fd *fd)
211{
212 u64 fd_addr;
213 uint16_t fd_offset;
214 uint32_t fd_length;
215 struct ldpaa_fas *fas;
216 uint32_t status, err;
Prabhakar Kushwahac7d10d82015-07-02 11:29:08 +0530217 u32 timeo = (CONFIG_SYS_HZ * 2) / 1000;
218 u32 time_start;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700219 struct qbman_release_desc releasedesc;
220 struct qbman_swp *swp = dflt_dpio->sw_portal;
221
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700222 fd_addr = ldpaa_fd_get_addr(fd);
223 fd_offset = ldpaa_fd_get_offset(fd);
224 fd_length = ldpaa_fd_get_len(fd);
225
226 debug("Rx frame:data addr=0x%p size=0x%x\n", (u64 *)fd_addr, fd_length);
227
228 if (fd->simple.frc & LDPAA_FD_FRC_FASV) {
229 /* Read the frame annotation status word and check for errors */
230 fas = (struct ldpaa_fas *)
231 ((uint8_t *)(fd_addr) +
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530232 dflt_dpni->buf_layout.private_data_size);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700233 status = le32_to_cpu(fas->status);
234 if (status & LDPAA_ETH_RX_ERR_MASK) {
235 printf("Rx frame error(s): 0x%08x\n",
236 status & LDPAA_ETH_RX_ERR_MASK);
237 goto error;
238 } else if (status & LDPAA_ETH_RX_UNSUPP_MASK) {
239 printf("Unsupported feature in bitmask: 0x%08x\n",
240 status & LDPAA_ETH_RX_UNSUPP_MASK);
241 goto error;
242 }
243 }
244
245 debug("Rx frame: To Upper layer\n");
246 net_process_received_packet((uint8_t *)(fd_addr) + fd_offset,
247 fd_length);
248
249error:
Prabhakar Kushwahadbe0f2a2015-03-20 19:28:15 -0700250 flush_dcache_range(fd_addr, fd_addr + LDPAA_ETH_RX_BUFFER_SIZE);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700251 qbman_release_desc_clear(&releasedesc);
252 qbman_release_desc_set_bpid(&releasedesc, dflt_dpbp->dpbp_attr.bpid);
Prabhakar Kushwahac7d10d82015-07-02 11:29:08 +0530253 time_start = get_timer(0);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700254 do {
255 /* Release buffer into the QBMAN */
256 err = qbman_swp_release(swp, &releasedesc, &fd_addr, 1);
Prabhakar Kushwahac7d10d82015-07-02 11:29:08 +0530257 } while (get_timer(time_start) < timeo && err == -EBUSY);
258
259 if (err == -EBUSY)
260 printf("Rx frame: QBMAN buffer release fails\n");
261
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700262 return;
263}
264
265static int ldpaa_eth_pull_dequeue_rx(struct eth_device *dev)
266{
267 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)dev->priv;
268 const struct ldpaa_dq *dq;
269 const struct dpaa_fd *fd;
Prabhakar Kushwahabe0c9552015-07-02 11:29:06 +0530270 int i = 5, err = 0, status;
271 u32 timeo = (CONFIG_SYS_HZ * 2) / 1000;
272 u32 time_start;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700273 static struct qbman_pull_desc pulldesc;
274 struct qbman_swp *swp = dflt_dpio->sw_portal;
275
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700276 while (--i) {
Prabhakar Kushwahadbe0f2a2015-03-20 19:28:15 -0700277 qbman_pull_desc_clear(&pulldesc);
278 qbman_pull_desc_set_numframes(&pulldesc, 1);
279 qbman_pull_desc_set_fq(&pulldesc, priv->rx_dflt_fqid);
280
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700281 err = qbman_swp_pull(swp, &pulldesc);
282 if (err < 0) {
283 printf("Dequeue frames error:0x%08x\n", err);
284 continue;
285 }
286
Prabhakar Kushwahabe0c9552015-07-02 11:29:06 +0530287 time_start = get_timer(0);
Prabhakar Kushwahadbe0f2a2015-03-20 19:28:15 -0700288
Prabhakar Kushwahabe0c9552015-07-02 11:29:06 +0530289 do {
290 dq = qbman_swp_dqrr_next(swp);
291 } while (get_timer(time_start) < timeo && !dq);
Prabhakar Kushwahadbe0f2a2015-03-20 19:28:15 -0700292
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700293 if (dq) {
294 /* Check for valid frame. If not sent a consume
295 * confirmation to QBMAN otherwise give it to NADK
296 * application and then send consume confirmation to
297 * QBMAN.
298 */
299 status = (uint8_t)ldpaa_dq_flags(dq);
300 if ((status & LDPAA_DQ_STAT_VALIDFRAME) == 0) {
301 debug("Dequeue RX frames:");
302 debug("No frame delivered\n");
303
304 qbman_swp_dqrr_consume(swp, dq);
Prabhakar Kushwaha37ed1a62015-07-02 11:29:07 +0530305 continue;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700306 }
307
308 fd = ldpaa_dq_fd(dq);
309
310 /* Obtain FD and process it */
311 ldpaa_eth_rx(priv, fd);
312 qbman_swp_dqrr_consume(swp, dq);
313 break;
Prabhakar Kushwahabe0c9552015-07-02 11:29:06 +0530314 } else {
315 err = -ENODATA;
316 debug("No DQRR entries\n");
317 break;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700318 }
319 }
320
321 return err;
322}
323
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700324static int ldpaa_eth_tx(struct eth_device *net_dev, void *buf, int len)
325{
326 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
327 struct dpaa_fd fd;
328 u64 buffer_start;
329 int data_offset, err;
Prabhakar Kushwaha20c30102015-07-02 11:29:05 +0530330 u32 timeo = (CONFIG_SYS_HZ * 10) / 1000;
331 u32 time_start;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700332 struct qbman_swp *swp = dflt_dpio->sw_portal;
333 struct qbman_eq_desc ed;
Prabhakar Kushwaha20c30102015-07-02 11:29:05 +0530334 struct qbman_release_desc releasedesc;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700335
336 /* Setup the FD fields */
337 memset(&fd, 0, sizeof(fd));
338
339 data_offset = priv->tx_data_offset;
340
341 do {
342 err = qbman_swp_acquire(dflt_dpio->sw_portal,
343 dflt_dpbp->dpbp_attr.bpid,
344 &buffer_start, 1);
345 } while (err == -EBUSY);
346
Ashish Kumar34f080c2017-10-26 16:03:22 +0530347 if (err <= 0) {
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700348 printf("qbman_swp_acquire() failed\n");
349 return -ENOMEM;
350 }
351
352 debug("TX data: malloc buffer start=0x%p\n", (u64 *)buffer_start);
353
354 memcpy(((uint8_t *)(buffer_start) + data_offset), buf, len);
355
Prabhakar Kushwahadbe0f2a2015-03-20 19:28:15 -0700356 flush_dcache_range(buffer_start, buffer_start +
357 LDPAA_ETH_RX_BUFFER_SIZE);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700358
359 ldpaa_fd_set_addr(&fd, (u64)buffer_start);
360 ldpaa_fd_set_offset(&fd, (uint16_t)(data_offset));
361 ldpaa_fd_set_bpid(&fd, dflt_dpbp->dpbp_attr.bpid);
362 ldpaa_fd_set_len(&fd, len);
363
364 fd.simple.ctrl = LDPAA_FD_CTRL_ASAL | LDPAA_FD_CTRL_PTA |
365 LDPAA_FD_CTRL_PTV1;
366
367 qbman_eq_desc_clear(&ed);
368 qbman_eq_desc_set_no_orp(&ed, 0);
369 qbman_eq_desc_set_qd(&ed, priv->tx_qdid, priv->tx_flow_id, 0);
Prabhakar Kushwaha20c30102015-07-02 11:29:05 +0530370
371 time_start = get_timer(0);
372
373 while (get_timer(time_start) < timeo) {
374 err = qbman_swp_enqueue(swp, &ed,
375 (const struct qbman_fd *)(&fd));
376 if (err != -EBUSY)
377 break;
378 }
379
380 if (err < 0) {
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700381 printf("error enqueueing Tx frame\n");
Prabhakar Kushwaha20c30102015-07-02 11:29:05 +0530382 goto error;
383 }
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700384
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700385 return err;
Prabhakar Kushwaha20c30102015-07-02 11:29:05 +0530386
387error:
388 qbman_release_desc_clear(&releasedesc);
389 qbman_release_desc_set_bpid(&releasedesc, dflt_dpbp->dpbp_attr.bpid);
390 time_start = get_timer(0);
391 do {
392 /* Release buffer into the QBMAN */
393 err = qbman_swp_release(swp, &releasedesc, &buffer_start, 1);
394 } while (get_timer(time_start) < timeo && err == -EBUSY);
395
396 if (err == -EBUSY)
397 printf("TX data: QBMAN buffer release fails\n");
398
399 return err;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700400}
401
Pankaj Bansale031d262018-10-10 14:08:32 +0530402static int ldpaa_get_dpmac_state(struct ldpaa_eth_priv *priv,
403 struct dpmac_link_state *state)
404{
Pankaj Bansale031d262018-10-10 14:08:32 +0530405 phy_interface_t enet_if;
Tom Rini13da6252018-10-11 20:07:48 -0400406 int phys_detected;
407#ifdef CONFIG_PHYLIB
408 struct phy_device *phydev = NULL;
409 int err, phy_num;
410#endif
Pankaj Bansale031d262018-10-10 14:08:32 +0530411
Pankaj Bansal50adb5e2018-10-10 14:08:34 +0530412 /* let's start off with maximum capabilities */
Pankaj Bansale031d262018-10-10 14:08:32 +0530413 enet_if = wriop_get_enet_if(priv->dpmac_id);
414 switch (enet_if) {
415 case PHY_INTERFACE_MODE_XGMII:
416 state->rate = SPEED_10000;
417 break;
418 default:
419 state->rate = SPEED_1000;
420 break;
421 }
422 state->up = 1;
423
Pankaj Bansal50adb5e2018-10-10 14:08:34 +0530424 phys_detected = 0;
Pankaj Bansale031d262018-10-10 14:08:32 +0530425#ifdef CONFIG_PHYLIB
426 state->options |= DPMAC_LINK_OPT_AUTONEG;
427
Pankaj Bansal50adb5e2018-10-10 14:08:34 +0530428 /* start the phy devices one by one and update the dpmac state */
429 for (phy_num = 0; phy_num < WRIOP_MAX_PHY_NUM; phy_num++) {
430 phydev = wriop_get_phy_dev(priv->dpmac_id, phy_num);
431 if (!phydev)
432 continue;
433
434 phys_detected++;
Pankaj Bansale031d262018-10-10 14:08:32 +0530435 err = phy_startup(phydev);
436 if (err) {
437 printf("%s: Could not initialize\n", phydev->dev->name);
438 state->up = 0;
Pankaj Bansal50adb5e2018-10-10 14:08:34 +0530439 break;
Pankaj Bansale031d262018-10-10 14:08:32 +0530440 }
441 if (phydev->link) {
442 state->rate = min(state->rate, (uint32_t)phydev->speed);
443 if (!phydev->duplex)
444 state->options |= DPMAC_LINK_OPT_HALF_DUPLEX;
445 if (!phydev->autoneg)
446 state->options &= ~DPMAC_LINK_OPT_AUTONEG;
447 } else {
Pankaj Bansal50adb5e2018-10-10 14:08:34 +0530448 /* break out of loop even if one phy is down */
Pankaj Bansale031d262018-10-10 14:08:32 +0530449 state->up = 0;
Pankaj Bansal50adb5e2018-10-10 14:08:34 +0530450 break;
Pankaj Bansale031d262018-10-10 14:08:32 +0530451 }
452 }
453#endif
Pankaj Bansal50adb5e2018-10-10 14:08:34 +0530454 if (!phys_detected)
Pankaj Bansale031d262018-10-10 14:08:32 +0530455 state->options &= ~DPMAC_LINK_OPT_AUTONEG;
456
457 if (!state->up) {
458 state->rate = 0;
459 state->options = 0;
460 return -ENOLINK;
461 }
462
463 return 0;
464}
465
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700466static int ldpaa_eth_open(struct eth_device *net_dev, bd_t *bd)
467{
468 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530469 struct dpmac_link_state dpmac_link_state = { 0 };
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530470#ifdef DEBUG
471 struct dpni_link_state link_state;
472#endif
Prabhakar Kushwahaef9329a2016-02-24 17:02:11 +0530473 int err = 0;
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530474 struct dpni_queue d_queue;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700475
476 if (net_dev->state == ETH_STATE_ACTIVE)
477 return 0;
478
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530479 if (get_mc_boot_status() != 0) {
480 printf("ERROR (MC is not booted)\n");
481 return -ENODEV;
482 }
483
484 if (get_dpl_apply_status() == 0) {
485 printf("ERROR (DPL is deployed. No device available)\n");
486 return -ENODEV;
487 }
Prabhakar Kushwahaef9329a2016-02-24 17:02:11 +0530488
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530489 /* DPMAC initialization */
490 err = ldpaa_dpmac_setup(priv);
491 if (err < 0)
492 goto err_dpmac_setup;
493
Pankaj Bansale031d262018-10-10 14:08:32 +0530494 err = ldpaa_get_dpmac_state(priv, &dpmac_link_state);
495 if (err < 0)
Pankaj Bansald24b1282018-10-10 14:08:29 +0530496 goto err_dpmac_bind;
Prabhakar Kushwahaef9329a2016-02-24 17:02:11 +0530497
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530498 /* DPMAC binding DPNI */
499 err = ldpaa_dpmac_bind(priv);
500 if (err)
Pankaj Bansald24b1282018-10-10 14:08:29 +0530501 goto err_dpmac_bind;
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530502
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700503 /* DPNI initialization */
504 err = ldpaa_dpni_setup(priv);
505 if (err < 0)
506 goto err_dpni_setup;
507
508 err = ldpaa_dpbp_setup();
509 if (err < 0)
510 goto err_dpbp_setup;
511
512 /* DPNI binding DPBP */
513 err = ldpaa_dpni_bind(priv);
514 if (err)
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530515 goto err_dpni_bind;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700516
Prabhakar Kushwahad2c20a32015-10-07 16:29:58 +0530517 err = dpni_add_mac_addr(dflt_mc_io, MC_CMD_NO_FLAGS,
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530518 dflt_dpni->dpni_handle, net_dev->enetaddr);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700519 if (err) {
Prabhakar Kushwahad2c20a32015-10-07 16:29:58 +0530520 printf("dpni_add_mac_addr() failed\n");
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700521 return err;
522 }
523
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530524 err = dpni_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700525 if (err < 0) {
526 printf("dpni_enable() failed\n");
527 return err;
528 }
529
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530530 err = dpmac_set_link_state(dflt_mc_io, MC_CMD_NO_FLAGS,
531 priv->dpmac_handle, &dpmac_link_state);
532 if (err < 0) {
533 printf("dpmac_set_link_state() failed\n");
534 return err;
535 }
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530536
537#ifdef DEBUG
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530538 printf("DPMAC link status: %d - ", dpmac_link_state.up);
539 dpmac_link_state.up == 0 ? printf("down\n") :
540 dpmac_link_state.up == 1 ? printf("up\n") : printf("error state\n");
541
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530542 err = dpni_get_link_state(dflt_mc_io, MC_CMD_NO_FLAGS,
543 dflt_dpni->dpni_handle, &link_state);
544 if (err < 0) {
545 printf("dpni_get_link_state() failed\n");
546 return err;
547 }
548
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530549 printf("DPNI link status: %d - ", link_state.up);
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530550 link_state.up == 0 ? printf("down\n") :
551 link_state.up == 1 ? printf("up\n") : printf("error state\n");
552#endif
553
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530554 memset(&d_queue, 0, sizeof(struct dpni_queue));
555 err = dpni_get_queue(dflt_mc_io, MC_CMD_NO_FLAGS,
556 dflt_dpni->dpni_handle, DPNI_QUEUE_RX,
557 0, 0, &d_queue);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700558 if (err) {
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530559 printf("dpni_get_queue failed\n");
560 goto err_get_queue;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700561 }
562
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530563 priv->rx_dflt_fqid = d_queue.fqid;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700564
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530565 err = dpni_get_qdid(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle,
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +0530566 &priv->tx_qdid);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700567 if (err) {
568 printf("dpni_get_qdid() failed\n");
569 goto err_qdid;
570 }
571
Pankaj Bansale031d262018-10-10 14:08:32 +0530572 return dpmac_link_state.up;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700573
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700574err_qdid:
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530575err_get_queue:
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530576 dpni_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
577err_dpni_bind:
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700578 ldpaa_dpbp_free();
579err_dpbp_setup:
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530580 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700581err_dpni_setup:
Pankaj Bansald24b1282018-10-10 14:08:29 +0530582err_dpmac_bind:
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530583 dpmac_close(dflt_mc_io, MC_CMD_NO_FLAGS, priv->dpmac_handle);
584 dpmac_destroy(dflt_mc_io,
585 dflt_dprc_handle,
586 MC_CMD_NO_FLAGS, priv->dpmac_id);
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530587err_dpmac_setup:
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700588 return err;
589}
590
591static void ldpaa_eth_stop(struct eth_device *net_dev)
592{
593 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
594 int err = 0;
Tom Rini13da6252018-10-11 20:07:48 -0400595#ifdef CONFIG_PHYLIB
Pankaj Bansal1c2289d2018-10-10 14:08:30 +0530596 struct phy_device *phydev = NULL;
Pankaj Bansal50adb5e2018-10-10 14:08:34 +0530597 int phy_num;
Tom Rini13da6252018-10-11 20:07:48 -0400598#endif
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700599
Prabhakar Kushwahadbe0f2a2015-03-20 19:28:15 -0700600 if ((net_dev->state == ETH_STATE_PASSIVE) ||
601 (net_dev->state == ETH_STATE_INIT))
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700602 return;
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530603
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530604#ifdef DEBUG
605 ldpaa_eth_get_dpni_counter();
Prabhakar Kushwaha314aa552015-12-24 15:33:37 +0530606 ldpaa_eth_get_dpmac_counter(net_dev);
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530607#endif
608
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530609 err = dprc_disconnect(dflt_mc_io, MC_CMD_NO_FLAGS,
610 dflt_dprc_handle, &dpmac_endpoint);
611 if (err < 0)
612 printf("dprc_disconnect() failed dpmac_endpoint\n");
613
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530614 err = dpmac_close(dflt_mc_io, MC_CMD_NO_FLAGS, priv->dpmac_handle);
615 if (err < 0)
616 printf("dpmac_close() failed\n");
617
618 err = dpmac_destroy(dflt_mc_io,
619 dflt_dprc_handle,
620 MC_CMD_NO_FLAGS,
621 priv->dpmac_id);
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530622 if (err < 0)
623 printf("dpmac_destroy() failed\n");
624
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700625 /* Stop Tx and Rx traffic */
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530626 err = dpni_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700627 if (err < 0)
628 printf("dpni_disable() failed\n");
629
630#ifdef CONFIG_PHYLIB
Pankaj Bansal50adb5e2018-10-10 14:08:34 +0530631 for (phy_num = 0; phy_num < WRIOP_MAX_PHY_NUM; phy_num++) {
632 phydev = wriop_get_phy_dev(priv->dpmac_id, phy_num);
633 if (phydev)
634 phy_shutdown(phydev);
635 }
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700636#endif
637
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530638 /* Free DPBP handle and reset. */
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700639 ldpaa_dpbp_free();
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530640
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530641 dpni_reset(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530642 if (err < 0)
643 printf("dpni_reset() failed\n");
644
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530645 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530646 if (err < 0)
647 printf("dpni_close() failed\n");
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700648}
649
650static void ldpaa_dpbp_drain_cnt(int count)
651{
652 uint64_t buf_array[7];
653 void *addr;
654 int ret, i;
655
656 BUG_ON(count > 7);
657
658 do {
659 ret = qbman_swp_acquire(dflt_dpio->sw_portal,
660 dflt_dpbp->dpbp_attr.bpid,
661 buf_array, count);
662 if (ret < 0) {
663 printf("qbman_swp_acquire() failed\n");
664 return;
665 }
666 for (i = 0; i < ret; i++) {
667 addr = (void *)buf_array[i];
668 debug("Free: buffer addr =0x%p\n", addr);
669 free(addr);
670 }
671 } while (ret);
672}
673
674static void ldpaa_dpbp_drain(void)
675{
676 int i;
677 for (i = 0; i < LDPAA_ETH_NUM_BUFS; i += 7)
678 ldpaa_dpbp_drain_cnt(7);
679}
680
681static int ldpaa_bp_add_7(uint16_t bpid)
682{
683 uint64_t buf_array[7];
684 u8 *addr;
685 int i;
686 struct qbman_release_desc rd;
687
688 for (i = 0; i < 7; i++) {
Prabhakar Kushwaha9a934802015-11-04 12:26:02 +0530689 addr = memalign(LDPAA_ETH_BUF_ALIGN, LDPAA_ETH_RX_BUFFER_SIZE);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700690 if (!addr) {
691 printf("addr allocation failed\n");
692 goto err_alloc;
693 }
694 memset(addr, 0x00, LDPAA_ETH_RX_BUFFER_SIZE);
Prabhakar Kushwahadbe0f2a2015-03-20 19:28:15 -0700695 flush_dcache_range((u64)addr,
696 (u64)(addr + LDPAA_ETH_RX_BUFFER_SIZE));
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700697
698 buf_array[i] = (uint64_t)addr;
699 debug("Release: buffer addr =0x%p\n", addr);
700 }
701
702release_bufs:
703 /* In case the portal is busy, retry until successful.
704 * This function is guaranteed to succeed in a reasonable amount
705 * of time.
706 */
707
708 do {
709 mdelay(1);
710 qbman_release_desc_clear(&rd);
711 qbman_release_desc_set_bpid(&rd, bpid);
712 } while (qbman_swp_release(dflt_dpio->sw_portal, &rd, buf_array, i));
713
714 return i;
715
716err_alloc:
717 if (i)
718 goto release_bufs;
719
720 return 0;
721}
722
723static int ldpaa_dpbp_seed(uint16_t bpid)
724{
725 int i;
726 int count;
727
728 for (i = 0; i < LDPAA_ETH_NUM_BUFS; i += 7) {
729 count = ldpaa_bp_add_7(bpid);
730 if (count < 7)
731 printf("Buffer Seed= %d\n", count);
732 }
733
734 return 0;
735}
736
737static int ldpaa_dpbp_setup(void)
738{
739 int err;
740
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +0530741 err = dpbp_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_attr.id,
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700742 &dflt_dpbp->dpbp_handle);
743 if (err) {
744 printf("dpbp_open() failed\n");
745 goto err_open;
746 }
747
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +0530748 err = dpbp_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700749 if (err) {
750 printf("dpbp_enable() failed\n");
751 goto err_enable;
752 }
753
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +0530754 err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
755 dflt_dpbp->dpbp_handle,
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700756 &dflt_dpbp->dpbp_attr);
757 if (err) {
758 printf("dpbp_get_attributes() failed\n");
759 goto err_get_attr;
760 }
761
762 err = ldpaa_dpbp_seed(dflt_dpbp->dpbp_attr.bpid);
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530763
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700764 if (err) {
765 printf("Buffer seeding failed for DPBP %d (bpid=%d)\n",
766 dflt_dpbp->dpbp_attr.id, dflt_dpbp->dpbp_attr.bpid);
767 goto err_seed;
768 }
769
770 return 0;
771
772err_seed:
773err_get_attr:
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +0530774 dpbp_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700775err_enable:
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +0530776 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700777err_open:
778 return err;
779}
780
781static void ldpaa_dpbp_free(void)
782{
783 ldpaa_dpbp_drain();
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +0530784 dpbp_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
785 dpbp_reset(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
786 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700787}
788
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530789static int ldpaa_dpmac_version_check(struct fsl_mc_io *mc_io,
790 struct ldpaa_eth_priv *priv)
791{
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530792 int error;
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530793 uint16_t major_ver, minor_ver;
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530794
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530795 error = dpmac_get_api_version(dflt_mc_io, 0,
796 &major_ver,
797 &minor_ver);
798 if ((major_ver < DPMAC_VER_MAJOR) ||
799 (major_ver == DPMAC_VER_MAJOR && minor_ver < DPMAC_VER_MINOR)) {
800 printf("DPMAC version mismatch found %u.%u,",
801 major_ver, minor_ver);
802 printf("supported version is %u.%u\n",
803 DPMAC_VER_MAJOR, DPMAC_VER_MINOR);
804 return error;
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530805 }
806
807 return error;
808}
809
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530810static int ldpaa_dpmac_setup(struct ldpaa_eth_priv *priv)
811{
812 int err = 0;
813 struct dpmac_cfg dpmac_cfg;
814
815 dpmac_cfg.mac_id = priv->dpmac_id;
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530816
817 err = dpmac_create(dflt_mc_io,
818 dflt_dprc_handle,
819 MC_CMD_NO_FLAGS, &dpmac_cfg,
820 &priv->dpmac_id);
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530821 if (err)
822 printf("dpmac_create() failed\n");
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530823
824 err = ldpaa_dpmac_version_check(dflt_mc_io, priv);
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530825 if (err < 0) {
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530826 printf("ldpaa_dpmac_version_check() failed: %d\n", err);
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530827 goto err_version_check;
828 }
829
830 err = dpmac_open(dflt_mc_io,
831 MC_CMD_NO_FLAGS,
832 priv->dpmac_id,
833 &priv->dpmac_handle);
834 if (err < 0) {
835 printf("dpmac_open() failed: %d\n", err);
836 goto err_open;
837 }
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530838
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530839 return err;
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530840
841err_open:
842err_version_check:
843 dpmac_destroy(dflt_mc_io,
844 dflt_dprc_handle,
845 MC_CMD_NO_FLAGS, priv->dpmac_id);
846
847 return err;
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530848}
849
850static int ldpaa_dpmac_bind(struct ldpaa_eth_priv *priv)
851{
852 int err = 0;
853 struct dprc_connection_cfg dprc_connection_cfg = {
854 /* If both rates are zero the connection */
855 /* will be configured in "best effort" mode. */
856 .committed_rate = 0,
857 .max_rate = 0
858 };
859
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530860#ifdef DEBUG
861 struct dprc_endpoint dbg_endpoint;
862 int state = 0;
863#endif
864
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530865 memset(&dpmac_endpoint, 0, sizeof(struct dprc_endpoint));
Ben Whitten34fd6c92015-12-30 13:05:58 +0000866 strcpy(dpmac_endpoint.type, "dpmac");
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530867 dpmac_endpoint.id = priv->dpmac_id;
868
869 memset(&dpni_endpoint, 0, sizeof(struct dprc_endpoint));
Ben Whitten34fd6c92015-12-30 13:05:58 +0000870 strcpy(dpni_endpoint.type, "dpni");
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530871 dpni_endpoint.id = dflt_dpni->dpni_id;
872
873 err = dprc_connect(dflt_mc_io, MC_CMD_NO_FLAGS,
874 dflt_dprc_handle,
875 &dpmac_endpoint,
876 &dpni_endpoint,
877 &dprc_connection_cfg);
Prabhakar Kushwahab4af96f2015-11-04 12:26:01 +0530878 if (err)
879 printf("dprc_connect() failed\n");
880
881#ifdef DEBUG
882 err = dprc_get_connection(dflt_mc_io, MC_CMD_NO_FLAGS,
883 dflt_dprc_handle, &dpni_endpoint,
884 &dbg_endpoint, &state);
885 printf("%s, DPMAC Type= %s\n", __func__, dbg_endpoint.type);
886 printf("%s, DPMAC ID= %d\n", __func__, dbg_endpoint.id);
887 printf("%s, DPMAC State= %d\n", __func__, state);
888
889 memset(&dbg_endpoint, 0, sizeof(struct dprc_endpoint));
890 err = dprc_get_connection(dflt_mc_io, MC_CMD_NO_FLAGS,
891 dflt_dprc_handle, &dpmac_endpoint,
892 &dbg_endpoint, &state);
893 printf("%s, DPNI Type= %s\n", __func__, dbg_endpoint.type);
894 printf("%s, DPNI ID= %d\n", __func__, dbg_endpoint.id);
895 printf("%s, DPNI State= %d\n", __func__, state);
896#endif
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530897 return err;
898}
899
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700900static int ldpaa_dpni_setup(struct ldpaa_eth_priv *priv)
901{
902 int err;
903
904 /* and get a handle for the DPNI this interface is associate with */
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530905 err = dpni_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_id,
906 &dflt_dpni->dpni_handle);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700907 if (err) {
908 printf("dpni_open() failed\n");
909 goto err_open;
910 }
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +0530911 err = dpni_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530912 dflt_dpni->dpni_handle,
913 &dflt_dpni->dpni_attrs);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700914 if (err) {
915 printf("dpni_get_attributes() failed (err=%d)\n", err);
916 goto err_get_attr;
917 }
918
919 /* Configure our buffers' layout */
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530920 dflt_dpni->buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700921 DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
Prabhakar Kushwaha9a934802015-11-04 12:26:02 +0530922 DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
923 DPNI_BUF_LAYOUT_OPT_DATA_ALIGN;
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530924 dflt_dpni->buf_layout.pass_parser_result = true;
925 dflt_dpni->buf_layout.pass_frame_status = true;
926 dflt_dpni->buf_layout.private_data_size = LDPAA_ETH_SWA_SIZE;
Prabhakar Kushwaha9a934802015-11-04 12:26:02 +0530927 /* HW erratum mandates data alignment in multiples of 256 */
928 dflt_dpni->buf_layout.data_align = LDPAA_ETH_BUF_ALIGN;
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530929
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700930 /* ...rx, ... */
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530931 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
932 dflt_dpni->dpni_handle,
933 &dflt_dpni->buf_layout, DPNI_QUEUE_RX);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700934 if (err) {
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530935 printf("dpni_set_buffer_layout() failed");
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700936 goto err_buf_layout;
937 }
938
939 /* ... tx, ... */
Prabhakar Kushwaha9a934802015-11-04 12:26:02 +0530940 /* remove Rx-only options */
941 dflt_dpni->buf_layout.options &= ~(DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
942 DPNI_BUF_LAYOUT_OPT_PARSER_RESULT);
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530943 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
944 dflt_dpni->dpni_handle,
945 &dflt_dpni->buf_layout, DPNI_QUEUE_TX);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700946 if (err) {
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530947 printf("dpni_set_buffer_layout() failed");
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700948 goto err_buf_layout;
949 }
950
951 /* ... tx-confirm. */
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530952 dflt_dpni->buf_layout.options &= ~DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE;
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530953 err = dpni_set_buffer_layout(dflt_mc_io, MC_CMD_NO_FLAGS,
954 dflt_dpni->dpni_handle,
955 &dflt_dpni->buf_layout,
956 DPNI_QUEUE_TX_CONFIRM);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700957 if (err) {
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530958 printf("dpni_set_buffer_layout() failed");
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700959 goto err_buf_layout;
960 }
961
962 /* Now that we've set our tx buffer layout, retrieve the minimum
963 * required tx data offset.
964 */
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +0530965 err = dpni_get_tx_data_offset(dflt_mc_io, MC_CMD_NO_FLAGS,
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530966 dflt_dpni->dpni_handle,
967 &priv->tx_data_offset);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700968 if (err) {
969 printf("dpni_get_tx_data_offset() failed\n");
970 goto err_data_offset;
971 }
972
973 /* Warn in case TX data offset is not multiple of 64 bytes. */
974 WARN_ON(priv->tx_data_offset % 64);
975
976 /* Accomodate SWA space. */
977 priv->tx_data_offset += LDPAA_ETH_SWA_SIZE;
978 debug("priv->tx_data_offset=%d\n", priv->tx_data_offset);
979
980 return 0;
981
982err_data_offset:
983err_buf_layout:
984err_get_attr:
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530985 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700986err_open:
987 return err;
988}
989
990static int ldpaa_dpni_bind(struct ldpaa_eth_priv *priv)
991{
992 struct dpni_pools_cfg pools_params;
Yogesh Gaur318c32f2017-11-15 11:59:31 +0530993 struct dpni_queue tx_queue;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700994 int err = 0;
995
Prabhakar Kushwahaa7992322016-03-28 14:11:05 +0530996 memset(&pools_params, 0, sizeof(pools_params));
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -0700997 pools_params.num_dpbp = 1;
998 pools_params.pools[0].dpbp_id = (uint16_t)dflt_dpbp->dpbp_attr.id;
999 pools_params.pools[0].buffer_size = LDPAA_ETH_RX_BUFFER_SIZE;
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +05301000 err = dpni_set_pools(dflt_mc_io, MC_CMD_NO_FLAGS,
1001 dflt_dpni->dpni_handle, &pools_params);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07001002 if (err) {
1003 printf("dpni_set_pools() failed\n");
1004 return err;
1005 }
1006
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301007 memset(&tx_queue, 0, sizeof(struct dpni_queue));
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07001008
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301009 err = dpni_set_queue(dflt_mc_io, MC_CMD_NO_FLAGS,
1010 dflt_dpni->dpni_handle,
1011 DPNI_QUEUE_TX, 0, 0, &tx_queue);
1012
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07001013 if (err) {
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301014 printf("dpni_set_queue() failed\n");
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07001015 return err;
1016 }
1017
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301018 err = dpni_set_tx_confirmation_mode(dflt_mc_io, MC_CMD_NO_FLAGS,
1019 dflt_dpni->dpni_handle,
1020 DPNI_CONF_DISABLE);
Prabhakar Kushwahab0efba72015-12-24 15:33:13 +05301021 if (err) {
Yogesh Gaur318c32f2017-11-15 11:59:31 +05301022 printf("dpni_set_tx_confirmation_mode() failed\n");
Prabhakar Kushwahab0efba72015-12-24 15:33:13 +05301023 return err;
1024 }
1025
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07001026 return 0;
1027}
1028
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +05301029static int ldpaa_eth_netdev_init(struct eth_device *net_dev,
1030 phy_interface_t enet_if)
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07001031{
1032 int err;
1033 struct ldpaa_eth_priv *priv = (struct ldpaa_eth_priv *)net_dev->priv;
1034
Pankaj Bansal8a921702018-08-02 16:31:28 +05301035 snprintf(net_dev->name, ETH_NAME_LEN, "DPMAC%d@%s", priv->dpmac_id,
1036 phy_interface_strings[enet_if]);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07001037
1038 net_dev->iobase = 0;
1039 net_dev->init = ldpaa_eth_open;
1040 net_dev->halt = ldpaa_eth_stop;
1041 net_dev->send = ldpaa_eth_tx;
1042 net_dev->recv = ldpaa_eth_pull_dequeue_rx;
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07001043
Prabhakar Kushwahaef9329a2016-02-24 17:02:11 +05301044#ifdef CONFIG_PHYLIB
1045 err = init_phy(net_dev);
1046 if (err < 0)
1047 return err;
1048#endif
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07001049
1050 err = eth_register(net_dev);
1051 if (err < 0) {
1052 printf("eth_register() = %d\n", err);
1053 return err;
1054 }
1055
1056 return 0;
1057}
1058
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +05301059int ldpaa_eth_init(int dpmac_id, phy_interface_t enet_if)
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07001060{
1061 struct eth_device *net_dev = NULL;
1062 struct ldpaa_eth_priv *priv = NULL;
1063 int err = 0;
1064
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07001065 /* Net device */
1066 net_dev = (struct eth_device *)malloc(sizeof(struct eth_device));
1067 if (!net_dev) {
1068 printf("eth_device malloc() failed\n");
1069 return -ENOMEM;
1070 }
1071 memset(net_dev, 0, sizeof(struct eth_device));
1072
1073 /* alloc the ldpaa ethernet private struct */
1074 priv = (struct ldpaa_eth_priv *)malloc(sizeof(struct ldpaa_eth_priv));
1075 if (!priv) {
1076 printf("ldpaa_eth_priv malloc() failed\n");
1077 return -ENOMEM;
1078 }
1079 memset(priv, 0, sizeof(struct ldpaa_eth_priv));
1080
1081 net_dev->priv = (void *)priv;
1082 priv->net_dev = (struct eth_device *)net_dev;
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +05301083 priv->dpmac_id = dpmac_id;
1084 debug("%s dpmac_id=%d\n", __func__, dpmac_id);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07001085
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +05301086 err = ldpaa_eth_netdev_init(net_dev, enet_if);
Prabhakar Kushwaha5350b992015-03-19 09:20:46 -07001087 if (err)
1088 goto err_netdev_init;
1089
1090 debug("ldpaa ethernet: Probed interface %s\n", net_dev->name);
1091 return 0;
1092
1093err_netdev_init:
1094 free(priv);
1095 net_dev->priv = NULL;
1096 free(net_dev);
1097
1098 return err;
1099}