2 * QLogic qlge NIC HBA Driver
3 * Copyright (c) 2003-2008 QLogic Corporation
4 * See LICENSE.qlge for copyright and licensing details.
5 * Author: Linux qlge network device driver by
6 * Ron Mercer <ron.mercer@qlogic.com>
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/list.h>
13 #include <linux/pci.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/pagemap.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/dmapool.h>
19 #include <linux/mempool.h>
20 #include <linux/spinlock.h>
21 #include <linux/kthread.h>
22 #include <linux/interrupt.h>
23 #include <linux/errno.h>
24 #include <linux/ioport.h>
27 #include <linux/ipv6.h>
29 #include <linux/tcp.h>
30 #include <linux/udp.h>
31 #include <linux/if_arp.h>
32 #include <linux/if_ether.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/ethtool.h>
36 #include <linux/skbuff.h>
37 #include <linux/rtnetlink.h>
38 #include <linux/if_vlan.h>
39 #include <linux/delay.h>
41 #include <linux/vmalloc.h>
42 #include <net/ip6_checksum.h>
46 char qlge_driver_name[] = DRV_NAME;
47 const char qlge_driver_version[] = DRV_VERSION;
49 MODULE_AUTHOR("Ron Mercer <ron.mercer@qlogic.com>");
50 MODULE_DESCRIPTION(DRV_STRING " ");
51 MODULE_LICENSE("GPL");
52 MODULE_VERSION(DRV_VERSION);
54 static const u32 default_msg =
55 NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK |
56 /* NETIF_MSG_TIMER | */
62 NETIF_MSG_INTR | NETIF_MSG_TX_DONE | NETIF_MSG_RX_STATUS |
63 /* NETIF_MSG_PKTDATA | */
64 NETIF_MSG_HW | NETIF_MSG_WOL | 0;
66 static int debug = 0x00007fff; /* defaults above */
67 module_param(debug, int, 0);
68 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
73 static int irq_type = MSIX_IRQ;
74 module_param(irq_type, int, MSIX_IRQ);
75 MODULE_PARM_DESC(irq_type, "0 = MSI-X, 1 = MSI, 2 = Legacy.");
77 static struct pci_device_id qlge_pci_tbl[] __devinitdata = {
78 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QLGE_DEVICE_ID)},
79 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QLGE_DEVICE_ID1)},
80 /* required last entry */
84 MODULE_DEVICE_TABLE(pci, qlge_pci_tbl);
86 /* This hardware semaphore causes exclusive access to
87 * resources shared between the NIC driver, MPI firmware,
88 * FCOE firmware and the FC driver.
90 static int ql_sem_trylock(struct ql_adapter *qdev, u32 sem_mask)
96 sem_bits = SEM_SET << SEM_XGMAC0_SHIFT;
99 sem_bits = SEM_SET << SEM_XGMAC1_SHIFT;
102 sem_bits = SEM_SET << SEM_ICB_SHIFT;
104 case SEM_MAC_ADDR_MASK:
105 sem_bits = SEM_SET << SEM_MAC_ADDR_SHIFT;
108 sem_bits = SEM_SET << SEM_FLASH_SHIFT;
111 sem_bits = SEM_SET << SEM_PROBE_SHIFT;
113 case SEM_RT_IDX_MASK:
114 sem_bits = SEM_SET << SEM_RT_IDX_SHIFT;
116 case SEM_PROC_REG_MASK:
117 sem_bits = SEM_SET << SEM_PROC_REG_SHIFT;
120 QPRINTK(qdev, PROBE, ALERT, "Bad Semaphore mask!.\n");
124 ql_write32(qdev, SEM, sem_bits | sem_mask);
125 return !(ql_read32(qdev, SEM) & sem_bits);
128 int ql_sem_spinlock(struct ql_adapter *qdev, u32 sem_mask)
130 unsigned int seconds = 3;
132 if (!ql_sem_trylock(qdev, sem_mask))
139 void ql_sem_unlock(struct ql_adapter *qdev, u32 sem_mask)
141 ql_write32(qdev, SEM, sem_mask);
142 ql_read32(qdev, SEM); /* flush */
145 /* This function waits for a specific bit to come ready
146 * in a given register. It is used mostly by the initialize
147 * process, but is also used in kernel thread API such as
148 * netdev->set_multi, netdev->set_mac_address, netdev->vlan_rx_add_vid.
150 int ql_wait_reg_rdy(struct ql_adapter *qdev, u32 reg, u32 bit, u32 err_bit)
153 int count = UDELAY_COUNT;
156 temp = ql_read32(qdev, reg);
158 /* check for errors */
159 if (temp & err_bit) {
160 QPRINTK(qdev, PROBE, ALERT,
161 "register 0x%.08x access error, value = 0x%.08x!.\n",
164 } else if (temp & bit)
166 udelay(UDELAY_DELAY);
169 QPRINTK(qdev, PROBE, ALERT,
170 "Timed out waiting for reg %x to come ready.\n", reg);
174 /* The CFG register is used to download TX and RX control blocks
175 * to the chip. This function waits for an operation to complete.
177 static int ql_wait_cfg(struct ql_adapter *qdev, u32 bit)
179 int count = UDELAY_COUNT;
183 temp = ql_read32(qdev, CFG);
188 udelay(UDELAY_DELAY);
195 /* Used to issue init control blocks to hw. Maps control block,
196 * sets address, triggers download, waits for completion.
198 int ql_write_cfg(struct ql_adapter *qdev, void *ptr, int size, u32 bit,
208 (bit & (CFG_LRQ | CFG_LR | CFG_LCQ)) ? PCI_DMA_TODEVICE :
211 map = pci_map_single(qdev->pdev, ptr, size, direction);
212 if (pci_dma_mapping_error(qdev->pdev, map)) {
213 QPRINTK(qdev, IFUP, ERR, "Couldn't map DMA area.\n");
217 status = ql_wait_cfg(qdev, bit);
219 QPRINTK(qdev, IFUP, ERR,
220 "Timed out waiting for CFG to come ready.\n");
224 status = ql_sem_spinlock(qdev, SEM_ICB_MASK);
227 ql_write32(qdev, ICB_L, (u32) map);
228 ql_write32(qdev, ICB_H, (u32) (map >> 32));
229 ql_sem_unlock(qdev, SEM_ICB_MASK); /* does flush too */
231 mask = CFG_Q_MASK | (bit << 16);
232 value = bit | (q_id << CFG_Q_SHIFT);
233 ql_write32(qdev, CFG, (mask | value));
236 * Wait for the bit to clear after signaling hw.
238 status = ql_wait_cfg(qdev, bit);
240 pci_unmap_single(qdev->pdev, map, size, direction);
244 /* Get a specific MAC address from the CAM. Used for debug and reg dump. */
245 int ql_get_mac_addr_reg(struct ql_adapter *qdev, u32 type, u16 index,
251 status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
255 case MAC_ADDR_TYPE_MULTI_MAC:
256 case MAC_ADDR_TYPE_CAM_MAC:
259 ql_wait_reg_rdy(qdev,
260 MAC_ADDR_IDX, MAC_ADDR_MW, MAC_ADDR_E);
263 ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
264 (index << MAC_ADDR_IDX_SHIFT) | /* index */
265 MAC_ADDR_ADR | MAC_ADDR_RS | type); /* type */
267 ql_wait_reg_rdy(qdev,
268 MAC_ADDR_IDX, MAC_ADDR_MR, MAC_ADDR_E);
271 *value++ = ql_read32(qdev, MAC_ADDR_DATA);
273 ql_wait_reg_rdy(qdev,
274 MAC_ADDR_IDX, MAC_ADDR_MW, MAC_ADDR_E);
277 ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
278 (index << MAC_ADDR_IDX_SHIFT) | /* index */
279 MAC_ADDR_ADR | MAC_ADDR_RS | type); /* type */
281 ql_wait_reg_rdy(qdev,
282 MAC_ADDR_IDX, MAC_ADDR_MR, MAC_ADDR_E);
285 *value++ = ql_read32(qdev, MAC_ADDR_DATA);
286 if (type == MAC_ADDR_TYPE_CAM_MAC) {
288 ql_wait_reg_rdy(qdev,
289 MAC_ADDR_IDX, MAC_ADDR_MW, MAC_ADDR_E);
292 ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
293 (index << MAC_ADDR_IDX_SHIFT) | /* index */
294 MAC_ADDR_ADR | MAC_ADDR_RS | type); /* type */
296 ql_wait_reg_rdy(qdev, MAC_ADDR_IDX,
297 MAC_ADDR_MR, MAC_ADDR_E);
300 *value++ = ql_read32(qdev, MAC_ADDR_DATA);
304 case MAC_ADDR_TYPE_VLAN:
305 case MAC_ADDR_TYPE_MULTI_FLTR:
307 QPRINTK(qdev, IFUP, CRIT,
308 "Address type %d not yet supported.\n", type);
312 ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
316 /* Set up a MAC, multicast or VLAN address for the
317 * inbound frame matching.
319 static int ql_set_mac_addr_reg(struct ql_adapter *qdev, u8 *addr, u32 type,
325 status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
329 case MAC_ADDR_TYPE_MULTI_MAC:
330 case MAC_ADDR_TYPE_CAM_MAC:
333 u32 upper = (addr[0] << 8) | addr[1];
335 (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) |
338 QPRINTK(qdev, IFUP, INFO,
339 "Adding %s address %02x:%02x:%02x:%02x:%02x:%02x"
340 " at index %d in the CAM.\n",
342 MAC_ADDR_TYPE_MULTI_MAC) ? "MULTICAST" :
343 "UNICAST"), addr[0], addr[1], addr[2], addr[3],
344 addr[4], addr[5], index);
347 ql_wait_reg_rdy(qdev,
348 MAC_ADDR_IDX, MAC_ADDR_MW, MAC_ADDR_E);
351 ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
352 (index << MAC_ADDR_IDX_SHIFT) | /* index */
354 ql_write32(qdev, MAC_ADDR_DATA, lower);
356 ql_wait_reg_rdy(qdev,
357 MAC_ADDR_IDX, MAC_ADDR_MW, MAC_ADDR_E);
360 ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
361 (index << MAC_ADDR_IDX_SHIFT) | /* index */
363 ql_write32(qdev, MAC_ADDR_DATA, upper);
365 ql_wait_reg_rdy(qdev,
366 MAC_ADDR_IDX, MAC_ADDR_MW, MAC_ADDR_E);
369 ql_write32(qdev, MAC_ADDR_IDX, (offset) | /* offset */
370 (index << MAC_ADDR_IDX_SHIFT) | /* index */
372 /* This field should also include the queue id
373 and possibly the function id. Right now we hardcode
374 the route field to NIC core.
376 if (type == MAC_ADDR_TYPE_CAM_MAC) {
377 cam_output = (CAM_OUT_ROUTE_NIC |
379 func << CAM_OUT_FUNC_SHIFT) |
381 rss_ring_first_cq_id <<
382 CAM_OUT_CQ_ID_SHIFT));
384 cam_output |= CAM_OUT_RV;
385 /* route to NIC core */
386 ql_write32(qdev, MAC_ADDR_DATA, cam_output);
390 case MAC_ADDR_TYPE_VLAN:
392 u32 enable_bit = *((u32 *) &addr[0]);
393 /* For VLAN, the addr actually holds a bit that
394 * either enables or disables the vlan id we are
395 * addressing. It's either MAC_ADDR_E on or off.
396 * That's bit-27 we're talking about.
398 QPRINTK(qdev, IFUP, INFO, "%s VLAN ID %d %s the CAM.\n",
399 (enable_bit ? "Adding" : "Removing"),
400 index, (enable_bit ? "to" : "from"));
403 ql_wait_reg_rdy(qdev,
404 MAC_ADDR_IDX, MAC_ADDR_MW, MAC_ADDR_E);
407 ql_write32(qdev, MAC_ADDR_IDX, offset | /* offset */
408 (index << MAC_ADDR_IDX_SHIFT) | /* index */
410 enable_bit); /* enable/disable */
413 case MAC_ADDR_TYPE_MULTI_FLTR:
415 QPRINTK(qdev, IFUP, CRIT,
416 "Address type %d not yet supported.\n", type);
420 ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
424 /* Get a specific frame routing value from the CAM.
425 * Used for debug and reg dump.
427 int ql_get_routing_reg(struct ql_adapter *qdev, u32 index, u32 *value)
431 status = ql_sem_spinlock(qdev, SEM_RT_IDX_MASK);
435 status = ql_wait_reg_rdy(qdev, RT_IDX, RT_IDX_MW, RT_IDX_E);
439 ql_write32(qdev, RT_IDX,
440 RT_IDX_TYPE_NICQ | RT_IDX_RS | (index << RT_IDX_IDX_SHIFT));
441 status = ql_wait_reg_rdy(qdev, RT_IDX, RT_IDX_MR, RT_IDX_E);
444 *value = ql_read32(qdev, RT_DATA);
446 ql_sem_unlock(qdev, SEM_RT_IDX_MASK);
450 /* The NIC function for this chip has 16 routing indexes. Each one can be used
451 * to route different frame types to various inbound queues. We send broadcast/
452 * multicast/error frames to the default queue for slow handling,
453 * and CAM hit/RSS frames to the fast handling queues.
455 static int ql_set_routing_reg(struct ql_adapter *qdev, u32 index, u32 mask,
461 status = ql_sem_spinlock(qdev, SEM_RT_IDX_MASK);
465 QPRINTK(qdev, IFUP, DEBUG,
466 "%s %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s mask %s the routing reg.\n",
467 (enable ? "Adding" : "Removing"),
468 ((index == RT_IDX_ALL_ERR_SLOT) ? "MAC ERROR/ALL ERROR" : ""),
469 ((index == RT_IDX_IP_CSUM_ERR_SLOT) ? "IP CSUM ERROR" : ""),
471 RT_IDX_TCP_UDP_CSUM_ERR_SLOT) ? "TCP/UDP CSUM ERROR" : ""),
472 ((index == RT_IDX_BCAST_SLOT) ? "BROADCAST" : ""),
473 ((index == RT_IDX_MCAST_MATCH_SLOT) ? "MULTICAST MATCH" : ""),
474 ((index == RT_IDX_ALLMULTI_SLOT) ? "ALL MULTICAST MATCH" : ""),
475 ((index == RT_IDX_UNUSED6_SLOT) ? "UNUSED6" : ""),
476 ((index == RT_IDX_UNUSED7_SLOT) ? "UNUSED7" : ""),
477 ((index == RT_IDX_RSS_MATCH_SLOT) ? "RSS ALL/IPV4 MATCH" : ""),
478 ((index == RT_IDX_RSS_IPV6_SLOT) ? "RSS IPV6" : ""),
479 ((index == RT_IDX_RSS_TCP4_SLOT) ? "RSS TCP4" : ""),
480 ((index == RT_IDX_RSS_TCP6_SLOT) ? "RSS TCP6" : ""),
481 ((index == RT_IDX_CAM_HIT_SLOT) ? "CAM HIT" : ""),
482 ((index == RT_IDX_UNUSED013) ? "UNUSED13" : ""),
483 ((index == RT_IDX_UNUSED014) ? "UNUSED14" : ""),
484 ((index == RT_IDX_PROMISCUOUS_SLOT) ? "PROMISCUOUS" : ""),
485 (enable ? "to" : "from"));
490 value = RT_IDX_DST_CAM_Q | /* dest */
491 RT_IDX_TYPE_NICQ | /* type */
492 (RT_IDX_CAM_HIT_SLOT << RT_IDX_IDX_SHIFT);/* index */
495 case RT_IDX_VALID: /* Promiscuous Mode frames. */
497 value = RT_IDX_DST_DFLT_Q | /* dest */
498 RT_IDX_TYPE_NICQ | /* type */
499 (RT_IDX_PROMISCUOUS_SLOT << RT_IDX_IDX_SHIFT);/* index */
502 case RT_IDX_ERR: /* Pass up MAC,IP,TCP/UDP error frames. */
504 value = RT_IDX_DST_DFLT_Q | /* dest */
505 RT_IDX_TYPE_NICQ | /* type */
506 (RT_IDX_ALL_ERR_SLOT << RT_IDX_IDX_SHIFT);/* index */
509 case RT_IDX_BCAST: /* Pass up Broadcast frames to default Q. */
511 value = RT_IDX_DST_DFLT_Q | /* dest */
512 RT_IDX_TYPE_NICQ | /* type */
513 (RT_IDX_BCAST_SLOT << RT_IDX_IDX_SHIFT);/* index */
516 case RT_IDX_MCAST: /* Pass up All Multicast frames. */
518 value = RT_IDX_DST_CAM_Q | /* dest */
519 RT_IDX_TYPE_NICQ | /* type */
520 (RT_IDX_ALLMULTI_SLOT << RT_IDX_IDX_SHIFT);/* index */
523 case RT_IDX_MCAST_MATCH: /* Pass up matched Multicast frames. */
525 value = RT_IDX_DST_CAM_Q | /* dest */
526 RT_IDX_TYPE_NICQ | /* type */
527 (RT_IDX_MCAST_MATCH_SLOT << RT_IDX_IDX_SHIFT);/* index */
530 case RT_IDX_RSS_MATCH: /* Pass up matched RSS frames. */
532 value = RT_IDX_DST_RSS | /* dest */
533 RT_IDX_TYPE_NICQ | /* type */
534 (RT_IDX_RSS_MATCH_SLOT << RT_IDX_IDX_SHIFT);/* index */
537 case 0: /* Clear the E-bit on an entry. */
539 value = RT_IDX_DST_DFLT_Q | /* dest */
540 RT_IDX_TYPE_NICQ | /* type */
541 (index << RT_IDX_IDX_SHIFT);/* index */
545 QPRINTK(qdev, IFUP, ERR, "Mask type %d not yet supported.\n",
552 status = ql_wait_reg_rdy(qdev, RT_IDX, RT_IDX_MW, 0);
555 value |= (enable ? RT_IDX_E : 0);
556 ql_write32(qdev, RT_IDX, value);
557 ql_write32(qdev, RT_DATA, enable ? mask : 0);
560 ql_sem_unlock(qdev, SEM_RT_IDX_MASK);
564 static void ql_enable_interrupts(struct ql_adapter *qdev)
566 ql_write32(qdev, INTR_EN, (INTR_EN_EI << 16) | INTR_EN_EI);
569 static void ql_disable_interrupts(struct ql_adapter *qdev)
571 ql_write32(qdev, INTR_EN, (INTR_EN_EI << 16));
574 /* If we're running with multiple MSI-X vectors then we enable on the fly.
575 * Otherwise, we may have multiple outstanding workers and don't want to
576 * enable until the last one finishes. In this case, the irq_cnt gets
577 * incremented everytime we queue a worker and decremented everytime
578 * a worker finishes. Once it hits zero we enable the interrupt.
580 u32 ql_enable_completion_interrupt(struct ql_adapter *qdev, u32 intr)
583 unsigned long hw_flags = 0;
584 struct intr_context *ctx = qdev->intr_context + intr;
586 if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags) && intr)) {
587 /* Always enable if we're MSIX multi interrupts and
588 * it's not the default (zeroeth) interrupt.
590 ql_write32(qdev, INTR_EN,
592 var = ql_read32(qdev, STS);
596 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
597 if (atomic_dec_and_test(&ctx->irq_cnt)) {
598 ql_write32(qdev, INTR_EN,
600 var = ql_read32(qdev, STS);
602 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
606 static u32 ql_disable_completion_interrupt(struct ql_adapter *qdev, u32 intr)
609 unsigned long hw_flags;
610 struct intr_context *ctx;
612 /* HW disables for us if we're MSIX multi interrupts and
613 * it's not the default (zeroeth) interrupt.
615 if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags) && intr))
618 ctx = qdev->intr_context + intr;
619 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
620 if (!atomic_read(&ctx->irq_cnt)) {
621 ql_write32(qdev, INTR_EN,
623 var = ql_read32(qdev, STS);
625 atomic_inc(&ctx->irq_cnt);
626 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
630 static void ql_enable_all_completion_interrupts(struct ql_adapter *qdev)
633 for (i = 0; i < qdev->intr_count; i++) {
634 /* The enable call does a atomic_dec_and_test
635 * and enables only if the result is zero.
636 * So we precharge it here.
638 if (unlikely(!test_bit(QL_MSIX_ENABLED, &qdev->flags) ||
640 atomic_set(&qdev->intr_context[i].irq_cnt, 1);
641 ql_enable_completion_interrupt(qdev, i);
646 int ql_read_flash_word(struct ql_adapter *qdev, int offset, u32 *data)
649 /* wait for reg to come ready */
650 status = ql_wait_reg_rdy(qdev,
651 FLASH_ADDR, FLASH_ADDR_RDY, FLASH_ADDR_ERR);
654 /* set up for reg read */
655 ql_write32(qdev, FLASH_ADDR, FLASH_ADDR_R | offset);
656 /* wait for reg to come ready */
657 status = ql_wait_reg_rdy(qdev,
658 FLASH_ADDR, FLASH_ADDR_RDY, FLASH_ADDR_ERR);
662 *data = ql_read32(qdev, FLASH_DATA);
667 static int ql_get_flash_params(struct ql_adapter *qdev)
671 u32 *p = (u32 *)&qdev->flash;
673 if (ql_sem_spinlock(qdev, SEM_FLASH_MASK))
676 for (i = 0; i < sizeof(qdev->flash) / sizeof(u32); i++, p++) {
677 status = ql_read_flash_word(qdev, i, p);
679 QPRINTK(qdev, IFUP, ERR, "Error reading flash.\n");
685 ql_sem_unlock(qdev, SEM_FLASH_MASK);
689 /* xgmac register are located behind the xgmac_addr and xgmac_data
690 * register pair. Each read/write requires us to wait for the ready
691 * bit before reading/writing the data.
693 static int ql_write_xgmac_reg(struct ql_adapter *qdev, u32 reg, u32 data)
696 /* wait for reg to come ready */
697 status = ql_wait_reg_rdy(qdev,
698 XGMAC_ADDR, XGMAC_ADDR_RDY, XGMAC_ADDR_XME);
701 /* write the data to the data reg */
702 ql_write32(qdev, XGMAC_DATA, data);
703 /* trigger the write */
704 ql_write32(qdev, XGMAC_ADDR, reg);
708 /* xgmac register are located behind the xgmac_addr and xgmac_data
709 * register pair. Each read/write requires us to wait for the ready
710 * bit before reading/writing the data.
712 int ql_read_xgmac_reg(struct ql_adapter *qdev, u32 reg, u32 *data)
715 /* wait for reg to come ready */
716 status = ql_wait_reg_rdy(qdev,
717 XGMAC_ADDR, XGMAC_ADDR_RDY, XGMAC_ADDR_XME);
720 /* set up for reg read */
721 ql_write32(qdev, XGMAC_ADDR, reg | XGMAC_ADDR_R);
722 /* wait for reg to come ready */
723 status = ql_wait_reg_rdy(qdev,
724 XGMAC_ADDR, XGMAC_ADDR_RDY, XGMAC_ADDR_XME);
728 *data = ql_read32(qdev, XGMAC_DATA);
733 /* This is used for reading the 64-bit statistics regs. */
734 int ql_read_xgmac_reg64(struct ql_adapter *qdev, u32 reg, u64 *data)
740 status = ql_read_xgmac_reg(qdev, reg, &lo);
744 status = ql_read_xgmac_reg(qdev, reg + 4, &hi);
748 *data = (u64) lo | ((u64) hi << 32);
754 /* Take the MAC Core out of reset.
755 * Enable statistics counting.
756 * Take the transmitter/receiver out of reset.
757 * This functionality may be done in the MPI firmware at a
760 static int ql_port_initialize(struct ql_adapter *qdev)
765 if (ql_sem_trylock(qdev, qdev->xg_sem_mask)) {
766 /* Another function has the semaphore, so
767 * wait for the port init bit to come ready.
769 QPRINTK(qdev, LINK, INFO,
770 "Another function has the semaphore, so wait for the port init bit to come ready.\n");
771 status = ql_wait_reg_rdy(qdev, STS, qdev->port_init, 0);
773 QPRINTK(qdev, LINK, CRIT,
774 "Port initialize timed out.\n");
779 QPRINTK(qdev, LINK, INFO, "Got xgmac semaphore!.\n");
780 /* Set the core reset. */
781 status = ql_read_xgmac_reg(qdev, GLOBAL_CFG, &data);
784 data |= GLOBAL_CFG_RESET;
785 status = ql_write_xgmac_reg(qdev, GLOBAL_CFG, data);
789 /* Clear the core reset and turn on jumbo for receiver. */
790 data &= ~GLOBAL_CFG_RESET; /* Clear core reset. */
791 data |= GLOBAL_CFG_JUMBO; /* Turn on jumbo. */
792 data |= GLOBAL_CFG_TX_STAT_EN;
793 data |= GLOBAL_CFG_RX_STAT_EN;
794 status = ql_write_xgmac_reg(qdev, GLOBAL_CFG, data);
798 /* Enable transmitter, and clear it's reset. */
799 status = ql_read_xgmac_reg(qdev, TX_CFG, &data);
802 data &= ~TX_CFG_RESET; /* Clear the TX MAC reset. */
803 data |= TX_CFG_EN; /* Enable the transmitter. */
804 status = ql_write_xgmac_reg(qdev, TX_CFG, data);
808 /* Enable receiver and clear it's reset. */
809 status = ql_read_xgmac_reg(qdev, RX_CFG, &data);
812 data &= ~RX_CFG_RESET; /* Clear the RX MAC reset. */
813 data |= RX_CFG_EN; /* Enable the receiver. */
814 status = ql_write_xgmac_reg(qdev, RX_CFG, data);
820 ql_write_xgmac_reg(qdev, MAC_TX_PARAMS, MAC_TX_PARAMS_JUMBO | (0x2580 << 16));
824 ql_write_xgmac_reg(qdev, MAC_RX_PARAMS, 0x2580);
828 /* Signal to the world that the port is enabled. */
829 ql_write32(qdev, STS, ((qdev->port_init << 16) | qdev->port_init));
831 ql_sem_unlock(qdev, qdev->xg_sem_mask);
835 /* Get the next large buffer. */
836 struct bq_desc *ql_get_curr_lbuf(struct rx_ring *rx_ring)
838 struct bq_desc *lbq_desc = &rx_ring->lbq[rx_ring->lbq_curr_idx];
839 rx_ring->lbq_curr_idx++;
840 if (rx_ring->lbq_curr_idx == rx_ring->lbq_len)
841 rx_ring->lbq_curr_idx = 0;
842 rx_ring->lbq_free_cnt++;
846 /* Get the next small buffer. */
847 struct bq_desc *ql_get_curr_sbuf(struct rx_ring *rx_ring)
849 struct bq_desc *sbq_desc = &rx_ring->sbq[rx_ring->sbq_curr_idx];
850 rx_ring->sbq_curr_idx++;
851 if (rx_ring->sbq_curr_idx == rx_ring->sbq_len)
852 rx_ring->sbq_curr_idx = 0;
853 rx_ring->sbq_free_cnt++;
857 /* Update an rx ring index. */
858 static void ql_update_cq(struct rx_ring *rx_ring)
860 rx_ring->cnsmr_idx++;
861 rx_ring->curr_entry++;
862 if (unlikely(rx_ring->cnsmr_idx == rx_ring->cq_len)) {
863 rx_ring->cnsmr_idx = 0;
864 rx_ring->curr_entry = rx_ring->cq_base;
868 static void ql_write_cq_idx(struct rx_ring *rx_ring)
870 ql_write_db_reg(rx_ring->cnsmr_idx, rx_ring->cnsmr_idx_db_reg);
873 /* Process (refill) a large buffer queue. */
874 static void ql_update_lbq(struct ql_adapter *qdev, struct rx_ring *rx_ring)
876 int clean_idx = rx_ring->lbq_clean_idx;
877 struct bq_desc *lbq_desc;
878 struct bq_element *bq;
882 while (rx_ring->lbq_free_cnt > 16) {
883 for (i = 0; i < 16; i++) {
884 QPRINTK(qdev, RX_STATUS, DEBUG,
885 "lbq: try cleaning clean_idx = %d.\n",
887 lbq_desc = &rx_ring->lbq[clean_idx];
889 if (lbq_desc->p.lbq_page == NULL) {
890 QPRINTK(qdev, RX_STATUS, DEBUG,
891 "lbq: getting new page for index %d.\n",
893 lbq_desc->p.lbq_page = alloc_page(GFP_ATOMIC);
894 if (lbq_desc->p.lbq_page == NULL) {
895 QPRINTK(qdev, RX_STATUS, ERR,
896 "Couldn't get a page.\n");
899 map = pci_map_page(qdev->pdev,
900 lbq_desc->p.lbq_page,
903 if (pci_dma_mapping_error(qdev->pdev, map)) {
904 QPRINTK(qdev, RX_STATUS, ERR,
905 "PCI mapping failed.\n");
908 pci_unmap_addr_set(lbq_desc, mapaddr, map);
909 pci_unmap_len_set(lbq_desc, maplen, PAGE_SIZE);
910 bq->addr_lo = /*lbq_desc->addr_lo = */
912 bq->addr_hi = /*lbq_desc->addr_hi = */
913 cpu_to_le32(map >> 32);
916 if (clean_idx == rx_ring->lbq_len)
920 rx_ring->lbq_clean_idx = clean_idx;
921 rx_ring->lbq_prod_idx += 16;
922 if (rx_ring->lbq_prod_idx == rx_ring->lbq_len)
923 rx_ring->lbq_prod_idx = 0;
924 QPRINTK(qdev, RX_STATUS, DEBUG,
925 "lbq: updating prod idx = %d.\n",
926 rx_ring->lbq_prod_idx);
927 ql_write_db_reg(rx_ring->lbq_prod_idx,
928 rx_ring->lbq_prod_idx_db_reg);
929 rx_ring->lbq_free_cnt -= 16;
933 /* Process (refill) a small buffer queue. */
934 static void ql_update_sbq(struct ql_adapter *qdev, struct rx_ring *rx_ring)
936 int clean_idx = rx_ring->sbq_clean_idx;
937 struct bq_desc *sbq_desc;
938 struct bq_element *bq;
942 while (rx_ring->sbq_free_cnt > 16) {
943 for (i = 0; i < 16; i++) {
944 sbq_desc = &rx_ring->sbq[clean_idx];
945 QPRINTK(qdev, RX_STATUS, DEBUG,
946 "sbq: try cleaning clean_idx = %d.\n",
949 if (sbq_desc->p.skb == NULL) {
950 QPRINTK(qdev, RX_STATUS, DEBUG,
951 "sbq: getting new skb for index %d.\n",
954 netdev_alloc_skb(qdev->ndev,
955 rx_ring->sbq_buf_size);
956 if (sbq_desc->p.skb == NULL) {
957 QPRINTK(qdev, PROBE, ERR,
958 "Couldn't get an skb.\n");
959 rx_ring->sbq_clean_idx = clean_idx;
962 skb_reserve(sbq_desc->p.skb, QLGE_SB_PAD);
963 map = pci_map_single(qdev->pdev,
964 sbq_desc->p.skb->data,
965 rx_ring->sbq_buf_size /
966 2, PCI_DMA_FROMDEVICE);
967 pci_unmap_addr_set(sbq_desc, mapaddr, map);
968 pci_unmap_len_set(sbq_desc, maplen,
969 rx_ring->sbq_buf_size / 2);
970 bq->addr_lo = cpu_to_le32(map);
971 bq->addr_hi = cpu_to_le32(map >> 32);
975 if (clean_idx == rx_ring->sbq_len)
978 rx_ring->sbq_clean_idx = clean_idx;
979 rx_ring->sbq_prod_idx += 16;
980 if (rx_ring->sbq_prod_idx == rx_ring->sbq_len)
981 rx_ring->sbq_prod_idx = 0;
982 QPRINTK(qdev, RX_STATUS, DEBUG,
983 "sbq: updating prod idx = %d.\n",
984 rx_ring->sbq_prod_idx);
985 ql_write_db_reg(rx_ring->sbq_prod_idx,
986 rx_ring->sbq_prod_idx_db_reg);
988 rx_ring->sbq_free_cnt -= 16;
992 static void ql_update_buffer_queues(struct ql_adapter *qdev,
993 struct rx_ring *rx_ring)
995 ql_update_sbq(qdev, rx_ring);
996 ql_update_lbq(qdev, rx_ring);
999 /* Unmaps tx buffers. Can be called from send() if a pci mapping
1000 * fails at some stage, or from the interrupt when a tx completes.
1002 static void ql_unmap_send(struct ql_adapter *qdev,
1003 struct tx_ring_desc *tx_ring_desc, int mapped)
1006 for (i = 0; i < mapped; i++) {
1007 if (i == 0 || (i == 7 && mapped > 7)) {
1009 * Unmap the skb->data area, or the
1010 * external sglist (AKA the Outbound
1011 * Address List (OAL)).
1012 * If its the zeroeth element, then it's
1013 * the skb->data area. If it's the 7th
1014 * element and there is more than 6 frags,
1018 QPRINTK(qdev, TX_DONE, DEBUG,
1019 "unmapping OAL area.\n");
1021 pci_unmap_single(qdev->pdev,
1022 pci_unmap_addr(&tx_ring_desc->map[i],
1024 pci_unmap_len(&tx_ring_desc->map[i],
1028 QPRINTK(qdev, TX_DONE, DEBUG, "unmapping frag %d.\n",
1030 pci_unmap_page(qdev->pdev,
1031 pci_unmap_addr(&tx_ring_desc->map[i],
1033 pci_unmap_len(&tx_ring_desc->map[i],
1034 maplen), PCI_DMA_TODEVICE);
1040 /* Map the buffers for this transmit. This will return
1041 * NETDEV_TX_BUSY or NETDEV_TX_OK based on success.
1043 static int ql_map_send(struct ql_adapter *qdev,
1044 struct ob_mac_iocb_req *mac_iocb_ptr,
1045 struct sk_buff *skb, struct tx_ring_desc *tx_ring_desc)
1047 int len = skb_headlen(skb);
1049 int frag_idx, err, map_idx = 0;
1050 struct tx_buf_desc *tbd = mac_iocb_ptr->tbd;
1051 int frag_cnt = skb_shinfo(skb)->nr_frags;
1054 QPRINTK(qdev, TX_QUEUED, DEBUG, "frag_cnt = %d.\n", frag_cnt);
1057 * Map the skb buffer first.
1059 map = pci_map_single(qdev->pdev, skb->data, len, PCI_DMA_TODEVICE);
1061 err = pci_dma_mapping_error(qdev->pdev, map);
1063 QPRINTK(qdev, TX_QUEUED, ERR,
1064 "PCI mapping failed with error: %d\n", err);
1066 return NETDEV_TX_BUSY;
1069 tbd->len = cpu_to_le32(len);
1070 tbd->addr = cpu_to_le64(map);
1071 pci_unmap_addr_set(&tx_ring_desc->map[map_idx], mapaddr, map);
1072 pci_unmap_len_set(&tx_ring_desc->map[map_idx], maplen, len);
1076 * This loop fills the remainder of the 8 address descriptors
1077 * in the IOCB. If there are more than 7 fragments, then the
1078 * eighth address desc will point to an external list (OAL).
1079 * When this happens, the remainder of the frags will be stored
1082 for (frag_idx = 0; frag_idx < frag_cnt; frag_idx++, map_idx++) {
1083 skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_idx];
1085 if (frag_idx == 6 && frag_cnt > 7) {
1086 /* Let's tack on an sglist.
1087 * Our control block will now
1089 * iocb->seg[0] = skb->data
1090 * iocb->seg[1] = frag[0]
1091 * iocb->seg[2] = frag[1]
1092 * iocb->seg[3] = frag[2]
1093 * iocb->seg[4] = frag[3]
1094 * iocb->seg[5] = frag[4]
1095 * iocb->seg[6] = frag[5]
1096 * iocb->seg[7] = ptr to OAL (external sglist)
1097 * oal->seg[0] = frag[6]
1098 * oal->seg[1] = frag[7]
1099 * oal->seg[2] = frag[8]
1100 * oal->seg[3] = frag[9]
1101 * oal->seg[4] = frag[10]
1104 /* Tack on the OAL in the eighth segment of IOCB. */
1105 map = pci_map_single(qdev->pdev, &tx_ring_desc->oal,
1108 err = pci_dma_mapping_error(qdev->pdev, map);
1110 QPRINTK(qdev, TX_QUEUED, ERR,
1111 "PCI mapping outbound address list with error: %d\n",
1116 tbd->addr = cpu_to_le64(map);
1118 * The length is the number of fragments
1119 * that remain to be mapped times the length
1120 * of our sglist (OAL).
1123 cpu_to_le32((sizeof(struct tx_buf_desc) *
1124 (frag_cnt - frag_idx)) | TX_DESC_C);
1125 pci_unmap_addr_set(&tx_ring_desc->map[map_idx], mapaddr,
1127 pci_unmap_len_set(&tx_ring_desc->map[map_idx], maplen,
1128 sizeof(struct oal));
1129 tbd = (struct tx_buf_desc *)&tx_ring_desc->oal;
1134 pci_map_page(qdev->pdev, frag->page,
1135 frag->page_offset, frag->size,
1138 err = pci_dma_mapping_error(qdev->pdev, map);
1140 QPRINTK(qdev, TX_QUEUED, ERR,
1141 "PCI mapping frags failed with error: %d.\n",
1146 tbd->addr = cpu_to_le64(map);
1147 tbd->len = cpu_to_le32(frag->size);
1148 pci_unmap_addr_set(&tx_ring_desc->map[map_idx], mapaddr, map);
1149 pci_unmap_len_set(&tx_ring_desc->map[map_idx], maplen,
1153 /* Save the number of segments we've mapped. */
1154 tx_ring_desc->map_cnt = map_idx;
1155 /* Terminate the last segment. */
1156 tbd->len = cpu_to_le32(le32_to_cpu(tbd->len) | TX_DESC_E);
1157 return NETDEV_TX_OK;
1161 * If the first frag mapping failed, then i will be zero.
1162 * This causes the unmap of the skb->data area. Otherwise
1163 * we pass in the number of frags that mapped successfully
1164 * so they can be umapped.
1166 ql_unmap_send(qdev, tx_ring_desc, map_idx);
1167 return NETDEV_TX_BUSY;
1170 void ql_realign_skb(struct sk_buff *skb, int len)
1172 void *temp_addr = skb->data;
1174 /* Undo the skb_reserve(skb,32) we did before
1175 * giving to hardware, and realign data on
1176 * a 2-byte boundary.
1178 skb->data -= QLGE_SB_PAD - NET_IP_ALIGN;
1179 skb->tail -= QLGE_SB_PAD - NET_IP_ALIGN;
1180 skb_copy_to_linear_data(skb, temp_addr,
1185 * This function builds an skb for the given inbound
1186 * completion. It will be rewritten for readability in the near
1187 * future, but for not it works well.
1189 static struct sk_buff *ql_build_rx_skb(struct ql_adapter *qdev,
1190 struct rx_ring *rx_ring,
1191 struct ib_mac_iocb_rsp *ib_mac_rsp)
1193 struct bq_desc *lbq_desc;
1194 struct bq_desc *sbq_desc;
1195 struct sk_buff *skb = NULL;
1196 u32 length = le32_to_cpu(ib_mac_rsp->data_len);
1197 u32 hdr_len = le32_to_cpu(ib_mac_rsp->hdr_len);
1200 * Handle the header buffer if present.
1202 if (ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HV &&
1203 ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS) {
1204 QPRINTK(qdev, RX_STATUS, DEBUG, "Header of %d bytes in small buffer.\n", hdr_len);
1206 * Headers fit nicely into a small buffer.
1208 sbq_desc = ql_get_curr_sbuf(rx_ring);
1209 pci_unmap_single(qdev->pdev,
1210 pci_unmap_addr(sbq_desc, mapaddr),
1211 pci_unmap_len(sbq_desc, maplen),
1212 PCI_DMA_FROMDEVICE);
1213 skb = sbq_desc->p.skb;
1214 ql_realign_skb(skb, hdr_len);
1215 skb_put(skb, hdr_len);
1216 sbq_desc->p.skb = NULL;
1220 * Handle the data buffer(s).
1222 if (unlikely(!length)) { /* Is there data too? */
1223 QPRINTK(qdev, RX_STATUS, DEBUG,
1224 "No Data buffer in this packet.\n");
1228 if (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_DS) {
1229 if (ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS) {
1230 QPRINTK(qdev, RX_STATUS, DEBUG,
1231 "Headers in small, data of %d bytes in small, combine them.\n", length);
1233 * Data is less than small buffer size so it's
1234 * stuffed in a small buffer.
1235 * For this case we append the data
1236 * from the "data" small buffer to the "header" small
1239 sbq_desc = ql_get_curr_sbuf(rx_ring);
1240 pci_dma_sync_single_for_cpu(qdev->pdev,
1242 (sbq_desc, mapaddr),
1245 PCI_DMA_FROMDEVICE);
1246 memcpy(skb_put(skb, length),
1247 sbq_desc->p.skb->data, length);
1248 pci_dma_sync_single_for_device(qdev->pdev,
1255 PCI_DMA_FROMDEVICE);
1257 QPRINTK(qdev, RX_STATUS, DEBUG,
1258 "%d bytes in a single small buffer.\n", length);
1259 sbq_desc = ql_get_curr_sbuf(rx_ring);
1260 skb = sbq_desc->p.skb;
1261 ql_realign_skb(skb, length);
1262 skb_put(skb, length);
1263 pci_unmap_single(qdev->pdev,
1264 pci_unmap_addr(sbq_desc,
1266 pci_unmap_len(sbq_desc,
1268 PCI_DMA_FROMDEVICE);
1269 sbq_desc->p.skb = NULL;
1271 } else if (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_DL) {
1272 if (ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS) {
1273 QPRINTK(qdev, RX_STATUS, DEBUG,
1274 "Header in small, %d bytes in large. Chain large to small!\n", length);
1276 * The data is in a single large buffer. We
1277 * chain it to the header buffer's skb and let
1280 lbq_desc = ql_get_curr_lbuf(rx_ring);
1281 pci_unmap_page(qdev->pdev,
1282 pci_unmap_addr(lbq_desc,
1284 pci_unmap_len(lbq_desc, maplen),
1285 PCI_DMA_FROMDEVICE);
1286 QPRINTK(qdev, RX_STATUS, DEBUG,
1287 "Chaining page to skb.\n");
1288 skb_fill_page_desc(skb, 0, lbq_desc->p.lbq_page,
1291 skb->data_len += length;
1292 skb->truesize += length;
1293 lbq_desc->p.lbq_page = NULL;
1296 * The headers and data are in a single large buffer. We
1297 * copy it to a new skb and let it go. This can happen with
1298 * jumbo mtu on a non-TCP/UDP frame.
1300 lbq_desc = ql_get_curr_lbuf(rx_ring);
1301 skb = netdev_alloc_skb(qdev->ndev, length);
1303 QPRINTK(qdev, PROBE, DEBUG,
1304 "No skb available, drop the packet.\n");
1307 skb_reserve(skb, NET_IP_ALIGN);
1308 QPRINTK(qdev, RX_STATUS, DEBUG,
1309 "%d bytes of headers and data in large. Chain page to new skb and pull tail.\n", length);
1310 skb_fill_page_desc(skb, 0, lbq_desc->p.lbq_page,
1313 skb->data_len += length;
1314 skb->truesize += length;
1316 lbq_desc->p.lbq_page = NULL;
1317 __pskb_pull_tail(skb,
1318 (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_V) ?
1319 VLAN_ETH_HLEN : ETH_HLEN);
1323 * The data is in a chain of large buffers
1324 * pointed to by a small buffer. We loop
1325 * thru and chain them to the our small header
1327 * frags: There are 18 max frags and our small
1328 * buffer will hold 32 of them. The thing is,
1329 * we'll use 3 max for our 9000 byte jumbo
1330 * frames. If the MTU goes up we could
1331 * eventually be in trouble.
1333 int size, offset, i = 0;
1334 struct bq_element *bq, bq_array[8];
1335 sbq_desc = ql_get_curr_sbuf(rx_ring);
1336 pci_unmap_single(qdev->pdev,
1337 pci_unmap_addr(sbq_desc, mapaddr),
1338 pci_unmap_len(sbq_desc, maplen),
1339 PCI_DMA_FROMDEVICE);
1340 if (!(ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS)) {
1342 * This is an non TCP/UDP IP frame, so
1343 * the headers aren't split into a small
1344 * buffer. We have to use the small buffer
1345 * that contains our sg list as our skb to
1346 * send upstairs. Copy the sg list here to
1347 * a local buffer and use it to find the
1350 QPRINTK(qdev, RX_STATUS, DEBUG,
1351 "%d bytes of headers & data in chain of large.\n", length);
1352 skb = sbq_desc->p.skb;
1354 memcpy(bq, skb->data, sizeof(bq_array));
1355 sbq_desc->p.skb = NULL;
1356 skb_reserve(skb, NET_IP_ALIGN);
1358 QPRINTK(qdev, RX_STATUS, DEBUG,
1359 "Headers in small, %d bytes of data in chain of large.\n", length);
1360 bq = (struct bq_element *)sbq_desc->p.skb->data;
1362 while (length > 0) {
1363 lbq_desc = ql_get_curr_lbuf(rx_ring);
1364 if ((bq->addr_lo & ~BQ_MASK) != lbq_desc->bq->addr_lo) {
1365 QPRINTK(qdev, RX_STATUS, ERR,
1366 "Panic!!! bad large buffer address, expected 0x%.08x, got 0x%.08x.\n",
1367 lbq_desc->bq->addr_lo, bq->addr_lo);
1370 pci_unmap_page(qdev->pdev,
1371 pci_unmap_addr(lbq_desc,
1373 pci_unmap_len(lbq_desc,
1375 PCI_DMA_FROMDEVICE);
1376 size = (length < PAGE_SIZE) ? length : PAGE_SIZE;
1379 QPRINTK(qdev, RX_STATUS, DEBUG,
1380 "Adding page %d to skb for %d bytes.\n",
1382 skb_fill_page_desc(skb, i, lbq_desc->p.lbq_page,
1385 skb->data_len += size;
1386 skb->truesize += size;
1388 lbq_desc->p.lbq_page = NULL;
1392 __pskb_pull_tail(skb, (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_V) ?
1393 VLAN_ETH_HLEN : ETH_HLEN);
1398 /* Process an inbound completion from an rx ring. */
1399 static void ql_process_mac_rx_intr(struct ql_adapter *qdev,
1400 struct rx_ring *rx_ring,
1401 struct ib_mac_iocb_rsp *ib_mac_rsp)
1403 struct net_device *ndev = qdev->ndev;
1404 struct sk_buff *skb = NULL;
1406 QL_DUMP_IB_MAC_RSP(ib_mac_rsp);
1408 skb = ql_build_rx_skb(qdev, rx_ring, ib_mac_rsp);
1409 if (unlikely(!skb)) {
1410 QPRINTK(qdev, RX_STATUS, DEBUG,
1411 "No skb available, drop packet.\n");
1415 prefetch(skb->data);
1417 if (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) {
1418 QPRINTK(qdev, RX_STATUS, DEBUG, "%s%s%s Multicast.\n",
1419 (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
1420 IB_MAC_IOCB_RSP_M_HASH ? "Hash" : "",
1421 (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
1422 IB_MAC_IOCB_RSP_M_REG ? "Registered" : "",
1423 (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
1424 IB_MAC_IOCB_RSP_M_PROM ? "Promiscuous" : "");
1426 if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_P) {
1427 QPRINTK(qdev, RX_STATUS, DEBUG, "Promiscuous Packet.\n");
1429 if (ib_mac_rsp->flags1 & (IB_MAC_IOCB_RSP_IE | IB_MAC_IOCB_RSP_TE)) {
1430 QPRINTK(qdev, RX_STATUS, ERR,
1431 "Bad checksum for this %s packet.\n",
1433 flags2 & IB_MAC_IOCB_RSP_T) ? "TCP" : "UDP"));
1434 skb->ip_summed = CHECKSUM_NONE;
1435 } else if (qdev->rx_csum &&
1436 ((ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_T) ||
1437 ((ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_U) &&
1438 !(ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_NU)))) {
1439 QPRINTK(qdev, RX_STATUS, DEBUG, "RX checksum done!\n");
1440 skb->ip_summed = CHECKSUM_UNNECESSARY;
1442 qdev->stats.rx_packets++;
1443 qdev->stats.rx_bytes += skb->len;
1444 skb->protocol = eth_type_trans(skb, ndev);
1445 if (qdev->vlgrp && (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_V)) {
1446 QPRINTK(qdev, RX_STATUS, DEBUG,
1447 "Passing a VLAN packet upstream.\n");
1448 vlan_hwaccel_rx(skb, qdev->vlgrp,
1449 le16_to_cpu(ib_mac_rsp->vlan_id));
1451 QPRINTK(qdev, RX_STATUS, DEBUG,
1452 "Passing a normal packet upstream.\n");
1455 ndev->last_rx = jiffies;
1458 /* Process an outbound completion from an rx ring. */
1459 static void ql_process_mac_tx_intr(struct ql_adapter *qdev,
1460 struct ob_mac_iocb_rsp *mac_rsp)
1462 struct tx_ring *tx_ring;
1463 struct tx_ring_desc *tx_ring_desc;
1465 QL_DUMP_OB_MAC_RSP(mac_rsp);
1466 tx_ring = &qdev->tx_ring[mac_rsp->txq_idx];
1467 tx_ring_desc = &tx_ring->q[mac_rsp->tid];
1468 ql_unmap_send(qdev, tx_ring_desc, tx_ring_desc->map_cnt);
1469 qdev->stats.tx_bytes += tx_ring_desc->map_cnt;
1470 qdev->stats.tx_packets++;
1471 dev_kfree_skb(tx_ring_desc->skb);
1472 tx_ring_desc->skb = NULL;
1474 if (unlikely(mac_rsp->flags1 & (OB_MAC_IOCB_RSP_E |
1477 OB_MAC_IOCB_RSP_P | OB_MAC_IOCB_RSP_B))) {
1478 if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_E) {
1479 QPRINTK(qdev, TX_DONE, WARNING,
1480 "Total descriptor length did not match transfer length.\n");
1482 if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_S) {
1483 QPRINTK(qdev, TX_DONE, WARNING,
1484 "Frame too short to be legal, not sent.\n");
1486 if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_L) {
1487 QPRINTK(qdev, TX_DONE, WARNING,
1488 "Frame too long, but sent anyway.\n");
1490 if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_B) {
1491 QPRINTK(qdev, TX_DONE, WARNING,
1492 "PCI backplane error. Frame not sent.\n");
1495 atomic_inc(&tx_ring->tx_count);
1498 /* Fire up a handler to reset the MPI processor. */
1499 void ql_queue_fw_error(struct ql_adapter *qdev)
1501 netif_stop_queue(qdev->ndev);
1502 netif_carrier_off(qdev->ndev);
1503 queue_delayed_work(qdev->workqueue, &qdev->mpi_reset_work, 0);
1506 void ql_queue_asic_error(struct ql_adapter *qdev)
1508 netif_stop_queue(qdev->ndev);
1509 netif_carrier_off(qdev->ndev);
1510 ql_disable_interrupts(qdev);
1511 queue_delayed_work(qdev->workqueue, &qdev->asic_reset_work, 0);
1514 static void ql_process_chip_ae_intr(struct ql_adapter *qdev,
1515 struct ib_ae_iocb_rsp *ib_ae_rsp)
1517 switch (ib_ae_rsp->event) {
1518 case MGMT_ERR_EVENT:
1519 QPRINTK(qdev, RX_ERR, ERR,
1520 "Management Processor Fatal Error.\n");
1521 ql_queue_fw_error(qdev);
1524 case CAM_LOOKUP_ERR_EVENT:
1525 QPRINTK(qdev, LINK, ERR,
1526 "Multiple CAM hits lookup occurred.\n");
1527 QPRINTK(qdev, DRV, ERR, "This event shouldn't occur.\n");
1528 ql_queue_asic_error(qdev);
1531 case SOFT_ECC_ERROR_EVENT:
1532 QPRINTK(qdev, RX_ERR, ERR, "Soft ECC error detected.\n");
1533 ql_queue_asic_error(qdev);
1536 case PCI_ERR_ANON_BUF_RD:
1537 QPRINTK(qdev, RX_ERR, ERR,
1538 "PCI error occurred when reading anonymous buffers from rx_ring %d.\n",
1540 ql_queue_asic_error(qdev);
1544 QPRINTK(qdev, DRV, ERR, "Unexpected event %d.\n",
1546 ql_queue_asic_error(qdev);
1551 static int ql_clean_outbound_rx_ring(struct rx_ring *rx_ring)
1553 struct ql_adapter *qdev = rx_ring->qdev;
1554 u32 prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
1555 struct ob_mac_iocb_rsp *net_rsp = NULL;
1558 /* While there are entries in the completion queue. */
1559 while (prod != rx_ring->cnsmr_idx) {
1561 QPRINTK(qdev, RX_STATUS, DEBUG,
1562 "cq_id = %d, prod = %d, cnsmr = %d.\n.", rx_ring->cq_id,
1563 prod, rx_ring->cnsmr_idx);
1565 net_rsp = (struct ob_mac_iocb_rsp *)rx_ring->curr_entry;
1567 switch (net_rsp->opcode) {
1569 case OPCODE_OB_MAC_TSO_IOCB:
1570 case OPCODE_OB_MAC_IOCB:
1571 ql_process_mac_tx_intr(qdev, net_rsp);
1574 QPRINTK(qdev, RX_STATUS, DEBUG,
1575 "Hit default case, not handled! dropping the packet, opcode = %x.\n",
1579 ql_update_cq(rx_ring);
1580 prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
1582 ql_write_cq_idx(rx_ring);
1583 if (netif_queue_stopped(qdev->ndev) && net_rsp != NULL) {
1584 struct tx_ring *tx_ring = &qdev->tx_ring[net_rsp->txq_idx];
1585 if (atomic_read(&tx_ring->queue_stopped) &&
1586 (atomic_read(&tx_ring->tx_count) > (tx_ring->wq_len / 4)))
1588 * The queue got stopped because the tx_ring was full.
1589 * Wake it up, because it's now at least 25% empty.
1591 netif_wake_queue(qdev->ndev);
1597 static int ql_clean_inbound_rx_ring(struct rx_ring *rx_ring, int budget)
1599 struct ql_adapter *qdev = rx_ring->qdev;
1600 u32 prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
1601 struct ql_net_rsp_iocb *net_rsp;
1604 /* While there are entries in the completion queue. */
1605 while (prod != rx_ring->cnsmr_idx) {
1607 QPRINTK(qdev, RX_STATUS, DEBUG,
1608 "cq_id = %d, prod = %d, cnsmr = %d.\n.", rx_ring->cq_id,
1609 prod, rx_ring->cnsmr_idx);
1611 net_rsp = rx_ring->curr_entry;
1613 switch (net_rsp->opcode) {
1614 case OPCODE_IB_MAC_IOCB:
1615 ql_process_mac_rx_intr(qdev, rx_ring,
1616 (struct ib_mac_iocb_rsp *)
1620 case OPCODE_IB_AE_IOCB:
1621 ql_process_chip_ae_intr(qdev, (struct ib_ae_iocb_rsp *)
1626 QPRINTK(qdev, RX_STATUS, DEBUG,
1627 "Hit default case, not handled! dropping the packet, opcode = %x.\n",
1632 ql_update_cq(rx_ring);
1633 prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
1634 if (count == budget)
1637 ql_update_buffer_queues(qdev, rx_ring);
1638 ql_write_cq_idx(rx_ring);
1642 static int ql_napi_poll_msix(struct napi_struct *napi, int budget)
1644 struct rx_ring *rx_ring = container_of(napi, struct rx_ring, napi);
1645 struct ql_adapter *qdev = rx_ring->qdev;
1646 int work_done = ql_clean_inbound_rx_ring(rx_ring, budget);
1648 QPRINTK(qdev, RX_STATUS, DEBUG, "Enter, NAPI POLL cq_id = %d.\n",
1651 if (work_done < budget) {
1652 __netif_rx_complete(qdev->ndev, napi);
1653 ql_enable_completion_interrupt(qdev, rx_ring->irq);
1658 static void ql_vlan_rx_register(struct net_device *ndev, struct vlan_group *grp)
1660 struct ql_adapter *qdev = netdev_priv(ndev);
1664 QPRINTK(qdev, IFUP, DEBUG, "Turning on VLAN in NIC_RCV_CFG.\n");
1665 ql_write32(qdev, NIC_RCV_CFG, NIC_RCV_CFG_VLAN_MASK |
1666 NIC_RCV_CFG_VLAN_MATCH_AND_NON);
1668 QPRINTK(qdev, IFUP, DEBUG,
1669 "Turning off VLAN in NIC_RCV_CFG.\n");
1670 ql_write32(qdev, NIC_RCV_CFG, NIC_RCV_CFG_VLAN_MASK);
1674 static void ql_vlan_rx_add_vid(struct net_device *ndev, u16 vid)
1676 struct ql_adapter *qdev = netdev_priv(ndev);
1677 u32 enable_bit = MAC_ADDR_E;
1679 spin_lock(&qdev->hw_lock);
1680 if (ql_set_mac_addr_reg
1681 (qdev, (u8 *) &enable_bit, MAC_ADDR_TYPE_VLAN, vid)) {
1682 QPRINTK(qdev, IFUP, ERR, "Failed to init vlan address.\n");
1684 spin_unlock(&qdev->hw_lock);
1687 static void ql_vlan_rx_kill_vid(struct net_device *ndev, u16 vid)
1689 struct ql_adapter *qdev = netdev_priv(ndev);
1692 spin_lock(&qdev->hw_lock);
1693 if (ql_set_mac_addr_reg
1694 (qdev, (u8 *) &enable_bit, MAC_ADDR_TYPE_VLAN, vid)) {
1695 QPRINTK(qdev, IFUP, ERR, "Failed to clear vlan address.\n");
1697 spin_unlock(&qdev->hw_lock);
1701 /* Worker thread to process a given rx_ring that is dedicated
1702 * to outbound completions.
1704 static void ql_tx_clean(struct work_struct *work)
1706 struct rx_ring *rx_ring =
1707 container_of(work, struct rx_ring, rx_work.work);
1708 ql_clean_outbound_rx_ring(rx_ring);
1709 ql_enable_completion_interrupt(rx_ring->qdev, rx_ring->irq);
1713 /* Worker thread to process a given rx_ring that is dedicated
1714 * to inbound completions.
1716 static void ql_rx_clean(struct work_struct *work)
1718 struct rx_ring *rx_ring =
1719 container_of(work, struct rx_ring, rx_work.work);
1720 ql_clean_inbound_rx_ring(rx_ring, 64);
1721 ql_enable_completion_interrupt(rx_ring->qdev, rx_ring->irq);
1724 /* MSI-X Multiple Vector Interrupt Handler for outbound completions. */
1725 static irqreturn_t qlge_msix_tx_isr(int irq, void *dev_id)
1727 struct rx_ring *rx_ring = dev_id;
1728 queue_delayed_work_on(rx_ring->cpu, rx_ring->qdev->q_workqueue,
1729 &rx_ring->rx_work, 0);
1733 /* MSI-X Multiple Vector Interrupt Handler for inbound completions. */
1734 static irqreturn_t qlge_msix_rx_isr(int irq, void *dev_id)
1736 struct rx_ring *rx_ring = dev_id;
1737 struct ql_adapter *qdev = rx_ring->qdev;
1738 netif_rx_schedule(qdev->ndev, &rx_ring->napi);
1742 /* This handles a fatal error, MPI activity, and the default
1743 * rx_ring in an MSI-X multiple vector environment.
1744 * In MSI/Legacy environment it also process the rest of
1747 static irqreturn_t qlge_isr(int irq, void *dev_id)
1749 struct rx_ring *rx_ring = dev_id;
1750 struct ql_adapter *qdev = rx_ring->qdev;
1751 struct intr_context *intr_context = &qdev->intr_context[0];
1756 spin_lock(&qdev->hw_lock);
1757 if (atomic_read(&qdev->intr_context[0].irq_cnt)) {
1758 QPRINTK(qdev, INTR, DEBUG, "Shared Interrupt, Not ours!\n");
1759 spin_unlock(&qdev->hw_lock);
1762 spin_unlock(&qdev->hw_lock);
1764 var = ql_disable_completion_interrupt(qdev, intr_context->intr);
1767 * Check for fatal error.
1770 ql_queue_asic_error(qdev);
1771 QPRINTK(qdev, INTR, ERR, "Got fatal error, STS = %x.\n", var);
1772 var = ql_read32(qdev, ERR_STS);
1773 QPRINTK(qdev, INTR, ERR,
1774 "Resetting chip. Error Status Register = 0x%x\n", var);
1779 * Check MPI processor activity.
1783 * We've got an async event or mailbox completion.
1784 * Handle it and clear the source of the interrupt.
1786 QPRINTK(qdev, INTR, ERR, "Got MPI processor interrupt.\n");
1787 ql_disable_completion_interrupt(qdev, intr_context->intr);
1788 queue_delayed_work_on(smp_processor_id(), qdev->workqueue,
1789 &qdev->mpi_work, 0);
1794 * Check the default queue and wake handler if active.
1796 rx_ring = &qdev->rx_ring[0];
1797 if (ql_read_sh_reg(rx_ring->prod_idx_sh_reg) != rx_ring->cnsmr_idx) {
1798 QPRINTK(qdev, INTR, INFO, "Waking handler for rx_ring[0].\n");
1799 ql_disable_completion_interrupt(qdev, intr_context->intr);
1800 queue_delayed_work_on(smp_processor_id(), qdev->q_workqueue,
1801 &rx_ring->rx_work, 0);
1805 if (!test_bit(QL_MSIX_ENABLED, &qdev->flags)) {
1807 * Start the DPC for each active queue.
1809 for (i = 1; i < qdev->rx_ring_count; i++) {
1810 rx_ring = &qdev->rx_ring[i];
1811 if (ql_read_sh_reg(rx_ring->prod_idx_sh_reg) !=
1812 rx_ring->cnsmr_idx) {
1813 QPRINTK(qdev, INTR, INFO,
1814 "Waking handler for rx_ring[%d].\n", i);
1815 ql_disable_completion_interrupt(qdev,
1818 if (i < qdev->rss_ring_first_cq_id)
1819 queue_delayed_work_on(rx_ring->cpu,
1824 netif_rx_schedule(qdev->ndev,
1830 ql_enable_completion_interrupt(qdev, intr_context->intr);
1831 return work_done ? IRQ_HANDLED : IRQ_NONE;
1834 static int ql_tso(struct sk_buff *skb, struct ob_mac_tso_iocb_req *mac_iocb_ptr)
1837 if (skb_is_gso(skb)) {
1839 if (skb_header_cloned(skb)) {
1840 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1845 mac_iocb_ptr->opcode = OPCODE_OB_MAC_TSO_IOCB;
1846 mac_iocb_ptr->flags3 |= OB_MAC_TSO_IOCB_IC;
1847 mac_iocb_ptr->frame_len = cpu_to_le32((u32) skb->len);
1848 mac_iocb_ptr->total_hdrs_len =
1849 cpu_to_le16(skb_transport_offset(skb) + tcp_hdrlen(skb));
1850 mac_iocb_ptr->net_trans_offset =
1851 cpu_to_le16(skb_network_offset(skb) |
1852 skb_transport_offset(skb)
1853 << OB_MAC_TRANSPORT_HDR_SHIFT);
1854 mac_iocb_ptr->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1855 mac_iocb_ptr->flags2 |= OB_MAC_TSO_IOCB_LSO;
1856 if (likely(skb->protocol == htons(ETH_P_IP))) {
1857 struct iphdr *iph = ip_hdr(skb);
1859 mac_iocb_ptr->flags1 |= OB_MAC_TSO_IOCB_IP4;
1860 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
1864 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1865 mac_iocb_ptr->flags1 |= OB_MAC_TSO_IOCB_IP6;
1866 tcp_hdr(skb)->check =
1867 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1868 &ipv6_hdr(skb)->daddr,
1876 static void ql_hw_csum_setup(struct sk_buff *skb,
1877 struct ob_mac_tso_iocb_req *mac_iocb_ptr)
1880 struct iphdr *iph = ip_hdr(skb);
1882 mac_iocb_ptr->opcode = OPCODE_OB_MAC_TSO_IOCB;
1883 mac_iocb_ptr->frame_len = cpu_to_le32((u32) skb->len);
1884 mac_iocb_ptr->net_trans_offset =
1885 cpu_to_le16(skb_network_offset(skb) |
1886 skb_transport_offset(skb) << OB_MAC_TRANSPORT_HDR_SHIFT);
1888 mac_iocb_ptr->flags1 |= OB_MAC_TSO_IOCB_IP4;
1889 len = (ntohs(iph->tot_len) - (iph->ihl << 2));
1890 if (likely(iph->protocol == IPPROTO_TCP)) {
1891 check = &(tcp_hdr(skb)->check);
1892 mac_iocb_ptr->flags2 |= OB_MAC_TSO_IOCB_TC;
1893 mac_iocb_ptr->total_hdrs_len =
1894 cpu_to_le16(skb_transport_offset(skb) +
1895 (tcp_hdr(skb)->doff << 2));
1897 check = &(udp_hdr(skb)->check);
1898 mac_iocb_ptr->flags2 |= OB_MAC_TSO_IOCB_UC;
1899 mac_iocb_ptr->total_hdrs_len =
1900 cpu_to_le16(skb_transport_offset(skb) +
1901 sizeof(struct udphdr));
1903 *check = ~csum_tcpudp_magic(iph->saddr,
1904 iph->daddr, len, iph->protocol, 0);
1907 static int qlge_send(struct sk_buff *skb, struct net_device *ndev)
1909 struct tx_ring_desc *tx_ring_desc;
1910 struct ob_mac_iocb_req *mac_iocb_ptr;
1911 struct ql_adapter *qdev = netdev_priv(ndev);
1913 struct tx_ring *tx_ring;
1914 u32 tx_ring_idx = (u32) QL_TXQ_IDX(qdev, skb);
1916 tx_ring = &qdev->tx_ring[tx_ring_idx];
1918 if (unlikely(atomic_read(&tx_ring->tx_count) < 2)) {
1919 QPRINTK(qdev, TX_QUEUED, INFO,
1920 "%s: shutting down tx queue %d du to lack of resources.\n",
1921 __func__, tx_ring_idx);
1922 netif_stop_queue(ndev);
1923 atomic_inc(&tx_ring->queue_stopped);
1924 return NETDEV_TX_BUSY;
1926 tx_ring_desc = &tx_ring->q[tx_ring->prod_idx];
1927 mac_iocb_ptr = tx_ring_desc->queue_entry;
1928 memset((void *)mac_iocb_ptr, 0, sizeof(mac_iocb_ptr));
1929 if (ql_map_send(qdev, mac_iocb_ptr, skb, tx_ring_desc) != NETDEV_TX_OK) {
1930 QPRINTK(qdev, TX_QUEUED, ERR, "Could not map the segments.\n");
1931 return NETDEV_TX_BUSY;
1934 mac_iocb_ptr->opcode = OPCODE_OB_MAC_IOCB;
1935 mac_iocb_ptr->tid = tx_ring_desc->index;
1936 /* We use the upper 32-bits to store the tx queue for this IO.
1937 * When we get the completion we can use it to establish the context.
1939 mac_iocb_ptr->txq_idx = tx_ring_idx;
1940 tx_ring_desc->skb = skb;
1942 mac_iocb_ptr->frame_len = cpu_to_le16((u16) skb->len);
1944 if (qdev->vlgrp && vlan_tx_tag_present(skb)) {
1945 QPRINTK(qdev, TX_QUEUED, DEBUG, "Adding a vlan tag %d.\n",
1946 vlan_tx_tag_get(skb));
1947 mac_iocb_ptr->flags3 |= OB_MAC_IOCB_V;
1948 mac_iocb_ptr->vlan_tci = cpu_to_le16(vlan_tx_tag_get(skb));
1950 tso = ql_tso(skb, (struct ob_mac_tso_iocb_req *)mac_iocb_ptr);
1952 dev_kfree_skb_any(skb);
1953 return NETDEV_TX_OK;
1954 } else if (unlikely(!tso) && (skb->ip_summed == CHECKSUM_PARTIAL)) {
1955 ql_hw_csum_setup(skb,
1956 (struct ob_mac_tso_iocb_req *)mac_iocb_ptr);
1958 QL_DUMP_OB_MAC_IOCB(mac_iocb_ptr);
1959 tx_ring->prod_idx++;
1960 if (tx_ring->prod_idx == tx_ring->wq_len)
1961 tx_ring->prod_idx = 0;
1964 ql_write_db_reg(tx_ring->prod_idx, tx_ring->prod_idx_db_reg);
1965 ndev->trans_start = jiffies;
1966 QPRINTK(qdev, TX_QUEUED, DEBUG, "tx queued, slot %d, len %d\n",
1967 tx_ring->prod_idx, skb->len);
1969 atomic_dec(&tx_ring->tx_count);
1970 return NETDEV_TX_OK;
1973 static void ql_free_shadow_space(struct ql_adapter *qdev)
1975 if (qdev->rx_ring_shadow_reg_area) {
1976 pci_free_consistent(qdev->pdev,
1978 qdev->rx_ring_shadow_reg_area,
1979 qdev->rx_ring_shadow_reg_dma);
1980 qdev->rx_ring_shadow_reg_area = NULL;
1982 if (qdev->tx_ring_shadow_reg_area) {
1983 pci_free_consistent(qdev->pdev,
1985 qdev->tx_ring_shadow_reg_area,
1986 qdev->tx_ring_shadow_reg_dma);
1987 qdev->tx_ring_shadow_reg_area = NULL;
1991 static int ql_alloc_shadow_space(struct ql_adapter *qdev)
1993 qdev->rx_ring_shadow_reg_area =
1994 pci_alloc_consistent(qdev->pdev,
1995 PAGE_SIZE, &qdev->rx_ring_shadow_reg_dma);
1996 if (qdev->rx_ring_shadow_reg_area == NULL) {
1997 QPRINTK(qdev, IFUP, ERR,
1998 "Allocation of RX shadow space failed.\n");
2001 qdev->tx_ring_shadow_reg_area =
2002 pci_alloc_consistent(qdev->pdev, PAGE_SIZE,
2003 &qdev->tx_ring_shadow_reg_dma);
2004 if (qdev->tx_ring_shadow_reg_area == NULL) {
2005 QPRINTK(qdev, IFUP, ERR,
2006 "Allocation of TX shadow space failed.\n");
2007 goto err_wqp_sh_area;
2012 pci_free_consistent(qdev->pdev,
2014 qdev->rx_ring_shadow_reg_area,
2015 qdev->rx_ring_shadow_reg_dma);
2019 static void ql_init_tx_ring(struct ql_adapter *qdev, struct tx_ring *tx_ring)
2021 struct tx_ring_desc *tx_ring_desc;
2023 struct ob_mac_iocb_req *mac_iocb_ptr;
2025 mac_iocb_ptr = tx_ring->wq_base;
2026 tx_ring_desc = tx_ring->q;
2027 for (i = 0; i < tx_ring->wq_len; i++) {
2028 tx_ring_desc->index = i;
2029 tx_ring_desc->skb = NULL;
2030 tx_ring_desc->queue_entry = mac_iocb_ptr;
2034 atomic_set(&tx_ring->tx_count, tx_ring->wq_len);
2035 atomic_set(&tx_ring->queue_stopped, 0);
2038 static void ql_free_tx_resources(struct ql_adapter *qdev,
2039 struct tx_ring *tx_ring)
2041 if (tx_ring->wq_base) {
2042 pci_free_consistent(qdev->pdev, tx_ring->wq_size,
2043 tx_ring->wq_base, tx_ring->wq_base_dma);
2044 tx_ring->wq_base = NULL;
2050 static int ql_alloc_tx_resources(struct ql_adapter *qdev,
2051 struct tx_ring *tx_ring)
2054 pci_alloc_consistent(qdev->pdev, tx_ring->wq_size,
2055 &tx_ring->wq_base_dma);
2057 if ((tx_ring->wq_base == NULL)
2058 || tx_ring->wq_base_dma & (tx_ring->wq_size - 1)) {
2059 QPRINTK(qdev, IFUP, ERR, "tx_ring alloc failed.\n");
2063 kmalloc(tx_ring->wq_len * sizeof(struct tx_ring_desc), GFP_KERNEL);
2064 if (tx_ring->q == NULL)
2069 pci_free_consistent(qdev->pdev, tx_ring->wq_size,
2070 tx_ring->wq_base, tx_ring->wq_base_dma);
2074 void ql_free_lbq_buffers(struct ql_adapter *qdev, struct rx_ring *rx_ring)
2077 struct bq_desc *lbq_desc;
2079 for (i = 0; i < rx_ring->lbq_len; i++) {
2080 lbq_desc = &rx_ring->lbq[i];
2081 if (lbq_desc->p.lbq_page) {
2082 pci_unmap_page(qdev->pdev,
2083 pci_unmap_addr(lbq_desc, mapaddr),
2084 pci_unmap_len(lbq_desc, maplen),
2085 PCI_DMA_FROMDEVICE);
2087 put_page(lbq_desc->p.lbq_page);
2088 lbq_desc->p.lbq_page = NULL;
2090 lbq_desc->bq->addr_lo = 0;
2091 lbq_desc->bq->addr_hi = 0;
2096 * Allocate and map a page for each element of the lbq.
2098 static int ql_alloc_lbq_buffers(struct ql_adapter *qdev,
2099 struct rx_ring *rx_ring)
2102 struct bq_desc *lbq_desc;
2104 struct bq_element *bq = rx_ring->lbq_base;
2106 for (i = 0; i < rx_ring->lbq_len; i++) {
2107 lbq_desc = &rx_ring->lbq[i];
2108 memset(lbq_desc, 0, sizeof(lbq_desc));
2110 lbq_desc->index = i;
2111 lbq_desc->p.lbq_page = alloc_page(GFP_ATOMIC);
2112 if (unlikely(!lbq_desc->p.lbq_page)) {
2113 QPRINTK(qdev, IFUP, ERR, "failed alloc_page().\n");
2116 map = pci_map_page(qdev->pdev,
2117 lbq_desc->p.lbq_page,
2118 0, PAGE_SIZE, PCI_DMA_FROMDEVICE);
2119 if (pci_dma_mapping_error(qdev->pdev, map)) {
2120 QPRINTK(qdev, IFUP, ERR,
2121 "PCI mapping failed.\n");
2124 pci_unmap_addr_set(lbq_desc, mapaddr, map);
2125 pci_unmap_len_set(lbq_desc, maplen, PAGE_SIZE);
2126 bq->addr_lo = cpu_to_le32(map);
2127 bq->addr_hi = cpu_to_le32(map >> 32);
2133 ql_free_lbq_buffers(qdev, rx_ring);
2137 void ql_free_sbq_buffers(struct ql_adapter *qdev, struct rx_ring *rx_ring)
2140 struct bq_desc *sbq_desc;
2142 for (i = 0; i < rx_ring->sbq_len; i++) {
2143 sbq_desc = &rx_ring->sbq[i];
2144 if (sbq_desc == NULL) {
2145 QPRINTK(qdev, IFUP, ERR, "sbq_desc %d is NULL.\n", i);
2148 if (sbq_desc->p.skb) {
2149 pci_unmap_single(qdev->pdev,
2150 pci_unmap_addr(sbq_desc, mapaddr),
2151 pci_unmap_len(sbq_desc, maplen),
2152 PCI_DMA_FROMDEVICE);
2153 dev_kfree_skb(sbq_desc->p.skb);
2154 sbq_desc->p.skb = NULL;
2156 if (sbq_desc->bq == NULL) {
2157 QPRINTK(qdev, IFUP, ERR, "sbq_desc->bq %d is NULL.\n",
2161 sbq_desc->bq->addr_lo = 0;
2162 sbq_desc->bq->addr_hi = 0;
2166 /* Allocate and map an skb for each element of the sbq. */
2167 static int ql_alloc_sbq_buffers(struct ql_adapter *qdev,
2168 struct rx_ring *rx_ring)
2171 struct bq_desc *sbq_desc;
2172 struct sk_buff *skb;
2174 struct bq_element *bq = rx_ring->sbq_base;
2176 for (i = 0; i < rx_ring->sbq_len; i++) {
2177 sbq_desc = &rx_ring->sbq[i];
2178 memset(sbq_desc, 0, sizeof(sbq_desc));
2179 sbq_desc->index = i;
2181 skb = netdev_alloc_skb(qdev->ndev, rx_ring->sbq_buf_size);
2182 if (unlikely(!skb)) {
2183 /* Better luck next round */
2184 QPRINTK(qdev, IFUP, ERR,
2185 "small buff alloc failed for %d bytes at index %d.\n",
2186 rx_ring->sbq_buf_size, i);
2189 skb_reserve(skb, QLGE_SB_PAD);
2190 sbq_desc->p.skb = skb;
2192 * Map only half the buffer. Because the
2193 * other half may get some data copied to it
2194 * when the completion arrives.
2196 map = pci_map_single(qdev->pdev,
2198 rx_ring->sbq_buf_size / 2,
2199 PCI_DMA_FROMDEVICE);
2200 if (pci_dma_mapping_error(qdev->pdev, map)) {
2201 QPRINTK(qdev, IFUP, ERR, "PCI mapping failed.\n");
2204 pci_unmap_addr_set(sbq_desc, mapaddr, map);
2205 pci_unmap_len_set(sbq_desc, maplen, rx_ring->sbq_buf_size / 2);
2206 bq->addr_lo = /*sbq_desc->addr_lo = */
2208 bq->addr_hi = /*sbq_desc->addr_hi = */
2209 cpu_to_le32(map >> 32);
2214 ql_free_sbq_buffers(qdev, rx_ring);
2218 static void ql_free_rx_resources(struct ql_adapter *qdev,
2219 struct rx_ring *rx_ring)
2221 if (rx_ring->sbq_len)
2222 ql_free_sbq_buffers(qdev, rx_ring);
2223 if (rx_ring->lbq_len)
2224 ql_free_lbq_buffers(qdev, rx_ring);
2226 /* Free the small buffer queue. */
2227 if (rx_ring->sbq_base) {
2228 pci_free_consistent(qdev->pdev,
2230 rx_ring->sbq_base, rx_ring->sbq_base_dma);
2231 rx_ring->sbq_base = NULL;
2234 /* Free the small buffer queue control blocks. */
2235 kfree(rx_ring->sbq);
2236 rx_ring->sbq = NULL;
2238 /* Free the large buffer queue. */
2239 if (rx_ring->lbq_base) {
2240 pci_free_consistent(qdev->pdev,
2242 rx_ring->lbq_base, rx_ring->lbq_base_dma);
2243 rx_ring->lbq_base = NULL;
2246 /* Free the large buffer queue control blocks. */
2247 kfree(rx_ring->lbq);
2248 rx_ring->lbq = NULL;
2250 /* Free the rx queue. */
2251 if (rx_ring->cq_base) {
2252 pci_free_consistent(qdev->pdev,
2254 rx_ring->cq_base, rx_ring->cq_base_dma);
2255 rx_ring->cq_base = NULL;
2259 /* Allocate queues and buffers for this completions queue based
2260 * on the values in the parameter structure. */
2261 static int ql_alloc_rx_resources(struct ql_adapter *qdev,
2262 struct rx_ring *rx_ring)
2266 * Allocate the completion queue for this rx_ring.
2269 pci_alloc_consistent(qdev->pdev, rx_ring->cq_size,
2270 &rx_ring->cq_base_dma);
2272 if (rx_ring->cq_base == NULL) {
2273 QPRINTK(qdev, IFUP, ERR, "rx_ring alloc failed.\n");
2277 if (rx_ring->sbq_len) {
2279 * Allocate small buffer queue.
2282 pci_alloc_consistent(qdev->pdev, rx_ring->sbq_size,
2283 &rx_ring->sbq_base_dma);
2285 if (rx_ring->sbq_base == NULL) {
2286 QPRINTK(qdev, IFUP, ERR,
2287 "Small buffer queue allocation failed.\n");
2292 * Allocate small buffer queue control blocks.
2295 kmalloc(rx_ring->sbq_len * sizeof(struct bq_desc),
2297 if (rx_ring->sbq == NULL) {
2298 QPRINTK(qdev, IFUP, ERR,
2299 "Small buffer queue control block allocation failed.\n");
2303 if (ql_alloc_sbq_buffers(qdev, rx_ring)) {
2304 QPRINTK(qdev, IFUP, ERR,
2305 "Small buffer allocation failed.\n");
2310 if (rx_ring->lbq_len) {
2312 * Allocate large buffer queue.
2315 pci_alloc_consistent(qdev->pdev, rx_ring->lbq_size,
2316 &rx_ring->lbq_base_dma);
2318 if (rx_ring->lbq_base == NULL) {
2319 QPRINTK(qdev, IFUP, ERR,
2320 "Large buffer queue allocation failed.\n");
2324 * Allocate large buffer queue control blocks.
2327 kmalloc(rx_ring->lbq_len * sizeof(struct bq_desc),
2329 if (rx_ring->lbq == NULL) {
2330 QPRINTK(qdev, IFUP, ERR,
2331 "Large buffer queue control block allocation failed.\n");
2336 * Allocate the buffers.
2338 if (ql_alloc_lbq_buffers(qdev, rx_ring)) {
2339 QPRINTK(qdev, IFUP, ERR,
2340 "Large buffer allocation failed.\n");
2348 ql_free_rx_resources(qdev, rx_ring);
2352 static void ql_tx_ring_clean(struct ql_adapter *qdev)
2354 struct tx_ring *tx_ring;
2355 struct tx_ring_desc *tx_ring_desc;
2359 * Loop through all queues and free
2362 for (j = 0; j < qdev->tx_ring_count; j++) {
2363 tx_ring = &qdev->tx_ring[j];
2364 for (i = 0; i < tx_ring->wq_len; i++) {
2365 tx_ring_desc = &tx_ring->q[i];
2366 if (tx_ring_desc && tx_ring_desc->skb) {
2367 QPRINTK(qdev, IFDOWN, ERR,
2368 "Freeing lost SKB %p, from queue %d, index %d.\n",
2369 tx_ring_desc->skb, j,
2370 tx_ring_desc->index);
2371 ql_unmap_send(qdev, tx_ring_desc,
2372 tx_ring_desc->map_cnt);
2373 dev_kfree_skb(tx_ring_desc->skb);
2374 tx_ring_desc->skb = NULL;
2380 static void ql_free_ring_cb(struct ql_adapter *qdev)
2382 kfree(qdev->ring_mem);
2385 static int ql_alloc_ring_cb(struct ql_adapter *qdev)
2387 /* Allocate space for tx/rx ring control blocks. */
2388 qdev->ring_mem_size =
2389 (qdev->tx_ring_count * sizeof(struct tx_ring)) +
2390 (qdev->rx_ring_count * sizeof(struct rx_ring));
2391 qdev->ring_mem = kmalloc(qdev->ring_mem_size, GFP_KERNEL);
2392 if (qdev->ring_mem == NULL) {
2395 qdev->rx_ring = qdev->ring_mem;
2396 qdev->tx_ring = qdev->ring_mem +
2397 (qdev->rx_ring_count * sizeof(struct rx_ring));
2402 static void ql_free_mem_resources(struct ql_adapter *qdev)
2406 for (i = 0; i < qdev->tx_ring_count; i++)
2407 ql_free_tx_resources(qdev, &qdev->tx_ring[i]);
2408 for (i = 0; i < qdev->rx_ring_count; i++)
2409 ql_free_rx_resources(qdev, &qdev->rx_ring[i]);
2410 ql_free_shadow_space(qdev);
2413 static int ql_alloc_mem_resources(struct ql_adapter *qdev)
2417 /* Allocate space for our shadow registers and such. */
2418 if (ql_alloc_shadow_space(qdev))
2421 for (i = 0; i < qdev->rx_ring_count; i++) {
2422 if (ql_alloc_rx_resources(qdev, &qdev->rx_ring[i]) != 0) {
2423 QPRINTK(qdev, IFUP, ERR,
2424 "RX resource allocation failed.\n");
2428 /* Allocate tx queue resources */
2429 for (i = 0; i < qdev->tx_ring_count; i++) {
2430 if (ql_alloc_tx_resources(qdev, &qdev->tx_ring[i]) != 0) {
2431 QPRINTK(qdev, IFUP, ERR,
2432 "TX resource allocation failed.\n");
2439 ql_free_mem_resources(qdev);
2443 /* Set up the rx ring control block and pass it to the chip.
2444 * The control block is defined as
2445 * "Completion Queue Initialization Control Block", or cqicb.
2447 static int ql_start_rx_ring(struct ql_adapter *qdev, struct rx_ring *rx_ring)
2449 struct cqicb *cqicb = &rx_ring->cqicb;
2450 void *shadow_reg = qdev->rx_ring_shadow_reg_area +
2451 (rx_ring->cq_id * sizeof(u64) * 4);
2452 u64 shadow_reg_dma = qdev->rx_ring_shadow_reg_dma +
2453 (rx_ring->cq_id * sizeof(u64) * 4);
2454 void __iomem *doorbell_area =
2455 qdev->doorbell_area + (DB_PAGE_SIZE * (128 + rx_ring->cq_id));
2459 /* Set up the shadow registers for this ring. */
2460 rx_ring->prod_idx_sh_reg = shadow_reg;
2461 rx_ring->prod_idx_sh_reg_dma = shadow_reg_dma;
2462 shadow_reg += sizeof(u64);
2463 shadow_reg_dma += sizeof(u64);
2464 rx_ring->lbq_base_indirect = shadow_reg;
2465 rx_ring->lbq_base_indirect_dma = shadow_reg_dma;
2466 shadow_reg += sizeof(u64);
2467 shadow_reg_dma += sizeof(u64);
2468 rx_ring->sbq_base_indirect = shadow_reg;
2469 rx_ring->sbq_base_indirect_dma = shadow_reg_dma;
2471 /* PCI doorbell mem area + 0x00 for consumer index register */
2472 rx_ring->cnsmr_idx_db_reg = (u32 *) doorbell_area;
2473 rx_ring->cnsmr_idx = 0;
2474 rx_ring->curr_entry = rx_ring->cq_base;
2476 /* PCI doorbell mem area + 0x04 for valid register */
2477 rx_ring->valid_db_reg = doorbell_area + 0x04;
2479 /* PCI doorbell mem area + 0x18 for large buffer consumer */
2480 rx_ring->lbq_prod_idx_db_reg = (u32 *) (doorbell_area + 0x18);
2482 /* PCI doorbell mem area + 0x1c */
2483 rx_ring->sbq_prod_idx_db_reg = (u32 *) (doorbell_area + 0x1c);
2485 memset((void *)cqicb, 0, sizeof(struct cqicb));
2486 cqicb->msix_vect = rx_ring->irq;
2488 cqicb->len = cpu_to_le16(rx_ring->cq_len | LEN_V | LEN_CPP_CONT);
2490 cqicb->addr_lo = cpu_to_le32(rx_ring->cq_base_dma);
2491 cqicb->addr_hi = cpu_to_le32((u64) rx_ring->cq_base_dma >> 32);
2493 cqicb->prod_idx_addr_lo = cpu_to_le32(rx_ring->prod_idx_sh_reg_dma);
2494 cqicb->prod_idx_addr_hi =
2495 cpu_to_le32((u64) rx_ring->prod_idx_sh_reg_dma >> 32);
2498 * Set up the control block load flags.
2500 cqicb->flags = FLAGS_LC | /* Load queue base address */
2501 FLAGS_LV | /* Load MSI-X vector */
2502 FLAGS_LI; /* Load irq delay values */
2503 if (rx_ring->lbq_len) {
2504 cqicb->flags |= FLAGS_LL; /* Load lbq values */
2505 *((u64 *) rx_ring->lbq_base_indirect) = rx_ring->lbq_base_dma;
2506 cqicb->lbq_addr_lo =
2507 cpu_to_le32(rx_ring->lbq_base_indirect_dma);
2508 cqicb->lbq_addr_hi =
2509 cpu_to_le32((u64) rx_ring->lbq_base_indirect_dma >> 32);
2510 cqicb->lbq_buf_size = cpu_to_le32(rx_ring->lbq_buf_size);
2511 bq_len = (u16) rx_ring->lbq_len;
2512 cqicb->lbq_len = cpu_to_le16(bq_len);
2513 rx_ring->lbq_prod_idx = rx_ring->lbq_len - 16;
2514 rx_ring->lbq_curr_idx = 0;
2515 rx_ring->lbq_clean_idx = rx_ring->lbq_prod_idx;
2516 rx_ring->lbq_free_cnt = 16;
2518 if (rx_ring->sbq_len) {
2519 cqicb->flags |= FLAGS_LS; /* Load sbq values */
2520 *((u64 *) rx_ring->sbq_base_indirect) = rx_ring->sbq_base_dma;
2521 cqicb->sbq_addr_lo =
2522 cpu_to_le32(rx_ring->sbq_base_indirect_dma);
2523 cqicb->sbq_addr_hi =
2524 cpu_to_le32((u64) rx_ring->sbq_base_indirect_dma >> 32);
2525 cqicb->sbq_buf_size =
2526 cpu_to_le16(((rx_ring->sbq_buf_size / 2) + 8) & 0xfffffff8);
2527 bq_len = (u16) rx_ring->sbq_len;
2528 cqicb->sbq_len = cpu_to_le16(bq_len);
2529 rx_ring->sbq_prod_idx = rx_ring->sbq_len - 16;
2530 rx_ring->sbq_curr_idx = 0;
2531 rx_ring->sbq_clean_idx = rx_ring->sbq_prod_idx;
2532 rx_ring->sbq_free_cnt = 16;
2534 switch (rx_ring->type) {
2536 /* If there's only one interrupt, then we use
2537 * worker threads to process the outbound
2538 * completion handling rx_rings. We do this so
2539 * they can be run on multiple CPUs. There is
2540 * room to play with this more where we would only
2541 * run in a worker if there are more than x number
2542 * of outbound completions on the queue and more
2543 * than one queue active. Some threshold that
2544 * would indicate a benefit in spite of the cost
2545 * of a context switch.
2546 * If there's more than one interrupt, then the
2547 * outbound completions are processed in the ISR.
2549 if (!test_bit(QL_MSIX_ENABLED, &qdev->flags))
2550 INIT_DELAYED_WORK(&rx_ring->rx_work, ql_tx_clean);
2552 /* With all debug warnings on we see a WARN_ON message
2553 * when we free the skb in the interrupt context.
2555 INIT_DELAYED_WORK(&rx_ring->rx_work, ql_tx_clean);
2557 cqicb->irq_delay = cpu_to_le16(qdev->tx_coalesce_usecs);
2558 cqicb->pkt_delay = cpu_to_le16(qdev->tx_max_coalesced_frames);
2561 INIT_DELAYED_WORK(&rx_ring->rx_work, ql_rx_clean);
2562 cqicb->irq_delay = 0;
2563 cqicb->pkt_delay = 0;
2566 /* Inbound completion handling rx_rings run in
2567 * separate NAPI contexts.
2569 netif_napi_add(qdev->ndev, &rx_ring->napi, ql_napi_poll_msix,
2571 cqicb->irq_delay = cpu_to_le16(qdev->rx_coalesce_usecs);
2572 cqicb->pkt_delay = cpu_to_le16(qdev->rx_max_coalesced_frames);
2575 QPRINTK(qdev, IFUP, DEBUG, "Invalid rx_ring->type = %d.\n",
2578 QPRINTK(qdev, IFUP, INFO, "Initializing rx work queue.\n");
2579 err = ql_write_cfg(qdev, cqicb, sizeof(struct cqicb),
2580 CFG_LCQ, rx_ring->cq_id);
2582 QPRINTK(qdev, IFUP, ERR, "Failed to load CQICB.\n");
2585 QPRINTK(qdev, IFUP, INFO, "Successfully loaded CQICB.\n");
2587 * Advance the producer index for the buffer queues.
2590 if (rx_ring->lbq_len)
2591 ql_write_db_reg(rx_ring->lbq_prod_idx,
2592 rx_ring->lbq_prod_idx_db_reg);
2593 if (rx_ring->sbq_len)
2594 ql_write_db_reg(rx_ring->sbq_prod_idx,
2595 rx_ring->sbq_prod_idx_db_reg);
2599 static int ql_start_tx_ring(struct ql_adapter *qdev, struct tx_ring *tx_ring)
2601 struct wqicb *wqicb = (struct wqicb *)tx_ring;
2602 void __iomem *doorbell_area =
2603 qdev->doorbell_area + (DB_PAGE_SIZE * tx_ring->wq_id);
2604 void *shadow_reg = qdev->tx_ring_shadow_reg_area +
2605 (tx_ring->wq_id * sizeof(u64));
2606 u64 shadow_reg_dma = qdev->tx_ring_shadow_reg_dma +
2607 (tx_ring->wq_id * sizeof(u64));
2611 * Assign doorbell registers for this tx_ring.
2613 /* TX PCI doorbell mem area for tx producer index */
2614 tx_ring->prod_idx_db_reg = (u32 *) doorbell_area;
2615 tx_ring->prod_idx = 0;
2616 /* TX PCI doorbell mem area + 0x04 */
2617 tx_ring->valid_db_reg = doorbell_area + 0x04;
2620 * Assign shadow registers for this tx_ring.
2622 tx_ring->cnsmr_idx_sh_reg = shadow_reg;
2623 tx_ring->cnsmr_idx_sh_reg_dma = shadow_reg_dma;
2625 wqicb->len = cpu_to_le16(tx_ring->wq_len | Q_LEN_V | Q_LEN_CPP_CONT);
2626 wqicb->flags = cpu_to_le16(Q_FLAGS_LC |
2627 Q_FLAGS_LB | Q_FLAGS_LI | Q_FLAGS_LO);
2628 wqicb->cq_id_rss = cpu_to_le16(tx_ring->cq_id);
2630 wqicb->addr_lo = cpu_to_le32(tx_ring->wq_base_dma);
2631 wqicb->addr_hi = cpu_to_le32((u64) tx_ring->wq_base_dma >> 32);
2633 wqicb->cnsmr_idx_addr_lo = cpu_to_le32(tx_ring->cnsmr_idx_sh_reg_dma);
2634 wqicb->cnsmr_idx_addr_hi =
2635 cpu_to_le32((u64) tx_ring->cnsmr_idx_sh_reg_dma >> 32);
2637 ql_init_tx_ring(qdev, tx_ring);
2639 err = ql_write_cfg(qdev, wqicb, sizeof(wqicb), CFG_LRQ,
2640 (u16) tx_ring->wq_id);
2642 QPRINTK(qdev, IFUP, ERR, "Failed to load tx_ring.\n");
2645 QPRINTK(qdev, IFUP, INFO, "Successfully loaded WQICB.\n");
2649 static void ql_disable_msix(struct ql_adapter *qdev)
2651 if (test_bit(QL_MSIX_ENABLED, &qdev->flags)) {
2652 pci_disable_msix(qdev->pdev);
2653 clear_bit(QL_MSIX_ENABLED, &qdev->flags);
2654 kfree(qdev->msi_x_entry);
2655 qdev->msi_x_entry = NULL;
2656 } else if (test_bit(QL_MSI_ENABLED, &qdev->flags)) {
2657 pci_disable_msi(qdev->pdev);
2658 clear_bit(QL_MSI_ENABLED, &qdev->flags);
2662 static void ql_enable_msix(struct ql_adapter *qdev)
2666 qdev->intr_count = 1;
2667 /* Get the MSIX vectors. */
2668 if (irq_type == MSIX_IRQ) {
2669 /* Try to alloc space for the msix struct,
2670 * if it fails then go to MSI/legacy.
2672 qdev->msi_x_entry = kcalloc(qdev->rx_ring_count,
2673 sizeof(struct msix_entry),
2675 if (!qdev->msi_x_entry) {
2680 for (i = 0; i < qdev->rx_ring_count; i++)
2681 qdev->msi_x_entry[i].entry = i;
2683 if (!pci_enable_msix
2684 (qdev->pdev, qdev->msi_x_entry, qdev->rx_ring_count)) {
2685 set_bit(QL_MSIX_ENABLED, &qdev->flags);
2686 qdev->intr_count = qdev->rx_ring_count;
2687 QPRINTK(qdev, IFUP, INFO,
2688 "MSI-X Enabled, got %d vectors.\n",
2692 kfree(qdev->msi_x_entry);
2693 qdev->msi_x_entry = NULL;
2694 QPRINTK(qdev, IFUP, WARNING,
2695 "MSI-X Enable failed, trying MSI.\n");
2700 if (irq_type == MSI_IRQ) {
2701 if (!pci_enable_msi(qdev->pdev)) {
2702 set_bit(QL_MSI_ENABLED, &qdev->flags);
2703 QPRINTK(qdev, IFUP, INFO,
2704 "Running with MSI interrupts.\n");
2709 QPRINTK(qdev, IFUP, DEBUG, "Running with legacy interrupts.\n");
2713 * Here we build the intr_context structures based on
2714 * our rx_ring count and intr vector count.
2715 * The intr_context structure is used to hook each vector
2716 * to possibly different handlers.
2718 static void ql_resolve_queues_to_irqs(struct ql_adapter *qdev)
2721 struct intr_context *intr_context = &qdev->intr_context[0];
2723 ql_enable_msix(qdev);
2725 if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags))) {
2726 /* Each rx_ring has it's
2727 * own intr_context since we have separate
2728 * vectors for each queue.
2729 * This only true when MSI-X is enabled.
2731 for (i = 0; i < qdev->intr_count; i++, intr_context++) {
2732 qdev->rx_ring[i].irq = i;
2733 intr_context->intr = i;
2734 intr_context->qdev = qdev;
2736 * We set up each vectors enable/disable/read bits so
2737 * there's no bit/mask calculations in the critical path.
2739 intr_context->intr_en_mask =
2740 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
2741 INTR_EN_TYPE_ENABLE | INTR_EN_IHD_MASK | INTR_EN_IHD
2743 intr_context->intr_dis_mask =
2744 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
2745 INTR_EN_TYPE_DISABLE | INTR_EN_IHD_MASK |
2747 intr_context->intr_read_mask =
2748 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
2749 INTR_EN_TYPE_READ | INTR_EN_IHD_MASK | INTR_EN_IHD |
2754 * Default queue handles bcast/mcast plus
2755 * async events. Needs buffers.
2757 intr_context->handler = qlge_isr;
2758 sprintf(intr_context->name, "%s-default-queue",
2760 } else if (i < qdev->rss_ring_first_cq_id) {
2762 * Outbound queue is for outbound completions only.
2764 intr_context->handler = qlge_msix_tx_isr;
2765 sprintf(intr_context->name, "%s-txq-%d",
2766 qdev->ndev->name, i);
2769 * Inbound queues handle unicast frames only.
2771 intr_context->handler = qlge_msix_rx_isr;
2772 sprintf(intr_context->name, "%s-rxq-%d",
2773 qdev->ndev->name, i);
2778 * All rx_rings use the same intr_context since
2779 * there is only one vector.
2781 intr_context->intr = 0;
2782 intr_context->qdev = qdev;
2784 * We set up each vectors enable/disable/read bits so
2785 * there's no bit/mask calculations in the critical path.
2787 intr_context->intr_en_mask =
2788 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK | INTR_EN_TYPE_ENABLE;
2789 intr_context->intr_dis_mask =
2790 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
2791 INTR_EN_TYPE_DISABLE;
2792 intr_context->intr_read_mask =
2793 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK | INTR_EN_TYPE_READ;
2795 * Single interrupt means one handler for all rings.
2797 intr_context->handler = qlge_isr;
2798 sprintf(intr_context->name, "%s-single_irq", qdev->ndev->name);
2799 for (i = 0; i < qdev->rx_ring_count; i++)
2800 qdev->rx_ring[i].irq = 0;
2804 static void ql_free_irq(struct ql_adapter *qdev)
2807 struct intr_context *intr_context = &qdev->intr_context[0];
2809 for (i = 0; i < qdev->intr_count; i++, intr_context++) {
2810 if (intr_context->hooked) {
2811 if (test_bit(QL_MSIX_ENABLED, &qdev->flags)) {
2812 free_irq(qdev->msi_x_entry[i].vector,
2814 QPRINTK(qdev, IFDOWN, ERR,
2815 "freeing msix interrupt %d.\n", i);
2817 free_irq(qdev->pdev->irq, &qdev->rx_ring[0]);
2818 QPRINTK(qdev, IFDOWN, ERR,
2819 "freeing msi interrupt %d.\n", i);
2823 ql_disable_msix(qdev);
2826 static int ql_request_irq(struct ql_adapter *qdev)
2830 struct pci_dev *pdev = qdev->pdev;
2831 struct intr_context *intr_context = &qdev->intr_context[0];
2833 ql_resolve_queues_to_irqs(qdev);
2835 for (i = 0; i < qdev->intr_count; i++, intr_context++) {
2836 atomic_set(&intr_context->irq_cnt, 0);
2837 if (test_bit(QL_MSIX_ENABLED, &qdev->flags)) {
2838 status = request_irq(qdev->msi_x_entry[i].vector,
2839 intr_context->handler,
2844 QPRINTK(qdev, IFUP, ERR,
2845 "Failed request for MSIX interrupt %d.\n",
2849 QPRINTK(qdev, IFUP, INFO,
2850 "Hooked intr %d, queue type %s%s%s, with name %s.\n",
2852 qdev->rx_ring[i].type ==
2853 DEFAULT_Q ? "DEFAULT_Q" : "",
2854 qdev->rx_ring[i].type ==
2856 qdev->rx_ring[i].type ==
2857 RX_Q ? "RX_Q" : "", intr_context->name);
2860 QPRINTK(qdev, IFUP, DEBUG,
2861 "trying msi or legacy interrupts.\n");
2862 QPRINTK(qdev, IFUP, DEBUG,
2863 "%s: irq = %d.\n", __func__, pdev->irq);
2864 QPRINTK(qdev, IFUP, DEBUG,
2865 "%s: context->name = %s.\n", __func__,
2866 intr_context->name);
2867 QPRINTK(qdev, IFUP, DEBUG,
2868 "%s: dev_id = 0x%p.\n", __func__,
2871 request_irq(pdev->irq, qlge_isr,
2872 test_bit(QL_MSI_ENABLED,
2874 flags) ? 0 : IRQF_SHARED,
2875 intr_context->name, &qdev->rx_ring[0]);
2879 QPRINTK(qdev, IFUP, ERR,
2880 "Hooked intr %d, queue type %s%s%s, with name %s.\n",
2882 qdev->rx_ring[0].type ==
2883 DEFAULT_Q ? "DEFAULT_Q" : "",
2884 qdev->rx_ring[0].type == TX_Q ? "TX_Q" : "",
2885 qdev->rx_ring[0].type == RX_Q ? "RX_Q" : "",
2886 intr_context->name);
2888 intr_context->hooked = 1;
2892 QPRINTK(qdev, IFUP, ERR, "Failed to get the interrupts!!!/n");
2897 static int ql_start_rss(struct ql_adapter *qdev)
2899 struct ricb *ricb = &qdev->ricb;
2902 u8 *hash_id = (u8 *) ricb->hash_cq_id;
2904 memset((void *)ricb, 0, sizeof(ricb));
2906 ricb->base_cq = qdev->rss_ring_first_cq_id | RSS_L4K;
2908 (RSS_L6K | RSS_LI | RSS_LB | RSS_LM | RSS_RI4 | RSS_RI6 | RSS_RT4 |
2910 ricb->mask = cpu_to_le16(qdev->rss_ring_count - 1);
2913 * Fill out the Indirection Table.
2915 for (i = 0; i < 32; i++)
2919 * Random values for the IPv6 and IPv4 Hash Keys.
2921 get_random_bytes((void *)&ricb->ipv6_hash_key[0], 40);
2922 get_random_bytes((void *)&ricb->ipv4_hash_key[0], 16);
2924 QPRINTK(qdev, IFUP, INFO, "Initializing RSS.\n");
2926 status = ql_write_cfg(qdev, ricb, sizeof(ricb), CFG_LR, 0);
2928 QPRINTK(qdev, IFUP, ERR, "Failed to load RICB.\n");
2931 QPRINTK(qdev, IFUP, INFO, "Successfully loaded RICB.\n");
2935 /* Initialize the frame-to-queue routing. */
2936 static int ql_route_initialize(struct ql_adapter *qdev)
2941 /* Clear all the entries in the routing table. */
2942 for (i = 0; i < 16; i++) {
2943 status = ql_set_routing_reg(qdev, i, 0, 0);
2945 QPRINTK(qdev, IFUP, ERR,
2946 "Failed to init routing register for CAM packets.\n");
2951 status = ql_set_routing_reg(qdev, RT_IDX_ALL_ERR_SLOT, RT_IDX_ERR, 1);
2953 QPRINTK(qdev, IFUP, ERR,
2954 "Failed to init routing register for error packets.\n");
2957 status = ql_set_routing_reg(qdev, RT_IDX_BCAST_SLOT, RT_IDX_BCAST, 1);
2959 QPRINTK(qdev, IFUP, ERR,
2960 "Failed to init routing register for broadcast packets.\n");
2963 /* If we have more than one inbound queue, then turn on RSS in the
2966 if (qdev->rss_ring_count > 1) {
2967 status = ql_set_routing_reg(qdev, RT_IDX_RSS_MATCH_SLOT,
2968 RT_IDX_RSS_MATCH, 1);
2970 QPRINTK(qdev, IFUP, ERR,
2971 "Failed to init routing register for MATCH RSS packets.\n");
2976 status = ql_set_routing_reg(qdev, RT_IDX_CAM_HIT_SLOT,
2979 QPRINTK(qdev, IFUP, ERR,
2980 "Failed to init routing register for CAM packets.\n");
2986 static int ql_adapter_initialize(struct ql_adapter *qdev)
2993 * Set up the System register to halt on errors.
2995 value = SYS_EFE | SYS_FAE;
2997 ql_write32(qdev, SYS, mask | value);
2999 /* Set the default queue. */
3000 value = NIC_RCV_CFG_DFQ;
3001 mask = NIC_RCV_CFG_DFQ_MASK;
3002 ql_write32(qdev, NIC_RCV_CFG, (mask | value));
3004 /* Set the MPI interrupt to enabled. */
3005 ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16) | INTR_MASK_PI);
3007 /* Enable the function, set pagesize, enable error checking. */
3008 value = FSC_FE | FSC_EPC_INBOUND | FSC_EPC_OUTBOUND |
3009 FSC_EC | FSC_VM_PAGE_4K | FSC_SH;
3011 /* Set/clear header splitting. */
3012 mask = FSC_VM_PAGESIZE_MASK |
3013 FSC_DBL_MASK | FSC_DBRST_MASK | (value << 16);
3014 ql_write32(qdev, FSC, mask | value);
3016 ql_write32(qdev, SPLT_HDR, SPLT_HDR_EP |
3017 min(SMALL_BUFFER_SIZE, MAX_SPLIT_SIZE));
3019 /* Start up the rx queues. */
3020 for (i = 0; i < qdev->rx_ring_count; i++) {
3021 status = ql_start_rx_ring(qdev, &qdev->rx_ring[i]);
3023 QPRINTK(qdev, IFUP, ERR,
3024 "Failed to start rx ring[%d].\n", i);
3029 /* If there is more than one inbound completion queue
3030 * then download a RICB to configure RSS.
3032 if (qdev->rss_ring_count > 1) {
3033 status = ql_start_rss(qdev);
3035 QPRINTK(qdev, IFUP, ERR, "Failed to start RSS.\n");
3040 /* Start up the tx queues. */
3041 for (i = 0; i < qdev->tx_ring_count; i++) {
3042 status = ql_start_tx_ring(qdev, &qdev->tx_ring[i]);
3044 QPRINTK(qdev, IFUP, ERR,
3045 "Failed to start tx ring[%d].\n", i);
3050 status = ql_port_initialize(qdev);
3052 QPRINTK(qdev, IFUP, ERR, "Failed to start port.\n");
3056 status = ql_set_mac_addr_reg(qdev, (u8 *) qdev->ndev->perm_addr,
3057 MAC_ADDR_TYPE_CAM_MAC, qdev->func);
3059 QPRINTK(qdev, IFUP, ERR, "Failed to init mac address.\n");
3063 status = ql_route_initialize(qdev);
3065 QPRINTK(qdev, IFUP, ERR, "Failed to init routing table.\n");
3069 /* Start NAPI for the RSS queues. */
3070 for (i = qdev->rss_ring_first_cq_id; i < qdev->rx_ring_count; i++) {
3071 QPRINTK(qdev, IFUP, INFO, "Enabling NAPI for rx_ring[%d].\n",
3073 napi_enable(&qdev->rx_ring[i].napi);
3079 /* Issue soft reset to chip. */
3080 static int ql_adapter_reset(struct ql_adapter *qdev)
3087 #define MAX_RESET_CNT 1
3090 QPRINTK(qdev, IFDOWN, DEBUG, "Issue soft reset to chip.\n");
3091 ql_write32(qdev, RST_FO, (RST_FO_FR << 16) | RST_FO_FR);
3092 /* Wait for reset to complete. */
3094 QPRINTK(qdev, IFDOWN, DEBUG, "Wait %d seconds for reset to complete.\n",
3097 value = ql_read32(qdev, RST_FO);
3098 if ((value & RST_FO_FR) == 0)
3102 } while ((--max_wait_time));
3103 if (value & RST_FO_FR) {
3104 QPRINTK(qdev, IFDOWN, ERR,
3105 "Stuck in SoftReset: FSC_SR:0x%08x\n", value);
3106 if (resetCnt < MAX_RESET_CNT)
3109 if (max_wait_time == 0) {
3110 status = -ETIMEDOUT;
3111 QPRINTK(qdev, IFDOWN, ERR,
3112 "ETIMEOUT!!! errored out of resetting the chip!\n");
3118 static void ql_display_dev_info(struct net_device *ndev)
3120 struct ql_adapter *qdev = (struct ql_adapter *)netdev_priv(ndev);
3122 QPRINTK(qdev, PROBE, INFO,
3123 "Function #%d, NIC Roll %d, NIC Rev = %d, "
3124 "XG Roll = %d, XG Rev = %d.\n",
3126 qdev->chip_rev_id & 0x0000000f,
3127 qdev->chip_rev_id >> 4 & 0x0000000f,
3128 qdev->chip_rev_id >> 8 & 0x0000000f,
3129 qdev->chip_rev_id >> 12 & 0x0000000f);
3130 QPRINTK(qdev, PROBE, INFO,
3131 "MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
3132 ndev->dev_addr[0], ndev->dev_addr[1],
3133 ndev->dev_addr[2], ndev->dev_addr[3], ndev->dev_addr[4],
3137 static int ql_adapter_down(struct ql_adapter *qdev)
3139 struct net_device *ndev = qdev->ndev;
3141 struct rx_ring *rx_ring;
3143 netif_stop_queue(ndev);
3144 netif_carrier_off(ndev);
3146 cancel_delayed_work_sync(&qdev->asic_reset_work);
3147 cancel_delayed_work_sync(&qdev->mpi_reset_work);
3148 cancel_delayed_work_sync(&qdev->mpi_work);
3150 /* The default queue at index 0 is always processed in
3153 cancel_delayed_work_sync(&qdev->rx_ring[0].rx_work);
3155 /* The rest of the rx_rings are processed in
3156 * a workqueue only if it's a single interrupt
3157 * environment (MSI/Legacy).
3159 for (i = 1; i > qdev->rx_ring_count; i++) {
3160 rx_ring = &qdev->rx_ring[i];
3161 /* Only the RSS rings use NAPI on multi irq
3162 * environment. Outbound completion processing
3163 * is done in interrupt context.
3165 if (i >= qdev->rss_ring_first_cq_id) {
3166 napi_disable(&rx_ring->napi);
3168 cancel_delayed_work_sync(&rx_ring->rx_work);
3172 clear_bit(QL_ADAPTER_UP, &qdev->flags);
3174 ql_disable_interrupts(qdev);
3176 ql_tx_ring_clean(qdev);
3178 spin_lock(&qdev->hw_lock);
3179 status = ql_adapter_reset(qdev);
3181 QPRINTK(qdev, IFDOWN, ERR, "reset(func #%d) FAILED!\n",
3183 spin_unlock(&qdev->hw_lock);
3187 static int ql_adapter_up(struct ql_adapter *qdev)
3191 spin_lock(&qdev->hw_lock);
3192 err = ql_adapter_initialize(qdev);
3194 QPRINTK(qdev, IFUP, INFO, "Unable to initialize adapter.\n");
3195 spin_unlock(&qdev->hw_lock);
3198 spin_unlock(&qdev->hw_lock);
3199 set_bit(QL_ADAPTER_UP, &qdev->flags);
3200 ql_enable_interrupts(qdev);
3201 ql_enable_all_completion_interrupts(qdev);
3202 if ((ql_read32(qdev, STS) & qdev->port_init)) {
3203 netif_carrier_on(qdev->ndev);
3204 netif_start_queue(qdev->ndev);
3209 ql_adapter_reset(qdev);
3213 static int ql_cycle_adapter(struct ql_adapter *qdev)
3217 status = ql_adapter_down(qdev);
3221 status = ql_adapter_up(qdev);
3227 QPRINTK(qdev, IFUP, ALERT,
3228 "Driver up/down cycle failed, closing device\n");
3230 dev_close(qdev->ndev);
3235 static void ql_release_adapter_resources(struct ql_adapter *qdev)
3237 ql_free_mem_resources(qdev);
3241 static int ql_get_adapter_resources(struct ql_adapter *qdev)
3245 if (ql_alloc_mem_resources(qdev)) {
3246 QPRINTK(qdev, IFUP, ERR, "Unable to allocate memory.\n");
3249 status = ql_request_irq(qdev);
3254 ql_free_mem_resources(qdev);
3258 static int qlge_close(struct net_device *ndev)
3260 struct ql_adapter *qdev = netdev_priv(ndev);
3263 * Wait for device to recover from a reset.
3264 * (Rarely happens, but possible.)
3266 while (!test_bit(QL_ADAPTER_UP, &qdev->flags))
3268 ql_adapter_down(qdev);
3269 ql_release_adapter_resources(qdev);
3270 ql_free_ring_cb(qdev);
3274 static int ql_configure_rings(struct ql_adapter *qdev)
3277 struct rx_ring *rx_ring;
3278 struct tx_ring *tx_ring;
3279 int cpu_cnt = num_online_cpus();
3282 * For each processor present we allocate one
3283 * rx_ring for outbound completions, and one
3284 * rx_ring for inbound completions. Plus there is
3285 * always the one default queue. For the CPU
3286 * counts we end up with the following rx_rings:
3288 * one default queue +
3289 * (CPU count * outbound completion rx_ring) +
3290 * (CPU count * inbound (RSS) completion rx_ring)
3291 * To keep it simple we limit the total number of
3292 * queues to < 32, so we truncate CPU to 8.
3293 * This limitation can be removed when requested.
3300 * rx_ring[0] is always the default queue.
3302 /* Allocate outbound completion ring for each CPU. */
3303 qdev->tx_ring_count = cpu_cnt;
3304 /* Allocate inbound completion (RSS) ring for each CPU. */
3305 qdev->rss_ring_count = cpu_cnt;
3306 /* cq_id for the first inbound ring handler. */
3307 qdev->rss_ring_first_cq_id = cpu_cnt + 1;
3309 * qdev->rx_ring_count:
3310 * Total number of rx_rings. This includes the one
3311 * default queue, a number of outbound completion
3312 * handler rx_rings, and the number of inbound
3313 * completion handler rx_rings.
3315 qdev->rx_ring_count = qdev->tx_ring_count + qdev->rss_ring_count + 1;
3317 if (ql_alloc_ring_cb(qdev))
3320 for (i = 0; i < qdev->tx_ring_count; i++) {
3321 tx_ring = &qdev->tx_ring[i];
3322 memset((void *)tx_ring, 0, sizeof(tx_ring));
3323 tx_ring->qdev = qdev;
3325 tx_ring->wq_len = qdev->tx_ring_size;
3327 tx_ring->wq_len * sizeof(struct ob_mac_iocb_req);
3330 * The completion queue ID for the tx rings start
3331 * immediately after the default Q ID, which is zero.
3333 tx_ring->cq_id = i + 1;
3336 for (i = 0; i < qdev->rx_ring_count; i++) {
3337 rx_ring = &qdev->rx_ring[i];
3338 memset((void *)rx_ring, 0, sizeof(rx_ring));
3339 rx_ring->qdev = qdev;
3341 rx_ring->cpu = i % cpu_cnt; /* CPU to run handler on. */
3342 if (i == 0) { /* Default queue at index 0. */
3344 * Default queue handles bcast/mcast plus
3345 * async events. Needs buffers.
3347 rx_ring->cq_len = qdev->rx_ring_size;
3349 rx_ring->cq_len * sizeof(struct ql_net_rsp_iocb);
3350 rx_ring->lbq_len = NUM_LARGE_BUFFERS;
3352 rx_ring->lbq_len * sizeof(struct bq_element);
3353 rx_ring->lbq_buf_size = LARGE_BUFFER_SIZE;
3354 rx_ring->sbq_len = NUM_SMALL_BUFFERS;
3356 rx_ring->sbq_len * sizeof(struct bq_element);
3357 rx_ring->sbq_buf_size = SMALL_BUFFER_SIZE * 2;
3358 rx_ring->type = DEFAULT_Q;
3359 } else if (i < qdev->rss_ring_first_cq_id) {
3361 * Outbound queue handles outbound completions only.
3363 /* outbound cq is same size as tx_ring it services. */
3364 rx_ring->cq_len = qdev->tx_ring_size;
3366 rx_ring->cq_len * sizeof(struct ql_net_rsp_iocb);
3367 rx_ring->lbq_len = 0;
3368 rx_ring->lbq_size = 0;
3369 rx_ring->lbq_buf_size = 0;
3370 rx_ring->sbq_len = 0;
3371 rx_ring->sbq_size = 0;
3372 rx_ring->sbq_buf_size = 0;
3373 rx_ring->type = TX_Q;
3374 } else { /* Inbound completions (RSS) queues */
3376 * Inbound queues handle unicast frames only.
3378 rx_ring->cq_len = qdev->rx_ring_size;
3380 rx_ring->cq_len * sizeof(struct ql_net_rsp_iocb);
3381 rx_ring->lbq_len = NUM_LARGE_BUFFERS;
3383 rx_ring->lbq_len * sizeof(struct bq_element);
3384 rx_ring->lbq_buf_size = LARGE_BUFFER_SIZE;
3385 rx_ring->sbq_len = NUM_SMALL_BUFFERS;
3387 rx_ring->sbq_len * sizeof(struct bq_element);
3388 rx_ring->sbq_buf_size = SMALL_BUFFER_SIZE * 2;
3389 rx_ring->type = RX_Q;
3395 static int qlge_open(struct net_device *ndev)
3398 struct ql_adapter *qdev = netdev_priv(ndev);
3400 err = ql_configure_rings(qdev);
3404 err = ql_get_adapter_resources(qdev);
3408 err = ql_adapter_up(qdev);
3415 ql_release_adapter_resources(qdev);
3416 ql_free_ring_cb(qdev);
3420 static int qlge_change_mtu(struct net_device *ndev, int new_mtu)
3422 struct ql_adapter *qdev = netdev_priv(ndev);
3424 if (ndev->mtu == 1500 && new_mtu == 9000) {
3425 QPRINTK(qdev, IFUP, ERR, "Changing to jumbo MTU.\n");
3426 } else if (ndev->mtu == 9000 && new_mtu == 1500) {
3427 QPRINTK(qdev, IFUP, ERR, "Changing to normal MTU.\n");
3428 } else if ((ndev->mtu == 1500 && new_mtu == 1500) ||
3429 (ndev->mtu == 9000 && new_mtu == 9000)) {
3433 ndev->mtu = new_mtu;
3437 static struct net_device_stats *qlge_get_stats(struct net_device
3440 struct ql_adapter *qdev = netdev_priv(ndev);
3441 return &qdev->stats;
3444 static void qlge_set_multicast_list(struct net_device *ndev)
3446 struct ql_adapter *qdev = (struct ql_adapter *)netdev_priv(ndev);
3447 struct dev_mc_list *mc_ptr;
3450 spin_lock(&qdev->hw_lock);
3452 * Set or clear promiscuous mode if a
3453 * transition is taking place.
3455 if (ndev->flags & IFF_PROMISC) {
3456 if (!test_bit(QL_PROMISCUOUS, &qdev->flags)) {
3457 if (ql_set_routing_reg
3458 (qdev, RT_IDX_PROMISCUOUS_SLOT, RT_IDX_VALID, 1)) {
3459 QPRINTK(qdev, HW, ERR,
3460 "Failed to set promiscous mode.\n");
3462 set_bit(QL_PROMISCUOUS, &qdev->flags);
3466 if (test_bit(QL_PROMISCUOUS, &qdev->flags)) {
3467 if (ql_set_routing_reg
3468 (qdev, RT_IDX_PROMISCUOUS_SLOT, RT_IDX_VALID, 0)) {
3469 QPRINTK(qdev, HW, ERR,
3470 "Failed to clear promiscous mode.\n");
3472 clear_bit(QL_PROMISCUOUS, &qdev->flags);
3478 * Set or clear all multicast mode if a
3479 * transition is taking place.
3481 if ((ndev->flags & IFF_ALLMULTI) ||
3482 (ndev->mc_count > MAX_MULTICAST_ENTRIES)) {
3483 if (!test_bit(QL_ALLMULTI, &qdev->flags)) {
3484 if (ql_set_routing_reg
3485 (qdev, RT_IDX_ALLMULTI_SLOT, RT_IDX_MCAST, 1)) {
3486 QPRINTK(qdev, HW, ERR,
3487 "Failed to set all-multi mode.\n");
3489 set_bit(QL_ALLMULTI, &qdev->flags);
3493 if (test_bit(QL_ALLMULTI, &qdev->flags)) {
3494 if (ql_set_routing_reg
3495 (qdev, RT_IDX_ALLMULTI_SLOT, RT_IDX_MCAST, 0)) {
3496 QPRINTK(qdev, HW, ERR,
3497 "Failed to clear all-multi mode.\n");
3499 clear_bit(QL_ALLMULTI, &qdev->flags);
3504 if (ndev->mc_count) {
3505 for (i = 0, mc_ptr = ndev->mc_list; mc_ptr;
3506 i++, mc_ptr = mc_ptr->next)
3507 if (ql_set_mac_addr_reg(qdev, (u8 *) mc_ptr->dmi_addr,
3508 MAC_ADDR_TYPE_MULTI_MAC, i)) {
3509 QPRINTK(qdev, HW, ERR,
3510 "Failed to loadmulticast address.\n");
3513 if (ql_set_routing_reg
3514 (qdev, RT_IDX_MCAST_MATCH_SLOT, RT_IDX_MCAST_MATCH, 1)) {
3515 QPRINTK(qdev, HW, ERR,
3516 "Failed to set multicast match mode.\n");
3518 set_bit(QL_ALLMULTI, &qdev->flags);
3522 spin_unlock(&qdev->hw_lock);
3525 static int qlge_set_mac_address(struct net_device *ndev, void *p)
3527 struct ql_adapter *qdev = (struct ql_adapter *)netdev_priv(ndev);
3528 struct sockaddr *addr = p;
3530 if (netif_running(ndev))
3533 if (!is_valid_ether_addr(addr->sa_data))
3534 return -EADDRNOTAVAIL;
3535 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
3537 spin_lock(&qdev->hw_lock);
3538 if (ql_set_mac_addr_reg(qdev, (u8 *) ndev->dev_addr,
3539 MAC_ADDR_TYPE_CAM_MAC, qdev->func)) {/* Unicast */
3540 QPRINTK(qdev, HW, ERR, "Failed to load MAC address.\n");
3543 spin_unlock(&qdev->hw_lock);
3548 static void qlge_tx_timeout(struct net_device *ndev)
3550 struct ql_adapter *qdev = (struct ql_adapter *)netdev_priv(ndev);
3551 queue_delayed_work(qdev->workqueue, &qdev->asic_reset_work, 0);
3554 static void ql_asic_reset_work(struct work_struct *work)
3556 struct ql_adapter *qdev =
3557 container_of(work, struct ql_adapter, asic_reset_work.work);
3558 ql_cycle_adapter(qdev);
3561 static void ql_get_board_info(struct ql_adapter *qdev)
3564 (ql_read32(qdev, STS) & STS_FUNC_ID_MASK) >> STS_FUNC_ID_SHIFT;
3566 qdev->xg_sem_mask = SEM_XGMAC1_MASK;
3567 qdev->port_link_up = STS_PL1;
3568 qdev->port_init = STS_PI1;
3569 qdev->mailbox_in = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC2_MBI;
3570 qdev->mailbox_out = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC2_MBO;
3572 qdev->xg_sem_mask = SEM_XGMAC0_MASK;
3573 qdev->port_link_up = STS_PL0;
3574 qdev->port_init = STS_PI0;
3575 qdev->mailbox_in = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC0_MBI;
3576 qdev->mailbox_out = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC0_MBO;
3578 qdev->chip_rev_id = ql_read32(qdev, REV_ID);
3581 static void ql_release_all(struct pci_dev *pdev)
3583 struct net_device *ndev = pci_get_drvdata(pdev);
3584 struct ql_adapter *qdev = netdev_priv(ndev);
3586 if (qdev->workqueue) {
3587 destroy_workqueue(qdev->workqueue);
3588 qdev->workqueue = NULL;
3590 if (qdev->q_workqueue) {
3591 destroy_workqueue(qdev->q_workqueue);
3592 qdev->q_workqueue = NULL;
3595 iounmap((void *)qdev->reg_base);
3596 if (qdev->doorbell_area)
3597 iounmap(qdev->doorbell_area);
3598 pci_release_regions(pdev);
3599 pci_set_drvdata(pdev, NULL);
3602 static int __devinit ql_init_device(struct pci_dev *pdev,
3603 struct net_device *ndev, int cards_found)
3605 struct ql_adapter *qdev = netdev_priv(ndev);
3609 memset((void *)qdev, 0, sizeof(qdev));
3610 err = pci_enable_device(pdev);
3612 dev_err(&pdev->dev, "PCI device enable failed.\n");
3616 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3618 dev_err(&pdev->dev, PFX "Cannot find PCI Express capability, "
3622 pci_read_config_word(pdev, pos + PCI_EXP_DEVCTL, &val16);
3623 val16 &= ~PCI_EXP_DEVCTL_NOSNOOP_EN;
3624 val16 |= (PCI_EXP_DEVCTL_CERE |
3625 PCI_EXP_DEVCTL_NFERE |
3626 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE);
3627 pci_write_config_word(pdev, pos + PCI_EXP_DEVCTL, val16);
3630 err = pci_request_regions(pdev, DRV_NAME);
3632 dev_err(&pdev->dev, "PCI region request failed.\n");
3636 pci_set_master(pdev);
3637 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
3638 set_bit(QL_DMA64, &qdev->flags);
3639 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
3641 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3643 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
3647 dev_err(&pdev->dev, "No usable DMA configuration.\n");
3651 pci_set_drvdata(pdev, ndev);
3653 ioremap_nocache(pci_resource_start(pdev, 1),
3654 pci_resource_len(pdev, 1));
3655 if (!qdev->reg_base) {
3656 dev_err(&pdev->dev, "Register mapping failed.\n");
3661 qdev->doorbell_area_size = pci_resource_len(pdev, 3);
3662 qdev->doorbell_area =
3663 ioremap_nocache(pci_resource_start(pdev, 3),
3664 pci_resource_len(pdev, 3));
3665 if (!qdev->doorbell_area) {
3666 dev_err(&pdev->dev, "Doorbell register mapping failed.\n");
3671 ql_get_board_info(qdev);
3674 qdev->msg_enable = netif_msg_init(debug, default_msg);
3675 spin_lock_init(&qdev->hw_lock);
3676 spin_lock_init(&qdev->stats_lock);
3678 /* make sure the EEPROM is good */
3679 err = ql_get_flash_params(qdev);
3681 dev_err(&pdev->dev, "Invalid FLASH.\n");
3685 if (!is_valid_ether_addr(qdev->flash.mac_addr))
3688 memcpy(ndev->dev_addr, qdev->flash.mac_addr, ndev->addr_len);
3689 memcpy(ndev->perm_addr, ndev->dev_addr, ndev->addr_len);
3691 /* Set up the default ring sizes. */
3692 qdev->tx_ring_size = NUM_TX_RING_ENTRIES;
3693 qdev->rx_ring_size = NUM_RX_RING_ENTRIES;
3695 /* Set up the coalescing parameters. */
3696 qdev->rx_coalesce_usecs = DFLT_COALESCE_WAIT;
3697 qdev->tx_coalesce_usecs = DFLT_COALESCE_WAIT;
3698 qdev->rx_max_coalesced_frames = DFLT_INTER_FRAME_WAIT;
3699 qdev->tx_max_coalesced_frames = DFLT_INTER_FRAME_WAIT;
3702 * Set up the operating parameters.
3706 qdev->q_workqueue = create_workqueue(ndev->name);
3707 qdev->workqueue = create_singlethread_workqueue(ndev->name);
3708 INIT_DELAYED_WORK(&qdev->asic_reset_work, ql_asic_reset_work);
3709 INIT_DELAYED_WORK(&qdev->mpi_reset_work, ql_mpi_reset_work);
3710 INIT_DELAYED_WORK(&qdev->mpi_work, ql_mpi_work);
3713 dev_info(&pdev->dev, "%s\n", DRV_STRING);
3714 dev_info(&pdev->dev, "Driver name: %s, Version: %s.\n",
3715 DRV_NAME, DRV_VERSION);
3719 ql_release_all(pdev);
3720 pci_disable_device(pdev);
3724 static int __devinit qlge_probe(struct pci_dev *pdev,
3725 const struct pci_device_id *pci_entry)
3727 struct net_device *ndev = NULL;
3728 struct ql_adapter *qdev = NULL;
3729 static int cards_found = 0;
3732 ndev = alloc_etherdev(sizeof(struct ql_adapter));
3736 err = ql_init_device(pdev, ndev, cards_found);
3742 qdev = netdev_priv(ndev);
3743 SET_NETDEV_DEV(ndev, &pdev->dev);
3750 | NETIF_F_HW_VLAN_TX
3751 | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER);
3753 if (test_bit(QL_DMA64, &qdev->flags))
3754 ndev->features |= NETIF_F_HIGHDMA;
3757 * Set up net_device structure.
3759 ndev->tx_queue_len = qdev->tx_ring_size;
3760 ndev->irq = pdev->irq;
3761 ndev->open = qlge_open;
3762 ndev->stop = qlge_close;
3763 ndev->hard_start_xmit = qlge_send;
3764 SET_ETHTOOL_OPS(ndev, &qlge_ethtool_ops);
3765 ndev->change_mtu = qlge_change_mtu;
3766 ndev->get_stats = qlge_get_stats;
3767 ndev->set_multicast_list = qlge_set_multicast_list;
3768 ndev->set_mac_address = qlge_set_mac_address;
3769 ndev->tx_timeout = qlge_tx_timeout;
3770 ndev->watchdog_timeo = 10 * HZ;
3771 ndev->vlan_rx_register = ql_vlan_rx_register;
3772 ndev->vlan_rx_add_vid = ql_vlan_rx_add_vid;
3773 ndev->vlan_rx_kill_vid = ql_vlan_rx_kill_vid;
3774 err = register_netdev(ndev);
3776 dev_err(&pdev->dev, "net device registration failed.\n");
3777 ql_release_all(pdev);
3778 pci_disable_device(pdev);
3781 netif_carrier_off(ndev);
3782 netif_stop_queue(ndev);
3783 ql_display_dev_info(ndev);
3788 static void __devexit qlge_remove(struct pci_dev *pdev)
3790 struct net_device *ndev = pci_get_drvdata(pdev);
3791 unregister_netdev(ndev);
3792 ql_release_all(pdev);
3793 pci_disable_device(pdev);
3798 * This callback is called by the PCI subsystem whenever
3799 * a PCI bus error is detected.
3801 static pci_ers_result_t qlge_io_error_detected(struct pci_dev *pdev,
3802 enum pci_channel_state state)
3804 struct net_device *ndev = pci_get_drvdata(pdev);
3805 struct ql_adapter *qdev = netdev_priv(ndev);
3807 if (netif_running(ndev))
3808 ql_adapter_down(qdev);
3810 pci_disable_device(pdev);
3812 /* Request a slot reset. */
3813 return PCI_ERS_RESULT_NEED_RESET;
3817 * This callback is called after the PCI buss has been reset.
3818 * Basically, this tries to restart the card from scratch.
3819 * This is a shortened version of the device probe/discovery code,
3820 * it resembles the first-half of the () routine.
3822 static pci_ers_result_t qlge_io_slot_reset(struct pci_dev *pdev)
3824 struct net_device *ndev = pci_get_drvdata(pdev);
3825 struct ql_adapter *qdev = netdev_priv(ndev);
3827 if (pci_enable_device(pdev)) {
3828 QPRINTK(qdev, IFUP, ERR,
3829 "Cannot re-enable PCI device after reset.\n");
3830 return PCI_ERS_RESULT_DISCONNECT;
3833 pci_set_master(pdev);
3835 netif_carrier_off(ndev);
3836 netif_stop_queue(ndev);
3837 ql_adapter_reset(qdev);
3839 /* Make sure the EEPROM is good */
3840 memcpy(ndev->perm_addr, ndev->dev_addr, ndev->addr_len);
3842 if (!is_valid_ether_addr(ndev->perm_addr)) {
3843 QPRINTK(qdev, IFUP, ERR, "After reset, invalid MAC address.\n");
3844 return PCI_ERS_RESULT_DISCONNECT;
3847 return PCI_ERS_RESULT_RECOVERED;
3850 static void qlge_io_resume(struct pci_dev *pdev)
3852 struct net_device *ndev = pci_get_drvdata(pdev);
3853 struct ql_adapter *qdev = netdev_priv(ndev);
3855 pci_set_master(pdev);
3857 if (netif_running(ndev)) {
3858 if (ql_adapter_up(qdev)) {
3859 QPRINTK(qdev, IFUP, ERR,
3860 "Device initialization failed after reset.\n");
3865 netif_device_attach(ndev);
3868 static struct pci_error_handlers qlge_err_handler = {
3869 .error_detected = qlge_io_error_detected,
3870 .slot_reset = qlge_io_slot_reset,
3871 .resume = qlge_io_resume,
3874 static int qlge_suspend(struct pci_dev *pdev, pm_message_t state)
3876 struct net_device *ndev = pci_get_drvdata(pdev);
3877 struct ql_adapter *qdev = netdev_priv(ndev);
3880 netif_device_detach(ndev);
3882 if (netif_running(ndev)) {
3883 err = ql_adapter_down(qdev);
3888 err = pci_save_state(pdev);
3892 pci_disable_device(pdev);
3894 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3900 static int qlge_resume(struct pci_dev *pdev)
3902 struct net_device *ndev = pci_get_drvdata(pdev);
3903 struct ql_adapter *qdev = netdev_priv(ndev);
3906 pci_set_power_state(pdev, PCI_D0);
3907 pci_restore_state(pdev);
3908 err = pci_enable_device(pdev);
3910 QPRINTK(qdev, IFUP, ERR, "Cannot enable PCI device from suspend\n");
3913 pci_set_master(pdev);
3915 pci_enable_wake(pdev, PCI_D3hot, 0);
3916 pci_enable_wake(pdev, PCI_D3cold, 0);
3918 if (netif_running(ndev)) {
3919 err = ql_adapter_up(qdev);
3924 netif_device_attach(ndev);
3928 #endif /* CONFIG_PM */
3930 static void qlge_shutdown(struct pci_dev *pdev)
3932 qlge_suspend(pdev, PMSG_SUSPEND);
3935 static struct pci_driver qlge_driver = {
3937 .id_table = qlge_pci_tbl,
3938 .probe = qlge_probe,
3939 .remove = __devexit_p(qlge_remove),
3941 .suspend = qlge_suspend,
3942 .resume = qlge_resume,
3944 .shutdown = qlge_shutdown,
3945 .err_handler = &qlge_err_handler
3948 static int __init qlge_init_module(void)
3950 return pci_register_driver(&qlge_driver);
3953 static void __exit qlge_exit(void)
3955 pci_unregister_driver(&qlge_driver);
3958 module_init(qlge_init_module);
3959 module_exit(qlge_exit);