aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/octeon/octeon-bgx-port.c
blob: 80b08dcdea81474e234686d6a903033672c0c259 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/**********************************************************************
 * Author: Cavium, Inc.
 *
 * Contact: support@cavium.com
 * This file is part of the OCTEON SDK
 *
 * Copyright (c) 2014 Cavium, Inc.
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, Version 2, as
 * published by the Free Software Foundation.
 *
 * This file is distributed in the hope that it will be useful, but
 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
 * NONINFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this file; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 * or visit http://www.gnu.org/licenses/.
 *
 * This file may also be available under a different license from Cavium.
 * Contact Cavium, Inc. for more information
 **********************************************************************/
#include <linux/platform_device.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/of_platform.h>
#include <linux/of_address.h>
#include <linux/of_mdio.h>
#include <linux/of_net.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/if_vlan.h>

#include <asm/octeon/cvmx-helper.h>
#include <asm/octeon/cvmx-helper-util.h>
#include <asm/octeon/cvmx-helper-cfg.h>
#include <asm/octeon/cvmx-bgxx-defs.h>
#include <asm/octeon/cvmx-helper-bgx.h>
#include <asm/octeon/cvmx-pko3.h>

#include "octeon-bgx.h"

struct bgx_port_priv {
	int numa_node;
	int bgx_interface;
	int index; /* Port index on BGX block*/
	int ipd_port;
	int xiface;
	const u8 *mac_addr;
	struct phy_device *phydev;
	struct device_node *phy_np;
	struct net_device *netdev;
	spinlock_t lock;
	unsigned int last_duplex;
	unsigned int last_link;
	unsigned int last_speed;
	struct delayed_work dwork;
};

static struct workqueue_struct *check_state_wq;
static DEFINE_MUTEX(check_state_wq_mutex);

static struct bgx_port_priv *bgx_port_netdev2priv(struct net_device *netdev)
{
	struct bgx_port_netdev_priv *nd_priv = netdev_priv(netdev);
	return nd_priv->bgx_priv;
}

void bgx_port_set_netdev(struct device *dev, struct net_device *netdev)
{
	struct bgx_port_netdev_priv *nd_priv = netdev_priv(netdev);
	struct bgx_port_priv *priv = dev_get_drvdata(dev);
	nd_priv->bgx_priv = priv;
	priv->netdev = netdev;
}
EXPORT_SYMBOL(bgx_port_set_netdev);

int bgx_port_ethtool_get_settings(struct net_device	*netdev,
				  struct ethtool_cmd	*cmd)
{
	cvmx_helper_link_info_t link_info;
	struct bgx_port_priv *p = bgx_port_netdev2priv(netdev);

	if (p->phydev)
		return phy_ethtool_gset(p->phydev, cmd);

	link_info = cvmx_helper_link_get(p->ipd_port);
	cmd->speed = link_info.s.link_up ? link_info.s.speed : 0;
	cmd->duplex = link_info.s.full_duplex ? DUPLEX_FULL : DUPLEX_HALF;

	return 0;
}
EXPORT_SYMBOL(bgx_port_ethtool_get_settings);

int bgx_port_ethtool_set_settings(struct net_device	*netdev,
				  struct ethtool_cmd	*cmd)
{
	struct bgx_port_priv *p = bgx_port_netdev2priv(netdev);

	if (!capable(CAP_NET_ADMIN))
		return -EPERM;

	if (p->phydev)
		return phy_ethtool_sset(p->phydev, cmd);

	return -EOPNOTSUPP;
}
EXPORT_SYMBOL(bgx_port_ethtool_set_settings);

int bgx_port_ethtool_nway_reset(struct net_device *netdev)
{
	struct bgx_port_priv *p = bgx_port_netdev2priv(netdev);

	if (!capable(CAP_NET_ADMIN))
		return -EPERM;

	if (p->phydev)
		return phy_start_aneg(p->phydev);

	return -EOPNOTSUPP;
}
EXPORT_SYMBOL(bgx_port_ethtool_nway_reset);

const u8 *bgx_port_get_mac(struct net_device *netdev)
{
	struct bgx_port_priv *priv = bgx_port_netdev2priv(netdev);
	return priv->mac_addr;
}
EXPORT_SYMBOL(bgx_port_get_mac);

static void bgx_port_write_cam(int numa_node, int interface, int index, int cam, const u8 *mac)
{
	int i;
	union cvmx_bgxx_cmr_rx_adrx_cam adr_cam;
	u64 m = 0;
	if (mac)
		for (i = 0; i < 6; i++)
			m |= (((u64)mac[i]) << ((5 - i) * 8));
	adr_cam.u64 = m;
	adr_cam.s.en = mac ? 1 : 0;
	adr_cam.s.id = index;
	cvmx_write_csr_node(numa_node, CVMX_BGXX_CMR_RX_ADRX_CAM(index * 8 + cam, interface), adr_cam.u64);
}

/* Set MAC address for the net_device that is attached. */
void bgx_port_set_rx_filtering(struct net_device *netdev)
{
	union cvmx_bgxx_cmrx_rx_adr_ctl adr_ctl;
	struct bgx_port_priv *priv = bgx_port_netdev2priv(netdev);
	int available_cam_entries, current_cam_entry;
	struct netdev_hw_addr *ha;

	available_cam_entries = 8;
	adr_ctl.u64 = 0;
	adr_ctl.s.bcst_accept = 1; /* Accept all Broadcast*/

	if ((netdev->flags & IFF_PROMISC) || netdev->uc.count > 7) {
		adr_ctl.s.cam_accept = 0; /* Reject CAM match */
		available_cam_entries = 0;
	} else {
		/* One CAM entry for the primary address, leaves seven
		 * for the secondary addresses.
		 */
		adr_ctl.s.cam_accept = 1; /* Accept CAM match */
		available_cam_entries = 7 - netdev->uc.count;
	}

	if (netdev->flags & IFF_PROMISC) {
		adr_ctl.s.mcst_mode = 1; /* Accept all Multicast */
	} else {
		if (netdev->flags & IFF_MULTICAST) {
			if ((netdev->flags & IFF_ALLMULTI) || netdev_mc_count(netdev) > available_cam_entries)
				adr_ctl.s.mcst_mode = 1; /* Accept all Multicast */
			else
				adr_ctl.s.mcst_mode = 2; /* Accept all Multicast via CAM */
		}
	}
	current_cam_entry = 0;
	if (adr_ctl.s.cam_accept) {
		bgx_port_write_cam(priv->numa_node, priv->bgx_interface, priv->index,
				   current_cam_entry, netdev->dev_addr);
		current_cam_entry++;
		netdev_for_each_uc_addr(ha, netdev) {
			bgx_port_write_cam(priv->numa_node, priv->bgx_interface, priv->index,
					   current_cam_entry, ha->addr);
			current_cam_entry++;
		}
	}
	if (adr_ctl.s.mcst_mode == 2) {/* Accept all Multicast via CAM */
		netdev_for_each_mc_addr(ha, netdev) {
			bgx_port_write_cam(priv->numa_node, priv->bgx_interface, priv->index,
					   current_cam_entry, ha->addr);
			current_cam_entry++;
		}
	}
	while (current_cam_entry < 8) {
		bgx_port_write_cam(priv->numa_node, priv->bgx_interface, priv->index,
				   current_cam_entry, NULL);
		current_cam_entry++;
	}
	cvmx_write_csr_node(priv->numa_node,
			    CVMX_BGXX_CMRX_RX_ADR_CTL(priv->index, priv->bgx_interface),
			    adr_ctl.u64);
}
EXPORT_SYMBOL(bgx_port_set_rx_filtering);

static void bgx_port_adjust_link(struct net_device *netdev)
{
	struct bgx_port_priv *p = bgx_port_netdev2priv(netdev);
	int link_changed = 0;
	unsigned int link, speed, duplex;
	unsigned long flags;

	spin_lock_irqsave(&p->lock, flags);

	if (!p->phydev->link && p->last_link)
		link_changed = -1;

	if (p->phydev->link
	    && (p->last_duplex != p->phydev->duplex
		|| p->last_link != p->phydev->link
		|| p->last_speed != p->phydev->speed)) {
		link_changed = 1;
	}

	link = p->last_link = p->phydev->link;
	speed = p->last_speed = p->phydev->speed;
	duplex = p->last_duplex = p->phydev->duplex;

	spin_unlock_irqrestore(&p->lock, flags);

	if (link_changed != 0) {
		cvmx_helper_link_info_t link_info;
		if (link_changed > 0) {
			pr_info("%s: Link is up - %d/%s\n", netdev->name,
				p->phydev->speed,
				DUPLEX_FULL == p->phydev->duplex ?
				"Full" : "Half");
		} else {
			pr_info("%s: Link is down\n", netdev->name);
		}
		link_info.u64 = 0;
		link_info.s.link_up = link ? 1 : 0;
		link_info.s.full_duplex = duplex ? 1 : 0;
		link_info.s.speed = speed;
		if (!link) {
			netif_carrier_off(netdev);
			mdelay(50); /* Let TX drain.  FIXME really check that it is drained. */
		}
		cvmx_helper_link_set(p->ipd_port, link_info);
		if (link)
			netif_carrier_on(netdev);
	}
}

static void bgx_port_check_state(struct work_struct *work)
{
	struct bgx_port_priv		*priv;
	cvmx_helper_link_info_t		link_info;

	priv = container_of(work, struct bgx_port_priv, dwork.work);

	link_info = cvmx_helper_link_get(priv->ipd_port);
	if (priv->last_link != link_info.s.link_up) {
		priv->last_link = link_info.s.link_up;
		if (link_info.s.link_up)
			pr_info("%s: Link is up - %d/%s\n",
				    priv->netdev->name, link_info.s.speed,
				    link_info.s.full_duplex ? "Full" : "Half");
		else
			pr_info("%s: Link is down\n", priv->netdev->name);
	}

	queue_delayed_work(check_state_wq, &priv->dwork, HZ);
}

int bgx_port_enable(struct net_device *netdev)
{
	bool dont_use_phy;
	union cvmx_bgxx_cmrx_config cfg;
	struct bgx_port_priv *priv = bgx_port_netdev2priv(netdev);

	cvmx_helper_set_1000x_mode(priv->xiface, priv->index, false);
	cvmx_helper_set_mac_phy_mode(priv->xiface, priv->index, false);

	cfg.u64 = cvmx_read_csr_node(priv->numa_node, CVMX_BGXX_CMRX_CONFIG(priv->index, priv->bgx_interface));
	if (cfg.s.lmac_type == 0) {
		/* 1G */
		union cvmx_bgxx_gmp_gmi_txx_append tx_append;
		union cvmx_bgxx_gmp_gmi_txx_min_pkt min_pkt;

		tx_append.u64 = cvmx_read_csr_node(priv->numa_node,
						   CVMX_BGXX_GMP_GMI_TXX_APPEND(priv->index, priv->bgx_interface));
		tx_append.s.fcs = 1;
		tx_append.s.pad = 1;

		cvmx_write_csr_node(priv->numa_node,
				    CVMX_BGXX_GMP_GMI_TXX_APPEND(priv->index, priv->bgx_interface),
				    tx_append.u64);

		min_pkt.u64 = 0;
		/* packets are padded (without FCS) to MIN_SIZE + 1 in SGMII */
		min_pkt.s.min_size = 60 - 1;
		cvmx_write_csr_node(priv->numa_node,
				    CVMX_BGXX_GMP_GMI_TXX_MIN_PKT(priv->index, priv->bgx_interface),
				    min_pkt.u64);
	} else {
		/* 10G or higher */
		union cvmx_bgxx_smux_tx_append tx_append;
		union cvmx_bgxx_smux_tx_min_pkt min_pkt;

		tx_append.u64 = cvmx_read_csr_node(priv->numa_node,
						   CVMX_BGXX_SMUX_TX_APPEND(priv->index, priv->bgx_interface));
		tx_append.s.fcs_d = 1;
		tx_append.s.pad = 1;

		cvmx_write_csr_node(priv->numa_node,
				    CVMX_BGXX_SMUX_TX_APPEND(priv->index, priv->bgx_interface),
				    tx_append.u64);

		min_pkt.u64 = 0;
		/* packets are padded(with FCS) to MIN_SIZE  in non-SGMII */
		min_pkt.s.min_size = 60 + 4;
		cvmx_write_csr_node(priv->numa_node,
				    CVMX_BGXX_SMUX_TX_MIN_PKT(priv->index, priv->bgx_interface),
				    min_pkt.u64);

	}

	switch (cvmx_helper_interface_get_mode(priv->xiface)) {
	case CVMX_HELPER_INTERFACE_MODE_XLAUI:
	case CVMX_HELPER_INTERFACE_MODE_XFI:
	case CVMX_HELPER_INTERFACE_MODE_10G_KR:
	case CVMX_HELPER_INTERFACE_MODE_40G_KR4:
		dont_use_phy = true;
		break;
	default:
		dont_use_phy = false;
		break;
	}

	if (priv->phy_np == NULL || dont_use_phy) {
		cvmx_helper_link_autoconf(priv->ipd_port);
		netif_carrier_on(netdev);

		mutex_lock(&check_state_wq_mutex);
		if (!check_state_wq) {
			check_state_wq =
				alloc_workqueue("check_state_wq", WQ_UNBOUND |
						WQ_MEM_RECLAIM, 1);
		}
		mutex_unlock(&check_state_wq_mutex);
		if (!check_state_wq)
			return -ENOMEM;

		INIT_DELAYED_WORK(&priv->dwork, bgx_port_check_state);
		queue_delayed_work(check_state_wq, &priv->dwork, 0);
		pr_info("%s: Link is not ready\n", netdev->name);

		return 0;
	} else {
		priv->phydev = of_phy_connect(netdev, priv->phy_np,
					      bgx_port_adjust_link, 0,
					      PHY_INTERFACE_MODE_SGMII);
		if (!priv->phydev)
			return -ENODEV;
	}

	netif_carrier_off(netdev);

	if (priv->phydev)
		phy_start_aneg(priv->phydev);

	return 0;
}
EXPORT_SYMBOL(bgx_port_enable);

int bgx_port_disable(struct net_device *netdev)
{
	struct bgx_port_priv *priv = bgx_port_netdev2priv(netdev);
	cvmx_helper_link_info_t link_info;

	if (priv->phydev)
		phy_disconnect(priv->phydev);
	priv->phydev = NULL;

	netif_carrier_off(netdev);
	link_info.u64 = 0;
	priv->last_link = 0;
	cvmx_helper_link_set(priv->ipd_port, link_info);

	if (priv->phy_np == NULL)
		cancel_delayed_work_sync(&priv->dwork);

	return 0;
}
EXPORT_SYMBOL(bgx_port_disable);

static int get_max_78xx_pass1_x_mtu(int xiface, int index)
{
	int	fifo_size;
	int	max_mtu = 1500;

	/* Due to errata PKO-20096, the mtu must be limited.
	 * PKO-20096 causes PKO to lock up when calculating the L4
	 * checksum for large packets. How large the packets can be
	 * depends on the amount of pko fifo assigned to the port.
	 *
	 *   FIFO size                Max frame size
	 *	2.5 KB				1920
	 *	5.0 KB				4480
	 *     10.0 KB				9600
	 *
	 * The maximum mtu is set to the largest frame size minus the
	 * l2 header.
	 */
	fifo_size = cvmx_pko3_port_fifo_size(xiface, index);

	switch (fifo_size) {
	case 2560:
		max_mtu = 1920 - ETH_HLEN - ETH_FCS_LEN - (2 * VLAN_HLEN);
		break;

	case 5120:
		max_mtu = 4480 - ETH_HLEN - ETH_FCS_LEN - (2 * VLAN_HLEN);
		break;

	case 10240:
		max_mtu = 9600 - ETH_HLEN - ETH_FCS_LEN - (2 * VLAN_HLEN);
		break;

	default:
		break;
	}

	return max_mtu;
}

int bgx_port_change_mtu(struct net_device *netdev, int new_mtu)
{
	union cvmx_bgxx_cmrx_config cfg;
	struct bgx_port_priv *priv = bgx_port_netdev2priv(netdev);
	int max_frame;

	if (new_mtu < 60 || new_mtu > 65392)
		return -EINVAL;

	if (OCTEON_IS_MODEL(OCTEON_CN78XX_PASS1_X)) {
		int	max_mtu;

		max_mtu = get_max_78xx_pass1_x_mtu(priv->xiface, priv->index);
		if (new_mtu > max_mtu) {
			new_mtu = max_mtu;
			netdev_warn(netdev, "Maximum MTU supported is %d",
				    max_mtu);
		}
	}

	netdev->mtu = new_mtu;

	max_frame = round_up(new_mtu + ETH_HLEN + ETH_FCS_LEN, 8);

	cfg.u64 = cvmx_read_csr_node(priv->numa_node, CVMX_BGXX_CMRX_CONFIG(priv->index, priv->bgx_interface));
	if (cfg.s.lmac_type == 0)
		cvmx_write_csr_node(priv->numa_node,		/* 1G */
				    CVMX_BGXX_GMP_GMI_RXX_JABBER(priv->index, priv->bgx_interface),
				    max_frame);
	else
		cvmx_write_csr_node(priv->numa_node,		/* 10G or higher */
				    CVMX_BGXX_SMUX_RX_JABBER(priv->index, priv->bgx_interface),
				    max_frame);


	return 0;
}
EXPORT_SYMBOL(bgx_port_change_mtu);

static int bgx_port_probe(struct platform_device *pdev)
{
	u64 addr;
	const u8 *mac;
	const __be32 *reg;
	u32 index;
	int r;
	struct bgx_port_priv *priv;
	int numa_node;

	reg = of_get_property(pdev->dev.parent->of_node, "reg", NULL);
	addr = of_translate_address(pdev->dev.parent->of_node, reg);
	mac = of_get_mac_address(pdev->dev.of_node);

	numa_node = (addr >> 36) & 0x7;

	r = of_property_read_u32(pdev->dev.of_node, "reg", &index);
	if (r)
		return -ENODEV;
	priv = kzalloc_node(sizeof(*priv), GFP_KERNEL, numa_node);
	if (!priv)
		return -ENOMEM;

	spin_lock_init(&priv->lock);
	priv->numa_node = numa_node;
	priv->bgx_interface = (addr >> 24) & 0xf;
	priv->index = index;
	priv->xiface = cvmx_helper_node_interface_to_xiface(numa_node, priv->bgx_interface);
	priv->ipd_port = cvmx_helper_get_ipd_port(priv->xiface, index);
	if (mac)
		priv->mac_addr = mac;

	priv->phy_np = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);

	if (priv->phy_np == NULL)
		cvmx_helper_set_port_phy_present(priv->xiface, priv->index, false);
	else
		cvmx_helper_set_port_phy_present(priv->xiface, priv->index, true);

	r = dev_set_drvdata(&pdev->dev, priv);

	if (priv->phy_np)
		__cvmx_helper_bgx_port_init(priv->ipd_port, 1);
	else
		__cvmx_helper_bgx_port_init(priv->ipd_port, 0);

	if (r)
		goto err;

	dev_info(&pdev->dev, "Probed\n");
	return 0;
err:
	kfree(priv);
	return r;
}

static int bgx_port_remove(struct platform_device *pdev)
{
	struct bgx_port_priv *priv = dev_get_drvdata(&pdev->dev);
	kfree(priv);
	return 0;
}

static void bgx_port_shutdown(struct platform_device *pdev)
{
	return;
}

static struct of_device_id bgx_port_match[] = {
	{
		.compatible = "cavium,octeon-7890-bgx-port",
	},
	{},
};
MODULE_DEVICE_TABLE(of, bgx_port_match);

static struct platform_driver bgx_port_driver = {
	.probe		= bgx_port_probe,
	.remove		= bgx_port_remove,
	.shutdown       = bgx_port_shutdown,
	.driver		= {
		.owner	= THIS_MODULE,
		.name	= KBUILD_MODNAME,
		.of_match_table = bgx_port_match,
	},
};

static int __init bgx_port_driver_init(void)
{
	int r;

	bgx_nexus_load();
	r =  platform_driver_register(&bgx_port_driver);
	return r;
}
module_init(bgx_port_driver_init);

static void __exit bgx_port_driver_exit(void)
{
	platform_driver_unregister(&bgx_port_driver);
	destroy_workqueue(check_state_wq);
}
module_exit(bgx_port_driver_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Cavium Networks <support@caviumnetworks.com>");
MODULE_DESCRIPTION("Cavium Networks BGX Ethernet MAC driver.");