blob: 0bf8d244185bf6a780b910edeed3827c85bb9756 [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;
Sunil Goutham4863dea2015-05-26 19:20:15 -070034 bool link_up;
35 int lmacid; /* ID within BGX */
36 int lmacid_bd; /* ID on board */
37 struct net_device netdev;
38 struct phy_device *phydev;
39 unsigned int last_duplex;
40 unsigned int last_link;
41 unsigned int last_speed;
42 bool is_sgmii;
43 struct delayed_work dwork;
44 struct workqueue_struct *check_link;
Aleksey Makarov0c886a12015-06-02 11:00:22 -070045};
Sunil Goutham4863dea2015-05-26 19:20:15 -070046
47struct bgx {
48 u8 bgx_id;
Sunil Goutham4863dea2015-05-26 19:20:15 -070049 struct lmac lmac[MAX_LMAC_PER_BGX];
50 int lmac_count;
Sunil Goutham4863dea2015-05-26 19:20:15 -070051 void __iomem *reg_base;
52 struct pci_dev *pdev;
Sunil Goutham57aaf632016-08-12 16:51:31 +053053 bool is_81xx;
Aleksey Makarov0c886a12015-06-02 11:00:22 -070054};
Sunil Goutham4863dea2015-05-26 19:20:15 -070055
Aleksey Makarovfd7ec062015-06-02 11:00:23 -070056static struct bgx *bgx_vnic[MAX_BGX_THUNDER];
Sunil Goutham4863dea2015-05-26 19:20:15 -070057static int lmac_count; /* Total no of LMACs in system */
58
59static int bgx_xaui_check_link(struct lmac *lmac);
60
61/* Supported devices */
62static const struct pci_device_id bgx_id_table[] = {
63 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_BGX) },
64 { 0, } /* end of table */
65};
66
67MODULE_AUTHOR("Cavium Inc");
68MODULE_DESCRIPTION("Cavium Thunder BGX/MAC Driver");
69MODULE_LICENSE("GPL v2");
70MODULE_VERSION(DRV_VERSION);
71MODULE_DEVICE_TABLE(pci, bgx_id_table);
72
73/* The Cavium ThunderX network controller can *only* be found in SoCs
74 * containing the ThunderX ARM64 CPU implementation. All accesses to the device
75 * registers on this platform are implicitly strongly ordered with respect
76 * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
77 * with no memory barriers in this driver. The readq()/writeq() functions add
78 * explicit ordering operation which in this case are redundant, and only
79 * add overhead.
80 */
81
82/* Register read/write APIs */
83static u64 bgx_reg_read(struct bgx *bgx, u8 lmac, u64 offset)
84{
85 void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
86
87 return readq_relaxed(addr);
88}
89
90static void bgx_reg_write(struct bgx *bgx, u8 lmac, u64 offset, u64 val)
91{
92 void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
93
94 writeq_relaxed(val, addr);
95}
96
97static void bgx_reg_modify(struct bgx *bgx, u8 lmac, u64 offset, u64 val)
98{
99 void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
100
101 writeq_relaxed(val | readq_relaxed(addr), addr);
102}
103
104static int bgx_poll_reg(struct bgx *bgx, u8 lmac, u64 reg, u64 mask, bool zero)
105{
106 int timeout = 100;
107 u64 reg_val;
108
109 while (timeout) {
110 reg_val = bgx_reg_read(bgx, lmac, reg);
111 if (zero && !(reg_val & mask))
112 return 0;
113 if (!zero && (reg_val & mask))
114 return 0;
115 usleep_range(1000, 2000);
116 timeout--;
117 }
118 return 1;
119}
120
121/* Return number of BGX present in HW */
122unsigned bgx_get_map(int node)
123{
124 int i;
125 unsigned map = 0;
126
127 for (i = 0; i < MAX_BGX_PER_CN88XX; i++) {
128 if (bgx_vnic[(node * MAX_BGX_PER_CN88XX) + i])
129 map |= (1 << i);
130 }
131
132 return map;
133}
134EXPORT_SYMBOL(bgx_get_map);
135
136/* Return number of LMAC configured for this BGX */
137int bgx_get_lmac_count(int node, int bgx_idx)
138{
139 struct bgx *bgx;
140
141 bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
142 if (bgx)
143 return bgx->lmac_count;
144
145 return 0;
146}
147EXPORT_SYMBOL(bgx_get_lmac_count);
148
149/* Returns the current link status of LMAC */
150void bgx_get_lmac_link_state(int node, int bgx_idx, int lmacid, void *status)
151{
152 struct bgx_link_status *link = (struct bgx_link_status *)status;
153 struct bgx *bgx;
154 struct lmac *lmac;
155
156 bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
157 if (!bgx)
158 return;
159
160 lmac = &bgx->lmac[lmacid];
161 link->link_up = lmac->link_up;
162 link->duplex = lmac->last_duplex;
163 link->speed = lmac->last_speed;
164}
165EXPORT_SYMBOL(bgx_get_lmac_link_state);
166
Aleksey Makarove610cb32015-06-02 11:00:21 -0700167const u8 *bgx_get_lmac_mac(int node, int bgx_idx, int lmacid)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700168{
169 struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
170
171 if (bgx)
172 return bgx->lmac[lmacid].mac;
173
174 return NULL;
175}
176EXPORT_SYMBOL(bgx_get_lmac_mac);
177
Aleksey Makarove610cb32015-06-02 11:00:21 -0700178void bgx_set_lmac_mac(int node, int bgx_idx, int lmacid, const u8 *mac)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700179{
180 struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
181
182 if (!bgx)
183 return;
184
185 ether_addr_copy(bgx->lmac[lmacid].mac, mac);
186}
187EXPORT_SYMBOL(bgx_set_lmac_mac);
188
Sunil Gouthambc69fdf2015-12-02 15:36:17 +0530189void bgx_lmac_rx_tx_enable(int node, int bgx_idx, int lmacid, bool enable)
190{
191 struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
192 u64 cfg;
193
194 if (!bgx)
195 return;
196
197 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
198 if (enable)
199 cfg |= CMR_PKT_RX_EN | CMR_PKT_TX_EN;
200 else
201 cfg &= ~(CMR_PKT_RX_EN | CMR_PKT_TX_EN);
202 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
203}
204EXPORT_SYMBOL(bgx_lmac_rx_tx_enable);
205
Sunil Goutham4863dea2015-05-26 19:20:15 -0700206static void bgx_sgmii_change_link_state(struct lmac *lmac)
207{
208 struct bgx *bgx = lmac->bgx;
209 u64 cmr_cfg;
210 u64 port_cfg = 0;
211 u64 misc_ctl = 0;
212
213 cmr_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_CMRX_CFG);
214 cmr_cfg &= ~CMR_EN;
215 bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg);
216
217 port_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG);
218 misc_ctl = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_PCS_MISCX_CTL);
219
220 if (lmac->link_up) {
221 misc_ctl &= ~PCS_MISC_CTL_GMX_ENO;
222 port_cfg &= ~GMI_PORT_CFG_DUPLEX;
223 port_cfg |= (lmac->last_duplex << 2);
224 } else {
225 misc_ctl |= PCS_MISC_CTL_GMX_ENO;
226 }
227
228 switch (lmac->last_speed) {
229 case 10:
230 port_cfg &= ~GMI_PORT_CFG_SPEED; /* speed 0 */
231 port_cfg |= GMI_PORT_CFG_SPEED_MSB; /* speed_msb 1 */
232 port_cfg &= ~GMI_PORT_CFG_SLOT_TIME; /* slottime 0 */
233 misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
234 misc_ctl |= 50; /* samp_pt */
235 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 64);
236 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_BURST, 0);
237 break;
238 case 100:
239 port_cfg &= ~GMI_PORT_CFG_SPEED; /* speed 0 */
240 port_cfg &= ~GMI_PORT_CFG_SPEED_MSB; /* speed_msb 0 */
241 port_cfg &= ~GMI_PORT_CFG_SLOT_TIME; /* slottime 0 */
242 misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
243 misc_ctl |= 5; /* samp_pt */
244 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 64);
245 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_BURST, 0);
246 break;
247 case 1000:
248 port_cfg |= GMI_PORT_CFG_SPEED; /* speed 1 */
249 port_cfg &= ~GMI_PORT_CFG_SPEED_MSB; /* speed_msb 0 */
250 port_cfg |= GMI_PORT_CFG_SLOT_TIME; /* slottime 1 */
251 misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
252 misc_ctl |= 1; /* samp_pt */
253 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 512);
254 if (lmac->last_duplex)
255 bgx_reg_write(bgx, lmac->lmacid,
256 BGX_GMP_GMI_TXX_BURST, 0);
257 else
258 bgx_reg_write(bgx, lmac->lmacid,
259 BGX_GMP_GMI_TXX_BURST, 8192);
260 break;
261 default:
262 break;
263 }
264 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_PCS_MISCX_CTL, misc_ctl);
265 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG, port_cfg);
266
267 port_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG);
268
269 /* renable lmac */
270 cmr_cfg |= CMR_EN;
271 bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg);
272}
273
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700274static void bgx_lmac_handler(struct net_device *netdev)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700275{
276 struct lmac *lmac = container_of(netdev, struct lmac, netdev);
xypron.glpk@gmx.de099a7282016-05-17 21:40:38 +0200277 struct phy_device *phydev;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700278 int link_changed = 0;
279
280 if (!lmac)
281 return;
282
xypron.glpk@gmx.de099a7282016-05-17 21:40:38 +0200283 phydev = lmac->phydev;
284
Sunil Goutham4863dea2015-05-26 19:20:15 -0700285 if (!phydev->link && lmac->last_link)
286 link_changed = -1;
287
288 if (phydev->link &&
289 (lmac->last_duplex != phydev->duplex ||
290 lmac->last_link != phydev->link ||
291 lmac->last_speed != phydev->speed)) {
292 link_changed = 1;
293 }
294
295 lmac->last_link = phydev->link;
296 lmac->last_speed = phydev->speed;
297 lmac->last_duplex = phydev->duplex;
298
299 if (!link_changed)
300 return;
301
302 if (link_changed > 0)
303 lmac->link_up = true;
304 else
305 lmac->link_up = false;
306
307 if (lmac->is_sgmii)
308 bgx_sgmii_change_link_state(lmac);
309 else
310 bgx_xaui_check_link(lmac);
311}
312
313u64 bgx_get_rx_stats(int node, int bgx_idx, int lmac, int idx)
314{
315 struct bgx *bgx;
316
317 bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
318 if (!bgx)
319 return 0;
320
321 if (idx > 8)
322 lmac = 0;
323 return bgx_reg_read(bgx, lmac, BGX_CMRX_RX_STAT0 + (idx * 8));
324}
325EXPORT_SYMBOL(bgx_get_rx_stats);
326
327u64 bgx_get_tx_stats(int node, int bgx_idx, int lmac, int idx)
328{
329 struct bgx *bgx;
330
331 bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
332 if (!bgx)
333 return 0;
334
335 return bgx_reg_read(bgx, lmac, BGX_CMRX_TX_STAT0 + (idx * 8));
336}
337EXPORT_SYMBOL(bgx_get_tx_stats);
338
339static void bgx_flush_dmac_addrs(struct bgx *bgx, int lmac)
340{
341 u64 offset;
342
343 while (bgx->lmac[lmac].dmac > 0) {
344 offset = ((bgx->lmac[lmac].dmac - 1) * sizeof(u64)) +
345 (lmac * MAX_DMAC_PER_LMAC * sizeof(u64));
346 bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM + offset, 0);
347 bgx->lmac[lmac].dmac--;
348 }
349}
350
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300351/* Configure BGX LMAC in internal loopback mode */
352void bgx_lmac_internal_loopback(int node, int bgx_idx,
353 int lmac_idx, bool enable)
354{
355 struct bgx *bgx;
356 struct lmac *lmac;
357 u64 cfg;
358
359 bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
360 if (!bgx)
361 return;
362
363 lmac = &bgx->lmac[lmac_idx];
364 if (lmac->is_sgmii) {
365 cfg = bgx_reg_read(bgx, lmac_idx, BGX_GMP_PCS_MRX_CTL);
366 if (enable)
367 cfg |= PCS_MRX_CTL_LOOPBACK1;
368 else
369 cfg &= ~PCS_MRX_CTL_LOOPBACK1;
370 bgx_reg_write(bgx, lmac_idx, BGX_GMP_PCS_MRX_CTL, cfg);
371 } else {
372 cfg = bgx_reg_read(bgx, lmac_idx, BGX_SPUX_CONTROL1);
373 if (enable)
374 cfg |= SPU_CTL_LOOPBACK;
375 else
376 cfg &= ~SPU_CTL_LOOPBACK;
377 bgx_reg_write(bgx, lmac_idx, BGX_SPUX_CONTROL1, cfg);
378 }
379}
380EXPORT_SYMBOL(bgx_lmac_internal_loopback);
381
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530382static int bgx_lmac_sgmii_init(struct bgx *bgx, struct lmac *lmac)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700383{
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530384 int lmacid = lmac->lmacid;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700385 u64 cfg;
386
387 bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_TXX_THRESH, 0x30);
388 /* max packet size */
389 bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_RXX_JABBER, MAX_FRAME_SIZE);
390
391 /* Disable frame alignment if using preamble */
392 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND);
393 if (cfg & 1)
394 bgx_reg_write(bgx, lmacid, BGX_GMP_GMI_TXX_SGMII_CTL, 0);
395
396 /* Enable lmac */
397 bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
398
399 /* PCS reset */
400 bgx_reg_modify(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, PCS_MRX_CTL_RESET);
401 if (bgx_poll_reg(bgx, lmacid, BGX_GMP_PCS_MRX_CTL,
402 PCS_MRX_CTL_RESET, true)) {
403 dev_err(&bgx->pdev->dev, "BGX PCS reset not completed\n");
404 return -1;
405 }
406
407 /* power down, reset autoneg, autoneg enable */
408 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_PCS_MRX_CTL);
409 cfg &= ~PCS_MRX_CTL_PWR_DN;
410 cfg |= (PCS_MRX_CTL_RST_AN | PCS_MRX_CTL_AN_EN);
411 bgx_reg_write(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, cfg);
412
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530413 if (lmac->lmac_type == BGX_MODE_QSGMII) {
414 /* Disable disparity check for QSGMII */
415 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_PCS_MISCX_CTL);
416 cfg &= ~PCS_MISC_CTL_DISP_EN;
417 bgx_reg_write(bgx, lmacid, BGX_GMP_PCS_MISCX_CTL, cfg);
418 return 0;
419 }
420
Sunil Goutham4863dea2015-05-26 19:20:15 -0700421 if (bgx_poll_reg(bgx, lmacid, BGX_GMP_PCS_MRX_STATUS,
422 PCS_MRX_STATUS_AN_CPT, false)) {
423 dev_err(&bgx->pdev->dev, "BGX AN_CPT not completed\n");
424 return -1;
425 }
426
427 return 0;
428}
429
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530430static int bgx_lmac_xaui_init(struct bgx *bgx, struct lmac *lmac)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700431{
432 u64 cfg;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530433 int lmacid = lmac->lmacid;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700434
435 /* Reset SPU */
436 bgx_reg_modify(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET);
437 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET, true)) {
438 dev_err(&bgx->pdev->dev, "BGX SPU reset not completed\n");
439 return -1;
440 }
441
442 /* Disable LMAC */
443 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
444 cfg &= ~CMR_EN;
445 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
446
447 bgx_reg_modify(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_LOW_POWER);
448 /* Set interleaved running disparity for RXAUI */
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530449 if (lmac->lmac_type != BGX_MODE_RXAUI)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700450 bgx_reg_modify(bgx, lmacid,
451 BGX_SPUX_MISC_CONTROL, SPU_MISC_CTL_RX_DIS);
452 else
453 bgx_reg_modify(bgx, lmacid, BGX_SPUX_MISC_CONTROL,
454 SPU_MISC_CTL_RX_DIS | SPU_MISC_CTL_INTLV_RDISP);
455
456 /* clear all interrupts */
457 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_RX_INT);
458 bgx_reg_write(bgx, lmacid, BGX_SMUX_RX_INT, cfg);
459 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_INT);
460 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_INT, cfg);
461 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
462 bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
463
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530464 if (lmac->use_training) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700465 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LP_CUP, 0x00);
466 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LD_CUP, 0x00);
467 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LD_REP, 0x00);
468 /* training enable */
469 bgx_reg_modify(bgx, lmacid,
470 BGX_SPUX_BR_PMD_CRTL, SPU_PMD_CRTL_TRAIN_EN);
471 }
472
473 /* Append FCS to each packet */
474 bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_APPEND, SMU_TX_APPEND_FCS_D);
475
476 /* Disable forward error correction */
477 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_FEC_CONTROL);
478 cfg &= ~SPU_FEC_CTL_FEC_EN;
479 bgx_reg_write(bgx, lmacid, BGX_SPUX_FEC_CONTROL, cfg);
480
481 /* Disable autoneg */
482 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_AN_CONTROL);
483 cfg = cfg & ~(SPU_AN_CTL_AN_EN | SPU_AN_CTL_XNP_EN);
484 bgx_reg_write(bgx, lmacid, BGX_SPUX_AN_CONTROL, cfg);
485
486 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_AN_ADV);
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530487 if (lmac->lmac_type == BGX_MODE_10G_KR)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700488 cfg |= (1 << 23);
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530489 else if (lmac->lmac_type == BGX_MODE_40G_KR)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700490 cfg |= (1 << 24);
491 else
492 cfg &= ~((1 << 23) | (1 << 24));
493 cfg = cfg & (~((1ULL << 25) | (1ULL << 22) | (1ULL << 12)));
494 bgx_reg_write(bgx, lmacid, BGX_SPUX_AN_ADV, cfg);
495
496 cfg = bgx_reg_read(bgx, 0, BGX_SPU_DBG_CONTROL);
497 cfg &= ~SPU_DBG_CTL_AN_ARB_LINK_CHK_EN;
498 bgx_reg_write(bgx, 0, BGX_SPU_DBG_CONTROL, cfg);
499
500 /* Enable lmac */
501 bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
502
503 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_CONTROL1);
504 cfg &= ~SPU_CTL_LOW_POWER;
505 bgx_reg_write(bgx, lmacid, BGX_SPUX_CONTROL1, cfg);
506
507 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_CTL);
508 cfg &= ~SMU_TX_CTL_UNI_EN;
509 cfg |= SMU_TX_CTL_DIC_EN;
510 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_CTL, cfg);
511
512 /* take lmac_count into account */
513 bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_THRESH, (0x100 - 1));
514 /* max packet size */
515 bgx_reg_modify(bgx, lmacid, BGX_SMUX_RX_JABBER, MAX_FRAME_SIZE);
516
517 return 0;
518}
519
520static int bgx_xaui_check_link(struct lmac *lmac)
521{
522 struct bgx *bgx = lmac->bgx;
523 int lmacid = lmac->lmacid;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530524 int lmac_type = lmac->lmac_type;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700525 u64 cfg;
526
527 bgx_reg_modify(bgx, lmacid, BGX_SPUX_MISC_CONTROL, SPU_MISC_CTL_RX_DIS);
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530528 if (lmac->use_training) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700529 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
530 if (!(cfg & (1ull << 13))) {
531 cfg = (1ull << 13) | (1ull << 14);
532 bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
533 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL);
534 cfg |= (1ull << 0);
535 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL, cfg);
536 return -1;
537 }
538 }
539
540 /* wait for PCS to come out of reset */
541 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET, true)) {
542 dev_err(&bgx->pdev->dev, "BGX SPU reset not completed\n");
543 return -1;
544 }
545
546 if ((lmac_type == BGX_MODE_10G_KR) || (lmac_type == BGX_MODE_XFI) ||
547 (lmac_type == BGX_MODE_40G_KR) || (lmac_type == BGX_MODE_XLAUI)) {
548 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_BR_STATUS1,
549 SPU_BR_STATUS_BLK_LOCK, false)) {
550 dev_err(&bgx->pdev->dev,
551 "SPU_BR_STATUS_BLK_LOCK not completed\n");
552 return -1;
553 }
554 } else {
555 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_BX_STATUS,
556 SPU_BX_STATUS_RX_ALIGN, false)) {
557 dev_err(&bgx->pdev->dev,
558 "SPU_BX_STATUS_RX_ALIGN not completed\n");
559 return -1;
560 }
561 }
562
563 /* Clear rcvflt bit (latching high) and read it back */
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530564 if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT)
565 bgx_reg_modify(bgx, lmacid,
566 BGX_SPUX_STATUS2, SPU_STATUS2_RCVFLT);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700567 if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT) {
568 dev_err(&bgx->pdev->dev, "Receive fault, retry training\n");
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530569 if (lmac->use_training) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700570 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
571 if (!(cfg & (1ull << 13))) {
572 cfg = (1ull << 13) | (1ull << 14);
573 bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
574 cfg = bgx_reg_read(bgx, lmacid,
575 BGX_SPUX_BR_PMD_CRTL);
576 cfg |= (1ull << 0);
577 bgx_reg_write(bgx, lmacid,
578 BGX_SPUX_BR_PMD_CRTL, cfg);
579 return -1;
580 }
581 }
582 return -1;
583 }
584
Sunil Goutham4863dea2015-05-26 19:20:15 -0700585 /* Wait for BGX RX to be idle */
586 if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_CTL, SMU_CTL_RX_IDLE, false)) {
587 dev_err(&bgx->pdev->dev, "SMU RX not idle\n");
588 return -1;
589 }
590
591 /* Wait for BGX TX to be idle */
592 if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_CTL, SMU_CTL_TX_IDLE, false)) {
593 dev_err(&bgx->pdev->dev, "SMU TX not idle\n");
594 return -1;
595 }
596
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530597 /* Clear receive packet disable */
Sunil Goutham4863dea2015-05-26 19:20:15 -0700598 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_MISC_CONTROL);
599 cfg &= ~SPU_MISC_CTL_RX_DIS;
600 bgx_reg_write(bgx, lmacid, BGX_SPUX_MISC_CONTROL, cfg);
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530601
602 /* Check for MAC RX faults */
603 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_RX_CTL);
604 /* 0 - Link is okay, 1 - Local fault, 2 - Remote fault */
605 cfg &= SMU_RX_CTL_STATUS;
606 if (!cfg)
607 return 0;
608
609 /* Rx local/remote fault seen.
610 * Do lmac reinit to see if condition recovers
611 */
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530612 bgx_lmac_xaui_init(bgx, lmac);
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530613
614 return -1;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700615}
616
617static void bgx_poll_for_link(struct work_struct *work)
618{
619 struct lmac *lmac;
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530620 u64 spu_link, smu_link;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700621
622 lmac = container_of(work, struct lmac, dwork.work);
623
624 /* Receive link is latching low. Force it high and verify it */
625 bgx_reg_modify(lmac->bgx, lmac->lmacid,
626 BGX_SPUX_STATUS1, SPU_STATUS1_RCV_LNK);
627 bgx_poll_reg(lmac->bgx, lmac->lmacid, BGX_SPUX_STATUS1,
628 SPU_STATUS1_RCV_LNK, false);
629
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530630 spu_link = bgx_reg_read(lmac->bgx, lmac->lmacid, BGX_SPUX_STATUS1);
631 smu_link = bgx_reg_read(lmac->bgx, lmac->lmacid, BGX_SMUX_RX_CTL);
632
633 if ((spu_link & SPU_STATUS1_RCV_LNK) &&
634 !(smu_link & SMU_RX_CTL_STATUS)) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700635 lmac->link_up = 1;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530636 if (lmac->lmac_type == BGX_MODE_XLAUI)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700637 lmac->last_speed = 40000;
638 else
639 lmac->last_speed = 10000;
640 lmac->last_duplex = 1;
641 } else {
642 lmac->link_up = 0;
Sunil Goutham0b72a9a2015-12-02 15:36:16 +0530643 lmac->last_speed = SPEED_UNKNOWN;
644 lmac->last_duplex = DUPLEX_UNKNOWN;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700645 }
646
647 if (lmac->last_link != lmac->link_up) {
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530648 if (lmac->link_up) {
649 if (bgx_xaui_check_link(lmac)) {
650 /* Errors, clear link_up state */
651 lmac->link_up = 0;
652 lmac->last_speed = SPEED_UNKNOWN;
653 lmac->last_duplex = DUPLEX_UNKNOWN;
654 }
655 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700656 lmac->last_link = lmac->link_up;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700657 }
658
659 queue_delayed_work(lmac->check_link, &lmac->dwork, HZ * 2);
660}
661
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530662static int phy_interface_mode(u8 lmac_type)
663{
664 if (lmac_type == BGX_MODE_QSGMII)
665 return PHY_INTERFACE_MODE_QSGMII;
666
667 return PHY_INTERFACE_MODE_SGMII;
668}
669
Sunil Goutham4863dea2015-05-26 19:20:15 -0700670static int bgx_lmac_enable(struct bgx *bgx, u8 lmacid)
671{
672 struct lmac *lmac;
673 u64 cfg;
674
675 lmac = &bgx->lmac[lmacid];
676 lmac->bgx = bgx;
677
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530678 if ((lmac->lmac_type == BGX_MODE_SGMII) ||
679 (lmac->lmac_type == BGX_MODE_QSGMII)) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700680 lmac->is_sgmii = 1;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530681 if (bgx_lmac_sgmii_init(bgx, lmac))
Sunil Goutham4863dea2015-05-26 19:20:15 -0700682 return -1;
683 } else {
684 lmac->is_sgmii = 0;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530685 if (bgx_lmac_xaui_init(bgx, lmac))
Sunil Goutham4863dea2015-05-26 19:20:15 -0700686 return -1;
687 }
688
689 if (lmac->is_sgmii) {
690 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND);
691 cfg |= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */
692 bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND, cfg);
693 bgx_reg_write(bgx, lmacid, BGX_GMP_GMI_TXX_MIN_PKT, 60 - 1);
694 } else {
695 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_APPEND);
696 cfg |= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */
697 bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_APPEND, cfg);
698 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_MIN_PKT, 60 + 4);
699 }
700
701 /* Enable lmac */
Sunil Gouthambc69fdf2015-12-02 15:36:17 +0530702 bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700703
704 /* Restore default cfg, incase low level firmware changed it */
705 bgx_reg_write(bgx, lmacid, BGX_CMRX_RX_DMAC_CTL, 0x03);
706
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530707 if ((lmac->lmac_type != BGX_MODE_XFI) &&
708 (lmac->lmac_type != BGX_MODE_XLAUI) &&
709 (lmac->lmac_type != BGX_MODE_40G_KR) &&
710 (lmac->lmac_type != BGX_MODE_10G_KR)) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700711 if (!lmac->phydev)
712 return -ENODEV;
713
714 lmac->phydev->dev_flags = 0;
715
716 if (phy_connect_direct(&lmac->netdev, lmac->phydev,
717 bgx_lmac_handler,
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530718 phy_interface_mode(lmac->lmac_type)))
Sunil Goutham4863dea2015-05-26 19:20:15 -0700719 return -ENODEV;
720
721 phy_start_aneg(lmac->phydev);
722 } else {
723 lmac->check_link = alloc_workqueue("check_link", WQ_UNBOUND |
724 WQ_MEM_RECLAIM, 1);
725 if (!lmac->check_link)
726 return -ENOMEM;
727 INIT_DELAYED_WORK(&lmac->dwork, bgx_poll_for_link);
728 queue_delayed_work(lmac->check_link, &lmac->dwork, 0);
729 }
730
731 return 0;
732}
733
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700734static void bgx_lmac_disable(struct bgx *bgx, u8 lmacid)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700735{
736 struct lmac *lmac;
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530737 u64 cfg;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700738
739 lmac = &bgx->lmac[lmacid];
740 if (lmac->check_link) {
741 /* Destroy work queue */
Thanneeru Srinivasulua7b1f532015-12-02 15:36:14 +0530742 cancel_delayed_work_sync(&lmac->dwork);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700743 destroy_workqueue(lmac->check_link);
744 }
745
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530746 /* Disable packet reception */
747 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
748 cfg &= ~CMR_PKT_RX_EN;
749 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
750
751 /* Give chance for Rx/Tx FIFO to get drained */
752 bgx_poll_reg(bgx, lmacid, BGX_CMRX_RX_FIFO_LEN, (u64)0x1FFF, true);
753 bgx_poll_reg(bgx, lmacid, BGX_CMRX_TX_FIFO_LEN, (u64)0x3FFF, true);
754
755 /* Disable packet transmission */
756 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
757 cfg &= ~CMR_PKT_TX_EN;
758 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
759
760 /* Disable serdes lanes */
761 if (!lmac->is_sgmii)
762 bgx_reg_modify(bgx, lmacid,
763 BGX_SPUX_CONTROL1, SPU_CTL_LOW_POWER);
764 else
765 bgx_reg_modify(bgx, lmacid,
766 BGX_GMP_PCS_MRX_CTL, PCS_MRX_CTL_PWR_DN);
767
768 /* Disable LMAC */
769 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
770 cfg &= ~CMR_EN;
771 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
772
Sunil Goutham4863dea2015-05-26 19:20:15 -0700773 bgx_flush_dmac_addrs(bgx, lmacid);
774
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530775 if ((lmac->lmac_type != BGX_MODE_XFI) &&
776 (lmac->lmac_type != BGX_MODE_XLAUI) &&
777 (lmac->lmac_type != BGX_MODE_40G_KR) &&
778 (lmac->lmac_type != BGX_MODE_10G_KR) && lmac->phydev)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700779 phy_disconnect(lmac->phydev);
780
781 lmac->phydev = NULL;
782}
783
Sunil Goutham4863dea2015-05-26 19:20:15 -0700784static void bgx_init_hw(struct bgx *bgx)
785{
786 int i;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530787 struct lmac *lmac;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700788
789 bgx_reg_modify(bgx, 0, BGX_CMR_GLOBAL_CFG, CMR_GLOBAL_CFG_FCS_STRIP);
790 if (bgx_reg_read(bgx, 0, BGX_CMR_BIST_STATUS))
791 dev_err(&bgx->pdev->dev, "BGX%d BIST failed\n", bgx->bgx_id);
792
793 /* Set lmac type and lane2serdes mapping */
794 for (i = 0; i < bgx->lmac_count; i++) {
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530795 lmac = &bgx->lmac[i];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700796 bgx_reg_write(bgx, i, BGX_CMRX_CFG,
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530797 (lmac->lmac_type << 8) | lmac->lane_to_sds);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700798 bgx->lmac[i].lmacid_bd = lmac_count;
799 lmac_count++;
800 }
801
802 bgx_reg_write(bgx, 0, BGX_CMR_TX_LMACS, bgx->lmac_count);
803 bgx_reg_write(bgx, 0, BGX_CMR_RX_LMACS, bgx->lmac_count);
804
805 /* Set the backpressure AND mask */
806 for (i = 0; i < bgx->lmac_count; i++)
807 bgx_reg_modify(bgx, 0, BGX_CMR_CHAN_MSK_AND,
808 ((1ULL << MAX_BGX_CHANS_PER_LMAC) - 1) <<
809 (i * MAX_BGX_CHANS_PER_LMAC));
810
811 /* Disable all MAC filtering */
812 for (i = 0; i < RX_DMAC_COUNT; i++)
813 bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM + (i * 8), 0x00);
814
815 /* Disable MAC steering (NCSI traffic) */
816 for (i = 0; i < RX_TRAFFIC_STEER_RULE_COUNT; i++)
817 bgx_reg_write(bgx, 0, BGX_CMR_RX_STREERING + (i * 8), 0x00);
818}
819
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530820static u8 bgx_get_lane2sds_cfg(struct bgx *bgx, struct lmac *lmac)
821{
822 return (u8)(bgx_reg_read(bgx, lmac->lmacid, BGX_CMRX_CFG) & 0xFF);
823}
824
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530825static void bgx_print_qlm_mode(struct bgx *bgx, u8 lmacid)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700826{
827 struct device *dev = &bgx->pdev->dev;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530828 struct lmac *lmac;
829 char str[20];
Sunil Goutham57aaf632016-08-12 16:51:31 +0530830 u8 dlm;
831
832 if (lmacid > MAX_LMAC_PER_BGX)
833 return;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530834
835 lmac = &bgx->lmac[lmacid];
Sunil Goutham57aaf632016-08-12 16:51:31 +0530836 dlm = (lmacid / 2) + (bgx->bgx_id * 2);
837 if (!bgx->is_81xx)
838 sprintf(str, "BGX%d QLM mode", bgx->bgx_id);
839 else
840 sprintf(str, "BGX%d DLM%d mode", bgx->bgx_id, dlm);
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530841
842 switch (lmac->lmac_type) {
843 case BGX_MODE_SGMII:
844 dev_info(dev, "%s: SGMII\n", (char *)str);
845 break;
846 case BGX_MODE_XAUI:
847 dev_info(dev, "%s: XAUI\n", (char *)str);
848 break;
849 case BGX_MODE_RXAUI:
850 dev_info(dev, "%s: RXAUI\n", (char *)str);
851 break;
852 case BGX_MODE_XFI:
853 if (!lmac->use_training)
854 dev_info(dev, "%s: XFI\n", (char *)str);
855 else
856 dev_info(dev, "%s: 10G_KR\n", (char *)str);
857 break;
858 case BGX_MODE_XLAUI:
859 if (!lmac->use_training)
860 dev_info(dev, "%s: XLAUI\n", (char *)str);
861 else
862 dev_info(dev, "%s: 40G_KR4\n", (char *)str);
863 break;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530864 case BGX_MODE_QSGMII:
865 if ((lmacid == 0) &&
866 (bgx_get_lane2sds_cfg(bgx, lmac) != lmacid))
867 return;
868 if ((lmacid == 2) &&
869 (bgx_get_lane2sds_cfg(bgx, lmac) == lmacid))
870 return;
871 dev_info(dev, "%s: QSGMII\n", (char *)str);
872 break;
873 case BGX_MODE_INVALID:
874 /* Nothing to do */
875 break;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530876 }
877}
878
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530879static void lmac_set_lane2sds(struct bgx *bgx, struct lmac *lmac)
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530880{
881 switch (lmac->lmac_type) {
882 case BGX_MODE_SGMII:
883 case BGX_MODE_XFI:
884 lmac->lane_to_sds = lmac->lmacid;
885 break;
886 case BGX_MODE_XAUI:
887 case BGX_MODE_XLAUI:
888 lmac->lane_to_sds = 0xE4;
889 break;
890 case BGX_MODE_RXAUI:
891 lmac->lane_to_sds = (lmac->lmacid) ? 0xE : 0x4;
892 break;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530893 case BGX_MODE_QSGMII:
894 /* There is no way to determine if DLM0/2 is QSGMII or
895 * DLM1/3 is configured to QSGMII as bootloader will
896 * configure all LMACs, so take whatever is configured
897 * by low level firmware.
898 */
899 lmac->lane_to_sds = bgx_get_lane2sds_cfg(bgx, lmac);
900 break;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530901 default:
902 lmac->lane_to_sds = 0;
903 break;
904 }
905}
906
907static void bgx_set_lmac_config(struct bgx *bgx, u8 idx)
908{
909 struct lmac *lmac;
Sunil Goutham57aaf632016-08-12 16:51:31 +0530910 struct lmac *olmac;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530911 u64 cmr_cfg;
Sunil Goutham57aaf632016-08-12 16:51:31 +0530912 u8 lmac_type;
913 u8 lane_to_sds;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530914
915 lmac = &bgx->lmac[idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700916
Sunil Goutham57aaf632016-08-12 16:51:31 +0530917 if (!bgx->is_81xx) {
918 /* Read LMAC0 type to figure out QLM mode
919 * This is configured by low level firmware
920 */
921 cmr_cfg = bgx_reg_read(bgx, 0, BGX_CMRX_CFG);
922 lmac->lmac_type = (cmr_cfg >> 8) & 0x07;
923 lmac->use_training =
924 bgx_reg_read(bgx, 0, BGX_SPUX_BR_PMD_CRTL) &
925 SPU_PMD_CRTL_TRAIN_EN;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530926 lmac_set_lane2sds(bgx, lmac);
Sunil Goutham57aaf632016-08-12 16:51:31 +0530927 return;
928 }
929
930 /* On 81xx BGX can be split across 2 DLMs
931 * firmware programs lmac_type of LMAC0 and LMAC2
Sunil Goutham4863dea2015-05-26 19:20:15 -0700932 */
Sunil Goutham57aaf632016-08-12 16:51:31 +0530933 if ((idx == 0) || (idx == 2)) {
934 cmr_cfg = bgx_reg_read(bgx, idx, BGX_CMRX_CFG);
935 lmac_type = (u8)((cmr_cfg >> 8) & 0x07);
936 lane_to_sds = (u8)(cmr_cfg & 0xFF);
937 /* Check if config is not reset value */
938 if ((lmac_type == 0) && (lane_to_sds == 0xE4))
939 lmac->lmac_type = BGX_MODE_INVALID;
940 else
941 lmac->lmac_type = lmac_type;
942 lmac->use_training =
943 bgx_reg_read(bgx, idx, BGX_SPUX_BR_PMD_CRTL) &
944 SPU_PMD_CRTL_TRAIN_EN;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530945 lmac_set_lane2sds(bgx, lmac);
Sunil Goutham57aaf632016-08-12 16:51:31 +0530946
947 /* Set LMAC type of other lmac on same DLM i.e LMAC 1/3 */
948 olmac = &bgx->lmac[idx + 1];
949 olmac->lmac_type = lmac->lmac_type;
950 olmac->use_training =
951 bgx_reg_read(bgx, idx + 1, BGX_SPUX_BR_PMD_CRTL) &
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530952 SPU_PMD_CRTL_TRAIN_EN;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530953 lmac_set_lane2sds(bgx, olmac);
Sunil Goutham57aaf632016-08-12 16:51:31 +0530954 }
955}
956
957static bool is_dlm0_in_bgx_mode(struct bgx *bgx)
958{
959 struct lmac *lmac;
960
961 if (!bgx->is_81xx)
962 return true;
963
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530964 lmac = &bgx->lmac[0];
Sunil Goutham57aaf632016-08-12 16:51:31 +0530965 if (lmac->lmac_type == BGX_MODE_INVALID)
966 return false;
967
968 return true;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530969}
Sunil Goutham4863dea2015-05-26 19:20:15 -0700970
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530971static void bgx_get_qlm_mode(struct bgx *bgx)
972{
Sunil Goutham57aaf632016-08-12 16:51:31 +0530973 struct lmac *lmac;
974 struct lmac *lmac01;
975 struct lmac *lmac23;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530976 u8 idx;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700977
Sunil Goutham57aaf632016-08-12 16:51:31 +0530978 /* Init all LMAC's type to invalid */
979 for (idx = 0; idx < MAX_LMAC_PER_BGX; idx++) {
980 lmac = &bgx->lmac[idx];
981 lmac->lmac_type = BGX_MODE_INVALID;
982 lmac->lmacid = idx;
983 }
984
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530985 /* It is assumed that low level firmware sets this value */
986 bgx->lmac_count = bgx_reg_read(bgx, 0, BGX_CMR_RX_LMACS) & 0x7;
987 if (bgx->lmac_count > MAX_LMAC_PER_BGX)
988 bgx->lmac_count = MAX_LMAC_PER_BGX;
989
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530990 for (idx = 0; idx < MAX_LMAC_PER_BGX; idx++)
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530991 bgx_set_lmac_config(bgx, idx);
Sunil Goutham57aaf632016-08-12 16:51:31 +0530992
993 if (!bgx->is_81xx) {
994 bgx_print_qlm_mode(bgx, 0);
995 return;
996 }
997
998 if (bgx->lmac_count) {
999 bgx_print_qlm_mode(bgx, 0);
1000 bgx_print_qlm_mode(bgx, 2);
1001 }
1002
1003 /* If DLM0 is not in BGX mode then LMAC0/1 have
1004 * to be configured with serdes lanes of DLM1
1005 */
1006 if (is_dlm0_in_bgx_mode(bgx) || (bgx->lmac_count > 2))
1007 return;
1008 for (idx = 0; idx < bgx->lmac_count; idx++) {
1009 lmac01 = &bgx->lmac[idx];
1010 lmac23 = &bgx->lmac[idx + 2];
1011 lmac01->lmac_type = lmac23->lmac_type;
1012 lmac01->lane_to_sds = lmac23->lane_to_sds;
1013 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001014}
1015
David Daney46b903a2015-08-10 17:58:37 -07001016#ifdef CONFIG_ACPI
1017
Robert Richter1d82efa2016-02-11 21:50:25 +05301018static int acpi_get_mac_address(struct device *dev, struct acpi_device *adev,
1019 u8 *dst)
David Daney46b903a2015-08-10 17:58:37 -07001020{
1021 u8 mac[ETH_ALEN];
1022 int ret;
1023
1024 ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
1025 "mac-address", mac, ETH_ALEN);
1026 if (ret)
1027 goto out;
1028
1029 if (!is_valid_ether_addr(mac)) {
Robert Richter1d82efa2016-02-11 21:50:25 +05301030 dev_err(dev, "MAC address invalid: %pM\n", mac);
David Daney46b903a2015-08-10 17:58:37 -07001031 ret = -EINVAL;
1032 goto out;
1033 }
1034
Robert Richter1d82efa2016-02-11 21:50:25 +05301035 dev_info(dev, "MAC address set to: %pM\n", mac);
1036
David Daney46b903a2015-08-10 17:58:37 -07001037 memcpy(dst, mac, ETH_ALEN);
1038out:
1039 return ret;
1040}
1041
1042/* Currently only sets the MAC address. */
1043static acpi_status bgx_acpi_register_phy(acpi_handle handle,
1044 u32 lvl, void *context, void **rv)
1045{
1046 struct bgx *bgx = context;
Robert Richter1d82efa2016-02-11 21:50:25 +05301047 struct device *dev = &bgx->pdev->dev;
David Daney46b903a2015-08-10 17:58:37 -07001048 struct acpi_device *adev;
1049
1050 if (acpi_bus_get_device(handle, &adev))
1051 goto out;
1052
Robert Richter1d82efa2016-02-11 21:50:25 +05301053 acpi_get_mac_address(dev, adev, bgx->lmac[bgx->lmac_count].mac);
David Daney46b903a2015-08-10 17:58:37 -07001054
Robert Richter1d82efa2016-02-11 21:50:25 +05301055 SET_NETDEV_DEV(&bgx->lmac[bgx->lmac_count].netdev, dev);
David Daney46b903a2015-08-10 17:58:37 -07001056
1057 bgx->lmac[bgx->lmac_count].lmacid = bgx->lmac_count;
1058out:
1059 bgx->lmac_count++;
1060 return AE_OK;
1061}
1062
1063static acpi_status bgx_acpi_match_id(acpi_handle handle, u32 lvl,
1064 void *context, void **ret_val)
1065{
1066 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
1067 struct bgx *bgx = context;
1068 char bgx_sel[5];
1069
1070 snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
1071 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &string))) {
1072 pr_warn("Invalid link device\n");
1073 return AE_OK;
1074 }
1075
1076 if (strncmp(string.pointer, bgx_sel, 4))
1077 return AE_OK;
1078
1079 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1080 bgx_acpi_register_phy, NULL, bgx, NULL);
1081
1082 kfree(string.pointer);
1083 return AE_CTRL_TERMINATE;
1084}
1085
1086static int bgx_init_acpi_phy(struct bgx *bgx)
1087{
1088 acpi_get_devices(NULL, bgx_acpi_match_id, bgx, (void **)NULL);
1089 return 0;
1090}
1091
1092#else
1093
1094static int bgx_init_acpi_phy(struct bgx *bgx)
1095{
1096 return -ENODEV;
1097}
1098
1099#endif /* CONFIG_ACPI */
1100
Robert Richterde387e12015-08-10 17:58:36 -07001101#if IS_ENABLED(CONFIG_OF_MDIO)
1102
1103static int bgx_init_of_phy(struct bgx *bgx)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001104{
David Daneyeee326f2016-02-11 21:50:24 +05301105 struct fwnode_handle *fwn;
David Daneyb7d3e3d2016-03-14 17:30:39 -07001106 struct device_node *node = NULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001107 u8 lmac = 0;
Robert Richterde387e12015-08-10 17:58:36 -07001108
David Daneyeee326f2016-02-11 21:50:24 +05301109 device_for_each_child_node(&bgx->pdev->dev, fwn) {
David Daney5fc7cf12016-03-11 09:53:09 -08001110 struct phy_device *pd;
David Daneyeee326f2016-02-11 21:50:24 +05301111 struct device_node *phy_np;
David Daneyb7d3e3d2016-03-14 17:30:39 -07001112 const char *mac;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001113
David Daney5fc7cf12016-03-11 09:53:09 -08001114 /* Should always be an OF node. But if it is not, we
1115 * cannot handle it, so exit the loop.
David Daneyeee326f2016-02-11 21:50:24 +05301116 */
David Daneyb7d3e3d2016-03-14 17:30:39 -07001117 node = to_of_node(fwn);
David Daneyeee326f2016-02-11 21:50:24 +05301118 if (!node)
1119 break;
1120
David Daneyeee326f2016-02-11 21:50:24 +05301121 mac = of_get_mac_address(node);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001122 if (mac)
1123 ether_addr_copy(bgx->lmac[lmac].mac, mac);
1124
1125 SET_NETDEV_DEV(&bgx->lmac[lmac].netdev, &bgx->pdev->dev);
1126 bgx->lmac[lmac].lmacid = lmac;
David Daney5fc7cf12016-03-11 09:53:09 -08001127
1128 phy_np = of_parse_phandle(node, "phy-handle", 0);
1129 /* If there is no phy or defective firmware presents
1130 * this cortina phy, for which there is no driver
1131 * support, ignore it.
1132 */
1133 if (phy_np &&
1134 !of_device_is_compatible(phy_np, "cortina,cs4223-slice")) {
1135 /* Wait until the phy drivers are available */
1136 pd = of_phy_find_device(phy_np);
1137 if (!pd)
David Daneyb7d3e3d2016-03-14 17:30:39 -07001138 goto defer;
David Daney5fc7cf12016-03-11 09:53:09 -08001139 bgx->lmac[lmac].phydev = pd;
1140 }
1141
Sunil Goutham4863dea2015-05-26 19:20:15 -07001142 lmac++;
David Daney65c66af2016-04-08 13:37:27 -07001143 if (lmac == MAX_LMAC_PER_BGX) {
1144 of_node_put(node);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001145 break;
David Daney65c66af2016-04-08 13:37:27 -07001146 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001147 }
Robert Richterde387e12015-08-10 17:58:36 -07001148 return 0;
David Daneyb7d3e3d2016-03-14 17:30:39 -07001149
1150defer:
1151 /* We are bailing out, try not to leak device reference counts
1152 * for phy devices we may have already found.
1153 */
1154 while (lmac) {
1155 if (bgx->lmac[lmac].phydev) {
1156 put_device(&bgx->lmac[lmac].phydev->mdio.dev);
1157 bgx->lmac[lmac].phydev = NULL;
1158 }
1159 lmac--;
1160 }
1161 of_node_put(node);
1162 return -EPROBE_DEFER;
Robert Richterde387e12015-08-10 17:58:36 -07001163}
1164
1165#else
1166
1167static int bgx_init_of_phy(struct bgx *bgx)
1168{
1169 return -ENODEV;
1170}
1171
1172#endif /* CONFIG_OF_MDIO */
1173
1174static int bgx_init_phy(struct bgx *bgx)
1175{
David Daney46b903a2015-08-10 17:58:37 -07001176 if (!acpi_disabled)
1177 return bgx_init_acpi_phy(bgx);
1178
Robert Richterde387e12015-08-10 17:58:36 -07001179 return bgx_init_of_phy(bgx);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001180}
1181
1182static int bgx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1183{
1184 int err;
1185 struct device *dev = &pdev->dev;
1186 struct bgx *bgx = NULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001187 u8 lmac;
Sunil Goutham57aaf632016-08-12 16:51:31 +05301188 u16 sdevid;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001189
1190 bgx = devm_kzalloc(dev, sizeof(*bgx), GFP_KERNEL);
1191 if (!bgx)
1192 return -ENOMEM;
1193 bgx->pdev = pdev;
1194
1195 pci_set_drvdata(pdev, bgx);
1196
1197 err = pci_enable_device(pdev);
1198 if (err) {
1199 dev_err(dev, "Failed to enable PCI device\n");
1200 pci_set_drvdata(pdev, NULL);
1201 return err;
1202 }
1203
1204 err = pci_request_regions(pdev, DRV_NAME);
1205 if (err) {
1206 dev_err(dev, "PCI request regions failed 0x%x\n", err);
1207 goto err_disable_device;
1208 }
1209
Sunil Goutham57aaf632016-08-12 16:51:31 +05301210 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &sdevid);
1211 if (sdevid == PCI_SUBSYS_DEVID_81XX_BGX)
1212 bgx->is_81xx = true;
1213
Sunil Goutham4863dea2015-05-26 19:20:15 -07001214 /* MAP configuration registers */
1215 bgx->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
1216 if (!bgx->reg_base) {
1217 dev_err(dev, "BGX: Cannot map CSR memory space, aborting\n");
1218 err = -ENOMEM;
1219 goto err_release_regions;
1220 }
1221 bgx->bgx_id = (pci_resource_start(pdev, PCI_CFG_REG_BAR_NUM) >> 24) & 1;
Robert Richterd768b672015-06-02 11:00:18 -07001222 bgx->bgx_id += nic_get_node_id(pdev) * MAX_BGX_PER_CN88XX;
1223
Sunil Goutham4863dea2015-05-26 19:20:15 -07001224 bgx_vnic[bgx->bgx_id] = bgx;
1225 bgx_get_qlm_mode(bgx);
1226
Robert Richterde387e12015-08-10 17:58:36 -07001227 err = bgx_init_phy(bgx);
1228 if (err)
1229 goto err_enable;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001230
1231 bgx_init_hw(bgx);
1232
1233 /* Enable all LMACs */
1234 for (lmac = 0; lmac < bgx->lmac_count; lmac++) {
1235 err = bgx_lmac_enable(bgx, lmac);
1236 if (err) {
1237 dev_err(dev, "BGX%d failed to enable lmac%d\n",
1238 bgx->bgx_id, lmac);
Sunil Goutham57aaf632016-08-12 16:51:31 +05301239 while (lmac)
1240 bgx_lmac_disable(bgx, --lmac);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001241 goto err_enable;
1242 }
1243 }
1244
1245 return 0;
1246
1247err_enable:
1248 bgx_vnic[bgx->bgx_id] = NULL;
1249err_release_regions:
1250 pci_release_regions(pdev);
1251err_disable_device:
1252 pci_disable_device(pdev);
1253 pci_set_drvdata(pdev, NULL);
1254 return err;
1255}
1256
1257static void bgx_remove(struct pci_dev *pdev)
1258{
1259 struct bgx *bgx = pci_get_drvdata(pdev);
1260 u8 lmac;
1261
1262 /* Disable all LMACs */
1263 for (lmac = 0; lmac < bgx->lmac_count; lmac++)
1264 bgx_lmac_disable(bgx, lmac);
1265
1266 bgx_vnic[bgx->bgx_id] = NULL;
1267 pci_release_regions(pdev);
1268 pci_disable_device(pdev);
1269 pci_set_drvdata(pdev, NULL);
1270}
1271
1272static struct pci_driver bgx_driver = {
1273 .name = DRV_NAME,
1274 .id_table = bgx_id_table,
1275 .probe = bgx_probe,
1276 .remove = bgx_remove,
1277};
1278
1279static int __init bgx_init_module(void)
1280{
1281 pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
1282
1283 return pci_register_driver(&bgx_driver);
1284}
1285
1286static void __exit bgx_cleanup_module(void)
1287{
1288 pci_unregister_driver(&bgx_driver);
1289}
1290
1291module_init(bgx_init_module);
1292module_exit(bgx_cleanup_module);