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