blob: 44974270093d34f7fae1f80be8320d0977f89f6d [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;
Aleksey Makarov0c886a12015-06-02 11:00:22 -070053};
Sunil Goutham4863dea2015-05-26 19:20:15 -070054
Aleksey Makarovfd7ec062015-06-02 11:00:23 -070055static struct bgx *bgx_vnic[MAX_BGX_THUNDER];
Sunil Goutham4863dea2015-05-26 19:20:15 -070056static int lmac_count; /* Total no of LMACs in system */
57
58static int bgx_xaui_check_link(struct lmac *lmac);
59
60/* Supported devices */
61static const struct pci_device_id bgx_id_table[] = {
62 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_BGX) },
63 { 0, } /* end of table */
64};
65
66MODULE_AUTHOR("Cavium Inc");
67MODULE_DESCRIPTION("Cavium Thunder BGX/MAC Driver");
68MODULE_LICENSE("GPL v2");
69MODULE_VERSION(DRV_VERSION);
70MODULE_DEVICE_TABLE(pci, bgx_id_table);
71
72/* The Cavium ThunderX network controller can *only* be found in SoCs
73 * containing the ThunderX ARM64 CPU implementation. All accesses to the device
74 * registers on this platform are implicitly strongly ordered with respect
75 * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
76 * with no memory barriers in this driver. The readq()/writeq() functions add
77 * explicit ordering operation which in this case are redundant, and only
78 * add overhead.
79 */
80
81/* Register read/write APIs */
82static u64 bgx_reg_read(struct bgx *bgx, u8 lmac, u64 offset)
83{
84 void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
85
86 return readq_relaxed(addr);
87}
88
89static void bgx_reg_write(struct bgx *bgx, u8 lmac, u64 offset, u64 val)
90{
91 void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
92
93 writeq_relaxed(val, addr);
94}
95
96static void bgx_reg_modify(struct bgx *bgx, u8 lmac, u64 offset, u64 val)
97{
98 void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
99
100 writeq_relaxed(val | readq_relaxed(addr), addr);
101}
102
103static int bgx_poll_reg(struct bgx *bgx, u8 lmac, u64 reg, u64 mask, bool zero)
104{
105 int timeout = 100;
106 u64 reg_val;
107
108 while (timeout) {
109 reg_val = bgx_reg_read(bgx, lmac, reg);
110 if (zero && !(reg_val & mask))
111 return 0;
112 if (!zero && (reg_val & mask))
113 return 0;
114 usleep_range(1000, 2000);
115 timeout--;
116 }
117 return 1;
118}
119
120/* Return number of BGX present in HW */
121unsigned bgx_get_map(int node)
122{
123 int i;
124 unsigned map = 0;
125
126 for (i = 0; i < MAX_BGX_PER_CN88XX; i++) {
127 if (bgx_vnic[(node * MAX_BGX_PER_CN88XX) + i])
128 map |= (1 << i);
129 }
130
131 return map;
132}
133EXPORT_SYMBOL(bgx_get_map);
134
135/* Return number of LMAC configured for this BGX */
136int bgx_get_lmac_count(int node, int bgx_idx)
137{
138 struct bgx *bgx;
139
140 bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
141 if (bgx)
142 return bgx->lmac_count;
143
144 return 0;
145}
146EXPORT_SYMBOL(bgx_get_lmac_count);
147
148/* Returns the current link status of LMAC */
149void bgx_get_lmac_link_state(int node, int bgx_idx, int lmacid, void *status)
150{
151 struct bgx_link_status *link = (struct bgx_link_status *)status;
152 struct bgx *bgx;
153 struct lmac *lmac;
154
155 bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
156 if (!bgx)
157 return;
158
159 lmac = &bgx->lmac[lmacid];
160 link->link_up = lmac->link_up;
161 link->duplex = lmac->last_duplex;
162 link->speed = lmac->last_speed;
163}
164EXPORT_SYMBOL(bgx_get_lmac_link_state);
165
Aleksey Makarove610cb32015-06-02 11:00:21 -0700166const u8 *bgx_get_lmac_mac(int node, int bgx_idx, int lmacid)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700167{
168 struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
169
170 if (bgx)
171 return bgx->lmac[lmacid].mac;
172
173 return NULL;
174}
175EXPORT_SYMBOL(bgx_get_lmac_mac);
176
Aleksey Makarove610cb32015-06-02 11:00:21 -0700177void bgx_set_lmac_mac(int node, int bgx_idx, int lmacid, const u8 *mac)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700178{
179 struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
180
181 if (!bgx)
182 return;
183
184 ether_addr_copy(bgx->lmac[lmacid].mac, mac);
185}
186EXPORT_SYMBOL(bgx_set_lmac_mac);
187
Sunil Gouthambc69fdf2015-12-02 15:36:17 +0530188void bgx_lmac_rx_tx_enable(int node, int bgx_idx, int lmacid, bool enable)
189{
190 struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
191 u64 cfg;
192
193 if (!bgx)
194 return;
195
196 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
197 if (enable)
198 cfg |= CMR_PKT_RX_EN | CMR_PKT_TX_EN;
199 else
200 cfg &= ~(CMR_PKT_RX_EN | CMR_PKT_TX_EN);
201 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
202}
203EXPORT_SYMBOL(bgx_lmac_rx_tx_enable);
204
Sunil Goutham4863dea2015-05-26 19:20:15 -0700205static void bgx_sgmii_change_link_state(struct lmac *lmac)
206{
207 struct bgx *bgx = lmac->bgx;
208 u64 cmr_cfg;
209 u64 port_cfg = 0;
210 u64 misc_ctl = 0;
211
212 cmr_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_CMRX_CFG);
213 cmr_cfg &= ~CMR_EN;
214 bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg);
215
216 port_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG);
217 misc_ctl = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_PCS_MISCX_CTL);
218
219 if (lmac->link_up) {
220 misc_ctl &= ~PCS_MISC_CTL_GMX_ENO;
221 port_cfg &= ~GMI_PORT_CFG_DUPLEX;
222 port_cfg |= (lmac->last_duplex << 2);
223 } else {
224 misc_ctl |= PCS_MISC_CTL_GMX_ENO;
225 }
226
227 switch (lmac->last_speed) {
228 case 10:
229 port_cfg &= ~GMI_PORT_CFG_SPEED; /* speed 0 */
230 port_cfg |= GMI_PORT_CFG_SPEED_MSB; /* speed_msb 1 */
231 port_cfg &= ~GMI_PORT_CFG_SLOT_TIME; /* slottime 0 */
232 misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
233 misc_ctl |= 50; /* samp_pt */
234 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 64);
235 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_BURST, 0);
236 break;
237 case 100:
238 port_cfg &= ~GMI_PORT_CFG_SPEED; /* speed 0 */
239 port_cfg &= ~GMI_PORT_CFG_SPEED_MSB; /* speed_msb 0 */
240 port_cfg &= ~GMI_PORT_CFG_SLOT_TIME; /* slottime 0 */
241 misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
242 misc_ctl |= 5; /* samp_pt */
243 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 64);
244 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_BURST, 0);
245 break;
246 case 1000:
247 port_cfg |= GMI_PORT_CFG_SPEED; /* speed 1 */
248 port_cfg &= ~GMI_PORT_CFG_SPEED_MSB; /* speed_msb 0 */
249 port_cfg |= GMI_PORT_CFG_SLOT_TIME; /* slottime 1 */
250 misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
251 misc_ctl |= 1; /* samp_pt */
252 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 512);
253 if (lmac->last_duplex)
254 bgx_reg_write(bgx, lmac->lmacid,
255 BGX_GMP_GMI_TXX_BURST, 0);
256 else
257 bgx_reg_write(bgx, lmac->lmacid,
258 BGX_GMP_GMI_TXX_BURST, 8192);
259 break;
260 default:
261 break;
262 }
263 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_PCS_MISCX_CTL, misc_ctl);
264 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG, port_cfg);
265
266 port_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG);
267
268 /* renable lmac */
269 cmr_cfg |= CMR_EN;
270 bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg);
271}
272
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700273static void bgx_lmac_handler(struct net_device *netdev)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700274{
275 struct lmac *lmac = container_of(netdev, struct lmac, netdev);
xypron.glpk@gmx.de099a7282016-05-17 21:40:38 +0200276 struct phy_device *phydev;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700277 int link_changed = 0;
278
279 if (!lmac)
280 return;
281
xypron.glpk@gmx.de099a7282016-05-17 21:40:38 +0200282 phydev = lmac->phydev;
283
Sunil Goutham4863dea2015-05-26 19:20:15 -0700284 if (!phydev->link && lmac->last_link)
285 link_changed = -1;
286
287 if (phydev->link &&
288 (lmac->last_duplex != phydev->duplex ||
289 lmac->last_link != phydev->link ||
290 lmac->last_speed != phydev->speed)) {
291 link_changed = 1;
292 }
293
294 lmac->last_link = phydev->link;
295 lmac->last_speed = phydev->speed;
296 lmac->last_duplex = phydev->duplex;
297
298 if (!link_changed)
299 return;
300
301 if (link_changed > 0)
302 lmac->link_up = true;
303 else
304 lmac->link_up = false;
305
306 if (lmac->is_sgmii)
307 bgx_sgmii_change_link_state(lmac);
308 else
309 bgx_xaui_check_link(lmac);
310}
311
312u64 bgx_get_rx_stats(int node, int bgx_idx, int lmac, int idx)
313{
314 struct bgx *bgx;
315
316 bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
317 if (!bgx)
318 return 0;
319
320 if (idx > 8)
321 lmac = 0;
322 return bgx_reg_read(bgx, lmac, BGX_CMRX_RX_STAT0 + (idx * 8));
323}
324EXPORT_SYMBOL(bgx_get_rx_stats);
325
326u64 bgx_get_tx_stats(int node, int bgx_idx, int lmac, int idx)
327{
328 struct bgx *bgx;
329
330 bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
331 if (!bgx)
332 return 0;
333
334 return bgx_reg_read(bgx, lmac, BGX_CMRX_TX_STAT0 + (idx * 8));
335}
336EXPORT_SYMBOL(bgx_get_tx_stats);
337
338static void bgx_flush_dmac_addrs(struct bgx *bgx, int lmac)
339{
340 u64 offset;
341
342 while (bgx->lmac[lmac].dmac > 0) {
343 offset = ((bgx->lmac[lmac].dmac - 1) * sizeof(u64)) +
344 (lmac * MAX_DMAC_PER_LMAC * sizeof(u64));
345 bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM + offset, 0);
346 bgx->lmac[lmac].dmac--;
347 }
348}
349
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300350/* Configure BGX LMAC in internal loopback mode */
351void bgx_lmac_internal_loopback(int node, int bgx_idx,
352 int lmac_idx, bool enable)
353{
354 struct bgx *bgx;
355 struct lmac *lmac;
356 u64 cfg;
357
358 bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
359 if (!bgx)
360 return;
361
362 lmac = &bgx->lmac[lmac_idx];
363 if (lmac->is_sgmii) {
364 cfg = bgx_reg_read(bgx, lmac_idx, BGX_GMP_PCS_MRX_CTL);
365 if (enable)
366 cfg |= PCS_MRX_CTL_LOOPBACK1;
367 else
368 cfg &= ~PCS_MRX_CTL_LOOPBACK1;
369 bgx_reg_write(bgx, lmac_idx, BGX_GMP_PCS_MRX_CTL, cfg);
370 } else {
371 cfg = bgx_reg_read(bgx, lmac_idx, BGX_SPUX_CONTROL1);
372 if (enable)
373 cfg |= SPU_CTL_LOOPBACK;
374 else
375 cfg &= ~SPU_CTL_LOOPBACK;
376 bgx_reg_write(bgx, lmac_idx, BGX_SPUX_CONTROL1, cfg);
377 }
378}
379EXPORT_SYMBOL(bgx_lmac_internal_loopback);
380
Sunil Goutham4863dea2015-05-26 19:20:15 -0700381static int bgx_lmac_sgmii_init(struct bgx *bgx, int lmacid)
382{
383 u64 cfg;
384
385 bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_TXX_THRESH, 0x30);
386 /* max packet size */
387 bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_RXX_JABBER, MAX_FRAME_SIZE);
388
389 /* Disable frame alignment if using preamble */
390 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND);
391 if (cfg & 1)
392 bgx_reg_write(bgx, lmacid, BGX_GMP_GMI_TXX_SGMII_CTL, 0);
393
394 /* Enable lmac */
395 bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
396
397 /* PCS reset */
398 bgx_reg_modify(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, PCS_MRX_CTL_RESET);
399 if (bgx_poll_reg(bgx, lmacid, BGX_GMP_PCS_MRX_CTL,
400 PCS_MRX_CTL_RESET, true)) {
401 dev_err(&bgx->pdev->dev, "BGX PCS reset not completed\n");
402 return -1;
403 }
404
405 /* power down, reset autoneg, autoneg enable */
406 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_PCS_MRX_CTL);
407 cfg &= ~PCS_MRX_CTL_PWR_DN;
408 cfg |= (PCS_MRX_CTL_RST_AN | PCS_MRX_CTL_AN_EN);
409 bgx_reg_write(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, cfg);
410
411 if (bgx_poll_reg(bgx, lmacid, BGX_GMP_PCS_MRX_STATUS,
412 PCS_MRX_STATUS_AN_CPT, false)) {
413 dev_err(&bgx->pdev->dev, "BGX AN_CPT not completed\n");
414 return -1;
415 }
416
417 return 0;
418}
419
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530420static int bgx_lmac_xaui_init(struct bgx *bgx, struct lmac *lmac)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700421{
422 u64 cfg;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530423 int lmacid = lmac->lmacid;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700424
425 /* Reset SPU */
426 bgx_reg_modify(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET);
427 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET, true)) {
428 dev_err(&bgx->pdev->dev, "BGX SPU reset not completed\n");
429 return -1;
430 }
431
432 /* Disable LMAC */
433 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
434 cfg &= ~CMR_EN;
435 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
436
437 bgx_reg_modify(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_LOW_POWER);
438 /* Set interleaved running disparity for RXAUI */
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530439 if (lmac->lmac_type != BGX_MODE_RXAUI)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700440 bgx_reg_modify(bgx, lmacid,
441 BGX_SPUX_MISC_CONTROL, SPU_MISC_CTL_RX_DIS);
442 else
443 bgx_reg_modify(bgx, lmacid, BGX_SPUX_MISC_CONTROL,
444 SPU_MISC_CTL_RX_DIS | SPU_MISC_CTL_INTLV_RDISP);
445
446 /* clear all interrupts */
447 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_RX_INT);
448 bgx_reg_write(bgx, lmacid, BGX_SMUX_RX_INT, cfg);
449 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_INT);
450 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_INT, cfg);
451 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
452 bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
453
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530454 if (lmac->use_training) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700455 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LP_CUP, 0x00);
456 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LD_CUP, 0x00);
457 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LD_REP, 0x00);
458 /* training enable */
459 bgx_reg_modify(bgx, lmacid,
460 BGX_SPUX_BR_PMD_CRTL, SPU_PMD_CRTL_TRAIN_EN);
461 }
462
463 /* Append FCS to each packet */
464 bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_APPEND, SMU_TX_APPEND_FCS_D);
465
466 /* Disable forward error correction */
467 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_FEC_CONTROL);
468 cfg &= ~SPU_FEC_CTL_FEC_EN;
469 bgx_reg_write(bgx, lmacid, BGX_SPUX_FEC_CONTROL, cfg);
470
471 /* Disable autoneg */
472 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_AN_CONTROL);
473 cfg = cfg & ~(SPU_AN_CTL_AN_EN | SPU_AN_CTL_XNP_EN);
474 bgx_reg_write(bgx, lmacid, BGX_SPUX_AN_CONTROL, cfg);
475
476 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_AN_ADV);
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530477 if (lmac->lmac_type == BGX_MODE_10G_KR)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700478 cfg |= (1 << 23);
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530479 else if (lmac->lmac_type == BGX_MODE_40G_KR)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700480 cfg |= (1 << 24);
481 else
482 cfg &= ~((1 << 23) | (1 << 24));
483 cfg = cfg & (~((1ULL << 25) | (1ULL << 22) | (1ULL << 12)));
484 bgx_reg_write(bgx, lmacid, BGX_SPUX_AN_ADV, cfg);
485
486 cfg = bgx_reg_read(bgx, 0, BGX_SPU_DBG_CONTROL);
487 cfg &= ~SPU_DBG_CTL_AN_ARB_LINK_CHK_EN;
488 bgx_reg_write(bgx, 0, BGX_SPU_DBG_CONTROL, cfg);
489
490 /* Enable lmac */
491 bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
492
493 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_CONTROL1);
494 cfg &= ~SPU_CTL_LOW_POWER;
495 bgx_reg_write(bgx, lmacid, BGX_SPUX_CONTROL1, cfg);
496
497 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_CTL);
498 cfg &= ~SMU_TX_CTL_UNI_EN;
499 cfg |= SMU_TX_CTL_DIC_EN;
500 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_CTL, cfg);
501
502 /* take lmac_count into account */
503 bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_THRESH, (0x100 - 1));
504 /* max packet size */
505 bgx_reg_modify(bgx, lmacid, BGX_SMUX_RX_JABBER, MAX_FRAME_SIZE);
506
507 return 0;
508}
509
510static int bgx_xaui_check_link(struct lmac *lmac)
511{
512 struct bgx *bgx = lmac->bgx;
513 int lmacid = lmac->lmacid;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530514 int lmac_type = lmac->lmac_type;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700515 u64 cfg;
516
517 bgx_reg_modify(bgx, lmacid, BGX_SPUX_MISC_CONTROL, SPU_MISC_CTL_RX_DIS);
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530518 if (lmac->use_training) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700519 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
520 if (!(cfg & (1ull << 13))) {
521 cfg = (1ull << 13) | (1ull << 14);
522 bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
523 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL);
524 cfg |= (1ull << 0);
525 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL, cfg);
526 return -1;
527 }
528 }
529
530 /* wait for PCS to come out of reset */
531 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET, true)) {
532 dev_err(&bgx->pdev->dev, "BGX SPU reset not completed\n");
533 return -1;
534 }
535
536 if ((lmac_type == BGX_MODE_10G_KR) || (lmac_type == BGX_MODE_XFI) ||
537 (lmac_type == BGX_MODE_40G_KR) || (lmac_type == BGX_MODE_XLAUI)) {
538 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_BR_STATUS1,
539 SPU_BR_STATUS_BLK_LOCK, false)) {
540 dev_err(&bgx->pdev->dev,
541 "SPU_BR_STATUS_BLK_LOCK not completed\n");
542 return -1;
543 }
544 } else {
545 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_BX_STATUS,
546 SPU_BX_STATUS_RX_ALIGN, false)) {
547 dev_err(&bgx->pdev->dev,
548 "SPU_BX_STATUS_RX_ALIGN not completed\n");
549 return -1;
550 }
551 }
552
553 /* Clear rcvflt bit (latching high) and read it back */
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530554 if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT)
555 bgx_reg_modify(bgx, lmacid,
556 BGX_SPUX_STATUS2, SPU_STATUS2_RCVFLT);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700557 if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT) {
558 dev_err(&bgx->pdev->dev, "Receive fault, retry training\n");
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530559 if (lmac->use_training) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700560 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
561 if (!(cfg & (1ull << 13))) {
562 cfg = (1ull << 13) | (1ull << 14);
563 bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
564 cfg = bgx_reg_read(bgx, lmacid,
565 BGX_SPUX_BR_PMD_CRTL);
566 cfg |= (1ull << 0);
567 bgx_reg_write(bgx, lmacid,
568 BGX_SPUX_BR_PMD_CRTL, cfg);
569 return -1;
570 }
571 }
572 return -1;
573 }
574
Sunil Goutham4863dea2015-05-26 19:20:15 -0700575 /* Wait for BGX RX to be idle */
576 if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_CTL, SMU_CTL_RX_IDLE, false)) {
577 dev_err(&bgx->pdev->dev, "SMU RX not idle\n");
578 return -1;
579 }
580
581 /* Wait for BGX TX to be idle */
582 if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_CTL, SMU_CTL_TX_IDLE, false)) {
583 dev_err(&bgx->pdev->dev, "SMU TX not idle\n");
584 return -1;
585 }
586
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530587 /* Clear receive packet disable */
Sunil Goutham4863dea2015-05-26 19:20:15 -0700588 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_MISC_CONTROL);
589 cfg &= ~SPU_MISC_CTL_RX_DIS;
590 bgx_reg_write(bgx, lmacid, BGX_SPUX_MISC_CONTROL, cfg);
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530591
592 /* Check for MAC RX faults */
593 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_RX_CTL);
594 /* 0 - Link is okay, 1 - Local fault, 2 - Remote fault */
595 cfg &= SMU_RX_CTL_STATUS;
596 if (!cfg)
597 return 0;
598
599 /* Rx local/remote fault seen.
600 * Do lmac reinit to see if condition recovers
601 */
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530602 bgx_lmac_xaui_init(bgx, lmac);
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530603
604 return -1;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700605}
606
607static void bgx_poll_for_link(struct work_struct *work)
608{
609 struct lmac *lmac;
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530610 u64 spu_link, smu_link;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700611
612 lmac = container_of(work, struct lmac, dwork.work);
613
614 /* Receive link is latching low. Force it high and verify it */
615 bgx_reg_modify(lmac->bgx, lmac->lmacid,
616 BGX_SPUX_STATUS1, SPU_STATUS1_RCV_LNK);
617 bgx_poll_reg(lmac->bgx, lmac->lmacid, BGX_SPUX_STATUS1,
618 SPU_STATUS1_RCV_LNK, false);
619
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530620 spu_link = bgx_reg_read(lmac->bgx, lmac->lmacid, BGX_SPUX_STATUS1);
621 smu_link = bgx_reg_read(lmac->bgx, lmac->lmacid, BGX_SMUX_RX_CTL);
622
623 if ((spu_link & SPU_STATUS1_RCV_LNK) &&
624 !(smu_link & SMU_RX_CTL_STATUS)) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700625 lmac->link_up = 1;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530626 if (lmac->lmac_type == BGX_MODE_XLAUI)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700627 lmac->last_speed = 40000;
628 else
629 lmac->last_speed = 10000;
630 lmac->last_duplex = 1;
631 } else {
632 lmac->link_up = 0;
Sunil Goutham0b72a9a2015-12-02 15:36:16 +0530633 lmac->last_speed = SPEED_UNKNOWN;
634 lmac->last_duplex = DUPLEX_UNKNOWN;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700635 }
636
637 if (lmac->last_link != lmac->link_up) {
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530638 if (lmac->link_up) {
639 if (bgx_xaui_check_link(lmac)) {
640 /* Errors, clear link_up state */
641 lmac->link_up = 0;
642 lmac->last_speed = SPEED_UNKNOWN;
643 lmac->last_duplex = DUPLEX_UNKNOWN;
644 }
645 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700646 lmac->last_link = lmac->link_up;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700647 }
648
649 queue_delayed_work(lmac->check_link, &lmac->dwork, HZ * 2);
650}
651
652static int bgx_lmac_enable(struct bgx *bgx, u8 lmacid)
653{
654 struct lmac *lmac;
655 u64 cfg;
656
657 lmac = &bgx->lmac[lmacid];
658 lmac->bgx = bgx;
659
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530660 if (lmac->lmac_type == BGX_MODE_SGMII) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700661 lmac->is_sgmii = 1;
662 if (bgx_lmac_sgmii_init(bgx, lmacid))
663 return -1;
664 } else {
665 lmac->is_sgmii = 0;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530666 if (bgx_lmac_xaui_init(bgx, lmac))
Sunil Goutham4863dea2015-05-26 19:20:15 -0700667 return -1;
668 }
669
670 if (lmac->is_sgmii) {
671 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND);
672 cfg |= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */
673 bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND, cfg);
674 bgx_reg_write(bgx, lmacid, BGX_GMP_GMI_TXX_MIN_PKT, 60 - 1);
675 } else {
676 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_APPEND);
677 cfg |= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */
678 bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_APPEND, cfg);
679 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_MIN_PKT, 60 + 4);
680 }
681
682 /* Enable lmac */
Sunil Gouthambc69fdf2015-12-02 15:36:17 +0530683 bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700684
685 /* Restore default cfg, incase low level firmware changed it */
686 bgx_reg_write(bgx, lmacid, BGX_CMRX_RX_DMAC_CTL, 0x03);
687
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530688 if ((lmac->lmac_type != BGX_MODE_XFI) &&
689 (lmac->lmac_type != BGX_MODE_XLAUI) &&
690 (lmac->lmac_type != BGX_MODE_40G_KR) &&
691 (lmac->lmac_type != BGX_MODE_10G_KR)) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700692 if (!lmac->phydev)
693 return -ENODEV;
694
695 lmac->phydev->dev_flags = 0;
696
697 if (phy_connect_direct(&lmac->netdev, lmac->phydev,
698 bgx_lmac_handler,
699 PHY_INTERFACE_MODE_SGMII))
700 return -ENODEV;
701
702 phy_start_aneg(lmac->phydev);
703 } else {
704 lmac->check_link = alloc_workqueue("check_link", WQ_UNBOUND |
705 WQ_MEM_RECLAIM, 1);
706 if (!lmac->check_link)
707 return -ENOMEM;
708 INIT_DELAYED_WORK(&lmac->dwork, bgx_poll_for_link);
709 queue_delayed_work(lmac->check_link, &lmac->dwork, 0);
710 }
711
712 return 0;
713}
714
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700715static void bgx_lmac_disable(struct bgx *bgx, u8 lmacid)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700716{
717 struct lmac *lmac;
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530718 u64 cfg;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700719
720 lmac = &bgx->lmac[lmacid];
721 if (lmac->check_link) {
722 /* Destroy work queue */
Thanneeru Srinivasulua7b1f532015-12-02 15:36:14 +0530723 cancel_delayed_work_sync(&lmac->dwork);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700724 destroy_workqueue(lmac->check_link);
725 }
726
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530727 /* Disable packet reception */
728 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
729 cfg &= ~CMR_PKT_RX_EN;
730 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
731
732 /* Give chance for Rx/Tx FIFO to get drained */
733 bgx_poll_reg(bgx, lmacid, BGX_CMRX_RX_FIFO_LEN, (u64)0x1FFF, true);
734 bgx_poll_reg(bgx, lmacid, BGX_CMRX_TX_FIFO_LEN, (u64)0x3FFF, true);
735
736 /* Disable packet transmission */
737 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
738 cfg &= ~CMR_PKT_TX_EN;
739 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
740
741 /* Disable serdes lanes */
742 if (!lmac->is_sgmii)
743 bgx_reg_modify(bgx, lmacid,
744 BGX_SPUX_CONTROL1, SPU_CTL_LOW_POWER);
745 else
746 bgx_reg_modify(bgx, lmacid,
747 BGX_GMP_PCS_MRX_CTL, PCS_MRX_CTL_PWR_DN);
748
749 /* Disable LMAC */
750 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
751 cfg &= ~CMR_EN;
752 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
753
Sunil Goutham4863dea2015-05-26 19:20:15 -0700754 bgx_flush_dmac_addrs(bgx, lmacid);
755
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530756 if ((lmac->lmac_type != BGX_MODE_XFI) &&
757 (lmac->lmac_type != BGX_MODE_XLAUI) &&
758 (lmac->lmac_type != BGX_MODE_40G_KR) &&
759 (lmac->lmac_type != BGX_MODE_10G_KR) && lmac->phydev)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700760 phy_disconnect(lmac->phydev);
761
762 lmac->phydev = NULL;
763}
764
Sunil Goutham4863dea2015-05-26 19:20:15 -0700765static void bgx_init_hw(struct bgx *bgx)
766{
767 int i;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530768 struct lmac *lmac;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700769
770 bgx_reg_modify(bgx, 0, BGX_CMR_GLOBAL_CFG, CMR_GLOBAL_CFG_FCS_STRIP);
771 if (bgx_reg_read(bgx, 0, BGX_CMR_BIST_STATUS))
772 dev_err(&bgx->pdev->dev, "BGX%d BIST failed\n", bgx->bgx_id);
773
774 /* Set lmac type and lane2serdes mapping */
775 for (i = 0; i < bgx->lmac_count; i++) {
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530776 lmac = &bgx->lmac[i];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700777 bgx_reg_write(bgx, i, BGX_CMRX_CFG,
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530778 (lmac->lmac_type << 8) | lmac->lane_to_sds);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700779 bgx->lmac[i].lmacid_bd = lmac_count;
780 lmac_count++;
781 }
782
783 bgx_reg_write(bgx, 0, BGX_CMR_TX_LMACS, bgx->lmac_count);
784 bgx_reg_write(bgx, 0, BGX_CMR_RX_LMACS, bgx->lmac_count);
785
786 /* Set the backpressure AND mask */
787 for (i = 0; i < bgx->lmac_count; i++)
788 bgx_reg_modify(bgx, 0, BGX_CMR_CHAN_MSK_AND,
789 ((1ULL << MAX_BGX_CHANS_PER_LMAC) - 1) <<
790 (i * MAX_BGX_CHANS_PER_LMAC));
791
792 /* Disable all MAC filtering */
793 for (i = 0; i < RX_DMAC_COUNT; i++)
794 bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM + (i * 8), 0x00);
795
796 /* Disable MAC steering (NCSI traffic) */
797 for (i = 0; i < RX_TRAFFIC_STEER_RULE_COUNT; i++)
798 bgx_reg_write(bgx, 0, BGX_CMR_RX_STREERING + (i * 8), 0x00);
799}
800
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530801static void bgx_print_qlm_mode(struct bgx *bgx, u8 lmacid)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700802{
803 struct device *dev = &bgx->pdev->dev;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530804 struct lmac *lmac;
805 char str[20];
806
807 lmac = &bgx->lmac[lmacid];
808 sprintf(str, "BGX%d QLM mode", bgx->bgx_id);
809
810 switch (lmac->lmac_type) {
811 case BGX_MODE_SGMII:
812 dev_info(dev, "%s: SGMII\n", (char *)str);
813 break;
814 case BGX_MODE_XAUI:
815 dev_info(dev, "%s: XAUI\n", (char *)str);
816 break;
817 case BGX_MODE_RXAUI:
818 dev_info(dev, "%s: RXAUI\n", (char *)str);
819 break;
820 case BGX_MODE_XFI:
821 if (!lmac->use_training)
822 dev_info(dev, "%s: XFI\n", (char *)str);
823 else
824 dev_info(dev, "%s: 10G_KR\n", (char *)str);
825 break;
826 case BGX_MODE_XLAUI:
827 if (!lmac->use_training)
828 dev_info(dev, "%s: XLAUI\n", (char *)str);
829 else
830 dev_info(dev, "%s: 40G_KR4\n", (char *)str);
831 break;
832 default:
833 dev_info(dev, "%s: INVALID\n", (char *)str);
834 }
835}
836
837static void lmac_set_lane2sds(struct lmac *lmac)
838{
839 switch (lmac->lmac_type) {
840 case BGX_MODE_SGMII:
841 case BGX_MODE_XFI:
842 lmac->lane_to_sds = lmac->lmacid;
843 break;
844 case BGX_MODE_XAUI:
845 case BGX_MODE_XLAUI:
846 lmac->lane_to_sds = 0xE4;
847 break;
848 case BGX_MODE_RXAUI:
849 lmac->lane_to_sds = (lmac->lmacid) ? 0xE : 0x4;
850 break;
851 default:
852 lmac->lane_to_sds = 0;
853 break;
854 }
855}
856
857static void bgx_set_lmac_config(struct bgx *bgx, u8 idx)
858{
859 struct lmac *lmac;
860 u64 cmr_cfg;
861
862 lmac = &bgx->lmac[idx];
863 lmac->lmacid = idx;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700864
865 /* Read LMAC0 type to figure out QLM mode
866 * This is configured by low level firmware
867 */
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530868 cmr_cfg = bgx_reg_read(bgx, 0, BGX_CMRX_CFG);
869 lmac->lmac_type = (cmr_cfg >> 8) & 0x07;
870 lmac->use_training =
871 bgx_reg_read(bgx, 0, BGX_SPUX_BR_PMD_CRTL) &
872 SPU_PMD_CRTL_TRAIN_EN;
873 lmac_set_lane2sds(lmac);
874}
Sunil Goutham4863dea2015-05-26 19:20:15 -0700875
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530876static void bgx_get_qlm_mode(struct bgx *bgx)
877{
878 u8 idx;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700879
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530880 /* It is assumed that low level firmware sets this value */
881 bgx->lmac_count = bgx_reg_read(bgx, 0, BGX_CMR_RX_LMACS) & 0x7;
882 if (bgx->lmac_count > MAX_LMAC_PER_BGX)
883 bgx->lmac_count = MAX_LMAC_PER_BGX;
884
885 for (idx = 0; idx < bgx->lmac_count; idx++)
886 bgx_set_lmac_config(bgx, idx);
887 bgx_print_qlm_mode(bgx, 0);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700888}
889
David Daney46b903a2015-08-10 17:58:37 -0700890#ifdef CONFIG_ACPI
891
Robert Richter1d82efa2016-02-11 21:50:25 +0530892static int acpi_get_mac_address(struct device *dev, struct acpi_device *adev,
893 u8 *dst)
David Daney46b903a2015-08-10 17:58:37 -0700894{
895 u8 mac[ETH_ALEN];
896 int ret;
897
898 ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
899 "mac-address", mac, ETH_ALEN);
900 if (ret)
901 goto out;
902
903 if (!is_valid_ether_addr(mac)) {
Robert Richter1d82efa2016-02-11 21:50:25 +0530904 dev_err(dev, "MAC address invalid: %pM\n", mac);
David Daney46b903a2015-08-10 17:58:37 -0700905 ret = -EINVAL;
906 goto out;
907 }
908
Robert Richter1d82efa2016-02-11 21:50:25 +0530909 dev_info(dev, "MAC address set to: %pM\n", mac);
910
David Daney46b903a2015-08-10 17:58:37 -0700911 memcpy(dst, mac, ETH_ALEN);
912out:
913 return ret;
914}
915
916/* Currently only sets the MAC address. */
917static acpi_status bgx_acpi_register_phy(acpi_handle handle,
918 u32 lvl, void *context, void **rv)
919{
920 struct bgx *bgx = context;
Robert Richter1d82efa2016-02-11 21:50:25 +0530921 struct device *dev = &bgx->pdev->dev;
David Daney46b903a2015-08-10 17:58:37 -0700922 struct acpi_device *adev;
923
924 if (acpi_bus_get_device(handle, &adev))
925 goto out;
926
Robert Richter1d82efa2016-02-11 21:50:25 +0530927 acpi_get_mac_address(dev, adev, bgx->lmac[bgx->lmac_count].mac);
David Daney46b903a2015-08-10 17:58:37 -0700928
Robert Richter1d82efa2016-02-11 21:50:25 +0530929 SET_NETDEV_DEV(&bgx->lmac[bgx->lmac_count].netdev, dev);
David Daney46b903a2015-08-10 17:58:37 -0700930
931 bgx->lmac[bgx->lmac_count].lmacid = bgx->lmac_count;
932out:
933 bgx->lmac_count++;
934 return AE_OK;
935}
936
937static acpi_status bgx_acpi_match_id(acpi_handle handle, u32 lvl,
938 void *context, void **ret_val)
939{
940 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
941 struct bgx *bgx = context;
942 char bgx_sel[5];
943
944 snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
945 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &string))) {
946 pr_warn("Invalid link device\n");
947 return AE_OK;
948 }
949
950 if (strncmp(string.pointer, bgx_sel, 4))
951 return AE_OK;
952
953 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
954 bgx_acpi_register_phy, NULL, bgx, NULL);
955
956 kfree(string.pointer);
957 return AE_CTRL_TERMINATE;
958}
959
960static int bgx_init_acpi_phy(struct bgx *bgx)
961{
962 acpi_get_devices(NULL, bgx_acpi_match_id, bgx, (void **)NULL);
963 return 0;
964}
965
966#else
967
968static int bgx_init_acpi_phy(struct bgx *bgx)
969{
970 return -ENODEV;
971}
972
973#endif /* CONFIG_ACPI */
974
Robert Richterde387e12015-08-10 17:58:36 -0700975#if IS_ENABLED(CONFIG_OF_MDIO)
976
977static int bgx_init_of_phy(struct bgx *bgx)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700978{
David Daneyeee326f2016-02-11 21:50:24 +0530979 struct fwnode_handle *fwn;
David Daneyb7d3e3d2016-03-14 17:30:39 -0700980 struct device_node *node = NULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700981 u8 lmac = 0;
Robert Richterde387e12015-08-10 17:58:36 -0700982
David Daneyeee326f2016-02-11 21:50:24 +0530983 device_for_each_child_node(&bgx->pdev->dev, fwn) {
David Daney5fc7cf12016-03-11 09:53:09 -0800984 struct phy_device *pd;
David Daneyeee326f2016-02-11 21:50:24 +0530985 struct device_node *phy_np;
David Daneyb7d3e3d2016-03-14 17:30:39 -0700986 const char *mac;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700987
David Daney5fc7cf12016-03-11 09:53:09 -0800988 /* Should always be an OF node. But if it is not, we
989 * cannot handle it, so exit the loop.
David Daneyeee326f2016-02-11 21:50:24 +0530990 */
David Daneyb7d3e3d2016-03-14 17:30:39 -0700991 node = to_of_node(fwn);
David Daneyeee326f2016-02-11 21:50:24 +0530992 if (!node)
993 break;
994
David Daneyeee326f2016-02-11 21:50:24 +0530995 mac = of_get_mac_address(node);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700996 if (mac)
997 ether_addr_copy(bgx->lmac[lmac].mac, mac);
998
999 SET_NETDEV_DEV(&bgx->lmac[lmac].netdev, &bgx->pdev->dev);
1000 bgx->lmac[lmac].lmacid = lmac;
David Daney5fc7cf12016-03-11 09:53:09 -08001001
1002 phy_np = of_parse_phandle(node, "phy-handle", 0);
1003 /* If there is no phy or defective firmware presents
1004 * this cortina phy, for which there is no driver
1005 * support, ignore it.
1006 */
1007 if (phy_np &&
1008 !of_device_is_compatible(phy_np, "cortina,cs4223-slice")) {
1009 /* Wait until the phy drivers are available */
1010 pd = of_phy_find_device(phy_np);
1011 if (!pd)
David Daneyb7d3e3d2016-03-14 17:30:39 -07001012 goto defer;
David Daney5fc7cf12016-03-11 09:53:09 -08001013 bgx->lmac[lmac].phydev = pd;
1014 }
1015
Sunil Goutham4863dea2015-05-26 19:20:15 -07001016 lmac++;
David Daney65c66af2016-04-08 13:37:27 -07001017 if (lmac == MAX_LMAC_PER_BGX) {
1018 of_node_put(node);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001019 break;
David Daney65c66af2016-04-08 13:37:27 -07001020 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001021 }
Robert Richterde387e12015-08-10 17:58:36 -07001022 return 0;
David Daneyb7d3e3d2016-03-14 17:30:39 -07001023
1024defer:
1025 /* We are bailing out, try not to leak device reference counts
1026 * for phy devices we may have already found.
1027 */
1028 while (lmac) {
1029 if (bgx->lmac[lmac].phydev) {
1030 put_device(&bgx->lmac[lmac].phydev->mdio.dev);
1031 bgx->lmac[lmac].phydev = NULL;
1032 }
1033 lmac--;
1034 }
1035 of_node_put(node);
1036 return -EPROBE_DEFER;
Robert Richterde387e12015-08-10 17:58:36 -07001037}
1038
1039#else
1040
1041static int bgx_init_of_phy(struct bgx *bgx)
1042{
1043 return -ENODEV;
1044}
1045
1046#endif /* CONFIG_OF_MDIO */
1047
1048static int bgx_init_phy(struct bgx *bgx)
1049{
David Daney46b903a2015-08-10 17:58:37 -07001050 if (!acpi_disabled)
1051 return bgx_init_acpi_phy(bgx);
1052
Robert Richterde387e12015-08-10 17:58:36 -07001053 return bgx_init_of_phy(bgx);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001054}
1055
1056static int bgx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1057{
1058 int err;
1059 struct device *dev = &pdev->dev;
1060 struct bgx *bgx = NULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001061 u8 lmac;
1062
1063 bgx = devm_kzalloc(dev, sizeof(*bgx), GFP_KERNEL);
1064 if (!bgx)
1065 return -ENOMEM;
1066 bgx->pdev = pdev;
1067
1068 pci_set_drvdata(pdev, bgx);
1069
1070 err = pci_enable_device(pdev);
1071 if (err) {
1072 dev_err(dev, "Failed to enable PCI device\n");
1073 pci_set_drvdata(pdev, NULL);
1074 return err;
1075 }
1076
1077 err = pci_request_regions(pdev, DRV_NAME);
1078 if (err) {
1079 dev_err(dev, "PCI request regions failed 0x%x\n", err);
1080 goto err_disable_device;
1081 }
1082
1083 /* MAP configuration registers */
1084 bgx->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
1085 if (!bgx->reg_base) {
1086 dev_err(dev, "BGX: Cannot map CSR memory space, aborting\n");
1087 err = -ENOMEM;
1088 goto err_release_regions;
1089 }
1090 bgx->bgx_id = (pci_resource_start(pdev, PCI_CFG_REG_BAR_NUM) >> 24) & 1;
Robert Richterd768b672015-06-02 11:00:18 -07001091 bgx->bgx_id += nic_get_node_id(pdev) * MAX_BGX_PER_CN88XX;
1092
Sunil Goutham4863dea2015-05-26 19:20:15 -07001093 bgx_vnic[bgx->bgx_id] = bgx;
1094 bgx_get_qlm_mode(bgx);
1095
Robert Richterde387e12015-08-10 17:58:36 -07001096 err = bgx_init_phy(bgx);
1097 if (err)
1098 goto err_enable;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001099
1100 bgx_init_hw(bgx);
1101
1102 /* Enable all LMACs */
1103 for (lmac = 0; lmac < bgx->lmac_count; lmac++) {
1104 err = bgx_lmac_enable(bgx, lmac);
1105 if (err) {
1106 dev_err(dev, "BGX%d failed to enable lmac%d\n",
1107 bgx->bgx_id, lmac);
1108 goto err_enable;
1109 }
1110 }
1111
1112 return 0;
1113
1114err_enable:
1115 bgx_vnic[bgx->bgx_id] = NULL;
1116err_release_regions:
1117 pci_release_regions(pdev);
1118err_disable_device:
1119 pci_disable_device(pdev);
1120 pci_set_drvdata(pdev, NULL);
1121 return err;
1122}
1123
1124static void bgx_remove(struct pci_dev *pdev)
1125{
1126 struct bgx *bgx = pci_get_drvdata(pdev);
1127 u8 lmac;
1128
1129 /* Disable all LMACs */
1130 for (lmac = 0; lmac < bgx->lmac_count; lmac++)
1131 bgx_lmac_disable(bgx, lmac);
1132
1133 bgx_vnic[bgx->bgx_id] = NULL;
1134 pci_release_regions(pdev);
1135 pci_disable_device(pdev);
1136 pci_set_drvdata(pdev, NULL);
1137}
1138
1139static struct pci_driver bgx_driver = {
1140 .name = DRV_NAME,
1141 .id_table = bgx_id_table,
1142 .probe = bgx_probe,
1143 .remove = bgx_remove,
1144};
1145
1146static int __init bgx_init_module(void)
1147{
1148 pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
1149
1150 return pci_register_driver(&bgx_driver);
1151}
1152
1153static void __exit bgx_cleanup_module(void)
1154{
1155 pci_unregister_driver(&bgx_driver);
1156}
1157
1158module_init(bgx_init_module);
1159module_exit(bgx_cleanup_module);