1 // SPDX-License-Identifier: GPL-2.0-only
2 /*******************************************************************************
3 This is the driver for the ST MAC 10/100/1000 on-chip Ethernet controllers.
4 ST Ethernet IPs are built around a Synopsys IP Core.
5
6 Copyright(C) 2007-2011 STMicroelectronics Ltd
7
8
9 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
10
11 Documentation available at:
12 http://www.stlinux.com
13 Support available at:
14 https://bugzilla.stlinux.com/
15 *******************************************************************************/
16
17 #include <linux/clk.h>
18 #include <linux/kernel.h>
19 #include <linux/interrupt.h>
20 #include <linux/ip.h>
21 #include <linux/tcp.h>
22 #include <linux/skbuff.h>
23 #include <linux/ethtool.h>
24 #include <linux/if_ether.h>
25 #include <linux/crc32.h>
26 #include <linux/mii.h>
27 #include <linux/if.h>
28 #include <linux/if_vlan.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/slab.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/prefetch.h>
33 #include <linux/pinctrl/consumer.h>
34 #ifdef CONFIG_DEBUG_FS
35 #include <linux/debugfs.h>
36 #include <linux/seq_file.h>
37 #endif /* CONFIG_DEBUG_FS */
38 #include <linux/net_tstamp.h>
39 #include <linux/phylink.h>
40 #include <linux/udp.h>
41 #include <net/pkt_cls.h>
42 #include "stmmac_ptp.h"
43 #include "stmmac.h"
44 #include <linux/reset.h>
45 #include <linux/of_mdio.h>
46 #include "dwmac1000.h"
47 #include "dwxgmac2.h"
48 #include "hwif.h"
49
50 /* As long as the interface is active, we keep the timestamping counter enabled
51 * with fine resolution and binary rollover. This avoid non-monotonic behavior
52 * (clock jumps) when changing timestamping settings at runtime.
53 */
54 #define STMMAC_HWTS_ACTIVE (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | \
55 PTP_TCR_TSCTRLSSR)
56
57 #define STMMAC_ALIGN(x) ALIGN(ALIGN(x, SMP_CACHE_BYTES), 16)
58 #define TSO_MAX_BUFF_SIZE (SZ_16K - 1)
59
60 /* Module parameters */
61 #define TX_TIMEO 5000
62 static int watchdog = TX_TIMEO;
63 module_param(watchdog, int, 0644);
64 MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
65
66 static int debug = -1;
67 module_param(debug, int, 0644);
68 MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
69
70 static int phyaddr = -1;
71 module_param(phyaddr, int, 0444);
72 MODULE_PARM_DESC(phyaddr, "Physical device address");
73
74 #define STMMAC_TX_THRESH(x) ((x)->dma_tx_size / 4)
75 #define STMMAC_RX_THRESH(x) ((x)->dma_rx_size / 4)
76
77 static int flow_ctrl = FLOW_AUTO;
78 module_param(flow_ctrl, int, 0644);
79 MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
80
81 static int pause = PAUSE_TIME;
82 module_param(pause, int, 0644);
83 MODULE_PARM_DESC(pause, "Flow Control Pause Time");
84
85 #define TC_DEFAULT 64
86 static int tc = TC_DEFAULT;
87 module_param(tc, int, 0644);
88 MODULE_PARM_DESC(tc, "DMA threshold control value");
89
90 #define DEFAULT_BUFSIZE 1536
91 static int buf_sz = DEFAULT_BUFSIZE;
92 module_param(buf_sz, int, 0644);
93 MODULE_PARM_DESC(buf_sz, "DMA buffer size");
94
95 #define STMMAC_RX_COPYBREAK 256
96
97 static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
98 NETIF_MSG_LINK | NETIF_MSG_IFUP |
99 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
100
101 #define STMMAC_DEFAULT_LPI_TIMER 1000
102 static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
103 module_param(eee_timer, int, 0644);
104 MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
105 #define STMMAC_LPI_T(x) (jiffies + usecs_to_jiffies(x))
106
107 /* By default the driver will use the ring mode to manage tx and rx descriptors,
108 * but allow user to force to use the chain instead of the ring
109 */
110 static unsigned int chain_mode;
111 module_param(chain_mode, int, 0444);
112 MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
113
114 static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
115
116 #ifdef CONFIG_DEBUG_FS
117 static const struct net_device_ops stmmac_netdev_ops;
118 static void stmmac_init_fs(struct net_device *dev);
119 static void stmmac_exit_fs(struct net_device *dev);
120 #endif
121
122 #define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
123
stmmac_bus_clks_config(struct stmmac_priv * priv,bool enabled)124 int stmmac_bus_clks_config(struct stmmac_priv *priv, bool enabled)
125 {
126 int ret = 0;
127
128 if (enabled) {
129 ret = clk_prepare_enable(priv->plat->stmmac_clk);
130 if (ret)
131 return ret;
132 ret = clk_prepare_enable(priv->plat->pclk);
133 if (ret) {
134 clk_disable_unprepare(priv->plat->stmmac_clk);
135 return ret;
136 }
137 } else {
138 clk_disable_unprepare(priv->plat->stmmac_clk);
139 clk_disable_unprepare(priv->plat->pclk);
140 }
141
142 return ret;
143 }
144 EXPORT_SYMBOL_GPL(stmmac_bus_clks_config);
145
146 /**
147 * stmmac_verify_args - verify the driver parameters.
148 * Description: it checks the driver parameters and set a default in case of
149 * errors.
150 */
stmmac_verify_args(void)151 static void stmmac_verify_args(void)
152 {
153 if (unlikely(watchdog < 0))
154 watchdog = TX_TIMEO;
155 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
156 buf_sz = DEFAULT_BUFSIZE;
157 if (unlikely(flow_ctrl > 1))
158 flow_ctrl = FLOW_AUTO;
159 else if (likely(flow_ctrl < 0))
160 flow_ctrl = FLOW_OFF;
161 if (unlikely((pause < 0) || (pause > 0xffff)))
162 pause = PAUSE_TIME;
163 if (eee_timer < 0)
164 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
165 }
166
167 /**
168 * stmmac_disable_all_queues - Disable all queues
169 * @priv: driver private structure
170 */
stmmac_disable_all_queues(struct stmmac_priv * priv)171 static void stmmac_disable_all_queues(struct stmmac_priv *priv)
172 {
173 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
174 u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
175 u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
176 u32 queue;
177
178 for (queue = 0; queue < maxq; queue++) {
179 struct stmmac_channel *ch = &priv->channel[queue];
180
181 if (queue < rx_queues_cnt)
182 napi_disable(&ch->rx_napi);
183 if (queue < tx_queues_cnt)
184 napi_disable(&ch->tx_napi);
185 }
186 }
187
188 /**
189 * stmmac_enable_all_queues - Enable all queues
190 * @priv: driver private structure
191 */
stmmac_enable_all_queues(struct stmmac_priv * priv)192 static void stmmac_enable_all_queues(struct stmmac_priv *priv)
193 {
194 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
195 u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
196 u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
197 u32 queue;
198
199 for (queue = 0; queue < maxq; queue++) {
200 struct stmmac_channel *ch = &priv->channel[queue];
201
202 if (queue < rx_queues_cnt)
203 napi_enable(&ch->rx_napi);
204 if (queue < tx_queues_cnt)
205 napi_enable(&ch->tx_napi);
206 }
207 }
208
stmmac_service_event_schedule(struct stmmac_priv * priv)209 static void stmmac_service_event_schedule(struct stmmac_priv *priv)
210 {
211 if (!test_bit(STMMAC_DOWN, &priv->state) &&
212 !test_and_set_bit(STMMAC_SERVICE_SCHED, &priv->state))
213 queue_work(priv->wq, &priv->service_task);
214 }
215
stmmac_global_err(struct stmmac_priv * priv)216 static void stmmac_global_err(struct stmmac_priv *priv)
217 {
218 netif_carrier_off(priv->dev);
219 set_bit(STMMAC_RESET_REQUESTED, &priv->state);
220 stmmac_service_event_schedule(priv);
221 }
222
223 /**
224 * stmmac_clk_csr_set - dynamically set the MDC clock
225 * @priv: driver private structure
226 * Description: this is to dynamically set the MDC clock according to the csr
227 * clock input.
228 * Note:
229 * If a specific clk_csr value is passed from the platform
230 * this means that the CSR Clock Range selection cannot be
231 * changed at run-time and it is fixed (as reported in the driver
232 * documentation). Viceversa the driver will try to set the MDC
233 * clock dynamically according to the actual clock input.
234 */
stmmac_clk_csr_set(struct stmmac_priv * priv)235 static void stmmac_clk_csr_set(struct stmmac_priv *priv)
236 {
237 u32 clk_rate;
238
239 clk_rate = clk_get_rate(priv->plat->stmmac_clk);
240
241 /* Platform provided default clk_csr would be assumed valid
242 * for all other cases except for the below mentioned ones.
243 * For values higher than the IEEE 802.3 specified frequency
244 * we can not estimate the proper divider as it is not known
245 * the frequency of clk_csr_i. So we do not change the default
246 * divider.
247 */
248 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
249 if (clk_rate < CSR_F_35M)
250 priv->clk_csr = STMMAC_CSR_20_35M;
251 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
252 priv->clk_csr = STMMAC_CSR_35_60M;
253 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
254 priv->clk_csr = STMMAC_CSR_60_100M;
255 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
256 priv->clk_csr = STMMAC_CSR_100_150M;
257 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
258 priv->clk_csr = STMMAC_CSR_150_250M;
259 else if ((clk_rate >= CSR_F_250M) && (clk_rate <= CSR_F_300M))
260 priv->clk_csr = STMMAC_CSR_250_300M;
261 }
262
263 if (priv->plat->has_sun8i) {
264 if (clk_rate > 160000000)
265 priv->clk_csr = 0x03;
266 else if (clk_rate > 80000000)
267 priv->clk_csr = 0x02;
268 else if (clk_rate > 40000000)
269 priv->clk_csr = 0x01;
270 else
271 priv->clk_csr = 0;
272 }
273
274 if (priv->plat->has_xgmac) {
275 if (clk_rate > 400000000)
276 priv->clk_csr = 0x5;
277 else if (clk_rate > 350000000)
278 priv->clk_csr = 0x4;
279 else if (clk_rate > 300000000)
280 priv->clk_csr = 0x3;
281 else if (clk_rate > 250000000)
282 priv->clk_csr = 0x2;
283 else if (clk_rate > 150000000)
284 priv->clk_csr = 0x1;
285 else
286 priv->clk_csr = 0x0;
287 }
288 }
289
print_pkt(unsigned char * buf,int len)290 static void print_pkt(unsigned char *buf, int len)
291 {
292 pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
293 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
294 }
295
stmmac_tx_avail(struct stmmac_priv * priv,u32 queue)296 static inline u32 stmmac_tx_avail(struct stmmac_priv *priv, u32 queue)
297 {
298 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
299 u32 avail;
300
301 if (tx_q->dirty_tx > tx_q->cur_tx)
302 avail = tx_q->dirty_tx - tx_q->cur_tx - 1;
303 else
304 avail = priv->dma_tx_size - tx_q->cur_tx + tx_q->dirty_tx - 1;
305
306 return avail;
307 }
308
309 /**
310 * stmmac_rx_dirty - Get RX queue dirty
311 * @priv: driver private structure
312 * @queue: RX queue index
313 */
stmmac_rx_dirty(struct stmmac_priv * priv,u32 queue)314 static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv, u32 queue)
315 {
316 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
317 u32 dirty;
318
319 if (rx_q->dirty_rx <= rx_q->cur_rx)
320 dirty = rx_q->cur_rx - rx_q->dirty_rx;
321 else
322 dirty = priv->dma_rx_size - rx_q->dirty_rx + rx_q->cur_rx;
323
324 return dirty;
325 }
326
327 /**
328 * stmmac_enable_eee_mode - check and enter in LPI mode
329 * @priv: driver private structure
330 * Description: this function is to verify and enter in LPI mode in case of
331 * EEE.
332 */
stmmac_enable_eee_mode(struct stmmac_priv * priv)333 static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
334 {
335 u32 tx_cnt = priv->plat->tx_queues_to_use;
336 u32 queue;
337
338 /* check if all TX queues have the work finished */
339 for (queue = 0; queue < tx_cnt; queue++) {
340 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
341
342 if (tx_q->dirty_tx != tx_q->cur_tx)
343 return; /* still unfinished work */
344 }
345
346 /* Check and enter in LPI mode */
347 if (!priv->tx_path_in_lpi_mode)
348 stmmac_set_eee_mode(priv, priv->hw,
349 priv->plat->en_tx_lpi_clockgating);
350 }
351
352 /**
353 * stmmac_disable_eee_mode - disable and exit from LPI mode
354 * @priv: driver private structure
355 * Description: this function is to exit and disable EEE in case of
356 * LPI state is true. This is called by the xmit.
357 */
stmmac_disable_eee_mode(struct stmmac_priv * priv)358 void stmmac_disable_eee_mode(struct stmmac_priv *priv)
359 {
360 stmmac_reset_eee_mode(priv, priv->hw);
361 del_timer_sync(&priv->eee_ctrl_timer);
362 priv->tx_path_in_lpi_mode = false;
363 }
364
365 /**
366 * stmmac_eee_ctrl_timer - EEE TX SW timer.
367 * @t: timer_list struct containing private info
368 * Description:
369 * if there is no data transfer and if we are not in LPI state,
370 * then MAC Transmitter can be moved to LPI state.
371 */
stmmac_eee_ctrl_timer(struct timer_list * t)372 static void stmmac_eee_ctrl_timer(struct timer_list *t)
373 {
374 struct stmmac_priv *priv = from_timer(priv, t, eee_ctrl_timer);
375
376 stmmac_enable_eee_mode(priv);
377 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
378 }
379
380 /**
381 * stmmac_eee_init - init EEE
382 * @priv: driver private structure
383 * Description:
384 * if the GMAC supports the EEE (from the HW cap reg) and the phy device
385 * can also manage EEE, this function enable the LPI state and start related
386 * timer.
387 */
stmmac_eee_init(struct stmmac_priv * priv)388 bool stmmac_eee_init(struct stmmac_priv *priv)
389 {
390 int eee_tw_timer = priv->eee_tw_timer;
391
392 /* Using PCS we cannot dial with the phy registers at this stage
393 * so we do not support extra feature like EEE.
394 */
395 if (priv->hw->pcs == STMMAC_PCS_TBI ||
396 priv->hw->pcs == STMMAC_PCS_RTBI)
397 return false;
398
399 /* Check if MAC core supports the EEE feature. */
400 if (!priv->dma_cap.eee)
401 return false;
402
403 mutex_lock(&priv->lock);
404
405 /* Check if it needs to be deactivated */
406 if (!priv->eee_active) {
407 if (priv->eee_enabled) {
408 netdev_dbg(priv->dev, "disable EEE\n");
409 del_timer_sync(&priv->eee_ctrl_timer);
410 stmmac_set_eee_timer(priv, priv->hw, 0, eee_tw_timer);
411 }
412 mutex_unlock(&priv->lock);
413 return false;
414 }
415
416 if (priv->eee_active && !priv->eee_enabled) {
417 timer_setup(&priv->eee_ctrl_timer, stmmac_eee_ctrl_timer, 0);
418 stmmac_set_eee_timer(priv, priv->hw, STMMAC_DEFAULT_LIT_LS,
419 eee_tw_timer);
420 }
421
422 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
423
424 mutex_unlock(&priv->lock);
425 netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n");
426 return true;
427 }
428
429 /* stmmac_get_tx_hwtstamp - get HW TX timestamps
430 * @priv: driver private structure
431 * @p : descriptor pointer
432 * @skb : the socket buffer
433 * Description :
434 * This function will read timestamp from the descriptor & pass it to stack.
435 * and also perform some sanity checks.
436 */
stmmac_get_tx_hwtstamp(struct stmmac_priv * priv,struct dma_desc * p,struct sk_buff * skb)437 static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
438 struct dma_desc *p, struct sk_buff *skb)
439 {
440 struct skb_shared_hwtstamps shhwtstamp;
441 bool found = false;
442 u64 ns = 0;
443
444 if (!priv->hwts_tx_en)
445 return;
446
447 /* exit if skb doesn't support hw tstamp */
448 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
449 return;
450
451 /* check tx tstamp status */
452 if (stmmac_get_tx_timestamp_status(priv, p)) {
453 stmmac_get_timestamp(priv, p, priv->adv_ts, &ns);
454 found = true;
455 } else if (!stmmac_get_mac_tx_timestamp(priv, priv->hw, &ns)) {
456 found = true;
457 }
458
459 if (found) {
460 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
461 shhwtstamp.hwtstamp = ns_to_ktime(ns);
462
463 netdev_dbg(priv->dev, "get valid TX hw timestamp %llu\n", ns);
464 /* pass tstamp to stack */
465 skb_tstamp_tx(skb, &shhwtstamp);
466 }
467 }
468
469 /* stmmac_get_rx_hwtstamp - get HW RX timestamps
470 * @priv: driver private structure
471 * @p : descriptor pointer
472 * @np : next descriptor pointer
473 * @skb : the socket buffer
474 * Description :
475 * This function will read received packet's timestamp from the descriptor
476 * and pass it to stack. It also perform some sanity checks.
477 */
stmmac_get_rx_hwtstamp(struct stmmac_priv * priv,struct dma_desc * p,struct dma_desc * np,struct sk_buff * skb)478 static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
479 struct dma_desc *np, struct sk_buff *skb)
480 {
481 struct skb_shared_hwtstamps *shhwtstamp = NULL;
482 struct dma_desc *desc = p;
483 u64 ns = 0;
484
485 if (!priv->hwts_rx_en)
486 return;
487 /* For GMAC4, the valid timestamp is from CTX next desc. */
488 if (priv->plat->has_gmac4 || priv->plat->has_xgmac)
489 desc = np;
490
491 /* Check if timestamp is available */
492 if (stmmac_get_rx_timestamp_status(priv, p, np, priv->adv_ts)) {
493 stmmac_get_timestamp(priv, desc, priv->adv_ts, &ns);
494 netdev_dbg(priv->dev, "get valid RX hw timestamp %llu\n", ns);
495 shhwtstamp = skb_hwtstamps(skb);
496 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
497 shhwtstamp->hwtstamp = ns_to_ktime(ns);
498 } else {
499 netdev_dbg(priv->dev, "cannot get RX hw timestamp\n");
500 }
501 }
502
503 /**
504 * stmmac_hwtstamp_set - control hardware timestamping.
505 * @dev: device pointer.
506 * @ifr: An IOCTL specific structure, that can contain a pointer to
507 * a proprietary structure used to pass information to the driver.
508 * Description:
509 * This function configures the MAC to enable/disable both outgoing(TX)
510 * and incoming(RX) packets time stamping based on user input.
511 * Return Value:
512 * 0 on success and an appropriate -ve integer on failure.
513 */
stmmac_hwtstamp_set(struct net_device * dev,struct ifreq * ifr)514 static int stmmac_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
515 {
516 struct stmmac_priv *priv = netdev_priv(dev);
517 struct hwtstamp_config config;
518 u32 ptp_v2 = 0;
519 u32 tstamp_all = 0;
520 u32 ptp_over_ipv4_udp = 0;
521 u32 ptp_over_ipv6_udp = 0;
522 u32 ptp_over_ethernet = 0;
523 u32 snap_type_sel = 0;
524 u32 ts_master_en = 0;
525 u32 ts_event_en = 0;
526
527 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
528 netdev_alert(priv->dev, "No support for HW time stamping\n");
529 priv->hwts_tx_en = 0;
530 priv->hwts_rx_en = 0;
531
532 return -EOPNOTSUPP;
533 }
534
535 if (copy_from_user(&config, ifr->ifr_data,
536 sizeof(config)))
537 return -EFAULT;
538
539 netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
540 __func__, config.flags, config.tx_type, config.rx_filter);
541
542 /* reserved for future extensions */
543 if (config.flags)
544 return -EINVAL;
545
546 if (config.tx_type != HWTSTAMP_TX_OFF &&
547 config.tx_type != HWTSTAMP_TX_ON)
548 return -ERANGE;
549
550 if (priv->adv_ts) {
551 switch (config.rx_filter) {
552 case HWTSTAMP_FILTER_NONE:
553 /* time stamp no incoming packet at all */
554 config.rx_filter = HWTSTAMP_FILTER_NONE;
555 break;
556
557 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
558 /* PTP v1, UDP, any kind of event packet */
559 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
560 /* 'xmac' hardware can support Sync, Pdelay_Req and
561 * Pdelay_resp by setting bit14 and bits17/16 to 01
562 * This leaves Delay_Req timestamps out.
563 * Enable all events *and* general purpose message
564 * timestamping
565 */
566 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
567 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
568 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
569 break;
570
571 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
572 /* PTP v1, UDP, Sync packet */
573 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
574 /* take time stamp for SYNC messages only */
575 ts_event_en = PTP_TCR_TSEVNTENA;
576
577 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
578 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
579 break;
580
581 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
582 /* PTP v1, UDP, Delay_req packet */
583 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
584 /* take time stamp for Delay_Req messages only */
585 ts_master_en = PTP_TCR_TSMSTRENA;
586 ts_event_en = PTP_TCR_TSEVNTENA;
587
588 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
589 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
590 break;
591
592 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
593 /* PTP v2, UDP, any kind of event packet */
594 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
595 ptp_v2 = PTP_TCR_TSVER2ENA;
596 /* take time stamp for all event messages */
597 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
598
599 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
600 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
601 break;
602
603 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
604 /* PTP v2, UDP, Sync packet */
605 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
606 ptp_v2 = PTP_TCR_TSVER2ENA;
607 /* take time stamp for SYNC messages only */
608 ts_event_en = PTP_TCR_TSEVNTENA;
609
610 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
611 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
612 break;
613
614 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
615 /* PTP v2, UDP, Delay_req packet */
616 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
617 ptp_v2 = PTP_TCR_TSVER2ENA;
618 /* take time stamp for Delay_Req messages only */
619 ts_master_en = PTP_TCR_TSMSTRENA;
620 ts_event_en = PTP_TCR_TSEVNTENA;
621
622 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
623 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
624 break;
625
626 case HWTSTAMP_FILTER_PTP_V2_EVENT:
627 /* PTP v2/802.AS1 any layer, any kind of event packet */
628 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
629 ptp_v2 = PTP_TCR_TSVER2ENA;
630 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
631 if (priv->synopsys_id < DWMAC_CORE_4_10)
632 ts_event_en = PTP_TCR_TSEVNTENA;
633 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
634 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
635 ptp_over_ethernet = PTP_TCR_TSIPENA;
636 break;
637
638 case HWTSTAMP_FILTER_PTP_V2_SYNC:
639 /* PTP v2/802.AS1, any layer, Sync packet */
640 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
641 ptp_v2 = PTP_TCR_TSVER2ENA;
642 /* take time stamp for SYNC messages only */
643 ts_event_en = PTP_TCR_TSEVNTENA;
644
645 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
646 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
647 ptp_over_ethernet = PTP_TCR_TSIPENA;
648 break;
649
650 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
651 /* PTP v2/802.AS1, any layer, Delay_req packet */
652 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
653 ptp_v2 = PTP_TCR_TSVER2ENA;
654 /* take time stamp for Delay_Req messages only */
655 ts_master_en = PTP_TCR_TSMSTRENA;
656 ts_event_en = PTP_TCR_TSEVNTENA;
657
658 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
659 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
660 ptp_over_ethernet = PTP_TCR_TSIPENA;
661 break;
662
663 case HWTSTAMP_FILTER_NTP_ALL:
664 case HWTSTAMP_FILTER_ALL:
665 /* time stamp any incoming packet */
666 config.rx_filter = HWTSTAMP_FILTER_ALL;
667 tstamp_all = PTP_TCR_TSENALL;
668 break;
669
670 default:
671 return -ERANGE;
672 }
673 } else {
674 switch (config.rx_filter) {
675 case HWTSTAMP_FILTER_NONE:
676 config.rx_filter = HWTSTAMP_FILTER_NONE;
677 break;
678 default:
679 /* PTP v1, UDP, any kind of event packet */
680 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
681 break;
682 }
683 }
684 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
685 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
686
687 priv->systime_flags = STMMAC_HWTS_ACTIVE;
688
689 if (priv->hwts_tx_en || priv->hwts_rx_en) {
690 priv->systime_flags |= tstamp_all | ptp_v2 |
691 ptp_over_ethernet | ptp_over_ipv6_udp |
692 ptp_over_ipv4_udp | ts_event_en |
693 ts_master_en | snap_type_sel;
694 }
695
696 stmmac_config_hw_tstamping(priv, priv->ptpaddr, priv->systime_flags);
697
698 memcpy(&priv->tstamp_config, &config, sizeof(config));
699
700 return copy_to_user(ifr->ifr_data, &config,
701 sizeof(config)) ? -EFAULT : 0;
702 }
703
704 /**
705 * stmmac_hwtstamp_get - read hardware timestamping.
706 * @dev: device pointer.
707 * @ifr: An IOCTL specific structure, that can contain a pointer to
708 * a proprietary structure used to pass information to the driver.
709 * Description:
710 * This function obtain the current hardware timestamping settings
711 * as requested.
712 */
stmmac_hwtstamp_get(struct net_device * dev,struct ifreq * ifr)713 static int stmmac_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
714 {
715 struct stmmac_priv *priv = netdev_priv(dev);
716 struct hwtstamp_config *config = &priv->tstamp_config;
717
718 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
719 return -EOPNOTSUPP;
720
721 return copy_to_user(ifr->ifr_data, config,
722 sizeof(*config)) ? -EFAULT : 0;
723 }
724
725 /**
726 * stmmac_init_tstamp_counter - init hardware timestamping counter
727 * @priv: driver private structure
728 * @systime_flags: timestamping flags
729 * Description:
730 * Initialize hardware counter for packet timestamping.
731 * This is valid as long as the interface is open and not suspended.
732 * Will be rerun after resuming from suspend, case in which the timestamping
733 * flags updated by stmmac_hwtstamp_set() also need to be restored.
734 */
stmmac_init_tstamp_counter(struct stmmac_priv * priv,u32 systime_flags)735 int stmmac_init_tstamp_counter(struct stmmac_priv *priv, u32 systime_flags)
736 {
737 bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
738 struct timespec64 now;
739 u32 sec_inc = 0;
740 u64 temp = 0;
741 int ret;
742
743 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
744 return -EOPNOTSUPP;
745
746 ret = clk_prepare_enable(priv->plat->clk_ptp_ref);
747 if (ret < 0) {
748 netdev_warn(priv->dev,
749 "failed to enable PTP reference clock: %pe\n",
750 ERR_PTR(ret));
751 return ret;
752 }
753
754 stmmac_config_hw_tstamping(priv, priv->ptpaddr, systime_flags);
755 priv->systime_flags = systime_flags;
756
757 /* program Sub Second Increment reg */
758 stmmac_config_sub_second_increment(priv, priv->ptpaddr,
759 priv->plat->clk_ptp_rate,
760 xmac, &sec_inc);
761 temp = div_u64(1000000000ULL, sec_inc);
762
763 /* Store sub second increment for later use */
764 priv->sub_second_inc = sec_inc;
765
766 /* calculate default added value:
767 * formula is :
768 * addend = (2^32)/freq_div_ratio;
769 * where, freq_div_ratio = 1e9ns/sec_inc
770 */
771 temp = (u64)(temp << 32);
772 priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate);
773 stmmac_config_addend(priv, priv->ptpaddr, priv->default_addend);
774
775 /* initialize system time */
776 ktime_get_real_ts64(&now);
777
778 /* lower 32 bits of tv_sec are safe until y2106 */
779 stmmac_init_systime(priv, priv->ptpaddr, (u32)now.tv_sec, now.tv_nsec);
780
781 return 0;
782 }
783 EXPORT_SYMBOL_GPL(stmmac_init_tstamp_counter);
784
785 /**
786 * stmmac_init_ptp - init PTP
787 * @priv: driver private structure
788 * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
789 * This is done by looking at the HW cap. register.
790 * This function also registers the ptp driver.
791 */
stmmac_init_ptp(struct stmmac_priv * priv)792 static int stmmac_init_ptp(struct stmmac_priv *priv)
793 {
794 bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
795 int ret;
796
797 ret = stmmac_init_tstamp_counter(priv, STMMAC_HWTS_ACTIVE);
798 if (ret)
799 return ret;
800
801 priv->adv_ts = 0;
802 /* Check if adv_ts can be enabled for dwmac 4.x / xgmac core */
803 if (xmac && priv->dma_cap.atime_stamp)
804 priv->adv_ts = 1;
805 /* Dwmac 3.x core with extend_desc can support adv_ts */
806 else if (priv->extend_desc && priv->dma_cap.atime_stamp)
807 priv->adv_ts = 1;
808
809 if (priv->dma_cap.time_stamp)
810 netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
811
812 if (priv->adv_ts)
813 netdev_info(priv->dev,
814 "IEEE 1588-2008 Advanced Timestamp supported\n");
815
816 priv->hwts_tx_en = 0;
817 priv->hwts_rx_en = 0;
818
819 return 0;
820 }
821
stmmac_release_ptp(struct stmmac_priv * priv)822 static void stmmac_release_ptp(struct stmmac_priv *priv)
823 {
824 clk_disable_unprepare(priv->plat->clk_ptp_ref);
825 stmmac_ptp_unregister(priv);
826 }
827
828 /**
829 * stmmac_mac_flow_ctrl - Configure flow control in all queues
830 * @priv: driver private structure
831 * @duplex: duplex passed to the next function
832 * Description: It is used for configuring the flow control in all queues
833 */
stmmac_mac_flow_ctrl(struct stmmac_priv * priv,u32 duplex)834 static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex)
835 {
836 u32 tx_cnt = priv->plat->tx_queues_to_use;
837
838 stmmac_flow_ctrl(priv, priv->hw, duplex, priv->flow_ctrl,
839 priv->pause, tx_cnt);
840 }
841
stmmac_validate(struct phylink_config * config,unsigned long * supported,struct phylink_link_state * state)842 static void stmmac_validate(struct phylink_config *config,
843 unsigned long *supported,
844 struct phylink_link_state *state)
845 {
846 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
847 __ETHTOOL_DECLARE_LINK_MODE_MASK(mac_supported) = { 0, };
848 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
849 int tx_cnt = priv->plat->tx_queues_to_use;
850 int max_speed = priv->plat->max_speed;
851
852 phylink_set(mac_supported, 10baseT_Half);
853 phylink_set(mac_supported, 10baseT_Full);
854 phylink_set(mac_supported, 100baseT_Half);
855 phylink_set(mac_supported, 100baseT_Full);
856 phylink_set(mac_supported, 1000baseT_Half);
857 phylink_set(mac_supported, 1000baseT_Full);
858 phylink_set(mac_supported, 1000baseKX_Full);
859
860 phylink_set(mac_supported, Autoneg);
861 phylink_set(mac_supported, Pause);
862 phylink_set(mac_supported, Asym_Pause);
863 phylink_set_port_modes(mac_supported);
864
865 /* Cut down 1G if asked to */
866 if ((max_speed > 0) && (max_speed < 1000)) {
867 phylink_set(mask, 1000baseT_Full);
868 phylink_set(mask, 1000baseX_Full);
869 } else if (priv->plat->has_xgmac) {
870 if (!max_speed || (max_speed >= 2500)) {
871 phylink_set(mac_supported, 2500baseT_Full);
872 phylink_set(mac_supported, 2500baseX_Full);
873 }
874 if (!max_speed || (max_speed >= 5000)) {
875 phylink_set(mac_supported, 5000baseT_Full);
876 }
877 if (!max_speed || (max_speed >= 10000)) {
878 phylink_set(mac_supported, 10000baseSR_Full);
879 phylink_set(mac_supported, 10000baseLR_Full);
880 phylink_set(mac_supported, 10000baseER_Full);
881 phylink_set(mac_supported, 10000baseLRM_Full);
882 phylink_set(mac_supported, 10000baseT_Full);
883 phylink_set(mac_supported, 10000baseKX4_Full);
884 phylink_set(mac_supported, 10000baseKR_Full);
885 }
886 if (!max_speed || (max_speed >= 25000)) {
887 phylink_set(mac_supported, 25000baseCR_Full);
888 phylink_set(mac_supported, 25000baseKR_Full);
889 phylink_set(mac_supported, 25000baseSR_Full);
890 }
891 if (!max_speed || (max_speed >= 40000)) {
892 phylink_set(mac_supported, 40000baseKR4_Full);
893 phylink_set(mac_supported, 40000baseCR4_Full);
894 phylink_set(mac_supported, 40000baseSR4_Full);
895 phylink_set(mac_supported, 40000baseLR4_Full);
896 }
897 if (!max_speed || (max_speed >= 50000)) {
898 phylink_set(mac_supported, 50000baseCR2_Full);
899 phylink_set(mac_supported, 50000baseKR2_Full);
900 phylink_set(mac_supported, 50000baseSR2_Full);
901 phylink_set(mac_supported, 50000baseKR_Full);
902 phylink_set(mac_supported, 50000baseSR_Full);
903 phylink_set(mac_supported, 50000baseCR_Full);
904 phylink_set(mac_supported, 50000baseLR_ER_FR_Full);
905 phylink_set(mac_supported, 50000baseDR_Full);
906 }
907 if (!max_speed || (max_speed >= 100000)) {
908 phylink_set(mac_supported, 100000baseKR4_Full);
909 phylink_set(mac_supported, 100000baseSR4_Full);
910 phylink_set(mac_supported, 100000baseCR4_Full);
911 phylink_set(mac_supported, 100000baseLR4_ER4_Full);
912 phylink_set(mac_supported, 100000baseKR2_Full);
913 phylink_set(mac_supported, 100000baseSR2_Full);
914 phylink_set(mac_supported, 100000baseCR2_Full);
915 phylink_set(mac_supported, 100000baseLR2_ER2_FR2_Full);
916 phylink_set(mac_supported, 100000baseDR2_Full);
917 }
918 }
919
920 /* Half-Duplex can only work with single queue */
921 if (tx_cnt > 1) {
922 phylink_set(mask, 10baseT_Half);
923 phylink_set(mask, 100baseT_Half);
924 phylink_set(mask, 1000baseT_Half);
925 }
926
927 linkmode_and(supported, supported, mac_supported);
928 linkmode_andnot(supported, supported, mask);
929
930 linkmode_and(state->advertising, state->advertising, mac_supported);
931 linkmode_andnot(state->advertising, state->advertising, mask);
932
933 /* If PCS is supported, check which modes it supports. */
934 stmmac_xpcs_validate(priv, &priv->hw->xpcs_args, supported, state);
935 }
936
stmmac_mac_pcs_get_state(struct phylink_config * config,struct phylink_link_state * state)937 static void stmmac_mac_pcs_get_state(struct phylink_config *config,
938 struct phylink_link_state *state)
939 {
940 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
941
942 state->link = 0;
943 stmmac_xpcs_get_state(priv, &priv->hw->xpcs_args, state);
944 }
945
stmmac_mac_config(struct phylink_config * config,unsigned int mode,const struct phylink_link_state * state)946 static void stmmac_mac_config(struct phylink_config *config, unsigned int mode,
947 const struct phylink_link_state *state)
948 {
949 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
950
951 stmmac_xpcs_config(priv, &priv->hw->xpcs_args, state);
952 }
953
stmmac_mac_an_restart(struct phylink_config * config)954 static void stmmac_mac_an_restart(struct phylink_config *config)
955 {
956 /* Not Supported */
957 }
958
stmmac_mac_link_down(struct phylink_config * config,unsigned int mode,phy_interface_t interface)959 static void stmmac_mac_link_down(struct phylink_config *config,
960 unsigned int mode, phy_interface_t interface)
961 {
962 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
963
964 stmmac_mac_set(priv, priv->ioaddr, false);
965 priv->eee_active = false;
966 priv->tx_lpi_enabled = false;
967 stmmac_eee_init(priv);
968 stmmac_set_eee_pls(priv, priv->hw, false);
969 }
970
stmmac_mac_link_up(struct phylink_config * config,struct phy_device * phy,unsigned int mode,phy_interface_t interface,int speed,int duplex,bool tx_pause,bool rx_pause)971 static void stmmac_mac_link_up(struct phylink_config *config,
972 struct phy_device *phy,
973 unsigned int mode, phy_interface_t interface,
974 int speed, int duplex,
975 bool tx_pause, bool rx_pause)
976 {
977 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev));
978 u32 ctrl;
979
980 stmmac_xpcs_link_up(priv, &priv->hw->xpcs_args, speed, interface);
981
982 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
983 ctrl &= ~priv->hw->link.speed_mask;
984
985 if (interface == PHY_INTERFACE_MODE_USXGMII) {
986 switch (speed) {
987 case SPEED_10000:
988 ctrl |= priv->hw->link.xgmii.speed10000;
989 break;
990 case SPEED_5000:
991 ctrl |= priv->hw->link.xgmii.speed5000;
992 break;
993 case SPEED_2500:
994 ctrl |= priv->hw->link.xgmii.speed2500;
995 break;
996 default:
997 return;
998 }
999 } else if (interface == PHY_INTERFACE_MODE_XLGMII) {
1000 switch (speed) {
1001 case SPEED_100000:
1002 ctrl |= priv->hw->link.xlgmii.speed100000;
1003 break;
1004 case SPEED_50000:
1005 ctrl |= priv->hw->link.xlgmii.speed50000;
1006 break;
1007 case SPEED_40000:
1008 ctrl |= priv->hw->link.xlgmii.speed40000;
1009 break;
1010 case SPEED_25000:
1011 ctrl |= priv->hw->link.xlgmii.speed25000;
1012 break;
1013 case SPEED_10000:
1014 ctrl |= priv->hw->link.xgmii.speed10000;
1015 break;
1016 case SPEED_2500:
1017 ctrl |= priv->hw->link.speed2500;
1018 break;
1019 case SPEED_1000:
1020 ctrl |= priv->hw->link.speed1000;
1021 break;
1022 default:
1023 return;
1024 }
1025 } else {
1026 switch (speed) {
1027 case SPEED_2500:
1028 ctrl |= priv->hw->link.speed2500;
1029 break;
1030 case SPEED_1000:
1031 ctrl |= priv->hw->link.speed1000;
1032 break;
1033 case SPEED_100:
1034 ctrl |= priv->hw->link.speed100;
1035 break;
1036 case SPEED_10:
1037 ctrl |= priv->hw->link.speed10;
1038 break;
1039 default:
1040 return;
1041 }
1042 }
1043
1044 priv->speed = speed;
1045
1046 if (priv->plat->fix_mac_speed)
1047 priv->plat->fix_mac_speed(priv->plat->bsp_priv, speed);
1048
1049 if (!duplex)
1050 ctrl &= ~priv->hw->link.duplex;
1051 else
1052 ctrl |= priv->hw->link.duplex;
1053
1054 /* Flow Control operation */
1055 if (tx_pause && rx_pause)
1056 stmmac_mac_flow_ctrl(priv, duplex);
1057
1058 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
1059
1060 stmmac_mac_set(priv, priv->ioaddr, true);
1061 if (phy && priv->dma_cap.eee) {
1062 priv->eee_active = phy_init_eee(phy, 1) >= 0;
1063 priv->eee_enabled = stmmac_eee_init(priv);
1064 priv->tx_lpi_enabled = priv->eee_enabled;
1065 stmmac_set_eee_pls(priv, priv->hw, true);
1066 }
1067 }
1068
1069 static const struct phylink_mac_ops stmmac_phylink_mac_ops = {
1070 .validate = stmmac_validate,
1071 .mac_pcs_get_state = stmmac_mac_pcs_get_state,
1072 .mac_config = stmmac_mac_config,
1073 .mac_an_restart = stmmac_mac_an_restart,
1074 .mac_link_down = stmmac_mac_link_down,
1075 .mac_link_up = stmmac_mac_link_up,
1076 };
1077
1078 /**
1079 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
1080 * @priv: driver private structure
1081 * Description: this is to verify if the HW supports the PCS.
1082 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
1083 * configured for the TBI, RTBI, or SGMII PHY interface.
1084 */
stmmac_check_pcs_mode(struct stmmac_priv * priv)1085 static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
1086 {
1087 int interface = priv->plat->interface;
1088
1089 if (priv->dma_cap.pcs) {
1090 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
1091 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
1092 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
1093 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
1094 netdev_dbg(priv->dev, "PCS RGMII support enabled\n");
1095 priv->hw->pcs = STMMAC_PCS_RGMII;
1096 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
1097 netdev_dbg(priv->dev, "PCS SGMII support enabled\n");
1098 priv->hw->pcs = STMMAC_PCS_SGMII;
1099 }
1100 }
1101 }
1102
1103 /**
1104 * stmmac_init_phy - PHY initialization
1105 * @dev: net device structure
1106 * Description: it initializes the driver's PHY state, and attaches the PHY
1107 * to the mac driver.
1108 * Return value:
1109 * 0 on success
1110 */
stmmac_init_phy(struct net_device * dev)1111 static int stmmac_init_phy(struct net_device *dev)
1112 {
1113 struct stmmac_priv *priv = netdev_priv(dev);
1114 struct device_node *node;
1115 int ret;
1116
1117 node = priv->plat->phylink_node;
1118
1119 if (node)
1120 ret = phylink_of_phy_connect(priv->phylink, node, 0);
1121
1122 /* Some DT bindings do not set-up the PHY handle. Let's try to
1123 * manually parse it
1124 */
1125 if (!node || ret) {
1126 int addr = priv->plat->phy_addr;
1127 struct phy_device *phydev;
1128
1129 phydev = mdiobus_get_phy(priv->mii, addr);
1130 if (!phydev) {
1131 netdev_err(priv->dev, "no phy at addr %d\n", addr);
1132 return -ENODEV;
1133 }
1134
1135 ret = phylink_connect_phy(priv->phylink, phydev);
1136 }
1137
1138 if (!priv->plat->pmt) {
1139 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1140
1141 phylink_ethtool_get_wol(priv->phylink, &wol);
1142 device_set_wakeup_capable(priv->device, !!wol.supported);
1143 }
1144
1145 return ret;
1146 }
1147
stmmac_phy_setup(struct stmmac_priv * priv)1148 static int stmmac_phy_setup(struct stmmac_priv *priv)
1149 {
1150 struct fwnode_handle *fwnode = of_fwnode_handle(priv->plat->phylink_node);
1151 int mode = priv->plat->phy_interface;
1152 struct phylink *phylink;
1153
1154 priv->phylink_config.dev = &priv->dev->dev;
1155 priv->phylink_config.type = PHYLINK_NETDEV;
1156 priv->phylink_config.pcs_poll = true;
1157
1158 if (!fwnode)
1159 fwnode = dev_fwnode(priv->device);
1160
1161 phylink = phylink_create(&priv->phylink_config, fwnode,
1162 mode, &stmmac_phylink_mac_ops);
1163 if (IS_ERR(phylink))
1164 return PTR_ERR(phylink);
1165
1166 priv->phylink = phylink;
1167 return 0;
1168 }
1169
stmmac_display_rx_rings(struct stmmac_priv * priv)1170 static void stmmac_display_rx_rings(struct stmmac_priv *priv)
1171 {
1172 u32 rx_cnt = priv->plat->rx_queues_to_use;
1173 unsigned int desc_size;
1174 void *head_rx;
1175 u32 queue;
1176
1177 /* Display RX rings */
1178 for (queue = 0; queue < rx_cnt; queue++) {
1179 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1180
1181 pr_info("\tRX Queue %u rings\n", queue);
1182
1183 if (priv->extend_desc) {
1184 head_rx = (void *)rx_q->dma_erx;
1185 desc_size = sizeof(struct dma_extended_desc);
1186 } else {
1187 head_rx = (void *)rx_q->dma_rx;
1188 desc_size = sizeof(struct dma_desc);
1189 }
1190
1191 /* Display RX ring */
1192 stmmac_display_ring(priv, head_rx, priv->dma_rx_size, true,
1193 rx_q->dma_rx_phy, desc_size);
1194 }
1195 }
1196
stmmac_display_tx_rings(struct stmmac_priv * priv)1197 static void stmmac_display_tx_rings(struct stmmac_priv *priv)
1198 {
1199 u32 tx_cnt = priv->plat->tx_queues_to_use;
1200 unsigned int desc_size;
1201 void *head_tx;
1202 u32 queue;
1203
1204 /* Display TX rings */
1205 for (queue = 0; queue < tx_cnt; queue++) {
1206 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1207
1208 pr_info("\tTX Queue %d rings\n", queue);
1209
1210 if (priv->extend_desc) {
1211 head_tx = (void *)tx_q->dma_etx;
1212 desc_size = sizeof(struct dma_extended_desc);
1213 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1214 head_tx = (void *)tx_q->dma_entx;
1215 desc_size = sizeof(struct dma_edesc);
1216 } else {
1217 head_tx = (void *)tx_q->dma_tx;
1218 desc_size = sizeof(struct dma_desc);
1219 }
1220
1221 stmmac_display_ring(priv, head_tx, priv->dma_tx_size, false,
1222 tx_q->dma_tx_phy, desc_size);
1223 }
1224 }
1225
stmmac_display_rings(struct stmmac_priv * priv)1226 static void stmmac_display_rings(struct stmmac_priv *priv)
1227 {
1228 /* Display RX ring */
1229 stmmac_display_rx_rings(priv);
1230
1231 /* Display TX ring */
1232 stmmac_display_tx_rings(priv);
1233 }
1234
stmmac_set_bfsize(int mtu,int bufsize)1235 static int stmmac_set_bfsize(int mtu, int bufsize)
1236 {
1237 int ret = bufsize;
1238
1239 if (mtu >= BUF_SIZE_8KiB)
1240 ret = BUF_SIZE_16KiB;
1241 else if (mtu >= BUF_SIZE_4KiB)
1242 ret = BUF_SIZE_8KiB;
1243 else if (mtu >= BUF_SIZE_2KiB)
1244 ret = BUF_SIZE_4KiB;
1245 else if (mtu > DEFAULT_BUFSIZE)
1246 ret = BUF_SIZE_2KiB;
1247 else
1248 ret = DEFAULT_BUFSIZE;
1249
1250 return ret;
1251 }
1252
1253 /**
1254 * stmmac_clear_rx_descriptors - clear RX descriptors
1255 * @priv: driver private structure
1256 * @queue: RX queue index
1257 * Description: this function is called to clear the RX descriptors
1258 * in case of both basic and extended descriptors are used.
1259 */
stmmac_clear_rx_descriptors(struct stmmac_priv * priv,u32 queue)1260 static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv, u32 queue)
1261 {
1262 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1263 int i;
1264
1265 /* Clear the RX descriptors */
1266 for (i = 0; i < priv->dma_rx_size; i++)
1267 if (priv->extend_desc)
1268 stmmac_init_rx_desc(priv, &rx_q->dma_erx[i].basic,
1269 priv->use_riwt, priv->mode,
1270 (i == priv->dma_rx_size - 1),
1271 priv->dma_buf_sz);
1272 else
1273 stmmac_init_rx_desc(priv, &rx_q->dma_rx[i],
1274 priv->use_riwt, priv->mode,
1275 (i == priv->dma_rx_size - 1),
1276 priv->dma_buf_sz);
1277 }
1278
1279 /**
1280 * stmmac_clear_tx_descriptors - clear tx descriptors
1281 * @priv: driver private structure
1282 * @queue: TX queue index.
1283 * Description: this function is called to clear the TX descriptors
1284 * in case of both basic and extended descriptors are used.
1285 */
stmmac_clear_tx_descriptors(struct stmmac_priv * priv,u32 queue)1286 static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv, u32 queue)
1287 {
1288 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1289 int i;
1290
1291 /* Clear the TX descriptors */
1292 for (i = 0; i < priv->dma_tx_size; i++) {
1293 int last = (i == (priv->dma_tx_size - 1));
1294 struct dma_desc *p;
1295
1296 if (priv->extend_desc)
1297 p = &tx_q->dma_etx[i].basic;
1298 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1299 p = &tx_q->dma_entx[i].basic;
1300 else
1301 p = &tx_q->dma_tx[i];
1302
1303 stmmac_init_tx_desc(priv, p, priv->mode, last);
1304 }
1305 }
1306
1307 /**
1308 * stmmac_clear_descriptors - clear descriptors
1309 * @priv: driver private structure
1310 * Description: this function is called to clear the TX and RX descriptors
1311 * in case of both basic and extended descriptors are used.
1312 */
stmmac_clear_descriptors(struct stmmac_priv * priv)1313 static void stmmac_clear_descriptors(struct stmmac_priv *priv)
1314 {
1315 u32 rx_queue_cnt = priv->plat->rx_queues_to_use;
1316 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1317 u32 queue;
1318
1319 /* Clear the RX descriptors */
1320 for (queue = 0; queue < rx_queue_cnt; queue++)
1321 stmmac_clear_rx_descriptors(priv, queue);
1322
1323 /* Clear the TX descriptors */
1324 for (queue = 0; queue < tx_queue_cnt; queue++)
1325 stmmac_clear_tx_descriptors(priv, queue);
1326 }
1327
1328 /**
1329 * stmmac_init_rx_buffers - init the RX descriptor buffer.
1330 * @priv: driver private structure
1331 * @p: descriptor pointer
1332 * @i: descriptor index
1333 * @flags: gfp flag
1334 * @queue: RX queue index
1335 * Description: this function is called to allocate a receive buffer, perform
1336 * the DMA mapping and init the descriptor.
1337 */
stmmac_init_rx_buffers(struct stmmac_priv * priv,struct dma_desc * p,int i,gfp_t flags,u32 queue)1338 static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
1339 int i, gfp_t flags, u32 queue)
1340 {
1341 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1342 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1343
1344 buf->page = page_pool_dev_alloc_pages(rx_q->page_pool);
1345 if (!buf->page)
1346 return -ENOMEM;
1347
1348 if (priv->sph) {
1349 buf->sec_page = page_pool_dev_alloc_pages(rx_q->page_pool);
1350 if (!buf->sec_page)
1351 return -ENOMEM;
1352
1353 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
1354 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
1355 } else {
1356 buf->sec_page = NULL;
1357 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
1358 }
1359
1360 buf->addr = page_pool_get_dma_addr(buf->page);
1361 stmmac_set_desc_addr(priv, p, buf->addr);
1362 if (priv->dma_buf_sz == BUF_SIZE_16KiB)
1363 stmmac_init_desc3(priv, p);
1364
1365 return 0;
1366 }
1367
1368 /**
1369 * stmmac_free_rx_buffer - free RX dma buffers
1370 * @priv: private structure
1371 * @queue: RX queue index
1372 * @i: buffer index.
1373 */
stmmac_free_rx_buffer(struct stmmac_priv * priv,u32 queue,int i)1374 static void stmmac_free_rx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1375 {
1376 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1377 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i];
1378
1379 if (buf->page)
1380 page_pool_put_full_page(rx_q->page_pool, buf->page, false);
1381 buf->page = NULL;
1382
1383 if (buf->sec_page)
1384 page_pool_put_full_page(rx_q->page_pool, buf->sec_page, false);
1385 buf->sec_page = NULL;
1386 }
1387
1388 /**
1389 * stmmac_free_tx_buffer - free RX dma buffers
1390 * @priv: private structure
1391 * @queue: RX queue index
1392 * @i: buffer index.
1393 */
stmmac_free_tx_buffer(struct stmmac_priv * priv,u32 queue,int i)1394 static void stmmac_free_tx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1395 {
1396 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1397
1398 if (tx_q->tx_skbuff_dma[i].buf) {
1399 if (tx_q->tx_skbuff_dma[i].map_as_page)
1400 dma_unmap_page(priv->device,
1401 tx_q->tx_skbuff_dma[i].buf,
1402 tx_q->tx_skbuff_dma[i].len,
1403 DMA_TO_DEVICE);
1404 else
1405 dma_unmap_single(priv->device,
1406 tx_q->tx_skbuff_dma[i].buf,
1407 tx_q->tx_skbuff_dma[i].len,
1408 DMA_TO_DEVICE);
1409 }
1410
1411 if (tx_q->tx_skbuff[i]) {
1412 dev_kfree_skb_any(tx_q->tx_skbuff[i]);
1413 tx_q->tx_skbuff[i] = NULL;
1414 tx_q->tx_skbuff_dma[i].buf = 0;
1415 tx_q->tx_skbuff_dma[i].map_as_page = false;
1416 }
1417 }
1418
1419 /**
1420 * init_dma_rx_desc_rings - init the RX descriptor rings
1421 * @dev: net device structure
1422 * @flags: gfp flag.
1423 * Description: this function initializes the DMA RX descriptors
1424 * and allocates the socket buffers. It supports the chained and ring
1425 * modes.
1426 */
init_dma_rx_desc_rings(struct net_device * dev,gfp_t flags)1427 static int init_dma_rx_desc_rings(struct net_device *dev, gfp_t flags)
1428 {
1429 struct stmmac_priv *priv = netdev_priv(dev);
1430 u32 rx_count = priv->plat->rx_queues_to_use;
1431 int ret = -ENOMEM;
1432 int queue;
1433 int i;
1434
1435 /* RX INITIALIZATION */
1436 netif_dbg(priv, probe, priv->dev,
1437 "SKB addresses:\nskb\t\tskb data\tdma data\n");
1438
1439 for (queue = 0; queue < rx_count; queue++) {
1440 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1441
1442 netif_dbg(priv, probe, priv->dev,
1443 "(%s) dma_rx_phy=0x%08x\n", __func__,
1444 (u32)rx_q->dma_rx_phy);
1445
1446 stmmac_clear_rx_descriptors(priv, queue);
1447
1448 for (i = 0; i < priv->dma_rx_size; i++) {
1449 struct dma_desc *p;
1450
1451 if (priv->extend_desc)
1452 p = &((rx_q->dma_erx + i)->basic);
1453 else
1454 p = rx_q->dma_rx + i;
1455
1456 ret = stmmac_init_rx_buffers(priv, p, i, flags,
1457 queue);
1458 if (ret)
1459 goto err_init_rx_buffers;
1460 }
1461
1462 rx_q->cur_rx = 0;
1463 rx_q->dirty_rx = (unsigned int)(i - priv->dma_rx_size);
1464
1465 /* Setup the chained descriptor addresses */
1466 if (priv->mode == STMMAC_CHAIN_MODE) {
1467 if (priv->extend_desc)
1468 stmmac_mode_init(priv, rx_q->dma_erx,
1469 rx_q->dma_rx_phy,
1470 priv->dma_rx_size, 1);
1471 else
1472 stmmac_mode_init(priv, rx_q->dma_rx,
1473 rx_q->dma_rx_phy,
1474 priv->dma_rx_size, 0);
1475 }
1476 }
1477
1478 return 0;
1479
1480 err_init_rx_buffers:
1481 while (queue >= 0) {
1482 while (--i >= 0)
1483 stmmac_free_rx_buffer(priv, queue, i);
1484
1485 if (queue == 0)
1486 break;
1487
1488 i = priv->dma_rx_size;
1489 queue--;
1490 }
1491
1492 return ret;
1493 }
1494
1495 /**
1496 * init_dma_tx_desc_rings - init the TX descriptor rings
1497 * @dev: net device structure.
1498 * Description: this function initializes the DMA TX descriptors
1499 * and allocates the socket buffers. It supports the chained and ring
1500 * modes.
1501 */
init_dma_tx_desc_rings(struct net_device * dev)1502 static int init_dma_tx_desc_rings(struct net_device *dev)
1503 {
1504 struct stmmac_priv *priv = netdev_priv(dev);
1505 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1506 u32 queue;
1507 int i;
1508
1509 for (queue = 0; queue < tx_queue_cnt; queue++) {
1510 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1511
1512 netif_dbg(priv, probe, priv->dev,
1513 "(%s) dma_tx_phy=0x%08x\n", __func__,
1514 (u32)tx_q->dma_tx_phy);
1515
1516 /* Setup the chained descriptor addresses */
1517 if (priv->mode == STMMAC_CHAIN_MODE) {
1518 if (priv->extend_desc)
1519 stmmac_mode_init(priv, tx_q->dma_etx,
1520 tx_q->dma_tx_phy,
1521 priv->dma_tx_size, 1);
1522 else if (!(tx_q->tbs & STMMAC_TBS_AVAIL))
1523 stmmac_mode_init(priv, tx_q->dma_tx,
1524 tx_q->dma_tx_phy,
1525 priv->dma_tx_size, 0);
1526 }
1527
1528 for (i = 0; i < priv->dma_tx_size; i++) {
1529 struct dma_desc *p;
1530 if (priv->extend_desc)
1531 p = &((tx_q->dma_etx + i)->basic);
1532 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1533 p = &((tx_q->dma_entx + i)->basic);
1534 else
1535 p = tx_q->dma_tx + i;
1536
1537 stmmac_clear_desc(priv, p);
1538
1539 tx_q->tx_skbuff_dma[i].buf = 0;
1540 tx_q->tx_skbuff_dma[i].map_as_page = false;
1541 tx_q->tx_skbuff_dma[i].len = 0;
1542 tx_q->tx_skbuff_dma[i].last_segment = false;
1543 tx_q->tx_skbuff[i] = NULL;
1544 }
1545
1546 tx_q->dirty_tx = 0;
1547 tx_q->cur_tx = 0;
1548 tx_q->mss = 0;
1549
1550 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
1551 }
1552
1553 return 0;
1554 }
1555
1556 /**
1557 * init_dma_desc_rings - init the RX/TX descriptor rings
1558 * @dev: net device structure
1559 * @flags: gfp flag.
1560 * Description: this function initializes the DMA RX/TX descriptors
1561 * and allocates the socket buffers. It supports the chained and ring
1562 * modes.
1563 */
init_dma_desc_rings(struct net_device * dev,gfp_t flags)1564 static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
1565 {
1566 struct stmmac_priv *priv = netdev_priv(dev);
1567 int ret;
1568
1569 ret = init_dma_rx_desc_rings(dev, flags);
1570 if (ret)
1571 return ret;
1572
1573 ret = init_dma_tx_desc_rings(dev);
1574
1575 stmmac_clear_descriptors(priv);
1576
1577 if (netif_msg_hw(priv))
1578 stmmac_display_rings(priv);
1579
1580 return ret;
1581 }
1582
1583 /**
1584 * dma_free_rx_skbufs - free RX dma buffers
1585 * @priv: private structure
1586 * @queue: RX queue index
1587 */
dma_free_rx_skbufs(struct stmmac_priv * priv,u32 queue)1588 static void dma_free_rx_skbufs(struct stmmac_priv *priv, u32 queue)
1589 {
1590 int i;
1591
1592 for (i = 0; i < priv->dma_rx_size; i++)
1593 stmmac_free_rx_buffer(priv, queue, i);
1594 }
1595
1596 /**
1597 * dma_free_tx_skbufs - free TX dma buffers
1598 * @priv: private structure
1599 * @queue: TX queue index
1600 */
dma_free_tx_skbufs(struct stmmac_priv * priv,u32 queue)1601 static void dma_free_tx_skbufs(struct stmmac_priv *priv, u32 queue)
1602 {
1603 int i;
1604
1605 for (i = 0; i < priv->dma_tx_size; i++)
1606 stmmac_free_tx_buffer(priv, queue, i);
1607 }
1608
1609 /**
1610 * stmmac_free_tx_skbufs - free TX skb buffers
1611 * @priv: private structure
1612 */
stmmac_free_tx_skbufs(struct stmmac_priv * priv)1613 static void stmmac_free_tx_skbufs(struct stmmac_priv *priv)
1614 {
1615 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1616 u32 queue;
1617
1618 for (queue = 0; queue < tx_queue_cnt; queue++)
1619 dma_free_tx_skbufs(priv, queue);
1620 }
1621
1622 /**
1623 * free_dma_rx_desc_resources - free RX dma desc resources
1624 * @priv: private structure
1625 */
free_dma_rx_desc_resources(struct stmmac_priv * priv)1626 static void free_dma_rx_desc_resources(struct stmmac_priv *priv)
1627 {
1628 u32 rx_count = priv->plat->rx_queues_to_use;
1629 u32 queue;
1630
1631 /* Free RX queue resources */
1632 for (queue = 0; queue < rx_count; queue++) {
1633 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1634
1635 /* Release the DMA RX socket buffers */
1636 dma_free_rx_skbufs(priv, queue);
1637
1638 /* Free DMA regions of consistent memory previously allocated */
1639 if (!priv->extend_desc)
1640 dma_free_coherent(priv->device, priv->dma_rx_size *
1641 sizeof(struct dma_desc),
1642 rx_q->dma_rx, rx_q->dma_rx_phy);
1643 else
1644 dma_free_coherent(priv->device, priv->dma_rx_size *
1645 sizeof(struct dma_extended_desc),
1646 rx_q->dma_erx, rx_q->dma_rx_phy);
1647
1648 kfree(rx_q->buf_pool);
1649 if (rx_q->page_pool)
1650 page_pool_destroy(rx_q->page_pool);
1651 }
1652 }
1653
1654 /**
1655 * free_dma_tx_desc_resources - free TX dma desc resources
1656 * @priv: private structure
1657 */
free_dma_tx_desc_resources(struct stmmac_priv * priv)1658 static void free_dma_tx_desc_resources(struct stmmac_priv *priv)
1659 {
1660 u32 tx_count = priv->plat->tx_queues_to_use;
1661 u32 queue;
1662
1663 /* Free TX queue resources */
1664 for (queue = 0; queue < tx_count; queue++) {
1665 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1666 size_t size;
1667 void *addr;
1668
1669 /* Release the DMA TX socket buffers */
1670 dma_free_tx_skbufs(priv, queue);
1671
1672 if (priv->extend_desc) {
1673 size = sizeof(struct dma_extended_desc);
1674 addr = tx_q->dma_etx;
1675 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) {
1676 size = sizeof(struct dma_edesc);
1677 addr = tx_q->dma_entx;
1678 } else {
1679 size = sizeof(struct dma_desc);
1680 addr = tx_q->dma_tx;
1681 }
1682
1683 size *= priv->dma_tx_size;
1684
1685 dma_free_coherent(priv->device, size, addr, tx_q->dma_tx_phy);
1686
1687 kfree(tx_q->tx_skbuff_dma);
1688 kfree(tx_q->tx_skbuff);
1689 }
1690 }
1691
1692 /**
1693 * alloc_dma_rx_desc_resources - alloc RX resources.
1694 * @priv: private structure
1695 * Description: according to which descriptor can be used (extend or basic)
1696 * this function allocates the resources for TX and RX paths. In case of
1697 * reception, for example, it pre-allocated the RX socket buffer in order to
1698 * allow zero-copy mechanism.
1699 */
alloc_dma_rx_desc_resources(struct stmmac_priv * priv)1700 static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv)
1701 {
1702 u32 rx_count = priv->plat->rx_queues_to_use;
1703 int ret = -ENOMEM;
1704 u32 queue;
1705
1706 /* RX queues buffers and DMA */
1707 for (queue = 0; queue < rx_count; queue++) {
1708 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1709 struct page_pool_params pp_params = { 0 };
1710 unsigned int num_pages;
1711
1712 rx_q->queue_index = queue;
1713 rx_q->priv_data = priv;
1714
1715 pp_params.flags = PP_FLAG_DMA_MAP;
1716 pp_params.pool_size = priv->dma_rx_size;
1717 num_pages = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE);
1718 pp_params.order = ilog2(num_pages);
1719 pp_params.nid = dev_to_node(priv->device);
1720 pp_params.dev = priv->device;
1721 pp_params.dma_dir = DMA_FROM_DEVICE;
1722
1723 rx_q->page_pool = page_pool_create(&pp_params);
1724 if (IS_ERR(rx_q->page_pool)) {
1725 ret = PTR_ERR(rx_q->page_pool);
1726 rx_q->page_pool = NULL;
1727 goto err_dma;
1728 }
1729
1730 rx_q->buf_pool = kcalloc(priv->dma_rx_size,
1731 sizeof(*rx_q->buf_pool),
1732 GFP_KERNEL);
1733 if (!rx_q->buf_pool)
1734 goto err_dma;
1735
1736 if (priv->extend_desc) {
1737 rx_q->dma_erx = dma_alloc_coherent(priv->device,
1738 priv->dma_rx_size *
1739 sizeof(struct dma_extended_desc),
1740 &rx_q->dma_rx_phy,
1741 GFP_KERNEL);
1742 if (!rx_q->dma_erx)
1743 goto err_dma;
1744
1745 } else {
1746 rx_q->dma_rx = dma_alloc_coherent(priv->device,
1747 priv->dma_rx_size *
1748 sizeof(struct dma_desc),
1749 &rx_q->dma_rx_phy,
1750 GFP_KERNEL);
1751 if (!rx_q->dma_rx)
1752 goto err_dma;
1753 }
1754 }
1755
1756 return 0;
1757
1758 err_dma:
1759 free_dma_rx_desc_resources(priv);
1760
1761 return ret;
1762 }
1763
1764 /**
1765 * alloc_dma_tx_desc_resources - alloc TX resources.
1766 * @priv: private structure
1767 * Description: according to which descriptor can be used (extend or basic)
1768 * this function allocates the resources for TX and RX paths. In case of
1769 * reception, for example, it pre-allocated the RX socket buffer in order to
1770 * allow zero-copy mechanism.
1771 */
alloc_dma_tx_desc_resources(struct stmmac_priv * priv)1772 static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv)
1773 {
1774 u32 tx_count = priv->plat->tx_queues_to_use;
1775 int ret = -ENOMEM;
1776 u32 queue;
1777
1778 /* TX queues buffers and DMA */
1779 for (queue = 0; queue < tx_count; queue++) {
1780 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1781 size_t size;
1782 void *addr;
1783
1784 tx_q->queue_index = queue;
1785 tx_q->priv_data = priv;
1786
1787 tx_q->tx_skbuff_dma = kcalloc(priv->dma_tx_size,
1788 sizeof(*tx_q->tx_skbuff_dma),
1789 GFP_KERNEL);
1790 if (!tx_q->tx_skbuff_dma)
1791 goto err_dma;
1792
1793 tx_q->tx_skbuff = kcalloc(priv->dma_tx_size,
1794 sizeof(struct sk_buff *),
1795 GFP_KERNEL);
1796 if (!tx_q->tx_skbuff)
1797 goto err_dma;
1798
1799 if (priv->extend_desc)
1800 size = sizeof(struct dma_extended_desc);
1801 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1802 size = sizeof(struct dma_edesc);
1803 else
1804 size = sizeof(struct dma_desc);
1805
1806 size *= priv->dma_tx_size;
1807
1808 addr = dma_alloc_coherent(priv->device, size,
1809 &tx_q->dma_tx_phy, GFP_KERNEL);
1810 if (!addr)
1811 goto err_dma;
1812
1813 if (priv->extend_desc)
1814 tx_q->dma_etx = addr;
1815 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
1816 tx_q->dma_entx = addr;
1817 else
1818 tx_q->dma_tx = addr;
1819 }
1820
1821 return 0;
1822
1823 err_dma:
1824 free_dma_tx_desc_resources(priv);
1825 return ret;
1826 }
1827
1828 /**
1829 * alloc_dma_desc_resources - alloc TX/RX resources.
1830 * @priv: private structure
1831 * Description: according to which descriptor can be used (extend or basic)
1832 * this function allocates the resources for TX and RX paths. In case of
1833 * reception, for example, it pre-allocated the RX socket buffer in order to
1834 * allow zero-copy mechanism.
1835 */
alloc_dma_desc_resources(struct stmmac_priv * priv)1836 static int alloc_dma_desc_resources(struct stmmac_priv *priv)
1837 {
1838 /* RX Allocation */
1839 int ret = alloc_dma_rx_desc_resources(priv);
1840
1841 if (ret)
1842 return ret;
1843
1844 ret = alloc_dma_tx_desc_resources(priv);
1845
1846 return ret;
1847 }
1848
1849 /**
1850 * free_dma_desc_resources - free dma desc resources
1851 * @priv: private structure
1852 */
free_dma_desc_resources(struct stmmac_priv * priv)1853 static void free_dma_desc_resources(struct stmmac_priv *priv)
1854 {
1855 /* Release the DMA RX socket buffers */
1856 free_dma_rx_desc_resources(priv);
1857
1858 /* Release the DMA TX socket buffers */
1859 free_dma_tx_desc_resources(priv);
1860 }
1861
1862 /**
1863 * stmmac_mac_enable_rx_queues - Enable MAC rx queues
1864 * @priv: driver private structure
1865 * Description: It is used for enabling the rx queues in the MAC
1866 */
stmmac_mac_enable_rx_queues(struct stmmac_priv * priv)1867 static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv)
1868 {
1869 u32 rx_queues_count = priv->plat->rx_queues_to_use;
1870 int queue;
1871 u8 mode;
1872
1873 for (queue = 0; queue < rx_queues_count; queue++) {
1874 mode = priv->plat->rx_queues_cfg[queue].mode_to_use;
1875 stmmac_rx_queue_enable(priv, priv->hw, mode, queue);
1876 }
1877 }
1878
1879 /**
1880 * stmmac_start_rx_dma - start RX DMA channel
1881 * @priv: driver private structure
1882 * @chan: RX channel index
1883 * Description:
1884 * This starts a RX DMA channel
1885 */
stmmac_start_rx_dma(struct stmmac_priv * priv,u32 chan)1886 static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan)
1887 {
1888 netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan);
1889 stmmac_start_rx(priv, priv->ioaddr, chan);
1890 }
1891
1892 /**
1893 * stmmac_start_tx_dma - start TX DMA channel
1894 * @priv: driver private structure
1895 * @chan: TX channel index
1896 * Description:
1897 * This starts a TX DMA channel
1898 */
stmmac_start_tx_dma(struct stmmac_priv * priv,u32 chan)1899 static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan)
1900 {
1901 netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan);
1902 stmmac_start_tx(priv, priv->ioaddr, chan);
1903 }
1904
1905 /**
1906 * stmmac_stop_rx_dma - stop RX DMA channel
1907 * @priv: driver private structure
1908 * @chan: RX channel index
1909 * Description:
1910 * This stops a RX DMA channel
1911 */
stmmac_stop_rx_dma(struct stmmac_priv * priv,u32 chan)1912 static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan)
1913 {
1914 netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan);
1915 stmmac_stop_rx(priv, priv->ioaddr, chan);
1916 }
1917
1918 /**
1919 * stmmac_stop_tx_dma - stop TX DMA channel
1920 * @priv: driver private structure
1921 * @chan: TX channel index
1922 * Description:
1923 * This stops a TX DMA channel
1924 */
stmmac_stop_tx_dma(struct stmmac_priv * priv,u32 chan)1925 static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan)
1926 {
1927 netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan);
1928 stmmac_stop_tx(priv, priv->ioaddr, chan);
1929 }
1930
1931 /**
1932 * stmmac_start_all_dma - start all RX and TX DMA channels
1933 * @priv: driver private structure
1934 * Description:
1935 * This starts all the RX and TX DMA channels
1936 */
stmmac_start_all_dma(struct stmmac_priv * priv)1937 static void stmmac_start_all_dma(struct stmmac_priv *priv)
1938 {
1939 u32 rx_channels_count = priv->plat->rx_queues_to_use;
1940 u32 tx_channels_count = priv->plat->tx_queues_to_use;
1941 u32 chan = 0;
1942
1943 for (chan = 0; chan < rx_channels_count; chan++)
1944 stmmac_start_rx_dma(priv, chan);
1945
1946 for (chan = 0; chan < tx_channels_count; chan++)
1947 stmmac_start_tx_dma(priv, chan);
1948 }
1949
1950 /**
1951 * stmmac_stop_all_dma - stop all RX and TX DMA channels
1952 * @priv: driver private structure
1953 * Description:
1954 * This stops the RX and TX DMA channels
1955 */
stmmac_stop_all_dma(struct stmmac_priv * priv)1956 static void stmmac_stop_all_dma(struct stmmac_priv *priv)
1957 {
1958 u32 rx_channels_count = priv->plat->rx_queues_to_use;
1959 u32 tx_channels_count = priv->plat->tx_queues_to_use;
1960 u32 chan = 0;
1961
1962 for (chan = 0; chan < rx_channels_count; chan++)
1963 stmmac_stop_rx_dma(priv, chan);
1964
1965 for (chan = 0; chan < tx_channels_count; chan++)
1966 stmmac_stop_tx_dma(priv, chan);
1967 }
1968
1969 /**
1970 * stmmac_dma_operation_mode - HW DMA operation mode
1971 * @priv: driver private structure
1972 * Description: it is used for configuring the DMA operation mode register in
1973 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
1974 */
stmmac_dma_operation_mode(struct stmmac_priv * priv)1975 static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
1976 {
1977 u32 rx_channels_count = priv->plat->rx_queues_to_use;
1978 u32 tx_channels_count = priv->plat->tx_queues_to_use;
1979 int rxfifosz = priv->plat->rx_fifo_size;
1980 int txfifosz = priv->plat->tx_fifo_size;
1981 u32 txmode = 0;
1982 u32 rxmode = 0;
1983 u32 chan = 0;
1984 u8 qmode = 0;
1985
1986 if (rxfifosz == 0)
1987 rxfifosz = priv->dma_cap.rx_fifo_size;
1988 if (txfifosz == 0)
1989 txfifosz = priv->dma_cap.tx_fifo_size;
1990
1991 /* Adjust for real per queue fifo size */
1992 rxfifosz /= rx_channels_count;
1993 txfifosz /= tx_channels_count;
1994
1995 if (priv->plat->force_thresh_dma_mode) {
1996 txmode = tc;
1997 rxmode = tc;
1998 } else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
1999 /*
2000 * In case of GMAC, SF mode can be enabled
2001 * to perform the TX COE in HW. This depends on:
2002 * 1) TX COE if actually supported
2003 * 2) There is no bugged Jumbo frame support
2004 * that needs to not insert csum in the TDES.
2005 */
2006 txmode = SF_DMA_MODE;
2007 rxmode = SF_DMA_MODE;
2008 priv->xstats.threshold = SF_DMA_MODE;
2009 } else {
2010 txmode = tc;
2011 rxmode = SF_DMA_MODE;
2012 }
2013
2014 /* configure all channels */
2015 for (chan = 0; chan < rx_channels_count; chan++) {
2016 qmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2017
2018 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan,
2019 rxfifosz, qmode);
2020 stmmac_set_dma_bfsize(priv, priv->ioaddr, priv->dma_buf_sz,
2021 chan);
2022 }
2023
2024 for (chan = 0; chan < tx_channels_count; chan++) {
2025 qmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2026
2027 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan,
2028 txfifosz, qmode);
2029 }
2030 }
2031
2032 /**
2033 * stmmac_tx_clean - to manage the transmission completion
2034 * @priv: driver private structure
2035 * @budget: napi budget limiting this functions packet handling
2036 * @queue: TX queue index
2037 * Description: it reclaims the transmit resources after transmission completes.
2038 */
stmmac_tx_clean(struct stmmac_priv * priv,int budget,u32 queue)2039 static int stmmac_tx_clean(struct stmmac_priv *priv, int budget, u32 queue)
2040 {
2041 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2042 unsigned int bytes_compl = 0, pkts_compl = 0;
2043 unsigned int entry, count = 0;
2044
2045 __netif_tx_lock_bh(netdev_get_tx_queue(priv->dev, queue));
2046
2047 priv->xstats.tx_clean++;
2048
2049 entry = tx_q->dirty_tx;
2050 while ((entry != tx_q->cur_tx) && (count < budget)) {
2051 struct sk_buff *skb = tx_q->tx_skbuff[entry];
2052 struct dma_desc *p;
2053 int status;
2054
2055 if (priv->extend_desc)
2056 p = (struct dma_desc *)(tx_q->dma_etx + entry);
2057 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
2058 p = &tx_q->dma_entx[entry].basic;
2059 else
2060 p = tx_q->dma_tx + entry;
2061
2062 status = stmmac_tx_status(priv, &priv->dev->stats,
2063 &priv->xstats, p, priv->ioaddr);
2064 /* Check if the descriptor is owned by the DMA */
2065 if (unlikely(status & tx_dma_own))
2066 break;
2067
2068 count++;
2069
2070 /* Make sure descriptor fields are read after reading
2071 * the own bit.
2072 */
2073 dma_rmb();
2074
2075 /* Just consider the last segment and ...*/
2076 if (likely(!(status & tx_not_ls))) {
2077 /* ... verify the status error condition */
2078 if (unlikely(status & tx_err)) {
2079 priv->dev->stats.tx_errors++;
2080 } else {
2081 priv->dev->stats.tx_packets++;
2082 priv->xstats.tx_pkt_n++;
2083 }
2084 stmmac_get_tx_hwtstamp(priv, p, skb);
2085 }
2086
2087 if (likely(tx_q->tx_skbuff_dma[entry].buf)) {
2088 if (tx_q->tx_skbuff_dma[entry].map_as_page)
2089 dma_unmap_page(priv->device,
2090 tx_q->tx_skbuff_dma[entry].buf,
2091 tx_q->tx_skbuff_dma[entry].len,
2092 DMA_TO_DEVICE);
2093 else
2094 dma_unmap_single(priv->device,
2095 tx_q->tx_skbuff_dma[entry].buf,
2096 tx_q->tx_skbuff_dma[entry].len,
2097 DMA_TO_DEVICE);
2098 tx_q->tx_skbuff_dma[entry].buf = 0;
2099 tx_q->tx_skbuff_dma[entry].len = 0;
2100 tx_q->tx_skbuff_dma[entry].map_as_page = false;
2101 }
2102
2103 stmmac_clean_desc3(priv, tx_q, p);
2104
2105 tx_q->tx_skbuff_dma[entry].last_segment = false;
2106 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
2107
2108 if (likely(skb != NULL)) {
2109 pkts_compl++;
2110 bytes_compl += skb->len;
2111 dev_consume_skb_any(skb);
2112 tx_q->tx_skbuff[entry] = NULL;
2113 }
2114
2115 stmmac_release_tx_desc(priv, p, priv->mode);
2116
2117 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
2118 }
2119 tx_q->dirty_tx = entry;
2120
2121 netdev_tx_completed_queue(netdev_get_tx_queue(priv->dev, queue),
2122 pkts_compl, bytes_compl);
2123
2124 if (unlikely(netif_tx_queue_stopped(netdev_get_tx_queue(priv->dev,
2125 queue))) &&
2126 stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH(priv)) {
2127
2128 netif_dbg(priv, tx_done, priv->dev,
2129 "%s: restart transmit\n", __func__);
2130 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, queue));
2131 }
2132
2133 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
2134 stmmac_enable_eee_mode(priv);
2135 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer));
2136 }
2137
2138 /* We still have pending packets, let's call for a new scheduling */
2139 if (tx_q->dirty_tx != tx_q->cur_tx)
2140 mod_timer(&tx_q->txtimer, STMMAC_COAL_TIMER(priv->tx_coal_timer));
2141
2142 __netif_tx_unlock_bh(netdev_get_tx_queue(priv->dev, queue));
2143
2144 return count;
2145 }
2146
2147 /**
2148 * stmmac_tx_err - to manage the tx error
2149 * @priv: driver private structure
2150 * @chan: channel index
2151 * Description: it cleans the descriptors and restarts the transmission
2152 * in case of transmission errors.
2153 */
stmmac_tx_err(struct stmmac_priv * priv,u32 chan)2154 static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan)
2155 {
2156 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2157
2158 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, chan));
2159
2160 stmmac_stop_tx_dma(priv, chan);
2161 dma_free_tx_skbufs(priv, chan);
2162 stmmac_clear_tx_descriptors(priv, chan);
2163 tx_q->dirty_tx = 0;
2164 tx_q->cur_tx = 0;
2165 tx_q->mss = 0;
2166 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, chan));
2167 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2168 tx_q->dma_tx_phy, chan);
2169 stmmac_start_tx_dma(priv, chan);
2170
2171 priv->dev->stats.tx_errors++;
2172 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, chan));
2173 }
2174
2175 /**
2176 * stmmac_set_dma_operation_mode - Set DMA operation mode by channel
2177 * @priv: driver private structure
2178 * @txmode: TX operating mode
2179 * @rxmode: RX operating mode
2180 * @chan: channel index
2181 * Description: it is used for configuring of the DMA operation mode in
2182 * runtime in order to program the tx/rx DMA thresholds or Store-And-Forward
2183 * mode.
2184 */
stmmac_set_dma_operation_mode(struct stmmac_priv * priv,u32 txmode,u32 rxmode,u32 chan)2185 static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
2186 u32 rxmode, u32 chan)
2187 {
2188 u8 rxqmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2189 u8 txqmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2190 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2191 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2192 int rxfifosz = priv->plat->rx_fifo_size;
2193 int txfifosz = priv->plat->tx_fifo_size;
2194
2195 if (rxfifosz == 0)
2196 rxfifosz = priv->dma_cap.rx_fifo_size;
2197 if (txfifosz == 0)
2198 txfifosz = priv->dma_cap.tx_fifo_size;
2199
2200 /* Adjust for real per queue fifo size */
2201 rxfifosz /= rx_channels_count;
2202 txfifosz /= tx_channels_count;
2203
2204 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, rxfifosz, rxqmode);
2205 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, txfifosz, txqmode);
2206 }
2207
stmmac_safety_feat_interrupt(struct stmmac_priv * priv)2208 static bool stmmac_safety_feat_interrupt(struct stmmac_priv *priv)
2209 {
2210 int ret;
2211
2212 ret = stmmac_safety_feat_irq_status(priv, priv->dev,
2213 priv->ioaddr, priv->dma_cap.asp, &priv->sstats);
2214 if (ret && (ret != -EINVAL)) {
2215 stmmac_global_err(priv);
2216 return true;
2217 }
2218
2219 return false;
2220 }
2221
stmmac_napi_check(struct stmmac_priv * priv,u32 chan)2222 static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan)
2223 {
2224 int status = stmmac_dma_interrupt_status(priv, priv->ioaddr,
2225 &priv->xstats, chan);
2226 struct stmmac_channel *ch = &priv->channel[chan];
2227 unsigned long flags;
2228
2229 if ((status & handle_rx) && (chan < priv->plat->rx_queues_to_use)) {
2230 if (napi_schedule_prep(&ch->rx_napi)) {
2231 spin_lock_irqsave(&ch->lock, flags);
2232 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
2233 spin_unlock_irqrestore(&ch->lock, flags);
2234 __napi_schedule(&ch->rx_napi);
2235 }
2236 }
2237
2238 if ((status & handle_tx) && (chan < priv->plat->tx_queues_to_use)) {
2239 if (napi_schedule_prep(&ch->tx_napi)) {
2240 spin_lock_irqsave(&ch->lock, flags);
2241 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
2242 spin_unlock_irqrestore(&ch->lock, flags);
2243 __napi_schedule(&ch->tx_napi);
2244 }
2245 }
2246
2247 return status;
2248 }
2249
2250 /**
2251 * stmmac_dma_interrupt - DMA ISR
2252 * @priv: driver private structure
2253 * Description: this is the DMA ISR. It is called by the main ISR.
2254 * It calls the dwmac dma routine and schedule poll method in case of some
2255 * work can be done.
2256 */
stmmac_dma_interrupt(struct stmmac_priv * priv)2257 static void stmmac_dma_interrupt(struct stmmac_priv *priv)
2258 {
2259 u32 tx_channel_count = priv->plat->tx_queues_to_use;
2260 u32 rx_channel_count = priv->plat->rx_queues_to_use;
2261 u32 channels_to_check = tx_channel_count > rx_channel_count ?
2262 tx_channel_count : rx_channel_count;
2263 u32 chan;
2264 int status[max_t(u32, MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES)];
2265
2266 /* Make sure we never check beyond our status buffer. */
2267 if (WARN_ON_ONCE(channels_to_check > ARRAY_SIZE(status)))
2268 channels_to_check = ARRAY_SIZE(status);
2269
2270 for (chan = 0; chan < channels_to_check; chan++)
2271 status[chan] = stmmac_napi_check(priv, chan);
2272
2273 for (chan = 0; chan < tx_channel_count; chan++) {
2274 if (unlikely(status[chan] & tx_hard_error_bump_tc)) {
2275 /* Try to bump up the dma threshold on this failure */
2276 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
2277 (tc <= 256)) {
2278 tc += 64;
2279 if (priv->plat->force_thresh_dma_mode)
2280 stmmac_set_dma_operation_mode(priv,
2281 tc,
2282 tc,
2283 chan);
2284 else
2285 stmmac_set_dma_operation_mode(priv,
2286 tc,
2287 SF_DMA_MODE,
2288 chan);
2289 priv->xstats.threshold = tc;
2290 }
2291 } else if (unlikely(status[chan] == tx_hard_error)) {
2292 stmmac_tx_err(priv, chan);
2293 }
2294 }
2295 }
2296
2297 /**
2298 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
2299 * @priv: driver private structure
2300 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
2301 */
stmmac_mmc_setup(struct stmmac_priv * priv)2302 static void stmmac_mmc_setup(struct stmmac_priv *priv)
2303 {
2304 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
2305 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
2306
2307 stmmac_mmc_intr_all_mask(priv, priv->mmcaddr);
2308
2309 if (priv->dma_cap.rmon) {
2310 stmmac_mmc_ctrl(priv, priv->mmcaddr, mode);
2311 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
2312 } else
2313 netdev_info(priv->dev, "No MAC Management Counters available\n");
2314 }
2315
2316 /**
2317 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
2318 * @priv: driver private structure
2319 * Description:
2320 * new GMAC chip generations have a new register to indicate the
2321 * presence of the optional feature/functions.
2322 * This can be also used to override the value passed through the
2323 * platform and necessary for old MAC10/100 and GMAC chips.
2324 */
stmmac_get_hw_features(struct stmmac_priv * priv)2325 static int stmmac_get_hw_features(struct stmmac_priv *priv)
2326 {
2327 return stmmac_get_hw_feature(priv, priv->ioaddr, &priv->dma_cap) == 0;
2328 }
2329
2330 /**
2331 * stmmac_check_ether_addr - check if the MAC addr is valid
2332 * @priv: driver private structure
2333 * Description:
2334 * it is to verify if the MAC address is valid, in case of failures it
2335 * generates a random MAC address
2336 */
stmmac_check_ether_addr(struct stmmac_priv * priv)2337 static void stmmac_check_ether_addr(struct stmmac_priv *priv)
2338 {
2339 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
2340 stmmac_get_umac_addr(priv, priv->hw, priv->dev->dev_addr, 0);
2341 if (!is_valid_ether_addr(priv->dev->dev_addr))
2342 eth_hw_addr_random(priv->dev);
2343 dev_info(priv->device, "device MAC address %pM\n",
2344 priv->dev->dev_addr);
2345 }
2346 }
2347
2348 /**
2349 * stmmac_init_dma_engine - DMA init.
2350 * @priv: driver private structure
2351 * Description:
2352 * It inits the DMA invoking the specific MAC/GMAC callback.
2353 * Some DMA parameters can be passed from the platform;
2354 * in case of these are not passed a default is kept for the MAC or GMAC.
2355 */
stmmac_init_dma_engine(struct stmmac_priv * priv)2356 static int stmmac_init_dma_engine(struct stmmac_priv *priv)
2357 {
2358 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2359 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2360 u32 dma_csr_ch = max(rx_channels_count, tx_channels_count);
2361 struct stmmac_rx_queue *rx_q;
2362 struct stmmac_tx_queue *tx_q;
2363 u32 chan = 0;
2364 int atds = 0;
2365 int ret = 0;
2366
2367 if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
2368 dev_err(priv->device, "Invalid DMA configuration\n");
2369 return -EINVAL;
2370 }
2371
2372 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
2373 atds = 1;
2374
2375 ret = stmmac_reset(priv, priv->ioaddr);
2376 if (ret) {
2377 dev_err(priv->device, "Failed to reset the dma\n");
2378 return ret;
2379 }
2380
2381 /* DMA Configuration */
2382 stmmac_dma_init(priv, priv->ioaddr, priv->plat->dma_cfg, atds);
2383
2384 if (priv->plat->axi)
2385 stmmac_axi(priv, priv->ioaddr, priv->plat->axi);
2386
2387 /* DMA CSR Channel configuration */
2388 for (chan = 0; chan < dma_csr_ch; chan++)
2389 stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
2390
2391 /* DMA RX Channel Configuration */
2392 for (chan = 0; chan < rx_channels_count; chan++) {
2393 rx_q = &priv->rx_queue[chan];
2394
2395 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2396 rx_q->dma_rx_phy, chan);
2397
2398 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
2399 (priv->dma_rx_size *
2400 sizeof(struct dma_desc));
2401 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
2402 rx_q->rx_tail_addr, chan);
2403 }
2404
2405 /* DMA TX Channel Configuration */
2406 for (chan = 0; chan < tx_channels_count; chan++) {
2407 tx_q = &priv->tx_queue[chan];
2408
2409 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2410 tx_q->dma_tx_phy, chan);
2411
2412 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
2413 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
2414 tx_q->tx_tail_addr, chan);
2415 }
2416
2417 return ret;
2418 }
2419
stmmac_tx_timer_arm(struct stmmac_priv * priv,u32 queue)2420 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue)
2421 {
2422 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2423
2424 mod_timer(&tx_q->txtimer, STMMAC_COAL_TIMER(priv->tx_coal_timer));
2425 }
2426
2427 /**
2428 * stmmac_tx_timer - mitigation sw timer for tx.
2429 * @t: data pointer
2430 * Description:
2431 * This is the timer handler to directly invoke the stmmac_tx_clean.
2432 */
stmmac_tx_timer(struct timer_list * t)2433 static void stmmac_tx_timer(struct timer_list *t)
2434 {
2435 struct stmmac_tx_queue *tx_q = from_timer(tx_q, t, txtimer);
2436 struct stmmac_priv *priv = tx_q->priv_data;
2437 struct stmmac_channel *ch;
2438
2439 ch = &priv->channel[tx_q->queue_index];
2440
2441 if (likely(napi_schedule_prep(&ch->tx_napi))) {
2442 unsigned long flags;
2443
2444 spin_lock_irqsave(&ch->lock, flags);
2445 stmmac_disable_dma_irq(priv, priv->ioaddr, ch->index, 0, 1);
2446 spin_unlock_irqrestore(&ch->lock, flags);
2447 __napi_schedule(&ch->tx_napi);
2448 }
2449 }
2450
2451 /**
2452 * stmmac_init_coalesce - init mitigation options.
2453 * @priv: driver private structure
2454 * Description:
2455 * This inits the coalesce parameters: i.e. timer rate,
2456 * timer handler and default threshold used for enabling the
2457 * interrupt on completion bit.
2458 */
stmmac_init_coalesce(struct stmmac_priv * priv)2459 static void stmmac_init_coalesce(struct stmmac_priv *priv)
2460 {
2461 u32 tx_channel_count = priv->plat->tx_queues_to_use;
2462 u32 chan;
2463
2464 priv->tx_coal_frames = STMMAC_TX_FRAMES;
2465 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
2466 priv->rx_coal_frames = STMMAC_RX_FRAMES;
2467
2468 for (chan = 0; chan < tx_channel_count; chan++) {
2469 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2470
2471 timer_setup(&tx_q->txtimer, stmmac_tx_timer, 0);
2472 }
2473 }
2474
stmmac_set_rings_length(struct stmmac_priv * priv)2475 static void stmmac_set_rings_length(struct stmmac_priv *priv)
2476 {
2477 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2478 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2479 u32 chan;
2480
2481 /* set TX ring length */
2482 for (chan = 0; chan < tx_channels_count; chan++)
2483 stmmac_set_tx_ring_len(priv, priv->ioaddr,
2484 (priv->dma_tx_size - 1), chan);
2485
2486 /* set RX ring length */
2487 for (chan = 0; chan < rx_channels_count; chan++)
2488 stmmac_set_rx_ring_len(priv, priv->ioaddr,
2489 (priv->dma_rx_size - 1), chan);
2490 }
2491
2492 /**
2493 * stmmac_set_tx_queue_weight - Set TX queue weight
2494 * @priv: driver private structure
2495 * Description: It is used for setting TX queues weight
2496 */
stmmac_set_tx_queue_weight(struct stmmac_priv * priv)2497 static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
2498 {
2499 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2500 u32 weight;
2501 u32 queue;
2502
2503 for (queue = 0; queue < tx_queues_count; queue++) {
2504 weight = priv->plat->tx_queues_cfg[queue].weight;
2505 stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue);
2506 }
2507 }
2508
2509 /**
2510 * stmmac_configure_cbs - Configure CBS in TX queue
2511 * @priv: driver private structure
2512 * Description: It is used for configuring CBS in AVB TX queues
2513 */
stmmac_configure_cbs(struct stmmac_priv * priv)2514 static void stmmac_configure_cbs(struct stmmac_priv *priv)
2515 {
2516 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2517 u32 mode_to_use;
2518 u32 queue;
2519
2520 /* queue 0 is reserved for legacy traffic */
2521 for (queue = 1; queue < tx_queues_count; queue++) {
2522 mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
2523 if (mode_to_use == MTL_QUEUE_DCB)
2524 continue;
2525
2526 stmmac_config_cbs(priv, priv->hw,
2527 priv->plat->tx_queues_cfg[queue].send_slope,
2528 priv->plat->tx_queues_cfg[queue].idle_slope,
2529 priv->plat->tx_queues_cfg[queue].high_credit,
2530 priv->plat->tx_queues_cfg[queue].low_credit,
2531 queue);
2532 }
2533 }
2534
2535 /**
2536 * stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel
2537 * @priv: driver private structure
2538 * Description: It is used for mapping RX queues to RX dma channels
2539 */
stmmac_rx_queue_dma_chan_map(struct stmmac_priv * priv)2540 static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv)
2541 {
2542 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2543 u32 queue;
2544 u32 chan;
2545
2546 for (queue = 0; queue < rx_queues_count; queue++) {
2547 chan = priv->plat->rx_queues_cfg[queue].chan;
2548 stmmac_map_mtl_to_dma(priv, priv->hw, queue, chan);
2549 }
2550 }
2551
2552 /**
2553 * stmmac_mac_config_rx_queues_prio - Configure RX Queue priority
2554 * @priv: driver private structure
2555 * Description: It is used for configuring the RX Queue Priority
2556 */
stmmac_mac_config_rx_queues_prio(struct stmmac_priv * priv)2557 static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
2558 {
2559 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2560 u32 queue;
2561 u32 prio;
2562
2563 for (queue = 0; queue < rx_queues_count; queue++) {
2564 if (!priv->plat->rx_queues_cfg[queue].use_prio)
2565 continue;
2566
2567 prio = priv->plat->rx_queues_cfg[queue].prio;
2568 stmmac_rx_queue_prio(priv, priv->hw, prio, queue);
2569 }
2570 }
2571
2572 /**
2573 * stmmac_mac_config_tx_queues_prio - Configure TX Queue priority
2574 * @priv: driver private structure
2575 * Description: It is used for configuring the TX Queue Priority
2576 */
stmmac_mac_config_tx_queues_prio(struct stmmac_priv * priv)2577 static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv)
2578 {
2579 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2580 u32 queue;
2581 u32 prio;
2582
2583 for (queue = 0; queue < tx_queues_count; queue++) {
2584 if (!priv->plat->tx_queues_cfg[queue].use_prio)
2585 continue;
2586
2587 prio = priv->plat->tx_queues_cfg[queue].prio;
2588 stmmac_tx_queue_prio(priv, priv->hw, prio, queue);
2589 }
2590 }
2591
2592 /**
2593 * stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing
2594 * @priv: driver private structure
2595 * Description: It is used for configuring the RX queue routing
2596 */
stmmac_mac_config_rx_queues_routing(struct stmmac_priv * priv)2597 static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv)
2598 {
2599 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2600 u32 queue;
2601 u8 packet;
2602
2603 for (queue = 0; queue < rx_queues_count; queue++) {
2604 /* no specific packet type routing specified for the queue */
2605 if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0)
2606 continue;
2607
2608 packet = priv->plat->rx_queues_cfg[queue].pkt_route;
2609 stmmac_rx_queue_routing(priv, priv->hw, packet, queue);
2610 }
2611 }
2612
stmmac_mac_config_rss(struct stmmac_priv * priv)2613 static void stmmac_mac_config_rss(struct stmmac_priv *priv)
2614 {
2615 if (!priv->dma_cap.rssen || !priv->plat->rss_en) {
2616 priv->rss.enable = false;
2617 return;
2618 }
2619
2620 if (priv->dev->features & NETIF_F_RXHASH)
2621 priv->rss.enable = true;
2622 else
2623 priv->rss.enable = false;
2624
2625 stmmac_rss_configure(priv, priv->hw, &priv->rss,
2626 priv->plat->rx_queues_to_use);
2627 }
2628
2629 /**
2630 * stmmac_mtl_configuration - Configure MTL
2631 * @priv: driver private structure
2632 * Description: It is used for configurring MTL
2633 */
stmmac_mtl_configuration(struct stmmac_priv * priv)2634 static void stmmac_mtl_configuration(struct stmmac_priv *priv)
2635 {
2636 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2637 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2638
2639 if (tx_queues_count > 1)
2640 stmmac_set_tx_queue_weight(priv);
2641
2642 /* Configure MTL RX algorithms */
2643 if (rx_queues_count > 1)
2644 stmmac_prog_mtl_rx_algorithms(priv, priv->hw,
2645 priv->plat->rx_sched_algorithm);
2646
2647 /* Configure MTL TX algorithms */
2648 if (tx_queues_count > 1)
2649 stmmac_prog_mtl_tx_algorithms(priv, priv->hw,
2650 priv->plat->tx_sched_algorithm);
2651
2652 /* Configure CBS in AVB TX queues */
2653 if (tx_queues_count > 1)
2654 stmmac_configure_cbs(priv);
2655
2656 /* Map RX MTL to DMA channels */
2657 stmmac_rx_queue_dma_chan_map(priv);
2658
2659 /* Enable MAC RX Queues */
2660 stmmac_mac_enable_rx_queues(priv);
2661
2662 /* Set RX priorities */
2663 if (rx_queues_count > 1)
2664 stmmac_mac_config_rx_queues_prio(priv);
2665
2666 /* Set TX priorities */
2667 if (tx_queues_count > 1)
2668 stmmac_mac_config_tx_queues_prio(priv);
2669
2670 /* Set RX routing */
2671 if (rx_queues_count > 1)
2672 stmmac_mac_config_rx_queues_routing(priv);
2673
2674 /* Receive Side Scaling */
2675 if (rx_queues_count > 1)
2676 stmmac_mac_config_rss(priv);
2677 }
2678
stmmac_safety_feat_configuration(struct stmmac_priv * priv)2679 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
2680 {
2681 if (priv->dma_cap.asp) {
2682 netdev_info(priv->dev, "Enabling Safety Features\n");
2683 stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp);
2684 } else {
2685 netdev_info(priv->dev, "No Safety Features support found\n");
2686 }
2687 }
2688
2689 /**
2690 * stmmac_hw_setup - setup mac in a usable state.
2691 * @dev : pointer to the device structure.
2692 * @ptp_register: register PTP if set
2693 * Description:
2694 * this is the main function to setup the HW in a usable state because the
2695 * dma engine is reset, the core registers are configured (e.g. AXI,
2696 * Checksum features, timers). The DMA is ready to start receiving and
2697 * transmitting.
2698 * Return value:
2699 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2700 * file on failure.
2701 */
stmmac_hw_setup(struct net_device * dev,bool ptp_register)2702 static int stmmac_hw_setup(struct net_device *dev, bool ptp_register)
2703 {
2704 struct stmmac_priv *priv = netdev_priv(dev);
2705 u32 rx_cnt = priv->plat->rx_queues_to_use;
2706 u32 tx_cnt = priv->plat->tx_queues_to_use;
2707 u32 chan;
2708 int ret;
2709
2710 /* DMA initialization and SW reset */
2711 ret = stmmac_init_dma_engine(priv);
2712 if (ret < 0) {
2713 netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
2714 __func__);
2715 return ret;
2716 }
2717
2718 /* Copy the MAC addr into the HW */
2719 stmmac_set_umac_addr(priv, priv->hw, dev->dev_addr, 0);
2720
2721 /* PS and related bits will be programmed according to the speed */
2722 if (priv->hw->pcs) {
2723 int speed = priv->plat->mac_port_sel_speed;
2724
2725 if ((speed == SPEED_10) || (speed == SPEED_100) ||
2726 (speed == SPEED_1000)) {
2727 priv->hw->ps = speed;
2728 } else {
2729 dev_warn(priv->device, "invalid port speed\n");
2730 priv->hw->ps = 0;
2731 }
2732 }
2733
2734 /* Initialize the MAC Core */
2735 stmmac_core_init(priv, priv->hw, dev);
2736
2737 /* Initialize MTL*/
2738 stmmac_mtl_configuration(priv);
2739
2740 /* Initialize Safety Features */
2741 stmmac_safety_feat_configuration(priv);
2742
2743 ret = stmmac_rx_ipc(priv, priv->hw);
2744 if (!ret) {
2745 netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
2746 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
2747 priv->hw->rx_csum = 0;
2748 }
2749
2750 /* Enable the MAC Rx/Tx */
2751 stmmac_mac_set(priv, priv->ioaddr, true);
2752
2753 /* Set the HW DMA mode and the COE */
2754 stmmac_dma_operation_mode(priv);
2755
2756 stmmac_mmc_setup(priv);
2757
2758 ret = stmmac_init_ptp(priv);
2759 if (ret == -EOPNOTSUPP)
2760 netdev_warn(priv->dev, "PTP not supported by HW\n");
2761 else if (ret)
2762 netdev_warn(priv->dev, "PTP init failed\n");
2763 else if (ptp_register)
2764 stmmac_ptp_register(priv);
2765
2766 priv->eee_tw_timer = STMMAC_DEFAULT_TWT_LS;
2767
2768 /* Convert the timer from msec to usec */
2769 if (!priv->tx_lpi_timer)
2770 priv->tx_lpi_timer = eee_timer * 1000;
2771
2772 if (priv->use_riwt) {
2773 if (!priv->rx_riwt)
2774 priv->rx_riwt = DEF_DMA_RIWT;
2775
2776 ret = stmmac_rx_watchdog(priv, priv->ioaddr, priv->rx_riwt, rx_cnt);
2777 }
2778
2779 if (priv->hw->pcs)
2780 stmmac_pcs_ctrl_ane(priv, priv->ioaddr, 1, priv->hw->ps, 0);
2781
2782 /* set TX and RX rings length */
2783 stmmac_set_rings_length(priv);
2784
2785 /* Enable TSO */
2786 if (priv->tso) {
2787 for (chan = 0; chan < tx_cnt; chan++) {
2788 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2789
2790 /* TSO and TBS cannot co-exist */
2791 if (tx_q->tbs & STMMAC_TBS_AVAIL)
2792 continue;
2793
2794 stmmac_enable_tso(priv, priv->ioaddr, 1, chan);
2795 }
2796 }
2797
2798 /* Enable Split Header */
2799 if (priv->sph && priv->hw->rx_csum) {
2800 for (chan = 0; chan < rx_cnt; chan++)
2801 stmmac_enable_sph(priv, priv->ioaddr, 1, chan);
2802 }
2803
2804 /* VLAN Tag Insertion */
2805 if (priv->dma_cap.vlins)
2806 stmmac_enable_vlan(priv, priv->hw, STMMAC_VLAN_INSERT);
2807
2808 /* TBS */
2809 for (chan = 0; chan < tx_cnt; chan++) {
2810 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2811 int enable = tx_q->tbs & STMMAC_TBS_AVAIL;
2812
2813 stmmac_enable_tbs(priv, priv->ioaddr, enable, chan);
2814 }
2815
2816 /* Configure real RX and TX queues */
2817 netif_set_real_num_rx_queues(dev, priv->plat->rx_queues_to_use);
2818 netif_set_real_num_tx_queues(dev, priv->plat->tx_queues_to_use);
2819
2820 /* Start the ball rolling... */
2821 stmmac_start_all_dma(priv);
2822
2823 return 0;
2824 }
2825
stmmac_hw_teardown(struct net_device * dev)2826 static void stmmac_hw_teardown(struct net_device *dev)
2827 {
2828 struct stmmac_priv *priv = netdev_priv(dev);
2829
2830 clk_disable_unprepare(priv->plat->clk_ptp_ref);
2831 }
2832
2833 /**
2834 * stmmac_open - open entry point of the driver
2835 * @dev : pointer to the device structure.
2836 * Description:
2837 * This function is the open entry point of the driver.
2838 * Return value:
2839 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2840 * file on failure.
2841 */
stmmac_open(struct net_device * dev)2842 static int stmmac_open(struct net_device *dev)
2843 {
2844 struct stmmac_priv *priv = netdev_priv(dev);
2845 int bfsize = 0;
2846 u32 chan;
2847 int ret;
2848
2849 ret = pm_runtime_get_sync(priv->device);
2850 if (ret < 0) {
2851 pm_runtime_put_noidle(priv->device);
2852 return ret;
2853 }
2854
2855 if (priv->hw->pcs != STMMAC_PCS_TBI &&
2856 priv->hw->pcs != STMMAC_PCS_RTBI &&
2857 priv->hw->xpcs == NULL) {
2858 ret = stmmac_init_phy(dev);
2859 if (ret) {
2860 netdev_err(priv->dev,
2861 "%s: Cannot attach to PHY (error: %d)\n",
2862 __func__, ret);
2863 goto init_phy_error;
2864 }
2865 }
2866
2867 /* Extra statistics */
2868 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
2869 priv->xstats.threshold = tc;
2870
2871 bfsize = stmmac_set_16kib_bfsize(priv, dev->mtu);
2872 if (bfsize < 0)
2873 bfsize = 0;
2874
2875 if (bfsize < BUF_SIZE_16KiB)
2876 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
2877
2878 priv->dma_buf_sz = bfsize;
2879 buf_sz = bfsize;
2880
2881 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
2882
2883 if (!priv->dma_tx_size)
2884 priv->dma_tx_size = DMA_DEFAULT_TX_SIZE;
2885 if (!priv->dma_rx_size)
2886 priv->dma_rx_size = DMA_DEFAULT_RX_SIZE;
2887
2888 /* Earlier check for TBS */
2889 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) {
2890 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2891 int tbs_en = priv->plat->tx_queues_cfg[chan].tbs_en;
2892
2893 /* Setup per-TXQ tbs flag before TX descriptor alloc */
2894 tx_q->tbs |= tbs_en ? STMMAC_TBS_AVAIL : 0;
2895 }
2896
2897 ret = alloc_dma_desc_resources(priv);
2898 if (ret < 0) {
2899 netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
2900 __func__);
2901 goto dma_desc_error;
2902 }
2903
2904 ret = init_dma_desc_rings(dev, GFP_KERNEL);
2905 if (ret < 0) {
2906 netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n",
2907 __func__);
2908 goto init_error;
2909 }
2910
2911 ret = stmmac_hw_setup(dev, true);
2912 if (ret < 0) {
2913 netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
2914 goto init_error;
2915 }
2916
2917 stmmac_init_coalesce(priv);
2918
2919 phylink_start(priv->phylink);
2920 /* We may have called phylink_speed_down before */
2921 phylink_speed_up(priv->phylink);
2922
2923 /* Request the IRQ lines */
2924 ret = request_irq(dev->irq, stmmac_interrupt,
2925 IRQF_SHARED, dev->name, dev);
2926 if (unlikely(ret < 0)) {
2927 netdev_err(priv->dev,
2928 "%s: ERROR: allocating the IRQ %d (error: %d)\n",
2929 __func__, dev->irq, ret);
2930 goto irq_error;
2931 }
2932
2933 /* Request the Wake IRQ in case of another line is used for WoL */
2934 if (priv->wol_irq != dev->irq) {
2935 ret = request_irq(priv->wol_irq, stmmac_interrupt,
2936 IRQF_SHARED, dev->name, dev);
2937 if (unlikely(ret < 0)) {
2938 netdev_err(priv->dev,
2939 "%s: ERROR: allocating the WoL IRQ %d (%d)\n",
2940 __func__, priv->wol_irq, ret);
2941 goto wolirq_error;
2942 }
2943 }
2944
2945 /* Request the IRQ lines */
2946 if (priv->lpi_irq > 0) {
2947 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
2948 dev->name, dev);
2949 if (unlikely(ret < 0)) {
2950 netdev_err(priv->dev,
2951 "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
2952 __func__, priv->lpi_irq, ret);
2953 goto lpiirq_error;
2954 }
2955 }
2956
2957 stmmac_enable_all_queues(priv);
2958 netif_tx_start_all_queues(priv->dev);
2959
2960 return 0;
2961
2962 lpiirq_error:
2963 if (priv->wol_irq != dev->irq)
2964 free_irq(priv->wol_irq, dev);
2965 wolirq_error:
2966 free_irq(dev->irq, dev);
2967 irq_error:
2968 phylink_stop(priv->phylink);
2969
2970 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
2971 del_timer_sync(&priv->tx_queue[chan].txtimer);
2972
2973 stmmac_hw_teardown(dev);
2974 init_error:
2975 free_dma_desc_resources(priv);
2976 dma_desc_error:
2977 phylink_disconnect_phy(priv->phylink);
2978 init_phy_error:
2979 pm_runtime_put(priv->device);
2980 return ret;
2981 }
2982
2983 /**
2984 * stmmac_release - close entry point of the driver
2985 * @dev : device pointer.
2986 * Description:
2987 * This is the stop entry point of the driver.
2988 */
stmmac_release(struct net_device * dev)2989 static int stmmac_release(struct net_device *dev)
2990 {
2991 struct stmmac_priv *priv = netdev_priv(dev);
2992 u32 chan;
2993
2994 if (device_may_wakeup(priv->device))
2995 phylink_speed_down(priv->phylink, false);
2996 /* Stop and disconnect the PHY */
2997 phylink_stop(priv->phylink);
2998 phylink_disconnect_phy(priv->phylink);
2999
3000 stmmac_disable_all_queues(priv);
3001
3002 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
3003 del_timer_sync(&priv->tx_queue[chan].txtimer);
3004
3005 /* Free the IRQ lines */
3006 free_irq(dev->irq, dev);
3007 if (priv->wol_irq != dev->irq)
3008 free_irq(priv->wol_irq, dev);
3009 if (priv->lpi_irq > 0)
3010 free_irq(priv->lpi_irq, dev);
3011
3012 if (priv->eee_enabled) {
3013 priv->tx_path_in_lpi_mode = false;
3014 del_timer_sync(&priv->eee_ctrl_timer);
3015 }
3016
3017 /* Stop TX/RX DMA and clear the descriptors */
3018 stmmac_stop_all_dma(priv);
3019
3020 /* Release and free the Rx/Tx resources */
3021 free_dma_desc_resources(priv);
3022
3023 /* Disable the MAC Rx/Tx */
3024 stmmac_mac_set(priv, priv->ioaddr, false);
3025
3026 netif_carrier_off(dev);
3027
3028 stmmac_release_ptp(priv);
3029
3030 pm_runtime_put(priv->device);
3031
3032 return 0;
3033 }
3034
stmmac_vlan_insert(struct stmmac_priv * priv,struct sk_buff * skb,struct stmmac_tx_queue * tx_q)3035 static bool stmmac_vlan_insert(struct stmmac_priv *priv, struct sk_buff *skb,
3036 struct stmmac_tx_queue *tx_q)
3037 {
3038 u16 tag = 0x0, inner_tag = 0x0;
3039 u32 inner_type = 0x0;
3040 struct dma_desc *p;
3041
3042 if (!priv->dma_cap.vlins)
3043 return false;
3044 if (!skb_vlan_tag_present(skb))
3045 return false;
3046 if (skb->vlan_proto == htons(ETH_P_8021AD)) {
3047 inner_tag = skb_vlan_tag_get(skb);
3048 inner_type = STMMAC_VLAN_INSERT;
3049 }
3050
3051 tag = skb_vlan_tag_get(skb);
3052
3053 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3054 p = &tx_q->dma_entx[tx_q->cur_tx].basic;
3055 else
3056 p = &tx_q->dma_tx[tx_q->cur_tx];
3057
3058 if (stmmac_set_desc_vlan_tag(priv, p, tag, inner_tag, inner_type))
3059 return false;
3060
3061 stmmac_set_tx_owner(priv, p);
3062 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
3063 return true;
3064 }
3065
3066 /**
3067 * stmmac_tso_allocator - close entry point of the driver
3068 * @priv: driver private structure
3069 * @des: buffer start address
3070 * @total_len: total length to fill in descriptors
3071 * @last_segment: condition for the last descriptor
3072 * @queue: TX queue index
3073 * Description:
3074 * This function fills descriptor and request new descriptors according to
3075 * buffer length to fill
3076 */
stmmac_tso_allocator(struct stmmac_priv * priv,dma_addr_t des,int total_len,bool last_segment,u32 queue)3077 static void stmmac_tso_allocator(struct stmmac_priv *priv, dma_addr_t des,
3078 int total_len, bool last_segment, u32 queue)
3079 {
3080 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3081 struct dma_desc *desc;
3082 u32 buff_size;
3083 int tmp_len;
3084
3085 tmp_len = total_len;
3086
3087 while (tmp_len > 0) {
3088 dma_addr_t curr_addr;
3089
3090 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
3091 priv->dma_tx_size);
3092 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
3093
3094 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3095 desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3096 else
3097 desc = &tx_q->dma_tx[tx_q->cur_tx];
3098
3099 curr_addr = des + (total_len - tmp_len);
3100 if (priv->dma_cap.addr64 <= 32)
3101 desc->des0 = cpu_to_le32(curr_addr);
3102 else
3103 stmmac_set_desc_addr(priv, desc, curr_addr);
3104
3105 buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
3106 TSO_MAX_BUFF_SIZE : tmp_len;
3107
3108 stmmac_prepare_tso_tx_desc(priv, desc, 0, buff_size,
3109 0, 1,
3110 (last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE),
3111 0, 0);
3112
3113 tmp_len -= TSO_MAX_BUFF_SIZE;
3114 }
3115 }
3116
3117 /**
3118 * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
3119 * @skb : the socket buffer
3120 * @dev : device pointer
3121 * Description: this is the transmit function that is called on TSO frames
3122 * (support available on GMAC4 and newer chips).
3123 * Diagram below show the ring programming in case of TSO frames:
3124 *
3125 * First Descriptor
3126 * --------
3127 * | DES0 |---> buffer1 = L2/L3/L4 header
3128 * | DES1 |---> TCP Payload (can continue on next descr...)
3129 * | DES2 |---> buffer 1 and 2 len
3130 * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
3131 * --------
3132 * |
3133 * ...
3134 * |
3135 * --------
3136 * | DES0 | --| Split TCP Payload on Buffers 1 and 2
3137 * | DES1 | --|
3138 * | DES2 | --> buffer 1 and 2 len
3139 * | DES3 |
3140 * --------
3141 *
3142 * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
3143 */
stmmac_tso_xmit(struct sk_buff * skb,struct net_device * dev)3144 static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
3145 {
3146 struct dma_desc *desc, *first, *mss_desc = NULL;
3147 struct stmmac_priv *priv = netdev_priv(dev);
3148 int desc_size, tmp_pay_len = 0, first_tx;
3149 int nfrags = skb_shinfo(skb)->nr_frags;
3150 u32 queue = skb_get_queue_mapping(skb);
3151 unsigned int first_entry, tx_packets;
3152 struct stmmac_tx_queue *tx_q;
3153 bool has_vlan, set_ic;
3154 u8 proto_hdr_len, hdr;
3155 u32 pay_len, mss;
3156 dma_addr_t des;
3157 int i;
3158
3159 tx_q = &priv->tx_queue[queue];
3160 first_tx = tx_q->cur_tx;
3161
3162 /* Compute header lengths */
3163 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) {
3164 proto_hdr_len = skb_transport_offset(skb) + sizeof(struct udphdr);
3165 hdr = sizeof(struct udphdr);
3166 } else {
3167 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
3168 hdr = tcp_hdrlen(skb);
3169 }
3170
3171 /* Desc availability based on threshold should be enough safe */
3172 if (unlikely(stmmac_tx_avail(priv, queue) <
3173 (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
3174 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
3175 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
3176 queue));
3177 /* This is a hard error, log it. */
3178 netdev_err(priv->dev,
3179 "%s: Tx Ring full when queue awake\n",
3180 __func__);
3181 }
3182 return NETDEV_TX_BUSY;
3183 }
3184
3185 pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
3186
3187 mss = skb_shinfo(skb)->gso_size;
3188
3189 /* set new MSS value if needed */
3190 if (mss != tx_q->mss) {
3191 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3192 mss_desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3193 else
3194 mss_desc = &tx_q->dma_tx[tx_q->cur_tx];
3195
3196 stmmac_set_mss(priv, mss_desc, mss);
3197 tx_q->mss = mss;
3198 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx,
3199 priv->dma_tx_size);
3200 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
3201 }
3202
3203 if (netif_msg_tx_queued(priv)) {
3204 pr_info("%s: hdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
3205 __func__, hdr, proto_hdr_len, pay_len, mss);
3206 pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
3207 skb->data_len);
3208 }
3209
3210 /* Check if VLAN can be inserted by HW */
3211 has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
3212
3213 first_entry = tx_q->cur_tx;
3214 WARN_ON(tx_q->tx_skbuff[first_entry]);
3215
3216 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3217 desc = &tx_q->dma_entx[first_entry].basic;
3218 else
3219 desc = &tx_q->dma_tx[first_entry];
3220 first = desc;
3221
3222 if (has_vlan)
3223 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
3224
3225 /* first descriptor: fill Headers on Buf1 */
3226 des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
3227 DMA_TO_DEVICE);
3228 if (dma_mapping_error(priv->device, des))
3229 goto dma_map_err;
3230
3231 tx_q->tx_skbuff_dma[first_entry].buf = des;
3232 tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
3233
3234 if (priv->dma_cap.addr64 <= 32) {
3235 first->des0 = cpu_to_le32(des);
3236
3237 /* Fill start of payload in buff2 of first descriptor */
3238 if (pay_len)
3239 first->des1 = cpu_to_le32(des + proto_hdr_len);
3240
3241 /* If needed take extra descriptors to fill the remaining payload */
3242 tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
3243 } else {
3244 stmmac_set_desc_addr(priv, first, des);
3245 tmp_pay_len = pay_len;
3246 des += proto_hdr_len;
3247 pay_len = 0;
3248 }
3249
3250 stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue);
3251
3252 /* Prepare fragments */
3253 for (i = 0; i < nfrags; i++) {
3254 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3255
3256 des = skb_frag_dma_map(priv->device, frag, 0,
3257 skb_frag_size(frag),
3258 DMA_TO_DEVICE);
3259 if (dma_mapping_error(priv->device, des))
3260 goto dma_map_err;
3261
3262 stmmac_tso_allocator(priv, des, skb_frag_size(frag),
3263 (i == nfrags - 1), queue);
3264
3265 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des;
3266 tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag);
3267 tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true;
3268 }
3269
3270 tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true;
3271
3272 /* Only the last descriptor gets to point to the skb. */
3273 tx_q->tx_skbuff[tx_q->cur_tx] = skb;
3274
3275 /* Manage tx mitigation */
3276 tx_packets = (tx_q->cur_tx + 1) - first_tx;
3277 tx_q->tx_count_frames += tx_packets;
3278
3279 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
3280 set_ic = true;
3281 else if (!priv->tx_coal_frames)
3282 set_ic = false;
3283 else if (tx_packets > priv->tx_coal_frames)
3284 set_ic = true;
3285 else if ((tx_q->tx_count_frames % priv->tx_coal_frames) < tx_packets)
3286 set_ic = true;
3287 else
3288 set_ic = false;
3289
3290 if (set_ic) {
3291 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3292 desc = &tx_q->dma_entx[tx_q->cur_tx].basic;
3293 else
3294 desc = &tx_q->dma_tx[tx_q->cur_tx];
3295
3296 tx_q->tx_count_frames = 0;
3297 stmmac_set_tx_ic(priv, desc);
3298 priv->xstats.tx_set_ic_bit++;
3299 }
3300
3301 /* We've used all descriptors we need for this skb, however,
3302 * advance cur_tx so that it references a fresh descriptor.
3303 * ndo_start_xmit will fill this descriptor the next time it's
3304 * called and stmmac_tx_clean may clean up to this descriptor.
3305 */
3306 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size);
3307
3308 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
3309 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
3310 __func__);
3311 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
3312 }
3313
3314 dev->stats.tx_bytes += skb->len;
3315 priv->xstats.tx_tso_frames++;
3316 priv->xstats.tx_tso_nfrags += nfrags;
3317
3318 if (priv->sarc_type)
3319 stmmac_set_desc_sarc(priv, first, priv->sarc_type);
3320
3321 skb_tx_timestamp(skb);
3322
3323 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
3324 priv->hwts_tx_en)) {
3325 /* declare that device is doing timestamping */
3326 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3327 stmmac_enable_tx_timestamp(priv, first);
3328 }
3329
3330 /* Complete the first descriptor before granting the DMA */
3331 stmmac_prepare_tso_tx_desc(priv, first, 1,
3332 proto_hdr_len,
3333 pay_len,
3334 1, tx_q->tx_skbuff_dma[first_entry].last_segment,
3335 hdr / 4, (skb->len - proto_hdr_len));
3336
3337 /* If context desc is used to change MSS */
3338 if (mss_desc) {
3339 /* Make sure that first descriptor has been completely
3340 * written, including its own bit. This is because MSS is
3341 * actually before first descriptor, so we need to make
3342 * sure that MSS's own bit is the last thing written.
3343 */
3344 dma_wmb();
3345 stmmac_set_tx_owner(priv, mss_desc);
3346 }
3347
3348 /* The own bit must be the latest setting done when prepare the
3349 * descriptor and then barrier is needed to make sure that
3350 * all is coherent before granting the DMA engine.
3351 */
3352 wmb();
3353
3354 if (netif_msg_pktdata(priv)) {
3355 pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
3356 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
3357 tx_q->cur_tx, first, nfrags);
3358 pr_info(">>> frame to be transmitted: ");
3359 print_pkt(skb->data, skb_headlen(skb));
3360 }
3361
3362 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
3363
3364 if (tx_q->tbs & STMMAC_TBS_AVAIL)
3365 desc_size = sizeof(struct dma_edesc);
3366 else
3367 desc_size = sizeof(struct dma_desc);
3368
3369 tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * desc_size);
3370 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
3371 stmmac_tx_timer_arm(priv, queue);
3372
3373 return NETDEV_TX_OK;
3374
3375 dma_map_err:
3376 dev_err(priv->device, "Tx dma map failed\n");
3377 dev_kfree_skb(skb);
3378 priv->dev->stats.tx_dropped++;
3379 return NETDEV_TX_OK;
3380 }
3381
3382 /**
3383 * stmmac_xmit - Tx entry point of the driver
3384 * @skb : the socket buffer
3385 * @dev : device pointer
3386 * Description : this is the tx entry point of the driver.
3387 * It programs the chain or the ring and supports oversized frames
3388 * and SG feature.
3389 */
stmmac_xmit(struct sk_buff * skb,struct net_device * dev)3390 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
3391 {
3392 unsigned int first_entry, tx_packets, enh_desc;
3393 struct stmmac_priv *priv = netdev_priv(dev);
3394 unsigned int nopaged_len = skb_headlen(skb);
3395 int i, csum_insertion = 0, is_jumbo = 0;
3396 u32 queue = skb_get_queue_mapping(skb);
3397 int nfrags = skb_shinfo(skb)->nr_frags;
3398 int gso = skb_shinfo(skb)->gso_type;
3399 struct dma_edesc *tbs_desc = NULL;
3400 int entry, desc_size, first_tx;
3401 struct dma_desc *desc, *first;
3402 struct stmmac_tx_queue *tx_q;
3403 bool has_vlan, set_ic;
3404 dma_addr_t des;
3405
3406 tx_q = &priv->tx_queue[queue];
3407 first_tx = tx_q->cur_tx;
3408
3409 if (priv->tx_path_in_lpi_mode)
3410 stmmac_disable_eee_mode(priv);
3411
3412 /* Manage oversized TCP frames for GMAC4 device */
3413 if (skb_is_gso(skb) && priv->tso) {
3414 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
3415 return stmmac_tso_xmit(skb, dev);
3416 if (priv->plat->has_gmac4 && (gso & SKB_GSO_UDP_L4))
3417 return stmmac_tso_xmit(skb, dev);
3418 }
3419
3420 if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) {
3421 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
3422 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
3423 queue));
3424 /* This is a hard error, log it. */
3425 netdev_err(priv->dev,
3426 "%s: Tx Ring full when queue awake\n",
3427 __func__);
3428 }
3429 return NETDEV_TX_BUSY;
3430 }
3431
3432 /* Check if VLAN can be inserted by HW */
3433 has_vlan = stmmac_vlan_insert(priv, skb, tx_q);
3434
3435 entry = tx_q->cur_tx;
3436 first_entry = entry;
3437 WARN_ON(tx_q->tx_skbuff[first_entry]);
3438
3439 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
3440
3441 if (likely(priv->extend_desc))
3442 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
3443 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
3444 desc = &tx_q->dma_entx[entry].basic;
3445 else
3446 desc = tx_q->dma_tx + entry;
3447
3448 first = desc;
3449
3450 if (has_vlan)
3451 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT);
3452
3453 enh_desc = priv->plat->enh_desc;
3454 /* To program the descriptors according to the size of the frame */
3455 if (enh_desc)
3456 is_jumbo = stmmac_is_jumbo_frm(priv, skb->len, enh_desc);
3457
3458 if (unlikely(is_jumbo)) {
3459 entry = stmmac_jumbo_frm(priv, tx_q, skb, csum_insertion);
3460 if (unlikely(entry < 0) && (entry != -EINVAL))
3461 goto dma_map_err;
3462 }
3463
3464 for (i = 0; i < nfrags; i++) {
3465 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3466 int len = skb_frag_size(frag);
3467 bool last_segment = (i == (nfrags - 1));
3468
3469 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
3470 WARN_ON(tx_q->tx_skbuff[entry]);
3471
3472 if (likely(priv->extend_desc))
3473 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
3474 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
3475 desc = &tx_q->dma_entx[entry].basic;
3476 else
3477 desc = tx_q->dma_tx + entry;
3478
3479 des = skb_frag_dma_map(priv->device, frag, 0, len,
3480 DMA_TO_DEVICE);
3481 if (dma_mapping_error(priv->device, des))
3482 goto dma_map_err; /* should reuse desc w/o issues */
3483
3484 tx_q->tx_skbuff_dma[entry].buf = des;
3485
3486 stmmac_set_desc_addr(priv, desc, des);
3487
3488 tx_q->tx_skbuff_dma[entry].map_as_page = true;
3489 tx_q->tx_skbuff_dma[entry].len = len;
3490 tx_q->tx_skbuff_dma[entry].last_segment = last_segment;
3491
3492 /* Prepare the descriptor and set the own bit too */
3493 stmmac_prepare_tx_desc(priv, desc, 0, len, csum_insertion,
3494 priv->mode, 1, last_segment, skb->len);
3495 }
3496
3497 /* Only the last descriptor gets to point to the skb. */
3498 tx_q->tx_skbuff[entry] = skb;
3499
3500 /* According to the coalesce parameter the IC bit for the latest
3501 * segment is reset and the timer re-started to clean the tx status.
3502 * This approach takes care about the fragments: desc is the first
3503 * element in case of no SG.
3504 */
3505 tx_packets = (entry + 1) - first_tx;
3506 tx_q->tx_count_frames += tx_packets;
3507
3508 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en)
3509 set_ic = true;
3510 else if (!priv->tx_coal_frames)
3511 set_ic = false;
3512 else if (tx_packets > priv->tx_coal_frames)
3513 set_ic = true;
3514 else if ((tx_q->tx_count_frames % priv->tx_coal_frames) < tx_packets)
3515 set_ic = true;
3516 else
3517 set_ic = false;
3518
3519 if (set_ic) {
3520 if (likely(priv->extend_desc))
3521 desc = &tx_q->dma_etx[entry].basic;
3522 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
3523 desc = &tx_q->dma_entx[entry].basic;
3524 else
3525 desc = &tx_q->dma_tx[entry];
3526
3527 tx_q->tx_count_frames = 0;
3528 stmmac_set_tx_ic(priv, desc);
3529 priv->xstats.tx_set_ic_bit++;
3530 }
3531
3532 /* We've used all descriptors we need for this skb, however,
3533 * advance cur_tx so that it references a fresh descriptor.
3534 * ndo_start_xmit will fill this descriptor the next time it's
3535 * called and stmmac_tx_clean may clean up to this descriptor.
3536 */
3537 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size);
3538 tx_q->cur_tx = entry;
3539
3540 if (netif_msg_pktdata(priv)) {
3541 netdev_dbg(priv->dev,
3542 "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
3543 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
3544 entry, first, nfrags);
3545
3546 netdev_dbg(priv->dev, ">>> frame to be transmitted: ");
3547 print_pkt(skb->data, skb->len);
3548 }
3549
3550 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
3551 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
3552 __func__);
3553 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
3554 }
3555
3556 dev->stats.tx_bytes += skb->len;
3557
3558 if (priv->sarc_type)
3559 stmmac_set_desc_sarc(priv, first, priv->sarc_type);
3560
3561 skb_tx_timestamp(skb);
3562
3563 /* Ready to fill the first descriptor and set the OWN bit w/o any
3564 * problems because all the descriptors are actually ready to be
3565 * passed to the DMA engine.
3566 */
3567 if (likely(!is_jumbo)) {
3568 bool last_segment = (nfrags == 0);
3569
3570 des = dma_map_single(priv->device, skb->data,
3571 nopaged_len, DMA_TO_DEVICE);
3572 if (dma_mapping_error(priv->device, des))
3573 goto dma_map_err;
3574
3575 tx_q->tx_skbuff_dma[first_entry].buf = des;
3576
3577 stmmac_set_desc_addr(priv, first, des);
3578
3579 tx_q->tx_skbuff_dma[first_entry].len = nopaged_len;
3580 tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment;
3581
3582 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
3583 priv->hwts_tx_en)) {
3584 /* declare that device is doing timestamping */
3585 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3586 stmmac_enable_tx_timestamp(priv, first);
3587 }
3588
3589 /* Prepare the first descriptor setting the OWN bit too */
3590 stmmac_prepare_tx_desc(priv, first, 1, nopaged_len,
3591 csum_insertion, priv->mode, 0, last_segment,
3592 skb->len);
3593 }
3594
3595 if (tx_q->tbs & STMMAC_TBS_EN) {
3596 struct timespec64 ts = ns_to_timespec64(skb->tstamp);
3597
3598 tbs_desc = &tx_q->dma_entx[first_entry];
3599 stmmac_set_desc_tbs(priv, tbs_desc, ts.tv_sec, ts.tv_nsec);
3600 }
3601
3602 stmmac_set_tx_owner(priv, first);
3603
3604 /* The own bit must be the latest setting done when prepare the
3605 * descriptor and then barrier is needed to make sure that
3606 * all is coherent before granting the DMA engine.
3607 */
3608 wmb();
3609
3610 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
3611
3612 stmmac_enable_dma_transmission(priv, priv->ioaddr);
3613
3614 if (likely(priv->extend_desc))
3615 desc_size = sizeof(struct dma_extended_desc);
3616 else if (tx_q->tbs & STMMAC_TBS_AVAIL)
3617 desc_size = sizeof(struct dma_edesc);
3618 else
3619 desc_size = sizeof(struct dma_desc);
3620
3621 tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * desc_size);
3622 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
3623 stmmac_tx_timer_arm(priv, queue);
3624
3625 return NETDEV_TX_OK;
3626
3627 dma_map_err:
3628 netdev_err(priv->dev, "Tx DMA map failed\n");
3629 dev_kfree_skb(skb);
3630 priv->dev->stats.tx_dropped++;
3631 return NETDEV_TX_OK;
3632 }
3633
stmmac_rx_vlan(struct net_device * dev,struct sk_buff * skb)3634 static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
3635 {
3636 struct vlan_ethhdr *veth;
3637 __be16 vlan_proto;
3638 u16 vlanid;
3639
3640 veth = (struct vlan_ethhdr *)skb->data;
3641 vlan_proto = veth->h_vlan_proto;
3642
3643 if ((vlan_proto == htons(ETH_P_8021Q) &&
3644 dev->features & NETIF_F_HW_VLAN_CTAG_RX) ||
3645 (vlan_proto == htons(ETH_P_8021AD) &&
3646 dev->features & NETIF_F_HW_VLAN_STAG_RX)) {
3647 /* pop the vlan tag */
3648 vlanid = ntohs(veth->h_vlan_TCI);
3649 memmove(skb->data + VLAN_HLEN, veth, ETH_ALEN * 2);
3650 skb_pull(skb, VLAN_HLEN);
3651 __vlan_hwaccel_put_tag(skb, vlan_proto, vlanid);
3652 }
3653 }
3654
3655 /**
3656 * stmmac_rx_refill - refill used skb preallocated buffers
3657 * @priv: driver private structure
3658 * @queue: RX queue index
3659 * Description : this is to reallocate the skb for the reception process
3660 * that is based on zero-copy.
3661 */
stmmac_rx_refill(struct stmmac_priv * priv,u32 queue)3662 static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
3663 {
3664 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
3665 int len, dirty = stmmac_rx_dirty(priv, queue);
3666 unsigned int entry = rx_q->dirty_rx;
3667
3668 len = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE) * PAGE_SIZE;
3669
3670 while (dirty-- > 0) {
3671 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry];
3672 struct dma_desc *p;
3673 bool use_rx_wd;
3674
3675 if (priv->extend_desc)
3676 p = (struct dma_desc *)(rx_q->dma_erx + entry);
3677 else
3678 p = rx_q->dma_rx + entry;
3679
3680 if (!buf->page) {
3681 buf->page = page_pool_dev_alloc_pages(rx_q->page_pool);
3682 if (!buf->page)
3683 break;
3684 }
3685
3686 if (priv->sph && !buf->sec_page) {
3687 buf->sec_page = page_pool_dev_alloc_pages(rx_q->page_pool);
3688 if (!buf->sec_page)
3689 break;
3690
3691 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page);
3692
3693 dma_sync_single_for_device(priv->device, buf->sec_addr,
3694 len, DMA_FROM_DEVICE);
3695 }
3696
3697 buf->addr = page_pool_get_dma_addr(buf->page);
3698
3699 /* Sync whole allocation to device. This will invalidate old
3700 * data.
3701 */
3702 dma_sync_single_for_device(priv->device, buf->addr, len,
3703 DMA_FROM_DEVICE);
3704
3705 stmmac_set_desc_addr(priv, p, buf->addr);
3706 if (priv->sph)
3707 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true);
3708 else
3709 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false);
3710 stmmac_refill_desc3(priv, rx_q, p);
3711
3712 rx_q->rx_count_frames++;
3713 rx_q->rx_count_frames += priv->rx_coal_frames;
3714 if (rx_q->rx_count_frames > priv->rx_coal_frames)
3715 rx_q->rx_count_frames = 0;
3716
3717 use_rx_wd = !priv->rx_coal_frames;
3718 use_rx_wd |= rx_q->rx_count_frames > 0;
3719 if (!priv->use_riwt)
3720 use_rx_wd = false;
3721
3722 dma_wmb();
3723 stmmac_set_rx_owner(priv, p, use_rx_wd);
3724
3725 entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size);
3726 }
3727 rx_q->dirty_rx = entry;
3728 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
3729 (rx_q->dirty_rx * sizeof(struct dma_desc));
3730 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
3731 }
3732
stmmac_rx_buf1_len(struct stmmac_priv * priv,struct dma_desc * p,int status,unsigned int len)3733 static unsigned int stmmac_rx_buf1_len(struct stmmac_priv *priv,
3734 struct dma_desc *p,
3735 int status, unsigned int len)
3736 {
3737 unsigned int plen = 0, hlen = 0;
3738 int coe = priv->hw->rx_csum;
3739
3740 /* Not first descriptor, buffer is always zero */
3741 if (priv->sph && len)
3742 return 0;
3743
3744 /* First descriptor, get split header length */
3745 stmmac_get_rx_header_len(priv, p, &hlen);
3746 if (priv->sph && hlen) {
3747 priv->xstats.rx_split_hdr_pkt_n++;
3748 return hlen;
3749 }
3750
3751 /* First descriptor, not last descriptor and not split header */
3752 if (status & rx_not_ls)
3753 return priv->dma_buf_sz;
3754
3755 plen = stmmac_get_rx_frame_len(priv, p, coe);
3756
3757 /* First descriptor and last descriptor and not split header */
3758 return min_t(unsigned int, priv->dma_buf_sz, plen);
3759 }
3760
stmmac_rx_buf2_len(struct stmmac_priv * priv,struct dma_desc * p,int status,unsigned int len)3761 static unsigned int stmmac_rx_buf2_len(struct stmmac_priv *priv,
3762 struct dma_desc *p,
3763 int status, unsigned int len)
3764 {
3765 int coe = priv->hw->rx_csum;
3766 unsigned int plen = 0;
3767
3768 /* Not split header, buffer is not available */
3769 if (!priv->sph)
3770 return 0;
3771
3772 /* Not last descriptor */
3773 if (status & rx_not_ls)
3774 return priv->dma_buf_sz;
3775
3776 plen = stmmac_get_rx_frame_len(priv, p, coe);
3777
3778 /* Last descriptor */
3779 return plen - len;
3780 }
3781
3782 /**
3783 * stmmac_rx - manage the receive process
3784 * @priv: driver private structure
3785 * @limit: napi bugget
3786 * @queue: RX queue index.
3787 * Description : this the function called by the napi poll method.
3788 * It gets all the frames inside the ring.
3789 */
stmmac_rx(struct stmmac_priv * priv,int limit,u32 queue)3790 static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
3791 {
3792 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
3793 struct stmmac_channel *ch = &priv->channel[queue];
3794 unsigned int count = 0, error = 0, len = 0;
3795 int status = 0, coe = priv->hw->rx_csum;
3796 unsigned int next_entry = rx_q->cur_rx;
3797 unsigned int desc_size;
3798 struct sk_buff *skb = NULL;
3799
3800 if (netif_msg_rx_status(priv)) {
3801 void *rx_head;
3802
3803 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
3804 if (priv->extend_desc) {
3805 rx_head = (void *)rx_q->dma_erx;
3806 desc_size = sizeof(struct dma_extended_desc);
3807 } else {
3808 rx_head = (void *)rx_q->dma_rx;
3809 desc_size = sizeof(struct dma_desc);
3810 }
3811
3812 stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true,
3813 rx_q->dma_rx_phy, desc_size);
3814 }
3815 while (count < limit) {
3816 unsigned int buf1_len = 0, buf2_len = 0;
3817 enum pkt_hash_types hash_type;
3818 struct stmmac_rx_buffer *buf;
3819 struct dma_desc *np, *p;
3820 int entry;
3821 u32 hash;
3822
3823 if (!count && rx_q->state_saved) {
3824 skb = rx_q->state.skb;
3825 error = rx_q->state.error;
3826 len = rx_q->state.len;
3827 } else {
3828 rx_q->state_saved = false;
3829 skb = NULL;
3830 error = 0;
3831 len = 0;
3832 }
3833
3834 if (count >= limit)
3835 break;
3836
3837 read_again:
3838 buf1_len = 0;
3839 buf2_len = 0;
3840 entry = next_entry;
3841 buf = &rx_q->buf_pool[entry];
3842
3843 if (priv->extend_desc)
3844 p = (struct dma_desc *)(rx_q->dma_erx + entry);
3845 else
3846 p = rx_q->dma_rx + entry;
3847
3848 /* read the status of the incoming frame */
3849 status = stmmac_rx_status(priv, &priv->dev->stats,
3850 &priv->xstats, p);
3851 /* check if managed by the DMA otherwise go ahead */
3852 if (unlikely(status & dma_own))
3853 break;
3854
3855 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx,
3856 priv->dma_rx_size);
3857 next_entry = rx_q->cur_rx;
3858
3859 if (priv->extend_desc)
3860 np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
3861 else
3862 np = rx_q->dma_rx + next_entry;
3863
3864 prefetch(np);
3865
3866 if (priv->extend_desc)
3867 stmmac_rx_extended_status(priv, &priv->dev->stats,
3868 &priv->xstats, rx_q->dma_erx + entry);
3869 if (unlikely(status == discard_frame)) {
3870 page_pool_recycle_direct(rx_q->page_pool, buf->page);
3871 buf->page = NULL;
3872 error = 1;
3873 if (!priv->hwts_rx_en)
3874 priv->dev->stats.rx_errors++;
3875 }
3876
3877 if (unlikely(error && (status & rx_not_ls)))
3878 goto read_again;
3879 if (unlikely(error)) {
3880 dev_kfree_skb(skb);
3881 skb = NULL;
3882 count++;
3883 continue;
3884 }
3885
3886 /* Buffer is good. Go on. */
3887
3888 prefetch(page_address(buf->page));
3889 if (buf->sec_page)
3890 prefetch(page_address(buf->sec_page));
3891
3892 buf1_len = stmmac_rx_buf1_len(priv, p, status, len);
3893 len += buf1_len;
3894 buf2_len = stmmac_rx_buf2_len(priv, p, status, len);
3895 len += buf2_len;
3896
3897 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
3898 * Type frames (LLC/LLC-SNAP)
3899 *
3900 * llc_snap is never checked in GMAC >= 4, so this ACS
3901 * feature is always disabled and packets need to be
3902 * stripped manually.
3903 */
3904 if (likely(!(status & rx_not_ls)) &&
3905 (likely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
3906 unlikely(status != llc_snap))) {
3907 if (buf2_len)
3908 buf2_len -= ETH_FCS_LEN;
3909 else
3910 buf1_len -= ETH_FCS_LEN;
3911
3912 len -= ETH_FCS_LEN;
3913 }
3914
3915 if (!skb) {
3916 skb = napi_alloc_skb(&ch->rx_napi, buf1_len);
3917 if (!skb) {
3918 priv->dev->stats.rx_dropped++;
3919 count++;
3920 goto drain_data;
3921 }
3922
3923 dma_sync_single_for_cpu(priv->device, buf->addr,
3924 buf1_len, DMA_FROM_DEVICE);
3925 skb_copy_to_linear_data(skb, page_address(buf->page),
3926 buf1_len);
3927 skb_put(skb, buf1_len);
3928
3929 /* Data payload copied into SKB, page ready for recycle */
3930 page_pool_recycle_direct(rx_q->page_pool, buf->page);
3931 buf->page = NULL;
3932 } else if (buf1_len) {
3933 dma_sync_single_for_cpu(priv->device, buf->addr,
3934 buf1_len, DMA_FROM_DEVICE);
3935 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
3936 buf->page, 0, buf1_len,
3937 priv->dma_buf_sz);
3938
3939 /* Data payload appended into SKB */
3940 page_pool_release_page(rx_q->page_pool, buf->page);
3941 buf->page = NULL;
3942 }
3943
3944 if (buf2_len) {
3945 dma_sync_single_for_cpu(priv->device, buf->sec_addr,
3946 buf2_len, DMA_FROM_DEVICE);
3947 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
3948 buf->sec_page, 0, buf2_len,
3949 priv->dma_buf_sz);
3950
3951 /* Data payload appended into SKB */
3952 page_pool_release_page(rx_q->page_pool, buf->sec_page);
3953 buf->sec_page = NULL;
3954 }
3955
3956 drain_data:
3957 if (likely(status & rx_not_ls))
3958 goto read_again;
3959 if (!skb)
3960 continue;
3961
3962 /* Got entire packet into SKB. Finish it. */
3963
3964 stmmac_get_rx_hwtstamp(priv, p, np, skb);
3965 stmmac_rx_vlan(priv->dev, skb);
3966 skb->protocol = eth_type_trans(skb, priv->dev);
3967
3968 if (unlikely(!coe))
3969 skb_checksum_none_assert(skb);
3970 else
3971 skb->ip_summed = CHECKSUM_UNNECESSARY;
3972
3973 if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type))
3974 skb_set_hash(skb, hash, hash_type);
3975
3976 skb_record_rx_queue(skb, queue);
3977 napi_gro_receive(&ch->rx_napi, skb);
3978 skb = NULL;
3979
3980 priv->dev->stats.rx_packets++;
3981 priv->dev->stats.rx_bytes += len;
3982 count++;
3983 }
3984
3985 if (status & rx_not_ls || skb) {
3986 rx_q->state_saved = true;
3987 rx_q->state.skb = skb;
3988 rx_q->state.error = error;
3989 rx_q->state.len = len;
3990 }
3991
3992 stmmac_rx_refill(priv, queue);
3993
3994 priv->xstats.rx_pkt_n += count;
3995
3996 return count;
3997 }
3998
stmmac_napi_poll_rx(struct napi_struct * napi,int budget)3999 static int stmmac_napi_poll_rx(struct napi_struct *napi, int budget)
4000 {
4001 struct stmmac_channel *ch =
4002 container_of(napi, struct stmmac_channel, rx_napi);
4003 struct stmmac_priv *priv = ch->priv_data;
4004 u32 chan = ch->index;
4005 int work_done;
4006
4007 priv->xstats.napi_poll++;
4008
4009 work_done = stmmac_rx(priv, budget, chan);
4010 if (work_done < budget && napi_complete_done(napi, work_done)) {
4011 unsigned long flags;
4012
4013 spin_lock_irqsave(&ch->lock, flags);
4014 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 0);
4015 spin_unlock_irqrestore(&ch->lock, flags);
4016 }
4017
4018 return work_done;
4019 }
4020
stmmac_napi_poll_tx(struct napi_struct * napi,int budget)4021 static int stmmac_napi_poll_tx(struct napi_struct *napi, int budget)
4022 {
4023 struct stmmac_channel *ch =
4024 container_of(napi, struct stmmac_channel, tx_napi);
4025 struct stmmac_priv *priv = ch->priv_data;
4026 u32 chan = ch->index;
4027 int work_done;
4028
4029 priv->xstats.napi_poll++;
4030
4031 work_done = stmmac_tx_clean(priv, priv->dma_tx_size, chan);
4032 work_done = min(work_done, budget);
4033
4034 if (work_done < budget && napi_complete_done(napi, work_done)) {
4035 unsigned long flags;
4036
4037 spin_lock_irqsave(&ch->lock, flags);
4038 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 0, 1);
4039 spin_unlock_irqrestore(&ch->lock, flags);
4040 }
4041
4042 return work_done;
4043 }
4044
4045 /**
4046 * stmmac_tx_timeout
4047 * @dev : Pointer to net device structure
4048 * @txqueue: the index of the hanging transmit queue
4049 * Description: this function is called when a packet transmission fails to
4050 * complete within a reasonable time. The driver will mark the error in the
4051 * netdev structure and arrange for the device to be reset to a sane state
4052 * in order to transmit a new packet.
4053 */
stmmac_tx_timeout(struct net_device * dev,unsigned int txqueue)4054 static void stmmac_tx_timeout(struct net_device *dev, unsigned int txqueue)
4055 {
4056 struct stmmac_priv *priv = netdev_priv(dev);
4057
4058 stmmac_global_err(priv);
4059 }
4060
4061 /**
4062 * stmmac_set_rx_mode - entry point for multicast addressing
4063 * @dev : pointer to the device structure
4064 * Description:
4065 * This function is a driver entry point which gets called by the kernel
4066 * whenever multicast addresses must be enabled/disabled.
4067 * Return value:
4068 * void.
4069 */
stmmac_set_rx_mode(struct net_device * dev)4070 static void stmmac_set_rx_mode(struct net_device *dev)
4071 {
4072 struct stmmac_priv *priv = netdev_priv(dev);
4073
4074 stmmac_set_filter(priv, priv->hw, dev);
4075 }
4076
4077 /**
4078 * stmmac_change_mtu - entry point to change MTU size for the device.
4079 * @dev : device pointer.
4080 * @new_mtu : the new MTU size for the device.
4081 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
4082 * to drive packet transmission. Ethernet has an MTU of 1500 octets
4083 * (ETH_DATA_LEN). This value can be changed with ifconfig.
4084 * Return value:
4085 * 0 on success and an appropriate (-)ve integer as defined in errno.h
4086 * file on failure.
4087 */
stmmac_change_mtu(struct net_device * dev,int new_mtu)4088 static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
4089 {
4090 struct stmmac_priv *priv = netdev_priv(dev);
4091 int txfifosz = priv->plat->tx_fifo_size;
4092 const int mtu = new_mtu;
4093
4094 if (txfifosz == 0)
4095 txfifosz = priv->dma_cap.tx_fifo_size;
4096
4097 txfifosz /= priv->plat->tx_queues_to_use;
4098
4099 if (netif_running(dev)) {
4100 netdev_err(priv->dev, "must be stopped to change its MTU\n");
4101 return -EBUSY;
4102 }
4103
4104 new_mtu = STMMAC_ALIGN(new_mtu);
4105
4106 /* If condition true, FIFO is too small or MTU too large */
4107 if ((txfifosz < new_mtu) || (new_mtu > BUF_SIZE_16KiB))
4108 return -EINVAL;
4109
4110 dev->mtu = mtu;
4111
4112 netdev_update_features(dev);
4113
4114 return 0;
4115 }
4116
stmmac_fix_features(struct net_device * dev,netdev_features_t features)4117 static netdev_features_t stmmac_fix_features(struct net_device *dev,
4118 netdev_features_t features)
4119 {
4120 struct stmmac_priv *priv = netdev_priv(dev);
4121
4122 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
4123 features &= ~NETIF_F_RXCSUM;
4124
4125 if (!priv->plat->tx_coe)
4126 features &= ~NETIF_F_CSUM_MASK;
4127
4128 /* Some GMAC devices have a bugged Jumbo frame support that
4129 * needs to have the Tx COE disabled for oversized frames
4130 * (due to limited buffer sizes). In this case we disable
4131 * the TX csum insertion in the TDES and not use SF.
4132 */
4133 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
4134 features &= ~NETIF_F_CSUM_MASK;
4135
4136 /* Disable tso if asked by ethtool */
4137 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
4138 if (features & NETIF_F_TSO)
4139 priv->tso = true;
4140 else
4141 priv->tso = false;
4142 }
4143
4144 return features;
4145 }
4146
stmmac_set_features(struct net_device * netdev,netdev_features_t features)4147 static int stmmac_set_features(struct net_device *netdev,
4148 netdev_features_t features)
4149 {
4150 struct stmmac_priv *priv = netdev_priv(netdev);
4151 bool sph_en;
4152 u32 chan;
4153
4154 /* Keep the COE Type in case of csum is supporting */
4155 if (features & NETIF_F_RXCSUM)
4156 priv->hw->rx_csum = priv->plat->rx_coe;
4157 else
4158 priv->hw->rx_csum = 0;
4159 /* No check needed because rx_coe has been set before and it will be
4160 * fixed in case of issue.
4161 */
4162 stmmac_rx_ipc(priv, priv->hw);
4163
4164 sph_en = (priv->hw->rx_csum > 0) && priv->sph;
4165 for (chan = 0; chan < priv->plat->rx_queues_to_use; chan++)
4166 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan);
4167
4168 return 0;
4169 }
4170
4171 /**
4172 * stmmac_interrupt - main ISR
4173 * @irq: interrupt number.
4174 * @dev_id: to pass the net device pointer (must be valid).
4175 * Description: this is the main driver interrupt service routine.
4176 * It can call:
4177 * o DMA service routine (to manage incoming frame reception and transmission
4178 * status)
4179 * o Core interrupts to manage: remote wake-up, management counter, LPI
4180 * interrupts.
4181 */
stmmac_interrupt(int irq,void * dev_id)4182 static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
4183 {
4184 struct net_device *dev = (struct net_device *)dev_id;
4185 struct stmmac_priv *priv = netdev_priv(dev);
4186 u32 rx_cnt = priv->plat->rx_queues_to_use;
4187 u32 tx_cnt = priv->plat->tx_queues_to_use;
4188 u32 queues_count;
4189 u32 queue;
4190 bool xmac;
4191
4192 xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
4193 queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt;
4194
4195 if (priv->irq_wake)
4196 pm_wakeup_event(priv->device, 0);
4197
4198 /* Check if adapter is up */
4199 if (test_bit(STMMAC_DOWN, &priv->state))
4200 return IRQ_HANDLED;
4201 /* Check if a fatal error happened */
4202 if (stmmac_safety_feat_interrupt(priv))
4203 return IRQ_HANDLED;
4204
4205 /* To handle GMAC own interrupts */
4206 if ((priv->plat->has_gmac) || xmac) {
4207 int status = stmmac_host_irq_status(priv, priv->hw, &priv->xstats);
4208
4209 if (unlikely(status)) {
4210 /* For LPI we need to save the tx status */
4211 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
4212 priv->tx_path_in_lpi_mode = true;
4213 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
4214 priv->tx_path_in_lpi_mode = false;
4215 }
4216
4217 for (queue = 0; queue < queues_count; queue++) {
4218 status = stmmac_host_mtl_irq_status(priv, priv->hw,
4219 queue);
4220 }
4221
4222 /* PCS link status */
4223 if (priv->hw->pcs) {
4224 if (priv->xstats.pcs_link)
4225 netif_carrier_on(dev);
4226 else
4227 netif_carrier_off(dev);
4228 }
4229 }
4230
4231 /* To handle DMA interrupts */
4232 stmmac_dma_interrupt(priv);
4233
4234 return IRQ_HANDLED;
4235 }
4236
4237 #ifdef CONFIG_NET_POLL_CONTROLLER
4238 /* Polling receive - used by NETCONSOLE and other diagnostic tools
4239 * to allow network I/O with interrupts disabled.
4240 */
stmmac_poll_controller(struct net_device * dev)4241 static void stmmac_poll_controller(struct net_device *dev)
4242 {
4243 disable_irq(dev->irq);
4244 stmmac_interrupt(dev->irq, dev);
4245 enable_irq(dev->irq);
4246 }
4247 #endif
4248
4249 /**
4250 * stmmac_ioctl - Entry point for the Ioctl
4251 * @dev: Device pointer.
4252 * @rq: An IOCTL specefic structure, that can contain a pointer to
4253 * a proprietary structure used to pass information to the driver.
4254 * @cmd: IOCTL command
4255 * Description:
4256 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
4257 */
stmmac_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)4258 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
4259 {
4260 struct stmmac_priv *priv = netdev_priv (dev);
4261 int ret = -EOPNOTSUPP;
4262
4263 if (!netif_running(dev))
4264 return -EINVAL;
4265
4266 switch (cmd) {
4267 case SIOCGMIIPHY:
4268 case SIOCGMIIREG:
4269 case SIOCSMIIREG:
4270 ret = phylink_mii_ioctl(priv->phylink, rq, cmd);
4271 break;
4272 case SIOCSHWTSTAMP:
4273 ret = stmmac_hwtstamp_set(dev, rq);
4274 break;
4275 case SIOCGHWTSTAMP:
4276 ret = stmmac_hwtstamp_get(dev, rq);
4277 break;
4278 default:
4279 break;
4280 }
4281
4282 return ret;
4283 }
4284
stmmac_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)4285 static int stmmac_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
4286 void *cb_priv)
4287 {
4288 struct stmmac_priv *priv = cb_priv;
4289 int ret = -EOPNOTSUPP;
4290
4291 if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
4292 return ret;
4293
4294 stmmac_disable_all_queues(priv);
4295
4296 switch (type) {
4297 case TC_SETUP_CLSU32:
4298 ret = stmmac_tc_setup_cls_u32(priv, priv, type_data);
4299 break;
4300 case TC_SETUP_CLSFLOWER:
4301 ret = stmmac_tc_setup_cls(priv, priv, type_data);
4302 break;
4303 default:
4304 break;
4305 }
4306
4307 stmmac_enable_all_queues(priv);
4308 return ret;
4309 }
4310
4311 static LIST_HEAD(stmmac_block_cb_list);
4312
stmmac_setup_tc(struct net_device * ndev,enum tc_setup_type type,void * type_data)4313 static int stmmac_setup_tc(struct net_device *ndev, enum tc_setup_type type,
4314 void *type_data)
4315 {
4316 struct stmmac_priv *priv = netdev_priv(ndev);
4317
4318 switch (type) {
4319 case TC_SETUP_BLOCK:
4320 return flow_block_cb_setup_simple(type_data,
4321 &stmmac_block_cb_list,
4322 stmmac_setup_tc_block_cb,
4323 priv, priv, true);
4324 case TC_SETUP_QDISC_CBS:
4325 return stmmac_tc_setup_cbs(priv, priv, type_data);
4326 case TC_SETUP_QDISC_TAPRIO:
4327 return stmmac_tc_setup_taprio(priv, priv, type_data);
4328 case TC_SETUP_QDISC_ETF:
4329 return stmmac_tc_setup_etf(priv, priv, type_data);
4330 default:
4331 return -EOPNOTSUPP;
4332 }
4333 }
4334
stmmac_select_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)4335 static u16 stmmac_select_queue(struct net_device *dev, struct sk_buff *skb,
4336 struct net_device *sb_dev)
4337 {
4338 int gso = skb_shinfo(skb)->gso_type;
4339
4340 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6 | SKB_GSO_UDP_L4)) {
4341 /*
4342 * There is no way to determine the number of TSO/USO
4343 * capable Queues. Let's use always the Queue 0
4344 * because if TSO/USO is supported then at least this
4345 * one will be capable.
4346 */
4347 return 0;
4348 }
4349
4350 return netdev_pick_tx(dev, skb, NULL) % dev->real_num_tx_queues;
4351 }
4352
stmmac_set_mac_address(struct net_device * ndev,void * addr)4353 static int stmmac_set_mac_address(struct net_device *ndev, void *addr)
4354 {
4355 struct stmmac_priv *priv = netdev_priv(ndev);
4356 int ret = 0;
4357
4358 ret = pm_runtime_get_sync(priv->device);
4359 if (ret < 0) {
4360 pm_runtime_put_noidle(priv->device);
4361 return ret;
4362 }
4363
4364 ret = eth_mac_addr(ndev, addr);
4365 if (ret)
4366 goto set_mac_error;
4367
4368 stmmac_set_umac_addr(priv, priv->hw, ndev->dev_addr, 0);
4369
4370 set_mac_error:
4371 pm_runtime_put(priv->device);
4372
4373 return ret;
4374 }
4375
4376 #ifdef CONFIG_DEBUG_FS
4377 static struct dentry *stmmac_fs_dir;
4378
sysfs_display_ring(void * head,int size,int extend_desc,struct seq_file * seq,dma_addr_t dma_phy_addr)4379 static void sysfs_display_ring(void *head, int size, int extend_desc,
4380 struct seq_file *seq, dma_addr_t dma_phy_addr)
4381 {
4382 int i;
4383 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
4384 struct dma_desc *p = (struct dma_desc *)head;
4385 dma_addr_t dma_addr;
4386
4387 for (i = 0; i < size; i++) {
4388 if (extend_desc) {
4389 dma_addr = dma_phy_addr + i * sizeof(*ep);
4390 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
4391 i, &dma_addr,
4392 le32_to_cpu(ep->basic.des0),
4393 le32_to_cpu(ep->basic.des1),
4394 le32_to_cpu(ep->basic.des2),
4395 le32_to_cpu(ep->basic.des3));
4396 ep++;
4397 } else {
4398 dma_addr = dma_phy_addr + i * sizeof(*p);
4399 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n",
4400 i, &dma_addr,
4401 le32_to_cpu(p->des0), le32_to_cpu(p->des1),
4402 le32_to_cpu(p->des2), le32_to_cpu(p->des3));
4403 p++;
4404 }
4405 seq_printf(seq, "\n");
4406 }
4407 }
4408
stmmac_rings_status_show(struct seq_file * seq,void * v)4409 static int stmmac_rings_status_show(struct seq_file *seq, void *v)
4410 {
4411 struct net_device *dev = seq->private;
4412 struct stmmac_priv *priv = netdev_priv(dev);
4413 u32 rx_count = priv->plat->rx_queues_to_use;
4414 u32 tx_count = priv->plat->tx_queues_to_use;
4415 u32 queue;
4416
4417 if ((dev->flags & IFF_UP) == 0)
4418 return 0;
4419
4420 for (queue = 0; queue < rx_count; queue++) {
4421 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4422
4423 seq_printf(seq, "RX Queue %d:\n", queue);
4424
4425 if (priv->extend_desc) {
4426 seq_printf(seq, "Extended descriptor ring:\n");
4427 sysfs_display_ring((void *)rx_q->dma_erx,
4428 priv->dma_rx_size, 1, seq, rx_q->dma_rx_phy);
4429 } else {
4430 seq_printf(seq, "Descriptor ring:\n");
4431 sysfs_display_ring((void *)rx_q->dma_rx,
4432 priv->dma_rx_size, 0, seq, rx_q->dma_rx_phy);
4433 }
4434 }
4435
4436 for (queue = 0; queue < tx_count; queue++) {
4437 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
4438
4439 seq_printf(seq, "TX Queue %d:\n", queue);
4440
4441 if (priv->extend_desc) {
4442 seq_printf(seq, "Extended descriptor ring:\n");
4443 sysfs_display_ring((void *)tx_q->dma_etx,
4444 priv->dma_tx_size, 1, seq, tx_q->dma_tx_phy);
4445 } else if (!(tx_q->tbs & STMMAC_TBS_AVAIL)) {
4446 seq_printf(seq, "Descriptor ring:\n");
4447 sysfs_display_ring((void *)tx_q->dma_tx,
4448 priv->dma_tx_size, 0, seq, tx_q->dma_tx_phy);
4449 }
4450 }
4451
4452 return 0;
4453 }
4454 DEFINE_SHOW_ATTRIBUTE(stmmac_rings_status);
4455
stmmac_dma_cap_show(struct seq_file * seq,void * v)4456 static int stmmac_dma_cap_show(struct seq_file *seq, void *v)
4457 {
4458 struct net_device *dev = seq->private;
4459 struct stmmac_priv *priv = netdev_priv(dev);
4460
4461 if (!priv->hw_cap_support) {
4462 seq_printf(seq, "DMA HW features not supported\n");
4463 return 0;
4464 }
4465
4466 seq_printf(seq, "==============================\n");
4467 seq_printf(seq, "\tDMA HW features\n");
4468 seq_printf(seq, "==============================\n");
4469
4470 seq_printf(seq, "\t10/100 Mbps: %s\n",
4471 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
4472 seq_printf(seq, "\t1000 Mbps: %s\n",
4473 (priv->dma_cap.mbps_1000) ? "Y" : "N");
4474 seq_printf(seq, "\tHalf duplex: %s\n",
4475 (priv->dma_cap.half_duplex) ? "Y" : "N");
4476 seq_printf(seq, "\tHash Filter: %s\n",
4477 (priv->dma_cap.hash_filter) ? "Y" : "N");
4478 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
4479 (priv->dma_cap.multi_addr) ? "Y" : "N");
4480 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n",
4481 (priv->dma_cap.pcs) ? "Y" : "N");
4482 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
4483 (priv->dma_cap.sma_mdio) ? "Y" : "N");
4484 seq_printf(seq, "\tPMT Remote wake up: %s\n",
4485 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
4486 seq_printf(seq, "\tPMT Magic Frame: %s\n",
4487 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
4488 seq_printf(seq, "\tRMON module: %s\n",
4489 (priv->dma_cap.rmon) ? "Y" : "N");
4490 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
4491 (priv->dma_cap.time_stamp) ? "Y" : "N");
4492 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n",
4493 (priv->dma_cap.atime_stamp) ? "Y" : "N");
4494 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n",
4495 (priv->dma_cap.eee) ? "Y" : "N");
4496 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
4497 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
4498 (priv->dma_cap.tx_coe) ? "Y" : "N");
4499 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
4500 seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
4501 (priv->dma_cap.rx_coe) ? "Y" : "N");
4502 } else {
4503 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
4504 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
4505 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
4506 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
4507 }
4508 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
4509 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
4510 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
4511 priv->dma_cap.number_rx_channel);
4512 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
4513 priv->dma_cap.number_tx_channel);
4514 seq_printf(seq, "\tNumber of Additional RX queues: %d\n",
4515 priv->dma_cap.number_rx_queues);
4516 seq_printf(seq, "\tNumber of Additional TX queues: %d\n",
4517 priv->dma_cap.number_tx_queues);
4518 seq_printf(seq, "\tEnhanced descriptors: %s\n",
4519 (priv->dma_cap.enh_desc) ? "Y" : "N");
4520 seq_printf(seq, "\tTX Fifo Size: %d\n", priv->dma_cap.tx_fifo_size);
4521 seq_printf(seq, "\tRX Fifo Size: %d\n", priv->dma_cap.rx_fifo_size);
4522 seq_printf(seq, "\tHash Table Size: %d\n", priv->dma_cap.hash_tb_sz);
4523 seq_printf(seq, "\tTSO: %s\n", priv->dma_cap.tsoen ? "Y" : "N");
4524 seq_printf(seq, "\tNumber of PPS Outputs: %d\n",
4525 priv->dma_cap.pps_out_num);
4526 seq_printf(seq, "\tSafety Features: %s\n",
4527 priv->dma_cap.asp ? "Y" : "N");
4528 seq_printf(seq, "\tFlexible RX Parser: %s\n",
4529 priv->dma_cap.frpsel ? "Y" : "N");
4530 seq_printf(seq, "\tEnhanced Addressing: %d\n",
4531 priv->dma_cap.addr64);
4532 seq_printf(seq, "\tReceive Side Scaling: %s\n",
4533 priv->dma_cap.rssen ? "Y" : "N");
4534 seq_printf(seq, "\tVLAN Hash Filtering: %s\n",
4535 priv->dma_cap.vlhash ? "Y" : "N");
4536 seq_printf(seq, "\tSplit Header: %s\n",
4537 priv->dma_cap.sphen ? "Y" : "N");
4538 seq_printf(seq, "\tVLAN TX Insertion: %s\n",
4539 priv->dma_cap.vlins ? "Y" : "N");
4540 seq_printf(seq, "\tDouble VLAN: %s\n",
4541 priv->dma_cap.dvlan ? "Y" : "N");
4542 seq_printf(seq, "\tNumber of L3/L4 Filters: %d\n",
4543 priv->dma_cap.l3l4fnum);
4544 seq_printf(seq, "\tARP Offloading: %s\n",
4545 priv->dma_cap.arpoffsel ? "Y" : "N");
4546 seq_printf(seq, "\tEnhancements to Scheduled Traffic (EST): %s\n",
4547 priv->dma_cap.estsel ? "Y" : "N");
4548 seq_printf(seq, "\tFrame Preemption (FPE): %s\n",
4549 priv->dma_cap.fpesel ? "Y" : "N");
4550 seq_printf(seq, "\tTime-Based Scheduling (TBS): %s\n",
4551 priv->dma_cap.tbssel ? "Y" : "N");
4552 return 0;
4553 }
4554 DEFINE_SHOW_ATTRIBUTE(stmmac_dma_cap);
4555
4556 /* Use network device events to rename debugfs file entries.
4557 */
stmmac_device_event(struct notifier_block * unused,unsigned long event,void * ptr)4558 static int stmmac_device_event(struct notifier_block *unused,
4559 unsigned long event, void *ptr)
4560 {
4561 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4562 struct stmmac_priv *priv = netdev_priv(dev);
4563
4564 if (dev->netdev_ops != &stmmac_netdev_ops)
4565 goto done;
4566
4567 switch (event) {
4568 case NETDEV_CHANGENAME:
4569 if (priv->dbgfs_dir)
4570 priv->dbgfs_dir = debugfs_rename(stmmac_fs_dir,
4571 priv->dbgfs_dir,
4572 stmmac_fs_dir,
4573 dev->name);
4574 break;
4575 }
4576 done:
4577 return NOTIFY_DONE;
4578 }
4579
4580 static struct notifier_block stmmac_notifier = {
4581 .notifier_call = stmmac_device_event,
4582 };
4583
stmmac_init_fs(struct net_device * dev)4584 static void stmmac_init_fs(struct net_device *dev)
4585 {
4586 struct stmmac_priv *priv = netdev_priv(dev);
4587
4588 rtnl_lock();
4589
4590 /* Create per netdev entries */
4591 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
4592
4593 /* Entry to report DMA RX/TX rings */
4594 debugfs_create_file("descriptors_status", 0444, priv->dbgfs_dir, dev,
4595 &stmmac_rings_status_fops);
4596
4597 /* Entry to report the DMA HW features */
4598 debugfs_create_file("dma_cap", 0444, priv->dbgfs_dir, dev,
4599 &stmmac_dma_cap_fops);
4600
4601 rtnl_unlock();
4602 }
4603
stmmac_exit_fs(struct net_device * dev)4604 static void stmmac_exit_fs(struct net_device *dev)
4605 {
4606 struct stmmac_priv *priv = netdev_priv(dev);
4607
4608 debugfs_remove_recursive(priv->dbgfs_dir);
4609 }
4610 #endif /* CONFIG_DEBUG_FS */
4611
stmmac_vid_crc32_le(__le16 vid_le)4612 static u32 stmmac_vid_crc32_le(__le16 vid_le)
4613 {
4614 unsigned char *data = (unsigned char *)&vid_le;
4615 unsigned char data_byte = 0;
4616 u32 crc = ~0x0;
4617 u32 temp = 0;
4618 int i, bits;
4619
4620 bits = get_bitmask_order(VLAN_VID_MASK);
4621 for (i = 0; i < bits; i++) {
4622 if ((i % 8) == 0)
4623 data_byte = data[i / 8];
4624
4625 temp = ((crc & 1) ^ data_byte) & 1;
4626 crc >>= 1;
4627 data_byte >>= 1;
4628
4629 if (temp)
4630 crc ^= 0xedb88320;
4631 }
4632
4633 return crc;
4634 }
4635
stmmac_vlan_update(struct stmmac_priv * priv,bool is_double)4636 static int stmmac_vlan_update(struct stmmac_priv *priv, bool is_double)
4637 {
4638 u32 crc, hash = 0;
4639 __le16 pmatch = 0;
4640 int count = 0;
4641 u16 vid = 0;
4642
4643 for_each_set_bit(vid, priv->active_vlans, VLAN_N_VID) {
4644 __le16 vid_le = cpu_to_le16(vid);
4645 crc = bitrev32(~stmmac_vid_crc32_le(vid_le)) >> 28;
4646 hash |= (1 << crc);
4647 count++;
4648 }
4649
4650 if (!priv->dma_cap.vlhash) {
4651 if (count > 2) /* VID = 0 always passes filter */
4652 return -EOPNOTSUPP;
4653
4654 pmatch = cpu_to_le16(vid);
4655 hash = 0;
4656 }
4657
4658 return stmmac_update_vlan_hash(priv, priv->hw, hash, pmatch, is_double);
4659 }
4660
stmmac_vlan_rx_add_vid(struct net_device * ndev,__be16 proto,u16 vid)4661 static int stmmac_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid)
4662 {
4663 struct stmmac_priv *priv = netdev_priv(ndev);
4664 bool is_double = false;
4665 int ret;
4666
4667 if (be16_to_cpu(proto) == ETH_P_8021AD)
4668 is_double = true;
4669
4670 set_bit(vid, priv->active_vlans);
4671 ret = stmmac_vlan_update(priv, is_double);
4672 if (ret) {
4673 clear_bit(vid, priv->active_vlans);
4674 return ret;
4675 }
4676
4677 if (priv->hw->num_vlan) {
4678 ret = stmmac_add_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
4679 if (ret)
4680 return ret;
4681 }
4682
4683 return 0;
4684 }
4685
stmmac_vlan_rx_kill_vid(struct net_device * ndev,__be16 proto,u16 vid)4686 static int stmmac_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid)
4687 {
4688 struct stmmac_priv *priv = netdev_priv(ndev);
4689 bool is_double = false;
4690 int ret;
4691
4692 ret = pm_runtime_get_sync(priv->device);
4693 if (ret < 0) {
4694 pm_runtime_put_noidle(priv->device);
4695 return ret;
4696 }
4697
4698 if (be16_to_cpu(proto) == ETH_P_8021AD)
4699 is_double = true;
4700
4701 clear_bit(vid, priv->active_vlans);
4702
4703 if (priv->hw->num_vlan) {
4704 ret = stmmac_del_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid);
4705 if (ret)
4706 goto del_vlan_error;
4707 }
4708
4709 ret = stmmac_vlan_update(priv, is_double);
4710
4711 del_vlan_error:
4712 pm_runtime_put(priv->device);
4713
4714 return ret;
4715 }
4716
4717 static const struct net_device_ops stmmac_netdev_ops = {
4718 .ndo_open = stmmac_open,
4719 .ndo_start_xmit = stmmac_xmit,
4720 .ndo_stop = stmmac_release,
4721 .ndo_change_mtu = stmmac_change_mtu,
4722 .ndo_fix_features = stmmac_fix_features,
4723 .ndo_set_features = stmmac_set_features,
4724 .ndo_set_rx_mode = stmmac_set_rx_mode,
4725 .ndo_tx_timeout = stmmac_tx_timeout,
4726 .ndo_do_ioctl = stmmac_ioctl,
4727 .ndo_setup_tc = stmmac_setup_tc,
4728 .ndo_select_queue = stmmac_select_queue,
4729 #ifdef CONFIG_NET_POLL_CONTROLLER
4730 .ndo_poll_controller = stmmac_poll_controller,
4731 #endif
4732 .ndo_set_mac_address = stmmac_set_mac_address,
4733 .ndo_vlan_rx_add_vid = stmmac_vlan_rx_add_vid,
4734 .ndo_vlan_rx_kill_vid = stmmac_vlan_rx_kill_vid,
4735 };
4736
stmmac_reset_subtask(struct stmmac_priv * priv)4737 static void stmmac_reset_subtask(struct stmmac_priv *priv)
4738 {
4739 if (!test_and_clear_bit(STMMAC_RESET_REQUESTED, &priv->state))
4740 return;
4741 if (test_bit(STMMAC_DOWN, &priv->state))
4742 return;
4743
4744 netdev_err(priv->dev, "Reset adapter.\n");
4745
4746 rtnl_lock();
4747 netif_trans_update(priv->dev);
4748 while (test_and_set_bit(STMMAC_RESETING, &priv->state))
4749 usleep_range(1000, 2000);
4750
4751 set_bit(STMMAC_DOWN, &priv->state);
4752 dev_close(priv->dev);
4753 dev_open(priv->dev, NULL);
4754 clear_bit(STMMAC_DOWN, &priv->state);
4755 clear_bit(STMMAC_RESETING, &priv->state);
4756 rtnl_unlock();
4757 }
4758
stmmac_service_task(struct work_struct * work)4759 static void stmmac_service_task(struct work_struct *work)
4760 {
4761 struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
4762 service_task);
4763
4764 stmmac_reset_subtask(priv);
4765 clear_bit(STMMAC_SERVICE_SCHED, &priv->state);
4766 }
4767
4768 /**
4769 * stmmac_hw_init - Init the MAC device
4770 * @priv: driver private structure
4771 * Description: this function is to configure the MAC device according to
4772 * some platform parameters or the HW capability register. It prepares the
4773 * driver to use either ring or chain modes and to setup either enhanced or
4774 * normal descriptors.
4775 */
stmmac_hw_init(struct stmmac_priv * priv)4776 static int stmmac_hw_init(struct stmmac_priv *priv)
4777 {
4778 int ret;
4779
4780 /* dwmac-sun8i only work in chain mode */
4781 if (priv->plat->has_sun8i)
4782 chain_mode = 1;
4783 priv->chain_mode = chain_mode;
4784
4785 /* Initialize HW Interface */
4786 ret = stmmac_hwif_init(priv);
4787 if (ret)
4788 return ret;
4789
4790 /* Get the HW capability (new GMAC newer than 3.50a) */
4791 priv->hw_cap_support = stmmac_get_hw_features(priv);
4792 if (priv->hw_cap_support) {
4793 dev_info(priv->device, "DMA HW capability register supported\n");
4794
4795 /* We can override some gmac/dma configuration fields: e.g.
4796 * enh_desc, tx_coe (e.g. that are passed through the
4797 * platform) with the values from the HW capability
4798 * register (if supported).
4799 */
4800 priv->plat->enh_desc = priv->dma_cap.enh_desc;
4801 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
4802 priv->hw->pmt = priv->plat->pmt;
4803 if (priv->dma_cap.hash_tb_sz) {
4804 priv->hw->multicast_filter_bins =
4805 (BIT(priv->dma_cap.hash_tb_sz) << 5);
4806 priv->hw->mcast_bits_log2 =
4807 ilog2(priv->hw->multicast_filter_bins);
4808 }
4809
4810 /* TXCOE doesn't work in thresh DMA mode */
4811 if (priv->plat->force_thresh_dma_mode)
4812 priv->plat->tx_coe = 0;
4813 else
4814 priv->plat->tx_coe = priv->dma_cap.tx_coe;
4815
4816 /* In case of GMAC4 rx_coe is from HW cap register. */
4817 priv->plat->rx_coe = priv->dma_cap.rx_coe;
4818
4819 if (priv->dma_cap.rx_coe_type2)
4820 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
4821 else if (priv->dma_cap.rx_coe_type1)
4822 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
4823
4824 } else {
4825 dev_info(priv->device, "No HW DMA feature register supported\n");
4826 }
4827
4828 if (priv->plat->rx_coe) {
4829 priv->hw->rx_csum = priv->plat->rx_coe;
4830 dev_info(priv->device, "RX Checksum Offload Engine supported\n");
4831 if (priv->synopsys_id < DWMAC_CORE_4_00)
4832 dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
4833 }
4834 if (priv->plat->tx_coe)
4835 dev_info(priv->device, "TX Checksum insertion supported\n");
4836
4837 if (priv->plat->pmt) {
4838 dev_info(priv->device, "Wake-Up On Lan supported\n");
4839 device_set_wakeup_capable(priv->device, 1);
4840 }
4841
4842 if (priv->dma_cap.tsoen)
4843 dev_info(priv->device, "TSO supported\n");
4844
4845 priv->hw->vlan_fail_q_en = priv->plat->vlan_fail_q_en;
4846 priv->hw->vlan_fail_q = priv->plat->vlan_fail_q;
4847
4848 /* Run HW quirks, if any */
4849 if (priv->hwif_quirks) {
4850 ret = priv->hwif_quirks(priv);
4851 if (ret)
4852 return ret;
4853 }
4854
4855 /* Rx Watchdog is available in the COREs newer than the 3.40.
4856 * In some case, for example on bugged HW this feature
4857 * has to be disable and this can be done by passing the
4858 * riwt_off field from the platform.
4859 */
4860 if (((priv->synopsys_id >= DWMAC_CORE_3_50) ||
4861 (priv->plat->has_xgmac)) && (!priv->plat->riwt_off)) {
4862 priv->use_riwt = 1;
4863 dev_info(priv->device,
4864 "Enable RX Mitigation via HW Watchdog Timer\n");
4865 }
4866
4867 return 0;
4868 }
4869
stmmac_napi_add(struct net_device * dev)4870 static void stmmac_napi_add(struct net_device *dev)
4871 {
4872 struct stmmac_priv *priv = netdev_priv(dev);
4873 u32 queue, maxq;
4874
4875 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
4876
4877 for (queue = 0; queue < maxq; queue++) {
4878 struct stmmac_channel *ch = &priv->channel[queue];
4879
4880 ch->priv_data = priv;
4881 ch->index = queue;
4882 spin_lock_init(&ch->lock);
4883
4884 if (queue < priv->plat->rx_queues_to_use) {
4885 netif_napi_add(dev, &ch->rx_napi, stmmac_napi_poll_rx,
4886 NAPI_POLL_WEIGHT);
4887 }
4888 if (queue < priv->plat->tx_queues_to_use) {
4889 netif_tx_napi_add(dev, &ch->tx_napi,
4890 stmmac_napi_poll_tx,
4891 NAPI_POLL_WEIGHT);
4892 }
4893 }
4894 }
4895
stmmac_napi_del(struct net_device * dev)4896 static void stmmac_napi_del(struct net_device *dev)
4897 {
4898 struct stmmac_priv *priv = netdev_priv(dev);
4899 u32 queue, maxq;
4900
4901 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
4902
4903 for (queue = 0; queue < maxq; queue++) {
4904 struct stmmac_channel *ch = &priv->channel[queue];
4905
4906 if (queue < priv->plat->rx_queues_to_use)
4907 netif_napi_del(&ch->rx_napi);
4908 if (queue < priv->plat->tx_queues_to_use)
4909 netif_napi_del(&ch->tx_napi);
4910 }
4911 }
4912
stmmac_reinit_queues(struct net_device * dev,u32 rx_cnt,u32 tx_cnt)4913 int stmmac_reinit_queues(struct net_device *dev, u32 rx_cnt, u32 tx_cnt)
4914 {
4915 struct stmmac_priv *priv = netdev_priv(dev);
4916 int ret = 0;
4917
4918 if (netif_running(dev))
4919 stmmac_release(dev);
4920
4921 stmmac_napi_del(dev);
4922
4923 priv->plat->rx_queues_to_use = rx_cnt;
4924 priv->plat->tx_queues_to_use = tx_cnt;
4925
4926 stmmac_napi_add(dev);
4927
4928 if (netif_running(dev))
4929 ret = stmmac_open(dev);
4930
4931 return ret;
4932 }
4933
stmmac_reinit_ringparam(struct net_device * dev,u32 rx_size,u32 tx_size)4934 int stmmac_reinit_ringparam(struct net_device *dev, u32 rx_size, u32 tx_size)
4935 {
4936 struct stmmac_priv *priv = netdev_priv(dev);
4937 int ret = 0;
4938
4939 if (netif_running(dev))
4940 stmmac_release(dev);
4941
4942 priv->dma_rx_size = rx_size;
4943 priv->dma_tx_size = tx_size;
4944
4945 if (netif_running(dev))
4946 ret = stmmac_open(dev);
4947
4948 return ret;
4949 }
4950
4951 /**
4952 * stmmac_dvr_probe
4953 * @device: device pointer
4954 * @plat_dat: platform data pointer
4955 * @res: stmmac resource pointer
4956 * Description: this is the main probe function used to
4957 * call the alloc_etherdev, allocate the priv structure.
4958 * Return:
4959 * returns 0 on success, otherwise errno.
4960 */
stmmac_dvr_probe(struct device * device,struct plat_stmmacenet_data * plat_dat,struct stmmac_resources * res)4961 int stmmac_dvr_probe(struct device *device,
4962 struct plat_stmmacenet_data *plat_dat,
4963 struct stmmac_resources *res)
4964 {
4965 struct net_device *ndev = NULL;
4966 struct stmmac_priv *priv;
4967 u32 rxq;
4968 int i, ret = 0;
4969
4970 ndev = devm_alloc_etherdev_mqs(device, sizeof(struct stmmac_priv),
4971 MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES);
4972 if (!ndev)
4973 return -ENOMEM;
4974
4975 SET_NETDEV_DEV(ndev, device);
4976
4977 priv = netdev_priv(ndev);
4978 priv->device = device;
4979 priv->dev = ndev;
4980
4981 stmmac_set_ethtool_ops(ndev);
4982 priv->pause = pause;
4983 priv->plat = plat_dat;
4984 priv->ioaddr = res->addr;
4985 priv->dev->base_addr = (unsigned long)res->addr;
4986
4987 priv->dev->irq = res->irq;
4988 priv->wol_irq = res->wol_irq;
4989 priv->lpi_irq = res->lpi_irq;
4990
4991 if (!IS_ERR_OR_NULL(res->mac))
4992 memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
4993
4994 dev_set_drvdata(device, priv->dev);
4995
4996 /* Verify driver arguments */
4997 stmmac_verify_args();
4998
4999 /* Allocate workqueue */
5000 priv->wq = create_singlethread_workqueue("stmmac_wq");
5001 if (!priv->wq) {
5002 dev_err(priv->device, "failed to create workqueue\n");
5003 return -ENOMEM;
5004 }
5005
5006 INIT_WORK(&priv->service_task, stmmac_service_task);
5007
5008 /* Override with kernel parameters if supplied XXX CRS XXX
5009 * this needs to have multiple instances
5010 */
5011 if ((phyaddr >= 0) && (phyaddr <= 31))
5012 priv->plat->phy_addr = phyaddr;
5013
5014 if (priv->plat->stmmac_rst) {
5015 ret = reset_control_assert(priv->plat->stmmac_rst);
5016 reset_control_deassert(priv->plat->stmmac_rst);
5017 /* Some reset controllers have only reset callback instead of
5018 * assert + deassert callbacks pair.
5019 */
5020 if (ret == -ENOTSUPP)
5021 reset_control_reset(priv->plat->stmmac_rst);
5022 }
5023
5024 /* Init MAC and get the capabilities */
5025 ret = stmmac_hw_init(priv);
5026 if (ret)
5027 goto error_hw_init;
5028
5029 stmmac_check_ether_addr(priv);
5030
5031 ndev->netdev_ops = &stmmac_netdev_ops;
5032
5033 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5034 NETIF_F_RXCSUM;
5035
5036 ret = stmmac_tc_init(priv, priv);
5037 if (!ret) {
5038 ndev->hw_features |= NETIF_F_HW_TC;
5039 }
5040
5041 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
5042 ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
5043 if (priv->plat->has_gmac4)
5044 ndev->hw_features |= NETIF_F_GSO_UDP_L4;
5045 priv->tso = true;
5046 dev_info(priv->device, "TSO feature enabled\n");
5047 }
5048
5049 if (priv->dma_cap.sphen) {
5050 ndev->hw_features |= NETIF_F_GRO;
5051 priv->sph = true;
5052 dev_info(priv->device, "SPH feature enabled\n");
5053 }
5054
5055 /* The current IP register MAC_HW_Feature1[ADDR64] only define
5056 * 32/40/64 bit width, but some SOC support others like i.MX8MP
5057 * support 34 bits but it map to 40 bits width in MAC_HW_Feature1[ADDR64].
5058 * So overwrite dma_cap.addr64 according to HW real design.
5059 */
5060 if (priv->plat->addr64)
5061 priv->dma_cap.addr64 = priv->plat->addr64;
5062
5063 if (priv->dma_cap.addr64) {
5064 ret = dma_set_mask_and_coherent(device,
5065 DMA_BIT_MASK(priv->dma_cap.addr64));
5066 if (!ret) {
5067 dev_info(priv->device, "Using %d bits DMA width\n",
5068 priv->dma_cap.addr64);
5069
5070 /*
5071 * If more than 32 bits can be addressed, make sure to
5072 * enable enhanced addressing mode.
5073 */
5074 if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT))
5075 priv->plat->dma_cfg->eame = true;
5076 } else {
5077 ret = dma_set_mask_and_coherent(device, DMA_BIT_MASK(32));
5078 if (ret) {
5079 dev_err(priv->device, "Failed to set DMA Mask\n");
5080 goto error_hw_init;
5081 }
5082
5083 priv->dma_cap.addr64 = 32;
5084 }
5085 }
5086
5087 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
5088 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
5089 #ifdef STMMAC_VLAN_TAG_USED
5090 /* Both mac100 and gmac support receive VLAN tag detection */
5091 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX;
5092 if (priv->dma_cap.vlhash) {
5093 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
5094 ndev->features |= NETIF_F_HW_VLAN_STAG_FILTER;
5095 }
5096 if (priv->dma_cap.vlins) {
5097 ndev->features |= NETIF_F_HW_VLAN_CTAG_TX;
5098 if (priv->dma_cap.dvlan)
5099 ndev->features |= NETIF_F_HW_VLAN_STAG_TX;
5100 }
5101 #endif
5102 priv->msg_enable = netif_msg_init(debug, default_msg_level);
5103
5104 /* Initialize RSS */
5105 rxq = priv->plat->rx_queues_to_use;
5106 netdev_rss_key_fill(priv->rss.key, sizeof(priv->rss.key));
5107 for (i = 0; i < ARRAY_SIZE(priv->rss.table); i++)
5108 priv->rss.table[i] = ethtool_rxfh_indir_default(i, rxq);
5109
5110 if (priv->dma_cap.rssen && priv->plat->rss_en)
5111 ndev->features |= NETIF_F_RXHASH;
5112
5113 /* MTU range: 46 - hw-specific max */
5114 ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
5115 if (priv->plat->has_xgmac)
5116 ndev->max_mtu = XGMAC_JUMBO_LEN;
5117 else if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
5118 ndev->max_mtu = JUMBO_LEN;
5119 else
5120 ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
5121 /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
5122 * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
5123 */
5124 if ((priv->plat->maxmtu < ndev->max_mtu) &&
5125 (priv->plat->maxmtu >= ndev->min_mtu))
5126 ndev->max_mtu = priv->plat->maxmtu;
5127 else if (priv->plat->maxmtu < ndev->min_mtu)
5128 dev_warn(priv->device,
5129 "%s: warning: maxmtu having invalid value (%d)\n",
5130 __func__, priv->plat->maxmtu);
5131
5132 if (flow_ctrl)
5133 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
5134
5135 /* Setup channels NAPI */
5136 stmmac_napi_add(ndev);
5137
5138 mutex_init(&priv->lock);
5139
5140 /* If a specific clk_csr value is passed from the platform
5141 * this means that the CSR Clock Range selection cannot be
5142 * changed at run-time and it is fixed. Viceversa the driver'll try to
5143 * set the MDC clock dynamically according to the csr actual
5144 * clock input.
5145 */
5146 if (priv->plat->clk_csr >= 0)
5147 priv->clk_csr = priv->plat->clk_csr;
5148 else
5149 stmmac_clk_csr_set(priv);
5150
5151 stmmac_check_pcs_mode(priv);
5152
5153 pm_runtime_get_noresume(device);
5154 pm_runtime_set_active(device);
5155 pm_runtime_enable(device);
5156
5157 if (priv->hw->pcs != STMMAC_PCS_TBI &&
5158 priv->hw->pcs != STMMAC_PCS_RTBI) {
5159 /* MDIO bus Registration */
5160 ret = stmmac_mdio_register(ndev);
5161 if (ret < 0) {
5162 dev_err(priv->device,
5163 "%s: MDIO bus (id: %d) registration failed",
5164 __func__, priv->plat->bus_id);
5165 goto error_mdio_register;
5166 }
5167 }
5168
5169 ret = stmmac_phy_setup(priv);
5170 if (ret) {
5171 netdev_err(ndev, "failed to setup phy (%d)\n", ret);
5172 goto error_phy_setup;
5173 }
5174
5175 ret = register_netdev(ndev);
5176 if (ret) {
5177 dev_err(priv->device, "%s: ERROR %i registering the device\n",
5178 __func__, ret);
5179 goto error_netdev_register;
5180 }
5181
5182 if (priv->plat->serdes_powerup) {
5183 ret = priv->plat->serdes_powerup(ndev,
5184 priv->plat->bsp_priv);
5185
5186 if (ret < 0)
5187 goto error_serdes_powerup;
5188 }
5189
5190 #ifdef CONFIG_DEBUG_FS
5191 stmmac_init_fs(ndev);
5192 #endif
5193
5194 /* Let pm_runtime_put() disable the clocks.
5195 * If CONFIG_PM is not enabled, the clocks will stay powered.
5196 */
5197 pm_runtime_put(device);
5198
5199 return ret;
5200
5201 error_serdes_powerup:
5202 unregister_netdev(ndev);
5203 error_netdev_register:
5204 phylink_destroy(priv->phylink);
5205 error_phy_setup:
5206 if (priv->hw->pcs != STMMAC_PCS_TBI &&
5207 priv->hw->pcs != STMMAC_PCS_RTBI)
5208 stmmac_mdio_unregister(ndev);
5209 error_mdio_register:
5210 stmmac_napi_del(ndev);
5211 error_hw_init:
5212 destroy_workqueue(priv->wq);
5213
5214 return ret;
5215 }
5216 EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
5217
5218 /**
5219 * stmmac_dvr_remove
5220 * @dev: device pointer
5221 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
5222 * changes the link status, releases the DMA descriptor rings.
5223 */
stmmac_dvr_remove(struct device * dev)5224 int stmmac_dvr_remove(struct device *dev)
5225 {
5226 struct net_device *ndev = dev_get_drvdata(dev);
5227 struct stmmac_priv *priv = netdev_priv(ndev);
5228
5229 netdev_info(priv->dev, "%s: removing driver", __func__);
5230
5231 stmmac_stop_all_dma(priv);
5232 stmmac_mac_set(priv, priv->ioaddr, false);
5233 netif_carrier_off(ndev);
5234 unregister_netdev(ndev);
5235
5236 /* Serdes power down needs to happen after VLAN filter
5237 * is deleted that is triggered by unregister_netdev().
5238 */
5239 if (priv->plat->serdes_powerdown)
5240 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
5241
5242 #ifdef CONFIG_DEBUG_FS
5243 stmmac_exit_fs(ndev);
5244 #endif
5245 phylink_destroy(priv->phylink);
5246 if (priv->plat->stmmac_rst)
5247 reset_control_assert(priv->plat->stmmac_rst);
5248 pm_runtime_put(dev);
5249 pm_runtime_disable(dev);
5250 if (priv->hw->pcs != STMMAC_PCS_TBI &&
5251 priv->hw->pcs != STMMAC_PCS_RTBI)
5252 stmmac_mdio_unregister(ndev);
5253 destroy_workqueue(priv->wq);
5254 mutex_destroy(&priv->lock);
5255
5256 return 0;
5257 }
5258 EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
5259
5260 /**
5261 * stmmac_suspend - suspend callback
5262 * @dev: device pointer
5263 * Description: this is the function to suspend the device and it is called
5264 * by the platform driver to stop the network queue, release the resources,
5265 * program the PMT register (for WoL), clean and release driver resources.
5266 */
stmmac_suspend(struct device * dev)5267 int stmmac_suspend(struct device *dev)
5268 {
5269 struct net_device *ndev = dev_get_drvdata(dev);
5270 struct stmmac_priv *priv = netdev_priv(ndev);
5271 u32 chan;
5272
5273 if (!ndev || !netif_running(ndev))
5274 return 0;
5275
5276 phylink_mac_change(priv->phylink, false);
5277
5278 mutex_lock(&priv->lock);
5279
5280 netif_device_detach(ndev);
5281
5282 stmmac_disable_all_queues(priv);
5283
5284 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
5285 del_timer_sync(&priv->tx_queue[chan].txtimer);
5286
5287 if (priv->eee_enabled) {
5288 priv->tx_path_in_lpi_mode = false;
5289 del_timer_sync(&priv->eee_ctrl_timer);
5290 }
5291
5292 /* Stop TX/RX DMA */
5293 stmmac_stop_all_dma(priv);
5294
5295 if (priv->plat->serdes_powerdown)
5296 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv);
5297
5298 /* Enable Power down mode by programming the PMT regs */
5299 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
5300 stmmac_pmt(priv, priv->hw, priv->wolopts);
5301 priv->irq_wake = 1;
5302 } else {
5303 mutex_unlock(&priv->lock);
5304 rtnl_lock();
5305 if (device_may_wakeup(priv->device))
5306 phylink_speed_down(priv->phylink, false);
5307 phylink_stop(priv->phylink);
5308 rtnl_unlock();
5309 mutex_lock(&priv->lock);
5310
5311 stmmac_mac_set(priv, priv->ioaddr, false);
5312 pinctrl_pm_select_sleep_state(priv->device);
5313 }
5314 mutex_unlock(&priv->lock);
5315
5316 priv->speed = SPEED_UNKNOWN;
5317 return 0;
5318 }
5319 EXPORT_SYMBOL_GPL(stmmac_suspend);
5320
5321 /**
5322 * stmmac_reset_queues_param - reset queue parameters
5323 * @priv: device pointer
5324 */
stmmac_reset_queues_param(struct stmmac_priv * priv)5325 static void stmmac_reset_queues_param(struct stmmac_priv *priv)
5326 {
5327 u32 rx_cnt = priv->plat->rx_queues_to_use;
5328 u32 tx_cnt = priv->plat->tx_queues_to_use;
5329 u32 queue;
5330
5331 for (queue = 0; queue < rx_cnt; queue++) {
5332 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
5333
5334 rx_q->cur_rx = 0;
5335 rx_q->dirty_rx = 0;
5336 }
5337
5338 for (queue = 0; queue < tx_cnt; queue++) {
5339 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
5340
5341 tx_q->cur_tx = 0;
5342 tx_q->dirty_tx = 0;
5343 tx_q->mss = 0;
5344
5345 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
5346 }
5347 }
5348
5349 /**
5350 * stmmac_resume - resume callback
5351 * @dev: device pointer
5352 * Description: when resume this function is invoked to setup the DMA and CORE
5353 * in a usable state.
5354 */
stmmac_resume(struct device * dev)5355 int stmmac_resume(struct device *dev)
5356 {
5357 struct net_device *ndev = dev_get_drvdata(dev);
5358 struct stmmac_priv *priv = netdev_priv(ndev);
5359 int ret;
5360
5361 if (!netif_running(ndev))
5362 return 0;
5363
5364 /* Power Down bit, into the PM register, is cleared
5365 * automatically as soon as a magic packet or a Wake-up frame
5366 * is received. Anyway, it's better to manually clear
5367 * this bit because it can generate problems while resuming
5368 * from another devices (e.g. serial console).
5369 */
5370 if (device_may_wakeup(priv->device) && priv->plat->pmt) {
5371 mutex_lock(&priv->lock);
5372 stmmac_pmt(priv, priv->hw, 0);
5373 mutex_unlock(&priv->lock);
5374 priv->irq_wake = 0;
5375 } else {
5376 pinctrl_pm_select_default_state(priv->device);
5377 /* reset the phy so that it's ready */
5378 if (priv->mii)
5379 stmmac_mdio_reset(priv->mii);
5380 }
5381
5382 if (priv->plat->serdes_powerup) {
5383 ret = priv->plat->serdes_powerup(ndev,
5384 priv->plat->bsp_priv);
5385
5386 if (ret < 0)
5387 return ret;
5388 }
5389
5390 if (!device_may_wakeup(priv->device) || !priv->plat->pmt) {
5391 rtnl_lock();
5392 phylink_start(priv->phylink);
5393 /* We may have called phylink_speed_down before */
5394 phylink_speed_up(priv->phylink);
5395 rtnl_unlock();
5396 }
5397
5398 rtnl_lock();
5399 mutex_lock(&priv->lock);
5400
5401 stmmac_reset_queues_param(priv);
5402
5403 stmmac_free_tx_skbufs(priv);
5404 stmmac_clear_descriptors(priv);
5405
5406 stmmac_hw_setup(ndev, false);
5407 stmmac_init_coalesce(priv);
5408 stmmac_set_rx_mode(ndev);
5409
5410 stmmac_restore_hw_vlan_rx_fltr(priv, ndev, priv->hw);
5411
5412 stmmac_enable_all_queues(priv);
5413
5414 mutex_unlock(&priv->lock);
5415 rtnl_unlock();
5416
5417 phylink_mac_change(priv->phylink, true);
5418
5419 netif_device_attach(ndev);
5420
5421 return 0;
5422 }
5423 EXPORT_SYMBOL_GPL(stmmac_resume);
5424
5425 #ifndef MODULE
stmmac_cmdline_opt(char * str)5426 static int __init stmmac_cmdline_opt(char *str)
5427 {
5428 char *opt;
5429
5430 if (!str || !*str)
5431 return -EINVAL;
5432 while ((opt = strsep(&str, ",")) != NULL) {
5433 if (!strncmp(opt, "debug:", 6)) {
5434 if (kstrtoint(opt + 6, 0, &debug))
5435 goto err;
5436 } else if (!strncmp(opt, "phyaddr:", 8)) {
5437 if (kstrtoint(opt + 8, 0, &phyaddr))
5438 goto err;
5439 } else if (!strncmp(opt, "buf_sz:", 7)) {
5440 if (kstrtoint(opt + 7, 0, &buf_sz))
5441 goto err;
5442 } else if (!strncmp(opt, "tc:", 3)) {
5443 if (kstrtoint(opt + 3, 0, &tc))
5444 goto err;
5445 } else if (!strncmp(opt, "watchdog:", 9)) {
5446 if (kstrtoint(opt + 9, 0, &watchdog))
5447 goto err;
5448 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
5449 if (kstrtoint(opt + 10, 0, &flow_ctrl))
5450 goto err;
5451 } else if (!strncmp(opt, "pause:", 6)) {
5452 if (kstrtoint(opt + 6, 0, &pause))
5453 goto err;
5454 } else if (!strncmp(opt, "eee_timer:", 10)) {
5455 if (kstrtoint(opt + 10, 0, &eee_timer))
5456 goto err;
5457 } else if (!strncmp(opt, "chain_mode:", 11)) {
5458 if (kstrtoint(opt + 11, 0, &chain_mode))
5459 goto err;
5460 }
5461 }
5462 return 0;
5463
5464 err:
5465 pr_err("%s: ERROR broken module parameter conversion", __func__);
5466 return -EINVAL;
5467 }
5468
5469 __setup("stmmaceth=", stmmac_cmdline_opt);
5470 #endif /* MODULE */
5471
stmmac_init(void)5472 static int __init stmmac_init(void)
5473 {
5474 #ifdef CONFIG_DEBUG_FS
5475 /* Create debugfs main directory if it doesn't exist yet */
5476 if (!stmmac_fs_dir)
5477 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
5478 register_netdevice_notifier(&stmmac_notifier);
5479 #endif
5480
5481 return 0;
5482 }
5483
stmmac_exit(void)5484 static void __exit stmmac_exit(void)
5485 {
5486 #ifdef CONFIG_DEBUG_FS
5487 unregister_netdevice_notifier(&stmmac_notifier);
5488 debugfs_remove_recursive(stmmac_fs_dir);
5489 #endif
5490 }
5491
5492 module_init(stmmac_init)
5493 module_exit(stmmac_exit)
5494
5495 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
5496 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
5497 MODULE_LICENSE("GPL");
5498