blob: fe77945bc49e52b9775e6754cd763125fa45a610 [file] [log] [blame]
Lendacky, Thomas47f164d2016-11-10 17:09:55 -06001/*
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) 2016 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) 2016 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/pci.h>
120#include <linux/log2.h>
121
122#include "xgbe.h"
123#include "xgbe-common.h"
124
125static int xgbe_config_msi(struct xgbe_prv_data *pdata)
126{
127 unsigned int msi_count;
128 unsigned int i, j;
129 int ret;
130
131 msi_count = XGBE_MSIX_BASE_COUNT;
132 msi_count += max(pdata->rx_ring_count,
133 pdata->tx_ring_count);
134 msi_count = roundup_pow_of_two(msi_count);
135
136 ret = pci_enable_msi_exact(pdata->pcidev, msi_count);
137 if (ret < 0) {
138 dev_info(pdata->dev, "MSI request for %u interrupts failed\n",
139 msi_count);
140
141 ret = pci_enable_msi(pdata->pcidev);
142 if (ret < 0) {
143 dev_info(pdata->dev, "MSI enablement failed\n");
144 return ret;
145 }
146
147 msi_count = 1;
148 }
149
150 pdata->irq_count = msi_count;
151
152 pdata->dev_irq = pdata->pcidev->irq;
153
154 if (msi_count > 1) {
155 pdata->ecc_irq = pdata->pcidev->irq + 1;
156 pdata->i2c_irq = pdata->pcidev->irq + 2;
157 pdata->an_irq = pdata->pcidev->irq + 3;
158
159 for (i = XGBE_MSIX_BASE_COUNT, j = 0;
160 (i < msi_count) && (j < XGBE_MAX_DMA_CHANNELS);
161 i++, j++)
162 pdata->channel_irq[j] = pdata->pcidev->irq + i;
163 pdata->channel_irq_count = j;
164
165 pdata->per_channel_irq = 1;
166 } else {
167 pdata->ecc_irq = pdata->pcidev->irq;
168 pdata->i2c_irq = pdata->pcidev->irq;
169 pdata->an_irq = pdata->pcidev->irq;
170 }
171
172 if (netif_msg_probe(pdata))
173 dev_dbg(pdata->dev, "MSI interrupts enabled\n");
174
175 return 0;
176}
177
178static int xgbe_config_msix(struct xgbe_prv_data *pdata)
179{
180 unsigned int msix_count;
181 unsigned int i, j;
182 int ret;
183
184 msix_count = XGBE_MSIX_BASE_COUNT;
185 msix_count += max(pdata->rx_ring_count,
186 pdata->tx_ring_count);
187
188 pdata->msix_entries = devm_kcalloc(pdata->dev, msix_count,
189 sizeof(struct msix_entry),
190 GFP_KERNEL);
191 if (!pdata->msix_entries)
192 return -ENOMEM;
193
194 for (i = 0; i < msix_count; i++)
195 pdata->msix_entries[i].entry = i;
196
197 ret = pci_enable_msix_range(pdata->pcidev, pdata->msix_entries,
198 XGBE_MSIX_MIN_COUNT, msix_count);
199 if (ret < 0) {
200 dev_info(pdata->dev, "MSI-X enablement failed\n");
201 devm_kfree(pdata->dev, pdata->msix_entries);
202 pdata->msix_entries = NULL;
203 return ret;
204 }
205
206 pdata->irq_count = ret;
207
208 pdata->dev_irq = pdata->msix_entries[0].vector;
209 pdata->ecc_irq = pdata->msix_entries[1].vector;
210 pdata->i2c_irq = pdata->msix_entries[2].vector;
211 pdata->an_irq = pdata->msix_entries[3].vector;
212
213 for (i = XGBE_MSIX_BASE_COUNT, j = 0; i < ret; i++, j++)
214 pdata->channel_irq[j] = pdata->msix_entries[i].vector;
215 pdata->channel_irq_count = j;
216
217 pdata->per_channel_irq = 1;
218
219 if (netif_msg_probe(pdata))
220 dev_dbg(pdata->dev, "MSI-X interrupts enabled\n");
221
222 return 0;
223}
224
225static int xgbe_config_irqs(struct xgbe_prv_data *pdata)
226{
227 int ret;
228
229 ret = xgbe_config_msix(pdata);
230 if (!ret)
231 goto out;
232
233 ret = xgbe_config_msi(pdata);
234 if (!ret)
235 goto out;
236
237 pdata->irq_count = 1;
238 pdata->irq_shared = 1;
239
240 pdata->dev_irq = pdata->pcidev->irq;
241 pdata->ecc_irq = pdata->pcidev->irq;
242 pdata->i2c_irq = pdata->pcidev->irq;
243 pdata->an_irq = pdata->pcidev->irq;
244
245out:
246 if (netif_msg_probe(pdata)) {
247 unsigned int i;
248
249 dev_dbg(pdata->dev, " dev irq=%d\n", pdata->dev_irq);
250 dev_dbg(pdata->dev, " ecc irq=%d\n", pdata->ecc_irq);
251 dev_dbg(pdata->dev, " i2c irq=%d\n", pdata->i2c_irq);
252 dev_dbg(pdata->dev, " an irq=%d\n", pdata->an_irq);
253 for (i = 0; i < pdata->channel_irq_count; i++)
254 dev_dbg(pdata->dev, " dma%u irq=%d\n",
255 i, pdata->channel_irq[i]);
256 }
257
258 return 0;
259}
260
261static int xgbe_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
262{
263 struct xgbe_prv_data *pdata;
264 struct device *dev = &pdev->dev;
265 void __iomem * const *iomap_table;
266 unsigned int ma_lo, ma_hi;
267 unsigned int reg;
268 int bar_mask;
269 int ret;
270
271 pdata = xgbe_alloc_pdata(dev);
272 if (IS_ERR(pdata)) {
273 ret = PTR_ERR(pdata);
274 goto err_alloc;
275 }
276
277 pdata->pcidev = pdev;
278 pci_set_drvdata(pdev, pdata);
279
280 /* Get the version data */
281 pdata->vdata = (struct xgbe_version_data *)id->driver_data;
282
283 ret = pcim_enable_device(pdev);
284 if (ret) {
285 dev_err(dev, "pcim_enable_device failed\n");
286 goto err_pci_enable;
287 }
288
289 /* Obtain the mmio areas for the device */
290 bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
291 ret = pcim_iomap_regions(pdev, bar_mask, XGBE_DRV_NAME);
292 if (ret) {
293 dev_err(dev, "pcim_iomap_regions failed\n");
294 goto err_pci_enable;
295 }
296
297 iomap_table = pcim_iomap_table(pdev);
298 if (!iomap_table) {
299 dev_err(dev, "pcim_iomap_table failed\n");
300 ret = -ENOMEM;
301 goto err_pci_enable;
302 }
303
304 pdata->xgmac_regs = iomap_table[XGBE_XGMAC_BAR];
305 if (!pdata->xgmac_regs) {
306 dev_err(dev, "xgmac ioremap failed\n");
307 ret = -ENOMEM;
308 goto err_pci_enable;
309 }
310 pdata->xprop_regs = pdata->xgmac_regs + XGBE_MAC_PROP_OFFSET;
311 pdata->xi2c_regs = pdata->xgmac_regs + XGBE_I2C_CTRL_OFFSET;
312 if (netif_msg_probe(pdata)) {
313 dev_dbg(dev, "xgmac_regs = %p\n", pdata->xgmac_regs);
314 dev_dbg(dev, "xprop_regs = %p\n", pdata->xprop_regs);
315 dev_dbg(dev, "xi2c_regs = %p\n", pdata->xi2c_regs);
316 }
317
318 pdata->xpcs_regs = iomap_table[XGBE_XPCS_BAR];
319 if (!pdata->xpcs_regs) {
320 dev_err(dev, "xpcs ioremap failed\n");
321 ret = -ENOMEM;
322 goto err_pci_enable;
323 }
324 if (netif_msg_probe(pdata))
325 dev_dbg(dev, "xpcs_regs = %p\n", pdata->xpcs_regs);
326
327 /* Configure the PCS indirect addressing support */
328 reg = XPCS32_IOREAD(pdata, PCS_V2_WINDOW_DEF);
329 pdata->xpcs_window = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, OFFSET);
330 pdata->xpcs_window <<= 6;
331 pdata->xpcs_window_size = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, SIZE);
332 pdata->xpcs_window_size = 1 << (pdata->xpcs_window_size + 7);
333 pdata->xpcs_window_mask = pdata->xpcs_window_size - 1;
334 if (netif_msg_probe(pdata)) {
335 dev_dbg(dev, "xpcs window = %#010x\n",
336 pdata->xpcs_window);
337 dev_dbg(dev, "xpcs window size = %#010x\n",
338 pdata->xpcs_window_size);
339 dev_dbg(dev, "xpcs window mask = %#010x\n",
340 pdata->xpcs_window_mask);
341 }
342
343 pci_set_master(pdev);
344
345 /* Enable all interrupts in the hardware */
346 XP_IOWRITE(pdata, XP_INT_EN, 0x1fffff);
347
348 /* Retrieve the MAC address */
349 ma_lo = XP_IOREAD(pdata, XP_MAC_ADDR_LO);
350 ma_hi = XP_IOREAD(pdata, XP_MAC_ADDR_HI);
351 pdata->mac_addr[0] = ma_lo & 0xff;
352 pdata->mac_addr[1] = (ma_lo >> 8) & 0xff;
353 pdata->mac_addr[2] = (ma_lo >> 16) & 0xff;
354 pdata->mac_addr[3] = (ma_lo >> 24) & 0xff;
355 pdata->mac_addr[4] = ma_hi & 0xff;
356 pdata->mac_addr[5] = (ma_hi >> 8) & 0xff;
357 if (!XP_GET_BITS(ma_hi, XP_MAC_ADDR_HI, VALID) ||
358 !is_valid_ether_addr(pdata->mac_addr)) {
359 dev_err(dev, "invalid mac address\n");
360 ret = -EINVAL;
361 goto err_pci_enable;
362 }
363
364 /* Clock settings */
365 pdata->sysclk_rate = XGBE_V2_DMA_CLOCK_FREQ;
366 pdata->ptpclk_rate = XGBE_V2_PTP_CLOCK_FREQ;
367
368 /* Set the DMA coherency values */
369 pdata->coherent = 1;
370 pdata->axdomain = XGBE_DMA_OS_AXDOMAIN;
371 pdata->arcache = XGBE_DMA_OS_ARCACHE;
372 pdata->awcache = XGBE_DMA_OS_AWCACHE;
373
374 /* Set the maximum channels and queues */
375 reg = XP_IOREAD(pdata, XP_PROP_1);
376 pdata->tx_max_channel_count = XP_GET_BITS(reg, XP_PROP_1, MAX_TX_DMA);
377 pdata->rx_max_channel_count = XP_GET_BITS(reg, XP_PROP_1, MAX_RX_DMA);
378 pdata->tx_max_q_count = XP_GET_BITS(reg, XP_PROP_1, MAX_TX_QUEUES);
379 pdata->rx_max_q_count = XP_GET_BITS(reg, XP_PROP_1, MAX_RX_QUEUES);
380 if (netif_msg_probe(pdata)) {
381 dev_dbg(dev, "max tx/rx channel count = %u/%u\n",
382 pdata->tx_max_channel_count,
383 pdata->tx_max_channel_count);
384 dev_dbg(dev, "max tx/rx hw queue count = %u/%u\n",
385 pdata->tx_max_q_count, pdata->rx_max_q_count);
386 }
387
388 /* Set the hardware channel and queue counts */
389 xgbe_set_counts(pdata);
390
391 /* Set the maximum fifo amounts */
392 reg = XP_IOREAD(pdata, XP_PROP_2);
393 pdata->tx_max_fifo_size = XP_GET_BITS(reg, XP_PROP_2, TX_FIFO_SIZE);
394 pdata->tx_max_fifo_size *= 16384;
395 pdata->tx_max_fifo_size = min(pdata->tx_max_fifo_size,
396 pdata->vdata->tx_max_fifo_size);
397 pdata->rx_max_fifo_size = XP_GET_BITS(reg, XP_PROP_2, RX_FIFO_SIZE);
398 pdata->rx_max_fifo_size *= 16384;
399 pdata->rx_max_fifo_size = min(pdata->rx_max_fifo_size,
400 pdata->vdata->rx_max_fifo_size);
401 if (netif_msg_probe(pdata))
402 dev_dbg(dev, "max tx/rx max fifo size = %u/%u\n",
403 pdata->tx_max_fifo_size, pdata->rx_max_fifo_size);
404
405 /* Configure interrupt support */
406 ret = xgbe_config_irqs(pdata);
407 if (ret)
408 goto err_pci_enable;
409
410 /* Configure the netdev resource */
411 ret = xgbe_config_netdev(pdata);
412 if (ret)
413 goto err_pci_enable;
414
415 netdev_notice(pdata->netdev, "net device enabled\n");
416
417 return 0;
418
419err_pci_enable:
420 xgbe_free_pdata(pdata);
421
422err_alloc:
423 dev_notice(dev, "net device not enabled\n");
424
425 return ret;
426}
427
428static void xgbe_pci_remove(struct pci_dev *pdev)
429{
430 struct xgbe_prv_data *pdata = pci_get_drvdata(pdev);
431
432 xgbe_deconfig_netdev(pdata);
433
434 xgbe_free_pdata(pdata);
435}
436
437#ifdef CONFIG_PM
438static int xgbe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
439{
440 struct xgbe_prv_data *pdata = pci_get_drvdata(pdev);
441 struct net_device *netdev = pdata->netdev;
442 int ret = 0;
443
444 if (netif_running(netdev))
445 ret = xgbe_powerdown(netdev, XGMAC_DRIVER_CONTEXT);
446
447 pdata->lpm_ctrl = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
448 pdata->lpm_ctrl |= MDIO_CTRL1_LPOWER;
449 XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, pdata->lpm_ctrl);
450
451 return ret;
452}
453
454static int xgbe_pci_resume(struct pci_dev *pdev)
455{
456 struct xgbe_prv_data *pdata = pci_get_drvdata(pdev);
457 struct net_device *netdev = pdata->netdev;
458 int ret = 0;
459
460 pdata->lpm_ctrl &= ~MDIO_CTRL1_LPOWER;
461 XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, pdata->lpm_ctrl);
462
463 if (netif_running(netdev)) {
464 ret = xgbe_powerup(netdev, XGMAC_DRIVER_CONTEXT);
465
466 /* Schedule a restart in case the link or phy state changed
467 * while we were powered down.
468 */
469 schedule_work(&pdata->restart_work);
470 }
471
472 return ret;
473}
474#endif /* CONFIG_PM */
475
476static const struct xgbe_version_data xgbe_v2a = {
477 .init_function_ptrs_phy_impl = xgbe_init_function_ptrs_phy_v2,
478 .xpcs_access = XGBE_XPCS_ACCESS_V2,
479 .mmc_64bit = 1,
480 .tx_max_fifo_size = 229376,
481 .rx_max_fifo_size = 229376,
482 .tx_tstamp_workaround = 1,
483};
484
485static const struct xgbe_version_data xgbe_v2b = {
486 .init_function_ptrs_phy_impl = xgbe_init_function_ptrs_phy_v2,
487 .xpcs_access = XGBE_XPCS_ACCESS_V2,
488 .mmc_64bit = 1,
489 .tx_max_fifo_size = 65536,
490 .rx_max_fifo_size = 65536,
491 .tx_tstamp_workaround = 1,
492};
493
494static const struct pci_device_id xgbe_pci_table[] = {
495 { PCI_VDEVICE(AMD, 0x1458),
496 .driver_data = (kernel_ulong_t)&xgbe_v2a },
497 { PCI_VDEVICE(AMD, 0x1459),
498 .driver_data = (kernel_ulong_t)&xgbe_v2b },
499 /* Last entry must be zero */
500 { 0, }
501};
502MODULE_DEVICE_TABLE(pci, xgbe_pci_table);
503
504static struct pci_driver xgbe_driver = {
505 .name = XGBE_DRV_NAME,
506 .id_table = xgbe_pci_table,
507 .probe = xgbe_pci_probe,
508 .remove = xgbe_pci_remove,
509#ifdef CONFIG_PM
510 .suspend = xgbe_pci_suspend,
511 .resume = xgbe_pci_resume,
512#endif
513};
514
515int xgbe_pci_init(void)
516{
517 return pci_register_driver(&xgbe_driver);
518}
519
520void xgbe_pci_exit(void)
521{
522 pci_unregister_driver(&xgbe_driver);
523}