blob: 1e4695270da6cc422c441542783a0ae24dd943a6 [file] [log] [blame]
Sunil Goutham4863dea2015-05-26 19:20:15 -07001/*
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 Daney46b903a2015-08-10 17:58:37 -07009#include <linux/acpi.h>
Sunil Goutham4863dea2015-05-26 19:20:15 -070010#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
27struct lmac {
28 struct bgx *bgx;
29 int dmac;
David Daney46b903a2015-08-10 17:58:37 -070030 u8 mac[ETH_ALEN];
Sunil Goutham0bcb7d52016-08-12 16:51:30 +053031 u8 lmac_type;
32 u8 lane_to_sds;
33 bool use_training;
Thanneeru Srinivasulu075ad762017-02-08 18:09:00 +053034 bool autoneg;
Sunil Goutham4863dea2015-05-26 19:20:15 -070035 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 Makarov0c886a12015-06-02 11:00:22 -070046};
Sunil Goutham4863dea2015-05-26 19:20:15 -070047
48struct bgx {
49 u8 bgx_id;
Sunil Goutham4863dea2015-05-26 19:20:15 -070050 struct lmac lmac[MAX_LMAC_PER_BGX];
Vadim Lomovtsev7aa48652017-01-12 07:28:06 -080051 u8 lmac_count;
Sunil Goutham64658592016-08-12 16:51:33 +053052 u8 max_lmac;
Vadim Lomovtsev7aa48652017-01-12 07:28:06 -080053 u8 acpi_lmac_idx;
Sunil Goutham4863dea2015-05-26 19:20:15 -070054 void __iomem *reg_base;
55 struct pci_dev *pdev;
Sunil Goutham09de3912016-08-12 16:51:35 +053056 bool is_dlm;
Sunil Goutham64658592016-08-12 16:51:33 +053057 bool is_rgx;
Aleksey Makarov0c886a12015-06-02 11:00:22 -070058};
Sunil Goutham4863dea2015-05-26 19:20:15 -070059
Aleksey Makarovfd7ec062015-06-02 11:00:23 -070060static struct bgx *bgx_vnic[MAX_BGX_THUNDER];
Sunil Goutham4863dea2015-05-26 19:20:15 -070061static int lmac_count; /* Total no of LMACs in system */
62
63static int bgx_xaui_check_link(struct lmac *lmac);
64
65/* Supported devices */
66static const struct pci_device_id bgx_id_table[] = {
67 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_BGX) },
Sunil Goutham64658592016-08-12 16:51:33 +053068 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_RGX) },
Sunil Goutham4863dea2015-05-26 19:20:15 -070069 { 0, } /* end of table */
70};
71
72MODULE_AUTHOR("Cavium Inc");
73MODULE_DESCRIPTION("Cavium Thunder BGX/MAC Driver");
74MODULE_LICENSE("GPL v2");
75MODULE_VERSION(DRV_VERSION);
76MODULE_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 */
88static 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
95static 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
102static 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
109static 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 */
127unsigned bgx_get_map(int node)
128{
129 int i;
130 unsigned map = 0;
131
Sunil Goutham09de3912016-08-12 16:51:35 +0530132 for (i = 0; i < MAX_BGX_PER_NODE; i++) {
133 if (bgx_vnic[(node * MAX_BGX_PER_NODE) + i])
Sunil Goutham4863dea2015-05-26 19:20:15 -0700134 map |= (1 << i);
135 }
136
137 return map;
138}
139EXPORT_SYMBOL(bgx_get_map);
140
141/* Return number of LMAC configured for this BGX */
142int bgx_get_lmac_count(int node, int bgx_idx)
143{
144 struct bgx *bgx;
145
Sunil Goutham09de3912016-08-12 16:51:35 +0530146 bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700147 if (bgx)
148 return bgx->lmac_count;
149
150 return 0;
151}
152EXPORT_SYMBOL(bgx_get_lmac_count);
153
154/* Returns the current link status of LMAC */
155void 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 Goutham09de3912016-08-12 16:51:35 +0530161 bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700162 if (!bgx)
163 return;
164
165 lmac = &bgx->lmac[lmacid];
Thanneeru Srinivasulu1cc70252016-11-24 14:48:01 +0530166 link->mac_type = lmac->lmac_type;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700167 link->link_up = lmac->link_up;
168 link->duplex = lmac->last_duplex;
169 link->speed = lmac->last_speed;
170}
171EXPORT_SYMBOL(bgx_get_lmac_link_state);
172
Aleksey Makarove610cb32015-06-02 11:00:21 -0700173const u8 *bgx_get_lmac_mac(int node, int bgx_idx, int lmacid)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700174{
Sunil Goutham09de3912016-08-12 16:51:35 +0530175 struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700176
177 if (bgx)
178 return bgx->lmac[lmacid].mac;
179
180 return NULL;
181}
182EXPORT_SYMBOL(bgx_get_lmac_mac);
183
Aleksey Makarove610cb32015-06-02 11:00:21 -0700184void bgx_set_lmac_mac(int node, int bgx_idx, int lmacid, const u8 *mac)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700185{
Sunil Goutham09de3912016-08-12 16:51:35 +0530186 struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700187
188 if (!bgx)
189 return;
190
191 ether_addr_copy(bgx->lmac[lmacid].mac, mac);
192}
193EXPORT_SYMBOL(bgx_set_lmac_mac);
194
Sunil Gouthambc69fdf2015-12-02 15:36:17 +0530195void bgx_lmac_rx_tx_enable(int node, int bgx_idx, int lmacid, bool enable)
196{
Sunil Goutham09de3912016-08-12 16:51:35 +0530197 struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Goutham64658592016-08-12 16:51:33 +0530198 struct lmac *lmac;
Sunil Gouthambc69fdf2015-12-02 15:36:17 +0530199 u64 cfg;
200
201 if (!bgx)
202 return;
Sunil Goutham64658592016-08-12 16:51:33 +0530203 lmac = &bgx->lmac[lmacid];
Sunil Gouthambc69fdf2015-12-02 15:36:17 +0530204
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 Goutham64658592016-08-12 16:51:33 +0530211
212 if (bgx->is_rgx)
213 xcv_setup_link(enable ? lmac->link_up : 0, lmac->last_speed);
Sunil Gouthambc69fdf2015-12-02 15:36:17 +0530214}
215EXPORT_SYMBOL(bgx_lmac_rx_tx_enable);
216
Sunil Goutham430da202016-11-24 14:48:03 +0530217void 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}
235EXPORT_SYMBOL(bgx_lmac_get_pfc);
236
237void 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}
256EXPORT_SYMBOL(bgx_lmac_set_pfc);
257
Sunil Goutham4863dea2015-05-26 19:20:15 -0700258static 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 Goutham64658592016-08-12 16:51:33 +0530321 /* Re-enable lmac */
Sunil Goutham4863dea2015-05-26 19:20:15 -0700322 cmr_cfg |= CMR_EN;
323 bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg);
Sunil Goutham64658592016-08-12 16:51:33 +0530324
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 Goutham4863dea2015-05-26 19:20:15 -0700327}
328
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700329static void bgx_lmac_handler(struct net_device *netdev)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700330{
331 struct lmac *lmac = container_of(netdev, struct lmac, netdev);
xypron.glpk@gmx.de099a7282016-05-17 21:40:38 +0200332 struct phy_device *phydev;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700333 int link_changed = 0;
334
335 if (!lmac)
336 return;
337
xypron.glpk@gmx.de099a7282016-05-17 21:40:38 +0200338 phydev = lmac->phydev;
339
Sunil Goutham4863dea2015-05-26 19:20:15 -0700340 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
368u64 bgx_get_rx_stats(int node, int bgx_idx, int lmac, int idx)
369{
370 struct bgx *bgx;
371
Sunil Goutham09de3912016-08-12 16:51:35 +0530372 bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700373 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}
380EXPORT_SYMBOL(bgx_get_rx_stats);
381
382u64 bgx_get_tx_stats(int node, int bgx_idx, int lmac, int idx)
383{
384 struct bgx *bgx;
385
Sunil Goutham09de3912016-08-12 16:51:35 +0530386 bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700387 if (!bgx)
388 return 0;
389
390 return bgx_reg_read(bgx, lmac, BGX_CMRX_TX_STAT0 + (idx * 8));
391}
392EXPORT_SYMBOL(bgx_get_tx_stats);
393
394static 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 Gouthamd77a2382015-08-30 12:29:16 +0300406/* Configure BGX LMAC in internal loopback mode */
407void 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 Goutham09de3912016-08-12 16:51:35 +0530414 bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300415 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}
435EXPORT_SYMBOL(bgx_lmac_internal_loopback);
436
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530437static int bgx_lmac_sgmii_init(struct bgx *bgx, struct lmac *lmac)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700438{
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530439 int lmacid = lmac->lmacid;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700440 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 Srinivasulu075ad762017-02-08 18:09:00 +0530465 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 Goutham4863dea2015-05-26 19:20:15 -0700476 bgx_reg_write(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, cfg);
477
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530478 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 Srinivasulu075ad762017-02-08 18:09:00 +0530486 if ((lmac->lmac_type == BGX_MODE_SGMII) && lmac->phydev) {
Sunil Goutham64658592016-08-12 16:51:33 +0530487 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 Goutham4863dea2015-05-26 19:20:15 -0700492 }
493
494 return 0;
495}
496
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530497static int bgx_lmac_xaui_init(struct bgx *bgx, struct lmac *lmac)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700498{
499 u64 cfg;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530500 int lmacid = lmac->lmacid;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700501
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 Goutham93db2cf2016-08-12 16:51:44 +0530516 if (lmac->lmac_type == BGX_MODE_RXAUI)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700517 bgx_reg_modify(bgx, lmacid, BGX_SPUX_MISC_CONTROL,
Sunil Goutham93db2cf2016-08-12 16:51:44 +0530518 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 Goutham4863dea2015-05-26 19:20:15 -0700524
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 Goutham0bcb7d52016-08-12 16:51:30 +0530533 if (lmac->use_training) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700534 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 Goutham0bcb7d52016-08-12 16:51:30 +0530556 if (lmac->lmac_type == BGX_MODE_10G_KR)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700557 cfg |= (1 << 23);
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530558 else if (lmac->lmac_type == BGX_MODE_40G_KR)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700559 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 Goutham430da202016-11-24 14:48:03 +0530581 /* 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 Goutham4863dea2015-05-26 19:20:15 -0700593 /* 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
601static int bgx_xaui_check_link(struct lmac *lmac)
602{
603 struct bgx *bgx = lmac->bgx;
604 int lmacid = lmac->lmacid;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530605 int lmac_type = lmac->lmac_type;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700606 u64 cfg;
607
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530608 if (lmac->use_training) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700609 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 Goutham3f4c68c2016-06-27 15:30:02 +0530644 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 Goutham4863dea2015-05-26 19:20:15 -0700647 if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT) {
648 dev_err(&bgx->pdev->dev, "Receive fault, retry training\n");
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530649 if (lmac->use_training) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700650 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 Goutham4863dea2015-05-26 19:20:15 -0700665 /* 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 Goutham3f4c68c2016-06-27 15:30:02 +0530677 /* 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 Goutham0bcb7d52016-08-12 16:51:30 +0530687 bgx_lmac_xaui_init(bgx, lmac);
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530688
689 return -1;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700690}
691
Thanneeru Srinivasulu075ad762017-02-08 18:09:00 +0530692static 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
736next_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 Goutham4863dea2015-05-26 19:20:15 -0700747static void bgx_poll_for_link(struct work_struct *work)
748{
749 struct lmac *lmac;
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530750 u64 spu_link, smu_link;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700751
752 lmac = container_of(work, struct lmac, dwork.work);
Thanneeru Srinivasulu075ad762017-02-08 18:09:00 +0530753 if (lmac->is_sgmii) {
754 bgx_poll_for_sgmii_link(lmac);
755 return;
756 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700757
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 Goutham3f4c68c2016-06-27 15:30:02 +0530764 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 Goutham4863dea2015-05-26 19:20:15 -0700769 lmac->link_up = 1;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530770 if (lmac->lmac_type == BGX_MODE_XLAUI)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700771 lmac->last_speed = 40000;
772 else
773 lmac->last_speed = 10000;
774 lmac->last_duplex = 1;
775 } else {
776 lmac->link_up = 0;
Sunil Goutham0b72a9a2015-12-02 15:36:16 +0530777 lmac->last_speed = SPEED_UNKNOWN;
778 lmac->last_duplex = DUPLEX_UNKNOWN;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700779 }
780
781 if (lmac->last_link != lmac->link_up) {
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530782 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 Goutham4863dea2015-05-26 19:20:15 -0700790 lmac->last_link = lmac->link_up;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700791 }
792
793 queue_delayed_work(lmac->check_link, &lmac->dwork, HZ * 2);
794}
795
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530796static int phy_interface_mode(u8 lmac_type)
797{
798 if (lmac_type == BGX_MODE_QSGMII)
799 return PHY_INTERFACE_MODE_QSGMII;
Sunil Goutham64658592016-08-12 16:51:33 +0530800 if (lmac_type == BGX_MODE_RGMII)
801 return PHY_INTERFACE_MODE_RGMII;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530802
803 return PHY_INTERFACE_MODE_SGMII;
804}
805
Sunil Goutham4863dea2015-05-26 19:20:15 -0700806static 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 Goutham3f8057c2016-08-12 16:51:32 +0530814 if ((lmac->lmac_type == BGX_MODE_SGMII) ||
Sunil Goutham64658592016-08-12 16:51:33 +0530815 (lmac->lmac_type == BGX_MODE_QSGMII) ||
816 (lmac->lmac_type == BGX_MODE_RGMII)) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700817 lmac->is_sgmii = 1;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530818 if (bgx_lmac_sgmii_init(bgx, lmac))
Sunil Goutham4863dea2015-05-26 19:20:15 -0700819 return -1;
820 } else {
821 lmac->is_sgmii = 0;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530822 if (bgx_lmac_xaui_init(bgx, lmac))
Sunil Goutham4863dea2015-05-26 19:20:15 -0700823 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 Gouthambc69fdf2015-12-02 15:36:17 +0530839 bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700840
841 /* Restore default cfg, incase low level firmware changed it */
842 bgx_reg_write(bgx, lmacid, BGX_CMRX_RX_DMAC_CTL, 0x03);
843
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530844 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 Srinivasulu075ad762017-02-08 18:09:00 +0530848 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 Goutham4863dea2015-05-26 19:20:15 -0700863 lmac->phydev->dev_flags = 0;
864
865 if (phy_connect_direct(&lmac->netdev, lmac->phydev,
866 bgx_lmac_handler,
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530867 phy_interface_mode(lmac->lmac_type)))
Sunil Goutham4863dea2015-05-26 19:20:15 -0700868 return -ENODEV;
869
870 phy_start_aneg(lmac->phydev);
Thanneeru Srinivasulu075ad762017-02-08 18:09:00 +0530871 return 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700872 }
873
Thanneeru Srinivasulu075ad762017-02-08 18:09:00 +0530874poll:
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 Goutham4863dea2015-05-26 19:20:15 -0700882 return 0;
883}
884
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700885static void bgx_lmac_disable(struct bgx *bgx, u8 lmacid)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700886{
887 struct lmac *lmac;
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530888 u64 cfg;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700889
890 lmac = &bgx->lmac[lmacid];
891 if (lmac->check_link) {
892 /* Destroy work queue */
Thanneeru Srinivasulua7b1f532015-12-02 15:36:14 +0530893 cancel_delayed_work_sync(&lmac->dwork);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700894 destroy_workqueue(lmac->check_link);
895 }
896
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530897 /* 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 Goutham4863dea2015-05-26 19:20:15 -0700924 bgx_flush_dmac_addrs(bgx, lmacid);
925
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530926 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 Goutham4863dea2015-05-26 19:20:15 -0700930 phy_disconnect(lmac->phydev);
931
932 lmac->phydev = NULL;
933}
934
Sunil Goutham4863dea2015-05-26 19:20:15 -0700935static void bgx_init_hw(struct bgx *bgx)
936{
937 int i;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530938 struct lmac *lmac;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700939
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 Goutham0bcb7d52016-08-12 16:51:30 +0530946 lmac = &bgx->lmac[i];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700947 bgx_reg_write(bgx, i, BGX_CMRX_CFG,
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530948 (lmac->lmac_type << 8) | lmac->lane_to_sds);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700949 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 Goutham3f8057c2016-08-12 16:51:32 +0530971static 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 Goutham0bcb7d52016-08-12 16:51:30 +0530976static void bgx_print_qlm_mode(struct bgx *bgx, u8 lmacid)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700977{
978 struct device *dev = &bgx->pdev->dev;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530979 struct lmac *lmac;
980 char str[20];
Sunil Goutham57aaf632016-08-12 16:51:31 +0530981 u8 dlm;
982
Sunil Goutham64658592016-08-12 16:51:33 +0530983 if (lmacid > bgx->max_lmac)
Sunil Goutham57aaf632016-08-12 16:51:31 +0530984 return;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530985
986 lmac = &bgx->lmac[lmacid];
Sunil Goutham57aaf632016-08-12 16:51:31 +0530987 dlm = (lmacid / 2) + (bgx->bgx_id * 2);
Sunil Goutham09de3912016-08-12 16:51:35 +0530988 if (!bgx->is_dlm)
Sunil Goutham57aaf632016-08-12 16:51:31 +0530989 sprintf(str, "BGX%d QLM mode", bgx->bgx_id);
990 else
991 sprintf(str, "BGX%d DLM%d mode", bgx->bgx_id, dlm);
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530992
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 Goutham3f8057c2016-08-12 16:51:32 +05301015 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 Goutham64658592016-08-12 16:51:33 +05301024 case BGX_MODE_RGMII:
1025 dev_info(dev, "%s: RGMII\n", (char *)str);
1026 break;
Sunil Goutham3f8057c2016-08-12 16:51:32 +05301027 case BGX_MODE_INVALID:
1028 /* Nothing to do */
1029 break;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301030 }
1031}
1032
Sunil Goutham3f8057c2016-08-12 16:51:32 +05301033static void lmac_set_lane2sds(struct bgx *bgx, struct lmac *lmac)
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301034{
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 Goutham64658592016-08-12 16:51:33 +05301042 case BGX_MODE_RGMII:
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301043 lmac->lane_to_sds = 0xE4;
1044 break;
1045 case BGX_MODE_RXAUI:
1046 lmac->lane_to_sds = (lmac->lmacid) ? 0xE : 0x4;
1047 break;
Sunil Goutham3f8057c2016-08-12 16:51:32 +05301048 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 Goutham0bcb7d52016-08-12 16:51:30 +05301056 default:
1057 lmac->lane_to_sds = 0;
1058 break;
1059 }
1060}
1061
Sunil Goutham64658592016-08-12 16:51:33 +05301062static 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 Goutham0bcb7d52016-08-12 16:51:30 +05301074static void bgx_set_lmac_config(struct bgx *bgx, u8 idx)
1075{
1076 struct lmac *lmac;
Sunil Goutham57aaf632016-08-12 16:51:31 +05301077 struct lmac *olmac;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301078 u64 cmr_cfg;
Sunil Goutham57aaf632016-08-12 16:51:31 +05301079 u8 lmac_type;
1080 u8 lane_to_sds;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301081
1082 lmac = &bgx->lmac[idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -07001083
Sunil Goutham09de3912016-08-12 16:51:35 +05301084 if (!bgx->is_dlm || bgx->is_rgx) {
Sunil Goutham57aaf632016-08-12 16:51:31 +05301085 /* 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 Goutham64658592016-08-12 16:51:33 +05301090 if (bgx->is_rgx)
1091 lmac->lmac_type = BGX_MODE_RGMII;
1092 lmac_set_training(bgx, lmac, 0);
Sunil Goutham3f8057c2016-08-12 16:51:32 +05301093 lmac_set_lane2sds(bgx, lmac);
Sunil Goutham57aaf632016-08-12 16:51:31 +05301094 return;
1095 }
1096
1097 /* On 81xx BGX can be split across 2 DLMs
1098 * firmware programs lmac_type of LMAC0 and LMAC2
Sunil Goutham4863dea2015-05-26 19:20:15 -07001099 */
Sunil Goutham57aaf632016-08-12 16:51:31 +05301100 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 Goutham64658592016-08-12 16:51:33 +05301109 lmac_set_training(bgx, lmac, lmac->lmacid);
Sunil Goutham3f8057c2016-08-12 16:51:32 +05301110 lmac_set_lane2sds(bgx, lmac);
Sunil Goutham57aaf632016-08-12 16:51:31 +05301111
Sunil Goutham57aaf632016-08-12 16:51:31 +05301112 olmac = &bgx->lmac[idx + 1];
Sunil Goutham52711562016-11-24 14:48:00 +05301113 /* 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 Goutham64658592016-08-12 16:51:33 +05301130 lmac_set_training(bgx, olmac, olmac->lmacid);
Sunil Goutham57aaf632016-08-12 16:51:31 +05301131 }
1132}
1133
1134static bool is_dlm0_in_bgx_mode(struct bgx *bgx)
1135{
1136 struct lmac *lmac;
1137
Sunil Goutham09de3912016-08-12 16:51:35 +05301138 if (!bgx->is_dlm)
Sunil Goutham57aaf632016-08-12 16:51:31 +05301139 return true;
1140
Sunil Goutham3f8057c2016-08-12 16:51:32 +05301141 lmac = &bgx->lmac[0];
Sunil Goutham57aaf632016-08-12 16:51:31 +05301142 if (lmac->lmac_type == BGX_MODE_INVALID)
1143 return false;
1144
1145 return true;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301146}
Sunil Goutham4863dea2015-05-26 19:20:15 -07001147
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301148static void bgx_get_qlm_mode(struct bgx *bgx)
1149{
Sunil Goutham57aaf632016-08-12 16:51:31 +05301150 struct lmac *lmac;
1151 struct lmac *lmac01;
1152 struct lmac *lmac23;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301153 u8 idx;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001154
Sunil Goutham57aaf632016-08-12 16:51:31 +05301155 /* Init all LMAC's type to invalid */
Sunil Goutham64658592016-08-12 16:51:33 +05301156 for (idx = 0; idx < bgx->max_lmac; idx++) {
Sunil Goutham57aaf632016-08-12 16:51:31 +05301157 lmac = &bgx->lmac[idx];
Sunil Goutham57aaf632016-08-12 16:51:31 +05301158 lmac->lmacid = idx;
Sunil Goutham64658592016-08-12 16:51:33 +05301159 lmac->lmac_type = BGX_MODE_INVALID;
1160 lmac->use_training = false;
Sunil Goutham57aaf632016-08-12 16:51:31 +05301161 }
1162
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301163 /* 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 Goutham64658592016-08-12 16:51:33 +05301165 if (bgx->lmac_count > bgx->max_lmac)
1166 bgx->lmac_count = bgx->max_lmac;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301167
Sunil Goutham64658592016-08-12 16:51:33 +05301168 for (idx = 0; idx < bgx->max_lmac; idx++)
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301169 bgx_set_lmac_config(bgx, idx);
Sunil Goutham57aaf632016-08-12 16:51:31 +05301170
Sunil Goutham09de3912016-08-12 16:51:35 +05301171 if (!bgx->is_dlm || bgx->is_rgx) {
Sunil Goutham57aaf632016-08-12 16:51:31 +05301172 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 Goutham4863dea2015-05-26 19:20:15 -07001192}
1193
David Daney46b903a2015-08-10 17:58:37 -07001194#ifdef CONFIG_ACPI
1195
Robert Richter1d82efa2016-02-11 21:50:25 +05301196static int acpi_get_mac_address(struct device *dev, struct acpi_device *adev,
1197 u8 *dst)
David Daney46b903a2015-08-10 17:58:37 -07001198{
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 Richter1d82efa2016-02-11 21:50:25 +05301208 dev_err(dev, "MAC address invalid: %pM\n", mac);
David Daney46b903a2015-08-10 17:58:37 -07001209 ret = -EINVAL;
1210 goto out;
1211 }
1212
Robert Richter1d82efa2016-02-11 21:50:25 +05301213 dev_info(dev, "MAC address set to: %pM\n", mac);
1214
David Daney46b903a2015-08-10 17:58:37 -07001215 memcpy(dst, mac, ETH_ALEN);
1216out:
1217 return ret;
1218}
1219
1220/* Currently only sets the MAC address. */
1221static acpi_status bgx_acpi_register_phy(acpi_handle handle,
1222 u32 lvl, void *context, void **rv)
1223{
1224 struct bgx *bgx = context;
Robert Richter1d82efa2016-02-11 21:50:25 +05301225 struct device *dev = &bgx->pdev->dev;
David Daney46b903a2015-08-10 17:58:37 -07001226 struct acpi_device *adev;
1227
1228 if (acpi_bus_get_device(handle, &adev))
1229 goto out;
1230
Vadim Lomovtsev7aa48652017-01-12 07:28:06 -08001231 acpi_get_mac_address(dev, adev, bgx->lmac[bgx->acpi_lmac_idx].mac);
David Daney46b903a2015-08-10 17:58:37 -07001232
Vadim Lomovtsev7aa48652017-01-12 07:28:06 -08001233 SET_NETDEV_DEV(&bgx->lmac[bgx->acpi_lmac_idx].netdev, dev);
David Daney46b903a2015-08-10 17:58:37 -07001234
Vadim Lomovtsev7aa48652017-01-12 07:28:06 -08001235 bgx->lmac[bgx->acpi_lmac_idx].lmacid = bgx->acpi_lmac_idx;
1236 bgx->acpi_lmac_idx++; /* move to next LMAC */
David Daney46b903a2015-08-10 17:58:37 -07001237out:
David Daney46b903a2015-08-10 17:58:37 -07001238 return AE_OK;
1239}
1240
1241static 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
1264static 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
1272static int bgx_init_acpi_phy(struct bgx *bgx)
1273{
1274 return -ENODEV;
1275}
1276
1277#endif /* CONFIG_ACPI */
1278
Robert Richterde387e12015-08-10 17:58:36 -07001279#if IS_ENABLED(CONFIG_OF_MDIO)
1280
1281static int bgx_init_of_phy(struct bgx *bgx)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001282{
David Daneyeee326f2016-02-11 21:50:24 +05301283 struct fwnode_handle *fwn;
David Daneyb7d3e3d2016-03-14 17:30:39 -07001284 struct device_node *node = NULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001285 u8 lmac = 0;
Robert Richterde387e12015-08-10 17:58:36 -07001286
David Daneyeee326f2016-02-11 21:50:24 +05301287 device_for_each_child_node(&bgx->pdev->dev, fwn) {
David Daney5fc7cf12016-03-11 09:53:09 -08001288 struct phy_device *pd;
David Daneyeee326f2016-02-11 21:50:24 +05301289 struct device_node *phy_np;
David Daneyb7d3e3d2016-03-14 17:30:39 -07001290 const char *mac;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001291
David Daney5fc7cf12016-03-11 09:53:09 -08001292 /* Should always be an OF node. But if it is not, we
1293 * cannot handle it, so exit the loop.
David Daneyeee326f2016-02-11 21:50:24 +05301294 */
David Daneyb7d3e3d2016-03-14 17:30:39 -07001295 node = to_of_node(fwn);
David Daneyeee326f2016-02-11 21:50:24 +05301296 if (!node)
1297 break;
1298
David Daneyeee326f2016-02-11 21:50:24 +05301299 mac = of_get_mac_address(node);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001300 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 Daney5fc7cf12016-03-11 09:53:09 -08001305
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 Daneyb7d3e3d2016-03-14 17:30:39 -07001316 goto defer;
David Daney5fc7cf12016-03-11 09:53:09 -08001317 bgx->lmac[lmac].phydev = pd;
1318 }
1319
Sunil Goutham4863dea2015-05-26 19:20:15 -07001320 lmac++;
Sunil Goutham64658592016-08-12 16:51:33 +05301321 if (lmac == bgx->max_lmac) {
David Daney65c66af2016-04-08 13:37:27 -07001322 of_node_put(node);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001323 break;
David Daney65c66af2016-04-08 13:37:27 -07001324 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001325 }
Robert Richterde387e12015-08-10 17:58:36 -07001326 return 0;
David Daneyb7d3e3d2016-03-14 17:30:39 -07001327
1328defer:
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 Richterde387e12015-08-10 17:58:36 -07001341}
1342
1343#else
1344
1345static int bgx_init_of_phy(struct bgx *bgx)
1346{
1347 return -ENODEV;
1348}
1349
1350#endif /* CONFIG_OF_MDIO */
1351
1352static int bgx_init_phy(struct bgx *bgx)
1353{
David Daney46b903a2015-08-10 17:58:37 -07001354 if (!acpi_disabled)
1355 return bgx_init_acpi_phy(bgx);
1356
Robert Richterde387e12015-08-10 17:58:36 -07001357 return bgx_init_of_phy(bgx);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001358}
1359
1360static 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 Goutham4863dea2015-05-26 19:20:15 -07001365 u8 lmac;
Sunil Goutham57aaf632016-08-12 16:51:31 +05301366 u16 sdevid;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001367
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 Richterd768b672015-06-02 11:00:18 -07001395
Sunil Goutham64658592016-08-12 16:51:33 +05301396 pci_read_config_word(pdev, PCI_DEVICE_ID, &sdevid);
1397 if (sdevid != PCI_DEVICE_ID_THUNDER_RGX) {
Radha Mohan Chintakuntla612e94b2016-11-15 17:37:16 +05301398 bgx->bgx_id = (pci_resource_start(pdev,
1399 PCI_CFG_REG_BAR_NUM) >> 24) & BGX_ID_MASK;
Sunil Goutham09de3912016-08-12 16:51:35 +05301400 bgx->bgx_id += nic_get_node_id(pdev) * MAX_BGX_PER_NODE;
Sunil Goutham64658592016-08-12 16:51:33 +05301401 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 Goutham09de3912016-08-12 16:51:35 +05301411 /* 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 Goutham4863dea2015-05-26 19:20:15 -07001419 bgx_get_qlm_mode(bgx);
1420
Robert Richterde387e12015-08-10 17:58:36 -07001421 err = bgx_init_phy(bgx);
1422 if (err)
1423 goto err_enable;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001424
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 Goutham57aaf632016-08-12 16:51:31 +05301433 while (lmac)
1434 bgx_lmac_disable(bgx, --lmac);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001435 goto err_enable;
1436 }
1437 }
1438
1439 return 0;
1440
1441err_enable:
1442 bgx_vnic[bgx->bgx_id] = NULL;
1443err_release_regions:
1444 pci_release_regions(pdev);
1445err_disable_device:
1446 pci_disable_device(pdev);
1447 pci_set_drvdata(pdev, NULL);
1448 return err;
1449}
1450
1451static 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
1466static 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
1473static 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
1480static void __exit bgx_cleanup_module(void)
1481{
1482 pci_unregister_driver(&bgx_driver);
1483}
1484
1485module_init(bgx_init_module);
1486module_exit(bgx_cleanup_module);