blob: dfb2bad7ced5f6544a4b4900af4a1e768de3f50a [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
Sunil Gouthamfff37fd2017-01-25 17:36:24 +0530898 if (!bgx->is_dlm && lmacid)
Sunil Goutham57aaf632016-08-12 16:51:31 +0530899 return;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530900
901 lmac = &bgx->lmac[lmacid];
Sunil Goutham09de3912016-08-12 16:51:35 +0530902 if (!bgx->is_dlm)
Sunil Goutham57aaf632016-08-12 16:51:31 +0530903 sprintf(str, "BGX%d QLM mode", bgx->bgx_id);
904 else
Sunil Gouthamfff37fd2017-01-25 17:36:24 +0530905 sprintf(str, "BGX%d LMAC%d mode", bgx->bgx_id, lmacid);
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530906
907 switch (lmac->lmac_type) {
908 case BGX_MODE_SGMII:
909 dev_info(dev, "%s: SGMII\n", (char *)str);
910 break;
911 case BGX_MODE_XAUI:
912 dev_info(dev, "%s: XAUI\n", (char *)str);
913 break;
914 case BGX_MODE_RXAUI:
915 dev_info(dev, "%s: RXAUI\n", (char *)str);
916 break;
917 case BGX_MODE_XFI:
918 if (!lmac->use_training)
919 dev_info(dev, "%s: XFI\n", (char *)str);
920 else
921 dev_info(dev, "%s: 10G_KR\n", (char *)str);
922 break;
923 case BGX_MODE_XLAUI:
924 if (!lmac->use_training)
925 dev_info(dev, "%s: XLAUI\n", (char *)str);
926 else
927 dev_info(dev, "%s: 40G_KR4\n", (char *)str);
928 break;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530929 case BGX_MODE_QSGMII:
930 if ((lmacid == 0) &&
931 (bgx_get_lane2sds_cfg(bgx, lmac) != lmacid))
932 return;
933 if ((lmacid == 2) &&
934 (bgx_get_lane2sds_cfg(bgx, lmac) == lmacid))
935 return;
936 dev_info(dev, "%s: QSGMII\n", (char *)str);
937 break;
Sunil Goutham64658592016-08-12 16:51:33 +0530938 case BGX_MODE_RGMII:
939 dev_info(dev, "%s: RGMII\n", (char *)str);
940 break;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530941 case BGX_MODE_INVALID:
942 /* Nothing to do */
943 break;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530944 }
945}
946
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530947static void lmac_set_lane2sds(struct bgx *bgx, struct lmac *lmac)
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530948{
949 switch (lmac->lmac_type) {
950 case BGX_MODE_SGMII:
951 case BGX_MODE_XFI:
952 lmac->lane_to_sds = lmac->lmacid;
953 break;
954 case BGX_MODE_XAUI:
955 case BGX_MODE_XLAUI:
Sunil Goutham64658592016-08-12 16:51:33 +0530956 case BGX_MODE_RGMII:
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530957 lmac->lane_to_sds = 0xE4;
958 break;
959 case BGX_MODE_RXAUI:
960 lmac->lane_to_sds = (lmac->lmacid) ? 0xE : 0x4;
961 break;
Sunil Goutham3f8057c2016-08-12 16:51:32 +0530962 case BGX_MODE_QSGMII:
963 /* There is no way to determine if DLM0/2 is QSGMII or
964 * DLM1/3 is configured to QSGMII as bootloader will
965 * configure all LMACs, so take whatever is configured
966 * by low level firmware.
967 */
968 lmac->lane_to_sds = bgx_get_lane2sds_cfg(bgx, lmac);
969 break;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530970 default:
971 lmac->lane_to_sds = 0;
972 break;
973 }
974}
975
Sunil Goutham64658592016-08-12 16:51:33 +0530976static void lmac_set_training(struct bgx *bgx, struct lmac *lmac, int lmacid)
977{
978 if ((lmac->lmac_type != BGX_MODE_10G_KR) &&
979 (lmac->lmac_type != BGX_MODE_40G_KR)) {
980 lmac->use_training = 0;
981 return;
982 }
983
984 lmac->use_training = bgx_reg_read(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL) &
985 SPU_PMD_CRTL_TRAIN_EN;
986}
987
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530988static void bgx_set_lmac_config(struct bgx *bgx, u8 idx)
989{
990 struct lmac *lmac;
991 u64 cmr_cfg;
Sunil Goutham57aaf632016-08-12 16:51:31 +0530992 u8 lmac_type;
993 u8 lane_to_sds;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +0530994
995 lmac = &bgx->lmac[idx];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700996
Sunil Goutham09de3912016-08-12 16:51:35 +0530997 if (!bgx->is_dlm || bgx->is_rgx) {
Sunil Goutham57aaf632016-08-12 16:51:31 +0530998 /* Read LMAC0 type to figure out QLM mode
999 * This is configured by low level firmware
1000 */
1001 cmr_cfg = bgx_reg_read(bgx, 0, BGX_CMRX_CFG);
1002 lmac->lmac_type = (cmr_cfg >> 8) & 0x07;
Sunil Goutham64658592016-08-12 16:51:33 +05301003 if (bgx->is_rgx)
1004 lmac->lmac_type = BGX_MODE_RGMII;
1005 lmac_set_training(bgx, lmac, 0);
Sunil Goutham3f8057c2016-08-12 16:51:32 +05301006 lmac_set_lane2sds(bgx, lmac);
Sunil Goutham57aaf632016-08-12 16:51:31 +05301007 return;
1008 }
1009
Sunil Gouthamfff37fd2017-01-25 17:36:24 +05301010 /* For DLMs or SLMs on 80/81/83xx so many lane configurations
1011 * are possible and vary across boards. Also Kernel doesn't have
1012 * any way to identify board type/info and since firmware does,
1013 * just take lmac type and serdes lane config as is.
Sunil Goutham4863dea2015-05-26 19:20:15 -07001014 */
Sunil Gouthamfff37fd2017-01-25 17:36:24 +05301015 cmr_cfg = bgx_reg_read(bgx, idx, BGX_CMRX_CFG);
1016 lmac_type = (u8)((cmr_cfg >> 8) & 0x07);
1017 lane_to_sds = (u8)(cmr_cfg & 0xFF);
1018 /* Check if config is reset value */
1019 if ((lmac_type == 0) && (lane_to_sds == 0xE4))
1020 lmac->lmac_type = BGX_MODE_INVALID;
1021 else
1022 lmac->lmac_type = lmac_type;
1023 lmac->lane_to_sds = lane_to_sds;
1024 lmac_set_training(bgx, lmac, lmac->lmacid);
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301025}
Sunil Goutham4863dea2015-05-26 19:20:15 -07001026
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301027static void bgx_get_qlm_mode(struct bgx *bgx)
1028{
Sunil Goutham57aaf632016-08-12 16:51:31 +05301029 struct lmac *lmac;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301030 u8 idx;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001031
Sunil Goutham57aaf632016-08-12 16:51:31 +05301032 /* Init all LMAC's type to invalid */
Sunil Goutham64658592016-08-12 16:51:33 +05301033 for (idx = 0; idx < bgx->max_lmac; idx++) {
Sunil Goutham57aaf632016-08-12 16:51:31 +05301034 lmac = &bgx->lmac[idx];
Sunil Goutham57aaf632016-08-12 16:51:31 +05301035 lmac->lmacid = idx;
Sunil Goutham64658592016-08-12 16:51:33 +05301036 lmac->lmac_type = BGX_MODE_INVALID;
1037 lmac->use_training = false;
Sunil Goutham57aaf632016-08-12 16:51:31 +05301038 }
1039
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301040 /* It is assumed that low level firmware sets this value */
1041 bgx->lmac_count = bgx_reg_read(bgx, 0, BGX_CMR_RX_LMACS) & 0x7;
Sunil Goutham64658592016-08-12 16:51:33 +05301042 if (bgx->lmac_count > bgx->max_lmac)
1043 bgx->lmac_count = bgx->max_lmac;
Sunil Goutham0bcb7d52016-08-12 16:51:30 +05301044
Sunil Goutham57aaf632016-08-12 16:51:31 +05301045 for (idx = 0; idx < bgx->lmac_count; idx++) {
Sunil Gouthamfff37fd2017-01-25 17:36:24 +05301046 bgx_set_lmac_config(bgx, idx);
1047 bgx_print_qlm_mode(bgx, idx);
Sunil Goutham57aaf632016-08-12 16:51:31 +05301048 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001049}
1050
David Daney46b903a2015-08-10 17:58:37 -07001051#ifdef CONFIG_ACPI
1052
Robert Richter1d82efa2016-02-11 21:50:25 +05301053static int acpi_get_mac_address(struct device *dev, struct acpi_device *adev,
1054 u8 *dst)
David Daney46b903a2015-08-10 17:58:37 -07001055{
1056 u8 mac[ETH_ALEN];
1057 int ret;
1058
1059 ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
1060 "mac-address", mac, ETH_ALEN);
1061 if (ret)
1062 goto out;
1063
1064 if (!is_valid_ether_addr(mac)) {
Robert Richter1d82efa2016-02-11 21:50:25 +05301065 dev_err(dev, "MAC address invalid: %pM\n", mac);
David Daney46b903a2015-08-10 17:58:37 -07001066 ret = -EINVAL;
1067 goto out;
1068 }
1069
Robert Richter1d82efa2016-02-11 21:50:25 +05301070 dev_info(dev, "MAC address set to: %pM\n", mac);
1071
David Daney46b903a2015-08-10 17:58:37 -07001072 memcpy(dst, mac, ETH_ALEN);
1073out:
1074 return ret;
1075}
1076
1077/* Currently only sets the MAC address. */
1078static acpi_status bgx_acpi_register_phy(acpi_handle handle,
1079 u32 lvl, void *context, void **rv)
1080{
1081 struct bgx *bgx = context;
Robert Richter1d82efa2016-02-11 21:50:25 +05301082 struct device *dev = &bgx->pdev->dev;
David Daney46b903a2015-08-10 17:58:37 -07001083 struct acpi_device *adev;
1084
1085 if (acpi_bus_get_device(handle, &adev))
1086 goto out;
1087
Vadim Lomovtsev7aa48652017-01-12 07:28:06 -08001088 acpi_get_mac_address(dev, adev, bgx->lmac[bgx->acpi_lmac_idx].mac);
David Daney46b903a2015-08-10 17:58:37 -07001089
Vadim Lomovtsev7aa48652017-01-12 07:28:06 -08001090 SET_NETDEV_DEV(&bgx->lmac[bgx->acpi_lmac_idx].netdev, dev);
David Daney46b903a2015-08-10 17:58:37 -07001091
Vadim Lomovtsev7aa48652017-01-12 07:28:06 -08001092 bgx->lmac[bgx->acpi_lmac_idx].lmacid = bgx->acpi_lmac_idx;
1093 bgx->acpi_lmac_idx++; /* move to next LMAC */
David Daney46b903a2015-08-10 17:58:37 -07001094out:
David Daney46b903a2015-08-10 17:58:37 -07001095 return AE_OK;
1096}
1097
1098static acpi_status bgx_acpi_match_id(acpi_handle handle, u32 lvl,
1099 void *context, void **ret_val)
1100{
1101 struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
1102 struct bgx *bgx = context;
1103 char bgx_sel[5];
1104
1105 snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
1106 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &string))) {
1107 pr_warn("Invalid link device\n");
1108 return AE_OK;
1109 }
1110
1111 if (strncmp(string.pointer, bgx_sel, 4))
1112 return AE_OK;
1113
1114 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1115 bgx_acpi_register_phy, NULL, bgx, NULL);
1116
1117 kfree(string.pointer);
1118 return AE_CTRL_TERMINATE;
1119}
1120
1121static int bgx_init_acpi_phy(struct bgx *bgx)
1122{
1123 acpi_get_devices(NULL, bgx_acpi_match_id, bgx, (void **)NULL);
1124 return 0;
1125}
1126
1127#else
1128
1129static int bgx_init_acpi_phy(struct bgx *bgx)
1130{
1131 return -ENODEV;
1132}
1133
1134#endif /* CONFIG_ACPI */
1135
Robert Richterde387e12015-08-10 17:58:36 -07001136#if IS_ENABLED(CONFIG_OF_MDIO)
1137
1138static int bgx_init_of_phy(struct bgx *bgx)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001139{
David Daneyeee326f2016-02-11 21:50:24 +05301140 struct fwnode_handle *fwn;
David Daneyb7d3e3d2016-03-14 17:30:39 -07001141 struct device_node *node = NULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001142 u8 lmac = 0;
Robert Richterde387e12015-08-10 17:58:36 -07001143
David Daneyeee326f2016-02-11 21:50:24 +05301144 device_for_each_child_node(&bgx->pdev->dev, fwn) {
David Daney5fc7cf12016-03-11 09:53:09 -08001145 struct phy_device *pd;
David Daneyeee326f2016-02-11 21:50:24 +05301146 struct device_node *phy_np;
David Daneyb7d3e3d2016-03-14 17:30:39 -07001147 const char *mac;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001148
David Daney5fc7cf12016-03-11 09:53:09 -08001149 /* Should always be an OF node. But if it is not, we
1150 * cannot handle it, so exit the loop.
David Daneyeee326f2016-02-11 21:50:24 +05301151 */
David Daneyb7d3e3d2016-03-14 17:30:39 -07001152 node = to_of_node(fwn);
David Daneyeee326f2016-02-11 21:50:24 +05301153 if (!node)
1154 break;
1155
David Daneyeee326f2016-02-11 21:50:24 +05301156 mac = of_get_mac_address(node);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001157 if (mac)
1158 ether_addr_copy(bgx->lmac[lmac].mac, mac);
1159
1160 SET_NETDEV_DEV(&bgx->lmac[lmac].netdev, &bgx->pdev->dev);
1161 bgx->lmac[lmac].lmacid = lmac;
David Daney5fc7cf12016-03-11 09:53:09 -08001162
1163 phy_np = of_parse_phandle(node, "phy-handle", 0);
1164 /* If there is no phy or defective firmware presents
1165 * this cortina phy, for which there is no driver
1166 * support, ignore it.
1167 */
1168 if (phy_np &&
1169 !of_device_is_compatible(phy_np, "cortina,cs4223-slice")) {
1170 /* Wait until the phy drivers are available */
1171 pd = of_phy_find_device(phy_np);
1172 if (!pd)
David Daneyb7d3e3d2016-03-14 17:30:39 -07001173 goto defer;
David Daney5fc7cf12016-03-11 09:53:09 -08001174 bgx->lmac[lmac].phydev = pd;
1175 }
1176
Sunil Goutham4863dea2015-05-26 19:20:15 -07001177 lmac++;
Sunil Goutham64658592016-08-12 16:51:33 +05301178 if (lmac == bgx->max_lmac) {
David Daney65c66af2016-04-08 13:37:27 -07001179 of_node_put(node);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001180 break;
David Daney65c66af2016-04-08 13:37:27 -07001181 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001182 }
Robert Richterde387e12015-08-10 17:58:36 -07001183 return 0;
David Daneyb7d3e3d2016-03-14 17:30:39 -07001184
1185defer:
1186 /* We are bailing out, try not to leak device reference counts
1187 * for phy devices we may have already found.
1188 */
1189 while (lmac) {
1190 if (bgx->lmac[lmac].phydev) {
1191 put_device(&bgx->lmac[lmac].phydev->mdio.dev);
1192 bgx->lmac[lmac].phydev = NULL;
1193 }
1194 lmac--;
1195 }
1196 of_node_put(node);
1197 return -EPROBE_DEFER;
Robert Richterde387e12015-08-10 17:58:36 -07001198}
1199
1200#else
1201
1202static int bgx_init_of_phy(struct bgx *bgx)
1203{
1204 return -ENODEV;
1205}
1206
1207#endif /* CONFIG_OF_MDIO */
1208
1209static int bgx_init_phy(struct bgx *bgx)
1210{
David Daney46b903a2015-08-10 17:58:37 -07001211 if (!acpi_disabled)
1212 return bgx_init_acpi_phy(bgx);
1213
Robert Richterde387e12015-08-10 17:58:36 -07001214 return bgx_init_of_phy(bgx);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001215}
1216
1217static int bgx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1218{
1219 int err;
1220 struct device *dev = &pdev->dev;
1221 struct bgx *bgx = NULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001222 u8 lmac;
Sunil Goutham57aaf632016-08-12 16:51:31 +05301223 u16 sdevid;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001224
1225 bgx = devm_kzalloc(dev, sizeof(*bgx), GFP_KERNEL);
1226 if (!bgx)
1227 return -ENOMEM;
1228 bgx->pdev = pdev;
1229
1230 pci_set_drvdata(pdev, bgx);
1231
1232 err = pci_enable_device(pdev);
1233 if (err) {
1234 dev_err(dev, "Failed to enable PCI device\n");
1235 pci_set_drvdata(pdev, NULL);
1236 return err;
1237 }
1238
1239 err = pci_request_regions(pdev, DRV_NAME);
1240 if (err) {
1241 dev_err(dev, "PCI request regions failed 0x%x\n", err);
1242 goto err_disable_device;
1243 }
1244
1245 /* MAP configuration registers */
1246 bgx->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
1247 if (!bgx->reg_base) {
1248 dev_err(dev, "BGX: Cannot map CSR memory space, aborting\n");
1249 err = -ENOMEM;
1250 goto err_release_regions;
1251 }
Robert Richterd768b672015-06-02 11:00:18 -07001252
Sunil Goutham64658592016-08-12 16:51:33 +05301253 pci_read_config_word(pdev, PCI_DEVICE_ID, &sdevid);
1254 if (sdevid != PCI_DEVICE_ID_THUNDER_RGX) {
Radha Mohan Chintakuntla612e94b2016-11-15 17:37:16 +05301255 bgx->bgx_id = (pci_resource_start(pdev,
1256 PCI_CFG_REG_BAR_NUM) >> 24) & BGX_ID_MASK;
Sunil Goutham09de3912016-08-12 16:51:35 +05301257 bgx->bgx_id += nic_get_node_id(pdev) * MAX_BGX_PER_NODE;
Sunil Goutham64658592016-08-12 16:51:33 +05301258 bgx->max_lmac = MAX_LMAC_PER_BGX;
1259 bgx_vnic[bgx->bgx_id] = bgx;
1260 } else {
1261 bgx->is_rgx = true;
1262 bgx->max_lmac = 1;
1263 bgx->bgx_id = MAX_BGX_PER_CN81XX - 1;
1264 bgx_vnic[bgx->bgx_id] = bgx;
1265 xcv_init_hw();
1266 }
1267
Sunil Goutham09de3912016-08-12 16:51:35 +05301268 /* On 81xx all are DLMs and on 83xx there are 3 BGX QLMs and one
1269 * BGX i.e BGX2 can be split across 2 DLMs.
1270 */
1271 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &sdevid);
1272 if ((sdevid == PCI_SUBSYS_DEVID_81XX_BGX) ||
1273 ((sdevid == PCI_SUBSYS_DEVID_83XX_BGX) && (bgx->bgx_id == 2)))
1274 bgx->is_dlm = true;
1275
Sunil Goutham4863dea2015-05-26 19:20:15 -07001276 bgx_get_qlm_mode(bgx);
1277
Robert Richterde387e12015-08-10 17:58:36 -07001278 err = bgx_init_phy(bgx);
1279 if (err)
1280 goto err_enable;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001281
1282 bgx_init_hw(bgx);
1283
1284 /* Enable all LMACs */
1285 for (lmac = 0; lmac < bgx->lmac_count; lmac++) {
1286 err = bgx_lmac_enable(bgx, lmac);
1287 if (err) {
1288 dev_err(dev, "BGX%d failed to enable lmac%d\n",
1289 bgx->bgx_id, lmac);
Sunil Goutham57aaf632016-08-12 16:51:31 +05301290 while (lmac)
1291 bgx_lmac_disable(bgx, --lmac);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001292 goto err_enable;
1293 }
1294 }
1295
1296 return 0;
1297
1298err_enable:
1299 bgx_vnic[bgx->bgx_id] = NULL;
1300err_release_regions:
1301 pci_release_regions(pdev);
1302err_disable_device:
1303 pci_disable_device(pdev);
1304 pci_set_drvdata(pdev, NULL);
1305 return err;
1306}
1307
1308static void bgx_remove(struct pci_dev *pdev)
1309{
1310 struct bgx *bgx = pci_get_drvdata(pdev);
1311 u8 lmac;
1312
1313 /* Disable all LMACs */
1314 for (lmac = 0; lmac < bgx->lmac_count; lmac++)
1315 bgx_lmac_disable(bgx, lmac);
1316
1317 bgx_vnic[bgx->bgx_id] = NULL;
1318 pci_release_regions(pdev);
1319 pci_disable_device(pdev);
1320 pci_set_drvdata(pdev, NULL);
1321}
1322
1323static struct pci_driver bgx_driver = {
1324 .name = DRV_NAME,
1325 .id_table = bgx_id_table,
1326 .probe = bgx_probe,
1327 .remove = bgx_remove,
1328};
1329
1330static int __init bgx_init_module(void)
1331{
1332 pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
1333
1334 return pci_register_driver(&bgx_driver);
1335}
1336
1337static void __exit bgx_cleanup_module(void)
1338{
1339 pci_unregister_driver(&bgx_driver);
1340}
1341
1342module_init(bgx_init_module);
1343module_exit(bgx_cleanup_module);