Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Cavium, Inc. |
| 3 | * |
| 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. |
| 7 | */ |
| 8 | |
David Daney | 46b903a | 2015-08-10 17:58:37 -0700 | [diff] [blame] | 9 | #include <linux/acpi.h> |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 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> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/of_mdio.h> |
| 18 | #include <linux/of_net.h> |
| 19 | |
| 20 | #include "nic_reg.h" |
| 21 | #include "nic.h" |
| 22 | #include "thunder_bgx.h" |
| 23 | |
| 24 | #define DRV_NAME "thunder-BGX" |
| 25 | #define DRV_VERSION "1.0" |
| 26 | |
| 27 | struct lmac { |
| 28 | struct bgx *bgx; |
| 29 | int dmac; |
David Daney | 46b903a | 2015-08-10 17:58:37 -0700 | [diff] [blame] | 30 | u8 mac[ETH_ALEN]; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 31 | u8 lmac_type; |
| 32 | u8 lane_to_sds; |
| 33 | bool use_training; |
Thanneeru Srinivasulu | 075ad76 | 2017-02-08 18:09:00 +0530 | [diff] [blame^] | 34 | bool autoneg; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 35 | bool link_up; |
| 36 | int lmacid; /* ID within BGX */ |
| 37 | int lmacid_bd; /* ID on board */ |
| 38 | struct net_device netdev; |
| 39 | struct phy_device *phydev; |
| 40 | unsigned int last_duplex; |
| 41 | unsigned int last_link; |
| 42 | unsigned int last_speed; |
| 43 | bool is_sgmii; |
| 44 | struct delayed_work dwork; |
| 45 | struct workqueue_struct *check_link; |
Aleksey Makarov | 0c886a1 | 2015-06-02 11:00:22 -0700 | [diff] [blame] | 46 | }; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 47 | |
| 48 | struct bgx { |
| 49 | u8 bgx_id; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 50 | struct lmac lmac[MAX_LMAC_PER_BGX]; |
Vadim Lomovtsev | 7aa4865 | 2017-01-12 07:28:06 -0800 | [diff] [blame] | 51 | u8 lmac_count; |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 52 | u8 max_lmac; |
Vadim Lomovtsev | 7aa4865 | 2017-01-12 07:28:06 -0800 | [diff] [blame] | 53 | u8 acpi_lmac_idx; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 54 | void __iomem *reg_base; |
| 55 | struct pci_dev *pdev; |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 56 | bool is_dlm; |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 57 | bool is_rgx; |
Aleksey Makarov | 0c886a1 | 2015-06-02 11:00:22 -0700 | [diff] [blame] | 58 | }; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 59 | |
Aleksey Makarov | fd7ec06 | 2015-06-02 11:00:23 -0700 | [diff] [blame] | 60 | static struct bgx *bgx_vnic[MAX_BGX_THUNDER]; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 61 | static int lmac_count; /* Total no of LMACs in system */ |
| 62 | |
| 63 | static int bgx_xaui_check_link(struct lmac *lmac); |
| 64 | |
| 65 | /* Supported devices */ |
| 66 | static const struct pci_device_id bgx_id_table[] = { |
| 67 | { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_BGX) }, |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 68 | { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_RGX) }, |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 69 | { 0, } /* end of table */ |
| 70 | }; |
| 71 | |
| 72 | MODULE_AUTHOR("Cavium Inc"); |
| 73 | MODULE_DESCRIPTION("Cavium Thunder BGX/MAC Driver"); |
| 74 | MODULE_LICENSE("GPL v2"); |
| 75 | MODULE_VERSION(DRV_VERSION); |
| 76 | MODULE_DEVICE_TABLE(pci, bgx_id_table); |
| 77 | |
| 78 | /* The Cavium ThunderX network controller can *only* be found in SoCs |
| 79 | * containing the ThunderX ARM64 CPU implementation. All accesses to the device |
| 80 | * registers on this platform are implicitly strongly ordered with respect |
| 81 | * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use |
| 82 | * with no memory barriers in this driver. The readq()/writeq() functions add |
| 83 | * explicit ordering operation which in this case are redundant, and only |
| 84 | * add overhead. |
| 85 | */ |
| 86 | |
| 87 | /* Register read/write APIs */ |
| 88 | static u64 bgx_reg_read(struct bgx *bgx, u8 lmac, u64 offset) |
| 89 | { |
| 90 | void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset; |
| 91 | |
| 92 | return readq_relaxed(addr); |
| 93 | } |
| 94 | |
| 95 | static void bgx_reg_write(struct bgx *bgx, u8 lmac, u64 offset, u64 val) |
| 96 | { |
| 97 | void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset; |
| 98 | |
| 99 | writeq_relaxed(val, addr); |
| 100 | } |
| 101 | |
| 102 | static void bgx_reg_modify(struct bgx *bgx, u8 lmac, u64 offset, u64 val) |
| 103 | { |
| 104 | void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset; |
| 105 | |
| 106 | writeq_relaxed(val | readq_relaxed(addr), addr); |
| 107 | } |
| 108 | |
| 109 | static int bgx_poll_reg(struct bgx *bgx, u8 lmac, u64 reg, u64 mask, bool zero) |
| 110 | { |
| 111 | int timeout = 100; |
| 112 | u64 reg_val; |
| 113 | |
| 114 | while (timeout) { |
| 115 | reg_val = bgx_reg_read(bgx, lmac, reg); |
| 116 | if (zero && !(reg_val & mask)) |
| 117 | return 0; |
| 118 | if (!zero && (reg_val & mask)) |
| 119 | return 0; |
| 120 | usleep_range(1000, 2000); |
| 121 | timeout--; |
| 122 | } |
| 123 | return 1; |
| 124 | } |
| 125 | |
| 126 | /* Return number of BGX present in HW */ |
| 127 | unsigned bgx_get_map(int node) |
| 128 | { |
| 129 | int i; |
| 130 | unsigned map = 0; |
| 131 | |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 132 | for (i = 0; i < MAX_BGX_PER_NODE; i++) { |
| 133 | if (bgx_vnic[(node * MAX_BGX_PER_NODE) + i]) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 134 | map |= (1 << i); |
| 135 | } |
| 136 | |
| 137 | return map; |
| 138 | } |
| 139 | EXPORT_SYMBOL(bgx_get_map); |
| 140 | |
| 141 | /* Return number of LMAC configured for this BGX */ |
| 142 | int bgx_get_lmac_count(int node, int bgx_idx) |
| 143 | { |
| 144 | struct bgx *bgx; |
| 145 | |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 146 | bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx]; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 147 | if (bgx) |
| 148 | return bgx->lmac_count; |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | EXPORT_SYMBOL(bgx_get_lmac_count); |
| 153 | |
| 154 | /* Returns the current link status of LMAC */ |
| 155 | void bgx_get_lmac_link_state(int node, int bgx_idx, int lmacid, void *status) |
| 156 | { |
| 157 | struct bgx_link_status *link = (struct bgx_link_status *)status; |
| 158 | struct bgx *bgx; |
| 159 | struct lmac *lmac; |
| 160 | |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 161 | bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx]; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 162 | if (!bgx) |
| 163 | return; |
| 164 | |
| 165 | lmac = &bgx->lmac[lmacid]; |
Thanneeru Srinivasulu | 1cc7025 | 2016-11-24 14:48:01 +0530 | [diff] [blame] | 166 | link->mac_type = lmac->lmac_type; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 167 | link->link_up = lmac->link_up; |
| 168 | link->duplex = lmac->last_duplex; |
| 169 | link->speed = lmac->last_speed; |
| 170 | } |
| 171 | EXPORT_SYMBOL(bgx_get_lmac_link_state); |
| 172 | |
Aleksey Makarov | e610cb3 | 2015-06-02 11:00:21 -0700 | [diff] [blame] | 173 | const u8 *bgx_get_lmac_mac(int node, int bgx_idx, int lmacid) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 174 | { |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 175 | struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx]; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 176 | |
| 177 | if (bgx) |
| 178 | return bgx->lmac[lmacid].mac; |
| 179 | |
| 180 | return NULL; |
| 181 | } |
| 182 | EXPORT_SYMBOL(bgx_get_lmac_mac); |
| 183 | |
Aleksey Makarov | e610cb3 | 2015-06-02 11:00:21 -0700 | [diff] [blame] | 184 | void bgx_set_lmac_mac(int node, int bgx_idx, int lmacid, const u8 *mac) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 185 | { |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 186 | struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx]; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 187 | |
| 188 | if (!bgx) |
| 189 | return; |
| 190 | |
| 191 | ether_addr_copy(bgx->lmac[lmacid].mac, mac); |
| 192 | } |
| 193 | EXPORT_SYMBOL(bgx_set_lmac_mac); |
| 194 | |
Sunil Goutham | bc69fdf | 2015-12-02 15:36:17 +0530 | [diff] [blame] | 195 | void bgx_lmac_rx_tx_enable(int node, int bgx_idx, int lmacid, bool enable) |
| 196 | { |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 197 | struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx]; |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 198 | struct lmac *lmac; |
Sunil Goutham | bc69fdf | 2015-12-02 15:36:17 +0530 | [diff] [blame] | 199 | u64 cfg; |
| 200 | |
| 201 | if (!bgx) |
| 202 | return; |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 203 | lmac = &bgx->lmac[lmacid]; |
Sunil Goutham | bc69fdf | 2015-12-02 15:36:17 +0530 | [diff] [blame] | 204 | |
| 205 | cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG); |
| 206 | if (enable) |
| 207 | cfg |= CMR_PKT_RX_EN | CMR_PKT_TX_EN; |
| 208 | else |
| 209 | cfg &= ~(CMR_PKT_RX_EN | CMR_PKT_TX_EN); |
| 210 | bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg); |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 211 | |
| 212 | if (bgx->is_rgx) |
| 213 | xcv_setup_link(enable ? lmac->link_up : 0, lmac->last_speed); |
Sunil Goutham | bc69fdf | 2015-12-02 15:36:17 +0530 | [diff] [blame] | 214 | } |
| 215 | EXPORT_SYMBOL(bgx_lmac_rx_tx_enable); |
| 216 | |
Sunil Goutham | 430da20 | 2016-11-24 14:48:03 +0530 | [diff] [blame] | 217 | void bgx_lmac_get_pfc(int node, int bgx_idx, int lmacid, void *pause) |
| 218 | { |
| 219 | struct pfc *pfc = (struct pfc *)pause; |
| 220 | struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx]; |
| 221 | struct lmac *lmac; |
| 222 | u64 cfg; |
| 223 | |
| 224 | if (!bgx) |
| 225 | return; |
| 226 | lmac = &bgx->lmac[lmacid]; |
| 227 | if (lmac->is_sgmii) |
| 228 | return; |
| 229 | |
| 230 | cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_CBFC_CTL); |
| 231 | pfc->fc_rx = cfg & RX_EN; |
| 232 | pfc->fc_tx = cfg & TX_EN; |
| 233 | pfc->autoneg = 0; |
| 234 | } |
| 235 | EXPORT_SYMBOL(bgx_lmac_get_pfc); |
| 236 | |
| 237 | void bgx_lmac_set_pfc(int node, int bgx_idx, int lmacid, void *pause) |
| 238 | { |
| 239 | struct pfc *pfc = (struct pfc *)pause; |
| 240 | struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx]; |
| 241 | struct lmac *lmac; |
| 242 | u64 cfg; |
| 243 | |
| 244 | if (!bgx) |
| 245 | return; |
| 246 | lmac = &bgx->lmac[lmacid]; |
| 247 | if (lmac->is_sgmii) |
| 248 | return; |
| 249 | |
| 250 | cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_CBFC_CTL); |
| 251 | cfg &= ~(RX_EN | TX_EN); |
| 252 | cfg |= (pfc->fc_rx ? RX_EN : 0x00); |
| 253 | cfg |= (pfc->fc_tx ? TX_EN : 0x00); |
| 254 | bgx_reg_write(bgx, lmacid, BGX_SMUX_CBFC_CTL, cfg); |
| 255 | } |
| 256 | EXPORT_SYMBOL(bgx_lmac_set_pfc); |
| 257 | |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 258 | static void bgx_sgmii_change_link_state(struct lmac *lmac) |
| 259 | { |
| 260 | struct bgx *bgx = lmac->bgx; |
| 261 | u64 cmr_cfg; |
| 262 | u64 port_cfg = 0; |
| 263 | u64 misc_ctl = 0; |
| 264 | |
| 265 | cmr_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_CMRX_CFG); |
| 266 | cmr_cfg &= ~CMR_EN; |
| 267 | bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg); |
| 268 | |
| 269 | port_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG); |
| 270 | misc_ctl = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_PCS_MISCX_CTL); |
| 271 | |
| 272 | if (lmac->link_up) { |
| 273 | misc_ctl &= ~PCS_MISC_CTL_GMX_ENO; |
| 274 | port_cfg &= ~GMI_PORT_CFG_DUPLEX; |
| 275 | port_cfg |= (lmac->last_duplex << 2); |
| 276 | } else { |
| 277 | misc_ctl |= PCS_MISC_CTL_GMX_ENO; |
| 278 | } |
| 279 | |
| 280 | switch (lmac->last_speed) { |
| 281 | case 10: |
| 282 | port_cfg &= ~GMI_PORT_CFG_SPEED; /* speed 0 */ |
| 283 | port_cfg |= GMI_PORT_CFG_SPEED_MSB; /* speed_msb 1 */ |
| 284 | port_cfg &= ~GMI_PORT_CFG_SLOT_TIME; /* slottime 0 */ |
| 285 | misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK; |
| 286 | misc_ctl |= 50; /* samp_pt */ |
| 287 | bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 64); |
| 288 | bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_BURST, 0); |
| 289 | break; |
| 290 | case 100: |
| 291 | port_cfg &= ~GMI_PORT_CFG_SPEED; /* speed 0 */ |
| 292 | port_cfg &= ~GMI_PORT_CFG_SPEED_MSB; /* speed_msb 0 */ |
| 293 | port_cfg &= ~GMI_PORT_CFG_SLOT_TIME; /* slottime 0 */ |
| 294 | misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK; |
| 295 | misc_ctl |= 5; /* samp_pt */ |
| 296 | bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 64); |
| 297 | bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_BURST, 0); |
| 298 | break; |
| 299 | case 1000: |
| 300 | port_cfg |= GMI_PORT_CFG_SPEED; /* speed 1 */ |
| 301 | port_cfg &= ~GMI_PORT_CFG_SPEED_MSB; /* speed_msb 0 */ |
| 302 | port_cfg |= GMI_PORT_CFG_SLOT_TIME; /* slottime 1 */ |
| 303 | misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK; |
| 304 | misc_ctl |= 1; /* samp_pt */ |
| 305 | bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 512); |
| 306 | if (lmac->last_duplex) |
| 307 | bgx_reg_write(bgx, lmac->lmacid, |
| 308 | BGX_GMP_GMI_TXX_BURST, 0); |
| 309 | else |
| 310 | bgx_reg_write(bgx, lmac->lmacid, |
| 311 | BGX_GMP_GMI_TXX_BURST, 8192); |
| 312 | break; |
| 313 | default: |
| 314 | break; |
| 315 | } |
| 316 | bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_PCS_MISCX_CTL, misc_ctl); |
| 317 | bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG, port_cfg); |
| 318 | |
| 319 | port_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG); |
| 320 | |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 321 | /* Re-enable lmac */ |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 322 | cmr_cfg |= CMR_EN; |
| 323 | bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg); |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 324 | |
| 325 | if (bgx->is_rgx && (cmr_cfg & (CMR_PKT_RX_EN | CMR_PKT_TX_EN))) |
| 326 | xcv_setup_link(lmac->link_up, lmac->last_speed); |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Aleksey Makarov | fd7ec06 | 2015-06-02 11:00:23 -0700 | [diff] [blame] | 329 | static void bgx_lmac_handler(struct net_device *netdev) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 330 | { |
| 331 | struct lmac *lmac = container_of(netdev, struct lmac, netdev); |
xypron.glpk@gmx.de | 099a728 | 2016-05-17 21:40:38 +0200 | [diff] [blame] | 332 | struct phy_device *phydev; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 333 | int link_changed = 0; |
| 334 | |
| 335 | if (!lmac) |
| 336 | return; |
| 337 | |
xypron.glpk@gmx.de | 099a728 | 2016-05-17 21:40:38 +0200 | [diff] [blame] | 338 | phydev = lmac->phydev; |
| 339 | |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 340 | if (!phydev->link && lmac->last_link) |
| 341 | link_changed = -1; |
| 342 | |
| 343 | if (phydev->link && |
| 344 | (lmac->last_duplex != phydev->duplex || |
| 345 | lmac->last_link != phydev->link || |
| 346 | lmac->last_speed != phydev->speed)) { |
| 347 | link_changed = 1; |
| 348 | } |
| 349 | |
| 350 | lmac->last_link = phydev->link; |
| 351 | lmac->last_speed = phydev->speed; |
| 352 | lmac->last_duplex = phydev->duplex; |
| 353 | |
| 354 | if (!link_changed) |
| 355 | return; |
| 356 | |
| 357 | if (link_changed > 0) |
| 358 | lmac->link_up = true; |
| 359 | else |
| 360 | lmac->link_up = false; |
| 361 | |
| 362 | if (lmac->is_sgmii) |
| 363 | bgx_sgmii_change_link_state(lmac); |
| 364 | else |
| 365 | bgx_xaui_check_link(lmac); |
| 366 | } |
| 367 | |
| 368 | u64 bgx_get_rx_stats(int node, int bgx_idx, int lmac, int idx) |
| 369 | { |
| 370 | struct bgx *bgx; |
| 371 | |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 372 | bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx]; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 373 | if (!bgx) |
| 374 | return 0; |
| 375 | |
| 376 | if (idx > 8) |
| 377 | lmac = 0; |
| 378 | return bgx_reg_read(bgx, lmac, BGX_CMRX_RX_STAT0 + (idx * 8)); |
| 379 | } |
| 380 | EXPORT_SYMBOL(bgx_get_rx_stats); |
| 381 | |
| 382 | u64 bgx_get_tx_stats(int node, int bgx_idx, int lmac, int idx) |
| 383 | { |
| 384 | struct bgx *bgx; |
| 385 | |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 386 | bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx]; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 387 | if (!bgx) |
| 388 | return 0; |
| 389 | |
| 390 | return bgx_reg_read(bgx, lmac, BGX_CMRX_TX_STAT0 + (idx * 8)); |
| 391 | } |
| 392 | EXPORT_SYMBOL(bgx_get_tx_stats); |
| 393 | |
| 394 | static void bgx_flush_dmac_addrs(struct bgx *bgx, int lmac) |
| 395 | { |
| 396 | u64 offset; |
| 397 | |
| 398 | while (bgx->lmac[lmac].dmac > 0) { |
| 399 | offset = ((bgx->lmac[lmac].dmac - 1) * sizeof(u64)) + |
| 400 | (lmac * MAX_DMAC_PER_LMAC * sizeof(u64)); |
| 401 | bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM + offset, 0); |
| 402 | bgx->lmac[lmac].dmac--; |
| 403 | } |
| 404 | } |
| 405 | |
Sunil Goutham | d77a238 | 2015-08-30 12:29:16 +0300 | [diff] [blame] | 406 | /* Configure BGX LMAC in internal loopback mode */ |
| 407 | void bgx_lmac_internal_loopback(int node, int bgx_idx, |
| 408 | int lmac_idx, bool enable) |
| 409 | { |
| 410 | struct bgx *bgx; |
| 411 | struct lmac *lmac; |
| 412 | u64 cfg; |
| 413 | |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 414 | bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx]; |
Sunil Goutham | d77a238 | 2015-08-30 12:29:16 +0300 | [diff] [blame] | 415 | if (!bgx) |
| 416 | return; |
| 417 | |
| 418 | lmac = &bgx->lmac[lmac_idx]; |
| 419 | if (lmac->is_sgmii) { |
| 420 | cfg = bgx_reg_read(bgx, lmac_idx, BGX_GMP_PCS_MRX_CTL); |
| 421 | if (enable) |
| 422 | cfg |= PCS_MRX_CTL_LOOPBACK1; |
| 423 | else |
| 424 | cfg &= ~PCS_MRX_CTL_LOOPBACK1; |
| 425 | bgx_reg_write(bgx, lmac_idx, BGX_GMP_PCS_MRX_CTL, cfg); |
| 426 | } else { |
| 427 | cfg = bgx_reg_read(bgx, lmac_idx, BGX_SPUX_CONTROL1); |
| 428 | if (enable) |
| 429 | cfg |= SPU_CTL_LOOPBACK; |
| 430 | else |
| 431 | cfg &= ~SPU_CTL_LOOPBACK; |
| 432 | bgx_reg_write(bgx, lmac_idx, BGX_SPUX_CONTROL1, cfg); |
| 433 | } |
| 434 | } |
| 435 | EXPORT_SYMBOL(bgx_lmac_internal_loopback); |
| 436 | |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 437 | static int bgx_lmac_sgmii_init(struct bgx *bgx, struct lmac *lmac) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 438 | { |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 439 | int lmacid = lmac->lmacid; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 440 | u64 cfg; |
| 441 | |
| 442 | bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_TXX_THRESH, 0x30); |
| 443 | /* max packet size */ |
| 444 | bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_RXX_JABBER, MAX_FRAME_SIZE); |
| 445 | |
| 446 | /* Disable frame alignment if using preamble */ |
| 447 | cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND); |
| 448 | if (cfg & 1) |
| 449 | bgx_reg_write(bgx, lmacid, BGX_GMP_GMI_TXX_SGMII_CTL, 0); |
| 450 | |
| 451 | /* Enable lmac */ |
| 452 | bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN); |
| 453 | |
| 454 | /* PCS reset */ |
| 455 | bgx_reg_modify(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, PCS_MRX_CTL_RESET); |
| 456 | if (bgx_poll_reg(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, |
| 457 | PCS_MRX_CTL_RESET, true)) { |
| 458 | dev_err(&bgx->pdev->dev, "BGX PCS reset not completed\n"); |
| 459 | return -1; |
| 460 | } |
| 461 | |
| 462 | /* power down, reset autoneg, autoneg enable */ |
| 463 | cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_PCS_MRX_CTL); |
| 464 | cfg &= ~PCS_MRX_CTL_PWR_DN; |
Thanneeru Srinivasulu | 075ad76 | 2017-02-08 18:09:00 +0530 | [diff] [blame^] | 465 | cfg |= PCS_MRX_CTL_RST_AN; |
| 466 | if (lmac->phydev) { |
| 467 | cfg |= PCS_MRX_CTL_AN_EN; |
| 468 | } else { |
| 469 | /* In scenarios where PHY driver is not present or it's a |
| 470 | * non-standard PHY, FW sets AN_EN to inform Linux driver |
| 471 | * to do auto-neg and link polling or not. |
| 472 | */ |
| 473 | if (cfg & PCS_MRX_CTL_AN_EN) |
| 474 | lmac->autoneg = true; |
| 475 | } |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 476 | bgx_reg_write(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, cfg); |
| 477 | |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 478 | if (lmac->lmac_type == BGX_MODE_QSGMII) { |
| 479 | /* Disable disparity check for QSGMII */ |
| 480 | cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_PCS_MISCX_CTL); |
| 481 | cfg &= ~PCS_MISC_CTL_DISP_EN; |
| 482 | bgx_reg_write(bgx, lmacid, BGX_GMP_PCS_MISCX_CTL, cfg); |
| 483 | return 0; |
| 484 | } |
| 485 | |
Thanneeru Srinivasulu | 075ad76 | 2017-02-08 18:09:00 +0530 | [diff] [blame^] | 486 | if ((lmac->lmac_type == BGX_MODE_SGMII) && lmac->phydev) { |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 487 | if (bgx_poll_reg(bgx, lmacid, BGX_GMP_PCS_MRX_STATUS, |
| 488 | PCS_MRX_STATUS_AN_CPT, false)) { |
| 489 | dev_err(&bgx->pdev->dev, "BGX AN_CPT not completed\n"); |
| 490 | return -1; |
| 491 | } |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 497 | static int bgx_lmac_xaui_init(struct bgx *bgx, struct lmac *lmac) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 498 | { |
| 499 | u64 cfg; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 500 | int lmacid = lmac->lmacid; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 501 | |
| 502 | /* Reset SPU */ |
| 503 | bgx_reg_modify(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET); |
| 504 | if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET, true)) { |
| 505 | dev_err(&bgx->pdev->dev, "BGX SPU reset not completed\n"); |
| 506 | return -1; |
| 507 | } |
| 508 | |
| 509 | /* Disable LMAC */ |
| 510 | cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG); |
| 511 | cfg &= ~CMR_EN; |
| 512 | bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg); |
| 513 | |
| 514 | bgx_reg_modify(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_LOW_POWER); |
| 515 | /* Set interleaved running disparity for RXAUI */ |
Sunil Goutham | 93db2cf | 2016-08-12 16:51:44 +0530 | [diff] [blame] | 516 | if (lmac->lmac_type == BGX_MODE_RXAUI) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 517 | bgx_reg_modify(bgx, lmacid, BGX_SPUX_MISC_CONTROL, |
Sunil Goutham | 93db2cf | 2016-08-12 16:51:44 +0530 | [diff] [blame] | 518 | SPU_MISC_CTL_INTLV_RDISP); |
| 519 | |
| 520 | /* Clear receive packet disable */ |
| 521 | cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_MISC_CONTROL); |
| 522 | cfg &= ~SPU_MISC_CTL_RX_DIS; |
| 523 | bgx_reg_write(bgx, lmacid, BGX_SPUX_MISC_CONTROL, cfg); |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 524 | |
| 525 | /* clear all interrupts */ |
| 526 | cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_RX_INT); |
| 527 | bgx_reg_write(bgx, lmacid, BGX_SMUX_RX_INT, cfg); |
| 528 | cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_INT); |
| 529 | bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_INT, cfg); |
| 530 | cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT); |
| 531 | bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg); |
| 532 | |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 533 | if (lmac->use_training) { |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 534 | bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LP_CUP, 0x00); |
| 535 | bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LD_CUP, 0x00); |
| 536 | bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LD_REP, 0x00); |
| 537 | /* training enable */ |
| 538 | bgx_reg_modify(bgx, lmacid, |
| 539 | BGX_SPUX_BR_PMD_CRTL, SPU_PMD_CRTL_TRAIN_EN); |
| 540 | } |
| 541 | |
| 542 | /* Append FCS to each packet */ |
| 543 | bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_APPEND, SMU_TX_APPEND_FCS_D); |
| 544 | |
| 545 | /* Disable forward error correction */ |
| 546 | cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_FEC_CONTROL); |
| 547 | cfg &= ~SPU_FEC_CTL_FEC_EN; |
| 548 | bgx_reg_write(bgx, lmacid, BGX_SPUX_FEC_CONTROL, cfg); |
| 549 | |
| 550 | /* Disable autoneg */ |
| 551 | cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_AN_CONTROL); |
| 552 | cfg = cfg & ~(SPU_AN_CTL_AN_EN | SPU_AN_CTL_XNP_EN); |
| 553 | bgx_reg_write(bgx, lmacid, BGX_SPUX_AN_CONTROL, cfg); |
| 554 | |
| 555 | cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_AN_ADV); |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 556 | if (lmac->lmac_type == BGX_MODE_10G_KR) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 557 | cfg |= (1 << 23); |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 558 | else if (lmac->lmac_type == BGX_MODE_40G_KR) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 559 | cfg |= (1 << 24); |
| 560 | else |
| 561 | cfg &= ~((1 << 23) | (1 << 24)); |
| 562 | cfg = cfg & (~((1ULL << 25) | (1ULL << 22) | (1ULL << 12))); |
| 563 | bgx_reg_write(bgx, lmacid, BGX_SPUX_AN_ADV, cfg); |
| 564 | |
| 565 | cfg = bgx_reg_read(bgx, 0, BGX_SPU_DBG_CONTROL); |
| 566 | cfg &= ~SPU_DBG_CTL_AN_ARB_LINK_CHK_EN; |
| 567 | bgx_reg_write(bgx, 0, BGX_SPU_DBG_CONTROL, cfg); |
| 568 | |
| 569 | /* Enable lmac */ |
| 570 | bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN); |
| 571 | |
| 572 | cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_CONTROL1); |
| 573 | cfg &= ~SPU_CTL_LOW_POWER; |
| 574 | bgx_reg_write(bgx, lmacid, BGX_SPUX_CONTROL1, cfg); |
| 575 | |
| 576 | cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_CTL); |
| 577 | cfg &= ~SMU_TX_CTL_UNI_EN; |
| 578 | cfg |= SMU_TX_CTL_DIC_EN; |
| 579 | bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_CTL, cfg); |
| 580 | |
Sunil Goutham | 430da20 | 2016-11-24 14:48:03 +0530 | [diff] [blame] | 581 | /* Enable receive and transmission of pause frames */ |
| 582 | bgx_reg_write(bgx, lmacid, BGX_SMUX_CBFC_CTL, ((0xffffULL << 32) | |
| 583 | BCK_EN | DRP_EN | TX_EN | RX_EN)); |
| 584 | /* Configure pause time and interval */ |
| 585 | bgx_reg_write(bgx, lmacid, |
| 586 | BGX_SMUX_TX_PAUSE_PKT_TIME, DEFAULT_PAUSE_TIME); |
| 587 | cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_PAUSE_PKT_INTERVAL); |
| 588 | cfg &= ~0xFFFFull; |
| 589 | bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_PAUSE_PKT_INTERVAL, |
| 590 | cfg | (DEFAULT_PAUSE_TIME - 0x1000)); |
| 591 | bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_PAUSE_ZERO, 0x01); |
| 592 | |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 593 | /* take lmac_count into account */ |
| 594 | bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_THRESH, (0x100 - 1)); |
| 595 | /* max packet size */ |
| 596 | bgx_reg_modify(bgx, lmacid, BGX_SMUX_RX_JABBER, MAX_FRAME_SIZE); |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | static int bgx_xaui_check_link(struct lmac *lmac) |
| 602 | { |
| 603 | struct bgx *bgx = lmac->bgx; |
| 604 | int lmacid = lmac->lmacid; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 605 | int lmac_type = lmac->lmac_type; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 606 | u64 cfg; |
| 607 | |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 608 | if (lmac->use_training) { |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 609 | cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT); |
| 610 | if (!(cfg & (1ull << 13))) { |
| 611 | cfg = (1ull << 13) | (1ull << 14); |
| 612 | bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg); |
| 613 | cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL); |
| 614 | cfg |= (1ull << 0); |
| 615 | bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL, cfg); |
| 616 | return -1; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /* wait for PCS to come out of reset */ |
| 621 | if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET, true)) { |
| 622 | dev_err(&bgx->pdev->dev, "BGX SPU reset not completed\n"); |
| 623 | return -1; |
| 624 | } |
| 625 | |
| 626 | if ((lmac_type == BGX_MODE_10G_KR) || (lmac_type == BGX_MODE_XFI) || |
| 627 | (lmac_type == BGX_MODE_40G_KR) || (lmac_type == BGX_MODE_XLAUI)) { |
| 628 | if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_BR_STATUS1, |
| 629 | SPU_BR_STATUS_BLK_LOCK, false)) { |
| 630 | dev_err(&bgx->pdev->dev, |
| 631 | "SPU_BR_STATUS_BLK_LOCK not completed\n"); |
| 632 | return -1; |
| 633 | } |
| 634 | } else { |
| 635 | if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_BX_STATUS, |
| 636 | SPU_BX_STATUS_RX_ALIGN, false)) { |
| 637 | dev_err(&bgx->pdev->dev, |
| 638 | "SPU_BX_STATUS_RX_ALIGN not completed\n"); |
| 639 | return -1; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /* Clear rcvflt bit (latching high) and read it back */ |
Sunil Goutham | 3f4c68c | 2016-06-27 15:30:02 +0530 | [diff] [blame] | 644 | if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT) |
| 645 | bgx_reg_modify(bgx, lmacid, |
| 646 | BGX_SPUX_STATUS2, SPU_STATUS2_RCVFLT); |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 647 | if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT) { |
| 648 | dev_err(&bgx->pdev->dev, "Receive fault, retry training\n"); |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 649 | if (lmac->use_training) { |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 650 | cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT); |
| 651 | if (!(cfg & (1ull << 13))) { |
| 652 | cfg = (1ull << 13) | (1ull << 14); |
| 653 | bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg); |
| 654 | cfg = bgx_reg_read(bgx, lmacid, |
| 655 | BGX_SPUX_BR_PMD_CRTL); |
| 656 | cfg |= (1ull << 0); |
| 657 | bgx_reg_write(bgx, lmacid, |
| 658 | BGX_SPUX_BR_PMD_CRTL, cfg); |
| 659 | return -1; |
| 660 | } |
| 661 | } |
| 662 | return -1; |
| 663 | } |
| 664 | |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 665 | /* Wait for BGX RX to be idle */ |
| 666 | if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_CTL, SMU_CTL_RX_IDLE, false)) { |
| 667 | dev_err(&bgx->pdev->dev, "SMU RX not idle\n"); |
| 668 | return -1; |
| 669 | } |
| 670 | |
| 671 | /* Wait for BGX TX to be idle */ |
| 672 | if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_CTL, SMU_CTL_TX_IDLE, false)) { |
| 673 | dev_err(&bgx->pdev->dev, "SMU TX not idle\n"); |
| 674 | return -1; |
| 675 | } |
| 676 | |
Sunil Goutham | 3f4c68c | 2016-06-27 15:30:02 +0530 | [diff] [blame] | 677 | /* Check for MAC RX faults */ |
| 678 | cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_RX_CTL); |
| 679 | /* 0 - Link is okay, 1 - Local fault, 2 - Remote fault */ |
| 680 | cfg &= SMU_RX_CTL_STATUS; |
| 681 | if (!cfg) |
| 682 | return 0; |
| 683 | |
| 684 | /* Rx local/remote fault seen. |
| 685 | * Do lmac reinit to see if condition recovers |
| 686 | */ |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 687 | bgx_lmac_xaui_init(bgx, lmac); |
Sunil Goutham | 3f4c68c | 2016-06-27 15:30:02 +0530 | [diff] [blame] | 688 | |
| 689 | return -1; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Thanneeru Srinivasulu | 075ad76 | 2017-02-08 18:09:00 +0530 | [diff] [blame^] | 692 | static void bgx_poll_for_sgmii_link(struct lmac *lmac) |
| 693 | { |
| 694 | u64 pcs_link, an_result; |
| 695 | u8 speed; |
| 696 | |
| 697 | pcs_link = bgx_reg_read(lmac->bgx, lmac->lmacid, |
| 698 | BGX_GMP_PCS_MRX_STATUS); |
| 699 | |
| 700 | /*Link state bit is sticky, read it again*/ |
| 701 | if (!(pcs_link & PCS_MRX_STATUS_LINK)) |
| 702 | pcs_link = bgx_reg_read(lmac->bgx, lmac->lmacid, |
| 703 | BGX_GMP_PCS_MRX_STATUS); |
| 704 | |
| 705 | if (bgx_poll_reg(lmac->bgx, lmac->lmacid, BGX_GMP_PCS_MRX_STATUS, |
| 706 | PCS_MRX_STATUS_AN_CPT, false)) { |
| 707 | lmac->link_up = false; |
| 708 | lmac->last_speed = SPEED_UNKNOWN; |
| 709 | lmac->last_duplex = DUPLEX_UNKNOWN; |
| 710 | goto next_poll; |
| 711 | } |
| 712 | |
| 713 | lmac->link_up = ((pcs_link & PCS_MRX_STATUS_LINK) != 0) ? true : false; |
| 714 | an_result = bgx_reg_read(lmac->bgx, lmac->lmacid, |
| 715 | BGX_GMP_PCS_ANX_AN_RESULTS); |
| 716 | |
| 717 | speed = (an_result >> 3) & 0x3; |
| 718 | lmac->last_duplex = (an_result >> 1) & 0x1; |
| 719 | switch (speed) { |
| 720 | case 0: |
| 721 | lmac->last_speed = 10; |
| 722 | break; |
| 723 | case 1: |
| 724 | lmac->last_speed = 100; |
| 725 | break; |
| 726 | case 2: |
| 727 | lmac->last_speed = 1000; |
| 728 | break; |
| 729 | default: |
| 730 | lmac->link_up = false; |
| 731 | lmac->last_speed = SPEED_UNKNOWN; |
| 732 | lmac->last_duplex = DUPLEX_UNKNOWN; |
| 733 | break; |
| 734 | } |
| 735 | |
| 736 | next_poll: |
| 737 | |
| 738 | if (lmac->last_link != lmac->link_up) { |
| 739 | if (lmac->link_up) |
| 740 | bgx_sgmii_change_link_state(lmac); |
| 741 | lmac->last_link = lmac->link_up; |
| 742 | } |
| 743 | |
| 744 | queue_delayed_work(lmac->check_link, &lmac->dwork, HZ * 3); |
| 745 | } |
| 746 | |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 747 | static void bgx_poll_for_link(struct work_struct *work) |
| 748 | { |
| 749 | struct lmac *lmac; |
Sunil Goutham | 3f4c68c | 2016-06-27 15:30:02 +0530 | [diff] [blame] | 750 | u64 spu_link, smu_link; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 751 | |
| 752 | lmac = container_of(work, struct lmac, dwork.work); |
Thanneeru Srinivasulu | 075ad76 | 2017-02-08 18:09:00 +0530 | [diff] [blame^] | 753 | if (lmac->is_sgmii) { |
| 754 | bgx_poll_for_sgmii_link(lmac); |
| 755 | return; |
| 756 | } |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 757 | |
| 758 | /* Receive link is latching low. Force it high and verify it */ |
| 759 | bgx_reg_modify(lmac->bgx, lmac->lmacid, |
| 760 | BGX_SPUX_STATUS1, SPU_STATUS1_RCV_LNK); |
| 761 | bgx_poll_reg(lmac->bgx, lmac->lmacid, BGX_SPUX_STATUS1, |
| 762 | SPU_STATUS1_RCV_LNK, false); |
| 763 | |
Sunil Goutham | 3f4c68c | 2016-06-27 15:30:02 +0530 | [diff] [blame] | 764 | spu_link = bgx_reg_read(lmac->bgx, lmac->lmacid, BGX_SPUX_STATUS1); |
| 765 | smu_link = bgx_reg_read(lmac->bgx, lmac->lmacid, BGX_SMUX_RX_CTL); |
| 766 | |
| 767 | if ((spu_link & SPU_STATUS1_RCV_LNK) && |
| 768 | !(smu_link & SMU_RX_CTL_STATUS)) { |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 769 | lmac->link_up = 1; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 770 | if (lmac->lmac_type == BGX_MODE_XLAUI) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 771 | lmac->last_speed = 40000; |
| 772 | else |
| 773 | lmac->last_speed = 10000; |
| 774 | lmac->last_duplex = 1; |
| 775 | } else { |
| 776 | lmac->link_up = 0; |
Sunil Goutham | 0b72a9a | 2015-12-02 15:36:16 +0530 | [diff] [blame] | 777 | lmac->last_speed = SPEED_UNKNOWN; |
| 778 | lmac->last_duplex = DUPLEX_UNKNOWN; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | if (lmac->last_link != lmac->link_up) { |
Sunil Goutham | 3f4c68c | 2016-06-27 15:30:02 +0530 | [diff] [blame] | 782 | if (lmac->link_up) { |
| 783 | if (bgx_xaui_check_link(lmac)) { |
| 784 | /* Errors, clear link_up state */ |
| 785 | lmac->link_up = 0; |
| 786 | lmac->last_speed = SPEED_UNKNOWN; |
| 787 | lmac->last_duplex = DUPLEX_UNKNOWN; |
| 788 | } |
| 789 | } |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 790 | lmac->last_link = lmac->link_up; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | queue_delayed_work(lmac->check_link, &lmac->dwork, HZ * 2); |
| 794 | } |
| 795 | |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 796 | static int phy_interface_mode(u8 lmac_type) |
| 797 | { |
| 798 | if (lmac_type == BGX_MODE_QSGMII) |
| 799 | return PHY_INTERFACE_MODE_QSGMII; |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 800 | if (lmac_type == BGX_MODE_RGMII) |
| 801 | return PHY_INTERFACE_MODE_RGMII; |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 802 | |
| 803 | return PHY_INTERFACE_MODE_SGMII; |
| 804 | } |
| 805 | |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 806 | static int bgx_lmac_enable(struct bgx *bgx, u8 lmacid) |
| 807 | { |
| 808 | struct lmac *lmac; |
| 809 | u64 cfg; |
| 810 | |
| 811 | lmac = &bgx->lmac[lmacid]; |
| 812 | lmac->bgx = bgx; |
| 813 | |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 814 | if ((lmac->lmac_type == BGX_MODE_SGMII) || |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 815 | (lmac->lmac_type == BGX_MODE_QSGMII) || |
| 816 | (lmac->lmac_type == BGX_MODE_RGMII)) { |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 817 | lmac->is_sgmii = 1; |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 818 | if (bgx_lmac_sgmii_init(bgx, lmac)) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 819 | return -1; |
| 820 | } else { |
| 821 | lmac->is_sgmii = 0; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 822 | if (bgx_lmac_xaui_init(bgx, lmac)) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 823 | return -1; |
| 824 | } |
| 825 | |
| 826 | if (lmac->is_sgmii) { |
| 827 | cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND); |
| 828 | cfg |= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */ |
| 829 | bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND, cfg); |
| 830 | bgx_reg_write(bgx, lmacid, BGX_GMP_GMI_TXX_MIN_PKT, 60 - 1); |
| 831 | } else { |
| 832 | cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_APPEND); |
| 833 | cfg |= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */ |
| 834 | bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_APPEND, cfg); |
| 835 | bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_MIN_PKT, 60 + 4); |
| 836 | } |
| 837 | |
| 838 | /* Enable lmac */ |
Sunil Goutham | bc69fdf | 2015-12-02 15:36:17 +0530 | [diff] [blame] | 839 | bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN); |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 840 | |
| 841 | /* Restore default cfg, incase low level firmware changed it */ |
| 842 | bgx_reg_write(bgx, lmacid, BGX_CMRX_RX_DMAC_CTL, 0x03); |
| 843 | |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 844 | if ((lmac->lmac_type != BGX_MODE_XFI) && |
| 845 | (lmac->lmac_type != BGX_MODE_XLAUI) && |
| 846 | (lmac->lmac_type != BGX_MODE_40G_KR) && |
| 847 | (lmac->lmac_type != BGX_MODE_10G_KR)) { |
Thanneeru Srinivasulu | 075ad76 | 2017-02-08 18:09:00 +0530 | [diff] [blame^] | 848 | if (!lmac->phydev) { |
| 849 | if (lmac->autoneg) { |
| 850 | bgx_reg_write(bgx, lmacid, |
| 851 | BGX_GMP_PCS_LINKX_TIMER, |
| 852 | PCS_LINKX_TIMER_COUNT); |
| 853 | goto poll; |
| 854 | } else { |
| 855 | /* Default to below link speed and duplex */ |
| 856 | lmac->link_up = true; |
| 857 | lmac->last_speed = 1000; |
| 858 | lmac->last_duplex = 1; |
| 859 | bgx_sgmii_change_link_state(lmac); |
| 860 | return 0; |
| 861 | } |
| 862 | } |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 863 | lmac->phydev->dev_flags = 0; |
| 864 | |
| 865 | if (phy_connect_direct(&lmac->netdev, lmac->phydev, |
| 866 | bgx_lmac_handler, |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 867 | phy_interface_mode(lmac->lmac_type))) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 868 | return -ENODEV; |
| 869 | |
| 870 | phy_start_aneg(lmac->phydev); |
Thanneeru Srinivasulu | 075ad76 | 2017-02-08 18:09:00 +0530 | [diff] [blame^] | 871 | return 0; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Thanneeru Srinivasulu | 075ad76 | 2017-02-08 18:09:00 +0530 | [diff] [blame^] | 874 | poll: |
| 875 | lmac->check_link = alloc_workqueue("check_link", WQ_UNBOUND | |
| 876 | WQ_MEM_RECLAIM, 1); |
| 877 | if (!lmac->check_link) |
| 878 | return -ENOMEM; |
| 879 | INIT_DELAYED_WORK(&lmac->dwork, bgx_poll_for_link); |
| 880 | queue_delayed_work(lmac->check_link, &lmac->dwork, 0); |
| 881 | |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 882 | return 0; |
| 883 | } |
| 884 | |
Aleksey Makarov | fd7ec06 | 2015-06-02 11:00:23 -0700 | [diff] [blame] | 885 | static void bgx_lmac_disable(struct bgx *bgx, u8 lmacid) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 886 | { |
| 887 | struct lmac *lmac; |
Sunil Goutham | 3f4c68c | 2016-06-27 15:30:02 +0530 | [diff] [blame] | 888 | u64 cfg; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 889 | |
| 890 | lmac = &bgx->lmac[lmacid]; |
| 891 | if (lmac->check_link) { |
| 892 | /* Destroy work queue */ |
Thanneeru Srinivasulu | a7b1f53 | 2015-12-02 15:36:14 +0530 | [diff] [blame] | 893 | cancel_delayed_work_sync(&lmac->dwork); |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 894 | destroy_workqueue(lmac->check_link); |
| 895 | } |
| 896 | |
Sunil Goutham | 3f4c68c | 2016-06-27 15:30:02 +0530 | [diff] [blame] | 897 | /* Disable packet reception */ |
| 898 | cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG); |
| 899 | cfg &= ~CMR_PKT_RX_EN; |
| 900 | bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg); |
| 901 | |
| 902 | /* Give chance for Rx/Tx FIFO to get drained */ |
| 903 | bgx_poll_reg(bgx, lmacid, BGX_CMRX_RX_FIFO_LEN, (u64)0x1FFF, true); |
| 904 | bgx_poll_reg(bgx, lmacid, BGX_CMRX_TX_FIFO_LEN, (u64)0x3FFF, true); |
| 905 | |
| 906 | /* Disable packet transmission */ |
| 907 | cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG); |
| 908 | cfg &= ~CMR_PKT_TX_EN; |
| 909 | bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg); |
| 910 | |
| 911 | /* Disable serdes lanes */ |
| 912 | if (!lmac->is_sgmii) |
| 913 | bgx_reg_modify(bgx, lmacid, |
| 914 | BGX_SPUX_CONTROL1, SPU_CTL_LOW_POWER); |
| 915 | else |
| 916 | bgx_reg_modify(bgx, lmacid, |
| 917 | BGX_GMP_PCS_MRX_CTL, PCS_MRX_CTL_PWR_DN); |
| 918 | |
| 919 | /* Disable LMAC */ |
| 920 | cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG); |
| 921 | cfg &= ~CMR_EN; |
| 922 | bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg); |
| 923 | |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 924 | bgx_flush_dmac_addrs(bgx, lmacid); |
| 925 | |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 926 | if ((lmac->lmac_type != BGX_MODE_XFI) && |
| 927 | (lmac->lmac_type != BGX_MODE_XLAUI) && |
| 928 | (lmac->lmac_type != BGX_MODE_40G_KR) && |
| 929 | (lmac->lmac_type != BGX_MODE_10G_KR) && lmac->phydev) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 930 | phy_disconnect(lmac->phydev); |
| 931 | |
| 932 | lmac->phydev = NULL; |
| 933 | } |
| 934 | |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 935 | static void bgx_init_hw(struct bgx *bgx) |
| 936 | { |
| 937 | int i; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 938 | struct lmac *lmac; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 939 | |
| 940 | bgx_reg_modify(bgx, 0, BGX_CMR_GLOBAL_CFG, CMR_GLOBAL_CFG_FCS_STRIP); |
| 941 | if (bgx_reg_read(bgx, 0, BGX_CMR_BIST_STATUS)) |
| 942 | dev_err(&bgx->pdev->dev, "BGX%d BIST failed\n", bgx->bgx_id); |
| 943 | |
| 944 | /* Set lmac type and lane2serdes mapping */ |
| 945 | for (i = 0; i < bgx->lmac_count; i++) { |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 946 | lmac = &bgx->lmac[i]; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 947 | bgx_reg_write(bgx, i, BGX_CMRX_CFG, |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 948 | (lmac->lmac_type << 8) | lmac->lane_to_sds); |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 949 | bgx->lmac[i].lmacid_bd = lmac_count; |
| 950 | lmac_count++; |
| 951 | } |
| 952 | |
| 953 | bgx_reg_write(bgx, 0, BGX_CMR_TX_LMACS, bgx->lmac_count); |
| 954 | bgx_reg_write(bgx, 0, BGX_CMR_RX_LMACS, bgx->lmac_count); |
| 955 | |
| 956 | /* Set the backpressure AND mask */ |
| 957 | for (i = 0; i < bgx->lmac_count; i++) |
| 958 | bgx_reg_modify(bgx, 0, BGX_CMR_CHAN_MSK_AND, |
| 959 | ((1ULL << MAX_BGX_CHANS_PER_LMAC) - 1) << |
| 960 | (i * MAX_BGX_CHANS_PER_LMAC)); |
| 961 | |
| 962 | /* Disable all MAC filtering */ |
| 963 | for (i = 0; i < RX_DMAC_COUNT; i++) |
| 964 | bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM + (i * 8), 0x00); |
| 965 | |
| 966 | /* Disable MAC steering (NCSI traffic) */ |
| 967 | for (i = 0; i < RX_TRAFFIC_STEER_RULE_COUNT; i++) |
| 968 | bgx_reg_write(bgx, 0, BGX_CMR_RX_STREERING + (i * 8), 0x00); |
| 969 | } |
| 970 | |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 971 | static u8 bgx_get_lane2sds_cfg(struct bgx *bgx, struct lmac *lmac) |
| 972 | { |
| 973 | return (u8)(bgx_reg_read(bgx, lmac->lmacid, BGX_CMRX_CFG) & 0xFF); |
| 974 | } |
| 975 | |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 976 | static void bgx_print_qlm_mode(struct bgx *bgx, u8 lmacid) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 977 | { |
| 978 | struct device *dev = &bgx->pdev->dev; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 979 | struct lmac *lmac; |
| 980 | char str[20]; |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 981 | u8 dlm; |
| 982 | |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 983 | if (lmacid > bgx->max_lmac) |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 984 | return; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 985 | |
| 986 | lmac = &bgx->lmac[lmacid]; |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 987 | dlm = (lmacid / 2) + (bgx->bgx_id * 2); |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 988 | if (!bgx->is_dlm) |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 989 | sprintf(str, "BGX%d QLM mode", bgx->bgx_id); |
| 990 | else |
| 991 | sprintf(str, "BGX%d DLM%d mode", bgx->bgx_id, dlm); |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 992 | |
| 993 | switch (lmac->lmac_type) { |
| 994 | case BGX_MODE_SGMII: |
| 995 | dev_info(dev, "%s: SGMII\n", (char *)str); |
| 996 | break; |
| 997 | case BGX_MODE_XAUI: |
| 998 | dev_info(dev, "%s: XAUI\n", (char *)str); |
| 999 | break; |
| 1000 | case BGX_MODE_RXAUI: |
| 1001 | dev_info(dev, "%s: RXAUI\n", (char *)str); |
| 1002 | break; |
| 1003 | case BGX_MODE_XFI: |
| 1004 | if (!lmac->use_training) |
| 1005 | dev_info(dev, "%s: XFI\n", (char *)str); |
| 1006 | else |
| 1007 | dev_info(dev, "%s: 10G_KR\n", (char *)str); |
| 1008 | break; |
| 1009 | case BGX_MODE_XLAUI: |
| 1010 | if (!lmac->use_training) |
| 1011 | dev_info(dev, "%s: XLAUI\n", (char *)str); |
| 1012 | else |
| 1013 | dev_info(dev, "%s: 40G_KR4\n", (char *)str); |
| 1014 | break; |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 1015 | case BGX_MODE_QSGMII: |
| 1016 | if ((lmacid == 0) && |
| 1017 | (bgx_get_lane2sds_cfg(bgx, lmac) != lmacid)) |
| 1018 | return; |
| 1019 | if ((lmacid == 2) && |
| 1020 | (bgx_get_lane2sds_cfg(bgx, lmac) == lmacid)) |
| 1021 | return; |
| 1022 | dev_info(dev, "%s: QSGMII\n", (char *)str); |
| 1023 | break; |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 1024 | case BGX_MODE_RGMII: |
| 1025 | dev_info(dev, "%s: RGMII\n", (char *)str); |
| 1026 | break; |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 1027 | case BGX_MODE_INVALID: |
| 1028 | /* Nothing to do */ |
| 1029 | break; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 1033 | static void lmac_set_lane2sds(struct bgx *bgx, struct lmac *lmac) |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 1034 | { |
| 1035 | switch (lmac->lmac_type) { |
| 1036 | case BGX_MODE_SGMII: |
| 1037 | case BGX_MODE_XFI: |
| 1038 | lmac->lane_to_sds = lmac->lmacid; |
| 1039 | break; |
| 1040 | case BGX_MODE_XAUI: |
| 1041 | case BGX_MODE_XLAUI: |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 1042 | case BGX_MODE_RGMII: |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 1043 | lmac->lane_to_sds = 0xE4; |
| 1044 | break; |
| 1045 | case BGX_MODE_RXAUI: |
| 1046 | lmac->lane_to_sds = (lmac->lmacid) ? 0xE : 0x4; |
| 1047 | break; |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 1048 | case BGX_MODE_QSGMII: |
| 1049 | /* There is no way to determine if DLM0/2 is QSGMII or |
| 1050 | * DLM1/3 is configured to QSGMII as bootloader will |
| 1051 | * configure all LMACs, so take whatever is configured |
| 1052 | * by low level firmware. |
| 1053 | */ |
| 1054 | lmac->lane_to_sds = bgx_get_lane2sds_cfg(bgx, lmac); |
| 1055 | break; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 1056 | default: |
| 1057 | lmac->lane_to_sds = 0; |
| 1058 | break; |
| 1059 | } |
| 1060 | } |
| 1061 | |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 1062 | static void lmac_set_training(struct bgx *bgx, struct lmac *lmac, int lmacid) |
| 1063 | { |
| 1064 | if ((lmac->lmac_type != BGX_MODE_10G_KR) && |
| 1065 | (lmac->lmac_type != BGX_MODE_40G_KR)) { |
| 1066 | lmac->use_training = 0; |
| 1067 | return; |
| 1068 | } |
| 1069 | |
| 1070 | lmac->use_training = bgx_reg_read(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL) & |
| 1071 | SPU_PMD_CRTL_TRAIN_EN; |
| 1072 | } |
| 1073 | |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 1074 | static void bgx_set_lmac_config(struct bgx *bgx, u8 idx) |
| 1075 | { |
| 1076 | struct lmac *lmac; |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1077 | struct lmac *olmac; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 1078 | u64 cmr_cfg; |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1079 | u8 lmac_type; |
| 1080 | u8 lane_to_sds; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 1081 | |
| 1082 | lmac = &bgx->lmac[idx]; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1083 | |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 1084 | if (!bgx->is_dlm || bgx->is_rgx) { |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1085 | /* Read LMAC0 type to figure out QLM mode |
| 1086 | * This is configured by low level firmware |
| 1087 | */ |
| 1088 | cmr_cfg = bgx_reg_read(bgx, 0, BGX_CMRX_CFG); |
| 1089 | lmac->lmac_type = (cmr_cfg >> 8) & 0x07; |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 1090 | if (bgx->is_rgx) |
| 1091 | lmac->lmac_type = BGX_MODE_RGMII; |
| 1092 | lmac_set_training(bgx, lmac, 0); |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 1093 | lmac_set_lane2sds(bgx, lmac); |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1094 | return; |
| 1095 | } |
| 1096 | |
| 1097 | /* On 81xx BGX can be split across 2 DLMs |
| 1098 | * firmware programs lmac_type of LMAC0 and LMAC2 |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1099 | */ |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1100 | if ((idx == 0) || (idx == 2)) { |
| 1101 | cmr_cfg = bgx_reg_read(bgx, idx, BGX_CMRX_CFG); |
| 1102 | lmac_type = (u8)((cmr_cfg >> 8) & 0x07); |
| 1103 | lane_to_sds = (u8)(cmr_cfg & 0xFF); |
| 1104 | /* Check if config is not reset value */ |
| 1105 | if ((lmac_type == 0) && (lane_to_sds == 0xE4)) |
| 1106 | lmac->lmac_type = BGX_MODE_INVALID; |
| 1107 | else |
| 1108 | lmac->lmac_type = lmac_type; |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 1109 | lmac_set_training(bgx, lmac, lmac->lmacid); |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 1110 | lmac_set_lane2sds(bgx, lmac); |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1111 | |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1112 | olmac = &bgx->lmac[idx + 1]; |
Sunil Goutham | 5271156 | 2016-11-24 14:48:00 +0530 | [diff] [blame] | 1113 | /* Check if other LMAC on the same DLM is already configured by |
| 1114 | * firmware, if so use the same config or else set as same, as |
| 1115 | * that of LMAC 0/2. |
| 1116 | * This check is needed as on 80xx only one lane of each of the |
| 1117 | * DLM of BGX0 is used, so have to rely on firmware for |
| 1118 | * distingushing 80xx from 81xx. |
| 1119 | */ |
| 1120 | cmr_cfg = bgx_reg_read(bgx, idx + 1, BGX_CMRX_CFG); |
| 1121 | lmac_type = (u8)((cmr_cfg >> 8) & 0x07); |
| 1122 | lane_to_sds = (u8)(cmr_cfg & 0xFF); |
| 1123 | if ((lmac_type == 0) && (lane_to_sds == 0xE4)) { |
| 1124 | olmac->lmac_type = lmac->lmac_type; |
| 1125 | lmac_set_lane2sds(bgx, olmac); |
| 1126 | } else { |
| 1127 | olmac->lmac_type = lmac_type; |
| 1128 | olmac->lane_to_sds = lane_to_sds; |
| 1129 | } |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 1130 | lmac_set_training(bgx, olmac, olmac->lmacid); |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | static bool is_dlm0_in_bgx_mode(struct bgx *bgx) |
| 1135 | { |
| 1136 | struct lmac *lmac; |
| 1137 | |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 1138 | if (!bgx->is_dlm) |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1139 | return true; |
| 1140 | |
Sunil Goutham | 3f8057c | 2016-08-12 16:51:32 +0530 | [diff] [blame] | 1141 | lmac = &bgx->lmac[0]; |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1142 | if (lmac->lmac_type == BGX_MODE_INVALID) |
| 1143 | return false; |
| 1144 | |
| 1145 | return true; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 1146 | } |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1147 | |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 1148 | static void bgx_get_qlm_mode(struct bgx *bgx) |
| 1149 | { |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1150 | struct lmac *lmac; |
| 1151 | struct lmac *lmac01; |
| 1152 | struct lmac *lmac23; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 1153 | u8 idx; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1154 | |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1155 | /* Init all LMAC's type to invalid */ |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 1156 | for (idx = 0; idx < bgx->max_lmac; idx++) { |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1157 | lmac = &bgx->lmac[idx]; |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1158 | lmac->lmacid = idx; |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 1159 | lmac->lmac_type = BGX_MODE_INVALID; |
| 1160 | lmac->use_training = false; |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1161 | } |
| 1162 | |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 1163 | /* It is assumed that low level firmware sets this value */ |
| 1164 | bgx->lmac_count = bgx_reg_read(bgx, 0, BGX_CMR_RX_LMACS) & 0x7; |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 1165 | if (bgx->lmac_count > bgx->max_lmac) |
| 1166 | bgx->lmac_count = bgx->max_lmac; |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 1167 | |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 1168 | for (idx = 0; idx < bgx->max_lmac; idx++) |
Sunil Goutham | 0bcb7d5 | 2016-08-12 16:51:30 +0530 | [diff] [blame] | 1169 | bgx_set_lmac_config(bgx, idx); |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1170 | |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 1171 | if (!bgx->is_dlm || bgx->is_rgx) { |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1172 | bgx_print_qlm_mode(bgx, 0); |
| 1173 | return; |
| 1174 | } |
| 1175 | |
| 1176 | if (bgx->lmac_count) { |
| 1177 | bgx_print_qlm_mode(bgx, 0); |
| 1178 | bgx_print_qlm_mode(bgx, 2); |
| 1179 | } |
| 1180 | |
| 1181 | /* If DLM0 is not in BGX mode then LMAC0/1 have |
| 1182 | * to be configured with serdes lanes of DLM1 |
| 1183 | */ |
| 1184 | if (is_dlm0_in_bgx_mode(bgx) || (bgx->lmac_count > 2)) |
| 1185 | return; |
| 1186 | for (idx = 0; idx < bgx->lmac_count; idx++) { |
| 1187 | lmac01 = &bgx->lmac[idx]; |
| 1188 | lmac23 = &bgx->lmac[idx + 2]; |
| 1189 | lmac01->lmac_type = lmac23->lmac_type; |
| 1190 | lmac01->lane_to_sds = lmac23->lane_to_sds; |
| 1191 | } |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
David Daney | 46b903a | 2015-08-10 17:58:37 -0700 | [diff] [blame] | 1194 | #ifdef CONFIG_ACPI |
| 1195 | |
Robert Richter | 1d82efa | 2016-02-11 21:50:25 +0530 | [diff] [blame] | 1196 | static int acpi_get_mac_address(struct device *dev, struct acpi_device *adev, |
| 1197 | u8 *dst) |
David Daney | 46b903a | 2015-08-10 17:58:37 -0700 | [diff] [blame] | 1198 | { |
| 1199 | u8 mac[ETH_ALEN]; |
| 1200 | int ret; |
| 1201 | |
| 1202 | ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev), |
| 1203 | "mac-address", mac, ETH_ALEN); |
| 1204 | if (ret) |
| 1205 | goto out; |
| 1206 | |
| 1207 | if (!is_valid_ether_addr(mac)) { |
Robert Richter | 1d82efa | 2016-02-11 21:50:25 +0530 | [diff] [blame] | 1208 | dev_err(dev, "MAC address invalid: %pM\n", mac); |
David Daney | 46b903a | 2015-08-10 17:58:37 -0700 | [diff] [blame] | 1209 | ret = -EINVAL; |
| 1210 | goto out; |
| 1211 | } |
| 1212 | |
Robert Richter | 1d82efa | 2016-02-11 21:50:25 +0530 | [diff] [blame] | 1213 | dev_info(dev, "MAC address set to: %pM\n", mac); |
| 1214 | |
David Daney | 46b903a | 2015-08-10 17:58:37 -0700 | [diff] [blame] | 1215 | memcpy(dst, mac, ETH_ALEN); |
| 1216 | out: |
| 1217 | return ret; |
| 1218 | } |
| 1219 | |
| 1220 | /* Currently only sets the MAC address. */ |
| 1221 | static acpi_status bgx_acpi_register_phy(acpi_handle handle, |
| 1222 | u32 lvl, void *context, void **rv) |
| 1223 | { |
| 1224 | struct bgx *bgx = context; |
Robert Richter | 1d82efa | 2016-02-11 21:50:25 +0530 | [diff] [blame] | 1225 | struct device *dev = &bgx->pdev->dev; |
David Daney | 46b903a | 2015-08-10 17:58:37 -0700 | [diff] [blame] | 1226 | struct acpi_device *adev; |
| 1227 | |
| 1228 | if (acpi_bus_get_device(handle, &adev)) |
| 1229 | goto out; |
| 1230 | |
Vadim Lomovtsev | 7aa4865 | 2017-01-12 07:28:06 -0800 | [diff] [blame] | 1231 | acpi_get_mac_address(dev, adev, bgx->lmac[bgx->acpi_lmac_idx].mac); |
David Daney | 46b903a | 2015-08-10 17:58:37 -0700 | [diff] [blame] | 1232 | |
Vadim Lomovtsev | 7aa4865 | 2017-01-12 07:28:06 -0800 | [diff] [blame] | 1233 | SET_NETDEV_DEV(&bgx->lmac[bgx->acpi_lmac_idx].netdev, dev); |
David Daney | 46b903a | 2015-08-10 17:58:37 -0700 | [diff] [blame] | 1234 | |
Vadim Lomovtsev | 7aa4865 | 2017-01-12 07:28:06 -0800 | [diff] [blame] | 1235 | bgx->lmac[bgx->acpi_lmac_idx].lmacid = bgx->acpi_lmac_idx; |
| 1236 | bgx->acpi_lmac_idx++; /* move to next LMAC */ |
David Daney | 46b903a | 2015-08-10 17:58:37 -0700 | [diff] [blame] | 1237 | out: |
David Daney | 46b903a | 2015-08-10 17:58:37 -0700 | [diff] [blame] | 1238 | return AE_OK; |
| 1239 | } |
| 1240 | |
| 1241 | static acpi_status bgx_acpi_match_id(acpi_handle handle, u32 lvl, |
| 1242 | void *context, void **ret_val) |
| 1243 | { |
| 1244 | struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 1245 | struct bgx *bgx = context; |
| 1246 | char bgx_sel[5]; |
| 1247 | |
| 1248 | snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id); |
| 1249 | if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &string))) { |
| 1250 | pr_warn("Invalid link device\n"); |
| 1251 | return AE_OK; |
| 1252 | } |
| 1253 | |
| 1254 | if (strncmp(string.pointer, bgx_sel, 4)) |
| 1255 | return AE_OK; |
| 1256 | |
| 1257 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1, |
| 1258 | bgx_acpi_register_phy, NULL, bgx, NULL); |
| 1259 | |
| 1260 | kfree(string.pointer); |
| 1261 | return AE_CTRL_TERMINATE; |
| 1262 | } |
| 1263 | |
| 1264 | static int bgx_init_acpi_phy(struct bgx *bgx) |
| 1265 | { |
| 1266 | acpi_get_devices(NULL, bgx_acpi_match_id, bgx, (void **)NULL); |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | #else |
| 1271 | |
| 1272 | static int bgx_init_acpi_phy(struct bgx *bgx) |
| 1273 | { |
| 1274 | return -ENODEV; |
| 1275 | } |
| 1276 | |
| 1277 | #endif /* CONFIG_ACPI */ |
| 1278 | |
Robert Richter | de387e1 | 2015-08-10 17:58:36 -0700 | [diff] [blame] | 1279 | #if IS_ENABLED(CONFIG_OF_MDIO) |
| 1280 | |
| 1281 | static int bgx_init_of_phy(struct bgx *bgx) |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1282 | { |
David Daney | eee326f | 2016-02-11 21:50:24 +0530 | [diff] [blame] | 1283 | struct fwnode_handle *fwn; |
David Daney | b7d3e3d | 2016-03-14 17:30:39 -0700 | [diff] [blame] | 1284 | struct device_node *node = NULL; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1285 | u8 lmac = 0; |
Robert Richter | de387e1 | 2015-08-10 17:58:36 -0700 | [diff] [blame] | 1286 | |
David Daney | eee326f | 2016-02-11 21:50:24 +0530 | [diff] [blame] | 1287 | device_for_each_child_node(&bgx->pdev->dev, fwn) { |
David Daney | 5fc7cf1 | 2016-03-11 09:53:09 -0800 | [diff] [blame] | 1288 | struct phy_device *pd; |
David Daney | eee326f | 2016-02-11 21:50:24 +0530 | [diff] [blame] | 1289 | struct device_node *phy_np; |
David Daney | b7d3e3d | 2016-03-14 17:30:39 -0700 | [diff] [blame] | 1290 | const char *mac; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1291 | |
David Daney | 5fc7cf1 | 2016-03-11 09:53:09 -0800 | [diff] [blame] | 1292 | /* Should always be an OF node. But if it is not, we |
| 1293 | * cannot handle it, so exit the loop. |
David Daney | eee326f | 2016-02-11 21:50:24 +0530 | [diff] [blame] | 1294 | */ |
David Daney | b7d3e3d | 2016-03-14 17:30:39 -0700 | [diff] [blame] | 1295 | node = to_of_node(fwn); |
David Daney | eee326f | 2016-02-11 21:50:24 +0530 | [diff] [blame] | 1296 | if (!node) |
| 1297 | break; |
| 1298 | |
David Daney | eee326f | 2016-02-11 21:50:24 +0530 | [diff] [blame] | 1299 | mac = of_get_mac_address(node); |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1300 | if (mac) |
| 1301 | ether_addr_copy(bgx->lmac[lmac].mac, mac); |
| 1302 | |
| 1303 | SET_NETDEV_DEV(&bgx->lmac[lmac].netdev, &bgx->pdev->dev); |
| 1304 | bgx->lmac[lmac].lmacid = lmac; |
David Daney | 5fc7cf1 | 2016-03-11 09:53:09 -0800 | [diff] [blame] | 1305 | |
| 1306 | phy_np = of_parse_phandle(node, "phy-handle", 0); |
| 1307 | /* If there is no phy or defective firmware presents |
| 1308 | * this cortina phy, for which there is no driver |
| 1309 | * support, ignore it. |
| 1310 | */ |
| 1311 | if (phy_np && |
| 1312 | !of_device_is_compatible(phy_np, "cortina,cs4223-slice")) { |
| 1313 | /* Wait until the phy drivers are available */ |
| 1314 | pd = of_phy_find_device(phy_np); |
| 1315 | if (!pd) |
David Daney | b7d3e3d | 2016-03-14 17:30:39 -0700 | [diff] [blame] | 1316 | goto defer; |
David Daney | 5fc7cf1 | 2016-03-11 09:53:09 -0800 | [diff] [blame] | 1317 | bgx->lmac[lmac].phydev = pd; |
| 1318 | } |
| 1319 | |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1320 | lmac++; |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 1321 | if (lmac == bgx->max_lmac) { |
David Daney | 65c66af | 2016-04-08 13:37:27 -0700 | [diff] [blame] | 1322 | of_node_put(node); |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1323 | break; |
David Daney | 65c66af | 2016-04-08 13:37:27 -0700 | [diff] [blame] | 1324 | } |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1325 | } |
Robert Richter | de387e1 | 2015-08-10 17:58:36 -0700 | [diff] [blame] | 1326 | return 0; |
David Daney | b7d3e3d | 2016-03-14 17:30:39 -0700 | [diff] [blame] | 1327 | |
| 1328 | defer: |
| 1329 | /* We are bailing out, try not to leak device reference counts |
| 1330 | * for phy devices we may have already found. |
| 1331 | */ |
| 1332 | while (lmac) { |
| 1333 | if (bgx->lmac[lmac].phydev) { |
| 1334 | put_device(&bgx->lmac[lmac].phydev->mdio.dev); |
| 1335 | bgx->lmac[lmac].phydev = NULL; |
| 1336 | } |
| 1337 | lmac--; |
| 1338 | } |
| 1339 | of_node_put(node); |
| 1340 | return -EPROBE_DEFER; |
Robert Richter | de387e1 | 2015-08-10 17:58:36 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | #else |
| 1344 | |
| 1345 | static int bgx_init_of_phy(struct bgx *bgx) |
| 1346 | { |
| 1347 | return -ENODEV; |
| 1348 | } |
| 1349 | |
| 1350 | #endif /* CONFIG_OF_MDIO */ |
| 1351 | |
| 1352 | static int bgx_init_phy(struct bgx *bgx) |
| 1353 | { |
David Daney | 46b903a | 2015-08-10 17:58:37 -0700 | [diff] [blame] | 1354 | if (!acpi_disabled) |
| 1355 | return bgx_init_acpi_phy(bgx); |
| 1356 | |
Robert Richter | de387e1 | 2015-08-10 17:58:36 -0700 | [diff] [blame] | 1357 | return bgx_init_of_phy(bgx); |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | static int bgx_probe(struct pci_dev *pdev, const struct pci_device_id *ent) |
| 1361 | { |
| 1362 | int err; |
| 1363 | struct device *dev = &pdev->dev; |
| 1364 | struct bgx *bgx = NULL; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1365 | u8 lmac; |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1366 | u16 sdevid; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1367 | |
| 1368 | bgx = devm_kzalloc(dev, sizeof(*bgx), GFP_KERNEL); |
| 1369 | if (!bgx) |
| 1370 | return -ENOMEM; |
| 1371 | bgx->pdev = pdev; |
| 1372 | |
| 1373 | pci_set_drvdata(pdev, bgx); |
| 1374 | |
| 1375 | err = pci_enable_device(pdev); |
| 1376 | if (err) { |
| 1377 | dev_err(dev, "Failed to enable PCI device\n"); |
| 1378 | pci_set_drvdata(pdev, NULL); |
| 1379 | return err; |
| 1380 | } |
| 1381 | |
| 1382 | err = pci_request_regions(pdev, DRV_NAME); |
| 1383 | if (err) { |
| 1384 | dev_err(dev, "PCI request regions failed 0x%x\n", err); |
| 1385 | goto err_disable_device; |
| 1386 | } |
| 1387 | |
| 1388 | /* MAP configuration registers */ |
| 1389 | bgx->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0); |
| 1390 | if (!bgx->reg_base) { |
| 1391 | dev_err(dev, "BGX: Cannot map CSR memory space, aborting\n"); |
| 1392 | err = -ENOMEM; |
| 1393 | goto err_release_regions; |
| 1394 | } |
Robert Richter | d768b67 | 2015-06-02 11:00:18 -0700 | [diff] [blame] | 1395 | |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 1396 | pci_read_config_word(pdev, PCI_DEVICE_ID, &sdevid); |
| 1397 | if (sdevid != PCI_DEVICE_ID_THUNDER_RGX) { |
Radha Mohan Chintakuntla | 612e94b | 2016-11-15 17:37:16 +0530 | [diff] [blame] | 1398 | bgx->bgx_id = (pci_resource_start(pdev, |
| 1399 | PCI_CFG_REG_BAR_NUM) >> 24) & BGX_ID_MASK; |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 1400 | bgx->bgx_id += nic_get_node_id(pdev) * MAX_BGX_PER_NODE; |
Sunil Goutham | 6465859 | 2016-08-12 16:51:33 +0530 | [diff] [blame] | 1401 | bgx->max_lmac = MAX_LMAC_PER_BGX; |
| 1402 | bgx_vnic[bgx->bgx_id] = bgx; |
| 1403 | } else { |
| 1404 | bgx->is_rgx = true; |
| 1405 | bgx->max_lmac = 1; |
| 1406 | bgx->bgx_id = MAX_BGX_PER_CN81XX - 1; |
| 1407 | bgx_vnic[bgx->bgx_id] = bgx; |
| 1408 | xcv_init_hw(); |
| 1409 | } |
| 1410 | |
Sunil Goutham | 09de391 | 2016-08-12 16:51:35 +0530 | [diff] [blame] | 1411 | /* On 81xx all are DLMs and on 83xx there are 3 BGX QLMs and one |
| 1412 | * BGX i.e BGX2 can be split across 2 DLMs. |
| 1413 | */ |
| 1414 | pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &sdevid); |
| 1415 | if ((sdevid == PCI_SUBSYS_DEVID_81XX_BGX) || |
| 1416 | ((sdevid == PCI_SUBSYS_DEVID_83XX_BGX) && (bgx->bgx_id == 2))) |
| 1417 | bgx->is_dlm = true; |
| 1418 | |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1419 | bgx_get_qlm_mode(bgx); |
| 1420 | |
Robert Richter | de387e1 | 2015-08-10 17:58:36 -0700 | [diff] [blame] | 1421 | err = bgx_init_phy(bgx); |
| 1422 | if (err) |
| 1423 | goto err_enable; |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1424 | |
| 1425 | bgx_init_hw(bgx); |
| 1426 | |
| 1427 | /* Enable all LMACs */ |
| 1428 | for (lmac = 0; lmac < bgx->lmac_count; lmac++) { |
| 1429 | err = bgx_lmac_enable(bgx, lmac); |
| 1430 | if (err) { |
| 1431 | dev_err(dev, "BGX%d failed to enable lmac%d\n", |
| 1432 | bgx->bgx_id, lmac); |
Sunil Goutham | 57aaf63 | 2016-08-12 16:51:31 +0530 | [diff] [blame] | 1433 | while (lmac) |
| 1434 | bgx_lmac_disable(bgx, --lmac); |
Sunil Goutham | 4863dea | 2015-05-26 19:20:15 -0700 | [diff] [blame] | 1435 | goto err_enable; |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | return 0; |
| 1440 | |
| 1441 | err_enable: |
| 1442 | bgx_vnic[bgx->bgx_id] = NULL; |
| 1443 | err_release_regions: |
| 1444 | pci_release_regions(pdev); |
| 1445 | err_disable_device: |
| 1446 | pci_disable_device(pdev); |
| 1447 | pci_set_drvdata(pdev, NULL); |
| 1448 | return err; |
| 1449 | } |
| 1450 | |
| 1451 | static void bgx_remove(struct pci_dev *pdev) |
| 1452 | { |
| 1453 | struct bgx *bgx = pci_get_drvdata(pdev); |
| 1454 | u8 lmac; |
| 1455 | |
| 1456 | /* Disable all LMACs */ |
| 1457 | for (lmac = 0; lmac < bgx->lmac_count; lmac++) |
| 1458 | bgx_lmac_disable(bgx, lmac); |
| 1459 | |
| 1460 | bgx_vnic[bgx->bgx_id] = NULL; |
| 1461 | pci_release_regions(pdev); |
| 1462 | pci_disable_device(pdev); |
| 1463 | pci_set_drvdata(pdev, NULL); |
| 1464 | } |
| 1465 | |
| 1466 | static struct pci_driver bgx_driver = { |
| 1467 | .name = DRV_NAME, |
| 1468 | .id_table = bgx_id_table, |
| 1469 | .probe = bgx_probe, |
| 1470 | .remove = bgx_remove, |
| 1471 | }; |
| 1472 | |
| 1473 | static int __init bgx_init_module(void) |
| 1474 | { |
| 1475 | pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION); |
| 1476 | |
| 1477 | return pci_register_driver(&bgx_driver); |
| 1478 | } |
| 1479 | |
| 1480 | static void __exit bgx_cleanup_module(void) |
| 1481 | { |
| 1482 | pci_unregister_driver(&bgx_driver); |
| 1483 | } |
| 1484 | |
| 1485 | module_init(bgx_init_module); |
| 1486 | module_exit(bgx_cleanup_module); |