blob: 3ad4036fb6d9083c15aac25632d4a7af5c0c6ff8 [file] [log] [blame]
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001/*
2 * AMD 10Gb Ethernet driver
3 *
4 * This file is available to you under your choice of the following two
5 * licenses:
6 *
7 * License 1: GPLv2
8 *
Lendacky, Thomasb4eee842016-02-17 11:48:08 -06009 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -050010 *
11 * This file is free software; you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or (at
14 * your option) any later version.
15 *
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 * This file incorporates work covered by the following copyright and
25 * permission notice:
26 * The Synopsys DWC ETHER XGMAC Software Driver and documentation
27 * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
28 * Inc. unless otherwise expressly agreed to in writing between Synopsys
29 * and you.
30 *
31 * The Software IS NOT an item of Licensed Software or Licensed Product
32 * under any End User Software License Agreement or Agreement for Licensed
33 * Product with Synopsys or any supplement thereto. Permission is hereby
34 * granted, free of charge, to any person obtaining a copy of this software
35 * annotated with this license and the Software, to deal in the Software
36 * without restriction, including without limitation the rights to use,
37 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
38 * of the Software, and to permit persons to whom the Software is furnished
39 * to do so, subject to the following conditions:
40 *
41 * The above copyright notice and this permission notice shall be included
42 * in all copies or substantial portions of the Software.
43 *
44 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
45 * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
47 * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
48 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54 * THE POSSIBILITY OF SUCH DAMAGE.
55 *
56 *
57 * License 2: Modified BSD
58 *
Lendacky, Thomasb4eee842016-02-17 11:48:08 -060059 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -050060 * All rights reserved.
61 *
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions are met:
64 * * Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * * Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * * Neither the name of Advanced Micro Devices, Inc. nor the
70 * names of its contributors may be used to endorse or promote products
71 * derived from this software without specific prior written permission.
72 *
73 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76 * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
77 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
78 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
79 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
80 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83 *
84 * This file incorporates work covered by the following copyright and
85 * permission notice:
86 * The Synopsys DWC ETHER XGMAC Software Driver and documentation
87 * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
88 * Inc. unless otherwise expressly agreed to in writing between Synopsys
89 * and you.
90 *
91 * The Software IS NOT an item of Licensed Software or Licensed Product
92 * under any End User Software License Agreement or Agreement for Licensed
93 * Product with Synopsys or any supplement thereto. Permission is hereby
94 * granted, free of charge, to any person obtaining a copy of this software
95 * annotated with this license and the Software, to deal in the Software
96 * without restriction, including without limitation the rights to use,
97 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98 * of the Software, and to permit persons to whom the Software is furnished
99 * to do so, subject to the following conditions:
100 *
101 * The above copyright notice and this permission notice shall be included
102 * in all copies or substantial portions of the Software.
103 *
104 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105 * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107 * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114 * THE POSSIBILITY OF SUCH DAMAGE.
115 */
116
117#include <linux/phy.h>
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600118#include <linux/mdio.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500119#include <linux/clk.h>
Lendacky, Thomas801c62d2014-06-24 16:19:24 -0500120#include <linux/bitrev.h>
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500121#include <linux/crc32.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500122
123#include "xgbe.h"
124#include "xgbe-common.h"
125
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500126static inline unsigned int xgbe_get_max_frame(struct xgbe_prv_data *pdata)
127{
128 return pdata->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
129}
130
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500131static unsigned int xgbe_usec_to_riwt(struct xgbe_prv_data *pdata,
132 unsigned int usec)
133{
134 unsigned long rate;
135 unsigned int ret;
136
137 DBGPR("-->xgbe_usec_to_riwt\n");
138
Lendacky, Thomas82a19032015-01-16 12:47:16 -0600139 rate = pdata->sysclk_rate;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500140
141 /*
142 * Convert the input usec value to the watchdog timer value. Each
143 * watchdog timer value is equivalent to 256 clock cycles.
144 * Calculate the required value as:
145 * ( usec * ( system_clock_mhz / 10^6 ) / 256
146 */
147 ret = (usec * (rate / 1000000)) / 256;
148
149 DBGPR("<--xgbe_usec_to_riwt\n");
150
151 return ret;
152}
153
154static unsigned int xgbe_riwt_to_usec(struct xgbe_prv_data *pdata,
155 unsigned int riwt)
156{
157 unsigned long rate;
158 unsigned int ret;
159
160 DBGPR("-->xgbe_riwt_to_usec\n");
161
Lendacky, Thomas82a19032015-01-16 12:47:16 -0600162 rate = pdata->sysclk_rate;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500163
164 /*
165 * Convert the input watchdog timer value to the usec value. Each
166 * watchdog timer value is equivalent to 256 clock cycles.
167 * Calculate the required value as:
168 * ( riwt * 256 ) / ( system_clock_mhz / 10^6 )
169 */
170 ret = (riwt * 256) / (rate / 1000000);
171
172 DBGPR("<--xgbe_riwt_to_usec\n");
173
174 return ret;
175}
176
177static int xgbe_config_pblx8(struct xgbe_prv_data *pdata)
178{
179 struct xgbe_channel *channel;
180 unsigned int i;
181
182 channel = pdata->channel;
183 for (i = 0; i < pdata->channel_count; i++, channel++)
184 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_CR, PBLX8,
185 pdata->pblx8);
186
187 return 0;
188}
189
190static int xgbe_get_tx_pbl_val(struct xgbe_prv_data *pdata)
191{
192 return XGMAC_DMA_IOREAD_BITS(pdata->channel, DMA_CH_TCR, PBL);
193}
194
195static int xgbe_config_tx_pbl_val(struct xgbe_prv_data *pdata)
196{
197 struct xgbe_channel *channel;
198 unsigned int i;
199
200 channel = pdata->channel;
201 for (i = 0; i < pdata->channel_count; i++, channel++) {
202 if (!channel->tx_ring)
203 break;
204
205 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, PBL,
206 pdata->tx_pbl);
207 }
208
209 return 0;
210}
211
212static int xgbe_get_rx_pbl_val(struct xgbe_prv_data *pdata)
213{
214 return XGMAC_DMA_IOREAD_BITS(pdata->channel, DMA_CH_RCR, PBL);
215}
216
217static int xgbe_config_rx_pbl_val(struct xgbe_prv_data *pdata)
218{
219 struct xgbe_channel *channel;
220 unsigned int i;
221
222 channel = pdata->channel;
223 for (i = 0; i < pdata->channel_count; i++, channel++) {
224 if (!channel->rx_ring)
225 break;
226
227 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, PBL,
228 pdata->rx_pbl);
229 }
230
231 return 0;
232}
233
234static int xgbe_config_osp_mode(struct xgbe_prv_data *pdata)
235{
236 struct xgbe_channel *channel;
237 unsigned int i;
238
239 channel = pdata->channel;
240 for (i = 0; i < pdata->channel_count; i++, channel++) {
241 if (!channel->tx_ring)
242 break;
243
244 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, OSP,
245 pdata->tx_osp_mode);
246 }
247
248 return 0;
249}
250
251static int xgbe_config_rsf_mode(struct xgbe_prv_data *pdata, unsigned int val)
252{
253 unsigned int i;
254
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500255 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500256 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RSF, val);
257
258 return 0;
259}
260
261static int xgbe_config_tsf_mode(struct xgbe_prv_data *pdata, unsigned int val)
262{
263 unsigned int i;
264
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500265 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500266 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TSF, val);
267
268 return 0;
269}
270
271static int xgbe_config_rx_threshold(struct xgbe_prv_data *pdata,
272 unsigned int val)
273{
274 unsigned int i;
275
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500276 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500277 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RTC, val);
278
279 return 0;
280}
281
282static int xgbe_config_tx_threshold(struct xgbe_prv_data *pdata,
283 unsigned int val)
284{
285 unsigned int i;
286
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500287 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500288 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TTC, val);
289
290 return 0;
291}
292
293static int xgbe_config_rx_coalesce(struct xgbe_prv_data *pdata)
294{
295 struct xgbe_channel *channel;
296 unsigned int i;
297
298 channel = pdata->channel;
299 for (i = 0; i < pdata->channel_count; i++, channel++) {
300 if (!channel->rx_ring)
301 break;
302
303 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RIWT, RWT,
304 pdata->rx_riwt);
305 }
306
307 return 0;
308}
309
310static int xgbe_config_tx_coalesce(struct xgbe_prv_data *pdata)
311{
312 return 0;
313}
314
315static void xgbe_config_rx_buffer_size(struct xgbe_prv_data *pdata)
316{
317 struct xgbe_channel *channel;
318 unsigned int i;
319
320 channel = pdata->channel;
321 for (i = 0; i < pdata->channel_count; i++, channel++) {
322 if (!channel->rx_ring)
323 break;
324
325 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, RBSZ,
326 pdata->rx_buf_size);
327 }
328}
329
330static void xgbe_config_tso_mode(struct xgbe_prv_data *pdata)
331{
332 struct xgbe_channel *channel;
333 unsigned int i;
334
335 channel = pdata->channel;
336 for (i = 0; i < pdata->channel_count; i++, channel++) {
337 if (!channel->tx_ring)
338 break;
339
340 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, TSE, 1);
341 }
342}
343
Lendacky, Thomas174fd252014-11-04 16:06:50 -0600344static void xgbe_config_sph_mode(struct xgbe_prv_data *pdata)
345{
346 struct xgbe_channel *channel;
347 unsigned int i;
348
349 channel = pdata->channel;
350 for (i = 0; i < pdata->channel_count; i++, channel++) {
351 if (!channel->rx_ring)
352 break;
353
354 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_CR, SPH, 1);
355 }
356
357 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, HDSMS, XGBE_SPH_HDSMS_SIZE);
358}
359
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -0600360static int xgbe_write_rss_reg(struct xgbe_prv_data *pdata, unsigned int type,
361 unsigned int index, unsigned int val)
362{
363 unsigned int wait;
364 int ret = 0;
365
366 mutex_lock(&pdata->rss_mutex);
367
368 if (XGMAC_IOREAD_BITS(pdata, MAC_RSSAR, OB)) {
369 ret = -EBUSY;
370 goto unlock;
371 }
372
373 XGMAC_IOWRITE(pdata, MAC_RSSDR, val);
374
375 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, RSSIA, index);
376 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, ADDRT, type);
377 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, CT, 0);
378 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, OB, 1);
379
380 wait = 1000;
381 while (wait--) {
382 if (!XGMAC_IOREAD_BITS(pdata, MAC_RSSAR, OB))
383 goto unlock;
384
385 usleep_range(1000, 1500);
386 }
387
388 ret = -EBUSY;
389
390unlock:
391 mutex_unlock(&pdata->rss_mutex);
392
393 return ret;
394}
395
396static int xgbe_write_rss_hash_key(struct xgbe_prv_data *pdata)
397{
398 unsigned int key_regs = sizeof(pdata->rss_key) / sizeof(u32);
399 unsigned int *key = (unsigned int *)&pdata->rss_key;
400 int ret;
401
402 while (key_regs--) {
403 ret = xgbe_write_rss_reg(pdata, XGBE_RSS_HASH_KEY_TYPE,
404 key_regs, *key++);
405 if (ret)
406 return ret;
407 }
408
409 return 0;
410}
411
412static int xgbe_write_rss_lookup_table(struct xgbe_prv_data *pdata)
413{
414 unsigned int i;
415 int ret;
416
417 for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++) {
418 ret = xgbe_write_rss_reg(pdata,
419 XGBE_RSS_LOOKUP_TABLE_TYPE, i,
420 pdata->rss_table[i]);
421 if (ret)
422 return ret;
423 }
424
425 return 0;
426}
427
Lendacky, Thomasf6ac8622014-11-04 16:07:23 -0600428static int xgbe_set_rss_hash_key(struct xgbe_prv_data *pdata, const u8 *key)
429{
430 memcpy(pdata->rss_key, key, sizeof(pdata->rss_key));
431
432 return xgbe_write_rss_hash_key(pdata);
433}
434
435static int xgbe_set_rss_lookup_table(struct xgbe_prv_data *pdata,
436 const u32 *table)
437{
438 unsigned int i;
439
440 for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++)
441 XGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH, table[i]);
442
443 return xgbe_write_rss_lookup_table(pdata);
444}
445
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -0600446static int xgbe_enable_rss(struct xgbe_prv_data *pdata)
447{
448 int ret;
449
450 if (!pdata->hw_feat.rss)
451 return -EOPNOTSUPP;
452
453 /* Program the hash key */
454 ret = xgbe_write_rss_hash_key(pdata);
455 if (ret)
456 return ret;
457
458 /* Program the lookup table */
459 ret = xgbe_write_rss_lookup_table(pdata);
460 if (ret)
461 return ret;
462
463 /* Set the RSS options */
464 XGMAC_IOWRITE(pdata, MAC_RSSCR, pdata->rss_options);
465
466 /* Enable RSS */
467 XGMAC_IOWRITE_BITS(pdata, MAC_RSSCR, RSSE, 1);
468
469 return 0;
470}
471
472static int xgbe_disable_rss(struct xgbe_prv_data *pdata)
473{
474 if (!pdata->hw_feat.rss)
475 return -EOPNOTSUPP;
476
477 XGMAC_IOWRITE_BITS(pdata, MAC_RSSCR, RSSE, 0);
478
479 return 0;
480}
481
482static void xgbe_config_rss(struct xgbe_prv_data *pdata)
483{
484 int ret;
485
486 if (!pdata->hw_feat.rss)
487 return;
488
489 if (pdata->netdev->features & NETIF_F_RXHASH)
490 ret = xgbe_enable_rss(pdata);
491 else
492 ret = xgbe_disable_rss(pdata);
493
494 if (ret)
495 netdev_err(pdata->netdev,
496 "error configuring RSS, RSS disabled\n");
497}
498
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500499static bool xgbe_is_pfc_queue(struct xgbe_prv_data *pdata,
500 unsigned int queue)
501{
502 unsigned int prio, tc;
503
504 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
505 /* Does this queue handle the priority? */
506 if (pdata->prio2q_map[prio] != queue)
507 continue;
508
509 /* Get the Traffic Class for this priority */
510 tc = pdata->ets->prio_tc[prio];
511
512 /* Check if PFC is enabled for this traffic class */
513 if (pdata->pfc->pfc_en & (1 << tc))
514 return true;
515 }
516
517 return false;
518}
519
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500520static int xgbe_disable_tx_flow_control(struct xgbe_prv_data *pdata)
521{
522 unsigned int max_q_count, q_count;
523 unsigned int reg, reg_val;
524 unsigned int i;
525
526 /* Clear MTL flow control */
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500527 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500528 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, 0);
529
530 /* Clear MAC flow control */
531 max_q_count = XGMAC_MAX_FLOW_CONTROL_QUEUES;
Lendacky, Thomas9fc69af2014-08-29 13:17:08 -0500532 q_count = min_t(unsigned int, pdata->tx_q_count, max_q_count);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500533 reg = MAC_Q0TFCR;
534 for (i = 0; i < q_count; i++) {
535 reg_val = XGMAC_IOREAD(pdata, reg);
536 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 0);
537 XGMAC_IOWRITE(pdata, reg, reg_val);
538
539 reg += MAC_QTFCR_INC;
540 }
541
542 return 0;
543}
544
545static int xgbe_enable_tx_flow_control(struct xgbe_prv_data *pdata)
546{
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600547 struct ieee_pfc *pfc = pdata->pfc;
548 struct ieee_ets *ets = pdata->ets;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500549 unsigned int max_q_count, q_count;
550 unsigned int reg, reg_val;
551 unsigned int i;
552
553 /* Set MTL flow control */
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600554 for (i = 0; i < pdata->rx_q_count; i++) {
555 unsigned int ehfc = 0;
556
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500557 if (pdata->rx_rfd[i]) {
558 /* Flow control thresholds are established */
559 if (pfc && ets) {
560 if (xgbe_is_pfc_queue(pdata, i))
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600561 ehfc = 1;
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500562 } else {
563 ehfc = 1;
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600564 }
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600565 }
566
567 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, ehfc);
568
569 netif_dbg(pdata, drv, pdata->netdev,
570 "flow control %s for RXq%u\n",
571 ehfc ? "enabled" : "disabled", i);
572 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500573
574 /* Set MAC flow control */
575 max_q_count = XGMAC_MAX_FLOW_CONTROL_QUEUES;
Lendacky, Thomas9fc69af2014-08-29 13:17:08 -0500576 q_count = min_t(unsigned int, pdata->tx_q_count, max_q_count);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500577 reg = MAC_Q0TFCR;
578 for (i = 0; i < q_count; i++) {
579 reg_val = XGMAC_IOREAD(pdata, reg);
580
581 /* Enable transmit flow control */
582 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 1);
583 /* Set pause time */
584 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, PT, 0xffff);
585
586 XGMAC_IOWRITE(pdata, reg, reg_val);
587
588 reg += MAC_QTFCR_INC;
589 }
590
591 return 0;
592}
593
594static int xgbe_disable_rx_flow_control(struct xgbe_prv_data *pdata)
595{
596 XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 0);
597
598 return 0;
599}
600
601static int xgbe_enable_rx_flow_control(struct xgbe_prv_data *pdata)
602{
603 XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 1);
604
605 return 0;
606}
607
608static int xgbe_config_tx_flow_control(struct xgbe_prv_data *pdata)
609{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500610 struct ieee_pfc *pfc = pdata->pfc;
611
612 if (pdata->tx_pause || (pfc && pfc->pfc_en))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500613 xgbe_enable_tx_flow_control(pdata);
614 else
615 xgbe_disable_tx_flow_control(pdata);
616
617 return 0;
618}
619
620static int xgbe_config_rx_flow_control(struct xgbe_prv_data *pdata)
621{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500622 struct ieee_pfc *pfc = pdata->pfc;
623
624 if (pdata->rx_pause || (pfc && pfc->pfc_en))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500625 xgbe_enable_rx_flow_control(pdata);
626 else
627 xgbe_disable_rx_flow_control(pdata);
628
629 return 0;
630}
631
632static void xgbe_config_flow_control(struct xgbe_prv_data *pdata)
633{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500634 struct ieee_pfc *pfc = pdata->pfc;
635
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500636 xgbe_config_tx_flow_control(pdata);
637 xgbe_config_rx_flow_control(pdata);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500638
639 XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, PFCE,
640 (pfc && pfc->pfc_en) ? 1 : 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500641}
642
643static void xgbe_enable_dma_interrupts(struct xgbe_prv_data *pdata)
644{
645 struct xgbe_channel *channel;
646 unsigned int dma_ch_isr, dma_ch_ier;
647 unsigned int i;
648
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600649 /* Set the interrupt mode if supported */
650 if (pdata->channel_irq_mode)
651 XGMAC_IOWRITE_BITS(pdata, DMA_MR, INTM,
652 pdata->channel_irq_mode);
653
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500654 channel = pdata->channel;
655 for (i = 0; i < pdata->channel_count; i++, channel++) {
656 /* Clear all the interrupts which are set */
657 dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
658 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
659
660 /* Clear all interrupt enable bits */
661 dma_ch_ier = 0;
662
663 /* Enable following interrupts
664 * NIE - Normal Interrupt Summary Enable
665 * AIE - Abnormal Interrupt Summary Enable
666 * FBEE - Fatal Bus Error Enable
667 */
668 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, NIE, 1);
669 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, AIE, 1);
670 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
671
672 if (channel->tx_ring) {
673 /* Enable the following Tx interrupts
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600674 * TIE - Transmit Interrupt Enable (unless using
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600675 * per channel interrupts in edge triggered
676 * mode)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500677 */
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600678 if (!pdata->per_channel_irq || pdata->channel_irq_mode)
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600679 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500680 }
681 if (channel->rx_ring) {
682 /* Enable following Rx interrupts
683 * RBUE - Receive Buffer Unavailable Enable
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600684 * RIE - Receive Interrupt Enable (unless using
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600685 * per channel interrupts in edge triggered
686 * mode)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500687 */
688 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 1);
Lendacky, Thomas4c70dd82016-11-10 17:10:17 -0600689 if (!pdata->per_channel_irq || pdata->channel_irq_mode)
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600690 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500691 }
692
693 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
694 }
695}
696
697static void xgbe_enable_mtl_interrupts(struct xgbe_prv_data *pdata)
698{
699 unsigned int mtl_q_isr;
700 unsigned int q_count, i;
701
702 q_count = max(pdata->hw_feat.tx_q_cnt, pdata->hw_feat.rx_q_cnt);
703 for (i = 0; i < q_count; i++) {
704 /* Clear all the interrupts which are set */
705 mtl_q_isr = XGMAC_MTL_IOREAD(pdata, i, MTL_Q_ISR);
706 XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_ISR, mtl_q_isr);
707
708 /* No MTL interrupts to be enabled */
Lendacky, Thomas91f87342014-07-02 13:04:34 -0500709 XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_IER, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500710 }
711}
712
713static void xgbe_enable_mac_interrupts(struct xgbe_prv_data *pdata)
714{
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500715 unsigned int mac_ier = 0;
716
717 /* Enable Timestamp interrupt */
718 XGMAC_SET_BITS(mac_ier, MAC_IER, TSIE, 1);
719
720 XGMAC_IOWRITE(pdata, MAC_IER, mac_ier);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500721
722 /* Enable all counter interrupts */
Lendacky, Thomasa3ba7c92014-09-05 18:02:36 -0500723 XGMAC_IOWRITE_BITS(pdata, MMC_RIER, ALL_INTERRUPTS, 0xffffffff);
724 XGMAC_IOWRITE_BITS(pdata, MMC_TIER, ALL_INTERRUPTS, 0xffffffff);
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -0600725
726 /* Enable MDIO single command completion interrupt */
727 XGMAC_IOWRITE_BITS(pdata, MAC_MDIOIER, SNGLCOMPIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500728}
729
Lendacky, Thomase78332b2016-11-10 17:10:26 -0600730static void xgbe_enable_ecc_interrupts(struct xgbe_prv_data *pdata)
731{
732 unsigned int ecc_isr, ecc_ier = 0;
733
734 if (!pdata->vdata->ecc_support)
735 return;
736
737 /* Clear all the interrupts which are set */
738 ecc_isr = XP_IOREAD(pdata, XP_ECC_ISR);
739 XP_IOWRITE(pdata, XP_ECC_ISR, ecc_isr);
740
741 /* Enable ECC interrupts */
742 XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_DED, 1);
743 XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_SEC, 1);
744 XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_DED, 1);
745 XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_SEC, 1);
746 XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_DED, 1);
747 XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_SEC, 1);
748
749 XP_IOWRITE(pdata, XP_ECC_IER, ecc_ier);
750}
751
752static void xgbe_disable_ecc_ded(struct xgbe_prv_data *pdata)
753{
754 unsigned int ecc_ier;
755
756 ecc_ier = XP_IOREAD(pdata, XP_ECC_IER);
757
758 /* Disable ECC DED interrupts */
759 XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_DED, 0);
760 XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_DED, 0);
761 XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_DED, 0);
762
763 XP_IOWRITE(pdata, XP_ECC_IER, ecc_ier);
764}
765
766static void xgbe_disable_ecc_sec(struct xgbe_prv_data *pdata,
767 enum xgbe_ecc_sec sec)
768{
769 unsigned int ecc_ier;
770
771 ecc_ier = XP_IOREAD(pdata, XP_ECC_IER);
772
773 /* Disable ECC SEC interrupt */
774 switch (sec) {
775 case XGBE_ECC_SEC_TX:
776 XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_SEC, 0);
777 break;
778 case XGBE_ECC_SEC_RX:
779 XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_SEC, 0);
780 break;
781 case XGBE_ECC_SEC_DESC:
782 XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_SEC, 0);
783 break;
784 }
785
786 XP_IOWRITE(pdata, XP_ECC_IER, ecc_ier);
787}
788
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500789static int xgbe_set_speed(struct xgbe_prv_data *pdata, int speed)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500790{
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500791 unsigned int ss;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600792
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500793 switch (speed) {
794 case SPEED_1000:
795 ss = 0x03;
796 break;
797 case SPEED_2500:
798 ss = 0x02;
799 break;
800 case SPEED_10000:
801 ss = 0x00;
802 break;
803 default:
804 return -EINVAL;
805 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500806
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500807 if (XGMAC_IOREAD_BITS(pdata, MAC_TCR, SS) != ss)
808 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, ss);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500809
810 return 0;
811}
812
Lendacky, Thomasb4eee842016-02-17 11:48:08 -0600813static int xgbe_enable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
814{
815 /* Put the VLAN tag in the Rx descriptor */
816 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLRXS, 1);
817
818 /* Don't check the VLAN type */
819 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, DOVLTC, 1);
820
821 /* Check only C-TAG (0x8100) packets */
822 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ERSVLM, 0);
823
824 /* Don't consider an S-TAG (0x88A8) packet as a VLAN packet */
825 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ESVL, 0);
826
827 /* Enable VLAN tag stripping */
828 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0x3);
829
830 return 0;
831}
832
833static int xgbe_disable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
834{
835 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0);
836
837 return 0;
838}
839
840static int xgbe_enable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
841{
842 /* Enable VLAN filtering */
843 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 1);
844
845 /* Enable VLAN Hash Table filtering */
846 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTHM, 1);
847
848 /* Disable VLAN tag inverse matching */
849 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTIM, 0);
850
851 /* Only filter on the lower 12-bits of the VLAN tag */
852 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ETV, 1);
853
854 /* In order for the VLAN Hash Table filtering to be effective,
855 * the VLAN tag identifier in the VLAN Tag Register must not
856 * be zero. Set the VLAN tag identifier to "1" to enable the
857 * VLAN Hash Table filtering. This implies that a VLAN tag of
858 * 1 will always pass filtering.
859 */
860 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VL, 1);
861
862 return 0;
863}
864
865static int xgbe_disable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
866{
867 /* Disable VLAN filtering */
868 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 0);
869
870 return 0;
871}
872
873static u32 xgbe_vid_crc32_le(__le16 vid_le)
874{
875 u32 poly = 0xedb88320; /* CRCPOLY_LE */
876 u32 crc = ~0;
877 u32 temp = 0;
878 unsigned char *data = (unsigned char *)&vid_le;
879 unsigned char data_byte = 0;
880 int i, bits;
881
882 bits = get_bitmask_order(VLAN_VID_MASK);
883 for (i = 0; i < bits; i++) {
884 if ((i % 8) == 0)
885 data_byte = data[i / 8];
886
887 temp = ((crc & 1) ^ data_byte) & 1;
888 crc >>= 1;
889 data_byte >>= 1;
890
891 if (temp)
892 crc ^= poly;
893 }
894
895 return crc;
896}
897
898static int xgbe_update_vlan_hash_table(struct xgbe_prv_data *pdata)
899{
900 u32 crc;
901 u16 vid;
902 __le16 vid_le;
903 u16 vlan_hash_table = 0;
904
905 /* Generate the VLAN Hash Table value */
906 for_each_set_bit(vid, pdata->active_vlans, VLAN_N_VID) {
907 /* Get the CRC32 value of the VLAN ID */
908 vid_le = cpu_to_le16(vid);
909 crc = bitrev32(~xgbe_vid_crc32_le(vid_le)) >> 28;
910
911 vlan_hash_table |= (1 << crc);
912 }
913
914 /* Set the VLAN Hash Table filtering register */
915 XGMAC_IOWRITE_BITS(pdata, MAC_VLANHTR, VLHT, vlan_hash_table);
916
917 return 0;
918}
919
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500920static int xgbe_set_promiscuous_mode(struct xgbe_prv_data *pdata,
921 unsigned int enable)
922{
923 unsigned int val = enable ? 1 : 0;
924
925 if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PR) == val)
926 return 0;
927
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500928 netif_dbg(pdata, drv, pdata->netdev, "%s promiscuous mode\n",
929 enable ? "entering" : "leaving");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500930 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, val);
931
Lendacky, Thomasb4eee842016-02-17 11:48:08 -0600932 /* Hardware will still perform VLAN filtering in promiscuous mode */
933 if (enable) {
934 xgbe_disable_rx_vlan_filtering(pdata);
935 } else {
936 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
937 xgbe_enable_rx_vlan_filtering(pdata);
938 }
939
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500940 return 0;
941}
942
943static int xgbe_set_all_multicast_mode(struct xgbe_prv_data *pdata,
944 unsigned int enable)
945{
946 unsigned int val = enable ? 1 : 0;
947
948 if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PM) == val)
949 return 0;
950
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500951 netif_dbg(pdata, drv, pdata->netdev, "%s allmulti mode\n",
952 enable ? "entering" : "leaving");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500953 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PM, val);
954
955 return 0;
956}
957
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500958static void xgbe_set_mac_reg(struct xgbe_prv_data *pdata,
959 struct netdev_hw_addr *ha, unsigned int *mac_reg)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500960{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500961 unsigned int mac_addr_hi, mac_addr_lo;
962 u8 *mac_addr;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500963
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500964 mac_addr_lo = 0;
965 mac_addr_hi = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500966
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500967 if (ha) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500968 mac_addr = (u8 *)&mac_addr_lo;
969 mac_addr[0] = ha->addr[0];
970 mac_addr[1] = ha->addr[1];
971 mac_addr[2] = ha->addr[2];
972 mac_addr[3] = ha->addr[3];
973 mac_addr = (u8 *)&mac_addr_hi;
974 mac_addr[0] = ha->addr[4];
975 mac_addr[1] = ha->addr[5];
976
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500977 netif_dbg(pdata, drv, pdata->netdev,
978 "adding mac address %pM at %#x\n",
979 ha->addr, *mac_reg);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500980
981 XGMAC_SET_BITS(mac_addr_hi, MAC_MACA1HR, AE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500982 }
983
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500984 XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_hi);
985 *mac_reg += MAC_MACA_INC;
986 XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_lo);
987 *mac_reg += MAC_MACA_INC;
988}
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500989
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500990static void xgbe_set_mac_addn_addrs(struct xgbe_prv_data *pdata)
991{
992 struct net_device *netdev = pdata->netdev;
993 struct netdev_hw_addr *ha;
994 unsigned int mac_reg;
995 unsigned int addn_macs;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500996
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500997 mac_reg = MAC_MACA1HR;
998 addn_macs = pdata->hw_feat.addn_mac;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500999
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05001000 if (netdev_uc_count(netdev) > addn_macs) {
1001 xgbe_set_promiscuous_mode(pdata, 1);
1002 } else {
1003 netdev_for_each_uc_addr(ha, netdev) {
1004 xgbe_set_mac_reg(pdata, ha, &mac_reg);
1005 addn_macs--;
1006 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001007
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05001008 if (netdev_mc_count(netdev) > addn_macs) {
1009 xgbe_set_all_multicast_mode(pdata, 1);
1010 } else {
1011 netdev_for_each_mc_addr(ha, netdev) {
1012 xgbe_set_mac_reg(pdata, ha, &mac_reg);
1013 addn_macs--;
1014 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001015 }
1016 }
1017
1018 /* Clear remaining additional MAC address entries */
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05001019 while (addn_macs--)
1020 xgbe_set_mac_reg(pdata, NULL, &mac_reg);
1021}
1022
1023static void xgbe_set_mac_hash_table(struct xgbe_prv_data *pdata)
1024{
1025 struct net_device *netdev = pdata->netdev;
1026 struct netdev_hw_addr *ha;
1027 unsigned int hash_reg;
1028 unsigned int hash_table_shift, hash_table_count;
1029 u32 hash_table[XGBE_MAC_HASH_TABLE_SIZE];
1030 u32 crc;
1031 unsigned int i;
1032
1033 hash_table_shift = 26 - (pdata->hw_feat.hash_table_size >> 7);
1034 hash_table_count = pdata->hw_feat.hash_table_size / 32;
1035 memset(hash_table, 0, sizeof(hash_table));
1036
1037 /* Build the MAC Hash Table register values */
1038 netdev_for_each_uc_addr(ha, netdev) {
1039 crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
1040 crc >>= hash_table_shift;
1041 hash_table[crc >> 5] |= (1 << (crc & 0x1f));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001042 }
1043
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05001044 netdev_for_each_mc_addr(ha, netdev) {
1045 crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
1046 crc >>= hash_table_shift;
1047 hash_table[crc >> 5] |= (1 << (crc & 0x1f));
1048 }
1049
1050 /* Set the MAC Hash Table registers */
1051 hash_reg = MAC_HTR0;
1052 for (i = 0; i < hash_table_count; i++) {
1053 XGMAC_IOWRITE(pdata, hash_reg, hash_table[i]);
1054 hash_reg += MAC_HTR_INC;
1055 }
1056}
1057
1058static int xgbe_add_mac_addresses(struct xgbe_prv_data *pdata)
1059{
1060 if (pdata->hw_feat.hash_table_size)
1061 xgbe_set_mac_hash_table(pdata);
1062 else
1063 xgbe_set_mac_addn_addrs(pdata);
1064
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001065 return 0;
1066}
1067
1068static int xgbe_set_mac_address(struct xgbe_prv_data *pdata, u8 *addr)
1069{
1070 unsigned int mac_addr_hi, mac_addr_lo;
1071
1072 mac_addr_hi = (addr[5] << 8) | (addr[4] << 0);
1073 mac_addr_lo = (addr[3] << 24) | (addr[2] << 16) |
1074 (addr[1] << 8) | (addr[0] << 0);
1075
1076 XGMAC_IOWRITE(pdata, MAC_MACA0HR, mac_addr_hi);
1077 XGMAC_IOWRITE(pdata, MAC_MACA0LR, mac_addr_lo);
1078
1079 return 0;
1080}
1081
Lendacky, Thomasb8763822015-04-09 12:11:57 -05001082static int xgbe_config_rx_mode(struct xgbe_prv_data *pdata)
1083{
1084 struct net_device *netdev = pdata->netdev;
1085 unsigned int pr_mode, am_mode;
1086
1087 pr_mode = ((netdev->flags & IFF_PROMISC) != 0);
1088 am_mode = ((netdev->flags & IFF_ALLMULTI) != 0);
1089
1090 xgbe_set_promiscuous_mode(pdata, pr_mode);
1091 xgbe_set_all_multicast_mode(pdata, am_mode);
1092
1093 xgbe_add_mac_addresses(pdata);
1094
1095 return 0;
1096}
1097
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001098static int xgbe_clr_gpio(struct xgbe_prv_data *pdata, unsigned int gpio)
1099{
1100 unsigned int reg;
1101
Lendacky, Thomas1c1f6192016-11-15 16:11:15 -06001102 if (gpio > 15)
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001103 return -EINVAL;
1104
1105 reg = XGMAC_IOREAD(pdata, MAC_GPIOSR);
1106
1107 reg &= ~(1 << (gpio + 16));
1108 XGMAC_IOWRITE(pdata, MAC_GPIOSR, reg);
1109
1110 return 0;
1111}
1112
1113static int xgbe_set_gpio(struct xgbe_prv_data *pdata, unsigned int gpio)
1114{
1115 unsigned int reg;
1116
Lendacky, Thomas1c1f6192016-11-15 16:11:15 -06001117 if (gpio > 15)
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001118 return -EINVAL;
1119
1120 reg = XGMAC_IOREAD(pdata, MAC_GPIOSR);
1121
1122 reg |= (1 << (gpio + 16));
1123 XGMAC_IOWRITE(pdata, MAC_GPIOSR, reg);
1124
1125 return 0;
1126}
1127
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001128static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
1129 int mmd_reg)
1130{
1131 unsigned long flags;
1132 unsigned int mmd_address, index, offset;
1133 int mmd_data;
1134
1135 if (mmd_reg & MII_ADDR_C45)
1136 mmd_address = mmd_reg & ~MII_ADDR_C45;
1137 else
1138 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1139
1140 /* The PCS registers are accessed using mmio. The underlying
1141 * management interface uses indirect addressing to access the MMD
1142 * register sets. This requires accessing of the PCS register in two
1143 * phases, an address phase and a data phase.
1144 *
1145 * The mmio interface is based on 16-bit offsets and values. All
1146 * register offsets must therefore be adjusted by left shifting the
1147 * offset 1 bit and reading 16 bits of data.
1148 */
1149 mmd_address <<= 1;
1150 index = mmd_address & ~pdata->xpcs_window_mask;
1151 offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
1152
1153 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomas4eccbfc2017-01-20 12:14:03 -06001154 XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001155 mmd_data = XPCS16_IOREAD(pdata, offset);
1156 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
1157
1158 return mmd_data;
1159}
1160
1161static void xgbe_write_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
1162 int mmd_reg, int mmd_data)
1163{
1164 unsigned long flags;
1165 unsigned int mmd_address, index, offset;
1166
1167 if (mmd_reg & MII_ADDR_C45)
1168 mmd_address = mmd_reg & ~MII_ADDR_C45;
1169 else
1170 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1171
1172 /* The PCS registers are accessed using mmio. The underlying
1173 * management interface uses indirect addressing to access the MMD
1174 * register sets. This requires accessing of the PCS register in two
1175 * phases, an address phase and a data phase.
1176 *
1177 * The mmio interface is based on 16-bit offsets and values. All
1178 * register offsets must therefore be adjusted by left shifting the
1179 * offset 1 bit and writing 16 bits of data.
1180 */
1181 mmd_address <<= 1;
1182 index = mmd_address & ~pdata->xpcs_window_mask;
1183 offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
1184
1185 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomas4eccbfc2017-01-20 12:14:03 -06001186 XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001187 XPCS16_IOWRITE(pdata, offset, mmd_data);
1188 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
1189}
1190
1191static int xgbe_read_mmd_regs_v1(struct xgbe_prv_data *pdata, int prtad,
1192 int mmd_reg)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001193{
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001194 unsigned long flags;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001195 unsigned int mmd_address;
1196 int mmd_data;
1197
1198 if (mmd_reg & MII_ADDR_C45)
1199 mmd_address = mmd_reg & ~MII_ADDR_C45;
1200 else
1201 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1202
1203 /* The PCS registers are accessed using mmio. The underlying APB3
1204 * management interface uses indirect addressing to access the MMD
1205 * register sets. This requires accessing of the PCS register in two
1206 * phases, an address phase and a data phase.
1207 *
1208 * The mmio interface is based on 32-bit offsets and values. All
1209 * register offsets must therefore be adjusted by left shifting the
1210 * offset 2 bits and reading 32 bits of data.
1211 */
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001212 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001213 XPCS32_IOWRITE(pdata, PCS_V1_WINDOW_SELECT, mmd_address >> 8);
1214 mmd_data = XPCS32_IOREAD(pdata, (mmd_address & 0xff) << 2);
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001215 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001216
1217 return mmd_data;
1218}
1219
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001220static void xgbe_write_mmd_regs_v1(struct xgbe_prv_data *pdata, int prtad,
1221 int mmd_reg, int mmd_data)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001222{
1223 unsigned int mmd_address;
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001224 unsigned long flags;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001225
1226 if (mmd_reg & MII_ADDR_C45)
1227 mmd_address = mmd_reg & ~MII_ADDR_C45;
1228 else
1229 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1230
1231 /* The PCS registers are accessed using mmio. The underlying APB3
1232 * management interface uses indirect addressing to access the MMD
1233 * register sets. This requires accessing of the PCS register in two
1234 * phases, an address phase and a data phase.
1235 *
1236 * The mmio interface is based on 32-bit offsets and values. All
1237 * register offsets must therefore be adjusted by left shifting the
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001238 * offset 2 bits and writing 32 bits of data.
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001239 */
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001240 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001241 XPCS32_IOWRITE(pdata, PCS_V1_WINDOW_SELECT, mmd_address >> 8);
1242 XPCS32_IOWRITE(pdata, (mmd_address & 0xff) << 2, mmd_data);
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001243 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001244}
1245
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001246static int xgbe_read_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
1247 int mmd_reg)
1248{
1249 switch (pdata->vdata->xpcs_access) {
1250 case XGBE_XPCS_ACCESS_V1:
1251 return xgbe_read_mmd_regs_v1(pdata, prtad, mmd_reg);
1252
1253 case XGBE_XPCS_ACCESS_V2:
1254 default:
1255 return xgbe_read_mmd_regs_v2(pdata, prtad, mmd_reg);
1256 }
1257}
1258
1259static void xgbe_write_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
1260 int mmd_reg, int mmd_data)
1261{
1262 switch (pdata->vdata->xpcs_access) {
1263 case XGBE_XPCS_ACCESS_V1:
1264 return xgbe_write_mmd_regs_v1(pdata, prtad, mmd_reg, mmd_data);
1265
1266 case XGBE_XPCS_ACCESS_V2:
1267 default:
1268 return xgbe_write_mmd_regs_v2(pdata, prtad, mmd_reg, mmd_data);
1269 }
1270}
1271
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001272static int xgbe_write_ext_mii_regs(struct xgbe_prv_data *pdata, int addr,
1273 int reg, u16 val)
1274{
1275 unsigned int mdio_sca, mdio_sccd;
1276
1277 reinit_completion(&pdata->mdio_complete);
1278
1279 mdio_sca = 0;
1280 XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, REG, reg);
1281 XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, DA, addr);
1282 XGMAC_IOWRITE(pdata, MAC_MDIOSCAR, mdio_sca);
1283
1284 mdio_sccd = 0;
1285 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, DATA, val);
1286 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, CMD, 1);
1287 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, BUSY, 1);
1288 XGMAC_IOWRITE(pdata, MAC_MDIOSCCDR, mdio_sccd);
1289
1290 if (!wait_for_completion_timeout(&pdata->mdio_complete, HZ)) {
1291 netdev_err(pdata->netdev, "mdio write operation timed out\n");
1292 return -ETIMEDOUT;
1293 }
1294
1295 return 0;
1296}
1297
1298static int xgbe_read_ext_mii_regs(struct xgbe_prv_data *pdata, int addr,
1299 int reg)
1300{
1301 unsigned int mdio_sca, mdio_sccd;
1302
1303 reinit_completion(&pdata->mdio_complete);
1304
1305 mdio_sca = 0;
1306 XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, REG, reg);
1307 XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, DA, addr);
1308 XGMAC_IOWRITE(pdata, MAC_MDIOSCAR, mdio_sca);
1309
1310 mdio_sccd = 0;
1311 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, CMD, 3);
1312 XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, BUSY, 1);
1313 XGMAC_IOWRITE(pdata, MAC_MDIOSCCDR, mdio_sccd);
1314
1315 if (!wait_for_completion_timeout(&pdata->mdio_complete, HZ)) {
1316 netdev_err(pdata->netdev, "mdio read operation timed out\n");
1317 return -ETIMEDOUT;
1318 }
1319
1320 return XGMAC_IOREAD_BITS(pdata, MAC_MDIOSCCDR, DATA);
1321}
1322
1323static int xgbe_set_ext_mii_mode(struct xgbe_prv_data *pdata, unsigned int port,
1324 enum xgbe_mdio_mode mode)
1325{
Lendacky, Thomasb42c6762017-02-28 15:03:01 -06001326 unsigned int reg_val = XGMAC_IOREAD(pdata, MAC_MDIOCL22R);
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06001327
1328 switch (mode) {
1329 case XGBE_MDIO_MODE_CL22:
1330 if (port > XGMAC_MAX_C22_PORT)
1331 return -EINVAL;
1332 reg_val |= (1 << port);
1333 break;
1334 case XGBE_MDIO_MODE_CL45:
1335 break;
1336 default:
1337 return -EINVAL;
1338 }
1339
1340 XGMAC_IOWRITE(pdata, MAC_MDIOCL22R, reg_val);
1341
1342 return 0;
1343}
1344
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001345static int xgbe_tx_complete(struct xgbe_ring_desc *rdesc)
1346{
1347 return !XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN);
1348}
1349
1350static int xgbe_disable_rx_csum(struct xgbe_prv_data *pdata)
1351{
1352 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 0);
1353
1354 return 0;
1355}
1356
1357static int xgbe_enable_rx_csum(struct xgbe_prv_data *pdata)
1358{
1359 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 1);
1360
1361 return 0;
1362}
1363
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001364static void xgbe_tx_desc_reset(struct xgbe_ring_data *rdata)
1365{
1366 struct xgbe_ring_desc *rdesc = rdata->rdesc;
1367
1368 /* Reset the Tx descriptor
1369 * Set buffer 1 (lo) address to zero
1370 * Set buffer 1 (hi) address to zero
1371 * Reset all other control bits (IC, TTSE, B2L & B1L)
1372 * Reset all other control bits (OWN, CTXT, FD, LD, CPC, CIC, etc)
1373 */
1374 rdesc->desc0 = 0;
1375 rdesc->desc1 = 0;
1376 rdesc->desc2 = 0;
1377 rdesc->desc3 = 0;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001378
1379 /* Make sure ownership is written to the descriptor */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001380 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001381}
1382
1383static void xgbe_tx_desc_init(struct xgbe_channel *channel)
1384{
1385 struct xgbe_ring *ring = channel->tx_ring;
1386 struct xgbe_ring_data *rdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001387 int i;
1388 int start_index = ring->cur;
1389
1390 DBGPR("-->tx_desc_init\n");
1391
1392 /* Initialze all descriptors */
1393 for (i = 0; i < ring->rdesc_count; i++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001394 rdata = XGBE_GET_DESC_DATA(ring, i);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001395
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001396 /* Initialize Tx descriptor */
1397 xgbe_tx_desc_reset(rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001398 }
1399
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001400 /* Update the total number of Tx descriptors */
1401 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDRLR, ring->rdesc_count - 1);
1402
1403 /* Update the starting address of descriptor ring */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001404 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001405 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_HI,
1406 upper_32_bits(rdata->rdesc_dma));
1407 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_LO,
1408 lower_32_bits(rdata->rdesc_dma));
1409
1410 DBGPR("<--tx_desc_init\n");
1411}
1412
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001413static void xgbe_rx_desc_reset(struct xgbe_prv_data *pdata,
1414 struct xgbe_ring_data *rdata, unsigned int index)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001415{
1416 struct xgbe_ring_desc *rdesc = rdata->rdesc;
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001417 unsigned int rx_usecs = pdata->rx_usecs;
1418 unsigned int rx_frames = pdata->rx_frames;
1419 unsigned int inte;
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001420 dma_addr_t hdr_dma, buf_dma;
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001421
1422 if (!rx_usecs && !rx_frames) {
1423 /* No coalescing, interrupt for every descriptor */
1424 inte = 1;
1425 } else {
1426 /* Set interrupt based on Rx frame coalescing setting */
1427 if (rx_frames && !((index + 1) % rx_frames))
1428 inte = 1;
1429 else
1430 inte = 0;
1431 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001432
1433 /* Reset the Rx descriptor
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001434 * Set buffer 1 (lo) address to header dma address (lo)
1435 * Set buffer 1 (hi) address to header dma address (hi)
1436 * Set buffer 2 (lo) address to buffer dma address (lo)
1437 * Set buffer 2 (hi) address to buffer dma address (hi) and
1438 * set control bits OWN and INTE
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001439 */
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001440 hdr_dma = rdata->rx.hdr.dma_base + rdata->rx.hdr.dma_off;
1441 buf_dma = rdata->rx.buf.dma_base + rdata->rx.buf.dma_off;
1442 rdesc->desc0 = cpu_to_le32(lower_32_bits(hdr_dma));
1443 rdesc->desc1 = cpu_to_le32(upper_32_bits(hdr_dma));
1444 rdesc->desc2 = cpu_to_le32(lower_32_bits(buf_dma));
1445 rdesc->desc3 = cpu_to_le32(upper_32_bits(buf_dma));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001446
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001447 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, INTE, inte);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001448
1449 /* Since the Rx DMA engine is likely running, make sure everything
1450 * is written to the descriptor(s) before setting the OWN bit
1451 * for the descriptor
1452 */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001453 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001454
1455 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN, 1);
1456
1457 /* Make sure ownership is written to the descriptor */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001458 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001459}
1460
1461static void xgbe_rx_desc_init(struct xgbe_channel *channel)
1462{
1463 struct xgbe_prv_data *pdata = channel->pdata;
1464 struct xgbe_ring *ring = channel->rx_ring;
1465 struct xgbe_ring_data *rdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001466 unsigned int start_index = ring->cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001467 unsigned int i;
1468
1469 DBGPR("-->rx_desc_init\n");
1470
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001471 /* Initialize all descriptors */
1472 for (i = 0; i < ring->rdesc_count; i++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001473 rdata = XGBE_GET_DESC_DATA(ring, i);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001474
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001475 /* Initialize Rx descriptor */
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001476 xgbe_rx_desc_reset(pdata, rdata, i);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001477 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001478
1479 /* Update the total number of Rx descriptors */
1480 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDRLR, ring->rdesc_count - 1);
1481
1482 /* Update the starting address of descriptor ring */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001483 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001484 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_HI,
1485 upper_32_bits(rdata->rdesc_dma));
1486 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_LO,
1487 lower_32_bits(rdata->rdesc_dma));
1488
1489 /* Update the Rx Descriptor Tail Pointer */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001490 rdata = XGBE_GET_DESC_DATA(ring, start_index + ring->rdesc_count - 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001491 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
1492 lower_32_bits(rdata->rdesc_dma));
1493
1494 DBGPR("<--rx_desc_init\n");
1495}
1496
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001497static void xgbe_update_tstamp_addend(struct xgbe_prv_data *pdata,
1498 unsigned int addend)
1499{
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001500 unsigned int count = 10000;
1501
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001502 /* Set the addend register value and tell the device */
1503 XGMAC_IOWRITE(pdata, MAC_TSAR, addend);
1504 XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSADDREG, 1);
1505
1506 /* Wait for addend update to complete */
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001507 while (--count && XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSADDREG))
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001508 udelay(5);
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001509
1510 if (!count)
1511 netdev_err(pdata->netdev,
1512 "timed out updating timestamp addend register\n");
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001513}
1514
1515static void xgbe_set_tstamp_time(struct xgbe_prv_data *pdata, unsigned int sec,
1516 unsigned int nsec)
1517{
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001518 unsigned int count = 10000;
1519
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001520 /* Set the time values and tell the device */
1521 XGMAC_IOWRITE(pdata, MAC_STSUR, sec);
1522 XGMAC_IOWRITE(pdata, MAC_STNUR, nsec);
1523 XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSINIT, 1);
1524
1525 /* Wait for time update to complete */
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001526 while (--count && XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSINIT))
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001527 udelay(5);
Lendacky, Thomas9018ff52017-06-28 13:42:07 -05001528
1529 if (!count)
1530 netdev_err(pdata->netdev, "timed out initializing timestamp\n");
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001531}
1532
1533static u64 xgbe_get_tstamp_time(struct xgbe_prv_data *pdata)
1534{
1535 u64 nsec;
1536
1537 nsec = XGMAC_IOREAD(pdata, MAC_STSR);
1538 nsec *= NSEC_PER_SEC;
1539 nsec += XGMAC_IOREAD(pdata, MAC_STNR);
1540
1541 return nsec;
1542}
1543
1544static u64 xgbe_get_tx_tstamp(struct xgbe_prv_data *pdata)
1545{
Lendacky, Thomasaba97772016-11-10 17:09:45 -06001546 unsigned int tx_snr, tx_ssr;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001547 u64 nsec;
1548
Lendacky, Thomasaba97772016-11-10 17:09:45 -06001549 if (pdata->vdata->tx_tstamp_workaround) {
1550 tx_snr = XGMAC_IOREAD(pdata, MAC_TXSNR);
1551 tx_ssr = XGMAC_IOREAD(pdata, MAC_TXSSR);
1552 } else {
1553 tx_ssr = XGMAC_IOREAD(pdata, MAC_TXSSR);
1554 tx_snr = XGMAC_IOREAD(pdata, MAC_TXSNR);
1555 }
1556
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001557 if (XGMAC_GET_BITS(tx_snr, MAC_TXSNR, TXTSSTSMIS))
1558 return 0;
1559
Lendacky, Thomasaba97772016-11-10 17:09:45 -06001560 nsec = tx_ssr;
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001561 nsec *= NSEC_PER_SEC;
1562 nsec += tx_snr;
1563
1564 return nsec;
1565}
1566
1567static void xgbe_get_rx_tstamp(struct xgbe_packet_data *packet,
1568 struct xgbe_ring_desc *rdesc)
1569{
1570 u64 nsec;
1571
1572 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSA) &&
1573 !XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSD)) {
1574 nsec = le32_to_cpu(rdesc->desc1);
1575 nsec <<= 32;
1576 nsec |= le32_to_cpu(rdesc->desc0);
1577 if (nsec != 0xffffffffffffffffULL) {
1578 packet->rx_tstamp = nsec;
1579 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1580 RX_TSTAMP, 1);
1581 }
1582 }
1583}
1584
1585static int xgbe_config_tstamp(struct xgbe_prv_data *pdata,
1586 unsigned int mac_tscr)
1587{
1588 /* Set one nano-second accuracy */
1589 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCTRLSSR, 1);
1590
1591 /* Set fine timestamp update */
1592 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCFUPDT, 1);
1593
1594 /* Overwrite earlier timestamps */
1595 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TXTSSTSM, 1);
1596
1597 XGMAC_IOWRITE(pdata, MAC_TSCR, mac_tscr);
1598
1599 /* Exit if timestamping is not enabled */
1600 if (!XGMAC_GET_BITS(mac_tscr, MAC_TSCR, TSENA))
1601 return 0;
1602
1603 /* Initialize time registers */
1604 XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SSINC, XGBE_TSTAMP_SSINC);
1605 XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SNSINC, XGBE_TSTAMP_SNSINC);
1606 xgbe_update_tstamp_addend(pdata, pdata->tstamp_addend);
1607 xgbe_set_tstamp_time(pdata, 0, 0);
1608
1609 /* Initialize the timecounter */
1610 timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc,
1611 ktime_to_ns(ktime_get_real()));
1612
1613 return 0;
1614}
1615
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001616static void xgbe_tx_start_xmit(struct xgbe_channel *channel,
1617 struct xgbe_ring *ring)
1618{
1619 struct xgbe_prv_data *pdata = channel->pdata;
1620 struct xgbe_ring_data *rdata;
1621
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001622 /* Make sure everything is written before the register write */
1623 wmb();
1624
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001625 /* Issue a poll command to Tx DMA by writing address
1626 * of next immediate free descriptor */
1627 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1628 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDTR_LO,
1629 lower_32_bits(rdata->rdesc_dma));
1630
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -05001631 /* Start the Tx timer */
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001632 if (pdata->tx_usecs && !channel->tx_timer_active) {
1633 channel->tx_timer_active = 1;
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -05001634 mod_timer(&channel->tx_timer,
1635 jiffies + usecs_to_jiffies(pdata->tx_usecs));
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001636 }
1637
1638 ring->tx.xmit_more = 0;
1639}
1640
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001641static void xgbe_dev_xmit(struct xgbe_channel *channel)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001642{
1643 struct xgbe_prv_data *pdata = channel->pdata;
1644 struct xgbe_ring *ring = channel->tx_ring;
1645 struct xgbe_ring_data *rdata;
1646 struct xgbe_ring_desc *rdesc;
1647 struct xgbe_packet_data *packet = &ring->packet_data;
1648 unsigned int csum, tso, vlan;
1649 unsigned int tso_context, vlan_context;
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001650 unsigned int tx_set_ic;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001651 int start_index = ring->cur;
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001652 int cur_index = ring->cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001653 int i;
1654
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001655 DBGPR("-->xgbe_dev_xmit\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001656
1657 csum = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1658 CSUM_ENABLE);
1659 tso = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1660 TSO_ENABLE);
1661 vlan = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1662 VLAN_CTAG);
1663
1664 if (tso && (packet->mss != ring->tx.cur_mss))
1665 tso_context = 1;
1666 else
1667 tso_context = 0;
1668
1669 if (vlan && (packet->vlan_ctag != ring->tx.cur_vlan_ctag))
1670 vlan_context = 1;
1671 else
1672 vlan_context = 0;
1673
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001674 /* Determine if an interrupt should be generated for this Tx:
1675 * Interrupt:
1676 * - Tx frame count exceeds the frame count setting
1677 * - Addition of Tx frame count to the frame count since the
1678 * last interrupt was set exceeds the frame count setting
1679 * No interrupt:
1680 * - No frame count setting specified (ethtool -C ethX tx-frames 0)
1681 * - Addition of Tx frame count to the frame count since the
1682 * last interrupt was set does not exceed the frame count setting
1683 */
1684 ring->coalesce_count += packet->tx_packets;
1685 if (!pdata->tx_frames)
1686 tx_set_ic = 0;
1687 else if (packet->tx_packets > pdata->tx_frames)
1688 tx_set_ic = 1;
1689 else if ((ring->coalesce_count % pdata->tx_frames) <
1690 packet->tx_packets)
1691 tx_set_ic = 1;
1692 else
1693 tx_set_ic = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001694
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001695 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001696 rdesc = rdata->rdesc;
1697
1698 /* Create a context descriptor if this is a TSO packet */
1699 if (tso_context || vlan_context) {
1700 if (tso_context) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001701 netif_dbg(pdata, tx_queued, pdata->netdev,
1702 "TSO context descriptor, mss=%u\n",
1703 packet->mss);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001704
1705 /* Set the MSS size */
1706 XGMAC_SET_BITS_LE(rdesc->desc2, TX_CONTEXT_DESC2,
1707 MSS, packet->mss);
1708
1709 /* Mark it as a CONTEXT descriptor */
1710 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1711 CTXT, 1);
1712
1713 /* Indicate this descriptor contains the MSS */
1714 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1715 TCMSSV, 1);
1716
1717 ring->tx.cur_mss = packet->mss;
1718 }
1719
1720 if (vlan_context) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001721 netif_dbg(pdata, tx_queued, pdata->netdev,
1722 "VLAN context descriptor, ctag=%u\n",
1723 packet->vlan_ctag);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001724
1725 /* Mark it as a CONTEXT descriptor */
1726 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1727 CTXT, 1);
1728
1729 /* Set the VLAN tag */
1730 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1731 VT, packet->vlan_ctag);
1732
1733 /* Indicate this descriptor contains the VLAN tag */
1734 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1735 VLTV, 1);
1736
1737 ring->tx.cur_vlan_ctag = packet->vlan_ctag;
1738 }
1739
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001740 cur_index++;
1741 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001742 rdesc = rdata->rdesc;
1743 }
1744
1745 /* Update buffer address (for TSO this is the header) */
1746 rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
1747 rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
1748
1749 /* Update the buffer length */
1750 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1751 rdata->skb_dma_len);
1752
1753 /* VLAN tag insertion check */
1754 if (vlan)
1755 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, VTIR,
1756 TX_NORMAL_DESC2_VLAN_INSERT);
1757
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001758 /* Timestamp enablement check */
1759 if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
1760 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, TTSE, 1);
1761
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001762 /* Mark it as First Descriptor */
1763 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FD, 1);
1764
1765 /* Mark it as a NORMAL descriptor */
1766 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1767
1768 /* Set OWN bit if not the first descriptor */
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001769 if (cur_index != start_index)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001770 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1771
1772 if (tso) {
1773 /* Enable TSO */
1774 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TSE, 1);
1775 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPPL,
1776 packet->tcp_payload_len);
1777 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPHDRLEN,
1778 packet->tcp_header_len / 4);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001779
1780 pdata->ext_stats.tx_tso_packets++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001781 } else {
1782 /* Enable CRC and Pad Insertion */
1783 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CPC, 0);
1784
1785 /* Enable HW CSUM */
1786 if (csum)
1787 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1788 CIC, 0x3);
1789
1790 /* Set the total length to be transmitted */
1791 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FL,
1792 packet->length);
1793 }
1794
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001795 for (i = cur_index - start_index + 1; i < packet->rdesc_count; i++) {
1796 cur_index++;
1797 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001798 rdesc = rdata->rdesc;
1799
1800 /* Update buffer address */
1801 rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
1802 rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
1803
1804 /* Update the buffer length */
1805 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1806 rdata->skb_dma_len);
1807
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001808 /* Set OWN bit */
1809 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1810
1811 /* Mark it as NORMAL descriptor */
1812 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1813
1814 /* Enable HW CSUM */
1815 if (csum)
1816 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1817 CIC, 0x3);
1818 }
1819
1820 /* Set LAST bit for the last descriptor */
1821 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD, 1);
1822
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001823 /* Set IC bit based on Tx coalescing settings */
1824 if (tx_set_ic)
1825 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, IC, 1);
1826
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001827 /* Save the Tx info to report back during cleanup */
1828 rdata->tx.packets = packet->tx_packets;
1829 rdata->tx.bytes = packet->tx_bytes;
1830
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001831 /* In case the Tx DMA engine is running, make sure everything
1832 * is written to the descriptor(s) before setting the OWN bit
1833 * for the first descriptor
1834 */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001835 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001836
1837 /* Set OWN bit for the first descriptor */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001838 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001839 rdesc = rdata->rdesc;
1840 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1841
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001842 if (netif_msg_tx_queued(pdata))
1843 xgbe_dump_tx_desc(pdata, ring, start_index,
1844 packet->rdesc_count, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001845
1846 /* Make sure ownership is written to the descriptor */
Lendacky, Thomas20986ed2015-10-26 17:13:54 -05001847 smp_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001848
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001849 ring->cur = cur_index + 1;
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001850 if (!packet->skb->xmit_more ||
1851 netif_xmit_stopped(netdev_get_tx_queue(pdata->netdev,
1852 channel->queue_index)))
1853 xgbe_tx_start_xmit(channel, ring);
1854 else
1855 ring->tx.xmit_more = 1;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001856
1857 DBGPR(" %s: descriptors %u to %u written\n",
1858 channel->name, start_index & (ring->rdesc_count - 1),
1859 (ring->cur - 1) & (ring->rdesc_count - 1));
1860
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001861 DBGPR("<--xgbe_dev_xmit\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001862}
1863
1864static int xgbe_dev_read(struct xgbe_channel *channel)
1865{
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001866 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001867 struct xgbe_ring *ring = channel->rx_ring;
1868 struct xgbe_ring_data *rdata;
1869 struct xgbe_ring_desc *rdesc;
1870 struct xgbe_packet_data *packet = &ring->packet_data;
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001871 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001872 unsigned int err, etlt, l34t;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001873
1874 DBGPR("-->xgbe_dev_read: cur = %d\n", ring->cur);
1875
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001876 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001877 rdesc = rdata->rdesc;
1878
1879 /* Check for data availability */
1880 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN))
1881 return 1;
1882
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001883 /* Make sure descriptor fields are read after reading the OWN bit */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001884 dma_rmb();
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001885
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001886 if (netif_msg_rx_status(pdata))
1887 xgbe_dump_rx_desc(pdata, ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001888
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001889 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CTXT)) {
1890 /* Timestamp Context Descriptor */
1891 xgbe_get_rx_tstamp(packet, rdesc);
1892
1893 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1894 CONTEXT, 1);
1895 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1896 CONTEXT_NEXT, 0);
1897 return 0;
1898 }
1899
1900 /* Normal Descriptor, be sure Context Descriptor bit is off */
1901 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, CONTEXT, 0);
1902
1903 /* Indicate if a Context Descriptor is next */
1904 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CDA))
1905 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1906 CONTEXT_NEXT, 1);
1907
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001908 /* Get the header length */
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001909 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, FD)) {
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001910 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1911 FIRST, 1);
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001912 rdata->rx.hdr_len = XGMAC_GET_BITS_LE(rdesc->desc2,
1913 RX_NORMAL_DESC2, HL);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001914 if (rdata->rx.hdr_len)
1915 pdata->ext_stats.rx_split_header_packets++;
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001916 } else {
1917 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1918 FIRST, 0);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001919 }
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001920
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001921 /* Get the RSS hash */
1922 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, RSV)) {
1923 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1924 RSS_HASH, 1);
1925
1926 packet->rss_hash = le32_to_cpu(rdesc->desc1);
1927
1928 l34t = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, L34T);
1929 switch (l34t) {
1930 case RX_DESC3_L34T_IPV4_TCP:
1931 case RX_DESC3_L34T_IPV4_UDP:
1932 case RX_DESC3_L34T_IPV6_TCP:
1933 case RX_DESC3_L34T_IPV6_UDP:
1934 packet->rss_hash_type = PKT_HASH_TYPE_L4;
Dan Carpenterb6267d32014-11-13 09:19:06 +03001935 break;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001936 default:
1937 packet->rss_hash_type = PKT_HASH_TYPE_L3;
1938 }
1939 }
1940
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001941 /* Not all the data has been transferred for this packet */
1942 if (!XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, LD))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001943 return 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001944
1945 /* This is the last of the data for this packet */
1946 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
Lendacky, Thomas622c36f2017-03-15 15:11:23 -05001947 LAST, 1);
1948
1949 /* Get the packet length */
1950 rdata->rx.len = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, PL);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001951
1952 /* Set checksum done indicator as appropriate */
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001953 if (netdev->features & NETIF_F_RXCSUM)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001954 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1955 CSUM_DONE, 1);
1956
1957 /* Check for errors (only valid in last descriptor) */
1958 err = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ES);
1959 etlt = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ETLT);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001960 netif_dbg(pdata, rx_status, netdev, "err=%u, etlt=%#x\n", err, etlt);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001961
Lendacky, Thomas7bba35b2014-11-20 11:03:38 -06001962 if (!err || !etlt) {
1963 /* No error if err is 0 or etlt is 0 */
Lendacky, Thomasc52e9c62014-06-24 16:19:18 -05001964 if ((etlt == 0x09) &&
1965 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001966 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1967 VLAN_CTAG, 1);
1968 packet->vlan_ctag = XGMAC_GET_BITS_LE(rdesc->desc0,
1969 RX_NORMAL_DESC0,
1970 OVT);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001971 netif_dbg(pdata, rx_status, netdev, "vlan-ctag=%#06x\n",
1972 packet->vlan_ctag);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001973 }
1974 } else {
1975 if ((etlt == 0x05) || (etlt == 0x06))
1976 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1977 CSUM_DONE, 0);
1978 else
1979 XGMAC_SET_BITS(packet->errors, RX_PACKET_ERRORS,
1980 FRAME, 1);
1981 }
1982
1983 DBGPR("<--xgbe_dev_read: %s - descriptor=%u (cur=%d)\n", channel->name,
1984 ring->cur & (ring->rdesc_count - 1), ring->cur);
1985
1986 return 0;
1987}
1988
1989static int xgbe_is_context_desc(struct xgbe_ring_desc *rdesc)
1990{
1991 /* Rx and Tx share CTXT bit, so check TDES3.CTXT bit */
1992 return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT);
1993}
1994
1995static int xgbe_is_last_desc(struct xgbe_ring_desc *rdesc)
1996{
1997 /* Rx and Tx share LD bit, so check TDES3.LD bit */
1998 return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD);
1999}
2000
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002001static int xgbe_enable_int(struct xgbe_channel *channel,
2002 enum xgbe_int int_id)
2003{
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002004 unsigned int dma_ch_ier;
2005
2006 dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
2007
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002008 switch (int_id) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002009 case XGMAC_INT_DMA_CH_SR_TI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002010 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002011 break;
2012 case XGMAC_INT_DMA_CH_SR_TPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002013 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002014 break;
2015 case XGMAC_INT_DMA_CH_SR_TBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002016 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002017 break;
2018 case XGMAC_INT_DMA_CH_SR_RI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002019 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002020 break;
2021 case XGMAC_INT_DMA_CH_SR_RBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002022 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002023 break;
2024 case XGMAC_INT_DMA_CH_SR_RPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002025 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 1);
2026 break;
2027 case XGMAC_INT_DMA_CH_SR_TI_RI:
2028 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
2029 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002030 break;
2031 case XGMAC_INT_DMA_CH_SR_FBE:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002032 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002033 break;
2034 case XGMAC_INT_DMA_ALL:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002035 dma_ch_ier |= channel->saved_ier;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002036 break;
2037 default:
2038 return -1;
2039 }
2040
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002041 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
2042
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002043 return 0;
2044}
2045
2046static int xgbe_disable_int(struct xgbe_channel *channel,
2047 enum xgbe_int int_id)
2048{
2049 unsigned int dma_ch_ier;
2050
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002051 dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
2052
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002053 switch (int_id) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002054 case XGMAC_INT_DMA_CH_SR_TI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002055 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002056 break;
2057 case XGMAC_INT_DMA_CH_SR_TPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002058 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002059 break;
2060 case XGMAC_INT_DMA_CH_SR_TBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002061 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002062 break;
2063 case XGMAC_INT_DMA_CH_SR_RI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002064 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002065 break;
2066 case XGMAC_INT_DMA_CH_SR_RBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002067 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002068 break;
2069 case XGMAC_INT_DMA_CH_SR_RPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002070 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 0);
2071 break;
2072 case XGMAC_INT_DMA_CH_SR_TI_RI:
2073 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
2074 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002075 break;
2076 case XGMAC_INT_DMA_CH_SR_FBE:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002077 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002078 break;
2079 case XGMAC_INT_DMA_ALL:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002080 channel->saved_ier = dma_ch_ier & XGBE_DMA_INTERRUPT_MASK;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05002081 dma_ch_ier &= ~XGBE_DMA_INTERRUPT_MASK;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002082 break;
2083 default:
2084 return -1;
2085 }
2086
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002087 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
2088
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002089 return 0;
2090}
2091
Lendacky, Thomas5ffc0332016-11-10 17:09:29 -06002092static int __xgbe_exit(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002093{
2094 unsigned int count = 2000;
2095
2096 DBGPR("-->xgbe_exit\n");
2097
2098 /* Issue a software reset */
2099 XGMAC_IOWRITE_BITS(pdata, DMA_MR, SWR, 1);
2100 usleep_range(10, 15);
2101
2102 /* Poll Until Poll Condition */
Dan Carpenterc7557e62015-12-15 13:12:29 +03002103 while (--count && XGMAC_IOREAD_BITS(pdata, DMA_MR, SWR))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002104 usleep_range(500, 600);
2105
2106 if (!count)
2107 return -EBUSY;
2108
2109 DBGPR("<--xgbe_exit\n");
2110
2111 return 0;
2112}
2113
Lendacky, Thomas5ffc0332016-11-10 17:09:29 -06002114static int xgbe_exit(struct xgbe_prv_data *pdata)
2115{
2116 int ret;
2117
2118 /* To guard against possible incorrectly generated interrupts,
2119 * issue the software reset twice.
2120 */
2121 ret = __xgbe_exit(pdata);
2122 if (ret)
2123 return ret;
2124
2125 return __xgbe_exit(pdata);
2126}
2127
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002128static int xgbe_flush_tx_queues(struct xgbe_prv_data *pdata)
2129{
2130 unsigned int i, count;
2131
Lendacky, Thomasa9a4a2d2014-08-29 13:16:50 -05002132 if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) < 0x21)
2133 return 0;
2134
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002135 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002136 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, FTQ, 1);
2137
2138 /* Poll Until Poll Condition */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002139 for (i = 0; i < pdata->tx_q_count; i++) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002140 count = 2000;
Dan Carpenterc7557e62015-12-15 13:12:29 +03002141 while (--count && XGMAC_MTL_IOREAD_BITS(pdata, i,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002142 MTL_Q_TQOMR, FTQ))
2143 usleep_range(500, 600);
2144
2145 if (!count)
2146 return -EBUSY;
2147 }
2148
2149 return 0;
2150}
2151
2152static void xgbe_config_dma_bus(struct xgbe_prv_data *pdata)
2153{
2154 /* Set enhanced addressing mode */
2155 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, EAME, 1);
2156
2157 /* Set the System Bus mode */
2158 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, UNDEF, 1);
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05002159 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, BLEN_256, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002160}
2161
2162static void xgbe_config_dma_cache(struct xgbe_prv_data *pdata)
2163{
2164 unsigned int arcache, awcache;
2165
2166 arcache = 0;
Lendacky, Thomascfa50c72014-07-02 13:04:57 -05002167 XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRC, pdata->arcache);
2168 XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRD, pdata->axdomain);
2169 XGMAC_SET_BITS(arcache, DMA_AXIARCR, TEC, pdata->arcache);
2170 XGMAC_SET_BITS(arcache, DMA_AXIARCR, TED, pdata->axdomain);
2171 XGMAC_SET_BITS(arcache, DMA_AXIARCR, THC, pdata->arcache);
2172 XGMAC_SET_BITS(arcache, DMA_AXIARCR, THD, pdata->axdomain);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002173 XGMAC_IOWRITE(pdata, DMA_AXIARCR, arcache);
2174
2175 awcache = 0;
Lendacky, Thomascfa50c72014-07-02 13:04:57 -05002176 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWC, pdata->awcache);
2177 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWD, pdata->axdomain);
2178 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPC, pdata->awcache);
2179 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPD, pdata->axdomain);
2180 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHC, pdata->awcache);
2181 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHD, pdata->axdomain);
2182 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDC, pdata->awcache);
2183 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDD, pdata->axdomain);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002184 XGMAC_IOWRITE(pdata, DMA_AXIAWCR, awcache);
2185}
2186
2187static void xgbe_config_mtl_mode(struct xgbe_prv_data *pdata)
2188{
2189 unsigned int i;
2190
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002191 /* Set Tx to weighted round robin scheduling algorithm */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002192 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_WRR);
2193
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002194 /* Set Tx traffic classes to use WRR algorithm with equal weights */
2195 for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
2196 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2197 MTL_TSA_ETS);
2198 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW, 1);
2199 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002200
2201 /* Set Rx to strict priority algorithm */
2202 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, RAA, MTL_RAA_SP);
2203}
2204
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002205static void xgbe_queue_flow_control_threshold(struct xgbe_prv_data *pdata,
2206 unsigned int queue,
2207 unsigned int q_fifo_size)
2208{
2209 unsigned int frame_fifo_size;
2210 unsigned int rfa, rfd;
2211
2212 frame_fifo_size = XGMAC_FLOW_CONTROL_ALIGN(xgbe_get_max_frame(pdata));
2213
2214 if (pdata->pfcq[queue] && (q_fifo_size > pdata->pfc_rfa)) {
2215 /* PFC is active for this queue */
2216 rfa = pdata->pfc_rfa;
2217 rfd = rfa + frame_fifo_size;
2218 if (rfd > XGMAC_FLOW_CONTROL_MAX)
2219 rfd = XGMAC_FLOW_CONTROL_MAX;
2220 if (rfa >= XGMAC_FLOW_CONTROL_MAX)
2221 rfa = XGMAC_FLOW_CONTROL_MAX - XGMAC_FLOW_CONTROL_UNIT;
2222 } else {
2223 /* This path deals with just maximum frame sizes which are
2224 * limited to a jumbo frame of 9,000 (plus headers, etc.)
2225 * so we can never exceed the maximum allowable RFA/RFD
2226 * values.
2227 */
2228 if (q_fifo_size <= 2048) {
2229 /* rx_rfd to zero to signal no flow control */
2230 pdata->rx_rfa[queue] = 0;
2231 pdata->rx_rfd[queue] = 0;
2232 return;
2233 }
2234
2235 if (q_fifo_size <= 4096) {
2236 /* Between 2048 and 4096 */
2237 pdata->rx_rfa[queue] = 0; /* Full - 1024 bytes */
2238 pdata->rx_rfd[queue] = 1; /* Full - 1536 bytes */
2239 return;
2240 }
2241
2242 if (q_fifo_size <= frame_fifo_size) {
2243 /* Between 4096 and max-frame */
2244 pdata->rx_rfa[queue] = 2; /* Full - 2048 bytes */
2245 pdata->rx_rfd[queue] = 5; /* Full - 3584 bytes */
2246 return;
2247 }
2248
2249 if (q_fifo_size <= (frame_fifo_size * 3)) {
2250 /* Between max-frame and 3 max-frames,
2251 * trigger if we get just over a frame of data and
2252 * resume when we have just under half a frame left.
2253 */
2254 rfa = q_fifo_size - frame_fifo_size;
2255 rfd = rfa + (frame_fifo_size / 2);
2256 } else {
2257 /* Above 3 max-frames - trigger when just over
2258 * 2 frames of space available
2259 */
2260 rfa = frame_fifo_size * 2;
2261 rfa += XGMAC_FLOW_CONTROL_UNIT;
2262 rfd = rfa + frame_fifo_size;
2263 }
2264 }
2265
2266 pdata->rx_rfa[queue] = XGMAC_FLOW_CONTROL_VALUE(rfa);
2267 pdata->rx_rfd[queue] = XGMAC_FLOW_CONTROL_VALUE(rfd);
2268}
2269
2270static void xgbe_calculate_flow_control_threshold(struct xgbe_prv_data *pdata,
2271 unsigned int *fifo)
2272{
2273 unsigned int q_fifo_size;
2274 unsigned int i;
2275
2276 for (i = 0; i < pdata->rx_q_count; i++) {
2277 q_fifo_size = (fifo[i] + 1) * XGMAC_FIFO_UNIT;
2278
2279 xgbe_queue_flow_control_threshold(pdata, i, q_fifo_size);
2280 }
2281}
2282
2283static void xgbe_config_flow_control_threshold(struct xgbe_prv_data *pdata)
2284{
2285 unsigned int i;
2286
2287 for (i = 0; i < pdata->rx_q_count; i++) {
2288 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFA,
2289 pdata->rx_rfa[i]);
2290 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFD,
2291 pdata->rx_rfd[i]);
2292 }
2293}
2294
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002295static unsigned int xgbe_get_tx_fifo_size(struct xgbe_prv_data *pdata)
2296{
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002297 /* The configured value may not be the actual amount of fifo RAM */
Lendacky, Thomasbd8255d2016-11-03 13:19:27 -05002298 return min_t(unsigned int, pdata->tx_max_fifo_size,
2299 pdata->hw_feat.tx_fifo_size);
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002300}
2301
2302static unsigned int xgbe_get_rx_fifo_size(struct xgbe_prv_data *pdata)
2303{
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002304 /* The configured value may not be the actual amount of fifo RAM */
Lendacky, Thomasbd8255d2016-11-03 13:19:27 -05002305 return min_t(unsigned int, pdata->rx_max_fifo_size,
2306 pdata->hw_feat.rx_fifo_size);
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002307}
2308
2309static void xgbe_calculate_equal_fifo(unsigned int fifo_size,
2310 unsigned int queue_count,
2311 unsigned int *fifo)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002312{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002313 unsigned int q_fifo_size;
2314 unsigned int p_fifo;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002315 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002316
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002317 q_fifo_size = fifo_size / queue_count;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002318
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002319 /* Calculate the fifo setting by dividing the queue's fifo size
2320 * by the fifo allocation increment (with 0 representing the
2321 * base allocation increment so decrement the result by 1).
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002322 */
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002323 p_fifo = q_fifo_size / XGMAC_FIFO_UNIT;
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002324 if (p_fifo)
2325 p_fifo--;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002326
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002327 /* Distribute the fifo equally amongst the queues */
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002328 for (i = 0; i < queue_count; i++)
2329 fifo[i] = p_fifo;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002330}
2331
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002332static unsigned int xgbe_set_nonprio_fifos(unsigned int fifo_size,
2333 unsigned int queue_count,
2334 unsigned int *fifo)
2335{
2336 unsigned int i;
2337
2338 BUILD_BUG_ON_NOT_POWER_OF_2(XGMAC_FIFO_MIN_ALLOC);
2339
2340 if (queue_count <= IEEE_8021QAZ_MAX_TCS)
2341 return fifo_size;
2342
2343 /* Rx queues 9 and up are for specialized packets,
2344 * such as PTP or DCB control packets, etc. and
2345 * don't require a large fifo
2346 */
2347 for (i = IEEE_8021QAZ_MAX_TCS; i < queue_count; i++) {
2348 fifo[i] = (XGMAC_FIFO_MIN_ALLOC / XGMAC_FIFO_UNIT) - 1;
2349 fifo_size -= XGMAC_FIFO_MIN_ALLOC;
2350 }
2351
2352 return fifo_size;
2353}
2354
2355static unsigned int xgbe_get_pfc_delay(struct xgbe_prv_data *pdata)
2356{
2357 unsigned int delay;
2358
2359 /* If a delay has been provided, use that */
2360 if (pdata->pfc->delay)
2361 return pdata->pfc->delay / 8;
2362
2363 /* Allow for two maximum size frames */
2364 delay = xgbe_get_max_frame(pdata);
2365 delay += XGMAC_ETH_PREAMBLE;
2366 delay *= 2;
2367
2368 /* Allow for PFC frame */
2369 delay += XGMAC_PFC_DATA_LEN;
2370 delay += ETH_HLEN + ETH_FCS_LEN;
2371 delay += XGMAC_ETH_PREAMBLE;
2372
2373 /* Allow for miscellaneous delays (LPI exit, cable, etc.) */
2374 delay += XGMAC_PFC_DELAYS;
2375
2376 return delay;
2377}
2378
2379static unsigned int xgbe_get_pfc_queues(struct xgbe_prv_data *pdata)
2380{
2381 unsigned int count, prio_queues;
2382 unsigned int i;
2383
2384 if (!pdata->pfc->pfc_en)
2385 return 0;
2386
2387 count = 0;
2388 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2389 for (i = 0; i < prio_queues; i++) {
2390 if (!xgbe_is_pfc_queue(pdata, i))
2391 continue;
2392
2393 pdata->pfcq[i] = 1;
2394 count++;
2395 }
2396
2397 return count;
2398}
2399
2400static void xgbe_calculate_dcb_fifo(struct xgbe_prv_data *pdata,
2401 unsigned int fifo_size,
2402 unsigned int *fifo)
2403{
2404 unsigned int q_fifo_size, rem_fifo, addn_fifo;
2405 unsigned int prio_queues;
2406 unsigned int pfc_count;
2407 unsigned int i;
2408
2409 q_fifo_size = XGMAC_FIFO_ALIGN(xgbe_get_max_frame(pdata));
2410 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2411 pfc_count = xgbe_get_pfc_queues(pdata);
2412
2413 if (!pfc_count || ((q_fifo_size * prio_queues) > fifo_size)) {
2414 /* No traffic classes with PFC enabled or can't do lossless */
2415 xgbe_calculate_equal_fifo(fifo_size, prio_queues, fifo);
2416 return;
2417 }
2418
2419 /* Calculate how much fifo we have to play with */
2420 rem_fifo = fifo_size - (q_fifo_size * prio_queues);
2421
2422 /* Calculate how much more than base fifo PFC needs, which also
2423 * becomes the threshold activation point (RFA)
2424 */
2425 pdata->pfc_rfa = xgbe_get_pfc_delay(pdata);
2426 pdata->pfc_rfa = XGMAC_FLOW_CONTROL_ALIGN(pdata->pfc_rfa);
2427
2428 if (pdata->pfc_rfa > q_fifo_size) {
2429 addn_fifo = pdata->pfc_rfa - q_fifo_size;
2430 addn_fifo = XGMAC_FIFO_ALIGN(addn_fifo);
2431 } else {
2432 addn_fifo = 0;
2433 }
2434
2435 /* Calculate DCB fifo settings:
2436 * - distribute remaining fifo between the VLAN priority
2437 * queues based on traffic class PFC enablement and overall
2438 * priority (0 is lowest priority, so start at highest)
2439 */
2440 i = prio_queues;
2441 while (i > 0) {
2442 i--;
2443
2444 fifo[i] = (q_fifo_size / XGMAC_FIFO_UNIT) - 1;
2445
2446 if (!pdata->pfcq[i] || !addn_fifo)
2447 continue;
2448
2449 if (addn_fifo > rem_fifo) {
2450 netdev_warn(pdata->netdev,
2451 "RXq%u cannot set needed fifo size\n", i);
2452 if (!rem_fifo)
2453 continue;
2454
2455 addn_fifo = rem_fifo;
2456 }
2457
2458 fifo[i] += (addn_fifo / XGMAC_FIFO_UNIT);
2459 rem_fifo -= addn_fifo;
2460 }
2461
2462 if (rem_fifo) {
2463 unsigned int inc_fifo = rem_fifo / prio_queues;
2464
2465 /* Distribute remaining fifo across queues */
2466 for (i = 0; i < prio_queues; i++)
2467 fifo[i] += (inc_fifo / XGMAC_FIFO_UNIT);
2468 }
2469}
2470
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002471static void xgbe_config_tx_fifo_size(struct xgbe_prv_data *pdata)
2472{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002473 unsigned int fifo_size;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002474 unsigned int fifo[XGBE_MAX_QUEUES];
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002475 unsigned int i;
2476
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002477 fifo_size = xgbe_get_tx_fifo_size(pdata);
2478
2479 xgbe_calculate_equal_fifo(fifo_size, pdata->tx_q_count, fifo);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002480
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002481 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002482 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TQS, fifo[i]);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002483
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002484 netif_info(pdata, drv, pdata->netdev,
2485 "%d Tx hardware queues, %d byte fifo per queue\n",
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002486 pdata->tx_q_count, ((fifo[0] + 1) * XGMAC_FIFO_UNIT));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002487}
2488
2489static void xgbe_config_rx_fifo_size(struct xgbe_prv_data *pdata)
2490{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002491 unsigned int fifo_size;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002492 unsigned int fifo[XGBE_MAX_QUEUES];
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002493 unsigned int prio_queues;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002494 unsigned int i;
2495
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002496 /* Clear any DCB related fifo/queue information */
2497 memset(pdata->pfcq, 0, sizeof(pdata->pfcq));
2498 pdata->pfc_rfa = 0;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002499
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002500 fifo_size = xgbe_get_rx_fifo_size(pdata);
2501 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2502
2503 /* Assign a minimum fifo to the non-VLAN priority queues */
2504 fifo_size = xgbe_set_nonprio_fifos(fifo_size, pdata->rx_q_count, fifo);
2505
2506 if (pdata->pfc && pdata->ets)
2507 xgbe_calculate_dcb_fifo(pdata, fifo_size, fifo);
2508 else
2509 xgbe_calculate_equal_fifo(fifo_size, prio_queues, fifo);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002510
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002511 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002512 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RQS, fifo[i]);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002513
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002514 xgbe_calculate_flow_control_threshold(pdata, fifo);
2515 xgbe_config_flow_control_threshold(pdata);
2516
2517 if (pdata->pfc && pdata->ets && pdata->pfc->pfc_en) {
2518 netif_info(pdata, drv, pdata->netdev,
2519 "%u Rx hardware queues\n", pdata->rx_q_count);
2520 for (i = 0; i < pdata->rx_q_count; i++)
2521 netif_info(pdata, drv, pdata->netdev,
2522 "RxQ%u, %u byte fifo queue\n", i,
2523 ((fifo[i] + 1) * XGMAC_FIFO_UNIT));
2524 } else {
2525 netif_info(pdata, drv, pdata->netdev,
2526 "%u Rx hardware queues, %u byte fifo per queue\n",
2527 pdata->rx_q_count,
2528 ((fifo[0] + 1) * XGMAC_FIFO_UNIT));
2529 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002530}
2531
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002532static void xgbe_config_queue_mapping(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002533{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002534 unsigned int qptc, qptc_extra, queue;
2535 unsigned int prio_queues;
2536 unsigned int ppq, ppq_extra, prio;
2537 unsigned int mask;
2538 unsigned int i, j, reg, reg_val;
2539
2540 /* Map the MTL Tx Queues to Traffic Classes
2541 * Note: Tx Queues >= Traffic Classes
2542 */
2543 qptc = pdata->tx_q_count / pdata->hw_feat.tc_cnt;
2544 qptc_extra = pdata->tx_q_count % pdata->hw_feat.tc_cnt;
2545
2546 for (i = 0, queue = 0; i < pdata->hw_feat.tc_cnt; i++) {
2547 for (j = 0; j < qptc; j++) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002548 netif_dbg(pdata, drv, pdata->netdev,
2549 "TXq%u mapped to TC%u\n", queue, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002550 XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
2551 Q2TCMAP, i);
2552 pdata->q2tc_map[queue++] = i;
2553 }
2554
2555 if (i < qptc_extra) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002556 netif_dbg(pdata, drv, pdata->netdev,
2557 "TXq%u mapped to TC%u\n", queue, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002558 XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
2559 Q2TCMAP, i);
2560 pdata->q2tc_map[queue++] = i;
2561 }
2562 }
2563
2564 /* Map the 8 VLAN priority values to available MTL Rx queues */
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002565 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002566 ppq = IEEE_8021QAZ_MAX_TCS / prio_queues;
2567 ppq_extra = IEEE_8021QAZ_MAX_TCS % prio_queues;
2568
2569 reg = MAC_RQC2R;
2570 reg_val = 0;
2571 for (i = 0, prio = 0; i < prio_queues;) {
2572 mask = 0;
2573 for (j = 0; j < ppq; j++) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002574 netif_dbg(pdata, drv, pdata->netdev,
2575 "PRIO%u mapped to RXq%u\n", prio, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002576 mask |= (1 << prio);
2577 pdata->prio2q_map[prio++] = i;
2578 }
2579
2580 if (i < ppq_extra) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002581 netif_dbg(pdata, drv, pdata->netdev,
2582 "PRIO%u mapped to RXq%u\n", prio, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002583 mask |= (1 << prio);
2584 pdata->prio2q_map[prio++] = i;
2585 }
2586
2587 reg_val |= (mask << ((i++ % MAC_RQC2_Q_PER_REG) << 3));
2588
2589 if ((i % MAC_RQC2_Q_PER_REG) && (i != prio_queues))
2590 continue;
2591
2592 XGMAC_IOWRITE(pdata, reg, reg_val);
2593 reg += MAC_RQC2_INC;
2594 reg_val = 0;
2595 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002596
2597 /* Select dynamic mapping of MTL Rx queue to DMA Rx channel */
2598 reg = MTL_RQDCM0R;
2599 reg_val = 0;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002600 for (i = 0; i < pdata->rx_q_count;) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002601 reg_val |= (0x80 << ((i++ % MTL_RQDCM_Q_PER_REG) << 3));
2602
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002603 if ((i % MTL_RQDCM_Q_PER_REG) && (i != pdata->rx_q_count))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002604 continue;
2605
2606 XGMAC_IOWRITE(pdata, reg, reg_val);
2607
2608 reg += MTL_RQDCM_INC;
2609 reg_val = 0;
2610 }
2611}
2612
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002613static void xgbe_config_tc(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002614{
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002615 unsigned int offset, queue, prio;
2616 u8 i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002617
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002618 netdev_reset_tc(pdata->netdev);
2619 if (!pdata->num_tcs)
2620 return;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002621
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002622 netdev_set_num_tc(pdata->netdev, pdata->num_tcs);
2623
2624 for (i = 0, queue = 0, offset = 0; i < pdata->num_tcs; i++) {
2625 while ((queue < pdata->tx_q_count) &&
2626 (pdata->q2tc_map[queue] == i))
2627 queue++;
2628
2629 netif_dbg(pdata, drv, pdata->netdev, "TC%u using TXq%u-%u\n",
2630 i, offset, queue - 1);
2631 netdev_set_tc_queue(pdata->netdev, i, queue - offset, offset);
2632 offset = queue;
2633 }
2634
2635 if (!pdata->ets)
2636 return;
2637
2638 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++)
2639 netdev_set_prio_tc_map(pdata->netdev, prio,
2640 pdata->ets->prio_tc[prio]);
2641}
2642
2643static void xgbe_config_dcb_tc(struct xgbe_prv_data *pdata)
2644{
2645 struct ieee_ets *ets = pdata->ets;
2646 unsigned int total_weight, min_weight, weight;
2647 unsigned int mask, reg, reg_val;
2648 unsigned int i, prio;
2649
2650 if (!ets)
2651 return;
2652
2653 /* Set Tx to deficit weighted round robin scheduling algorithm (when
2654 * traffic class is using ETS algorithm)
2655 */
2656 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_DWRR);
2657
2658 /* Set Traffic Class algorithms */
2659 total_weight = pdata->netdev->mtu * pdata->hw_feat.tc_cnt;
2660 min_weight = total_weight / 100;
2661 if (!min_weight)
2662 min_weight = 1;
2663
2664 for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
2665 /* Map the priorities to the traffic class */
2666 mask = 0;
2667 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
2668 if (ets->prio_tc[prio] == i)
2669 mask |= (1 << prio);
2670 }
2671 mask &= 0xff;
2672
2673 netif_dbg(pdata, drv, pdata->netdev, "TC%u PRIO mask=%#x\n",
2674 i, mask);
2675 reg = MTL_TCPM0R + (MTL_TCPM_INC * (i / MTL_TCPM_TC_PER_REG));
2676 reg_val = XGMAC_IOREAD(pdata, reg);
2677
2678 reg_val &= ~(0xff << ((i % MTL_TCPM_TC_PER_REG) << 3));
2679 reg_val |= (mask << ((i % MTL_TCPM_TC_PER_REG) << 3));
2680
2681 XGMAC_IOWRITE(pdata, reg, reg_val);
2682
2683 /* Set the traffic class algorithm */
2684 switch (ets->tc_tsa[i]) {
2685 case IEEE_8021QAZ_TSA_STRICT:
2686 netif_dbg(pdata, drv, pdata->netdev,
2687 "TC%u using SP\n", i);
2688 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2689 MTL_TSA_SP);
2690 break;
2691 case IEEE_8021QAZ_TSA_ETS:
2692 weight = total_weight * ets->tc_tx_bw[i] / 100;
2693 weight = clamp(weight, min_weight, total_weight);
2694
2695 netif_dbg(pdata, drv, pdata->netdev,
2696 "TC%u using DWRR (weight %u)\n", i, weight);
2697 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2698 MTL_TSA_ETS);
2699 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW,
2700 weight);
2701 break;
2702 }
2703 }
2704
2705 xgbe_config_tc(pdata);
2706}
2707
2708static void xgbe_config_dcb_pfc(struct xgbe_prv_data *pdata)
2709{
2710 if (!test_bit(XGBE_DOWN, &pdata->dev_state)) {
2711 /* Just stop the Tx queues while Rx fifo is changed */
2712 netif_tx_stop_all_queues(pdata->netdev);
2713
2714 /* Suspend Rx so that fifo's can be adjusted */
2715 pdata->hw_if.disable_rx(pdata);
2716 }
2717
2718 xgbe_config_rx_fifo_size(pdata);
2719 xgbe_config_flow_control(pdata);
2720
2721 if (!test_bit(XGBE_DOWN, &pdata->dev_state)) {
2722 /* Resume Rx */
2723 pdata->hw_if.enable_rx(pdata);
2724
2725 /* Resume Tx queues */
2726 netif_tx_start_all_queues(pdata->netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002727 }
2728}
2729
2730static void xgbe_config_mac_address(struct xgbe_prv_data *pdata)
2731{
2732 xgbe_set_mac_address(pdata, pdata->netdev->dev_addr);
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05002733
2734 /* Filtering is done using perfect filtering and hash filtering */
2735 if (pdata->hw_feat.hash_table_size) {
2736 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HPF, 1);
2737 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HUC, 1);
2738 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HMC, 1);
2739 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002740}
2741
2742static void xgbe_config_jumbo_enable(struct xgbe_prv_data *pdata)
2743{
2744 unsigned int val;
2745
2746 val = (pdata->netdev->mtu > XGMAC_STD_PACKET_MTU) ? 1 : 0;
2747
2748 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, JE, val);
2749}
2750
Lendacky, Thomas916102c2015-01-16 12:46:45 -06002751static void xgbe_config_mac_speed(struct xgbe_prv_data *pdata)
2752{
Lendacky, Thomase57f7a32016-11-03 13:18:27 -05002753 xgbe_set_speed(pdata, pdata->phy_speed);
Lendacky, Thomas916102c2015-01-16 12:46:45 -06002754}
2755
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002756static void xgbe_config_checksum_offload(struct xgbe_prv_data *pdata)
2757{
2758 if (pdata->netdev->features & NETIF_F_RXCSUM)
2759 xgbe_enable_rx_csum(pdata);
2760 else
2761 xgbe_disable_rx_csum(pdata);
2762}
2763
2764static void xgbe_config_vlan_support(struct xgbe_prv_data *pdata)
2765{
Lendacky, Thomas6e5eed02014-06-24 16:19:12 -05002766 /* Indicate that VLAN Tx CTAGs come from context descriptors */
2767 XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, CSVL, 0);
2768 XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, VLTI, 1);
2769
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05002770 /* Set the current VLAN Hash Table register value */
2771 xgbe_update_vlan_hash_table(pdata);
2772
2773 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
2774 xgbe_enable_rx_vlan_filtering(pdata);
2775 else
2776 xgbe_disable_rx_vlan_filtering(pdata);
2777
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002778 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2779 xgbe_enable_rx_vlan_stripping(pdata);
2780 else
2781 xgbe_disable_rx_vlan_stripping(pdata);
2782}
2783
Lendacky, Thomas60265102014-09-05 18:02:30 -05002784static u64 xgbe_mmc_read(struct xgbe_prv_data *pdata, unsigned int reg_lo)
2785{
2786 bool read_hi;
2787 u64 val;
2788
Lendacky, Thomase5a20b92016-11-03 13:19:07 -05002789 if (pdata->vdata->mmc_64bit) {
2790 switch (reg_lo) {
2791 /* These registers are always 32 bit */
2792 case MMC_RXRUNTERROR:
2793 case MMC_RXJABBERERROR:
2794 case MMC_RXUNDERSIZE_G:
2795 case MMC_RXOVERSIZE_G:
2796 case MMC_RXWATCHDOGERROR:
2797 read_hi = false;
2798 break;
Lendacky, Thomas60265102014-09-05 18:02:30 -05002799
Lendacky, Thomase5a20b92016-11-03 13:19:07 -05002800 default:
2801 read_hi = true;
2802 }
2803 } else {
2804 switch (reg_lo) {
2805 /* These registers are always 64 bit */
2806 case MMC_TXOCTETCOUNT_GB_LO:
2807 case MMC_TXOCTETCOUNT_G_LO:
2808 case MMC_RXOCTETCOUNT_GB_LO:
2809 case MMC_RXOCTETCOUNT_G_LO:
2810 read_hi = true;
2811 break;
2812
2813 default:
2814 read_hi = false;
2815 }
Lendacky, Thomas3947d782015-09-30 08:52:38 -05002816 }
Lendacky, Thomas60265102014-09-05 18:02:30 -05002817
2818 val = XGMAC_IOREAD(pdata, reg_lo);
2819
2820 if (read_hi)
2821 val |= ((u64)XGMAC_IOREAD(pdata, reg_lo + 4) << 32);
2822
2823 return val;
2824}
2825
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002826static void xgbe_tx_mmc_int(struct xgbe_prv_data *pdata)
2827{
2828 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2829 unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_TISR);
2830
2831 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_GB))
2832 stats->txoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002833 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002834
2835 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_GB))
2836 stats->txframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002837 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002838
2839 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_G))
2840 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002841 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002842
2843 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_G))
2844 stats->txmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002845 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002846
2847 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX64OCTETS_GB))
2848 stats->tx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002849 xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002850
2851 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX65TO127OCTETS_GB))
2852 stats->tx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002853 xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002854
2855 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX128TO255OCTETS_GB))
2856 stats->tx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002857 xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002858
2859 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX256TO511OCTETS_GB))
2860 stats->tx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002861 xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002862
2863 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX512TO1023OCTETS_GB))
2864 stats->tx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002865 xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002866
2867 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX1024TOMAXOCTETS_GB))
2868 stats->tx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002869 xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002870
2871 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNICASTFRAMES_GB))
2872 stats->txunicastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002873 xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002874
2875 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_GB))
2876 stats->txmulticastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002877 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002878
2879 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_GB))
2880 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002881 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002882
2883 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNDERFLOWERROR))
2884 stats->txunderflowerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002885 xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002886
2887 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_G))
2888 stats->txoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002889 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002890
2891 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_G))
2892 stats->txframecount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002893 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002894
2895 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXPAUSEFRAMES))
2896 stats->txpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002897 xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002898
2899 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXVLANFRAMES_G))
2900 stats->txvlanframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002901 xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002902}
2903
2904static void xgbe_rx_mmc_int(struct xgbe_prv_data *pdata)
2905{
2906 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2907 unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_RISR);
2908
2909 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFRAMECOUNT_GB))
2910 stats->rxframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002911 xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002912
2913 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_GB))
2914 stats->rxoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002915 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002916
2917 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_G))
2918 stats->rxoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002919 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002920
2921 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXBROADCASTFRAMES_G))
2922 stats->rxbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002923 xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002924
2925 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXMULTICASTFRAMES_G))
2926 stats->rxmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002927 xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002928
2929 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXCRCERROR))
2930 stats->rxcrcerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002931 xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002932
2933 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXRUNTERROR))
2934 stats->rxrunterror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002935 xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002936
2937 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXJABBERERROR))
2938 stats->rxjabbererror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002939 xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002940
2941 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNDERSIZE_G))
2942 stats->rxundersize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002943 xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002944
2945 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOVERSIZE_G))
2946 stats->rxoversize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002947 xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002948
2949 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX64OCTETS_GB))
2950 stats->rx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002951 xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002952
2953 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX65TO127OCTETS_GB))
2954 stats->rx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002955 xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002956
2957 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX128TO255OCTETS_GB))
2958 stats->rx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002959 xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002960
2961 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX256TO511OCTETS_GB))
2962 stats->rx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002963 xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002964
2965 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX512TO1023OCTETS_GB))
2966 stats->rx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002967 xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002968
2969 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX1024TOMAXOCTETS_GB))
2970 stats->rx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002971 xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002972
2973 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNICASTFRAMES_G))
2974 stats->rxunicastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002975 xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002976
2977 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXLENGTHERROR))
2978 stats->rxlengtherror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002979 xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002980
2981 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOUTOFRANGETYPE))
2982 stats->rxoutofrangetype +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002983 xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002984
2985 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXPAUSEFRAMES))
2986 stats->rxpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002987 xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002988
2989 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFIFOOVERFLOW))
2990 stats->rxfifooverflow +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002991 xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002992
2993 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXVLANFRAMES_GB))
2994 stats->rxvlanframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002995 xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002996
2997 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXWATCHDOGERROR))
2998 stats->rxwatchdogerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002999 xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003000}
3001
3002static void xgbe_read_mmc_stats(struct xgbe_prv_data *pdata)
3003{
3004 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
3005
3006 /* Freeze counters */
3007 XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 1);
3008
3009 stats->txoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003010 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003011
3012 stats->txframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003013 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003014
3015 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003016 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003017
3018 stats->txmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003019 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003020
3021 stats->tx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003022 xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003023
3024 stats->tx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003025 xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003026
3027 stats->tx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003028 xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003029
3030 stats->tx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003031 xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003032
3033 stats->tx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003034 xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003035
3036 stats->tx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003037 xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003038
3039 stats->txunicastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003040 xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003041
3042 stats->txmulticastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003043 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003044
3045 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003046 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003047
3048 stats->txunderflowerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003049 xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003050
3051 stats->txoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003052 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003053
3054 stats->txframecount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003055 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003056
3057 stats->txpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003058 xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003059
3060 stats->txvlanframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003061 xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003062
3063 stats->rxframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003064 xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003065
3066 stats->rxoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003067 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003068
3069 stats->rxoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003070 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003071
3072 stats->rxbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003073 xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003074
3075 stats->rxmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003076 xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003077
3078 stats->rxcrcerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003079 xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003080
3081 stats->rxrunterror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003082 xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003083
3084 stats->rxjabbererror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003085 xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003086
3087 stats->rxundersize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003088 xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003089
3090 stats->rxoversize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003091 xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003092
3093 stats->rx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003094 xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003095
3096 stats->rx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003097 xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003098
3099 stats->rx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003100 xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003101
3102 stats->rx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003103 xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003104
3105 stats->rx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003106 xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003107
3108 stats->rx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003109 xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003110
3111 stats->rxunicastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003112 xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003113
3114 stats->rxlengtherror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003115 xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003116
3117 stats->rxoutofrangetype +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003118 xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003119
3120 stats->rxpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003121 xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003122
3123 stats->rxfifooverflow +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003124 xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003125
3126 stats->rxvlanframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003127 xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003128
3129 stats->rxwatchdogerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05003130 xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003131
3132 /* Un-freeze counters */
3133 XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 0);
3134}
3135
3136static void xgbe_config_mmc(struct xgbe_prv_data *pdata)
3137{
3138 /* Set counters to reset on read */
3139 XGMAC_IOWRITE_BITS(pdata, MMC_CR, ROR, 1);
3140
3141 /* Reset the counters */
3142 XGMAC_IOWRITE_BITS(pdata, MMC_CR, CR, 1);
3143}
3144
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003145static void xgbe_txq_prepare_tx_stop(struct xgbe_prv_data *pdata,
3146 unsigned int queue)
3147{
3148 unsigned int tx_status;
3149 unsigned long tx_timeout;
3150
3151 /* The Tx engine cannot be stopped if it is actively processing
3152 * packets. Wait for the Tx queue to empty the Tx fifo. Don't
3153 * wait forever though...
3154 */
3155 tx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
3156 while (time_before(jiffies, tx_timeout)) {
3157 tx_status = XGMAC_MTL_IOREAD(pdata, queue, MTL_Q_TQDR);
3158 if ((XGMAC_GET_BITS(tx_status, MTL_Q_TQDR, TRCSTS) != 1) &&
3159 (XGMAC_GET_BITS(tx_status, MTL_Q_TQDR, TXQSTS) == 0))
3160 break;
3161
3162 usleep_range(500, 1000);
3163 }
3164
3165 if (!time_before(jiffies, tx_timeout))
3166 netdev_info(pdata->netdev,
3167 "timed out waiting for Tx queue %u to empty\n",
3168 queue);
3169}
3170
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003171static void xgbe_prepare_tx_stop(struct xgbe_prv_data *pdata,
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003172 unsigned int queue)
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003173{
3174 unsigned int tx_dsr, tx_pos, tx_qidx;
3175 unsigned int tx_status;
3176 unsigned long tx_timeout;
3177
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003178 if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) > 0x20)
3179 return xgbe_txq_prepare_tx_stop(pdata, queue);
3180
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003181 /* Calculate the status register to read and the position within */
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003182 if (queue < DMA_DSRX_FIRST_QUEUE) {
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003183 tx_dsr = DMA_DSR0;
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003184 tx_pos = (queue * DMA_DSR_Q_WIDTH) + DMA_DSR0_TPS_START;
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003185 } else {
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003186 tx_qidx = queue - DMA_DSRX_FIRST_QUEUE;
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003187
3188 tx_dsr = DMA_DSR1 + ((tx_qidx / DMA_DSRX_QPR) * DMA_DSRX_INC);
3189 tx_pos = ((tx_qidx % DMA_DSRX_QPR) * DMA_DSR_Q_WIDTH) +
3190 DMA_DSRX_TPS_START;
3191 }
3192
3193 /* The Tx engine cannot be stopped if it is actively processing
3194 * descriptors. Wait for the Tx engine to enter the stopped or
3195 * suspended state. Don't wait forever though...
3196 */
3197 tx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
3198 while (time_before(jiffies, tx_timeout)) {
3199 tx_status = XGMAC_IOREAD(pdata, tx_dsr);
3200 tx_status = GET_BITS(tx_status, tx_pos, DMA_DSR_TPS_WIDTH);
3201 if ((tx_status == DMA_TPS_STOPPED) ||
3202 (tx_status == DMA_TPS_SUSPENDED))
3203 break;
3204
3205 usleep_range(500, 1000);
3206 }
3207
3208 if (!time_before(jiffies, tx_timeout))
3209 netdev_info(pdata->netdev,
3210 "timed out waiting for Tx DMA channel %u to stop\n",
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003211 queue);
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003212}
3213
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003214static void xgbe_enable_tx(struct xgbe_prv_data *pdata)
3215{
3216 struct xgbe_channel *channel;
3217 unsigned int i;
3218
3219 /* Enable each Tx DMA channel */
3220 channel = pdata->channel;
3221 for (i = 0; i < pdata->channel_count; i++, channel++) {
3222 if (!channel->tx_ring)
3223 break;
3224
3225 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 1);
3226 }
3227
3228 /* Enable each Tx queue */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05003229 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003230 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN,
3231 MTL_Q_ENABLED);
3232
3233 /* Enable MAC Tx */
3234 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
3235}
3236
3237static void xgbe_disable_tx(struct xgbe_prv_data *pdata)
3238{
3239 struct xgbe_channel *channel;
3240 unsigned int i;
3241
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003242 /* Prepare for Tx DMA channel stop */
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003243 for (i = 0; i < pdata->tx_q_count; i++)
3244 xgbe_prepare_tx_stop(pdata, i);
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003245
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003246 /* Disable MAC Tx */
3247 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
3248
3249 /* Disable each Tx queue */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05003250 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003251 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN, 0);
3252
3253 /* Disable each Tx DMA channel */
3254 channel = pdata->channel;
3255 for (i = 0; i < pdata->channel_count; i++, channel++) {
3256 if (!channel->tx_ring)
3257 break;
3258
3259 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 0);
3260 }
3261}
3262
Lendacky, Thomasc3727d62016-02-17 11:49:16 -06003263static void xgbe_prepare_rx_stop(struct xgbe_prv_data *pdata,
3264 unsigned int queue)
3265{
3266 unsigned int rx_status;
3267 unsigned long rx_timeout;
3268
3269 /* The Rx engine cannot be stopped if it is actively processing
3270 * packets. Wait for the Rx queue to empty the Rx fifo. Don't
3271 * wait forever though...
3272 */
3273 rx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
3274 while (time_before(jiffies, rx_timeout)) {
3275 rx_status = XGMAC_MTL_IOREAD(pdata, queue, MTL_Q_RQDR);
3276 if ((XGMAC_GET_BITS(rx_status, MTL_Q_RQDR, PRXQ) == 0) &&
3277 (XGMAC_GET_BITS(rx_status, MTL_Q_RQDR, RXQSTS) == 0))
3278 break;
3279
3280 usleep_range(500, 1000);
3281 }
3282
3283 if (!time_before(jiffies, rx_timeout))
3284 netdev_info(pdata->netdev,
3285 "timed out waiting for Rx queue %u to empty\n",
3286 queue);
3287}
3288
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003289static void xgbe_enable_rx(struct xgbe_prv_data *pdata)
3290{
3291 struct xgbe_channel *channel;
3292 unsigned int reg_val, i;
3293
3294 /* Enable each Rx DMA channel */
3295 channel = pdata->channel;
3296 for (i = 0; i < pdata->channel_count; i++, channel++) {
3297 if (!channel->rx_ring)
3298 break;
3299
3300 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 1);
3301 }
3302
3303 /* Enable each Rx queue */
3304 reg_val = 0;
Lendacky, Thomas853eb162014-07-29 08:57:31 -05003305 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003306 reg_val |= (0x02 << (i << 1));
3307 XGMAC_IOWRITE(pdata, MAC_RQC0R, reg_val);
3308
3309 /* Enable MAC Rx */
3310 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 1);
3311 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 1);
3312 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 1);
3313 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 1);
3314}
3315
3316static void xgbe_disable_rx(struct xgbe_prv_data *pdata)
3317{
3318 struct xgbe_channel *channel;
3319 unsigned int i;
3320
3321 /* Disable MAC Rx */
3322 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 0);
3323 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 0);
3324 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 0);
3325 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 0);
3326
Lendacky, Thomasc3727d62016-02-17 11:49:16 -06003327 /* Prepare for Rx DMA channel stop */
3328 for (i = 0; i < pdata->rx_q_count; i++)
3329 xgbe_prepare_rx_stop(pdata, i);
3330
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003331 /* Disable each Rx queue */
3332 XGMAC_IOWRITE(pdata, MAC_RQC0R, 0);
3333
3334 /* Disable each Rx DMA channel */
3335 channel = pdata->channel;
3336 for (i = 0; i < pdata->channel_count; i++, channel++) {
3337 if (!channel->rx_ring)
3338 break;
3339
3340 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 0);
3341 }
3342}
3343
3344static void xgbe_powerup_tx(struct xgbe_prv_data *pdata)
3345{
3346 struct xgbe_channel *channel;
3347 unsigned int i;
3348
3349 /* Enable each Tx DMA channel */
3350 channel = pdata->channel;
3351 for (i = 0; i < pdata->channel_count; i++, channel++) {
3352 if (!channel->tx_ring)
3353 break;
3354
3355 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 1);
3356 }
3357
3358 /* Enable MAC Tx */
3359 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
3360}
3361
3362static void xgbe_powerdown_tx(struct xgbe_prv_data *pdata)
3363{
3364 struct xgbe_channel *channel;
3365 unsigned int i;
3366
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003367 /* Prepare for Tx DMA channel stop */
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003368 for (i = 0; i < pdata->tx_q_count; i++)
3369 xgbe_prepare_tx_stop(pdata, i);
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003370
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003371 /* Disable MAC Tx */
3372 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
3373
3374 /* Disable each Tx DMA channel */
3375 channel = pdata->channel;
3376 for (i = 0; i < pdata->channel_count; i++, channel++) {
3377 if (!channel->tx_ring)
3378 break;
3379
3380 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 0);
3381 }
3382}
3383
3384static void xgbe_powerup_rx(struct xgbe_prv_data *pdata)
3385{
3386 struct xgbe_channel *channel;
3387 unsigned int i;
3388
3389 /* Enable each Rx DMA channel */
3390 channel = pdata->channel;
3391 for (i = 0; i < pdata->channel_count; i++, channel++) {
3392 if (!channel->rx_ring)
3393 break;
3394
3395 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 1);
3396 }
3397}
3398
3399static void xgbe_powerdown_rx(struct xgbe_prv_data *pdata)
3400{
3401 struct xgbe_channel *channel;
3402 unsigned int i;
3403
3404 /* Disable each Rx DMA channel */
3405 channel = pdata->channel;
3406 for (i = 0; i < pdata->channel_count; i++, channel++) {
3407 if (!channel->rx_ring)
3408 break;
3409
3410 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 0);
3411 }
3412}
3413
3414static int xgbe_init(struct xgbe_prv_data *pdata)
3415{
3416 struct xgbe_desc_if *desc_if = &pdata->desc_if;
3417 int ret;
3418
3419 DBGPR("-->xgbe_init\n");
3420
3421 /* Flush Tx queues */
3422 ret = xgbe_flush_tx_queues(pdata);
Lendacky, Thomas738f7f62017-01-20 12:14:13 -06003423 if (ret) {
3424 netdev_err(pdata->netdev, "error flushing TX queues\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003425 return ret;
Lendacky, Thomas738f7f62017-01-20 12:14:13 -06003426 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003427
3428 /*
3429 * Initialize DMA related features
3430 */
3431 xgbe_config_dma_bus(pdata);
3432 xgbe_config_dma_cache(pdata);
3433 xgbe_config_osp_mode(pdata);
3434 xgbe_config_pblx8(pdata);
3435 xgbe_config_tx_pbl_val(pdata);
3436 xgbe_config_rx_pbl_val(pdata);
3437 xgbe_config_rx_coalesce(pdata);
3438 xgbe_config_tx_coalesce(pdata);
3439 xgbe_config_rx_buffer_size(pdata);
3440 xgbe_config_tso_mode(pdata);
Lendacky, Thomas174fd252014-11-04 16:06:50 -06003441 xgbe_config_sph_mode(pdata);
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003442 xgbe_config_rss(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003443 desc_if->wrapper_tx_desc_init(pdata);
3444 desc_if->wrapper_rx_desc_init(pdata);
3445 xgbe_enable_dma_interrupts(pdata);
3446
3447 /*
3448 * Initialize MTL related features
3449 */
3450 xgbe_config_mtl_mode(pdata);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003451 xgbe_config_queue_mapping(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003452 xgbe_config_tsf_mode(pdata, pdata->tx_sf_mode);
3453 xgbe_config_rsf_mode(pdata, pdata->rx_sf_mode);
3454 xgbe_config_tx_threshold(pdata, pdata->tx_threshold);
3455 xgbe_config_rx_threshold(pdata, pdata->rx_threshold);
3456 xgbe_config_tx_fifo_size(pdata);
3457 xgbe_config_rx_fifo_size(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003458 /*TODO: Error Packet and undersized good Packet forwarding enable
3459 (FEP and FUP)
3460 */
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003461 xgbe_config_dcb_tc(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003462 xgbe_enable_mtl_interrupts(pdata);
3463
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003464 /*
3465 * Initialize MAC related features
3466 */
3467 xgbe_config_mac_address(pdata);
Lendacky, Thomasb8763822015-04-09 12:11:57 -05003468 xgbe_config_rx_mode(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003469 xgbe_config_jumbo_enable(pdata);
3470 xgbe_config_flow_control(pdata);
Lendacky, Thomas916102c2015-01-16 12:46:45 -06003471 xgbe_config_mac_speed(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003472 xgbe_config_checksum_offload(pdata);
3473 xgbe_config_vlan_support(pdata);
3474 xgbe_config_mmc(pdata);
3475 xgbe_enable_mac_interrupts(pdata);
3476
Lendacky, Thomase78332b2016-11-10 17:10:26 -06003477 /*
3478 * Initialize ECC related features
3479 */
3480 xgbe_enable_ecc_interrupts(pdata);
3481
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003482 DBGPR("<--xgbe_init\n");
3483
3484 return 0;
3485}
3486
3487void xgbe_init_function_ptrs_dev(struct xgbe_hw_if *hw_if)
3488{
3489 DBGPR("-->xgbe_init_function_ptrs\n");
3490
3491 hw_if->tx_complete = xgbe_tx_complete;
3492
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003493 hw_if->set_mac_address = xgbe_set_mac_address;
Lendacky, Thomasb8763822015-04-09 12:11:57 -05003494 hw_if->config_rx_mode = xgbe_config_rx_mode;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003495
3496 hw_if->enable_rx_csum = xgbe_enable_rx_csum;
3497 hw_if->disable_rx_csum = xgbe_disable_rx_csum;
3498
3499 hw_if->enable_rx_vlan_stripping = xgbe_enable_rx_vlan_stripping;
3500 hw_if->disable_rx_vlan_stripping = xgbe_disable_rx_vlan_stripping;
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05003501 hw_if->enable_rx_vlan_filtering = xgbe_enable_rx_vlan_filtering;
3502 hw_if->disable_rx_vlan_filtering = xgbe_disable_rx_vlan_filtering;
3503 hw_if->update_vlan_hash_table = xgbe_update_vlan_hash_table;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003504
3505 hw_if->read_mmd_regs = xgbe_read_mmd_regs;
3506 hw_if->write_mmd_regs = xgbe_write_mmd_regs;
3507
Lendacky, Thomase57f7a32016-11-03 13:18:27 -05003508 hw_if->set_speed = xgbe_set_speed;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003509
Lendacky, Thomas732f2ab2016-11-10 17:11:14 -06003510 hw_if->set_ext_mii_mode = xgbe_set_ext_mii_mode;
3511 hw_if->read_ext_mii_regs = xgbe_read_ext_mii_regs;
3512 hw_if->write_ext_mii_regs = xgbe_write_ext_mii_regs;
3513
3514 hw_if->set_gpio = xgbe_set_gpio;
3515 hw_if->clr_gpio = xgbe_clr_gpio;
3516
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003517 hw_if->enable_tx = xgbe_enable_tx;
3518 hw_if->disable_tx = xgbe_disable_tx;
3519 hw_if->enable_rx = xgbe_enable_rx;
3520 hw_if->disable_rx = xgbe_disable_rx;
3521
3522 hw_if->powerup_tx = xgbe_powerup_tx;
3523 hw_if->powerdown_tx = xgbe_powerdown_tx;
3524 hw_if->powerup_rx = xgbe_powerup_rx;
3525 hw_if->powerdown_rx = xgbe_powerdown_rx;
3526
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06003527 hw_if->dev_xmit = xgbe_dev_xmit;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003528 hw_if->dev_read = xgbe_dev_read;
3529 hw_if->enable_int = xgbe_enable_int;
3530 hw_if->disable_int = xgbe_disable_int;
3531 hw_if->init = xgbe_init;
3532 hw_if->exit = xgbe_exit;
3533
3534 /* Descriptor related Sequences have to be initialized here */
3535 hw_if->tx_desc_init = xgbe_tx_desc_init;
3536 hw_if->rx_desc_init = xgbe_rx_desc_init;
3537 hw_if->tx_desc_reset = xgbe_tx_desc_reset;
3538 hw_if->rx_desc_reset = xgbe_rx_desc_reset;
3539 hw_if->is_last_desc = xgbe_is_last_desc;
3540 hw_if->is_context_desc = xgbe_is_context_desc;
Lendacky, Thomas16958a22014-11-20 11:04:08 -06003541 hw_if->tx_start_xmit = xgbe_tx_start_xmit;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003542
3543 /* For FLOW ctrl */
3544 hw_if->config_tx_flow_control = xgbe_config_tx_flow_control;
3545 hw_if->config_rx_flow_control = xgbe_config_rx_flow_control;
3546
3547 /* For RX coalescing */
3548 hw_if->config_rx_coalesce = xgbe_config_rx_coalesce;
3549 hw_if->config_tx_coalesce = xgbe_config_tx_coalesce;
3550 hw_if->usec_to_riwt = xgbe_usec_to_riwt;
3551 hw_if->riwt_to_usec = xgbe_riwt_to_usec;
3552
3553 /* For RX and TX threshold config */
3554 hw_if->config_rx_threshold = xgbe_config_rx_threshold;
3555 hw_if->config_tx_threshold = xgbe_config_tx_threshold;
3556
3557 /* For RX and TX Store and Forward Mode config */
3558 hw_if->config_rsf_mode = xgbe_config_rsf_mode;
3559 hw_if->config_tsf_mode = xgbe_config_tsf_mode;
3560
3561 /* For TX DMA Operating on Second Frame config */
3562 hw_if->config_osp_mode = xgbe_config_osp_mode;
3563
3564 /* For RX and TX PBL config */
3565 hw_if->config_rx_pbl_val = xgbe_config_rx_pbl_val;
3566 hw_if->get_rx_pbl_val = xgbe_get_rx_pbl_val;
3567 hw_if->config_tx_pbl_val = xgbe_config_tx_pbl_val;
3568 hw_if->get_tx_pbl_val = xgbe_get_tx_pbl_val;
3569 hw_if->config_pblx8 = xgbe_config_pblx8;
3570
3571 /* For MMC statistics support */
3572 hw_if->tx_mmc_int = xgbe_tx_mmc_int;
3573 hw_if->rx_mmc_int = xgbe_rx_mmc_int;
3574 hw_if->read_mmc_stats = xgbe_read_mmc_stats;
3575
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05003576 /* For PTP config */
3577 hw_if->config_tstamp = xgbe_config_tstamp;
3578 hw_if->update_tstamp_addend = xgbe_update_tstamp_addend;
3579 hw_if->set_tstamp_time = xgbe_set_tstamp_time;
3580 hw_if->get_tstamp_time = xgbe_get_tstamp_time;
3581 hw_if->get_tx_tstamp = xgbe_get_tx_tstamp;
3582
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003583 /* For Data Center Bridging config */
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06003584 hw_if->config_tc = xgbe_config_tc;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003585 hw_if->config_dcb_tc = xgbe_config_dcb_tc;
3586 hw_if->config_dcb_pfc = xgbe_config_dcb_pfc;
3587
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003588 /* For Receive Side Scaling */
3589 hw_if->enable_rss = xgbe_enable_rss;
3590 hw_if->disable_rss = xgbe_disable_rss;
Lendacky, Thomasf6ac8622014-11-04 16:07:23 -06003591 hw_if->set_rss_hash_key = xgbe_set_rss_hash_key;
3592 hw_if->set_rss_lookup_table = xgbe_set_rss_lookup_table;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003593
Lendacky, Thomase78332b2016-11-10 17:10:26 -06003594 /* For ECC */
3595 hw_if->disable_ecc_ded = xgbe_disable_ecc_ded;
3596 hw_if->disable_ecc_sec = xgbe_disable_ecc_sec;
3597
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003598 DBGPR("<--xgbe_init_function_ptrs\n");
3599}