e1000_ethtool.c 13.8 KB
Newer Older
1 2 3
/*******************************************************************************

  
Scott Feldman's avatar
Scott Feldman committed
4
  Copyright(c) 1999 - 2002 Intel Corporation. All rights reserved.
5
  
Scott Feldman's avatar
Scott Feldman committed
6 7 8 9
  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.
10
  
Scott Feldman's avatar
Scott Feldman committed
11 12 13 14
  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.
15
  
Scott Feldman's avatar
Scott Feldman committed
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
  
Scott Feldman's avatar
Scott Feldman committed
20 21
  The full GNU General Public License is included in this distribution in the
  file called LICENSE.
22
  
Scott Feldman's avatar
Scott Feldman committed
23 24 25
  Contact Information:
  Linux NICS <linux.nics@intel.com>
  Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26 27 28 29 30 31 32 33 34 35 36 37 38 39

*******************************************************************************/

/* ethtool support for e1000 */

#include "e1000.h"

#include <asm/uaccess.h>

extern char e1000_driver_name[];
extern char e1000_driver_version[];

extern int e1000_up(struct e1000_adapter *adapter);
extern void e1000_down(struct e1000_adapter *adapter);
Scott Feldman's avatar
Scott Feldman committed
40
extern void e1000_reset(struct e1000_adapter *adapter);
41 42 43 44

static void
e1000_ethtool_gset(struct e1000_adapter *adapter, struct ethtool_cmd *ecmd)
{
45
	struct e1000_hw *hw = &adapter->hw;
46

47
	if(hw->media_type == e1000_media_type_copper) {
48 49 50 51 52 53 54 55 56 57 58

		ecmd->supported = (SUPPORTED_10baseT_Half |
		                   SUPPORTED_10baseT_Full |
		                   SUPPORTED_100baseT_Half |
		                   SUPPORTED_100baseT_Full |
		                   SUPPORTED_1000baseT_Full|
		                   SUPPORTED_Autoneg |
		                   SUPPORTED_TP);

		ecmd->advertising = ADVERTISED_TP;

59
		if(hw->autoneg == 1) {
60 61 62 63
			ecmd->advertising |= ADVERTISED_Autoneg;

			/* the e1000 autoneg seems to match ethtool nicely */

64
			ecmd->advertising |= hw->autoneg_advertised;
65 66 67
		}

		ecmd->port = PORT_TP;
68
		ecmd->phy_address = hw->phy_addr;
69

70
		if(hw->mac_type == e1000_82543)
71 72 73 74 75 76 77 78 79 80 81 82 83 84
			ecmd->transceiver = XCVR_EXTERNAL;
		else
			ecmd->transceiver = XCVR_INTERNAL;

	} else {
		ecmd->supported   = (SUPPORTED_1000baseT_Full |
				     SUPPORTED_FIBRE |
				     SUPPORTED_Autoneg);

		ecmd->advertising = (SUPPORTED_1000baseT_Full |
				     SUPPORTED_FIBRE |
				     SUPPORTED_Autoneg);

		ecmd->port = PORT_FIBRE;
Scott Feldman's avatar
Scott Feldman committed
85 86 87 88 89

		if(hw->mac_type >= e1000_82545)
			ecmd->transceiver = XCVR_INTERNAL;
		else
			ecmd->transceiver = XCVR_EXTERNAL;
90 91 92 93
	}

	if(netif_carrier_ok(adapter->netdev)) {

94
		e1000_get_speed_and_duplex(hw, &adapter->link_speed,
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		                                   &adapter->link_duplex);
		ecmd->speed = adapter->link_speed;

		/* unfortunatly FULL_DUPLEX != DUPLEX_FULL
		 *          and HALF_DUPLEX != DUPLEX_HALF */

		if(adapter->link_duplex == FULL_DUPLEX)
			ecmd->duplex = DUPLEX_FULL;
		else
			ecmd->duplex = DUPLEX_HALF;
	} else {
		ecmd->speed = -1;
		ecmd->duplex = -1;
	}

110
	ecmd->autoneg = (hw->autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE);
111 112 113 114 115
}

static int
e1000_ethtool_sset(struct e1000_adapter *adapter, struct ethtool_cmd *ecmd)
{
116
	struct e1000_hw *hw = &adapter->hw;
117 118

	if(ecmd->autoneg == AUTONEG_ENABLE) {
119
		hw->autoneg = 1;
Jeb J. Cramer's avatar
Jeb J. Cramer committed
120 121
		hw->autoneg_advertised = 0x002F;
		ecmd->advertising = 0x002F;
122
	} else {
123
		hw->autoneg = 0;
124 125
		switch(ecmd->speed + ecmd->duplex) {
		case SPEED_10 + DUPLEX_HALF:
126
			hw->forced_speed_duplex = e1000_10_half;
127 128
			break;
		case SPEED_10 + DUPLEX_FULL:
129
			hw->forced_speed_duplex = e1000_10_full;
130 131
			break;
		case SPEED_100 + DUPLEX_HALF:
132
			hw->forced_speed_duplex = e1000_100_half;
133 134
			break;
		case SPEED_100 + DUPLEX_FULL:
135
			hw->forced_speed_duplex = e1000_100_full;
136 137
			break;
		case SPEED_1000 + DUPLEX_FULL:
138 139
			hw->autoneg = 1;
			hw->autoneg_advertised = ADVERTISE_1000_FULL;
140 141 142 143 144 145 146 147 148
			break;
		case SPEED_1000 + DUPLEX_HALF: /* not supported */
		default:
			return -EINVAL;
		}
	}

	/* reset the link */

Scott Feldman's avatar
Scott Feldman committed
149 150 151 152 153
	if(netif_running(adapter->netdev)) {
		e1000_down(adapter);
		e1000_up(adapter);
	} else
		e1000_reset(adapter);
154 155 156 157 158

	return 0;
}

static inline int
159
e1000_eeprom_size(struct e1000_hw *hw)
160
{
161 162
	if((hw->mac_type > e1000_82544) &&
	   (E1000_READ_REG(hw, EECD) & E1000_EECD_SIZE))
163 164 165
		return 512;
	else
		return 128;
166 167 168 169 170 171 172 173
}

static void
e1000_ethtool_gdrvinfo(struct e1000_adapter *adapter,
                       struct ethtool_drvinfo *drvinfo)
{
	strncpy(drvinfo->driver,  e1000_driver_name, 32);
	strncpy(drvinfo->version, e1000_driver_version, 32);
Scott Feldman's avatar
Scott Feldman committed
174
	strncpy(drvinfo->fw_version, "N/A", 32);
175
	strncpy(drvinfo->bus_info, adapter->pdev->slot_name, 32);
Scott Feldman's avatar
Scott Feldman committed
176 177
#define E1000_REGS_LEN 32
	drvinfo->regdump_len  = E1000_REGS_LEN * sizeof(uint32_t);
178
	drvinfo->eedump_len  = e1000_eeprom_size(&adapter->hw);
179 180
}

Scott Feldman's avatar
Scott Feldman committed
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
static void
e1000_ethtool_gregs(struct e1000_adapter *adapter,
                    struct ethtool_regs *regs, uint32_t *regs_buff)
{
	struct e1000_hw *hw = &adapter->hw;

	regs->version = (1 << 24) | (hw->revision_id << 16) | hw->device_id;

	regs_buff[0]  = E1000_READ_REG(hw, CTRL);
	regs_buff[1]  = E1000_READ_REG(hw, STATUS);

	regs_buff[2]  = E1000_READ_REG(hw, RCTL);
	regs_buff[3]  = E1000_READ_REG(hw, RDLEN);
	regs_buff[4]  = E1000_READ_REG(hw, RDH);
	regs_buff[5]  = E1000_READ_REG(hw, RDT);
	regs_buff[6]  = E1000_READ_REG(hw, RDTR);
	
	regs_buff[7]  = E1000_READ_REG(hw, TCTL);
	regs_buff[8]  = E1000_READ_REG(hw, TDLEN);
	regs_buff[9]  = E1000_READ_REG(hw, TDH);
	regs_buff[10] = E1000_READ_REG(hw, TDT);
	regs_buff[11] = E1000_READ_REG(hw, TIDV);

	return;
}

Scott Feldman's avatar
Scott Feldman committed
207
static int
208 209 210
e1000_ethtool_geeprom(struct e1000_adapter *adapter,
                      struct ethtool_eeprom *eeprom, uint16_t *eeprom_buff)
{
211
	struct e1000_hw *hw = &adapter->hw;
212 213
	int i, max_len, first_word, last_word;

Jeb J. Cramer's avatar
Jeb J. Cramer committed
214
	if(eeprom->len == 0)
Scott Feldman's avatar
Scott Feldman committed
215
		return -EINVAL;
216

217
	eeprom->magic = hw->vendor_id | (hw->device_id << 16);
218

219
	max_len = e1000_eeprom_size(hw);
220

Scott Feldman's avatar
Scott Feldman committed
221 222 223
	if(eeprom->offset > eeprom->offset + eeprom->len)
		return -EINVAL;
	
224
	if((eeprom->offset + eeprom->len) > max_len)
225 226
		eeprom->len = (max_len - eeprom->offset);

227 228 229 230 231
	first_word = eeprom->offset >> 1;
	last_word = (eeprom->offset + eeprom->len - 1) >> 1;

	for(i = 0; i <= (last_word - first_word); i++)
		e1000_read_eeprom(hw, first_word + i, &eeprom_buff[i]);
Jeb J. Cramer's avatar
Jeb J. Cramer committed
232

Scott Feldman's avatar
Scott Feldman committed
233
	return 0;
234 235 236 237 238 239 240 241 242 243 244
}

static int 
e1000_ethtool_seeprom(struct e1000_adapter *adapter,
                      struct ethtool_eeprom *eeprom, void *user_data)
{
	struct e1000_hw *hw = &adapter->hw;
	uint16_t eeprom_buff[256];
	int i, max_len, first_word, last_word;
	void *ptr;

245 246 247
	if(eeprom->len == 0)
		return -EOPNOTSUPP;

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
	if(eeprom->magic != (hw->vendor_id | (hw->device_id << 16)))
		return -EFAULT;

	max_len = e1000_eeprom_size(hw);

	if((eeprom->offset + eeprom->len) > max_len)
		eeprom->len = (max_len - eeprom->offset);

	first_word = eeprom->offset >> 1;
	last_word = (eeprom->offset + eeprom->len - 1) >> 1;
	ptr = (void *)eeprom_buff;

	if(eeprom->offset & 1) {
		/* need read/modify/write of first changed EEPROM word */
		/* only the second byte of the word is being modified */
		e1000_read_eeprom(hw, first_word, &eeprom_buff[0]);
		ptr++;
	}
	if((eeprom->offset + eeprom->len) & 1) {
		/* need read/modify/write of last changed EEPROM word */
		/* only the first byte of the word is being modified */
		e1000_read_eeprom(hw, last_word,
		                  &eeprom_buff[last_word - first_word]);
	}
	if(copy_from_user(ptr, user_data, eeprom->len))
		return -EFAULT;

	for(i = 0; i <= (last_word - first_word); i++)
		e1000_write_eeprom(hw, first_word + i, eeprom_buff[i]);

	/* Update the checksum over the first part of the EEPROM if needed */
	if(first_word <= EEPROM_CHECKSUM_REG)
		e1000_update_eeprom_checksum(hw);

	return 0;
283 284 285 286 287
}

static void
e1000_ethtool_gwol(struct e1000_adapter *adapter, struct ethtool_wolinfo *wol)
{
288
	struct e1000_hw *hw = &adapter->hw;
Scott Feldman's avatar
Scott Feldman committed
289 290 291 292 293 294

	switch(adapter->hw.device_id) {
	case E1000_DEV_ID_82542:
	case E1000_DEV_ID_82543GC_FIBER:
	case E1000_DEV_ID_82543GC_COPPER:
	case E1000_DEV_ID_82544EI_FIBER:
295 296 297 298
		wol->supported = 0;
		wol->wolopts   = 0;
		return;

Scott Feldman's avatar
Scott Feldman committed
299 300 301 302 303 304 305 306 307
	case E1000_DEV_ID_82546EB_FIBER:
		/* Wake events only supported on port A for dual fiber */
		if(E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1) {
			wol->supported = 0;
			wol->wolopts   = 0;
			return;
		}
		/* Fall Through */

Jeb J. Cramer's avatar
Jeb J. Cramer committed
308
	default:
Scott Feldman's avatar
Scott Feldman committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
		wol->supported = WAKE_PHY | WAKE_UCAST | 
				 WAKE_MCAST | WAKE_BCAST | WAKE_MAGIC;
		
		wol->wolopts = 0;
		if(adapter->wol & E1000_WUFC_LNKC)
			wol->wolopts |= WAKE_PHY;
		if(adapter->wol & E1000_WUFC_EX)
			wol->wolopts |= WAKE_UCAST;
		if(adapter->wol & E1000_WUFC_MC)
			wol->wolopts |= WAKE_MCAST;
		if(adapter->wol & E1000_WUFC_BC)
			wol->wolopts |= WAKE_BCAST;
		if(adapter->wol & E1000_WUFC_MAG)
			wol->wolopts |= WAKE_MAGIC;
		return;
	}
325 326 327 328 329
}

static int
e1000_ethtool_swol(struct e1000_adapter *adapter, struct ethtool_wolinfo *wol)
{
330
	struct e1000_hw *hw = &adapter->hw;
331

Scott Feldman's avatar
Scott Feldman committed
332 333 334 335 336
	switch(adapter->hw.device_id) {
	case E1000_DEV_ID_82542:
	case E1000_DEV_ID_82543GC_FIBER:
	case E1000_DEV_ID_82543GC_COPPER:
	case E1000_DEV_ID_82544EI_FIBER:
337
		return wol->wolopts ? -EOPNOTSUPP : 0;
338

Scott Feldman's avatar
Scott Feldman committed
339 340 341 342 343 344
	case E1000_DEV_ID_82546EB_FIBER:
		/* Wake events only supported on port A for dual fiber */
		if(E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
			return wol->wolopts ? -EOPNOTSUPP : 0;
		/* Fall Through */

Jeb J. Cramer's avatar
Jeb J. Cramer committed
345
	default:
346
		if(wol->wolopts & (WAKE_ARP | WAKE_MAGICSECURE))
Scott Feldman's avatar
Scott Feldman committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
			return -EOPNOTSUPP;

		adapter->wol = 0;

		if(wol->wolopts & WAKE_PHY)
			adapter->wol |= E1000_WUFC_LNKC;
		if(wol->wolopts & WAKE_UCAST)
			adapter->wol |= E1000_WUFC_EX;
		if(wol->wolopts & WAKE_MCAST)
			adapter->wol |= E1000_WUFC_MC;
		if(wol->wolopts & WAKE_BCAST)
			adapter->wol |= E1000_WUFC_BC;
		if(wol->wolopts & WAKE_MAGIC)
			adapter->wol |= E1000_WUFC_MAG;
	}
362 363 364 365

	return 0;
}

Scott Feldman's avatar
Scott Feldman committed
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

/* toggle LED 4 times per second = 2 "blinks" per second */
#define E1000_ID_INTERVAL	(HZ/4)

/* bit defines for adapter->led_status */
#define E1000_LED_ON		0

static void
e1000_led_blink_callback(unsigned long data)
{
	struct e1000_adapter *adapter = (struct e1000_adapter *) data;
	
	if(test_and_change_bit(E1000_LED_ON, &adapter->led_status))
		e1000_led_off(&adapter->hw);
	else
		e1000_led_on(&adapter->hw);

	mod_timer(&adapter->blink_timer, jiffies + E1000_ID_INTERVAL);
}

static int
e1000_ethtool_led_blink(struct e1000_adapter *adapter, struct ethtool_value *id)
{
	if(!adapter->blink_timer.function) {
		init_timer(&adapter->blink_timer);
		adapter->blink_timer.function = e1000_led_blink_callback;
		adapter->blink_timer.data = (unsigned long) adapter;
	}

	e1000_setup_led(&adapter->hw);
	mod_timer(&adapter->blink_timer, jiffies);
	
	set_current_state(TASK_INTERRUPTIBLE);
	if(id->data)
		schedule_timeout(id->data * HZ);
	else
		schedule_timeout(MAX_SCHEDULE_TIMEOUT);
	
	del_timer_sync(&adapter->blink_timer);
	e1000_led_off(&adapter->hw);
	clear_bit(E1000_LED_ON, &adapter->led_status);
	e1000_cleanup_led(&adapter->hw);

	return 0;
}

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
int
e1000_ethtool_ioctl(struct net_device *netdev, struct ifreq *ifr)
{
	struct e1000_adapter *adapter = netdev->priv;
	void *addr = ifr->ifr_data;
	uint32_t cmd;

	if(get_user(cmd, (uint32_t *) addr))
		return -EFAULT;

	switch(cmd) {
	case ETHTOOL_GSET: {
		struct ethtool_cmd ecmd = {ETHTOOL_GSET};
		e1000_ethtool_gset(adapter, &ecmd);
		if(copy_to_user(addr, &ecmd, sizeof(ecmd)))
			return -EFAULT;
		return 0;
	}
	case ETHTOOL_SSET: {
		struct ethtool_cmd ecmd;
		if(!capable(CAP_NET_ADMIN))
			return -EPERM;
		if(copy_from_user(&ecmd, addr, sizeof(ecmd)))
			return -EFAULT;
		return e1000_ethtool_sset(adapter, &ecmd);
	}
	case ETHTOOL_GDRVINFO: {
		struct ethtool_drvinfo drvinfo = {ETHTOOL_GDRVINFO};
		e1000_ethtool_gdrvinfo(adapter, &drvinfo);
		if(copy_to_user(addr, &drvinfo, sizeof(drvinfo)))
			return -EFAULT;
		return 0;
	}
Scott Feldman's avatar
Scott Feldman committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
	case ETHTOOL_GREGS: {
		struct ethtool_regs regs = {ETHTOOL_GREGS};
		uint32_t regs_buff[E1000_REGS_LEN];

		if(copy_from_user(&regs, addr, sizeof(regs)))
			return -EFAULT;
		e1000_ethtool_gregs(adapter, &regs, regs_buff);
		if(copy_to_user(addr, &regs, sizeof(regs)))
			return -EFAULT;

		addr += offsetof(struct ethtool_regs, data);
		if(copy_to_user(addr, regs_buff, regs.len))
			return -EFAULT;

		return 0;
	}
461 462 463
	case ETHTOOL_NWAY_RST: {
		if(!capable(CAP_NET_ADMIN))
			return -EPERM;
Scott Feldman's avatar
Scott Feldman committed
464 465 466 467
		if(netif_running(netdev)) {
			e1000_down(adapter);
			e1000_up(adapter);
		}
468 469
		return 0;
	}
Scott Feldman's avatar
Scott Feldman committed
470 471 472 473 474 475
	case ETHTOOL_PHYS_ID: {
		struct ethtool_value id;
		if(copy_from_user(&id, addr, sizeof(id)))
			return -EFAULT;
		return e1000_ethtool_led_blink(adapter, &id);
	}
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
	case ETHTOOL_GLINK: {
		struct ethtool_value link = {ETHTOOL_GLINK};
		link.data = netif_carrier_ok(netdev);
		if(copy_to_user(addr, &link, sizeof(link)))
			return -EFAULT;
		return 0;
	}
	case ETHTOOL_GWOL: {
		struct ethtool_wolinfo wol = {ETHTOOL_GWOL};
		e1000_ethtool_gwol(adapter, &wol);
		if(copy_to_user(addr, &wol, sizeof(wol)) != 0)
			return -EFAULT;
		return 0;
	}
	case ETHTOOL_SWOL: {
		struct ethtool_wolinfo wol;
		if(!capable(CAP_NET_ADMIN))
			return -EPERM;
		if(copy_from_user(&wol, addr, sizeof(wol)) != 0)
			return -EFAULT;
		return e1000_ethtool_swol(adapter, &wol);
	}
	case ETHTOOL_GEEPROM: {
		struct ethtool_eeprom eeprom = {ETHTOOL_GEEPROM};
		uint16_t eeprom_buff[256];
501
		void *ptr;
Scott Feldman's avatar
Scott Feldman committed
502
		int err;
503 504 505 506

		if(copy_from_user(&eeprom, addr, sizeof(eeprom)))
			return -EFAULT;

Jeb J. Cramer's avatar
Jeb J. Cramer committed
507 508
		if((err = e1000_ethtool_geeprom(adapter, 
			&eeprom, eeprom_buff)))
Scott Feldman's avatar
Scott Feldman committed
509
			return err;
510 511 512 513 514

		if(copy_to_user(addr, &eeprom, sizeof(eeprom)))
			return -EFAULT;

		addr += offsetof(struct ethtool_eeprom, data);
515
		ptr = ((void *)eeprom_buff) + (eeprom.offset & 1);
516

517
		if(copy_to_user(addr, ptr, eeprom.len))
518 519 520
			return -EFAULT;
		return 0;
	}
521 522 523 524 525 526 527 528 529 530 531 532
	case ETHTOOL_SEEPROM: {
		struct ethtool_eeprom eeprom;

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

		if(copy_from_user(&eeprom, addr, sizeof(eeprom)))
			return -EFAULT;

		addr += offsetof(struct ethtool_eeprom, data);
		return e1000_ethtool_seeprom(adapter, &eeprom, addr);
	}
533 534 535 536 537 538
	default:
		return -EOPNOTSUPP;
	}
}