netxen_nic_niu.c 23.8 KB
Newer Older
A
Amit S. Kale 已提交
1 2 3
/*
 * Copyright (C) 2003 - 2006 NetXen, Inc.
 * All rights reserved.
4
 *
A
Amit S. Kale 已提交
5 6 7 8
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
9
 *
A
Amit S. Kale 已提交
10 11 12 13
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14
 *
A
Amit S. Kale 已提交
15 16 17 18
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA  02111-1307, USA.
19
 *
A
Amit S. Kale 已提交
20 21
 * The full GNU General Public License is included in this distribution
 * in the file called LICENSE.
22
 *
A
Amit S. Kale 已提交
23 24 25 26 27 28 29 30 31 32 33 34
 * Contact Information:
 *    info@netxen.com
 * NetXen,
 * 3965 Freedom Circle, Fourth floor,
 * Santa Clara, CA 95054
 *
 *
 * Provides access to the Network Interface Unit h/w block.
 *
 */

#include "netxen_nic.h"
35 36 37 38 39 40 41 42

#define NETXEN_GB_MAC_SOFT_RESET	0x80000000
#define NETXEN_GB_MAC_RESET_PROT_BLK   0x000F0000
#define NETXEN_GB_MAC_ENABLE_TX_RX     0x00000005
#define NETXEN_GB_MAC_PAUSED_FRMS      0x00000020

static long phy_lock_timeout = 100000000;

A
Adrian Bunk 已提交
43
static int phy_lock(struct netxen_adapter *adapter)
44 45 46 47 48
{
	int i;
	int done = 0, timeout = 0;

	while (!done) {
49 50
		done = netxen_nic_reg_read(adapter,
				NETXEN_PCIE_REG(PCIE_SEM3_LOCK));
51 52 53 54 55 56 57 58 59 60 61 62 63 64
		if (done == 1)
			break;
		if (timeout >= phy_lock_timeout) {
			return -1;
		}
		timeout++;
		if (!in_atomic())
			schedule();
		else {
			for (i = 0; i < 20; i++)
				cpu_relax();
		}
	}

65 66
	netxen_crb_writelit_adapter(adapter,
			NETXEN_PHY_LOCK_ID, PHY_LOCK_DRIVER);
67 68 69
	return 0;
}

A
Adrian Bunk 已提交
70
static int phy_unlock(struct netxen_adapter *adapter)
71
{
72
	adapter->pci_read_immediate(adapter, NETXEN_PCIE_REG(PCIE_SEM3_UNLOCK));
73

74 75
	return 0;
}
A
Amit S. Kale 已提交
76

77
/*
A
Amit S. Kale 已提交
78 79 80 81
 * netxen_niu_gbe_phy_read - read a register from the GbE PHY via
 * mii management interface.
 *
 * Note: The MII management interface goes through port 0.
82
 *	Individual phys are addressed as follows:
A
Amit S. Kale 已提交
83 84 85 86
 * @param phy  [15:8]  phy id
 * @param reg  [7:0]   register number
 *
 * @returns  0 on success
87
 *	  -1 on error
A
Amit S. Kale 已提交
88 89
 *
 */
90
int netxen_niu_gbe_phy_read(struct netxen_adapter *adapter, long reg,
91
				__u32 * readval)
A
Amit S. Kale 已提交
92 93 94 95
{
	long timeout = 0;
	long result = 0;
	long restore = 0;
96
	long phy = adapter->physical_port;
A
Al Viro 已提交
97 98 99 100
	__u32 address;
	__u32 command;
	__u32 status;
	__u32 mac_cfg0;
A
Amit S. Kale 已提交
101

102
	if (phy_lock(adapter) != 0) {
103 104 105 106 107 108 109 110
		return -1;
	}

	/*
	 * MII mgmt all goes through port 0 MAC interface,
	 * so it cannot be in reset
	 */

111
	if (adapter->hw_read_wx(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(0),
A
Amit S. Kale 已提交
112 113 114
				  &mac_cfg0, 4))
		return -EIO;
	if (netxen_gb_get_soft_reset(mac_cfg0)) {
A
Al Viro 已提交
115
		__u32 temp;
A
Amit S. Kale 已提交
116 117 118 119 120
		temp = 0;
		netxen_gb_tx_reset_pb(temp);
		netxen_gb_rx_reset_pb(temp);
		netxen_gb_tx_reset_mac(temp);
		netxen_gb_rx_reset_mac(temp);
121
		if (adapter->hw_write_wx(adapter,
A
Amit S. Kale 已提交
122 123 124 125 126 127 128 129 130
					   NETXEN_NIU_GB_MAC_CONFIG_0(0),
					   &temp, 4))
			return -EIO;
		restore = 1;
	}

	address = 0;
	netxen_gb_mii_mgmt_reg_addr(address, reg);
	netxen_gb_mii_mgmt_phy_addr(address, phy);
131
	if (adapter->hw_write_wx(adapter, NETXEN_NIU_GB_MII_MGMT_ADDR(0),
A
Amit S. Kale 已提交
132 133 134
				   &address, 4))
		return -EIO;
	command = 0;		/* turn off any prior activity */
135
	if (adapter->hw_write_wx(adapter, NETXEN_NIU_GB_MII_MGMT_COMMAND(0),
A
Amit S. Kale 已提交
136 137 138 139
				   &command, 4))
		return -EIO;
	/* send read command */
	netxen_gb_mii_mgmt_set_read_cycle(command);
140
	if (adapter->hw_write_wx(adapter, NETXEN_NIU_GB_MII_MGMT_COMMAND(0),
A
Amit S. Kale 已提交
141 142 143 144 145
				   &command, 4))
		return -EIO;

	status = 0;
	do {
146
		if (adapter->hw_read_wx(adapter,
A
Amit S. Kale 已提交
147 148 149 150 151 152 153 154 155
					  NETXEN_NIU_GB_MII_MGMT_INDICATE(0),
					  &status, 4))
			return -EIO;
		timeout++;
	} while ((netxen_get_gb_mii_mgmt_busy(status)
		  || netxen_get_gb_mii_mgmt_notvalid(status))
		 && (timeout++ < NETXEN_NIU_PHY_WAITMAX));

	if (timeout < NETXEN_NIU_PHY_WAITMAX) {
156
		if (adapter->hw_read_wx(adapter,
A
Amit S. Kale 已提交
157 158 159 160 161 162 163 164
					  NETXEN_NIU_GB_MII_MGMT_STATUS(0),
					  readval, 4))
			return -EIO;
		result = 0;
	} else
		result = -1;

	if (restore)
165
		if (adapter->hw_write_wx(adapter,
A
Amit S. Kale 已提交
166 167 168
					   NETXEN_NIU_GB_MAC_CONFIG_0(0),
					   &mac_cfg0, 4))
			return -EIO;
169
	phy_unlock(adapter);
A
Amit S. Kale 已提交
170 171 172
	return result;
}

173
/*
A
Amit S. Kale 已提交
174 175 176 177
 * netxen_niu_gbe_phy_write - write a register to the GbE PHY via
 * mii management interface.
 *
 * Note: The MII management interface goes through port 0.
178
 *	Individual phys are addressed as follows:
A
Amit S. Kale 已提交
179 180 181 182
 * @param phy      [15:8]  phy id
 * @param reg      [7:0]   register number
 *
 * @returns  0 on success
183
 *	  -1 on error
A
Amit S. Kale 已提交
184 185
 *
 */
186
int netxen_niu_gbe_phy_write(struct netxen_adapter *adapter, long reg,
187
				__u32 val)
A
Amit S. Kale 已提交
188 189 190 191
{
	long timeout = 0;
	long result = 0;
	long restore = 0;
192
	long phy = adapter->physical_port;
A
Al Viro 已提交
193 194 195 196
	__u32 address;
	__u32 command;
	__u32 status;
	__u32 mac_cfg0;
A
Amit S. Kale 已提交
197

198 199 200 201 202
	/*
	 * MII mgmt all goes through port 0 MAC interface, so it
	 * cannot be in reset
	 */

203
	if (adapter->hw_read_wx(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(0),
A
Amit S. Kale 已提交
204 205 206
				  &mac_cfg0, 4))
		return -EIO;
	if (netxen_gb_get_soft_reset(mac_cfg0)) {
A
Al Viro 已提交
207
		__u32 temp;
A
Amit S. Kale 已提交
208 209 210 211 212 213
		temp = 0;
		netxen_gb_tx_reset_pb(temp);
		netxen_gb_rx_reset_pb(temp);
		netxen_gb_tx_reset_mac(temp);
		netxen_gb_rx_reset_mac(temp);

214
		if (adapter->hw_write_wx(adapter,
A
Amit S. Kale 已提交
215 216 217 218 219 220 221
					   NETXEN_NIU_GB_MAC_CONFIG_0(0),
					   &temp, 4))
			return -EIO;
		restore = 1;
	}

	command = 0;		/* turn off any prior activity */
222
	if (adapter->hw_write_wx(adapter, NETXEN_NIU_GB_MII_MGMT_COMMAND(0),
A
Amit S. Kale 已提交
223 224 225 226 227 228
				   &command, 4))
		return -EIO;

	address = 0;
	netxen_gb_mii_mgmt_reg_addr(address, reg);
	netxen_gb_mii_mgmt_phy_addr(address, phy);
229
	if (adapter->hw_write_wx(adapter, NETXEN_NIU_GB_MII_MGMT_ADDR(0),
A
Amit S. Kale 已提交
230 231 232
				   &address, 4))
		return -EIO;

233
	if (adapter->hw_write_wx(adapter, NETXEN_NIU_GB_MII_MGMT_CTRL(0),
A
Amit S. Kale 已提交
234 235 236 237 238
				   &val, 4))
		return -EIO;

	status = 0;
	do {
239
		if (adapter->hw_read_wx(adapter,
A
Amit S. Kale 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253
					  NETXEN_NIU_GB_MII_MGMT_INDICATE(0),
					  &status, 4))
			return -EIO;
		timeout++;
	} while ((netxen_get_gb_mii_mgmt_busy(status))
		 && (timeout++ < NETXEN_NIU_PHY_WAITMAX));

	if (timeout < NETXEN_NIU_PHY_WAITMAX)
		result = 0;
	else
		result = -EIO;

	/* restore the state of port 0 MAC in case we tampered with it */
	if (restore)
254
		if (adapter->hw_write_wx(adapter,
A
Amit S. Kale 已提交
255 256 257 258 259 260 261
					   NETXEN_NIU_GB_MAC_CONFIG_0(0),
					   &mac_cfg0, 4))
			return -EIO;

	return result;
}

262
int netxen_niu_xgbe_enable_phy_interrupts(struct netxen_adapter *adapter)
A
Amit S. Kale 已提交
263 264 265 266 267
{
	netxen_crb_writelit_adapter(adapter, NETXEN_NIU_INT_MASK, 0x3f);
	return 0;
}

268
int netxen_niu_gbe_enable_phy_interrupts(struct netxen_adapter *adapter)
A
Amit S. Kale 已提交
269 270
{
	int result = 0;
A
Al Viro 已提交
271
	__u32 enable = 0;
A
Amit S. Kale 已提交
272 273 274 275 276
	netxen_set_phy_int_link_status_changed(enable);
	netxen_set_phy_int_autoneg_completed(enable);
	netxen_set_phy_int_speed_changed(enable);

	if (0 !=
277
	    netxen_niu_gbe_phy_write(adapter,
A
Amit S. Kale 已提交
278 279 280 281 282 283 284
				     NETXEN_NIU_GB_MII_MGMT_ADDR_INT_ENABLE,
				     enable))
		result = -EIO;

	return result;
}

285
int netxen_niu_xgbe_disable_phy_interrupts(struct netxen_adapter *adapter)
A
Amit S. Kale 已提交
286 287 288 289 290
{
	netxen_crb_writelit_adapter(adapter, NETXEN_NIU_INT_MASK, 0x7f);
	return 0;
}

291
int netxen_niu_gbe_disable_phy_interrupts(struct netxen_adapter *adapter)
A
Amit S. Kale 已提交
292 293 294
{
	int result = 0;
	if (0 !=
295
	    netxen_niu_gbe_phy_write(adapter,
A
Amit S. Kale 已提交
296 297 298 299 300 301
				     NETXEN_NIU_GB_MII_MGMT_ADDR_INT_ENABLE, 0))
		result = -EIO;

	return result;
}

A
Adrian Bunk 已提交
302
#if 0
303
int netxen_niu_xgbe_clear_phy_interrupts(struct netxen_adapter *adapter)
A
Amit S. Kale 已提交
304 305 306 307
{
	netxen_crb_writelit_adapter(adapter, NETXEN_NIU_ACTIVE_INT, -1);
	return 0;
}
A
Adrian Bunk 已提交
308
#endif  /*  0  */
A
Amit S. Kale 已提交
309

A
Adrian Bunk 已提交
310
static int netxen_niu_gbe_clear_phy_interrupts(struct netxen_adapter *adapter)
A
Amit S. Kale 已提交
311 312 313
{
	int result = 0;
	if (0 !=
314
	    netxen_niu_gbe_phy_write(adapter,
A
Amit S. Kale 已提交
315 316 317 318 319 320 321
				     NETXEN_NIU_GB_MII_MGMT_ADDR_INT_STATUS,
				     -EIO))
		result = -EIO;

	return result;
}

322
/*
A
Amit S. Kale 已提交
323 324 325
 * netxen_niu_gbe_set_mii_mode- Set 10/100 Mbit Mode for GbE MAC
 *
 */
A
Adrian Bunk 已提交
326 327
static void netxen_niu_gbe_set_mii_mode(struct netxen_adapter *adapter,
					int port, long enable)
A
Amit S. Kale 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
{
	netxen_crb_writelit_adapter(adapter, NETXEN_NIU_MODE, 0x2);
	netxen_crb_writelit_adapter(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port),
				    0x80000000);
	netxen_crb_writelit_adapter(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port),
				    0x0000f0025);
	netxen_crb_writelit_adapter(adapter, NETXEN_NIU_GB_MAC_CONFIG_1(port),
				    0xf1ff);
	netxen_crb_writelit_adapter(adapter,
				    NETXEN_NIU_GB0_GMII_MODE + (port << 3), 0);
	netxen_crb_writelit_adapter(adapter,
				    NETXEN_NIU_GB0_MII_MODE + (port << 3), 1);
	netxen_crb_writelit_adapter(adapter,
				    (NETXEN_NIU_GB0_HALF_DUPLEX + port * 4), 0);
	netxen_crb_writelit_adapter(adapter,
				    NETXEN_NIU_GB_MII_MGMT_CONFIG(port), 0x7);

	if (enable) {
346 347 348
		/*
		 * Do NOT enable flow control until a suitable solution for
		 *  shutting down pause frames is found.
A
Amit S. Kale 已提交
349 350 351 352 353 354
		 */
		netxen_crb_writelit_adapter(adapter,
					    NETXEN_NIU_GB_MAC_CONFIG_0(port),
					    0x5);
	}

355
	if (netxen_niu_gbe_enable_phy_interrupts(adapter))
A
Amit S. Kale 已提交
356
		printk(KERN_ERR PFX "ERROR enabling PHY interrupts\n");
357
	if (netxen_niu_gbe_clear_phy_interrupts(adapter))
A
Amit S. Kale 已提交
358 359 360
		printk(KERN_ERR PFX "ERROR clearing PHY interrupts\n");
}

361
/*
A
Amit S. Kale 已提交
362 363
 * netxen_niu_gbe_set_gmii_mode- Set GbE Mode for GbE MAC
 */
A
Adrian Bunk 已提交
364 365
static void netxen_niu_gbe_set_gmii_mode(struct netxen_adapter *adapter,
					 int port, long enable)
A
Amit S. Kale 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
{
	netxen_crb_writelit_adapter(adapter, NETXEN_NIU_MODE, 0x2);
	netxen_crb_writelit_adapter(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port),
				    0x80000000);
	netxen_crb_writelit_adapter(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port),
				    0x0000f0025);
	netxen_crb_writelit_adapter(adapter, NETXEN_NIU_GB_MAC_CONFIG_1(port),
				    0xf2ff);
	netxen_crb_writelit_adapter(adapter,
				    NETXEN_NIU_GB0_MII_MODE + (port << 3), 0);
	netxen_crb_writelit_adapter(adapter,
				    NETXEN_NIU_GB0_GMII_MODE + (port << 3), 1);
	netxen_crb_writelit_adapter(adapter,
				    (NETXEN_NIU_GB0_HALF_DUPLEX + port * 4), 0);
	netxen_crb_writelit_adapter(adapter,
				    NETXEN_NIU_GB_MII_MGMT_CONFIG(port), 0x7);

	if (enable) {
384 385 386
		/*
		 * Do NOT enable flow control until a suitable solution for
		 *  shutting down pause frames is found.
A
Amit S. Kale 已提交
387 388 389 390 391 392
		 */
		netxen_crb_writelit_adapter(adapter,
					    NETXEN_NIU_GB_MAC_CONFIG_0(port),
					    0x5);
	}

393
	if (netxen_niu_gbe_enable_phy_interrupts(adapter))
A
Amit S. Kale 已提交
394
		printk(KERN_ERR PFX "ERROR enabling PHY interrupts\n");
395
	if (netxen_niu_gbe_clear_phy_interrupts(adapter))
A
Amit S. Kale 已提交
396 397 398 399 400 401
		printk(KERN_ERR PFX "ERROR clearing PHY interrupts\n");
}

int netxen_niu_gbe_init_port(struct netxen_adapter *adapter, int port)
{
	int result = 0;
A
Al Viro 已提交
402
	__u32 status;
403 404 405 406

	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
		return 0;

407
	if (adapter->disable_phy_interrupts)
408
		adapter->disable_phy_interrupts(adapter);
A
Amit S. Kale 已提交
409 410
	mdelay(2);

411 412
	if (0 == netxen_niu_gbe_phy_read(adapter,
			NETXEN_NIU_GB_MII_MGMT_ADDR_PHY_STATUS, &status)) {
A
Amit S. Kale 已提交
413 414 415 416 417 418 419 420 421 422 423
		if (netxen_get_phy_link(status)) {
			if (netxen_get_phy_speed(status) == 2) {
				netxen_niu_gbe_set_gmii_mode(adapter, port, 1);
			} else if ((netxen_get_phy_speed(status) == 1)
				   || (netxen_get_phy_speed(status) == 0)) {
				netxen_niu_gbe_set_mii_mode(adapter, port, 1);
			} else {
				result = -1;
			}

		} else {
424 425 426 427 428 429
			/*
			 * We don't have link. Cable  must be unconnected.
			 * Enable phy interrupts so we take action when
			 * plugged in.
			 */

A
Amit S. Kale 已提交
430 431
			netxen_crb_writelit_adapter(adapter,
						    NETXEN_NIU_GB_MAC_CONFIG_0
432 433
						    (port),
						    NETXEN_GB_MAC_SOFT_RESET);
A
Amit S. Kale 已提交
434 435
			netxen_crb_writelit_adapter(adapter,
						    NETXEN_NIU_GB_MAC_CONFIG_0
436 437 438 439 440
						    (port),
						    NETXEN_GB_MAC_RESET_PROT_BLK
						    | NETXEN_GB_MAC_ENABLE_TX_RX
						    |
						    NETXEN_GB_MAC_PAUSED_FRMS);
441
			if (netxen_niu_gbe_clear_phy_interrupts(adapter))
A
Amit S. Kale 已提交
442 443
				printk(KERN_ERR PFX
				       "ERROR clearing PHY interrupts\n");
444
			if (netxen_niu_gbe_enable_phy_interrupts(adapter))
A
Amit S. Kale 已提交
445 446
				printk(KERN_ERR PFX
				       "ERROR enabling PHY interrupts\n");
447
			if (netxen_niu_gbe_clear_phy_interrupts(adapter))
A
Amit S. Kale 已提交
448 449 450 451 452 453 454 455 456 457
				printk(KERN_ERR PFX
				       "ERROR clearing PHY interrupts\n");
			result = -1;
		}
	} else {
		result = -EIO;
	}
	return result;
}

458 459
int netxen_niu_xg_init_port(struct netxen_adapter *adapter, int port)
{
460 461 462 463 464 465
	if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
		netxen_crb_writelit_adapter(adapter,
			NETXEN_NIU_XGE_CONFIG_1+(0x10000*port), 0x1447);
		netxen_crb_writelit_adapter(adapter,
			NETXEN_NIU_XGE_CONFIG_0+(0x10000*port), 0x5);
	}
466

467
	return 0;
468 469
}

A
Adrian Bunk 已提交
470
#if 0
471
/*
A
Amit S. Kale 已提交
472 473
 * netxen_niu_gbe_handle_phy_interrupt - Handles GbE PHY interrupts
 * @param enable 0 means don't enable the port
474
 *		 1 means enable (or re-enable) the port
A
Amit S. Kale 已提交
475 476 477 478 479
 */
int netxen_niu_gbe_handle_phy_interrupt(struct netxen_adapter *adapter,
					int port, long enable)
{
	int result = 0;
A
Al Viro 已提交
480
	__u32 int_src;
A
Amit S. Kale 已提交
481 482 483 484

	printk(KERN_INFO PFX "NETXEN: Handling PHY interrupt on port %d"
	       " (device enable = %d)\n", (int)port, (int)enable);

485 486 487 488
	/*
	 * The read of the PHY INT status will clear the pending
	 * interrupt status
	 */
489
	if (netxen_niu_gbe_phy_read(adapter,
A
Amit S. Kale 已提交
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
				    NETXEN_NIU_GB_MII_MGMT_ADDR_INT_STATUS,
				    &int_src) != 0)
		result = -EINVAL;
	else {
		printk(KERN_INFO PFX "PHY Interrupt source = 0x%x \n", int_src);
		if (netxen_get_phy_int_jabber(int_src))
			printk(KERN_INFO PFX "jabber Interrupt ");
		if (netxen_get_phy_int_polarity_changed(int_src))
			printk(KERN_INFO PFX "polarity changed ");
		if (netxen_get_phy_int_energy_detect(int_src))
			printk(KERN_INFO PFX "energy detect \n");
		if (netxen_get_phy_int_downshift(int_src))
			printk(KERN_INFO PFX "downshift \n");
		if (netxen_get_phy_int_mdi_xover_changed(int_src))
			printk(KERN_INFO PFX "mdi_xover_changed ");
		if (netxen_get_phy_int_fifo_over_underflow(int_src))
			printk(KERN_INFO PFX "fifo_over_underflow ");
		if (netxen_get_phy_int_false_carrier(int_src))
			printk(KERN_INFO PFX "false_carrier ");
		if (netxen_get_phy_int_symbol_error(int_src))
			printk(KERN_INFO PFX "symbol_error ");
		if (netxen_get_phy_int_autoneg_completed(int_src))
			printk(KERN_INFO PFX "autoneg_completed ");
		if (netxen_get_phy_int_page_received(int_src))
			printk(KERN_INFO PFX "page_received ");
		if (netxen_get_phy_int_duplex_changed(int_src))
			printk(KERN_INFO PFX "duplex_changed ");
		if (netxen_get_phy_int_autoneg_error(int_src))
			printk(KERN_INFO PFX "autoneg_error ");
		if ((netxen_get_phy_int_speed_changed(int_src))
		    || (netxen_get_phy_int_link_status_changed(int_src))) {
A
Al Viro 已提交
521
			__u32 status;
A
Amit S. Kale 已提交
522 523 524 525

			printk(KERN_INFO PFX
			       "speed_changed or link status changed");
			if (netxen_niu_gbe_phy_read
526
			    (adapter,
A
Amit S. Kale 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
			     NETXEN_NIU_GB_MII_MGMT_ADDR_PHY_STATUS,
			     &status) == 0) {
				if (netxen_get_phy_speed(status) == 2) {
					printk
					    (KERN_INFO PFX "Link speed changed"
					     " to 1000 Mbps\n");
					netxen_niu_gbe_set_gmii_mode(adapter,
								     port,
								     enable);
				} else if (netxen_get_phy_speed(status) == 1) {
					printk
					    (KERN_INFO PFX "Link speed changed"
					     " to 100 Mbps\n");
					netxen_niu_gbe_set_mii_mode(adapter,
								    port,
								    enable);
				} else if (netxen_get_phy_speed(status) == 0) {
					printk
					    (KERN_INFO PFX "Link speed changed"
					     " to 10 Mbps\n");
					netxen_niu_gbe_set_mii_mode(adapter,
								    port,
								    enable);
				} else {
551 552
					printk(KERN_ERR PFX "ERROR reading "
					       "PHY status. Invalid speed.\n");
A
Amit S. Kale 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565
					result = -1;
				}
			} else {
				printk(KERN_ERR PFX
				       "ERROR reading PHY status.\n");
				result = -1;
			}

		}
		printk(KERN_INFO "\n");
	}
	return result;
}
A
Adrian Bunk 已提交
566
#endif  /*  0  */
A
Amit S. Kale 已提交
567 568 569 570 571

/*
 * Return the current station MAC address.
 * Note that the passed-in value must already be in network byte order.
 */
A
Adrian Bunk 已提交
572 573
static int netxen_niu_macaddr_get(struct netxen_adapter *adapter,
				  netxen_ethernet_macaddr_t * addr)
A
Amit S. Kale 已提交
574
{
A
Al Viro 已提交
575 576
	u32 stationhigh;
	u32 stationlow;
577
	int phy = adapter->physical_port;
A
Al Viro 已提交
578
	u8 val[8];
A
Amit S. Kale 已提交
579 580 581 582 583 584

	if (addr == NULL)
		return -EINVAL;
	if ((phy < 0) || (phy > 3))
		return -EINVAL;

585
	if (adapter->hw_read_wx(adapter, NETXEN_NIU_GB_STATION_ADDR_0(phy),
A
Amit S. Kale 已提交
586 587
				  &stationhigh, 4))
		return -EIO;
588
	if (adapter->hw_read_wx(adapter, NETXEN_NIU_GB_STATION_ADDR_1(phy),
A
Amit S. Kale 已提交
589 590
				  &stationlow, 4))
		return -EIO;
A
Al Viro 已提交
591 592
	((__le32 *)val)[1] = cpu_to_le32(stationhigh);
	((__le32 *)val)[0] = cpu_to_le32(stationlow);
A
Amit S. Kale 已提交
593

A
Al Viro 已提交
594
	memcpy(addr, val + 2, 6);
A
Amit S. Kale 已提交
595 596 597 598 599 600 601 602

	return 0;
}

/*
 * Set the station MAC address.
 * Note that the passed-in value must already be in network byte order.
 */
603
int netxen_niu_macaddr_set(struct netxen_adapter *adapter,
A
Amit S. Kale 已提交
604 605
			   netxen_ethernet_macaddr_t addr)
{
A
Al Viro 已提交
606 607
	u8 temp[4];
	u32 val;
608
	int phy = adapter->physical_port;
609
	unsigned char mac_addr[6];
610
	int i;
611
	DECLARE_MAC_BUF(mac);
612

613 614 615
	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
		return 0;

616
	for (i = 0; i < 10; i++) {
A
Al Viro 已提交
617 618 619
		temp[0] = temp[1] = 0;
		memcpy(temp + 2, addr, 2);
		val = le32_to_cpu(*(__le32 *)temp);
620 621
		if (adapter->hw_write_wx(adapter,
				NETXEN_NIU_GB_STATION_ADDR_1(phy), &val, 4))
622
			return -EIO;
A
Amit S. Kale 已提交
623

A
Al Viro 已提交
624 625
		memcpy(temp, ((u8 *) addr) + 2, sizeof(__le32));
		val = le32_to_cpu(*(__le32 *)temp);
626 627
		if (adapter->hw_write_wx(adapter,
				NETXEN_NIU_GB_STATION_ADDR_0(phy), &val, 4))
628
			return -2;
A
Amit S. Kale 已提交
629

630
		netxen_niu_macaddr_get(adapter,
631
				       (netxen_ethernet_macaddr_t *) mac_addr);
632
		if (memcmp(mac_addr, addr, 6) == 0)
633 634
			break;
	}
A
Amit S. Kale 已提交
635

636 637
	if (i == 10) {
		printk(KERN_ERR "%s: cannot set Mac addr for %s\n",
638
		       netxen_nic_driver_name, adapter->netdev->name);
639 640 641 642
		printk(KERN_ERR "MAC address set: %s.\n",
		       print_mac(mac, addr));
		printk(KERN_ERR "MAC address get: %s.\n",
		       print_mac(mac, mac_addr));
643
	}
A
Amit S. Kale 已提交
644 645 646
	return 0;
}

A
Adrian Bunk 已提交
647
#if 0
A
Amit S. Kale 已提交
648 649 650 651
/* Enable a GbE interface */
int netxen_niu_enable_gbe_port(struct netxen_adapter *adapter,
			       int port, netxen_niu_gbe_ifmode_t mode)
{
A
Al Viro 已提交
652 653 654
	__u32 mac_cfg0;
	__u32 mac_cfg1;
	__u32 mii_cfg;
A
Amit S. Kale 已提交
655 656 657 658 659 660

	if ((port < 0) || (port > NETXEN_NIU_MAX_GBE_PORTS))
		return -EINVAL;

	mac_cfg0 = 0;
	netxen_gb_soft_reset(mac_cfg0);
661
	if (adapter->hw_write_wx(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port),
A
Amit S. Kale 已提交
662 663 664 665 666 667 668 669 670 671 672
				   &mac_cfg0, 4))
		return -EIO;
	mac_cfg0 = 0;
	netxen_gb_enable_tx(mac_cfg0);
	netxen_gb_enable_rx(mac_cfg0);
	netxen_gb_unset_rx_flowctl(mac_cfg0);
	netxen_gb_tx_reset_pb(mac_cfg0);
	netxen_gb_rx_reset_pb(mac_cfg0);
	netxen_gb_tx_reset_mac(mac_cfg0);
	netxen_gb_rx_reset_mac(mac_cfg0);

673
	if (adapter->hw_write_wx(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port),
A
Amit S. Kale 已提交
674 675 676 677 678 679 680 681 682 683 684 685
				   &mac_cfg0, 4))
		return -EIO;
	mac_cfg1 = 0;
	netxen_gb_set_preamblelen(mac_cfg1, 0xf);
	netxen_gb_set_duplex(mac_cfg1);
	netxen_gb_set_crc_enable(mac_cfg1);
	netxen_gb_set_padshort(mac_cfg1);
	netxen_gb_set_checklength(mac_cfg1);
	netxen_gb_set_hugeframes(mac_cfg1);

	if (mode == NETXEN_NIU_10_100_MB) {
		netxen_gb_set_intfmode(mac_cfg1, 1);
686
		if (adapter->hw_write_wx(adapter,
A
Amit S. Kale 已提交
687 688 689 690 691 692 693 694 695 696 697 698
					   NETXEN_NIU_GB_MAC_CONFIG_1(port),
					   &mac_cfg1, 4))
			return -EIO;

		/* set mii mode */
		netxen_crb_writelit_adapter(adapter, NETXEN_NIU_GB0_GMII_MODE +
					    (port << 3), 0);
		netxen_crb_writelit_adapter(adapter, NETXEN_NIU_GB0_MII_MODE +
					    (port << 3), 1);

	} else if (mode == NETXEN_NIU_1000_MB) {
		netxen_gb_set_intfmode(mac_cfg1, 2);
699
		if (adapter->hw_write_wx(adapter,
A
Amit S. Kale 已提交
700 701 702 703 704 705 706 707 708 709 710
					   NETXEN_NIU_GB_MAC_CONFIG_1(port),
					   &mac_cfg1, 4))
			return -EIO;
		/* set gmii mode */
		netxen_crb_writelit_adapter(adapter, NETXEN_NIU_GB0_MII_MODE +
					    (port << 3), 0);
		netxen_crb_writelit_adapter(adapter, NETXEN_NIU_GB0_GMII_MODE +
					    (port << 3), 1);
	}
	mii_cfg = 0;
	netxen_gb_set_mii_mgmt_clockselect(mii_cfg, 7);
711
	if (adapter->hw_write_wx(adapter, NETXEN_NIU_GB_MII_MGMT_CONFIG(port),
A
Amit S. Kale 已提交
712 713 714 715 716 717 718 719
				   &mii_cfg, 4))
		return -EIO;
	mac_cfg0 = 0;
	netxen_gb_enable_tx(mac_cfg0);
	netxen_gb_enable_rx(mac_cfg0);
	netxen_gb_unset_rx_flowctl(mac_cfg0);
	netxen_gb_unset_tx_flowctl(mac_cfg0);

720
	if (adapter->hw_write_wx(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port),
A
Amit S. Kale 已提交
721 722 723 724
				   &mac_cfg0, 4))
		return -EIO;
	return 0;
}
A
Adrian Bunk 已提交
725
#endif  /*  0  */
A
Amit S. Kale 已提交
726 727

/* Disable a GbE interface */
728
int netxen_niu_disable_gbe_port(struct netxen_adapter *adapter)
A
Amit S. Kale 已提交
729
{
A
Al Viro 已提交
730
	__u32 mac_cfg0;
731
	u32 port = adapter->physical_port;
A
Amit S. Kale 已提交
732

733 734 735
	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
		return 0;

736
	if (port > NETXEN_NIU_MAX_GBE_PORTS)
737
		return -EINVAL;
A
Amit S. Kale 已提交
738 739
	mac_cfg0 = 0;
	netxen_gb_soft_reset(mac_cfg0);
740
	if (adapter->hw_write_wx(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port),
A
Amit S. Kale 已提交
741 742 743 744 745 746
				   &mac_cfg0, 4))
		return -EIO;
	return 0;
}

/* Disable an XG interface */
747
int netxen_niu_disable_xg_port(struct netxen_adapter *adapter)
A
Amit S. Kale 已提交
748
{
A
Al Viro 已提交
749
	__u32 mac_cfg;
750
	u32 port = adapter->physical_port;
A
Amit S. Kale 已提交
751

752 753 754
	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
		return 0;

755
	if (port > NETXEN_NIU_MAX_XG_PORTS)
756
		return -EINVAL;
757

A
Amit S. Kale 已提交
758
	mac_cfg = 0;
759
	if (adapter->hw_write_wx(adapter,
760
		NETXEN_NIU_XGE_CONFIG_0 + (0x10000 * port), &mac_cfg, 4))
A
Amit S. Kale 已提交
761 762 763 764 765
		return -EIO;
	return 0;
}

/* Set promiscuous mode for a GbE interface */
766
int netxen_niu_set_promiscuous_mode(struct netxen_adapter *adapter,
A
Amit S. Kale 已提交
767 768
				    netxen_niu_prom_mode_t mode)
{
A
Al Viro 已提交
769
	__u32 reg;
770
	u32 port = adapter->physical_port;
A
Amit S. Kale 已提交
771

772
	if (port > NETXEN_NIU_MAX_GBE_PORTS)
A
Amit S. Kale 已提交
773 774 775
		return -EINVAL;

	/* save previous contents */
776
	if (adapter->hw_read_wx(adapter, NETXEN_NIU_GB_DROP_WRONGADDR,
A
Amit S. Kale 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
				  &reg, 4))
		return -EIO;
	if (mode == NETXEN_NIU_PROMISC_MODE) {
		switch (port) {
		case 0:
			netxen_clear_gb_drop_gb0(reg);
			break;
		case 1:
			netxen_clear_gb_drop_gb1(reg);
			break;
		case 2:
			netxen_clear_gb_drop_gb2(reg);
			break;
		case 3:
			netxen_clear_gb_drop_gb3(reg);
			break;
		default:
			return -EIO;
		}
	} else {
		switch (port) {
		case 0:
			netxen_set_gb_drop_gb0(reg);
			break;
		case 1:
			netxen_set_gb_drop_gb1(reg);
			break;
		case 2:
			netxen_set_gb_drop_gb2(reg);
			break;
		case 3:
			netxen_set_gb_drop_gb3(reg);
			break;
		default:
			return -EIO;
		}
	}
814
	if (adapter->hw_write_wx(adapter, NETXEN_NIU_GB_DROP_WRONGADDR,
A
Amit S. Kale 已提交
815 816 817 818 819 820 821 822 823
				   &reg, 4))
		return -EIO;
	return 0;
}

/*
 * Set the MAC address for an XG port
 * Note that the passed-in value must already be in network byte order.
 */
824
int netxen_niu_xg_macaddr_set(struct netxen_adapter *adapter,
A
Amit S. Kale 已提交
825 826
			      netxen_ethernet_macaddr_t addr)
{
827
	int phy = adapter->physical_port;
A
Al Viro 已提交
828 829
	u8 temp[4];
	u32 val;
A
Amit S. Kale 已提交
830

831 832 833
	if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
		return 0;

834 835 836
	if ((phy < 0) || (phy > NETXEN_NIU_MAX_XG_PORTS))
		return -EIO;

A
Al Viro 已提交
837
	temp[0] = temp[1] = 0;
838 839 840 841
	switch (phy) {
	case 0:
	    memcpy(temp + 2, addr, 2);
	    val = le32_to_cpu(*(__le32 *)temp);
842
	    if (adapter->hw_write_wx(adapter, NETXEN_NIU_XGE_STATION_ADDR_0_1,
843
				&val, 4))
A
Amit S. Kale 已提交
844 845
		return -EIO;

846 847
	    memcpy(&temp, ((u8 *) addr) + 2, sizeof(__le32));
	    val = le32_to_cpu(*(__le32 *)temp);
848
	    if (adapter->hw_write_wx(adapter, NETXEN_NIU_XGE_STATION_ADDR_0_HI,
849 850 851 852 853 854 855
				&val, 4))
		return -EIO;
	    break;

	case 1:
	    memcpy(temp + 2, addr, 2);
	    val = le32_to_cpu(*(__le32 *)temp);
856
	    if (adapter->hw_write_wx(adapter, NETXEN_NIU_XG1_STATION_ADDR_0_1,
857 858 859 860 861
				&val, 4))
		return -EIO;

	    memcpy(&temp, ((u8 *) addr) + 2, sizeof(__le32));
	    val = le32_to_cpu(*(__le32 *)temp);
862
	    if (adapter->hw_write_wx(adapter, NETXEN_NIU_XG1_STATION_ADDR_0_HI,
863
				&val, 4))
A
Amit S. Kale 已提交
864
		return -EIO;
865 866 867 868 869 870
	    break;

	default:
	    printk(KERN_ERR "Unknown port %d\n", phy);
	    break;
	}
A
Amit S. Kale 已提交
871 872 873 874

	return 0;
}

A
Adrian Bunk 已提交
875
#if 0
A
Amit S. Kale 已提交
876 877 878 879
/*
 * Return the current station MAC address.
 * Note that the passed-in value must already be in network byte order.
 */
880
int netxen_niu_xg_macaddr_get(struct netxen_adapter *adapter,
A
Amit S. Kale 已提交
881 882
			      netxen_ethernet_macaddr_t * addr)
{
883
	int phy = adapter->physical_port;
A
Al Viro 已提交
884 885 886
	u32 stationhigh;
	u32 stationlow;
	u8 val[8];
A
Amit S. Kale 已提交
887 888 889 890 891 892

	if (addr == NULL)
		return -EINVAL;
	if (phy != 0)
		return -EINVAL;

893
	if (adapter->hw_read_wx(adapter, NETXEN_NIU_XGE_STATION_ADDR_0_HI,
A
Amit S. Kale 已提交
894 895
				  &stationhigh, 4))
		return -EIO;
896
	if (adapter->hw_read_wx(adapter, NETXEN_NIU_XGE_STATION_ADDR_0_1,
A
Amit S. Kale 已提交
897 898
				  &stationlow, 4))
		return -EIO;
A
Al Viro 已提交
899 900
	((__le32 *)val)[1] = cpu_to_le32(stationhigh);
	((__le32 *)val)[0] = cpu_to_le32(stationlow);
A
Amit S. Kale 已提交
901

A
Al Viro 已提交
902
	memcpy(addr, val + 2, 6);
A
Amit S. Kale 已提交
903 904 905

	return 0;
}
A
Adrian Bunk 已提交
906
#endif  /*  0  */
A
Amit S. Kale 已提交
907 908

int netxen_niu_xg_set_promiscuous_mode(struct netxen_adapter *adapter,
909
				       netxen_niu_prom_mode_t mode)
A
Amit S. Kale 已提交
910
{
A
Al Viro 已提交
911
	__u32 reg;
912
	u32 port = adapter->physical_port;
A
Amit S. Kale 已提交
913

914
	if (port > NETXEN_NIU_MAX_XG_PORTS)
A
Amit S. Kale 已提交
915 916
		return -EINVAL;

917
	if (adapter->hw_read_wx(adapter,
918 919
		NETXEN_NIU_XGE_CONFIG_1 + (0x10000 * port), &reg, 4))
			return -EIO;
A
Amit S. Kale 已提交
920 921 922 923 924
	if (mode == NETXEN_NIU_PROMISC_MODE)
		reg = (reg | 0x2000UL);
	else
		reg = (reg & ~0x2000UL);

925 926 927 928 929
	if (mode == NETXEN_NIU_ALLMULTI_MODE)
		reg = (reg | 0x1000UL);
	else
		reg = (reg & ~0x1000UL);

930 931
	netxen_crb_writelit_adapter(adapter,
		NETXEN_NIU_XGE_CONFIG_1 + (0x10000 * port), reg);
A
Amit S. Kale 已提交
932 933 934

	return 0;
}