2 * Copyright (C) 2015 Cavium, Inc.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License
6 * as published by the Free Software Foundation.
9 #include <linux/acpi.h>
10 #include <linux/module.h>
11 #include <linux/interrupt.h>
12 #include <linux/pci.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/phy.h>
17 #include <linux/of_mdio.h>
18 #include <linux/of_net.h>
22 #include "thunder_bgx.h"
24 #define DRV_NAME "thunder_bgx"
25 #define DRV_VERSION "1.0"
27 /* RX_DMAC_CTL configuration */
29 MCAST_MODE_REJECT
= 0x0,
30 MCAST_MODE_ACCEPT
= 0x1,
31 MCAST_MODE_CAM_FILTER
= 0x2,
35 #define BCAST_ACCEPT BIT(0)
36 #define CAM_ACCEPT BIT(3)
37 #define MCAST_MODE_MASK 0x3
38 #define BGX_MCAST_MODE(x) (x << 1)
47 /* actual number of DMACs configured */
49 /* overal number of possible DMACs could be configured per LMAC */
51 struct dmac_map
*dmacs
; /* DMAC:VFs tracking filter array */
58 int lmacid
; /* ID within BGX */
59 int lmacid_bd
; /* ID on board */
60 struct net_device netdev
;
61 struct phy_device
*phydev
;
62 unsigned int last_duplex
;
63 unsigned int last_link
;
64 unsigned int last_speed
;
66 struct delayed_work dwork
;
67 struct workqueue_struct
*check_link
;
72 struct lmac lmac
[MAX_LMAC_PER_BGX
];
76 void __iomem
*reg_base
;
82 static struct bgx
*bgx_vnic
[MAX_BGX_THUNDER
];
83 static int lmac_count
; /* Total no of LMACs in system */
85 static int bgx_xaui_check_link(struct lmac
*lmac
);
87 /* Supported devices */
88 static const struct pci_device_id bgx_id_table
[] = {
89 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM
, PCI_DEVICE_ID_THUNDER_BGX
) },
90 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM
, PCI_DEVICE_ID_THUNDER_RGX
) },
91 { 0, } /* end of table */
94 MODULE_AUTHOR("Cavium Inc");
95 MODULE_DESCRIPTION("Cavium Thunder BGX/MAC Driver");
96 MODULE_LICENSE("GPL v2");
97 MODULE_VERSION(DRV_VERSION
);
98 MODULE_DEVICE_TABLE(pci
, bgx_id_table
);
100 /* The Cavium ThunderX network controller can *only* be found in SoCs
101 * containing the ThunderX ARM64 CPU implementation. All accesses to the device
102 * registers on this platform are implicitly strongly ordered with respect
103 * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
104 * with no memory barriers in this driver. The readq()/writeq() functions add
105 * explicit ordering operation which in this case are redundant, and only
109 /* Register read/write APIs */
110 static u64
bgx_reg_read(struct bgx
*bgx
, u8 lmac
, u64 offset
)
112 void __iomem
*addr
= bgx
->reg_base
+ ((u32
)lmac
<< 20) + offset
;
114 return readq_relaxed(addr
);
117 static void bgx_reg_write(struct bgx
*bgx
, u8 lmac
, u64 offset
, u64 val
)
119 void __iomem
*addr
= bgx
->reg_base
+ ((u32
)lmac
<< 20) + offset
;
121 writeq_relaxed(val
, addr
);
124 static void bgx_reg_modify(struct bgx
*bgx
, u8 lmac
, u64 offset
, u64 val
)
126 void __iomem
*addr
= bgx
->reg_base
+ ((u32
)lmac
<< 20) + offset
;
128 writeq_relaxed(val
| readq_relaxed(addr
), addr
);
131 static int bgx_poll_reg(struct bgx
*bgx
, u8 lmac
, u64 reg
, u64 mask
, bool zero
)
137 reg_val
= bgx_reg_read(bgx
, lmac
, reg
);
138 if (zero
&& !(reg_val
& mask
))
140 if (!zero
&& (reg_val
& mask
))
142 usleep_range(1000, 2000);
148 static int max_bgx_per_node
;
149 static void set_max_bgx_per_node(struct pci_dev
*pdev
)
153 if (max_bgx_per_node
)
156 pci_read_config_word(pdev
, PCI_SUBSYSTEM_ID
, &sdevid
);
158 case PCI_SUBSYS_DEVID_81XX_BGX
:
159 case PCI_SUBSYS_DEVID_81XX_RGX
:
160 max_bgx_per_node
= MAX_BGX_PER_CN81XX
;
162 case PCI_SUBSYS_DEVID_83XX_BGX
:
163 max_bgx_per_node
= MAX_BGX_PER_CN83XX
;
165 case PCI_SUBSYS_DEVID_88XX_BGX
:
167 max_bgx_per_node
= MAX_BGX_PER_CN88XX
;
172 static struct bgx
*get_bgx(int node
, int bgx_idx
)
174 int idx
= (node
* max_bgx_per_node
) + bgx_idx
;
176 return bgx_vnic
[idx
];
179 /* Return number of BGX present in HW */
180 unsigned bgx_get_map(int node
)
185 for (i
= 0; i
< max_bgx_per_node
; i
++) {
186 if (bgx_vnic
[(node
* max_bgx_per_node
) + i
])
192 EXPORT_SYMBOL(bgx_get_map
);
194 /* Return number of LMAC configured for this BGX */
195 int bgx_get_lmac_count(int node
, int bgx_idx
)
199 bgx
= get_bgx(node
, bgx_idx
);
201 return bgx
->lmac_count
;
205 EXPORT_SYMBOL(bgx_get_lmac_count
);
207 /* Returns the current link status of LMAC */
208 void bgx_get_lmac_link_state(int node
, int bgx_idx
, int lmacid
, void *status
)
210 struct bgx_link_status
*link
= (struct bgx_link_status
*)status
;
214 bgx
= get_bgx(node
, bgx_idx
);
218 lmac
= &bgx
->lmac
[lmacid
];
219 link
->mac_type
= lmac
->lmac_type
;
220 link
->link_up
= lmac
->link_up
;
221 link
->duplex
= lmac
->last_duplex
;
222 link
->speed
= lmac
->last_speed
;
224 EXPORT_SYMBOL(bgx_get_lmac_link_state
);
226 const u8
*bgx_get_lmac_mac(int node
, int bgx_idx
, int lmacid
)
228 struct bgx
*bgx
= get_bgx(node
, bgx_idx
);
231 return bgx
->lmac
[lmacid
].mac
;
235 EXPORT_SYMBOL(bgx_get_lmac_mac
);
237 void bgx_set_lmac_mac(int node
, int bgx_idx
, int lmacid
, const u8
*mac
)
239 struct bgx
*bgx
= get_bgx(node
, bgx_idx
);
244 ether_addr_copy(bgx
->lmac
[lmacid
].mac
, mac
);
246 EXPORT_SYMBOL(bgx_set_lmac_mac
);
248 static void bgx_flush_dmac_cam_filter(struct bgx
*bgx
, int lmacid
)
250 struct lmac
*lmac
= NULL
;
253 lmac
= &bgx
->lmac
[lmacid
];
254 /* reset CAM filters */
255 for (idx
= 0; idx
< lmac
->dmacs_count
; idx
++)
256 bgx_reg_write(bgx
, 0, BGX_CMR_RX_DMACX_CAM
+
257 ((lmacid
* lmac
->dmacs_count
) + idx
) *
261 static void bgx_lmac_remove_filters(struct lmac
*lmac
, u8 vf_id
)
268 /* We've got reset filters request from some of attached VF, while the
269 * others might want to keep their configuration. So in this case lets
270 * iterate over all of configured filters and decrease number of
271 * referencies. if some addresses get zero refs remove them from list
273 for (i
= lmac
->dmacs_cfg
- 1; i
>= 0; i
--) {
274 lmac
->dmacs
[i
].vf_map
&= ~BIT_ULL(vf_id
);
275 if (!lmac
->dmacs
[i
].vf_map
) {
277 lmac
->dmacs
[i
].dmac
= 0;
278 lmac
->dmacs
[i
].vf_map
= 0;
283 static int bgx_lmac_save_filter(struct lmac
*lmac
, u64 dmac
, u8 vf_id
)
290 /* At the same time we could have several VFs 'attached' to some
291 * particular LMAC, and each VF is represented as network interface
292 * for kernel. So from user perspective it should be possible to
293 * manipulate with its' (VF) receive modes. However from PF
294 * driver perspective we need to keep track of filter configurations
295 * for different VFs to prevent filter values dupes
297 for (i
= 0; i
< lmac
->dmacs_cfg
; i
++) {
298 if (lmac
->dmacs
[i
].dmac
== dmac
) {
299 lmac
->dmacs
[i
].vf_map
|= BIT_ULL(vf_id
);
304 if (!(lmac
->dmacs_cfg
< lmac
->dmacs_count
))
307 /* keep it for further tracking */
308 lmac
->dmacs
[lmac
->dmacs_cfg
].dmac
= dmac
;
309 lmac
->dmacs
[lmac
->dmacs_cfg
].vf_map
= BIT_ULL(vf_id
);
314 static int bgx_set_dmac_cam_filter_mac(struct bgx
*bgx
, int lmacid
,
315 u64 cam_dmac
, u8 idx
)
317 struct lmac
*lmac
= NULL
;
320 /* skip zero addresses as meaningless */
321 if (!cam_dmac
|| !bgx
)
324 lmac
= &bgx
->lmac
[lmacid
];
326 /* configure DCAM filtering for designated LMAC */
327 cfg
= RX_DMACX_CAM_LMACID(lmacid
& LMAC_ID_MASK
) |
328 RX_DMACX_CAM_EN
| cam_dmac
;
329 bgx_reg_write(bgx
, 0, BGX_CMR_RX_DMACX_CAM
+
330 ((lmacid
* lmac
->dmacs_count
) + idx
) * sizeof(u64
), cfg
);
334 void bgx_set_dmac_cam_filter(int node
, int bgx_idx
, int lmacid
,
335 u64 cam_dmac
, u8 vf_id
)
337 struct bgx
*bgx
= get_bgx(node
, bgx_idx
);
338 struct lmac
*lmac
= NULL
;
343 lmac
= &bgx
->lmac
[lmacid
];
346 cam_dmac
= ether_addr_to_u64(lmac
->mac
);
348 /* since we might have several VFs attached to particular LMAC
349 * and kernel could call mcast config for each of them with the
350 * same MAC, check if requested MAC is already in filtering list and
351 * updare/prepare list of MACs to be applied later to HW filters
353 bgx_lmac_save_filter(lmac
, cam_dmac
, vf_id
);
355 EXPORT_SYMBOL(bgx_set_dmac_cam_filter
);
357 void bgx_set_xcast_mode(int node
, int bgx_idx
, int lmacid
, u8 mode
)
359 struct bgx
*bgx
= get_bgx(node
, bgx_idx
);
360 struct lmac
*lmac
= NULL
;
367 lmac
= &bgx
->lmac
[lmacid
];
369 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_CMRX_RX_DMAC_CTL
);
370 if (mode
& BGX_XCAST_BCAST_ACCEPT
)
373 cfg
&= ~BCAST_ACCEPT
;
375 /* disable all MCASTs and DMAC filtering */
376 cfg
&= ~(CAM_ACCEPT
| BGX_MCAST_MODE(MCAST_MODE_MASK
));
378 /* check requested bits and set filtergin mode appropriately */
379 if (mode
& (BGX_XCAST_MCAST_ACCEPT
)) {
380 cfg
|= (BGX_MCAST_MODE(MCAST_MODE_ACCEPT
));
381 } else if (mode
& BGX_XCAST_MCAST_FILTER
) {
382 cfg
|= (BGX_MCAST_MODE(MCAST_MODE_CAM_FILTER
) | CAM_ACCEPT
);
383 for (i
= 0; i
< lmac
->dmacs_cfg
; i
++)
384 bgx_set_dmac_cam_filter_mac(bgx
, lmacid
,
385 lmac
->dmacs
[i
].dmac
, i
);
387 bgx_reg_write(bgx
, lmacid
, BGX_CMRX_RX_DMAC_CTL
, cfg
);
389 EXPORT_SYMBOL(bgx_set_xcast_mode
);
391 void bgx_reset_xcast_mode(int node
, int bgx_idx
, int lmacid
, u8 vf_id
)
393 struct bgx
*bgx
= get_bgx(node
, bgx_idx
);
398 bgx_lmac_remove_filters(&bgx
->lmac
[lmacid
], vf_id
);
399 bgx_flush_dmac_cam_filter(bgx
, lmacid
);
400 bgx_set_xcast_mode(node
, bgx_idx
, lmacid
,
401 (BGX_XCAST_BCAST_ACCEPT
| BGX_XCAST_MCAST_ACCEPT
));
403 EXPORT_SYMBOL(bgx_reset_xcast_mode
);
405 void bgx_lmac_rx_tx_enable(int node
, int bgx_idx
, int lmacid
, bool enable
)
407 struct bgx
*bgx
= get_bgx(node
, bgx_idx
);
413 lmac
= &bgx
->lmac
[lmacid
];
415 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_CMRX_CFG
);
417 cfg
|= CMR_PKT_RX_EN
| CMR_PKT_TX_EN
;
419 /* enable TX FIFO Underflow interrupt */
420 bgx_reg_modify(bgx
, lmacid
, BGX_GMP_GMI_TXX_INT_ENA_W1S
,
423 cfg
&= ~(CMR_PKT_RX_EN
| CMR_PKT_TX_EN
);
425 /* Disable TX FIFO Underflow interrupt */
426 bgx_reg_modify(bgx
, lmacid
, BGX_GMP_GMI_TXX_INT_ENA_W1C
,
429 bgx_reg_write(bgx
, lmacid
, BGX_CMRX_CFG
, cfg
);
432 xcv_setup_link(enable
? lmac
->link_up
: 0, lmac
->last_speed
);
434 EXPORT_SYMBOL(bgx_lmac_rx_tx_enable
);
436 /* Enables or disables timestamp insertion by BGX for Rx packets */
437 void bgx_config_timestamping(int node
, int bgx_idx
, int lmacid
, bool enable
)
439 struct bgx
*bgx
= get_bgx(node
, bgx_idx
);
446 lmac
= &bgx
->lmac
[lmacid
];
448 if (lmac
->lmac_type
== BGX_MODE_SGMII
||
449 lmac
->lmac_type
== BGX_MODE_QSGMII
||
450 lmac
->lmac_type
== BGX_MODE_RGMII
)
451 csr_offset
= BGX_GMP_GMI_RXX_FRM_CTL
;
453 csr_offset
= BGX_SMUX_RX_FRM_CTL
;
455 cfg
= bgx_reg_read(bgx
, lmacid
, csr_offset
);
458 cfg
|= BGX_PKT_RX_PTP_EN
;
460 cfg
&= ~BGX_PKT_RX_PTP_EN
;
461 bgx_reg_write(bgx
, lmacid
, csr_offset
, cfg
);
463 EXPORT_SYMBOL(bgx_config_timestamping
);
465 void bgx_lmac_get_pfc(int node
, int bgx_idx
, int lmacid
, void *pause
)
467 struct pfc
*pfc
= (struct pfc
*)pause
;
468 struct bgx
*bgx
= get_bgx(node
, bgx_idx
);
474 lmac
= &bgx
->lmac
[lmacid
];
478 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SMUX_CBFC_CTL
);
479 pfc
->fc_rx
= cfg
& RX_EN
;
480 pfc
->fc_tx
= cfg
& TX_EN
;
483 EXPORT_SYMBOL(bgx_lmac_get_pfc
);
485 void bgx_lmac_set_pfc(int node
, int bgx_idx
, int lmacid
, void *pause
)
487 struct pfc
*pfc
= (struct pfc
*)pause
;
488 struct bgx
*bgx
= get_bgx(node
, bgx_idx
);
494 lmac
= &bgx
->lmac
[lmacid
];
498 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SMUX_CBFC_CTL
);
499 cfg
&= ~(RX_EN
| TX_EN
);
500 cfg
|= (pfc
->fc_rx
? RX_EN
: 0x00);
501 cfg
|= (pfc
->fc_tx
? TX_EN
: 0x00);
502 bgx_reg_write(bgx
, lmacid
, BGX_SMUX_CBFC_CTL
, cfg
);
504 EXPORT_SYMBOL(bgx_lmac_set_pfc
);
506 static void bgx_sgmii_change_link_state(struct lmac
*lmac
)
508 struct bgx
*bgx
= lmac
->bgx
;
514 cmr_cfg
= bgx_reg_read(bgx
, lmac
->lmacid
, BGX_CMRX_CFG
);
515 tx_en
= cmr_cfg
& CMR_PKT_TX_EN
;
516 rx_en
= cmr_cfg
& CMR_PKT_RX_EN
;
517 cmr_cfg
&= ~(CMR_PKT_RX_EN
| CMR_PKT_TX_EN
);
518 bgx_reg_write(bgx
, lmac
->lmacid
, BGX_CMRX_CFG
, cmr_cfg
);
520 /* Wait for BGX RX to be idle */
521 if (bgx_poll_reg(bgx
, lmac
->lmacid
, BGX_GMP_GMI_PRTX_CFG
,
522 GMI_PORT_CFG_RX_IDLE
, false)) {
523 dev_err(&bgx
->pdev
->dev
, "BGX%d LMAC%d GMI RX not idle\n",
524 bgx
->bgx_id
, lmac
->lmacid
);
528 /* Wait for BGX TX to be idle */
529 if (bgx_poll_reg(bgx
, lmac
->lmacid
, BGX_GMP_GMI_PRTX_CFG
,
530 GMI_PORT_CFG_TX_IDLE
, false)) {
531 dev_err(&bgx
->pdev
->dev
, "BGX%d LMAC%d GMI TX not idle\n",
532 bgx
->bgx_id
, lmac
->lmacid
);
536 port_cfg
= bgx_reg_read(bgx
, lmac
->lmacid
, BGX_GMP_GMI_PRTX_CFG
);
537 misc_ctl
= bgx_reg_read(bgx
, lmac
->lmacid
, BGX_GMP_PCS_MISCX_CTL
);
540 misc_ctl
&= ~PCS_MISC_CTL_GMX_ENO
;
541 port_cfg
&= ~GMI_PORT_CFG_DUPLEX
;
542 port_cfg
|= (lmac
->last_duplex
<< 2);
544 misc_ctl
|= PCS_MISC_CTL_GMX_ENO
;
547 switch (lmac
->last_speed
) {
549 port_cfg
&= ~GMI_PORT_CFG_SPEED
; /* speed 0 */
550 port_cfg
|= GMI_PORT_CFG_SPEED_MSB
; /* speed_msb 1 */
551 port_cfg
&= ~GMI_PORT_CFG_SLOT_TIME
; /* slottime 0 */
552 misc_ctl
&= ~PCS_MISC_CTL_SAMP_PT_MASK
;
553 misc_ctl
|= 50; /* samp_pt */
554 bgx_reg_write(bgx
, lmac
->lmacid
, BGX_GMP_GMI_TXX_SLOT
, 64);
555 bgx_reg_write(bgx
, lmac
->lmacid
, BGX_GMP_GMI_TXX_BURST
, 0);
558 port_cfg
&= ~GMI_PORT_CFG_SPEED
; /* speed 0 */
559 port_cfg
&= ~GMI_PORT_CFG_SPEED_MSB
; /* speed_msb 0 */
560 port_cfg
&= ~GMI_PORT_CFG_SLOT_TIME
; /* slottime 0 */
561 misc_ctl
&= ~PCS_MISC_CTL_SAMP_PT_MASK
;
562 misc_ctl
|= 5; /* samp_pt */
563 bgx_reg_write(bgx
, lmac
->lmacid
, BGX_GMP_GMI_TXX_SLOT
, 64);
564 bgx_reg_write(bgx
, lmac
->lmacid
, BGX_GMP_GMI_TXX_BURST
, 0);
567 port_cfg
|= GMI_PORT_CFG_SPEED
; /* speed 1 */
568 port_cfg
&= ~GMI_PORT_CFG_SPEED_MSB
; /* speed_msb 0 */
569 port_cfg
|= GMI_PORT_CFG_SLOT_TIME
; /* slottime 1 */
570 misc_ctl
&= ~PCS_MISC_CTL_SAMP_PT_MASK
;
571 misc_ctl
|= 1; /* samp_pt */
572 bgx_reg_write(bgx
, lmac
->lmacid
, BGX_GMP_GMI_TXX_SLOT
, 512);
573 if (lmac
->last_duplex
)
574 bgx_reg_write(bgx
, lmac
->lmacid
,
575 BGX_GMP_GMI_TXX_BURST
, 0);
577 bgx_reg_write(bgx
, lmac
->lmacid
,
578 BGX_GMP_GMI_TXX_BURST
, 8192);
583 bgx_reg_write(bgx
, lmac
->lmacid
, BGX_GMP_PCS_MISCX_CTL
, misc_ctl
);
584 bgx_reg_write(bgx
, lmac
->lmacid
, BGX_GMP_GMI_PRTX_CFG
, port_cfg
);
586 /* Restore CMR config settings */
587 cmr_cfg
|= (rx_en
? CMR_PKT_RX_EN
: 0) | (tx_en
? CMR_PKT_TX_EN
: 0);
588 bgx_reg_write(bgx
, lmac
->lmacid
, BGX_CMRX_CFG
, cmr_cfg
);
590 if (bgx
->is_rgx
&& (cmr_cfg
& (CMR_PKT_RX_EN
| CMR_PKT_TX_EN
)))
591 xcv_setup_link(lmac
->link_up
, lmac
->last_speed
);
594 static void bgx_lmac_handler(struct net_device
*netdev
)
596 struct lmac
*lmac
= container_of(netdev
, struct lmac
, netdev
);
597 struct phy_device
*phydev
;
598 int link_changed
= 0;
603 phydev
= lmac
->phydev
;
605 if (!phydev
->link
&& lmac
->last_link
)
609 (lmac
->last_duplex
!= phydev
->duplex
||
610 lmac
->last_link
!= phydev
->link
||
611 lmac
->last_speed
!= phydev
->speed
)) {
615 lmac
->last_link
= phydev
->link
;
616 lmac
->last_speed
= phydev
->speed
;
617 lmac
->last_duplex
= phydev
->duplex
;
622 if (link_changed
> 0)
623 lmac
->link_up
= true;
625 lmac
->link_up
= false;
628 bgx_sgmii_change_link_state(lmac
);
630 bgx_xaui_check_link(lmac
);
633 u64
bgx_get_rx_stats(int node
, int bgx_idx
, int lmac
, int idx
)
637 bgx
= get_bgx(node
, bgx_idx
);
643 return bgx_reg_read(bgx
, lmac
, BGX_CMRX_RX_STAT0
+ (idx
* 8));
645 EXPORT_SYMBOL(bgx_get_rx_stats
);
647 u64
bgx_get_tx_stats(int node
, int bgx_idx
, int lmac
, int idx
)
651 bgx
= get_bgx(node
, bgx_idx
);
655 return bgx_reg_read(bgx
, lmac
, BGX_CMRX_TX_STAT0
+ (idx
* 8));
657 EXPORT_SYMBOL(bgx_get_tx_stats
);
659 /* Configure BGX LMAC in internal loopback mode */
660 void bgx_lmac_internal_loopback(int node
, int bgx_idx
,
661 int lmac_idx
, bool enable
)
667 bgx
= get_bgx(node
, bgx_idx
);
671 lmac
= &bgx
->lmac
[lmac_idx
];
672 if (lmac
->is_sgmii
) {
673 cfg
= bgx_reg_read(bgx
, lmac_idx
, BGX_GMP_PCS_MRX_CTL
);
675 cfg
|= PCS_MRX_CTL_LOOPBACK1
;
677 cfg
&= ~PCS_MRX_CTL_LOOPBACK1
;
678 bgx_reg_write(bgx
, lmac_idx
, BGX_GMP_PCS_MRX_CTL
, cfg
);
680 cfg
= bgx_reg_read(bgx
, lmac_idx
, BGX_SPUX_CONTROL1
);
682 cfg
|= SPU_CTL_LOOPBACK
;
684 cfg
&= ~SPU_CTL_LOOPBACK
;
685 bgx_reg_write(bgx
, lmac_idx
, BGX_SPUX_CONTROL1
, cfg
);
688 EXPORT_SYMBOL(bgx_lmac_internal_loopback
);
690 static int bgx_lmac_sgmii_init(struct bgx
*bgx
, struct lmac
*lmac
)
692 int lmacid
= lmac
->lmacid
;
695 bgx_reg_modify(bgx
, lmacid
, BGX_GMP_GMI_TXX_THRESH
, 0x30);
696 /* max packet size */
697 bgx_reg_modify(bgx
, lmacid
, BGX_GMP_GMI_RXX_JABBER
, MAX_FRAME_SIZE
);
699 /* Disable frame alignment if using preamble */
700 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_GMP_GMI_TXX_APPEND
);
702 bgx_reg_write(bgx
, lmacid
, BGX_GMP_GMI_TXX_SGMII_CTL
, 0);
705 bgx_reg_modify(bgx
, lmacid
, BGX_CMRX_CFG
, CMR_EN
);
708 bgx_reg_modify(bgx
, lmacid
, BGX_GMP_PCS_MRX_CTL
, PCS_MRX_CTL_RESET
);
709 if (bgx_poll_reg(bgx
, lmacid
, BGX_GMP_PCS_MRX_CTL
,
710 PCS_MRX_CTL_RESET
, true)) {
711 dev_err(&bgx
->pdev
->dev
, "BGX PCS reset not completed\n");
715 /* power down, reset autoneg, autoneg enable */
716 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_GMP_PCS_MRX_CTL
);
717 cfg
&= ~PCS_MRX_CTL_PWR_DN
;
718 cfg
|= PCS_MRX_CTL_RST_AN
;
720 cfg
|= PCS_MRX_CTL_AN_EN
;
722 /* In scenarios where PHY driver is not present or it's a
723 * non-standard PHY, FW sets AN_EN to inform Linux driver
724 * to do auto-neg and link polling or not.
726 if (cfg
& PCS_MRX_CTL_AN_EN
)
727 lmac
->autoneg
= true;
729 bgx_reg_write(bgx
, lmacid
, BGX_GMP_PCS_MRX_CTL
, cfg
);
731 if (lmac
->lmac_type
== BGX_MODE_QSGMII
) {
732 /* Disable disparity check for QSGMII */
733 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_GMP_PCS_MISCX_CTL
);
734 cfg
&= ~PCS_MISC_CTL_DISP_EN
;
735 bgx_reg_write(bgx
, lmacid
, BGX_GMP_PCS_MISCX_CTL
, cfg
);
739 if ((lmac
->lmac_type
== BGX_MODE_SGMII
) && lmac
->phydev
) {
740 if (bgx_poll_reg(bgx
, lmacid
, BGX_GMP_PCS_MRX_STATUS
,
741 PCS_MRX_STATUS_AN_CPT
, false)) {
742 dev_err(&bgx
->pdev
->dev
, "BGX AN_CPT not completed\n");
750 static int bgx_lmac_xaui_init(struct bgx
*bgx
, struct lmac
*lmac
)
753 int lmacid
= lmac
->lmacid
;
756 bgx_reg_modify(bgx
, lmacid
, BGX_SPUX_CONTROL1
, SPU_CTL_RESET
);
757 if (bgx_poll_reg(bgx
, lmacid
, BGX_SPUX_CONTROL1
, SPU_CTL_RESET
, true)) {
758 dev_err(&bgx
->pdev
->dev
, "BGX SPU reset not completed\n");
763 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_CMRX_CFG
);
765 bgx_reg_write(bgx
, lmacid
, BGX_CMRX_CFG
, cfg
);
767 bgx_reg_modify(bgx
, lmacid
, BGX_SPUX_CONTROL1
, SPU_CTL_LOW_POWER
);
768 /* Set interleaved running disparity for RXAUI */
769 if (lmac
->lmac_type
== BGX_MODE_RXAUI
)
770 bgx_reg_modify(bgx
, lmacid
, BGX_SPUX_MISC_CONTROL
,
771 SPU_MISC_CTL_INTLV_RDISP
);
773 /* Clear receive packet disable */
774 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SPUX_MISC_CONTROL
);
775 cfg
&= ~SPU_MISC_CTL_RX_DIS
;
776 bgx_reg_write(bgx
, lmacid
, BGX_SPUX_MISC_CONTROL
, cfg
);
778 /* clear all interrupts */
779 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SMUX_RX_INT
);
780 bgx_reg_write(bgx
, lmacid
, BGX_SMUX_RX_INT
, cfg
);
781 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SMUX_TX_INT
);
782 bgx_reg_write(bgx
, lmacid
, BGX_SMUX_TX_INT
, cfg
);
783 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SPUX_INT
);
784 bgx_reg_write(bgx
, lmacid
, BGX_SPUX_INT
, cfg
);
786 if (lmac
->use_training
) {
787 bgx_reg_write(bgx
, lmacid
, BGX_SPUX_BR_PMD_LP_CUP
, 0x00);
788 bgx_reg_write(bgx
, lmacid
, BGX_SPUX_BR_PMD_LD_CUP
, 0x00);
789 bgx_reg_write(bgx
, lmacid
, BGX_SPUX_BR_PMD_LD_REP
, 0x00);
790 /* training enable */
791 bgx_reg_modify(bgx
, lmacid
,
792 BGX_SPUX_BR_PMD_CRTL
, SPU_PMD_CRTL_TRAIN_EN
);
795 /* Append FCS to each packet */
796 bgx_reg_modify(bgx
, lmacid
, BGX_SMUX_TX_APPEND
, SMU_TX_APPEND_FCS_D
);
798 /* Disable forward error correction */
799 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SPUX_FEC_CONTROL
);
800 cfg
&= ~SPU_FEC_CTL_FEC_EN
;
801 bgx_reg_write(bgx
, lmacid
, BGX_SPUX_FEC_CONTROL
, cfg
);
803 /* Disable autoneg */
804 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SPUX_AN_CONTROL
);
805 cfg
= cfg
& ~(SPU_AN_CTL_AN_EN
| SPU_AN_CTL_XNP_EN
);
806 bgx_reg_write(bgx
, lmacid
, BGX_SPUX_AN_CONTROL
, cfg
);
808 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SPUX_AN_ADV
);
809 if (lmac
->lmac_type
== BGX_MODE_10G_KR
)
811 else if (lmac
->lmac_type
== BGX_MODE_40G_KR
)
814 cfg
&= ~((1 << 23) | (1 << 24));
815 cfg
= cfg
& (~((1ULL << 25) | (1ULL << 22) | (1ULL << 12)));
816 bgx_reg_write(bgx
, lmacid
, BGX_SPUX_AN_ADV
, cfg
);
818 cfg
= bgx_reg_read(bgx
, 0, BGX_SPU_DBG_CONTROL
);
819 cfg
&= ~SPU_DBG_CTL_AN_ARB_LINK_CHK_EN
;
820 bgx_reg_write(bgx
, 0, BGX_SPU_DBG_CONTROL
, cfg
);
823 bgx_reg_modify(bgx
, lmacid
, BGX_CMRX_CFG
, CMR_EN
);
825 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SPUX_CONTROL1
);
826 cfg
&= ~SPU_CTL_LOW_POWER
;
827 bgx_reg_write(bgx
, lmacid
, BGX_SPUX_CONTROL1
, cfg
);
829 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SMUX_TX_CTL
);
830 cfg
&= ~SMU_TX_CTL_UNI_EN
;
831 cfg
|= SMU_TX_CTL_DIC_EN
;
832 bgx_reg_write(bgx
, lmacid
, BGX_SMUX_TX_CTL
, cfg
);
834 /* Enable receive and transmission of pause frames */
835 bgx_reg_write(bgx
, lmacid
, BGX_SMUX_CBFC_CTL
, ((0xffffULL
<< 32) |
836 BCK_EN
| DRP_EN
| TX_EN
| RX_EN
));
837 /* Configure pause time and interval */
838 bgx_reg_write(bgx
, lmacid
,
839 BGX_SMUX_TX_PAUSE_PKT_TIME
, DEFAULT_PAUSE_TIME
);
840 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SMUX_TX_PAUSE_PKT_INTERVAL
);
842 bgx_reg_write(bgx
, lmacid
, BGX_SMUX_TX_PAUSE_PKT_INTERVAL
,
843 cfg
| (DEFAULT_PAUSE_TIME
- 0x1000));
844 bgx_reg_write(bgx
, lmacid
, BGX_SMUX_TX_PAUSE_ZERO
, 0x01);
846 /* take lmac_count into account */
847 bgx_reg_modify(bgx
, lmacid
, BGX_SMUX_TX_THRESH
, (0x100 - 1));
848 /* max packet size */
849 bgx_reg_modify(bgx
, lmacid
, BGX_SMUX_RX_JABBER
, MAX_FRAME_SIZE
);
854 static int bgx_xaui_check_link(struct lmac
*lmac
)
856 struct bgx
*bgx
= lmac
->bgx
;
857 int lmacid
= lmac
->lmacid
;
858 int lmac_type
= lmac
->lmac_type
;
861 if (lmac
->use_training
) {
862 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SPUX_INT
);
863 if (!(cfg
& (1ull << 13))) {
864 cfg
= (1ull << 13) | (1ull << 14);
865 bgx_reg_write(bgx
, lmacid
, BGX_SPUX_INT
, cfg
);
866 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SPUX_BR_PMD_CRTL
);
868 bgx_reg_write(bgx
, lmacid
, BGX_SPUX_BR_PMD_CRTL
, cfg
);
873 /* wait for PCS to come out of reset */
874 if (bgx_poll_reg(bgx
, lmacid
, BGX_SPUX_CONTROL1
, SPU_CTL_RESET
, true)) {
875 dev_err(&bgx
->pdev
->dev
, "BGX SPU reset not completed\n");
879 if ((lmac_type
== BGX_MODE_10G_KR
) || (lmac_type
== BGX_MODE_XFI
) ||
880 (lmac_type
== BGX_MODE_40G_KR
) || (lmac_type
== BGX_MODE_XLAUI
)) {
881 if (bgx_poll_reg(bgx
, lmacid
, BGX_SPUX_BR_STATUS1
,
882 SPU_BR_STATUS_BLK_LOCK
, false)) {
883 dev_err(&bgx
->pdev
->dev
,
884 "SPU_BR_STATUS_BLK_LOCK not completed\n");
888 if (bgx_poll_reg(bgx
, lmacid
, BGX_SPUX_BX_STATUS
,
889 SPU_BX_STATUS_RX_ALIGN
, false)) {
890 dev_err(&bgx
->pdev
->dev
,
891 "SPU_BX_STATUS_RX_ALIGN not completed\n");
896 /* Clear rcvflt bit (latching high) and read it back */
897 if (bgx_reg_read(bgx
, lmacid
, BGX_SPUX_STATUS2
) & SPU_STATUS2_RCVFLT
)
898 bgx_reg_modify(bgx
, lmacid
,
899 BGX_SPUX_STATUS2
, SPU_STATUS2_RCVFLT
);
900 if (bgx_reg_read(bgx
, lmacid
, BGX_SPUX_STATUS2
) & SPU_STATUS2_RCVFLT
) {
901 dev_err(&bgx
->pdev
->dev
, "Receive fault, retry training\n");
902 if (lmac
->use_training
) {
903 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SPUX_INT
);
904 if (!(cfg
& (1ull << 13))) {
905 cfg
= (1ull << 13) | (1ull << 14);
906 bgx_reg_write(bgx
, lmacid
, BGX_SPUX_INT
, cfg
);
907 cfg
= bgx_reg_read(bgx
, lmacid
,
908 BGX_SPUX_BR_PMD_CRTL
);
910 bgx_reg_write(bgx
, lmacid
,
911 BGX_SPUX_BR_PMD_CRTL
, cfg
);
918 /* Wait for BGX RX to be idle */
919 if (bgx_poll_reg(bgx
, lmacid
, BGX_SMUX_CTL
, SMU_CTL_RX_IDLE
, false)) {
920 dev_err(&bgx
->pdev
->dev
, "SMU RX not idle\n");
924 /* Wait for BGX TX to be idle */
925 if (bgx_poll_reg(bgx
, lmacid
, BGX_SMUX_CTL
, SMU_CTL_TX_IDLE
, false)) {
926 dev_err(&bgx
->pdev
->dev
, "SMU TX not idle\n");
930 /* Check for MAC RX faults */
931 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SMUX_RX_CTL
);
932 /* 0 - Link is okay, 1 - Local fault, 2 - Remote fault */
933 cfg
&= SMU_RX_CTL_STATUS
;
937 /* Rx local/remote fault seen.
938 * Do lmac reinit to see if condition recovers
940 bgx_lmac_xaui_init(bgx
, lmac
);
945 static void bgx_poll_for_sgmii_link(struct lmac
*lmac
)
947 u64 pcs_link
, an_result
;
950 pcs_link
= bgx_reg_read(lmac
->bgx
, lmac
->lmacid
,
951 BGX_GMP_PCS_MRX_STATUS
);
953 /*Link state bit is sticky, read it again*/
954 if (!(pcs_link
& PCS_MRX_STATUS_LINK
))
955 pcs_link
= bgx_reg_read(lmac
->bgx
, lmac
->lmacid
,
956 BGX_GMP_PCS_MRX_STATUS
);
958 if (bgx_poll_reg(lmac
->bgx
, lmac
->lmacid
, BGX_GMP_PCS_MRX_STATUS
,
959 PCS_MRX_STATUS_AN_CPT
, false)) {
960 lmac
->link_up
= false;
961 lmac
->last_speed
= SPEED_UNKNOWN
;
962 lmac
->last_duplex
= DUPLEX_UNKNOWN
;
966 lmac
->link_up
= ((pcs_link
& PCS_MRX_STATUS_LINK
) != 0) ? true : false;
967 an_result
= bgx_reg_read(lmac
->bgx
, lmac
->lmacid
,
968 BGX_GMP_PCS_ANX_AN_RESULTS
);
970 speed
= (an_result
>> 3) & 0x3;
971 lmac
->last_duplex
= (an_result
>> 1) & 0x1;
974 lmac
->last_speed
= 10;
977 lmac
->last_speed
= 100;
980 lmac
->last_speed
= 1000;
983 lmac
->link_up
= false;
984 lmac
->last_speed
= SPEED_UNKNOWN
;
985 lmac
->last_duplex
= DUPLEX_UNKNOWN
;
991 if (lmac
->last_link
!= lmac
->link_up
) {
993 bgx_sgmii_change_link_state(lmac
);
994 lmac
->last_link
= lmac
->link_up
;
997 queue_delayed_work(lmac
->check_link
, &lmac
->dwork
, HZ
* 3);
1000 static void bgx_poll_for_link(struct work_struct
*work
)
1003 u64 spu_link
, smu_link
;
1005 lmac
= container_of(work
, struct lmac
, dwork
.work
);
1006 if (lmac
->is_sgmii
) {
1007 bgx_poll_for_sgmii_link(lmac
);
1011 /* Receive link is latching low. Force it high and verify it */
1012 bgx_reg_modify(lmac
->bgx
, lmac
->lmacid
,
1013 BGX_SPUX_STATUS1
, SPU_STATUS1_RCV_LNK
);
1014 bgx_poll_reg(lmac
->bgx
, lmac
->lmacid
, BGX_SPUX_STATUS1
,
1015 SPU_STATUS1_RCV_LNK
, false);
1017 spu_link
= bgx_reg_read(lmac
->bgx
, lmac
->lmacid
, BGX_SPUX_STATUS1
);
1018 smu_link
= bgx_reg_read(lmac
->bgx
, lmac
->lmacid
, BGX_SMUX_RX_CTL
);
1020 if ((spu_link
& SPU_STATUS1_RCV_LNK
) &&
1021 !(smu_link
& SMU_RX_CTL_STATUS
)) {
1023 if (lmac
->lmac_type
== BGX_MODE_XLAUI
)
1024 lmac
->last_speed
= 40000;
1026 lmac
->last_speed
= 10000;
1027 lmac
->last_duplex
= 1;
1030 lmac
->last_speed
= SPEED_UNKNOWN
;
1031 lmac
->last_duplex
= DUPLEX_UNKNOWN
;
1034 if (lmac
->last_link
!= lmac
->link_up
) {
1035 if (lmac
->link_up
) {
1036 if (bgx_xaui_check_link(lmac
)) {
1037 /* Errors, clear link_up state */
1039 lmac
->last_speed
= SPEED_UNKNOWN
;
1040 lmac
->last_duplex
= DUPLEX_UNKNOWN
;
1043 lmac
->last_link
= lmac
->link_up
;
1046 queue_delayed_work(lmac
->check_link
, &lmac
->dwork
, HZ
* 2);
1049 static int phy_interface_mode(u8 lmac_type
)
1051 if (lmac_type
== BGX_MODE_QSGMII
)
1052 return PHY_INTERFACE_MODE_QSGMII
;
1053 if (lmac_type
== BGX_MODE_RGMII
)
1054 return PHY_INTERFACE_MODE_RGMII
;
1056 return PHY_INTERFACE_MODE_SGMII
;
1059 static int bgx_lmac_enable(struct bgx
*bgx
, u8 lmacid
)
1064 lmac
= &bgx
->lmac
[lmacid
];
1067 if ((lmac
->lmac_type
== BGX_MODE_SGMII
) ||
1068 (lmac
->lmac_type
== BGX_MODE_QSGMII
) ||
1069 (lmac
->lmac_type
== BGX_MODE_RGMII
)) {
1071 if (bgx_lmac_sgmii_init(bgx
, lmac
))
1075 if (bgx_lmac_xaui_init(bgx
, lmac
))
1079 if (lmac
->is_sgmii
) {
1080 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_GMP_GMI_TXX_APPEND
);
1081 cfg
|= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */
1082 bgx_reg_modify(bgx
, lmacid
, BGX_GMP_GMI_TXX_APPEND
, cfg
);
1083 bgx_reg_write(bgx
, lmacid
, BGX_GMP_GMI_TXX_MIN_PKT
, 60 - 1);
1085 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_SMUX_TX_APPEND
);
1086 cfg
|= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */
1087 bgx_reg_modify(bgx
, lmacid
, BGX_SMUX_TX_APPEND
, cfg
);
1088 bgx_reg_write(bgx
, lmacid
, BGX_SMUX_TX_MIN_PKT
, 60 + 4);
1091 /* actual number of filters available to exact LMAC */
1092 lmac
->dmacs_count
= (RX_DMAC_COUNT
/ bgx
->lmac_count
);
1093 lmac
->dmacs
= kcalloc(lmac
->dmacs_count
, sizeof(*lmac
->dmacs
),
1099 bgx_reg_modify(bgx
, lmacid
, BGX_CMRX_CFG
, CMR_EN
);
1101 /* Restore default cfg, incase low level firmware changed it */
1102 bgx_reg_write(bgx
, lmacid
, BGX_CMRX_RX_DMAC_CTL
, 0x03);
1104 if ((lmac
->lmac_type
!= BGX_MODE_XFI
) &&
1105 (lmac
->lmac_type
!= BGX_MODE_XLAUI
) &&
1106 (lmac
->lmac_type
!= BGX_MODE_40G_KR
) &&
1107 (lmac
->lmac_type
!= BGX_MODE_10G_KR
)) {
1108 if (!lmac
->phydev
) {
1109 if (lmac
->autoneg
) {
1110 bgx_reg_write(bgx
, lmacid
,
1111 BGX_GMP_PCS_LINKX_TIMER
,
1112 PCS_LINKX_TIMER_COUNT
);
1115 /* Default to below link speed and duplex */
1116 lmac
->link_up
= true;
1117 lmac
->last_speed
= 1000;
1118 lmac
->last_duplex
= 1;
1119 bgx_sgmii_change_link_state(lmac
);
1123 lmac
->phydev
->dev_flags
= 0;
1125 if (phy_connect_direct(&lmac
->netdev
, lmac
->phydev
,
1127 phy_interface_mode(lmac
->lmac_type
)))
1130 phy_start(lmac
->phydev
);
1135 lmac
->check_link
= alloc_workqueue("check_link", WQ_UNBOUND
|
1137 if (!lmac
->check_link
)
1139 INIT_DELAYED_WORK(&lmac
->dwork
, bgx_poll_for_link
);
1140 queue_delayed_work(lmac
->check_link
, &lmac
->dwork
, 0);
1145 static void bgx_lmac_disable(struct bgx
*bgx
, u8 lmacid
)
1150 lmac
= &bgx
->lmac
[lmacid
];
1151 if (lmac
->check_link
) {
1152 /* Destroy work queue */
1153 cancel_delayed_work_sync(&lmac
->dwork
);
1154 destroy_workqueue(lmac
->check_link
);
1157 /* Disable packet reception */
1158 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_CMRX_CFG
);
1159 cfg
&= ~CMR_PKT_RX_EN
;
1160 bgx_reg_write(bgx
, lmacid
, BGX_CMRX_CFG
, cfg
);
1162 /* Give chance for Rx/Tx FIFO to get drained */
1163 bgx_poll_reg(bgx
, lmacid
, BGX_CMRX_RX_FIFO_LEN
, (u64
)0x1FFF, true);
1164 bgx_poll_reg(bgx
, lmacid
, BGX_CMRX_TX_FIFO_LEN
, (u64
)0x3FFF, true);
1166 /* Disable packet transmission */
1167 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_CMRX_CFG
);
1168 cfg
&= ~CMR_PKT_TX_EN
;
1169 bgx_reg_write(bgx
, lmacid
, BGX_CMRX_CFG
, cfg
);
1171 /* Disable serdes lanes */
1172 if (!lmac
->is_sgmii
)
1173 bgx_reg_modify(bgx
, lmacid
,
1174 BGX_SPUX_CONTROL1
, SPU_CTL_LOW_POWER
);
1176 bgx_reg_modify(bgx
, lmacid
,
1177 BGX_GMP_PCS_MRX_CTL
, PCS_MRX_CTL_PWR_DN
);
1180 cfg
= bgx_reg_read(bgx
, lmacid
, BGX_CMRX_CFG
);
1182 bgx_reg_write(bgx
, lmacid
, BGX_CMRX_CFG
, cfg
);
1184 bgx_flush_dmac_cam_filter(bgx
, lmacid
);
1187 if ((lmac
->lmac_type
!= BGX_MODE_XFI
) &&
1188 (lmac
->lmac_type
!= BGX_MODE_XLAUI
) &&
1189 (lmac
->lmac_type
!= BGX_MODE_40G_KR
) &&
1190 (lmac
->lmac_type
!= BGX_MODE_10G_KR
) && lmac
->phydev
)
1191 phy_disconnect(lmac
->phydev
);
1193 lmac
->phydev
= NULL
;
1196 static void bgx_init_hw(struct bgx
*bgx
)
1201 bgx_reg_modify(bgx
, 0, BGX_CMR_GLOBAL_CFG
, CMR_GLOBAL_CFG_FCS_STRIP
);
1202 if (bgx_reg_read(bgx
, 0, BGX_CMR_BIST_STATUS
))
1203 dev_err(&bgx
->pdev
->dev
, "BGX%d BIST failed\n", bgx
->bgx_id
);
1205 /* Set lmac type and lane2serdes mapping */
1206 for (i
= 0; i
< bgx
->lmac_count
; i
++) {
1207 lmac
= &bgx
->lmac
[i
];
1208 bgx_reg_write(bgx
, i
, BGX_CMRX_CFG
,
1209 (lmac
->lmac_type
<< 8) | lmac
->lane_to_sds
);
1210 bgx
->lmac
[i
].lmacid_bd
= lmac_count
;
1214 bgx_reg_write(bgx
, 0, BGX_CMR_TX_LMACS
, bgx
->lmac_count
);
1215 bgx_reg_write(bgx
, 0, BGX_CMR_RX_LMACS
, bgx
->lmac_count
);
1217 /* Set the backpressure AND mask */
1218 for (i
= 0; i
< bgx
->lmac_count
; i
++)
1219 bgx_reg_modify(bgx
, 0, BGX_CMR_CHAN_MSK_AND
,
1220 ((1ULL << MAX_BGX_CHANS_PER_LMAC
) - 1) <<
1221 (i
* MAX_BGX_CHANS_PER_LMAC
));
1223 /* Disable all MAC filtering */
1224 for (i
= 0; i
< RX_DMAC_COUNT
; i
++)
1225 bgx_reg_write(bgx
, 0, BGX_CMR_RX_DMACX_CAM
+ (i
* 8), 0x00);
1227 /* Disable MAC steering (NCSI traffic) */
1228 for (i
= 0; i
< RX_TRAFFIC_STEER_RULE_COUNT
; i
++)
1229 bgx_reg_write(bgx
, 0, BGX_CMR_RX_STREERING
+ (i
* 8), 0x00);
1232 static u8
bgx_get_lane2sds_cfg(struct bgx
*bgx
, struct lmac
*lmac
)
1234 return (u8
)(bgx_reg_read(bgx
, lmac
->lmacid
, BGX_CMRX_CFG
) & 0xFF);
1237 static void bgx_print_qlm_mode(struct bgx
*bgx
, u8 lmacid
)
1239 struct device
*dev
= &bgx
->pdev
->dev
;
1243 if (!bgx
->is_dlm
&& lmacid
)
1246 lmac
= &bgx
->lmac
[lmacid
];
1248 sprintf(str
, "BGX%d QLM mode", bgx
->bgx_id
);
1250 sprintf(str
, "BGX%d LMAC%d mode", bgx
->bgx_id
, lmacid
);
1252 switch (lmac
->lmac_type
) {
1253 case BGX_MODE_SGMII
:
1254 dev_info(dev
, "%s: SGMII\n", (char *)str
);
1257 dev_info(dev
, "%s: XAUI\n", (char *)str
);
1259 case BGX_MODE_RXAUI
:
1260 dev_info(dev
, "%s: RXAUI\n", (char *)str
);
1263 if (!lmac
->use_training
)
1264 dev_info(dev
, "%s: XFI\n", (char *)str
);
1266 dev_info(dev
, "%s: 10G_KR\n", (char *)str
);
1268 case BGX_MODE_XLAUI
:
1269 if (!lmac
->use_training
)
1270 dev_info(dev
, "%s: XLAUI\n", (char *)str
);
1272 dev_info(dev
, "%s: 40G_KR4\n", (char *)str
);
1274 case BGX_MODE_QSGMII
:
1275 dev_info(dev
, "%s: QSGMII\n", (char *)str
);
1277 case BGX_MODE_RGMII
:
1278 dev_info(dev
, "%s: RGMII\n", (char *)str
);
1280 case BGX_MODE_INVALID
:
1286 static void lmac_set_lane2sds(struct bgx
*bgx
, struct lmac
*lmac
)
1288 switch (lmac
->lmac_type
) {
1289 case BGX_MODE_SGMII
:
1291 lmac
->lane_to_sds
= lmac
->lmacid
;
1294 case BGX_MODE_XLAUI
:
1295 case BGX_MODE_RGMII
:
1296 lmac
->lane_to_sds
= 0xE4;
1298 case BGX_MODE_RXAUI
:
1299 lmac
->lane_to_sds
= (lmac
->lmacid
) ? 0xE : 0x4;
1301 case BGX_MODE_QSGMII
:
1302 /* There is no way to determine if DLM0/2 is QSGMII or
1303 * DLM1/3 is configured to QSGMII as bootloader will
1304 * configure all LMACs, so take whatever is configured
1305 * by low level firmware.
1307 lmac
->lane_to_sds
= bgx_get_lane2sds_cfg(bgx
, lmac
);
1310 lmac
->lane_to_sds
= 0;
1315 static void lmac_set_training(struct bgx
*bgx
, struct lmac
*lmac
, int lmacid
)
1317 if ((lmac
->lmac_type
!= BGX_MODE_10G_KR
) &&
1318 (lmac
->lmac_type
!= BGX_MODE_40G_KR
)) {
1319 lmac
->use_training
= 0;
1323 lmac
->use_training
= bgx_reg_read(bgx
, lmacid
, BGX_SPUX_BR_PMD_CRTL
) &
1324 SPU_PMD_CRTL_TRAIN_EN
;
1327 static void bgx_set_lmac_config(struct bgx
*bgx
, u8 idx
)
1334 lmac
= &bgx
->lmac
[idx
];
1336 if (!bgx
->is_dlm
|| bgx
->is_rgx
) {
1337 /* Read LMAC0 type to figure out QLM mode
1338 * This is configured by low level firmware
1340 cmr_cfg
= bgx_reg_read(bgx
, 0, BGX_CMRX_CFG
);
1341 lmac
->lmac_type
= (cmr_cfg
>> 8) & 0x07;
1343 lmac
->lmac_type
= BGX_MODE_RGMII
;
1344 lmac_set_training(bgx
, lmac
, 0);
1345 lmac_set_lane2sds(bgx
, lmac
);
1349 /* For DLMs or SLMs on 80/81/83xx so many lane configurations
1350 * are possible and vary across boards. Also Kernel doesn't have
1351 * any way to identify board type/info and since firmware does,
1352 * just take lmac type and serdes lane config as is.
1354 cmr_cfg
= bgx_reg_read(bgx
, idx
, BGX_CMRX_CFG
);
1355 lmac_type
= (u8
)((cmr_cfg
>> 8) & 0x07);
1356 lane_to_sds
= (u8
)(cmr_cfg
& 0xFF);
1357 /* Check if config is reset value */
1358 if ((lmac_type
== 0) && (lane_to_sds
== 0xE4))
1359 lmac
->lmac_type
= BGX_MODE_INVALID
;
1361 lmac
->lmac_type
= lmac_type
;
1362 lmac
->lane_to_sds
= lane_to_sds
;
1363 lmac_set_training(bgx
, lmac
, lmac
->lmacid
);
1366 static void bgx_get_qlm_mode(struct bgx
*bgx
)
1371 /* Init all LMAC's type to invalid */
1372 for (idx
= 0; idx
< bgx
->max_lmac
; idx
++) {
1373 lmac
= &bgx
->lmac
[idx
];
1375 lmac
->lmac_type
= BGX_MODE_INVALID
;
1376 lmac
->use_training
= false;
1379 /* It is assumed that low level firmware sets this value */
1380 bgx
->lmac_count
= bgx_reg_read(bgx
, 0, BGX_CMR_RX_LMACS
) & 0x7;
1381 if (bgx
->lmac_count
> bgx
->max_lmac
)
1382 bgx
->lmac_count
= bgx
->max_lmac
;
1384 for (idx
= 0; idx
< bgx
->lmac_count
; idx
++) {
1385 bgx_set_lmac_config(bgx
, idx
);
1386 bgx_print_qlm_mode(bgx
, idx
);
1392 static int acpi_get_mac_address(struct device
*dev
, struct acpi_device
*adev
,
1398 ret
= fwnode_property_read_u8_array(acpi_fwnode_handle(adev
),
1399 "mac-address", mac
, ETH_ALEN
);
1403 if (!is_valid_ether_addr(mac
)) {
1404 dev_err(dev
, "MAC address invalid: %pM\n", mac
);
1409 dev_info(dev
, "MAC address set to: %pM\n", mac
);
1411 memcpy(dst
, mac
, ETH_ALEN
);
1416 /* Currently only sets the MAC address. */
1417 static acpi_status
bgx_acpi_register_phy(acpi_handle handle
,
1418 u32 lvl
, void *context
, void **rv
)
1420 struct bgx
*bgx
= context
;
1421 struct device
*dev
= &bgx
->pdev
->dev
;
1422 struct acpi_device
*adev
;
1424 if (acpi_bus_get_device(handle
, &adev
))
1427 acpi_get_mac_address(dev
, adev
, bgx
->lmac
[bgx
->acpi_lmac_idx
].mac
);
1429 SET_NETDEV_DEV(&bgx
->lmac
[bgx
->acpi_lmac_idx
].netdev
, dev
);
1431 bgx
->lmac
[bgx
->acpi_lmac_idx
].lmacid
= bgx
->acpi_lmac_idx
;
1432 bgx
->acpi_lmac_idx
++; /* move to next LMAC */
1437 static acpi_status
bgx_acpi_match_id(acpi_handle handle
, u32 lvl
,
1438 void *context
, void **ret_val
)
1440 struct acpi_buffer string
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1441 struct bgx
*bgx
= context
;
1444 snprintf(bgx_sel
, 5, "BGX%d", bgx
->bgx_id
);
1445 if (ACPI_FAILURE(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &string
))) {
1446 pr_warn("Invalid link device\n");
1450 if (strncmp(string
.pointer
, bgx_sel
, 4))
1453 acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, 1,
1454 bgx_acpi_register_phy
, NULL
, bgx
, NULL
);
1456 kfree(string
.pointer
);
1457 return AE_CTRL_TERMINATE
;
1460 static int bgx_init_acpi_phy(struct bgx
*bgx
)
1462 acpi_get_devices(NULL
, bgx_acpi_match_id
, bgx
, (void **)NULL
);
1468 static int bgx_init_acpi_phy(struct bgx
*bgx
)
1473 #endif /* CONFIG_ACPI */
1475 #if IS_ENABLED(CONFIG_OF_MDIO)
1477 static int bgx_init_of_phy(struct bgx
*bgx
)
1479 struct fwnode_handle
*fwn
;
1480 struct device_node
*node
= NULL
;
1483 device_for_each_child_node(&bgx
->pdev
->dev
, fwn
) {
1484 struct phy_device
*pd
;
1485 struct device_node
*phy_np
;
1488 /* Should always be an OF node. But if it is not, we
1489 * cannot handle it, so exit the loop.
1491 node
= to_of_node(fwn
);
1495 mac
= of_get_mac_address(node
);
1497 ether_addr_copy(bgx
->lmac
[lmac
].mac
, mac
);
1499 SET_NETDEV_DEV(&bgx
->lmac
[lmac
].netdev
, &bgx
->pdev
->dev
);
1500 bgx
->lmac
[lmac
].lmacid
= lmac
;
1502 phy_np
= of_parse_phandle(node
, "phy-handle", 0);
1503 /* If there is no phy or defective firmware presents
1504 * this cortina phy, for which there is no driver
1505 * support, ignore it.
1508 !of_device_is_compatible(phy_np
, "cortina,cs4223-slice")) {
1509 /* Wait until the phy drivers are available */
1510 pd
= of_phy_find_device(phy_np
);
1513 bgx
->lmac
[lmac
].phydev
= pd
;
1517 if (lmac
== bgx
->max_lmac
) {
1525 /* We are bailing out, try not to leak device reference counts
1526 * for phy devices we may have already found.
1529 if (bgx
->lmac
[lmac
].phydev
) {
1530 put_device(&bgx
->lmac
[lmac
].phydev
->mdio
.dev
);
1531 bgx
->lmac
[lmac
].phydev
= NULL
;
1536 return -EPROBE_DEFER
;
1541 static int bgx_init_of_phy(struct bgx
*bgx
)
1546 #endif /* CONFIG_OF_MDIO */
1548 static int bgx_init_phy(struct bgx
*bgx
)
1551 return bgx_init_acpi_phy(bgx
);
1553 return bgx_init_of_phy(bgx
);
1556 static irqreturn_t
bgx_intr_handler(int irq
, void *data
)
1558 struct bgx
*bgx
= (struct bgx
*)data
;
1562 for (lmac
= 0; lmac
< bgx
->lmac_count
; lmac
++) {
1563 status
= bgx_reg_read(bgx
, lmac
, BGX_GMP_GMI_TXX_INT
);
1564 if (status
& GMI_TXX_INT_UNDFLW
) {
1565 pci_err(bgx
->pdev
, "BGX%d lmac%d UNDFLW\n",
1567 val
= bgx_reg_read(bgx
, lmac
, BGX_CMRX_CFG
);
1569 bgx_reg_write(bgx
, lmac
, BGX_CMRX_CFG
, val
);
1571 bgx_reg_write(bgx
, lmac
, BGX_CMRX_CFG
, val
);
1573 /* clear interrupts */
1574 bgx_reg_write(bgx
, lmac
, BGX_GMP_GMI_TXX_INT
, status
);
1580 static void bgx_register_intr(struct pci_dev
*pdev
)
1582 struct bgx
*bgx
= pci_get_drvdata(pdev
);
1585 ret
= pci_alloc_irq_vectors(pdev
, BGX_LMAC_VEC_OFFSET
,
1586 BGX_LMAC_VEC_OFFSET
, PCI_IRQ_ALL_TYPES
);
1588 pci_err(pdev
, "Req for #%d msix vectors failed\n",
1589 BGX_LMAC_VEC_OFFSET
);
1592 ret
= pci_request_irq(pdev
, GMPX_GMI_TX_INT
, bgx_intr_handler
, NULL
,
1593 bgx
, "BGX%d", bgx
->bgx_id
);
1595 pci_free_irq(pdev
, GMPX_GMI_TX_INT
, bgx
);
1598 static int bgx_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
1601 struct device
*dev
= &pdev
->dev
;
1602 struct bgx
*bgx
= NULL
;
1606 bgx
= devm_kzalloc(dev
, sizeof(*bgx
), GFP_KERNEL
);
1611 pci_set_drvdata(pdev
, bgx
);
1613 err
= pcim_enable_device(pdev
);
1615 dev_err(dev
, "Failed to enable PCI device\n");
1616 pci_set_drvdata(pdev
, NULL
);
1620 err
= pci_request_regions(pdev
, DRV_NAME
);
1622 dev_err(dev
, "PCI request regions failed 0x%x\n", err
);
1623 goto err_disable_device
;
1626 /* MAP configuration registers */
1627 bgx
->reg_base
= pcim_iomap(pdev
, PCI_CFG_REG_BAR_NUM
, 0);
1628 if (!bgx
->reg_base
) {
1629 dev_err(dev
, "BGX: Cannot map CSR memory space, aborting\n");
1631 goto err_release_regions
;
1634 set_max_bgx_per_node(pdev
);
1636 pci_read_config_word(pdev
, PCI_DEVICE_ID
, &sdevid
);
1637 if (sdevid
!= PCI_DEVICE_ID_THUNDER_RGX
) {
1638 bgx
->bgx_id
= (pci_resource_start(pdev
,
1639 PCI_CFG_REG_BAR_NUM
) >> 24) & BGX_ID_MASK
;
1640 bgx
->bgx_id
+= nic_get_node_id(pdev
) * max_bgx_per_node
;
1641 bgx
->max_lmac
= MAX_LMAC_PER_BGX
;
1642 bgx_vnic
[bgx
->bgx_id
] = bgx
;
1646 bgx
->bgx_id
= MAX_BGX_PER_CN81XX
- 1;
1647 bgx_vnic
[bgx
->bgx_id
] = bgx
;
1651 /* On 81xx all are DLMs and on 83xx there are 3 BGX QLMs and one
1652 * BGX i.e BGX2 can be split across 2 DLMs.
1654 pci_read_config_word(pdev
, PCI_SUBSYSTEM_ID
, &sdevid
);
1655 if ((sdevid
== PCI_SUBSYS_DEVID_81XX_BGX
) ||
1656 ((sdevid
== PCI_SUBSYS_DEVID_83XX_BGX
) && (bgx
->bgx_id
== 2)))
1659 bgx_get_qlm_mode(bgx
);
1661 err
= bgx_init_phy(bgx
);
1667 bgx_register_intr(pdev
);
1669 /* Enable all LMACs */
1670 for (lmac
= 0; lmac
< bgx
->lmac_count
; lmac
++) {
1671 err
= bgx_lmac_enable(bgx
, lmac
);
1673 dev_err(dev
, "BGX%d failed to enable lmac%d\n",
1676 bgx_lmac_disable(bgx
, --lmac
);
1684 bgx_vnic
[bgx
->bgx_id
] = NULL
;
1685 pci_free_irq(pdev
, GMPX_GMI_TX_INT
, bgx
);
1686 err_release_regions
:
1687 pci_release_regions(pdev
);
1689 pci_disable_device(pdev
);
1690 pci_set_drvdata(pdev
, NULL
);
1694 static void bgx_remove(struct pci_dev
*pdev
)
1696 struct bgx
*bgx
= pci_get_drvdata(pdev
);
1699 /* Disable all LMACs */
1700 for (lmac
= 0; lmac
< bgx
->lmac_count
; lmac
++)
1701 bgx_lmac_disable(bgx
, lmac
);
1703 pci_free_irq(pdev
, GMPX_GMI_TX_INT
, bgx
);
1705 bgx_vnic
[bgx
->bgx_id
] = NULL
;
1706 pci_release_regions(pdev
);
1707 pci_disable_device(pdev
);
1708 pci_set_drvdata(pdev
, NULL
);
1711 static struct pci_driver bgx_driver
= {
1713 .id_table
= bgx_id_table
,
1715 .remove
= bgx_remove
,
1718 static int __init
bgx_init_module(void)
1720 pr_info("%s, ver %s\n", DRV_NAME
, DRV_VERSION
);
1722 return pci_register_driver(&bgx_driver
);
1725 static void __exit
bgx_cleanup_module(void)
1727 pci_unregister_driver(&bgx_driver
);
1730 module_init(bgx_init_module
);
1731 module_exit(bgx_cleanup_module
);