qla3xxx.c 101.8 KB
Newer Older
R
Ron Mercer 已提交
1 2 3 4 5 6 7
/*
 * QLogic QLA3xxx NIC HBA Driver
 * Copyright (c)  2003-2006 QLogic Corporation
 *
 * See LICENSE.qla3xxx for copyright and licensing details.
 */

8 9
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

R
Ron Mercer 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <linux/dma-mapping.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/dmapool.h>
#include <linux/mempool.h>
#include <linux/spinlock.h>
#include <linux/kthread.h>
#include <linux/interrupt.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/ip.h>
27
#include <linux/in.h>
R
Ron Mercer 已提交
28 29 30 31 32 33 34 35 36 37
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
#include <linux/if_vlan.h>
#include <linux/delay.h>
#include <linux/mm.h>
38
#include <linux/prefetch.h>
R
Ron Mercer 已提交
39 40 41

#include "qla3xxx.h"

42 43
#define DRV_NAME	"qla3xxx"
#define DRV_STRING	"QLogic ISP3XXX Network Driver"
R
root 已提交
44
#define DRV_VERSION	"v2.03.00-k5"
R
Ron Mercer 已提交
45 46 47 48

static const char ql3xxx_driver_name[] = DRV_NAME;
static const char ql3xxx_driver_version[] = DRV_VERSION;

49 50 51
#define TIMED_OUT_MSG							\
"Timed out waiting for management port to get free before issuing command\n"

R
Ron Mercer 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
MODULE_AUTHOR("QLogic Corporation");
MODULE_DESCRIPTION("QLogic ISP3XXX Network Driver " DRV_VERSION " ");
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_VERSION);

static const u32 default_msg
    = NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK
    | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN;

static int debug = -1;		/* defaults above */
module_param(debug, int, 0);
MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");

static int msi;
module_param(msi, int, 0);
MODULE_PARM_DESC(msi, "Turn on Message Signaled Interrupts.");

69
static DEFINE_PCI_DEVICE_TABLE(ql3xxx_pci_tbl) = {
R
Ron Mercer 已提交
70
	{PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QL3022_DEVICE_ID)},
71
	{PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QL3032_DEVICE_ID)},
R
Ron Mercer 已提交
72 73 74 75 76 77
	/* required last entry */
	{0,}
};

MODULE_DEVICE_TABLE(pci, ql3xxx_pci_tbl);

78 79 80
/*
 *  These are the known PHY's which are used
 */
81
enum PHY_DEVICE_TYPE {
82 83 84 85
   PHY_TYPE_UNKNOWN   = 0,
   PHY_VITESSE_VSC8211,
   PHY_AGERE_ET1011C,
   MAX_PHY_DEV_TYPES
86 87 88 89 90 91 92 93 94 95 96 97 98
};

struct PHY_DEVICE_INFO {
	const enum PHY_DEVICE_TYPE	phyDevice;
	const u32		phyIdOUI;
	const u16		phyIdModel;
	const char		*name;
};

static const struct PHY_DEVICE_INFO PHY_DEVICES[] = {
	{PHY_TYPE_UNKNOWN,    0x000000, 0x0, "PHY_TYPE_UNKNOWN"},
	{PHY_VITESSE_VSC8211, 0x0003f1, 0xb, "PHY_VITESSE_VSC8211"},
	{PHY_AGERE_ET1011C,   0x00a0bc, 0x1, "PHY_AGERE_ET1011C"},
99 100 101
};


R
Ron Mercer 已提交
102 103 104 105 106 107
/*
 * Caller must take hw_lock.
 */
static int ql_sem_spinlock(struct ql3_adapter *qdev,
			    u32 sem_mask, u32 sem_bits)
{
108 109
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
R
Ron Mercer 已提交
110 111 112 113 114 115 116 117 118 119
	u32 value;
	unsigned int seconds = 3;

	do {
		writel((sem_mask | sem_bits),
		       &port_regs->CommonRegs.semaphoreReg);
		value = readl(&port_regs->CommonRegs.semaphoreReg);
		if ((value & (sem_mask >> 16)) == sem_bits)
			return 0;
		ssleep(1);
120
	} while (--seconds);
R
Ron Mercer 已提交
121 122 123 124 125
	return -1;
}

static void ql_sem_unlock(struct ql3_adapter *qdev, u32 sem_mask)
{
126 127
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
R
Ron Mercer 已提交
128 129 130 131 132 133
	writel(sem_mask, &port_regs->CommonRegs.semaphoreReg);
	readl(&port_regs->CommonRegs.semaphoreReg);
}

static int ql_sem_lock(struct ql3_adapter *qdev, u32 sem_mask, u32 sem_bits)
{
134 135
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
R
Ron Mercer 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149
	u32 value;

	writel((sem_mask | sem_bits), &port_regs->CommonRegs.semaphoreReg);
	value = readl(&port_regs->CommonRegs.semaphoreReg);
	return ((value & (sem_mask >> 16)) == sem_bits);
}

/*
 * Caller holds hw_lock.
 */
static int ql_wait_for_drvr_lock(struct ql3_adapter *qdev)
{
	int i = 0;

150 151 152 153 154 155 156 157 158 159
	while (i < 10) {
		if (i)
			ssleep(1);

		if (ql_sem_lock(qdev,
				QL_DRVR_SEM_MASK,
				(QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index)
				 * 2) << 1)) {
			netdev_printk(KERN_DEBUG, qdev->ndev,
				      "driver lock acquired\n");
R
Ron Mercer 已提交
160 161 162
			return 1;
		}
	}
163 164 165

	netdev_err(qdev->ndev, "Timed out waiting for driver lock...\n");
	return 0;
R
Ron Mercer 已提交
166 167 168 169
}

static void ql_set_register_page(struct ql3_adapter *qdev, u32 page)
{
170 171
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
R
Ron Mercer 已提交
172 173 174 175 176 177 178

	writel(((ISP_CONTROL_NP_MASK << 16) | page),
			&port_regs->CommonRegs.ispControlStatus);
	readl(&port_regs->CommonRegs.ispControlStatus);
	qdev->current_page = page;
}

179
static u32 ql_read_common_reg_l(struct ql3_adapter *qdev, u32 __iomem *reg)
R
Ron Mercer 已提交
180 181 182 183 184 185 186 187 188 189 190
{
	u32 value;
	unsigned long hw_flags;

	spin_lock_irqsave(&qdev->hw_lock, hw_flags);
	value = readl(reg);
	spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);

	return value;
}

191
static u32 ql_read_common_reg(struct ql3_adapter *qdev, u32 __iomem *reg)
R
Ron Mercer 已提交
192 193 194 195 196 197 198 199 200 201 202 203
{
	return readl(reg);
}

static u32 ql_read_page0_reg_l(struct ql3_adapter *qdev, u32 __iomem *reg)
{
	u32 value;
	unsigned long hw_flags;

	spin_lock_irqsave(&qdev->hw_lock, hw_flags);

	if (qdev->current_page != 0)
204
		ql_set_register_page(qdev, 0);
R
Ron Mercer 已提交
205 206 207 208 209 210 211 212 213
	value = readl(reg);

	spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
	return value;
}

static u32 ql_read_page0_reg(struct ql3_adapter *qdev, u32 __iomem *reg)
{
	if (qdev->current_page != 0)
214
		ql_set_register_page(qdev, 0);
R
Ron Mercer 已提交
215 216 217 218
	return readl(reg);
}

static void ql_write_common_reg_l(struct ql3_adapter *qdev,
A
Al Viro 已提交
219
				u32 __iomem *reg, u32 value)
R
Ron Mercer 已提交
220 221 222 223
{
	unsigned long hw_flags;

	spin_lock_irqsave(&qdev->hw_lock, hw_flags);
A
Al Viro 已提交
224
	writel(value, reg);
R
Ron Mercer 已提交
225 226 227 228 229
	readl(reg);
	spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
}

static void ql_write_common_reg(struct ql3_adapter *qdev,
A
Al Viro 已提交
230
				u32 __iomem *reg, u32 value)
R
Ron Mercer 已提交
231
{
A
Al Viro 已提交
232
	writel(value, reg);
R
Ron Mercer 已提交
233 234 235
	readl(reg);
}

236 237 238 239 240 241 242 243
static void ql_write_nvram_reg(struct ql3_adapter *qdev,
				u32 __iomem *reg, u32 value)
{
	writel(value, reg);
	readl(reg);
	udelay(1);
}

R
Ron Mercer 已提交
244
static void ql_write_page0_reg(struct ql3_adapter *qdev,
A
Al Viro 已提交
245
			       u32 __iomem *reg, u32 value)
R
Ron Mercer 已提交
246 247
{
	if (qdev->current_page != 0)
248
		ql_set_register_page(qdev, 0);
A
Al Viro 已提交
249
	writel(value, reg);
R
Ron Mercer 已提交
250 251 252 253 254 255 256
	readl(reg);
}

/*
 * Caller holds hw_lock. Only called during init.
 */
static void ql_write_page1_reg(struct ql3_adapter *qdev,
A
Al Viro 已提交
257
			       u32 __iomem *reg, u32 value)
R
Ron Mercer 已提交
258 259
{
	if (qdev->current_page != 1)
260
		ql_set_register_page(qdev, 1);
A
Al Viro 已提交
261
	writel(value, reg);
R
Ron Mercer 已提交
262 263 264 265 266 267 268
	readl(reg);
}

/*
 * Caller holds hw_lock. Only called during init.
 */
static void ql_write_page2_reg(struct ql3_adapter *qdev,
A
Al Viro 已提交
269
			       u32 __iomem *reg, u32 value)
R
Ron Mercer 已提交
270 271
{
	if (qdev->current_page != 2)
272
		ql_set_register_page(qdev, 2);
A
Al Viro 已提交
273
	writel(value, reg);
R
Ron Mercer 已提交
274 275 276 277 278
	readl(reg);
}

static void ql_disable_interrupts(struct ql3_adapter *qdev)
{
279 280
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
R
Ron Mercer 已提交
281 282 283 284 285 286 287 288

	ql_write_common_reg_l(qdev, &port_regs->CommonRegs.ispInterruptMaskReg,
			    (ISP_IMR_ENABLE_INT << 16));

}

static void ql_enable_interrupts(struct ql3_adapter *qdev)
{
289 290
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
R
Ron Mercer 已提交
291 292 293 294 295 296 297 298 299

	ql_write_common_reg_l(qdev, &port_regs->CommonRegs.ispInterruptMaskReg,
			    ((0xff << 16) | ISP_IMR_ENABLE_INT));

}

static void ql_release_to_lrg_buf_free_list(struct ql3_adapter *qdev,
					    struct ql_rcv_buf_cb *lrg_buf_cb)
{
300 301
	dma_addr_t map;
	int err;
R
Ron Mercer 已提交
302 303 304 305 306 307 308 309 310 311
	lrg_buf_cb->next = NULL;

	if (qdev->lrg_buf_free_tail == NULL) {	/* The list is empty  */
		qdev->lrg_buf_free_head = qdev->lrg_buf_free_tail = lrg_buf_cb;
	} else {
		qdev->lrg_buf_free_tail->next = lrg_buf_cb;
		qdev->lrg_buf_free_tail = lrg_buf_cb;
	}

	if (!lrg_buf_cb->skb) {
312 313
		lrg_buf_cb->skb = netdev_alloc_skb(qdev->ndev,
						   qdev->lrg_buffer_len);
R
Ron Mercer 已提交
314
		if (unlikely(!lrg_buf_cb->skb)) {
315
			netdev_err(qdev->ndev, "failed netdev_alloc_skb()\n");
R
Ron Mercer 已提交
316 317 318 319 320 321 322 323 324 325 326 327
			qdev->lrg_buf_skb_check++;
		} else {
			/*
			 * We save some space to copy the ethhdr from first
			 * buffer
			 */
			skb_reserve(lrg_buf_cb->skb, QL_HEADER_SPACE);
			map = pci_map_single(qdev->pdev,
					     lrg_buf_cb->skb->data,
					     qdev->lrg_buffer_len -
					     QL_HEADER_SPACE,
					     PCI_DMA_FROMDEVICE);
328
			err = pci_dma_mapping_error(qdev->pdev, map);
329
			if (err) {
330 331 332
				netdev_err(qdev->ndev,
					   "PCI mapping failed with error: %d\n",
					   err);
333 334 335 336 337 338 339
				dev_kfree_skb(lrg_buf_cb->skb);
				lrg_buf_cb->skb = NULL;

				qdev->lrg_buf_skb_check++;
				return;
			}

R
Ron Mercer 已提交
340 341 342 343
			lrg_buf_cb->buf_phy_addr_low =
			    cpu_to_le32(LS_64BITS(map));
			lrg_buf_cb->buf_phy_addr_high =
			    cpu_to_le32(MS_64BITS(map));
344 345
			dma_unmap_addr_set(lrg_buf_cb, mapaddr, map);
			dma_unmap_len_set(lrg_buf_cb, maplen,
R
Ron Mercer 已提交
346 347 348 349 350 351 352 353 354 355 356
					  qdev->lrg_buffer_len -
					  QL_HEADER_SPACE);
		}
	}

	qdev->lrg_buf_free_count++;
}

static struct ql_rcv_buf_cb *ql_get_from_lrg_buf_free_list(struct ql3_adapter
							   *qdev)
{
357
	struct ql_rcv_buf_cb *lrg_buf_cb = qdev->lrg_buf_free_head;
R
Ron Mercer 已提交
358

359 360 361
	if (lrg_buf_cb != NULL) {
		qdev->lrg_buf_free_head = lrg_buf_cb->next;
		if (qdev->lrg_buf_free_head == NULL)
R
Ron Mercer 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
			qdev->lrg_buf_free_tail = NULL;
		qdev->lrg_buf_free_count--;
	}

	return lrg_buf_cb;
}

static u32 addrBits = EEPROM_NO_ADDR_BITS;
static u32 dataBits = EEPROM_NO_DATA_BITS;

static void fm93c56a_deselect(struct ql3_adapter *qdev);
static void eeprom_readword(struct ql3_adapter *qdev, u32 eepromAddr,
			    unsigned short *value);

/*
 * Caller holds hw_lock.
 */
static void fm93c56a_select(struct ql3_adapter *qdev)
{
	struct ql3xxx_port_registers __iomem *port_regs =
382
			qdev->mem_map_registers;
383
	__iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
R
Ron Mercer 已提交
384 385

	qdev->eeprom_cmd_data = AUBURN_EEPROM_CS_1;
386 387 388
	ql_write_nvram_reg(qdev, spir, ISP_NVRAM_MASK | qdev->eeprom_cmd_data);
	ql_write_nvram_reg(qdev, spir,
			   ((ISP_NVRAM_MASK << 16) | qdev->eeprom_cmd_data));
R
Ron Mercer 已提交
389 390 391 392 393 394 395 396 397 398 399 400
}

/*
 * Caller holds hw_lock.
 */
static void fm93c56a_cmd(struct ql3_adapter *qdev, u32 cmd, u32 eepromAddr)
{
	int i;
	u32 mask;
	u32 dataBit;
	u32 previousBit;
	struct ql3xxx_port_registers __iomem *port_regs =
401
			qdev->mem_map_registers;
402
	__iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
R
Ron Mercer 已提交
403 404

	/* Clock in a zero, then do the start bit */
405 406 407 408 409 410 411 412 413
	ql_write_nvram_reg(qdev, spir,
			   (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
			    AUBURN_EEPROM_DO_1));
	ql_write_nvram_reg(qdev, spir,
			   (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
			    AUBURN_EEPROM_DO_1 | AUBURN_EEPROM_CLK_RISE));
	ql_write_nvram_reg(qdev, spir,
			   (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
			    AUBURN_EEPROM_DO_1 | AUBURN_EEPROM_CLK_FALL));
R
Ron Mercer 已提交
414 415 416 417 418

	mask = 1 << (FM93C56A_CMD_BITS - 1);
	/* Force the previous data bit to be different */
	previousBit = 0xffff;
	for (i = 0; i < FM93C56A_CMD_BITS; i++) {
419 420 421
		dataBit = (cmd & mask)
			? AUBURN_EEPROM_DO_1
			: AUBURN_EEPROM_DO_0;
R
Ron Mercer 已提交
422
		if (previousBit != dataBit) {
423 424 425 426
			/* If the bit changed, change the DO state to match */
			ql_write_nvram_reg(qdev, spir,
					   (ISP_NVRAM_MASK |
					    qdev->eeprom_cmd_data | dataBit));
R
Ron Mercer 已提交
427 428
			previousBit = dataBit;
		}
429 430 431 432 433 434
		ql_write_nvram_reg(qdev, spir,
				   (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
				    dataBit | AUBURN_EEPROM_CLK_RISE));
		ql_write_nvram_reg(qdev, spir,
				   (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
				    dataBit | AUBURN_EEPROM_CLK_FALL));
R
Ron Mercer 已提交
435 436 437 438 439 440 441
		cmd = cmd << 1;
	}

	mask = 1 << (addrBits - 1);
	/* Force the previous data bit to be different */
	previousBit = 0xffff;
	for (i = 0; i < addrBits; i++) {
442 443
		dataBit = (eepromAddr & mask) ? AUBURN_EEPROM_DO_1
			: AUBURN_EEPROM_DO_0;
R
Ron Mercer 已提交
444 445 446 447 448
		if (previousBit != dataBit) {
			/*
			 * If the bit changed, then change the DO state to
			 * match
			 */
449 450 451
			ql_write_nvram_reg(qdev, spir,
					   (ISP_NVRAM_MASK |
					    qdev->eeprom_cmd_data | dataBit));
R
Ron Mercer 已提交
452 453
			previousBit = dataBit;
		}
454 455 456 457 458 459
		ql_write_nvram_reg(qdev, spir,
				   (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
				    dataBit | AUBURN_EEPROM_CLK_RISE));
		ql_write_nvram_reg(qdev, spir,
				   (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
				    dataBit | AUBURN_EEPROM_CLK_FALL));
R
Ron Mercer 已提交
460 461 462 463 464 465 466 467 468 469
		eepromAddr = eepromAddr << 1;
	}
}

/*
 * Caller holds hw_lock.
 */
static void fm93c56a_deselect(struct ql3_adapter *qdev)
{
	struct ql3xxx_port_registers __iomem *port_regs =
470
			qdev->mem_map_registers;
471
	__iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
472

R
Ron Mercer 已提交
473
	qdev->eeprom_cmd_data = AUBURN_EEPROM_CS_0;
474
	ql_write_nvram_reg(qdev, spir, ISP_NVRAM_MASK | qdev->eeprom_cmd_data);
R
Ron Mercer 已提交
475 476 477 478 479 480 481 482 483 484 485
}

/*
 * Caller holds hw_lock.
 */
static void fm93c56a_datain(struct ql3_adapter *qdev, unsigned short *value)
{
	int i;
	u32 data = 0;
	u32 dataBit;
	struct ql3xxx_port_registers __iomem *port_regs =
486
			qdev->mem_map_registers;
487
	__iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
R
Ron Mercer 已提交
488 489 490 491

	/* Read the data bits */
	/* The first bit is a dummy.  Clock right over it. */
	for (i = 0; i < dataBits; i++) {
492 493 494 495 496 497 498 499
		ql_write_nvram_reg(qdev, spir,
				   ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
				   AUBURN_EEPROM_CLK_RISE);
		ql_write_nvram_reg(qdev, spir,
				   ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
				   AUBURN_EEPROM_CLK_FALL);
		dataBit = (ql_read_common_reg(qdev, spir) &
			   AUBURN_EEPROM_DI_1) ? 1 : 0;
R
Ron Mercer 已提交
500 501
		data = (data << 1) | dataBit;
	}
502
	*value = (u16)data;
R
Ron Mercer 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516
}

/*
 * Caller holds hw_lock.
 */
static void eeprom_readword(struct ql3_adapter *qdev,
			    u32 eepromAddr, unsigned short *value)
{
	fm93c56a_select(qdev);
	fm93c56a_cmd(qdev, (int)FM93C56A_READ, eepromAddr);
	fm93c56a_datain(qdev, value);
	fm93c56a_deselect(qdev);
}

A
Al Viro 已提交
517 518 519 520 521 522
static void ql_set_mac_addr(struct net_device *ndev, u16 *addr)
{
	__le16 *p = (__le16 *)ndev->dev_addr;
	p[0] = cpu_to_le16(addr[0]);
	p[1] = cpu_to_le16(addr[1]);
	p[2] = cpu_to_le16(addr[2]);
R
Ron Mercer 已提交
523 524 525 526 527 528 529 530 531 532 533
}

static int ql_get_nvram_params(struct ql3_adapter *qdev)
{
	u16 *pEEPROMData;
	u16 checksum = 0;
	u32 index;
	unsigned long hw_flags;

	spin_lock_irqsave(&qdev->hw_lock, hw_flags);

534
	pEEPROMData = (u16 *)&qdev->nvram_data;
R
Ron Mercer 已提交
535
	qdev->eeprom_cmd_data = 0;
536
	if (ql_sem_spinlock(qdev, QL_NVRAM_SEM_MASK,
R
Ron Mercer 已提交
537 538
			(QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
			 2) << 10)) {
539
		pr_err("%s: Failed ql_sem_spinlock()\n", __func__);
R
Ron Mercer 已提交
540 541 542 543 544 545 546 547 548 549 550 551
		spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
		return -1;
	}

	for (index = 0; index < EEPROM_SIZE; index++) {
		eeprom_readword(qdev, index, pEEPROMData);
		checksum += *pEEPROMData;
		pEEPROMData++;
	}
	ql_sem_unlock(qdev, QL_NVRAM_SEM_MASK);

	if (checksum != 0) {
552 553
		netdev_err(qdev->ndev, "checksum should be zero, is %x!!\n",
			   checksum);
R
Ron Mercer 已提交
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
		spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
		return -1;
	}

	spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
	return checksum;
}

static const u32 PHYAddr[2] = {
	PORT0_PHY_ADDRESS, PORT1_PHY_ADDRESS
};

static int ql_wait_for_mii_ready(struct ql3_adapter *qdev)
{
	struct ql3xxx_port_registers __iomem *port_regs =
569
			qdev->mem_map_registers;
R
Ron Mercer 已提交
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	u32 temp;
	int count = 1000;

	while (count) {
		temp = ql_read_page0_reg(qdev, &port_regs->macMIIStatusReg);
		if (!(temp & MAC_MII_STATUS_BSY))
			return 0;
		udelay(10);
		count--;
	}
	return -1;
}

static void ql_mii_enable_scan_mode(struct ql3_adapter *qdev)
{
	struct ql3xxx_port_registers __iomem *port_regs =
586
			qdev->mem_map_registers;
R
Ron Mercer 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	u32 scanControl;

	if (qdev->numPorts > 1) {
		/* Auto scan will cycle through multiple ports */
		scanControl = MAC_MII_CONTROL_AS | MAC_MII_CONTROL_SC;
	} else {
		scanControl = MAC_MII_CONTROL_SC;
	}

	/*
	 * Scan register 1 of PHY/PETBI,
	 * Set up to scan both devices
	 * The autoscan starts from the first register, completes
	 * the last one before rolling over to the first
	 */
	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
			   PHYAddr[0] | MII_SCAN_REGISTER);

	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
			   (scanControl) |
			   ((MAC_MII_CONTROL_SC | MAC_MII_CONTROL_AS) << 16));
}

static u8 ql_mii_disable_scan_mode(struct ql3_adapter *qdev)
{
	u8 ret;
	struct ql3xxx_port_registers __iomem *port_regs =
614
					qdev->mem_map_registers;
R
Ron Mercer 已提交
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640

	/* See if scan mode is enabled before we turn it off */
	if (ql_read_page0_reg(qdev, &port_regs->macMIIMgmtControlReg) &
	    (MAC_MII_CONTROL_AS | MAC_MII_CONTROL_SC)) {
		/* Scan is enabled */
		ret = 1;
	} else {
		/* Scan is disabled */
		ret = 0;
	}

	/*
	 * When disabling scan mode you must first change the MII register
	 * address
	 */
	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
			   PHYAddr[0] | MII_SCAN_REGISTER);

	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
			   ((MAC_MII_CONTROL_SC | MAC_MII_CONTROL_AS |
			     MAC_MII_CONTROL_RC) << 16));

	return ret;
}

static int ql_mii_write_reg_ex(struct ql3_adapter *qdev,
641
			       u16 regAddr, u16 value, u32 phyAddr)
R
Ron Mercer 已提交
642 643
{
	struct ql3xxx_port_registers __iomem *port_regs =
644
			qdev->mem_map_registers;
R
Ron Mercer 已提交
645 646 647 648 649
	u8 scanWasEnabled;

	scanWasEnabled = ql_mii_disable_scan_mode(qdev);

	if (ql_wait_for_mii_ready(qdev)) {
650
		netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
R
Ron Mercer 已提交
651 652 653 654
		return -1;
	}

	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
655
			   phyAddr | regAddr);
R
Ron Mercer 已提交
656 657 658 659 660

	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtDataReg, value);

	/* Wait for write to complete 9/10/04 SJP */
	if (ql_wait_for_mii_ready(qdev)) {
661
		netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
R
Ron Mercer 已提交
662 663 664 665 666 667 668 669 670 671
		return -1;
	}

	if (scanWasEnabled)
		ql_mii_enable_scan_mode(qdev);

	return 0;
}

static int ql_mii_read_reg_ex(struct ql3_adapter *qdev, u16 regAddr,
672
			      u16 *value, u32 phyAddr)
R
Ron Mercer 已提交
673 674
{
	struct ql3xxx_port_registers __iomem *port_regs =
675
			qdev->mem_map_registers;
R
Ron Mercer 已提交
676 677 678 679 680 681
	u8 scanWasEnabled;
	u32 temp;

	scanWasEnabled = ql_mii_disable_scan_mode(qdev);

	if (ql_wait_for_mii_ready(qdev)) {
682
		netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
R
Ron Mercer 已提交
683 684 685 686
		return -1;
	}

	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
687
			   phyAddr | regAddr);
R
Ron Mercer 已提交
688 689 690 691 692 693 694 695 696

	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
			   (MAC_MII_CONTROL_RC << 16));

	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
			   (MAC_MII_CONTROL_RC << 16) | MAC_MII_CONTROL_RC);

	/* Wait for the read to complete */
	if (ql_wait_for_mii_ready(qdev)) {
697
		netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
R
Ron Mercer 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
		return -1;
	}

	temp = ql_read_page0_reg(qdev, &port_regs->macMIIMgmtDataReg);
	*value = (u16) temp;

	if (scanWasEnabled)
		ql_mii_enable_scan_mode(qdev);

	return 0;
}

static int ql_mii_write_reg(struct ql3_adapter *qdev, u16 regAddr, u16 value)
{
	struct ql3xxx_port_registers __iomem *port_regs =
713
			qdev->mem_map_registers;
R
Ron Mercer 已提交
714 715 716 717

	ql_mii_disable_scan_mode(qdev);

	if (ql_wait_for_mii_ready(qdev)) {
718
		netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
R
Ron Mercer 已提交
719 720 721 722 723 724 725 726 727 728
		return -1;
	}

	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
			   qdev->PHYAddr | regAddr);

	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtDataReg, value);

	/* Wait for write to complete. */
	if (ql_wait_for_mii_ready(qdev)) {
729
		netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
R
Ron Mercer 已提交
730 731 732 733 734 735 736 737 738 739 740 741
		return -1;
	}

	ql_mii_enable_scan_mode(qdev);

	return 0;
}

static int ql_mii_read_reg(struct ql3_adapter *qdev, u16 regAddr, u16 *value)
{
	u32 temp;
	struct ql3xxx_port_registers __iomem *port_regs =
742
			qdev->mem_map_registers;
R
Ron Mercer 已提交
743 744 745 746

	ql_mii_disable_scan_mode(qdev);

	if (ql_wait_for_mii_ready(qdev)) {
747
		netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
R
Ron Mercer 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761
		return -1;
	}

	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
			   qdev->PHYAddr | regAddr);

	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
			   (MAC_MII_CONTROL_RC << 16));

	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
			   (MAC_MII_CONTROL_RC << 16) | MAC_MII_CONTROL_RC);

	/* Wait for the read to complete */
	if (ql_wait_for_mii_ready(qdev)) {
762
		netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
R
Ron Mercer 已提交
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
		return -1;
	}

	temp = ql_read_page0_reg(qdev, &port_regs->macMIIMgmtDataReg);
	*value = (u16) temp;

	ql_mii_enable_scan_mode(qdev);

	return 0;
}

static void ql_petbi_reset(struct ql3_adapter *qdev)
{
	ql_mii_write_reg(qdev, PETBI_CONTROL_REG, PETBI_CTRL_SOFT_RESET);
}

static void ql_petbi_start_neg(struct ql3_adapter *qdev)
{
	u16 reg;

	/* Enable Auto-negotiation sense */
	ql_mii_read_reg(qdev, PETBI_TBI_CTRL, &reg);
	reg |= PETBI_TBI_AUTO_SENSE;
	ql_mii_write_reg(qdev, PETBI_TBI_CTRL, reg);

	ql_mii_write_reg(qdev, PETBI_NEG_ADVER,
			 PETBI_NEG_PAUSE | PETBI_NEG_DUPLEX);

	ql_mii_write_reg(qdev, PETBI_CONTROL_REG,
			 PETBI_CTRL_AUTO_NEG | PETBI_CTRL_RESTART_NEG |
			 PETBI_CTRL_FULL_DUPLEX | PETBI_CTRL_SPEED_1000);

}

797
static void ql_petbi_reset_ex(struct ql3_adapter *qdev)
R
Ron Mercer 已提交
798 799
{
	ql_mii_write_reg_ex(qdev, PETBI_CONTROL_REG, PETBI_CTRL_SOFT_RESET,
800
			    PHYAddr[qdev->mac_index]);
R
Ron Mercer 已提交
801 802
}

803
static void ql_petbi_start_neg_ex(struct ql3_adapter *qdev)
R
Ron Mercer 已提交
804 805 806 807
{
	u16 reg;

	/* Enable Auto-negotiation sense */
808
	ql_mii_read_reg_ex(qdev, PETBI_TBI_CTRL, &reg,
809
			   PHYAddr[qdev->mac_index]);
R
Ron Mercer 已提交
810
	reg |= PETBI_TBI_AUTO_SENSE;
811
	ql_mii_write_reg_ex(qdev, PETBI_TBI_CTRL, reg,
812
			    PHYAddr[qdev->mac_index]);
R
Ron Mercer 已提交
813 814

	ql_mii_write_reg_ex(qdev, PETBI_NEG_ADVER,
815
			    PETBI_NEG_PAUSE | PETBI_NEG_DUPLEX,
816
			    PHYAddr[qdev->mac_index]);
R
Ron Mercer 已提交
817 818 819 820

	ql_mii_write_reg_ex(qdev, PETBI_CONTROL_REG,
			    PETBI_CTRL_AUTO_NEG | PETBI_CTRL_RESTART_NEG |
			    PETBI_CTRL_FULL_DUPLEX | PETBI_CTRL_SPEED_1000,
821
			    PHYAddr[qdev->mac_index]);
R
Ron Mercer 已提交
822 823 824 825 826 827 828 829
}

static void ql_petbi_init(struct ql3_adapter *qdev)
{
	ql_petbi_reset(qdev);
	ql_petbi_start_neg(qdev);
}

830
static void ql_petbi_init_ex(struct ql3_adapter *qdev)
R
Ron Mercer 已提交
831
{
832 833
	ql_petbi_reset_ex(qdev);
	ql_petbi_start_neg_ex(qdev);
R
Ron Mercer 已提交
834 835 836 837 838 839 840 841 842 843 844 845
}

static int ql_is_petbi_neg_pause(struct ql3_adapter *qdev)
{
	u16 reg;

	if (ql_mii_read_reg(qdev, PETBI_NEG_PARTNER, &reg) < 0)
		return 0;

	return (reg & PETBI_NEG_PAUSE_MASK) == PETBI_NEG_PAUSE;
}

846 847
static void phyAgereSpecificInit(struct ql3_adapter *qdev, u32 miiAddr)
{
848
	netdev_info(qdev->ndev, "enabling Agere specific PHY\n");
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
	/* power down device bit 11 = 1 */
	ql_mii_write_reg_ex(qdev, 0x00, 0x1940, miiAddr);
	/* enable diagnostic mode bit 2 = 1 */
	ql_mii_write_reg_ex(qdev, 0x12, 0x840e, miiAddr);
	/* 1000MB amplitude adjust (see Agere errata) */
	ql_mii_write_reg_ex(qdev, 0x10, 0x8805, miiAddr);
	/* 1000MB amplitude adjust (see Agere errata) */
	ql_mii_write_reg_ex(qdev, 0x11, 0xf03e, miiAddr);
	/* 100MB amplitude adjust (see Agere errata) */
	ql_mii_write_reg_ex(qdev, 0x10, 0x8806, miiAddr);
	/* 100MB amplitude adjust (see Agere errata) */
	ql_mii_write_reg_ex(qdev, 0x11, 0x003e, miiAddr);
	/* 10MB amplitude adjust (see Agere errata) */
	ql_mii_write_reg_ex(qdev, 0x10, 0x8807, miiAddr);
	/* 10MB amplitude adjust (see Agere errata) */
	ql_mii_write_reg_ex(qdev, 0x11, 0x1f00, miiAddr);
	/* point to hidden reg 0x2806 */
	ql_mii_write_reg_ex(qdev, 0x10, 0x2806, miiAddr);
	/* Write new PHYAD w/bit 5 set */
868 869
	ql_mii_write_reg_ex(qdev, 0x11,
			    0x0020 | (PHYAddr[qdev->mac_index] >> 8), miiAddr);
870
	/*
871 872 873 874 875 876 877 878 879
	 * Disable diagnostic mode bit 2 = 0
	 * Power up device bit 11 = 0
	 * Link up (on) and activity (blink)
	 */
	ql_mii_write_reg(qdev, 0x12, 0x840a);
	ql_mii_write_reg(qdev, 0x00, 0x1140);
	ql_mii_write_reg(qdev, 0x1c, 0xfaf0);
}

880 881
static enum PHY_DEVICE_TYPE getPhyType(struct ql3_adapter *qdev,
				       u16 phyIdReg0, u16 phyIdReg1)
882
{
883
	enum PHY_DEVICE_TYPE result = PHY_TYPE_UNKNOWN;
884
	u32   oui;
885
	u16   model;
886
	int i;
887

888
	if (phyIdReg0 == 0xffff)
889
		return result;
890

891
	if (phyIdReg1 == 0xffff)
892 893 894 895 896 897 898 899
		return result;

	/* oui is split between two registers */
	oui = (phyIdReg0 << 6) | ((phyIdReg1 & PHY_OUI_1_MASK) >> 10);

	model = (phyIdReg1 & PHY_MODEL_MASK) >> 4;

	/* Scan table for this PHY */
900
	for (i = 0; i < MAX_PHY_DEV_TYPES; i++) {
901 902 903 904
		if ((oui == PHY_DEVICES[i].phyIdOUI) &&
		    (model == PHY_DEVICES[i].phyIdModel)) {
			netdev_info(qdev->ndev, "Phy: %s\n",
				    PHY_DEVICES[i].name);
905 906
			result = PHY_DEVICES[i].phyDevice;
			break;
907 908 909 910 911 912
		}
	}

	return result;
}

R
Ron Mercer 已提交
913 914 915 916
static int ql_phy_get_speed(struct ql3_adapter *qdev)
{
	u16 reg;

917 918
	switch (qdev->phyType) {
	case PHY_AGERE_ET1011C: {
919 920 921 922 923 924 925
		if (ql_mii_read_reg(qdev, 0x1A, &reg) < 0)
			return 0;

		reg = (reg >> 8) & 3;
		break;
	}
	default:
926 927
		if (ql_mii_read_reg(qdev, AUX_CONTROL_STATUS, &reg) < 0)
			return 0;
R
Ron Mercer 已提交
928

929
		reg = (((reg & 0x18) >> 3) & 3);
930
	}
R
Ron Mercer 已提交
931

932 933
	switch (reg) {
	case 2:
R
Ron Mercer 已提交
934
		return SPEED_1000;
935
	case 1:
R
Ron Mercer 已提交
936
		return SPEED_100;
937
	case 0:
R
Ron Mercer 已提交
938
		return SPEED_10;
939
	default:
R
Ron Mercer 已提交
940
		return -1;
941
	}
R
Ron Mercer 已提交
942 943 944 945 946 947
}

static int ql_is_full_dup(struct ql3_adapter *qdev)
{
	u16 reg;

948 949
	switch (qdev->phyType) {
	case PHY_AGERE_ET1011C: {
950 951
		if (ql_mii_read_reg(qdev, 0x1A, &reg))
			return 0;
952

953 954 955
		return ((reg & 0x0080) && (reg & 0x1000)) != 0;
	}
	case PHY_VITESSE_VSC8211:
956
	default: {
957 958 959 960 961
		if (ql_mii_read_reg(qdev, AUX_CONTROL_STATUS, &reg) < 0)
			return 0;
		return (reg & PHY_AUX_DUPLEX_STAT) != 0;
	}
	}
R
Ron Mercer 已提交
962 963 964 965 966 967 968 969 970 971 972 973
}

static int ql_is_phy_neg_pause(struct ql3_adapter *qdev)
{
	u16 reg;

	if (ql_mii_read_reg(qdev, PHY_NEG_PARTNER, &reg) < 0)
		return 0;

	return (reg & PHY_NEG_PAUSE) != 0;
}

974 975 976 977 978 979 980 981 982 983
static int PHY_Setup(struct ql3_adapter *qdev)
{
	u16   reg1;
	u16   reg2;
	bool  agereAddrChangeNeeded = false;
	u32 miiAddr = 0;
	int err;

	/*  Determine the PHY we are using by reading the ID's */
	err = ql_mii_read_reg(qdev, PHY_ID_0_REG, &reg1);
984
	if (err != 0) {
985
		netdev_err(qdev->ndev, "Could not read from reg PHY_ID_0_REG\n");
986
		return err;
987 988 989
	}

	err = ql_mii_read_reg(qdev, PHY_ID_1_REG, &reg2);
990
	if (err != 0) {
991
		netdev_err(qdev->ndev, "Could not read from reg PHY_ID_1_REG\n");
992
		return err;
993 994 995 996 997
	}

	/*  Check if we have a Agere PHY */
	if ((reg1 == 0xffff) || (reg2 == 0xffff)) {

998
		/* Determine which MII address we should be using
999
		   determined by the index of the card */
1000
		if (qdev->mac_index == 0)
1001
			miiAddr = MII_AGERE_ADDR_1;
1002
		else
1003
			miiAddr = MII_AGERE_ADDR_2;
1004

1005 1006
		err = ql_mii_read_reg_ex(qdev, PHY_ID_0_REG, &reg1, miiAddr);
		if (err != 0) {
1007 1008
			netdev_err(qdev->ndev,
				   "Could not read from reg PHY_ID_0_REG after Agere detected\n");
1009
			return err;
1010 1011 1012
		}

		err = ql_mii_read_reg_ex(qdev, PHY_ID_1_REG, &reg2, miiAddr);
1013
		if (err != 0) {
1014
			netdev_err(qdev->ndev, "Could not read from reg PHY_ID_1_REG after Agere detected\n");
1015
			return err;
1016
		}
1017

1018
		/*  We need to remember to initialize the Agere PHY */
1019
		agereAddrChangeNeeded = true;
1020 1021 1022 1023 1024 1025 1026 1027
	}

	/*  Determine the particular PHY we have on board to apply
	    PHY specific initializations */
	qdev->phyType = getPhyType(qdev, reg1, reg2);

	if ((qdev->phyType == PHY_AGERE_ET1011C) && agereAddrChangeNeeded) {
		/* need this here so address gets changed */
1028
		phyAgereSpecificInit(qdev, miiAddr);
1029
	} else if (qdev->phyType == PHY_TYPE_UNKNOWN) {
1030
		netdev_err(qdev->ndev, "PHY is unknown\n");
1031 1032 1033 1034 1035 1036
		return -EIO;
	}

	return 0;
}

R
Ron Mercer 已提交
1037 1038 1039 1040 1041 1042
/*
 * Caller holds hw_lock.
 */
static void ql_mac_enable(struct ql3_adapter *qdev, u32 enable)
{
	struct ql3xxx_port_registers __iomem *port_regs =
1043
			qdev->mem_map_registers;
R
Ron Mercer 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	u32 value;

	if (enable)
		value = (MAC_CONFIG_REG_PE | (MAC_CONFIG_REG_PE << 16));
	else
		value = (MAC_CONFIG_REG_PE << 16);

	if (qdev->mac_index)
		ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
	else
		ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
}

/*
 * Caller holds hw_lock.
 */
static void ql_mac_cfg_soft_reset(struct ql3_adapter *qdev, u32 enable)
{
	struct ql3xxx_port_registers __iomem *port_regs =
1063
			qdev->mem_map_registers;
R
Ron Mercer 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
	u32 value;

	if (enable)
		value = (MAC_CONFIG_REG_SR | (MAC_CONFIG_REG_SR << 16));
	else
		value = (MAC_CONFIG_REG_SR << 16);

	if (qdev->mac_index)
		ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
	else
		ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
}

/*
 * Caller holds hw_lock.
 */
static void ql_mac_cfg_gig(struct ql3_adapter *qdev, u32 enable)
{
	struct ql3xxx_port_registers __iomem *port_regs =
1083
			qdev->mem_map_registers;
R
Ron Mercer 已提交
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
	u32 value;

	if (enable)
		value = (MAC_CONFIG_REG_GM | (MAC_CONFIG_REG_GM << 16));
	else
		value = (MAC_CONFIG_REG_GM << 16);

	if (qdev->mac_index)
		ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
	else
		ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
}

/*
 * Caller holds hw_lock.
 */
static void ql_mac_cfg_full_dup(struct ql3_adapter *qdev, u32 enable)
{
	struct ql3xxx_port_registers __iomem *port_regs =
1103
			qdev->mem_map_registers;
R
Ron Mercer 已提交
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
	u32 value;

	if (enable)
		value = (MAC_CONFIG_REG_FD | (MAC_CONFIG_REG_FD << 16));
	else
		value = (MAC_CONFIG_REG_FD << 16);

	if (qdev->mac_index)
		ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
	else
		ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
}

/*
 * Caller holds hw_lock.
 */
static void ql_mac_cfg_pause(struct ql3_adapter *qdev, u32 enable)
{
	struct ql3xxx_port_registers __iomem *port_regs =
1123
			qdev->mem_map_registers;
R
Ron Mercer 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	u32 value;

	if (enable)
		value =
		    ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) |
		     ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) << 16));
	else
		value = ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) << 16);

	if (qdev->mac_index)
		ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
	else
		ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
}

/*
 * Caller holds hw_lock.
 */
static int ql_is_fiber(struct ql3_adapter *qdev)
{
	struct ql3xxx_port_registers __iomem *port_regs =
1145
			qdev->mem_map_registers;
R
Ron Mercer 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
	u32 bitToCheck = 0;
	u32 temp;

	switch (qdev->mac_index) {
	case 0:
		bitToCheck = PORT_STATUS_SM0;
		break;
	case 1:
		bitToCheck = PORT_STATUS_SM1;
		break;
	}

	temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
	return (temp & bitToCheck) != 0;
}

static int ql_is_auto_cfg(struct ql3_adapter *qdev)
{
	u16 reg;
	ql_mii_read_reg(qdev, 0x00, &reg);
	return (reg & 0x1000) != 0;
}

/*
 * Caller holds hw_lock.
 */
static int ql_is_auto_neg_complete(struct ql3_adapter *qdev)
{
	struct ql3xxx_port_registers __iomem *port_regs =
1175
			qdev->mem_map_registers;
R
Ron Mercer 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
	u32 bitToCheck = 0;
	u32 temp;

	switch (qdev->mac_index) {
	case 0:
		bitToCheck = PORT_STATUS_AC0;
		break;
	case 1:
		bitToCheck = PORT_STATUS_AC1;
		break;
	}

	temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
	if (temp & bitToCheck) {
1190
		netif_info(qdev, link, qdev->ndev, "Auto-Negotiate complete\n");
R
Ron Mercer 已提交
1191 1192
		return 1;
	}
1193 1194
	netif_info(qdev, link, qdev->ndev, "Auto-Negotiate incomplete\n");
	return 0;
R
Ron Mercer 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
}

/*
 *  ql_is_neg_pause() returns 1 if pause was negotiated to be on
 */
static int ql_is_neg_pause(struct ql3_adapter *qdev)
{
	if (ql_is_fiber(qdev))
		return ql_is_petbi_neg_pause(qdev);
	else
		return ql_is_phy_neg_pause(qdev);
}

static int ql_auto_neg_error(struct ql3_adapter *qdev)
{
	struct ql3xxx_port_registers __iomem *port_regs =
1211
			qdev->mem_map_registers;
R
Ron Mercer 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	u32 bitToCheck = 0;
	u32 temp;

	switch (qdev->mac_index) {
	case 0:
		bitToCheck = PORT_STATUS_AE0;
		break;
	case 1:
		bitToCheck = PORT_STATUS_AE1;
		break;
	}
	temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
	return (temp & bitToCheck) != 0;
}

static u32 ql_get_link_speed(struct ql3_adapter *qdev)
{
	if (ql_is_fiber(qdev))
		return SPEED_1000;
	else
		return ql_phy_get_speed(qdev);
}

static int ql_is_link_full_dup(struct ql3_adapter *qdev)
{
	if (ql_is_fiber(qdev))
		return 1;
	else
		return ql_is_full_dup(qdev);
}

/*
 * Caller holds hw_lock.
 */
static int ql_link_down_detect(struct ql3_adapter *qdev)
{
	struct ql3xxx_port_registers __iomem *port_regs =
1249
			qdev->mem_map_registers;
R
Ron Mercer 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
	u32 bitToCheck = 0;
	u32 temp;

	switch (qdev->mac_index) {
	case 0:
		bitToCheck = ISP_CONTROL_LINK_DN_0;
		break;
	case 1:
		bitToCheck = ISP_CONTROL_LINK_DN_1;
		break;
	}

	temp =
	    ql_read_common_reg(qdev, &port_regs->CommonRegs.ispControlStatus);
	return (temp & bitToCheck) != 0;
}

/*
 * Caller holds hw_lock.
 */
static int ql_link_down_detect_clear(struct ql3_adapter *qdev)
{
	struct ql3xxx_port_registers __iomem *port_regs =
1273
			qdev->mem_map_registers;
R
Ron Mercer 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299

	switch (qdev->mac_index) {
	case 0:
		ql_write_common_reg(qdev,
				    &port_regs->CommonRegs.ispControlStatus,
				    (ISP_CONTROL_LINK_DN_0) |
				    (ISP_CONTROL_LINK_DN_0 << 16));
		break;

	case 1:
		ql_write_common_reg(qdev,
				    &port_regs->CommonRegs.ispControlStatus,
				    (ISP_CONTROL_LINK_DN_1) |
				    (ISP_CONTROL_LINK_DN_1 << 16));
		break;

	default:
		return 1;
	}

	return 0;
}

/*
 * Caller holds hw_lock.
 */
1300
static int ql_this_adapter_controls_port(struct ql3_adapter *qdev)
R
Ron Mercer 已提交
1301 1302
{
	struct ql3xxx_port_registers __iomem *port_regs =
1303
			qdev->mem_map_registers;
R
Ron Mercer 已提交
1304 1305 1306
	u32 bitToCheck = 0;
	u32 temp;

1307
	switch (qdev->mac_index) {
R
Ron Mercer 已提交
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
	case 0:
		bitToCheck = PORT_STATUS_F1_ENABLED;
		break;
	case 1:
		bitToCheck = PORT_STATUS_F3_ENABLED;
		break;
	default:
		break;
	}

	temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
	if (temp & bitToCheck) {
1320 1321
		netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
			     "not link master\n");
R
Ron Mercer 已提交
1322 1323
		return 0;
	}
1324 1325 1326

	netif_printk(qdev, link, KERN_DEBUG, qdev->ndev, "link master\n");
	return 1;
R
Ron Mercer 已提交
1327 1328
}

1329
static void ql_phy_reset_ex(struct ql3_adapter *qdev)
R
Ron Mercer 已提交
1330
{
1331
	ql_mii_write_reg_ex(qdev, CONTROL_REG, PHY_CTRL_SOFT_RESET,
1332
			    PHYAddr[qdev->mac_index]);
R
Ron Mercer 已提交
1333 1334
}

1335
static void ql_phy_start_neg_ex(struct ql3_adapter *qdev)
R
Ron Mercer 已提交
1336 1337
{
	u16 reg;
1338 1339
	u16 portConfiguration;

1340
	if (qdev->phyType == PHY_AGERE_ET1011C)
1341
		ql_mii_write_reg(qdev, 0x13, 0x0000);
1342
					/* turn off external loopback */
R
Ron Mercer 已提交
1343

1344 1345 1346
	if (qdev->mac_index == 0)
		portConfiguration =
			qdev->nvram_data.macCfg_port0.portConfiguration;
1347
	else
1348 1349
		portConfiguration =
			qdev->nvram_data.macCfg_port1.portConfiguration;
1350 1351 1352

	/*  Some HBA's in the field are set to 0 and they need to
	    be reinterpreted with a default value */
1353
	if (portConfiguration == 0)
1354 1355 1356
		portConfiguration = PORT_CONFIG_DEFAULT;

	/* Set the 1000 advertisements */
1357
	ql_mii_read_reg_ex(qdev, PHY_GIG_CONTROL, &reg,
1358 1359 1360
			   PHYAddr[qdev->mac_index]);
	reg &= ~PHY_GIG_ALL_PARAMS;

1361 1362
	if (portConfiguration & PORT_CONFIG_1000MB_SPEED) {
		if (portConfiguration & PORT_CONFIG_FULL_DUPLEX_ENABLED)
1363
			reg |= PHY_GIG_ADV_1000F;
1364
		else
1365
			reg |= PHY_GIG_ADV_1000H;
1366 1367
	}

1368
	ql_mii_write_reg_ex(qdev, PHY_GIG_CONTROL, reg,
1369 1370 1371 1372 1373 1374 1375
			    PHYAddr[qdev->mac_index]);

	/* Set the 10/100 & pause negotiation advertisements */
	ql_mii_read_reg_ex(qdev, PHY_NEG_ADVER, &reg,
			   PHYAddr[qdev->mac_index]);
	reg &= ~PHY_NEG_ALL_PARAMS;

1376
	if (portConfiguration & PORT_CONFIG_SYM_PAUSE_ENABLED)
1377 1378
		reg |= PHY_NEG_ASY_PAUSE | PHY_NEG_SYM_PAUSE;

1379 1380
	if (portConfiguration & PORT_CONFIG_FULL_DUPLEX_ENABLED) {
		if (portConfiguration & PORT_CONFIG_100MB_SPEED)
1381
			reg |= PHY_NEG_ADV_100F;
1382

1383
		if (portConfiguration & PORT_CONFIG_10MB_SPEED)
1384 1385 1386
			reg |= PHY_NEG_ADV_10F;
	}

1387 1388
	if (portConfiguration & PORT_CONFIG_HALF_DUPLEX_ENABLED) {
		if (portConfiguration & PORT_CONFIG_100MB_SPEED)
1389
			reg |= PHY_NEG_ADV_100H;
1390

1391
		if (portConfiguration & PORT_CONFIG_10MB_SPEED)
1392 1393 1394
			reg |= PHY_NEG_ADV_10H;
	}

1395
	if (portConfiguration & PORT_CONFIG_1000MB_SPEED)
1396
		reg |= 1;
1397

1398
	ql_mii_write_reg_ex(qdev, PHY_NEG_ADVER, reg,
1399
			    PHYAddr[qdev->mac_index]);
R
Ron Mercer 已提交
1400

1401
	ql_mii_read_reg_ex(qdev, CONTROL_REG, &reg, PHYAddr[qdev->mac_index]);
1402 1403

	ql_mii_write_reg_ex(qdev, CONTROL_REG,
1404 1405
			    reg | PHY_CTRL_RESTART_NEG | PHY_CTRL_AUTO_NEG,
			    PHYAddr[qdev->mac_index]);
R
Ron Mercer 已提交
1406 1407
}

1408
static void ql_phy_init_ex(struct ql3_adapter *qdev)
R
Ron Mercer 已提交
1409
{
1410 1411 1412
	ql_phy_reset_ex(qdev);
	PHY_Setup(qdev);
	ql_phy_start_neg_ex(qdev);
R
Ron Mercer 已提交
1413 1414 1415 1416 1417 1418 1419 1420
}

/*
 * Caller holds hw_lock.
 */
static u32 ql_get_link_state(struct ql3_adapter *qdev)
{
	struct ql3xxx_port_registers __iomem *port_regs =
1421
			qdev->mem_map_registers;
R
Ron Mercer 已提交
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
	u32 bitToCheck = 0;
	u32 temp, linkState;

	switch (qdev->mac_index) {
	case 0:
		bitToCheck = PORT_STATUS_UP0;
		break;
	case 1:
		bitToCheck = PORT_STATUS_UP1;
		break;
	}
1433

R
Ron Mercer 已提交
1434
	temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1435
	if (temp & bitToCheck)
R
Ron Mercer 已提交
1436
		linkState = LS_UP;
1437
	else
R
Ron Mercer 已提交
1438
		linkState = LS_DOWN;
1439

R
Ron Mercer 已提交
1440 1441 1442 1443 1444
	return linkState;
}

static int ql_port_start(struct ql3_adapter *qdev)
{
1445
	if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
R
Ron Mercer 已提交
1446
		(QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
1447
			 2) << 7)) {
1448
		netdev_err(qdev->ndev, "Could not get hw lock for GIO\n");
R
Ron Mercer 已提交
1449
		return -1;
1450
	}
R
Ron Mercer 已提交
1451 1452 1453 1454 1455

	if (ql_is_fiber(qdev)) {
		ql_petbi_init(qdev);
	} else {
		/* Copper port */
1456
		ql_phy_init_ex(qdev);
R
Ron Mercer 已提交
1457 1458 1459 1460 1461 1462 1463 1464 1465
	}

	ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
	return 0;
}

static int ql_finish_auto_neg(struct ql3_adapter *qdev)
{

1466
	if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
R
Ron Mercer 已提交
1467 1468 1469 1470 1471
		(QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
			 2) << 7))
		return -1;

	if (!ql_auto_neg_error(qdev)) {
1472
		if (test_bit(QL_LINK_MASTER, &qdev->flags)) {
R
Ron Mercer 已提交
1473
			/* configure the MAC */
1474 1475
			netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
				     "Configuring link\n");
R
Ron Mercer 已提交
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
			ql_mac_cfg_soft_reset(qdev, 1);
			ql_mac_cfg_gig(qdev,
				       (ql_get_link_speed
					(qdev) ==
					SPEED_1000));
			ql_mac_cfg_full_dup(qdev,
					    ql_is_link_full_dup
					    (qdev));
			ql_mac_cfg_pause(qdev,
					 ql_is_neg_pause
					 (qdev));
			ql_mac_cfg_soft_reset(qdev, 0);

			/* enable the MAC */
1490 1491
			netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
				     "Enabling mac\n");
R
Ron Mercer 已提交
1492 1493 1494 1495 1496 1497
			ql_mac_enable(qdev, 1);
		}

		qdev->port_link_state = LS_UP;
		netif_start_queue(qdev->ndev);
		netif_carrier_on(qdev->ndev);
1498 1499 1500 1501
		netif_info(qdev, link, qdev->ndev,
			   "Link is up at %d Mbps, %s duplex\n",
			   ql_get_link_speed(qdev),
			   ql_is_link_full_dup(qdev) ? "full" : "half");
R
Ron Mercer 已提交
1502 1503 1504

	} else {	/* Remote error detected */

1505
		if (test_bit(QL_LINK_MASTER, &qdev->flags)) {
1506 1507
			netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
				     "Remote error detected. Calling ql_port_start()\n");
R
Ron Mercer 已提交
1508 1509 1510 1511 1512
			/*
			 * ql_port_start() is shared code and needs
			 * to lock the PHY on it's own.
			 */
			ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1513
			if (ql_port_start(qdev))	/* Restart port */
R
Ron Mercer 已提交
1514
				return -1;
1515
			return 0;
R
Ron Mercer 已提交
1516 1517 1518 1519 1520 1521
		}
	}
	ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
	return 0;
}

1522
static void ql_link_state_machine_work(struct work_struct *work)
R
Ron Mercer 已提交
1523
{
1524 1525 1526
	struct ql3_adapter *qdev =
		container_of(work, struct ql3_adapter, link_state_work.work);

R
Ron Mercer 已提交
1527 1528 1529 1530 1531 1532 1533
	u32 curr_link_state;
	unsigned long hw_flags;

	spin_lock_irqsave(&qdev->hw_lock, hw_flags);

	curr_link_state = ql_get_link_state(qdev);

1534
	if (test_bit(QL_RESET_ACTIVE, &qdev->flags)) {
1535 1536
		netif_info(qdev, link, qdev->ndev,
			   "Reset in progress, skip processing link state\n");
1537

1538
		spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1539 1540

		/* Restart timer on 2 second interval. */
1541
		mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);
1542

R
Ron Mercer 已提交
1543 1544 1545 1546 1547
		return;
	}

	switch (qdev->port_link_state) {
	default:
1548
		if (test_bit(QL_LINK_MASTER, &qdev->flags))
R
Ron Mercer 已提交
1549 1550 1551 1552 1553 1554
			ql_port_start(qdev);
		qdev->port_link_state = LS_DOWN;
		/* Fall Through */

	case LS_DOWN:
		if (curr_link_state == LS_UP) {
1555
			netif_info(qdev, link, qdev->ndev, "Link is up\n");
R
Ron Mercer 已提交
1556 1557 1558 1559 1560 1561
			if (ql_is_auto_neg_complete(qdev))
				ql_finish_auto_neg(qdev);

			if (qdev->port_link_state == LS_UP)
				ql_link_down_detect_clear(qdev);

1562
			qdev->port_link_state = LS_UP;
R
Ron Mercer 已提交
1563 1564 1565 1566 1567 1568 1569 1570
		}
		break;

	case LS_UP:
		/*
		 * See if the link is currently down or went down and came
		 * back up
		 */
1571
		if (curr_link_state == LS_DOWN) {
1572
			netif_info(qdev, link, qdev->ndev, "Link is down\n");
R
Ron Mercer 已提交
1573 1574
			qdev->port_link_state = LS_DOWN;
		}
1575 1576
		if (ql_link_down_detect(qdev))
			qdev->port_link_state = LS_DOWN;
R
Ron Mercer 已提交
1577 1578 1579
		break;
	}
	spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1580 1581 1582

	/* Restart timer on 2 second interval. */
	mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);
R
Ron Mercer 已提交
1583 1584 1585 1586 1587 1588 1589
}

/*
 * Caller must take hw_lock and QL_PHY_GIO_SEM.
 */
static void ql_get_phy_owner(struct ql3_adapter *qdev)
{
1590
	if (ql_this_adapter_controls_port(qdev))
1591
		set_bit(QL_LINK_MASTER, &qdev->flags);
R
Ron Mercer 已提交
1592
	else
1593
		clear_bit(QL_LINK_MASTER, &qdev->flags);
R
Ron Mercer 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602
}

/*
 * Caller must take hw_lock and QL_PHY_GIO_SEM.
 */
static void ql_init_scan_mode(struct ql3_adapter *qdev)
{
	ql_mii_enable_scan_mode(qdev);

1603
	if (test_bit(QL_LINK_OPTICAL, &qdev->flags)) {
1604 1605
		if (ql_this_adapter_controls_port(qdev))
			ql_petbi_init_ex(qdev);
R
Ron Mercer 已提交
1606
	} else {
1607 1608
		if (ql_this_adapter_controls_port(qdev))
			ql_phy_init_ex(qdev);
R
Ron Mercer 已提交
1609 1610 1611 1612
	}
}

/*
1613 1614 1615 1616
 * MII_Setup needs to be called before taking the PHY out of reset
 * so that the management interface clock speed can be set properly.
 * It would be better if we had a way to disable MDC until after the
 * PHY is out of reset, but we don't have that capability.
R
Ron Mercer 已提交
1617 1618 1619 1620 1621
 */
static int ql_mii_setup(struct ql3_adapter *qdev)
{
	u32 reg;
	struct ql3xxx_port_registers __iomem *port_regs =
1622
			qdev->mem_map_registers;
R
Ron Mercer 已提交
1623

1624
	if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
R
Ron Mercer 已提交
1625 1626 1627 1628
			(QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
			 2) << 7))
		return -1;

1629
	if (qdev->device_id == QL3032_DEVICE_ID)
1630
		ql_write_page0_reg(qdev,
1631 1632
			&port_regs->macMIIMgmtControlReg, 0x0f00000);

R
Ron Mercer 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
	/* Divide 125MHz clock by 28 to meet PHY timing requirements */
	reg = MAC_MII_CONTROL_CLK_SEL_DIV28;

	ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
			   reg | ((MAC_MII_CONTROL_CLK_SEL_MASK) << 16));

	ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
	return 0;
}

1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
#define SUPPORTED_OPTICAL_MODES	(SUPPORTED_1000baseT_Full |	\
				 SUPPORTED_FIBRE |		\
				 SUPPORTED_Autoneg)
#define SUPPORTED_TP_MODES	(SUPPORTED_10baseT_Half |	\
				 SUPPORTED_10baseT_Full |	\
				 SUPPORTED_100baseT_Half |	\
				 SUPPORTED_100baseT_Full |	\
				 SUPPORTED_1000baseT_Half |	\
				 SUPPORTED_1000baseT_Full |	\
				 SUPPORTED_Autoneg |		\
1653
				 SUPPORTED_TP)			\
1654

R
Ron Mercer 已提交
1655 1656
static u32 ql_supported_modes(struct ql3_adapter *qdev)
{
1657 1658
	if (test_bit(QL_LINK_OPTICAL, &qdev->flags))
		return SUPPORTED_OPTICAL_MODES;
R
Ron Mercer 已提交
1659

1660
	return SUPPORTED_TP_MODES;
R
Ron Mercer 已提交
1661 1662 1663 1664 1665 1666 1667
}

static int ql_get_auto_cfg_status(struct ql3_adapter *qdev)
{
	int status;
	unsigned long hw_flags;
	spin_lock_irqsave(&qdev->hw_lock, hw_flags);
1668 1669 1670
	if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
			    (QL_RESOURCE_BITS_BASE_CODE |
			     (qdev->mac_index) * 2) << 7)) {
1671
		spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
R
Ron Mercer 已提交
1672
		return 0;
1673
	}
R
Ron Mercer 已提交
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
	status = ql_is_auto_cfg(qdev);
	ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
	spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
	return status;
}

static u32 ql_get_speed(struct ql3_adapter *qdev)
{
	u32 status;
	unsigned long hw_flags;
	spin_lock_irqsave(&qdev->hw_lock, hw_flags);
1685 1686 1687
	if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
			    (QL_RESOURCE_BITS_BASE_CODE |
			     (qdev->mac_index) * 2) << 7)) {
1688
		spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
R
Ron Mercer 已提交
1689
		return 0;
1690
	}
R
Ron Mercer 已提交
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
	status = ql_get_link_speed(qdev);
	ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
	spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
	return status;
}

static int ql_get_full_dup(struct ql3_adapter *qdev)
{
	int status;
	unsigned long hw_flags;
	spin_lock_irqsave(&qdev->hw_lock, hw_flags);
1702 1703 1704
	if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
			    (QL_RESOURCE_BITS_BASE_CODE |
			     (qdev->mac_index) * 2) << 7)) {
1705
		spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
R
Ron Mercer 已提交
1706
		return 0;
1707
	}
R
Ron Mercer 已提交
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
	status = ql_is_link_full_dup(qdev);
	ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
	spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
	return status;
}

static int ql_get_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
{
	struct ql3_adapter *qdev = netdev_priv(ndev);

	ecmd->transceiver = XCVR_INTERNAL;
	ecmd->supported = ql_supported_modes(qdev);

1721
	if (test_bit(QL_LINK_OPTICAL, &qdev->flags)) {
R
Ron Mercer 已提交
1722 1723 1724 1725 1726 1727 1728
		ecmd->port = PORT_FIBRE;
	} else {
		ecmd->port = PORT_TP;
		ecmd->phy_address = qdev->PHYAddr;
	}
	ecmd->advertising = ql_supported_modes(qdev);
	ecmd->autoneg = ql_get_auto_cfg_status(qdev);
1729
	ethtool_cmd_speed_set(ecmd, ql_get_speed(qdev));
R
Ron Mercer 已提交
1730 1731 1732 1733 1734 1735 1736 1737
	ecmd->duplex = ql_get_full_dup(qdev);
	return 0;
}

static void ql_get_drvinfo(struct net_device *ndev,
			   struct ethtool_drvinfo *drvinfo)
{
	struct ql3_adapter *qdev = netdev_priv(ndev);
1738 1739 1740 1741 1742 1743
	strlcpy(drvinfo->driver, ql3xxx_driver_name, sizeof(drvinfo->driver));
	strlcpy(drvinfo->version, ql3xxx_driver_version,
		sizeof(drvinfo->version));
	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
	strlcpy(drvinfo->bus_info, pci_name(qdev->pdev),
		sizeof(drvinfo->bus_info));
R
Ron Mercer 已提交
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
	drvinfo->regdump_len = 0;
	drvinfo->eedump_len = 0;
}

static u32 ql_get_msglevel(struct net_device *ndev)
{
	struct ql3_adapter *qdev = netdev_priv(ndev);
	return qdev->msg_enable;
}

static void ql_set_msglevel(struct net_device *ndev, u32 value)
{
	struct ql3_adapter *qdev = netdev_priv(ndev);
	qdev->msg_enable = value;
}

1760 1761 1762 1763
static void ql_get_pauseparam(struct net_device *ndev,
			      struct ethtool_pauseparam *pause)
{
	struct ql3_adapter *qdev = netdev_priv(ndev);
1764 1765
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
1766 1767

	u32 reg;
1768
	if (qdev->mac_index == 0)
1769 1770 1771 1772 1773 1774 1775 1776 1777
		reg = ql_read_page0_reg(qdev, &port_regs->mac0ConfigReg);
	else
		reg = ql_read_page0_reg(qdev, &port_regs->mac1ConfigReg);

	pause->autoneg  = ql_get_auto_cfg_status(qdev);
	pause->rx_pause = (reg & MAC_CONFIG_REG_RF) >> 2;
	pause->tx_pause = (reg & MAC_CONFIG_REG_TF) >> 1;
}

1778
static const struct ethtool_ops ql3xxx_ethtool_ops = {
R
Ron Mercer 已提交
1779 1780 1781 1782 1783
	.get_settings = ql_get_settings,
	.get_drvinfo = ql_get_drvinfo,
	.get_link = ethtool_op_get_link,
	.get_msglevel = ql_get_msglevel,
	.set_msglevel = ql_set_msglevel,
1784
	.get_pauseparam = ql_get_pauseparam,
R
Ron Mercer 已提交
1785 1786 1787 1788 1789
};

static int ql_populate_free_queue(struct ql3_adapter *qdev)
{
	struct ql_rcv_buf_cb *lrg_buf_cb = qdev->lrg_buf_free_head;
1790 1791
	dma_addr_t map;
	int err;
R
Ron Mercer 已提交
1792 1793 1794

	while (lrg_buf_cb) {
		if (!lrg_buf_cb->skb) {
1795 1796 1797
			lrg_buf_cb->skb =
				netdev_alloc_skb(qdev->ndev,
						 qdev->lrg_buffer_len);
R
Ron Mercer 已提交
1798
			if (unlikely(!lrg_buf_cb->skb)) {
1799 1800
				netdev_printk(KERN_DEBUG, qdev->ndev,
					      "Failed netdev_alloc_skb()\n");
R
Ron Mercer 已提交
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
				break;
			} else {
				/*
				 * We save some space to copy the ethhdr from
				 * first buffer
				 */
				skb_reserve(lrg_buf_cb->skb, QL_HEADER_SPACE);
				map = pci_map_single(qdev->pdev,
						     lrg_buf_cb->skb->data,
						     qdev->lrg_buffer_len -
						     QL_HEADER_SPACE,
						     PCI_DMA_FROMDEVICE);
1813

1814
				err = pci_dma_mapping_error(qdev->pdev, map);
1815
				if (err) {
1816 1817 1818
					netdev_err(qdev->ndev,
						   "PCI mapping failed with error: %d\n",
						   err);
1819 1820 1821 1822 1823 1824
					dev_kfree_skb(lrg_buf_cb->skb);
					lrg_buf_cb->skb = NULL;
					break;
				}


R
Ron Mercer 已提交
1825
				lrg_buf_cb->buf_phy_addr_low =
1826
					cpu_to_le32(LS_64BITS(map));
R
Ron Mercer 已提交
1827
				lrg_buf_cb->buf_phy_addr_high =
1828
					cpu_to_le32(MS_64BITS(map));
1829 1830
				dma_unmap_addr_set(lrg_buf_cb, mapaddr, map);
				dma_unmap_len_set(lrg_buf_cb, maplen,
R
Ron Mercer 已提交
1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
						  qdev->lrg_buffer_len -
						  QL_HEADER_SPACE);
				--qdev->lrg_buf_skb_check;
				if (!qdev->lrg_buf_skb_check)
					return 1;
			}
		}
		lrg_buf_cb = lrg_buf_cb->next;
	}
	return 0;
}

1843 1844 1845 1846 1847
/*
 * Caller holds hw_lock.
 */
static void ql_update_small_bufq_prod_index(struct ql3_adapter *qdev)
{
1848 1849 1850
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;

1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
	if (qdev->small_buf_release_cnt >= 16) {
		while (qdev->small_buf_release_cnt >= 16) {
			qdev->small_buf_q_producer_index++;

			if (qdev->small_buf_q_producer_index ==
			    NUM_SBUFQ_ENTRIES)
				qdev->small_buf_q_producer_index = 0;
			qdev->small_buf_release_cnt -= 8;
		}
		wmb();
		writel(qdev->small_buf_q_producer_index,
			&port_regs->CommonRegs.rxSmallQProducerIndex);
	}
}

R
Ron Mercer 已提交
1866 1867 1868 1869 1870 1871 1872 1873
/*
 * Caller holds hw_lock.
 */
static void ql_update_lrg_bufq_prod_index(struct ql3_adapter *qdev)
{
	struct bufq_addr_element *lrg_buf_q_ele;
	int i;
	struct ql_rcv_buf_cb *lrg_buf_cb;
1874 1875
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
R
Ron Mercer 已提交
1876

1877 1878
	if ((qdev->lrg_buf_free_count >= 8) &&
	    (qdev->lrg_buf_release_cnt >= 16)) {
R
Ron Mercer 已提交
1879 1880 1881 1882 1883 1884 1885

		if (qdev->lrg_buf_skb_check)
			if (!ql_populate_free_queue(qdev))
				return;

		lrg_buf_q_ele = qdev->lrg_buf_next_free;

1886 1887
		while ((qdev->lrg_buf_release_cnt >= 16) &&
		       (qdev->lrg_buf_free_count >= 8)) {
R
Ron Mercer 已提交
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902

			for (i = 0; i < 8; i++) {
				lrg_buf_cb =
				    ql_get_from_lrg_buf_free_list(qdev);
				lrg_buf_q_ele->addr_high =
				    lrg_buf_cb->buf_phy_addr_high;
				lrg_buf_q_ele->addr_low =
				    lrg_buf_cb->buf_phy_addr_low;
				lrg_buf_q_ele++;

				qdev->lrg_buf_release_cnt--;
			}

			qdev->lrg_buf_q_producer_index++;

1903 1904
			if (qdev->lrg_buf_q_producer_index ==
			    qdev->num_lbufq_entries)
R
Ron Mercer 已提交
1905 1906 1907
				qdev->lrg_buf_q_producer_index = 0;

			if (qdev->lrg_buf_q_producer_index ==
1908
			    (qdev->num_lbufq_entries - 1)) {
R
Ron Mercer 已提交
1909 1910 1911
				lrg_buf_q_ele = qdev->lrg_buf_q_virt_addr;
			}
		}
1912
		wmb();
R
Ron Mercer 已提交
1913
		qdev->lrg_buf_next_free = lrg_buf_q_ele;
1914 1915
		writel(qdev->lrg_buf_q_producer_index,
			&port_regs->CommonRegs.rxLargeQProducerIndex);
R
Ron Mercer 已提交
1916 1917 1918 1919 1920 1921 1922
	}
}

static void ql_process_mac_tx_intr(struct ql3_adapter *qdev,
				   struct ob_mac_iocb_rsp *mac_rsp)
{
	struct ql_tx_buf_cb *tx_cb;
1923
	int i;
1924
	int retval = 0;
R
Ron Mercer 已提交
1925

1926
	if (mac_rsp->flags & OB_MAC_IOCB_RSP_S) {
1927 1928
		netdev_warn(qdev->ndev,
			    "Frame too short but it was padded and sent\n");
1929
	}
1930

R
Ron Mercer 已提交
1931
	tx_cb = &qdev->tx_buf[mac_rsp->transaction_id];
1932 1933

	/*  Check the transmit response flags for any errors */
1934
	if (mac_rsp->flags & OB_MAC_IOCB_RSP_S) {
1935 1936
		netdev_err(qdev->ndev,
			   "Frame too short to be legal, frame not sent\n");
1937

1938
		qdev->ndev->stats.tx_errors++;
1939 1940 1941 1942
		retval = -EIO;
		goto frame_not_sent;
	}

1943
	if (tx_cb->seg_count == 0) {
1944 1945
		netdev_err(qdev->ndev, "tx_cb->seg_count == 0: %d\n",
			   mac_rsp->transaction_id);
1946

1947
		qdev->ndev->stats.tx_errors++;
1948 1949 1950 1951
		retval = -EIO;
		goto invalid_seg_count;
	}

R
Ron Mercer 已提交
1952
	pci_unmap_single(qdev->pdev,
1953 1954
			 dma_unmap_addr(&tx_cb->map[0], mapaddr),
			 dma_unmap_len(&tx_cb->map[0], maplen),
1955 1956 1957 1958 1959
			 PCI_DMA_TODEVICE);
	tx_cb->seg_count--;
	if (tx_cb->seg_count) {
		for (i = 1; i < tx_cb->seg_count; i++) {
			pci_unmap_page(qdev->pdev,
1960
				       dma_unmap_addr(&tx_cb->map[i],
1961
						      mapaddr),
1962
				       dma_unmap_len(&tx_cb->map[i], maplen),
1963 1964 1965
				       PCI_DMA_TODEVICE);
		}
	}
1966 1967
	qdev->ndev->stats.tx_packets++;
	qdev->ndev->stats.tx_bytes += tx_cb->skb->len;
1968 1969

frame_not_sent:
1970
	dev_kfree_skb_irq(tx_cb->skb);
R
Ron Mercer 已提交
1971
	tx_cb->skb = NULL;
1972 1973

invalid_seg_count:
R
Ron Mercer 已提交
1974 1975 1976
	atomic_inc(&qdev->tx_count);
}

1977
static void ql_get_sbuf(struct ql3_adapter *qdev)
R
Ron Mercer 已提交
1978 1979 1980 1981 1982 1983
{
	if (++qdev->small_buf_index == NUM_SMALL_BUFFERS)
		qdev->small_buf_index = 0;
	qdev->small_buf_release_cnt++;
}

1984
static struct ql_rcv_buf_cb *ql_get_lbuf(struct ql3_adapter *qdev)
R
Ron Mercer 已提交
1985 1986 1987 1988 1989 1990
{
	struct ql_rcv_buf_cb *lrg_buf_cb = NULL;
	lrg_buf_cb = &qdev->lrg_buf[qdev->lrg_buf_index];
	qdev->lrg_buf_release_cnt++;
	if (++qdev->lrg_buf_index == qdev->num_large_buffers)
		qdev->lrg_buf_index = 0;
1991
	return lrg_buf_cb;
R
Ron Mercer 已提交
1992 1993
}

1994 1995
/*
 * The difference between 3022 and 3032 for inbound completions:
1996 1997 1998 1999 2000
 * 3022 uses two buffers per completion.  The first buffer contains
 * (some) header info, the second the remainder of the headers plus
 * the data.  For this chip we reserve some space at the top of the
 * receive buffer so that the header info in buffer one can be
 * prepended to the buffer two.  Buffer two is the sent up while
2001
 * buffer one is returned to the hardware to be reused.
2002
 * 3032 receives all of it's data and headers in one buffer for a
2003 2004 2005
 * simpler process.  3032 also supports checksum verification as
 * can be seen in ql_process_macip_rx_intr().
 */
R
Ron Mercer 已提交
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
static void ql_process_mac_rx_intr(struct ql3_adapter *qdev,
				   struct ib_mac_iocb_rsp *ib_mac_rsp_ptr)
{
	struct ql_rcv_buf_cb *lrg_buf_cb1 = NULL;
	struct ql_rcv_buf_cb *lrg_buf_cb2 = NULL;
	struct sk_buff *skb;
	u16 length = le16_to_cpu(ib_mac_rsp_ptr->length);

	/*
	 * Get the inbound address list (small buffer).
	 */
R
Ron Mercer 已提交
2017
	ql_get_sbuf(qdev);
R
Ron Mercer 已提交
2018

R
Ron Mercer 已提交
2019 2020
	if (qdev->device_id == QL3022_DEVICE_ID)
		lrg_buf_cb1 = ql_get_lbuf(qdev);
R
Ron Mercer 已提交
2021 2022

	/* start of second buffer */
R
Ron Mercer 已提交
2023
	lrg_buf_cb2 = ql_get_lbuf(qdev);
R
Ron Mercer 已提交
2024 2025
	skb = lrg_buf_cb2->skb;

2026 2027
	qdev->ndev->stats.rx_packets++;
	qdev->ndev->stats.rx_bytes += length;
R
Ron Mercer 已提交
2028 2029 2030

	skb_put(skb, length);
	pci_unmap_single(qdev->pdev,
2031 2032
			 dma_unmap_addr(lrg_buf_cb2, mapaddr),
			 dma_unmap_len(lrg_buf_cb2, maplen),
R
Ron Mercer 已提交
2033 2034
			 PCI_DMA_FROMDEVICE);
	prefetch(skb->data);
2035
	skb_checksum_none_assert(skb);
R
Ron Mercer 已提交
2036 2037 2038 2039 2040
	skb->protocol = eth_type_trans(skb, qdev->ndev);

	netif_receive_skb(skb);
	lrg_buf_cb2->skb = NULL;

2041 2042
	if (qdev->device_id == QL3022_DEVICE_ID)
		ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb1);
R
Ron Mercer 已提交
2043 2044 2045 2046 2047 2048 2049 2050
	ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb2);
}

static void ql_process_macip_rx_intr(struct ql3_adapter *qdev,
				     struct ib_ip_iocb_rsp *ib_ip_rsp_ptr)
{
	struct ql_rcv_buf_cb *lrg_buf_cb1 = NULL;
	struct ql_rcv_buf_cb *lrg_buf_cb2 = NULL;
2051
	struct sk_buff *skb1 = NULL, *skb2;
R
Ron Mercer 已提交
2052 2053 2054 2055 2056 2057 2058 2059
	struct net_device *ndev = qdev->ndev;
	u16 length = le16_to_cpu(ib_ip_rsp_ptr->length);
	u16 size = 0;

	/*
	 * Get the inbound address list (small buffer).
	 */

R
Ron Mercer 已提交
2060
	ql_get_sbuf(qdev);
R
Ron Mercer 已提交
2061

2062 2063
	if (qdev->device_id == QL3022_DEVICE_ID) {
		/* start of first buffer on 3022 */
R
Ron Mercer 已提交
2064
		lrg_buf_cb1 = ql_get_lbuf(qdev);
2065 2066 2067 2068 2069
		skb1 = lrg_buf_cb1->skb;
		size = ETH_HLEN;
		if (*((u16 *) skb1->data) != 0xFFFF)
			size += VLAN_ETH_HLEN - ETH_HLEN;
	}
R
Ron Mercer 已提交
2070 2071

	/* start of second buffer */
R
Ron Mercer 已提交
2072
	lrg_buf_cb2 = ql_get_lbuf(qdev);
R
Ron Mercer 已提交
2073 2074 2075 2076
	skb2 = lrg_buf_cb2->skb;

	skb_put(skb2, length);	/* Just the second buffer length here. */
	pci_unmap_single(qdev->pdev,
2077 2078
			 dma_unmap_addr(lrg_buf_cb2, mapaddr),
			 dma_unmap_len(lrg_buf_cb2, maplen),
R
Ron Mercer 已提交
2079 2080 2081
			 PCI_DMA_FROMDEVICE);
	prefetch(skb2->data);

2082
	skb_checksum_none_assert(skb2);
2083 2084 2085 2086 2087
	if (qdev->device_id == QL3022_DEVICE_ID) {
		/*
		 * Copy the ethhdr from first buffer to second. This
		 * is necessary for 3022 IP completions.
		 */
2088 2089
		skb_copy_from_linear_data_offset(skb1, VLAN_ID_LEN,
						 skb_push(skb2, size), size);
2090 2091
	} else {
		u16 checksum = le16_to_cpu(ib_ip_rsp_ptr->checksum);
2092 2093 2094
		if (checksum &
			(IB_IP_IOCB_RSP_3032_ICE |
			 IB_IP_IOCB_RSP_3032_CE)) {
2095 2096 2097 2098 2099
			netdev_err(ndev,
				   "%s: Bad checksum for this %s packet, checksum = %x\n",
				   __func__,
				   ((checksum & IB_IP_IOCB_RSP_3032_TCP) ?
				    "TCP" : "UDP"), checksum);
2100 2101 2102
		} else if ((checksum & IB_IP_IOCB_RSP_3032_TCP) ||
				(checksum & IB_IP_IOCB_RSP_3032_UDP &&
				!(checksum & IB_IP_IOCB_RSP_3032_NUC))) {
2103
			skb2->ip_summed = CHECKSUM_UNNECESSARY;
2104
		}
2105
	}
R
Ron Mercer 已提交
2106 2107 2108
	skb2->protocol = eth_type_trans(skb2, qdev->ndev);

	netif_receive_skb(skb2);
2109 2110
	ndev->stats.rx_packets++;
	ndev->stats.rx_bytes += length;
R
Ron Mercer 已提交
2111 2112
	lrg_buf_cb2->skb = NULL;

2113 2114
	if (qdev->device_id == QL3022_DEVICE_ID)
		ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb1);
R
Ron Mercer 已提交
2115 2116 2117 2118 2119 2120 2121 2122
	ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb2);
}

static int ql_tx_rx_clean(struct ql3_adapter *qdev,
			  int *tx_cleaned, int *rx_cleaned, int work_to_do)
{
	struct net_rsp_iocb *net_rsp;
	struct net_device *ndev = qdev->ndev;
2123
	int work_done = 0;
R
Ron Mercer 已提交
2124 2125

	/* While there are entries in the completion queue. */
2126
	while ((le32_to_cpu(*(qdev->prsp_producer_index)) !=
2127
		qdev->rsp_consumer_index) && (work_done < work_to_do)) {
R
Ron Mercer 已提交
2128 2129

		net_rsp = qdev->rsp_current;
2130
		rmb();
2131
		/*
2132 2133
		 * Fix 4032 chip's undocumented "feature" where bit-8 is set
		 * if the inbound completion is for a VLAN.
2134 2135 2136
		 */
		if (qdev->device_id == QL3032_DEVICE_ID)
			net_rsp->opcode &= 0x7f;
R
Ron Mercer 已提交
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146
		switch (net_rsp->opcode) {

		case OPCODE_OB_MAC_IOCB_FN0:
		case OPCODE_OB_MAC_IOCB_FN2:
			ql_process_mac_tx_intr(qdev, (struct ob_mac_iocb_rsp *)
					       net_rsp);
			(*tx_cleaned)++;
			break;

		case OPCODE_IB_MAC_IOCB:
2147
		case OPCODE_IB_3032_MAC_IOCB:
R
Ron Mercer 已提交
2148 2149 2150 2151 2152 2153
			ql_process_mac_rx_intr(qdev, (struct ib_mac_iocb_rsp *)
					       net_rsp);
			(*rx_cleaned)++;
			break;

		case OPCODE_IB_IP_IOCB:
2154
		case OPCODE_IB_3032_IP_IOCB:
R
Ron Mercer 已提交
2155 2156 2157 2158
			ql_process_macip_rx_intr(qdev, (struct ib_ip_iocb_rsp *)
						 net_rsp);
			(*rx_cleaned)++;
			break;
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
		default: {
			u32 *tmp = (u32 *)net_rsp;
			netdev_err(ndev,
				   "Hit default case, not handled!\n"
				   "	dropping the packet, opcode = %x\n"
				   "0x%08lx 0x%08lx 0x%08lx 0x%08lx\n",
				   net_rsp->opcode,
				   (unsigned long int)tmp[0],
				   (unsigned long int)tmp[1],
				   (unsigned long int)tmp[2],
				   (unsigned long int)tmp[3]);
		}
R
Ron Mercer 已提交
2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
		}

		qdev->rsp_consumer_index++;

		if (qdev->rsp_consumer_index == NUM_RSP_Q_ENTRIES) {
			qdev->rsp_consumer_index = 0;
			qdev->rsp_current = qdev->rsp_q_virt_addr;
		} else {
			qdev->rsp_current++;
		}
2181 2182

		work_done = *tx_cleaned + *rx_cleaned;
R
Ron Mercer 已提交
2183 2184
	}

2185
	return work_done;
R
Ron Mercer 已提交
2186 2187
}

2188
static int ql_poll(struct napi_struct *napi, int budget)
R
Ron Mercer 已提交
2189
{
2190
	struct ql3_adapter *qdev = container_of(napi, struct ql3_adapter, napi);
R
Ron Mercer 已提交
2191
	int rx_cleaned = 0, tx_cleaned = 0;
2192
	unsigned long hw_flags;
2193 2194
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
R
Ron Mercer 已提交
2195

2196
	ql_tx_rx_clean(qdev, &tx_cleaned, &rx_cleaned, budget);
R
Ron Mercer 已提交
2197

2198
	if (tx_cleaned + rx_cleaned != budget) {
2199
		spin_lock_irqsave(&qdev->hw_lock, hw_flags);
2200
		__napi_complete(napi);
2201 2202 2203 2204
		ql_update_small_bufq_prod_index(qdev);
		ql_update_lrg_bufq_prod_index(qdev);
		writel(qdev->rsp_consumer_index,
			    &port_regs->CommonRegs.rspQConsumerIndex);
2205 2206
		spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);

R
Ron Mercer 已提交
2207 2208
		ql_enable_interrupts(qdev);
	}
2209
	return tx_cleaned + rx_cleaned;
R
Ron Mercer 已提交
2210 2211
}

2212
static irqreturn_t ql3xxx_isr(int irq, void *dev_id)
R
Ron Mercer 已提交
2213 2214 2215 2216
{

	struct net_device *ndev = dev_id;
	struct ql3_adapter *qdev = netdev_priv(ndev);
2217 2218
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
R
Ron Mercer 已提交
2219 2220 2221 2222
	u32 value;
	int handled = 1;
	u32 var;

2223 2224
	value = ql_read_common_reg_l(qdev,
				     &port_regs->CommonRegs.ispControlStatus);
R
Ron Mercer 已提交
2225 2226 2227 2228 2229 2230 2231

	if (value & (ISP_CONTROL_FE | ISP_CONTROL_RI)) {
		spin_lock(&qdev->adapter_lock);
		netif_stop_queue(qdev->ndev);
		netif_carrier_off(qdev->ndev);
		ql_disable_interrupts(qdev);
		qdev->port_link_state = LS_DOWN;
2232
		set_bit(QL_RESET_ACTIVE, &qdev->flags) ;
R
Ron Mercer 已提交
2233 2234 2235 2236 2237 2238 2239 2240

		if (value & ISP_CONTROL_FE) {
			/*
			 * Chip Fatal Error.
			 */
			var =
			    ql_read_page0_reg_l(qdev,
					      &port_regs->PortFatalErrStatus);
2241 2242 2243
			netdev_warn(ndev,
				    "Resetting chip. PortFatalErrStatus register = 0x%x\n",
				    var);
2244
			set_bit(QL_RESET_START, &qdev->flags) ;
R
Ron Mercer 已提交
2245 2246 2247 2248
		} else {
			/*
			 * Soft Reset Requested.
			 */
2249
			set_bit(QL_RESET_PER_SCSI, &qdev->flags) ;
2250 2251 2252
			netdev_err(ndev,
				   "Another function issued a reset to the chip. ISR value = %x\n",
				   value);
R
Ron Mercer 已提交
2253
		}
D
David Howells 已提交
2254
		queue_delayed_work(qdev->workqueue, &qdev->reset_work, 0);
R
Ron Mercer 已提交
2255 2256
		spin_unlock(&qdev->adapter_lock);
	} else if (value & ISP_IMR_DISABLE_CMPL_INT) {
2257
		ql_disable_interrupts(qdev);
2258
		if (likely(napi_schedule_prep(&qdev->napi)))
2259
			__napi_schedule(&qdev->napi);
2260
	} else
R
Ron Mercer 已提交
2261 2262 2263 2264 2265
		return IRQ_NONE;

	return IRQ_RETVAL(handled);
}

2266
/*
2267 2268 2269 2270 2271
 * Get the total number of segments needed for the given number of fragments.
 * This is necessary because outbound address lists (OAL) will be used when
 * more than two frags are given.  Each address list has 5 addr/len pairs.
 * The 5th pair in each OAL is used to  point to the next OAL if more frags
 * are coming.  That is why the frags:segment count ratio is not linear.
2272
 */
2273
static int ql_get_seg_count(struct ql3_adapter *qdev, unsigned short frags)
2274
{
2275 2276 2277
	if (qdev->device_id == QL3022_DEVICE_ID)
		return 1;

2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
	if (frags <= 2)
		return frags + 1;
	else if (frags <= 6)
		return frags + 2;
	else if (frags <= 10)
		return frags + 3;
	else if (frags <= 14)
		return frags + 4;
	else if (frags <= 18)
		return frags + 5;
2288 2289 2290
	return -1;
}

2291
static void ql_hw_csum_setup(const struct sk_buff *skb,
2292 2293
			     struct ob_mac_iocb_req *mac_iocb_ptr)
{
2294
	const struct iphdr *ip = ip_hdr(skb);
2295

2296 2297
	mac_iocb_ptr->ip_hdr_off = skb_network_offset(skb);
	mac_iocb_ptr->ip_hdr_len = ip->ihl;
2298

2299 2300
	if (ip->protocol == IPPROTO_TCP) {
		mac_iocb_ptr->flags1 |= OB_3032MAC_IOCB_REQ_TC |
2301
			OB_3032MAC_IOCB_REQ_IC;
2302 2303
	} else {
		mac_iocb_ptr->flags1 |= OB_3032MAC_IOCB_REQ_UC |
2304
			OB_3032MAC_IOCB_REQ_IC;
2305
	}
2306

2307 2308 2309
}

/*
2310 2311
 * Map the buffers for this transmit.
 * This will return NETDEV_TX_BUSY or NETDEV_TX_OK based on success.
2312
 */
2313 2314 2315 2316
static int ql_send_map(struct ql3_adapter *qdev,
				struct ob_mac_iocb_req *mac_iocb_ptr,
				struct ql_tx_buf_cb *tx_cb,
				struct sk_buff *skb)
R
Ron Mercer 已提交
2317
{
2318 2319
	struct oal *oal;
	struct oal_entry *oal_entry;
2320
	int len = skb_headlen(skb);
2321 2322 2323
	dma_addr_t map;
	int err;
	int completed_segs, i;
2324 2325
	int seg_cnt, seg = 0;
	int frag_cnt = (int)skb_shinfo(skb)->nr_frags;
R
Ron Mercer 已提交
2326

2327
	seg_cnt = tx_cb->seg_count;
2328 2329 2330
	/*
	 * Map the skb buffer first.
	 */
2331
	map = pci_map_single(qdev->pdev, skb->data, len, PCI_DMA_TODEVICE);
2332

2333
	err = pci_dma_mapping_error(qdev->pdev, map);
2334
	if (err) {
2335 2336
		netdev_err(qdev->ndev, "PCI mapping failed with error: %d\n",
			   err);
2337 2338 2339

		return NETDEV_TX_BUSY;
	}
2340

2341 2342 2343 2344
	oal_entry = (struct oal_entry *)&mac_iocb_ptr->buf_addr0_low;
	oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
	oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
	oal_entry->len = cpu_to_le32(len);
2345 2346
	dma_unmap_addr_set(&tx_cb->map[seg], mapaddr, map);
	dma_unmap_len_set(&tx_cb->map[seg], maplen, len);
2347 2348
	seg++;

2349
	if (seg_cnt == 1) {
2350
		/* Terminate the last segment. */
2351
		oal_entry->len |= cpu_to_le32(OAL_LAST_ENTRY);
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
		return NETDEV_TX_OK;
	}
	oal = tx_cb->oal;
	for (completed_segs = 0;
	     completed_segs < frag_cnt;
	     completed_segs++, seg++) {
		skb_frag_t *frag = &skb_shinfo(skb)->frags[completed_segs];
		oal_entry++;
		/*
		 * Check for continuation requirements.
		 * It's strange but necessary.
		 * Continuation entry points to outbound address list.
		 */
		if ((seg == 2 && seg_cnt > 3) ||
		    (seg == 7 && seg_cnt > 8) ||
		    (seg == 12 && seg_cnt > 13) ||
		    (seg == 17 && seg_cnt > 18)) {
			map = pci_map_single(qdev->pdev, oal,
					     sizeof(struct oal),
					     PCI_DMA_TODEVICE);
2372

2373
			err = pci_dma_mapping_error(qdev->pdev, map);
2374
			if (err) {
2375
				netdev_err(qdev->ndev,
2376
					   "PCI mapping outbound address list with error: %d\n",
2377
					   err);
2378 2379 2380
				goto map_error;
			}

2381 2382
			oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
			oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
2383 2384
			oal_entry->len = cpu_to_le32(sizeof(struct oal) |
						     OAL_CONT_ENTRY);
2385 2386
			dma_unmap_addr_set(&tx_cb->map[seg], mapaddr, map);
			dma_unmap_len_set(&tx_cb->map[seg], maplen,
2387 2388 2389 2390
					  sizeof(struct oal));
			oal_entry = (struct oal_entry *)oal;
			oal++;
			seg++;
2391
		}
2392

E
Eric Dumazet 已提交
2393
		map = skb_frag_dma_map(&qdev->pdev->dev, frag, 0, skb_frag_size(frag),
2394
				       DMA_TO_DEVICE);
2395

2396
		err = dma_mapping_error(&qdev->pdev->dev, map);
2397 2398 2399 2400 2401 2402 2403 2404 2405
		if (err) {
			netdev_err(qdev->ndev,
				   "PCI mapping frags failed with error: %d\n",
				   err);
			goto map_error;
		}

		oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
		oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
E
Eric Dumazet 已提交
2406
		oal_entry->len = cpu_to_le32(skb_frag_size(frag));
2407
		dma_unmap_addr_set(&tx_cb->map[seg], mapaddr, map);
E
Eric Dumazet 已提交
2408
		dma_unmap_len_set(&tx_cb->map[seg], maplen, skb_frag_size(frag));
2409 2410 2411
		}
	/* Terminate the last segment. */
	oal_entry->len |= cpu_to_le32(OAL_LAST_ENTRY);
2412
	return NETDEV_TX_OK;
2413 2414 2415

map_error:
	/* A PCI mapping failed and now we will need to back out
2416
	 * We need to traverse through the oal's and associated pages which
2417 2418
	 * have been mapped and now we must unmap them to clean up properly
	 */
2419

2420 2421 2422
	seg = 1;
	oal_entry = (struct oal_entry *)&mac_iocb_ptr->buf_addr0_low;
	oal = tx_cb->oal;
2423
	for (i = 0; i < completed_segs; i++, seg++) {
2424 2425
		oal_entry++;

2426 2427 2428 2429 2430 2431 2432 2433 2434
		/*
		 * Check for continuation requirements.
		 * It's strange but necessary.
		 */

		if ((seg == 2 && seg_cnt > 3) ||
		    (seg == 7 && seg_cnt > 8) ||
		    (seg == 12 && seg_cnt > 13) ||
		    (seg == 17 && seg_cnt > 18)) {
2435
			pci_unmap_single(qdev->pdev,
2436 2437
				dma_unmap_addr(&tx_cb->map[seg], mapaddr),
				dma_unmap_len(&tx_cb->map[seg], maplen),
2438 2439 2440 2441 2442 2443
				 PCI_DMA_TODEVICE);
			oal++;
			seg++;
		}

		pci_unmap_page(qdev->pdev,
2444 2445
			       dma_unmap_addr(&tx_cb->map[seg], mapaddr),
			       dma_unmap_len(&tx_cb->map[seg], maplen),
2446 2447 2448 2449
			       PCI_DMA_TODEVICE);
	}

	pci_unmap_single(qdev->pdev,
2450 2451
			 dma_unmap_addr(&tx_cb->map[0], mapaddr),
			 dma_unmap_addr(&tx_cb->map[0], maplen),
2452 2453 2454 2455
			 PCI_DMA_TODEVICE);

	return NETDEV_TX_BUSY;

2456 2457 2458 2459 2460 2461
}

/*
 * The difference between 3022 and 3032 sends:
 * 3022 only supports a simple single segment transmission.
 * 3032 supports checksumming and scatter/gather lists (fragments).
2462 2463 2464
 * The 3032 supports sglists by using the 3 addr/len pairs (ALP)
 * in the IOCB plus a chain of outbound address lists (OAL) that
 * each contain 5 ALPs.  The last ALP of the IOCB (3rd) or OAL (5th)
2465
 * will be used to point to an OAL when more ALP entries are required.
2466
 * The IOCB is always the top of the chain followed by one or more
2467 2468
 * OALs (when necessary).
 */
2469 2470
static netdev_tx_t ql3xxx_send(struct sk_buff *skb,
			       struct net_device *ndev)
2471
{
2472
	struct ql3_adapter *qdev = netdev_priv(ndev);
2473 2474
	struct ql3xxx_port_registers __iomem *port_regs =
			qdev->mem_map_registers;
2475 2476 2477 2478
	struct ql_tx_buf_cb *tx_cb;
	u32 tot_len = skb->len;
	struct ob_mac_iocb_req *mac_iocb_ptr;

2479
	if (unlikely(atomic_read(&qdev->tx_count) < 2))
2480
		return NETDEV_TX_BUSY;
2481

2482 2483 2484 2485
	tx_cb = &qdev->tx_buf[qdev->req_producer_index];
	tx_cb->seg_count = ql_get_seg_count(qdev,
					     skb_shinfo(skb)->nr_frags);
	if (tx_cb->seg_count == -1) {
2486
		netdev_err(ndev, "%s: invalid segment count!\n", __func__);
2487 2488
		return NETDEV_TX_OK;
	}
2489

2490
	mac_iocb_ptr = tx_cb->queue_entry;
2491
	memset((void *)mac_iocb_ptr, 0, sizeof(struct ob_mac_iocb_req));
2492 2493 2494 2495 2496 2497
	mac_iocb_ptr->opcode = qdev->mac_ob_opcode;
	mac_iocb_ptr->flags = OB_MAC_IOCB_REQ_X;
	mac_iocb_ptr->flags |= qdev->mb_bit_mask;
	mac_iocb_ptr->transaction_id = qdev->req_producer_index;
	mac_iocb_ptr->data_len = cpu_to_le16((u16) tot_len);
	tx_cb->skb = skb;
2498 2499
	if (qdev->device_id == QL3032_DEVICE_ID &&
	    skb->ip_summed == CHECKSUM_PARTIAL)
2500
		ql_hw_csum_setup(skb, mac_iocb_ptr);
2501

2502
	if (ql_send_map(qdev, mac_iocb_ptr, tx_cb, skb) != NETDEV_TX_OK) {
2503
		netdev_err(ndev, "%s: Could not map the segments!\n", __func__);
2504 2505
		return NETDEV_TX_BUSY;
	}
2506

2507
	wmb();
R
Ron Mercer 已提交
2508 2509 2510 2511 2512
	qdev->req_producer_index++;
	if (qdev->req_producer_index == NUM_REQ_Q_ENTRIES)
		qdev->req_producer_index = 0;
	wmb();
	ql_write_common_reg_l(qdev,
A
Al Viro 已提交
2513
			    &port_regs->CommonRegs.reqQProducerIndex,
R
Ron Mercer 已提交
2514 2515
			    qdev->req_producer_index);

2516 2517 2518
	netif_printk(qdev, tx_queued, KERN_DEBUG, ndev,
		     "tx queued, slot %d, len %d\n",
		     qdev->req_producer_index, skb->len);
R
Ron Mercer 已提交
2519

2520
	atomic_dec(&qdev->tx_count);
R
Ron Mercer 已提交
2521 2522
	return NETDEV_TX_OK;
}
2523

R
Ron Mercer 已提交
2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
static int ql_alloc_net_req_rsp_queues(struct ql3_adapter *qdev)
{
	qdev->req_q_size =
	    (u32) (NUM_REQ_Q_ENTRIES * sizeof(struct ob_mac_iocb_req));

	qdev->req_q_virt_addr =
	    pci_alloc_consistent(qdev->pdev,
				 (size_t) qdev->req_q_size,
				 &qdev->req_q_phy_addr);

	if ((qdev->req_q_virt_addr == NULL) ||
	    LS_64BITS(qdev->req_q_phy_addr) & (qdev->req_q_size - 1)) {
2536
		netdev_err(qdev->ndev, "reqQ failed\n");
R
Ron Mercer 已提交
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548
		return -ENOMEM;
	}

	qdev->rsp_q_size = NUM_RSP_Q_ENTRIES * sizeof(struct net_rsp_iocb);

	qdev->rsp_q_virt_addr =
	    pci_alloc_consistent(qdev->pdev,
				 (size_t) qdev->rsp_q_size,
				 &qdev->rsp_q_phy_addr);

	if ((qdev->rsp_q_virt_addr == NULL) ||
	    LS_64BITS(qdev->rsp_q_phy_addr) & (qdev->rsp_q_size - 1)) {
2549
		netdev_err(qdev->ndev, "rspQ allocation failed\n");
R
Ron Mercer 已提交
2550 2551 2552 2553 2554 2555
		pci_free_consistent(qdev->pdev, (size_t) qdev->req_q_size,
				    qdev->req_q_virt_addr,
				    qdev->req_q_phy_addr);
		return -ENOMEM;
	}

2556
	set_bit(QL_ALLOC_REQ_RSP_Q_DONE, &qdev->flags);
R
Ron Mercer 已提交
2557 2558 2559 2560 2561 2562

	return 0;
}

static void ql_free_net_req_rsp_queues(struct ql3_adapter *qdev)
{
2563
	if (!test_bit(QL_ALLOC_REQ_RSP_Q_DONE, &qdev->flags)) {
2564
		netdev_info(qdev->ndev, "Already done\n");
R
Ron Mercer 已提交
2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
		return;
	}

	pci_free_consistent(qdev->pdev,
			    qdev->req_q_size,
			    qdev->req_q_virt_addr, qdev->req_q_phy_addr);

	qdev->req_q_virt_addr = NULL;

	pci_free_consistent(qdev->pdev,
			    qdev->rsp_q_size,
			    qdev->rsp_q_virt_addr, qdev->rsp_q_phy_addr);

	qdev->rsp_q_virt_addr = NULL;

2580
	clear_bit(QL_ALLOC_REQ_RSP_Q_DONE, &qdev->flags);
R
Ron Mercer 已提交
2581 2582 2583 2584 2585 2586
}

static int ql_alloc_buffer_queues(struct ql3_adapter *qdev)
{
	/* Create Large Buffer Queue */
	qdev->lrg_buf_q_size =
2587
		qdev->num_lbufq_entries * sizeof(struct lrg_buf_q_entry);
R
Ron Mercer 已提交
2588 2589 2590 2591 2592
	if (qdev->lrg_buf_q_size < PAGE_SIZE)
		qdev->lrg_buf_q_alloc_size = PAGE_SIZE;
	else
		qdev->lrg_buf_q_alloc_size = qdev->lrg_buf_q_size * 2;

2593 2594 2595
	qdev->lrg_buf =
		kmalloc(qdev->num_large_buffers * sizeof(struct ql_rcv_buf_cb),
			GFP_KERNEL);
2596
	if (qdev->lrg_buf == NULL) {
2597
		netdev_err(qdev->ndev, "qdev->lrg_buf alloc failed\n");
2598 2599
		return -ENOMEM;
	}
2600

R
Ron Mercer 已提交
2601
	qdev->lrg_buf_q_alloc_virt_addr =
2602 2603 2604
		pci_alloc_consistent(qdev->pdev,
				     qdev->lrg_buf_q_alloc_size,
				     &qdev->lrg_buf_q_alloc_phy_addr);
R
Ron Mercer 已提交
2605 2606

	if (qdev->lrg_buf_q_alloc_virt_addr == NULL) {
2607
		netdev_err(qdev->ndev, "lBufQ failed\n");
R
Ron Mercer 已提交
2608 2609 2610 2611 2612 2613 2614
		return -ENOMEM;
	}
	qdev->lrg_buf_q_virt_addr = qdev->lrg_buf_q_alloc_virt_addr;
	qdev->lrg_buf_q_phy_addr = qdev->lrg_buf_q_alloc_phy_addr;

	/* Create Small Buffer Queue */
	qdev->small_buf_q_size =
2615
		NUM_SBUFQ_ENTRIES * sizeof(struct lrg_buf_q_entry);
R
Ron Mercer 已提交
2616 2617 2618 2619 2620 2621
	if (qdev->small_buf_q_size < PAGE_SIZE)
		qdev->small_buf_q_alloc_size = PAGE_SIZE;
	else
		qdev->small_buf_q_alloc_size = qdev->small_buf_q_size * 2;

	qdev->small_buf_q_alloc_virt_addr =
2622 2623 2624
		pci_alloc_consistent(qdev->pdev,
				     qdev->small_buf_q_alloc_size,
				     &qdev->small_buf_q_alloc_phy_addr);
R
Ron Mercer 已提交
2625 2626

	if (qdev->small_buf_q_alloc_virt_addr == NULL) {
2627
		netdev_err(qdev->ndev, "Small Buffer Queue allocation failed\n");
R
Ron Mercer 已提交
2628 2629 2630 2631 2632 2633 2634 2635
		pci_free_consistent(qdev->pdev, qdev->lrg_buf_q_alloc_size,
				    qdev->lrg_buf_q_alloc_virt_addr,
				    qdev->lrg_buf_q_alloc_phy_addr);
		return -ENOMEM;
	}

	qdev->small_buf_q_virt_addr = qdev->small_buf_q_alloc_virt_addr;
	qdev->small_buf_q_phy_addr = qdev->small_buf_q_alloc_phy_addr;
2636
	set_bit(QL_ALLOC_BUFQS_DONE, &qdev->flags);
R
Ron Mercer 已提交
2637 2638 2639 2640 2641
	return 0;
}

static void ql_free_buffer_queues(struct ql3_adapter *qdev)
{
2642
	if (!test_bit(QL_ALLOC_BUFQS_DONE, &qdev->flags)) {
2643
		netdev_info(qdev->ndev, "Already done\n");
R
Ron Mercer 已提交
2644 2645
		return;
	}
2646
	kfree(qdev->lrg_buf);
R
Ron Mercer 已提交
2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660
	pci_free_consistent(qdev->pdev,
			    qdev->lrg_buf_q_alloc_size,
			    qdev->lrg_buf_q_alloc_virt_addr,
			    qdev->lrg_buf_q_alloc_phy_addr);

	qdev->lrg_buf_q_virt_addr = NULL;

	pci_free_consistent(qdev->pdev,
			    qdev->small_buf_q_alloc_size,
			    qdev->small_buf_q_alloc_virt_addr,
			    qdev->small_buf_q_alloc_phy_addr);

	qdev->small_buf_q_virt_addr = NULL;

2661
	clear_bit(QL_ALLOC_BUFQS_DONE, &qdev->flags);
R
Ron Mercer 已提交
2662 2663 2664 2665 2666 2667 2668 2669 2670
}

static int ql_alloc_small_buffers(struct ql3_adapter *qdev)
{
	int i;
	struct bufq_addr_element *small_buf_q_entry;

	/* Currently we allocate on one of memory and use it for smallbuffers */
	qdev->small_buf_total_size =
2671 2672
		(QL_ADDR_ELE_PER_BUFQ_ENTRY * NUM_SBUFQ_ENTRIES *
		 QL_SMALL_BUFFER_SIZE);
R
Ron Mercer 已提交
2673 2674

	qdev->small_buf_virt_addr =
2675 2676 2677
		pci_alloc_consistent(qdev->pdev,
				     qdev->small_buf_total_size,
				     &qdev->small_buf_phy_addr);
R
Ron Mercer 已提交
2678 2679

	if (qdev->small_buf_virt_addr == NULL) {
2680
		netdev_err(qdev->ndev, "Failed to get small buffer memory\n");
R
Ron Mercer 已提交
2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698
		return -ENOMEM;
	}

	qdev->small_buf_phy_addr_low = LS_64BITS(qdev->small_buf_phy_addr);
	qdev->small_buf_phy_addr_high = MS_64BITS(qdev->small_buf_phy_addr);

	small_buf_q_entry = qdev->small_buf_q_virt_addr;

	/* Initialize the small buffer queue. */
	for (i = 0; i < (QL_ADDR_ELE_PER_BUFQ_ENTRY * NUM_SBUFQ_ENTRIES); i++) {
		small_buf_q_entry->addr_high =
		    cpu_to_le32(qdev->small_buf_phy_addr_high);
		small_buf_q_entry->addr_low =
		    cpu_to_le32(qdev->small_buf_phy_addr_low +
				(i * QL_SMALL_BUFFER_SIZE));
		small_buf_q_entry++;
	}
	qdev->small_buf_index = 0;
2699
	set_bit(QL_ALLOC_SMALL_BUF_DONE, &qdev->flags);
R
Ron Mercer 已提交
2700 2701 2702 2703 2704
	return 0;
}

static void ql_free_small_buffers(struct ql3_adapter *qdev)
{
2705
	if (!test_bit(QL_ALLOC_SMALL_BUF_DONE, &qdev->flags)) {
2706
		netdev_info(qdev->ndev, "Already done\n");
R
Ron Mercer 已提交
2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723
		return;
	}
	if (qdev->small_buf_virt_addr != NULL) {
		pci_free_consistent(qdev->pdev,
				    qdev->small_buf_total_size,
				    qdev->small_buf_virt_addr,
				    qdev->small_buf_phy_addr);

		qdev->small_buf_virt_addr = NULL;
	}
}

static void ql_free_large_buffers(struct ql3_adapter *qdev)
{
	int i = 0;
	struct ql_rcv_buf_cb *lrg_buf_cb;

2724
	for (i = 0; i < qdev->num_large_buffers; i++) {
R
Ron Mercer 已提交
2725 2726 2727 2728
		lrg_buf_cb = &qdev->lrg_buf[i];
		if (lrg_buf_cb->skb) {
			dev_kfree_skb(lrg_buf_cb->skb);
			pci_unmap_single(qdev->pdev,
2729 2730
					 dma_unmap_addr(lrg_buf_cb, mapaddr),
					 dma_unmap_len(lrg_buf_cb, maplen),
R
Ron Mercer 已提交
2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
					 PCI_DMA_FROMDEVICE);
			memset(lrg_buf_cb, 0, sizeof(struct ql_rcv_buf_cb));
		} else {
			break;
		}
	}
}

static void ql_init_large_buffers(struct ql3_adapter *qdev)
{
	int i;
	struct ql_rcv_buf_cb *lrg_buf_cb;
	struct bufq_addr_element *buf_addr_ele = qdev->lrg_buf_q_virt_addr;

2745
	for (i = 0; i < qdev->num_large_buffers; i++) {
R
Ron Mercer 已提交
2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759
		lrg_buf_cb = &qdev->lrg_buf[i];
		buf_addr_ele->addr_high = lrg_buf_cb->buf_phy_addr_high;
		buf_addr_ele->addr_low = lrg_buf_cb->buf_phy_addr_low;
		buf_addr_ele++;
	}
	qdev->lrg_buf_index = 0;
	qdev->lrg_buf_skb_check = 0;
}

static int ql_alloc_large_buffers(struct ql3_adapter *qdev)
{
	int i;
	struct ql_rcv_buf_cb *lrg_buf_cb;
	struct sk_buff *skb;
2760 2761
	dma_addr_t map;
	int err;
R
Ron Mercer 已提交
2762

2763
	for (i = 0; i < qdev->num_large_buffers; i++) {
2764 2765
		skb = netdev_alloc_skb(qdev->ndev,
				       qdev->lrg_buffer_len);
R
Ron Mercer 已提交
2766 2767
		if (unlikely(!skb)) {
			/* Better luck next round */
2768 2769 2770
			netdev_err(qdev->ndev,
				   "large buff alloc failed for %d bytes at index %d\n",
				   qdev->lrg_buffer_len * 2, i);
R
Ron Mercer 已提交
2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788
			ql_free_large_buffers(qdev);
			return -ENOMEM;
		} else {

			lrg_buf_cb = &qdev->lrg_buf[i];
			memset(lrg_buf_cb, 0, sizeof(struct ql_rcv_buf_cb));
			lrg_buf_cb->index = i;
			lrg_buf_cb->skb = skb;
			/*
			 * We save some space to copy the ethhdr from first
			 * buffer
			 */
			skb_reserve(skb, QL_HEADER_SPACE);
			map = pci_map_single(qdev->pdev,
					     skb->data,
					     qdev->lrg_buffer_len -
					     QL_HEADER_SPACE,
					     PCI_DMA_FROMDEVICE);
2789

2790
			err = pci_dma_mapping_error(qdev->pdev, map);
2791
			if (err) {
2792 2793 2794
				netdev_err(qdev->ndev,
					   "PCI mapping failed with error: %d\n",
					   err);
2795 2796 2797 2798
				ql_free_large_buffers(qdev);
				return -ENOMEM;
			}

2799 2800
			dma_unmap_addr_set(lrg_buf_cb, mapaddr, map);
			dma_unmap_len_set(lrg_buf_cb, maplen,
R
Ron Mercer 已提交
2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811
					  qdev->lrg_buffer_len -
					  QL_HEADER_SPACE);
			lrg_buf_cb->buf_phy_addr_low =
			    cpu_to_le32(LS_64BITS(map));
			lrg_buf_cb->buf_phy_addr_high =
			    cpu_to_le32(MS_64BITS(map));
		}
	}
	return 0;
}

2812 2813 2814 2815 2816 2817 2818
static void ql_free_send_free_list(struct ql3_adapter *qdev)
{
	struct ql_tx_buf_cb *tx_cb;
	int i;

	tx_cb = &qdev->tx_buf[0];
	for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
2819 2820
		kfree(tx_cb->oal);
		tx_cb->oal = NULL;
2821 2822 2823 2824 2825
		tx_cb++;
	}
}

static int ql_create_send_free_list(struct ql3_adapter *qdev)
R
Ron Mercer 已提交
2826 2827 2828
{
	struct ql_tx_buf_cb *tx_cb;
	int i;
2829
	struct ob_mac_iocb_req *req_q_curr = qdev->req_q_virt_addr;
R
Ron Mercer 已提交
2830 2831 2832

	/* Create free list of transmit buffers */
	for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
2833

R
Ron Mercer 已提交
2834 2835 2836 2837
		tx_cb = &qdev->tx_buf[i];
		tx_cb->skb = NULL;
		tx_cb->queue_entry = req_q_curr;
		req_q_curr++;
2838 2839 2840
		tx_cb->oal = kmalloc(512, GFP_KERNEL);
		if (tx_cb->oal == NULL)
			return -1;
R
Ron Mercer 已提交
2841
	}
2842
	return 0;
R
Ron Mercer 已提交
2843 2844 2845 2846
}

static int ql_alloc_mem_resources(struct ql3_adapter *qdev)
{
2847 2848
	if (qdev->ndev->mtu == NORMAL_MTU_SIZE) {
		qdev->num_lbufq_entries = NUM_LBUFQ_ENTRIES;
R
Ron Mercer 已提交
2849
		qdev->lrg_buffer_len = NORMAL_MTU_SIZE;
2850
	} else if (qdev->ndev->mtu == JUMBO_MTU_SIZE) {
2851 2852 2853 2854
		/*
		 * Bigger buffers, so less of them.
		 */
		qdev->num_lbufq_entries = JUMBO_NUM_LBUFQ_ENTRIES;
R
Ron Mercer 已提交
2855 2856
		qdev->lrg_buffer_len = JUMBO_MTU_SIZE;
	} else {
2857 2858
		netdev_err(qdev->ndev, "Invalid mtu size: %d.  Only %d and %d are accepted.\n",
			   qdev->ndev->mtu, NORMAL_MTU_SIZE, JUMBO_MTU_SIZE);
R
Ron Mercer 已提交
2859 2860
		return -ENOMEM;
	}
2861 2862
	qdev->num_large_buffers =
		qdev->num_lbufq_entries * QL_ADDR_ELE_PER_BUFQ_ENTRY;
R
Ron Mercer 已提交
2863 2864
	qdev->lrg_buffer_len += VLAN_ETH_HLEN + VLAN_ID_LEN + QL_HEADER_SPACE;
	qdev->max_frame_size =
2865
		(qdev->lrg_buffer_len - QL_HEADER_SPACE) + ETHERNET_CRC_SIZE;
R
Ron Mercer 已提交
2866 2867 2868 2869 2870 2871 2872

	/*
	 * First allocate a page of shared memory and use it for shadow
	 * locations of Network Request Queue Consumer Address Register and
	 * Network Completion Queue Producer Index Register
	 */
	qdev->shadow_reg_virt_addr =
2873 2874
		pci_alloc_consistent(qdev->pdev,
				     PAGE_SIZE, &qdev->shadow_reg_phy_addr);
R
Ron Mercer 已提交
2875 2876

	if (qdev->shadow_reg_virt_addr != NULL) {
2877
		qdev->preq_consumer_index = qdev->shadow_reg_virt_addr;
R
Ron Mercer 已提交
2878
		qdev->req_consumer_index_phy_addr_high =
2879
			MS_64BITS(qdev->shadow_reg_phy_addr);
R
Ron Mercer 已提交
2880
		qdev->req_consumer_index_phy_addr_low =
2881
			LS_64BITS(qdev->shadow_reg_phy_addr);
R
Ron Mercer 已提交
2882 2883

		qdev->prsp_producer_index =
2884
			(__le32 *) (((u8 *) qdev->preq_consumer_index) + 8);
R
Ron Mercer 已提交
2885
		qdev->rsp_producer_index_phy_addr_high =
2886
			qdev->req_consumer_index_phy_addr_high;
R
Ron Mercer 已提交
2887
		qdev->rsp_producer_index_phy_addr_low =
2888
			qdev->req_consumer_index_phy_addr_low + 8;
R
Ron Mercer 已提交
2889
	} else {
2890
		netdev_err(qdev->ndev, "shadowReg Alloc failed\n");
R
Ron Mercer 已提交
2891 2892 2893 2894
		return -ENOMEM;
	}

	if (ql_alloc_net_req_rsp_queues(qdev) != 0) {
2895
		netdev_err(qdev->ndev, "ql_alloc_net_req_rsp_queues failed\n");
R
Ron Mercer 已提交
2896 2897 2898 2899
		goto err_req_rsp;
	}

	if (ql_alloc_buffer_queues(qdev) != 0) {
2900
		netdev_err(qdev->ndev, "ql_alloc_buffer_queues failed\n");
R
Ron Mercer 已提交
2901 2902 2903 2904
		goto err_buffer_queues;
	}

	if (ql_alloc_small_buffers(qdev) != 0) {
2905
		netdev_err(qdev->ndev, "ql_alloc_small_buffers failed\n");
R
Ron Mercer 已提交
2906 2907 2908 2909
		goto err_small_buffers;
	}

	if (ql_alloc_large_buffers(qdev) != 0) {
2910
		netdev_err(qdev->ndev, "ql_alloc_large_buffers failed\n");
R
Ron Mercer 已提交
2911 2912 2913 2914 2915
		goto err_small_buffers;
	}

	/* Initialize the large buffer queue. */
	ql_init_large_buffers(qdev);
2916 2917
	if (ql_create_send_free_list(qdev))
		goto err_free_list;
R
Ron Mercer 已提交
2918 2919 2920 2921

	qdev->rsp_current = qdev->rsp_q_virt_addr;

	return 0;
2922 2923
err_free_list:
	ql_free_send_free_list(qdev);
R
Ron Mercer 已提交
2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938
err_small_buffers:
	ql_free_buffer_queues(qdev);
err_buffer_queues:
	ql_free_net_req_rsp_queues(qdev);
err_req_rsp:
	pci_free_consistent(qdev->pdev,
			    PAGE_SIZE,
			    qdev->shadow_reg_virt_addr,
			    qdev->shadow_reg_phy_addr);

	return -ENOMEM;
}

static void ql_free_mem_resources(struct ql3_adapter *qdev)
{
2939
	ql_free_send_free_list(qdev);
R
Ron Mercer 已提交
2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954
	ql_free_large_buffers(qdev);
	ql_free_small_buffers(qdev);
	ql_free_buffer_queues(qdev);
	ql_free_net_req_rsp_queues(qdev);
	if (qdev->shadow_reg_virt_addr != NULL) {
		pci_free_consistent(qdev->pdev,
				    PAGE_SIZE,
				    qdev->shadow_reg_virt_addr,
				    qdev->shadow_reg_phy_addr);
		qdev->shadow_reg_virt_addr = NULL;
	}
}

static int ql_init_misc_registers(struct ql3_adapter *qdev)
{
A
Al Viro 已提交
2955 2956
	struct ql3xxx_local_ram_registers __iomem *local_ram =
	    (void __iomem *)qdev->mem_map_registers;
R
Ron Mercer 已提交
2957

2958
	if (ql_sem_spinlock(qdev, QL_DDR_RAM_SEM_MASK,
R
Ron Mercer 已提交
2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013
			(QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
			 2) << 4))
		return -1;

	ql_write_page2_reg(qdev,
			   &local_ram->bufletSize, qdev->nvram_data.bufletSize);

	ql_write_page2_reg(qdev,
			   &local_ram->maxBufletCount,
			   qdev->nvram_data.bufletCount);

	ql_write_page2_reg(qdev,
			   &local_ram->freeBufletThresholdLow,
			   (qdev->nvram_data.tcpWindowThreshold25 << 16) |
			   (qdev->nvram_data.tcpWindowThreshold0));

	ql_write_page2_reg(qdev,
			   &local_ram->freeBufletThresholdHigh,
			   qdev->nvram_data.tcpWindowThreshold50);

	ql_write_page2_reg(qdev,
			   &local_ram->ipHashTableBase,
			   (qdev->nvram_data.ipHashTableBaseHi << 16) |
			   qdev->nvram_data.ipHashTableBaseLo);
	ql_write_page2_reg(qdev,
			   &local_ram->ipHashTableCount,
			   qdev->nvram_data.ipHashTableSize);
	ql_write_page2_reg(qdev,
			   &local_ram->tcpHashTableBase,
			   (qdev->nvram_data.tcpHashTableBaseHi << 16) |
			   qdev->nvram_data.tcpHashTableBaseLo);
	ql_write_page2_reg(qdev,
			   &local_ram->tcpHashTableCount,
			   qdev->nvram_data.tcpHashTableSize);
	ql_write_page2_reg(qdev,
			   &local_ram->ncbBase,
			   (qdev->nvram_data.ncbTableBaseHi << 16) |
			   qdev->nvram_data.ncbTableBaseLo);
	ql_write_page2_reg(qdev,
			   &local_ram->maxNcbCount,
			   qdev->nvram_data.ncbTableSize);
	ql_write_page2_reg(qdev,
			   &local_ram->drbBase,
			   (qdev->nvram_data.drbTableBaseHi << 16) |
			   qdev->nvram_data.drbTableBaseLo);
	ql_write_page2_reg(qdev,
			   &local_ram->maxDrbCount,
			   qdev->nvram_data.drbTableSize);
	ql_sem_unlock(qdev, QL_DDR_RAM_SEM_MASK);
	return 0;
}

static int ql_adapter_initialize(struct ql3_adapter *qdev)
{
	u32 value;
3014 3015
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
3016
	__iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
R
Ron Mercer 已提交
3017
	struct ql3xxx_host_memory_registers __iomem *hmem_regs =
3018
		(void __iomem *)port_regs;
R
Ron Mercer 已提交
3019 3020
	u32 delay = 10;
	int status = 0;
3021
	unsigned long hw_flags = 0;
R
Ron Mercer 已提交
3022

3023
	if (ql_mii_setup(qdev))
R
Ron Mercer 已提交
3024 3025 3026
		return -1;

	/* Bring out PHY out of reset */
3027
	ql_write_common_reg(qdev, spir,
R
Ron Mercer 已提交
3028 3029
			    (ISP_SERIAL_PORT_IF_WE |
			     (ISP_SERIAL_PORT_IF_WE << 16)));
3030 3031
	/* Give the PHY time to come out of reset. */
	mdelay(100);
R
Ron Mercer 已提交
3032 3033 3034 3035
	qdev->port_link_state = LS_DOWN;
	netif_carrier_off(qdev->ndev);

	/* V2 chip fix for ARS-39168. */
3036
	ql_write_common_reg(qdev, spir,
R
Ron Mercer 已提交
3037 3038 3039 3040
			    (ISP_SERIAL_PORT_IF_SDE |
			     (ISP_SERIAL_PORT_IF_SDE << 16)));

	/* Request Queue Registers */
3041 3042
	*((u32 *)(qdev->preq_consumer_index)) = 0;
	atomic_set(&qdev->tx_count, NUM_REQ_Q_ENTRIES);
R
Ron Mercer 已提交
3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060
	qdev->req_producer_index = 0;

	ql_write_page1_reg(qdev,
			   &hmem_regs->reqConsumerIndexAddrHigh,
			   qdev->req_consumer_index_phy_addr_high);
	ql_write_page1_reg(qdev,
			   &hmem_regs->reqConsumerIndexAddrLow,
			   qdev->req_consumer_index_phy_addr_low);

	ql_write_page1_reg(qdev,
			   &hmem_regs->reqBaseAddrHigh,
			   MS_64BITS(qdev->req_q_phy_addr));
	ql_write_page1_reg(qdev,
			   &hmem_regs->reqBaseAddrLow,
			   LS_64BITS(qdev->req_q_phy_addr));
	ql_write_page1_reg(qdev, &hmem_regs->reqLength, NUM_REQ_Q_ENTRIES);

	/* Response Queue Registers */
A
Al Viro 已提交
3061
	*((__le16 *) (qdev->prsp_producer_index)) = 0;
R
Ron Mercer 已提交
3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091
	qdev->rsp_consumer_index = 0;
	qdev->rsp_current = qdev->rsp_q_virt_addr;

	ql_write_page1_reg(qdev,
			   &hmem_regs->rspProducerIndexAddrHigh,
			   qdev->rsp_producer_index_phy_addr_high);

	ql_write_page1_reg(qdev,
			   &hmem_regs->rspProducerIndexAddrLow,
			   qdev->rsp_producer_index_phy_addr_low);

	ql_write_page1_reg(qdev,
			   &hmem_regs->rspBaseAddrHigh,
			   MS_64BITS(qdev->rsp_q_phy_addr));

	ql_write_page1_reg(qdev,
			   &hmem_regs->rspBaseAddrLow,
			   LS_64BITS(qdev->rsp_q_phy_addr));

	ql_write_page1_reg(qdev, &hmem_regs->rspLength, NUM_RSP_Q_ENTRIES);

	/* Large Buffer Queue */
	ql_write_page1_reg(qdev,
			   &hmem_regs->rxLargeQBaseAddrHigh,
			   MS_64BITS(qdev->lrg_buf_q_phy_addr));

	ql_write_page1_reg(qdev,
			   &hmem_regs->rxLargeQBaseAddrLow,
			   LS_64BITS(qdev->lrg_buf_q_phy_addr));

3092 3093 3094
	ql_write_page1_reg(qdev,
			   &hmem_regs->rxLargeQLength,
			   qdev->num_lbufq_entries);
R
Ron Mercer 已提交
3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115

	ql_write_page1_reg(qdev,
			   &hmem_regs->rxLargeBufferLength,
			   qdev->lrg_buffer_len);

	/* Small Buffer Queue */
	ql_write_page1_reg(qdev,
			   &hmem_regs->rxSmallQBaseAddrHigh,
			   MS_64BITS(qdev->small_buf_q_phy_addr));

	ql_write_page1_reg(qdev,
			   &hmem_regs->rxSmallQBaseAddrLow,
			   LS_64BITS(qdev->small_buf_q_phy_addr));

	ql_write_page1_reg(qdev, &hmem_regs->rxSmallQLength, NUM_SBUFQ_ENTRIES);
	ql_write_page1_reg(qdev,
			   &hmem_regs->rxSmallBufferLength,
			   QL_SMALL_BUFFER_SIZE);

	qdev->small_buf_q_producer_index = NUM_SBUFQ_ENTRIES - 1;
	qdev->small_buf_release_cnt = 8;
3116
	qdev->lrg_buf_q_producer_index = qdev->num_lbufq_entries - 1;
R
Ron Mercer 已提交
3117
	qdev->lrg_buf_release_cnt = 8;
3118
	qdev->lrg_buf_next_free = qdev->lrg_buf_q_virt_addr;
R
Ron Mercer 已提交
3119 3120 3121 3122 3123 3124 3125
	qdev->small_buf_index = 0;
	qdev->lrg_buf_index = 0;
	qdev->lrg_buf_free_count = 0;
	qdev->lrg_buf_free_head = NULL;
	qdev->lrg_buf_free_tail = NULL;

	ql_write_common_reg(qdev,
A
Al Viro 已提交
3126
			    &port_regs->CommonRegs.
R
Ron Mercer 已提交
3127 3128 3129
			    rxSmallQProducerIndex,
			    qdev->small_buf_q_producer_index);
	ql_write_common_reg(qdev,
A
Al Viro 已提交
3130
			    &port_regs->CommonRegs.
R
Ron Mercer 已提交
3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142
			    rxLargeQProducerIndex,
			    qdev->lrg_buf_q_producer_index);

	/*
	 * Find out if the chip has already been initialized.  If it has, then
	 * we skip some of the initialization.
	 */
	clear_bit(QL_LINK_MASTER, &qdev->flags);
	value = ql_read_page0_reg(qdev, &port_regs->portStatus);
	if ((value & PORT_STATUS_IC) == 0) {

		/* Chip has not been configured yet, so let it rip. */
3143
		if (ql_init_misc_registers(qdev)) {
R
Ron Mercer 已提交
3144 3145 3146 3147 3148 3149 3150 3151 3152
			status = -1;
			goto out;
		}

		value = qdev->nvram_data.tcpMaxWindowSize;
		ql_write_page0_reg(qdev, &port_regs->tcpMaxWindow, value);

		value = (0xFFFF << 16) | qdev->nvram_data.extHwConfig;

3153
		if (ql_sem_spinlock(qdev, QL_FLASH_SEM_MASK,
R
Ron Mercer 已提交
3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166
				(QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index)
				 * 2) << 13)) {
			status = -1;
			goto out;
		}
		ql_write_page0_reg(qdev, &port_regs->ExternalHWConfig, value);
		ql_write_page0_reg(qdev, &port_regs->InternalChipConfig,
				   (((INTERNAL_CHIP_SD | INTERNAL_CHIP_WE) <<
				     16) | (INTERNAL_CHIP_SD |
					    INTERNAL_CHIP_WE)));
		ql_sem_unlock(qdev, QL_FLASH_SEM_MASK);
	}

3167 3168 3169 3170 3171 3172 3173 3174
	if (qdev->mac_index)
		ql_write_page0_reg(qdev,
				   &port_regs->mac1MaxFrameLengthReg,
				   qdev->max_frame_size);
	else
		ql_write_page0_reg(qdev,
					   &port_regs->mac0MaxFrameLengthReg,
					   qdev->max_frame_size);
R
Ron Mercer 已提交
3175

3176
	if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
R
Ron Mercer 已提交
3177 3178 3179 3180 3181 3182
			(QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
			 2) << 7)) {
		status = -1;
		goto out;
	}

3183
	PHY_Setup(qdev);
R
Ron Mercer 已提交
3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231
	ql_init_scan_mode(qdev);
	ql_get_phy_owner(qdev);

	/* Load the MAC Configuration */

	/* Program lower 32 bits of the MAC address */
	ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
			   (MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16));
	ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
			   ((qdev->ndev->dev_addr[2] << 24)
			    | (qdev->ndev->dev_addr[3] << 16)
			    | (qdev->ndev->dev_addr[4] << 8)
			    | qdev->ndev->dev_addr[5]));

	/* Program top 16 bits of the MAC address */
	ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
			   ((MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16) | 1));
	ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
			   ((qdev->ndev->dev_addr[0] << 8)
			    | qdev->ndev->dev_addr[1]));

	/* Enable Primary MAC */
	ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
			   ((MAC_ADDR_INDIRECT_PTR_REG_PE << 16) |
			    MAC_ADDR_INDIRECT_PTR_REG_PE));

	/* Clear Primary and Secondary IP addresses */
	ql_write_page0_reg(qdev, &port_regs->ipAddrIndexReg,
			   ((IP_ADDR_INDEX_REG_MASK << 16) |
			    (qdev->mac_index << 2)));
	ql_write_page0_reg(qdev, &port_regs->ipAddrDataReg, 0);

	ql_write_page0_reg(qdev, &port_regs->ipAddrIndexReg,
			   ((IP_ADDR_INDEX_REG_MASK << 16) |
			    ((qdev->mac_index << 2) + 1)));
	ql_write_page0_reg(qdev, &port_regs->ipAddrDataReg, 0);

	ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);

	/* Indicate Configuration Complete */
	ql_write_page0_reg(qdev,
			   &port_regs->portControl,
			   ((PORT_CONTROL_CC << 16) | PORT_CONTROL_CC));

	do {
		value = ql_read_page0_reg(qdev, &port_regs->portStatus);
		if (value & PORT_STATUS_IC)
			break;
3232
		spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
R
Ron Mercer 已提交
3233
		msleep(500);
3234
		spin_lock_irqsave(&qdev->hw_lock, hw_flags);
R
Ron Mercer 已提交
3235 3236 3237
	} while (--delay);

	if (delay == 0) {
3238
		netdev_err(qdev->ndev, "Hw Initialization timeout\n");
R
Ron Mercer 已提交
3239 3240 3241 3242 3243
		status = -1;
		goto out;
	}

	/* Enable Ethernet Function */
3244 3245 3246
	if (qdev->device_id == QL3032_DEVICE_ID) {
		value =
		    (QL3032_PORT_CONTROL_EF | QL3032_PORT_CONTROL_KIE |
3247 3248
		     QL3032_PORT_CONTROL_EIv6 | QL3032_PORT_CONTROL_EIv4 |
			QL3032_PORT_CONTROL_ET);
3249 3250 3251 3252 3253 3254 3255 3256 3257 3258
		ql_write_page0_reg(qdev, &port_regs->functionControl,
				   ((value << 16) | value));
	} else {
		value =
		    (PORT_CONTROL_EF | PORT_CONTROL_ET | PORT_CONTROL_EI |
		     PORT_CONTROL_HH);
		ql_write_page0_reg(qdev, &port_regs->portControl,
				   ((value << 16) | value));
	}

R
Ron Mercer 已提交
3259 3260 3261 3262 3263 3264 3265 3266 3267 3268

out:
	return status;
}

/*
 * Caller holds hw_lock.
 */
static int ql_adapter_reset(struct ql3_adapter *qdev)
{
3269 3270
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
R
Ron Mercer 已提交
3271 3272 3273 3274 3275 3276 3277 3278 3279 3280
	int status = 0;
	u16 value;
	int max_wait_time;

	set_bit(QL_RESET_ACTIVE, &qdev->flags);
	clear_bit(QL_RESET_DONE, &qdev->flags);

	/*
	 * Issue soft reset to chip.
	 */
3281
	netdev_printk(KERN_DEBUG, qdev->ndev, "Issue soft reset to chip\n");
R
Ron Mercer 已提交
3282
	ql_write_common_reg(qdev,
A
Al Viro 已提交
3283
			    &port_regs->CommonRegs.ispControlStatus,
R
Ron Mercer 已提交
3284 3285 3286
			    ((ISP_CONTROL_SR << 16) | ISP_CONTROL_SR));

	/* Wait 3 seconds for reset to complete. */
3287 3288
	netdev_printk(KERN_DEBUG, qdev->ndev,
		      "Wait 10 milliseconds for reset to complete\n");
R
Ron Mercer 已提交
3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308

	/* Wait until the firmware tells us the Soft Reset is done */
	max_wait_time = 5;
	do {
		value =
		    ql_read_common_reg(qdev,
				       &port_regs->CommonRegs.ispControlStatus);
		if ((value & ISP_CONTROL_SR) == 0)
			break;

		ssleep(1);
	} while ((--max_wait_time));

	/*
	 * Also, make sure that the Network Reset Interrupt bit has been
	 * cleared after the soft reset has taken place.
	 */
	value =
	    ql_read_common_reg(qdev, &port_regs->CommonRegs.ispControlStatus);
	if (value & ISP_CONTROL_RI) {
3309 3310
		netdev_printk(KERN_DEBUG, qdev->ndev,
			      "clearing RI after reset\n");
R
Ron Mercer 已提交
3311
		ql_write_common_reg(qdev,
A
Al Viro 已提交
3312
				    &port_regs->CommonRegs.
R
Ron Mercer 已提交
3313 3314 3315 3316 3317 3318 3319
				    ispControlStatus,
				    ((ISP_CONTROL_RI << 16) | ISP_CONTROL_RI));
	}

	if (max_wait_time == 0) {
		/* Issue Force Soft Reset */
		ql_write_common_reg(qdev,
A
Al Viro 已提交
3320
				    &port_regs->CommonRegs.
R
Ron Mercer 已提交
3321 3322 3323 3324 3325 3326 3327 3328 3329
				    ispControlStatus,
				    ((ISP_CONTROL_FSR << 16) |
				     ISP_CONTROL_FSR));
		/*
		 * Wait until the firmware tells us the Force Soft Reset is
		 * done
		 */
		max_wait_time = 5;
		do {
3330 3331 3332 3333
			value = ql_read_common_reg(qdev,
						   &port_regs->CommonRegs.
						   ispControlStatus);
			if ((value & ISP_CONTROL_FSR) == 0)
R
Ron Mercer 已提交
3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347
				break;
			ssleep(1);
		} while ((--max_wait_time));
	}
	if (max_wait_time == 0)
		status = 1;

	clear_bit(QL_RESET_ACTIVE, &qdev->flags);
	set_bit(QL_RESET_DONE, &qdev->flags);
	return status;
}

static void ql_set_mac_info(struct ql3_adapter *qdev)
{
3348 3349
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
R
Ron Mercer 已提交
3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364
	u32 value, port_status;
	u8 func_number;

	/* Get the function number */
	value =
	    ql_read_common_reg_l(qdev, &port_regs->CommonRegs.ispControlStatus);
	func_number = (u8) ((value >> 4) & OPCODE_FUNC_ID_MASK);
	port_status = ql_read_page0_reg(qdev, &port_regs->portStatus);
	switch (value & ISP_CONTROL_FN_MASK) {
	case ISP_CONTROL_FN0_NET:
		qdev->mac_index = 0;
		qdev->mac_ob_opcode = OUTBOUND_MAC_IOCB | func_number;
		qdev->mb_bit_mask = FN0_MA_BITS_MASK;
		qdev->PHYAddr = PORT0_PHY_ADDRESS;
		if (port_status & PORT_STATUS_SM0)
3365
			set_bit(QL_LINK_OPTICAL, &qdev->flags);
R
Ron Mercer 已提交
3366
		else
3367
			clear_bit(QL_LINK_OPTICAL, &qdev->flags);
R
Ron Mercer 已提交
3368 3369 3370 3371 3372 3373 3374 3375
		break;

	case ISP_CONTROL_FN1_NET:
		qdev->mac_index = 1;
		qdev->mac_ob_opcode = OUTBOUND_MAC_IOCB | func_number;
		qdev->mb_bit_mask = FN1_MA_BITS_MASK;
		qdev->PHYAddr = PORT1_PHY_ADDRESS;
		if (port_status & PORT_STATUS_SM1)
3376
			set_bit(QL_LINK_OPTICAL, &qdev->flags);
R
Ron Mercer 已提交
3377
		else
3378
			clear_bit(QL_LINK_OPTICAL, &qdev->flags);
R
Ron Mercer 已提交
3379 3380 3381 3382 3383
		break;

	case ISP_CONTROL_FN0_SCSI:
	case ISP_CONTROL_FN1_SCSI:
	default:
3384 3385 3386
		netdev_printk(KERN_DEBUG, qdev->ndev,
			      "Invalid function number, ispControlStatus = 0x%x\n",
			      value);
R
Ron Mercer 已提交
3387 3388
		break;
	}
A
Al Viro 已提交
3389
	qdev->numPorts = qdev->nvram_data.version_and_numPorts >> 8;
R
Ron Mercer 已提交
3390 3391 3392 3393
}

static void ql_display_dev_info(struct net_device *ndev)
{
3394
	struct ql3_adapter *qdev = netdev_priv(ndev);
R
Ron Mercer 已提交
3395 3396
	struct pci_dev *pdev = qdev->pdev;

3397 3398 3399
	netdev_info(ndev,
		    "%s Adapter %d RevisionID %d found %s on PCI slot %d\n",
		    DRV_NAME, qdev->index, qdev->chip_rev_id,
3400
		    qdev->device_id == QL3032_DEVICE_ID ? "QLA3032" : "QLA3022",
3401 3402 3403
		    qdev->pci_slot);
	netdev_info(ndev, "%s Interface\n",
		test_bit(QL_LINK_OPTICAL, &qdev->flags) ? "OPTICAL" : "COPPER");
R
Ron Mercer 已提交
3404 3405 3406 3407

	/*
	 * Print PCI bus width/type.
	 */
3408 3409 3410
	netdev_info(ndev, "Bus interface is %s %s\n",
		    ((qdev->pci_width == 64) ? "64-bit" : "32-bit"),
		    ((qdev->pci_x) ? "PCI-X" : "PCI"));
R
Ron Mercer 已提交
3411

3412 3413 3414
	netdev_info(ndev, "mem  IO base address adjusted = 0x%p\n",
		    qdev->mem_map_registers);
	netdev_info(ndev, "Interrupt number = %d\n", pdev->irq);
R
Ron Mercer 已提交
3415

3416
	netif_info(qdev, probe, ndev, "MAC address %pM\n", ndev->dev_addr);
R
Ron Mercer 已提交
3417 3418 3419 3420 3421 3422 3423 3424 3425 3426
}

static int ql_adapter_down(struct ql3_adapter *qdev, int do_reset)
{
	struct net_device *ndev = qdev->ndev;
	int retval = 0;

	netif_stop_queue(ndev);
	netif_carrier_off(ndev);

3427 3428
	clear_bit(QL_ADAPTER_UP, &qdev->flags);
	clear_bit(QL_LINK_MASTER, &qdev->flags);
R
Ron Mercer 已提交
3429 3430 3431 3432 3433

	ql_disable_interrupts(qdev);

	free_irq(qdev->pdev->irq, ndev);

3434
	if (qdev->msi && test_bit(QL_MSI_ENABLED, &qdev->flags)) {
3435
		netdev_info(qdev->ndev, "calling pci_disable_msi()\n");
3436
		clear_bit(QL_MSI_ENABLED, &qdev->flags);
R
Ron Mercer 已提交
3437 3438 3439 3440 3441
		pci_disable_msi(qdev->pdev);
	}

	del_timer_sync(&qdev->adapter_timer);

3442
	napi_disable(&qdev->napi);
R
Ron Mercer 已提交
3443 3444 3445 3446 3447 3448 3449

	if (do_reset) {
		int soft_reset;
		unsigned long hw_flags;

		spin_lock_irqsave(&qdev->hw_lock, hw_flags);
		if (ql_wait_for_drvr_lock(qdev)) {
3450 3451
			soft_reset = ql_adapter_reset(qdev);
			if (soft_reset) {
3452 3453
				netdev_err(ndev, "ql_adapter_reset(%d) FAILED!\n",
					   qdev->index);
R
Ron Mercer 已提交
3454
			}
3455 3456
			netdev_err(ndev,
				   "Releasing driver lock via chip reset\n");
R
Ron Mercer 已提交
3457
		} else {
3458 3459
			netdev_err(ndev,
				   "Could not acquire driver lock to do reset!\n");
R
Ron Mercer 已提交
3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471
			retval = -1;
		}
		spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
	}
	ql_free_mem_resources(qdev);
	return retval;
}

static int ql_adapter_up(struct ql3_adapter *qdev)
{
	struct net_device *ndev = qdev->ndev;
	int err;
3472
	unsigned long irq_flags = IRQF_SHARED;
R
Ron Mercer 已提交
3473 3474 3475
	unsigned long hw_flags;

	if (ql_alloc_mem_resources(qdev)) {
3476
		netdev_err(ndev, "Unable to  allocate buffers\n");
R
Ron Mercer 已提交
3477 3478 3479 3480 3481
		return -ENOMEM;
	}

	if (qdev->msi) {
		if (pci_enable_msi(qdev->pdev)) {
3482 3483
			netdev_err(ndev,
				   "User requested MSI, but MSI failed to initialize.  Continuing without MSI.\n");
R
Ron Mercer 已提交
3484 3485
			qdev->msi = 0;
		} else {
3486
			netdev_info(ndev, "MSI Enabled...\n");
3487
			set_bit(QL_MSI_ENABLED, &qdev->flags);
3488
			irq_flags &= ~IRQF_SHARED;
R
Ron Mercer 已提交
3489 3490 3491
		}
	}

3492 3493 3494
	err = request_irq(qdev->pdev->irq, ql3xxx_isr,
			  irq_flags, ndev->name, ndev);
	if (err) {
3495
		netdev_err(ndev,
3496
			   "Failed to reserve interrupt %d - already in use\n",
3497
			   qdev->pdev->irq);
R
Ron Mercer 已提交
3498 3499 3500 3501 3502
		goto err_irq;
	}

	spin_lock_irqsave(&qdev->hw_lock, hw_flags);

3503 3504 3505 3506
	err = ql_wait_for_drvr_lock(qdev);
	if (err) {
		err = ql_adapter_initialize(qdev);
		if (err) {
3507
			netdev_err(ndev, "Unable to initialize adapter\n");
R
Ron Mercer 已提交
3508 3509
			goto err_init;
		}
3510
		netdev_err(ndev, "Releasing driver lock\n");
R
Ron Mercer 已提交
3511 3512
		ql_sem_unlock(qdev, QL_DRVR_SEM_MASK);
	} else {
3513
		netdev_err(ndev, "Could not acquire driver lock\n");
R
Ron Mercer 已提交
3514 3515 3516 3517 3518
		goto err_lock;
	}

	spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);

3519
	set_bit(QL_ADAPTER_UP, &qdev->flags);
R
Ron Mercer 已提交
3520 3521 3522

	mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);

3523
	napi_enable(&qdev->napi);
R
Ron Mercer 已提交
3524 3525 3526 3527 3528 3529
	ql_enable_interrupts(qdev);
	return 0;

err_init:
	ql_sem_unlock(qdev, QL_DRVR_SEM_MASK);
err_lock:
3530
	spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
R
Ron Mercer 已提交
3531 3532
	free_irq(qdev->pdev->irq, ndev);
err_irq:
3533
	if (qdev->msi && test_bit(QL_MSI_ENABLED, &qdev->flags)) {
3534
		netdev_info(ndev, "calling pci_disable_msi()\n");
3535
		clear_bit(QL_MSI_ENABLED, &qdev->flags);
R
Ron Mercer 已提交
3536 3537 3538 3539 3540 3541 3542
		pci_disable_msi(qdev->pdev);
	}
	return err;
}

static int ql_cycle_adapter(struct ql3_adapter *qdev, int reset)
{
3543
	if (ql_adapter_down(qdev, reset) || ql_adapter_up(qdev)) {
3544 3545
		netdev_err(qdev->ndev,
			   "Driver up/down cycle failed, closing device\n");
3546
		rtnl_lock();
R
Ron Mercer 已提交
3547
		dev_close(qdev->ndev);
3548
		rtnl_unlock();
R
Ron Mercer 已提交
3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561
		return -1;
	}
	return 0;
}

static int ql3xxx_close(struct net_device *ndev)
{
	struct ql3_adapter *qdev = netdev_priv(ndev);

	/*
	 * Wait for device to recover from a reset.
	 * (Rarely happens, but possible.)
	 */
3562
	while (!test_bit(QL_ADAPTER_UP, &qdev->flags))
R
Ron Mercer 已提交
3563 3564
		msleep(50);

3565
	ql_adapter_down(qdev, QL_DO_RESET);
R
Ron Mercer 已提交
3566 3567 3568 3569 3570 3571
	return 0;
}

static int ql3xxx_open(struct net_device *ndev)
{
	struct ql3_adapter *qdev = netdev_priv(ndev);
3572
	return ql_adapter_up(qdev);
R
Ron Mercer 已提交
3573 3574 3575 3576
}

static int ql3xxx_set_mac_address(struct net_device *ndev, void *p)
{
3577
	struct ql3_adapter *qdev = netdev_priv(ndev);
R
Ron Mercer 已提交
3578
	struct ql3xxx_port_registers __iomem *port_regs =
3579
			qdev->mem_map_registers;
R
Ron Mercer 已提交
3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611
	struct sockaddr *addr = p;
	unsigned long hw_flags;

	if (netif_running(ndev))
		return -EBUSY;

	if (!is_valid_ether_addr(addr->sa_data))
		return -EADDRNOTAVAIL;

	memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);

	spin_lock_irqsave(&qdev->hw_lock, hw_flags);
	/* Program lower 32 bits of the MAC address */
	ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
			   (MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16));
	ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
			   ((ndev->dev_addr[2] << 24) | (ndev->
							 dev_addr[3] << 16) |
			    (ndev->dev_addr[4] << 8) | ndev->dev_addr[5]));

	/* Program top 16 bits of the MAC address */
	ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
			   ((MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16) | 1));
	ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
			   ((ndev->dev_addr[0] << 8) | ndev->dev_addr[1]));
	spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);

	return 0;
}

static void ql3xxx_tx_timeout(struct net_device *ndev)
{
3612
	struct ql3_adapter *qdev = netdev_priv(ndev);
R
Ron Mercer 已提交
3613

3614
	netdev_err(ndev, "Resetting...\n");
R
Ron Mercer 已提交
3615 3616 3617 3618 3619 3620 3621 3622
	/*
	 * Stop the queues, we've got a problem.
	 */
	netif_stop_queue(ndev);

	/*
	 * Wake up the worker to process this event.
	 */
D
David Howells 已提交
3623
	queue_delayed_work(qdev->workqueue, &qdev->tx_timeout_work, 0);
R
Ron Mercer 已提交
3624 3625
}

D
David Howells 已提交
3626
static void ql_reset_work(struct work_struct *work)
R
Ron Mercer 已提交
3627
{
D
David Howells 已提交
3628 3629
	struct ql3_adapter *qdev =
		container_of(work, struct ql3_adapter, reset_work.work);
R
Ron Mercer 已提交
3630 3631 3632 3633
	struct net_device *ndev = qdev->ndev;
	u32 value;
	struct ql_tx_buf_cb *tx_cb;
	int max_wait_time, i;
3634 3635
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
R
Ron Mercer 已提交
3636 3637
	unsigned long hw_flags;

3638 3639
	if (test_bit((QL_RESET_PER_SCSI | QL_RESET_START), &qdev->flags)) {
		clear_bit(QL_LINK_MASTER, &qdev->flags);
R
Ron Mercer 已提交
3640 3641 3642 3643 3644

		/*
		 * Loop through the active list and return the skb.
		 */
		for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
3645
			int j;
R
Ron Mercer 已提交
3646 3647
			tx_cb = &qdev->tx_buf[i];
			if (tx_cb->skb) {
3648 3649
				netdev_printk(KERN_DEBUG, ndev,
					      "Freeing lost SKB\n");
R
Ron Mercer 已提交
3650
				pci_unmap_single(qdev->pdev,
3651 3652
					 dma_unmap_addr(&tx_cb->map[0],
							mapaddr),
3653
					 dma_unmap_len(&tx_cb->map[0], maplen),
3654
					 PCI_DMA_TODEVICE);
3655
				for (j = 1; j < tx_cb->seg_count; j++) {
3656
					pci_unmap_page(qdev->pdev,
3657 3658 3659 3660
					       dma_unmap_addr(&tx_cb->map[j],
							      mapaddr),
					       dma_unmap_len(&tx_cb->map[j],
							     maplen),
3661 3662
					       PCI_DMA_TODEVICE);
				}
R
Ron Mercer 已提交
3663 3664 3665 3666 3667
				dev_kfree_skb(tx_cb->skb);
				tx_cb->skb = NULL;
			}
		}

3668
		netdev_err(ndev, "Clearing NRI after reset\n");
R
Ron Mercer 已提交
3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683
		spin_lock_irqsave(&qdev->hw_lock, hw_flags);
		ql_write_common_reg(qdev,
				    &port_regs->CommonRegs.
				    ispControlStatus,
				    ((ISP_CONTROL_RI << 16) | ISP_CONTROL_RI));
		/*
		 * Wait the for Soft Reset to Complete.
		 */
		max_wait_time = 10;
		do {
			value = ql_read_common_reg(qdev,
						   &port_regs->CommonRegs.

						   ispControlStatus);
			if ((value & ISP_CONTROL_SR) == 0) {
3684 3685
				netdev_printk(KERN_DEBUG, ndev,
					      "reset completed\n");
R
Ron Mercer 已提交
3686 3687 3688 3689
				break;
			}

			if (value & ISP_CONTROL_RI) {
3690 3691
				netdev_printk(KERN_DEBUG, ndev,
					      "clearing NRI after reset\n");
R
Ron Mercer 已提交
3692
				ql_write_common_reg(qdev,
A
Al Viro 已提交
3693
						    &port_regs->
R
Ron Mercer 已提交
3694 3695 3696 3697 3698 3699
						    CommonRegs.
						    ispControlStatus,
						    ((ISP_CONTROL_RI <<
						      16) | ISP_CONTROL_RI));
			}

3700
			spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
R
Ron Mercer 已提交
3701
			ssleep(1);
3702
			spin_lock_irqsave(&qdev->hw_lock, hw_flags);
R
Ron Mercer 已提交
3703 3704 3705 3706 3707 3708 3709 3710 3711
		} while (--max_wait_time);
		spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);

		if (value & ISP_CONTROL_SR) {

			/*
			 * Set the reset flags and clear the board again.
			 * Nothing else to do...
			 */
3712 3713 3714
			netdev_err(ndev,
				   "Timed out waiting for reset to complete\n");
			netdev_err(ndev, "Do a reset\n");
3715 3716 3717
			clear_bit(QL_RESET_PER_SCSI, &qdev->flags);
			clear_bit(QL_RESET_START, &qdev->flags);
			ql_cycle_adapter(qdev, QL_DO_RESET);
R
Ron Mercer 已提交
3718 3719 3720
			return;
		}

3721 3722 3723 3724
		clear_bit(QL_RESET_ACTIVE, &qdev->flags);
		clear_bit(QL_RESET_PER_SCSI, &qdev->flags);
		clear_bit(QL_RESET_START, &qdev->flags);
		ql_cycle_adapter(qdev, QL_NO_RESET);
R
Ron Mercer 已提交
3725 3726 3727
	}
}

D
David Howells 已提交
3728
static void ql_tx_timeout_work(struct work_struct *work)
R
Ron Mercer 已提交
3729
{
D
David Howells 已提交
3730 3731 3732 3733
	struct ql3_adapter *qdev =
		container_of(work, struct ql3_adapter, tx_timeout_work.work);

	ql_cycle_adapter(qdev, QL_DO_RESET);
R
Ron Mercer 已提交
3734 3735 3736 3737
}

static void ql_get_board_info(struct ql3_adapter *qdev)
{
3738 3739
	struct ql3xxx_port_registers __iomem *port_regs =
		qdev->mem_map_registers;
R
Ron Mercer 已提交
3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758
	u32 value;

	value = ql_read_page0_reg_l(qdev, &port_regs->portStatus);

	qdev->chip_rev_id = ((value & PORT_STATUS_REV_ID_MASK) >> 12);
	if (value & PORT_STATUS_64)
		qdev->pci_width = 64;
	else
		qdev->pci_width = 32;
	if (value & PORT_STATUS_X)
		qdev->pci_x = 1;
	else
		qdev->pci_x = 0;
	qdev->pci_slot = (u8) PCI_SLOT(qdev->pdev->devfn);
}

static void ql3xxx_timer(unsigned long ptr)
{
	struct ql3_adapter *qdev = (struct ql3_adapter *)ptr;
3759
	queue_delayed_work(qdev->workqueue, &qdev->link_state_work, 0);
R
Ron Mercer 已提交
3760 3761
}

3762 3763 3764 3765 3766 3767 3768 3769 3770 3771
static const struct net_device_ops ql3xxx_netdev_ops = {
	.ndo_open		= ql3xxx_open,
	.ndo_start_xmit		= ql3xxx_send,
	.ndo_stop		= ql3xxx_close,
	.ndo_change_mtu		= eth_change_mtu,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_set_mac_address	= ql3xxx_set_mac_address,
	.ndo_tx_timeout		= ql3xxx_tx_timeout,
};

R
Ron Mercer 已提交
3772 3773 3774 3775 3776
static int __devinit ql3xxx_probe(struct pci_dev *pdev,
				  const struct pci_device_id *pci_entry)
{
	struct net_device *ndev = NULL;
	struct ql3_adapter *qdev = NULL;
3777
	static int cards_found;
3778
	int uninitialized_var(pci_using_dac), err;
R
Ron Mercer 已提交
3779 3780 3781

	err = pci_enable_device(pdev);
	if (err) {
3782
		pr_err("%s cannot enable PCI device\n", pci_name(pdev));
R
Ron Mercer 已提交
3783 3784 3785 3786 3787
		goto err_out;
	}

	err = pci_request_regions(pdev, DRV_NAME);
	if (err) {
3788
		pr_err("%s cannot obtain PCI resources\n", pci_name(pdev));
R
Ron Mercer 已提交
3789 3790 3791 3792 3793
		goto err_out_disable_pdev;
	}

	pci_set_master(pdev);

3794
	if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
R
Ron Mercer 已提交
3795
		pci_using_dac = 1;
3796
		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3797
	} else if (!(err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))) {
R
Ron Mercer 已提交
3798
		pci_using_dac = 0;
3799
		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
R
Ron Mercer 已提交
3800 3801 3802
	}

	if (err) {
3803
		pr_err("%s no usable DMA configuration\n", pci_name(pdev));
R
Ron Mercer 已提交
3804 3805 3806 3807
		goto err_out_free_regions;
	}

	ndev = alloc_etherdev(sizeof(struct ql3_adapter));
3808
	if (!ndev) {
3809
		pr_err("%s could not alloc etherdev\n", pci_name(pdev));
3810
		err = -ENOMEM;
R
Ron Mercer 已提交
3811
		goto err_out_free_regions;
3812
	}
R
Ron Mercer 已提交
3813 3814 3815 3816 3817 3818 3819 3820 3821

	SET_NETDEV_DEV(ndev, &pdev->dev);

	pci_set_drvdata(pdev, ndev);

	qdev = netdev_priv(ndev);
	qdev->index = cards_found;
	qdev->ndev = ndev;
	qdev->pdev = pdev;
3822
	qdev->device_id = pci_entry->device;
R
Ron Mercer 已提交
3823 3824 3825 3826 3827 3828
	qdev->port_link_state = LS_DOWN;
	if (msi)
		qdev->msi = 1;

	qdev->msg_enable = netif_msg_init(debug, default_msg);

3829 3830 3831
	if (pci_using_dac)
		ndev->features |= NETIF_F_HIGHDMA;
	if (qdev->device_id == QL3032_DEVICE_ID)
3832
		ndev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
3833

3834
	qdev->mem_map_registers = pci_ioremap_bar(pdev, 1);
R
Ron Mercer 已提交
3835
	if (!qdev->mem_map_registers) {
3836
		pr_err("%s: cannot map device registers\n", pci_name(pdev));
3837
		err = -EIO;
R
Ron Mercer 已提交
3838 3839 3840 3841 3842 3843 3844
		goto err_out_free_ndev;
	}

	spin_lock_init(&qdev->adapter_lock);
	spin_lock_init(&qdev->hw_lock);

	/* Set driver entry points */
3845
	ndev->netdev_ops = &ql3xxx_netdev_ops;
R
Ron Mercer 已提交
3846 3847 3848
	SET_ETHTOOL_OPS(ndev, &ql3xxx_ethtool_ops);
	ndev->watchdog_timeo = 5 * HZ;

3849
	netif_napi_add(ndev, &qdev->napi, ql_poll, 64);
R
Ron Mercer 已提交
3850 3851 3852 3853 3854

	ndev->irq = pdev->irq;

	/* make sure the EEPROM is good */
	if (ql_get_nvram_params(qdev)) {
3855 3856
		pr_alert("%s: Adapter #%d, Invalid NVRAM parameters\n",
			 __func__, qdev->index);
3857
		err = -EIO;
R
Ron Mercer 已提交
3858 3859 3860 3861 3862 3863 3864
		goto err_out_iounmap;
	}

	ql_set_mac_info(qdev);

	/* Validate and set parameters */
	if (qdev->mac_index) {
R
Ron Mercer 已提交
3865
		ndev->mtu = qdev->nvram_data.macCfg_port1.etherMtu_mac ;
A
Al Viro 已提交
3866
		ql_set_mac_addr(ndev, qdev->nvram_data.funcCfg_fn2.macAddress);
R
Ron Mercer 已提交
3867
	} else {
R
Ron Mercer 已提交
3868
		ndev->mtu = qdev->nvram_data.macCfg_port0.etherMtu_mac ;
A
Al Viro 已提交
3869
		ql_set_mac_addr(ndev, qdev->nvram_data.funcCfg_fn0.macAddress);
R
Ron Mercer 已提交
3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881
	}
	memcpy(ndev->perm_addr, ndev->dev_addr, ndev->addr_len);

	ndev->tx_queue_len = NUM_REQ_Q_ENTRIES;

	/* Record PCI bus information. */
	ql_get_board_info(qdev);

	/*
	 * Set the Maximum Memory Read Byte Count value. We do this to handle
	 * jumbo frames.
	 */
3882
	if (qdev->pci_x)
R
Ron Mercer 已提交
3883 3884 3885 3886
		pci_write_config_word(pdev, (int)0x4e, (u16) 0x0036);

	err = register_netdev(ndev);
	if (err) {
3887
		pr_err("%s: cannot register net device\n", pci_name(pdev));
R
Ron Mercer 已提交
3888 3889 3890 3891 3892 3893 3894 3895 3896
		goto err_out_iounmap;
	}

	/* we're going to reset, so assume we have no link for now */

	netif_carrier_off(ndev);
	netif_stop_queue(ndev);

	qdev->workqueue = create_singlethread_workqueue(ndev->name);
D
David Howells 已提交
3897 3898
	INIT_DELAYED_WORK(&qdev->reset_work, ql_reset_work);
	INIT_DELAYED_WORK(&qdev->tx_timeout_work, ql_tx_timeout_work);
3899
	INIT_DELAYED_WORK(&qdev->link_state_work, ql_link_state_machine_work);
R
Ron Mercer 已提交
3900 3901 3902 3903 3904 3905

	init_timer(&qdev->adapter_timer);
	qdev->adapter_timer.function = ql3xxx_timer;
	qdev->adapter_timer.expires = jiffies + HZ * 2;	/* two second delay */
	qdev->adapter_timer.data = (unsigned long)qdev;

3906 3907 3908 3909
	if (!cards_found) {
		pr_alert("%s\n", DRV_STRING);
		pr_alert("Driver name: %s, Version: %s\n",
			 DRV_NAME, DRV_VERSION);
R
Ron Mercer 已提交
3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944
	}
	ql_display_dev_info(ndev);

	cards_found++;
	return 0;

err_out_iounmap:
	iounmap(qdev->mem_map_registers);
err_out_free_ndev:
	free_netdev(ndev);
err_out_free_regions:
	pci_release_regions(pdev);
err_out_disable_pdev:
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);
err_out:
	return err;
}

static void __devexit ql3xxx_remove(struct pci_dev *pdev)
{
	struct net_device *ndev = pci_get_drvdata(pdev);
	struct ql3_adapter *qdev = netdev_priv(ndev);

	unregister_netdev(ndev);

	ql_disable_interrupts(qdev);

	if (qdev->workqueue) {
		cancel_delayed_work(&qdev->reset_work);
		cancel_delayed_work(&qdev->tx_timeout_work);
		destroy_workqueue(qdev->workqueue);
		qdev->workqueue = NULL;
	}

3945
	iounmap(qdev->mem_map_registers);
R
Ron Mercer 已提交
3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970
	pci_release_regions(pdev);
	pci_set_drvdata(pdev, NULL);
	free_netdev(ndev);
}

static struct pci_driver ql3xxx_driver = {

	.name = DRV_NAME,
	.id_table = ql3xxx_pci_tbl,
	.probe = ql3xxx_probe,
	.remove = __devexit_p(ql3xxx_remove),
};

static int __init ql3xxx_init_module(void)
{
	return pci_register_driver(&ql3xxx_driver);
}

static void __exit ql3xxx_exit(void)
{
	pci_unregister_driver(&ql3xxx_driver);
}

module_init(ql3xxx_init_module);
module_exit(ql3xxx_exit);