]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/amd/xgbe/xgbe-main.c
e5077fd5b01234592b5ba636f6f2cee2c1843196
[karo-tx-linux.git] / drivers / net / ethernet / amd / xgbe / xgbe-main.c
1 /*
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  *
9  * Copyright (c) 2014 Advanced Micro Devices, Inc.
10  *
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  *
59  * Copyright (c) 2014 Advanced Micro Devices, Inc.
60  * 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/module.h>
118 #include <linux/device.h>
119 #include <linux/platform_device.h>
120 #include <linux/spinlock.h>
121 #include <linux/netdevice.h>
122 #include <linux/etherdevice.h>
123 #include <linux/io.h>
124 #include <linux/of.h>
125 #include <linux/of_net.h>
126 #include <linux/clk.h>
127
128 #include "xgbe.h"
129 #include "xgbe-common.h"
130
131 MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
132 MODULE_LICENSE("Dual BSD/GPL");
133 MODULE_VERSION(XGBE_DRV_VERSION);
134 MODULE_DESCRIPTION(XGBE_DRV_DESC);
135
136 static void xgbe_default_config(struct xgbe_prv_data *pdata)
137 {
138         DBGPR("-->xgbe_default_config\n");
139
140         pdata->pblx8 = DMA_PBL_X8_ENABLE;
141         pdata->tx_sf_mode = MTL_TSF_ENABLE;
142         pdata->tx_threshold = MTL_TX_THRESHOLD_64;
143         pdata->tx_pbl = DMA_PBL_16;
144         pdata->tx_osp_mode = DMA_OSP_ENABLE;
145         pdata->rx_sf_mode = MTL_RSF_DISABLE;
146         pdata->rx_threshold = MTL_RX_THRESHOLD_64;
147         pdata->rx_pbl = DMA_PBL_16;
148         pdata->pause_autoneg = 1;
149         pdata->tx_pause = 1;
150         pdata->rx_pause = 1;
151         pdata->power_down = 0;
152         pdata->default_autoneg = AUTONEG_ENABLE;
153         pdata->default_speed = SPEED_10000;
154
155         DBGPR("<--xgbe_default_config\n");
156 }
157
158 static void xgbe_init_all_fptrs(struct xgbe_prv_data *pdata)
159 {
160         xgbe_init_function_ptrs_dev(&pdata->hw_if);
161         xgbe_init_function_ptrs_desc(&pdata->desc_if);
162 }
163
164 static int xgbe_probe(struct platform_device *pdev)
165 {
166         struct xgbe_prv_data *pdata;
167         struct xgbe_hw_if *hw_if;
168         struct xgbe_desc_if *desc_if;
169         struct net_device *netdev;
170         struct device *dev = &pdev->dev;
171         struct resource *res;
172         const u8 *mac_addr;
173         int ret;
174
175         DBGPR("--> xgbe_probe\n");
176
177         netdev = alloc_etherdev_mq(sizeof(struct xgbe_prv_data),
178                                    XGBE_MAX_DMA_CHANNELS);
179         if (!netdev) {
180                 dev_err(dev, "alloc_etherdev failed\n");
181                 ret = -ENOMEM;
182                 goto err_alloc;
183         }
184         SET_NETDEV_DEV(netdev, dev);
185         pdata = netdev_priv(netdev);
186         pdata->netdev = netdev;
187         pdata->pdev = pdev;
188         pdata->dev = dev;
189         platform_set_drvdata(pdev, netdev);
190
191         spin_lock_init(&pdata->lock);
192         mutex_init(&pdata->xpcs_mutex);
193         spin_lock_init(&pdata->tstamp_lock);
194
195         /* Set and validate the number of descriptors for a ring */
196         BUILD_BUG_ON_NOT_POWER_OF_2(XGBE_TX_DESC_CNT);
197         pdata->tx_desc_count = XGBE_TX_DESC_CNT;
198         if (pdata->tx_desc_count & (pdata->tx_desc_count - 1)) {
199                 dev_err(dev, "tx descriptor count (%d) is not valid\n",
200                         pdata->tx_desc_count);
201                 ret = -EINVAL;
202                 goto err_io;
203         }
204         BUILD_BUG_ON_NOT_POWER_OF_2(XGBE_RX_DESC_CNT);
205         pdata->rx_desc_count = XGBE_RX_DESC_CNT;
206         if (pdata->rx_desc_count & (pdata->rx_desc_count - 1)) {
207                 dev_err(dev, "rx descriptor count (%d) is not valid\n",
208                         pdata->rx_desc_count);
209                 ret = -EINVAL;
210                 goto err_io;
211         }
212
213         /* Obtain the system clock setting */
214         pdata->sysclk = devm_clk_get(dev, XGBE_DMA_CLOCK);
215         if (IS_ERR(pdata->sysclk)) {
216                 dev_err(dev, "dma devm_clk_get failed\n");
217                 ret = PTR_ERR(pdata->sysclk);
218                 goto err_io;
219         }
220
221         /* Obtain the PTP clock setting */
222         pdata->ptpclk = devm_clk_get(dev, XGBE_PTP_CLOCK);
223         if (IS_ERR(pdata->ptpclk)) {
224                 dev_err(dev, "ptp devm_clk_get failed\n");
225                 ret = PTR_ERR(pdata->ptpclk);
226                 goto err_io;
227         }
228
229         /* Obtain the mmio areas for the device */
230         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
231         pdata->xgmac_regs = devm_ioremap_resource(dev, res);
232         if (IS_ERR(pdata->xgmac_regs)) {
233                 dev_err(dev, "xgmac ioremap failed\n");
234                 ret = PTR_ERR(pdata->xgmac_regs);
235                 goto err_io;
236         }
237         DBGPR("  xgmac_regs = %p\n", pdata->xgmac_regs);
238
239         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
240         pdata->xpcs_regs = devm_ioremap_resource(dev, res);
241         if (IS_ERR(pdata->xpcs_regs)) {
242                 dev_err(dev, "xpcs ioremap failed\n");
243                 ret = PTR_ERR(pdata->xpcs_regs);
244                 goto err_io;
245         }
246         DBGPR("  xpcs_regs  = %p\n", pdata->xpcs_regs);
247
248         /* Set the DMA mask */
249         if (!dev->dma_mask)
250                 dev->dma_mask = &dev->coherent_dma_mask;
251         ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40));
252         if (ret) {
253                 dev_err(dev, "dma_set_mask_and_coherent failed\n");
254                 goto err_io;
255         }
256
257         if (of_property_read_bool(dev->of_node, "dma-coherent")) {
258                 pdata->axdomain = XGBE_DMA_OS_AXDOMAIN;
259                 pdata->arcache = XGBE_DMA_OS_ARCACHE;
260                 pdata->awcache = XGBE_DMA_OS_AWCACHE;
261         } else {
262                 pdata->axdomain = XGBE_DMA_SYS_AXDOMAIN;
263                 pdata->arcache = XGBE_DMA_SYS_ARCACHE;
264                 pdata->awcache = XGBE_DMA_SYS_AWCACHE;
265         }
266
267         ret = platform_get_irq(pdev, 0);
268         if (ret < 0) {
269                 dev_err(dev, "platform_get_irq failed\n");
270                 goto err_io;
271         }
272         netdev->irq = ret;
273         netdev->base_addr = (unsigned long)pdata->xgmac_regs;
274
275         /* Set all the function pointers */
276         xgbe_init_all_fptrs(pdata);
277         hw_if = &pdata->hw_if;
278         desc_if = &pdata->desc_if;
279
280         /* Issue software reset to device */
281         hw_if->exit(pdata);
282
283         /* Populate the hardware features */
284         xgbe_get_all_hw_features(pdata);
285
286         /* Retrieve the MAC address */
287         mac_addr = of_get_mac_address(dev->of_node);
288         if (!mac_addr) {
289                 dev_err(dev, "invalid mac address for this device\n");
290                 ret = -EINVAL;
291                 goto err_io;
292         }
293         memcpy(netdev->dev_addr, mac_addr, netdev->addr_len);
294
295         /* Retrieve the PHY mode - it must be "xgmii" */
296         pdata->phy_mode = of_get_phy_mode(dev->of_node);
297         if (pdata->phy_mode != PHY_INTERFACE_MODE_XGMII) {
298                 dev_err(dev, "invalid phy-mode specified for this device\n");
299                 ret = -EINVAL;
300                 goto err_io;
301         }
302
303         /* Set default configuration data */
304         xgbe_default_config(pdata);
305
306         /* Calculate the number of Tx and Rx rings to be created
307          *  -Tx (DMA) Channels map 1-to-1 to Tx Queues so set
308          *   the number of Tx queues to the number of Tx channels
309          *   enabled
310          *  -Rx (DMA) Channels do not map 1-to-1 so use the actual
311          *   number of Rx queues
312          */
313         pdata->tx_ring_count = min_t(unsigned int, num_online_cpus(),
314                                      pdata->hw_feat.tx_ch_cnt);
315         pdata->tx_q_count = pdata->tx_ring_count;
316         ret = netif_set_real_num_tx_queues(netdev, pdata->tx_ring_count);
317         if (ret) {
318                 dev_err(dev, "error setting real tx queue count\n");
319                 goto err_io;
320         }
321
322         pdata->rx_ring_count = min_t(unsigned int,
323                                      netif_get_num_default_rss_queues(),
324                                      pdata->hw_feat.rx_ch_cnt);
325         pdata->rx_q_count = pdata->hw_feat.rx_q_cnt;
326         ret = netif_set_real_num_rx_queues(netdev, pdata->rx_ring_count);
327         if (ret) {
328                 dev_err(dev, "error setting real rx queue count\n");
329                 goto err_io;
330         }
331
332         /* Prepare to regsiter with MDIO */
333         pdata->mii_bus_id = kasprintf(GFP_KERNEL, "%s", pdev->name);
334         if (!pdata->mii_bus_id) {
335                 dev_err(dev, "failed to allocate mii bus id\n");
336                 ret = -ENOMEM;
337                 goto err_io;
338         }
339         ret = xgbe_mdio_register(pdata);
340         if (ret)
341                 goto err_bus_id;
342
343         /* Set device operations */
344         netdev->netdev_ops = xgbe_get_netdev_ops();
345         netdev->ethtool_ops = xgbe_get_ethtool_ops();
346 #ifdef CONFIG_AMD_XGBE_DCB
347         netdev->dcbnl_ops = xgbe_get_dcbnl_ops();
348 #endif
349
350         /* Set device features */
351         netdev->hw_features = NETIF_F_SG |
352                               NETIF_F_IP_CSUM |
353                               NETIF_F_IPV6_CSUM |
354                               NETIF_F_RXCSUM |
355                               NETIF_F_TSO |
356                               NETIF_F_TSO6 |
357                               NETIF_F_GRO |
358                               NETIF_F_HW_VLAN_CTAG_RX |
359                               NETIF_F_HW_VLAN_CTAG_TX |
360                               NETIF_F_HW_VLAN_CTAG_FILTER;
361
362         netdev->vlan_features |= NETIF_F_SG |
363                                  NETIF_F_IP_CSUM |
364                                  NETIF_F_IPV6_CSUM |
365                                  NETIF_F_TSO |
366                                  NETIF_F_TSO6;
367
368         netdev->features |= netdev->hw_features;
369         pdata->netdev_features = netdev->features;
370
371         netdev->priv_flags |= IFF_UNICAST_FLT;
372
373         xgbe_init_rx_coalesce(pdata);
374         xgbe_init_tx_coalesce(pdata);
375
376         netif_carrier_off(netdev);
377         ret = register_netdev(netdev);
378         if (ret) {
379                 dev_err(dev, "net device registration failed\n");
380                 goto err_reg_netdev;
381         }
382
383         xgbe_ptp_register(pdata);
384
385         xgbe_debugfs_init(pdata);
386
387         netdev_notice(netdev, "net device enabled\n");
388
389         DBGPR("<-- xgbe_probe\n");
390
391         return 0;
392
393 err_reg_netdev:
394         xgbe_mdio_unregister(pdata);
395
396 err_bus_id:
397         kfree(pdata->mii_bus_id);
398
399 err_io:
400         free_netdev(netdev);
401
402 err_alloc:
403         dev_notice(dev, "net device not enabled\n");
404
405         return ret;
406 }
407
408 static int xgbe_remove(struct platform_device *pdev)
409 {
410         struct net_device *netdev = platform_get_drvdata(pdev);
411         struct xgbe_prv_data *pdata = netdev_priv(netdev);
412
413         DBGPR("-->xgbe_remove\n");
414
415         xgbe_debugfs_exit(pdata);
416
417         xgbe_ptp_unregister(pdata);
418
419         unregister_netdev(netdev);
420
421         xgbe_mdio_unregister(pdata);
422
423         kfree(pdata->mii_bus_id);
424
425         free_netdev(netdev);
426
427         DBGPR("<--xgbe_remove\n");
428
429         return 0;
430 }
431
432 #ifdef CONFIG_PM
433 static int xgbe_suspend(struct device *dev)
434 {
435         struct net_device *netdev = dev_get_drvdata(dev);
436         int ret;
437
438         DBGPR("-->xgbe_suspend\n");
439
440         if (!netif_running(netdev)) {
441                 DBGPR("<--xgbe_dev_suspend\n");
442                 return -EINVAL;
443         }
444
445         ret = xgbe_powerdown(netdev, XGMAC_DRIVER_CONTEXT);
446
447         DBGPR("<--xgbe_suspend\n");
448
449         return ret;
450 }
451
452 static int xgbe_resume(struct device *dev)
453 {
454         struct net_device *netdev = dev_get_drvdata(dev);
455         int ret;
456
457         DBGPR("-->xgbe_resume\n");
458
459         if (!netif_running(netdev)) {
460                 DBGPR("<--xgbe_dev_resume\n");
461                 return -EINVAL;
462         }
463
464         ret = xgbe_powerup(netdev, XGMAC_DRIVER_CONTEXT);
465
466         DBGPR("<--xgbe_resume\n");
467
468         return ret;
469 }
470 #endif /* CONFIG_PM */
471
472 static const struct of_device_id xgbe_of_match[] = {
473         { .compatible = "amd,xgbe-seattle-v1a", },
474         {},
475 };
476
477 MODULE_DEVICE_TABLE(of, xgbe_of_match);
478 static SIMPLE_DEV_PM_OPS(xgbe_pm_ops, xgbe_suspend, xgbe_resume);
479
480 static struct platform_driver xgbe_driver = {
481         .driver = {
482                 .name = "amd-xgbe",
483                 .of_match_table = xgbe_of_match,
484                 .pm = &xgbe_pm_ops,
485         },
486         .probe = xgbe_probe,
487         .remove = xgbe_remove,
488 };
489
490 module_platform_driver(xgbe_driver);