blob: 2f85b64f01fa06d708e52bde776708f96b55c30e [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];
Vadim Lomovtsev7aa48652017-01-12 07:28:06 -080050 u8 lmac_count;
Sunil Goutham64658592016-08-12 16:51:33 +053051 u8 max_lmac;
Vadim Lomovtsev7aa48652017-01-12 07:28:06 -080052 u8 acpi_lmac_idx;
Sunil Goutham4863dea2015-05-26 19:20:15 -070053 void __iomem *reg_base;
54 struct pci_dev *pdev;
Sunil Goutham09de3912016-08-12 16:51:35 +053055 bool is_dlm;
Sunil Goutham64658592016-08-12 16:51:33 +053056 bool is_rgx;
Aleksey Makarov0c886a12015-06-02 11:00:22 -070057};
Sunil Goutham4863dea2015-05-26 19:20:15 -070058
Aleksey Makarovfd7ec062015-06-02 11:00:23 -070059static struct bgx *bgx_vnic[MAX_BGX_THUNDER];
Sunil Goutham4863dea2015-05-26 19:20:15 -070060static int lmac_count; /* Total no of LMACs in system */
61
62static int bgx_xaui_check_link(struct lmac *lmac);
63
64/* Supported devices */
65static const struct pci_device_id bgx_id_table[] = {
66 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_BGX) },
Sunil Goutham64658592016-08-12 16:51:33 +053067 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_RGX) },
Sunil Goutham4863dea2015-05-26 19:20:15 -070068 { 0, } /* end of table */
69};
70
71MODULE_AUTHOR("Cavium Inc");
72MODULE_DESCRIPTION("Cavium Thunder BGX/MAC Driver");
73MODULE_LICENSE("GPL v2");
74MODULE_VERSION(DRV_VERSION);
75MODULE_DEVICE_TABLE(pci, bgx_id_table);
76
77/* The Cavium ThunderX network controller can *only* be found in SoCs
78 * containing the ThunderX ARM64 CPU implementation. All accesses to the device
79 * registers on this platform are implicitly strongly ordered with respect
80 * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
81 * with no memory barriers in this driver. The readq()/writeq() functions add
82 * explicit ordering operation which in this case are redundant, and only
83 * add overhead.
84 */
85
86/* Register read/write APIs */
87static u64 bgx_reg_read(struct bgx *bgx, u8 lmac, u64 offset)
88{
89 void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
90
91 return readq_relaxed(addr);
92}
93
94static void bgx_reg_write(struct bgx *bgx, u8 lmac, u64 offset, u64 val)
95{
96 void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
97
98 writeq_relaxed(val, addr);
99}
100
101static void bgx_reg_modify(struct bgx *bgx, u8 lmac, u64 offset, u64 val)
102{
103 void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
104
105 writeq_relaxed(val | readq_relaxed(addr), addr);
106}
107
108static int bgx_poll_reg(struct bgx *bgx, u8 lmac, u64 reg, u64 mask, bool zero)
109{
110 int timeout = 100;
111 u64 reg_val;
112
113 while (timeout) {
114 reg_val = bgx_reg_read(bgx, lmac, reg);
115 if (zero && !(reg_val & mask))
116 return 0;
117 if (!zero && (reg_val & mask))
118 return 0;
119 usleep_range(1000, 2000);
120 timeout--;
121 }
122 return 1;
123}
124
125/* Return number of BGX present in HW */
126unsigned bgx_get_map(int node)
127{
128 int i;
129 unsigned map = 0;
130
Sunil Goutham09de3912016-08-12 16:51:35 +0530131 for (i = 0; i < MAX_BGX_PER_NODE; i++) {
132 if (bgx_vnic[(node * MAX_BGX_PER_NODE) + i])
Sunil Goutham4863dea2015-05-26 19:20:15 -0700133 map |= (1 << i);
134 }
135
136 return map;
137}
138EXPORT_SYMBOL(bgx_get_map);
139
140/* Return number of LMAC configured for this BGX */
141int bgx_get_lmac_count(int node, int bgx_idx)
142{
143 struct bgx *bgx;
144
Sunil Goutham09de3912016-08-12 16:51:35 +0530145 bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700146 if (bgx)
147 return bgx->lmac_count;
148
149 return 0;
150}
151EXPORT_SYMBOL(bgx_get_lmac_count);
152
153/* Returns the current link status of LMAC */
154void bgx_get_lmac_link_state(int node, int bgx_idx, int lmacid, void *status)
155{
156 struct bgx_link_status *link = (struct bgx_link_status *)status;
157 struct bgx *bgx;
158 struct lmac *lmac;
159
Sunil Goutham09de3912016-08-12 16:51:35 +0530160 bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700161 if (!bgx)
162 return;
163
164 lmac = &bgx->lmac[lmacid];
Thanneeru Srinivasulu1cc70252016-11-24 14:48:01 +0530165 link->mac_type = lmac->lmac_type;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700166 link->link_up = lmac->link_up;
167 link->duplex = lmac->last_duplex;
168 link->speed = lmac->last_speed;
169}
170EXPORT_SYMBOL(bgx_get_lmac_link_state);
171
Aleksey Makarove610cb32015-06-02 11:00:21 -0700172const u8 *bgx_get_lmac_mac(int node, int bgx_idx, int lmacid)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700173{
Sunil Goutham09de3912016-08-12 16:51:35 +0530174 struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700175
176 if (bgx)
177 return bgx->lmac[lmacid].mac;
178
179 return NULL;
180}
181EXPORT_SYMBOL(bgx_get_lmac_mac);
182
Aleksey Makarove610cb32015-06-02 11:00:21 -0700183void bgx_set_lmac_mac(int node, int bgx_idx, int lmacid, const u8 *mac)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700184{
Sunil Goutham09de3912016-08-12 16:51:35 +0530185 struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700186
187 if (!bgx)
188 return;
189
190 ether_addr_copy(bgx->lmac[lmacid].mac, mac);
191}
192EXPORT_SYMBOL(bgx_set_lmac_mac);
193
Sunil Gouthambc69fdf2015-12-02 15:36:17 +0530194void bgx_lmac_rx_tx_enable(int node, int bgx_idx, int lmacid, bool enable)
195{
Sunil Goutham09de3912016-08-12 16:51:35 +0530196 struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Goutham64658592016-08-12 16:51:33 +0530197 struct lmac *lmac;
Sunil Gouthambc69fdf2015-12-02 15:36:17 +0530198 u64 cfg;
199
200 if (!bgx)
201 return;
Sunil Goutham64658592016-08-12 16:51:33 +0530202 lmac = &bgx->lmac[lmacid];
Sunil Gouthambc69fdf2015-12-02 15:36:17 +0530203
204 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
205 if (enable)
206 cfg |= CMR_PKT_RX_EN | CMR_PKT_TX_EN;
207 else
208 cfg &= ~(CMR_PKT_RX_EN | CMR_PKT_TX_EN);
209 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
Sunil Goutham64658592016-08-12 16:51:33 +0530210
211 if (bgx->is_rgx)
212 xcv_setup_link(enable ? lmac->link_up : 0, lmac->last_speed);
Sunil Gouthambc69fdf2015-12-02 15:36:17 +0530213}
214EXPORT_SYMBOL(bgx_lmac_rx_tx_enable);
215
Sunil Goutham430da202016-11-24 14:48:03 +0530216void bgx_lmac_get_pfc(int node, int bgx_idx, int lmacid, void *pause)
217{
218 struct pfc *pfc = (struct pfc *)pause;
219 struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
220 struct lmac *lmac;
221 u64 cfg;
222
223 if (!bgx)
224 return;
225 lmac = &bgx->lmac[lmacid];
226 if (lmac->is_sgmii)
227 return;
228
229 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_CBFC_CTL);
230 pfc->fc_rx = cfg & RX_EN;
231 pfc->fc_tx = cfg & TX_EN;
232 pfc->autoneg = 0;
233}
234EXPORT_SYMBOL(bgx_lmac_get_pfc);
235
236void bgx_lmac_set_pfc(int node, int bgx_idx, int lmacid, void *pause)
237{
238 struct pfc *pfc = (struct pfc *)pause;
239 struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
240 struct lmac *lmac;
241 u64 cfg;
242
243 if (!bgx)
244 return;
245 lmac = &bgx->lmac[lmacid];
246 if (lmac->is_sgmii)
247 return;
248
249 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_CBFC_CTL);
250 cfg &= ~(RX_EN | TX_EN);
251 cfg |= (pfc->fc_rx ? RX_EN : 0x00);
252 cfg |= (pfc->fc_tx ? TX_EN : 0x00);
253 bgx_reg_write(bgx, lmacid, BGX_SMUX_CBFC_CTL, cfg);
254}
255EXPORT_SYMBOL(bgx_lmac_set_pfc);
256
Sunil Goutham4863dea2015-05-26 19:20:15 -0700257static void bgx_sgmii_change_link_state(struct lmac *lmac)
258{
259 struct bgx *bgx = lmac->bgx;
260 u64 cmr_cfg;
261 u64 port_cfg = 0;
262 u64 misc_ctl = 0;
263
264 cmr_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_CMRX_CFG);
265 cmr_cfg &= ~CMR_EN;
266 bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg);
267
268 port_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG);
269 misc_ctl = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_PCS_MISCX_CTL);
270
271 if (lmac->link_up) {
272 misc_ctl &= ~PCS_MISC_CTL_GMX_ENO;
273 port_cfg &= ~GMI_PORT_CFG_DUPLEX;
274 port_cfg |= (lmac->last_duplex << 2);
275 } else {
276 misc_ctl |= PCS_MISC_CTL_GMX_ENO;
277 }
278
279 switch (lmac->last_speed) {
280 case 10:
281 port_cfg &= ~GMI_PORT_CFG_SPEED; /* speed 0 */
282 port_cfg |= GMI_PORT_CFG_SPEED_MSB; /* speed_msb 1 */
283 port_cfg &= ~GMI_PORT_CFG_SLOT_TIME; /* slottime 0 */
284 misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
285 misc_ctl |= 50; /* samp_pt */
286 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 64);
287 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_BURST, 0);
288 break;
289 case 100:
290 port_cfg &= ~GMI_PORT_CFG_SPEED; /* speed 0 */
291 port_cfg &= ~GMI_PORT_CFG_SPEED_MSB; /* speed_msb 0 */
292 port_cfg &= ~GMI_PORT_CFG_SLOT_TIME; /* slottime 0 */
293 misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
294 misc_ctl |= 5; /* samp_pt */
295 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 64);
296 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_BURST, 0);
297 break;
298 case 1000:
299 port_cfg |= GMI_PORT_CFG_SPEED; /* speed 1 */
300 port_cfg &= ~GMI_PORT_CFG_SPEED_MSB; /* speed_msb 0 */
301 port_cfg |= GMI_PORT_CFG_SLOT_TIME; /* slottime 1 */
302 misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
303 misc_ctl |= 1; /* samp_pt */
304 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 512);
305 if (lmac->last_duplex)
306 bgx_reg_write(bgx, lmac->lmacid,
307 BGX_GMP_GMI_TXX_BURST, 0);
308 else
309 bgx_reg_write(bgx, lmac->lmacid,
310 BGX_GMP_GMI_TXX_BURST, 8192);
311 break;
312 default:
313 break;
314 }
315 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_PCS_MISCX_CTL, misc_ctl);
316 bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG, port_cfg);
317
318 port_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG);
319
Sunil Goutham64658592016-08-12 16:51:33 +0530320 /* Re-enable lmac */
Sunil Goutham4863dea2015-05-26 19:20:15 -0700321 cmr_cfg |= CMR_EN;
322 bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg);
Sunil Goutham64658592016-08-12 16:51:33 +0530323
324 if (bgx->is_rgx && (cmr_cfg & (CMR_PKT_RX_EN | CMR_PKT_TX_EN)))
325 xcv_setup_link(lmac->link_up, lmac->last_speed);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700326}
327
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700328static void bgx_lmac_handler(struct net_device *netdev)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700329{
330 struct lmac *lmac = container_of(netdev, struct lmac, netdev);
xypron.glpk@gmx.de099a7282016-05-17 21:40:38 +0200331 struct phy_device *phydev;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700332 int link_changed = 0;
333
334 if (!lmac)
335 return;
336
xypron.glpk@gmx.de099a7282016-05-17 21:40:38 +0200337 phydev = lmac->phydev;
338
Sunil Goutham4863dea2015-05-26 19:20:15 -0700339 if (!phydev->link && lmac->last_link)
340 link_changed = -1;
341
342 if (phydev->link &&
343 (lmac->last_duplex != phydev->duplex ||
344 lmac->last_link != phydev->link ||
345 lmac->last_speed != phydev->speed)) {
346 link_changed = 1;
347 }
348
349 lmac->last_link = phydev->link;
350 lmac->last_speed = phydev->speed;
351 lmac->last_duplex = phydev->duplex;
352
353 if (!link_changed)
354 return;
355
356 if (link_changed > 0)
357 lmac->link_up = true;
358 else
359 lmac->link_up = false;
360
361 if (lmac->is_sgmii)
362 bgx_sgmii_change_link_state(lmac);
363 else
364 bgx_xaui_check_link(lmac);
365}
366
367u64 bgx_get_rx_stats(int node, int bgx_idx, int lmac, int idx)
368{
369 struct bgx *bgx;
370
Sunil Goutham09de3912016-08-12 16:51:35 +0530371 bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700372 if (!bgx)
373 return 0;
374
375 if (idx > 8)
376 lmac = 0;
377 return bgx_reg_read(bgx, lmac, BGX_CMRX_RX_STAT0 + (idx * 8));
378}
379EXPORT_SYMBOL(bgx_get_rx_stats);
380
381u64 bgx_get_tx_stats(int node, int bgx_idx, int lmac, int idx)
382{
383 struct bgx *bgx;
384
Sunil Goutham09de3912016-08-12 16:51:35 +0530385 bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700386 if (!bgx)
387 return 0;
388
389 return bgx_reg_read(bgx, lmac, BGX_CMRX_TX_STAT0 + (idx * 8));
390}
391EXPORT_SYMBOL(bgx_get_tx_stats);
392
393static void bgx_flush_dmac_addrs(struct bgx *bgx, int lmac)
394{
395 u64 offset;
396
397 while (bgx->lmac[lmac].dmac > 0) {
398 offset = ((bgx->lmac[lmac].dmac - 1) * sizeof(u64)) +
399 (lmac * MAX_DMAC_PER_LMAC * sizeof(u64));
400 bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM + offset, 0);
401 bgx->lmac[lmac].dmac--;
402 }
403}
404
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300405/* Configure BGX LMAC in internal loopback mode */
406void bgx_lmac_internal_loopback(int node, int bgx_idx,
407 int lmac_idx, bool enable)
408{
409 struct bgx *bgx;
410 struct lmac *lmac;
411 u64 cfg;
412
Sunil Goutham09de3912016-08-12 16:51:35 +0530413 bgx = bgx_vnic[(node * MAX_BGX_PER_NODE) + bgx_idx];
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300414 if (!bgx)
415 return;
416
417 lmac = &bgx->lmac[lmac_idx];
418 if (lmac->is_sgmii) {
419 cfg = bgx_reg_read(bgx, lmac_idx, BGX_GMP_PCS_MRX_CTL);
420 if (enable)
421 cfg |= PCS_MRX_CTL_LOOPBACK1;
422 else
423 cfg &= ~PCS_MRX_CTL_LOOPBACK1;
424 bgx_reg_write(bgx, lmac_idx, BGX_GMP_PCS_MRX_CTL, cfg);
425 } else {
426 cfg = bgx_reg_read(bgx, lmac_idx, BGX_SPUX_CONTROL1);
427 if (enable)
428 cfg |= SPU_CTL_LOOPBACK;
429 else
430 cfg &= ~SPU_CTL_LOOPBACK;
431 bgx_reg_write(bgx, lmac_idx, BGX_SPUX_CONTROL1, cfg);
432 }
433}
434EXPORT_SYMBOL(bgx_lmac_internal_loopback);
435
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530436static int bgx_lmac_sgmii_init(struct bgx *bgx, struct lmac *lmac)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700437{
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530438 int lmacid = lmac->lmacid;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700439 u64 cfg;
440
441 bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_TXX_THRESH, 0x30);
442 /* max packet size */
443 bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_RXX_JABBER, MAX_FRAME_SIZE);
444
445 /* Disable frame alignment if using preamble */
446 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND);
447 if (cfg & 1)
448 bgx_reg_write(bgx, lmacid, BGX_GMP_GMI_TXX_SGMII_CTL, 0);
449
450 /* Enable lmac */
451 bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
452
453 /* PCS reset */
454 bgx_reg_modify(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, PCS_MRX_CTL_RESET);
455 if (bgx_poll_reg(bgx, lmacid, BGX_GMP_PCS_MRX_CTL,
456 PCS_MRX_CTL_RESET, true)) {
457 dev_err(&bgx->pdev->dev, "BGX PCS reset not completed\n");
458 return -1;
459 }
460
461 /* power down, reset autoneg, autoneg enable */
462 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_PCS_MRX_CTL);
463 cfg &= ~PCS_MRX_CTL_PWR_DN;
464 cfg |= (PCS_MRX_CTL_RST_AN | PCS_MRX_CTL_AN_EN);
465 bgx_reg_write(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, cfg);
466
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530467 if (lmac->lmac_type == BGX_MODE_QSGMII) {
468 /* Disable disparity check for QSGMII */
469 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_PCS_MISCX_CTL);
470 cfg &= ~PCS_MISC_CTL_DISP_EN;
471 bgx_reg_write(bgx, lmacid, BGX_GMP_PCS_MISCX_CTL, cfg);
472 return 0;
473 }
474
Sunil Goutham64658592016-08-12 16:51:33 +0530475 if (lmac->lmac_type == BGX_MODE_SGMII) {
476 if (bgx_poll_reg(bgx, lmacid, BGX_GMP_PCS_MRX_STATUS,
477 PCS_MRX_STATUS_AN_CPT, false)) {
478 dev_err(&bgx->pdev->dev, "BGX AN_CPT not completed\n");
479 return -1;
480 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700481 }
482
483 return 0;
484}
485
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530486static int bgx_lmac_xaui_init(struct bgx *bgx, struct lmac *lmac)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700487{
488 u64 cfg;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530489 int lmacid = lmac->lmacid;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700490
491 /* Reset SPU */
492 bgx_reg_modify(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET);
493 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET, true)) {
494 dev_err(&bgx->pdev->dev, "BGX SPU reset not completed\n");
495 return -1;
496 }
497
498 /* Disable LMAC */
499 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
500 cfg &= ~CMR_EN;
501 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
502
503 bgx_reg_modify(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_LOW_POWER);
504 /* Set interleaved running disparity for RXAUI */
Sunil Goutham93db2cf2016-08-12 16:51:44 +0530505 if (lmac->lmac_type == BGX_MODE_RXAUI)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700506 bgx_reg_modify(bgx, lmacid, BGX_SPUX_MISC_CONTROL,
Sunil Goutham93db2cf2016-08-12 16:51:44 +0530507 SPU_MISC_CTL_INTLV_RDISP);
508
509 /* Clear receive packet disable */
510 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_MISC_CONTROL);
511 cfg &= ~SPU_MISC_CTL_RX_DIS;
512 bgx_reg_write(bgx, lmacid, BGX_SPUX_MISC_CONTROL, cfg);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700513
514 /* clear all interrupts */
515 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_RX_INT);
516 bgx_reg_write(bgx, lmacid, BGX_SMUX_RX_INT, cfg);
517 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_INT);
518 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_INT, cfg);
519 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
520 bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
521
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530522 if (lmac->use_training) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700523 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LP_CUP, 0x00);
524 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LD_CUP, 0x00);
525 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LD_REP, 0x00);
526 /* training enable */
527 bgx_reg_modify(bgx, lmacid,
528 BGX_SPUX_BR_PMD_CRTL, SPU_PMD_CRTL_TRAIN_EN);
529 }
530
531 /* Append FCS to each packet */
532 bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_APPEND, SMU_TX_APPEND_FCS_D);
533
534 /* Disable forward error correction */
535 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_FEC_CONTROL);
536 cfg &= ~SPU_FEC_CTL_FEC_EN;
537 bgx_reg_write(bgx, lmacid, BGX_SPUX_FEC_CONTROL, cfg);
538
539 /* Disable autoneg */
540 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_AN_CONTROL);
541 cfg = cfg & ~(SPU_AN_CTL_AN_EN | SPU_AN_CTL_XNP_EN);
542 bgx_reg_write(bgx, lmacid, BGX_SPUX_AN_CONTROL, cfg);
543
544 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_AN_ADV);
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530545 if (lmac->lmac_type == BGX_MODE_10G_KR)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700546 cfg |= (1 << 23);
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530547 else if (lmac->lmac_type == BGX_MODE_40G_KR)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700548 cfg |= (1 << 24);
549 else
550 cfg &= ~((1 << 23) | (1 << 24));
551 cfg = cfg & (~((1ULL << 25) | (1ULL << 22) | (1ULL << 12)));
552 bgx_reg_write(bgx, lmacid, BGX_SPUX_AN_ADV, cfg);
553
554 cfg = bgx_reg_read(bgx, 0, BGX_SPU_DBG_CONTROL);
555 cfg &= ~SPU_DBG_CTL_AN_ARB_LINK_CHK_EN;
556 bgx_reg_write(bgx, 0, BGX_SPU_DBG_CONTROL, cfg);
557
558 /* Enable lmac */
559 bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
560
561 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_CONTROL1);
562 cfg &= ~SPU_CTL_LOW_POWER;
563 bgx_reg_write(bgx, lmacid, BGX_SPUX_CONTROL1, cfg);
564
565 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_CTL);
566 cfg &= ~SMU_TX_CTL_UNI_EN;
567 cfg |= SMU_TX_CTL_DIC_EN;
568 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_CTL, cfg);
569
Sunil Goutham430da202016-11-24 14:48:03 +0530570 /* Enable receive and transmission of pause frames */
571 bgx_reg_write(bgx, lmacid, BGX_SMUX_CBFC_CTL, ((0xffffULL << 32) |
572 BCK_EN | DRP_EN | TX_EN | RX_EN));
573 /* Configure pause time and interval */
574 bgx_reg_write(bgx, lmacid,
575 BGX_SMUX_TX_PAUSE_PKT_TIME, DEFAULT_PAUSE_TIME);
576 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_PAUSE_PKT_INTERVAL);
577 cfg &= ~0xFFFFull;
578 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_PAUSE_PKT_INTERVAL,
579 cfg | (DEFAULT_PAUSE_TIME - 0x1000));
580 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_PAUSE_ZERO, 0x01);
581
Sunil Goutham4863dea2015-05-26 19:20:15 -0700582 /* take lmac_count into account */
583 bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_THRESH, (0x100 - 1));
584 /* max packet size */
585 bgx_reg_modify(bgx, lmacid, BGX_SMUX_RX_JABBER, MAX_FRAME_SIZE);
586
587 return 0;
588}
589
590static int bgx_xaui_check_link(struct lmac *lmac)
591{
592 struct bgx *bgx = lmac->bgx;
593 int lmacid = lmac->lmacid;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530594 int lmac_type = lmac->lmac_type;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700595 u64 cfg;
596
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530597 if (lmac->use_training) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700598 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
599 if (!(cfg & (1ull << 13))) {
600 cfg = (1ull << 13) | (1ull << 14);
601 bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
602 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL);
603 cfg |= (1ull << 0);
604 bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL, cfg);
605 return -1;
606 }
607 }
608
609 /* wait for PCS to come out of reset */
610 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET, true)) {
611 dev_err(&bgx->pdev->dev, "BGX SPU reset not completed\n");
612 return -1;
613 }
614
615 if ((lmac_type == BGX_MODE_10G_KR) || (lmac_type == BGX_MODE_XFI) ||
616 (lmac_type == BGX_MODE_40G_KR) || (lmac_type == BGX_MODE_XLAUI)) {
617 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_BR_STATUS1,
618 SPU_BR_STATUS_BLK_LOCK, false)) {
619 dev_err(&bgx->pdev->dev,
620 "SPU_BR_STATUS_BLK_LOCK not completed\n");
621 return -1;
622 }
623 } else {
624 if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_BX_STATUS,
625 SPU_BX_STATUS_RX_ALIGN, false)) {
626 dev_err(&bgx->pdev->dev,
627 "SPU_BX_STATUS_RX_ALIGN not completed\n");
628 return -1;
629 }
630 }
631
632 /* Clear rcvflt bit (latching high) and read it back */
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530633 if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT)
634 bgx_reg_modify(bgx, lmacid,
635 BGX_SPUX_STATUS2, SPU_STATUS2_RCVFLT);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700636 if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT) {
637 dev_err(&bgx->pdev->dev, "Receive fault, retry training\n");
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530638 if (lmac->use_training) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700639 cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
640 if (!(cfg & (1ull << 13))) {
641 cfg = (1ull << 13) | (1ull << 14);
642 bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
643 cfg = bgx_reg_read(bgx, lmacid,
644 BGX_SPUX_BR_PMD_CRTL);
645 cfg |= (1ull << 0);
646 bgx_reg_write(bgx, lmacid,
647 BGX_SPUX_BR_PMD_CRTL, cfg);
648 return -1;
649 }
650 }
651 return -1;
652 }
653
Sunil Goutham4863dea2015-05-26 19:20:15 -0700654 /* Wait for BGX RX to be idle */
655 if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_CTL, SMU_CTL_RX_IDLE, false)) {
656 dev_err(&bgx->pdev->dev, "SMU RX not idle\n");
657 return -1;
658 }
659
660 /* Wait for BGX TX to be idle */
661 if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_CTL, SMU_CTL_TX_IDLE, false)) {
662 dev_err(&bgx->pdev->dev, "SMU TX not idle\n");
663 return -1;
664 }
665
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530666 /* Check for MAC RX faults */
667 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_RX_CTL);
668 /* 0 - Link is okay, 1 - Local fault, 2 - Remote fault */
669 cfg &= SMU_RX_CTL_STATUS;
670 if (!cfg)
671 return 0;
672
673 /* Rx local/remote fault seen.
674 * Do lmac reinit to see if condition recovers
675 */
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530676 bgx_lmac_xaui_init(bgx, lmac);
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530677
678 return -1;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700679}
680
681static void bgx_poll_for_link(struct work_struct *work)
682{
683 struct lmac *lmac;
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530684 u64 spu_link, smu_link;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700685
686 lmac = container_of(work, struct lmac, dwork.work);
687
688 /* Receive link is latching low. Force it high and verify it */
689 bgx_reg_modify(lmac->bgx, lmac->lmacid,
690 BGX_SPUX_STATUS1, SPU_STATUS1_RCV_LNK);
691 bgx_poll_reg(lmac->bgx, lmac->lmacid, BGX_SPUX_STATUS1,
692 SPU_STATUS1_RCV_LNK, false);
693
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530694 spu_link = bgx_reg_read(lmac->bgx, lmac->lmacid, BGX_SPUX_STATUS1);
695 smu_link = bgx_reg_read(lmac->bgx, lmac->lmacid, BGX_SMUX_RX_CTL);
696
697 if ((spu_link & SPU_STATUS1_RCV_LNK) &&
698 !(smu_link & SMU_RX_CTL_STATUS)) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700699 lmac->link_up = 1;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530700 if (lmac->lmac_type == BGX_MODE_XLAUI)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700701 lmac->last_speed = 40000;
702 else
703 lmac->last_speed = 10000;
704 lmac->last_duplex = 1;
705 } else {
706 lmac->link_up = 0;
Sunil Goutham0b72a9a2015-12-02 15:36:16 +0530707 lmac->last_speed = SPEED_UNKNOWN;
708 lmac->last_duplex = DUPLEX_UNKNOWN;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700709 }
710
711 if (lmac->last_link != lmac->link_up) {
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530712 if (lmac->link_up) {
713 if (bgx_xaui_check_link(lmac)) {
714 /* Errors, clear link_up state */
715 lmac->link_up = 0;
716 lmac->last_speed = SPEED_UNKNOWN;
717 lmac->last_duplex = DUPLEX_UNKNOWN;
718 }
719 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700720 lmac->last_link = lmac->link_up;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700721 }
722
723 queue_delayed_work(lmac->check_link, &lmac->dwork, HZ * 2);
724}
725
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530726static int phy_interface_mode(u8 lmac_type)
727{
728 if (lmac_type == BGX_MODE_QSGMII)
729 return PHY_INTERFACE_MODE_QSGMII;
Sunil Goutham64658592016-08-12 16:51:33 +0530730 if (lmac_type == BGX_MODE_RGMII)
731 return PHY_INTERFACE_MODE_RGMII;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530732
733 return PHY_INTERFACE_MODE_SGMII;
734}
735
Sunil Goutham4863dea2015-05-26 19:20:15 -0700736static int bgx_lmac_enable(struct bgx *bgx, u8 lmacid)
737{
738 struct lmac *lmac;
739 u64 cfg;
740
741 lmac = &bgx->lmac[lmacid];
742 lmac->bgx = bgx;
743
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530744 if ((lmac->lmac_type == BGX_MODE_SGMII) ||
Sunil Goutham64658592016-08-12 16:51:33 +0530745 (lmac->lmac_type == BGX_MODE_QSGMII) ||
746 (lmac->lmac_type == BGX_MODE_RGMII)) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700747 lmac->is_sgmii = 1;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530748 if (bgx_lmac_sgmii_init(bgx, lmac))
Sunil Goutham4863dea2015-05-26 19:20:15 -0700749 return -1;
750 } else {
751 lmac->is_sgmii = 0;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530752 if (bgx_lmac_xaui_init(bgx, lmac))
Sunil Goutham4863dea2015-05-26 19:20:15 -0700753 return -1;
754 }
755
756 if (lmac->is_sgmii) {
757 cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND);
758 cfg |= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */
759 bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND, cfg);
760 bgx_reg_write(bgx, lmacid, BGX_GMP_GMI_TXX_MIN_PKT, 60 - 1);
761 } else {
762 cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_APPEND);
763 cfg |= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */
764 bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_APPEND, cfg);
765 bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_MIN_PKT, 60 + 4);
766 }
767
768 /* Enable lmac */
Sunil Gouthambc69fdf2015-12-02 15:36:17 +0530769 bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700770
771 /* Restore default cfg, incase low level firmware changed it */
772 bgx_reg_write(bgx, lmacid, BGX_CMRX_RX_DMAC_CTL, 0x03);
773
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530774 if ((lmac->lmac_type != BGX_MODE_XFI) &&
775 (lmac->lmac_type != BGX_MODE_XLAUI) &&
776 (lmac->lmac_type != BGX_MODE_40G_KR) &&
777 (lmac->lmac_type != BGX_MODE_10G_KR)) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700778 if (!lmac->phydev)
779 return -ENODEV;
780
781 lmac->phydev->dev_flags = 0;
782
783 if (phy_connect_direct(&lmac->netdev, lmac->phydev,
784 bgx_lmac_handler,
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530785 phy_interface_mode(lmac->lmac_type)))
Sunil Goutham4863dea2015-05-26 19:20:15 -0700786 return -ENODEV;
787
788 phy_start_aneg(lmac->phydev);
789 } else {
790 lmac->check_link = alloc_workqueue("check_link", WQ_UNBOUND |
791 WQ_MEM_RECLAIM, 1);
792 if (!lmac->check_link)
793 return -ENOMEM;
794 INIT_DELAYED_WORK(&lmac->dwork, bgx_poll_for_link);
795 queue_delayed_work(lmac->check_link, &lmac->dwork, 0);
796 }
797
798 return 0;
799}
800
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700801static void bgx_lmac_disable(struct bgx *bgx, u8 lmacid)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700802{
803 struct lmac *lmac;
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530804 u64 cfg;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700805
806 lmac = &bgx->lmac[lmacid];
807 if (lmac->check_link) {
808 /* Destroy work queue */
Thanneeru Srinivasulua7b1f532015-12-02 15:36:14 +0530809 cancel_delayed_work_sync(&lmac->dwork);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700810 destroy_workqueue(lmac->check_link);
811 }
812
Sunil Goutham3f4c68c2016-06-27 15:30:02 +0530813 /* Disable packet reception */
814 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
815 cfg &= ~CMR_PKT_RX_EN;
816 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
817
818 /* Give chance for Rx/Tx FIFO to get drained */
819 bgx_poll_reg(bgx, lmacid, BGX_CMRX_RX_FIFO_LEN, (u64)0x1FFF, true);
820 bgx_poll_reg(bgx, lmacid, BGX_CMRX_TX_FIFO_LEN, (u64)0x3FFF, true);
821
822 /* Disable packet transmission */
823 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
824 cfg &= ~CMR_PKT_TX_EN;
825 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
826
827 /* Disable serdes lanes */
828 if (!lmac->is_sgmii)
829 bgx_reg_modify(bgx, lmacid,
830 BGX_SPUX_CONTROL1, SPU_CTL_LOW_POWER);
831 else
832 bgx_reg_modify(bgx, lmacid,
833 BGX_GMP_PCS_MRX_CTL, PCS_MRX_CTL_PWR_DN);
834
835 /* Disable LMAC */
836 cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
837 cfg &= ~CMR_EN;
838 bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
839
Sunil Goutham4863dea2015-05-26 19:20:15 -0700840 bgx_flush_dmac_addrs(bgx, lmacid);
841
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530842 if ((lmac->lmac_type != BGX_MODE_XFI) &&
843 (lmac->lmac_type != BGX_MODE_XLAUI) &&
844 (lmac->lmac_type != BGX_MODE_40G_KR) &&
845 (lmac->lmac_type != BGX_MODE_10G_KR) && lmac->phydev)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700846 phy_disconnect(lmac->phydev);
847
848 lmac->phydev = NULL;
849}
850
Sunil Goutham4863dea2015-05-26 19:20:15 -0700851static void bgx_init_hw(struct bgx *bgx)
852{
853 int i;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530854 struct lmac *lmac;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700855
856 bgx_reg_modify(bgx, 0, BGX_CMR_GLOBAL_CFG, CMR_GLOBAL_CFG_FCS_STRIP);
857 if (bgx_reg_read(bgx, 0, BGX_CMR_BIST_STATUS))
858 dev_err(&bgx->pdev->dev, "BGX%d BIST failed\n", bgx->bgx_id);
859
860 /* Set lmac type and lane2serdes mapping */
861 for (i = 0; i < bgx->lmac_count; i++) {
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530862 lmac = &bgx->lmac[i];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700863 bgx_reg_write(bgx, i, BGX_CMRX_CFG,
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530864 (lmac->lmac_type << 8) | lmac->lane_to_sds);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700865 bgx->lmac[i].lmacid_bd = lmac_count;
866 lmac_count++;
867 }
868
869 bgx_reg_write(bgx, 0, BGX_CMR_TX_LMACS, bgx->lmac_count);
870 bgx_reg_write(bgx, 0, BGX_CMR_RX_LMACS, bgx->lmac_count);
871
872 /* Set the backpressure AND mask */
873 for (i = 0; i < bgx->lmac_count; i++)
874 bgx_reg_modify(bgx, 0, BGX_CMR_CHAN_MSK_AND,
875 ((1ULL << MAX_BGX_CHANS_PER_LMAC) - 1) <<
876 (i * MAX_BGX_CHANS_PER_LMAC));
877
878 /* Disable all MAC filtering */
879 for (i = 0; i < RX_DMAC_COUNT; i++)
880 bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM + (i * 8), 0x00);
881
882 /* Disable MAC steering (NCSI traffic) */
883 for (i = 0; i < RX_TRAFFIC_STEER_RULE_COUNT; i++)
884 bgx_reg_write(bgx, 0, BGX_CMR_RX_STREERING + (i * 8), 0x00);
885}
886
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530887static u8 bgx_get_lane2sds_cfg(struct bgx *bgx, struct lmac *lmac)
888{
889 return (u8)(bgx_reg_read(bgx, lmac->lmacid, BGX_CMRX_CFG) & 0xFF);
890}
891
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530892static void bgx_print_qlm_mode(struct bgx *bgx, u8 lmacid)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700893{
894 struct device *dev = &bgx->pdev->dev;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530895 struct lmac *lmac;
896 char str[20];
Sunil Goutham57aaf632016-08-12 16:51:31 +0530897 u8 dlm;
898
Sunil Goutham64658592016-08-12 16:51:33 +0530899 if (lmacid > bgx->max_lmac)
Sunil Goutham57aaf632016-08-12 16:51:31 +0530900 return;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530901
902 lmac = &bgx->lmac[lmacid];
Sunil Goutham57aaf632016-08-12 16:51:31 +0530903 dlm = (lmacid / 2) + (bgx->bgx_id * 2);
Sunil Goutham09de3912016-08-12 16:51:35 +0530904 if (!bgx->is_dlm)
Sunil Goutham57aaf632016-08-12 16:51:31 +0530905 sprintf(str, "BGX%d QLM mode", bgx->bgx_id);
906 else
907 sprintf(str, "BGX%d DLM%d mode", bgx->bgx_id, dlm);
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530908
909 switch (lmac->lmac_type) {
910 case BGX_MODE_SGMII:
911 dev_info(dev, "%s: SGMII\n", (char *)str);
912 break;
913 case BGX_MODE_XAUI:
914 dev_info(dev, "%s: XAUI\n", (char *)str);
915 break;
916 case BGX_MODE_RXAUI:
917 dev_info(dev, "%s: RXAUI\n", (char *)str);
918 break;
919 case BGX_MODE_XFI:
920 if (!lmac->use_training)
921 dev_info(dev, "%s: XFI\n", (char *)str);
922 else
923 dev_info(dev, "%s: 10G_KR\n", (char *)str);
924 break;
925 case BGX_MODE_XLAUI:
926 if (!lmac->use_training)
927 dev_info(dev, "%s: XLAUI\n", (char *)str);
928 else
929 dev_info(dev, "%s: 40G_KR4\n", (char *)str);
930 break;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530931 case BGX_MODE_QSGMII:
932 if ((lmacid == 0) &&
933 (bgx_get_lane2sds_cfg(bgx, lmac) != lmacid))
934 return;
935 if ((lmacid == 2) &&
936 (bgx_get_lane2sds_cfg(bgx, lmac) == lmacid))
937 return;
938 dev_info(dev, "%s: QSGMII\n", (char *)str);
939 break;
Sunil Goutham64658592016-08-12 16:51:33 +0530940 case BGX_MODE_RGMII:
941 dev_info(dev, "%s: RGMII\n", (char *)str);
942 break;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530943 case BGX_MODE_INVALID:
944 /* Nothing to do */
945 break;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530946 }
947}
948
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530949static void lmac_set_lane2sds(struct bgx *bgx, struct lmac *lmac)
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530950{
951 switch (lmac->lmac_type) {
952 case BGX_MODE_SGMII:
953 case BGX_MODE_XFI:
954 lmac->lane_to_sds = lmac->lmacid;
955 break;
956 case BGX_MODE_XAUI:
957 case BGX_MODE_XLAUI:
Sunil Goutham64658592016-08-12 16:51:33 +0530958 case BGX_MODE_RGMII:
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530959 lmac->lane_to_sds = 0xE4;
960 break;
961 case BGX_MODE_RXAUI:
962 lmac->lane_to_sds = (lmac->lmacid) ? 0xE : 0x4;
963 break;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530964 case BGX_MODE_QSGMII:
965 /* There is no way to determine if DLM0/2 is QSGMII or
966 * DLM1/3 is configured to QSGMII as bootloader will
967 * configure all LMACs, so take whatever is configured
968 * by low level firmware.
969 */
970 lmac->lane_to_sds = bgx_get_lane2sds_cfg(bgx, lmac);
971 break;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530972 default:
973 lmac->lane_to_sds = 0;
974 break;
975 }
976}
977
Sunil Goutham64658592016-08-12 16:51:33 +0530978static void lmac_set_training(struct bgx *bgx, struct lmac *lmac, int lmacid)
979{
980 if ((lmac->lmac_type != BGX_MODE_10G_KR) &&
981 (lmac->lmac_type != BGX_MODE_40G_KR)) {
982 lmac->use_training = 0;
983 return;
984 }
985
986 lmac->use_training = bgx_reg_read(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL) &
987 SPU_PMD_CRTL_TRAIN_EN;
988}
989
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530990static void bgx_set_lmac_config(struct bgx *bgx, u8 idx)
991{
992 struct lmac *lmac;
Sunil Goutham57aaf632016-08-12 16:51:31 +0530993 struct lmac *olmac;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530994 u64 cmr_cfg;
Sunil Goutham57aaf632016-08-12 16:51:31 +0530995 u8 lmac_type;
996 u8 lane_to_sds;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530997
998 lmac = &bgx->lmac[idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700999
Sunil Goutham09de3912016-08-12 16:51:35 +05301000 if (!bgx->is_dlm || bgx->is_rgx) {
Sunil Goutham57aaf632016-08-12 16:51:31 +05301001 /* Read LMAC0 type to figure out QLM mode
1002 * This is configured by low level firmware
1003 */
1004 cmr_cfg = bgx_reg_read(bgx, 0, BGX_CMRX_CFG);
1005 lmac->lmac_type = (cmr_cfg >> 8) & 0x07;
Sunil Goutham64658592016-08-12 16:51:33 +05301006 if (bgx->is_rgx)
1007 lmac->lmac_type = BGX_MODE_RGMII;
1008 lmac_set_training(bgx, lmac, 0);
Sunil Goutham3f8057c2016-08-12 16:51:32 +05301009 lmac_set_lane2sds(bgx, lmac);
Sunil Goutham57aaf632016-08-12 16:51:31 +05301010 return;
1011 }
1012
1013 /* On 81xx BGX can be split across 2 DLMs
1014 * firmware programs lmac_type of LMAC0 and LMAC2
Sunil Goutham4863dea2015-05-26 19:20:15 -07001015 */
Sunil Goutham57aaf632016-08-12 16:51:31 +05301016 if ((idx == 0) || (idx == 2)) {
1017 cmr_cfg = bgx_reg_read(bgx, idx, BGX_CMRX_CFG);
1018 lmac_type = (u8)((cmr_cfg >> 8) & 0x07);
1019 lane_to_sds = (u8)(cmr_cfg & 0xFF);
1020 /* Check if config is not reset value */
1021 if ((lmac_type == 0) && (lane_to_sds == 0xE4))
1022 lmac->lmac_type = BGX_MODE_INVALID;
1023 else
1024 lmac->lmac_type = lmac_type;
Sunil Goutham64658592016-08-12 16:51:33 +05301025 lmac_set_training(bgx, lmac, lmac->lmacid);
Sunil Goutham3f8057c2016-08-12 16:51:32 +05301026 lmac_set_lane2sds(bgx, lmac);
Sunil Goutham57aaf632016-08-12 16:51:31 +05301027
Sunil Goutham57aaf632016-08-12 16:51:31 +05301028 olmac = &bgx->lmac[idx + 1];
Sunil Goutham52711562016-11-24 14:48:00 +05301029 /* Check if other LMAC on the same DLM is already configured by
1030 * firmware, if so use the same config or else set as same, as
1031 * that of LMAC 0/2.
1032 * This check is needed as on 80xx only one lane of each of the
1033 * DLM of BGX0 is used, so have to rely on firmware for
1034 * distingushing 80xx from 81xx.
1035 */
1036 cmr_cfg = bgx_reg_read(bgx, idx + 1, BGX_CMRX_CFG);
1037 lmac_type = (u8)((cmr_cfg >> 8) & 0x07);
1038 lane_to_sds = (u8)(cmr_cfg & 0xFF);
1039 if ((lmac_type == 0) && (lane_to_sds == 0xE4)) {
1040 olmac->lmac_type = lmac->lmac_type;
1041 lmac_set_lane2sds(bgx, olmac);
1042 } else {
1043 olmac->lmac_type = lmac_type;
1044 olmac->lane_to_sds = lane_to_sds;
1045 }
Sunil Goutham64658592016-08-12 16:51:33 +05301046 lmac_set_training(bgx, olmac, olmac->lmacid);
Sunil Goutham57aaf632016-08-12 16:51:31 +05301047 }
1048}
1049
1050static bool is_dlm0_in_bgx_mode(struct bgx *bgx)
1051{
1052 struct lmac *lmac;
1053
Sunil Goutham09de3912016-08-12 16:51:35 +05301054 if (!bgx->is_dlm)
Sunil Goutham57aaf632016-08-12 16:51:31 +05301055 return true;
1056
Sunil Goutham3f8057c2016-08-12 16:51:32 +05301057 lmac = &bgx->lmac[0];
Sunil Goutham57aaf632016-08-12 16:51:31 +05301058 if (lmac->lmac_type == BGX_MODE_INVALID)
1059 return false;
1060
1061 return true;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301062}
Sunil Goutham4863dea2015-05-26 19:20:15 -07001063
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301064static void bgx_get_qlm_mode(struct bgx *bgx)
1065{
Sunil Goutham57aaf632016-08-12 16:51:31 +05301066 struct lmac *lmac;
1067 struct lmac *lmac01;
1068 struct lmac *lmac23;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301069 u8 idx;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001070
Sunil Goutham57aaf632016-08-12 16:51:31 +05301071 /* Init all LMAC's type to invalid */
Sunil Goutham64658592016-08-12 16:51:33 +05301072 for (idx = 0; idx < bgx->max_lmac; idx++) {
Sunil Goutham57aaf632016-08-12 16:51:31 +05301073 lmac = &bgx->lmac[idx];
Sunil Goutham57aaf632016-08-12 16:51:31 +05301074 lmac->lmacid = idx;
Sunil Goutham64658592016-08-12 16:51:33 +05301075 lmac->lmac_type = BGX_MODE_INVALID;
1076 lmac->use_training = false;
Sunil Goutham57aaf632016-08-12 16:51:31 +05301077 }
1078
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301079 /* It is assumed that low level firmware sets this value */
1080 bgx->lmac_count = bgx_reg_read(bgx, 0, BGX_CMR_RX_LMACS) & 0x7;
Sunil Goutham64658592016-08-12 16:51:33 +05301081 if (bgx->lmac_count > bgx->max_lmac)
1082 bgx->lmac_count = bgx->max_lmac;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301083
Sunil Goutham64658592016-08-12 16:51:33 +05301084 for (idx = 0; idx < bgx->max_lmac; idx++)
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301085 bgx_set_lmac_config(bgx, idx);
Sunil Goutham57aaf632016-08-12 16:51:31 +05301086
Sunil Goutham09de3912016-08-12 16:51:35 +05301087 if (!bgx->is_dlm || bgx->is_rgx) {
Sunil Goutham57aaf632016-08-12 16:51:31 +05301088 bgx_print_qlm_mode(bgx, 0);
1089 return;
1090 }
1091
1092 if (bgx->lmac_count) {
1093 bgx_print_qlm_mode(bgx, 0);
1094 bgx_print_qlm_mode(bgx, 2);
1095 }
1096
1097 /* If DLM0 is not in BGX mode then LMAC0/1 have
1098 * to be configured with serdes lanes of DLM1
1099 */
1100 if (is_dlm0_in_bgx_mode(bgx) || (bgx->lmac_count > 2))
1101 return;
1102 for (idx = 0; idx < bgx->lmac_count; idx++) {
1103 lmac01 = &bgx->lmac[idx];
1104 lmac23 = &bgx->lmac[idx + 2];
1105 lmac01->lmac_type = lmac23->lmac_type;
1106 lmac01->lane_to_sds = lmac23->lane_to_sds;
1107 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001108}
1109
David Daney46b903a2015-08-10 17:58:37 -07001110#ifdef CONFIG_ACPI
1111
Robert Richter1d82efa2016-02-11 21:50:25 +05301112static int acpi_get_mac_address(struct device *dev, struct acpi_device *adev,
1113 u8 *dst)
David Daney46b903a2015-08-10 17:58:37 -07001114{
1115 u8 mac[ETH_ALEN];
1116 int ret;
1117
1118 ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
1119 "mac-address", mac, ETH_ALEN);
1120 if (ret)
1121 goto out;
1122
1123 if (!is_valid_ether_addr(mac)) {
Robert Richter1d82efa2016-02-11 21:50:25 +05301124 dev_err(dev, "MAC address invalid: %pM\n", mac);
David Daney46b903a2015-08-10 17:58:37 -07001125 ret = -EINVAL;
1126 goto out;
1127 }
1128
Robert Richter1d82efa2016-02-11 21:50:25 +05301129 dev_info(dev, "MAC address set to: %pM\n", mac);
1130
David Daney46b903a2015-08-10 17:58:37 -07001131 memcpy(dst, mac, ETH_ALEN);
1132out:
1133 return ret;
1134}
1135
1136/* Currently only sets the MAC address. */
1137static acpi_status bgx_acpi_register_phy(acpi_handle handle,
1138 u32 lvl, void *context, void **rv)
1139{
1140 struct bgx *bgx = context;
Robert Richter1d82efa2016-02-11 21:50:25 +05301141 struct device *dev = &bgx->pdev->dev;
David Daney46b903a2015-08-10 17:58:37 -07001142 struct acpi_device *adev;
1143
1144 if (acpi_bus_get_device(handle, &adev))
1145 goto out;
1146
Vadim Lomovtsev7aa48652017-01-12 07:28:06 -08001147 acpi_get_mac_address(dev, adev, bgx->lmac[bgx->acpi_lmac_idx].mac);
David Daney46b903a2015-08-10 17:58:37 -07001148
Vadim Lomovtsev7aa48652017-01-12 07:28:06 -08001149 SET_NETDEV_DEV(&bgx->lmac[bgx->acpi_lmac_idx].netdev, dev);
David Daney46b903a2015-08-10 17:58:37 -07001150
Vadim Lomovtsev7aa48652017-01-12 07:28:06 -08001151 bgx->lmac[bgx->acpi_lmac_idx].lmacid = bgx->acpi_lmac_idx;
1152 bgx->acpi_lmac_idx++; /* move to next LMAC */
David Daney46b903a2015-08-10 17:58:37 -07001153out:
David Daney46b903a2015-08-10 17:58:37 -07001154 return AE_OK;
1155}
1156
1157static acpi_status bgx_acpi_match_id(acpi_handle handle, u32 lvl,
1158 void *context, void **ret_val)
1159{
1160 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
1161 struct bgx *bgx = context;
1162 char bgx_sel[5];
1163
1164 snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
1165 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &string))) {
1166 pr_warn("Invalid link device\n");
1167 return AE_OK;
1168 }
1169
1170 if (strncmp(string.pointer, bgx_sel, 4))
1171 return AE_OK;
1172
1173 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1174 bgx_acpi_register_phy, NULL, bgx, NULL);
1175
1176 kfree(string.pointer);
1177 return AE_CTRL_TERMINATE;
1178}
1179
1180static int bgx_init_acpi_phy(struct bgx *bgx)
1181{
1182 acpi_get_devices(NULL, bgx_acpi_match_id, bgx, (void **)NULL);
1183 return 0;
1184}
1185
1186#else
1187
1188static int bgx_init_acpi_phy(struct bgx *bgx)
1189{
1190 return -ENODEV;
1191}
1192
1193#endif /* CONFIG_ACPI */
1194
Robert Richterde387e12015-08-10 17:58:36 -07001195#if IS_ENABLED(CONFIG_OF_MDIO)
1196
1197static int bgx_init_of_phy(struct bgx *bgx)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001198{
David Daneyeee326f2016-02-11 21:50:24 +05301199 struct fwnode_handle *fwn;
David Daneyb7d3e3d2016-03-14 17:30:39 -07001200 struct device_node *node = NULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001201 u8 lmac = 0;
Robert Richterde387e12015-08-10 17:58:36 -07001202
David Daneyeee326f2016-02-11 21:50:24 +05301203 device_for_each_child_node(&bgx->pdev->dev, fwn) {
David Daney5fc7cf12016-03-11 09:53:09 -08001204 struct phy_device *pd;
David Daneyeee326f2016-02-11 21:50:24 +05301205 struct device_node *phy_np;
David Daneyb7d3e3d2016-03-14 17:30:39 -07001206 const char *mac;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001207
David Daney5fc7cf12016-03-11 09:53:09 -08001208 /* Should always be an OF node. But if it is not, we
1209 * cannot handle it, so exit the loop.
David Daneyeee326f2016-02-11 21:50:24 +05301210 */
David Daneyb7d3e3d2016-03-14 17:30:39 -07001211 node = to_of_node(fwn);
David Daneyeee326f2016-02-11 21:50:24 +05301212 if (!node)
1213 break;
1214
David Daneyeee326f2016-02-11 21:50:24 +05301215 mac = of_get_mac_address(node);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001216 if (mac)
1217 ether_addr_copy(bgx->lmac[lmac].mac, mac);
1218
1219 SET_NETDEV_DEV(&bgx->lmac[lmac].netdev, &bgx->pdev->dev);
1220 bgx->lmac[lmac].lmacid = lmac;
David Daney5fc7cf12016-03-11 09:53:09 -08001221
1222 phy_np = of_parse_phandle(node, "phy-handle", 0);
1223 /* If there is no phy or defective firmware presents
1224 * this cortina phy, for which there is no driver
1225 * support, ignore it.
1226 */
1227 if (phy_np &&
1228 !of_device_is_compatible(phy_np, "cortina,cs4223-slice")) {
1229 /* Wait until the phy drivers are available */
1230 pd = of_phy_find_device(phy_np);
1231 if (!pd)
David Daneyb7d3e3d2016-03-14 17:30:39 -07001232 goto defer;
David Daney5fc7cf12016-03-11 09:53:09 -08001233 bgx->lmac[lmac].phydev = pd;
1234 }
1235
Sunil Goutham4863dea2015-05-26 19:20:15 -07001236 lmac++;
Sunil Goutham64658592016-08-12 16:51:33 +05301237 if (lmac == bgx->max_lmac) {
David Daney65c66af2016-04-08 13:37:27 -07001238 of_node_put(node);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001239 break;
David Daney65c66af2016-04-08 13:37:27 -07001240 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001241 }
Robert Richterde387e12015-08-10 17:58:36 -07001242 return 0;
David Daneyb7d3e3d2016-03-14 17:30:39 -07001243
1244defer:
1245 /* We are bailing out, try not to leak device reference counts
1246 * for phy devices we may have already found.
1247 */
1248 while (lmac) {
1249 if (bgx->lmac[lmac].phydev) {
1250 put_device(&bgx->lmac[lmac].phydev->mdio.dev);
1251 bgx->lmac[lmac].phydev = NULL;
1252 }
1253 lmac--;
1254 }
1255 of_node_put(node);
1256 return -EPROBE_DEFER;
Robert Richterde387e12015-08-10 17:58:36 -07001257}
1258
1259#else
1260
1261static int bgx_init_of_phy(struct bgx *bgx)
1262{
1263 return -ENODEV;
1264}
1265
1266#endif /* CONFIG_OF_MDIO */
1267
1268static int bgx_init_phy(struct bgx *bgx)
1269{
David Daney46b903a2015-08-10 17:58:37 -07001270 if (!acpi_disabled)
1271 return bgx_init_acpi_phy(bgx);
1272
Robert Richterde387e12015-08-10 17:58:36 -07001273 return bgx_init_of_phy(bgx);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001274}
1275
1276static int bgx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1277{
1278 int err;
1279 struct device *dev = &pdev->dev;
1280 struct bgx *bgx = NULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001281 u8 lmac;
Sunil Goutham57aaf632016-08-12 16:51:31 +05301282 u16 sdevid;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001283
1284 bgx = devm_kzalloc(dev, sizeof(*bgx), GFP_KERNEL);
1285 if (!bgx)
1286 return -ENOMEM;
1287 bgx->pdev = pdev;
1288
1289 pci_set_drvdata(pdev, bgx);
1290
1291 err = pci_enable_device(pdev);
1292 if (err) {
1293 dev_err(dev, "Failed to enable PCI device\n");
1294 pci_set_drvdata(pdev, NULL);
1295 return err;
1296 }
1297
1298 err = pci_request_regions(pdev, DRV_NAME);
1299 if (err) {
1300 dev_err(dev, "PCI request regions failed 0x%x\n", err);
1301 goto err_disable_device;
1302 }
1303
1304 /* MAP configuration registers */
1305 bgx->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
1306 if (!bgx->reg_base) {
1307 dev_err(dev, "BGX: Cannot map CSR memory space, aborting\n");
1308 err = -ENOMEM;
1309 goto err_release_regions;
1310 }
Robert Richterd768b672015-06-02 11:00:18 -07001311
Sunil Goutham64658592016-08-12 16:51:33 +05301312 pci_read_config_word(pdev, PCI_DEVICE_ID, &sdevid);
1313 if (sdevid != PCI_DEVICE_ID_THUNDER_RGX) {
Radha Mohan Chintakuntla612e94b2016-11-15 17:37:16 +05301314 bgx->bgx_id = (pci_resource_start(pdev,
1315 PCI_CFG_REG_BAR_NUM) >> 24) & BGX_ID_MASK;
Sunil Goutham09de3912016-08-12 16:51:35 +05301316 bgx->bgx_id += nic_get_node_id(pdev) * MAX_BGX_PER_NODE;
Sunil Goutham64658592016-08-12 16:51:33 +05301317 bgx->max_lmac = MAX_LMAC_PER_BGX;
1318 bgx_vnic[bgx->bgx_id] = bgx;
1319 } else {
1320 bgx->is_rgx = true;
1321 bgx->max_lmac = 1;
1322 bgx->bgx_id = MAX_BGX_PER_CN81XX - 1;
1323 bgx_vnic[bgx->bgx_id] = bgx;
1324 xcv_init_hw();
1325 }
1326
Sunil Goutham09de3912016-08-12 16:51:35 +05301327 /* On 81xx all are DLMs and on 83xx there are 3 BGX QLMs and one
1328 * BGX i.e BGX2 can be split across 2 DLMs.
1329 */
1330 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &sdevid);
1331 if ((sdevid == PCI_SUBSYS_DEVID_81XX_BGX) ||
1332 ((sdevid == PCI_SUBSYS_DEVID_83XX_BGX) && (bgx->bgx_id == 2)))
1333 bgx->is_dlm = true;
1334
Sunil Goutham4863dea2015-05-26 19:20:15 -07001335 bgx_get_qlm_mode(bgx);
1336
Robert Richterde387e12015-08-10 17:58:36 -07001337 err = bgx_init_phy(bgx);
1338 if (err)
1339 goto err_enable;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001340
1341 bgx_init_hw(bgx);
1342
1343 /* Enable all LMACs */
1344 for (lmac = 0; lmac < bgx->lmac_count; lmac++) {
1345 err = bgx_lmac_enable(bgx, lmac);
1346 if (err) {
1347 dev_err(dev, "BGX%d failed to enable lmac%d\n",
1348 bgx->bgx_id, lmac);
Sunil Goutham57aaf632016-08-12 16:51:31 +05301349 while (lmac)
1350 bgx_lmac_disable(bgx, --lmac);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001351 goto err_enable;
1352 }
1353 }
1354
1355 return 0;
1356
1357err_enable:
1358 bgx_vnic[bgx->bgx_id] = NULL;
1359err_release_regions:
1360 pci_release_regions(pdev);
1361err_disable_device:
1362 pci_disable_device(pdev);
1363 pci_set_drvdata(pdev, NULL);
1364 return err;
1365}
1366
1367static void bgx_remove(struct pci_dev *pdev)
1368{
1369 struct bgx *bgx = pci_get_drvdata(pdev);
1370 u8 lmac;
1371
1372 /* Disable all LMACs */
1373 for (lmac = 0; lmac < bgx->lmac_count; lmac++)
1374 bgx_lmac_disable(bgx, lmac);
1375
1376 bgx_vnic[bgx->bgx_id] = NULL;
1377 pci_release_regions(pdev);
1378 pci_disable_device(pdev);
1379 pci_set_drvdata(pdev, NULL);
1380}
1381
1382static struct pci_driver bgx_driver = {
1383 .name = DRV_NAME,
1384 .id_table = bgx_id_table,
1385 .probe = bgx_probe,
1386 .remove = bgx_remove,
1387};
1388
1389static int __init bgx_init_module(void)
1390{
1391 pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
1392
1393 return pci_register_driver(&bgx_driver);
1394}
1395
1396static void __exit bgx_cleanup_module(void)
1397{
1398 pci_unregister_driver(&bgx_driver);
1399}
1400
1401module_init(bgx_init_module);
1402module_exit(bgx_cleanup_module);