Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / net / ethernet / qlogic / qla3xxx.c
blobe61560e16385e855164a2062aa56ae254212721d
1 /*
2 * QLogic QLA3xxx NIC HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
5 * See LICENSE.qla3xxx for copyright and licensing details.
6 */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/list.h>
15 #include <linux/pci.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/dmapool.h>
20 #include <linux/mempool.h>
21 #include <linux/spinlock.h>
22 #include <linux/kthread.h>
23 #include <linux/interrupt.h>
24 #include <linux/errno.h>
25 #include <linux/ioport.h>
26 #include <linux/ip.h>
27 #include <linux/in.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_ether.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/ethtool.h>
33 #include <linux/skbuff.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/if_vlan.h>
36 #include <linux/delay.h>
37 #include <linux/mm.h>
38 #include <linux/prefetch.h>
40 #include "qla3xxx.h"
42 #define DRV_NAME "qla3xxx"
43 #define DRV_STRING "QLogic ISP3XXX Network Driver"
44 #define DRV_VERSION "v2.03.00-k5"
46 static const char ql3xxx_driver_name[] = DRV_NAME;
47 static const char ql3xxx_driver_version[] = DRV_VERSION;
49 #define TIMED_OUT_MSG \
50 "Timed out waiting for management port to get free before issuing command\n"
52 MODULE_AUTHOR("QLogic Corporation");
53 MODULE_DESCRIPTION("QLogic ISP3XXX Network Driver " DRV_VERSION " ");
54 MODULE_LICENSE("GPL");
55 MODULE_VERSION(DRV_VERSION);
57 static const u32 default_msg
58 = NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK
59 | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN;
61 static int debug = -1; /* defaults above */
62 module_param(debug, int, 0);
63 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
65 static int msi;
66 module_param(msi, int, 0);
67 MODULE_PARM_DESC(msi, "Turn on Message Signaled Interrupts.");
69 static DEFINE_PCI_DEVICE_TABLE(ql3xxx_pci_tbl) = {
70 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QL3022_DEVICE_ID)},
71 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QL3032_DEVICE_ID)},
72 /* required last entry */
73 {0,}
76 MODULE_DEVICE_TABLE(pci, ql3xxx_pci_tbl);
79 * These are the known PHY's which are used
81 enum PHY_DEVICE_TYPE {
82 PHY_TYPE_UNKNOWN = 0,
83 PHY_VITESSE_VSC8211,
84 PHY_AGERE_ET1011C,
85 MAX_PHY_DEV_TYPES
88 struct PHY_DEVICE_INFO {
89 const enum PHY_DEVICE_TYPE phyDevice;
90 const u32 phyIdOUI;
91 const u16 phyIdModel;
92 const char *name;
95 static const struct PHY_DEVICE_INFO PHY_DEVICES[] = {
96 {PHY_TYPE_UNKNOWN, 0x000000, 0x0, "PHY_TYPE_UNKNOWN"},
97 {PHY_VITESSE_VSC8211, 0x0003f1, 0xb, "PHY_VITESSE_VSC8211"},
98 {PHY_AGERE_ET1011C, 0x00a0bc, 0x1, "PHY_AGERE_ET1011C"},
103 * Caller must take hw_lock.
105 static int ql_sem_spinlock(struct ql3_adapter *qdev,
106 u32 sem_mask, u32 sem_bits)
108 struct ql3xxx_port_registers __iomem *port_regs =
109 qdev->mem_map_registers;
110 u32 value;
111 unsigned int seconds = 3;
113 do {
114 writel((sem_mask | sem_bits),
115 &port_regs->CommonRegs.semaphoreReg);
116 value = readl(&port_regs->CommonRegs.semaphoreReg);
117 if ((value & (sem_mask >> 16)) == sem_bits)
118 return 0;
119 ssleep(1);
120 } while (--seconds);
121 return -1;
124 static void ql_sem_unlock(struct ql3_adapter *qdev, u32 sem_mask)
126 struct ql3xxx_port_registers __iomem *port_regs =
127 qdev->mem_map_registers;
128 writel(sem_mask, &port_regs->CommonRegs.semaphoreReg);
129 readl(&port_regs->CommonRegs.semaphoreReg);
132 static int ql_sem_lock(struct ql3_adapter *qdev, u32 sem_mask, u32 sem_bits)
134 struct ql3xxx_port_registers __iomem *port_regs =
135 qdev->mem_map_registers;
136 u32 value;
138 writel((sem_mask | sem_bits), &port_regs->CommonRegs.semaphoreReg);
139 value = readl(&port_regs->CommonRegs.semaphoreReg);
140 return ((value & (sem_mask >> 16)) == sem_bits);
144 * Caller holds hw_lock.
146 static int ql_wait_for_drvr_lock(struct ql3_adapter *qdev)
148 int i = 0;
150 while (i < 10) {
151 if (i)
152 ssleep(1);
154 if (ql_sem_lock(qdev,
155 QL_DRVR_SEM_MASK,
156 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index)
157 * 2) << 1)) {
158 netdev_printk(KERN_DEBUG, qdev->ndev,
159 "driver lock acquired\n");
160 return 1;
164 netdev_err(qdev->ndev, "Timed out waiting for driver lock...\n");
165 return 0;
168 static void ql_set_register_page(struct ql3_adapter *qdev, u32 page)
170 struct ql3xxx_port_registers __iomem *port_regs =
171 qdev->mem_map_registers;
173 writel(((ISP_CONTROL_NP_MASK << 16) | page),
174 &port_regs->CommonRegs.ispControlStatus);
175 readl(&port_regs->CommonRegs.ispControlStatus);
176 qdev->current_page = page;
179 static u32 ql_read_common_reg_l(struct ql3_adapter *qdev, u32 __iomem *reg)
181 u32 value;
182 unsigned long hw_flags;
184 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
185 value = readl(reg);
186 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
188 return value;
191 static u32 ql_read_common_reg(struct ql3_adapter *qdev, u32 __iomem *reg)
193 return readl(reg);
196 static u32 ql_read_page0_reg_l(struct ql3_adapter *qdev, u32 __iomem *reg)
198 u32 value;
199 unsigned long hw_flags;
201 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
203 if (qdev->current_page != 0)
204 ql_set_register_page(qdev, 0);
205 value = readl(reg);
207 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
208 return value;
211 static u32 ql_read_page0_reg(struct ql3_adapter *qdev, u32 __iomem *reg)
213 if (qdev->current_page != 0)
214 ql_set_register_page(qdev, 0);
215 return readl(reg);
218 static void ql_write_common_reg_l(struct ql3_adapter *qdev,
219 u32 __iomem *reg, u32 value)
221 unsigned long hw_flags;
223 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
224 writel(value, reg);
225 readl(reg);
226 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
229 static void ql_write_common_reg(struct ql3_adapter *qdev,
230 u32 __iomem *reg, u32 value)
232 writel(value, reg);
233 readl(reg);
236 static void ql_write_nvram_reg(struct ql3_adapter *qdev,
237 u32 __iomem *reg, u32 value)
239 writel(value, reg);
240 readl(reg);
241 udelay(1);
244 static void ql_write_page0_reg(struct ql3_adapter *qdev,
245 u32 __iomem *reg, u32 value)
247 if (qdev->current_page != 0)
248 ql_set_register_page(qdev, 0);
249 writel(value, reg);
250 readl(reg);
254 * Caller holds hw_lock. Only called during init.
256 static void ql_write_page1_reg(struct ql3_adapter *qdev,
257 u32 __iomem *reg, u32 value)
259 if (qdev->current_page != 1)
260 ql_set_register_page(qdev, 1);
261 writel(value, reg);
262 readl(reg);
266 * Caller holds hw_lock. Only called during init.
268 static void ql_write_page2_reg(struct ql3_adapter *qdev,
269 u32 __iomem *reg, u32 value)
271 if (qdev->current_page != 2)
272 ql_set_register_page(qdev, 2);
273 writel(value, reg);
274 readl(reg);
277 static void ql_disable_interrupts(struct ql3_adapter *qdev)
279 struct ql3xxx_port_registers __iomem *port_regs =
280 qdev->mem_map_registers;
282 ql_write_common_reg_l(qdev, &port_regs->CommonRegs.ispInterruptMaskReg,
283 (ISP_IMR_ENABLE_INT << 16));
287 static void ql_enable_interrupts(struct ql3_adapter *qdev)
289 struct ql3xxx_port_registers __iomem *port_regs =
290 qdev->mem_map_registers;
292 ql_write_common_reg_l(qdev, &port_regs->CommonRegs.ispInterruptMaskReg,
293 ((0xff << 16) | ISP_IMR_ENABLE_INT));
297 static void ql_release_to_lrg_buf_free_list(struct ql3_adapter *qdev,
298 struct ql_rcv_buf_cb *lrg_buf_cb)
300 dma_addr_t map;
301 int err;
302 lrg_buf_cb->next = NULL;
304 if (qdev->lrg_buf_free_tail == NULL) { /* The list is empty */
305 qdev->lrg_buf_free_head = qdev->lrg_buf_free_tail = lrg_buf_cb;
306 } else {
307 qdev->lrg_buf_free_tail->next = lrg_buf_cb;
308 qdev->lrg_buf_free_tail = lrg_buf_cb;
311 if (!lrg_buf_cb->skb) {
312 lrg_buf_cb->skb = netdev_alloc_skb(qdev->ndev,
313 qdev->lrg_buffer_len);
314 if (unlikely(!lrg_buf_cb->skb)) {
315 netdev_err(qdev->ndev, "failed netdev_alloc_skb()\n");
316 qdev->lrg_buf_skb_check++;
317 } else {
319 * We save some space to copy the ethhdr from first
320 * buffer
322 skb_reserve(lrg_buf_cb->skb, QL_HEADER_SPACE);
323 map = pci_map_single(qdev->pdev,
324 lrg_buf_cb->skb->data,
325 qdev->lrg_buffer_len -
326 QL_HEADER_SPACE,
327 PCI_DMA_FROMDEVICE);
328 err = pci_dma_mapping_error(qdev->pdev, map);
329 if (err) {
330 netdev_err(qdev->ndev,
331 "PCI mapping failed with error: %d\n",
332 err);
333 dev_kfree_skb(lrg_buf_cb->skb);
334 lrg_buf_cb->skb = NULL;
336 qdev->lrg_buf_skb_check++;
337 return;
340 lrg_buf_cb->buf_phy_addr_low =
341 cpu_to_le32(LS_64BITS(map));
342 lrg_buf_cb->buf_phy_addr_high =
343 cpu_to_le32(MS_64BITS(map));
344 dma_unmap_addr_set(lrg_buf_cb, mapaddr, map);
345 dma_unmap_len_set(lrg_buf_cb, maplen,
346 qdev->lrg_buffer_len -
347 QL_HEADER_SPACE);
351 qdev->lrg_buf_free_count++;
354 static struct ql_rcv_buf_cb *ql_get_from_lrg_buf_free_list(struct ql3_adapter
355 *qdev)
357 struct ql_rcv_buf_cb *lrg_buf_cb = qdev->lrg_buf_free_head;
359 if (lrg_buf_cb != NULL) {
360 qdev->lrg_buf_free_head = lrg_buf_cb->next;
361 if (qdev->lrg_buf_free_head == NULL)
362 qdev->lrg_buf_free_tail = NULL;
363 qdev->lrg_buf_free_count--;
366 return lrg_buf_cb;
369 static u32 addrBits = EEPROM_NO_ADDR_BITS;
370 static u32 dataBits = EEPROM_NO_DATA_BITS;
372 static void fm93c56a_deselect(struct ql3_adapter *qdev);
373 static void eeprom_readword(struct ql3_adapter *qdev, u32 eepromAddr,
374 unsigned short *value);
377 * Caller holds hw_lock.
379 static void fm93c56a_select(struct ql3_adapter *qdev)
381 struct ql3xxx_port_registers __iomem *port_regs =
382 qdev->mem_map_registers;
383 __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
385 qdev->eeprom_cmd_data = AUBURN_EEPROM_CS_1;
386 ql_write_nvram_reg(qdev, spir, ISP_NVRAM_MASK | qdev->eeprom_cmd_data);
387 ql_write_nvram_reg(qdev, spir,
388 ((ISP_NVRAM_MASK << 16) | qdev->eeprom_cmd_data));
392 * Caller holds hw_lock.
394 static void fm93c56a_cmd(struct ql3_adapter *qdev, u32 cmd, u32 eepromAddr)
396 int i;
397 u32 mask;
398 u32 dataBit;
399 u32 previousBit;
400 struct ql3xxx_port_registers __iomem *port_regs =
401 qdev->mem_map_registers;
402 __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
404 /* Clock in a zero, then do the start bit */
405 ql_write_nvram_reg(qdev, spir,
406 (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
407 AUBURN_EEPROM_DO_1));
408 ql_write_nvram_reg(qdev, spir,
409 (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
410 AUBURN_EEPROM_DO_1 | AUBURN_EEPROM_CLK_RISE));
411 ql_write_nvram_reg(qdev, spir,
412 (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
413 AUBURN_EEPROM_DO_1 | AUBURN_EEPROM_CLK_FALL));
415 mask = 1 << (FM93C56A_CMD_BITS - 1);
416 /* Force the previous data bit to be different */
417 previousBit = 0xffff;
418 for (i = 0; i < FM93C56A_CMD_BITS; i++) {
419 dataBit = (cmd & mask)
420 ? AUBURN_EEPROM_DO_1
421 : AUBURN_EEPROM_DO_0;
422 if (previousBit != dataBit) {
423 /* If the bit changed, change the DO state to match */
424 ql_write_nvram_reg(qdev, spir,
425 (ISP_NVRAM_MASK |
426 qdev->eeprom_cmd_data | dataBit));
427 previousBit = dataBit;
429 ql_write_nvram_reg(qdev, spir,
430 (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
431 dataBit | AUBURN_EEPROM_CLK_RISE));
432 ql_write_nvram_reg(qdev, spir,
433 (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
434 dataBit | AUBURN_EEPROM_CLK_FALL));
435 cmd = cmd << 1;
438 mask = 1 << (addrBits - 1);
439 /* Force the previous data bit to be different */
440 previousBit = 0xffff;
441 for (i = 0; i < addrBits; i++) {
442 dataBit = (eepromAddr & mask) ? AUBURN_EEPROM_DO_1
443 : AUBURN_EEPROM_DO_0;
444 if (previousBit != dataBit) {
446 * If the bit changed, then change the DO state to
447 * match
449 ql_write_nvram_reg(qdev, spir,
450 (ISP_NVRAM_MASK |
451 qdev->eeprom_cmd_data | dataBit));
452 previousBit = dataBit;
454 ql_write_nvram_reg(qdev, spir,
455 (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
456 dataBit | AUBURN_EEPROM_CLK_RISE));
457 ql_write_nvram_reg(qdev, spir,
458 (ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
459 dataBit | AUBURN_EEPROM_CLK_FALL));
460 eepromAddr = eepromAddr << 1;
465 * Caller holds hw_lock.
467 static void fm93c56a_deselect(struct ql3_adapter *qdev)
469 struct ql3xxx_port_registers __iomem *port_regs =
470 qdev->mem_map_registers;
471 __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
473 qdev->eeprom_cmd_data = AUBURN_EEPROM_CS_0;
474 ql_write_nvram_reg(qdev, spir, ISP_NVRAM_MASK | qdev->eeprom_cmd_data);
478 * Caller holds hw_lock.
480 static void fm93c56a_datain(struct ql3_adapter *qdev, unsigned short *value)
482 int i;
483 u32 data = 0;
484 u32 dataBit;
485 struct ql3xxx_port_registers __iomem *port_regs =
486 qdev->mem_map_registers;
487 __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
489 /* Read the data bits */
490 /* The first bit is a dummy. Clock right over it. */
491 for (i = 0; i < dataBits; i++) {
492 ql_write_nvram_reg(qdev, spir,
493 ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
494 AUBURN_EEPROM_CLK_RISE);
495 ql_write_nvram_reg(qdev, spir,
496 ISP_NVRAM_MASK | qdev->eeprom_cmd_data |
497 AUBURN_EEPROM_CLK_FALL);
498 dataBit = (ql_read_common_reg(qdev, spir) &
499 AUBURN_EEPROM_DI_1) ? 1 : 0;
500 data = (data << 1) | dataBit;
502 *value = (u16)data;
506 * Caller holds hw_lock.
508 static void eeprom_readword(struct ql3_adapter *qdev,
509 u32 eepromAddr, unsigned short *value)
511 fm93c56a_select(qdev);
512 fm93c56a_cmd(qdev, (int)FM93C56A_READ, eepromAddr);
513 fm93c56a_datain(qdev, value);
514 fm93c56a_deselect(qdev);
517 static void ql_set_mac_addr(struct net_device *ndev, u16 *addr)
519 __le16 *p = (__le16 *)ndev->dev_addr;
520 p[0] = cpu_to_le16(addr[0]);
521 p[1] = cpu_to_le16(addr[1]);
522 p[2] = cpu_to_le16(addr[2]);
525 static int ql_get_nvram_params(struct ql3_adapter *qdev)
527 u16 *pEEPROMData;
528 u16 checksum = 0;
529 u32 index;
530 unsigned long hw_flags;
532 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
534 pEEPROMData = (u16 *)&qdev->nvram_data;
535 qdev->eeprom_cmd_data = 0;
536 if (ql_sem_spinlock(qdev, QL_NVRAM_SEM_MASK,
537 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
538 2) << 10)) {
539 pr_err("%s: Failed ql_sem_spinlock()\n", __func__);
540 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
541 return -1;
544 for (index = 0; index < EEPROM_SIZE; index++) {
545 eeprom_readword(qdev, index, pEEPROMData);
546 checksum += *pEEPROMData;
547 pEEPROMData++;
549 ql_sem_unlock(qdev, QL_NVRAM_SEM_MASK);
551 if (checksum != 0) {
552 netdev_err(qdev->ndev, "checksum should be zero, is %x!!\n",
553 checksum);
554 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
555 return -1;
558 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
559 return checksum;
562 static const u32 PHYAddr[2] = {
563 PORT0_PHY_ADDRESS, PORT1_PHY_ADDRESS
566 static int ql_wait_for_mii_ready(struct ql3_adapter *qdev)
568 struct ql3xxx_port_registers __iomem *port_regs =
569 qdev->mem_map_registers;
570 u32 temp;
571 int count = 1000;
573 while (count) {
574 temp = ql_read_page0_reg(qdev, &port_regs->macMIIStatusReg);
575 if (!(temp & MAC_MII_STATUS_BSY))
576 return 0;
577 udelay(10);
578 count--;
580 return -1;
583 static void ql_mii_enable_scan_mode(struct ql3_adapter *qdev)
585 struct ql3xxx_port_registers __iomem *port_regs =
586 qdev->mem_map_registers;
587 u32 scanControl;
589 if (qdev->numPorts > 1) {
590 /* Auto scan will cycle through multiple ports */
591 scanControl = MAC_MII_CONTROL_AS | MAC_MII_CONTROL_SC;
592 } else {
593 scanControl = MAC_MII_CONTROL_SC;
597 * Scan register 1 of PHY/PETBI,
598 * Set up to scan both devices
599 * The autoscan starts from the first register, completes
600 * the last one before rolling over to the first
602 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
603 PHYAddr[0] | MII_SCAN_REGISTER);
605 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
606 (scanControl) |
607 ((MAC_MII_CONTROL_SC | MAC_MII_CONTROL_AS) << 16));
610 static u8 ql_mii_disable_scan_mode(struct ql3_adapter *qdev)
612 u8 ret;
613 struct ql3xxx_port_registers __iomem *port_regs =
614 qdev->mem_map_registers;
616 /* See if scan mode is enabled before we turn it off */
617 if (ql_read_page0_reg(qdev, &port_regs->macMIIMgmtControlReg) &
618 (MAC_MII_CONTROL_AS | MAC_MII_CONTROL_SC)) {
619 /* Scan is enabled */
620 ret = 1;
621 } else {
622 /* Scan is disabled */
623 ret = 0;
627 * When disabling scan mode you must first change the MII register
628 * address
630 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
631 PHYAddr[0] | MII_SCAN_REGISTER);
633 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
634 ((MAC_MII_CONTROL_SC | MAC_MII_CONTROL_AS |
635 MAC_MII_CONTROL_RC) << 16));
637 return ret;
640 static int ql_mii_write_reg_ex(struct ql3_adapter *qdev,
641 u16 regAddr, u16 value, u32 phyAddr)
643 struct ql3xxx_port_registers __iomem *port_regs =
644 qdev->mem_map_registers;
645 u8 scanWasEnabled;
647 scanWasEnabled = ql_mii_disable_scan_mode(qdev);
649 if (ql_wait_for_mii_ready(qdev)) {
650 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
651 return -1;
654 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
655 phyAddr | regAddr);
657 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtDataReg, value);
659 /* Wait for write to complete 9/10/04 SJP */
660 if (ql_wait_for_mii_ready(qdev)) {
661 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
662 return -1;
665 if (scanWasEnabled)
666 ql_mii_enable_scan_mode(qdev);
668 return 0;
671 static int ql_mii_read_reg_ex(struct ql3_adapter *qdev, u16 regAddr,
672 u16 *value, u32 phyAddr)
674 struct ql3xxx_port_registers __iomem *port_regs =
675 qdev->mem_map_registers;
676 u8 scanWasEnabled;
677 u32 temp;
679 scanWasEnabled = ql_mii_disable_scan_mode(qdev);
681 if (ql_wait_for_mii_ready(qdev)) {
682 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
683 return -1;
686 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
687 phyAddr | regAddr);
689 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
690 (MAC_MII_CONTROL_RC << 16));
692 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
693 (MAC_MII_CONTROL_RC << 16) | MAC_MII_CONTROL_RC);
695 /* Wait for the read to complete */
696 if (ql_wait_for_mii_ready(qdev)) {
697 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
698 return -1;
701 temp = ql_read_page0_reg(qdev, &port_regs->macMIIMgmtDataReg);
702 *value = (u16) temp;
704 if (scanWasEnabled)
705 ql_mii_enable_scan_mode(qdev);
707 return 0;
710 static int ql_mii_write_reg(struct ql3_adapter *qdev, u16 regAddr, u16 value)
712 struct ql3xxx_port_registers __iomem *port_regs =
713 qdev->mem_map_registers;
715 ql_mii_disable_scan_mode(qdev);
717 if (ql_wait_for_mii_ready(qdev)) {
718 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
719 return -1;
722 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
723 qdev->PHYAddr | regAddr);
725 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtDataReg, value);
727 /* Wait for write to complete. */
728 if (ql_wait_for_mii_ready(qdev)) {
729 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
730 return -1;
733 ql_mii_enable_scan_mode(qdev);
735 return 0;
738 static int ql_mii_read_reg(struct ql3_adapter *qdev, u16 regAddr, u16 *value)
740 u32 temp;
741 struct ql3xxx_port_registers __iomem *port_regs =
742 qdev->mem_map_registers;
744 ql_mii_disable_scan_mode(qdev);
746 if (ql_wait_for_mii_ready(qdev)) {
747 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
748 return -1;
751 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtAddrReg,
752 qdev->PHYAddr | regAddr);
754 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
755 (MAC_MII_CONTROL_RC << 16));
757 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
758 (MAC_MII_CONTROL_RC << 16) | MAC_MII_CONTROL_RC);
760 /* Wait for the read to complete */
761 if (ql_wait_for_mii_ready(qdev)) {
762 netif_warn(qdev, link, qdev->ndev, TIMED_OUT_MSG);
763 return -1;
766 temp = ql_read_page0_reg(qdev, &port_regs->macMIIMgmtDataReg);
767 *value = (u16) temp;
769 ql_mii_enable_scan_mode(qdev);
771 return 0;
774 static void ql_petbi_reset(struct ql3_adapter *qdev)
776 ql_mii_write_reg(qdev, PETBI_CONTROL_REG, PETBI_CTRL_SOFT_RESET);
779 static void ql_petbi_start_neg(struct ql3_adapter *qdev)
781 u16 reg;
783 /* Enable Auto-negotiation sense */
784 ql_mii_read_reg(qdev, PETBI_TBI_CTRL, &reg);
785 reg |= PETBI_TBI_AUTO_SENSE;
786 ql_mii_write_reg(qdev, PETBI_TBI_CTRL, reg);
788 ql_mii_write_reg(qdev, PETBI_NEG_ADVER,
789 PETBI_NEG_PAUSE | PETBI_NEG_DUPLEX);
791 ql_mii_write_reg(qdev, PETBI_CONTROL_REG,
792 PETBI_CTRL_AUTO_NEG | PETBI_CTRL_RESTART_NEG |
793 PETBI_CTRL_FULL_DUPLEX | PETBI_CTRL_SPEED_1000);
797 static void ql_petbi_reset_ex(struct ql3_adapter *qdev)
799 ql_mii_write_reg_ex(qdev, PETBI_CONTROL_REG, PETBI_CTRL_SOFT_RESET,
800 PHYAddr[qdev->mac_index]);
803 static void ql_petbi_start_neg_ex(struct ql3_adapter *qdev)
805 u16 reg;
807 /* Enable Auto-negotiation sense */
808 ql_mii_read_reg_ex(qdev, PETBI_TBI_CTRL, &reg,
809 PHYAddr[qdev->mac_index]);
810 reg |= PETBI_TBI_AUTO_SENSE;
811 ql_mii_write_reg_ex(qdev, PETBI_TBI_CTRL, reg,
812 PHYAddr[qdev->mac_index]);
814 ql_mii_write_reg_ex(qdev, PETBI_NEG_ADVER,
815 PETBI_NEG_PAUSE | PETBI_NEG_DUPLEX,
816 PHYAddr[qdev->mac_index]);
818 ql_mii_write_reg_ex(qdev, PETBI_CONTROL_REG,
819 PETBI_CTRL_AUTO_NEG | PETBI_CTRL_RESTART_NEG |
820 PETBI_CTRL_FULL_DUPLEX | PETBI_CTRL_SPEED_1000,
821 PHYAddr[qdev->mac_index]);
824 static void ql_petbi_init(struct ql3_adapter *qdev)
826 ql_petbi_reset(qdev);
827 ql_petbi_start_neg(qdev);
830 static void ql_petbi_init_ex(struct ql3_adapter *qdev)
832 ql_petbi_reset_ex(qdev);
833 ql_petbi_start_neg_ex(qdev);
836 static int ql_is_petbi_neg_pause(struct ql3_adapter *qdev)
838 u16 reg;
840 if (ql_mii_read_reg(qdev, PETBI_NEG_PARTNER, &reg) < 0)
841 return 0;
843 return (reg & PETBI_NEG_PAUSE_MASK) == PETBI_NEG_PAUSE;
846 static void phyAgereSpecificInit(struct ql3_adapter *qdev, u32 miiAddr)
848 netdev_info(qdev->ndev, "enabling Agere specific PHY\n");
849 /* power down device bit 11 = 1 */
850 ql_mii_write_reg_ex(qdev, 0x00, 0x1940, miiAddr);
851 /* enable diagnostic mode bit 2 = 1 */
852 ql_mii_write_reg_ex(qdev, 0x12, 0x840e, miiAddr);
853 /* 1000MB amplitude adjust (see Agere errata) */
854 ql_mii_write_reg_ex(qdev, 0x10, 0x8805, miiAddr);
855 /* 1000MB amplitude adjust (see Agere errata) */
856 ql_mii_write_reg_ex(qdev, 0x11, 0xf03e, miiAddr);
857 /* 100MB amplitude adjust (see Agere errata) */
858 ql_mii_write_reg_ex(qdev, 0x10, 0x8806, miiAddr);
859 /* 100MB amplitude adjust (see Agere errata) */
860 ql_mii_write_reg_ex(qdev, 0x11, 0x003e, miiAddr);
861 /* 10MB amplitude adjust (see Agere errata) */
862 ql_mii_write_reg_ex(qdev, 0x10, 0x8807, miiAddr);
863 /* 10MB amplitude adjust (see Agere errata) */
864 ql_mii_write_reg_ex(qdev, 0x11, 0x1f00, miiAddr);
865 /* point to hidden reg 0x2806 */
866 ql_mii_write_reg_ex(qdev, 0x10, 0x2806, miiAddr);
867 /* Write new PHYAD w/bit 5 set */
868 ql_mii_write_reg_ex(qdev, 0x11,
869 0x0020 | (PHYAddr[qdev->mac_index] >> 8), miiAddr);
871 * Disable diagnostic mode bit 2 = 0
872 * Power up device bit 11 = 0
873 * Link up (on) and activity (blink)
875 ql_mii_write_reg(qdev, 0x12, 0x840a);
876 ql_mii_write_reg(qdev, 0x00, 0x1140);
877 ql_mii_write_reg(qdev, 0x1c, 0xfaf0);
880 static enum PHY_DEVICE_TYPE getPhyType(struct ql3_adapter *qdev,
881 u16 phyIdReg0, u16 phyIdReg1)
883 enum PHY_DEVICE_TYPE result = PHY_TYPE_UNKNOWN;
884 u32 oui;
885 u16 model;
886 int i;
888 if (phyIdReg0 == 0xffff)
889 return result;
891 if (phyIdReg1 == 0xffff)
892 return result;
894 /* oui is split between two registers */
895 oui = (phyIdReg0 << 6) | ((phyIdReg1 & PHY_OUI_1_MASK) >> 10);
897 model = (phyIdReg1 & PHY_MODEL_MASK) >> 4;
899 /* Scan table for this PHY */
900 for (i = 0; i < MAX_PHY_DEV_TYPES; i++) {
901 if ((oui == PHY_DEVICES[i].phyIdOUI) &&
902 (model == PHY_DEVICES[i].phyIdModel)) {
903 netdev_info(qdev->ndev, "Phy: %s\n",
904 PHY_DEVICES[i].name);
905 result = PHY_DEVICES[i].phyDevice;
906 break;
910 return result;
913 static int ql_phy_get_speed(struct ql3_adapter *qdev)
915 u16 reg;
917 switch (qdev->phyType) {
918 case PHY_AGERE_ET1011C: {
919 if (ql_mii_read_reg(qdev, 0x1A, &reg) < 0)
920 return 0;
922 reg = (reg >> 8) & 3;
923 break;
925 default:
926 if (ql_mii_read_reg(qdev, AUX_CONTROL_STATUS, &reg) < 0)
927 return 0;
929 reg = (((reg & 0x18) >> 3) & 3);
932 switch (reg) {
933 case 2:
934 return SPEED_1000;
935 case 1:
936 return SPEED_100;
937 case 0:
938 return SPEED_10;
939 default:
940 return -1;
944 static int ql_is_full_dup(struct ql3_adapter *qdev)
946 u16 reg;
948 switch (qdev->phyType) {
949 case PHY_AGERE_ET1011C: {
950 if (ql_mii_read_reg(qdev, 0x1A, &reg))
951 return 0;
953 return ((reg & 0x0080) && (reg & 0x1000)) != 0;
955 case PHY_VITESSE_VSC8211:
956 default: {
957 if (ql_mii_read_reg(qdev, AUX_CONTROL_STATUS, &reg) < 0)
958 return 0;
959 return (reg & PHY_AUX_DUPLEX_STAT) != 0;
964 static int ql_is_phy_neg_pause(struct ql3_adapter *qdev)
966 u16 reg;
968 if (ql_mii_read_reg(qdev, PHY_NEG_PARTNER, &reg) < 0)
969 return 0;
971 return (reg & PHY_NEG_PAUSE) != 0;
974 static int PHY_Setup(struct ql3_adapter *qdev)
976 u16 reg1;
977 u16 reg2;
978 bool agereAddrChangeNeeded = false;
979 u32 miiAddr = 0;
980 int err;
982 /* Determine the PHY we are using by reading the ID's */
983 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;
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;
995 /* Check if we have a Agere PHY */
996 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;
1005 err = ql_mii_read_reg_ex(qdev, PHY_ID_0_REG, &reg1, miiAddr);
1006 if (err != 0) {
1007 netdev_err(qdev->ndev,
1008 "Could not read from reg PHY_ID_0_REG after Agere detected\n");
1009 return err;
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;
1018 /* We need to remember to initialize the Agere PHY */
1019 agereAddrChangeNeeded = true;
1022 /* Determine the particular PHY we have on board to apply
1023 PHY specific initializations */
1024 qdev->phyType = getPhyType(qdev, reg1, reg2);
1026 if ((qdev->phyType == PHY_AGERE_ET1011C) && agereAddrChangeNeeded) {
1027 /* 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 return -EIO;
1034 return 0;
1038 * Caller holds hw_lock.
1040 static void ql_mac_enable(struct ql3_adapter *qdev, u32 enable)
1042 struct ql3xxx_port_registers __iomem *port_regs =
1043 qdev->mem_map_registers;
1044 u32 value;
1046 if (enable)
1047 value = (MAC_CONFIG_REG_PE | (MAC_CONFIG_REG_PE << 16));
1048 else
1049 value = (MAC_CONFIG_REG_PE << 16);
1051 if (qdev->mac_index)
1052 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1053 else
1054 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1058 * Caller holds hw_lock.
1060 static void ql_mac_cfg_soft_reset(struct ql3_adapter *qdev, u32 enable)
1062 struct ql3xxx_port_registers __iomem *port_regs =
1063 qdev->mem_map_registers;
1064 u32 value;
1066 if (enable)
1067 value = (MAC_CONFIG_REG_SR | (MAC_CONFIG_REG_SR << 16));
1068 else
1069 value = (MAC_CONFIG_REG_SR << 16);
1071 if (qdev->mac_index)
1072 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1073 else
1074 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1078 * Caller holds hw_lock.
1080 static void ql_mac_cfg_gig(struct ql3_adapter *qdev, u32 enable)
1082 struct ql3xxx_port_registers __iomem *port_regs =
1083 qdev->mem_map_registers;
1084 u32 value;
1086 if (enable)
1087 value = (MAC_CONFIG_REG_GM | (MAC_CONFIG_REG_GM << 16));
1088 else
1089 value = (MAC_CONFIG_REG_GM << 16);
1091 if (qdev->mac_index)
1092 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1093 else
1094 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1098 * Caller holds hw_lock.
1100 static void ql_mac_cfg_full_dup(struct ql3_adapter *qdev, u32 enable)
1102 struct ql3xxx_port_registers __iomem *port_regs =
1103 qdev->mem_map_registers;
1104 u32 value;
1106 if (enable)
1107 value = (MAC_CONFIG_REG_FD | (MAC_CONFIG_REG_FD << 16));
1108 else
1109 value = (MAC_CONFIG_REG_FD << 16);
1111 if (qdev->mac_index)
1112 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1113 else
1114 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1118 * Caller holds hw_lock.
1120 static void ql_mac_cfg_pause(struct ql3_adapter *qdev, u32 enable)
1122 struct ql3xxx_port_registers __iomem *port_regs =
1123 qdev->mem_map_registers;
1124 u32 value;
1126 if (enable)
1127 value =
1128 ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) |
1129 ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) << 16));
1130 else
1131 value = ((MAC_CONFIG_REG_TF | MAC_CONFIG_REG_RF) << 16);
1133 if (qdev->mac_index)
1134 ql_write_page0_reg(qdev, &port_regs->mac1ConfigReg, value);
1135 else
1136 ql_write_page0_reg(qdev, &port_regs->mac0ConfigReg, value);
1140 * Caller holds hw_lock.
1142 static int ql_is_fiber(struct ql3_adapter *qdev)
1144 struct ql3xxx_port_registers __iomem *port_regs =
1145 qdev->mem_map_registers;
1146 u32 bitToCheck = 0;
1147 u32 temp;
1149 switch (qdev->mac_index) {
1150 case 0:
1151 bitToCheck = PORT_STATUS_SM0;
1152 break;
1153 case 1:
1154 bitToCheck = PORT_STATUS_SM1;
1155 break;
1158 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1159 return (temp & bitToCheck) != 0;
1162 static int ql_is_auto_cfg(struct ql3_adapter *qdev)
1164 u16 reg;
1165 ql_mii_read_reg(qdev, 0x00, &reg);
1166 return (reg & 0x1000) != 0;
1170 * Caller holds hw_lock.
1172 static int ql_is_auto_neg_complete(struct ql3_adapter *qdev)
1174 struct ql3xxx_port_registers __iomem *port_regs =
1175 qdev->mem_map_registers;
1176 u32 bitToCheck = 0;
1177 u32 temp;
1179 switch (qdev->mac_index) {
1180 case 0:
1181 bitToCheck = PORT_STATUS_AC0;
1182 break;
1183 case 1:
1184 bitToCheck = PORT_STATUS_AC1;
1185 break;
1188 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1189 if (temp & bitToCheck) {
1190 netif_info(qdev, link, qdev->ndev, "Auto-Negotiate complete\n");
1191 return 1;
1193 netif_info(qdev, link, qdev->ndev, "Auto-Negotiate incomplete\n");
1194 return 0;
1198 * ql_is_neg_pause() returns 1 if pause was negotiated to be on
1200 static int ql_is_neg_pause(struct ql3_adapter *qdev)
1202 if (ql_is_fiber(qdev))
1203 return ql_is_petbi_neg_pause(qdev);
1204 else
1205 return ql_is_phy_neg_pause(qdev);
1208 static int ql_auto_neg_error(struct ql3_adapter *qdev)
1210 struct ql3xxx_port_registers __iomem *port_regs =
1211 qdev->mem_map_registers;
1212 u32 bitToCheck = 0;
1213 u32 temp;
1215 switch (qdev->mac_index) {
1216 case 0:
1217 bitToCheck = PORT_STATUS_AE0;
1218 break;
1219 case 1:
1220 bitToCheck = PORT_STATUS_AE1;
1221 break;
1223 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1224 return (temp & bitToCheck) != 0;
1227 static u32 ql_get_link_speed(struct ql3_adapter *qdev)
1229 if (ql_is_fiber(qdev))
1230 return SPEED_1000;
1231 else
1232 return ql_phy_get_speed(qdev);
1235 static int ql_is_link_full_dup(struct ql3_adapter *qdev)
1237 if (ql_is_fiber(qdev))
1238 return 1;
1239 else
1240 return ql_is_full_dup(qdev);
1244 * Caller holds hw_lock.
1246 static int ql_link_down_detect(struct ql3_adapter *qdev)
1248 struct ql3xxx_port_registers __iomem *port_regs =
1249 qdev->mem_map_registers;
1250 u32 bitToCheck = 0;
1251 u32 temp;
1253 switch (qdev->mac_index) {
1254 case 0:
1255 bitToCheck = ISP_CONTROL_LINK_DN_0;
1256 break;
1257 case 1:
1258 bitToCheck = ISP_CONTROL_LINK_DN_1;
1259 break;
1262 temp =
1263 ql_read_common_reg(qdev, &port_regs->CommonRegs.ispControlStatus);
1264 return (temp & bitToCheck) != 0;
1268 * Caller holds hw_lock.
1270 static int ql_link_down_detect_clear(struct ql3_adapter *qdev)
1272 struct ql3xxx_port_registers __iomem *port_regs =
1273 qdev->mem_map_registers;
1275 switch (qdev->mac_index) {
1276 case 0:
1277 ql_write_common_reg(qdev,
1278 &port_regs->CommonRegs.ispControlStatus,
1279 (ISP_CONTROL_LINK_DN_0) |
1280 (ISP_CONTROL_LINK_DN_0 << 16));
1281 break;
1283 case 1:
1284 ql_write_common_reg(qdev,
1285 &port_regs->CommonRegs.ispControlStatus,
1286 (ISP_CONTROL_LINK_DN_1) |
1287 (ISP_CONTROL_LINK_DN_1 << 16));
1288 break;
1290 default:
1291 return 1;
1294 return 0;
1298 * Caller holds hw_lock.
1300 static int ql_this_adapter_controls_port(struct ql3_adapter *qdev)
1302 struct ql3xxx_port_registers __iomem *port_regs =
1303 qdev->mem_map_registers;
1304 u32 bitToCheck = 0;
1305 u32 temp;
1307 switch (qdev->mac_index) {
1308 case 0:
1309 bitToCheck = PORT_STATUS_F1_ENABLED;
1310 break;
1311 case 1:
1312 bitToCheck = PORT_STATUS_F3_ENABLED;
1313 break;
1314 default:
1315 break;
1318 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1319 if (temp & bitToCheck) {
1320 netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
1321 "not link master\n");
1322 return 0;
1325 netif_printk(qdev, link, KERN_DEBUG, qdev->ndev, "link master\n");
1326 return 1;
1329 static void ql_phy_reset_ex(struct ql3_adapter *qdev)
1331 ql_mii_write_reg_ex(qdev, CONTROL_REG, PHY_CTRL_SOFT_RESET,
1332 PHYAddr[qdev->mac_index]);
1335 static void ql_phy_start_neg_ex(struct ql3_adapter *qdev)
1337 u16 reg;
1338 u16 portConfiguration;
1340 if (qdev->phyType == PHY_AGERE_ET1011C)
1341 ql_mii_write_reg(qdev, 0x13, 0x0000);
1342 /* turn off external loopback */
1344 if (qdev->mac_index == 0)
1345 portConfiguration =
1346 qdev->nvram_data.macCfg_port0.portConfiguration;
1347 else
1348 portConfiguration =
1349 qdev->nvram_data.macCfg_port1.portConfiguration;
1351 /* Some HBA's in the field are set to 0 and they need to
1352 be reinterpreted with a default value */
1353 if (portConfiguration == 0)
1354 portConfiguration = PORT_CONFIG_DEFAULT;
1356 /* Set the 1000 advertisements */
1357 ql_mii_read_reg_ex(qdev, PHY_GIG_CONTROL, &reg,
1358 PHYAddr[qdev->mac_index]);
1359 reg &= ~PHY_GIG_ALL_PARAMS;
1361 if (portConfiguration & PORT_CONFIG_1000MB_SPEED) {
1362 if (portConfiguration & PORT_CONFIG_FULL_DUPLEX_ENABLED)
1363 reg |= PHY_GIG_ADV_1000F;
1364 else
1365 reg |= PHY_GIG_ADV_1000H;
1368 ql_mii_write_reg_ex(qdev, PHY_GIG_CONTROL, reg,
1369 PHYAddr[qdev->mac_index]);
1371 /* Set the 10/100 & pause negotiation advertisements */
1372 ql_mii_read_reg_ex(qdev, PHY_NEG_ADVER, &reg,
1373 PHYAddr[qdev->mac_index]);
1374 reg &= ~PHY_NEG_ALL_PARAMS;
1376 if (portConfiguration & PORT_CONFIG_SYM_PAUSE_ENABLED)
1377 reg |= PHY_NEG_ASY_PAUSE | PHY_NEG_SYM_PAUSE;
1379 if (portConfiguration & PORT_CONFIG_FULL_DUPLEX_ENABLED) {
1380 if (portConfiguration & PORT_CONFIG_100MB_SPEED)
1381 reg |= PHY_NEG_ADV_100F;
1383 if (portConfiguration & PORT_CONFIG_10MB_SPEED)
1384 reg |= PHY_NEG_ADV_10F;
1387 if (portConfiguration & PORT_CONFIG_HALF_DUPLEX_ENABLED) {
1388 if (portConfiguration & PORT_CONFIG_100MB_SPEED)
1389 reg |= PHY_NEG_ADV_100H;
1391 if (portConfiguration & PORT_CONFIG_10MB_SPEED)
1392 reg |= PHY_NEG_ADV_10H;
1395 if (portConfiguration & PORT_CONFIG_1000MB_SPEED)
1396 reg |= 1;
1398 ql_mii_write_reg_ex(qdev, PHY_NEG_ADVER, reg,
1399 PHYAddr[qdev->mac_index]);
1401 ql_mii_read_reg_ex(qdev, CONTROL_REG, &reg, PHYAddr[qdev->mac_index]);
1403 ql_mii_write_reg_ex(qdev, CONTROL_REG,
1404 reg | PHY_CTRL_RESTART_NEG | PHY_CTRL_AUTO_NEG,
1405 PHYAddr[qdev->mac_index]);
1408 static void ql_phy_init_ex(struct ql3_adapter *qdev)
1410 ql_phy_reset_ex(qdev);
1411 PHY_Setup(qdev);
1412 ql_phy_start_neg_ex(qdev);
1416 * Caller holds hw_lock.
1418 static u32 ql_get_link_state(struct ql3_adapter *qdev)
1420 struct ql3xxx_port_registers __iomem *port_regs =
1421 qdev->mem_map_registers;
1422 u32 bitToCheck = 0;
1423 u32 temp, linkState;
1425 switch (qdev->mac_index) {
1426 case 0:
1427 bitToCheck = PORT_STATUS_UP0;
1428 break;
1429 case 1:
1430 bitToCheck = PORT_STATUS_UP1;
1431 break;
1434 temp = ql_read_page0_reg(qdev, &port_regs->portStatus);
1435 if (temp & bitToCheck)
1436 linkState = LS_UP;
1437 else
1438 linkState = LS_DOWN;
1440 return linkState;
1443 static int ql_port_start(struct ql3_adapter *qdev)
1445 if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
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");
1449 return -1;
1452 if (ql_is_fiber(qdev)) {
1453 ql_petbi_init(qdev);
1454 } else {
1455 /* Copper port */
1456 ql_phy_init_ex(qdev);
1459 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1460 return 0;
1463 static int ql_finish_auto_neg(struct ql3_adapter *qdev)
1466 if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
1467 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
1468 2) << 7))
1469 return -1;
1471 if (!ql_auto_neg_error(qdev)) {
1472 if (test_bit(QL_LINK_MASTER, &qdev->flags)) {
1473 /* configure the MAC */
1474 netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
1475 "Configuring link\n");
1476 ql_mac_cfg_soft_reset(qdev, 1);
1477 ql_mac_cfg_gig(qdev,
1478 (ql_get_link_speed
1479 (qdev) ==
1480 SPEED_1000));
1481 ql_mac_cfg_full_dup(qdev,
1482 ql_is_link_full_dup
1483 (qdev));
1484 ql_mac_cfg_pause(qdev,
1485 ql_is_neg_pause
1486 (qdev));
1487 ql_mac_cfg_soft_reset(qdev, 0);
1489 /* enable the MAC */
1490 netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
1491 "Enabling mac\n");
1492 ql_mac_enable(qdev, 1);
1495 qdev->port_link_state = LS_UP;
1496 netif_start_queue(qdev->ndev);
1497 netif_carrier_on(qdev->ndev);
1498 netif_info(qdev, link, qdev->ndev,
1499 "Link is up at %d Mbps, %s duplex\n",
1500 ql_get_link_speed(qdev),
1501 ql_is_link_full_dup(qdev) ? "full" : "half");
1503 } else { /* Remote error detected */
1505 if (test_bit(QL_LINK_MASTER, &qdev->flags)) {
1506 netif_printk(qdev, link, KERN_DEBUG, qdev->ndev,
1507 "Remote error detected. Calling ql_port_start()\n");
1509 * ql_port_start() is shared code and needs
1510 * to lock the PHY on it's own.
1512 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1513 if (ql_port_start(qdev)) /* Restart port */
1514 return -1;
1515 return 0;
1518 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1519 return 0;
1522 static void ql_link_state_machine_work(struct work_struct *work)
1524 struct ql3_adapter *qdev =
1525 container_of(work, struct ql3_adapter, link_state_work.work);
1527 u32 curr_link_state;
1528 unsigned long hw_flags;
1530 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
1532 curr_link_state = ql_get_link_state(qdev);
1534 if (test_bit(QL_RESET_ACTIVE, &qdev->flags)) {
1535 netif_info(qdev, link, qdev->ndev,
1536 "Reset in progress, skip processing link state\n");
1538 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1540 /* Restart timer on 2 second interval. */
1541 mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);
1543 return;
1546 switch (qdev->port_link_state) {
1547 default:
1548 if (test_bit(QL_LINK_MASTER, &qdev->flags))
1549 ql_port_start(qdev);
1550 qdev->port_link_state = LS_DOWN;
1551 /* Fall Through */
1553 case LS_DOWN:
1554 if (curr_link_state == LS_UP) {
1555 netif_info(qdev, link, qdev->ndev, "Link is up\n");
1556 if (ql_is_auto_neg_complete(qdev))
1557 ql_finish_auto_neg(qdev);
1559 if (qdev->port_link_state == LS_UP)
1560 ql_link_down_detect_clear(qdev);
1562 qdev->port_link_state = LS_UP;
1564 break;
1566 case LS_UP:
1568 * See if the link is currently down or went down and came
1569 * back up
1571 if (curr_link_state == LS_DOWN) {
1572 netif_info(qdev, link, qdev->ndev, "Link is down\n");
1573 qdev->port_link_state = LS_DOWN;
1575 if (ql_link_down_detect(qdev))
1576 qdev->port_link_state = LS_DOWN;
1577 break;
1579 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1581 /* Restart timer on 2 second interval. */
1582 mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);
1586 * Caller must take hw_lock and QL_PHY_GIO_SEM.
1588 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);
1592 else
1593 clear_bit(QL_LINK_MASTER, &qdev->flags);
1597 * Caller must take hw_lock and QL_PHY_GIO_SEM.
1599 static void ql_init_scan_mode(struct ql3_adapter *qdev)
1601 ql_mii_enable_scan_mode(qdev);
1603 if (test_bit(QL_LINK_OPTICAL, &qdev->flags)) {
1604 if (ql_this_adapter_controls_port(qdev))
1605 ql_petbi_init_ex(qdev);
1606 } else {
1607 if (ql_this_adapter_controls_port(qdev))
1608 ql_phy_init_ex(qdev);
1613 * MII_Setup needs to be called before taking the PHY out of reset
1614 * so that the management interface clock speed can be set properly.
1615 * It would be better if we had a way to disable MDC until after the
1616 * PHY is out of reset, but we don't have that capability.
1618 static int ql_mii_setup(struct ql3_adapter *qdev)
1620 u32 reg;
1621 struct ql3xxx_port_registers __iomem *port_regs =
1622 qdev->mem_map_registers;
1624 if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
1625 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
1626 2) << 7))
1627 return -1;
1629 if (qdev->device_id == QL3032_DEVICE_ID)
1630 ql_write_page0_reg(qdev,
1631 &port_regs->macMIIMgmtControlReg, 0x0f00000);
1633 /* Divide 125MHz clock by 28 to meet PHY timing requirements */
1634 reg = MAC_MII_CONTROL_CLK_SEL_DIV28;
1636 ql_write_page0_reg(qdev, &port_regs->macMIIMgmtControlReg,
1637 reg | ((MAC_MII_CONTROL_CLK_SEL_MASK) << 16));
1639 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1640 return 0;
1643 #define SUPPORTED_OPTICAL_MODES (SUPPORTED_1000baseT_Full | \
1644 SUPPORTED_FIBRE | \
1645 SUPPORTED_Autoneg)
1646 #define SUPPORTED_TP_MODES (SUPPORTED_10baseT_Half | \
1647 SUPPORTED_10baseT_Full | \
1648 SUPPORTED_100baseT_Half | \
1649 SUPPORTED_100baseT_Full | \
1650 SUPPORTED_1000baseT_Half | \
1651 SUPPORTED_1000baseT_Full | \
1652 SUPPORTED_Autoneg | \
1653 SUPPORTED_TP) \
1655 static u32 ql_supported_modes(struct ql3_adapter *qdev)
1657 if (test_bit(QL_LINK_OPTICAL, &qdev->flags))
1658 return SUPPORTED_OPTICAL_MODES;
1660 return SUPPORTED_TP_MODES;
1663 static int ql_get_auto_cfg_status(struct ql3_adapter *qdev)
1665 int status;
1666 unsigned long hw_flags;
1667 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
1668 if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
1669 (QL_RESOURCE_BITS_BASE_CODE |
1670 (qdev->mac_index) * 2) << 7)) {
1671 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1672 return 0;
1674 status = ql_is_auto_cfg(qdev);
1675 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1676 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1677 return status;
1680 static u32 ql_get_speed(struct ql3_adapter *qdev)
1682 u32 status;
1683 unsigned long hw_flags;
1684 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
1685 if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
1686 (QL_RESOURCE_BITS_BASE_CODE |
1687 (qdev->mac_index) * 2) << 7)) {
1688 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1689 return 0;
1691 status = ql_get_link_speed(qdev);
1692 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1693 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1694 return status;
1697 static int ql_get_full_dup(struct ql3_adapter *qdev)
1699 int status;
1700 unsigned long hw_flags;
1701 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
1702 if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
1703 (QL_RESOURCE_BITS_BASE_CODE |
1704 (qdev->mac_index) * 2) << 7)) {
1705 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1706 return 0;
1708 status = ql_is_link_full_dup(qdev);
1709 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
1710 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
1711 return status;
1714 static int ql_get_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
1716 struct ql3_adapter *qdev = netdev_priv(ndev);
1718 ecmd->transceiver = XCVR_INTERNAL;
1719 ecmd->supported = ql_supported_modes(qdev);
1721 if (test_bit(QL_LINK_OPTICAL, &qdev->flags)) {
1722 ecmd->port = PORT_FIBRE;
1723 } else {
1724 ecmd->port = PORT_TP;
1725 ecmd->phy_address = qdev->PHYAddr;
1727 ecmd->advertising = ql_supported_modes(qdev);
1728 ecmd->autoneg = ql_get_auto_cfg_status(qdev);
1729 ethtool_cmd_speed_set(ecmd, ql_get_speed(qdev));
1730 ecmd->duplex = ql_get_full_dup(qdev);
1731 return 0;
1734 static void ql_get_drvinfo(struct net_device *ndev,
1735 struct ethtool_drvinfo *drvinfo)
1737 struct ql3_adapter *qdev = netdev_priv(ndev);
1738 strlcpy(drvinfo->driver, ql3xxx_driver_name, sizeof(drvinfo->driver));
1739 strlcpy(drvinfo->version, ql3xxx_driver_version,
1740 sizeof(drvinfo->version));
1741 strlcpy(drvinfo->bus_info, pci_name(qdev->pdev),
1742 sizeof(drvinfo->bus_info));
1743 drvinfo->regdump_len = 0;
1744 drvinfo->eedump_len = 0;
1747 static u32 ql_get_msglevel(struct net_device *ndev)
1749 struct ql3_adapter *qdev = netdev_priv(ndev);
1750 return qdev->msg_enable;
1753 static void ql_set_msglevel(struct net_device *ndev, u32 value)
1755 struct ql3_adapter *qdev = netdev_priv(ndev);
1756 qdev->msg_enable = value;
1759 static void ql_get_pauseparam(struct net_device *ndev,
1760 struct ethtool_pauseparam *pause)
1762 struct ql3_adapter *qdev = netdev_priv(ndev);
1763 struct ql3xxx_port_registers __iomem *port_regs =
1764 qdev->mem_map_registers;
1766 u32 reg;
1767 if (qdev->mac_index == 0)
1768 reg = ql_read_page0_reg(qdev, &port_regs->mac0ConfigReg);
1769 else
1770 reg = ql_read_page0_reg(qdev, &port_regs->mac1ConfigReg);
1772 pause->autoneg = ql_get_auto_cfg_status(qdev);
1773 pause->rx_pause = (reg & MAC_CONFIG_REG_RF) >> 2;
1774 pause->tx_pause = (reg & MAC_CONFIG_REG_TF) >> 1;
1777 static const struct ethtool_ops ql3xxx_ethtool_ops = {
1778 .get_settings = ql_get_settings,
1779 .get_drvinfo = ql_get_drvinfo,
1780 .get_link = ethtool_op_get_link,
1781 .get_msglevel = ql_get_msglevel,
1782 .set_msglevel = ql_set_msglevel,
1783 .get_pauseparam = ql_get_pauseparam,
1786 static int ql_populate_free_queue(struct ql3_adapter *qdev)
1788 struct ql_rcv_buf_cb *lrg_buf_cb = qdev->lrg_buf_free_head;
1789 dma_addr_t map;
1790 int err;
1792 while (lrg_buf_cb) {
1793 if (!lrg_buf_cb->skb) {
1794 lrg_buf_cb->skb =
1795 netdev_alloc_skb(qdev->ndev,
1796 qdev->lrg_buffer_len);
1797 if (unlikely(!lrg_buf_cb->skb)) {
1798 netdev_printk(KERN_DEBUG, qdev->ndev,
1799 "Failed netdev_alloc_skb()\n");
1800 break;
1801 } else {
1803 * We save some space to copy the ethhdr from
1804 * first buffer
1806 skb_reserve(lrg_buf_cb->skb, QL_HEADER_SPACE);
1807 map = pci_map_single(qdev->pdev,
1808 lrg_buf_cb->skb->data,
1809 qdev->lrg_buffer_len -
1810 QL_HEADER_SPACE,
1811 PCI_DMA_FROMDEVICE);
1813 err = pci_dma_mapping_error(qdev->pdev, map);
1814 if (err) {
1815 netdev_err(qdev->ndev,
1816 "PCI mapping failed with error: %d\n",
1817 err);
1818 dev_kfree_skb(lrg_buf_cb->skb);
1819 lrg_buf_cb->skb = NULL;
1820 break;
1824 lrg_buf_cb->buf_phy_addr_low =
1825 cpu_to_le32(LS_64BITS(map));
1826 lrg_buf_cb->buf_phy_addr_high =
1827 cpu_to_le32(MS_64BITS(map));
1828 dma_unmap_addr_set(lrg_buf_cb, mapaddr, map);
1829 dma_unmap_len_set(lrg_buf_cb, maplen,
1830 qdev->lrg_buffer_len -
1831 QL_HEADER_SPACE);
1832 --qdev->lrg_buf_skb_check;
1833 if (!qdev->lrg_buf_skb_check)
1834 return 1;
1837 lrg_buf_cb = lrg_buf_cb->next;
1839 return 0;
1843 * Caller holds hw_lock.
1845 static void ql_update_small_bufq_prod_index(struct ql3_adapter *qdev)
1847 struct ql3xxx_port_registers __iomem *port_regs =
1848 qdev->mem_map_registers;
1850 if (qdev->small_buf_release_cnt >= 16) {
1851 while (qdev->small_buf_release_cnt >= 16) {
1852 qdev->small_buf_q_producer_index++;
1854 if (qdev->small_buf_q_producer_index ==
1855 NUM_SBUFQ_ENTRIES)
1856 qdev->small_buf_q_producer_index = 0;
1857 qdev->small_buf_release_cnt -= 8;
1859 wmb();
1860 writel(qdev->small_buf_q_producer_index,
1861 &port_regs->CommonRegs.rxSmallQProducerIndex);
1866 * Caller holds hw_lock.
1868 static void ql_update_lrg_bufq_prod_index(struct ql3_adapter *qdev)
1870 struct bufq_addr_element *lrg_buf_q_ele;
1871 int i;
1872 struct ql_rcv_buf_cb *lrg_buf_cb;
1873 struct ql3xxx_port_registers __iomem *port_regs =
1874 qdev->mem_map_registers;
1876 if ((qdev->lrg_buf_free_count >= 8) &&
1877 (qdev->lrg_buf_release_cnt >= 16)) {
1879 if (qdev->lrg_buf_skb_check)
1880 if (!ql_populate_free_queue(qdev))
1881 return;
1883 lrg_buf_q_ele = qdev->lrg_buf_next_free;
1885 while ((qdev->lrg_buf_release_cnt >= 16) &&
1886 (qdev->lrg_buf_free_count >= 8)) {
1888 for (i = 0; i < 8; i++) {
1889 lrg_buf_cb =
1890 ql_get_from_lrg_buf_free_list(qdev);
1891 lrg_buf_q_ele->addr_high =
1892 lrg_buf_cb->buf_phy_addr_high;
1893 lrg_buf_q_ele->addr_low =
1894 lrg_buf_cb->buf_phy_addr_low;
1895 lrg_buf_q_ele++;
1897 qdev->lrg_buf_release_cnt--;
1900 qdev->lrg_buf_q_producer_index++;
1902 if (qdev->lrg_buf_q_producer_index ==
1903 qdev->num_lbufq_entries)
1904 qdev->lrg_buf_q_producer_index = 0;
1906 if (qdev->lrg_buf_q_producer_index ==
1907 (qdev->num_lbufq_entries - 1)) {
1908 lrg_buf_q_ele = qdev->lrg_buf_q_virt_addr;
1911 wmb();
1912 qdev->lrg_buf_next_free = lrg_buf_q_ele;
1913 writel(qdev->lrg_buf_q_producer_index,
1914 &port_regs->CommonRegs.rxLargeQProducerIndex);
1918 static void ql_process_mac_tx_intr(struct ql3_adapter *qdev,
1919 struct ob_mac_iocb_rsp *mac_rsp)
1921 struct ql_tx_buf_cb *tx_cb;
1922 int i;
1923 int retval = 0;
1925 if (mac_rsp->flags & OB_MAC_IOCB_RSP_S) {
1926 netdev_warn(qdev->ndev,
1927 "Frame too short but it was padded and sent\n");
1930 tx_cb = &qdev->tx_buf[mac_rsp->transaction_id];
1932 /* Check the transmit response flags for any errors */
1933 if (mac_rsp->flags & OB_MAC_IOCB_RSP_S) {
1934 netdev_err(qdev->ndev,
1935 "Frame too short to be legal, frame not sent\n");
1937 qdev->ndev->stats.tx_errors++;
1938 retval = -EIO;
1939 goto frame_not_sent;
1942 if (tx_cb->seg_count == 0) {
1943 netdev_err(qdev->ndev, "tx_cb->seg_count == 0: %d\n",
1944 mac_rsp->transaction_id);
1946 qdev->ndev->stats.tx_errors++;
1947 retval = -EIO;
1948 goto invalid_seg_count;
1951 pci_unmap_single(qdev->pdev,
1952 dma_unmap_addr(&tx_cb->map[0], mapaddr),
1953 dma_unmap_len(&tx_cb->map[0], maplen),
1954 PCI_DMA_TODEVICE);
1955 tx_cb->seg_count--;
1956 if (tx_cb->seg_count) {
1957 for (i = 1; i < tx_cb->seg_count; i++) {
1958 pci_unmap_page(qdev->pdev,
1959 dma_unmap_addr(&tx_cb->map[i],
1960 mapaddr),
1961 dma_unmap_len(&tx_cb->map[i], maplen),
1962 PCI_DMA_TODEVICE);
1965 qdev->ndev->stats.tx_packets++;
1966 qdev->ndev->stats.tx_bytes += tx_cb->skb->len;
1968 frame_not_sent:
1969 dev_kfree_skb_irq(tx_cb->skb);
1970 tx_cb->skb = NULL;
1972 invalid_seg_count:
1973 atomic_inc(&qdev->tx_count);
1976 static void ql_get_sbuf(struct ql3_adapter *qdev)
1978 if (++qdev->small_buf_index == NUM_SMALL_BUFFERS)
1979 qdev->small_buf_index = 0;
1980 qdev->small_buf_release_cnt++;
1983 static struct ql_rcv_buf_cb *ql_get_lbuf(struct ql3_adapter *qdev)
1985 struct ql_rcv_buf_cb *lrg_buf_cb = NULL;
1986 lrg_buf_cb = &qdev->lrg_buf[qdev->lrg_buf_index];
1987 qdev->lrg_buf_release_cnt++;
1988 if (++qdev->lrg_buf_index == qdev->num_large_buffers)
1989 qdev->lrg_buf_index = 0;
1990 return lrg_buf_cb;
1994 * The difference between 3022 and 3032 for inbound completions:
1995 * 3022 uses two buffers per completion. The first buffer contains
1996 * (some) header info, the second the remainder of the headers plus
1997 * the data. For this chip we reserve some space at the top of the
1998 * receive buffer so that the header info in buffer one can be
1999 * prepended to the buffer two. Buffer two is the sent up while
2000 * buffer one is returned to the hardware to be reused.
2001 * 3032 receives all of it's data and headers in one buffer for a
2002 * simpler process. 3032 also supports checksum verification as
2003 * can be seen in ql_process_macip_rx_intr().
2005 static void ql_process_mac_rx_intr(struct ql3_adapter *qdev,
2006 struct ib_mac_iocb_rsp *ib_mac_rsp_ptr)
2008 struct ql_rcv_buf_cb *lrg_buf_cb1 = NULL;
2009 struct ql_rcv_buf_cb *lrg_buf_cb2 = NULL;
2010 struct sk_buff *skb;
2011 u16 length = le16_to_cpu(ib_mac_rsp_ptr->length);
2014 * Get the inbound address list (small buffer).
2016 ql_get_sbuf(qdev);
2018 if (qdev->device_id == QL3022_DEVICE_ID)
2019 lrg_buf_cb1 = ql_get_lbuf(qdev);
2021 /* start of second buffer */
2022 lrg_buf_cb2 = ql_get_lbuf(qdev);
2023 skb = lrg_buf_cb2->skb;
2025 qdev->ndev->stats.rx_packets++;
2026 qdev->ndev->stats.rx_bytes += length;
2028 skb_put(skb, length);
2029 pci_unmap_single(qdev->pdev,
2030 dma_unmap_addr(lrg_buf_cb2, mapaddr),
2031 dma_unmap_len(lrg_buf_cb2, maplen),
2032 PCI_DMA_FROMDEVICE);
2033 prefetch(skb->data);
2034 skb_checksum_none_assert(skb);
2035 skb->protocol = eth_type_trans(skb, qdev->ndev);
2037 netif_receive_skb(skb);
2038 lrg_buf_cb2->skb = NULL;
2040 if (qdev->device_id == QL3022_DEVICE_ID)
2041 ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb1);
2042 ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb2);
2045 static void ql_process_macip_rx_intr(struct ql3_adapter *qdev,
2046 struct ib_ip_iocb_rsp *ib_ip_rsp_ptr)
2048 struct ql_rcv_buf_cb *lrg_buf_cb1 = NULL;
2049 struct ql_rcv_buf_cb *lrg_buf_cb2 = NULL;
2050 struct sk_buff *skb1 = NULL, *skb2;
2051 struct net_device *ndev = qdev->ndev;
2052 u16 length = le16_to_cpu(ib_ip_rsp_ptr->length);
2053 u16 size = 0;
2056 * Get the inbound address list (small buffer).
2059 ql_get_sbuf(qdev);
2061 if (qdev->device_id == QL3022_DEVICE_ID) {
2062 /* start of first buffer on 3022 */
2063 lrg_buf_cb1 = ql_get_lbuf(qdev);
2064 skb1 = lrg_buf_cb1->skb;
2065 size = ETH_HLEN;
2066 if (*((u16 *) skb1->data) != 0xFFFF)
2067 size += VLAN_ETH_HLEN - ETH_HLEN;
2070 /* start of second buffer */
2071 lrg_buf_cb2 = ql_get_lbuf(qdev);
2072 skb2 = lrg_buf_cb2->skb;
2074 skb_put(skb2, length); /* Just the second buffer length here. */
2075 pci_unmap_single(qdev->pdev,
2076 dma_unmap_addr(lrg_buf_cb2, mapaddr),
2077 dma_unmap_len(lrg_buf_cb2, maplen),
2078 PCI_DMA_FROMDEVICE);
2079 prefetch(skb2->data);
2081 skb_checksum_none_assert(skb2);
2082 if (qdev->device_id == QL3022_DEVICE_ID) {
2084 * Copy the ethhdr from first buffer to second. This
2085 * is necessary for 3022 IP completions.
2087 skb_copy_from_linear_data_offset(skb1, VLAN_ID_LEN,
2088 skb_push(skb2, size), size);
2089 } else {
2090 u16 checksum = le16_to_cpu(ib_ip_rsp_ptr->checksum);
2091 if (checksum &
2092 (IB_IP_IOCB_RSP_3032_ICE |
2093 IB_IP_IOCB_RSP_3032_CE)) {
2094 netdev_err(ndev,
2095 "%s: Bad checksum for this %s packet, checksum = %x\n",
2096 __func__,
2097 ((checksum & IB_IP_IOCB_RSP_3032_TCP) ?
2098 "TCP" : "UDP"), checksum);
2099 } else if ((checksum & IB_IP_IOCB_RSP_3032_TCP) ||
2100 (checksum & IB_IP_IOCB_RSP_3032_UDP &&
2101 !(checksum & IB_IP_IOCB_RSP_3032_NUC))) {
2102 skb2->ip_summed = CHECKSUM_UNNECESSARY;
2105 skb2->protocol = eth_type_trans(skb2, qdev->ndev);
2107 netif_receive_skb(skb2);
2108 ndev->stats.rx_packets++;
2109 ndev->stats.rx_bytes += length;
2110 lrg_buf_cb2->skb = NULL;
2112 if (qdev->device_id == QL3022_DEVICE_ID)
2113 ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb1);
2114 ql_release_to_lrg_buf_free_list(qdev, lrg_buf_cb2);
2117 static int ql_tx_rx_clean(struct ql3_adapter *qdev,
2118 int *tx_cleaned, int *rx_cleaned, int work_to_do)
2120 struct net_rsp_iocb *net_rsp;
2121 struct net_device *ndev = qdev->ndev;
2122 int work_done = 0;
2124 /* While there are entries in the completion queue. */
2125 while ((le32_to_cpu(*(qdev->prsp_producer_index)) !=
2126 qdev->rsp_consumer_index) && (work_done < work_to_do)) {
2128 net_rsp = qdev->rsp_current;
2129 rmb();
2131 * Fix 4032 chip's undocumented "feature" where bit-8 is set
2132 * if the inbound completion is for a VLAN.
2134 if (qdev->device_id == QL3032_DEVICE_ID)
2135 net_rsp->opcode &= 0x7f;
2136 switch (net_rsp->opcode) {
2138 case OPCODE_OB_MAC_IOCB_FN0:
2139 case OPCODE_OB_MAC_IOCB_FN2:
2140 ql_process_mac_tx_intr(qdev, (struct ob_mac_iocb_rsp *)
2141 net_rsp);
2142 (*tx_cleaned)++;
2143 break;
2145 case OPCODE_IB_MAC_IOCB:
2146 case OPCODE_IB_3032_MAC_IOCB:
2147 ql_process_mac_rx_intr(qdev, (struct ib_mac_iocb_rsp *)
2148 net_rsp);
2149 (*rx_cleaned)++;
2150 break;
2152 case OPCODE_IB_IP_IOCB:
2153 case OPCODE_IB_3032_IP_IOCB:
2154 ql_process_macip_rx_intr(qdev, (struct ib_ip_iocb_rsp *)
2155 net_rsp);
2156 (*rx_cleaned)++;
2157 break;
2158 default: {
2159 u32 *tmp = (u32 *)net_rsp;
2160 netdev_err(ndev,
2161 "Hit default case, not handled!\n"
2162 " dropping the packet, opcode = %x\n"
2163 "0x%08lx 0x%08lx 0x%08lx 0x%08lx\n",
2164 net_rsp->opcode,
2165 (unsigned long int)tmp[0],
2166 (unsigned long int)tmp[1],
2167 (unsigned long int)tmp[2],
2168 (unsigned long int)tmp[3]);
2172 qdev->rsp_consumer_index++;
2174 if (qdev->rsp_consumer_index == NUM_RSP_Q_ENTRIES) {
2175 qdev->rsp_consumer_index = 0;
2176 qdev->rsp_current = qdev->rsp_q_virt_addr;
2177 } else {
2178 qdev->rsp_current++;
2181 work_done = *tx_cleaned + *rx_cleaned;
2184 return work_done;
2187 static int ql_poll(struct napi_struct *napi, int budget)
2189 struct ql3_adapter *qdev = container_of(napi, struct ql3_adapter, napi);
2190 int rx_cleaned = 0, tx_cleaned = 0;
2191 unsigned long hw_flags;
2192 struct ql3xxx_port_registers __iomem *port_regs =
2193 qdev->mem_map_registers;
2195 ql_tx_rx_clean(qdev, &tx_cleaned, &rx_cleaned, budget);
2197 if (tx_cleaned + rx_cleaned != budget) {
2198 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
2199 __napi_complete(napi);
2200 ql_update_small_bufq_prod_index(qdev);
2201 ql_update_lrg_bufq_prod_index(qdev);
2202 writel(qdev->rsp_consumer_index,
2203 &port_regs->CommonRegs.rspQConsumerIndex);
2204 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
2206 ql_enable_interrupts(qdev);
2208 return tx_cleaned + rx_cleaned;
2211 static irqreturn_t ql3xxx_isr(int irq, void *dev_id)
2214 struct net_device *ndev = dev_id;
2215 struct ql3_adapter *qdev = netdev_priv(ndev);
2216 struct ql3xxx_port_registers __iomem *port_regs =
2217 qdev->mem_map_registers;
2218 u32 value;
2219 int handled = 1;
2220 u32 var;
2222 value = ql_read_common_reg_l(qdev,
2223 &port_regs->CommonRegs.ispControlStatus);
2225 if (value & (ISP_CONTROL_FE | ISP_CONTROL_RI)) {
2226 spin_lock(&qdev->adapter_lock);
2227 netif_stop_queue(qdev->ndev);
2228 netif_carrier_off(qdev->ndev);
2229 ql_disable_interrupts(qdev);
2230 qdev->port_link_state = LS_DOWN;
2231 set_bit(QL_RESET_ACTIVE, &qdev->flags) ;
2233 if (value & ISP_CONTROL_FE) {
2235 * Chip Fatal Error.
2237 var =
2238 ql_read_page0_reg_l(qdev,
2239 &port_regs->PortFatalErrStatus);
2240 netdev_warn(ndev,
2241 "Resetting chip. PortFatalErrStatus register = 0x%x\n",
2242 var);
2243 set_bit(QL_RESET_START, &qdev->flags) ;
2244 } else {
2246 * Soft Reset Requested.
2248 set_bit(QL_RESET_PER_SCSI, &qdev->flags) ;
2249 netdev_err(ndev,
2250 "Another function issued a reset to the chip. ISR value = %x\n",
2251 value);
2253 queue_delayed_work(qdev->workqueue, &qdev->reset_work, 0);
2254 spin_unlock(&qdev->adapter_lock);
2255 } else if (value & ISP_IMR_DISABLE_CMPL_INT) {
2256 ql_disable_interrupts(qdev);
2257 if (likely(napi_schedule_prep(&qdev->napi)))
2258 __napi_schedule(&qdev->napi);
2259 } else
2260 return IRQ_NONE;
2262 return IRQ_RETVAL(handled);
2266 * Get the total number of segments needed for the given number of fragments.
2267 * This is necessary because outbound address lists (OAL) will be used when
2268 * more than two frags are given. Each address list has 5 addr/len pairs.
2269 * The 5th pair in each OAL is used to point to the next OAL if more frags
2270 * are coming. That is why the frags:segment count ratio is not linear.
2272 static int ql_get_seg_count(struct ql3_adapter *qdev, unsigned short frags)
2274 if (qdev->device_id == QL3022_DEVICE_ID)
2275 return 1;
2277 if (frags <= 2)
2278 return frags + 1;
2279 else if (frags <= 6)
2280 return frags + 2;
2281 else if (frags <= 10)
2282 return frags + 3;
2283 else if (frags <= 14)
2284 return frags + 4;
2285 else if (frags <= 18)
2286 return frags + 5;
2287 return -1;
2290 static void ql_hw_csum_setup(const struct sk_buff *skb,
2291 struct ob_mac_iocb_req *mac_iocb_ptr)
2293 const struct iphdr *ip = ip_hdr(skb);
2295 mac_iocb_ptr->ip_hdr_off = skb_network_offset(skb);
2296 mac_iocb_ptr->ip_hdr_len = ip->ihl;
2298 if (ip->protocol == IPPROTO_TCP) {
2299 mac_iocb_ptr->flags1 |= OB_3032MAC_IOCB_REQ_TC |
2300 OB_3032MAC_IOCB_REQ_IC;
2301 } else {
2302 mac_iocb_ptr->flags1 |= OB_3032MAC_IOCB_REQ_UC |
2303 OB_3032MAC_IOCB_REQ_IC;
2309 * Map the buffers for this transmit.
2310 * This will return NETDEV_TX_BUSY or NETDEV_TX_OK based on success.
2312 static int ql_send_map(struct ql3_adapter *qdev,
2313 struct ob_mac_iocb_req *mac_iocb_ptr,
2314 struct ql_tx_buf_cb *tx_cb,
2315 struct sk_buff *skb)
2317 struct oal *oal;
2318 struct oal_entry *oal_entry;
2319 int len = skb_headlen(skb);
2320 dma_addr_t map;
2321 int err;
2322 int completed_segs, i;
2323 int seg_cnt, seg = 0;
2324 int frag_cnt = (int)skb_shinfo(skb)->nr_frags;
2326 seg_cnt = tx_cb->seg_count;
2328 * Map the skb buffer first.
2330 map = pci_map_single(qdev->pdev, skb->data, len, PCI_DMA_TODEVICE);
2332 err = pci_dma_mapping_error(qdev->pdev, map);
2333 if (err) {
2334 netdev_err(qdev->ndev, "PCI mapping failed with error: %d\n",
2335 err);
2337 return NETDEV_TX_BUSY;
2340 oal_entry = (struct oal_entry *)&mac_iocb_ptr->buf_addr0_low;
2341 oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
2342 oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
2343 oal_entry->len = cpu_to_le32(len);
2344 dma_unmap_addr_set(&tx_cb->map[seg], mapaddr, map);
2345 dma_unmap_len_set(&tx_cb->map[seg], maplen, len);
2346 seg++;
2348 if (seg_cnt == 1) {
2349 /* Terminate the last segment. */
2350 oal_entry->len |= cpu_to_le32(OAL_LAST_ENTRY);
2351 return NETDEV_TX_OK;
2353 oal = tx_cb->oal;
2354 for (completed_segs = 0;
2355 completed_segs < frag_cnt;
2356 completed_segs++, seg++) {
2357 skb_frag_t *frag = &skb_shinfo(skb)->frags[completed_segs];
2358 oal_entry++;
2360 * Check for continuation requirements.
2361 * It's strange but necessary.
2362 * Continuation entry points to outbound address list.
2364 if ((seg == 2 && seg_cnt > 3) ||
2365 (seg == 7 && seg_cnt > 8) ||
2366 (seg == 12 && seg_cnt > 13) ||
2367 (seg == 17 && seg_cnt > 18)) {
2368 map = pci_map_single(qdev->pdev, oal,
2369 sizeof(struct oal),
2370 PCI_DMA_TODEVICE);
2372 err = pci_dma_mapping_error(qdev->pdev, map);
2373 if (err) {
2374 netdev_err(qdev->ndev,
2375 "PCI mapping outbound address list with error: %d\n",
2376 err);
2377 goto map_error;
2380 oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
2381 oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
2382 oal_entry->len = cpu_to_le32(sizeof(struct oal) |
2383 OAL_CONT_ENTRY);
2384 dma_unmap_addr_set(&tx_cb->map[seg], mapaddr, map);
2385 dma_unmap_len_set(&tx_cb->map[seg], maplen,
2386 sizeof(struct oal));
2387 oal_entry = (struct oal_entry *)oal;
2388 oal++;
2389 seg++;
2392 map = skb_frag_dma_map(&qdev->pdev->dev, frag, 0, skb_frag_size(frag),
2393 DMA_TO_DEVICE);
2395 err = dma_mapping_error(&qdev->pdev->dev, map);
2396 if (err) {
2397 netdev_err(qdev->ndev,
2398 "PCI mapping frags failed with error: %d\n",
2399 err);
2400 goto map_error;
2403 oal_entry->dma_lo = cpu_to_le32(LS_64BITS(map));
2404 oal_entry->dma_hi = cpu_to_le32(MS_64BITS(map));
2405 oal_entry->len = cpu_to_le32(skb_frag_size(frag));
2406 dma_unmap_addr_set(&tx_cb->map[seg], mapaddr, map);
2407 dma_unmap_len_set(&tx_cb->map[seg], maplen, skb_frag_size(frag));
2409 /* Terminate the last segment. */
2410 oal_entry->len |= cpu_to_le32(OAL_LAST_ENTRY);
2411 return NETDEV_TX_OK;
2413 map_error:
2414 /* A PCI mapping failed and now we will need to back out
2415 * We need to traverse through the oal's and associated pages which
2416 * have been mapped and now we must unmap them to clean up properly
2419 seg = 1;
2420 oal_entry = (struct oal_entry *)&mac_iocb_ptr->buf_addr0_low;
2421 oal = tx_cb->oal;
2422 for (i = 0; i < completed_segs; i++, seg++) {
2423 oal_entry++;
2426 * Check for continuation requirements.
2427 * It's strange but necessary.
2430 if ((seg == 2 && seg_cnt > 3) ||
2431 (seg == 7 && seg_cnt > 8) ||
2432 (seg == 12 && seg_cnt > 13) ||
2433 (seg == 17 && seg_cnt > 18)) {
2434 pci_unmap_single(qdev->pdev,
2435 dma_unmap_addr(&tx_cb->map[seg], mapaddr),
2436 dma_unmap_len(&tx_cb->map[seg], maplen),
2437 PCI_DMA_TODEVICE);
2438 oal++;
2439 seg++;
2442 pci_unmap_page(qdev->pdev,
2443 dma_unmap_addr(&tx_cb->map[seg], mapaddr),
2444 dma_unmap_len(&tx_cb->map[seg], maplen),
2445 PCI_DMA_TODEVICE);
2448 pci_unmap_single(qdev->pdev,
2449 dma_unmap_addr(&tx_cb->map[0], mapaddr),
2450 dma_unmap_addr(&tx_cb->map[0], maplen),
2451 PCI_DMA_TODEVICE);
2453 return NETDEV_TX_BUSY;
2458 * The difference between 3022 and 3032 sends:
2459 * 3022 only supports a simple single segment transmission.
2460 * 3032 supports checksumming and scatter/gather lists (fragments).
2461 * The 3032 supports sglists by using the 3 addr/len pairs (ALP)
2462 * in the IOCB plus a chain of outbound address lists (OAL) that
2463 * each contain 5 ALPs. The last ALP of the IOCB (3rd) or OAL (5th)
2464 * will be used to point to an OAL when more ALP entries are required.
2465 * The IOCB is always the top of the chain followed by one or more
2466 * OALs (when necessary).
2468 static netdev_tx_t ql3xxx_send(struct sk_buff *skb,
2469 struct net_device *ndev)
2471 struct ql3_adapter *qdev = netdev_priv(ndev);
2472 struct ql3xxx_port_registers __iomem *port_regs =
2473 qdev->mem_map_registers;
2474 struct ql_tx_buf_cb *tx_cb;
2475 u32 tot_len = skb->len;
2476 struct ob_mac_iocb_req *mac_iocb_ptr;
2478 if (unlikely(atomic_read(&qdev->tx_count) < 2))
2479 return NETDEV_TX_BUSY;
2481 tx_cb = &qdev->tx_buf[qdev->req_producer_index];
2482 tx_cb->seg_count = ql_get_seg_count(qdev,
2483 skb_shinfo(skb)->nr_frags);
2484 if (tx_cb->seg_count == -1) {
2485 netdev_err(ndev, "%s: invalid segment count!\n", __func__);
2486 return NETDEV_TX_OK;
2489 mac_iocb_ptr = tx_cb->queue_entry;
2490 memset((void *)mac_iocb_ptr, 0, sizeof(struct ob_mac_iocb_req));
2491 mac_iocb_ptr->opcode = qdev->mac_ob_opcode;
2492 mac_iocb_ptr->flags = OB_MAC_IOCB_REQ_X;
2493 mac_iocb_ptr->flags |= qdev->mb_bit_mask;
2494 mac_iocb_ptr->transaction_id = qdev->req_producer_index;
2495 mac_iocb_ptr->data_len = cpu_to_le16((u16) tot_len);
2496 tx_cb->skb = skb;
2497 if (qdev->device_id == QL3032_DEVICE_ID &&
2498 skb->ip_summed == CHECKSUM_PARTIAL)
2499 ql_hw_csum_setup(skb, mac_iocb_ptr);
2501 if (ql_send_map(qdev, mac_iocb_ptr, tx_cb, skb) != NETDEV_TX_OK) {
2502 netdev_err(ndev, "%s: Could not map the segments!\n", __func__);
2503 return NETDEV_TX_BUSY;
2506 wmb();
2507 qdev->req_producer_index++;
2508 if (qdev->req_producer_index == NUM_REQ_Q_ENTRIES)
2509 qdev->req_producer_index = 0;
2510 wmb();
2511 ql_write_common_reg_l(qdev,
2512 &port_regs->CommonRegs.reqQProducerIndex,
2513 qdev->req_producer_index);
2515 netif_printk(qdev, tx_queued, KERN_DEBUG, ndev,
2516 "tx queued, slot %d, len %d\n",
2517 qdev->req_producer_index, skb->len);
2519 atomic_dec(&qdev->tx_count);
2520 return NETDEV_TX_OK;
2523 static int ql_alloc_net_req_rsp_queues(struct ql3_adapter *qdev)
2525 qdev->req_q_size =
2526 (u32) (NUM_REQ_Q_ENTRIES * sizeof(struct ob_mac_iocb_req));
2528 qdev->req_q_virt_addr =
2529 pci_alloc_consistent(qdev->pdev,
2530 (size_t) qdev->req_q_size,
2531 &qdev->req_q_phy_addr);
2533 if ((qdev->req_q_virt_addr == NULL) ||
2534 LS_64BITS(qdev->req_q_phy_addr) & (qdev->req_q_size - 1)) {
2535 netdev_err(qdev->ndev, "reqQ failed\n");
2536 return -ENOMEM;
2539 qdev->rsp_q_size = NUM_RSP_Q_ENTRIES * sizeof(struct net_rsp_iocb);
2541 qdev->rsp_q_virt_addr =
2542 pci_alloc_consistent(qdev->pdev,
2543 (size_t) qdev->rsp_q_size,
2544 &qdev->rsp_q_phy_addr);
2546 if ((qdev->rsp_q_virt_addr == NULL) ||
2547 LS_64BITS(qdev->rsp_q_phy_addr) & (qdev->rsp_q_size - 1)) {
2548 netdev_err(qdev->ndev, "rspQ allocation failed\n");
2549 pci_free_consistent(qdev->pdev, (size_t) qdev->req_q_size,
2550 qdev->req_q_virt_addr,
2551 qdev->req_q_phy_addr);
2552 return -ENOMEM;
2555 set_bit(QL_ALLOC_REQ_RSP_Q_DONE, &qdev->flags);
2557 return 0;
2560 static void ql_free_net_req_rsp_queues(struct ql3_adapter *qdev)
2562 if (!test_bit(QL_ALLOC_REQ_RSP_Q_DONE, &qdev->flags)) {
2563 netdev_info(qdev->ndev, "Already done\n");
2564 return;
2567 pci_free_consistent(qdev->pdev,
2568 qdev->req_q_size,
2569 qdev->req_q_virt_addr, qdev->req_q_phy_addr);
2571 qdev->req_q_virt_addr = NULL;
2573 pci_free_consistent(qdev->pdev,
2574 qdev->rsp_q_size,
2575 qdev->rsp_q_virt_addr, qdev->rsp_q_phy_addr);
2577 qdev->rsp_q_virt_addr = NULL;
2579 clear_bit(QL_ALLOC_REQ_RSP_Q_DONE, &qdev->flags);
2582 static int ql_alloc_buffer_queues(struct ql3_adapter *qdev)
2584 /* Create Large Buffer Queue */
2585 qdev->lrg_buf_q_size =
2586 qdev->num_lbufq_entries * sizeof(struct lrg_buf_q_entry);
2587 if (qdev->lrg_buf_q_size < PAGE_SIZE)
2588 qdev->lrg_buf_q_alloc_size = PAGE_SIZE;
2589 else
2590 qdev->lrg_buf_q_alloc_size = qdev->lrg_buf_q_size * 2;
2592 qdev->lrg_buf =
2593 kmalloc(qdev->num_large_buffers * sizeof(struct ql_rcv_buf_cb),
2594 GFP_KERNEL);
2595 if (qdev->lrg_buf == NULL) {
2596 netdev_err(qdev->ndev, "qdev->lrg_buf alloc failed\n");
2597 return -ENOMEM;
2600 qdev->lrg_buf_q_alloc_virt_addr =
2601 pci_alloc_consistent(qdev->pdev,
2602 qdev->lrg_buf_q_alloc_size,
2603 &qdev->lrg_buf_q_alloc_phy_addr);
2605 if (qdev->lrg_buf_q_alloc_virt_addr == NULL) {
2606 netdev_err(qdev->ndev, "lBufQ failed\n");
2607 return -ENOMEM;
2609 qdev->lrg_buf_q_virt_addr = qdev->lrg_buf_q_alloc_virt_addr;
2610 qdev->lrg_buf_q_phy_addr = qdev->lrg_buf_q_alloc_phy_addr;
2612 /* Create Small Buffer Queue */
2613 qdev->small_buf_q_size =
2614 NUM_SBUFQ_ENTRIES * sizeof(struct lrg_buf_q_entry);
2615 if (qdev->small_buf_q_size < PAGE_SIZE)
2616 qdev->small_buf_q_alloc_size = PAGE_SIZE;
2617 else
2618 qdev->small_buf_q_alloc_size = qdev->small_buf_q_size * 2;
2620 qdev->small_buf_q_alloc_virt_addr =
2621 pci_alloc_consistent(qdev->pdev,
2622 qdev->small_buf_q_alloc_size,
2623 &qdev->small_buf_q_alloc_phy_addr);
2625 if (qdev->small_buf_q_alloc_virt_addr == NULL) {
2626 netdev_err(qdev->ndev, "Small Buffer Queue allocation failed\n");
2627 pci_free_consistent(qdev->pdev, qdev->lrg_buf_q_alloc_size,
2628 qdev->lrg_buf_q_alloc_virt_addr,
2629 qdev->lrg_buf_q_alloc_phy_addr);
2630 return -ENOMEM;
2633 qdev->small_buf_q_virt_addr = qdev->small_buf_q_alloc_virt_addr;
2634 qdev->small_buf_q_phy_addr = qdev->small_buf_q_alloc_phy_addr;
2635 set_bit(QL_ALLOC_BUFQS_DONE, &qdev->flags);
2636 return 0;
2639 static void ql_free_buffer_queues(struct ql3_adapter *qdev)
2641 if (!test_bit(QL_ALLOC_BUFQS_DONE, &qdev->flags)) {
2642 netdev_info(qdev->ndev, "Already done\n");
2643 return;
2645 kfree(qdev->lrg_buf);
2646 pci_free_consistent(qdev->pdev,
2647 qdev->lrg_buf_q_alloc_size,
2648 qdev->lrg_buf_q_alloc_virt_addr,
2649 qdev->lrg_buf_q_alloc_phy_addr);
2651 qdev->lrg_buf_q_virt_addr = NULL;
2653 pci_free_consistent(qdev->pdev,
2654 qdev->small_buf_q_alloc_size,
2655 qdev->small_buf_q_alloc_virt_addr,
2656 qdev->small_buf_q_alloc_phy_addr);
2658 qdev->small_buf_q_virt_addr = NULL;
2660 clear_bit(QL_ALLOC_BUFQS_DONE, &qdev->flags);
2663 static int ql_alloc_small_buffers(struct ql3_adapter *qdev)
2665 int i;
2666 struct bufq_addr_element *small_buf_q_entry;
2668 /* Currently we allocate on one of memory and use it for smallbuffers */
2669 qdev->small_buf_total_size =
2670 (QL_ADDR_ELE_PER_BUFQ_ENTRY * NUM_SBUFQ_ENTRIES *
2671 QL_SMALL_BUFFER_SIZE);
2673 qdev->small_buf_virt_addr =
2674 pci_alloc_consistent(qdev->pdev,
2675 qdev->small_buf_total_size,
2676 &qdev->small_buf_phy_addr);
2678 if (qdev->small_buf_virt_addr == NULL) {
2679 netdev_err(qdev->ndev, "Failed to get small buffer memory\n");
2680 return -ENOMEM;
2683 qdev->small_buf_phy_addr_low = LS_64BITS(qdev->small_buf_phy_addr);
2684 qdev->small_buf_phy_addr_high = MS_64BITS(qdev->small_buf_phy_addr);
2686 small_buf_q_entry = qdev->small_buf_q_virt_addr;
2688 /* Initialize the small buffer queue. */
2689 for (i = 0; i < (QL_ADDR_ELE_PER_BUFQ_ENTRY * NUM_SBUFQ_ENTRIES); i++) {
2690 small_buf_q_entry->addr_high =
2691 cpu_to_le32(qdev->small_buf_phy_addr_high);
2692 small_buf_q_entry->addr_low =
2693 cpu_to_le32(qdev->small_buf_phy_addr_low +
2694 (i * QL_SMALL_BUFFER_SIZE));
2695 small_buf_q_entry++;
2697 qdev->small_buf_index = 0;
2698 set_bit(QL_ALLOC_SMALL_BUF_DONE, &qdev->flags);
2699 return 0;
2702 static void ql_free_small_buffers(struct ql3_adapter *qdev)
2704 if (!test_bit(QL_ALLOC_SMALL_BUF_DONE, &qdev->flags)) {
2705 netdev_info(qdev->ndev, "Already done\n");
2706 return;
2708 if (qdev->small_buf_virt_addr != NULL) {
2709 pci_free_consistent(qdev->pdev,
2710 qdev->small_buf_total_size,
2711 qdev->small_buf_virt_addr,
2712 qdev->small_buf_phy_addr);
2714 qdev->small_buf_virt_addr = NULL;
2718 static void ql_free_large_buffers(struct ql3_adapter *qdev)
2720 int i = 0;
2721 struct ql_rcv_buf_cb *lrg_buf_cb;
2723 for (i = 0; i < qdev->num_large_buffers; i++) {
2724 lrg_buf_cb = &qdev->lrg_buf[i];
2725 if (lrg_buf_cb->skb) {
2726 dev_kfree_skb(lrg_buf_cb->skb);
2727 pci_unmap_single(qdev->pdev,
2728 dma_unmap_addr(lrg_buf_cb, mapaddr),
2729 dma_unmap_len(lrg_buf_cb, maplen),
2730 PCI_DMA_FROMDEVICE);
2731 memset(lrg_buf_cb, 0, sizeof(struct ql_rcv_buf_cb));
2732 } else {
2733 break;
2738 static void ql_init_large_buffers(struct ql3_adapter *qdev)
2740 int i;
2741 struct ql_rcv_buf_cb *lrg_buf_cb;
2742 struct bufq_addr_element *buf_addr_ele = qdev->lrg_buf_q_virt_addr;
2744 for (i = 0; i < qdev->num_large_buffers; i++) {
2745 lrg_buf_cb = &qdev->lrg_buf[i];
2746 buf_addr_ele->addr_high = lrg_buf_cb->buf_phy_addr_high;
2747 buf_addr_ele->addr_low = lrg_buf_cb->buf_phy_addr_low;
2748 buf_addr_ele++;
2750 qdev->lrg_buf_index = 0;
2751 qdev->lrg_buf_skb_check = 0;
2754 static int ql_alloc_large_buffers(struct ql3_adapter *qdev)
2756 int i;
2757 struct ql_rcv_buf_cb *lrg_buf_cb;
2758 struct sk_buff *skb;
2759 dma_addr_t map;
2760 int err;
2762 for (i = 0; i < qdev->num_large_buffers; i++) {
2763 skb = netdev_alloc_skb(qdev->ndev,
2764 qdev->lrg_buffer_len);
2765 if (unlikely(!skb)) {
2766 /* Better luck next round */
2767 netdev_err(qdev->ndev,
2768 "large buff alloc failed for %d bytes at index %d\n",
2769 qdev->lrg_buffer_len * 2, i);
2770 ql_free_large_buffers(qdev);
2771 return -ENOMEM;
2772 } else {
2774 lrg_buf_cb = &qdev->lrg_buf[i];
2775 memset(lrg_buf_cb, 0, sizeof(struct ql_rcv_buf_cb));
2776 lrg_buf_cb->index = i;
2777 lrg_buf_cb->skb = skb;
2779 * We save some space to copy the ethhdr from first
2780 * buffer
2782 skb_reserve(skb, QL_HEADER_SPACE);
2783 map = pci_map_single(qdev->pdev,
2784 skb->data,
2785 qdev->lrg_buffer_len -
2786 QL_HEADER_SPACE,
2787 PCI_DMA_FROMDEVICE);
2789 err = pci_dma_mapping_error(qdev->pdev, map);
2790 if (err) {
2791 netdev_err(qdev->ndev,
2792 "PCI mapping failed with error: %d\n",
2793 err);
2794 ql_free_large_buffers(qdev);
2795 return -ENOMEM;
2798 dma_unmap_addr_set(lrg_buf_cb, mapaddr, map);
2799 dma_unmap_len_set(lrg_buf_cb, maplen,
2800 qdev->lrg_buffer_len -
2801 QL_HEADER_SPACE);
2802 lrg_buf_cb->buf_phy_addr_low =
2803 cpu_to_le32(LS_64BITS(map));
2804 lrg_buf_cb->buf_phy_addr_high =
2805 cpu_to_le32(MS_64BITS(map));
2808 return 0;
2811 static void ql_free_send_free_list(struct ql3_adapter *qdev)
2813 struct ql_tx_buf_cb *tx_cb;
2814 int i;
2816 tx_cb = &qdev->tx_buf[0];
2817 for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
2818 kfree(tx_cb->oal);
2819 tx_cb->oal = NULL;
2820 tx_cb++;
2824 static int ql_create_send_free_list(struct ql3_adapter *qdev)
2826 struct ql_tx_buf_cb *tx_cb;
2827 int i;
2828 struct ob_mac_iocb_req *req_q_curr = qdev->req_q_virt_addr;
2830 /* Create free list of transmit buffers */
2831 for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
2833 tx_cb = &qdev->tx_buf[i];
2834 tx_cb->skb = NULL;
2835 tx_cb->queue_entry = req_q_curr;
2836 req_q_curr++;
2837 tx_cb->oal = kmalloc(512, GFP_KERNEL);
2838 if (tx_cb->oal == NULL)
2839 return -1;
2841 return 0;
2844 static int ql_alloc_mem_resources(struct ql3_adapter *qdev)
2846 if (qdev->ndev->mtu == NORMAL_MTU_SIZE) {
2847 qdev->num_lbufq_entries = NUM_LBUFQ_ENTRIES;
2848 qdev->lrg_buffer_len = NORMAL_MTU_SIZE;
2849 } else if (qdev->ndev->mtu == JUMBO_MTU_SIZE) {
2851 * Bigger buffers, so less of them.
2853 qdev->num_lbufq_entries = JUMBO_NUM_LBUFQ_ENTRIES;
2854 qdev->lrg_buffer_len = JUMBO_MTU_SIZE;
2855 } else {
2856 netdev_err(qdev->ndev, "Invalid mtu size: %d. Only %d and %d are accepted.\n",
2857 qdev->ndev->mtu, NORMAL_MTU_SIZE, JUMBO_MTU_SIZE);
2858 return -ENOMEM;
2860 qdev->num_large_buffers =
2861 qdev->num_lbufq_entries * QL_ADDR_ELE_PER_BUFQ_ENTRY;
2862 qdev->lrg_buffer_len += VLAN_ETH_HLEN + VLAN_ID_LEN + QL_HEADER_SPACE;
2863 qdev->max_frame_size =
2864 (qdev->lrg_buffer_len - QL_HEADER_SPACE) + ETHERNET_CRC_SIZE;
2867 * First allocate a page of shared memory and use it for shadow
2868 * locations of Network Request Queue Consumer Address Register and
2869 * Network Completion Queue Producer Index Register
2871 qdev->shadow_reg_virt_addr =
2872 pci_alloc_consistent(qdev->pdev,
2873 PAGE_SIZE, &qdev->shadow_reg_phy_addr);
2875 if (qdev->shadow_reg_virt_addr != NULL) {
2876 qdev->preq_consumer_index = qdev->shadow_reg_virt_addr;
2877 qdev->req_consumer_index_phy_addr_high =
2878 MS_64BITS(qdev->shadow_reg_phy_addr);
2879 qdev->req_consumer_index_phy_addr_low =
2880 LS_64BITS(qdev->shadow_reg_phy_addr);
2882 qdev->prsp_producer_index =
2883 (__le32 *) (((u8 *) qdev->preq_consumer_index) + 8);
2884 qdev->rsp_producer_index_phy_addr_high =
2885 qdev->req_consumer_index_phy_addr_high;
2886 qdev->rsp_producer_index_phy_addr_low =
2887 qdev->req_consumer_index_phy_addr_low + 8;
2888 } else {
2889 netdev_err(qdev->ndev, "shadowReg Alloc failed\n");
2890 return -ENOMEM;
2893 if (ql_alloc_net_req_rsp_queues(qdev) != 0) {
2894 netdev_err(qdev->ndev, "ql_alloc_net_req_rsp_queues failed\n");
2895 goto err_req_rsp;
2898 if (ql_alloc_buffer_queues(qdev) != 0) {
2899 netdev_err(qdev->ndev, "ql_alloc_buffer_queues failed\n");
2900 goto err_buffer_queues;
2903 if (ql_alloc_small_buffers(qdev) != 0) {
2904 netdev_err(qdev->ndev, "ql_alloc_small_buffers failed\n");
2905 goto err_small_buffers;
2908 if (ql_alloc_large_buffers(qdev) != 0) {
2909 netdev_err(qdev->ndev, "ql_alloc_large_buffers failed\n");
2910 goto err_small_buffers;
2913 /* Initialize the large buffer queue. */
2914 ql_init_large_buffers(qdev);
2915 if (ql_create_send_free_list(qdev))
2916 goto err_free_list;
2918 qdev->rsp_current = qdev->rsp_q_virt_addr;
2920 return 0;
2921 err_free_list:
2922 ql_free_send_free_list(qdev);
2923 err_small_buffers:
2924 ql_free_buffer_queues(qdev);
2925 err_buffer_queues:
2926 ql_free_net_req_rsp_queues(qdev);
2927 err_req_rsp:
2928 pci_free_consistent(qdev->pdev,
2929 PAGE_SIZE,
2930 qdev->shadow_reg_virt_addr,
2931 qdev->shadow_reg_phy_addr);
2933 return -ENOMEM;
2936 static void ql_free_mem_resources(struct ql3_adapter *qdev)
2938 ql_free_send_free_list(qdev);
2939 ql_free_large_buffers(qdev);
2940 ql_free_small_buffers(qdev);
2941 ql_free_buffer_queues(qdev);
2942 ql_free_net_req_rsp_queues(qdev);
2943 if (qdev->shadow_reg_virt_addr != NULL) {
2944 pci_free_consistent(qdev->pdev,
2945 PAGE_SIZE,
2946 qdev->shadow_reg_virt_addr,
2947 qdev->shadow_reg_phy_addr);
2948 qdev->shadow_reg_virt_addr = NULL;
2952 static int ql_init_misc_registers(struct ql3_adapter *qdev)
2954 struct ql3xxx_local_ram_registers __iomem *local_ram =
2955 (void __iomem *)qdev->mem_map_registers;
2957 if (ql_sem_spinlock(qdev, QL_DDR_RAM_SEM_MASK,
2958 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
2959 2) << 4))
2960 return -1;
2962 ql_write_page2_reg(qdev,
2963 &local_ram->bufletSize, qdev->nvram_data.bufletSize);
2965 ql_write_page2_reg(qdev,
2966 &local_ram->maxBufletCount,
2967 qdev->nvram_data.bufletCount);
2969 ql_write_page2_reg(qdev,
2970 &local_ram->freeBufletThresholdLow,
2971 (qdev->nvram_data.tcpWindowThreshold25 << 16) |
2972 (qdev->nvram_data.tcpWindowThreshold0));
2974 ql_write_page2_reg(qdev,
2975 &local_ram->freeBufletThresholdHigh,
2976 qdev->nvram_data.tcpWindowThreshold50);
2978 ql_write_page2_reg(qdev,
2979 &local_ram->ipHashTableBase,
2980 (qdev->nvram_data.ipHashTableBaseHi << 16) |
2981 qdev->nvram_data.ipHashTableBaseLo);
2982 ql_write_page2_reg(qdev,
2983 &local_ram->ipHashTableCount,
2984 qdev->nvram_data.ipHashTableSize);
2985 ql_write_page2_reg(qdev,
2986 &local_ram->tcpHashTableBase,
2987 (qdev->nvram_data.tcpHashTableBaseHi << 16) |
2988 qdev->nvram_data.tcpHashTableBaseLo);
2989 ql_write_page2_reg(qdev,
2990 &local_ram->tcpHashTableCount,
2991 qdev->nvram_data.tcpHashTableSize);
2992 ql_write_page2_reg(qdev,
2993 &local_ram->ncbBase,
2994 (qdev->nvram_data.ncbTableBaseHi << 16) |
2995 qdev->nvram_data.ncbTableBaseLo);
2996 ql_write_page2_reg(qdev,
2997 &local_ram->maxNcbCount,
2998 qdev->nvram_data.ncbTableSize);
2999 ql_write_page2_reg(qdev,
3000 &local_ram->drbBase,
3001 (qdev->nvram_data.drbTableBaseHi << 16) |
3002 qdev->nvram_data.drbTableBaseLo);
3003 ql_write_page2_reg(qdev,
3004 &local_ram->maxDrbCount,
3005 qdev->nvram_data.drbTableSize);
3006 ql_sem_unlock(qdev, QL_DDR_RAM_SEM_MASK);
3007 return 0;
3010 static int ql_adapter_initialize(struct ql3_adapter *qdev)
3012 u32 value;
3013 struct ql3xxx_port_registers __iomem *port_regs =
3014 qdev->mem_map_registers;
3015 __iomem u32 *spir = &port_regs->CommonRegs.serialPortInterfaceReg;
3016 struct ql3xxx_host_memory_registers __iomem *hmem_regs =
3017 (void __iomem *)port_regs;
3018 u32 delay = 10;
3019 int status = 0;
3021 if (ql_mii_setup(qdev))
3022 return -1;
3024 /* Bring out PHY out of reset */
3025 ql_write_common_reg(qdev, spir,
3026 (ISP_SERIAL_PORT_IF_WE |
3027 (ISP_SERIAL_PORT_IF_WE << 16)));
3028 /* Give the PHY time to come out of reset. */
3029 mdelay(100);
3030 qdev->port_link_state = LS_DOWN;
3031 netif_carrier_off(qdev->ndev);
3033 /* V2 chip fix for ARS-39168. */
3034 ql_write_common_reg(qdev, spir,
3035 (ISP_SERIAL_PORT_IF_SDE |
3036 (ISP_SERIAL_PORT_IF_SDE << 16)));
3038 /* Request Queue Registers */
3039 *((u32 *)(qdev->preq_consumer_index)) = 0;
3040 atomic_set(&qdev->tx_count, NUM_REQ_Q_ENTRIES);
3041 qdev->req_producer_index = 0;
3043 ql_write_page1_reg(qdev,
3044 &hmem_regs->reqConsumerIndexAddrHigh,
3045 qdev->req_consumer_index_phy_addr_high);
3046 ql_write_page1_reg(qdev,
3047 &hmem_regs->reqConsumerIndexAddrLow,
3048 qdev->req_consumer_index_phy_addr_low);
3050 ql_write_page1_reg(qdev,
3051 &hmem_regs->reqBaseAddrHigh,
3052 MS_64BITS(qdev->req_q_phy_addr));
3053 ql_write_page1_reg(qdev,
3054 &hmem_regs->reqBaseAddrLow,
3055 LS_64BITS(qdev->req_q_phy_addr));
3056 ql_write_page1_reg(qdev, &hmem_regs->reqLength, NUM_REQ_Q_ENTRIES);
3058 /* Response Queue Registers */
3059 *((__le16 *) (qdev->prsp_producer_index)) = 0;
3060 qdev->rsp_consumer_index = 0;
3061 qdev->rsp_current = qdev->rsp_q_virt_addr;
3063 ql_write_page1_reg(qdev,
3064 &hmem_regs->rspProducerIndexAddrHigh,
3065 qdev->rsp_producer_index_phy_addr_high);
3067 ql_write_page1_reg(qdev,
3068 &hmem_regs->rspProducerIndexAddrLow,
3069 qdev->rsp_producer_index_phy_addr_low);
3071 ql_write_page1_reg(qdev,
3072 &hmem_regs->rspBaseAddrHigh,
3073 MS_64BITS(qdev->rsp_q_phy_addr));
3075 ql_write_page1_reg(qdev,
3076 &hmem_regs->rspBaseAddrLow,
3077 LS_64BITS(qdev->rsp_q_phy_addr));
3079 ql_write_page1_reg(qdev, &hmem_regs->rspLength, NUM_RSP_Q_ENTRIES);
3081 /* Large Buffer Queue */
3082 ql_write_page1_reg(qdev,
3083 &hmem_regs->rxLargeQBaseAddrHigh,
3084 MS_64BITS(qdev->lrg_buf_q_phy_addr));
3086 ql_write_page1_reg(qdev,
3087 &hmem_regs->rxLargeQBaseAddrLow,
3088 LS_64BITS(qdev->lrg_buf_q_phy_addr));
3090 ql_write_page1_reg(qdev,
3091 &hmem_regs->rxLargeQLength,
3092 qdev->num_lbufq_entries);
3094 ql_write_page1_reg(qdev,
3095 &hmem_regs->rxLargeBufferLength,
3096 qdev->lrg_buffer_len);
3098 /* Small Buffer Queue */
3099 ql_write_page1_reg(qdev,
3100 &hmem_regs->rxSmallQBaseAddrHigh,
3101 MS_64BITS(qdev->small_buf_q_phy_addr));
3103 ql_write_page1_reg(qdev,
3104 &hmem_regs->rxSmallQBaseAddrLow,
3105 LS_64BITS(qdev->small_buf_q_phy_addr));
3107 ql_write_page1_reg(qdev, &hmem_regs->rxSmallQLength, NUM_SBUFQ_ENTRIES);
3108 ql_write_page1_reg(qdev,
3109 &hmem_regs->rxSmallBufferLength,
3110 QL_SMALL_BUFFER_SIZE);
3112 qdev->small_buf_q_producer_index = NUM_SBUFQ_ENTRIES - 1;
3113 qdev->small_buf_release_cnt = 8;
3114 qdev->lrg_buf_q_producer_index = qdev->num_lbufq_entries - 1;
3115 qdev->lrg_buf_release_cnt = 8;
3116 qdev->lrg_buf_next_free = qdev->lrg_buf_q_virt_addr;
3117 qdev->small_buf_index = 0;
3118 qdev->lrg_buf_index = 0;
3119 qdev->lrg_buf_free_count = 0;
3120 qdev->lrg_buf_free_head = NULL;
3121 qdev->lrg_buf_free_tail = NULL;
3123 ql_write_common_reg(qdev,
3124 &port_regs->CommonRegs.
3125 rxSmallQProducerIndex,
3126 qdev->small_buf_q_producer_index);
3127 ql_write_common_reg(qdev,
3128 &port_regs->CommonRegs.
3129 rxLargeQProducerIndex,
3130 qdev->lrg_buf_q_producer_index);
3133 * Find out if the chip has already been initialized. If it has, then
3134 * we skip some of the initialization.
3136 clear_bit(QL_LINK_MASTER, &qdev->flags);
3137 value = ql_read_page0_reg(qdev, &port_regs->portStatus);
3138 if ((value & PORT_STATUS_IC) == 0) {
3140 /* Chip has not been configured yet, so let it rip. */
3141 if (ql_init_misc_registers(qdev)) {
3142 status = -1;
3143 goto out;
3146 value = qdev->nvram_data.tcpMaxWindowSize;
3147 ql_write_page0_reg(qdev, &port_regs->tcpMaxWindow, value);
3149 value = (0xFFFF << 16) | qdev->nvram_data.extHwConfig;
3151 if (ql_sem_spinlock(qdev, QL_FLASH_SEM_MASK,
3152 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index)
3153 * 2) << 13)) {
3154 status = -1;
3155 goto out;
3157 ql_write_page0_reg(qdev, &port_regs->ExternalHWConfig, value);
3158 ql_write_page0_reg(qdev, &port_regs->InternalChipConfig,
3159 (((INTERNAL_CHIP_SD | INTERNAL_CHIP_WE) <<
3160 16) | (INTERNAL_CHIP_SD |
3161 INTERNAL_CHIP_WE)));
3162 ql_sem_unlock(qdev, QL_FLASH_SEM_MASK);
3165 if (qdev->mac_index)
3166 ql_write_page0_reg(qdev,
3167 &port_regs->mac1MaxFrameLengthReg,
3168 qdev->max_frame_size);
3169 else
3170 ql_write_page0_reg(qdev,
3171 &port_regs->mac0MaxFrameLengthReg,
3172 qdev->max_frame_size);
3174 if (ql_sem_spinlock(qdev, QL_PHY_GIO_SEM_MASK,
3175 (QL_RESOURCE_BITS_BASE_CODE | (qdev->mac_index) *
3176 2) << 7)) {
3177 status = -1;
3178 goto out;
3181 PHY_Setup(qdev);
3182 ql_init_scan_mode(qdev);
3183 ql_get_phy_owner(qdev);
3185 /* Load the MAC Configuration */
3187 /* Program lower 32 bits of the MAC address */
3188 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3189 (MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16));
3190 ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
3191 ((qdev->ndev->dev_addr[2] << 24)
3192 | (qdev->ndev->dev_addr[3] << 16)
3193 | (qdev->ndev->dev_addr[4] << 8)
3194 | qdev->ndev->dev_addr[5]));
3196 /* Program top 16 bits of the MAC address */
3197 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3198 ((MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16) | 1));
3199 ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
3200 ((qdev->ndev->dev_addr[0] << 8)
3201 | qdev->ndev->dev_addr[1]));
3203 /* Enable Primary MAC */
3204 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3205 ((MAC_ADDR_INDIRECT_PTR_REG_PE << 16) |
3206 MAC_ADDR_INDIRECT_PTR_REG_PE));
3208 /* Clear Primary and Secondary IP addresses */
3209 ql_write_page0_reg(qdev, &port_regs->ipAddrIndexReg,
3210 ((IP_ADDR_INDEX_REG_MASK << 16) |
3211 (qdev->mac_index << 2)));
3212 ql_write_page0_reg(qdev, &port_regs->ipAddrDataReg, 0);
3214 ql_write_page0_reg(qdev, &port_regs->ipAddrIndexReg,
3215 ((IP_ADDR_INDEX_REG_MASK << 16) |
3216 ((qdev->mac_index << 2) + 1)));
3217 ql_write_page0_reg(qdev, &port_regs->ipAddrDataReg, 0);
3219 ql_sem_unlock(qdev, QL_PHY_GIO_SEM_MASK);
3221 /* Indicate Configuration Complete */
3222 ql_write_page0_reg(qdev,
3223 &port_regs->portControl,
3224 ((PORT_CONTROL_CC << 16) | PORT_CONTROL_CC));
3226 do {
3227 value = ql_read_page0_reg(qdev, &port_regs->portStatus);
3228 if (value & PORT_STATUS_IC)
3229 break;
3230 spin_unlock_irq(&qdev->hw_lock);
3231 msleep(500);
3232 spin_lock_irq(&qdev->hw_lock);
3233 } while (--delay);
3235 if (delay == 0) {
3236 netdev_err(qdev->ndev, "Hw Initialization timeout\n");
3237 status = -1;
3238 goto out;
3241 /* Enable Ethernet Function */
3242 if (qdev->device_id == QL3032_DEVICE_ID) {
3243 value =
3244 (QL3032_PORT_CONTROL_EF | QL3032_PORT_CONTROL_KIE |
3245 QL3032_PORT_CONTROL_EIv6 | QL3032_PORT_CONTROL_EIv4 |
3246 QL3032_PORT_CONTROL_ET);
3247 ql_write_page0_reg(qdev, &port_regs->functionControl,
3248 ((value << 16) | value));
3249 } else {
3250 value =
3251 (PORT_CONTROL_EF | PORT_CONTROL_ET | PORT_CONTROL_EI |
3252 PORT_CONTROL_HH);
3253 ql_write_page0_reg(qdev, &port_regs->portControl,
3254 ((value << 16) | value));
3258 out:
3259 return status;
3263 * Caller holds hw_lock.
3265 static int ql_adapter_reset(struct ql3_adapter *qdev)
3267 struct ql3xxx_port_registers __iomem *port_regs =
3268 qdev->mem_map_registers;
3269 int status = 0;
3270 u16 value;
3271 int max_wait_time;
3273 set_bit(QL_RESET_ACTIVE, &qdev->flags);
3274 clear_bit(QL_RESET_DONE, &qdev->flags);
3277 * Issue soft reset to chip.
3279 netdev_printk(KERN_DEBUG, qdev->ndev, "Issue soft reset to chip\n");
3280 ql_write_common_reg(qdev,
3281 &port_regs->CommonRegs.ispControlStatus,
3282 ((ISP_CONTROL_SR << 16) | ISP_CONTROL_SR));
3284 /* Wait 3 seconds for reset to complete. */
3285 netdev_printk(KERN_DEBUG, qdev->ndev,
3286 "Wait 10 milliseconds for reset to complete\n");
3288 /* Wait until the firmware tells us the Soft Reset is done */
3289 max_wait_time = 5;
3290 do {
3291 value =
3292 ql_read_common_reg(qdev,
3293 &port_regs->CommonRegs.ispControlStatus);
3294 if ((value & ISP_CONTROL_SR) == 0)
3295 break;
3297 ssleep(1);
3298 } while ((--max_wait_time));
3301 * Also, make sure that the Network Reset Interrupt bit has been
3302 * cleared after the soft reset has taken place.
3304 value =
3305 ql_read_common_reg(qdev, &port_regs->CommonRegs.ispControlStatus);
3306 if (value & ISP_CONTROL_RI) {
3307 netdev_printk(KERN_DEBUG, qdev->ndev,
3308 "clearing RI after reset\n");
3309 ql_write_common_reg(qdev,
3310 &port_regs->CommonRegs.
3311 ispControlStatus,
3312 ((ISP_CONTROL_RI << 16) | ISP_CONTROL_RI));
3315 if (max_wait_time == 0) {
3316 /* Issue Force Soft Reset */
3317 ql_write_common_reg(qdev,
3318 &port_regs->CommonRegs.
3319 ispControlStatus,
3320 ((ISP_CONTROL_FSR << 16) |
3321 ISP_CONTROL_FSR));
3323 * Wait until the firmware tells us the Force Soft Reset is
3324 * done
3326 max_wait_time = 5;
3327 do {
3328 value = ql_read_common_reg(qdev,
3329 &port_regs->CommonRegs.
3330 ispControlStatus);
3331 if ((value & ISP_CONTROL_FSR) == 0)
3332 break;
3333 ssleep(1);
3334 } while ((--max_wait_time));
3336 if (max_wait_time == 0)
3337 status = 1;
3339 clear_bit(QL_RESET_ACTIVE, &qdev->flags);
3340 set_bit(QL_RESET_DONE, &qdev->flags);
3341 return status;
3344 static void ql_set_mac_info(struct ql3_adapter *qdev)
3346 struct ql3xxx_port_registers __iomem *port_regs =
3347 qdev->mem_map_registers;
3348 u32 value, port_status;
3349 u8 func_number;
3351 /* Get the function number */
3352 value =
3353 ql_read_common_reg_l(qdev, &port_regs->CommonRegs.ispControlStatus);
3354 func_number = (u8) ((value >> 4) & OPCODE_FUNC_ID_MASK);
3355 port_status = ql_read_page0_reg(qdev, &port_regs->portStatus);
3356 switch (value & ISP_CONTROL_FN_MASK) {
3357 case ISP_CONTROL_FN0_NET:
3358 qdev->mac_index = 0;
3359 qdev->mac_ob_opcode = OUTBOUND_MAC_IOCB | func_number;
3360 qdev->mb_bit_mask = FN0_MA_BITS_MASK;
3361 qdev->PHYAddr = PORT0_PHY_ADDRESS;
3362 if (port_status & PORT_STATUS_SM0)
3363 set_bit(QL_LINK_OPTICAL, &qdev->flags);
3364 else
3365 clear_bit(QL_LINK_OPTICAL, &qdev->flags);
3366 break;
3368 case ISP_CONTROL_FN1_NET:
3369 qdev->mac_index = 1;
3370 qdev->mac_ob_opcode = OUTBOUND_MAC_IOCB | func_number;
3371 qdev->mb_bit_mask = FN1_MA_BITS_MASK;
3372 qdev->PHYAddr = PORT1_PHY_ADDRESS;
3373 if (port_status & PORT_STATUS_SM1)
3374 set_bit(QL_LINK_OPTICAL, &qdev->flags);
3375 else
3376 clear_bit(QL_LINK_OPTICAL, &qdev->flags);
3377 break;
3379 case ISP_CONTROL_FN0_SCSI:
3380 case ISP_CONTROL_FN1_SCSI:
3381 default:
3382 netdev_printk(KERN_DEBUG, qdev->ndev,
3383 "Invalid function number, ispControlStatus = 0x%x\n",
3384 value);
3385 break;
3387 qdev->numPorts = qdev->nvram_data.version_and_numPorts >> 8;
3390 static void ql_display_dev_info(struct net_device *ndev)
3392 struct ql3_adapter *qdev = netdev_priv(ndev);
3393 struct pci_dev *pdev = qdev->pdev;
3395 netdev_info(ndev,
3396 "%s Adapter %d RevisionID %d found %s on PCI slot %d\n",
3397 DRV_NAME, qdev->index, qdev->chip_rev_id,
3398 qdev->device_id == QL3032_DEVICE_ID ? "QLA3032" : "QLA3022",
3399 qdev->pci_slot);
3400 netdev_info(ndev, "%s Interface\n",
3401 test_bit(QL_LINK_OPTICAL, &qdev->flags) ? "OPTICAL" : "COPPER");
3404 * Print PCI bus width/type.
3406 netdev_info(ndev, "Bus interface is %s %s\n",
3407 ((qdev->pci_width == 64) ? "64-bit" : "32-bit"),
3408 ((qdev->pci_x) ? "PCI-X" : "PCI"));
3410 netdev_info(ndev, "mem IO base address adjusted = 0x%p\n",
3411 qdev->mem_map_registers);
3412 netdev_info(ndev, "Interrupt number = %d\n", pdev->irq);
3414 netif_info(qdev, probe, ndev, "MAC address %pM\n", ndev->dev_addr);
3417 static int ql_adapter_down(struct ql3_adapter *qdev, int do_reset)
3419 struct net_device *ndev = qdev->ndev;
3420 int retval = 0;
3422 netif_stop_queue(ndev);
3423 netif_carrier_off(ndev);
3425 clear_bit(QL_ADAPTER_UP, &qdev->flags);
3426 clear_bit(QL_LINK_MASTER, &qdev->flags);
3428 ql_disable_interrupts(qdev);
3430 free_irq(qdev->pdev->irq, ndev);
3432 if (qdev->msi && test_bit(QL_MSI_ENABLED, &qdev->flags)) {
3433 netdev_info(qdev->ndev, "calling pci_disable_msi()\n");
3434 clear_bit(QL_MSI_ENABLED, &qdev->flags);
3435 pci_disable_msi(qdev->pdev);
3438 del_timer_sync(&qdev->adapter_timer);
3440 napi_disable(&qdev->napi);
3442 if (do_reset) {
3443 int soft_reset;
3444 unsigned long hw_flags;
3446 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
3447 if (ql_wait_for_drvr_lock(qdev)) {
3448 soft_reset = ql_adapter_reset(qdev);
3449 if (soft_reset) {
3450 netdev_err(ndev, "ql_adapter_reset(%d) FAILED!\n",
3451 qdev->index);
3453 netdev_err(ndev,
3454 "Releasing driver lock via chip reset\n");
3455 } else {
3456 netdev_err(ndev,
3457 "Could not acquire driver lock to do reset!\n");
3458 retval = -1;
3460 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
3462 ql_free_mem_resources(qdev);
3463 return retval;
3466 static int ql_adapter_up(struct ql3_adapter *qdev)
3468 struct net_device *ndev = qdev->ndev;
3469 int err;
3470 unsigned long irq_flags = IRQF_SHARED;
3471 unsigned long hw_flags;
3473 if (ql_alloc_mem_resources(qdev)) {
3474 netdev_err(ndev, "Unable to allocate buffers\n");
3475 return -ENOMEM;
3478 if (qdev->msi) {
3479 if (pci_enable_msi(qdev->pdev)) {
3480 netdev_err(ndev,
3481 "User requested MSI, but MSI failed to initialize. Continuing without MSI.\n");
3482 qdev->msi = 0;
3483 } else {
3484 netdev_info(ndev, "MSI Enabled...\n");
3485 set_bit(QL_MSI_ENABLED, &qdev->flags);
3486 irq_flags &= ~IRQF_SHARED;
3490 err = request_irq(qdev->pdev->irq, ql3xxx_isr,
3491 irq_flags, ndev->name, ndev);
3492 if (err) {
3493 netdev_err(ndev,
3494 "Failed to reserve interrupt %d - already in use\n",
3495 qdev->pdev->irq);
3496 goto err_irq;
3499 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
3501 err = ql_wait_for_drvr_lock(qdev);
3502 if (err) {
3503 err = ql_adapter_initialize(qdev);
3504 if (err) {
3505 netdev_err(ndev, "Unable to initialize adapter\n");
3506 goto err_init;
3508 netdev_err(ndev, "Releasing driver lock\n");
3509 ql_sem_unlock(qdev, QL_DRVR_SEM_MASK);
3510 } else {
3511 netdev_err(ndev, "Could not acquire driver lock\n");
3512 goto err_lock;
3515 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
3517 set_bit(QL_ADAPTER_UP, &qdev->flags);
3519 mod_timer(&qdev->adapter_timer, jiffies + HZ * 1);
3521 napi_enable(&qdev->napi);
3522 ql_enable_interrupts(qdev);
3523 return 0;
3525 err_init:
3526 ql_sem_unlock(qdev, QL_DRVR_SEM_MASK);
3527 err_lock:
3528 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
3529 free_irq(qdev->pdev->irq, ndev);
3530 err_irq:
3531 if (qdev->msi && test_bit(QL_MSI_ENABLED, &qdev->flags)) {
3532 netdev_info(ndev, "calling pci_disable_msi()\n");
3533 clear_bit(QL_MSI_ENABLED, &qdev->flags);
3534 pci_disable_msi(qdev->pdev);
3536 return err;
3539 static int ql_cycle_adapter(struct ql3_adapter *qdev, int reset)
3541 if (ql_adapter_down(qdev, reset) || ql_adapter_up(qdev)) {
3542 netdev_err(qdev->ndev,
3543 "Driver up/down cycle failed, closing device\n");
3544 rtnl_lock();
3545 dev_close(qdev->ndev);
3546 rtnl_unlock();
3547 return -1;
3549 return 0;
3552 static int ql3xxx_close(struct net_device *ndev)
3554 struct ql3_adapter *qdev = netdev_priv(ndev);
3557 * Wait for device to recover from a reset.
3558 * (Rarely happens, but possible.)
3560 while (!test_bit(QL_ADAPTER_UP, &qdev->flags))
3561 msleep(50);
3563 ql_adapter_down(qdev, QL_DO_RESET);
3564 return 0;
3567 static int ql3xxx_open(struct net_device *ndev)
3569 struct ql3_adapter *qdev = netdev_priv(ndev);
3570 return ql_adapter_up(qdev);
3573 static int ql3xxx_set_mac_address(struct net_device *ndev, void *p)
3575 struct ql3_adapter *qdev = netdev_priv(ndev);
3576 struct ql3xxx_port_registers __iomem *port_regs =
3577 qdev->mem_map_registers;
3578 struct sockaddr *addr = p;
3579 unsigned long hw_flags;
3581 if (netif_running(ndev))
3582 return -EBUSY;
3584 if (!is_valid_ether_addr(addr->sa_data))
3585 return -EADDRNOTAVAIL;
3587 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
3589 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
3590 /* Program lower 32 bits of the MAC address */
3591 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3592 (MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16));
3593 ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
3594 ((ndev->dev_addr[2] << 24) | (ndev->
3595 dev_addr[3] << 16) |
3596 (ndev->dev_addr[4] << 8) | ndev->dev_addr[5]));
3598 /* Program top 16 bits of the MAC address */
3599 ql_write_page0_reg(qdev, &port_regs->macAddrIndirectPtrReg,
3600 ((MAC_ADDR_INDIRECT_PTR_REG_RP_MASK << 16) | 1));
3601 ql_write_page0_reg(qdev, &port_regs->macAddrDataReg,
3602 ((ndev->dev_addr[0] << 8) | ndev->dev_addr[1]));
3603 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
3605 return 0;
3608 static void ql3xxx_tx_timeout(struct net_device *ndev)
3610 struct ql3_adapter *qdev = netdev_priv(ndev);
3612 netdev_err(ndev, "Resetting...\n");
3614 * Stop the queues, we've got a problem.
3616 netif_stop_queue(ndev);
3619 * Wake up the worker to process this event.
3621 queue_delayed_work(qdev->workqueue, &qdev->tx_timeout_work, 0);
3624 static void ql_reset_work(struct work_struct *work)
3626 struct ql3_adapter *qdev =
3627 container_of(work, struct ql3_adapter, reset_work.work);
3628 struct net_device *ndev = qdev->ndev;
3629 u32 value;
3630 struct ql_tx_buf_cb *tx_cb;
3631 int max_wait_time, i;
3632 struct ql3xxx_port_registers __iomem *port_regs =
3633 qdev->mem_map_registers;
3634 unsigned long hw_flags;
3636 if (test_bit((QL_RESET_PER_SCSI | QL_RESET_START), &qdev->flags)) {
3637 clear_bit(QL_LINK_MASTER, &qdev->flags);
3640 * Loop through the active list and return the skb.
3642 for (i = 0; i < NUM_REQ_Q_ENTRIES; i++) {
3643 int j;
3644 tx_cb = &qdev->tx_buf[i];
3645 if (tx_cb->skb) {
3646 netdev_printk(KERN_DEBUG, ndev,
3647 "Freeing lost SKB\n");
3648 pci_unmap_single(qdev->pdev,
3649 dma_unmap_addr(&tx_cb->map[0],
3650 mapaddr),
3651 dma_unmap_len(&tx_cb->map[0], maplen),
3652 PCI_DMA_TODEVICE);
3653 for (j = 1; j < tx_cb->seg_count; j++) {
3654 pci_unmap_page(qdev->pdev,
3655 dma_unmap_addr(&tx_cb->map[j],
3656 mapaddr),
3657 dma_unmap_len(&tx_cb->map[j],
3658 maplen),
3659 PCI_DMA_TODEVICE);
3661 dev_kfree_skb(tx_cb->skb);
3662 tx_cb->skb = NULL;
3666 netdev_err(ndev, "Clearing NRI after reset\n");
3667 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
3668 ql_write_common_reg(qdev,
3669 &port_regs->CommonRegs.
3670 ispControlStatus,
3671 ((ISP_CONTROL_RI << 16) | ISP_CONTROL_RI));
3673 * Wait the for Soft Reset to Complete.
3675 max_wait_time = 10;
3676 do {
3677 value = ql_read_common_reg(qdev,
3678 &port_regs->CommonRegs.
3680 ispControlStatus);
3681 if ((value & ISP_CONTROL_SR) == 0) {
3682 netdev_printk(KERN_DEBUG, ndev,
3683 "reset completed\n");
3684 break;
3687 if (value & ISP_CONTROL_RI) {
3688 netdev_printk(KERN_DEBUG, ndev,
3689 "clearing NRI after reset\n");
3690 ql_write_common_reg(qdev,
3691 &port_regs->
3692 CommonRegs.
3693 ispControlStatus,
3694 ((ISP_CONTROL_RI <<
3695 16) | ISP_CONTROL_RI));
3698 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
3699 ssleep(1);
3700 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
3701 } while (--max_wait_time);
3702 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
3704 if (value & ISP_CONTROL_SR) {
3707 * Set the reset flags and clear the board again.
3708 * Nothing else to do...
3710 netdev_err(ndev,
3711 "Timed out waiting for reset to complete\n");
3712 netdev_err(ndev, "Do a reset\n");
3713 clear_bit(QL_RESET_PER_SCSI, &qdev->flags);
3714 clear_bit(QL_RESET_START, &qdev->flags);
3715 ql_cycle_adapter(qdev, QL_DO_RESET);
3716 return;
3719 clear_bit(QL_RESET_ACTIVE, &qdev->flags);
3720 clear_bit(QL_RESET_PER_SCSI, &qdev->flags);
3721 clear_bit(QL_RESET_START, &qdev->flags);
3722 ql_cycle_adapter(qdev, QL_NO_RESET);
3726 static void ql_tx_timeout_work(struct work_struct *work)
3728 struct ql3_adapter *qdev =
3729 container_of(work, struct ql3_adapter, tx_timeout_work.work);
3731 ql_cycle_adapter(qdev, QL_DO_RESET);
3734 static void ql_get_board_info(struct ql3_adapter *qdev)
3736 struct ql3xxx_port_registers __iomem *port_regs =
3737 qdev->mem_map_registers;
3738 u32 value;
3740 value = ql_read_page0_reg_l(qdev, &port_regs->portStatus);
3742 qdev->chip_rev_id = ((value & PORT_STATUS_REV_ID_MASK) >> 12);
3743 if (value & PORT_STATUS_64)
3744 qdev->pci_width = 64;
3745 else
3746 qdev->pci_width = 32;
3747 if (value & PORT_STATUS_X)
3748 qdev->pci_x = 1;
3749 else
3750 qdev->pci_x = 0;
3751 qdev->pci_slot = (u8) PCI_SLOT(qdev->pdev->devfn);
3754 static void ql3xxx_timer(unsigned long ptr)
3756 struct ql3_adapter *qdev = (struct ql3_adapter *)ptr;
3757 queue_delayed_work(qdev->workqueue, &qdev->link_state_work, 0);
3760 static const struct net_device_ops ql3xxx_netdev_ops = {
3761 .ndo_open = ql3xxx_open,
3762 .ndo_start_xmit = ql3xxx_send,
3763 .ndo_stop = ql3xxx_close,
3764 .ndo_change_mtu = eth_change_mtu,
3765 .ndo_validate_addr = eth_validate_addr,
3766 .ndo_set_mac_address = ql3xxx_set_mac_address,
3767 .ndo_tx_timeout = ql3xxx_tx_timeout,
3770 static int __devinit ql3xxx_probe(struct pci_dev *pdev,
3771 const struct pci_device_id *pci_entry)
3773 struct net_device *ndev = NULL;
3774 struct ql3_adapter *qdev = NULL;
3775 static int cards_found;
3776 int uninitialized_var(pci_using_dac), err;
3778 err = pci_enable_device(pdev);
3779 if (err) {
3780 pr_err("%s cannot enable PCI device\n", pci_name(pdev));
3781 goto err_out;
3784 err = pci_request_regions(pdev, DRV_NAME);
3785 if (err) {
3786 pr_err("%s cannot obtain PCI resources\n", pci_name(pdev));
3787 goto err_out_disable_pdev;
3790 pci_set_master(pdev);
3792 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3793 pci_using_dac = 1;
3794 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3795 } else if (!(err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))) {
3796 pci_using_dac = 0;
3797 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
3800 if (err) {
3801 pr_err("%s no usable DMA configuration\n", pci_name(pdev));
3802 goto err_out_free_regions;
3805 ndev = alloc_etherdev(sizeof(struct ql3_adapter));
3806 if (!ndev) {
3807 pr_err("%s could not alloc etherdev\n", pci_name(pdev));
3808 err = -ENOMEM;
3809 goto err_out_free_regions;
3812 SET_NETDEV_DEV(ndev, &pdev->dev);
3814 pci_set_drvdata(pdev, ndev);
3816 qdev = netdev_priv(ndev);
3817 qdev->index = cards_found;
3818 qdev->ndev = ndev;
3819 qdev->pdev = pdev;
3820 qdev->device_id = pci_entry->device;
3821 qdev->port_link_state = LS_DOWN;
3822 if (msi)
3823 qdev->msi = 1;
3825 qdev->msg_enable = netif_msg_init(debug, default_msg);
3827 if (pci_using_dac)
3828 ndev->features |= NETIF_F_HIGHDMA;
3829 if (qdev->device_id == QL3032_DEVICE_ID)
3830 ndev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
3832 qdev->mem_map_registers = pci_ioremap_bar(pdev, 1);
3833 if (!qdev->mem_map_registers) {
3834 pr_err("%s: cannot map device registers\n", pci_name(pdev));
3835 err = -EIO;
3836 goto err_out_free_ndev;
3839 spin_lock_init(&qdev->adapter_lock);
3840 spin_lock_init(&qdev->hw_lock);
3842 /* Set driver entry points */
3843 ndev->netdev_ops = &ql3xxx_netdev_ops;
3844 SET_ETHTOOL_OPS(ndev, &ql3xxx_ethtool_ops);
3845 ndev->watchdog_timeo = 5 * HZ;
3847 netif_napi_add(ndev, &qdev->napi, ql_poll, 64);
3849 ndev->irq = pdev->irq;
3851 /* make sure the EEPROM is good */
3852 if (ql_get_nvram_params(qdev)) {
3853 pr_alert("%s: Adapter #%d, Invalid NVRAM parameters\n",
3854 __func__, qdev->index);
3855 err = -EIO;
3856 goto err_out_iounmap;
3859 ql_set_mac_info(qdev);
3861 /* Validate and set parameters */
3862 if (qdev->mac_index) {
3863 ndev->mtu = qdev->nvram_data.macCfg_port1.etherMtu_mac ;
3864 ql_set_mac_addr(ndev, qdev->nvram_data.funcCfg_fn2.macAddress);
3865 } else {
3866 ndev->mtu = qdev->nvram_data.macCfg_port0.etherMtu_mac ;
3867 ql_set_mac_addr(ndev, qdev->nvram_data.funcCfg_fn0.macAddress);
3869 memcpy(ndev->perm_addr, ndev->dev_addr, ndev->addr_len);
3871 ndev->tx_queue_len = NUM_REQ_Q_ENTRIES;
3873 /* Record PCI bus information. */
3874 ql_get_board_info(qdev);
3877 * Set the Maximum Memory Read Byte Count value. We do this to handle
3878 * jumbo frames.
3880 if (qdev->pci_x)
3881 pci_write_config_word(pdev, (int)0x4e, (u16) 0x0036);
3883 err = register_netdev(ndev);
3884 if (err) {
3885 pr_err("%s: cannot register net device\n", pci_name(pdev));
3886 goto err_out_iounmap;
3889 /* we're going to reset, so assume we have no link for now */
3891 netif_carrier_off(ndev);
3892 netif_stop_queue(ndev);
3894 qdev->workqueue = create_singlethread_workqueue(ndev->name);
3895 INIT_DELAYED_WORK(&qdev->reset_work, ql_reset_work);
3896 INIT_DELAYED_WORK(&qdev->tx_timeout_work, ql_tx_timeout_work);
3897 INIT_DELAYED_WORK(&qdev->link_state_work, ql_link_state_machine_work);
3899 init_timer(&qdev->adapter_timer);
3900 qdev->adapter_timer.function = ql3xxx_timer;
3901 qdev->adapter_timer.expires = jiffies + HZ * 2; /* two second delay */
3902 qdev->adapter_timer.data = (unsigned long)qdev;
3904 if (!cards_found) {
3905 pr_alert("%s\n", DRV_STRING);
3906 pr_alert("Driver name: %s, Version: %s\n",
3907 DRV_NAME, DRV_VERSION);
3909 ql_display_dev_info(ndev);
3911 cards_found++;
3912 return 0;
3914 err_out_iounmap:
3915 iounmap(qdev->mem_map_registers);
3916 err_out_free_ndev:
3917 free_netdev(ndev);
3918 err_out_free_regions:
3919 pci_release_regions(pdev);
3920 err_out_disable_pdev:
3921 pci_disable_device(pdev);
3922 pci_set_drvdata(pdev, NULL);
3923 err_out:
3924 return err;
3927 static void __devexit ql3xxx_remove(struct pci_dev *pdev)
3929 struct net_device *ndev = pci_get_drvdata(pdev);
3930 struct ql3_adapter *qdev = netdev_priv(ndev);
3932 unregister_netdev(ndev);
3934 ql_disable_interrupts(qdev);
3936 if (qdev->workqueue) {
3937 cancel_delayed_work(&qdev->reset_work);
3938 cancel_delayed_work(&qdev->tx_timeout_work);
3939 destroy_workqueue(qdev->workqueue);
3940 qdev->workqueue = NULL;
3943 iounmap(qdev->mem_map_registers);
3944 pci_release_regions(pdev);
3945 pci_set_drvdata(pdev, NULL);
3946 free_netdev(ndev);
3949 static struct pci_driver ql3xxx_driver = {
3951 .name = DRV_NAME,
3952 .id_table = ql3xxx_pci_tbl,
3953 .probe = ql3xxx_probe,
3954 .remove = __devexit_p(ql3xxx_remove),
3957 static int __init ql3xxx_init_module(void)
3959 return pci_register_driver(&ql3xxx_driver);
3962 static void __exit ql3xxx_exit(void)
3964 pci_unregister_driver(&ql3xxx_driver);
3967 module_init(ql3xxx_init_module);
3968 module_exit(ql3xxx_exit);