blob: 75c3df1251c8ec6f8673aa056ee0c4dad0bb9f79 [file] [log] [blame]
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001/*
2 * AMD 10Gb Ethernet driver
3 *
4 * This file is available to you under your choice of the following two
5 * licenses:
6 *
7 * License 1: GPLv2
8 *
Lendacky, Thomasb4eee842016-02-17 11:48:08 -06009 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -050010 *
11 * This file is free software; you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or (at
14 * your option) any later version.
15 *
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 * This file incorporates work covered by the following copyright and
25 * permission notice:
26 * The Synopsys DWC ETHER XGMAC Software Driver and documentation
27 * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
28 * Inc. unless otherwise expressly agreed to in writing between Synopsys
29 * and you.
30 *
31 * The Software IS NOT an item of Licensed Software or Licensed Product
32 * under any End User Software License Agreement or Agreement for Licensed
33 * Product with Synopsys or any supplement thereto. Permission is hereby
34 * granted, free of charge, to any person obtaining a copy of this software
35 * annotated with this license and the Software, to deal in the Software
36 * without restriction, including without limitation the rights to use,
37 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
38 * of the Software, and to permit persons to whom the Software is furnished
39 * to do so, subject to the following conditions:
40 *
41 * The above copyright notice and this permission notice shall be included
42 * in all copies or substantial portions of the Software.
43 *
44 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
45 * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
47 * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
48 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54 * THE POSSIBILITY OF SUCH DAMAGE.
55 *
56 *
57 * License 2: Modified BSD
58 *
Lendacky, Thomasb4eee842016-02-17 11:48:08 -060059 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -050060 * All rights reserved.
61 *
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions are met:
64 * * Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 * * Redistributions in binary form must reproduce the above copyright
67 * notice, this list of conditions and the following disclaimer in the
68 * documentation and/or other materials provided with the distribution.
69 * * Neither the name of Advanced Micro Devices, Inc. nor the
70 * names of its contributors may be used to endorse or promote products
71 * derived from this software without specific prior written permission.
72 *
73 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76 * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
77 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
78 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
79 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
80 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83 *
84 * This file incorporates work covered by the following copyright and
85 * permission notice:
86 * The Synopsys DWC ETHER XGMAC Software Driver and documentation
87 * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
88 * Inc. unless otherwise expressly agreed to in writing between Synopsys
89 * and you.
90 *
91 * The Software IS NOT an item of Licensed Software or Licensed Product
92 * under any End User Software License Agreement or Agreement for Licensed
93 * Product with Synopsys or any supplement thereto. Permission is hereby
94 * granted, free of charge, to any person obtaining a copy of this software
95 * annotated with this license and the Software, to deal in the Software
96 * without restriction, including without limitation the rights to use,
97 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98 * of the Software, and to permit persons to whom the Software is furnished
99 * to do so, subject to the following conditions:
100 *
101 * The above copyright notice and this permission notice shall be included
102 * in all copies or substantial portions of the Software.
103 *
104 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105 * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107 * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114 * THE POSSIBILITY OF SUCH DAMAGE.
115 */
116
117#include <linux/phy.h>
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600118#include <linux/mdio.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500119#include <linux/clk.h>
Lendacky, Thomas801c62d2014-06-24 16:19:24 -0500120#include <linux/bitrev.h>
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500121#include <linux/crc32.h>
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500122
123#include "xgbe.h"
124#include "xgbe-common.h"
125
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500126static inline unsigned int xgbe_get_max_frame(struct xgbe_prv_data *pdata)
127{
128 return pdata->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
129}
130
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500131static unsigned int xgbe_usec_to_riwt(struct xgbe_prv_data *pdata,
132 unsigned int usec)
133{
134 unsigned long rate;
135 unsigned int ret;
136
137 DBGPR("-->xgbe_usec_to_riwt\n");
138
Lendacky, Thomas82a19032015-01-16 12:47:16 -0600139 rate = pdata->sysclk_rate;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500140
141 /*
142 * Convert the input usec value to the watchdog timer value. Each
143 * watchdog timer value is equivalent to 256 clock cycles.
144 * Calculate the required value as:
145 * ( usec * ( system_clock_mhz / 10^6 ) / 256
146 */
147 ret = (usec * (rate / 1000000)) / 256;
148
149 DBGPR("<--xgbe_usec_to_riwt\n");
150
151 return ret;
152}
153
154static unsigned int xgbe_riwt_to_usec(struct xgbe_prv_data *pdata,
155 unsigned int riwt)
156{
157 unsigned long rate;
158 unsigned int ret;
159
160 DBGPR("-->xgbe_riwt_to_usec\n");
161
Lendacky, Thomas82a19032015-01-16 12:47:16 -0600162 rate = pdata->sysclk_rate;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500163
164 /*
165 * Convert the input watchdog timer value to the usec value. Each
166 * watchdog timer value is equivalent to 256 clock cycles.
167 * Calculate the required value as:
168 * ( riwt * 256 ) / ( system_clock_mhz / 10^6 )
169 */
170 ret = (riwt * 256) / (rate / 1000000);
171
172 DBGPR("<--xgbe_riwt_to_usec\n");
173
174 return ret;
175}
176
177static int xgbe_config_pblx8(struct xgbe_prv_data *pdata)
178{
179 struct xgbe_channel *channel;
180 unsigned int i;
181
182 channel = pdata->channel;
183 for (i = 0; i < pdata->channel_count; i++, channel++)
184 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_CR, PBLX8,
185 pdata->pblx8);
186
187 return 0;
188}
189
190static int xgbe_get_tx_pbl_val(struct xgbe_prv_data *pdata)
191{
192 return XGMAC_DMA_IOREAD_BITS(pdata->channel, DMA_CH_TCR, PBL);
193}
194
195static int xgbe_config_tx_pbl_val(struct xgbe_prv_data *pdata)
196{
197 struct xgbe_channel *channel;
198 unsigned int i;
199
200 channel = pdata->channel;
201 for (i = 0; i < pdata->channel_count; i++, channel++) {
202 if (!channel->tx_ring)
203 break;
204
205 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, PBL,
206 pdata->tx_pbl);
207 }
208
209 return 0;
210}
211
212static int xgbe_get_rx_pbl_val(struct xgbe_prv_data *pdata)
213{
214 return XGMAC_DMA_IOREAD_BITS(pdata->channel, DMA_CH_RCR, PBL);
215}
216
217static int xgbe_config_rx_pbl_val(struct xgbe_prv_data *pdata)
218{
219 struct xgbe_channel *channel;
220 unsigned int i;
221
222 channel = pdata->channel;
223 for (i = 0; i < pdata->channel_count; i++, channel++) {
224 if (!channel->rx_ring)
225 break;
226
227 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, PBL,
228 pdata->rx_pbl);
229 }
230
231 return 0;
232}
233
234static int xgbe_config_osp_mode(struct xgbe_prv_data *pdata)
235{
236 struct xgbe_channel *channel;
237 unsigned int i;
238
239 channel = pdata->channel;
240 for (i = 0; i < pdata->channel_count; i++, channel++) {
241 if (!channel->tx_ring)
242 break;
243
244 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, OSP,
245 pdata->tx_osp_mode);
246 }
247
248 return 0;
249}
250
251static int xgbe_config_rsf_mode(struct xgbe_prv_data *pdata, unsigned int val)
252{
253 unsigned int i;
254
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500255 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500256 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RSF, val);
257
258 return 0;
259}
260
261static int xgbe_config_tsf_mode(struct xgbe_prv_data *pdata, unsigned int val)
262{
263 unsigned int i;
264
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500265 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500266 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TSF, val);
267
268 return 0;
269}
270
271static int xgbe_config_rx_threshold(struct xgbe_prv_data *pdata,
272 unsigned int val)
273{
274 unsigned int i;
275
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500276 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500277 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RTC, val);
278
279 return 0;
280}
281
282static int xgbe_config_tx_threshold(struct xgbe_prv_data *pdata,
283 unsigned int val)
284{
285 unsigned int i;
286
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500287 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500288 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TTC, val);
289
290 return 0;
291}
292
293static int xgbe_config_rx_coalesce(struct xgbe_prv_data *pdata)
294{
295 struct xgbe_channel *channel;
296 unsigned int i;
297
298 channel = pdata->channel;
299 for (i = 0; i < pdata->channel_count; i++, channel++) {
300 if (!channel->rx_ring)
301 break;
302
303 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RIWT, RWT,
304 pdata->rx_riwt);
305 }
306
307 return 0;
308}
309
310static int xgbe_config_tx_coalesce(struct xgbe_prv_data *pdata)
311{
312 return 0;
313}
314
315static void xgbe_config_rx_buffer_size(struct xgbe_prv_data *pdata)
316{
317 struct xgbe_channel *channel;
318 unsigned int i;
319
320 channel = pdata->channel;
321 for (i = 0; i < pdata->channel_count; i++, channel++) {
322 if (!channel->rx_ring)
323 break;
324
325 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, RBSZ,
326 pdata->rx_buf_size);
327 }
328}
329
330static void xgbe_config_tso_mode(struct xgbe_prv_data *pdata)
331{
332 struct xgbe_channel *channel;
333 unsigned int i;
334
335 channel = pdata->channel;
336 for (i = 0; i < pdata->channel_count; i++, channel++) {
337 if (!channel->tx_ring)
338 break;
339
340 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, TSE, 1);
341 }
342}
343
Lendacky, Thomas174fd252014-11-04 16:06:50 -0600344static void xgbe_config_sph_mode(struct xgbe_prv_data *pdata)
345{
346 struct xgbe_channel *channel;
347 unsigned int i;
348
349 channel = pdata->channel;
350 for (i = 0; i < pdata->channel_count; i++, channel++) {
351 if (!channel->rx_ring)
352 break;
353
354 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_CR, SPH, 1);
355 }
356
357 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, HDSMS, XGBE_SPH_HDSMS_SIZE);
358}
359
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -0600360static int xgbe_write_rss_reg(struct xgbe_prv_data *pdata, unsigned int type,
361 unsigned int index, unsigned int val)
362{
363 unsigned int wait;
364 int ret = 0;
365
366 mutex_lock(&pdata->rss_mutex);
367
368 if (XGMAC_IOREAD_BITS(pdata, MAC_RSSAR, OB)) {
369 ret = -EBUSY;
370 goto unlock;
371 }
372
373 XGMAC_IOWRITE(pdata, MAC_RSSDR, val);
374
375 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, RSSIA, index);
376 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, ADDRT, type);
377 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, CT, 0);
378 XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, OB, 1);
379
380 wait = 1000;
381 while (wait--) {
382 if (!XGMAC_IOREAD_BITS(pdata, MAC_RSSAR, OB))
383 goto unlock;
384
385 usleep_range(1000, 1500);
386 }
387
388 ret = -EBUSY;
389
390unlock:
391 mutex_unlock(&pdata->rss_mutex);
392
393 return ret;
394}
395
396static int xgbe_write_rss_hash_key(struct xgbe_prv_data *pdata)
397{
398 unsigned int key_regs = sizeof(pdata->rss_key) / sizeof(u32);
399 unsigned int *key = (unsigned int *)&pdata->rss_key;
400 int ret;
401
402 while (key_regs--) {
403 ret = xgbe_write_rss_reg(pdata, XGBE_RSS_HASH_KEY_TYPE,
404 key_regs, *key++);
405 if (ret)
406 return ret;
407 }
408
409 return 0;
410}
411
412static int xgbe_write_rss_lookup_table(struct xgbe_prv_data *pdata)
413{
414 unsigned int i;
415 int ret;
416
417 for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++) {
418 ret = xgbe_write_rss_reg(pdata,
419 XGBE_RSS_LOOKUP_TABLE_TYPE, i,
420 pdata->rss_table[i]);
421 if (ret)
422 return ret;
423 }
424
425 return 0;
426}
427
Lendacky, Thomasf6ac8622014-11-04 16:07:23 -0600428static int xgbe_set_rss_hash_key(struct xgbe_prv_data *pdata, const u8 *key)
429{
430 memcpy(pdata->rss_key, key, sizeof(pdata->rss_key));
431
432 return xgbe_write_rss_hash_key(pdata);
433}
434
435static int xgbe_set_rss_lookup_table(struct xgbe_prv_data *pdata,
436 const u32 *table)
437{
438 unsigned int i;
439
440 for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++)
441 XGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH, table[i]);
442
443 return xgbe_write_rss_lookup_table(pdata);
444}
445
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -0600446static int xgbe_enable_rss(struct xgbe_prv_data *pdata)
447{
448 int ret;
449
450 if (!pdata->hw_feat.rss)
451 return -EOPNOTSUPP;
452
453 /* Program the hash key */
454 ret = xgbe_write_rss_hash_key(pdata);
455 if (ret)
456 return ret;
457
458 /* Program the lookup table */
459 ret = xgbe_write_rss_lookup_table(pdata);
460 if (ret)
461 return ret;
462
463 /* Set the RSS options */
464 XGMAC_IOWRITE(pdata, MAC_RSSCR, pdata->rss_options);
465
466 /* Enable RSS */
467 XGMAC_IOWRITE_BITS(pdata, MAC_RSSCR, RSSE, 1);
468
469 return 0;
470}
471
472static int xgbe_disable_rss(struct xgbe_prv_data *pdata)
473{
474 if (!pdata->hw_feat.rss)
475 return -EOPNOTSUPP;
476
477 XGMAC_IOWRITE_BITS(pdata, MAC_RSSCR, RSSE, 0);
478
479 return 0;
480}
481
482static void xgbe_config_rss(struct xgbe_prv_data *pdata)
483{
484 int ret;
485
486 if (!pdata->hw_feat.rss)
487 return;
488
489 if (pdata->netdev->features & NETIF_F_RXHASH)
490 ret = xgbe_enable_rss(pdata);
491 else
492 ret = xgbe_disable_rss(pdata);
493
494 if (ret)
495 netdev_err(pdata->netdev,
496 "error configuring RSS, RSS disabled\n");
497}
498
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500499static bool xgbe_is_pfc_queue(struct xgbe_prv_data *pdata,
500 unsigned int queue)
501{
502 unsigned int prio, tc;
503
504 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
505 /* Does this queue handle the priority? */
506 if (pdata->prio2q_map[prio] != queue)
507 continue;
508
509 /* Get the Traffic Class for this priority */
510 tc = pdata->ets->prio_tc[prio];
511
512 /* Check if PFC is enabled for this traffic class */
513 if (pdata->pfc->pfc_en & (1 << tc))
514 return true;
515 }
516
517 return false;
518}
519
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500520static int xgbe_disable_tx_flow_control(struct xgbe_prv_data *pdata)
521{
522 unsigned int max_q_count, q_count;
523 unsigned int reg, reg_val;
524 unsigned int i;
525
526 /* Clear MTL flow control */
Lendacky, Thomas853eb162014-07-29 08:57:31 -0500527 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500528 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, 0);
529
530 /* Clear MAC flow control */
531 max_q_count = XGMAC_MAX_FLOW_CONTROL_QUEUES;
Lendacky, Thomas9fc69af2014-08-29 13:17:08 -0500532 q_count = min_t(unsigned int, pdata->tx_q_count, max_q_count);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500533 reg = MAC_Q0TFCR;
534 for (i = 0; i < q_count; i++) {
535 reg_val = XGMAC_IOREAD(pdata, reg);
536 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 0);
537 XGMAC_IOWRITE(pdata, reg, reg_val);
538
539 reg += MAC_QTFCR_INC;
540 }
541
542 return 0;
543}
544
545static int xgbe_enable_tx_flow_control(struct xgbe_prv_data *pdata)
546{
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600547 struct ieee_pfc *pfc = pdata->pfc;
548 struct ieee_ets *ets = pdata->ets;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500549 unsigned int max_q_count, q_count;
550 unsigned int reg, reg_val;
551 unsigned int i;
552
553 /* Set MTL flow control */
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600554 for (i = 0; i < pdata->rx_q_count; i++) {
555 unsigned int ehfc = 0;
556
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500557 if (pdata->rx_rfd[i]) {
558 /* Flow control thresholds are established */
559 if (pfc && ets) {
560 if (xgbe_is_pfc_queue(pdata, i))
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600561 ehfc = 1;
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -0500562 } else {
563 ehfc = 1;
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600564 }
Lendacky, Thomas8dba2a22016-02-17 11:48:48 -0600565 }
566
567 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, ehfc);
568
569 netif_dbg(pdata, drv, pdata->netdev,
570 "flow control %s for RXq%u\n",
571 ehfc ? "enabled" : "disabled", i);
572 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500573
574 /* Set MAC flow control */
575 max_q_count = XGMAC_MAX_FLOW_CONTROL_QUEUES;
Lendacky, Thomas9fc69af2014-08-29 13:17:08 -0500576 q_count = min_t(unsigned int, pdata->tx_q_count, max_q_count);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500577 reg = MAC_Q0TFCR;
578 for (i = 0; i < q_count; i++) {
579 reg_val = XGMAC_IOREAD(pdata, reg);
580
581 /* Enable transmit flow control */
582 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 1);
583 /* Set pause time */
584 XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, PT, 0xffff);
585
586 XGMAC_IOWRITE(pdata, reg, reg_val);
587
588 reg += MAC_QTFCR_INC;
589 }
590
591 return 0;
592}
593
594static int xgbe_disable_rx_flow_control(struct xgbe_prv_data *pdata)
595{
596 XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 0);
597
598 return 0;
599}
600
601static int xgbe_enable_rx_flow_control(struct xgbe_prv_data *pdata)
602{
603 XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 1);
604
605 return 0;
606}
607
608static int xgbe_config_tx_flow_control(struct xgbe_prv_data *pdata)
609{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500610 struct ieee_pfc *pfc = pdata->pfc;
611
612 if (pdata->tx_pause || (pfc && pfc->pfc_en))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500613 xgbe_enable_tx_flow_control(pdata);
614 else
615 xgbe_disable_tx_flow_control(pdata);
616
617 return 0;
618}
619
620static int xgbe_config_rx_flow_control(struct xgbe_prv_data *pdata)
621{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500622 struct ieee_pfc *pfc = pdata->pfc;
623
624 if (pdata->rx_pause || (pfc && pfc->pfc_en))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500625 xgbe_enable_rx_flow_control(pdata);
626 else
627 xgbe_disable_rx_flow_control(pdata);
628
629 return 0;
630}
631
632static void xgbe_config_flow_control(struct xgbe_prv_data *pdata)
633{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500634 struct ieee_pfc *pfc = pdata->pfc;
635
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500636 xgbe_config_tx_flow_control(pdata);
637 xgbe_config_rx_flow_control(pdata);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -0500638
639 XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, PFCE,
640 (pfc && pfc->pfc_en) ? 1 : 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500641}
642
643static void xgbe_enable_dma_interrupts(struct xgbe_prv_data *pdata)
644{
645 struct xgbe_channel *channel;
646 unsigned int dma_ch_isr, dma_ch_ier;
647 unsigned int i;
648
649 channel = pdata->channel;
650 for (i = 0; i < pdata->channel_count; i++, channel++) {
651 /* Clear all the interrupts which are set */
652 dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
653 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
654
655 /* Clear all interrupt enable bits */
656 dma_ch_ier = 0;
657
658 /* Enable following interrupts
659 * NIE - Normal Interrupt Summary Enable
660 * AIE - Abnormal Interrupt Summary Enable
661 * FBEE - Fatal Bus Error Enable
662 */
663 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, NIE, 1);
664 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, AIE, 1);
665 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
666
667 if (channel->tx_ring) {
668 /* Enable the following Tx interrupts
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600669 * TIE - Transmit Interrupt Enable (unless using
670 * per channel interrupts)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500671 */
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600672 if (!pdata->per_channel_irq)
673 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500674 }
675 if (channel->rx_ring) {
676 /* Enable following Rx interrupts
677 * RBUE - Receive Buffer Unavailable Enable
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600678 * RIE - Receive Interrupt Enable (unless using
679 * per channel interrupts)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500680 */
681 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 1);
Lendacky, Thomas9227dc52014-11-04 16:06:56 -0600682 if (!pdata->per_channel_irq)
683 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500684 }
685
686 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
687 }
688}
689
690static void xgbe_enable_mtl_interrupts(struct xgbe_prv_data *pdata)
691{
692 unsigned int mtl_q_isr;
693 unsigned int q_count, i;
694
695 q_count = max(pdata->hw_feat.tx_q_cnt, pdata->hw_feat.rx_q_cnt);
696 for (i = 0; i < q_count; i++) {
697 /* Clear all the interrupts which are set */
698 mtl_q_isr = XGMAC_MTL_IOREAD(pdata, i, MTL_Q_ISR);
699 XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_ISR, mtl_q_isr);
700
701 /* No MTL interrupts to be enabled */
Lendacky, Thomas91f87342014-07-02 13:04:34 -0500702 XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_IER, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500703 }
704}
705
706static void xgbe_enable_mac_interrupts(struct xgbe_prv_data *pdata)
707{
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -0500708 unsigned int mac_ier = 0;
709
710 /* Enable Timestamp interrupt */
711 XGMAC_SET_BITS(mac_ier, MAC_IER, TSIE, 1);
712
713 XGMAC_IOWRITE(pdata, MAC_IER, mac_ier);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500714
715 /* Enable all counter interrupts */
Lendacky, Thomasa3ba7c92014-09-05 18:02:36 -0500716 XGMAC_IOWRITE_BITS(pdata, MMC_RIER, ALL_INTERRUPTS, 0xffffffff);
717 XGMAC_IOWRITE_BITS(pdata, MMC_TIER, ALL_INTERRUPTS, 0xffffffff);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500718}
719
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500720static int xgbe_set_speed(struct xgbe_prv_data *pdata, int speed)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500721{
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500722 unsigned int ss;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600723
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500724 switch (speed) {
725 case SPEED_1000:
726 ss = 0x03;
727 break;
728 case SPEED_2500:
729 ss = 0x02;
730 break;
731 case SPEED_10000:
732 ss = 0x00;
733 break;
734 default:
735 return -EINVAL;
736 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500737
Lendacky, Thomase57f7a32016-11-03 13:18:27 -0500738 if (XGMAC_IOREAD_BITS(pdata, MAC_TCR, SS) != ss)
739 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, ss);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500740
741 return 0;
742}
743
Lendacky, Thomasb4eee842016-02-17 11:48:08 -0600744static int xgbe_enable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
745{
746 /* Put the VLAN tag in the Rx descriptor */
747 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLRXS, 1);
748
749 /* Don't check the VLAN type */
750 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, DOVLTC, 1);
751
752 /* Check only C-TAG (0x8100) packets */
753 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ERSVLM, 0);
754
755 /* Don't consider an S-TAG (0x88A8) packet as a VLAN packet */
756 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ESVL, 0);
757
758 /* Enable VLAN tag stripping */
759 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0x3);
760
761 return 0;
762}
763
764static int xgbe_disable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
765{
766 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0);
767
768 return 0;
769}
770
771static int xgbe_enable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
772{
773 /* Enable VLAN filtering */
774 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 1);
775
776 /* Enable VLAN Hash Table filtering */
777 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTHM, 1);
778
779 /* Disable VLAN tag inverse matching */
780 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTIM, 0);
781
782 /* Only filter on the lower 12-bits of the VLAN tag */
783 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ETV, 1);
784
785 /* In order for the VLAN Hash Table filtering to be effective,
786 * the VLAN tag identifier in the VLAN Tag Register must not
787 * be zero. Set the VLAN tag identifier to "1" to enable the
788 * VLAN Hash Table filtering. This implies that a VLAN tag of
789 * 1 will always pass filtering.
790 */
791 XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VL, 1);
792
793 return 0;
794}
795
796static int xgbe_disable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
797{
798 /* Disable VLAN filtering */
799 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 0);
800
801 return 0;
802}
803
804static u32 xgbe_vid_crc32_le(__le16 vid_le)
805{
806 u32 poly = 0xedb88320; /* CRCPOLY_LE */
807 u32 crc = ~0;
808 u32 temp = 0;
809 unsigned char *data = (unsigned char *)&vid_le;
810 unsigned char data_byte = 0;
811 int i, bits;
812
813 bits = get_bitmask_order(VLAN_VID_MASK);
814 for (i = 0; i < bits; i++) {
815 if ((i % 8) == 0)
816 data_byte = data[i / 8];
817
818 temp = ((crc & 1) ^ data_byte) & 1;
819 crc >>= 1;
820 data_byte >>= 1;
821
822 if (temp)
823 crc ^= poly;
824 }
825
826 return crc;
827}
828
829static int xgbe_update_vlan_hash_table(struct xgbe_prv_data *pdata)
830{
831 u32 crc;
832 u16 vid;
833 __le16 vid_le;
834 u16 vlan_hash_table = 0;
835
836 /* Generate the VLAN Hash Table value */
837 for_each_set_bit(vid, pdata->active_vlans, VLAN_N_VID) {
838 /* Get the CRC32 value of the VLAN ID */
839 vid_le = cpu_to_le16(vid);
840 crc = bitrev32(~xgbe_vid_crc32_le(vid_le)) >> 28;
841
842 vlan_hash_table |= (1 << crc);
843 }
844
845 /* Set the VLAN Hash Table filtering register */
846 XGMAC_IOWRITE_BITS(pdata, MAC_VLANHTR, VLHT, vlan_hash_table);
847
848 return 0;
849}
850
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500851static int xgbe_set_promiscuous_mode(struct xgbe_prv_data *pdata,
852 unsigned int enable)
853{
854 unsigned int val = enable ? 1 : 0;
855
856 if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PR) == val)
857 return 0;
858
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500859 netif_dbg(pdata, drv, pdata->netdev, "%s promiscuous mode\n",
860 enable ? "entering" : "leaving");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500861 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, val);
862
Lendacky, Thomasb4eee842016-02-17 11:48:08 -0600863 /* Hardware will still perform VLAN filtering in promiscuous mode */
864 if (enable) {
865 xgbe_disable_rx_vlan_filtering(pdata);
866 } else {
867 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
868 xgbe_enable_rx_vlan_filtering(pdata);
869 }
870
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500871 return 0;
872}
873
874static int xgbe_set_all_multicast_mode(struct xgbe_prv_data *pdata,
875 unsigned int enable)
876{
877 unsigned int val = enable ? 1 : 0;
878
879 if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PM) == val)
880 return 0;
881
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500882 netif_dbg(pdata, drv, pdata->netdev, "%s allmulti mode\n",
883 enable ? "entering" : "leaving");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500884 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PM, val);
885
886 return 0;
887}
888
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500889static void xgbe_set_mac_reg(struct xgbe_prv_data *pdata,
890 struct netdev_hw_addr *ha, unsigned int *mac_reg)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500891{
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500892 unsigned int mac_addr_hi, mac_addr_lo;
893 u8 *mac_addr;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500894
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500895 mac_addr_lo = 0;
896 mac_addr_hi = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500897
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500898 if (ha) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500899 mac_addr = (u8 *)&mac_addr_lo;
900 mac_addr[0] = ha->addr[0];
901 mac_addr[1] = ha->addr[1];
902 mac_addr[2] = ha->addr[2];
903 mac_addr[3] = ha->addr[3];
904 mac_addr = (u8 *)&mac_addr_hi;
905 mac_addr[0] = ha->addr[4];
906 mac_addr[1] = ha->addr[5];
907
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -0500908 netif_dbg(pdata, drv, pdata->netdev,
909 "adding mac address %pM at %#x\n",
910 ha->addr, *mac_reg);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500911
912 XGMAC_SET_BITS(mac_addr_hi, MAC_MACA1HR, AE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500913 }
914
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500915 XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_hi);
916 *mac_reg += MAC_MACA_INC;
917 XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_lo);
918 *mac_reg += MAC_MACA_INC;
919}
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500920
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500921static void xgbe_set_mac_addn_addrs(struct xgbe_prv_data *pdata)
922{
923 struct net_device *netdev = pdata->netdev;
924 struct netdev_hw_addr *ha;
925 unsigned int mac_reg;
926 unsigned int addn_macs;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500927
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500928 mac_reg = MAC_MACA1HR;
929 addn_macs = pdata->hw_feat.addn_mac;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500930
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500931 if (netdev_uc_count(netdev) > addn_macs) {
932 xgbe_set_promiscuous_mode(pdata, 1);
933 } else {
934 netdev_for_each_uc_addr(ha, netdev) {
935 xgbe_set_mac_reg(pdata, ha, &mac_reg);
936 addn_macs--;
937 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500938
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500939 if (netdev_mc_count(netdev) > addn_macs) {
940 xgbe_set_all_multicast_mode(pdata, 1);
941 } else {
942 netdev_for_each_mc_addr(ha, netdev) {
943 xgbe_set_mac_reg(pdata, ha, &mac_reg);
944 addn_macs--;
945 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500946 }
947 }
948
949 /* Clear remaining additional MAC address entries */
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500950 while (addn_macs--)
951 xgbe_set_mac_reg(pdata, NULL, &mac_reg);
952}
953
954static void xgbe_set_mac_hash_table(struct xgbe_prv_data *pdata)
955{
956 struct net_device *netdev = pdata->netdev;
957 struct netdev_hw_addr *ha;
958 unsigned int hash_reg;
959 unsigned int hash_table_shift, hash_table_count;
960 u32 hash_table[XGBE_MAC_HASH_TABLE_SIZE];
961 u32 crc;
962 unsigned int i;
963
964 hash_table_shift = 26 - (pdata->hw_feat.hash_table_size >> 7);
965 hash_table_count = pdata->hw_feat.hash_table_size / 32;
966 memset(hash_table, 0, sizeof(hash_table));
967
968 /* Build the MAC Hash Table register values */
969 netdev_for_each_uc_addr(ha, netdev) {
970 crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
971 crc >>= hash_table_shift;
972 hash_table[crc >> 5] |= (1 << (crc & 0x1f));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500973 }
974
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -0500975 netdev_for_each_mc_addr(ha, netdev) {
976 crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
977 crc >>= hash_table_shift;
978 hash_table[crc >> 5] |= (1 << (crc & 0x1f));
979 }
980
981 /* Set the MAC Hash Table registers */
982 hash_reg = MAC_HTR0;
983 for (i = 0; i < hash_table_count; i++) {
984 XGMAC_IOWRITE(pdata, hash_reg, hash_table[i]);
985 hash_reg += MAC_HTR_INC;
986 }
987}
988
989static int xgbe_add_mac_addresses(struct xgbe_prv_data *pdata)
990{
991 if (pdata->hw_feat.hash_table_size)
992 xgbe_set_mac_hash_table(pdata);
993 else
994 xgbe_set_mac_addn_addrs(pdata);
995
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -0500996 return 0;
997}
998
999static int xgbe_set_mac_address(struct xgbe_prv_data *pdata, u8 *addr)
1000{
1001 unsigned int mac_addr_hi, mac_addr_lo;
1002
1003 mac_addr_hi = (addr[5] << 8) | (addr[4] << 0);
1004 mac_addr_lo = (addr[3] << 24) | (addr[2] << 16) |
1005 (addr[1] << 8) | (addr[0] << 0);
1006
1007 XGMAC_IOWRITE(pdata, MAC_MACA0HR, mac_addr_hi);
1008 XGMAC_IOWRITE(pdata, MAC_MACA0LR, mac_addr_lo);
1009
1010 return 0;
1011}
1012
Lendacky, Thomasb8763822015-04-09 12:11:57 -05001013static int xgbe_config_rx_mode(struct xgbe_prv_data *pdata)
1014{
1015 struct net_device *netdev = pdata->netdev;
1016 unsigned int pr_mode, am_mode;
1017
1018 pr_mode = ((netdev->flags & IFF_PROMISC) != 0);
1019 am_mode = ((netdev->flags & IFF_ALLMULTI) != 0);
1020
1021 xgbe_set_promiscuous_mode(pdata, pr_mode);
1022 xgbe_set_all_multicast_mode(pdata, am_mode);
1023
1024 xgbe_add_mac_addresses(pdata);
1025
1026 return 0;
1027}
1028
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001029static int xgbe_read_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
1030 int mmd_reg)
1031{
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001032 unsigned long flags;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001033 unsigned int mmd_address;
1034 int mmd_data;
1035
1036 if (mmd_reg & MII_ADDR_C45)
1037 mmd_address = mmd_reg & ~MII_ADDR_C45;
1038 else
1039 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1040
1041 /* The PCS registers are accessed using mmio. The underlying APB3
1042 * management interface uses indirect addressing to access the MMD
1043 * register sets. This requires accessing of the PCS register in two
1044 * phases, an address phase and a data phase.
1045 *
1046 * The mmio interface is based on 32-bit offsets and values. All
1047 * register offsets must therefore be adjusted by left shifting the
1048 * offset 2 bits and reading 32 bits of data.
1049 */
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001050 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001051 XPCS_IOWRITE(pdata, PCS_MMD_SELECT << 2, mmd_address >> 8);
1052 mmd_data = XPCS_IOREAD(pdata, (mmd_address & 0xff) << 2);
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001053 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001054
1055 return mmd_data;
1056}
1057
1058static void xgbe_write_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
1059 int mmd_reg, int mmd_data)
1060{
1061 unsigned int mmd_address;
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001062 unsigned long flags;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001063
1064 if (mmd_reg & MII_ADDR_C45)
1065 mmd_address = mmd_reg & ~MII_ADDR_C45;
1066 else
1067 mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1068
1069 /* The PCS registers are accessed using mmio. The underlying APB3
1070 * management interface uses indirect addressing to access the MMD
1071 * register sets. This requires accessing of the PCS register in two
1072 * phases, an address phase and a data phase.
1073 *
1074 * The mmio interface is based on 32-bit offsets and values. All
1075 * register offsets must therefore be adjusted by left shifting the
1076 * offset 2 bits and reading 32 bits of data.
1077 */
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001078 spin_lock_irqsave(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001079 XPCS_IOWRITE(pdata, PCS_MMD_SELECT << 2, mmd_address >> 8);
1080 XPCS_IOWRITE(pdata, (mmd_address & 0xff) << 2, mmd_data);
Lendacky, Thomasced3fca2016-02-17 11:49:28 -06001081 spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001082}
1083
1084static int xgbe_tx_complete(struct xgbe_ring_desc *rdesc)
1085{
1086 return !XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN);
1087}
1088
1089static int xgbe_disable_rx_csum(struct xgbe_prv_data *pdata)
1090{
1091 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 0);
1092
1093 return 0;
1094}
1095
1096static int xgbe_enable_rx_csum(struct xgbe_prv_data *pdata)
1097{
1098 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 1);
1099
1100 return 0;
1101}
1102
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001103static void xgbe_tx_desc_reset(struct xgbe_ring_data *rdata)
1104{
1105 struct xgbe_ring_desc *rdesc = rdata->rdesc;
1106
1107 /* Reset the Tx descriptor
1108 * Set buffer 1 (lo) address to zero
1109 * Set buffer 1 (hi) address to zero
1110 * Reset all other control bits (IC, TTSE, B2L & B1L)
1111 * Reset all other control bits (OWN, CTXT, FD, LD, CPC, CIC, etc)
1112 */
1113 rdesc->desc0 = 0;
1114 rdesc->desc1 = 0;
1115 rdesc->desc2 = 0;
1116 rdesc->desc3 = 0;
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001117
1118 /* Make sure ownership is written to the descriptor */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001119 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001120}
1121
1122static void xgbe_tx_desc_init(struct xgbe_channel *channel)
1123{
1124 struct xgbe_ring *ring = channel->tx_ring;
1125 struct xgbe_ring_data *rdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001126 int i;
1127 int start_index = ring->cur;
1128
1129 DBGPR("-->tx_desc_init\n");
1130
1131 /* Initialze all descriptors */
1132 for (i = 0; i < ring->rdesc_count; i++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001133 rdata = XGBE_GET_DESC_DATA(ring, i);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001134
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001135 /* Initialize Tx descriptor */
1136 xgbe_tx_desc_reset(rdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001137 }
1138
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001139 /* Update the total number of Tx descriptors */
1140 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDRLR, ring->rdesc_count - 1);
1141
1142 /* Update the starting address of descriptor ring */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001143 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001144 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_HI,
1145 upper_32_bits(rdata->rdesc_dma));
1146 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_LO,
1147 lower_32_bits(rdata->rdesc_dma));
1148
1149 DBGPR("<--tx_desc_init\n");
1150}
1151
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001152static void xgbe_rx_desc_reset(struct xgbe_prv_data *pdata,
1153 struct xgbe_ring_data *rdata, unsigned int index)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001154{
1155 struct xgbe_ring_desc *rdesc = rdata->rdesc;
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001156 unsigned int rx_usecs = pdata->rx_usecs;
1157 unsigned int rx_frames = pdata->rx_frames;
1158 unsigned int inte;
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001159 dma_addr_t hdr_dma, buf_dma;
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001160
1161 if (!rx_usecs && !rx_frames) {
1162 /* No coalescing, interrupt for every descriptor */
1163 inte = 1;
1164 } else {
1165 /* Set interrupt based on Rx frame coalescing setting */
1166 if (rx_frames && !((index + 1) % rx_frames))
1167 inte = 1;
1168 else
1169 inte = 0;
1170 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001171
1172 /* Reset the Rx descriptor
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001173 * Set buffer 1 (lo) address to header dma address (lo)
1174 * Set buffer 1 (hi) address to header dma address (hi)
1175 * Set buffer 2 (lo) address to buffer dma address (lo)
1176 * Set buffer 2 (hi) address to buffer dma address (hi) and
1177 * set control bits OWN and INTE
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001178 */
Lendacky, Thomascfbfd862015-07-06 11:57:37 -05001179 hdr_dma = rdata->rx.hdr.dma_base + rdata->rx.hdr.dma_off;
1180 buf_dma = rdata->rx.buf.dma_base + rdata->rx.buf.dma_off;
1181 rdesc->desc0 = cpu_to_le32(lower_32_bits(hdr_dma));
1182 rdesc->desc1 = cpu_to_le32(upper_32_bits(hdr_dma));
1183 rdesc->desc2 = cpu_to_le32(lower_32_bits(buf_dma));
1184 rdesc->desc3 = cpu_to_le32(upper_32_bits(buf_dma));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001185
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001186 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, INTE, inte);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001187
1188 /* Since the Rx DMA engine is likely running, make sure everything
1189 * is written to the descriptor(s) before setting the OWN bit
1190 * for the descriptor
1191 */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001192 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001193
1194 XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN, 1);
1195
1196 /* Make sure ownership is written to the descriptor */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001197 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001198}
1199
1200static void xgbe_rx_desc_init(struct xgbe_channel *channel)
1201{
1202 struct xgbe_prv_data *pdata = channel->pdata;
1203 struct xgbe_ring *ring = channel->rx_ring;
1204 struct xgbe_ring_data *rdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001205 unsigned int start_index = ring->cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001206 unsigned int i;
1207
1208 DBGPR("-->rx_desc_init\n");
1209
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001210 /* Initialize all descriptors */
1211 for (i = 0; i < ring->rdesc_count; i++) {
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001212 rdata = XGBE_GET_DESC_DATA(ring, i);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001213
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001214 /* Initialize Rx descriptor */
Lendacky, Thomas8dee19e2015-04-09 12:11:51 -05001215 xgbe_rx_desc_reset(pdata, rdata, i);
Lendacky, Thomas08dcc472014-11-04 16:06:44 -06001216 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001217
1218 /* Update the total number of Rx descriptors */
1219 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDRLR, ring->rdesc_count - 1);
1220
1221 /* Update the starting address of descriptor ring */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001222 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001223 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_HI,
1224 upper_32_bits(rdata->rdesc_dma));
1225 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_LO,
1226 lower_32_bits(rdata->rdesc_dma));
1227
1228 /* Update the Rx Descriptor Tail Pointer */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001229 rdata = XGBE_GET_DESC_DATA(ring, start_index + ring->rdesc_count - 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001230 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
1231 lower_32_bits(rdata->rdesc_dma));
1232
1233 DBGPR("<--rx_desc_init\n");
1234}
1235
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001236static void xgbe_update_tstamp_addend(struct xgbe_prv_data *pdata,
1237 unsigned int addend)
1238{
1239 /* Set the addend register value and tell the device */
1240 XGMAC_IOWRITE(pdata, MAC_TSAR, addend);
1241 XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSADDREG, 1);
1242
1243 /* Wait for addend update to complete */
1244 while (XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSADDREG))
1245 udelay(5);
1246}
1247
1248static void xgbe_set_tstamp_time(struct xgbe_prv_data *pdata, unsigned int sec,
1249 unsigned int nsec)
1250{
1251 /* Set the time values and tell the device */
1252 XGMAC_IOWRITE(pdata, MAC_STSUR, sec);
1253 XGMAC_IOWRITE(pdata, MAC_STNUR, nsec);
1254 XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSINIT, 1);
1255
1256 /* Wait for time update to complete */
1257 while (XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSINIT))
1258 udelay(5);
1259}
1260
1261static u64 xgbe_get_tstamp_time(struct xgbe_prv_data *pdata)
1262{
1263 u64 nsec;
1264
1265 nsec = XGMAC_IOREAD(pdata, MAC_STSR);
1266 nsec *= NSEC_PER_SEC;
1267 nsec += XGMAC_IOREAD(pdata, MAC_STNR);
1268
1269 return nsec;
1270}
1271
1272static u64 xgbe_get_tx_tstamp(struct xgbe_prv_data *pdata)
1273{
1274 unsigned int tx_snr;
1275 u64 nsec;
1276
1277 tx_snr = XGMAC_IOREAD(pdata, MAC_TXSNR);
1278 if (XGMAC_GET_BITS(tx_snr, MAC_TXSNR, TXTSSTSMIS))
1279 return 0;
1280
1281 nsec = XGMAC_IOREAD(pdata, MAC_TXSSR);
1282 nsec *= NSEC_PER_SEC;
1283 nsec += tx_snr;
1284
1285 return nsec;
1286}
1287
1288static void xgbe_get_rx_tstamp(struct xgbe_packet_data *packet,
1289 struct xgbe_ring_desc *rdesc)
1290{
1291 u64 nsec;
1292
1293 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSA) &&
1294 !XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSD)) {
1295 nsec = le32_to_cpu(rdesc->desc1);
1296 nsec <<= 32;
1297 nsec |= le32_to_cpu(rdesc->desc0);
1298 if (nsec != 0xffffffffffffffffULL) {
1299 packet->rx_tstamp = nsec;
1300 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1301 RX_TSTAMP, 1);
1302 }
1303 }
1304}
1305
1306static int xgbe_config_tstamp(struct xgbe_prv_data *pdata,
1307 unsigned int mac_tscr)
1308{
1309 /* Set one nano-second accuracy */
1310 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCTRLSSR, 1);
1311
1312 /* Set fine timestamp update */
1313 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCFUPDT, 1);
1314
1315 /* Overwrite earlier timestamps */
1316 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TXTSSTSM, 1);
1317
1318 XGMAC_IOWRITE(pdata, MAC_TSCR, mac_tscr);
1319
1320 /* Exit if timestamping is not enabled */
1321 if (!XGMAC_GET_BITS(mac_tscr, MAC_TSCR, TSENA))
1322 return 0;
1323
1324 /* Initialize time registers */
1325 XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SSINC, XGBE_TSTAMP_SSINC);
1326 XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SNSINC, XGBE_TSTAMP_SNSINC);
1327 xgbe_update_tstamp_addend(pdata, pdata->tstamp_addend);
1328 xgbe_set_tstamp_time(pdata, 0, 0);
1329
1330 /* Initialize the timecounter */
1331 timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc,
1332 ktime_to_ns(ktime_get_real()));
1333
1334 return 0;
1335}
1336
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001337static void xgbe_tx_start_xmit(struct xgbe_channel *channel,
1338 struct xgbe_ring *ring)
1339{
1340 struct xgbe_prv_data *pdata = channel->pdata;
1341 struct xgbe_ring_data *rdata;
1342
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001343 /* Make sure everything is written before the register write */
1344 wmb();
1345
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001346 /* Issue a poll command to Tx DMA by writing address
1347 * of next immediate free descriptor */
1348 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1349 XGMAC_DMA_IOWRITE(channel, DMA_CH_TDTR_LO,
1350 lower_32_bits(rdata->rdesc_dma));
1351
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -05001352 /* Start the Tx timer */
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001353 if (pdata->tx_usecs && !channel->tx_timer_active) {
1354 channel->tx_timer_active = 1;
Lendacky, Thomasc635eaa2015-03-20 11:50:28 -05001355 mod_timer(&channel->tx_timer,
1356 jiffies + usecs_to_jiffies(pdata->tx_usecs));
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001357 }
1358
1359 ring->tx.xmit_more = 0;
1360}
1361
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001362static void xgbe_dev_xmit(struct xgbe_channel *channel)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001363{
1364 struct xgbe_prv_data *pdata = channel->pdata;
1365 struct xgbe_ring *ring = channel->tx_ring;
1366 struct xgbe_ring_data *rdata;
1367 struct xgbe_ring_desc *rdesc;
1368 struct xgbe_packet_data *packet = &ring->packet_data;
1369 unsigned int csum, tso, vlan;
1370 unsigned int tso_context, vlan_context;
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001371 unsigned int tx_set_ic;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001372 int start_index = ring->cur;
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001373 int cur_index = ring->cur;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001374 int i;
1375
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001376 DBGPR("-->xgbe_dev_xmit\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001377
1378 csum = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1379 CSUM_ENABLE);
1380 tso = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1381 TSO_ENABLE);
1382 vlan = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1383 VLAN_CTAG);
1384
1385 if (tso && (packet->mss != ring->tx.cur_mss))
1386 tso_context = 1;
1387 else
1388 tso_context = 0;
1389
1390 if (vlan && (packet->vlan_ctag != ring->tx.cur_vlan_ctag))
1391 vlan_context = 1;
1392 else
1393 vlan_context = 0;
1394
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001395 /* Determine if an interrupt should be generated for this Tx:
1396 * Interrupt:
1397 * - Tx frame count exceeds the frame count setting
1398 * - Addition of Tx frame count to the frame count since the
1399 * last interrupt was set exceeds the frame count setting
1400 * No interrupt:
1401 * - No frame count setting specified (ethtool -C ethX tx-frames 0)
1402 * - Addition of Tx frame count to the frame count since the
1403 * last interrupt was set does not exceed the frame count setting
1404 */
1405 ring->coalesce_count += packet->tx_packets;
1406 if (!pdata->tx_frames)
1407 tx_set_ic = 0;
1408 else if (packet->tx_packets > pdata->tx_frames)
1409 tx_set_ic = 1;
1410 else if ((ring->coalesce_count % pdata->tx_frames) <
1411 packet->tx_packets)
1412 tx_set_ic = 1;
1413 else
1414 tx_set_ic = 0;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001415
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001416 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001417 rdesc = rdata->rdesc;
1418
1419 /* Create a context descriptor if this is a TSO packet */
1420 if (tso_context || vlan_context) {
1421 if (tso_context) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001422 netif_dbg(pdata, tx_queued, pdata->netdev,
1423 "TSO context descriptor, mss=%u\n",
1424 packet->mss);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001425
1426 /* Set the MSS size */
1427 XGMAC_SET_BITS_LE(rdesc->desc2, TX_CONTEXT_DESC2,
1428 MSS, packet->mss);
1429
1430 /* Mark it as a CONTEXT descriptor */
1431 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1432 CTXT, 1);
1433
1434 /* Indicate this descriptor contains the MSS */
1435 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1436 TCMSSV, 1);
1437
1438 ring->tx.cur_mss = packet->mss;
1439 }
1440
1441 if (vlan_context) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001442 netif_dbg(pdata, tx_queued, pdata->netdev,
1443 "VLAN context descriptor, ctag=%u\n",
1444 packet->vlan_ctag);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001445
1446 /* Mark it as a CONTEXT descriptor */
1447 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1448 CTXT, 1);
1449
1450 /* Set the VLAN tag */
1451 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1452 VT, packet->vlan_ctag);
1453
1454 /* Indicate this descriptor contains the VLAN tag */
1455 XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1456 VLTV, 1);
1457
1458 ring->tx.cur_vlan_ctag = packet->vlan_ctag;
1459 }
1460
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001461 cur_index++;
1462 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001463 rdesc = rdata->rdesc;
1464 }
1465
1466 /* Update buffer address (for TSO this is the header) */
1467 rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
1468 rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
1469
1470 /* Update the buffer length */
1471 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1472 rdata->skb_dma_len);
1473
1474 /* VLAN tag insertion check */
1475 if (vlan)
1476 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, VTIR,
1477 TX_NORMAL_DESC2_VLAN_INSERT);
1478
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001479 /* Timestamp enablement check */
1480 if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
1481 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, TTSE, 1);
1482
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001483 /* Mark it as First Descriptor */
1484 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FD, 1);
1485
1486 /* Mark it as a NORMAL descriptor */
1487 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1488
1489 /* Set OWN bit if not the first descriptor */
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001490 if (cur_index != start_index)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001491 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1492
1493 if (tso) {
1494 /* Enable TSO */
1495 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TSE, 1);
1496 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPPL,
1497 packet->tcp_payload_len);
1498 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPHDRLEN,
1499 packet->tcp_header_len / 4);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001500
1501 pdata->ext_stats.tx_tso_packets++;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001502 } else {
1503 /* Enable CRC and Pad Insertion */
1504 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CPC, 0);
1505
1506 /* Enable HW CSUM */
1507 if (csum)
1508 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1509 CIC, 0x3);
1510
1511 /* Set the total length to be transmitted */
1512 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FL,
1513 packet->length);
1514 }
1515
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001516 for (i = cur_index - start_index + 1; i < packet->rdesc_count; i++) {
1517 cur_index++;
1518 rdata = XGBE_GET_DESC_DATA(ring, cur_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001519 rdesc = rdata->rdesc;
1520
1521 /* Update buffer address */
1522 rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
1523 rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
1524
1525 /* Update the buffer length */
1526 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1527 rdata->skb_dma_len);
1528
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001529 /* Set OWN bit */
1530 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1531
1532 /* Mark it as NORMAL descriptor */
1533 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1534
1535 /* Enable HW CSUM */
1536 if (csum)
1537 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1538 CIC, 0x3);
1539 }
1540
1541 /* Set LAST bit for the last descriptor */
1542 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD, 1);
1543
Lendacky, Thomaseb79e642014-11-20 11:04:02 -06001544 /* Set IC bit based on Tx coalescing settings */
1545 if (tx_set_ic)
1546 XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, IC, 1);
1547
Lendacky, Thomas5fb4b862014-11-20 11:03:50 -06001548 /* Save the Tx info to report back during cleanup */
1549 rdata->tx.packets = packet->tx_packets;
1550 rdata->tx.bytes = packet->tx_bytes;
1551
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001552 /* In case the Tx DMA engine is running, make sure everything
1553 * is written to the descriptor(s) before setting the OWN bit
1554 * for the first descriptor
1555 */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001556 dma_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001557
1558 /* Set OWN bit for the first descriptor */
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001559 rdata = XGBE_GET_DESC_DATA(ring, start_index);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001560 rdesc = rdata->rdesc;
1561 XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1562
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001563 if (netif_msg_tx_queued(pdata))
1564 xgbe_dump_tx_desc(pdata, ring, start_index,
1565 packet->rdesc_count, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001566
1567 /* Make sure ownership is written to the descriptor */
Lendacky, Thomas20986ed2015-10-26 17:13:54 -05001568 smp_wmb();
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001569
Lendacky, Thomasa83ef422015-01-16 12:46:55 -06001570 ring->cur = cur_index + 1;
Lendacky, Thomas16958a22014-11-20 11:04:08 -06001571 if (!packet->skb->xmit_more ||
1572 netif_xmit_stopped(netdev_get_tx_queue(pdata->netdev,
1573 channel->queue_index)))
1574 xgbe_tx_start_xmit(channel, ring);
1575 else
1576 ring->tx.xmit_more = 1;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001577
1578 DBGPR(" %s: descriptors %u to %u written\n",
1579 channel->name, start_index & (ring->rdesc_count - 1),
1580 (ring->cur - 1) & (ring->rdesc_count - 1));
1581
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06001582 DBGPR("<--xgbe_dev_xmit\n");
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001583}
1584
1585static int xgbe_dev_read(struct xgbe_channel *channel)
1586{
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001587 struct xgbe_prv_data *pdata = channel->pdata;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001588 struct xgbe_ring *ring = channel->rx_ring;
1589 struct xgbe_ring_data *rdata;
1590 struct xgbe_ring_desc *rdesc;
1591 struct xgbe_packet_data *packet = &ring->packet_data;
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001592 struct net_device *netdev = pdata->netdev;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001593 unsigned int err, etlt, l34t;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001594
1595 DBGPR("-->xgbe_dev_read: cur = %d\n", ring->cur);
1596
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001597 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001598 rdesc = rdata->rdesc;
1599
1600 /* Check for data availability */
1601 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN))
1602 return 1;
1603
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001604 /* Make sure descriptor fields are read after reading the OWN bit */
Lendacky, Thomasceb8f6b2015-03-20 11:50:16 -05001605 dma_rmb();
Lendacky, Thomas5449e272014-11-20 11:03:26 -06001606
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001607 if (netif_msg_rx_status(pdata))
1608 xgbe_dump_rx_desc(pdata, ring, ring->cur);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001609
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05001610 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CTXT)) {
1611 /* Timestamp Context Descriptor */
1612 xgbe_get_rx_tstamp(packet, rdesc);
1613
1614 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1615 CONTEXT, 1);
1616 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1617 CONTEXT_NEXT, 0);
1618 return 0;
1619 }
1620
1621 /* Normal Descriptor, be sure Context Descriptor bit is off */
1622 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, CONTEXT, 0);
1623
1624 /* Indicate if a Context Descriptor is next */
1625 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CDA))
1626 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1627 CONTEXT_NEXT, 1);
1628
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001629 /* Get the header length */
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001630 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, FD)) {
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001631 rdata->rx.hdr_len = XGMAC_GET_BITS_LE(rdesc->desc2,
1632 RX_NORMAL_DESC2, HL);
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001633 if (rdata->rx.hdr_len)
1634 pdata->ext_stats.rx_split_header_packets++;
1635 }
Lendacky, Thomas174fd252014-11-04 16:06:50 -06001636
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001637 /* Get the RSS hash */
1638 if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, RSV)) {
1639 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1640 RSS_HASH, 1);
1641
1642 packet->rss_hash = le32_to_cpu(rdesc->desc1);
1643
1644 l34t = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, L34T);
1645 switch (l34t) {
1646 case RX_DESC3_L34T_IPV4_TCP:
1647 case RX_DESC3_L34T_IPV4_UDP:
1648 case RX_DESC3_L34T_IPV6_TCP:
1649 case RX_DESC3_L34T_IPV6_UDP:
1650 packet->rss_hash_type = PKT_HASH_TYPE_L4;
Dan Carpenterb6267d32014-11-13 09:19:06 +03001651 break;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06001652 default:
1653 packet->rss_hash_type = PKT_HASH_TYPE_L3;
1654 }
1655 }
1656
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001657 /* Get the packet length */
Lendacky, Thomasc9f140e2014-11-20 11:03:44 -06001658 rdata->rx.len = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, PL);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001659
1660 if (!XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, LD)) {
1661 /* Not all the data has been transferred for this packet */
1662 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1663 INCOMPLETE, 1);
1664 return 0;
1665 }
1666
1667 /* This is the last of the data for this packet */
1668 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1669 INCOMPLETE, 0);
1670
1671 /* Set checksum done indicator as appropriate */
Lendacky, Thomas5452b2d2015-05-14 11:43:57 -05001672 if (netdev->features & NETIF_F_RXCSUM)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001673 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1674 CSUM_DONE, 1);
1675
1676 /* Check for errors (only valid in last descriptor) */
1677 err = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ES);
1678 etlt = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ETLT);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001679 netif_dbg(pdata, rx_status, netdev, "err=%u, etlt=%#x\n", err, etlt);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001680
Lendacky, Thomas7bba35b2014-11-20 11:03:38 -06001681 if (!err || !etlt) {
1682 /* No error if err is 0 or etlt is 0 */
Lendacky, Thomasc52e9c62014-06-24 16:19:18 -05001683 if ((etlt == 0x09) &&
1684 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001685 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1686 VLAN_CTAG, 1);
1687 packet->vlan_ctag = XGMAC_GET_BITS_LE(rdesc->desc0,
1688 RX_NORMAL_DESC0,
1689 OVT);
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05001690 netif_dbg(pdata, rx_status, netdev, "vlan-ctag=%#06x\n",
1691 packet->vlan_ctag);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001692 }
1693 } else {
1694 if ((etlt == 0x05) || (etlt == 0x06))
1695 XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
1696 CSUM_DONE, 0);
1697 else
1698 XGMAC_SET_BITS(packet->errors, RX_PACKET_ERRORS,
1699 FRAME, 1);
1700 }
1701
1702 DBGPR("<--xgbe_dev_read: %s - descriptor=%u (cur=%d)\n", channel->name,
1703 ring->cur & (ring->rdesc_count - 1), ring->cur);
1704
1705 return 0;
1706}
1707
1708static int xgbe_is_context_desc(struct xgbe_ring_desc *rdesc)
1709{
1710 /* Rx and Tx share CTXT bit, so check TDES3.CTXT bit */
1711 return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT);
1712}
1713
1714static int xgbe_is_last_desc(struct xgbe_ring_desc *rdesc)
1715{
1716 /* Rx and Tx share LD bit, so check TDES3.LD bit */
1717 return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD);
1718}
1719
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001720static int xgbe_enable_int(struct xgbe_channel *channel,
1721 enum xgbe_int int_id)
1722{
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001723 unsigned int dma_ch_ier;
1724
1725 dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
1726
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001727 switch (int_id) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001728 case XGMAC_INT_DMA_CH_SR_TI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001729 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001730 break;
1731 case XGMAC_INT_DMA_CH_SR_TPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001732 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001733 break;
1734 case XGMAC_INT_DMA_CH_SR_TBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001735 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001736 break;
1737 case XGMAC_INT_DMA_CH_SR_RI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001738 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001739 break;
1740 case XGMAC_INT_DMA_CH_SR_RBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001741 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001742 break;
1743 case XGMAC_INT_DMA_CH_SR_RPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001744 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 1);
1745 break;
1746 case XGMAC_INT_DMA_CH_SR_TI_RI:
1747 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 1);
1748 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001749 break;
1750 case XGMAC_INT_DMA_CH_SR_FBE:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001751 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001752 break;
1753 case XGMAC_INT_DMA_ALL:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001754 dma_ch_ier |= channel->saved_ier;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001755 break;
1756 default:
1757 return -1;
1758 }
1759
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001760 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
1761
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001762 return 0;
1763}
1764
1765static int xgbe_disable_int(struct xgbe_channel *channel,
1766 enum xgbe_int int_id)
1767{
1768 unsigned int dma_ch_ier;
1769
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001770 dma_ch_ier = XGMAC_DMA_IOREAD(channel, DMA_CH_IER);
1771
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001772 switch (int_id) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001773 case XGMAC_INT_DMA_CH_SR_TI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001774 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001775 break;
1776 case XGMAC_INT_DMA_CH_SR_TPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001777 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TXSE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001778 break;
1779 case XGMAC_INT_DMA_CH_SR_TBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001780 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TBUE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001781 break;
1782 case XGMAC_INT_DMA_CH_SR_RI:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001783 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001784 break;
1785 case XGMAC_INT_DMA_CH_SR_RBU:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001786 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RBUE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001787 break;
1788 case XGMAC_INT_DMA_CH_SR_RPS:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001789 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RSE, 0);
1790 break;
1791 case XGMAC_INT_DMA_CH_SR_TI_RI:
1792 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, TIE, 0);
1793 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, RIE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001794 break;
1795 case XGMAC_INT_DMA_CH_SR_FBE:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001796 XGMAC_SET_BITS(dma_ch_ier, DMA_CH_IER, FBEE, 0);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001797 break;
1798 case XGMAC_INT_DMA_ALL:
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001799 channel->saved_ier = dma_ch_ier & XGBE_DMA_INTERRUPT_MASK;
Lendacky, Thomasd0a8ba62014-06-24 16:19:06 -05001800 dma_ch_ier &= ~XGBE_DMA_INTERRUPT_MASK;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001801 break;
1802 default:
1803 return -1;
1804 }
1805
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001806 XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, dma_ch_ier);
1807
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001808 return 0;
1809}
1810
1811static int xgbe_exit(struct xgbe_prv_data *pdata)
1812{
1813 unsigned int count = 2000;
1814
1815 DBGPR("-->xgbe_exit\n");
1816
1817 /* Issue a software reset */
1818 XGMAC_IOWRITE_BITS(pdata, DMA_MR, SWR, 1);
1819 usleep_range(10, 15);
1820
1821 /* Poll Until Poll Condition */
Dan Carpenterc7557e62015-12-15 13:12:29 +03001822 while (--count && XGMAC_IOREAD_BITS(pdata, DMA_MR, SWR))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001823 usleep_range(500, 600);
1824
1825 if (!count)
1826 return -EBUSY;
1827
1828 DBGPR("<--xgbe_exit\n");
1829
1830 return 0;
1831}
1832
1833static int xgbe_flush_tx_queues(struct xgbe_prv_data *pdata)
1834{
1835 unsigned int i, count;
1836
Lendacky, Thomasa9a4a2d2014-08-29 13:16:50 -05001837 if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) < 0x21)
1838 return 0;
1839
Lendacky, Thomas853eb162014-07-29 08:57:31 -05001840 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001841 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, FTQ, 1);
1842
1843 /* Poll Until Poll Condition */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05001844 for (i = 0; i < pdata->tx_q_count; i++) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001845 count = 2000;
Dan Carpenterc7557e62015-12-15 13:12:29 +03001846 while (--count && XGMAC_MTL_IOREAD_BITS(pdata, i,
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001847 MTL_Q_TQOMR, FTQ))
1848 usleep_range(500, 600);
1849
1850 if (!count)
1851 return -EBUSY;
1852 }
1853
1854 return 0;
1855}
1856
1857static void xgbe_config_dma_bus(struct xgbe_prv_data *pdata)
1858{
1859 /* Set enhanced addressing mode */
1860 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, EAME, 1);
1861
1862 /* Set the System Bus mode */
1863 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, UNDEF, 1);
Lendacky, Thomas9867e8f2014-07-02 13:04:46 -05001864 XGMAC_IOWRITE_BITS(pdata, DMA_SBMR, BLEN_256, 1);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001865}
1866
1867static void xgbe_config_dma_cache(struct xgbe_prv_data *pdata)
1868{
1869 unsigned int arcache, awcache;
1870
1871 arcache = 0;
Lendacky, Thomascfa50c72014-07-02 13:04:57 -05001872 XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRC, pdata->arcache);
1873 XGMAC_SET_BITS(arcache, DMA_AXIARCR, DRD, pdata->axdomain);
1874 XGMAC_SET_BITS(arcache, DMA_AXIARCR, TEC, pdata->arcache);
1875 XGMAC_SET_BITS(arcache, DMA_AXIARCR, TED, pdata->axdomain);
1876 XGMAC_SET_BITS(arcache, DMA_AXIARCR, THC, pdata->arcache);
1877 XGMAC_SET_BITS(arcache, DMA_AXIARCR, THD, pdata->axdomain);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001878 XGMAC_IOWRITE(pdata, DMA_AXIARCR, arcache);
1879
1880 awcache = 0;
Lendacky, Thomascfa50c72014-07-02 13:04:57 -05001881 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWC, pdata->awcache);
1882 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, DWD, pdata->axdomain);
1883 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPC, pdata->awcache);
1884 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RPD, pdata->axdomain);
1885 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHC, pdata->awcache);
1886 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, RHD, pdata->axdomain);
1887 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDC, pdata->awcache);
1888 XGMAC_SET_BITS(awcache, DMA_AXIAWCR, TDD, pdata->axdomain);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001889 XGMAC_IOWRITE(pdata, DMA_AXIAWCR, awcache);
1890}
1891
1892static void xgbe_config_mtl_mode(struct xgbe_prv_data *pdata)
1893{
1894 unsigned int i;
1895
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001896 /* Set Tx to weighted round robin scheduling algorithm */
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001897 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_WRR);
1898
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05001899 /* Set Tx traffic classes to use WRR algorithm with equal weights */
1900 for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
1901 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
1902 MTL_TSA_ETS);
1903 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW, 1);
1904 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05001905
1906 /* Set Rx to strict priority algorithm */
1907 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, RAA, MTL_RAA_SP);
1908}
1909
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05001910static void xgbe_queue_flow_control_threshold(struct xgbe_prv_data *pdata,
1911 unsigned int queue,
1912 unsigned int q_fifo_size)
1913{
1914 unsigned int frame_fifo_size;
1915 unsigned int rfa, rfd;
1916
1917 frame_fifo_size = XGMAC_FLOW_CONTROL_ALIGN(xgbe_get_max_frame(pdata));
1918
1919 if (pdata->pfcq[queue] && (q_fifo_size > pdata->pfc_rfa)) {
1920 /* PFC is active for this queue */
1921 rfa = pdata->pfc_rfa;
1922 rfd = rfa + frame_fifo_size;
1923 if (rfd > XGMAC_FLOW_CONTROL_MAX)
1924 rfd = XGMAC_FLOW_CONTROL_MAX;
1925 if (rfa >= XGMAC_FLOW_CONTROL_MAX)
1926 rfa = XGMAC_FLOW_CONTROL_MAX - XGMAC_FLOW_CONTROL_UNIT;
1927 } else {
1928 /* This path deals with just maximum frame sizes which are
1929 * limited to a jumbo frame of 9,000 (plus headers, etc.)
1930 * so we can never exceed the maximum allowable RFA/RFD
1931 * values.
1932 */
1933 if (q_fifo_size <= 2048) {
1934 /* rx_rfd to zero to signal no flow control */
1935 pdata->rx_rfa[queue] = 0;
1936 pdata->rx_rfd[queue] = 0;
1937 return;
1938 }
1939
1940 if (q_fifo_size <= 4096) {
1941 /* Between 2048 and 4096 */
1942 pdata->rx_rfa[queue] = 0; /* Full - 1024 bytes */
1943 pdata->rx_rfd[queue] = 1; /* Full - 1536 bytes */
1944 return;
1945 }
1946
1947 if (q_fifo_size <= frame_fifo_size) {
1948 /* Between 4096 and max-frame */
1949 pdata->rx_rfa[queue] = 2; /* Full - 2048 bytes */
1950 pdata->rx_rfd[queue] = 5; /* Full - 3584 bytes */
1951 return;
1952 }
1953
1954 if (q_fifo_size <= (frame_fifo_size * 3)) {
1955 /* Between max-frame and 3 max-frames,
1956 * trigger if we get just over a frame of data and
1957 * resume when we have just under half a frame left.
1958 */
1959 rfa = q_fifo_size - frame_fifo_size;
1960 rfd = rfa + (frame_fifo_size / 2);
1961 } else {
1962 /* Above 3 max-frames - trigger when just over
1963 * 2 frames of space available
1964 */
1965 rfa = frame_fifo_size * 2;
1966 rfa += XGMAC_FLOW_CONTROL_UNIT;
1967 rfd = rfa + frame_fifo_size;
1968 }
1969 }
1970
1971 pdata->rx_rfa[queue] = XGMAC_FLOW_CONTROL_VALUE(rfa);
1972 pdata->rx_rfd[queue] = XGMAC_FLOW_CONTROL_VALUE(rfd);
1973}
1974
1975static void xgbe_calculate_flow_control_threshold(struct xgbe_prv_data *pdata,
1976 unsigned int *fifo)
1977{
1978 unsigned int q_fifo_size;
1979 unsigned int i;
1980
1981 for (i = 0; i < pdata->rx_q_count; i++) {
1982 q_fifo_size = (fifo[i] + 1) * XGMAC_FIFO_UNIT;
1983
1984 xgbe_queue_flow_control_threshold(pdata, i, q_fifo_size);
1985 }
1986}
1987
1988static void xgbe_config_flow_control_threshold(struct xgbe_prv_data *pdata)
1989{
1990 unsigned int i;
1991
1992 for (i = 0; i < pdata->rx_q_count; i++) {
1993 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFA,
1994 pdata->rx_rfa[i]);
1995 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFD,
1996 pdata->rx_rfd[i]);
1997 }
1998}
1999
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002000static unsigned int xgbe_get_tx_fifo_size(struct xgbe_prv_data *pdata)
2001{
2002 unsigned int fifo_size;
2003
2004 /* Calculate the configured fifo size */
2005 fifo_size = 1 << (pdata->hw_feat.tx_fifo_size + 7);
2006
2007 /* The configured value may not be the actual amount of fifo RAM */
2008 return min_t(unsigned int, XGMAC_FIFO_TX_MAX, fifo_size);
2009}
2010
2011static unsigned int xgbe_get_rx_fifo_size(struct xgbe_prv_data *pdata)
2012{
2013 unsigned int fifo_size;
2014
2015 /* Calculate the configured fifo size */
2016 fifo_size = 1 << (pdata->hw_feat.rx_fifo_size + 7);
2017
2018 /* The configured value may not be the actual amount of fifo RAM */
2019 return min_t(unsigned int, XGMAC_FIFO_RX_MAX, fifo_size);
2020}
2021
2022static void xgbe_calculate_equal_fifo(unsigned int fifo_size,
2023 unsigned int queue_count,
2024 unsigned int *fifo)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002025{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002026 unsigned int q_fifo_size;
2027 unsigned int p_fifo;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002028 unsigned int i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002029
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002030 q_fifo_size = fifo_size / queue_count;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002031
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002032 /* Calculate the fifo setting by dividing the queue's fifo size
2033 * by the fifo allocation increment (with 0 representing the
2034 * base allocation increment so decrement the result by 1).
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002035 */
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002036 p_fifo = q_fifo_size / XGMAC_FIFO_UNIT;
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002037 if (p_fifo)
2038 p_fifo--;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002039
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002040 /* Distribute the fifo equally amongst the queues */
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002041 for (i = 0; i < queue_count; i++)
2042 fifo[i] = p_fifo;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002043}
2044
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002045static unsigned int xgbe_set_nonprio_fifos(unsigned int fifo_size,
2046 unsigned int queue_count,
2047 unsigned int *fifo)
2048{
2049 unsigned int i;
2050
2051 BUILD_BUG_ON_NOT_POWER_OF_2(XGMAC_FIFO_MIN_ALLOC);
2052
2053 if (queue_count <= IEEE_8021QAZ_MAX_TCS)
2054 return fifo_size;
2055
2056 /* Rx queues 9 and up are for specialized packets,
2057 * such as PTP or DCB control packets, etc. and
2058 * don't require a large fifo
2059 */
2060 for (i = IEEE_8021QAZ_MAX_TCS; i < queue_count; i++) {
2061 fifo[i] = (XGMAC_FIFO_MIN_ALLOC / XGMAC_FIFO_UNIT) - 1;
2062 fifo_size -= XGMAC_FIFO_MIN_ALLOC;
2063 }
2064
2065 return fifo_size;
2066}
2067
2068static unsigned int xgbe_get_pfc_delay(struct xgbe_prv_data *pdata)
2069{
2070 unsigned int delay;
2071
2072 /* If a delay has been provided, use that */
2073 if (pdata->pfc->delay)
2074 return pdata->pfc->delay / 8;
2075
2076 /* Allow for two maximum size frames */
2077 delay = xgbe_get_max_frame(pdata);
2078 delay += XGMAC_ETH_PREAMBLE;
2079 delay *= 2;
2080
2081 /* Allow for PFC frame */
2082 delay += XGMAC_PFC_DATA_LEN;
2083 delay += ETH_HLEN + ETH_FCS_LEN;
2084 delay += XGMAC_ETH_PREAMBLE;
2085
2086 /* Allow for miscellaneous delays (LPI exit, cable, etc.) */
2087 delay += XGMAC_PFC_DELAYS;
2088
2089 return delay;
2090}
2091
2092static unsigned int xgbe_get_pfc_queues(struct xgbe_prv_data *pdata)
2093{
2094 unsigned int count, prio_queues;
2095 unsigned int i;
2096
2097 if (!pdata->pfc->pfc_en)
2098 return 0;
2099
2100 count = 0;
2101 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2102 for (i = 0; i < prio_queues; i++) {
2103 if (!xgbe_is_pfc_queue(pdata, i))
2104 continue;
2105
2106 pdata->pfcq[i] = 1;
2107 count++;
2108 }
2109
2110 return count;
2111}
2112
2113static void xgbe_calculate_dcb_fifo(struct xgbe_prv_data *pdata,
2114 unsigned int fifo_size,
2115 unsigned int *fifo)
2116{
2117 unsigned int q_fifo_size, rem_fifo, addn_fifo;
2118 unsigned int prio_queues;
2119 unsigned int pfc_count;
2120 unsigned int i;
2121
2122 q_fifo_size = XGMAC_FIFO_ALIGN(xgbe_get_max_frame(pdata));
2123 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2124 pfc_count = xgbe_get_pfc_queues(pdata);
2125
2126 if (!pfc_count || ((q_fifo_size * prio_queues) > fifo_size)) {
2127 /* No traffic classes with PFC enabled or can't do lossless */
2128 xgbe_calculate_equal_fifo(fifo_size, prio_queues, fifo);
2129 return;
2130 }
2131
2132 /* Calculate how much fifo we have to play with */
2133 rem_fifo = fifo_size - (q_fifo_size * prio_queues);
2134
2135 /* Calculate how much more than base fifo PFC needs, which also
2136 * becomes the threshold activation point (RFA)
2137 */
2138 pdata->pfc_rfa = xgbe_get_pfc_delay(pdata);
2139 pdata->pfc_rfa = XGMAC_FLOW_CONTROL_ALIGN(pdata->pfc_rfa);
2140
2141 if (pdata->pfc_rfa > q_fifo_size) {
2142 addn_fifo = pdata->pfc_rfa - q_fifo_size;
2143 addn_fifo = XGMAC_FIFO_ALIGN(addn_fifo);
2144 } else {
2145 addn_fifo = 0;
2146 }
2147
2148 /* Calculate DCB fifo settings:
2149 * - distribute remaining fifo between the VLAN priority
2150 * queues based on traffic class PFC enablement and overall
2151 * priority (0 is lowest priority, so start at highest)
2152 */
2153 i = prio_queues;
2154 while (i > 0) {
2155 i--;
2156
2157 fifo[i] = (q_fifo_size / XGMAC_FIFO_UNIT) - 1;
2158
2159 if (!pdata->pfcq[i] || !addn_fifo)
2160 continue;
2161
2162 if (addn_fifo > rem_fifo) {
2163 netdev_warn(pdata->netdev,
2164 "RXq%u cannot set needed fifo size\n", i);
2165 if (!rem_fifo)
2166 continue;
2167
2168 addn_fifo = rem_fifo;
2169 }
2170
2171 fifo[i] += (addn_fifo / XGMAC_FIFO_UNIT);
2172 rem_fifo -= addn_fifo;
2173 }
2174
2175 if (rem_fifo) {
2176 unsigned int inc_fifo = rem_fifo / prio_queues;
2177
2178 /* Distribute remaining fifo across queues */
2179 for (i = 0; i < prio_queues; i++)
2180 fifo[i] += (inc_fifo / XGMAC_FIFO_UNIT);
2181 }
2182}
2183
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002184static void xgbe_config_tx_fifo_size(struct xgbe_prv_data *pdata)
2185{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002186 unsigned int fifo_size;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002187 unsigned int fifo[XGBE_MAX_QUEUES];
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002188 unsigned int i;
2189
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002190 fifo_size = xgbe_get_tx_fifo_size(pdata);
2191
2192 xgbe_calculate_equal_fifo(fifo_size, pdata->tx_q_count, fifo);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002193
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002194 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002195 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TQS, fifo[i]);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002196
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002197 netif_info(pdata, drv, pdata->netdev,
2198 "%d Tx hardware queues, %d byte fifo per queue\n",
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002199 pdata->tx_q_count, ((fifo[0] + 1) * XGMAC_FIFO_UNIT));
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002200}
2201
2202static void xgbe_config_rx_fifo_size(struct xgbe_prv_data *pdata)
2203{
Lendacky, Thomas9c439e42015-09-30 08:53:03 -05002204 unsigned int fifo_size;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002205 unsigned int fifo[XGBE_MAX_QUEUES];
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002206 unsigned int prio_queues;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002207 unsigned int i;
2208
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002209 /* Clear any DCB related fifo/queue information */
2210 memset(pdata->pfcq, 0, sizeof(pdata->pfcq));
2211 pdata->pfc_rfa = 0;
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002212
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002213 fifo_size = xgbe_get_rx_fifo_size(pdata);
2214 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2215
2216 /* Assign a minimum fifo to the non-VLAN priority queues */
2217 fifo_size = xgbe_set_nonprio_fifos(fifo_size, pdata->rx_q_count, fifo);
2218
2219 if (pdata->pfc && pdata->ets)
2220 xgbe_calculate_dcb_fifo(pdata, fifo_size, fifo);
2221 else
2222 xgbe_calculate_equal_fifo(fifo_size, prio_queues, fifo);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002223
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002224 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomas586e3cfb2016-11-03 13:17:48 -05002225 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RQS, fifo[i]);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002226
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002227 xgbe_calculate_flow_control_threshold(pdata, fifo);
2228 xgbe_config_flow_control_threshold(pdata);
2229
2230 if (pdata->pfc && pdata->ets && pdata->pfc->pfc_en) {
2231 netif_info(pdata, drv, pdata->netdev,
2232 "%u Rx hardware queues\n", pdata->rx_q_count);
2233 for (i = 0; i < pdata->rx_q_count; i++)
2234 netif_info(pdata, drv, pdata->netdev,
2235 "RxQ%u, %u byte fifo queue\n", i,
2236 ((fifo[i] + 1) * XGMAC_FIFO_UNIT));
2237 } else {
2238 netif_info(pdata, drv, pdata->netdev,
2239 "%u Rx hardware queues, %u byte fifo per queue\n",
2240 pdata->rx_q_count,
2241 ((fifo[0] + 1) * XGMAC_FIFO_UNIT));
2242 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002243}
2244
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002245static void xgbe_config_queue_mapping(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002246{
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002247 unsigned int qptc, qptc_extra, queue;
2248 unsigned int prio_queues;
2249 unsigned int ppq, ppq_extra, prio;
2250 unsigned int mask;
2251 unsigned int i, j, reg, reg_val;
2252
2253 /* Map the MTL Tx Queues to Traffic Classes
2254 * Note: Tx Queues >= Traffic Classes
2255 */
2256 qptc = pdata->tx_q_count / pdata->hw_feat.tc_cnt;
2257 qptc_extra = pdata->tx_q_count % pdata->hw_feat.tc_cnt;
2258
2259 for (i = 0, queue = 0; i < pdata->hw_feat.tc_cnt; i++) {
2260 for (j = 0; j < qptc; j++) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002261 netif_dbg(pdata, drv, pdata->netdev,
2262 "TXq%u mapped to TC%u\n", queue, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002263 XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
2264 Q2TCMAP, i);
2265 pdata->q2tc_map[queue++] = i;
2266 }
2267
2268 if (i < qptc_extra) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002269 netif_dbg(pdata, drv, pdata->netdev,
2270 "TXq%u mapped to TC%u\n", queue, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002271 XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
2272 Q2TCMAP, i);
2273 pdata->q2tc_map[queue++] = i;
2274 }
2275 }
2276
2277 /* Map the 8 VLAN priority values to available MTL Rx queues */
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002278 prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002279 ppq = IEEE_8021QAZ_MAX_TCS / prio_queues;
2280 ppq_extra = IEEE_8021QAZ_MAX_TCS % prio_queues;
2281
2282 reg = MAC_RQC2R;
2283 reg_val = 0;
2284 for (i = 0, prio = 0; i < prio_queues;) {
2285 mask = 0;
2286 for (j = 0; j < ppq; j++) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002287 netif_dbg(pdata, drv, pdata->netdev,
2288 "PRIO%u mapped to RXq%u\n", prio, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002289 mask |= (1 << prio);
2290 pdata->prio2q_map[prio++] = i;
2291 }
2292
2293 if (i < ppq_extra) {
Lendacky, Thomas34bf65d2015-05-14 11:44:03 -05002294 netif_dbg(pdata, drv, pdata->netdev,
2295 "PRIO%u mapped to RXq%u\n", prio, i);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002296 mask |= (1 << prio);
2297 pdata->prio2q_map[prio++] = i;
2298 }
2299
2300 reg_val |= (mask << ((i++ % MAC_RQC2_Q_PER_REG) << 3));
2301
2302 if ((i % MAC_RQC2_Q_PER_REG) && (i != prio_queues))
2303 continue;
2304
2305 XGMAC_IOWRITE(pdata, reg, reg_val);
2306 reg += MAC_RQC2_INC;
2307 reg_val = 0;
2308 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002309
2310 /* Select dynamic mapping of MTL Rx queue to DMA Rx channel */
2311 reg = MTL_RQDCM0R;
2312 reg_val = 0;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002313 for (i = 0; i < pdata->rx_q_count;) {
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002314 reg_val |= (0x80 << ((i++ % MTL_RQDCM_Q_PER_REG) << 3));
2315
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05002316 if ((i % MTL_RQDCM_Q_PER_REG) && (i != pdata->rx_q_count))
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002317 continue;
2318
2319 XGMAC_IOWRITE(pdata, reg, reg_val);
2320
2321 reg += MTL_RQDCM_INC;
2322 reg_val = 0;
2323 }
2324}
2325
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002326static void xgbe_config_tc(struct xgbe_prv_data *pdata)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002327{
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002328 unsigned int offset, queue, prio;
2329 u8 i;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002330
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002331 netdev_reset_tc(pdata->netdev);
2332 if (!pdata->num_tcs)
2333 return;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002334
Lendacky, Thomas43e0dcf2016-11-03 13:18:16 -05002335 netdev_set_num_tc(pdata->netdev, pdata->num_tcs);
2336
2337 for (i = 0, queue = 0, offset = 0; i < pdata->num_tcs; i++) {
2338 while ((queue < pdata->tx_q_count) &&
2339 (pdata->q2tc_map[queue] == i))
2340 queue++;
2341
2342 netif_dbg(pdata, drv, pdata->netdev, "TC%u using TXq%u-%u\n",
2343 i, offset, queue - 1);
2344 netdev_set_tc_queue(pdata->netdev, i, queue - offset, offset);
2345 offset = queue;
2346 }
2347
2348 if (!pdata->ets)
2349 return;
2350
2351 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++)
2352 netdev_set_prio_tc_map(pdata->netdev, prio,
2353 pdata->ets->prio_tc[prio]);
2354}
2355
2356static void xgbe_config_dcb_tc(struct xgbe_prv_data *pdata)
2357{
2358 struct ieee_ets *ets = pdata->ets;
2359 unsigned int total_weight, min_weight, weight;
2360 unsigned int mask, reg, reg_val;
2361 unsigned int i, prio;
2362
2363 if (!ets)
2364 return;
2365
2366 /* Set Tx to deficit weighted round robin scheduling algorithm (when
2367 * traffic class is using ETS algorithm)
2368 */
2369 XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_DWRR);
2370
2371 /* Set Traffic Class algorithms */
2372 total_weight = pdata->netdev->mtu * pdata->hw_feat.tc_cnt;
2373 min_weight = total_weight / 100;
2374 if (!min_weight)
2375 min_weight = 1;
2376
2377 for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
2378 /* Map the priorities to the traffic class */
2379 mask = 0;
2380 for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
2381 if (ets->prio_tc[prio] == i)
2382 mask |= (1 << prio);
2383 }
2384 mask &= 0xff;
2385
2386 netif_dbg(pdata, drv, pdata->netdev, "TC%u PRIO mask=%#x\n",
2387 i, mask);
2388 reg = MTL_TCPM0R + (MTL_TCPM_INC * (i / MTL_TCPM_TC_PER_REG));
2389 reg_val = XGMAC_IOREAD(pdata, reg);
2390
2391 reg_val &= ~(0xff << ((i % MTL_TCPM_TC_PER_REG) << 3));
2392 reg_val |= (mask << ((i % MTL_TCPM_TC_PER_REG) << 3));
2393
2394 XGMAC_IOWRITE(pdata, reg, reg_val);
2395
2396 /* Set the traffic class algorithm */
2397 switch (ets->tc_tsa[i]) {
2398 case IEEE_8021QAZ_TSA_STRICT:
2399 netif_dbg(pdata, drv, pdata->netdev,
2400 "TC%u using SP\n", i);
2401 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2402 MTL_TSA_SP);
2403 break;
2404 case IEEE_8021QAZ_TSA_ETS:
2405 weight = total_weight * ets->tc_tx_bw[i] / 100;
2406 weight = clamp(weight, min_weight, total_weight);
2407
2408 netif_dbg(pdata, drv, pdata->netdev,
2409 "TC%u using DWRR (weight %u)\n", i, weight);
2410 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2411 MTL_TSA_ETS);
2412 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW,
2413 weight);
2414 break;
2415 }
2416 }
2417
2418 xgbe_config_tc(pdata);
2419}
2420
2421static void xgbe_config_dcb_pfc(struct xgbe_prv_data *pdata)
2422{
2423 if (!test_bit(XGBE_DOWN, &pdata->dev_state)) {
2424 /* Just stop the Tx queues while Rx fifo is changed */
2425 netif_tx_stop_all_queues(pdata->netdev);
2426
2427 /* Suspend Rx so that fifo's can be adjusted */
2428 pdata->hw_if.disable_rx(pdata);
2429 }
2430
2431 xgbe_config_rx_fifo_size(pdata);
2432 xgbe_config_flow_control(pdata);
2433
2434 if (!test_bit(XGBE_DOWN, &pdata->dev_state)) {
2435 /* Resume Rx */
2436 pdata->hw_if.enable_rx(pdata);
2437
2438 /* Resume Tx queues */
2439 netif_tx_start_all_queues(pdata->netdev);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002440 }
2441}
2442
2443static void xgbe_config_mac_address(struct xgbe_prv_data *pdata)
2444{
2445 xgbe_set_mac_address(pdata, pdata->netdev->dev_addr);
Lendacky, Thomasb85e4d82014-06-24 16:19:29 -05002446
2447 /* Filtering is done using perfect filtering and hash filtering */
2448 if (pdata->hw_feat.hash_table_size) {
2449 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HPF, 1);
2450 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HUC, 1);
2451 XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HMC, 1);
2452 }
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002453}
2454
2455static void xgbe_config_jumbo_enable(struct xgbe_prv_data *pdata)
2456{
2457 unsigned int val;
2458
2459 val = (pdata->netdev->mtu > XGMAC_STD_PACKET_MTU) ? 1 : 0;
2460
2461 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, JE, val);
2462}
2463
Lendacky, Thomas916102c2015-01-16 12:46:45 -06002464static void xgbe_config_mac_speed(struct xgbe_prv_data *pdata)
2465{
Lendacky, Thomase57f7a32016-11-03 13:18:27 -05002466 xgbe_set_speed(pdata, pdata->phy_speed);
Lendacky, Thomas916102c2015-01-16 12:46:45 -06002467}
2468
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002469static void xgbe_config_checksum_offload(struct xgbe_prv_data *pdata)
2470{
2471 if (pdata->netdev->features & NETIF_F_RXCSUM)
2472 xgbe_enable_rx_csum(pdata);
2473 else
2474 xgbe_disable_rx_csum(pdata);
2475}
2476
2477static void xgbe_config_vlan_support(struct xgbe_prv_data *pdata)
2478{
Lendacky, Thomas6e5eed02014-06-24 16:19:12 -05002479 /* Indicate that VLAN Tx CTAGs come from context descriptors */
2480 XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, CSVL, 0);
2481 XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, VLTI, 1);
2482
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05002483 /* Set the current VLAN Hash Table register value */
2484 xgbe_update_vlan_hash_table(pdata);
2485
2486 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
2487 xgbe_enable_rx_vlan_filtering(pdata);
2488 else
2489 xgbe_disable_rx_vlan_filtering(pdata);
2490
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002491 if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2492 xgbe_enable_rx_vlan_stripping(pdata);
2493 else
2494 xgbe_disable_rx_vlan_stripping(pdata);
2495}
2496
Lendacky, Thomas60265102014-09-05 18:02:30 -05002497static u64 xgbe_mmc_read(struct xgbe_prv_data *pdata, unsigned int reg_lo)
2498{
2499 bool read_hi;
2500 u64 val;
2501
2502 switch (reg_lo) {
2503 /* These registers are always 64 bit */
2504 case MMC_TXOCTETCOUNT_GB_LO:
2505 case MMC_TXOCTETCOUNT_G_LO:
2506 case MMC_RXOCTETCOUNT_GB_LO:
2507 case MMC_RXOCTETCOUNT_G_LO:
2508 read_hi = true;
2509 break;
2510
2511 default:
2512 read_hi = false;
Lendacky, Thomas3947d782015-09-30 08:52:38 -05002513 }
Lendacky, Thomas60265102014-09-05 18:02:30 -05002514
2515 val = XGMAC_IOREAD(pdata, reg_lo);
2516
2517 if (read_hi)
2518 val |= ((u64)XGMAC_IOREAD(pdata, reg_lo + 4) << 32);
2519
2520 return val;
2521}
2522
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002523static void xgbe_tx_mmc_int(struct xgbe_prv_data *pdata)
2524{
2525 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2526 unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_TISR);
2527
2528 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_GB))
2529 stats->txoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002530 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002531
2532 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_GB))
2533 stats->txframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002534 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002535
2536 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_G))
2537 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002538 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002539
2540 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_G))
2541 stats->txmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002542 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002543
2544 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX64OCTETS_GB))
2545 stats->tx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002546 xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002547
2548 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX65TO127OCTETS_GB))
2549 stats->tx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002550 xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002551
2552 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX128TO255OCTETS_GB))
2553 stats->tx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002554 xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002555
2556 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX256TO511OCTETS_GB))
2557 stats->tx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002558 xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002559
2560 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX512TO1023OCTETS_GB))
2561 stats->tx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002562 xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002563
2564 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX1024TOMAXOCTETS_GB))
2565 stats->tx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002566 xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002567
2568 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNICASTFRAMES_GB))
2569 stats->txunicastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002570 xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002571
2572 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_GB))
2573 stats->txmulticastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002574 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002575
2576 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_GB))
2577 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002578 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002579
2580 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNDERFLOWERROR))
2581 stats->txunderflowerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002582 xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002583
2584 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_G))
2585 stats->txoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002586 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002587
2588 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_G))
2589 stats->txframecount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002590 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002591
2592 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXPAUSEFRAMES))
2593 stats->txpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002594 xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002595
2596 if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXVLANFRAMES_G))
2597 stats->txvlanframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002598 xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002599}
2600
2601static void xgbe_rx_mmc_int(struct xgbe_prv_data *pdata)
2602{
2603 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2604 unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_RISR);
2605
2606 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFRAMECOUNT_GB))
2607 stats->rxframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002608 xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002609
2610 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_GB))
2611 stats->rxoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002612 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002613
2614 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_G))
2615 stats->rxoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002616 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002617
2618 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXBROADCASTFRAMES_G))
2619 stats->rxbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002620 xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002621
2622 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXMULTICASTFRAMES_G))
2623 stats->rxmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002624 xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002625
2626 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXCRCERROR))
2627 stats->rxcrcerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002628 xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002629
2630 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXRUNTERROR))
2631 stats->rxrunterror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002632 xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002633
2634 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXJABBERERROR))
2635 stats->rxjabbererror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002636 xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002637
2638 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNDERSIZE_G))
2639 stats->rxundersize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002640 xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002641
2642 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOVERSIZE_G))
2643 stats->rxoversize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002644 xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002645
2646 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX64OCTETS_GB))
2647 stats->rx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002648 xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002649
2650 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX65TO127OCTETS_GB))
2651 stats->rx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002652 xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002653
2654 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX128TO255OCTETS_GB))
2655 stats->rx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002656 xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002657
2658 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX256TO511OCTETS_GB))
2659 stats->rx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002660 xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002661
2662 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX512TO1023OCTETS_GB))
2663 stats->rx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002664 xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002665
2666 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX1024TOMAXOCTETS_GB))
2667 stats->rx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002668 xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002669
2670 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNICASTFRAMES_G))
2671 stats->rxunicastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002672 xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002673
2674 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXLENGTHERROR))
2675 stats->rxlengtherror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002676 xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002677
2678 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOUTOFRANGETYPE))
2679 stats->rxoutofrangetype +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002680 xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002681
2682 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXPAUSEFRAMES))
2683 stats->rxpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002684 xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002685
2686 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFIFOOVERFLOW))
2687 stats->rxfifooverflow +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002688 xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002689
2690 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXVLANFRAMES_GB))
2691 stats->rxvlanframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002692 xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002693
2694 if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXWATCHDOGERROR))
2695 stats->rxwatchdogerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002696 xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002697}
2698
2699static void xgbe_read_mmc_stats(struct xgbe_prv_data *pdata)
2700{
2701 struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2702
2703 /* Freeze counters */
2704 XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 1);
2705
2706 stats->txoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002707 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002708
2709 stats->txframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002710 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002711
2712 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002713 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002714
2715 stats->txmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002716 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002717
2718 stats->tx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002719 xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002720
2721 stats->tx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002722 xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002723
2724 stats->tx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002725 xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002726
2727 stats->tx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002728 xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002729
2730 stats->tx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002731 xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002732
2733 stats->tx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002734 xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002735
2736 stats->txunicastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002737 xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002738
2739 stats->txmulticastframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002740 xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002741
2742 stats->txbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002743 xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002744
2745 stats->txunderflowerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002746 xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002747
2748 stats->txoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002749 xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002750
2751 stats->txframecount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002752 xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002753
2754 stats->txpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002755 xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002756
2757 stats->txvlanframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002758 xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002759
2760 stats->rxframecount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002761 xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002762
2763 stats->rxoctetcount_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002764 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002765
2766 stats->rxoctetcount_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002767 xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002768
2769 stats->rxbroadcastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002770 xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002771
2772 stats->rxmulticastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002773 xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002774
2775 stats->rxcrcerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002776 xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002777
2778 stats->rxrunterror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002779 xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002780
2781 stats->rxjabbererror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002782 xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002783
2784 stats->rxundersize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002785 xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002786
2787 stats->rxoversize_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002788 xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002789
2790 stats->rx64octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002791 xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002792
2793 stats->rx65to127octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002794 xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002795
2796 stats->rx128to255octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002797 xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002798
2799 stats->rx256to511octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002800 xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002801
2802 stats->rx512to1023octets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002803 xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002804
2805 stats->rx1024tomaxoctets_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002806 xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002807
2808 stats->rxunicastframes_g +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002809 xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002810
2811 stats->rxlengtherror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002812 xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002813
2814 stats->rxoutofrangetype +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002815 xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002816
2817 stats->rxpauseframes +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002818 xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002819
2820 stats->rxfifooverflow +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002821 xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002822
2823 stats->rxvlanframes_gb +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002824 xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002825
2826 stats->rxwatchdogerror +=
Lendacky, Thomas60265102014-09-05 18:02:30 -05002827 xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002828
2829 /* Un-freeze counters */
2830 XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 0);
2831}
2832
2833static void xgbe_config_mmc(struct xgbe_prv_data *pdata)
2834{
2835 /* Set counters to reset on read */
2836 XGMAC_IOWRITE_BITS(pdata, MMC_CR, ROR, 1);
2837
2838 /* Reset the counters */
2839 XGMAC_IOWRITE_BITS(pdata, MMC_CR, CR, 1);
2840}
2841
Lendacky, Thomas16edd342014-11-20 11:03:32 -06002842static void xgbe_prepare_tx_stop(struct xgbe_prv_data *pdata,
2843 struct xgbe_channel *channel)
2844{
2845 unsigned int tx_dsr, tx_pos, tx_qidx;
2846 unsigned int tx_status;
2847 unsigned long tx_timeout;
2848
2849 /* Calculate the status register to read and the position within */
2850 if (channel->queue_index < DMA_DSRX_FIRST_QUEUE) {
2851 tx_dsr = DMA_DSR0;
2852 tx_pos = (channel->queue_index * DMA_DSR_Q_WIDTH) +
2853 DMA_DSR0_TPS_START;
2854 } else {
2855 tx_qidx = channel->queue_index - DMA_DSRX_FIRST_QUEUE;
2856
2857 tx_dsr = DMA_DSR1 + ((tx_qidx / DMA_DSRX_QPR) * DMA_DSRX_INC);
2858 tx_pos = ((tx_qidx % DMA_DSRX_QPR) * DMA_DSR_Q_WIDTH) +
2859 DMA_DSRX_TPS_START;
2860 }
2861
2862 /* The Tx engine cannot be stopped if it is actively processing
2863 * descriptors. Wait for the Tx engine to enter the stopped or
2864 * suspended state. Don't wait forever though...
2865 */
2866 tx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
2867 while (time_before(jiffies, tx_timeout)) {
2868 tx_status = XGMAC_IOREAD(pdata, tx_dsr);
2869 tx_status = GET_BITS(tx_status, tx_pos, DMA_DSR_TPS_WIDTH);
2870 if ((tx_status == DMA_TPS_STOPPED) ||
2871 (tx_status == DMA_TPS_SUSPENDED))
2872 break;
2873
2874 usleep_range(500, 1000);
2875 }
2876
2877 if (!time_before(jiffies, tx_timeout))
2878 netdev_info(pdata->netdev,
2879 "timed out waiting for Tx DMA channel %u to stop\n",
2880 channel->queue_index);
2881}
2882
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002883static void xgbe_enable_tx(struct xgbe_prv_data *pdata)
2884{
2885 struct xgbe_channel *channel;
2886 unsigned int i;
2887
2888 /* Enable each Tx DMA channel */
2889 channel = pdata->channel;
2890 for (i = 0; i < pdata->channel_count; i++, channel++) {
2891 if (!channel->tx_ring)
2892 break;
2893
2894 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 1);
2895 }
2896
2897 /* Enable each Tx queue */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002898 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002899 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN,
2900 MTL_Q_ENABLED);
2901
2902 /* Enable MAC Tx */
2903 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
2904}
2905
2906static void xgbe_disable_tx(struct xgbe_prv_data *pdata)
2907{
2908 struct xgbe_channel *channel;
2909 unsigned int i;
2910
Lendacky, Thomas16edd342014-11-20 11:03:32 -06002911 /* Prepare for Tx DMA channel stop */
2912 channel = pdata->channel;
2913 for (i = 0; i < pdata->channel_count; i++, channel++) {
2914 if (!channel->tx_ring)
2915 break;
2916
2917 xgbe_prepare_tx_stop(pdata, channel);
2918 }
2919
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002920 /* Disable MAC Tx */
2921 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
2922
2923 /* Disable each Tx queue */
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002924 for (i = 0; i < pdata->tx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002925 XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN, 0);
2926
2927 /* Disable each Tx DMA channel */
2928 channel = pdata->channel;
2929 for (i = 0; i < pdata->channel_count; i++, channel++) {
2930 if (!channel->tx_ring)
2931 break;
2932
2933 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 0);
2934 }
2935}
2936
Lendacky, Thomasc3727d62016-02-17 11:49:16 -06002937static void xgbe_prepare_rx_stop(struct xgbe_prv_data *pdata,
2938 unsigned int queue)
2939{
2940 unsigned int rx_status;
2941 unsigned long rx_timeout;
2942
2943 /* The Rx engine cannot be stopped if it is actively processing
2944 * packets. Wait for the Rx queue to empty the Rx fifo. Don't
2945 * wait forever though...
2946 */
2947 rx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
2948 while (time_before(jiffies, rx_timeout)) {
2949 rx_status = XGMAC_MTL_IOREAD(pdata, queue, MTL_Q_RQDR);
2950 if ((XGMAC_GET_BITS(rx_status, MTL_Q_RQDR, PRXQ) == 0) &&
2951 (XGMAC_GET_BITS(rx_status, MTL_Q_RQDR, RXQSTS) == 0))
2952 break;
2953
2954 usleep_range(500, 1000);
2955 }
2956
2957 if (!time_before(jiffies, rx_timeout))
2958 netdev_info(pdata->netdev,
2959 "timed out waiting for Rx queue %u to empty\n",
2960 queue);
2961}
2962
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002963static void xgbe_enable_rx(struct xgbe_prv_data *pdata)
2964{
2965 struct xgbe_channel *channel;
2966 unsigned int reg_val, i;
2967
2968 /* Enable each Rx DMA channel */
2969 channel = pdata->channel;
2970 for (i = 0; i < pdata->channel_count; i++, channel++) {
2971 if (!channel->rx_ring)
2972 break;
2973
2974 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 1);
2975 }
2976
2977 /* Enable each Rx queue */
2978 reg_val = 0;
Lendacky, Thomas853eb162014-07-29 08:57:31 -05002979 for (i = 0; i < pdata->rx_q_count; i++)
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05002980 reg_val |= (0x02 << (i << 1));
2981 XGMAC_IOWRITE(pdata, MAC_RQC0R, reg_val);
2982
2983 /* Enable MAC Rx */
2984 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 1);
2985 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 1);
2986 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 1);
2987 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 1);
2988}
2989
2990static void xgbe_disable_rx(struct xgbe_prv_data *pdata)
2991{
2992 struct xgbe_channel *channel;
2993 unsigned int i;
2994
2995 /* Disable MAC Rx */
2996 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 0);
2997 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 0);
2998 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 0);
2999 XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 0);
3000
Lendacky, Thomasc3727d62016-02-17 11:49:16 -06003001 /* Prepare for Rx DMA channel stop */
3002 for (i = 0; i < pdata->rx_q_count; i++)
3003 xgbe_prepare_rx_stop(pdata, i);
3004
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003005 /* Disable each Rx queue */
3006 XGMAC_IOWRITE(pdata, MAC_RQC0R, 0);
3007
3008 /* Disable each Rx DMA channel */
3009 channel = pdata->channel;
3010 for (i = 0; i < pdata->channel_count; i++, channel++) {
3011 if (!channel->rx_ring)
3012 break;
3013
3014 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 0);
3015 }
3016}
3017
3018static void xgbe_powerup_tx(struct xgbe_prv_data *pdata)
3019{
3020 struct xgbe_channel *channel;
3021 unsigned int i;
3022
3023 /* Enable each Tx DMA channel */
3024 channel = pdata->channel;
3025 for (i = 0; i < pdata->channel_count; i++, channel++) {
3026 if (!channel->tx_ring)
3027 break;
3028
3029 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 1);
3030 }
3031
3032 /* Enable MAC Tx */
3033 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
3034}
3035
3036static void xgbe_powerdown_tx(struct xgbe_prv_data *pdata)
3037{
3038 struct xgbe_channel *channel;
3039 unsigned int i;
3040
Lendacky, Thomas16edd342014-11-20 11:03:32 -06003041 /* Prepare for Tx DMA channel stop */
3042 channel = pdata->channel;
3043 for (i = 0; i < pdata->channel_count; i++, channel++) {
3044 if (!channel->tx_ring)
3045 break;
3046
3047 xgbe_prepare_tx_stop(pdata, channel);
3048 }
3049
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003050 /* Disable MAC Tx */
3051 XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
3052
3053 /* Disable each Tx DMA channel */
3054 channel = pdata->channel;
3055 for (i = 0; i < pdata->channel_count; i++, channel++) {
3056 if (!channel->tx_ring)
3057 break;
3058
3059 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_TCR, ST, 0);
3060 }
3061}
3062
3063static void xgbe_powerup_rx(struct xgbe_prv_data *pdata)
3064{
3065 struct xgbe_channel *channel;
3066 unsigned int i;
3067
3068 /* Enable each Rx DMA channel */
3069 channel = pdata->channel;
3070 for (i = 0; i < pdata->channel_count; i++, channel++) {
3071 if (!channel->rx_ring)
3072 break;
3073
3074 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 1);
3075 }
3076}
3077
3078static void xgbe_powerdown_rx(struct xgbe_prv_data *pdata)
3079{
3080 struct xgbe_channel *channel;
3081 unsigned int i;
3082
3083 /* Disable each Rx DMA channel */
3084 channel = pdata->channel;
3085 for (i = 0; i < pdata->channel_count; i++, channel++) {
3086 if (!channel->rx_ring)
3087 break;
3088
3089 XGMAC_DMA_IOWRITE_BITS(channel, DMA_CH_RCR, SR, 0);
3090 }
3091}
3092
3093static int xgbe_init(struct xgbe_prv_data *pdata)
3094{
3095 struct xgbe_desc_if *desc_if = &pdata->desc_if;
3096 int ret;
3097
3098 DBGPR("-->xgbe_init\n");
3099
3100 /* Flush Tx queues */
3101 ret = xgbe_flush_tx_queues(pdata);
3102 if (ret)
3103 return ret;
3104
3105 /*
3106 * Initialize DMA related features
3107 */
3108 xgbe_config_dma_bus(pdata);
3109 xgbe_config_dma_cache(pdata);
3110 xgbe_config_osp_mode(pdata);
3111 xgbe_config_pblx8(pdata);
3112 xgbe_config_tx_pbl_val(pdata);
3113 xgbe_config_rx_pbl_val(pdata);
3114 xgbe_config_rx_coalesce(pdata);
3115 xgbe_config_tx_coalesce(pdata);
3116 xgbe_config_rx_buffer_size(pdata);
3117 xgbe_config_tso_mode(pdata);
Lendacky, Thomas174fd252014-11-04 16:06:50 -06003118 xgbe_config_sph_mode(pdata);
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003119 xgbe_config_rss(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003120 desc_if->wrapper_tx_desc_init(pdata);
3121 desc_if->wrapper_rx_desc_init(pdata);
3122 xgbe_enable_dma_interrupts(pdata);
3123
3124 /*
3125 * Initialize MTL related features
3126 */
3127 xgbe_config_mtl_mode(pdata);
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003128 xgbe_config_queue_mapping(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003129 xgbe_config_tsf_mode(pdata, pdata->tx_sf_mode);
3130 xgbe_config_rsf_mode(pdata, pdata->rx_sf_mode);
3131 xgbe_config_tx_threshold(pdata, pdata->tx_threshold);
3132 xgbe_config_rx_threshold(pdata, pdata->rx_threshold);
3133 xgbe_config_tx_fifo_size(pdata);
3134 xgbe_config_rx_fifo_size(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003135 /*TODO: Error Packet and undersized good Packet forwarding enable
3136 (FEP and FUP)
3137 */
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003138 xgbe_config_dcb_tc(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003139 xgbe_enable_mtl_interrupts(pdata);
3140
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003141 /*
3142 * Initialize MAC related features
3143 */
3144 xgbe_config_mac_address(pdata);
Lendacky, Thomasb8763822015-04-09 12:11:57 -05003145 xgbe_config_rx_mode(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003146 xgbe_config_jumbo_enable(pdata);
3147 xgbe_config_flow_control(pdata);
Lendacky, Thomas916102c2015-01-16 12:46:45 -06003148 xgbe_config_mac_speed(pdata);
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003149 xgbe_config_checksum_offload(pdata);
3150 xgbe_config_vlan_support(pdata);
3151 xgbe_config_mmc(pdata);
3152 xgbe_enable_mac_interrupts(pdata);
3153
3154 DBGPR("<--xgbe_init\n");
3155
3156 return 0;
3157}
3158
3159void xgbe_init_function_ptrs_dev(struct xgbe_hw_if *hw_if)
3160{
3161 DBGPR("-->xgbe_init_function_ptrs\n");
3162
3163 hw_if->tx_complete = xgbe_tx_complete;
3164
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003165 hw_if->set_mac_address = xgbe_set_mac_address;
Lendacky, Thomasb8763822015-04-09 12:11:57 -05003166 hw_if->config_rx_mode = xgbe_config_rx_mode;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003167
3168 hw_if->enable_rx_csum = xgbe_enable_rx_csum;
3169 hw_if->disable_rx_csum = xgbe_disable_rx_csum;
3170
3171 hw_if->enable_rx_vlan_stripping = xgbe_enable_rx_vlan_stripping;
3172 hw_if->disable_rx_vlan_stripping = xgbe_disable_rx_vlan_stripping;
Lendacky, Thomas801c62d2014-06-24 16:19:24 -05003173 hw_if->enable_rx_vlan_filtering = xgbe_enable_rx_vlan_filtering;
3174 hw_if->disable_rx_vlan_filtering = xgbe_disable_rx_vlan_filtering;
3175 hw_if->update_vlan_hash_table = xgbe_update_vlan_hash_table;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003176
3177 hw_if->read_mmd_regs = xgbe_read_mmd_regs;
3178 hw_if->write_mmd_regs = xgbe_write_mmd_regs;
3179
Lendacky, Thomase57f7a32016-11-03 13:18:27 -05003180 hw_if->set_speed = xgbe_set_speed;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003181
3182 hw_if->enable_tx = xgbe_enable_tx;
3183 hw_if->disable_tx = xgbe_disable_tx;
3184 hw_if->enable_rx = xgbe_enable_rx;
3185 hw_if->disable_rx = xgbe_disable_rx;
3186
3187 hw_if->powerup_tx = xgbe_powerup_tx;
3188 hw_if->powerdown_tx = xgbe_powerdown_tx;
3189 hw_if->powerup_rx = xgbe_powerup_rx;
3190 hw_if->powerdown_rx = xgbe_powerdown_rx;
3191
Lendacky, Thomasa9d41982014-11-04 16:06:32 -06003192 hw_if->dev_xmit = xgbe_dev_xmit;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003193 hw_if->dev_read = xgbe_dev_read;
3194 hw_if->enable_int = xgbe_enable_int;
3195 hw_if->disable_int = xgbe_disable_int;
3196 hw_if->init = xgbe_init;
3197 hw_if->exit = xgbe_exit;
3198
3199 /* Descriptor related Sequences have to be initialized here */
3200 hw_if->tx_desc_init = xgbe_tx_desc_init;
3201 hw_if->rx_desc_init = xgbe_rx_desc_init;
3202 hw_if->tx_desc_reset = xgbe_tx_desc_reset;
3203 hw_if->rx_desc_reset = xgbe_rx_desc_reset;
3204 hw_if->is_last_desc = xgbe_is_last_desc;
3205 hw_if->is_context_desc = xgbe_is_context_desc;
Lendacky, Thomas16958a22014-11-20 11:04:08 -06003206 hw_if->tx_start_xmit = xgbe_tx_start_xmit;
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003207
3208 /* For FLOW ctrl */
3209 hw_if->config_tx_flow_control = xgbe_config_tx_flow_control;
3210 hw_if->config_rx_flow_control = xgbe_config_rx_flow_control;
3211
3212 /* For RX coalescing */
3213 hw_if->config_rx_coalesce = xgbe_config_rx_coalesce;
3214 hw_if->config_tx_coalesce = xgbe_config_tx_coalesce;
3215 hw_if->usec_to_riwt = xgbe_usec_to_riwt;
3216 hw_if->riwt_to_usec = xgbe_riwt_to_usec;
3217
3218 /* For RX and TX threshold config */
3219 hw_if->config_rx_threshold = xgbe_config_rx_threshold;
3220 hw_if->config_tx_threshold = xgbe_config_tx_threshold;
3221
3222 /* For RX and TX Store and Forward Mode config */
3223 hw_if->config_rsf_mode = xgbe_config_rsf_mode;
3224 hw_if->config_tsf_mode = xgbe_config_tsf_mode;
3225
3226 /* For TX DMA Operating on Second Frame config */
3227 hw_if->config_osp_mode = xgbe_config_osp_mode;
3228
3229 /* For RX and TX PBL config */
3230 hw_if->config_rx_pbl_val = xgbe_config_rx_pbl_val;
3231 hw_if->get_rx_pbl_val = xgbe_get_rx_pbl_val;
3232 hw_if->config_tx_pbl_val = xgbe_config_tx_pbl_val;
3233 hw_if->get_tx_pbl_val = xgbe_get_tx_pbl_val;
3234 hw_if->config_pblx8 = xgbe_config_pblx8;
3235
3236 /* For MMC statistics support */
3237 hw_if->tx_mmc_int = xgbe_tx_mmc_int;
3238 hw_if->rx_mmc_int = xgbe_rx_mmc_int;
3239 hw_if->read_mmc_stats = xgbe_read_mmc_stats;
3240
Lendacky, Thomas23e4eef2014-07-29 08:57:19 -05003241 /* For PTP config */
3242 hw_if->config_tstamp = xgbe_config_tstamp;
3243 hw_if->update_tstamp_addend = xgbe_update_tstamp_addend;
3244 hw_if->set_tstamp_time = xgbe_set_tstamp_time;
3245 hw_if->get_tstamp_time = xgbe_get_tstamp_time;
3246 hw_if->get_tx_tstamp = xgbe_get_tx_tstamp;
3247
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003248 /* For Data Center Bridging config */
Lendacky, Thomasb3b71592016-02-17 11:49:08 -06003249 hw_if->config_tc = xgbe_config_tc;
Lendacky, Thomasfca2d992014-07-29 08:57:55 -05003250 hw_if->config_dcb_tc = xgbe_config_dcb_tc;
3251 hw_if->config_dcb_pfc = xgbe_config_dcb_pfc;
3252
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003253 /* For Receive Side Scaling */
3254 hw_if->enable_rss = xgbe_enable_rss;
3255 hw_if->disable_rss = xgbe_disable_rss;
Lendacky, Thomasf6ac8622014-11-04 16:07:23 -06003256 hw_if->set_rss_hash_key = xgbe_set_rss_hash_key;
3257 hw_if->set_rss_lookup_table = xgbe_set_rss_lookup_table;
Lendacky, Thomas5b9dfe22014-11-04 16:07:02 -06003258
Lendacky, Thomasc5aa9e32014-06-05 09:15:06 -05003259 DBGPR("<--xgbe_init_function_ptrs\n");
3260}