blob: 0a7ab63deaf9e1d2c53dbc5bbe248765a8b95fde [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
649 channel = pdata->channel;
650 for (i = 0; i < pdata->channel_count; i++, channel++) {
651 /* Clear all the interrupts which are set */
652 dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
653 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
654
655 /* Clear all interrupt enable bits */
656 dma_ch_ier = 0;
657
658 /* Enable following interrupts
659 * NIE - Normal Interrupt Summary Enable
660 * AIE - Abnormal Interrupt Summary Enable
661 * FBEE - Fatal Bus Error Enable
662 */
663 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, NIE, 1);
664 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, AIE, 1);
665 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
666
667 if (channel->tx_ring) {
668 /* Enable the following Tx interrupts
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600669 * TIE - Transmit Interrupt Enable (unless using
670 * per channel interrupts)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500671 */
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600672 if (!pdata->per_channel_irq)
673 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500674 }
675 if (channel->rx_ring) {
676 /* Enable following Rx interrupts
677 * RBUE - Receive Buffer Unavailable Enable
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600678 * RIE - Receive Interrupt Enable (unless using
679 * per channel interrupts)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500680 */
681 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 1);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600682 if (!pdata->per_channel_irq)
683 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500684 }
685
686 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
687 }
688}
689
690static void xgbe_enable_mtl_interrupts(struct xgbe_prv_data *pdata)
691{
692 unsigned int mtl_q_isr;
693 unsigned int q_count, i;
694
695 q_count = max(pdata->hw_feat.tx_q_cnt, pdata->hw_feat.rx_q_cnt);
696 for (i = 0; i < q_count; i++) {
697 /* Clear all the interrupts which are set */
698 mtl_q_isr = XGMAC_MTL_IOREAD(pdata, i, MTL_Q_ISR);
699 XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_ISR, mtl_q_isr);
700
701 /* No MTL interrupts to be enabled */
Lendacky, Thomas91f87342014-07-02 13:04:34 -0500702 XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_IER, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500703 }
704}
705
706static void xgbe_enable_mac_interrupts(struct xgbe_prv_data *pdata)
707{
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500708 unsigned int mac_ier = 0;
709
710 /* Enable Timestamp interrupt */
711 XGMAC_SET_BITS(mac_ier, MAC_IER, TSIE, 1);
712
713 XGMAC_IOWRITE(pdata, MAC_IER, mac_ier);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500714
715 /* Enable all counter interrupts */
Lendacky, Thomasa3ba7c92014-09-05 18:02:36 -0500716 XGMAC_IOWRITE_BITS(pdata, MMC_RIER, ALL_INTERRUPTS, 0xffffffff);
717 XGMAC_IOWRITE_BITS(pdata, MMC_TIER, ALL_INTERRUPTS, 0xffffffff);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500718}
719
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500720static int xgbe_set_speed(struct xgbe_prv_data *pdata, int speed)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500721{
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500722 unsigned int ss;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600723
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500724 switch (speed) {
725 case SPEED_1000:
726 ss = 0x03;
727 break;
728 case SPEED_2500:
729 ss = 0x02;
730 break;
731 case SPEED_10000:
732 ss = 0x00;
733 break;
734 default:
735 return -EINVAL;
736 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500737
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500738 if (XGMAC_IOREAD_BITS(pdata, MAC_TCR, SS) != ss)
739 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, ss);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500740
741 return 0;
742}
743
Lendacky, Thomasb4eee842016-02-17 11:48:08 -0600744static int xgbe_enable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
745{
746 /* Put the VLAN tag in the Rx descriptor */
747 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLRXS, 1);
748
749 /* Don't check the VLAN type */
750 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, DOVLTC, 1);
751
752 /* Check only C-TAG (0x8100) packets */
753 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ERSVLM, 0);
754
755 /* Don't consider an S-TAG (0x88A8) packet as a VLAN packet */
756 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ESVL, 0);
757
758 /* Enable VLAN tag stripping */
759 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0x3);
760
761 return 0;
762}
763
764static int xgbe_disable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
765{
766 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0);
767
768 return 0;
769}
770
771static int xgbe_enable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
772{
773 /* Enable VLAN filtering */
774 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 1);
775
776 /* Enable VLAN Hash Table filtering */
777 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTHM, 1);
778
779 /* Disable VLAN tag inverse matching */
780 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTIM, 0);
781
782 /* Only filter on the lower 12-bits of the VLAN tag */
783 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ETV, 1);
784
785 /* In order for the VLAN Hash Table filtering to be effective,
786 * the VLAN tag identifier in the VLAN Tag Register must not
787 * be zero. Set the VLAN tag identifier to "1" to enable the
788 * VLAN Hash Table filtering. This implies that a VLAN tag of
789 * 1 will always pass filtering.
790 */
791 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VL, 1);
792
793 return 0;
794}
795
796static int xgbe_disable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
797{
798 /* Disable VLAN filtering */
799 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 0);
800
801 return 0;
802}
803
804static u32 xgbe_vid_crc32_le(__le16 vid_le)
805{
806 u32 poly = 0xedb88320; /* CRCPOLY_LE */
807 u32 crc = ~0;
808 u32 temp = 0;
809 unsigned char *data = (unsigned char *)&vid_le;
810 unsigned char data_byte = 0;
811 int i, bits;
812
813 bits = get_bitmask_order(VLAN_VID_MASK);
814 for (i = 0; i < bits; i++) {
815 if ((i % 8) == 0)
816 data_byte = data[i / 8];
817
818 temp = ((crc & 1) ^ data_byte) & 1;
819 crc >>= 1;
820 data_byte >>= 1;
821
822 if (temp)
823 crc ^= poly;
824 }
825
826 return crc;
827}
828
829static int xgbe_update_vlan_hash_table(struct xgbe_prv_data *pdata)
830{
831 u32 crc;
832 u16 vid;
833 __le16 vid_le;
834 u16 vlan_hash_table = 0;
835
836 /* Generate the VLAN Hash Table value */
837 for_each_set_bit(vid, pdata->active_vlans, VLAN_N_VID) {
838 /* Get the CRC32 value of the VLAN ID */
839 vid_le = cpu_to_le16(vid);
840 crc = bitrev32(~xgbe_vid_crc32_le(vid_le)) >> 28;
841
842 vlan_hash_table |= (1 << crc);
843 }
844
845 /* Set the VLAN Hash Table filtering register */
846 XGMAC_IOWRITE_BITS(pdata, MAC_VLANHTR, VLHT, vlan_hash_table);
847
848 return 0;
849}
850
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500851static int xgbe_set_promiscuous_mode(struct xgbe_prv_data *pdata,
852 unsigned int enable)
853{
854 unsigned int val = enable ? 1 : 0;
855
856 if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PR) == val)
857 return 0;
858
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500859 netif_dbg(pdata, drv, pdata->netdev, "%s promiscuous mode\n",
860 enable ? "entering" : "leaving");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500861 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, val);
862
Lendacky, Thomasb4eee842016-02-17 11:48:08 -0600863 /* Hardware will still perform VLAN filtering in promiscuous mode */
864 if (enable) {
865 xgbe_disable_rx_vlan_filtering(pdata);
866 } else {
867 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
868 xgbe_enable_rx_vlan_filtering(pdata);
869 }
870
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500871 return 0;
872}
873
874static int xgbe_set_all_multicast_mode(struct xgbe_prv_data *pdata,
875 unsigned int enable)
876{
877 unsigned int val = enable ? 1 : 0;
878
879 if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PM) == val)
880 return 0;
881
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500882 netif_dbg(pdata, drv, pdata->netdev, "%s allmulti mode\n",
883 enable ? "entering" : "leaving");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500884 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PM, val);
885
886 return 0;
887}
888
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500889static void xgbe_set_mac_reg(struct xgbe_prv_data *pdata,
890 struct netdev_hw_addr *ha, unsigned int *mac_reg)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500891{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500892 unsigned int mac_addr_hi, mac_addr_lo;
893 u8 *mac_addr;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500894
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500895 mac_addr_lo = 0;
896 mac_addr_hi = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500897
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500898 if (ha) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500899 mac_addr = (u8 *)&mac_addr_lo;
900 mac_addr[0] = ha->addr[0];
901 mac_addr[1] = ha->addr[1];
902 mac_addr[2] = ha->addr[2];
903 mac_addr[3] = ha->addr[3];
904 mac_addr = (u8 *)&mac_addr_hi;
905 mac_addr[0] = ha->addr[4];
906 mac_addr[1] = ha->addr[5];
907
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500908 netif_dbg(pdata, drv, pdata->netdev,
909 "adding mac address %pM at %#x\n",
910 ha->addr, *mac_reg);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500911
912 XGMAC_SET_BITS(mac_addr_hi, MAC_MACA1HR, AE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500913 }
914
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500915 XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_hi);
916 *mac_reg += MAC_MACA_INC;
917 XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_lo);
918 *mac_reg += MAC_MACA_INC;
919}
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500920
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500921static void xgbe_set_mac_addn_addrs(struct xgbe_prv_data *pdata)
922{
923 struct net_device *netdev = pdata->netdev;
924 struct netdev_hw_addr *ha;
925 unsigned int mac_reg;
926 unsigned int addn_macs;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500927
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500928 mac_reg = MAC_MACA1HR;
929 addn_macs = pdata->hw_feat.addn_mac;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500930
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500931 if (netdev_uc_count(netdev) > addn_macs) {
932 xgbe_set_promiscuous_mode(pdata, 1);
933 } else {
934 netdev_for_each_uc_addr(ha, netdev) {
935 xgbe_set_mac_reg(pdata, ha, &mac_reg);
936 addn_macs--;
937 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500938
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500939 if (netdev_mc_count(netdev) > addn_macs) {
940 xgbe_set_all_multicast_mode(pdata, 1);
941 } else {
942 netdev_for_each_mc_addr(ha, netdev) {
943 xgbe_set_mac_reg(pdata, ha, &mac_reg);
944 addn_macs--;
945 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500946 }
947 }
948
949 /* Clear remaining additional MAC address entries */
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500950 while (addn_macs--)
951 xgbe_set_mac_reg(pdata, NULL, &mac_reg);
952}
953
954static void xgbe_set_mac_hash_table(struct xgbe_prv_data *pdata)
955{
956 struct net_device *netdev = pdata->netdev;
957 struct netdev_hw_addr *ha;
958 unsigned int hash_reg;
959 unsigned int hash_table_shift, hash_table_count;
960 u32 hash_table[XGBE_MAC_HASH_TABLE_SIZE];
961 u32 crc;
962 unsigned int i;
963
964 hash_table_shift = 26 - (pdata->hw_feat.hash_table_size >> 7);
965 hash_table_count = pdata->hw_feat.hash_table_size / 32;
966 memset(hash_table, 0, sizeof(hash_table));
967
968 /* Build the MAC Hash Table register values */
969 netdev_for_each_uc_addr(ha, netdev) {
970 crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
971 crc >>= hash_table_shift;
972 hash_table[crc >> 5] |= (1 << (crc & 0x1f));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500973 }
974
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500975 netdev_for_each_mc_addr(ha, netdev) {
976 crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
977 crc >>= hash_table_shift;
978 hash_table[crc >> 5] |= (1 << (crc & 0x1f));
979 }
980
981 /* Set the MAC Hash Table registers */
982 hash_reg = MAC_HTR0;
983 for (i = 0; i < hash_table_count; i++) {
984 XGMAC_IOWRITE(pdata, hash_reg, hash_table[i]);
985 hash_reg += MAC_HTR_INC;
986 }
987}
988
989static int xgbe_add_mac_addresses(struct xgbe_prv_data *pdata)
990{
991 if (pdata->hw_feat.hash_table_size)
992 xgbe_set_mac_hash_table(pdata);
993 else
994 xgbe_set_mac_addn_addrs(pdata);
995
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500996 return 0;
997}
998
999static int xgbe_set_mac_address(struct xgbe_prv_data *pdata, u8 *addr)
1000{
1001 unsigned int mac_addr_hi, mac_addr_lo;
1002
1003 mac_addr_hi = (addr[5] << 8) | (addr[4] << 0);
1004 mac_addr_lo = (addr[3] << 24) | (addr[2] << 16) |
1005 (addr[1] << 8) | (addr[0] << 0);
1006
1007 XGMAC_IOWRITE(pdata, MAC_MACA0HR, mac_addr_hi);
1008 XGMAC_IOWRITE(pdata, MAC_MACA0LR, mac_addr_lo);
1009
1010 return 0;
1011}
1012
Lendacky, Thomasb8763822015-04-09 12:11:57 -05001013static int xgbe_config_rx_mode(struct xgbe_prv_data *pdata)
1014{
1015 struct net_device *netdev = pdata->netdev;
1016 unsigned int pr_mode, am_mode;
1017
1018 pr_mode = ((netdev->flags & IFF_PROMISC) != 0);
1019 am_mode = ((netdev->flags & IFF_ALLMULTI) != 0);
1020
1021 xgbe_set_promiscuous_mode(pdata, pr_mode);
1022 xgbe_set_all_multicast_mode(pdata, am_mode);
1023
1024 xgbe_add_mac_addresses(pdata);
1025
1026 return 0;
1027}
1028
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001029static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
1030 int mmd_reg)
1031{
1032 unsigned long flags;
1033 unsigned int mmd_address, index, offset;
1034 int mmd_data;
1035
1036 if (mmd_reg & MII_ADDR_C45)
1037 mmd_address = mmd_reg & ~MII_ADDR_C45;
1038 else
1039 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1040
1041 /* The PCS registers are accessed using mmio. The underlying
1042 * management interface uses indirect addressing to access the MMD
1043 * register sets. This requires accessing of the PCS register in two
1044 * phases, an address phase and a data phase.
1045 *
1046 * The mmio interface is based on 16-bit offsets and values. All
1047 * register offsets must therefore be adjusted by left shifting the
1048 * offset 1 bit and reading 16 bits of data.
1049 */
1050 mmd_address <<= 1;
1051 index = mmd_address & ~pdata->xpcs_window_mask;
1052 offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
1053
1054 spin_lock_irqsave(&pdata->xpcs_lock, flags);
1055 XPCS32_IOWRITE(pdata, PCS_V2_WINDOW_SELECT, index);
1056 mmd_data = XPCS16_IOREAD(pdata, offset);
1057 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
1058
1059 return mmd_data;
1060}
1061
1062static void xgbe_write_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
1063 int mmd_reg, int mmd_data)
1064{
1065 unsigned long flags;
1066 unsigned int mmd_address, index, offset;
1067
1068 if (mmd_reg & MII_ADDR_C45)
1069 mmd_address = mmd_reg & ~MII_ADDR_C45;
1070 else
1071 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1072
1073 /* The PCS registers are accessed using mmio. The underlying
1074 * management interface uses indirect addressing to access the MMD
1075 * register sets. This requires accessing of the PCS register in two
1076 * phases, an address phase and a data phase.
1077 *
1078 * The mmio interface is based on 16-bit offsets and values. All
1079 * register offsets must therefore be adjusted by left shifting the
1080 * offset 1 bit and writing 16 bits of data.
1081 */
1082 mmd_address <<= 1;
1083 index = mmd_address & ~pdata->xpcs_window_mask;
1084 offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
1085
1086 spin_lock_irqsave(&pdata->xpcs_lock, flags);
1087 XPCS32_IOWRITE(pdata, PCS_V2_WINDOW_SELECT, index);
1088 XPCS16_IOWRITE(pdata, offset, mmd_data);
1089 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
1090}
1091
1092static int xgbe_read_mmd_regs_v1(struct xgbe_prv_data *pdata, int prtad,
1093 int mmd_reg)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001094{
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001095 unsigned long flags;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001096 unsigned int mmd_address;
1097 int mmd_data;
1098
1099 if (mmd_reg & MII_ADDR_C45)
1100 mmd_address = mmd_reg & ~MII_ADDR_C45;
1101 else
1102 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1103
1104 /* The PCS registers are accessed using mmio. The underlying APB3
1105 * management interface uses indirect addressing to access the MMD
1106 * register sets. This requires accessing of the PCS register in two
1107 * phases, an address phase and a data phase.
1108 *
1109 * The mmio interface is based on 32-bit offsets and values. All
1110 * register offsets must therefore be adjusted by left shifting the
1111 * offset 2 bits and reading 32 bits of data.
1112 */
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001113 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001114 XPCS32_IOWRITE(pdata, PCS_V1_WINDOW_SELECT, mmd_address >> 8);
1115 mmd_data = XPCS32_IOREAD(pdata, (mmd_address & 0xff) << 2);
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001116 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001117
1118 return mmd_data;
1119}
1120
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001121static void xgbe_write_mmd_regs_v1(struct xgbe_prv_data *pdata, int prtad,
1122 int mmd_reg, int mmd_data)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001123{
1124 unsigned int mmd_address;
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001125 unsigned long flags;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001126
1127 if (mmd_reg & MII_ADDR_C45)
1128 mmd_address = mmd_reg & ~MII_ADDR_C45;
1129 else
1130 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1131
1132 /* The PCS registers are accessed using mmio. The underlying APB3
1133 * management interface uses indirect addressing to access the MMD
1134 * register sets. This requires accessing of the PCS register in two
1135 * phases, an address phase and a data phase.
1136 *
1137 * The mmio interface is based on 32-bit offsets and values. All
1138 * register offsets must therefore be adjusted by left shifting the
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001139 * offset 2 bits and writing 32 bits of data.
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001140 */
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001141 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001142 XPCS32_IOWRITE(pdata, PCS_V1_WINDOW_SELECT, mmd_address >> 8);
1143 XPCS32_IOWRITE(pdata, (mmd_address & 0xff) << 2, mmd_data);
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001144 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001145}
1146
Lendacky, Thomasb03a4a62016-11-03 13:18:56 -05001147static int xgbe_read_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
1148 int mmd_reg)
1149{
1150 switch (pdata->vdata->xpcs_access) {
1151 case XGBE_XPCS_ACCESS_V1:
1152 return xgbe_read_mmd_regs_v1(pdata, prtad, mmd_reg);
1153
1154 case XGBE_XPCS_ACCESS_V2:
1155 default:
1156 return xgbe_read_mmd_regs_v2(pdata, prtad, mmd_reg);
1157 }
1158}
1159
1160static void xgbe_write_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
1161 int mmd_reg, int mmd_data)
1162{
1163 switch (pdata->vdata->xpcs_access) {
1164 case XGBE_XPCS_ACCESS_V1:
1165 return xgbe_write_mmd_regs_v1(pdata, prtad, mmd_reg, mmd_data);
1166
1167 case XGBE_XPCS_ACCESS_V2:
1168 default:
1169 return xgbe_write_mmd_regs_v2(pdata, prtad, mmd_reg, mmd_data);
1170 }
1171}
1172
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001173static int xgbe_tx_complete(struct xgbe_ring_desc *rdesc)
1174{
1175 return !XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN);
1176}
1177
1178static int xgbe_disable_rx_csum(struct xgbe_prv_data *pdata)
1179{
1180 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 0);
1181
1182 return 0;
1183}
1184
1185static int xgbe_enable_rx_csum(struct xgbe_prv_data *pdata)
1186{
1187 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 1);
1188
1189 return 0;
1190}
1191
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001192static void xgbe_tx_desc_reset(struct xgbe_ring_data *rdata)
1193{
1194 struct xgbe_ring_desc *rdesc = rdata->rdesc;
1195
1196 /* Reset the Tx descriptor
1197 * Set buffer 1 (lo) address to zero
1198 * Set buffer 1 (hi) address to zero
1199 * Reset all other control bits (IC, TTSE, B2L & B1L)
1200 * Reset all other control bits (OWN, CTXT, FD, LD, CPC, CIC, etc)
1201 */
1202 rdesc->desc0 = 0;
1203 rdesc->desc1 = 0;
1204 rdesc->desc2 = 0;
1205 rdesc->desc3 = 0;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001206
1207 /* Make sure ownership is written to the descriptor */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001208 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001209}
1210
1211static void xgbe_tx_desc_init(struct xgbe_channel *channel)
1212{
1213 struct xgbe_ring *ring = channel->tx_ring;
1214 struct xgbe_ring_data *rdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001215 int i;
1216 int start_index = ring->cur;
1217
1218 DBGPR("-->tx_desc_init\n");
1219
1220 /* Initialze all descriptors */
1221 for (i = 0; i < ring->rdesc_count; i++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001222 rdata = XGBE_GET_DESC_DATA(ring, i);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001223
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001224 /* Initialize Tx descriptor */
1225 xgbe_tx_desc_reset(rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001226 }
1227
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001228 /* Update the total number of Tx descriptors */
1229 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDRLR, ring->rdesc_count - 1);
1230
1231 /* Update the starting address of descriptor ring */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001232 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001233 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_HI,
1234 upper_32_bits(rdata->rdesc_dma));
1235 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_LO,
1236 lower_32_bits(rdata->rdesc_dma));
1237
1238 DBGPR("<--tx_desc_init\n");
1239}
1240
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001241static void xgbe_rx_desc_reset(struct xgbe_prv_data *pdata,
1242 struct xgbe_ring_data *rdata, unsigned int index)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001243{
1244 struct xgbe_ring_desc *rdesc = rdata->rdesc;
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001245 unsigned int rx_usecs = pdata->rx_usecs;
1246 unsigned int rx_frames = pdata->rx_frames;
1247 unsigned int inte;
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001248 dma_addr_t hdr_dma, buf_dma;
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001249
1250 if (!rx_usecs && !rx_frames) {
1251 /* No coalescing, interrupt for every descriptor */
1252 inte = 1;
1253 } else {
1254 /* Set interrupt based on Rx frame coalescing setting */
1255 if (rx_frames && !((index + 1) % rx_frames))
1256 inte = 1;
1257 else
1258 inte = 0;
1259 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001260
1261 /* Reset the Rx descriptor
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001262 * Set buffer 1 (lo) address to header dma address (lo)
1263 * Set buffer 1 (hi) address to header dma address (hi)
1264 * Set buffer 2 (lo) address to buffer dma address (lo)
1265 * Set buffer 2 (hi) address to buffer dma address (hi) and
1266 * set control bits OWN and INTE
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001267 */
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001268 hdr_dma = rdata->rx.hdr.dma_base + rdata->rx.hdr.dma_off;
1269 buf_dma = rdata->rx.buf.dma_base + rdata->rx.buf.dma_off;
1270 rdesc->desc0 = cpu_to_le32(lower_32_bits(hdr_dma));
1271 rdesc->desc1 = cpu_to_le32(upper_32_bits(hdr_dma));
1272 rdesc->desc2 = cpu_to_le32(lower_32_bits(buf_dma));
1273 rdesc->desc3 = cpu_to_le32(upper_32_bits(buf_dma));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001274
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001275 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, INTE, inte);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001276
1277 /* Since the Rx DMA engine is likely running, make sure everything
1278 * is written to the descriptor(s) before setting the OWN bit
1279 * for the descriptor
1280 */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001281 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001282
1283 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN, 1);
1284
1285 /* Make sure ownership is written to the descriptor */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001286 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001287}
1288
1289static void xgbe_rx_desc_init(struct xgbe_channel *channel)
1290{
1291 struct xgbe_prv_data *pdata = channel->pdata;
1292 struct xgbe_ring *ring = channel->rx_ring;
1293 struct xgbe_ring_data *rdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001294 unsigned int start_index = ring->cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001295 unsigned int i;
1296
1297 DBGPR("-->rx_desc_init\n");
1298
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001299 /* Initialize all descriptors */
1300 for (i = 0; i < ring->rdesc_count; i++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001301 rdata = XGBE_GET_DESC_DATA(ring, i);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001302
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001303 /* Initialize Rx descriptor */
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001304 xgbe_rx_desc_reset(pdata, rdata, i);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001305 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001306
1307 /* Update the total number of Rx descriptors */
1308 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDRLR, ring->rdesc_count - 1);
1309
1310 /* Update the starting address of descriptor ring */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001311 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001312 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_HI,
1313 upper_32_bits(rdata->rdesc_dma));
1314 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_LO,
1315 lower_32_bits(rdata->rdesc_dma));
1316
1317 /* Update the Rx Descriptor Tail Pointer */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001318 rdata = XGBE_GET_DESC_DATA(ring, start_index + ring->rdesc_count - 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001319 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
1320 lower_32_bits(rdata->rdesc_dma));
1321
1322 DBGPR("<--rx_desc_init\n");
1323}
1324
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001325static void xgbe_update_tstamp_addend(struct xgbe_prv_data *pdata,
1326 unsigned int addend)
1327{
1328 /* Set the addend register value and tell the device */
1329 XGMAC_IOWRITE(pdata, MAC_TSAR, addend);
1330 XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSADDREG, 1);
1331
1332 /* Wait for addend update to complete */
1333 while (XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSADDREG))
1334 udelay(5);
1335}
1336
1337static void xgbe_set_tstamp_time(struct xgbe_prv_data *pdata, unsigned int sec,
1338 unsigned int nsec)
1339{
1340 /* Set the time values and tell the device */
1341 XGMAC_IOWRITE(pdata, MAC_STSUR, sec);
1342 XGMAC_IOWRITE(pdata, MAC_STNUR, nsec);
1343 XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSINIT, 1);
1344
1345 /* Wait for time update to complete */
1346 while (XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSINIT))
1347 udelay(5);
1348}
1349
1350static u64 xgbe_get_tstamp_time(struct xgbe_prv_data *pdata)
1351{
1352 u64 nsec;
1353
1354 nsec = XGMAC_IOREAD(pdata, MAC_STSR);
1355 nsec *= NSEC_PER_SEC;
1356 nsec += XGMAC_IOREAD(pdata, MAC_STNR);
1357
1358 return nsec;
1359}
1360
1361static u64 xgbe_get_tx_tstamp(struct xgbe_prv_data *pdata)
1362{
1363 unsigned int tx_snr;
1364 u64 nsec;
1365
1366 tx_snr = XGMAC_IOREAD(pdata, MAC_TXSNR);
1367 if (XGMAC_GET_BITS(tx_snr, MAC_TXSNR, TXTSSTSMIS))
1368 return 0;
1369
1370 nsec = XGMAC_IOREAD(pdata, MAC_TXSSR);
1371 nsec *= NSEC_PER_SEC;
1372 nsec += tx_snr;
1373
1374 return nsec;
1375}
1376
1377static void xgbe_get_rx_tstamp(struct xgbe_packet_data *packet,
1378 struct xgbe_ring_desc *rdesc)
1379{
1380 u64 nsec;
1381
1382 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSA) &&
1383 !XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSD)) {
1384 nsec = le32_to_cpu(rdesc->desc1);
1385 nsec <<= 32;
1386 nsec |= le32_to_cpu(rdesc->desc0);
1387 if (nsec != 0xffffffffffffffffULL) {
1388 packet->rx_tstamp = nsec;
1389 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1390 RX_TSTAMP, 1);
1391 }
1392 }
1393}
1394
1395static int xgbe_config_tstamp(struct xgbe_prv_data *pdata,
1396 unsigned int mac_tscr)
1397{
1398 /* Set one nano-second accuracy */
1399 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCTRLSSR, 1);
1400
1401 /* Set fine timestamp update */
1402 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCFUPDT, 1);
1403
1404 /* Overwrite earlier timestamps */
1405 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TXTSSTSM, 1);
1406
1407 XGMAC_IOWRITE(pdata, MAC_TSCR, mac_tscr);
1408
1409 /* Exit if timestamping is not enabled */
1410 if (!XGMAC_GET_BITS(mac_tscr, MAC_TSCR, TSENA))
1411 return 0;
1412
1413 /* Initialize time registers */
1414 XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SSINC, XGBE_TSTAMP_SSINC);
1415 XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SNSINC, XGBE_TSTAMP_SNSINC);
1416 xgbe_update_tstamp_addend(pdata, pdata->tstamp_addend);
1417 xgbe_set_tstamp_time(pdata, 0, 0);
1418
1419 /* Initialize the timecounter */
1420 timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc,
1421 ktime_to_ns(ktime_get_real()));
1422
1423 return 0;
1424}
1425
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001426static void xgbe_tx_start_xmit(struct xgbe_channel *channel,
1427 struct xgbe_ring *ring)
1428{
1429 struct xgbe_prv_data *pdata = channel->pdata;
1430 struct xgbe_ring_data *rdata;
1431
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001432 /* Make sure everything is written before the register write */
1433 wmb();
1434
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001435 /* Issue a poll command to Tx DMA by writing address
1436 * of next immediate free descriptor */
1437 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1438 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDTR_LO,
1439 lower_32_bits(rdata->rdesc_dma));
1440
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -05001441 /* Start the Tx timer */
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001442 if (pdata->tx_usecs && !channel->tx_timer_active) {
1443 channel->tx_timer_active = 1;
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -05001444 mod_timer(&channel->tx_timer,
1445 jiffies + usecs_to_jiffies(pdata->tx_usecs));
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001446 }
1447
1448 ring->tx.xmit_more = 0;
1449}
1450
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001451static void xgbe_dev_xmit(struct xgbe_channel *channel)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001452{
1453 struct xgbe_prv_data *pdata = channel->pdata;
1454 struct xgbe_ring *ring = channel->tx_ring;
1455 struct xgbe_ring_data *rdata;
1456 struct xgbe_ring_desc *rdesc;
1457 struct xgbe_packet_data *packet = &ring->packet_data;
1458 unsigned int csum, tso, vlan;
1459 unsigned int tso_context, vlan_context;
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001460 unsigned int tx_set_ic;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001461 int start_index = ring->cur;
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001462 int cur_index = ring->cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001463 int i;
1464
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001465 DBGPR("-->xgbe_dev_xmit\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001466
1467 csum = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1468 CSUM_ENABLE);
1469 tso = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1470 TSO_ENABLE);
1471 vlan = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1472 VLAN_CTAG);
1473
1474 if (tso && (packet->mss != ring->tx.cur_mss))
1475 tso_context = 1;
1476 else
1477 tso_context = 0;
1478
1479 if (vlan && (packet->vlan_ctag != ring->tx.cur_vlan_ctag))
1480 vlan_context = 1;
1481 else
1482 vlan_context = 0;
1483
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001484 /* Determine if an interrupt should be generated for this Tx:
1485 * Interrupt:
1486 * - Tx frame count exceeds the frame count setting
1487 * - Addition of Tx frame count to the frame count since the
1488 * last interrupt was set exceeds the frame count setting
1489 * No interrupt:
1490 * - No frame count setting specified (ethtool -C ethX tx-frames 0)
1491 * - Addition of Tx frame count to the frame count since the
1492 * last interrupt was set does not exceed the frame count setting
1493 */
1494 ring->coalesce_count += packet->tx_packets;
1495 if (!pdata->tx_frames)
1496 tx_set_ic = 0;
1497 else if (packet->tx_packets > pdata->tx_frames)
1498 tx_set_ic = 1;
1499 else if ((ring->coalesce_count % pdata->tx_frames) <
1500 packet->tx_packets)
1501 tx_set_ic = 1;
1502 else
1503 tx_set_ic = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001504
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001505 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001506 rdesc = rdata->rdesc;
1507
1508 /* Create a context descriptor if this is a TSO packet */
1509 if (tso_context || vlan_context) {
1510 if (tso_context) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001511 netif_dbg(pdata, tx_queued, pdata->netdev,
1512 "TSO context descriptor, mss=%u\n",
1513 packet->mss);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001514
1515 /* Set the MSS size */
1516 XGMAC_SET_BITS_LE(rdesc->desc2, TX_CONTEXT_DESC2,
1517 MSS, packet->mss);
1518
1519 /* Mark it as a CONTEXT descriptor */
1520 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1521 CTXT, 1);
1522
1523 /* Indicate this descriptor contains the MSS */
1524 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1525 TCMSSV, 1);
1526
1527 ring->tx.cur_mss = packet->mss;
1528 }
1529
1530 if (vlan_context) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001531 netif_dbg(pdata, tx_queued, pdata->netdev,
1532 "VLAN context descriptor, ctag=%u\n",
1533 packet->vlan_ctag);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001534
1535 /* Mark it as a CONTEXT descriptor */
1536 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1537 CTXT, 1);
1538
1539 /* Set the VLAN tag */
1540 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1541 VT, packet->vlan_ctag);
1542
1543 /* Indicate this descriptor contains the VLAN tag */
1544 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1545 VLTV, 1);
1546
1547 ring->tx.cur_vlan_ctag = packet->vlan_ctag;
1548 }
1549
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001550 cur_index++;
1551 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001552 rdesc = rdata->rdesc;
1553 }
1554
1555 /* Update buffer address (for TSO this is the header) */
1556 rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
1557 rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
1558
1559 /* Update the buffer length */
1560 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1561 rdata->skb_dma_len);
1562
1563 /* VLAN tag insertion check */
1564 if (vlan)
1565 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, VTIR,
1566 TX_NORMAL_DESC2_VLAN_INSERT);
1567
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001568 /* Timestamp enablement check */
1569 if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
1570 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, TTSE, 1);
1571
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001572 /* Mark it as First Descriptor */
1573 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FD, 1);
1574
1575 /* Mark it as a NORMAL descriptor */
1576 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1577
1578 /* Set OWN bit if not the first descriptor */
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001579 if (cur_index != start_index)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001580 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1581
1582 if (tso) {
1583 /* Enable TSO */
1584 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TSE, 1);
1585 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPPL,
1586 packet->tcp_payload_len);
1587 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPHDRLEN,
1588 packet->tcp_header_len / 4);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001589
1590 pdata->ext_stats.tx_tso_packets++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001591 } else {
1592 /* Enable CRC and Pad Insertion */
1593 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CPC, 0);
1594
1595 /* Enable HW CSUM */
1596 if (csum)
1597 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1598 CIC, 0x3);
1599
1600 /* Set the total length to be transmitted */
1601 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FL,
1602 packet->length);
1603 }
1604
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001605 for (i = cur_index - start_index + 1; i < packet->rdesc_count; i++) {
1606 cur_index++;
1607 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001608 rdesc = rdata->rdesc;
1609
1610 /* Update buffer address */
1611 rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
1612 rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
1613
1614 /* Update the buffer length */
1615 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1616 rdata->skb_dma_len);
1617
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001618 /* Set OWN bit */
1619 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1620
1621 /* Mark it as NORMAL descriptor */
1622 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1623
1624 /* Enable HW CSUM */
1625 if (csum)
1626 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1627 CIC, 0x3);
1628 }
1629
1630 /* Set LAST bit for the last descriptor */
1631 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD, 1);
1632
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001633 /* Set IC bit based on Tx coalescing settings */
1634 if (tx_set_ic)
1635 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, IC, 1);
1636
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001637 /* Save the Tx info to report back during cleanup */
1638 rdata->tx.packets = packet->tx_packets;
1639 rdata->tx.bytes = packet->tx_bytes;
1640
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001641 /* In case the Tx DMA engine is running, make sure everything
1642 * is written to the descriptor(s) before setting the OWN bit
1643 * for the first descriptor
1644 */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001645 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001646
1647 /* Set OWN bit for the first descriptor */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001648 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001649 rdesc = rdata->rdesc;
1650 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1651
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001652 if (netif_msg_tx_queued(pdata))
1653 xgbe_dump_tx_desc(pdata, ring, start_index,
1654 packet->rdesc_count, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001655
1656 /* Make sure ownership is written to the descriptor */
Lendacky, Thomas20986ed2015-10-26 17:13:54 -05001657 smp_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001658
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001659 ring->cur = cur_index + 1;
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001660 if (!packet->skb->xmit_more ||
1661 netif_xmit_stopped(netdev_get_tx_queue(pdata->netdev,
1662 channel->queue_index)))
1663 xgbe_tx_start_xmit(channel, ring);
1664 else
1665 ring->tx.xmit_more = 1;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001666
1667 DBGPR(" %s: descriptors %u to %u written\n",
1668 channel->name, start_index & (ring->rdesc_count - 1),
1669 (ring->cur - 1) & (ring->rdesc_count - 1));
1670
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001671 DBGPR("<--xgbe_dev_xmit\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001672}
1673
1674static int xgbe_dev_read(struct xgbe_channel *channel)
1675{
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001676 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001677 struct xgbe_ring *ring = channel->rx_ring;
1678 struct xgbe_ring_data *rdata;
1679 struct xgbe_ring_desc *rdesc;
1680 struct xgbe_packet_data *packet = &ring->packet_data;
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001681 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001682 unsigned int err, etlt, l34t;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001683
1684 DBGPR("-->xgbe_dev_read: cur = %d\n", ring->cur);
1685
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001686 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001687 rdesc = rdata->rdesc;
1688
1689 /* Check for data availability */
1690 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN))
1691 return 1;
1692
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001693 /* Make sure descriptor fields are read after reading the OWN bit */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001694 dma_rmb();
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001695
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001696 if (netif_msg_rx_status(pdata))
1697 xgbe_dump_rx_desc(pdata, ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001698
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001699 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CTXT)) {
1700 /* Timestamp Context Descriptor */
1701 xgbe_get_rx_tstamp(packet, rdesc);
1702
1703 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1704 CONTEXT, 1);
1705 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1706 CONTEXT_NEXT, 0);
1707 return 0;
1708 }
1709
1710 /* Normal Descriptor, be sure Context Descriptor bit is off */
1711 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, CONTEXT, 0);
1712
1713 /* Indicate if a Context Descriptor is next */
1714 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CDA))
1715 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1716 CONTEXT_NEXT, 1);
1717
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001718 /* Get the header length */
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001719 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, FD)) {
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001720 rdata->rx.hdr_len = XGMAC_GET_BITS_LE(rdesc->desc2,
1721 RX_NORMAL_DESC2, HL);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001722 if (rdata->rx.hdr_len)
1723 pdata->ext_stats.rx_split_header_packets++;
1724 }
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001725
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001726 /* Get the RSS hash */
1727 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, RSV)) {
1728 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1729 RSS_HASH, 1);
1730
1731 packet->rss_hash = le32_to_cpu(rdesc->desc1);
1732
1733 l34t = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, L34T);
1734 switch (l34t) {
1735 case RX_DESC3_L34T_IPV4_TCP:
1736 case RX_DESC3_L34T_IPV4_UDP:
1737 case RX_DESC3_L34T_IPV6_TCP:
1738 case RX_DESC3_L34T_IPV6_UDP:
1739 packet->rss_hash_type = PKT_HASH_TYPE_L4;
Dan Carpenterb6267d32014-11-13 09:19:06 +03001740 break;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001741 default:
1742 packet->rss_hash_type = PKT_HASH_TYPE_L3;
1743 }
1744 }
1745
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001746 /* Get the packet length */
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001747 rdata->rx.len = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, PL);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001748
1749 if (!XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, LD)) {
1750 /* Not all the data has been transferred for this packet */
1751 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1752 INCOMPLETE, 1);
1753 return 0;
1754 }
1755
1756 /* This is the last of the data for this packet */
1757 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1758 INCOMPLETE, 0);
1759
1760 /* Set checksum done indicator as appropriate */
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001761 if (netdev->features & NETIF_F_RXCSUM)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001762 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1763 CSUM_DONE, 1);
1764
1765 /* Check for errors (only valid in last descriptor) */
1766 err = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ES);
1767 etlt = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ETLT);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001768 netif_dbg(pdata, rx_status, netdev, "err=%u, etlt=%#x\n", err, etlt);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001769
Lendacky, Thomas7bba35b2014-11-20 11:03:38 -06001770 if (!err || !etlt) {
1771 /* No error if err is 0 or etlt is 0 */
Lendacky, Thomasc52e9c62014-06-24 16:19:18 -05001772 if ((etlt == 0x09) &&
1773 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001774 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1775 VLAN_CTAG, 1);
1776 packet->vlan_ctag = XGMAC_GET_BITS_LE(rdesc->desc0,
1777 RX_NORMAL_DESC0,
1778 OVT);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001779 netif_dbg(pdata, rx_status, netdev, "vlan-ctag=%#06x\n",
1780 packet->vlan_ctag);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001781 }
1782 } else {
1783 if ((etlt == 0x05) || (etlt == 0x06))
1784 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1785 CSUM_DONE, 0);
1786 else
1787 XGMAC_SET_BITS(packet->errors, RX_PACKET_ERRORS,
1788 FRAME, 1);
1789 }
1790
1791 DBGPR("<--xgbe_dev_read: %s - descriptor=%u (cur=%d)\n", channel->name,
1792 ring->cur & (ring->rdesc_count - 1), ring->cur);
1793
1794 return 0;
1795}
1796
1797static int xgbe_is_context_desc(struct xgbe_ring_desc *rdesc)
1798{
1799 /* Rx and Tx share CTXT bit, so check TDES3.CTXT bit */
1800 return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT);
1801}
1802
1803static int xgbe_is_last_desc(struct xgbe_ring_desc *rdesc)
1804{
1805 /* Rx and Tx share LD bit, so check TDES3.LD bit */
1806 return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD);
1807}
1808
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001809static int xgbe_enable_int(struct xgbe_channel *channel,
1810 enum xgbe_int int_id)
1811{
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001812 unsigned int dma_ch_ier;
1813
1814 dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
1815
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001816 switch (int_id) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001817 case XGMAC_INT_DMA_CH_SR_TI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001818 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001819 break;
1820 case XGMAC_INT_DMA_CH_SR_TPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001821 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001822 break;
1823 case XGMAC_INT_DMA_CH_SR_TBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001824 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001825 break;
1826 case XGMAC_INT_DMA_CH_SR_RI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001827 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001828 break;
1829 case XGMAC_INT_DMA_CH_SR_RBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001830 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001831 break;
1832 case XGMAC_INT_DMA_CH_SR_RPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001833 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 1);
1834 break;
1835 case XGMAC_INT_DMA_CH_SR_TI_RI:
1836 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
1837 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001838 break;
1839 case XGMAC_INT_DMA_CH_SR_FBE:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001840 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001841 break;
1842 case XGMAC_INT_DMA_ALL:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001843 dma_ch_ier |= channel->saved_ier;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001844 break;
1845 default:
1846 return -1;
1847 }
1848
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001849 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
1850
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001851 return 0;
1852}
1853
1854static int xgbe_disable_int(struct xgbe_channel *channel,
1855 enum xgbe_int int_id)
1856{
1857 unsigned int dma_ch_ier;
1858
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001859 dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
1860
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001861 switch (int_id) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001862 case XGMAC_INT_DMA_CH_SR_TI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001863 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001864 break;
1865 case XGMAC_INT_DMA_CH_SR_TPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001866 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001867 break;
1868 case XGMAC_INT_DMA_CH_SR_TBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001869 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001870 break;
1871 case XGMAC_INT_DMA_CH_SR_RI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001872 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001873 break;
1874 case XGMAC_INT_DMA_CH_SR_RBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001875 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001876 break;
1877 case XGMAC_INT_DMA_CH_SR_RPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001878 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 0);
1879 break;
1880 case XGMAC_INT_DMA_CH_SR_TI_RI:
1881 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
1882 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001883 break;
1884 case XGMAC_INT_DMA_CH_SR_FBE:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001885 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001886 break;
1887 case XGMAC_INT_DMA_ALL:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001888 channel->saved_ier = dma_ch_ier & XGBE_DMA_INTERRUPT_MASK;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001889 dma_ch_ier &= ~XGBE_DMA_INTERRUPT_MASK;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001890 break;
1891 default:
1892 return -1;
1893 }
1894
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001895 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
1896
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001897 return 0;
1898}
1899
1900static int xgbe_exit(struct xgbe_prv_data *pdata)
1901{
1902 unsigned int count = 2000;
1903
1904 DBGPR("-->xgbe_exit\n");
1905
1906 /* Issue a software reset */
1907 XGMAC_IOWRITE_BITS(pdata, DMA_MR, SWR, 1);
1908 usleep_range(10, 15);
1909
1910 /* Poll Until Poll Condition */
Dan Carpenterc7557e62015-12-15 13:12:29 +03001911 while (--count && XGMAC_IOREAD_BITS(pdata, DMA_MR, SWR))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001912 usleep_range(500, 600);
1913
1914 if (!count)
1915 return -EBUSY;
1916
1917 DBGPR("<--xgbe_exit\n");
1918
1919 return 0;
1920}
1921
1922static int xgbe_flush_tx_queues(struct xgbe_prv_data *pdata)
1923{
1924 unsigned int i, count;
1925
Lendacky, Thomasa9a4a2d2014-08-29 13:16:50 -05001926 if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) < 0x21)
1927 return 0;
1928
Lendacky, Thomas853eb162014-07-29 08:57:31 -05001929 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001930 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, FTQ, 1);
1931
1932 /* Poll Until Poll Condition */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05001933 for (i = 0; i < pdata->tx_q_count; i++) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001934 count = 2000;
Dan Carpenterc7557e62015-12-15 13:12:29 +03001935 while (--count && XGMAC_MTL_IOREAD_BITS(pdata, i,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001936 MTL_Q_TQOMR, FTQ))
1937 usleep_range(500, 600);
1938
1939 if (!count)
1940 return -EBUSY;
1941 }
1942
1943 return 0;
1944}
1945
1946static void xgbe_config_dma_bus(struct xgbe_prv_data *pdata)
1947{
1948 /* Set enhanced addressing mode */
1949 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, EAME, 1);
1950
1951 /* Set the System Bus mode */
1952 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, UNDEF, 1);
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001953 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, BLEN_256, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001954}
1955
1956static void xgbe_config_dma_cache(struct xgbe_prv_data *pdata)
1957{
1958 unsigned int arcache, awcache;
1959
1960 arcache = 0;
Lendacky, Thomascfa50c72014-07-02 13:04:57 -05001961 XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRC, pdata->arcache);
1962 XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRD, pdata->axdomain);
1963 XGMAC_SET_BITS(arcache, DMA_AXIARCR, TEC, pdata->arcache);
1964 XGMAC_SET_BITS(arcache, DMA_AXIARCR, TED, pdata->axdomain);
1965 XGMAC_SET_BITS(arcache, DMA_AXIARCR, THC, pdata->arcache);
1966 XGMAC_SET_BITS(arcache, DMA_AXIARCR, THD, pdata->axdomain);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001967 XGMAC_IOWRITE(pdata, DMA_AXIARCR, arcache);
1968
1969 awcache = 0;
Lendacky, Thomascfa50c72014-07-02 13:04:57 -05001970 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWC, pdata->awcache);
1971 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWD, pdata->axdomain);
1972 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPC, pdata->awcache);
1973 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPD, pdata->axdomain);
1974 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHC, pdata->awcache);
1975 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHD, pdata->axdomain);
1976 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDC, pdata->awcache);
1977 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDD, pdata->axdomain);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001978 XGMAC_IOWRITE(pdata, DMA_AXIAWCR, awcache);
1979}
1980
1981static void xgbe_config_mtl_mode(struct xgbe_prv_data *pdata)
1982{
1983 unsigned int i;
1984
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001985 /* Set Tx to weighted round robin scheduling algorithm */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001986 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_WRR);
1987
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001988 /* Set Tx traffic classes to use WRR algorithm with equal weights */
1989 for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
1990 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
1991 MTL_TSA_ETS);
1992 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW, 1);
1993 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001994
1995 /* Set Rx to strict priority algorithm */
1996 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, RAA, MTL_RAA_SP);
1997}
1998
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05001999static void xgbe_queue_flow_control_threshold(struct xgbe_prv_data *pdata,
2000 unsigned int queue,
2001 unsigned int q_fifo_size)
2002{
2003 unsigned int frame_fifo_size;
2004 unsigned int rfa, rfd;
2005
2006 frame_fifo_size = XGMAC_FLOW_CONTROL_ALIGN(xgbe_get_max_frame(pdata));
2007
2008 if (pdata->pfcq[queue] && (q_fifo_size > pdata->pfc_rfa)) {
2009 /* PFC is active for this queue */
2010 rfa = pdata->pfc_rfa;
2011 rfd = rfa + frame_fifo_size;
2012 if (rfd > XGMAC_FLOW_CONTROL_MAX)
2013 rfd = XGMAC_FLOW_CONTROL_MAX;
2014 if (rfa >= XGMAC_FLOW_CONTROL_MAX)
2015 rfa = XGMAC_FLOW_CONTROL_MAX - XGMAC_FLOW_CONTROL_UNIT;
2016 } else {
2017 /* This path deals with just maximum frame sizes which are
2018 * limited to a jumbo frame of 9,000 (plus headers, etc.)
2019 * so we can never exceed the maximum allowable RFA/RFD
2020 * values.
2021 */
2022 if (q_fifo_size <= 2048) {
2023 /* rx_rfd to zero to signal no flow control */
2024 pdata->rx_rfa[queue] = 0;
2025 pdata->rx_rfd[queue] = 0;
2026 return;
2027 }
2028
2029 if (q_fifo_size <= 4096) {
2030 /* Between 2048 and 4096 */
2031 pdata->rx_rfa[queue] = 0; /* Full - 1024 bytes */
2032 pdata->rx_rfd[queue] = 1; /* Full - 1536 bytes */
2033 return;
2034 }
2035
2036 if (q_fifo_size <= frame_fifo_size) {
2037 /* Between 4096 and max-frame */
2038 pdata->rx_rfa[queue] = 2; /* Full - 2048 bytes */
2039 pdata->rx_rfd[queue] = 5; /* Full - 3584 bytes */
2040 return;
2041 }
2042
2043 if (q_fifo_size <= (frame_fifo_size * 3)) {
2044 /* Between max-frame and 3 max-frames,
2045 * trigger if we get just over a frame of data and
2046 * resume when we have just under half a frame left.
2047 */
2048 rfa = q_fifo_size - frame_fifo_size;
2049 rfd = rfa + (frame_fifo_size / 2);
2050 } else {
2051 /* Above 3 max-frames - trigger when just over
2052 * 2 frames of space available
2053 */
2054 rfa = frame_fifo_size * 2;
2055 rfa += XGMAC_FLOW_CONTROL_UNIT;
2056 rfd = rfa + frame_fifo_size;
2057 }
2058 }
2059
2060 pdata->rx_rfa[queue] = XGMAC_FLOW_CONTROL_VALUE(rfa);
2061 pdata->rx_rfd[queue] = XGMAC_FLOW_CONTROL_VALUE(rfd);
2062}
2063
2064static void xgbe_calculate_flow_control_threshold(struct xgbe_prv_data *pdata,
2065 unsigned int *fifo)
2066{
2067 unsigned int q_fifo_size;
2068 unsigned int i;
2069
2070 for (i = 0; i < pdata->rx_q_count; i++) {
2071 q_fifo_size = (fifo[i] + 1) * XGMAC_FIFO_UNIT;
2072
2073 xgbe_queue_flow_control_threshold(pdata, i, q_fifo_size);
2074 }
2075}
2076
2077static void xgbe_config_flow_control_threshold(struct xgbe_prv_data *pdata)
2078{
2079 unsigned int i;
2080
2081 for (i = 0; i < pdata->rx_q_count; i++) {
2082 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFA,
2083 pdata->rx_rfa[i]);
2084 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFD,
2085 pdata->rx_rfd[i]);
2086 }
2087}
2088
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002089static unsigned int xgbe_get_tx_fifo_size(struct xgbe_prv_data *pdata)
2090{
2091 unsigned int fifo_size;
2092
2093 /* Calculate the configured fifo size */
2094 fifo_size = 1 << (pdata->hw_feat.tx_fifo_size + 7);
2095
2096 /* The configured value may not be the actual amount of fifo RAM */
2097 return min_t(unsigned int, XGMAC_FIFO_TX_MAX, fifo_size);
2098}
2099
2100static unsigned int xgbe_get_rx_fifo_size(struct xgbe_prv_data *pdata)
2101{
2102 unsigned int fifo_size;
2103
2104 /* Calculate the configured fifo size */
2105 fifo_size = 1 << (pdata->hw_feat.rx_fifo_size + 7);
2106
2107 /* The configured value may not be the actual amount of fifo RAM */
2108 return min_t(unsigned int, XGMAC_FIFO_RX_MAX, fifo_size);
2109}
2110
2111static void xgbe_calculate_equal_fifo(unsigned int fifo_size,
2112 unsigned int queue_count,
2113 unsigned int *fifo)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002114{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002115 unsigned int q_fifo_size;
2116 unsigned int p_fifo;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002117 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002118
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002119 q_fifo_size = fifo_size / queue_count;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002120
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002121 /* Calculate the fifo setting by dividing the queue's fifo size
2122 * by the fifo allocation increment (with 0 representing the
2123 * base allocation increment so decrement the result by 1).
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002124 */
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002125 p_fifo = q_fifo_size / XGMAC_FIFO_UNIT;
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002126 if (p_fifo)
2127 p_fifo--;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002128
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002129 /* Distribute the fifo equally amongst the queues */
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002130 for (i = 0; i < queue_count; i++)
2131 fifo[i] = p_fifo;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002132}
2133
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002134static unsigned int xgbe_set_nonprio_fifos(unsigned int fifo_size,
2135 unsigned int queue_count,
2136 unsigned int *fifo)
2137{
2138 unsigned int i;
2139
2140 BUILD_BUG_ON_NOT_POWER_OF_2(XGMAC_FIFO_MIN_ALLOC);
2141
2142 if (queue_count <= IEEE_8021QAZ_MAX_TCS)
2143 return fifo_size;
2144
2145 /* Rx queues 9 and up are for specialized packets,
2146 * such as PTP or DCB control packets, etc. and
2147 * don't require a large fifo
2148 */
2149 for (i = IEEE_8021QAZ_MAX_TCS; i < queue_count; i++) {
2150 fifo[i] = (XGMAC_FIFO_MIN_ALLOC / XGMAC_FIFO_UNIT) - 1;
2151 fifo_size -= XGMAC_FIFO_MIN_ALLOC;
2152 }
2153
2154 return fifo_size;
2155}
2156
2157static unsigned int xgbe_get_pfc_delay(struct xgbe_prv_data *pdata)
2158{
2159 unsigned int delay;
2160
2161 /* If a delay has been provided, use that */
2162 if (pdata->pfc->delay)
2163 return pdata->pfc->delay / 8;
2164
2165 /* Allow for two maximum size frames */
2166 delay = xgbe_get_max_frame(pdata);
2167 delay += XGMAC_ETH_PREAMBLE;
2168 delay *= 2;
2169
2170 /* Allow for PFC frame */
2171 delay += XGMAC_PFC_DATA_LEN;
2172 delay += ETH_HLEN + ETH_FCS_LEN;
2173 delay += XGMAC_ETH_PREAMBLE;
2174
2175 /* Allow for miscellaneous delays (LPI exit, cable, etc.) */
2176 delay += XGMAC_PFC_DELAYS;
2177
2178 return delay;
2179}
2180
2181static unsigned int xgbe_get_pfc_queues(struct xgbe_prv_data *pdata)
2182{
2183 unsigned int count, prio_queues;
2184 unsigned int i;
2185
2186 if (!pdata->pfc->pfc_en)
2187 return 0;
2188
2189 count = 0;
2190 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2191 for (i = 0; i < prio_queues; i++) {
2192 if (!xgbe_is_pfc_queue(pdata, i))
2193 continue;
2194
2195 pdata->pfcq[i] = 1;
2196 count++;
2197 }
2198
2199 return count;
2200}
2201
2202static void xgbe_calculate_dcb_fifo(struct xgbe_prv_data *pdata,
2203 unsigned int fifo_size,
2204 unsigned int *fifo)
2205{
2206 unsigned int q_fifo_size, rem_fifo, addn_fifo;
2207 unsigned int prio_queues;
2208 unsigned int pfc_count;
2209 unsigned int i;
2210
2211 q_fifo_size = XGMAC_FIFO_ALIGN(xgbe_get_max_frame(pdata));
2212 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2213 pfc_count = xgbe_get_pfc_queues(pdata);
2214
2215 if (!pfc_count || ((q_fifo_size * prio_queues) > fifo_size)) {
2216 /* No traffic classes with PFC enabled or can't do lossless */
2217 xgbe_calculate_equal_fifo(fifo_size, prio_queues, fifo);
2218 return;
2219 }
2220
2221 /* Calculate how much fifo we have to play with */
2222 rem_fifo = fifo_size - (q_fifo_size * prio_queues);
2223
2224 /* Calculate how much more than base fifo PFC needs, which also
2225 * becomes the threshold activation point (RFA)
2226 */
2227 pdata->pfc_rfa = xgbe_get_pfc_delay(pdata);
2228 pdata->pfc_rfa = XGMAC_FLOW_CONTROL_ALIGN(pdata->pfc_rfa);
2229
2230 if (pdata->pfc_rfa > q_fifo_size) {
2231 addn_fifo = pdata->pfc_rfa - q_fifo_size;
2232 addn_fifo = XGMAC_FIFO_ALIGN(addn_fifo);
2233 } else {
2234 addn_fifo = 0;
2235 }
2236
2237 /* Calculate DCB fifo settings:
2238 * - distribute remaining fifo between the VLAN priority
2239 * queues based on traffic class PFC enablement and overall
2240 * priority (0 is lowest priority, so start at highest)
2241 */
2242 i = prio_queues;
2243 while (i > 0) {
2244 i--;
2245
2246 fifo[i] = (q_fifo_size / XGMAC_FIFO_UNIT) - 1;
2247
2248 if (!pdata->pfcq[i] || !addn_fifo)
2249 continue;
2250
2251 if (addn_fifo > rem_fifo) {
2252 netdev_warn(pdata->netdev,
2253 "RXq%u cannot set needed fifo size\n", i);
2254 if (!rem_fifo)
2255 continue;
2256
2257 addn_fifo = rem_fifo;
2258 }
2259
2260 fifo[i] += (addn_fifo / XGMAC_FIFO_UNIT);
2261 rem_fifo -= addn_fifo;
2262 }
2263
2264 if (rem_fifo) {
2265 unsigned int inc_fifo = rem_fifo / prio_queues;
2266
2267 /* Distribute remaining fifo across queues */
2268 for (i = 0; i < prio_queues; i++)
2269 fifo[i] += (inc_fifo / XGMAC_FIFO_UNIT);
2270 }
2271}
2272
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002273static void xgbe_config_tx_fifo_size(struct xgbe_prv_data *pdata)
2274{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002275 unsigned int fifo_size;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002276 unsigned int fifo[XGBE_MAX_QUEUES];
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002277 unsigned int i;
2278
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002279 fifo_size = xgbe_get_tx_fifo_size(pdata);
2280
2281 xgbe_calculate_equal_fifo(fifo_size, pdata->tx_q_count, fifo);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002282
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002283 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002284 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TQS, fifo[i]);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002285
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002286 netif_info(pdata, drv, pdata->netdev,
2287 "%d Tx hardware queues, %d byte fifo per queue\n",
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002288 pdata->tx_q_count, ((fifo[0] + 1) * XGMAC_FIFO_UNIT));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002289}
2290
2291static void xgbe_config_rx_fifo_size(struct xgbe_prv_data *pdata)
2292{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002293 unsigned int fifo_size;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002294 unsigned int fifo[XGBE_MAX_QUEUES];
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002295 unsigned int prio_queues;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002296 unsigned int i;
2297
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002298 /* Clear any DCB related fifo/queue information */
2299 memset(pdata->pfcq, 0, sizeof(pdata->pfcq));
2300 pdata->pfc_rfa = 0;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002301
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002302 fifo_size = xgbe_get_rx_fifo_size(pdata);
2303 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2304
2305 /* Assign a minimum fifo to the non-VLAN priority queues */
2306 fifo_size = xgbe_set_nonprio_fifos(fifo_size, pdata->rx_q_count, fifo);
2307
2308 if (pdata->pfc && pdata->ets)
2309 xgbe_calculate_dcb_fifo(pdata, fifo_size, fifo);
2310 else
2311 xgbe_calculate_equal_fifo(fifo_size, prio_queues, fifo);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002312
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002313 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002314 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RQS, fifo[i]);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002315
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002316 xgbe_calculate_flow_control_threshold(pdata, fifo);
2317 xgbe_config_flow_control_threshold(pdata);
2318
2319 if (pdata->pfc && pdata->ets && pdata->pfc->pfc_en) {
2320 netif_info(pdata, drv, pdata->netdev,
2321 "%u Rx hardware queues\n", pdata->rx_q_count);
2322 for (i = 0; i < pdata->rx_q_count; i++)
2323 netif_info(pdata, drv, pdata->netdev,
2324 "RxQ%u, %u byte fifo queue\n", i,
2325 ((fifo[i] + 1) * XGMAC_FIFO_UNIT));
2326 } else {
2327 netif_info(pdata, drv, pdata->netdev,
2328 "%u Rx hardware queues, %u byte fifo per queue\n",
2329 pdata->rx_q_count,
2330 ((fifo[0] + 1) * XGMAC_FIFO_UNIT));
2331 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002332}
2333
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002334static void xgbe_config_queue_mapping(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002335{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002336 unsigned int qptc, qptc_extra, queue;
2337 unsigned int prio_queues;
2338 unsigned int ppq, ppq_extra, prio;
2339 unsigned int mask;
2340 unsigned int i, j, reg, reg_val;
2341
2342 /* Map the MTL Tx Queues to Traffic Classes
2343 * Note: Tx Queues >= Traffic Classes
2344 */
2345 qptc = pdata->tx_q_count / pdata->hw_feat.tc_cnt;
2346 qptc_extra = pdata->tx_q_count % pdata->hw_feat.tc_cnt;
2347
2348 for (i = 0, queue = 0; i < pdata->hw_feat.tc_cnt; i++) {
2349 for (j = 0; j < qptc; j++) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002350 netif_dbg(pdata, drv, pdata->netdev,
2351 "TXq%u mapped to TC%u\n", queue, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002352 XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
2353 Q2TCMAP, i);
2354 pdata->q2tc_map[queue++] = i;
2355 }
2356
2357 if (i < qptc_extra) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002358 netif_dbg(pdata, drv, pdata->netdev,
2359 "TXq%u mapped to TC%u\n", queue, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002360 XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
2361 Q2TCMAP, i);
2362 pdata->q2tc_map[queue++] = i;
2363 }
2364 }
2365
2366 /* Map the 8 VLAN priority values to available MTL Rx queues */
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002367 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002368 ppq = IEEE_8021QAZ_MAX_TCS / prio_queues;
2369 ppq_extra = IEEE_8021QAZ_MAX_TCS % prio_queues;
2370
2371 reg = MAC_RQC2R;
2372 reg_val = 0;
2373 for (i = 0, prio = 0; i < prio_queues;) {
2374 mask = 0;
2375 for (j = 0; j < ppq; j++) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002376 netif_dbg(pdata, drv, pdata->netdev,
2377 "PRIO%u mapped to RXq%u\n", prio, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002378 mask |= (1 << prio);
2379 pdata->prio2q_map[prio++] = i;
2380 }
2381
2382 if (i < ppq_extra) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002383 netif_dbg(pdata, drv, pdata->netdev,
2384 "PRIO%u mapped to RXq%u\n", prio, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002385 mask |= (1 << prio);
2386 pdata->prio2q_map[prio++] = i;
2387 }
2388
2389 reg_val |= (mask << ((i++ % MAC_RQC2_Q_PER_REG) << 3));
2390
2391 if ((i % MAC_RQC2_Q_PER_REG) && (i != prio_queues))
2392 continue;
2393
2394 XGMAC_IOWRITE(pdata, reg, reg_val);
2395 reg += MAC_RQC2_INC;
2396 reg_val = 0;
2397 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002398
2399 /* Select dynamic mapping of MTL Rx queue to DMA Rx channel */
2400 reg = MTL_RQDCM0R;
2401 reg_val = 0;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002402 for (i = 0; i < pdata->rx_q_count;) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002403 reg_val |= (0x80 << ((i++ % MTL_RQDCM_Q_PER_REG) << 3));
2404
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002405 if ((i % MTL_RQDCM_Q_PER_REG) && (i != pdata->rx_q_count))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002406 continue;
2407
2408 XGMAC_IOWRITE(pdata, reg, reg_val);
2409
2410 reg += MTL_RQDCM_INC;
2411 reg_val = 0;
2412 }
2413}
2414
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002415static void xgbe_config_tc(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002416{
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002417 unsigned int offset, queue, prio;
2418 u8 i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002419
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002420 netdev_reset_tc(pdata->netdev);
2421 if (!pdata->num_tcs)
2422 return;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002423
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002424 netdev_set_num_tc(pdata->netdev, pdata->num_tcs);
2425
2426 for (i = 0, queue = 0, offset = 0; i < pdata->num_tcs; i++) {
2427 while ((queue < pdata->tx_q_count) &&
2428 (pdata->q2tc_map[queue] == i))
2429 queue++;
2430
2431 netif_dbg(pdata, drv, pdata->netdev, "TC%u using TXq%u-%u\n",
2432 i, offset, queue - 1);
2433 netdev_set_tc_queue(pdata->netdev, i, queue - offset, offset);
2434 offset = queue;
2435 }
2436
2437 if (!pdata->ets)
2438 return;
2439
2440 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++)
2441 netdev_set_prio_tc_map(pdata->netdev, prio,
2442 pdata->ets->prio_tc[prio]);
2443}
2444
2445static void xgbe_config_dcb_tc(struct xgbe_prv_data *pdata)
2446{
2447 struct ieee_ets *ets = pdata->ets;
2448 unsigned int total_weight, min_weight, weight;
2449 unsigned int mask, reg, reg_val;
2450 unsigned int i, prio;
2451
2452 if (!ets)
2453 return;
2454
2455 /* Set Tx to deficit weighted round robin scheduling algorithm (when
2456 * traffic class is using ETS algorithm)
2457 */
2458 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_DWRR);
2459
2460 /* Set Traffic Class algorithms */
2461 total_weight = pdata->netdev->mtu * pdata->hw_feat.tc_cnt;
2462 min_weight = total_weight / 100;
2463 if (!min_weight)
2464 min_weight = 1;
2465
2466 for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
2467 /* Map the priorities to the traffic class */
2468 mask = 0;
2469 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
2470 if (ets->prio_tc[prio] == i)
2471 mask |= (1 << prio);
2472 }
2473 mask &= 0xff;
2474
2475 netif_dbg(pdata, drv, pdata->netdev, "TC%u PRIO mask=%#x\n",
2476 i, mask);
2477 reg = MTL_TCPM0R + (MTL_TCPM_INC * (i / MTL_TCPM_TC_PER_REG));
2478 reg_val = XGMAC_IOREAD(pdata, reg);
2479
2480 reg_val &= ~(0xff << ((i % MTL_TCPM_TC_PER_REG) << 3));
2481 reg_val |= (mask << ((i % MTL_TCPM_TC_PER_REG) << 3));
2482
2483 XGMAC_IOWRITE(pdata, reg, reg_val);
2484
2485 /* Set the traffic class algorithm */
2486 switch (ets->tc_tsa[i]) {
2487 case IEEE_8021QAZ_TSA_STRICT:
2488 netif_dbg(pdata, drv, pdata->netdev,
2489 "TC%u using SP\n", i);
2490 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2491 MTL_TSA_SP);
2492 break;
2493 case IEEE_8021QAZ_TSA_ETS:
2494 weight = total_weight * ets->tc_tx_bw[i] / 100;
2495 weight = clamp(weight, min_weight, total_weight);
2496
2497 netif_dbg(pdata, drv, pdata->netdev,
2498 "TC%u using DWRR (weight %u)\n", i, weight);
2499 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2500 MTL_TSA_ETS);
2501 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW,
2502 weight);
2503 break;
2504 }
2505 }
2506
2507 xgbe_config_tc(pdata);
2508}
2509
2510static void xgbe_config_dcb_pfc(struct xgbe_prv_data *pdata)
2511{
2512 if (!test_bit(XGBE_DOWN, &pdata->dev_state)) {
2513 /* Just stop the Tx queues while Rx fifo is changed */
2514 netif_tx_stop_all_queues(pdata->netdev);
2515
2516 /* Suspend Rx so that fifo's can be adjusted */
2517 pdata->hw_if.disable_rx(pdata);
2518 }
2519
2520 xgbe_config_rx_fifo_size(pdata);
2521 xgbe_config_flow_control(pdata);
2522
2523 if (!test_bit(XGBE_DOWN, &pdata->dev_state)) {
2524 /* Resume Rx */
2525 pdata->hw_if.enable_rx(pdata);
2526
2527 /* Resume Tx queues */
2528 netif_tx_start_all_queues(pdata->netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002529 }
2530}
2531
2532static void xgbe_config_mac_address(struct xgbe_prv_data *pdata)
2533{
2534 xgbe_set_mac_address(pdata, pdata->netdev->dev_addr);
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05002535
2536 /* Filtering is done using perfect filtering and hash filtering */
2537 if (pdata->hw_feat.hash_table_size) {
2538 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HPF, 1);
2539 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HUC, 1);
2540 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HMC, 1);
2541 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002542}
2543
2544static void xgbe_config_jumbo_enable(struct xgbe_prv_data *pdata)
2545{
2546 unsigned int val;
2547
2548 val = (pdata->netdev->mtu > XGMAC_STD_PACKET_MTU) ? 1 : 0;
2549
2550 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, JE, val);
2551}
2552
Lendacky, Thomas916102c2015-01-16 12:46:45 -06002553static void xgbe_config_mac_speed(struct xgbe_prv_data *pdata)
2554{
Lendacky, Thomase57f7a32016-11-03 13:18:27 -05002555 xgbe_set_speed(pdata, pdata->phy_speed);
Lendacky, Thomas916102c2015-01-16 12:46:45 -06002556}
2557
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002558static void xgbe_config_checksum_offload(struct xgbe_prv_data *pdata)
2559{
2560 if (pdata->netdev->features & NETIF_F_RXCSUM)
2561 xgbe_enable_rx_csum(pdata);
2562 else
2563 xgbe_disable_rx_csum(pdata);
2564}
2565
2566static void xgbe_config_vlan_support(struct xgbe_prv_data *pdata)
2567{
Lendacky, Thomas6e5eed02014-06-24 16:19:12 -05002568 /* Indicate that VLAN Tx CTAGs come from context descriptors */
2569 XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, CSVL, 0);
2570 XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, VLTI, 1);
2571
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05002572 /* Set the current VLAN Hash Table register value */
2573 xgbe_update_vlan_hash_table(pdata);
2574
2575 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
2576 xgbe_enable_rx_vlan_filtering(pdata);
2577 else
2578 xgbe_disable_rx_vlan_filtering(pdata);
2579
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002580 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2581 xgbe_enable_rx_vlan_stripping(pdata);
2582 else
2583 xgbe_disable_rx_vlan_stripping(pdata);
2584}
2585
Lendacky, Thomas60265102014-09-05 18:02:30 -05002586static u64 xgbe_mmc_read(struct xgbe_prv_data *pdata, unsigned int reg_lo)
2587{
2588 bool read_hi;
2589 u64 val;
2590
Lendacky, Thomase5a20b92016-11-03 13:19:07 -05002591 if (pdata->vdata->mmc_64bit) {
2592 switch (reg_lo) {
2593 /* These registers are always 32 bit */
2594 case MMC_RXRUNTERROR:
2595 case MMC_RXJABBERERROR:
2596 case MMC_RXUNDERSIZE_G:
2597 case MMC_RXOVERSIZE_G:
2598 case MMC_RXWATCHDOGERROR:
2599 read_hi = false;
2600 break;
Lendacky, Thomas60265102014-09-05 18:02:30 -05002601
Lendacky, Thomase5a20b92016-11-03 13:19:07 -05002602 default:
2603 read_hi = true;
2604 }
2605 } else {
2606 switch (reg_lo) {
2607 /* These registers are always 64 bit */
2608 case MMC_TXOCTETCOUNT_GB_LO:
2609 case MMC_TXOCTETCOUNT_G_LO:
2610 case MMC_RXOCTETCOUNT_GB_LO:
2611 case MMC_RXOCTETCOUNT_G_LO:
2612 read_hi = true;
2613 break;
2614
2615 default:
2616 read_hi = false;
2617 }
Lendacky, Thomas3947d782015-09-30 08:52:38 -05002618 }
Lendacky, Thomas60265102014-09-05 18:02:30 -05002619
2620 val = XGMAC_IOREAD(pdata, reg_lo);
2621
2622 if (read_hi)
2623 val |= ((u64)XGMAC_IOREAD(pdata, reg_lo + 4) << 32);
2624
2625 return val;
2626}
2627
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002628static void xgbe_tx_mmc_int(struct xgbe_prv_data *pdata)
2629{
2630 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2631 unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_TISR);
2632
2633 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_GB))
2634 stats->txoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002635 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002636
2637 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_GB))
2638 stats->txframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002639 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002640
2641 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_G))
2642 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002643 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002644
2645 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_G))
2646 stats->txmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002647 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002648
2649 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX64OCTETS_GB))
2650 stats->tx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002651 xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002652
2653 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX65TO127OCTETS_GB))
2654 stats->tx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002655 xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002656
2657 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX128TO255OCTETS_GB))
2658 stats->tx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002659 xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002660
2661 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX256TO511OCTETS_GB))
2662 stats->tx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002663 xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002664
2665 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX512TO1023OCTETS_GB))
2666 stats->tx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002667 xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002668
2669 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX1024TOMAXOCTETS_GB))
2670 stats->tx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002671 xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002672
2673 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNICASTFRAMES_GB))
2674 stats->txunicastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002675 xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002676
2677 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_GB))
2678 stats->txmulticastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002679 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002680
2681 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_GB))
2682 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002683 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002684
2685 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNDERFLOWERROR))
2686 stats->txunderflowerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002687 xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002688
2689 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_G))
2690 stats->txoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002691 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002692
2693 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_G))
2694 stats->txframecount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002695 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002696
2697 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXPAUSEFRAMES))
2698 stats->txpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002699 xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002700
2701 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXVLANFRAMES_G))
2702 stats->txvlanframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002703 xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002704}
2705
2706static void xgbe_rx_mmc_int(struct xgbe_prv_data *pdata)
2707{
2708 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2709 unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_RISR);
2710
2711 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFRAMECOUNT_GB))
2712 stats->rxframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002713 xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002714
2715 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_GB))
2716 stats->rxoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002717 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002718
2719 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_G))
2720 stats->rxoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002721 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002722
2723 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXBROADCASTFRAMES_G))
2724 stats->rxbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002725 xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002726
2727 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXMULTICASTFRAMES_G))
2728 stats->rxmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002729 xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002730
2731 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXCRCERROR))
2732 stats->rxcrcerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002733 xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002734
2735 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXRUNTERROR))
2736 stats->rxrunterror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002737 xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002738
2739 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXJABBERERROR))
2740 stats->rxjabbererror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002741 xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002742
2743 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNDERSIZE_G))
2744 stats->rxundersize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002745 xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002746
2747 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOVERSIZE_G))
2748 stats->rxoversize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002749 xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002750
2751 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX64OCTETS_GB))
2752 stats->rx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002753 xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002754
2755 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX65TO127OCTETS_GB))
2756 stats->rx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002757 xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002758
2759 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX128TO255OCTETS_GB))
2760 stats->rx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002761 xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002762
2763 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX256TO511OCTETS_GB))
2764 stats->rx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002765 xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002766
2767 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX512TO1023OCTETS_GB))
2768 stats->rx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002769 xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002770
2771 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX1024TOMAXOCTETS_GB))
2772 stats->rx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002773 xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002774
2775 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNICASTFRAMES_G))
2776 stats->rxunicastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002777 xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002778
2779 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXLENGTHERROR))
2780 stats->rxlengtherror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002781 xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002782
2783 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOUTOFRANGETYPE))
2784 stats->rxoutofrangetype +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002785 xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002786
2787 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXPAUSEFRAMES))
2788 stats->rxpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002789 xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002790
2791 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFIFOOVERFLOW))
2792 stats->rxfifooverflow +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002793 xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002794
2795 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXVLANFRAMES_GB))
2796 stats->rxvlanframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002797 xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002798
2799 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXWATCHDOGERROR))
2800 stats->rxwatchdogerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002801 xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002802}
2803
2804static void xgbe_read_mmc_stats(struct xgbe_prv_data *pdata)
2805{
2806 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2807
2808 /* Freeze counters */
2809 XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 1);
2810
2811 stats->txoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002812 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002813
2814 stats->txframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002815 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002816
2817 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002818 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002819
2820 stats->txmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002821 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002822
2823 stats->tx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002824 xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002825
2826 stats->tx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002827 xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002828
2829 stats->tx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002830 xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002831
2832 stats->tx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002833 xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002834
2835 stats->tx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002836 xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002837
2838 stats->tx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002839 xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002840
2841 stats->txunicastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002842 xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002843
2844 stats->txmulticastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002845 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002846
2847 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002848 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002849
2850 stats->txunderflowerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002851 xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002852
2853 stats->txoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002854 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002855
2856 stats->txframecount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002857 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002858
2859 stats->txpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002860 xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002861
2862 stats->txvlanframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002863 xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002864
2865 stats->rxframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002866 xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002867
2868 stats->rxoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002869 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002870
2871 stats->rxoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002872 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002873
2874 stats->rxbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002875 xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002876
2877 stats->rxmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002878 xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002879
2880 stats->rxcrcerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002881 xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002882
2883 stats->rxrunterror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002884 xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002885
2886 stats->rxjabbererror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002887 xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002888
2889 stats->rxundersize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002890 xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002891
2892 stats->rxoversize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002893 xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002894
2895 stats->rx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002896 xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002897
2898 stats->rx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002899 xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002900
2901 stats->rx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002902 xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002903
2904 stats->rx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002905 xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002906
2907 stats->rx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002908 xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002909
2910 stats->rx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002911 xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002912
2913 stats->rxunicastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002914 xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002915
2916 stats->rxlengtherror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002917 xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002918
2919 stats->rxoutofrangetype +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002920 xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002921
2922 stats->rxpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002923 xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002924
2925 stats->rxfifooverflow +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002926 xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002927
2928 stats->rxvlanframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002929 xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002930
2931 stats->rxwatchdogerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002932 xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002933
2934 /* Un-freeze counters */
2935 XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 0);
2936}
2937
2938static void xgbe_config_mmc(struct xgbe_prv_data *pdata)
2939{
2940 /* Set counters to reset on read */
2941 XGMAC_IOWRITE_BITS(pdata, MMC_CR, ROR, 1);
2942
2943 /* Reset the counters */
2944 XGMAC_IOWRITE_BITS(pdata, MMC_CR, CR, 1);
2945}
2946
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05002947static void xgbe_txq_prepare_tx_stop(struct xgbe_prv_data *pdata,
2948 unsigned int queue)
2949{
2950 unsigned int tx_status;
2951 unsigned long tx_timeout;
2952
2953 /* The Tx engine cannot be stopped if it is actively processing
2954 * packets. Wait for the Tx queue to empty the Tx fifo. Don't
2955 * wait forever though...
2956 */
2957 tx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
2958 while (time_before(jiffies, tx_timeout)) {
2959 tx_status = XGMAC_MTL_IOREAD(pdata, queue, MTL_Q_TQDR);
2960 if ((XGMAC_GET_BITS(tx_status, MTL_Q_TQDR, TRCSTS) != 1) &&
2961 (XGMAC_GET_BITS(tx_status, MTL_Q_TQDR, TXQSTS) == 0))
2962 break;
2963
2964 usleep_range(500, 1000);
2965 }
2966
2967 if (!time_before(jiffies, tx_timeout))
2968 netdev_info(pdata->netdev,
2969 "timed out waiting for Tx queue %u to empty\n",
2970 queue);
2971}
2972
Lendacky, Thomas16edd342014-11-20 11:03:32 -06002973static void xgbe_prepare_tx_stop(struct xgbe_prv_data *pdata,
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05002974 unsigned int queue)
Lendacky, Thomas16edd342014-11-20 11:03:32 -06002975{
2976 unsigned int tx_dsr, tx_pos, tx_qidx;
2977 unsigned int tx_status;
2978 unsigned long tx_timeout;
2979
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05002980 if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) > 0x20)
2981 return xgbe_txq_prepare_tx_stop(pdata, queue);
2982
Lendacky, Thomas16edd342014-11-20 11:03:32 -06002983 /* Calculate the status register to read and the position within */
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05002984 if (queue < DMA_DSRX_FIRST_QUEUE) {
Lendacky, Thomas16edd342014-11-20 11:03:32 -06002985 tx_dsr = DMA_DSR0;
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05002986 tx_pos = (queue * DMA_DSR_Q_WIDTH) + DMA_DSR0_TPS_START;
Lendacky, Thomas16edd342014-11-20 11:03:32 -06002987 } else {
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05002988 tx_qidx = queue - DMA_DSRX_FIRST_QUEUE;
Lendacky, Thomas16edd342014-11-20 11:03:32 -06002989
2990 tx_dsr = DMA_DSR1 + ((tx_qidx / DMA_DSRX_QPR) * DMA_DSRX_INC);
2991 tx_pos = ((tx_qidx % DMA_DSRX_QPR) * DMA_DSR_Q_WIDTH) +
2992 DMA_DSRX_TPS_START;
2993 }
2994
2995 /* The Tx engine cannot be stopped if it is actively processing
2996 * descriptors. Wait for the Tx engine to enter the stopped or
2997 * suspended state. Don't wait forever though...
2998 */
2999 tx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
3000 while (time_before(jiffies, tx_timeout)) {
3001 tx_status = XGMAC_IOREAD(pdata, tx_dsr);
3002 tx_status = GET_BITS(tx_status, tx_pos, DMA_DSR_TPS_WIDTH);
3003 if ((tx_status == DMA_TPS_STOPPED) ||
3004 (tx_status == DMA_TPS_SUSPENDED))
3005 break;
3006
3007 usleep_range(500, 1000);
3008 }
3009
3010 if (!time_before(jiffies, tx_timeout))
3011 netdev_info(pdata->netdev,
3012 "timed out waiting for Tx DMA channel %u to stop\n",
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003013 queue);
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003014}
3015
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003016static void xgbe_enable_tx(struct xgbe_prv_data *pdata)
3017{
3018 struct xgbe_channel *channel;
3019 unsigned int i;
3020
3021 /* Enable each Tx DMA channel */
3022 channel = pdata->channel;
3023 for (i = 0; i < pdata->channel_count; i++, channel++) {
3024 if (!channel->tx_ring)
3025 break;
3026
3027 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 1);
3028 }
3029
3030 /* Enable each Tx queue */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05003031 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003032 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN,
3033 MTL_Q_ENABLED);
3034
3035 /* Enable MAC Tx */
3036 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
3037}
3038
3039static void xgbe_disable_tx(struct xgbe_prv_data *pdata)
3040{
3041 struct xgbe_channel *channel;
3042 unsigned int i;
3043
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003044 /* Prepare for Tx DMA channel stop */
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003045 for (i = 0; i < pdata->tx_q_count; i++)
3046 xgbe_prepare_tx_stop(pdata, i);
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003047
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003048 /* Disable MAC Tx */
3049 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
3050
3051 /* Disable each Tx queue */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05003052 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003053 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN, 0);
3054
3055 /* Disable each Tx DMA channel */
3056 channel = pdata->channel;
3057 for (i = 0; i < pdata->channel_count; i++, channel++) {
3058 if (!channel->tx_ring)
3059 break;
3060
3061 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 0);
3062 }
3063}
3064
Lendacky, Thomasc3727d62016-02-17 11:49:16 -06003065static void xgbe_prepare_rx_stop(struct xgbe_prv_data *pdata,
3066 unsigned int queue)
3067{
3068 unsigned int rx_status;
3069 unsigned long rx_timeout;
3070
3071 /* The Rx engine cannot be stopped if it is actively processing
3072 * packets. Wait for the Rx queue to empty the Rx fifo. Don't
3073 * wait forever though...
3074 */
3075 rx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
3076 while (time_before(jiffies, rx_timeout)) {
3077 rx_status = XGMAC_MTL_IOREAD(pdata, queue, MTL_Q_RQDR);
3078 if ((XGMAC_GET_BITS(rx_status, MTL_Q_RQDR, PRXQ) == 0) &&
3079 (XGMAC_GET_BITS(rx_status, MTL_Q_RQDR, RXQSTS) == 0))
3080 break;
3081
3082 usleep_range(500, 1000);
3083 }
3084
3085 if (!time_before(jiffies, rx_timeout))
3086 netdev_info(pdata->netdev,
3087 "timed out waiting for Rx queue %u to empty\n",
3088 queue);
3089}
3090
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003091static void xgbe_enable_rx(struct xgbe_prv_data *pdata)
3092{
3093 struct xgbe_channel *channel;
3094 unsigned int reg_val, i;
3095
3096 /* Enable each Rx DMA channel */
3097 channel = pdata->channel;
3098 for (i = 0; i < pdata->channel_count; i++, channel++) {
3099 if (!channel->rx_ring)
3100 break;
3101
3102 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 1);
3103 }
3104
3105 /* Enable each Rx queue */
3106 reg_val = 0;
Lendacky, Thomas853eb162014-07-29 08:57:31 -05003107 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003108 reg_val |= (0x02 << (i << 1));
3109 XGMAC_IOWRITE(pdata, MAC_RQC0R, reg_val);
3110
3111 /* Enable MAC Rx */
3112 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 1);
3113 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 1);
3114 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 1);
3115 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 1);
3116}
3117
3118static void xgbe_disable_rx(struct xgbe_prv_data *pdata)
3119{
3120 struct xgbe_channel *channel;
3121 unsigned int i;
3122
3123 /* Disable MAC Rx */
3124 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 0);
3125 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 0);
3126 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 0);
3127 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 0);
3128
Lendacky, Thomasc3727d62016-02-17 11:49:16 -06003129 /* Prepare for Rx DMA channel stop */
3130 for (i = 0; i < pdata->rx_q_count; i++)
3131 xgbe_prepare_rx_stop(pdata, i);
3132
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003133 /* Disable each Rx queue */
3134 XGMAC_IOWRITE(pdata, MAC_RQC0R, 0);
3135
3136 /* Disable each Rx DMA channel */
3137 channel = pdata->channel;
3138 for (i = 0; i < pdata->channel_count; i++, channel++) {
3139 if (!channel->rx_ring)
3140 break;
3141
3142 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 0);
3143 }
3144}
3145
3146static void xgbe_powerup_tx(struct xgbe_prv_data *pdata)
3147{
3148 struct xgbe_channel *channel;
3149 unsigned int i;
3150
3151 /* Enable each Tx DMA channel */
3152 channel = pdata->channel;
3153 for (i = 0; i < pdata->channel_count; i++, channel++) {
3154 if (!channel->tx_ring)
3155 break;
3156
3157 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 1);
3158 }
3159
3160 /* Enable MAC Tx */
3161 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
3162}
3163
3164static void xgbe_powerdown_tx(struct xgbe_prv_data *pdata)
3165{
3166 struct xgbe_channel *channel;
3167 unsigned int i;
3168
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003169 /* Prepare for Tx DMA channel stop */
Lendacky, Thomas4b8acdf2016-11-03 13:19:17 -05003170 for (i = 0; i < pdata->tx_q_count; i++)
3171 xgbe_prepare_tx_stop(pdata, i);
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003172
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003173 /* Disable MAC Tx */
3174 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
3175
3176 /* Disable each Tx DMA channel */
3177 channel = pdata->channel;
3178 for (i = 0; i < pdata->channel_count; i++, channel++) {
3179 if (!channel->tx_ring)
3180 break;
3181
3182 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 0);
3183 }
3184}
3185
3186static void xgbe_powerup_rx(struct xgbe_prv_data *pdata)
3187{
3188 struct xgbe_channel *channel;
3189 unsigned int i;
3190
3191 /* Enable each Rx DMA channel */
3192 channel = pdata->channel;
3193 for (i = 0; i < pdata->channel_count; i++, channel++) {
3194 if (!channel->rx_ring)
3195 break;
3196
3197 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 1);
3198 }
3199}
3200
3201static void xgbe_powerdown_rx(struct xgbe_prv_data *pdata)
3202{
3203 struct xgbe_channel *channel;
3204 unsigned int i;
3205
3206 /* Disable each Rx DMA channel */
3207 channel = pdata->channel;
3208 for (i = 0; i < pdata->channel_count; i++, channel++) {
3209 if (!channel->rx_ring)
3210 break;
3211
3212 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 0);
3213 }
3214}
3215
3216static int xgbe_init(struct xgbe_prv_data *pdata)
3217{
3218 struct xgbe_desc_if *desc_if = &pdata->desc_if;
3219 int ret;
3220
3221 DBGPR("-->xgbe_init\n");
3222
3223 /* Flush Tx queues */
3224 ret = xgbe_flush_tx_queues(pdata);
3225 if (ret)
3226 return ret;
3227
3228 /*
3229 * Initialize DMA related features
3230 */
3231 xgbe_config_dma_bus(pdata);
3232 xgbe_config_dma_cache(pdata);
3233 xgbe_config_osp_mode(pdata);
3234 xgbe_config_pblx8(pdata);
3235 xgbe_config_tx_pbl_val(pdata);
3236 xgbe_config_rx_pbl_val(pdata);
3237 xgbe_config_rx_coalesce(pdata);
3238 xgbe_config_tx_coalesce(pdata);
3239 xgbe_config_rx_buffer_size(pdata);
3240 xgbe_config_tso_mode(pdata);
Lendacky, Thomas174fd252014-11-04 16:06:50 -06003241 xgbe_config_sph_mode(pdata);
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003242 xgbe_config_rss(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003243 desc_if->wrapper_tx_desc_init(pdata);
3244 desc_if->wrapper_rx_desc_init(pdata);
3245 xgbe_enable_dma_interrupts(pdata);
3246
3247 /*
3248 * Initialize MTL related features
3249 */
3250 xgbe_config_mtl_mode(pdata);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003251 xgbe_config_queue_mapping(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003252 xgbe_config_tsf_mode(pdata, pdata->tx_sf_mode);
3253 xgbe_config_rsf_mode(pdata, pdata->rx_sf_mode);
3254 xgbe_config_tx_threshold(pdata, pdata->tx_threshold);
3255 xgbe_config_rx_threshold(pdata, pdata->rx_threshold);
3256 xgbe_config_tx_fifo_size(pdata);
3257 xgbe_config_rx_fifo_size(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003258 /*TODO: Error Packet and undersized good Packet forwarding enable
3259 (FEP and FUP)
3260 */
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003261 xgbe_config_dcb_tc(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003262 xgbe_enable_mtl_interrupts(pdata);
3263
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003264 /*
3265 * Initialize MAC related features
3266 */
3267 xgbe_config_mac_address(pdata);
Lendacky, Thomasb8763822015-04-09 12:11:57 -05003268 xgbe_config_rx_mode(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003269 xgbe_config_jumbo_enable(pdata);
3270 xgbe_config_flow_control(pdata);
Lendacky, Thomas916102c2015-01-16 12:46:45 -06003271 xgbe_config_mac_speed(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003272 xgbe_config_checksum_offload(pdata);
3273 xgbe_config_vlan_support(pdata);
3274 xgbe_config_mmc(pdata);
3275 xgbe_enable_mac_interrupts(pdata);
3276
3277 DBGPR("<--xgbe_init\n");
3278
3279 return 0;
3280}
3281
3282void xgbe_init_function_ptrs_dev(struct xgbe_hw_if *hw_if)
3283{
3284 DBGPR("-->xgbe_init_function_ptrs\n");
3285
3286 hw_if->tx_complete = xgbe_tx_complete;
3287
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003288 hw_if->set_mac_address = xgbe_set_mac_address;
Lendacky, Thomasb8763822015-04-09 12:11:57 -05003289 hw_if->config_rx_mode = xgbe_config_rx_mode;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003290
3291 hw_if->enable_rx_csum = xgbe_enable_rx_csum;
3292 hw_if->disable_rx_csum = xgbe_disable_rx_csum;
3293
3294 hw_if->enable_rx_vlan_stripping = xgbe_enable_rx_vlan_stripping;
3295 hw_if->disable_rx_vlan_stripping = xgbe_disable_rx_vlan_stripping;
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05003296 hw_if->enable_rx_vlan_filtering = xgbe_enable_rx_vlan_filtering;
3297 hw_if->disable_rx_vlan_filtering = xgbe_disable_rx_vlan_filtering;
3298 hw_if->update_vlan_hash_table = xgbe_update_vlan_hash_table;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003299
3300 hw_if->read_mmd_regs = xgbe_read_mmd_regs;
3301 hw_if->write_mmd_regs = xgbe_write_mmd_regs;
3302
Lendacky, Thomase57f7a32016-11-03 13:18:27 -05003303 hw_if->set_speed = xgbe_set_speed;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003304
3305 hw_if->enable_tx = xgbe_enable_tx;
3306 hw_if->disable_tx = xgbe_disable_tx;
3307 hw_if->enable_rx = xgbe_enable_rx;
3308 hw_if->disable_rx = xgbe_disable_rx;
3309
3310 hw_if->powerup_tx = xgbe_powerup_tx;
3311 hw_if->powerdown_tx = xgbe_powerdown_tx;
3312 hw_if->powerup_rx = xgbe_powerup_rx;
3313 hw_if->powerdown_rx = xgbe_powerdown_rx;
3314
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06003315 hw_if->dev_xmit = xgbe_dev_xmit;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003316 hw_if->dev_read = xgbe_dev_read;
3317 hw_if->enable_int = xgbe_enable_int;
3318 hw_if->disable_int = xgbe_disable_int;
3319 hw_if->init = xgbe_init;
3320 hw_if->exit = xgbe_exit;
3321
3322 /* Descriptor related Sequences have to be initialized here */
3323 hw_if->tx_desc_init = xgbe_tx_desc_init;
3324 hw_if->rx_desc_init = xgbe_rx_desc_init;
3325 hw_if->tx_desc_reset = xgbe_tx_desc_reset;
3326 hw_if->rx_desc_reset = xgbe_rx_desc_reset;
3327 hw_if->is_last_desc = xgbe_is_last_desc;
3328 hw_if->is_context_desc = xgbe_is_context_desc;
Lendacky, Thomas16958a22014-11-20 11:04:08 -06003329 hw_if->tx_start_xmit = xgbe_tx_start_xmit;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003330
3331 /* For FLOW ctrl */
3332 hw_if->config_tx_flow_control = xgbe_config_tx_flow_control;
3333 hw_if->config_rx_flow_control = xgbe_config_rx_flow_control;
3334
3335 /* For RX coalescing */
3336 hw_if->config_rx_coalesce = xgbe_config_rx_coalesce;
3337 hw_if->config_tx_coalesce = xgbe_config_tx_coalesce;
3338 hw_if->usec_to_riwt = xgbe_usec_to_riwt;
3339 hw_if->riwt_to_usec = xgbe_riwt_to_usec;
3340
3341 /* For RX and TX threshold config */
3342 hw_if->config_rx_threshold = xgbe_config_rx_threshold;
3343 hw_if->config_tx_threshold = xgbe_config_tx_threshold;
3344
3345 /* For RX and TX Store and Forward Mode config */
3346 hw_if->config_rsf_mode = xgbe_config_rsf_mode;
3347 hw_if->config_tsf_mode = xgbe_config_tsf_mode;
3348
3349 /* For TX DMA Operating on Second Frame config */
3350 hw_if->config_osp_mode = xgbe_config_osp_mode;
3351
3352 /* For RX and TX PBL config */
3353 hw_if->config_rx_pbl_val = xgbe_config_rx_pbl_val;
3354 hw_if->get_rx_pbl_val = xgbe_get_rx_pbl_val;
3355 hw_if->config_tx_pbl_val = xgbe_config_tx_pbl_val;
3356 hw_if->get_tx_pbl_val = xgbe_get_tx_pbl_val;
3357 hw_if->config_pblx8 = xgbe_config_pblx8;
3358
3359 /* For MMC statistics support */
3360 hw_if->tx_mmc_int = xgbe_tx_mmc_int;
3361 hw_if->rx_mmc_int = xgbe_rx_mmc_int;
3362 hw_if->read_mmc_stats = xgbe_read_mmc_stats;
3363
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05003364 /* For PTP config */
3365 hw_if->config_tstamp = xgbe_config_tstamp;
3366 hw_if->update_tstamp_addend = xgbe_update_tstamp_addend;
3367 hw_if->set_tstamp_time = xgbe_set_tstamp_time;
3368 hw_if->get_tstamp_time = xgbe_get_tstamp_time;
3369 hw_if->get_tx_tstamp = xgbe_get_tx_tstamp;
3370
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003371 /* For Data Center Bridging config */
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06003372 hw_if->config_tc = xgbe_config_tc;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003373 hw_if->config_dcb_tc = xgbe_config_dcb_tc;
3374 hw_if->config_dcb_pfc = xgbe_config_dcb_pfc;
3375
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003376 /* For Receive Side Scaling */
3377 hw_if->enable_rss = xgbe_enable_rss;
3378 hw_if->disable_rss = xgbe_disable_rss;
Lendacky, Thomasf6ac8622014-11-04 16:07:23 -06003379 hw_if->set_rss_hash_key = xgbe_set_rss_hash_key;
3380 hw_if->set_rss_lookup_table = xgbe_set_rss_lookup_table;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003381
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003382 DBGPR("<--xgbe_init_function_ptrs\n");
3383}