blob: f8fffea6ced51b56d84041c5469d368e842b2a3e [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
720static int xgbe_set_gmii_speed(struct xgbe_prv_data *pdata)
721{
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600722 if (XGMAC_IOREAD_BITS(pdata, MAC_TCR, SS) == 0x3)
723 return 0;
724
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500725 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, 0x3);
726
727 return 0;
728}
729
730static int xgbe_set_gmii_2500_speed(struct xgbe_prv_data *pdata)
731{
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600732 if (XGMAC_IOREAD_BITS(pdata, MAC_TCR, SS) == 0x2)
733 return 0;
734
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500735 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, 0x2);
736
737 return 0;
738}
739
740static int xgbe_set_xgmii_speed(struct xgbe_prv_data *pdata)
741{
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600742 if (XGMAC_IOREAD_BITS(pdata, MAC_TCR, SS) == 0)
743 return 0;
744
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500745 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, 0);
746
747 return 0;
748}
749
Lendacky, Thomasb4eee842016-02-17 11:48:08 -0600750static int xgbe_enable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
751{
752 /* Put the VLAN tag in the Rx descriptor */
753 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLRXS, 1);
754
755 /* Don't check the VLAN type */
756 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, DOVLTC, 1);
757
758 /* Check only C-TAG (0x8100) packets */
759 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ERSVLM, 0);
760
761 /* Don't consider an S-TAG (0x88A8) packet as a VLAN packet */
762 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ESVL, 0);
763
764 /* Enable VLAN tag stripping */
765 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0x3);
766
767 return 0;
768}
769
770static int xgbe_disable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
771{
772 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0);
773
774 return 0;
775}
776
777static int xgbe_enable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
778{
779 /* Enable VLAN filtering */
780 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 1);
781
782 /* Enable VLAN Hash Table filtering */
783 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTHM, 1);
784
785 /* Disable VLAN tag inverse matching */
786 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTIM, 0);
787
788 /* Only filter on the lower 12-bits of the VLAN tag */
789 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ETV, 1);
790
791 /* In order for the VLAN Hash Table filtering to be effective,
792 * the VLAN tag identifier in the VLAN Tag Register must not
793 * be zero. Set the VLAN tag identifier to "1" to enable the
794 * VLAN Hash Table filtering. This implies that a VLAN tag of
795 * 1 will always pass filtering.
796 */
797 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VL, 1);
798
799 return 0;
800}
801
802static int xgbe_disable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
803{
804 /* Disable VLAN filtering */
805 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 0);
806
807 return 0;
808}
809
810static u32 xgbe_vid_crc32_le(__le16 vid_le)
811{
812 u32 poly = 0xedb88320; /* CRCPOLY_LE */
813 u32 crc = ~0;
814 u32 temp = 0;
815 unsigned char *data = (unsigned char *)&vid_le;
816 unsigned char data_byte = 0;
817 int i, bits;
818
819 bits = get_bitmask_order(VLAN_VID_MASK);
820 for (i = 0; i < bits; i++) {
821 if ((i % 8) == 0)
822 data_byte = data[i / 8];
823
824 temp = ((crc & 1) ^ data_byte) & 1;
825 crc >>= 1;
826 data_byte >>= 1;
827
828 if (temp)
829 crc ^= poly;
830 }
831
832 return crc;
833}
834
835static int xgbe_update_vlan_hash_table(struct xgbe_prv_data *pdata)
836{
837 u32 crc;
838 u16 vid;
839 __le16 vid_le;
840 u16 vlan_hash_table = 0;
841
842 /* Generate the VLAN Hash Table value */
843 for_each_set_bit(vid, pdata->active_vlans, VLAN_N_VID) {
844 /* Get the CRC32 value of the VLAN ID */
845 vid_le = cpu_to_le16(vid);
846 crc = bitrev32(~xgbe_vid_crc32_le(vid_le)) >> 28;
847
848 vlan_hash_table |= (1 << crc);
849 }
850
851 /* Set the VLAN Hash Table filtering register */
852 XGMAC_IOWRITE_BITS(pdata, MAC_VLANHTR, VLHT, vlan_hash_table);
853
854 return 0;
855}
856
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500857static int xgbe_set_promiscuous_mode(struct xgbe_prv_data *pdata,
858 unsigned int enable)
859{
860 unsigned int val = enable ? 1 : 0;
861
862 if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PR) == val)
863 return 0;
864
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500865 netif_dbg(pdata, drv, pdata->netdev, "%s promiscuous mode\n",
866 enable ? "entering" : "leaving");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500867 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, val);
868
Lendacky, Thomasb4eee842016-02-17 11:48:08 -0600869 /* Hardware will still perform VLAN filtering in promiscuous mode */
870 if (enable) {
871 xgbe_disable_rx_vlan_filtering(pdata);
872 } else {
873 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
874 xgbe_enable_rx_vlan_filtering(pdata);
875 }
876
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500877 return 0;
878}
879
880static int xgbe_set_all_multicast_mode(struct xgbe_prv_data *pdata,
881 unsigned int enable)
882{
883 unsigned int val = enable ? 1 : 0;
884
885 if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PM) == val)
886 return 0;
887
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500888 netif_dbg(pdata, drv, pdata->netdev, "%s allmulti mode\n",
889 enable ? "entering" : "leaving");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500890 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PM, val);
891
892 return 0;
893}
894
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500895static void xgbe_set_mac_reg(struct xgbe_prv_data *pdata,
896 struct netdev_hw_addr *ha, unsigned int *mac_reg)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500897{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500898 unsigned int mac_addr_hi, mac_addr_lo;
899 u8 *mac_addr;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500900
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500901 mac_addr_lo = 0;
902 mac_addr_hi = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500903
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500904 if (ha) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500905 mac_addr = (u8 *)&mac_addr_lo;
906 mac_addr[0] = ha->addr[0];
907 mac_addr[1] = ha->addr[1];
908 mac_addr[2] = ha->addr[2];
909 mac_addr[3] = ha->addr[3];
910 mac_addr = (u8 *)&mac_addr_hi;
911 mac_addr[0] = ha->addr[4];
912 mac_addr[1] = ha->addr[5];
913
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500914 netif_dbg(pdata, drv, pdata->netdev,
915 "adding mac address %pM at %#x\n",
916 ha->addr, *mac_reg);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500917
918 XGMAC_SET_BITS(mac_addr_hi, MAC_MACA1HR, AE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500919 }
920
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500921 XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_hi);
922 *mac_reg += MAC_MACA_INC;
923 XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_lo);
924 *mac_reg += MAC_MACA_INC;
925}
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500926
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500927static void xgbe_set_mac_addn_addrs(struct xgbe_prv_data *pdata)
928{
929 struct net_device *netdev = pdata->netdev;
930 struct netdev_hw_addr *ha;
931 unsigned int mac_reg;
932 unsigned int addn_macs;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500933
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500934 mac_reg = MAC_MACA1HR;
935 addn_macs = pdata->hw_feat.addn_mac;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500936
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500937 if (netdev_uc_count(netdev) > addn_macs) {
938 xgbe_set_promiscuous_mode(pdata, 1);
939 } else {
940 netdev_for_each_uc_addr(ha, netdev) {
941 xgbe_set_mac_reg(pdata, ha, &mac_reg);
942 addn_macs--;
943 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500944
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500945 if (netdev_mc_count(netdev) > addn_macs) {
946 xgbe_set_all_multicast_mode(pdata, 1);
947 } else {
948 netdev_for_each_mc_addr(ha, netdev) {
949 xgbe_set_mac_reg(pdata, ha, &mac_reg);
950 addn_macs--;
951 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500952 }
953 }
954
955 /* Clear remaining additional MAC address entries */
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500956 while (addn_macs--)
957 xgbe_set_mac_reg(pdata, NULL, &mac_reg);
958}
959
960static void xgbe_set_mac_hash_table(struct xgbe_prv_data *pdata)
961{
962 struct net_device *netdev = pdata->netdev;
963 struct netdev_hw_addr *ha;
964 unsigned int hash_reg;
965 unsigned int hash_table_shift, hash_table_count;
966 u32 hash_table[XGBE_MAC_HASH_TABLE_SIZE];
967 u32 crc;
968 unsigned int i;
969
970 hash_table_shift = 26 - (pdata->hw_feat.hash_table_size >> 7);
971 hash_table_count = pdata->hw_feat.hash_table_size / 32;
972 memset(hash_table, 0, sizeof(hash_table));
973
974 /* Build the MAC Hash Table register values */
975 netdev_for_each_uc_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));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500979 }
980
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500981 netdev_for_each_mc_addr(ha, netdev) {
982 crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
983 crc >>= hash_table_shift;
984 hash_table[crc >> 5] |= (1 << (crc & 0x1f));
985 }
986
987 /* Set the MAC Hash Table registers */
988 hash_reg = MAC_HTR0;
989 for (i = 0; i < hash_table_count; i++) {
990 XGMAC_IOWRITE(pdata, hash_reg, hash_table[i]);
991 hash_reg += MAC_HTR_INC;
992 }
993}
994
995static int xgbe_add_mac_addresses(struct xgbe_prv_data *pdata)
996{
997 if (pdata->hw_feat.hash_table_size)
998 xgbe_set_mac_hash_table(pdata);
999 else
1000 xgbe_set_mac_addn_addrs(pdata);
1001
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001002 return 0;
1003}
1004
1005static int xgbe_set_mac_address(struct xgbe_prv_data *pdata, u8 *addr)
1006{
1007 unsigned int mac_addr_hi, mac_addr_lo;
1008
1009 mac_addr_hi = (addr[5] << 8) | (addr[4] << 0);
1010 mac_addr_lo = (addr[3] << 24) | (addr[2] << 16) |
1011 (addr[1] << 8) | (addr[0] << 0);
1012
1013 XGMAC_IOWRITE(pdata, MAC_MACA0HR, mac_addr_hi);
1014 XGMAC_IOWRITE(pdata, MAC_MACA0LR, mac_addr_lo);
1015
1016 return 0;
1017}
1018
Lendacky, Thomasb8763822015-04-09 12:11:57 -05001019static int xgbe_config_rx_mode(struct xgbe_prv_data *pdata)
1020{
1021 struct net_device *netdev = pdata->netdev;
1022 unsigned int pr_mode, am_mode;
1023
1024 pr_mode = ((netdev->flags & IFF_PROMISC) != 0);
1025 am_mode = ((netdev->flags & IFF_ALLMULTI) != 0);
1026
1027 xgbe_set_promiscuous_mode(pdata, pr_mode);
1028 xgbe_set_all_multicast_mode(pdata, am_mode);
1029
1030 xgbe_add_mac_addresses(pdata);
1031
1032 return 0;
1033}
1034
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001035static int xgbe_read_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
1036 int mmd_reg)
1037{
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001038 unsigned long flags;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001039 unsigned int mmd_address;
1040 int mmd_data;
1041
1042 if (mmd_reg & MII_ADDR_C45)
1043 mmd_address = mmd_reg & ~MII_ADDR_C45;
1044 else
1045 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1046
1047 /* The PCS registers are accessed using mmio. The underlying APB3
1048 * management interface uses indirect addressing to access the MMD
1049 * register sets. This requires accessing of the PCS register in two
1050 * phases, an address phase and a data phase.
1051 *
1052 * The mmio interface is based on 32-bit offsets and values. All
1053 * register offsets must therefore be adjusted by left shifting the
1054 * offset 2 bits and reading 32 bits of data.
1055 */
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001056 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001057 XPCS_IOWRITE(pdata, PCS_MMD_SELECT << 2, mmd_address >> 8);
1058 mmd_data = XPCS_IOREAD(pdata, (mmd_address & 0xff) << 2);
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001059 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001060
1061 return mmd_data;
1062}
1063
1064static void xgbe_write_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
1065 int mmd_reg, int mmd_data)
1066{
1067 unsigned int mmd_address;
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001068 unsigned long flags;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001069
1070 if (mmd_reg & MII_ADDR_C45)
1071 mmd_address = mmd_reg & ~MII_ADDR_C45;
1072 else
1073 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1074
1075 /* The PCS registers are accessed using mmio. The underlying APB3
1076 * management interface uses indirect addressing to access the MMD
1077 * register sets. This requires accessing of the PCS register in two
1078 * phases, an address phase and a data phase.
1079 *
1080 * The mmio interface is based on 32-bit offsets and values. All
1081 * register offsets must therefore be adjusted by left shifting the
1082 * offset 2 bits and reading 32 bits of data.
1083 */
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001084 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001085 XPCS_IOWRITE(pdata, PCS_MMD_SELECT << 2, mmd_address >> 8);
1086 XPCS_IOWRITE(pdata, (mmd_address & 0xff) << 2, mmd_data);
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001087 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001088}
1089
1090static int xgbe_tx_complete(struct xgbe_ring_desc *rdesc)
1091{
1092 return !XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN);
1093}
1094
1095static int xgbe_disable_rx_csum(struct xgbe_prv_data *pdata)
1096{
1097 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 0);
1098
1099 return 0;
1100}
1101
1102static int xgbe_enable_rx_csum(struct xgbe_prv_data *pdata)
1103{
1104 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 1);
1105
1106 return 0;
1107}
1108
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001109static void xgbe_tx_desc_reset(struct xgbe_ring_data *rdata)
1110{
1111 struct xgbe_ring_desc *rdesc = rdata->rdesc;
1112
1113 /* Reset the Tx descriptor
1114 * Set buffer 1 (lo) address to zero
1115 * Set buffer 1 (hi) address to zero
1116 * Reset all other control bits (IC, TTSE, B2L & B1L)
1117 * Reset all other control bits (OWN, CTXT, FD, LD, CPC, CIC, etc)
1118 */
1119 rdesc->desc0 = 0;
1120 rdesc->desc1 = 0;
1121 rdesc->desc2 = 0;
1122 rdesc->desc3 = 0;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001123
1124 /* Make sure ownership is written to the descriptor */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001125 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001126}
1127
1128static void xgbe_tx_desc_init(struct xgbe_channel *channel)
1129{
1130 struct xgbe_ring *ring = channel->tx_ring;
1131 struct xgbe_ring_data *rdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001132 int i;
1133 int start_index = ring->cur;
1134
1135 DBGPR("-->tx_desc_init\n");
1136
1137 /* Initialze all descriptors */
1138 for (i = 0; i < ring->rdesc_count; i++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001139 rdata = XGBE_GET_DESC_DATA(ring, i);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001140
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001141 /* Initialize Tx descriptor */
1142 xgbe_tx_desc_reset(rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001143 }
1144
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001145 /* Update the total number of Tx descriptors */
1146 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDRLR, ring->rdesc_count - 1);
1147
1148 /* Update the starting address of descriptor ring */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001149 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001150 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_HI,
1151 upper_32_bits(rdata->rdesc_dma));
1152 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_LO,
1153 lower_32_bits(rdata->rdesc_dma));
1154
1155 DBGPR("<--tx_desc_init\n");
1156}
1157
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001158static void xgbe_rx_desc_reset(struct xgbe_prv_data *pdata,
1159 struct xgbe_ring_data *rdata, unsigned int index)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001160{
1161 struct xgbe_ring_desc *rdesc = rdata->rdesc;
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001162 unsigned int rx_usecs = pdata->rx_usecs;
1163 unsigned int rx_frames = pdata->rx_frames;
1164 unsigned int inte;
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001165 dma_addr_t hdr_dma, buf_dma;
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001166
1167 if (!rx_usecs && !rx_frames) {
1168 /* No coalescing, interrupt for every descriptor */
1169 inte = 1;
1170 } else {
1171 /* Set interrupt based on Rx frame coalescing setting */
1172 if (rx_frames && !((index + 1) % rx_frames))
1173 inte = 1;
1174 else
1175 inte = 0;
1176 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001177
1178 /* Reset the Rx descriptor
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001179 * Set buffer 1 (lo) address to header dma address (lo)
1180 * Set buffer 1 (hi) address to header dma address (hi)
1181 * Set buffer 2 (lo) address to buffer dma address (lo)
1182 * Set buffer 2 (hi) address to buffer dma address (hi) and
1183 * set control bits OWN and INTE
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001184 */
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001185 hdr_dma = rdata->rx.hdr.dma_base + rdata->rx.hdr.dma_off;
1186 buf_dma = rdata->rx.buf.dma_base + rdata->rx.buf.dma_off;
1187 rdesc->desc0 = cpu_to_le32(lower_32_bits(hdr_dma));
1188 rdesc->desc1 = cpu_to_le32(upper_32_bits(hdr_dma));
1189 rdesc->desc2 = cpu_to_le32(lower_32_bits(buf_dma));
1190 rdesc->desc3 = cpu_to_le32(upper_32_bits(buf_dma));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001191
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001192 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, INTE, inte);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001193
1194 /* Since the Rx DMA engine is likely running, make sure everything
1195 * is written to the descriptor(s) before setting the OWN bit
1196 * for the descriptor
1197 */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001198 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001199
1200 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN, 1);
1201
1202 /* Make sure ownership is written to the descriptor */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001203 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001204}
1205
1206static void xgbe_rx_desc_init(struct xgbe_channel *channel)
1207{
1208 struct xgbe_prv_data *pdata = channel->pdata;
1209 struct xgbe_ring *ring = channel->rx_ring;
1210 struct xgbe_ring_data *rdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001211 unsigned int start_index = ring->cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001212 unsigned int i;
1213
1214 DBGPR("-->rx_desc_init\n");
1215
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001216 /* Initialize all descriptors */
1217 for (i = 0; i < ring->rdesc_count; i++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001218 rdata = XGBE_GET_DESC_DATA(ring, i);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001219
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001220 /* Initialize Rx descriptor */
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001221 xgbe_rx_desc_reset(pdata, rdata, i);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001222 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001223
1224 /* Update the total number of Rx descriptors */
1225 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDRLR, ring->rdesc_count - 1);
1226
1227 /* Update the starting address of descriptor ring */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001228 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001229 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_HI,
1230 upper_32_bits(rdata->rdesc_dma));
1231 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_LO,
1232 lower_32_bits(rdata->rdesc_dma));
1233
1234 /* Update the Rx Descriptor Tail Pointer */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001235 rdata = XGBE_GET_DESC_DATA(ring, start_index + ring->rdesc_count - 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001236 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
1237 lower_32_bits(rdata->rdesc_dma));
1238
1239 DBGPR("<--rx_desc_init\n");
1240}
1241
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001242static void xgbe_update_tstamp_addend(struct xgbe_prv_data *pdata,
1243 unsigned int addend)
1244{
1245 /* Set the addend register value and tell the device */
1246 XGMAC_IOWRITE(pdata, MAC_TSAR, addend);
1247 XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSADDREG, 1);
1248
1249 /* Wait for addend update to complete */
1250 while (XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSADDREG))
1251 udelay(5);
1252}
1253
1254static void xgbe_set_tstamp_time(struct xgbe_prv_data *pdata, unsigned int sec,
1255 unsigned int nsec)
1256{
1257 /* Set the time values and tell the device */
1258 XGMAC_IOWRITE(pdata, MAC_STSUR, sec);
1259 XGMAC_IOWRITE(pdata, MAC_STNUR, nsec);
1260 XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSINIT, 1);
1261
1262 /* Wait for time update to complete */
1263 while (XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSINIT))
1264 udelay(5);
1265}
1266
1267static u64 xgbe_get_tstamp_time(struct xgbe_prv_data *pdata)
1268{
1269 u64 nsec;
1270
1271 nsec = XGMAC_IOREAD(pdata, MAC_STSR);
1272 nsec *= NSEC_PER_SEC;
1273 nsec += XGMAC_IOREAD(pdata, MAC_STNR);
1274
1275 return nsec;
1276}
1277
1278static u64 xgbe_get_tx_tstamp(struct xgbe_prv_data *pdata)
1279{
1280 unsigned int tx_snr;
1281 u64 nsec;
1282
1283 tx_snr = XGMAC_IOREAD(pdata, MAC_TXSNR);
1284 if (XGMAC_GET_BITS(tx_snr, MAC_TXSNR, TXTSSTSMIS))
1285 return 0;
1286
1287 nsec = XGMAC_IOREAD(pdata, MAC_TXSSR);
1288 nsec *= NSEC_PER_SEC;
1289 nsec += tx_snr;
1290
1291 return nsec;
1292}
1293
1294static void xgbe_get_rx_tstamp(struct xgbe_packet_data *packet,
1295 struct xgbe_ring_desc *rdesc)
1296{
1297 u64 nsec;
1298
1299 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSA) &&
1300 !XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSD)) {
1301 nsec = le32_to_cpu(rdesc->desc1);
1302 nsec <<= 32;
1303 nsec |= le32_to_cpu(rdesc->desc0);
1304 if (nsec != 0xffffffffffffffffULL) {
1305 packet->rx_tstamp = nsec;
1306 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1307 RX_TSTAMP, 1);
1308 }
1309 }
1310}
1311
1312static int xgbe_config_tstamp(struct xgbe_prv_data *pdata,
1313 unsigned int mac_tscr)
1314{
1315 /* Set one nano-second accuracy */
1316 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCTRLSSR, 1);
1317
1318 /* Set fine timestamp update */
1319 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCFUPDT, 1);
1320
1321 /* Overwrite earlier timestamps */
1322 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TXTSSTSM, 1);
1323
1324 XGMAC_IOWRITE(pdata, MAC_TSCR, mac_tscr);
1325
1326 /* Exit if timestamping is not enabled */
1327 if (!XGMAC_GET_BITS(mac_tscr, MAC_TSCR, TSENA))
1328 return 0;
1329
1330 /* Initialize time registers */
1331 XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SSINC, XGBE_TSTAMP_SSINC);
1332 XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SNSINC, XGBE_TSTAMP_SNSINC);
1333 xgbe_update_tstamp_addend(pdata, pdata->tstamp_addend);
1334 xgbe_set_tstamp_time(pdata, 0, 0);
1335
1336 /* Initialize the timecounter */
1337 timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc,
1338 ktime_to_ns(ktime_get_real()));
1339
1340 return 0;
1341}
1342
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001343static void xgbe_tx_start_xmit(struct xgbe_channel *channel,
1344 struct xgbe_ring *ring)
1345{
1346 struct xgbe_prv_data *pdata = channel->pdata;
1347 struct xgbe_ring_data *rdata;
1348
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001349 /* Make sure everything is written before the register write */
1350 wmb();
1351
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001352 /* Issue a poll command to Tx DMA by writing address
1353 * of next immediate free descriptor */
1354 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1355 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDTR_LO,
1356 lower_32_bits(rdata->rdesc_dma));
1357
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -05001358 /* Start the Tx timer */
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001359 if (pdata->tx_usecs && !channel->tx_timer_active) {
1360 channel->tx_timer_active = 1;
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -05001361 mod_timer(&channel->tx_timer,
1362 jiffies + usecs_to_jiffies(pdata->tx_usecs));
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001363 }
1364
1365 ring->tx.xmit_more = 0;
1366}
1367
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001368static void xgbe_dev_xmit(struct xgbe_channel *channel)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001369{
1370 struct xgbe_prv_data *pdata = channel->pdata;
1371 struct xgbe_ring *ring = channel->tx_ring;
1372 struct xgbe_ring_data *rdata;
1373 struct xgbe_ring_desc *rdesc;
1374 struct xgbe_packet_data *packet = &ring->packet_data;
1375 unsigned int csum, tso, vlan;
1376 unsigned int tso_context, vlan_context;
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001377 unsigned int tx_set_ic;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001378 int start_index = ring->cur;
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001379 int cur_index = ring->cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001380 int i;
1381
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001382 DBGPR("-->xgbe_dev_xmit\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001383
1384 csum = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1385 CSUM_ENABLE);
1386 tso = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1387 TSO_ENABLE);
1388 vlan = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1389 VLAN_CTAG);
1390
1391 if (tso && (packet->mss != ring->tx.cur_mss))
1392 tso_context = 1;
1393 else
1394 tso_context = 0;
1395
1396 if (vlan && (packet->vlan_ctag != ring->tx.cur_vlan_ctag))
1397 vlan_context = 1;
1398 else
1399 vlan_context = 0;
1400
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001401 /* Determine if an interrupt should be generated for this Tx:
1402 * Interrupt:
1403 * - Tx frame count exceeds the frame count setting
1404 * - Addition of Tx frame count to the frame count since the
1405 * last interrupt was set exceeds the frame count setting
1406 * No interrupt:
1407 * - No frame count setting specified (ethtool -C ethX tx-frames 0)
1408 * - Addition of Tx frame count to the frame count since the
1409 * last interrupt was set does not exceed the frame count setting
1410 */
1411 ring->coalesce_count += packet->tx_packets;
1412 if (!pdata->tx_frames)
1413 tx_set_ic = 0;
1414 else if (packet->tx_packets > pdata->tx_frames)
1415 tx_set_ic = 1;
1416 else if ((ring->coalesce_count % pdata->tx_frames) <
1417 packet->tx_packets)
1418 tx_set_ic = 1;
1419 else
1420 tx_set_ic = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001421
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001422 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001423 rdesc = rdata->rdesc;
1424
1425 /* Create a context descriptor if this is a TSO packet */
1426 if (tso_context || vlan_context) {
1427 if (tso_context) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001428 netif_dbg(pdata, tx_queued, pdata->netdev,
1429 "TSO context descriptor, mss=%u\n",
1430 packet->mss);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001431
1432 /* Set the MSS size */
1433 XGMAC_SET_BITS_LE(rdesc->desc2, TX_CONTEXT_DESC2,
1434 MSS, packet->mss);
1435
1436 /* Mark it as a CONTEXT descriptor */
1437 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1438 CTXT, 1);
1439
1440 /* Indicate this descriptor contains the MSS */
1441 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1442 TCMSSV, 1);
1443
1444 ring->tx.cur_mss = packet->mss;
1445 }
1446
1447 if (vlan_context) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001448 netif_dbg(pdata, tx_queued, pdata->netdev,
1449 "VLAN context descriptor, ctag=%u\n",
1450 packet->vlan_ctag);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001451
1452 /* Mark it as a CONTEXT descriptor */
1453 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1454 CTXT, 1);
1455
1456 /* Set the VLAN tag */
1457 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1458 VT, packet->vlan_ctag);
1459
1460 /* Indicate this descriptor contains the VLAN tag */
1461 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1462 VLTV, 1);
1463
1464 ring->tx.cur_vlan_ctag = packet->vlan_ctag;
1465 }
1466
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001467 cur_index++;
1468 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001469 rdesc = rdata->rdesc;
1470 }
1471
1472 /* Update buffer address (for TSO this is the header) */
1473 rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
1474 rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
1475
1476 /* Update the buffer length */
1477 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1478 rdata->skb_dma_len);
1479
1480 /* VLAN tag insertion check */
1481 if (vlan)
1482 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, VTIR,
1483 TX_NORMAL_DESC2_VLAN_INSERT);
1484
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001485 /* Timestamp enablement check */
1486 if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
1487 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, TTSE, 1);
1488
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001489 /* Mark it as First Descriptor */
1490 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FD, 1);
1491
1492 /* Mark it as a NORMAL descriptor */
1493 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1494
1495 /* Set OWN bit if not the first descriptor */
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001496 if (cur_index != start_index)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001497 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1498
1499 if (tso) {
1500 /* Enable TSO */
1501 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TSE, 1);
1502 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPPL,
1503 packet->tcp_payload_len);
1504 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPHDRLEN,
1505 packet->tcp_header_len / 4);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001506
1507 pdata->ext_stats.tx_tso_packets++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001508 } else {
1509 /* Enable CRC and Pad Insertion */
1510 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CPC, 0);
1511
1512 /* Enable HW CSUM */
1513 if (csum)
1514 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1515 CIC, 0x3);
1516
1517 /* Set the total length to be transmitted */
1518 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FL,
1519 packet->length);
1520 }
1521
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001522 for (i = cur_index - start_index + 1; i < packet->rdesc_count; i++) {
1523 cur_index++;
1524 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001525 rdesc = rdata->rdesc;
1526
1527 /* Update buffer address */
1528 rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
1529 rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
1530
1531 /* Update the buffer length */
1532 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1533 rdata->skb_dma_len);
1534
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001535 /* Set OWN bit */
1536 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1537
1538 /* Mark it as NORMAL descriptor */
1539 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1540
1541 /* Enable HW CSUM */
1542 if (csum)
1543 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1544 CIC, 0x3);
1545 }
1546
1547 /* Set LAST bit for the last descriptor */
1548 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD, 1);
1549
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001550 /* Set IC bit based on Tx coalescing settings */
1551 if (tx_set_ic)
1552 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, IC, 1);
1553
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001554 /* Save the Tx info to report back during cleanup */
1555 rdata->tx.packets = packet->tx_packets;
1556 rdata->tx.bytes = packet->tx_bytes;
1557
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001558 /* In case the Tx DMA engine is running, make sure everything
1559 * is written to the descriptor(s) before setting the OWN bit
1560 * for the first descriptor
1561 */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001562 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001563
1564 /* Set OWN bit for the first descriptor */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001565 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001566 rdesc = rdata->rdesc;
1567 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1568
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001569 if (netif_msg_tx_queued(pdata))
1570 xgbe_dump_tx_desc(pdata, ring, start_index,
1571 packet->rdesc_count, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001572
1573 /* Make sure ownership is written to the descriptor */
Lendacky, Thomas20986ed2015-10-26 17:13:54 -05001574 smp_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001575
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001576 ring->cur = cur_index + 1;
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001577 if (!packet->skb->xmit_more ||
1578 netif_xmit_stopped(netdev_get_tx_queue(pdata->netdev,
1579 channel->queue_index)))
1580 xgbe_tx_start_xmit(channel, ring);
1581 else
1582 ring->tx.xmit_more = 1;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001583
1584 DBGPR(" %s: descriptors %u to %u written\n",
1585 channel->name, start_index & (ring->rdesc_count - 1),
1586 (ring->cur - 1) & (ring->rdesc_count - 1));
1587
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001588 DBGPR("<--xgbe_dev_xmit\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001589}
1590
1591static int xgbe_dev_read(struct xgbe_channel *channel)
1592{
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001593 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001594 struct xgbe_ring *ring = channel->rx_ring;
1595 struct xgbe_ring_data *rdata;
1596 struct xgbe_ring_desc *rdesc;
1597 struct xgbe_packet_data *packet = &ring->packet_data;
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001598 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001599 unsigned int err, etlt, l34t;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001600
1601 DBGPR("-->xgbe_dev_read: cur = %d\n", ring->cur);
1602
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001603 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001604 rdesc = rdata->rdesc;
1605
1606 /* Check for data availability */
1607 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN))
1608 return 1;
1609
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001610 /* Make sure descriptor fields are read after reading the OWN bit */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001611 dma_rmb();
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001612
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001613 if (netif_msg_rx_status(pdata))
1614 xgbe_dump_rx_desc(pdata, ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001615
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001616 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CTXT)) {
1617 /* Timestamp Context Descriptor */
1618 xgbe_get_rx_tstamp(packet, rdesc);
1619
1620 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1621 CONTEXT, 1);
1622 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1623 CONTEXT_NEXT, 0);
1624 return 0;
1625 }
1626
1627 /* Normal Descriptor, be sure Context Descriptor bit is off */
1628 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, CONTEXT, 0);
1629
1630 /* Indicate if a Context Descriptor is next */
1631 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CDA))
1632 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1633 CONTEXT_NEXT, 1);
1634
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001635 /* Get the header length */
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001636 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, FD)) {
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001637 rdata->rx.hdr_len = XGMAC_GET_BITS_LE(rdesc->desc2,
1638 RX_NORMAL_DESC2, HL);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001639 if (rdata->rx.hdr_len)
1640 pdata->ext_stats.rx_split_header_packets++;
1641 }
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001642
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001643 /* Get the RSS hash */
1644 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, RSV)) {
1645 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1646 RSS_HASH, 1);
1647
1648 packet->rss_hash = le32_to_cpu(rdesc->desc1);
1649
1650 l34t = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, L34T);
1651 switch (l34t) {
1652 case RX_DESC3_L34T_IPV4_TCP:
1653 case RX_DESC3_L34T_IPV4_UDP:
1654 case RX_DESC3_L34T_IPV6_TCP:
1655 case RX_DESC3_L34T_IPV6_UDP:
1656 packet->rss_hash_type = PKT_HASH_TYPE_L4;
Dan Carpenterb6267d32014-11-13 09:19:06 +03001657 break;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001658 default:
1659 packet->rss_hash_type = PKT_HASH_TYPE_L3;
1660 }
1661 }
1662
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001663 /* Get the packet length */
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001664 rdata->rx.len = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, PL);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001665
1666 if (!XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, LD)) {
1667 /* Not all the data has been transferred for this packet */
1668 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1669 INCOMPLETE, 1);
1670 return 0;
1671 }
1672
1673 /* This is the last of the data for this packet */
1674 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1675 INCOMPLETE, 0);
1676
1677 /* Set checksum done indicator as appropriate */
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001678 if (netdev->features & NETIF_F_RXCSUM)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001679 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1680 CSUM_DONE, 1);
1681
1682 /* Check for errors (only valid in last descriptor) */
1683 err = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ES);
1684 etlt = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ETLT);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001685 netif_dbg(pdata, rx_status, netdev, "err=%u, etlt=%#x\n", err, etlt);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001686
Lendacky, Thomas7bba35b2014-11-20 11:03:38 -06001687 if (!err || !etlt) {
1688 /* No error if err is 0 or etlt is 0 */
Lendacky, Thomasc52e9c62014-06-24 16:19:18 -05001689 if ((etlt == 0x09) &&
1690 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001691 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1692 VLAN_CTAG, 1);
1693 packet->vlan_ctag = XGMAC_GET_BITS_LE(rdesc->desc0,
1694 RX_NORMAL_DESC0,
1695 OVT);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001696 netif_dbg(pdata, rx_status, netdev, "vlan-ctag=%#06x\n",
1697 packet->vlan_ctag);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001698 }
1699 } else {
1700 if ((etlt == 0x05) || (etlt == 0x06))
1701 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1702 CSUM_DONE, 0);
1703 else
1704 XGMAC_SET_BITS(packet->errors, RX_PACKET_ERRORS,
1705 FRAME, 1);
1706 }
1707
1708 DBGPR("<--xgbe_dev_read: %s - descriptor=%u (cur=%d)\n", channel->name,
1709 ring->cur & (ring->rdesc_count - 1), ring->cur);
1710
1711 return 0;
1712}
1713
1714static int xgbe_is_context_desc(struct xgbe_ring_desc *rdesc)
1715{
1716 /* Rx and Tx share CTXT bit, so check TDES3.CTXT bit */
1717 return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT);
1718}
1719
1720static int xgbe_is_last_desc(struct xgbe_ring_desc *rdesc)
1721{
1722 /* Rx and Tx share LD bit, so check TDES3.LD bit */
1723 return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD);
1724}
1725
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001726static int xgbe_enable_int(struct xgbe_channel *channel,
1727 enum xgbe_int int_id)
1728{
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001729 unsigned int dma_ch_ier;
1730
1731 dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
1732
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001733 switch (int_id) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001734 case XGMAC_INT_DMA_CH_SR_TI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001735 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001736 break;
1737 case XGMAC_INT_DMA_CH_SR_TPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001738 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001739 break;
1740 case XGMAC_INT_DMA_CH_SR_TBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001741 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001742 break;
1743 case XGMAC_INT_DMA_CH_SR_RI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001744 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001745 break;
1746 case XGMAC_INT_DMA_CH_SR_RBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001747 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001748 break;
1749 case XGMAC_INT_DMA_CH_SR_RPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001750 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 1);
1751 break;
1752 case XGMAC_INT_DMA_CH_SR_TI_RI:
1753 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
1754 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001755 break;
1756 case XGMAC_INT_DMA_CH_SR_FBE:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001757 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001758 break;
1759 case XGMAC_INT_DMA_ALL:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001760 dma_ch_ier |= channel->saved_ier;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001761 break;
1762 default:
1763 return -1;
1764 }
1765
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001766 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
1767
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001768 return 0;
1769}
1770
1771static int xgbe_disable_int(struct xgbe_channel *channel,
1772 enum xgbe_int int_id)
1773{
1774 unsigned int dma_ch_ier;
1775
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001776 dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
1777
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001778 switch (int_id) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001779 case XGMAC_INT_DMA_CH_SR_TI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001780 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001781 break;
1782 case XGMAC_INT_DMA_CH_SR_TPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001783 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001784 break;
1785 case XGMAC_INT_DMA_CH_SR_TBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001786 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001787 break;
1788 case XGMAC_INT_DMA_CH_SR_RI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001789 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001790 break;
1791 case XGMAC_INT_DMA_CH_SR_RBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001792 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001793 break;
1794 case XGMAC_INT_DMA_CH_SR_RPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001795 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 0);
1796 break;
1797 case XGMAC_INT_DMA_CH_SR_TI_RI:
1798 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
1799 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001800 break;
1801 case XGMAC_INT_DMA_CH_SR_FBE:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001802 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001803 break;
1804 case XGMAC_INT_DMA_ALL:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001805 channel->saved_ier = dma_ch_ier & XGBE_DMA_INTERRUPT_MASK;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001806 dma_ch_ier &= ~XGBE_DMA_INTERRUPT_MASK;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001807 break;
1808 default:
1809 return -1;
1810 }
1811
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001812 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
1813
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001814 return 0;
1815}
1816
1817static int xgbe_exit(struct xgbe_prv_data *pdata)
1818{
1819 unsigned int count = 2000;
1820
1821 DBGPR("-->xgbe_exit\n");
1822
1823 /* Issue a software reset */
1824 XGMAC_IOWRITE_BITS(pdata, DMA_MR, SWR, 1);
1825 usleep_range(10, 15);
1826
1827 /* Poll Until Poll Condition */
Dan Carpenterc7557e62015-12-15 13:12:29 +03001828 while (--count && XGMAC_IOREAD_BITS(pdata, DMA_MR, SWR))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001829 usleep_range(500, 600);
1830
1831 if (!count)
1832 return -EBUSY;
1833
1834 DBGPR("<--xgbe_exit\n");
1835
1836 return 0;
1837}
1838
1839static int xgbe_flush_tx_queues(struct xgbe_prv_data *pdata)
1840{
1841 unsigned int i, count;
1842
Lendacky, Thomasa9a4a2d2014-08-29 13:16:50 -05001843 if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) < 0x21)
1844 return 0;
1845
Lendacky, Thomas853eb162014-07-29 08:57:31 -05001846 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001847 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, FTQ, 1);
1848
1849 /* Poll Until Poll Condition */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05001850 for (i = 0; i < pdata->tx_q_count; i++) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001851 count = 2000;
Dan Carpenterc7557e62015-12-15 13:12:29 +03001852 while (--count && XGMAC_MTL_IOREAD_BITS(pdata, i,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001853 MTL_Q_TQOMR, FTQ))
1854 usleep_range(500, 600);
1855
1856 if (!count)
1857 return -EBUSY;
1858 }
1859
1860 return 0;
1861}
1862
1863static void xgbe_config_dma_bus(struct xgbe_prv_data *pdata)
1864{
1865 /* Set enhanced addressing mode */
1866 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, EAME, 1);
1867
1868 /* Set the System Bus mode */
1869 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, UNDEF, 1);
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001870 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, BLEN_256, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001871}
1872
1873static void xgbe_config_dma_cache(struct xgbe_prv_data *pdata)
1874{
1875 unsigned int arcache, awcache;
1876
1877 arcache = 0;
Lendacky, Thomascfa50c72014-07-02 13:04:57 -05001878 XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRC, pdata->arcache);
1879 XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRD, pdata->axdomain);
1880 XGMAC_SET_BITS(arcache, DMA_AXIARCR, TEC, pdata->arcache);
1881 XGMAC_SET_BITS(arcache, DMA_AXIARCR, TED, pdata->axdomain);
1882 XGMAC_SET_BITS(arcache, DMA_AXIARCR, THC, pdata->arcache);
1883 XGMAC_SET_BITS(arcache, DMA_AXIARCR, THD, pdata->axdomain);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001884 XGMAC_IOWRITE(pdata, DMA_AXIARCR, arcache);
1885
1886 awcache = 0;
Lendacky, Thomascfa50c72014-07-02 13:04:57 -05001887 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWC, pdata->awcache);
1888 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWD, pdata->axdomain);
1889 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPC, pdata->awcache);
1890 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPD, pdata->axdomain);
1891 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHC, pdata->awcache);
1892 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHD, pdata->axdomain);
1893 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDC, pdata->awcache);
1894 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDD, pdata->axdomain);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001895 XGMAC_IOWRITE(pdata, DMA_AXIAWCR, awcache);
1896}
1897
1898static void xgbe_config_mtl_mode(struct xgbe_prv_data *pdata)
1899{
1900 unsigned int i;
1901
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001902 /* Set Tx to weighted round robin scheduling algorithm */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001903 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_WRR);
1904
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001905 /* Set Tx traffic classes to use WRR algorithm with equal weights */
1906 for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
1907 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
1908 MTL_TSA_ETS);
1909 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW, 1);
1910 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001911
1912 /* Set Rx to strict priority algorithm */
1913 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, RAA, MTL_RAA_SP);
1914}
1915
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05001916static void xgbe_queue_flow_control_threshold(struct xgbe_prv_data *pdata,
1917 unsigned int queue,
1918 unsigned int q_fifo_size)
1919{
1920 unsigned int frame_fifo_size;
1921 unsigned int rfa, rfd;
1922
1923 frame_fifo_size = XGMAC_FLOW_CONTROL_ALIGN(xgbe_get_max_frame(pdata));
1924
1925 if (pdata->pfcq[queue] && (q_fifo_size > pdata->pfc_rfa)) {
1926 /* PFC is active for this queue */
1927 rfa = pdata->pfc_rfa;
1928 rfd = rfa + frame_fifo_size;
1929 if (rfd > XGMAC_FLOW_CONTROL_MAX)
1930 rfd = XGMAC_FLOW_CONTROL_MAX;
1931 if (rfa >= XGMAC_FLOW_CONTROL_MAX)
1932 rfa = XGMAC_FLOW_CONTROL_MAX - XGMAC_FLOW_CONTROL_UNIT;
1933 } else {
1934 /* This path deals with just maximum frame sizes which are
1935 * limited to a jumbo frame of 9,000 (plus headers, etc.)
1936 * so we can never exceed the maximum allowable RFA/RFD
1937 * values.
1938 */
1939 if (q_fifo_size <= 2048) {
1940 /* rx_rfd to zero to signal no flow control */
1941 pdata->rx_rfa[queue] = 0;
1942 pdata->rx_rfd[queue] = 0;
1943 return;
1944 }
1945
1946 if (q_fifo_size <= 4096) {
1947 /* Between 2048 and 4096 */
1948 pdata->rx_rfa[queue] = 0; /* Full - 1024 bytes */
1949 pdata->rx_rfd[queue] = 1; /* Full - 1536 bytes */
1950 return;
1951 }
1952
1953 if (q_fifo_size <= frame_fifo_size) {
1954 /* Between 4096 and max-frame */
1955 pdata->rx_rfa[queue] = 2; /* Full - 2048 bytes */
1956 pdata->rx_rfd[queue] = 5; /* Full - 3584 bytes */
1957 return;
1958 }
1959
1960 if (q_fifo_size <= (frame_fifo_size * 3)) {
1961 /* Between max-frame and 3 max-frames,
1962 * trigger if we get just over a frame of data and
1963 * resume when we have just under half a frame left.
1964 */
1965 rfa = q_fifo_size - frame_fifo_size;
1966 rfd = rfa + (frame_fifo_size / 2);
1967 } else {
1968 /* Above 3 max-frames - trigger when just over
1969 * 2 frames of space available
1970 */
1971 rfa = frame_fifo_size * 2;
1972 rfa += XGMAC_FLOW_CONTROL_UNIT;
1973 rfd = rfa + frame_fifo_size;
1974 }
1975 }
1976
1977 pdata->rx_rfa[queue] = XGMAC_FLOW_CONTROL_VALUE(rfa);
1978 pdata->rx_rfd[queue] = XGMAC_FLOW_CONTROL_VALUE(rfd);
1979}
1980
1981static void xgbe_calculate_flow_control_threshold(struct xgbe_prv_data *pdata,
1982 unsigned int *fifo)
1983{
1984 unsigned int q_fifo_size;
1985 unsigned int i;
1986
1987 for (i = 0; i < pdata->rx_q_count; i++) {
1988 q_fifo_size = (fifo[i] + 1) * XGMAC_FIFO_UNIT;
1989
1990 xgbe_queue_flow_control_threshold(pdata, i, q_fifo_size);
1991 }
1992}
1993
1994static void xgbe_config_flow_control_threshold(struct xgbe_prv_data *pdata)
1995{
1996 unsigned int i;
1997
1998 for (i = 0; i < pdata->rx_q_count; i++) {
1999 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFA,
2000 pdata->rx_rfa[i]);
2001 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFD,
2002 pdata->rx_rfd[i]);
2003 }
2004}
2005
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002006static unsigned int xgbe_get_tx_fifo_size(struct xgbe_prv_data *pdata)
2007{
2008 unsigned int fifo_size;
2009
2010 /* Calculate the configured fifo size */
2011 fifo_size = 1 << (pdata->hw_feat.tx_fifo_size + 7);
2012
2013 /* The configured value may not be the actual amount of fifo RAM */
2014 return min_t(unsigned int, XGMAC_FIFO_TX_MAX, fifo_size);
2015}
2016
2017static unsigned int xgbe_get_rx_fifo_size(struct xgbe_prv_data *pdata)
2018{
2019 unsigned int fifo_size;
2020
2021 /* Calculate the configured fifo size */
2022 fifo_size = 1 << (pdata->hw_feat.rx_fifo_size + 7);
2023
2024 /* The configured value may not be the actual amount of fifo RAM */
2025 return min_t(unsigned int, XGMAC_FIFO_RX_MAX, fifo_size);
2026}
2027
2028static void xgbe_calculate_equal_fifo(unsigned int fifo_size,
2029 unsigned int queue_count,
2030 unsigned int *fifo)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002031{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002032 unsigned int q_fifo_size;
2033 unsigned int p_fifo;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002034 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002035
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002036 q_fifo_size = fifo_size / queue_count;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002037
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002038 /* Calculate the fifo setting by dividing the queue's fifo size
2039 * by the fifo allocation increment (with 0 representing the
2040 * base allocation increment so decrement the result by 1).
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002041 */
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002042 p_fifo = q_fifo_size / XGMAC_FIFO_UNIT;
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002043 if (p_fifo)
2044 p_fifo--;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002045
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002046 /* Distribute the fifo equally amongst the queues */
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002047 for (i = 0; i < queue_count; i++)
2048 fifo[i] = p_fifo;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002049}
2050
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002051static unsigned int xgbe_set_nonprio_fifos(unsigned int fifo_size,
2052 unsigned int queue_count,
2053 unsigned int *fifo)
2054{
2055 unsigned int i;
2056
2057 BUILD_BUG_ON_NOT_POWER_OF_2(XGMAC_FIFO_MIN_ALLOC);
2058
2059 if (queue_count <= IEEE_8021QAZ_MAX_TCS)
2060 return fifo_size;
2061
2062 /* Rx queues 9 and up are for specialized packets,
2063 * such as PTP or DCB control packets, etc. and
2064 * don't require a large fifo
2065 */
2066 for (i = IEEE_8021QAZ_MAX_TCS; i < queue_count; i++) {
2067 fifo[i] = (XGMAC_FIFO_MIN_ALLOC / XGMAC_FIFO_UNIT) - 1;
2068 fifo_size -= XGMAC_FIFO_MIN_ALLOC;
2069 }
2070
2071 return fifo_size;
2072}
2073
2074static unsigned int xgbe_get_pfc_delay(struct xgbe_prv_data *pdata)
2075{
2076 unsigned int delay;
2077
2078 /* If a delay has been provided, use that */
2079 if (pdata->pfc->delay)
2080 return pdata->pfc->delay / 8;
2081
2082 /* Allow for two maximum size frames */
2083 delay = xgbe_get_max_frame(pdata);
2084 delay += XGMAC_ETH_PREAMBLE;
2085 delay *= 2;
2086
2087 /* Allow for PFC frame */
2088 delay += XGMAC_PFC_DATA_LEN;
2089 delay += ETH_HLEN + ETH_FCS_LEN;
2090 delay += XGMAC_ETH_PREAMBLE;
2091
2092 /* Allow for miscellaneous delays (LPI exit, cable, etc.) */
2093 delay += XGMAC_PFC_DELAYS;
2094
2095 return delay;
2096}
2097
2098static unsigned int xgbe_get_pfc_queues(struct xgbe_prv_data *pdata)
2099{
2100 unsigned int count, prio_queues;
2101 unsigned int i;
2102
2103 if (!pdata->pfc->pfc_en)
2104 return 0;
2105
2106 count = 0;
2107 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2108 for (i = 0; i < prio_queues; i++) {
2109 if (!xgbe_is_pfc_queue(pdata, i))
2110 continue;
2111
2112 pdata->pfcq[i] = 1;
2113 count++;
2114 }
2115
2116 return count;
2117}
2118
2119static void xgbe_calculate_dcb_fifo(struct xgbe_prv_data *pdata,
2120 unsigned int fifo_size,
2121 unsigned int *fifo)
2122{
2123 unsigned int q_fifo_size, rem_fifo, addn_fifo;
2124 unsigned int prio_queues;
2125 unsigned int pfc_count;
2126 unsigned int i;
2127
2128 q_fifo_size = XGMAC_FIFO_ALIGN(xgbe_get_max_frame(pdata));
2129 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2130 pfc_count = xgbe_get_pfc_queues(pdata);
2131
2132 if (!pfc_count || ((q_fifo_size * prio_queues) > fifo_size)) {
2133 /* No traffic classes with PFC enabled or can't do lossless */
2134 xgbe_calculate_equal_fifo(fifo_size, prio_queues, fifo);
2135 return;
2136 }
2137
2138 /* Calculate how much fifo we have to play with */
2139 rem_fifo = fifo_size - (q_fifo_size * prio_queues);
2140
2141 /* Calculate how much more than base fifo PFC needs, which also
2142 * becomes the threshold activation point (RFA)
2143 */
2144 pdata->pfc_rfa = xgbe_get_pfc_delay(pdata);
2145 pdata->pfc_rfa = XGMAC_FLOW_CONTROL_ALIGN(pdata->pfc_rfa);
2146
2147 if (pdata->pfc_rfa > q_fifo_size) {
2148 addn_fifo = pdata->pfc_rfa - q_fifo_size;
2149 addn_fifo = XGMAC_FIFO_ALIGN(addn_fifo);
2150 } else {
2151 addn_fifo = 0;
2152 }
2153
2154 /* Calculate DCB fifo settings:
2155 * - distribute remaining fifo between the VLAN priority
2156 * queues based on traffic class PFC enablement and overall
2157 * priority (0 is lowest priority, so start at highest)
2158 */
2159 i = prio_queues;
2160 while (i > 0) {
2161 i--;
2162
2163 fifo[i] = (q_fifo_size / XGMAC_FIFO_UNIT) - 1;
2164
2165 if (!pdata->pfcq[i] || !addn_fifo)
2166 continue;
2167
2168 if (addn_fifo > rem_fifo) {
2169 netdev_warn(pdata->netdev,
2170 "RXq%u cannot set needed fifo size\n", i);
2171 if (!rem_fifo)
2172 continue;
2173
2174 addn_fifo = rem_fifo;
2175 }
2176
2177 fifo[i] += (addn_fifo / XGMAC_FIFO_UNIT);
2178 rem_fifo -= addn_fifo;
2179 }
2180
2181 if (rem_fifo) {
2182 unsigned int inc_fifo = rem_fifo / prio_queues;
2183
2184 /* Distribute remaining fifo across queues */
2185 for (i = 0; i < prio_queues; i++)
2186 fifo[i] += (inc_fifo / XGMAC_FIFO_UNIT);
2187 }
2188}
2189
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002190static void xgbe_config_tx_fifo_size(struct xgbe_prv_data *pdata)
2191{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002192 unsigned int fifo_size;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002193 unsigned int fifo[XGBE_MAX_QUEUES];
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002194 unsigned int i;
2195
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002196 fifo_size = xgbe_get_tx_fifo_size(pdata);
2197
2198 xgbe_calculate_equal_fifo(fifo_size, pdata->tx_q_count, fifo);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002199
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002200 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002201 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TQS, fifo[i]);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002202
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002203 netif_info(pdata, drv, pdata->netdev,
2204 "%d Tx hardware queues, %d byte fifo per queue\n",
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002205 pdata->tx_q_count, ((fifo[0] + 1) * XGMAC_FIFO_UNIT));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002206}
2207
2208static void xgbe_config_rx_fifo_size(struct xgbe_prv_data *pdata)
2209{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002210 unsigned int fifo_size;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002211 unsigned int fifo[XGBE_MAX_QUEUES];
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002212 unsigned int prio_queues;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002213 unsigned int i;
2214
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002215 /* Clear any DCB related fifo/queue information */
2216 memset(pdata->pfcq, 0, sizeof(pdata->pfcq));
2217 pdata->pfc_rfa = 0;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002218
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002219 fifo_size = xgbe_get_rx_fifo_size(pdata);
2220 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2221
2222 /* Assign a minimum fifo to the non-VLAN priority queues */
2223 fifo_size = xgbe_set_nonprio_fifos(fifo_size, pdata->rx_q_count, fifo);
2224
2225 if (pdata->pfc && pdata->ets)
2226 xgbe_calculate_dcb_fifo(pdata, fifo_size, fifo);
2227 else
2228 xgbe_calculate_equal_fifo(fifo_size, prio_queues, fifo);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002229
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002230 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002231 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RQS, fifo[i]);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002232
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002233 xgbe_calculate_flow_control_threshold(pdata, fifo);
2234 xgbe_config_flow_control_threshold(pdata);
2235
2236 if (pdata->pfc && pdata->ets && pdata->pfc->pfc_en) {
2237 netif_info(pdata, drv, pdata->netdev,
2238 "%u Rx hardware queues\n", pdata->rx_q_count);
2239 for (i = 0; i < pdata->rx_q_count; i++)
2240 netif_info(pdata, drv, pdata->netdev,
2241 "RxQ%u, %u byte fifo queue\n", i,
2242 ((fifo[i] + 1) * XGMAC_FIFO_UNIT));
2243 } else {
2244 netif_info(pdata, drv, pdata->netdev,
2245 "%u Rx hardware queues, %u byte fifo per queue\n",
2246 pdata->rx_q_count,
2247 ((fifo[0] + 1) * XGMAC_FIFO_UNIT));
2248 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002249}
2250
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002251static void xgbe_config_queue_mapping(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002252{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002253 unsigned int qptc, qptc_extra, queue;
2254 unsigned int prio_queues;
2255 unsigned int ppq, ppq_extra, prio;
2256 unsigned int mask;
2257 unsigned int i, j, reg, reg_val;
2258
2259 /* Map the MTL Tx Queues to Traffic Classes
2260 * Note: Tx Queues >= Traffic Classes
2261 */
2262 qptc = pdata->tx_q_count / pdata->hw_feat.tc_cnt;
2263 qptc_extra = pdata->tx_q_count % pdata->hw_feat.tc_cnt;
2264
2265 for (i = 0, queue = 0; i < pdata->hw_feat.tc_cnt; i++) {
2266 for (j = 0; j < qptc; j++) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002267 netif_dbg(pdata, drv, pdata->netdev,
2268 "TXq%u mapped to TC%u\n", queue, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002269 XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
2270 Q2TCMAP, i);
2271 pdata->q2tc_map[queue++] = i;
2272 }
2273
2274 if (i < qptc_extra) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002275 netif_dbg(pdata, drv, pdata->netdev,
2276 "TXq%u mapped to TC%u\n", queue, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002277 XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
2278 Q2TCMAP, i);
2279 pdata->q2tc_map[queue++] = i;
2280 }
2281 }
2282
2283 /* Map the 8 VLAN priority values to available MTL Rx queues */
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002284 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002285 ppq = IEEE_8021QAZ_MAX_TCS / prio_queues;
2286 ppq_extra = IEEE_8021QAZ_MAX_TCS % prio_queues;
2287
2288 reg = MAC_RQC2R;
2289 reg_val = 0;
2290 for (i = 0, prio = 0; i < prio_queues;) {
2291 mask = 0;
2292 for (j = 0; j < ppq; j++) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002293 netif_dbg(pdata, drv, pdata->netdev,
2294 "PRIO%u mapped to RXq%u\n", prio, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002295 mask |= (1 << prio);
2296 pdata->prio2q_map[prio++] = i;
2297 }
2298
2299 if (i < ppq_extra) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002300 netif_dbg(pdata, drv, pdata->netdev,
2301 "PRIO%u mapped to RXq%u\n", prio, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002302 mask |= (1 << prio);
2303 pdata->prio2q_map[prio++] = i;
2304 }
2305
2306 reg_val |= (mask << ((i++ % MAC_RQC2_Q_PER_REG) << 3));
2307
2308 if ((i % MAC_RQC2_Q_PER_REG) && (i != prio_queues))
2309 continue;
2310
2311 XGMAC_IOWRITE(pdata, reg, reg_val);
2312 reg += MAC_RQC2_INC;
2313 reg_val = 0;
2314 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002315
2316 /* Select dynamic mapping of MTL Rx queue to DMA Rx channel */
2317 reg = MTL_RQDCM0R;
2318 reg_val = 0;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002319 for (i = 0; i < pdata->rx_q_count;) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002320 reg_val |= (0x80 << ((i++ % MTL_RQDCM_Q_PER_REG) << 3));
2321
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002322 if ((i % MTL_RQDCM_Q_PER_REG) && (i != pdata->rx_q_count))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002323 continue;
2324
2325 XGMAC_IOWRITE(pdata, reg, reg_val);
2326
2327 reg += MTL_RQDCM_INC;
2328 reg_val = 0;
2329 }
2330}
2331
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002332static void xgbe_config_tc(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002333{
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002334 unsigned int offset, queue, prio;
2335 u8 i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002336
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002337 netdev_reset_tc(pdata->netdev);
2338 if (!pdata->num_tcs)
2339 return;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002340
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002341 netdev_set_num_tc(pdata->netdev, pdata->num_tcs);
2342
2343 for (i = 0, queue = 0, offset = 0; i < pdata->num_tcs; i++) {
2344 while ((queue < pdata->tx_q_count) &&
2345 (pdata->q2tc_map[queue] == i))
2346 queue++;
2347
2348 netif_dbg(pdata, drv, pdata->netdev, "TC%u using TXq%u-%u\n",
2349 i, offset, queue - 1);
2350 netdev_set_tc_queue(pdata->netdev, i, queue - offset, offset);
2351 offset = queue;
2352 }
2353
2354 if (!pdata->ets)
2355 return;
2356
2357 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++)
2358 netdev_set_prio_tc_map(pdata->netdev, prio,
2359 pdata->ets->prio_tc[prio]);
2360}
2361
2362static void xgbe_config_dcb_tc(struct xgbe_prv_data *pdata)
2363{
2364 struct ieee_ets *ets = pdata->ets;
2365 unsigned int total_weight, min_weight, weight;
2366 unsigned int mask, reg, reg_val;
2367 unsigned int i, prio;
2368
2369 if (!ets)
2370 return;
2371
2372 /* Set Tx to deficit weighted round robin scheduling algorithm (when
2373 * traffic class is using ETS algorithm)
2374 */
2375 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_DWRR);
2376
2377 /* Set Traffic Class algorithms */
2378 total_weight = pdata->netdev->mtu * pdata->hw_feat.tc_cnt;
2379 min_weight = total_weight / 100;
2380 if (!min_weight)
2381 min_weight = 1;
2382
2383 for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
2384 /* Map the priorities to the traffic class */
2385 mask = 0;
2386 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
2387 if (ets->prio_tc[prio] == i)
2388 mask |= (1 << prio);
2389 }
2390 mask &= 0xff;
2391
2392 netif_dbg(pdata, drv, pdata->netdev, "TC%u PRIO mask=%#x\n",
2393 i, mask);
2394 reg = MTL_TCPM0R + (MTL_TCPM_INC * (i / MTL_TCPM_TC_PER_REG));
2395 reg_val = XGMAC_IOREAD(pdata, reg);
2396
2397 reg_val &= ~(0xff << ((i % MTL_TCPM_TC_PER_REG) << 3));
2398 reg_val |= (mask << ((i % MTL_TCPM_TC_PER_REG) << 3));
2399
2400 XGMAC_IOWRITE(pdata, reg, reg_val);
2401
2402 /* Set the traffic class algorithm */
2403 switch (ets->tc_tsa[i]) {
2404 case IEEE_8021QAZ_TSA_STRICT:
2405 netif_dbg(pdata, drv, pdata->netdev,
2406 "TC%u using SP\n", i);
2407 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2408 MTL_TSA_SP);
2409 break;
2410 case IEEE_8021QAZ_TSA_ETS:
2411 weight = total_weight * ets->tc_tx_bw[i] / 100;
2412 weight = clamp(weight, min_weight, total_weight);
2413
2414 netif_dbg(pdata, drv, pdata->netdev,
2415 "TC%u using DWRR (weight %u)\n", i, weight);
2416 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2417 MTL_TSA_ETS);
2418 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW,
2419 weight);
2420 break;
2421 }
2422 }
2423
2424 xgbe_config_tc(pdata);
2425}
2426
2427static void xgbe_config_dcb_pfc(struct xgbe_prv_data *pdata)
2428{
2429 if (!test_bit(XGBE_DOWN, &pdata->dev_state)) {
2430 /* Just stop the Tx queues while Rx fifo is changed */
2431 netif_tx_stop_all_queues(pdata->netdev);
2432
2433 /* Suspend Rx so that fifo's can be adjusted */
2434 pdata->hw_if.disable_rx(pdata);
2435 }
2436
2437 xgbe_config_rx_fifo_size(pdata);
2438 xgbe_config_flow_control(pdata);
2439
2440 if (!test_bit(XGBE_DOWN, &pdata->dev_state)) {
2441 /* Resume Rx */
2442 pdata->hw_if.enable_rx(pdata);
2443
2444 /* Resume Tx queues */
2445 netif_tx_start_all_queues(pdata->netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002446 }
2447}
2448
2449static void xgbe_config_mac_address(struct xgbe_prv_data *pdata)
2450{
2451 xgbe_set_mac_address(pdata, pdata->netdev->dev_addr);
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05002452
2453 /* Filtering is done using perfect filtering and hash filtering */
2454 if (pdata->hw_feat.hash_table_size) {
2455 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HPF, 1);
2456 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HUC, 1);
2457 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HMC, 1);
2458 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002459}
2460
2461static void xgbe_config_jumbo_enable(struct xgbe_prv_data *pdata)
2462{
2463 unsigned int val;
2464
2465 val = (pdata->netdev->mtu > XGMAC_STD_PACKET_MTU) ? 1 : 0;
2466
2467 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, JE, val);
2468}
2469
Lendacky, Thomas916102c2015-01-16 12:46:45 -06002470static void xgbe_config_mac_speed(struct xgbe_prv_data *pdata)
2471{
2472 switch (pdata->phy_speed) {
2473 case SPEED_10000:
2474 xgbe_set_xgmii_speed(pdata);
2475 break;
2476
2477 case SPEED_2500:
2478 xgbe_set_gmii_2500_speed(pdata);
2479 break;
2480
2481 case SPEED_1000:
2482 xgbe_set_gmii_speed(pdata);
2483 break;
2484 }
2485}
2486
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002487static void xgbe_config_checksum_offload(struct xgbe_prv_data *pdata)
2488{
2489 if (pdata->netdev->features & NETIF_F_RXCSUM)
2490 xgbe_enable_rx_csum(pdata);
2491 else
2492 xgbe_disable_rx_csum(pdata);
2493}
2494
2495static void xgbe_config_vlan_support(struct xgbe_prv_data *pdata)
2496{
Lendacky, Thomas6e5eed02014-06-24 16:19:12 -05002497 /* Indicate that VLAN Tx CTAGs come from context descriptors */
2498 XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, CSVL, 0);
2499 XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, VLTI, 1);
2500
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05002501 /* Set the current VLAN Hash Table register value */
2502 xgbe_update_vlan_hash_table(pdata);
2503
2504 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
2505 xgbe_enable_rx_vlan_filtering(pdata);
2506 else
2507 xgbe_disable_rx_vlan_filtering(pdata);
2508
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002509 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2510 xgbe_enable_rx_vlan_stripping(pdata);
2511 else
2512 xgbe_disable_rx_vlan_stripping(pdata);
2513}
2514
Lendacky, Thomas60265102014-09-05 18:02:30 -05002515static u64 xgbe_mmc_read(struct xgbe_prv_data *pdata, unsigned int reg_lo)
2516{
2517 bool read_hi;
2518 u64 val;
2519
2520 switch (reg_lo) {
2521 /* These registers are always 64 bit */
2522 case MMC_TXOCTETCOUNT_GB_LO:
2523 case MMC_TXOCTETCOUNT_G_LO:
2524 case MMC_RXOCTETCOUNT_GB_LO:
2525 case MMC_RXOCTETCOUNT_G_LO:
2526 read_hi = true;
2527 break;
2528
2529 default:
2530 read_hi = false;
Lendacky, Thomas3947d782015-09-30 08:52:38 -05002531 }
Lendacky, Thomas60265102014-09-05 18:02:30 -05002532
2533 val = XGMAC_IOREAD(pdata, reg_lo);
2534
2535 if (read_hi)
2536 val |= ((u64)XGMAC_IOREAD(pdata, reg_lo + 4) << 32);
2537
2538 return val;
2539}
2540
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002541static void xgbe_tx_mmc_int(struct xgbe_prv_data *pdata)
2542{
2543 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2544 unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_TISR);
2545
2546 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_GB))
2547 stats->txoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002548 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002549
2550 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_GB))
2551 stats->txframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002552 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002553
2554 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_G))
2555 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002556 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002557
2558 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_G))
2559 stats->txmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002560 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002561
2562 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX64OCTETS_GB))
2563 stats->tx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002564 xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002565
2566 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX65TO127OCTETS_GB))
2567 stats->tx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002568 xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002569
2570 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX128TO255OCTETS_GB))
2571 stats->tx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002572 xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002573
2574 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX256TO511OCTETS_GB))
2575 stats->tx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002576 xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002577
2578 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX512TO1023OCTETS_GB))
2579 stats->tx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002580 xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002581
2582 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX1024TOMAXOCTETS_GB))
2583 stats->tx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002584 xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002585
2586 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNICASTFRAMES_GB))
2587 stats->txunicastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002588 xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002589
2590 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_GB))
2591 stats->txmulticastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002592 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002593
2594 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_GB))
2595 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002596 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002597
2598 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNDERFLOWERROR))
2599 stats->txunderflowerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002600 xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002601
2602 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_G))
2603 stats->txoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002604 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002605
2606 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_G))
2607 stats->txframecount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002608 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002609
2610 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXPAUSEFRAMES))
2611 stats->txpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002612 xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002613
2614 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXVLANFRAMES_G))
2615 stats->txvlanframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002616 xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002617}
2618
2619static void xgbe_rx_mmc_int(struct xgbe_prv_data *pdata)
2620{
2621 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2622 unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_RISR);
2623
2624 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFRAMECOUNT_GB))
2625 stats->rxframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002626 xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002627
2628 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_GB))
2629 stats->rxoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002630 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002631
2632 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_G))
2633 stats->rxoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002634 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002635
2636 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXBROADCASTFRAMES_G))
2637 stats->rxbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002638 xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002639
2640 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXMULTICASTFRAMES_G))
2641 stats->rxmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002642 xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002643
2644 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXCRCERROR))
2645 stats->rxcrcerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002646 xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002647
2648 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXRUNTERROR))
2649 stats->rxrunterror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002650 xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002651
2652 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXJABBERERROR))
2653 stats->rxjabbererror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002654 xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002655
2656 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNDERSIZE_G))
2657 stats->rxundersize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002658 xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002659
2660 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOVERSIZE_G))
2661 stats->rxoversize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002662 xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002663
2664 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX64OCTETS_GB))
2665 stats->rx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002666 xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002667
2668 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX65TO127OCTETS_GB))
2669 stats->rx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002670 xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002671
2672 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX128TO255OCTETS_GB))
2673 stats->rx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002674 xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002675
2676 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX256TO511OCTETS_GB))
2677 stats->rx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002678 xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002679
2680 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX512TO1023OCTETS_GB))
2681 stats->rx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002682 xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002683
2684 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX1024TOMAXOCTETS_GB))
2685 stats->rx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002686 xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002687
2688 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNICASTFRAMES_G))
2689 stats->rxunicastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002690 xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002691
2692 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXLENGTHERROR))
2693 stats->rxlengtherror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002694 xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002695
2696 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOUTOFRANGETYPE))
2697 stats->rxoutofrangetype +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002698 xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002699
2700 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXPAUSEFRAMES))
2701 stats->rxpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002702 xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002703
2704 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFIFOOVERFLOW))
2705 stats->rxfifooverflow +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002706 xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002707
2708 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXVLANFRAMES_GB))
2709 stats->rxvlanframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002710 xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002711
2712 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXWATCHDOGERROR))
2713 stats->rxwatchdogerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002714 xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002715}
2716
2717static void xgbe_read_mmc_stats(struct xgbe_prv_data *pdata)
2718{
2719 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2720
2721 /* Freeze counters */
2722 XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 1);
2723
2724 stats->txoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002725 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002726
2727 stats->txframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002728 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002729
2730 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002731 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002732
2733 stats->txmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002734 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002735
2736 stats->tx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002737 xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002738
2739 stats->tx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002740 xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002741
2742 stats->tx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002743 xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002744
2745 stats->tx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002746 xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002747
2748 stats->tx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002749 xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002750
2751 stats->tx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002752 xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002753
2754 stats->txunicastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002755 xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002756
2757 stats->txmulticastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002758 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002759
2760 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002761 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002762
2763 stats->txunderflowerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002764 xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002765
2766 stats->txoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002767 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002768
2769 stats->txframecount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002770 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002771
2772 stats->txpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002773 xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002774
2775 stats->txvlanframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002776 xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002777
2778 stats->rxframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002779 xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002780
2781 stats->rxoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002782 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002783
2784 stats->rxoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002785 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002786
2787 stats->rxbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002788 xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002789
2790 stats->rxmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002791 xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002792
2793 stats->rxcrcerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002794 xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002795
2796 stats->rxrunterror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002797 xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002798
2799 stats->rxjabbererror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002800 xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002801
2802 stats->rxundersize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002803 xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002804
2805 stats->rxoversize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002806 xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002807
2808 stats->rx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002809 xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002810
2811 stats->rx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002812 xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002813
2814 stats->rx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002815 xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002816
2817 stats->rx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002818 xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002819
2820 stats->rx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002821 xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002822
2823 stats->rx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002824 xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002825
2826 stats->rxunicastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002827 xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002828
2829 stats->rxlengtherror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002830 xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002831
2832 stats->rxoutofrangetype +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002833 xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002834
2835 stats->rxpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002836 xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002837
2838 stats->rxfifooverflow +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002839 xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002840
2841 stats->rxvlanframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002842 xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002843
2844 stats->rxwatchdogerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002845 xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002846
2847 /* Un-freeze counters */
2848 XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 0);
2849}
2850
2851static void xgbe_config_mmc(struct xgbe_prv_data *pdata)
2852{
2853 /* Set counters to reset on read */
2854 XGMAC_IOWRITE_BITS(pdata, MMC_CR, ROR, 1);
2855
2856 /* Reset the counters */
2857 XGMAC_IOWRITE_BITS(pdata, MMC_CR, CR, 1);
2858}
2859
Lendacky, Thomas16edd342014-11-20 11:03:32 -06002860static void xgbe_prepare_tx_stop(struct xgbe_prv_data *pdata,
2861 struct xgbe_channel *channel)
2862{
2863 unsigned int tx_dsr, tx_pos, tx_qidx;
2864 unsigned int tx_status;
2865 unsigned long tx_timeout;
2866
2867 /* Calculate the status register to read and the position within */
2868 if (channel->queue_index < DMA_DSRX_FIRST_QUEUE) {
2869 tx_dsr = DMA_DSR0;
2870 tx_pos = (channel->queue_index * DMA_DSR_Q_WIDTH) +
2871 DMA_DSR0_TPS_START;
2872 } else {
2873 tx_qidx = channel->queue_index - DMA_DSRX_FIRST_QUEUE;
2874
2875 tx_dsr = DMA_DSR1 + ((tx_qidx / DMA_DSRX_QPR) * DMA_DSRX_INC);
2876 tx_pos = ((tx_qidx % DMA_DSRX_QPR) * DMA_DSR_Q_WIDTH) +
2877 DMA_DSRX_TPS_START;
2878 }
2879
2880 /* The Tx engine cannot be stopped if it is actively processing
2881 * descriptors. Wait for the Tx engine to enter the stopped or
2882 * suspended state. Don't wait forever though...
2883 */
2884 tx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
2885 while (time_before(jiffies, tx_timeout)) {
2886 tx_status = XGMAC_IOREAD(pdata, tx_dsr);
2887 tx_status = GET_BITS(tx_status, tx_pos, DMA_DSR_TPS_WIDTH);
2888 if ((tx_status == DMA_TPS_STOPPED) ||
2889 (tx_status == DMA_TPS_SUSPENDED))
2890 break;
2891
2892 usleep_range(500, 1000);
2893 }
2894
2895 if (!time_before(jiffies, tx_timeout))
2896 netdev_info(pdata->netdev,
2897 "timed out waiting for Tx DMA channel %u to stop\n",
2898 channel->queue_index);
2899}
2900
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002901static void xgbe_enable_tx(struct xgbe_prv_data *pdata)
2902{
2903 struct xgbe_channel *channel;
2904 unsigned int i;
2905
2906 /* Enable each Tx DMA channel */
2907 channel = pdata->channel;
2908 for (i = 0; i < pdata->channel_count; i++, channel++) {
2909 if (!channel->tx_ring)
2910 break;
2911
2912 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 1);
2913 }
2914
2915 /* Enable each Tx queue */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002916 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002917 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN,
2918 MTL_Q_ENABLED);
2919
2920 /* Enable MAC Tx */
2921 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
2922}
2923
2924static void xgbe_disable_tx(struct xgbe_prv_data *pdata)
2925{
2926 struct xgbe_channel *channel;
2927 unsigned int i;
2928
Lendacky, Thomas16edd342014-11-20 11:03:32 -06002929 /* Prepare for Tx DMA channel stop */
2930 channel = pdata->channel;
2931 for (i = 0; i < pdata->channel_count; i++, channel++) {
2932 if (!channel->tx_ring)
2933 break;
2934
2935 xgbe_prepare_tx_stop(pdata, channel);
2936 }
2937
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002938 /* Disable MAC Tx */
2939 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
2940
2941 /* Disable each Tx queue */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002942 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002943 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN, 0);
2944
2945 /* Disable each Tx DMA channel */
2946 channel = pdata->channel;
2947 for (i = 0; i < pdata->channel_count; i++, channel++) {
2948 if (!channel->tx_ring)
2949 break;
2950
2951 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 0);
2952 }
2953}
2954
Lendacky, Thomasc3727d62016-02-17 11:49:16 -06002955static void xgbe_prepare_rx_stop(struct xgbe_prv_data *pdata,
2956 unsigned int queue)
2957{
2958 unsigned int rx_status;
2959 unsigned long rx_timeout;
2960
2961 /* The Rx engine cannot be stopped if it is actively processing
2962 * packets. Wait for the Rx queue to empty the Rx fifo. Don't
2963 * wait forever though...
2964 */
2965 rx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
2966 while (time_before(jiffies, rx_timeout)) {
2967 rx_status = XGMAC_MTL_IOREAD(pdata, queue, MTL_Q_RQDR);
2968 if ((XGMAC_GET_BITS(rx_status, MTL_Q_RQDR, PRXQ) == 0) &&
2969 (XGMAC_GET_BITS(rx_status, MTL_Q_RQDR, RXQSTS) == 0))
2970 break;
2971
2972 usleep_range(500, 1000);
2973 }
2974
2975 if (!time_before(jiffies, rx_timeout))
2976 netdev_info(pdata->netdev,
2977 "timed out waiting for Rx queue %u to empty\n",
2978 queue);
2979}
2980
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002981static void xgbe_enable_rx(struct xgbe_prv_data *pdata)
2982{
2983 struct xgbe_channel *channel;
2984 unsigned int reg_val, i;
2985
2986 /* Enable each Rx DMA channel */
2987 channel = pdata->channel;
2988 for (i = 0; i < pdata->channel_count; i++, channel++) {
2989 if (!channel->rx_ring)
2990 break;
2991
2992 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 1);
2993 }
2994
2995 /* Enable each Rx queue */
2996 reg_val = 0;
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002997 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002998 reg_val |= (0x02 << (i << 1));
2999 XGMAC_IOWRITE(pdata, MAC_RQC0R, reg_val);
3000
3001 /* Enable MAC Rx */
3002 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 1);
3003 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 1);
3004 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 1);
3005 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 1);
3006}
3007
3008static void xgbe_disable_rx(struct xgbe_prv_data *pdata)
3009{
3010 struct xgbe_channel *channel;
3011 unsigned int i;
3012
3013 /* Disable MAC Rx */
3014 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 0);
3015 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 0);
3016 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 0);
3017 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 0);
3018
Lendacky, Thomasc3727d62016-02-17 11:49:16 -06003019 /* Prepare for Rx DMA channel stop */
3020 for (i = 0; i < pdata->rx_q_count; i++)
3021 xgbe_prepare_rx_stop(pdata, i);
3022
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003023 /* Disable each Rx queue */
3024 XGMAC_IOWRITE(pdata, MAC_RQC0R, 0);
3025
3026 /* Disable each Rx DMA channel */
3027 channel = pdata->channel;
3028 for (i = 0; i < pdata->channel_count; i++, channel++) {
3029 if (!channel->rx_ring)
3030 break;
3031
3032 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 0);
3033 }
3034}
3035
3036static void xgbe_powerup_tx(struct xgbe_prv_data *pdata)
3037{
3038 struct xgbe_channel *channel;
3039 unsigned int i;
3040
3041 /* Enable each Tx DMA channel */
3042 channel = pdata->channel;
3043 for (i = 0; i < pdata->channel_count; i++, channel++) {
3044 if (!channel->tx_ring)
3045 break;
3046
3047 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 1);
3048 }
3049
3050 /* Enable MAC Tx */
3051 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
3052}
3053
3054static void xgbe_powerdown_tx(struct xgbe_prv_data *pdata)
3055{
3056 struct xgbe_channel *channel;
3057 unsigned int i;
3058
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003059 /* Prepare for Tx DMA channel stop */
3060 channel = pdata->channel;
3061 for (i = 0; i < pdata->channel_count; i++, channel++) {
3062 if (!channel->tx_ring)
3063 break;
3064
3065 xgbe_prepare_tx_stop(pdata, channel);
3066 }
3067
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003068 /* Disable MAC Tx */
3069 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
3070
3071 /* Disable each Tx DMA channel */
3072 channel = pdata->channel;
3073 for (i = 0; i < pdata->channel_count; i++, channel++) {
3074 if (!channel->tx_ring)
3075 break;
3076
3077 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 0);
3078 }
3079}
3080
3081static void xgbe_powerup_rx(struct xgbe_prv_data *pdata)
3082{
3083 struct xgbe_channel *channel;
3084 unsigned int i;
3085
3086 /* Enable each Rx DMA channel */
3087 channel = pdata->channel;
3088 for (i = 0; i < pdata->channel_count; i++, channel++) {
3089 if (!channel->rx_ring)
3090 break;
3091
3092 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 1);
3093 }
3094}
3095
3096static void xgbe_powerdown_rx(struct xgbe_prv_data *pdata)
3097{
3098 struct xgbe_channel *channel;
3099 unsigned int i;
3100
3101 /* Disable each Rx DMA channel */
3102 channel = pdata->channel;
3103 for (i = 0; i < pdata->channel_count; i++, channel++) {
3104 if (!channel->rx_ring)
3105 break;
3106
3107 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 0);
3108 }
3109}
3110
3111static int xgbe_init(struct xgbe_prv_data *pdata)
3112{
3113 struct xgbe_desc_if *desc_if = &pdata->desc_if;
3114 int ret;
3115
3116 DBGPR("-->xgbe_init\n");
3117
3118 /* Flush Tx queues */
3119 ret = xgbe_flush_tx_queues(pdata);
3120 if (ret)
3121 return ret;
3122
3123 /*
3124 * Initialize DMA related features
3125 */
3126 xgbe_config_dma_bus(pdata);
3127 xgbe_config_dma_cache(pdata);
3128 xgbe_config_osp_mode(pdata);
3129 xgbe_config_pblx8(pdata);
3130 xgbe_config_tx_pbl_val(pdata);
3131 xgbe_config_rx_pbl_val(pdata);
3132 xgbe_config_rx_coalesce(pdata);
3133 xgbe_config_tx_coalesce(pdata);
3134 xgbe_config_rx_buffer_size(pdata);
3135 xgbe_config_tso_mode(pdata);
Lendacky, Thomas174fd252014-11-04 16:06:50 -06003136 xgbe_config_sph_mode(pdata);
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003137 xgbe_config_rss(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003138 desc_if->wrapper_tx_desc_init(pdata);
3139 desc_if->wrapper_rx_desc_init(pdata);
3140 xgbe_enable_dma_interrupts(pdata);
3141
3142 /*
3143 * Initialize MTL related features
3144 */
3145 xgbe_config_mtl_mode(pdata);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003146 xgbe_config_queue_mapping(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003147 xgbe_config_tsf_mode(pdata, pdata->tx_sf_mode);
3148 xgbe_config_rsf_mode(pdata, pdata->rx_sf_mode);
3149 xgbe_config_tx_threshold(pdata, pdata->tx_threshold);
3150 xgbe_config_rx_threshold(pdata, pdata->rx_threshold);
3151 xgbe_config_tx_fifo_size(pdata);
3152 xgbe_config_rx_fifo_size(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003153 /*TODO: Error Packet and undersized good Packet forwarding enable
3154 (FEP and FUP)
3155 */
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003156 xgbe_config_dcb_tc(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003157 xgbe_enable_mtl_interrupts(pdata);
3158
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003159 /*
3160 * Initialize MAC related features
3161 */
3162 xgbe_config_mac_address(pdata);
Lendacky, Thomasb8763822015-04-09 12:11:57 -05003163 xgbe_config_rx_mode(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003164 xgbe_config_jumbo_enable(pdata);
3165 xgbe_config_flow_control(pdata);
Lendacky, Thomas916102c2015-01-16 12:46:45 -06003166 xgbe_config_mac_speed(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003167 xgbe_config_checksum_offload(pdata);
3168 xgbe_config_vlan_support(pdata);
3169 xgbe_config_mmc(pdata);
3170 xgbe_enable_mac_interrupts(pdata);
3171
3172 DBGPR("<--xgbe_init\n");
3173
3174 return 0;
3175}
3176
3177void xgbe_init_function_ptrs_dev(struct xgbe_hw_if *hw_if)
3178{
3179 DBGPR("-->xgbe_init_function_ptrs\n");
3180
3181 hw_if->tx_complete = xgbe_tx_complete;
3182
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003183 hw_if->set_mac_address = xgbe_set_mac_address;
Lendacky, Thomasb8763822015-04-09 12:11:57 -05003184 hw_if->config_rx_mode = xgbe_config_rx_mode;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003185
3186 hw_if->enable_rx_csum = xgbe_enable_rx_csum;
3187 hw_if->disable_rx_csum = xgbe_disable_rx_csum;
3188
3189 hw_if->enable_rx_vlan_stripping = xgbe_enable_rx_vlan_stripping;
3190 hw_if->disable_rx_vlan_stripping = xgbe_disable_rx_vlan_stripping;
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05003191 hw_if->enable_rx_vlan_filtering = xgbe_enable_rx_vlan_filtering;
3192 hw_if->disable_rx_vlan_filtering = xgbe_disable_rx_vlan_filtering;
3193 hw_if->update_vlan_hash_table = xgbe_update_vlan_hash_table;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003194
3195 hw_if->read_mmd_regs = xgbe_read_mmd_regs;
3196 hw_if->write_mmd_regs = xgbe_write_mmd_regs;
3197
3198 hw_if->set_gmii_speed = xgbe_set_gmii_speed;
3199 hw_if->set_gmii_2500_speed = xgbe_set_gmii_2500_speed;
3200 hw_if->set_xgmii_speed = xgbe_set_xgmii_speed;
3201
3202 hw_if->enable_tx = xgbe_enable_tx;
3203 hw_if->disable_tx = xgbe_disable_tx;
3204 hw_if->enable_rx = xgbe_enable_rx;
3205 hw_if->disable_rx = xgbe_disable_rx;
3206
3207 hw_if->powerup_tx = xgbe_powerup_tx;
3208 hw_if->powerdown_tx = xgbe_powerdown_tx;
3209 hw_if->powerup_rx = xgbe_powerup_rx;
3210 hw_if->powerdown_rx = xgbe_powerdown_rx;
3211
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06003212 hw_if->dev_xmit = xgbe_dev_xmit;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003213 hw_if->dev_read = xgbe_dev_read;
3214 hw_if->enable_int = xgbe_enable_int;
3215 hw_if->disable_int = xgbe_disable_int;
3216 hw_if->init = xgbe_init;
3217 hw_if->exit = xgbe_exit;
3218
3219 /* Descriptor related Sequences have to be initialized here */
3220 hw_if->tx_desc_init = xgbe_tx_desc_init;
3221 hw_if->rx_desc_init = xgbe_rx_desc_init;
3222 hw_if->tx_desc_reset = xgbe_tx_desc_reset;
3223 hw_if->rx_desc_reset = xgbe_rx_desc_reset;
3224 hw_if->is_last_desc = xgbe_is_last_desc;
3225 hw_if->is_context_desc = xgbe_is_context_desc;
Lendacky, Thomas16958a22014-11-20 11:04:08 -06003226 hw_if->tx_start_xmit = xgbe_tx_start_xmit;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003227
3228 /* For FLOW ctrl */
3229 hw_if->config_tx_flow_control = xgbe_config_tx_flow_control;
3230 hw_if->config_rx_flow_control = xgbe_config_rx_flow_control;
3231
3232 /* For RX coalescing */
3233 hw_if->config_rx_coalesce = xgbe_config_rx_coalesce;
3234 hw_if->config_tx_coalesce = xgbe_config_tx_coalesce;
3235 hw_if->usec_to_riwt = xgbe_usec_to_riwt;
3236 hw_if->riwt_to_usec = xgbe_riwt_to_usec;
3237
3238 /* For RX and TX threshold config */
3239 hw_if->config_rx_threshold = xgbe_config_rx_threshold;
3240 hw_if->config_tx_threshold = xgbe_config_tx_threshold;
3241
3242 /* For RX and TX Store and Forward Mode config */
3243 hw_if->config_rsf_mode = xgbe_config_rsf_mode;
3244 hw_if->config_tsf_mode = xgbe_config_tsf_mode;
3245
3246 /* For TX DMA Operating on Second Frame config */
3247 hw_if->config_osp_mode = xgbe_config_osp_mode;
3248
3249 /* For RX and TX PBL config */
3250 hw_if->config_rx_pbl_val = xgbe_config_rx_pbl_val;
3251 hw_if->get_rx_pbl_val = xgbe_get_rx_pbl_val;
3252 hw_if->config_tx_pbl_val = xgbe_config_tx_pbl_val;
3253 hw_if->get_tx_pbl_val = xgbe_get_tx_pbl_val;
3254 hw_if->config_pblx8 = xgbe_config_pblx8;
3255
3256 /* For MMC statistics support */
3257 hw_if->tx_mmc_int = xgbe_tx_mmc_int;
3258 hw_if->rx_mmc_int = xgbe_rx_mmc_int;
3259 hw_if->read_mmc_stats = xgbe_read_mmc_stats;
3260
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05003261 /* For PTP config */
3262 hw_if->config_tstamp = xgbe_config_tstamp;
3263 hw_if->update_tstamp_addend = xgbe_update_tstamp_addend;
3264 hw_if->set_tstamp_time = xgbe_set_tstamp_time;
3265 hw_if->get_tstamp_time = xgbe_get_tstamp_time;
3266 hw_if->get_tx_tstamp = xgbe_get_tx_tstamp;
3267
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003268 /* For Data Center Bridging config */
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06003269 hw_if->config_tc = xgbe_config_tc;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003270 hw_if->config_dcb_tc = xgbe_config_dcb_tc;
3271 hw_if->config_dcb_pfc = xgbe_config_dcb_pfc;
3272
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003273 /* For Receive Side Scaling */
3274 hw_if->enable_rss = xgbe_enable_rss;
3275 hw_if->disable_rss = xgbe_disable_rss;
Lendacky, Thomasf6ac8622014-11-04 16:07:23 -06003276 hw_if->set_rss_hash_key = xgbe_set_rss_hash_key;
3277 hw_if->set_rss_lookup_table = xgbe_set_rss_lookup_table;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003278
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003279 DBGPR("<--xgbe_init_function_ptrs\n");
3280}