tenxpress.c 24.6 KB
Newer Older
1
/****************************************************************************
2 3
 * Driver for Solarflare Solarstorm network controllers and boards
 * Copyright 2007-2008 Solarflare Communications Inc.
4 5 6 7 8 9 10
 *
 * This program 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, incorporated herein by reference.
 */

#include <linux/delay.h>
H
Herbert Xu 已提交
11
#include <linux/rtnetlink.h>
12 13 14 15 16 17 18
#include <linux/seq_file.h>
#include "efx.h"
#include "mdio_10g.h"
#include "falcon.h"
#include "phy.h"
#include "falcon_hwdefs.h"
#include "boards.h"
19 20
#include "workarounds.h"
#include "selftest.h"
21

22 23 24 25
/* We expect these MMDs to be in the package.  SFT9001 also has a
 * clause 22 extension MMD, but since it doesn't have all the generic
 * MMD registers it is pointless to include it here.
 */
26 27
#define TENXPRESS_REQUIRED_DEVS (MDIO_MMDREG_DEVS_PMAPMD	| \
				 MDIO_MMDREG_DEVS_PCS		| \
B
Ben Hutchings 已提交
28 29
				 MDIO_MMDREG_DEVS_PHYXS		| \
				 MDIO_MMDREG_DEVS_AN)
30

31 32 33 34 35 36 37 38 39 40
#define SFX7101_LOOPBACKS ((1 << LOOPBACK_PHYXS) |	\
			   (1 << LOOPBACK_PCS) |	\
			   (1 << LOOPBACK_PMAPMD) |	\
			   (1 << LOOPBACK_NETWORK))

#define SFT9001_LOOPBACKS ((1 << LOOPBACK_GPHY) |	\
			   (1 << LOOPBACK_PHYXS) |	\
			   (1 << LOOPBACK_PCS) |	\
			   (1 << LOOPBACK_PMAPMD) |	\
			   (1 << LOOPBACK_NETWORK))
41

42 43 44 45 46
/* We complain if we fail to see the link partner as 10G capable this many
 * times in a row (must be > 1 as sampling the autoneg. registers is racy)
 */
#define MAX_BAD_LP_TRIES	(5)

47 48 49 50 51 52 53 54 55 56 57 58
/* LASI Control */
#define PMA_PMD_LASI_CTRL	36866
#define PMA_PMD_LASI_STATUS	36869
#define PMA_PMD_LS_ALARM_LBN	0
#define PMA_PMD_LS_ALARM_WIDTH	1
#define PMA_PMD_TX_ALARM_LBN	1
#define PMA_PMD_TX_ALARM_WIDTH	1
#define PMA_PMD_RX_ALARM_LBN	2
#define PMA_PMD_RX_ALARM_WIDTH	1
#define PMA_PMD_AN_ALARM_LBN	3
#define PMA_PMD_AN_ALARM_WIDTH	1

59
/* Extended control register */
60 61 62 63 64 65 66 67 68 69 70
#define PMA_PMD_XCONTROL_REG	49152
#define PMA_PMD_EXT_GMII_EN_LBN	1
#define PMA_PMD_EXT_GMII_EN_WIDTH 1
#define PMA_PMD_EXT_CLK_OUT_LBN	2
#define PMA_PMD_EXT_CLK_OUT_WIDTH 1
#define PMA_PMD_LNPGA_POWERDOWN_LBN 8	/* SFX7101 only */
#define PMA_PMD_LNPGA_POWERDOWN_WIDTH 1
#define PMA_PMD_EXT_CLK312_LBN	8	/* SFT9001 only */
#define PMA_PMD_EXT_CLK312_WIDTH 1
#define PMA_PMD_EXT_LPOWER_LBN  12
#define PMA_PMD_EXT_LPOWER_WIDTH 1
71 72
#define PMA_PMD_EXT_ROBUST_LBN	14
#define PMA_PMD_EXT_ROBUST_WIDTH 1
73 74
#define PMA_PMD_EXT_SSR_LBN	15
#define PMA_PMD_EXT_SSR_WIDTH	1
75 76

/* extended status register */
77
#define PMA_PMD_XSTATUS_REG	49153
78 79 80
#define PMA_PMD_XSTAT_FLP_LBN   (12)

/* LED control register */
81
#define PMA_PMD_LED_CTRL_REG	49159
82 83 84
#define PMA_PMA_LED_ACTIVITY_LBN	(3)

/* LED function override register */
85
#define PMA_PMD_LED_OVERR_REG	49161
86 87 88 89 90 91 92 93 94 95
/* Bit positions for different LEDs (there are more but not wired on SFE4001)*/
#define PMA_PMD_LED_LINK_LBN	(0)
#define PMA_PMD_LED_SPEED_LBN	(2)
#define PMA_PMD_LED_TX_LBN	(4)
#define PMA_PMD_LED_RX_LBN	(6)
/* Override settings */
#define	PMA_PMD_LED_AUTO	(0)	/* H/W control */
#define	PMA_PMD_LED_ON		(1)
#define	PMA_PMD_LED_OFF		(2)
#define PMA_PMD_LED_FLASH	(3)
B
Ben Hutchings 已提交
96
#define PMA_PMD_LED_MASK	3
97 98 99 100 101
/* All LEDs under hardware control */
#define PMA_PMD_LED_FULL_AUTO	(0)
/* Green and Amber under hardware control, Red off */
#define PMA_PMD_LED_DEFAULT	(PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN)

102 103 104 105 106 107 108 109 110 111
#define PMA_PMD_SPEED_ENABLE_REG 49192
#define PMA_PMD_100TX_ADV_LBN    1
#define PMA_PMD_100TX_ADV_WIDTH  1
#define PMA_PMD_1000T_ADV_LBN    2
#define PMA_PMD_1000T_ADV_WIDTH  1
#define PMA_PMD_10000T_ADV_LBN   3
#define PMA_PMD_10000T_ADV_WIDTH 1
#define PMA_PMD_SPEED_LBN        4
#define PMA_PMD_SPEED_WIDTH      4

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/* Cable diagnostics - SFT9001 only */
#define PMA_PMD_CDIAG_CTRL_REG  49213
#define CDIAG_CTRL_IMMED_LBN    15
#define CDIAG_CTRL_BRK_LINK_LBN 12
#define CDIAG_CTRL_IN_PROG_LBN  11
#define CDIAG_CTRL_LEN_UNIT_LBN 10
#define CDIAG_CTRL_LEN_METRES   1
#define PMA_PMD_CDIAG_RES_REG   49174
#define CDIAG_RES_A_LBN         12
#define CDIAG_RES_B_LBN         8
#define CDIAG_RES_C_LBN         4
#define CDIAG_RES_D_LBN         0
#define CDIAG_RES_WIDTH         4
#define CDIAG_RES_OPEN          2
#define CDIAG_RES_OK            1
#define CDIAG_RES_INVALID       0
/* Set of 4 registers for pairs A-D */
#define PMA_PMD_CDIAG_LEN_REG   49175

131 132 133 134 135 136 137
/* Serdes control registers - SFT9001 only */
#define PMA_PMD_CSERDES_CTRL_REG 64258
/* Set the 156.25 MHz output to 312.5 MHz to drive Falcon's XMAC */
#define PMA_PMD_CSERDES_DEFAULT	0x000f

/* Misc register defines - SFX7101 only */
#define PCS_CLOCK_CTRL_REG	55297
138 139
#define PLL312_RST_N_LBN 2

140
#define PCS_SOFT_RST2_REG	55302
141 142 143
#define SERDES_RST_N_LBN 13
#define XGXS_RST_N_LBN 12

144
#define	PCS_TEST_SELECT_REG	55303	/* PRM 10.5.8 */
145 146
#define	CLK312_EN_LBN 3

147
/* PHYXS registers */
148 149 150 151
#define PHYXS_XCONTROL_REG	49152
#define PHYXS_RESET_LBN		15
#define PHYXS_RESET_WIDTH	1

152 153 154 155
#define PHYXS_TEST1         (49162)
#define LOOPBACK_NEAR_LBN   (8)
#define LOOPBACK_NEAR_WIDTH (1)

156 157 158 159
#define PCS_10GBASET_STAT1       32
#define PCS_10GBASET_BLKLK_LBN   0
#define PCS_10GBASET_BLKLK_WIDTH 1

160
/* Boot status register */
161 162 163 164 165 166 167 168 169 170
#define PCS_BOOT_STATUS_REG		53248
#define PCS_BOOT_FATAL_ERROR_LBN	0
#define PCS_BOOT_PROGRESS_LBN		1
#define PCS_BOOT_PROGRESS_WIDTH		2
#define PCS_BOOT_PROGRESS_INIT		0
#define PCS_BOOT_PROGRESS_WAIT_MDIO	1
#define PCS_BOOT_PROGRESS_CHECKSUM	2
#define PCS_BOOT_PROGRESS_JUMP		3
#define PCS_BOOT_DOWNLOAD_WAIT_LBN	3
#define PCS_BOOT_CODE_STARTED_LBN	4
171

172 173 174 175 176 177 178 179 180 181 182 183 184
/* 100M/1G PHY registers */
#define GPHY_XCONTROL_REG	49152
#define GPHY_ISOLATE_LBN	10
#define GPHY_ISOLATE_WIDTH	1
#define GPHY_DUPLEX_LBN	  	8
#define GPHY_DUPLEX_WIDTH	1
#define GPHY_LOOPBACK_NEAR_LBN	14
#define GPHY_LOOPBACK_NEAR_WIDTH 1

#define C22EXT_STATUS_REG       49153
#define C22EXT_STATUS_LINK_LBN  2
#define C22EXT_STATUS_LINK_WIDTH 1

185 186 187 188 189 190 191
#define C22EXT_MSTSLV_CTRL			49161
#define C22EXT_MSTSLV_CTRL_ADV_1000_HD_LBN	8
#define C22EXT_MSTSLV_CTRL_ADV_1000_FD_LBN	9

#define C22EXT_MSTSLV_STATUS			49162
#define C22EXT_MSTSLV_STATUS_LP_1000_HD_LBN	10
#define C22EXT_MSTSLV_STATUS_LP_1000_FD_LBN	11
192

193 194 195 196 197
/* Time to wait between powering down the LNPGA and turning off the power
 * rails */
#define LNPGA_PDOWN_WAIT	(HZ / 5)

struct tenxpress_phy_data {
198
	enum efx_loopback_mode loopback_mode;
199
	enum efx_phy_mode phy_mode;
200 201 202
	int bad_lp_tries;
};

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
static ssize_t show_phy_short_reach(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
	int reg;

	reg = mdio_clause45_read(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
				 MDIO_PMAPMD_10GBT_TXPWR);
	return sprintf(buf, "%d\n",
		       !!(reg & (1 << MDIO_PMAPMD_10GBT_TXPWR_SHORT_LBN)));
}

static ssize_t set_phy_short_reach(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t count)
{
	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));

	rtnl_lock();
	mdio_clause45_set_flag(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
			       MDIO_PMAPMD_10GBT_TXPWR,
			       MDIO_PMAPMD_10GBT_TXPWR_SHORT_LBN,
			       count != 0 && *buf != '0');
	efx_reconfigure_port(efx);
	rtnl_unlock();

	return count;
}

static DEVICE_ATTR(phy_short_reach, 0644, show_phy_short_reach,
		   set_phy_short_reach);

235
int sft9001_wait_boot(struct efx_nic *efx)
236
{
237
	unsigned long timeout = jiffies + HZ + 1;
238 239
	int boot_stat;

240 241
	for (;;) {
		boot_stat = mdio_clause45_read(efx, efx->mii.phy_id,
242 243
					       MDIO_MMD_PCS,
					       PCS_BOOT_STATUS_REG);
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
		if (boot_stat >= 0) {
			EFX_LOG(efx, "PHY boot status = %#x\n", boot_stat);
			switch (boot_stat &
				((1 << PCS_BOOT_FATAL_ERROR_LBN) |
				 (3 << PCS_BOOT_PROGRESS_LBN) |
				 (1 << PCS_BOOT_DOWNLOAD_WAIT_LBN) |
				 (1 << PCS_BOOT_CODE_STARTED_LBN))) {
			case ((1 << PCS_BOOT_FATAL_ERROR_LBN) |
			      (PCS_BOOT_PROGRESS_CHECKSUM <<
			       PCS_BOOT_PROGRESS_LBN)):
			case ((1 << PCS_BOOT_FATAL_ERROR_LBN) |
			      (PCS_BOOT_PROGRESS_INIT <<
			       PCS_BOOT_PROGRESS_LBN) |
			      (1 << PCS_BOOT_DOWNLOAD_WAIT_LBN)):
				return -EINVAL;
			case ((PCS_BOOT_PROGRESS_WAIT_MDIO <<
			       PCS_BOOT_PROGRESS_LBN) |
			      (1 << PCS_BOOT_DOWNLOAD_WAIT_LBN)):
				return (efx->phy_mode & PHY_MODE_SPECIAL) ?
					0 : -EIO;
			case ((PCS_BOOT_PROGRESS_JUMP <<
			       PCS_BOOT_PROGRESS_LBN) |
			      (1 << PCS_BOOT_CODE_STARTED_LBN)):
			case ((PCS_BOOT_PROGRESS_JUMP <<
			       PCS_BOOT_PROGRESS_LBN) |
			      (1 << PCS_BOOT_DOWNLOAD_WAIT_LBN) |
			      (1 << PCS_BOOT_CODE_STARTED_LBN)):
				return (efx->phy_mode & PHY_MODE_SPECIAL) ?
					-EIO : 0;
			default:
				if (boot_stat & (1 << PCS_BOOT_FATAL_ERROR_LBN))
					return -EIO;
				break;
			}
		}

		if (time_after_eq(jiffies, timeout))
			return -ETIMEDOUT;

		msleep(50);
284 285 286 287 288
	}
}

static int tenxpress_init(struct efx_nic *efx)
{
289 290
	int phy_id = efx->mii.phy_id;
	int reg;
291

292 293 294 295 296 297 298 299 300 301 302
	if (efx->phy_type == PHY_TYPE_SFX7101) {
		/* Enable 312.5 MHz clock */
		mdio_clause45_write(efx, phy_id,
				    MDIO_MMD_PCS, PCS_TEST_SELECT_REG,
				    1 << CLK312_EN_LBN);
	} else {
		/* Enable 312.5 MHz clock and GMII */
		reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
					 PMA_PMD_XCONTROL_REG);
		reg |= ((1 << PMA_PMD_EXT_GMII_EN_LBN) |
			(1 << PMA_PMD_EXT_CLK_OUT_LBN) |
303 304 305
			(1 << PMA_PMD_EXT_CLK312_LBN) |
			(1 << PMA_PMD_EXT_ROBUST_LBN));

306 307 308 309 310 311
		mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
				    PMA_PMD_XCONTROL_REG, reg);
		mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_C22EXT,
				       GPHY_XCONTROL_REG, GPHY_ISOLATE_LBN,
				       false);
	}
312 313

	/* Set the LEDs up as: Green = Link, Amber = Link/Act, Red = Off */
314 315 316 317 318 319 320 321
	if (efx->phy_type == PHY_TYPE_SFX7101) {
		mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PMAPMD,
				       PMA_PMD_LED_CTRL_REG,
				       PMA_PMA_LED_ACTIVITY_LBN,
				       true);
		mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
				    PMA_PMD_LED_OVERR_REG, PMA_PMD_LED_DEFAULT);
	}
322

323
	return 0;
324 325 326 327 328 329 330 331
}

static int tenxpress_phy_init(struct efx_nic *efx)
{
	struct tenxpress_phy_data *phy_data;
	int rc = 0;

	phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
332 333
	if (!phy_data)
		return -ENOMEM;
334
	efx->phy_data = phy_data;
335
	phy_data->phy_mode = efx->phy_mode;
336

337 338 339 340 341 342 343 344 345 346 347 348
	if (!(efx->phy_mode & PHY_MODE_SPECIAL)) {
		if (efx->phy_type == PHY_TYPE_SFT9001A) {
			int reg;
			reg = mdio_clause45_read(efx, efx->mii.phy_id,
						 MDIO_MMD_PMAPMD,
						 PMA_PMD_XCONTROL_REG);
			reg |= (1 << PMA_PMD_EXT_SSR_LBN);
			mdio_clause45_write(efx, efx->mii.phy_id,
					    MDIO_MMD_PMAPMD,
					    PMA_PMD_XCONTROL_REG, reg);
			mdelay(200);
		}
349

350 351 352 353 354 355 356 357 358
		rc = mdio_clause45_wait_reset_mmds(efx,
						   TENXPRESS_REQUIRED_DEVS);
		if (rc < 0)
			goto fail;

		rc = mdio_clause45_check_mmds(efx, TENXPRESS_REQUIRED_DEVS, 0);
		if (rc < 0)
			goto fail;
	}
359 360 361 362

	rc = tenxpress_init(efx);
	if (rc < 0)
		goto fail;
363
	mdio_clause45_set_pause(efx);
364

365 366 367 368 369 370 371
	if (efx->phy_type == PHY_TYPE_SFT9001B) {
		rc = device_create_file(&efx->pci_dev->dev,
					&dev_attr_phy_short_reach);
		if (rc)
			goto fail;
	}

372 373
	schedule_timeout_uninterruptible(HZ / 5); /* 200ms */

374
	/* Let XGXS and SerDes out of reset */
375 376 377 378 379 380 381 382 383 384
	falcon_reset_xaui(efx);

	return 0;

 fail:
	kfree(efx->phy_data);
	efx->phy_data = NULL;
	return rc;
}

385 386 387
/* Perform a "special software reset" on the PHY. The caller is
 * responsible for saving and restoring the PHY hardware registers
 * properly, and masking/unmasking LASI */
388 389 390 391
static int tenxpress_special_reset(struct efx_nic *efx)
{
	int rc, reg;

392 393
	/* The XGMAC clock is driven from the SFC7101/SFT9001 312MHz clock, so
	 * a special software reset can glitch the XGMAC sufficiently for stats
394 395
	 * requests to fail. */
	efx_stats_disable(efx);
396 397 398

	/* Initiate reset */
	reg = mdio_clause45_read(efx, efx->mii.phy_id,
399
				 MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG);
400 401
	reg |= (1 << PMA_PMD_EXT_SSR_LBN);
	mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
402
			    PMA_PMD_XCONTROL_REG, reg);
403

404
	mdelay(200);
405 406 407 408 409

	/* Wait for the blocks to come out of reset */
	rc = mdio_clause45_wait_reset_mmds(efx,
					   TENXPRESS_REQUIRED_DEVS);
	if (rc < 0)
410
		goto out;
411 412 413 414

	/* Try and reconfigure the device */
	rc = tenxpress_init(efx);
	if (rc < 0)
415
		goto out;
416

417 418
	/* Wait for the XGXS state machine to churn */
	mdelay(10);
419 420
out:
	efx_stats_enable(efx);
421
	return rc;
422 423
}

424
static void sfx7101_check_bad_lp(struct efx_nic *efx, bool link_ok)
425 426
{
	struct tenxpress_phy_data *pd = efx->phy_data;
B
Ben Hutchings 已提交
427 428
	int phy_id = efx->mii.phy_id;
	bool bad_lp;
429 430
	int reg;

B
Ben Hutchings 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443
	if (link_ok) {
		bad_lp = false;
	} else {
		/* Check that AN has started but not completed. */
		reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
					 MDIO_AN_STATUS);
		if (!(reg & (1 << MDIO_AN_STATUS_LP_AN_CAP_LBN)))
			return; /* LP status is unknown */
		bad_lp = !(reg & (1 << MDIO_AN_STATUS_AN_DONE_LBN));
		if (bad_lp)
			pd->bad_lp_tries++;
	}

444
	/* Nothing to do if all is well and was previously so. */
B
Ben Hutchings 已提交
445
	if (!pd->bad_lp_tries)
446 447
		return;

B
Ben Hutchings 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	/* Use the RX (red) LED as an error indicator once we've seen AN
	 * failure several times in a row, and also log a message. */
	if (!bad_lp || pd->bad_lp_tries == MAX_BAD_LP_TRIES) {
		reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
					 PMA_PMD_LED_OVERR_REG);
		reg &= ~(PMA_PMD_LED_MASK << PMA_PMD_LED_RX_LBN);
		if (!bad_lp) {
			reg |= PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN;
		} else {
			reg |= PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN;
			EFX_ERR(efx, "appears to be plugged into a port"
				" that is not 10GBASE-T capable. The PHY"
				" supports 10GBASE-T ONLY, so no link can"
				" be established\n");
		}
		mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
				    PMA_PMD_LED_OVERR_REG, reg);
		pd->bad_lp_tries = bad_lp;
466 467 468
	}
}

469
static bool sfx7101_link_ok(struct efx_nic *efx)
470
{
471 472 473 474 475 476 477 478 479 480 481
	return mdio_clause45_links_ok(efx,
				      MDIO_MMDREG_DEVS_PMAPMD |
				      MDIO_MMDREG_DEVS_PCS |
				      MDIO_MMDREG_DEVS_PHYXS);
}

static bool sft9001_link_ok(struct efx_nic *efx, struct ethtool_cmd *ecmd)
{
	int phy_id = efx->mii.phy_id;
	u32 reg;

482
	if (efx_phy_mode_disabled(efx->phy_mode))
483
		return false;
484 485
	else if (efx->loopback_mode == LOOPBACK_GPHY)
		return true;
486
	else if (efx->loopback_mode)
B
Ben Hutchings 已提交
487 488 489
		return mdio_clause45_links_ok(efx,
					      MDIO_MMDREG_DEVS_PMAPMD |
					      MDIO_MMDREG_DEVS_PHYXS);
490 491 492 493 494 495 496 497 498 499 500 501 502

	/* We must use the same definition of link state as LASI,
	 * otherwise we can miss a link state transition
	 */
	if (ecmd->speed == 10000) {
		reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PCS,
					 PCS_10GBASET_STAT1);
		return reg & (1 << PCS_10GBASET_BLKLK_LBN);
	} else {
		reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_C22EXT,
					 C22EXT_STATUS_REG);
		return reg & (1 << C22EXT_STATUS_LINK_LBN);
	}
503 504
}

505
static void tenxpress_ext_loopback(struct efx_nic *efx)
506 507 508
{
	int phy_id = efx->mii.phy_id;

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
	mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PHYXS,
			       PHYXS_TEST1, LOOPBACK_NEAR_LBN,
			       efx->loopback_mode == LOOPBACK_PHYXS);
	if (efx->phy_type != PHY_TYPE_SFX7101)
		mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_C22EXT,
				       GPHY_XCONTROL_REG,
				       GPHY_LOOPBACK_NEAR_LBN,
				       efx->loopback_mode == LOOPBACK_GPHY);
}

static void tenxpress_low_power(struct efx_nic *efx)
{
	int phy_id = efx->mii.phy_id;

	if (efx->phy_type == PHY_TYPE_SFX7101)
		mdio_clause45_set_mmds_lpower(
			efx, !!(efx->phy_mode & PHY_MODE_LOW_POWER),
			TENXPRESS_REQUIRED_DEVS);
527
	else
528 529 530 531
		mdio_clause45_set_flag(
			efx, phy_id, MDIO_MMD_PMAPMD,
			PMA_PMD_XCONTROL_REG, PMA_PMD_EXT_LPOWER_LBN,
			!!(efx->phy_mode & PHY_MODE_LOW_POWER));
532 533
}

534 535
static void tenxpress_phy_reconfigure(struct efx_nic *efx)
{
536
	struct tenxpress_phy_data *phy_data = efx->phy_data;
537
	struct ethtool_cmd ecmd;
538
	bool phy_mode_change, loop_reset;
539

540
	if (efx->phy_mode & (PHY_MODE_OFF | PHY_MODE_SPECIAL)) {
541
		phy_data->phy_mode = efx->phy_mode;
542
		return;
543
	}
544

545 546 547 548 549 550 551
	tenxpress_low_power(efx);

	phy_mode_change = (efx->phy_mode == PHY_MODE_NORMAL &&
			   phy_data->phy_mode != PHY_MODE_NORMAL);
	loop_reset = (LOOPBACK_OUT_OF(phy_data, efx, efx->phy_op->loopbacks) ||
		      LOOPBACK_CHANGED(phy_data, efx, 1 << LOOPBACK_GPHY));

552
	if (loop_reset || phy_mode_change) {
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
		int rc;

		efx->phy_op->get_settings(efx, &ecmd);

		if (loop_reset || phy_mode_change) {
			tenxpress_special_reset(efx);

			/* Reset XAUI if we were in 10G, and are staying
			 * in 10G. If we're moving into and out of 10G
			 * then xaui will be reset anyway */
			if (EFX_IS10G(efx))
				falcon_reset_xaui(efx);
		}

		rc = efx->phy_op->set_settings(efx, &ecmd);
		WARN_ON(rc);
569 570 571 572
	}

	mdio_clause45_transmit_disable(efx);
	mdio_clause45_phy_reconfigure(efx);
573
	tenxpress_ext_loopback(efx);
574 575

	phy_data->loopback_mode = efx->loopback_mode;
576
	phy_data->phy_mode = efx->phy_mode;
577 578 579 580 581 582 583 584 585 586 587

	if (efx->phy_type == PHY_TYPE_SFX7101) {
		efx->link_speed = 10000;
		efx->link_fd = true;
		efx->link_up = sfx7101_link_ok(efx);
	} else {
		efx->phy_op->get_settings(efx, &ecmd);
		efx->link_speed = ecmd.speed;
		efx->link_fd = ecmd.duplex == DUPLEX_FULL;
		efx->link_up = sft9001_link_ok(efx, &ecmd);
	}
B
Ben Hutchings 已提交
588
	efx->link_fc = mdio_clause45_get_pause(efx);
589 590 591
}

/* Poll PHY for interrupt */
592
static void tenxpress_phy_poll(struct efx_nic *efx)
593 594
{
	struct tenxpress_phy_data *phy_data = efx->phy_data;
595
	bool change = false;
596

597
	if (efx->phy_type == PHY_TYPE_SFX7101) {
598
		bool link_ok = sfx7101_link_ok(efx);
599 600 601
		if (link_ok != efx->link_up) {
			change = true;
		} else {
602
			unsigned int link_fc = mdio_clause45_get_pause(efx);
603 604 605 606
			if (link_fc != efx->link_fc)
				change = true;
		}
		sfx7101_check_bad_lp(efx, link_ok);
607 608 609 610
	} else if (efx->loopback_mode) {
		bool link_ok = sft9001_link_ok(efx, NULL);
		if (link_ok != efx->link_up)
			change = true;
611
	} else {
612 613 614 615
		u32 status = mdio_clause45_read(efx, efx->mii.phy_id,
						MDIO_MMD_PMAPMD,
						PMA_PMD_LASI_STATUS);
		if (status & (1 << PMA_PMD_LS_ALARM_LBN))
616 617
			change = true;
	}
618

619
	if (change)
620
		falcon_sim_phy_event(efx);
621

622
	if (phy_data->phy_mode != PHY_MODE_NORMAL)
623
		return;
624 625 626 627 628 629
}

static void tenxpress_phy_fini(struct efx_nic *efx)
{
	int reg;

630
	if (efx->phy_type == PHY_TYPE_SFT9001B)
631 632
		device_remove_file(&efx->pci_dev->dev,
				   &dev_attr_phy_short_reach);
633 634

	if (efx->phy_type == PHY_TYPE_SFX7101) {
635 636 637 638 639 640 641 642 643 644
		/* Power down the LNPGA */
		reg = (1 << PMA_PMD_LNPGA_POWERDOWN_LBN);
		mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
				    PMA_PMD_XCONTROL_REG, reg);

		/* Waiting here ensures that the board fini, which can turn
		 * off the power to the PHY, won't get run until the LNPGA
		 * powerdown has been given long enough to complete. */
		schedule_timeout_uninterruptible(LNPGA_PDOWN_WAIT); /* 200 ms */
	}
645 646 647 648 649 650 651 652

	kfree(efx->phy_data);
	efx->phy_data = NULL;
}


/* Set the RX and TX LEDs and Link LED flashing. The other LEDs
 * (which probably aren't wired anyway) are left in AUTO mode */
653
void tenxpress_phy_blink(struct efx_nic *efx, bool blink)
654 655 656 657 658 659 660 661 662 663 664 665 666 667
{
	int reg;

	if (blink)
		reg = (PMA_PMD_LED_FLASH << PMA_PMD_LED_TX_LBN) |
			(PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN) |
			(PMA_PMD_LED_FLASH << PMA_PMD_LED_LINK_LBN);
	else
		reg = PMA_PMD_LED_DEFAULT;

	mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
			    PMA_PMD_LED_OVERR_REG, reg);
}

668
static const char *const sfx7101_test_names[] = {
669 670 671 672
	"bist"
};

static int
673
sfx7101_run_tests(struct efx_nic *efx, int *results, unsigned flags)
B
Ben Hutchings 已提交
674
{
675 676 677 678 679
	int rc;

	if (!(flags & ETH_TEST_FL_OFFLINE))
		return 0;

B
Ben Hutchings 已提交
680
	/* BIST is automatically run after a special software reset */
681 682 683
	rc = tenxpress_special_reset(efx);
	results[0] = rc ? -1 : 1;
	return rc;
B
Ben Hutchings 已提交
684 685
}

686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
static const char *const sft9001_test_names[] = {
	"bist",
	"cable.pairA.status",
	"cable.pairB.status",
	"cable.pairC.status",
	"cable.pairD.status",
	"cable.pairA.length",
	"cable.pairB.length",
	"cable.pairC.length",
	"cable.pairD.length",
};

static int sft9001_run_tests(struct efx_nic *efx, int *results, unsigned flags)
{
	struct ethtool_cmd ecmd;
	int phy_id = efx->mii.phy_id;
702
	int rc = 0, rc2, i, ctrl_reg, res_reg;
703

704 705
	if (flags & ETH_TEST_FL_OFFLINE)
		efx->phy_op->get_settings(efx, &ecmd);
706 707 708 709 710 711 712

	/* Initialise cable diagnostic results to unknown failure */
	for (i = 1; i < 9; ++i)
		results[i] = -1;

	/* Run cable diagnostics; wait up to 5 seconds for them to complete.
	 * A cable fault is not a self-test failure, but a timeout is. */
713 714 715 716 717 718 719
	ctrl_reg = ((1 << CDIAG_CTRL_IMMED_LBN) |
		    (CDIAG_CTRL_LEN_METRES << CDIAG_CTRL_LEN_UNIT_LBN));
	if (flags & ETH_TEST_FL_OFFLINE) {
		/* Break the link in order to run full diagnostics.  We
		 * must reset the PHY to resume normal service. */
		ctrl_reg |= (1 << CDIAG_CTRL_BRK_LINK_LBN);
	}
720
	mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
721
			    PMA_PMD_CDIAG_CTRL_REG, ctrl_reg);
722 723 724 725 726 727
	i = 0;
	while (mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
				  PMA_PMD_CDIAG_CTRL_REG) &
	       (1 << CDIAG_CTRL_IN_PROG_LBN)) {
		if (++i == 50) {
			rc = -ETIMEDOUT;
728
			goto out;
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
		}
		msleep(100);
	}
	res_reg = mdio_clause45_read(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
				     PMA_PMD_CDIAG_RES_REG);
	for (i = 0; i < 4; i++) {
		int pair_res =
			(res_reg >> (CDIAG_RES_A_LBN - i * CDIAG_RES_WIDTH))
			& ((1 << CDIAG_RES_WIDTH) - 1);
		int len_reg = mdio_clause45_read(efx, efx->mii.phy_id,
						 MDIO_MMD_PMAPMD,
						 PMA_PMD_CDIAG_LEN_REG + i);
		if (pair_res == CDIAG_RES_OK)
			results[1 + i] = 1;
		else if (pair_res == CDIAG_RES_INVALID)
			results[1 + i] = -1;
		else
			results[1 + i] = -pair_res;
		if (pair_res != CDIAG_RES_INVALID &&
		    pair_res != CDIAG_RES_OPEN &&
		    len_reg != 0xffff)
			results[5 + i] = len_reg;
	}

753 754 755 756 757 758 759 760 761 762 763 764
out:
	if (flags & ETH_TEST_FL_OFFLINE) {
		/* Reset, running the BIST and then resuming normal service. */
		rc2 = tenxpress_special_reset(efx);
		results[0] = rc2 ? -1 : 1;
		if (!rc)
			rc = rc2;

		rc2 = efx->phy_op->set_settings(efx, &ecmd);
		if (!rc)
			rc = rc2;
	}
765 766 767 768

	return rc;
}

769 770
static void
tenxpress_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
B
Ben Hutchings 已提交
771
{
772 773
	int phy_id = efx->mii.phy_id;
	u32 adv = 0, lpa = 0;
B
Ben Hutchings 已提交
774 775
	int reg;

776
	if (efx->phy_type != PHY_TYPE_SFX7101) {
777 778 779 780 781 782 783
		reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_C22EXT,
					 C22EXT_MSTSLV_CTRL);
		if (reg & (1 << C22EXT_MSTSLV_CTRL_ADV_1000_FD_LBN))
			adv |= ADVERTISED_1000baseT_Full;
		reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_C22EXT,
					 C22EXT_MSTSLV_STATUS);
		if (reg & (1 << C22EXT_MSTSLV_STATUS_LP_1000_HD_LBN))
784
			lpa |= ADVERTISED_1000baseT_Half;
785
		if (reg & (1 << C22EXT_MSTSLV_STATUS_LP_1000_FD_LBN))
786 787
			lpa |= ADVERTISED_1000baseT_Full;
	}
788 789 790 791 792 793
	reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
				 MDIO_AN_10GBT_CTRL);
	if (reg & (1 << MDIO_AN_10GBT_CTRL_ADV_10G_LBN))
		adv |= ADVERTISED_10000baseT_Full;
	reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
				 MDIO_AN_10GBT_STATUS);
B
Ben Hutchings 已提交
794 795 796
	if (reg & (1 << MDIO_AN_10GBT_STATUS_LP_10G_LBN))
		lpa |= ADVERTISED_10000baseT_Full;

797
	mdio_clause45_get_settings_ext(efx, ecmd, adv, lpa);
798

799 800 801
	if (efx->phy_type != PHY_TYPE_SFX7101)
		ecmd->supported |= (SUPPORTED_100baseT_Full |
				    SUPPORTED_1000baseT_Full);
802 803 804 805 806

	/* In loopback, the PHY automatically brings up the correct interface,
	 * but doesn't advertise the correct speed. So override it */
	if (efx->loopback_mode == LOOPBACK_GPHY)
		ecmd->speed = SPEED_1000;
807
	else if (LOOPBACK_MASK(efx) & efx->phy_op->loopbacks)
808
		ecmd->speed = SPEED_10000;
B
Ben Hutchings 已提交
809 810
}

811
static int tenxpress_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
812
{
813 814
	if (!ecmd->autoneg)
		return -EINVAL;
815

816
	return mdio_clause45_set_settings(efx, ecmd);
817 818
}

819
static void sfx7101_set_npage_adv(struct efx_nic *efx, u32 advertising)
820
{
821 822 823 824
	mdio_clause45_set_flag(efx, efx->mii.phy_id, MDIO_MMD_AN,
			       MDIO_AN_10GBT_CTRL,
			       MDIO_AN_10GBT_CTRL_ADV_10G_LBN,
			       advertising & ADVERTISED_10000baseT_Full);
825 826
}

827
static void sft9001_set_npage_adv(struct efx_nic *efx, u32 advertising)
828
{
829 830 831 832 833 834 835 836 837 838
	int phy_id = efx->mii.phy_id;

	mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_C22EXT,
			       C22EXT_MSTSLV_CTRL,
			       C22EXT_MSTSLV_CTRL_ADV_1000_FD_LBN,
			       advertising & ADVERTISED_1000baseT_Full);
	mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_AN,
			       MDIO_AN_10GBT_CTRL,
			       MDIO_AN_10GBT_CTRL_ADV_10G_LBN,
			       advertising & ADVERTISED_10000baseT_Full);
839 840 841
}

struct efx_phy_operations falcon_sfx7101_phy_ops = {
842
	.macs		  = EFX_XMAC,
843 844
	.init             = tenxpress_phy_init,
	.reconfigure      = tenxpress_phy_reconfigure,
845
	.poll             = tenxpress_phy_poll,
846
	.fini             = tenxpress_phy_fini,
847
	.clear_interrupt  = efx_port_dummy_op_void,
848 849 850
	.get_settings	  = tenxpress_get_settings,
	.set_settings	  = tenxpress_set_settings,
	.set_npage_adv    = sfx7101_set_npage_adv,
851 852 853
	.num_tests	  = ARRAY_SIZE(sfx7101_test_names),
	.test_names	  = sfx7101_test_names,
	.run_tests	  = sfx7101_run_tests,
854
	.mmds             = TENXPRESS_REQUIRED_DEVS,
855 856 857 858 859 860 861 862 863 864
	.loopbacks        = SFX7101_LOOPBACKS,
};

struct efx_phy_operations falcon_sft9001_phy_ops = {
	.macs		  = EFX_GMAC | EFX_XMAC,
	.init             = tenxpress_phy_init,
	.reconfigure      = tenxpress_phy_reconfigure,
	.poll             = tenxpress_phy_poll,
	.fini             = tenxpress_phy_fini,
	.clear_interrupt  = efx_port_dummy_op_void,
865 866 867
	.get_settings	  = tenxpress_get_settings,
	.set_settings	  = tenxpress_set_settings,
	.set_npage_adv    = sft9001_set_npage_adv,
868 869 870
	.num_tests	  = ARRAY_SIZE(sft9001_test_names),
	.test_names	  = sft9001_test_names,
	.run_tests	  = sft9001_run_tests,
871 872
	.mmds             = TENXPRESS_REQUIRED_DEVS,
	.loopbacks        = SFT9001_LOOPBACKS,
873
};