blob: 8033555e53c2f6f524211b4d848dcc13a0e14f78 [file] [log] [blame]
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001/*
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>
7 */
8#include <linux/kernel.h>
9#include <linux/init.h>
Jiri Pirko18c49b92011-07-21 03:24:11 +000010#include <linux/bitops.h>
Ron Mercerc4e84bd2008-09-18 11:56:28 -040011#include <linux/types.h>
12#include <linux/module.h>
13#include <linux/list.h>
14#include <linux/pci.h>
15#include <linux/dma-mapping.h>
16#include <linux/pagemap.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/dmapool.h>
20#include <linux/mempool.h>
21#include <linux/spinlock.h>
22#include <linux/kthread.h>
23#include <linux/interrupt.h>
24#include <linux/errno.h>
25#include <linux/ioport.h>
26#include <linux/in.h>
27#include <linux/ip.h>
28#include <linux/ipv6.h>
29#include <net/ipv6.h>
30#include <linux/tcp.h>
31#include <linux/udp.h>
32#include <linux/if_arp.h>
33#include <linux/if_ether.h>
34#include <linux/netdevice.h>
35#include <linux/etherdevice.h>
36#include <linux/ethtool.h>
Jiri Pirko18c49b92011-07-21 03:24:11 +000037#include <linux/if_vlan.h>
Ron Mercerc4e84bd2008-09-18 11:56:28 -040038#include <linux/skbuff.h>
Ron Mercerc4e84bd2008-09-18 11:56:28 -040039#include <linux/delay.h>
40#include <linux/mm.h>
41#include <linux/vmalloc.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040042#include <linux/prefetch.h>
Kamalesh Babulalb7c6bfb2008-10-13 18:41:01 -070043#include <net/ip6_checksum.h>
Ron Mercerc4e84bd2008-09-18 11:56:28 -040044
45#include "qlge.h"
46
47char qlge_driver_name[] = DRV_NAME;
48const char qlge_driver_version[] = DRV_VERSION;
49
50MODULE_AUTHOR("Ron Mercer <ron.mercer@qlogic.com>");
51MODULE_DESCRIPTION(DRV_STRING " ");
52MODULE_LICENSE("GPL");
53MODULE_VERSION(DRV_VERSION);
54
55static const u32 default_msg =
56 NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK |
57/* NETIF_MSG_TIMER | */
58 NETIF_MSG_IFDOWN |
59 NETIF_MSG_IFUP |
60 NETIF_MSG_RX_ERR |
61 NETIF_MSG_TX_ERR |
Ron Mercer49740972009-02-26 10:08:36 +000062/* NETIF_MSG_TX_QUEUED | */
63/* NETIF_MSG_INTR | NETIF_MSG_TX_DONE | NETIF_MSG_RX_STATUS | */
Ron Mercerc4e84bd2008-09-18 11:56:28 -040064/* NETIF_MSG_PKTDATA | */
65 NETIF_MSG_HW | NETIF_MSG_WOL | 0;
66
Sonny Rao84cf7022010-11-18 11:50:02 +000067static int debug = -1; /* defaults above */
68module_param(debug, int, 0664);
Ron Mercerc4e84bd2008-09-18 11:56:28 -040069MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
70
71#define MSIX_IRQ 0
72#define MSI_IRQ 1
73#define LEG_IRQ 2
Ron Mercera5a62a12009-11-11 12:54:05 +000074static int qlge_irq_type = MSIX_IRQ;
Sonny Rao84cf7022010-11-18 11:50:02 +000075module_param(qlge_irq_type, int, 0664);
Ron Mercera5a62a12009-11-11 12:54:05 +000076MODULE_PARM_DESC(qlge_irq_type, "0 = MSI-X, 1 = MSI, 2 = Legacy.");
Ron Mercerc4e84bd2008-09-18 11:56:28 -040077
Ron Mercer8aae2602010-01-15 13:31:28 +000078static int qlge_mpi_coredump;
79module_param(qlge_mpi_coredump, int, 0);
80MODULE_PARM_DESC(qlge_mpi_coredump,
81 "Option to enable MPI firmware dump. "
Ron Mercerd5c1da52010-01-15 13:31:34 +000082 "Default is OFF - Do Not allocate memory. ");
83
84static int qlge_force_coredump;
85module_param(qlge_force_coredump, int, 0);
86MODULE_PARM_DESC(qlge_force_coredump,
87 "Option to allow force of firmware core dump. "
88 "Default is OFF - Do not allow.");
Ron Mercer8aae2602010-01-15 13:31:28 +000089
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000090static DEFINE_PCI_DEVICE_TABLE(qlge_pci_tbl) = {
Ron Mercerb0c2aad2009-02-26 10:08:35 +000091 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QLGE_DEVICE_ID_8012)},
Ron Mercercdca8d02009-03-02 08:07:31 +000092 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QLGE_DEVICE_ID_8000)},
Ron Mercerc4e84bd2008-09-18 11:56:28 -040093 /* required last entry */
94 {0,}
95};
96
97MODULE_DEVICE_TABLE(pci, qlge_pci_tbl);
98
stephen hemmingerac409212010-10-21 07:50:54 +000099static int ql_wol(struct ql_adapter *qdev);
100static void qlge_set_multicast_list(struct net_device *ndev);
101
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400102/* This hardware semaphore causes exclusive access to
103 * resources shared between the NIC driver, MPI firmware,
104 * FCOE firmware and the FC driver.
105 */
106static int ql_sem_trylock(struct ql_adapter *qdev, u32 sem_mask)
107{
108 u32 sem_bits = 0;
109
110 switch (sem_mask) {
111 case SEM_XGMAC0_MASK:
112 sem_bits = SEM_SET << SEM_XGMAC0_SHIFT;
113 break;
114 case SEM_XGMAC1_MASK:
115 sem_bits = SEM_SET << SEM_XGMAC1_SHIFT;
116 break;
117 case SEM_ICB_MASK:
118 sem_bits = SEM_SET << SEM_ICB_SHIFT;
119 break;
120 case SEM_MAC_ADDR_MASK:
121 sem_bits = SEM_SET << SEM_MAC_ADDR_SHIFT;
122 break;
123 case SEM_FLASH_MASK:
124 sem_bits = SEM_SET << SEM_FLASH_SHIFT;
125 break;
126 case SEM_PROBE_MASK:
127 sem_bits = SEM_SET << SEM_PROBE_SHIFT;
128 break;
129 case SEM_RT_IDX_MASK:
130 sem_bits = SEM_SET << SEM_RT_IDX_SHIFT;
131 break;
132 case SEM_PROC_REG_MASK:
133 sem_bits = SEM_SET << SEM_PROC_REG_SHIFT;
134 break;
135 default:
Joe Perchesae9540f72010-02-09 11:49:52 +0000136 netif_alert(qdev, probe, qdev->ndev, "bad Semaphore mask!.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400137 return -EINVAL;
138 }
139
140 ql_write32(qdev, SEM, sem_bits | sem_mask);
141 return !(ql_read32(qdev, SEM) & sem_bits);
142}
143
144int ql_sem_spinlock(struct ql_adapter *qdev, u32 sem_mask)
145{
Ron Mercer0857e9d2009-01-09 11:31:52 +0000146 unsigned int wait_count = 30;
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400147 do {
148 if (!ql_sem_trylock(qdev, sem_mask))
149 return 0;
Ron Mercer0857e9d2009-01-09 11:31:52 +0000150 udelay(100);
151 } while (--wait_count);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400152 return -ETIMEDOUT;
153}
154
155void ql_sem_unlock(struct ql_adapter *qdev, u32 sem_mask)
156{
157 ql_write32(qdev, SEM, sem_mask);
158 ql_read32(qdev, SEM); /* flush */
159}
160
161/* This function waits for a specific bit to come ready
162 * in a given register. It is used mostly by the initialize
163 * process, but is also used in kernel thread API such as
164 * netdev->set_multi, netdev->set_mac_address, netdev->vlan_rx_add_vid.
165 */
166int ql_wait_reg_rdy(struct ql_adapter *qdev, u32 reg, u32 bit, u32 err_bit)
167{
168 u32 temp;
169 int count = UDELAY_COUNT;
170
171 while (count) {
172 temp = ql_read32(qdev, reg);
173
174 /* check for errors */
175 if (temp & err_bit) {
Joe Perchesae9540f72010-02-09 11:49:52 +0000176 netif_alert(qdev, probe, qdev->ndev,
177 "register 0x%.08x access error, value = 0x%.08x!.\n",
178 reg, temp);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400179 return -EIO;
180 } else if (temp & bit)
181 return 0;
182 udelay(UDELAY_DELAY);
183 count--;
184 }
Joe Perchesae9540f72010-02-09 11:49:52 +0000185 netif_alert(qdev, probe, qdev->ndev,
186 "Timed out waiting for reg %x to come ready.\n", reg);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400187 return -ETIMEDOUT;
188}
189
190/* The CFG register is used to download TX and RX control blocks
191 * to the chip. This function waits for an operation to complete.
192 */
193static int ql_wait_cfg(struct ql_adapter *qdev, u32 bit)
194{
195 int count = UDELAY_COUNT;
196 u32 temp;
197
198 while (count) {
199 temp = ql_read32(qdev, CFG);
200 if (temp & CFG_LE)
201 return -EIO;
202 if (!(temp & bit))
203 return 0;
204 udelay(UDELAY_DELAY);
205 count--;
206 }
207 return -ETIMEDOUT;
208}
209
210
211/* Used to issue init control blocks to hw. Maps control block,
212 * sets address, triggers download, waits for completion.
213 */
214int ql_write_cfg(struct ql_adapter *qdev, void *ptr, int size, u32 bit,
215 u16 q_id)
216{
217 u64 map;
218 int status = 0;
219 int direction;
220 u32 mask;
221 u32 value;
222
223 direction =
224 (bit & (CFG_LRQ | CFG_LR | CFG_LCQ)) ? PCI_DMA_TODEVICE :
225 PCI_DMA_FROMDEVICE;
226
227 map = pci_map_single(qdev->pdev, ptr, size, direction);
228 if (pci_dma_mapping_error(qdev->pdev, map)) {
Joe Perchesae9540f72010-02-09 11:49:52 +0000229 netif_err(qdev, ifup, qdev->ndev, "Couldn't map DMA area.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400230 return -ENOMEM;
231 }
232
Ron Mercer4322c5b2009-07-02 06:06:06 +0000233 status = ql_sem_spinlock(qdev, SEM_ICB_MASK);
234 if (status)
235 return status;
236
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400237 status = ql_wait_cfg(qdev, bit);
238 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +0000239 netif_err(qdev, ifup, qdev->ndev,
240 "Timed out waiting for CFG to come ready.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400241 goto exit;
242 }
243
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400244 ql_write32(qdev, ICB_L, (u32) map);
245 ql_write32(qdev, ICB_H, (u32) (map >> 32));
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400246
247 mask = CFG_Q_MASK | (bit << 16);
248 value = bit | (q_id << CFG_Q_SHIFT);
249 ql_write32(qdev, CFG, (mask | value));
250
251 /*
252 * Wait for the bit to clear after signaling hw.
253 */
254 status = ql_wait_cfg(qdev, bit);
255exit:
Ron Mercer4322c5b2009-07-02 06:06:06 +0000256 ql_sem_unlock(qdev, SEM_ICB_MASK); /* does flush too */
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400257 pci_unmap_single(qdev->pdev, map, size, direction);
258 return status;
259}
260
261/* Get a specific MAC address from the CAM. Used for debug and reg dump. */
262int ql_get_mac_addr_reg(struct ql_adapter *qdev, u32 type, u16 index,
263 u32 *value)
264{
265 u32 offset = 0;
266 int status;
267
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400268 switch (type) {
269 case MAC_ADDR_TYPE_MULTI_MAC:
270 case MAC_ADDR_TYPE_CAM_MAC:
271 {
272 status =
273 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800274 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400275 if (status)
276 goto exit;
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 */
280 status =
281 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800282 MAC_ADDR_IDX, MAC_ADDR_MR, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400283 if (status)
284 goto exit;
285 *value++ = ql_read32(qdev, MAC_ADDR_DATA);
286 status =
287 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800288 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400289 if (status)
290 goto exit;
291 ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
292 (index << MAC_ADDR_IDX_SHIFT) | /* index */
293 MAC_ADDR_ADR | MAC_ADDR_RS | type); /* type */
294 status =
295 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800296 MAC_ADDR_IDX, MAC_ADDR_MR, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400297 if (status)
298 goto exit;
299 *value++ = ql_read32(qdev, MAC_ADDR_DATA);
300 if (type == MAC_ADDR_TYPE_CAM_MAC) {
301 status =
302 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800303 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400304 if (status)
305 goto exit;
306 ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
307 (index << MAC_ADDR_IDX_SHIFT) | /* index */
308 MAC_ADDR_ADR | MAC_ADDR_RS | type); /* type */
309 status =
310 ql_wait_reg_rdy(qdev, MAC_ADDR_IDX,
Ron Mercer939678f2009-01-04 17:08:29 -0800311 MAC_ADDR_MR, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400312 if (status)
313 goto exit;
314 *value++ = ql_read32(qdev, MAC_ADDR_DATA);
315 }
316 break;
317 }
318 case MAC_ADDR_TYPE_VLAN:
319 case MAC_ADDR_TYPE_MULTI_FLTR:
320 default:
Joe Perchesae9540f72010-02-09 11:49:52 +0000321 netif_crit(qdev, ifup, qdev->ndev,
322 "Address type %d not yet supported.\n", type);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400323 status = -EPERM;
324 }
325exit:
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400326 return status;
327}
328
329/* Set up a MAC, multicast or VLAN address for the
330 * inbound frame matching.
331 */
332static int ql_set_mac_addr_reg(struct ql_adapter *qdev, u8 *addr, u32 type,
333 u16 index)
334{
335 u32 offset = 0;
336 int status = 0;
337
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400338 switch (type) {
339 case MAC_ADDR_TYPE_MULTI_MAC:
Ron Mercer76b26692009-10-08 09:54:40 +0000340 {
341 u32 upper = (addr[0] << 8) | addr[1];
342 u32 lower = (addr[2] << 24) | (addr[3] << 16) |
343 (addr[4] << 8) | (addr[5]);
344
345 status =
346 ql_wait_reg_rdy(qdev,
347 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
348 if (status)
349 goto exit;
350 ql_write32(qdev, MAC_ADDR_IDX, (offset++) |
351 (index << MAC_ADDR_IDX_SHIFT) |
352 type | MAC_ADDR_E);
353 ql_write32(qdev, MAC_ADDR_DATA, lower);
354 status =
355 ql_wait_reg_rdy(qdev,
356 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
357 if (status)
358 goto exit;
359 ql_write32(qdev, MAC_ADDR_IDX, (offset++) |
360 (index << MAC_ADDR_IDX_SHIFT) |
361 type | MAC_ADDR_E);
362
363 ql_write32(qdev, MAC_ADDR_DATA, upper);
364 status =
365 ql_wait_reg_rdy(qdev,
366 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
367 if (status)
368 goto exit;
369 break;
370 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400371 case MAC_ADDR_TYPE_CAM_MAC:
372 {
373 u32 cam_output;
374 u32 upper = (addr[0] << 8) | addr[1];
375 u32 lower =
376 (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) |
377 (addr[5]);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400378 status =
379 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800380 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400381 if (status)
382 goto exit;
383 ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
384 (index << MAC_ADDR_IDX_SHIFT) | /* index */
385 type); /* type */
386 ql_write32(qdev, MAC_ADDR_DATA, lower);
387 status =
388 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800389 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400390 if (status)
391 goto exit;
392 ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
393 (index << MAC_ADDR_IDX_SHIFT) | /* index */
394 type); /* type */
395 ql_write32(qdev, MAC_ADDR_DATA, upper);
396 status =
397 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800398 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400399 if (status)
400 goto exit;
401 ql_write32(qdev, MAC_ADDR_IDX, (offset) | /* offset */
402 (index << MAC_ADDR_IDX_SHIFT) | /* index */
403 type); /* type */
404 /* This field should also include the queue id
405 and possibly the function id. Right now we hardcode
406 the route field to NIC core.
407 */
Ron Mercer76b26692009-10-08 09:54:40 +0000408 cam_output = (CAM_OUT_ROUTE_NIC |
409 (qdev->
410 func << CAM_OUT_FUNC_SHIFT) |
411 (0 << CAM_OUT_CQ_ID_SHIFT));
Jiri Pirko18c49b92011-07-21 03:24:11 +0000412 if (qdev->ndev->features & NETIF_F_HW_VLAN_RX)
Ron Mercer76b26692009-10-08 09:54:40 +0000413 cam_output |= CAM_OUT_RV;
414 /* route to NIC core */
415 ql_write32(qdev, MAC_ADDR_DATA, cam_output);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400416 break;
417 }
418 case MAC_ADDR_TYPE_VLAN:
419 {
420 u32 enable_bit = *((u32 *) &addr[0]);
421 /* For VLAN, the addr actually holds a bit that
422 * either enables or disables the vlan id we are
423 * addressing. It's either MAC_ADDR_E on or off.
424 * That's bit-27 we're talking about.
425 */
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400426 status =
427 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800428 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400429 if (status)
430 goto exit;
431 ql_write32(qdev, MAC_ADDR_IDX, offset | /* offset */
432 (index << MAC_ADDR_IDX_SHIFT) | /* index */
433 type | /* type */
434 enable_bit); /* enable/disable */
435 break;
436 }
437 case MAC_ADDR_TYPE_MULTI_FLTR:
438 default:
Joe Perchesae9540f72010-02-09 11:49:52 +0000439 netif_crit(qdev, ifup, qdev->ndev,
440 "Address type %d not yet supported.\n", type);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400441 status = -EPERM;
442 }
443exit:
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400444 return status;
445}
446
Ron Mercer7fab3bfe2009-07-02 06:06:11 +0000447/* Set or clear MAC address in hardware. We sometimes
448 * have to clear it to prevent wrong frame routing
449 * especially in a bonding environment.
450 */
451static int ql_set_mac_addr(struct ql_adapter *qdev, int set)
452{
453 int status;
454 char zero_mac_addr[ETH_ALEN];
455 char *addr;
456
457 if (set) {
Ron Mercer801e9092010-02-17 06:41:22 +0000458 addr = &qdev->current_mac_addr[0];
Joe Perchesae9540f72010-02-09 11:49:52 +0000459 netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
460 "Set Mac addr %pM\n", addr);
Ron Mercer7fab3bfe2009-07-02 06:06:11 +0000461 } else {
462 memset(zero_mac_addr, 0, ETH_ALEN);
463 addr = &zero_mac_addr[0];
Joe Perchesae9540f72010-02-09 11:49:52 +0000464 netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
465 "Clearing MAC address\n");
Ron Mercer7fab3bfe2009-07-02 06:06:11 +0000466 }
467 status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
468 if (status)
469 return status;
470 status = ql_set_mac_addr_reg(qdev, (u8 *) addr,
471 MAC_ADDR_TYPE_CAM_MAC, qdev->func * MAX_CQ);
472 ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
473 if (status)
Joe Perchesae9540f72010-02-09 11:49:52 +0000474 netif_err(qdev, ifup, qdev->ndev,
475 "Failed to init mac address.\n");
Ron Mercer7fab3bfe2009-07-02 06:06:11 +0000476 return status;
477}
478
Ron Mercer6a473302009-07-02 06:06:12 +0000479void ql_link_on(struct ql_adapter *qdev)
480{
Joe Perchesae9540f72010-02-09 11:49:52 +0000481 netif_err(qdev, link, qdev->ndev, "Link is up.\n");
Ron Mercer6a473302009-07-02 06:06:12 +0000482 netif_carrier_on(qdev->ndev);
483 ql_set_mac_addr(qdev, 1);
484}
485
486void ql_link_off(struct ql_adapter *qdev)
487{
Joe Perchesae9540f72010-02-09 11:49:52 +0000488 netif_err(qdev, link, qdev->ndev, "Link is down.\n");
Ron Mercer6a473302009-07-02 06:06:12 +0000489 netif_carrier_off(qdev->ndev);
490 ql_set_mac_addr(qdev, 0);
491}
492
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400493/* Get a specific frame routing value from the CAM.
494 * Used for debug and reg dump.
495 */
496int ql_get_routing_reg(struct ql_adapter *qdev, u32 index, u32 *value)
497{
498 int status = 0;
499
Ron Mercer939678f2009-01-04 17:08:29 -0800500 status = ql_wait_reg_rdy(qdev, RT_IDX, RT_IDX_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400501 if (status)
502 goto exit;
503
504 ql_write32(qdev, RT_IDX,
505 RT_IDX_TYPE_NICQ | RT_IDX_RS | (index << RT_IDX_IDX_SHIFT));
Ron Mercer939678f2009-01-04 17:08:29 -0800506 status = ql_wait_reg_rdy(qdev, RT_IDX, RT_IDX_MR, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400507 if (status)
508 goto exit;
509 *value = ql_read32(qdev, RT_DATA);
510exit:
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400511 return status;
512}
513
514/* The NIC function for this chip has 16 routing indexes. Each one can be used
515 * to route different frame types to various inbound queues. We send broadcast/
516 * multicast/error frames to the default queue for slow handling,
517 * and CAM hit/RSS frames to the fast handling queues.
518 */
519static int ql_set_routing_reg(struct ql_adapter *qdev, u32 index, u32 mask,
520 int enable)
521{
Ron Mercer8587ea32009-02-23 10:42:15 +0000522 int status = -EINVAL; /* Return error if no mask match. */
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400523 u32 value = 0;
524
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400525 switch (mask) {
526 case RT_IDX_CAM_HIT:
527 {
528 value = RT_IDX_DST_CAM_Q | /* dest */
529 RT_IDX_TYPE_NICQ | /* type */
530 (RT_IDX_CAM_HIT_SLOT << RT_IDX_IDX_SHIFT);/* index */
531 break;
532 }
533 case RT_IDX_VALID: /* Promiscuous Mode frames. */
534 {
535 value = RT_IDX_DST_DFLT_Q | /* dest */
536 RT_IDX_TYPE_NICQ | /* type */
537 (RT_IDX_PROMISCUOUS_SLOT << RT_IDX_IDX_SHIFT);/* index */
538 break;
539 }
540 case RT_IDX_ERR: /* Pass up MAC,IP,TCP/UDP error frames. */
541 {
542 value = RT_IDX_DST_DFLT_Q | /* dest */
543 RT_IDX_TYPE_NICQ | /* type */
544 (RT_IDX_ALL_ERR_SLOT << RT_IDX_IDX_SHIFT);/* index */
545 break;
546 }
Ron Mercerfbc2ac32010-07-05 12:19:41 +0000547 case RT_IDX_IP_CSUM_ERR: /* Pass up IP CSUM error frames. */
548 {
549 value = RT_IDX_DST_DFLT_Q | /* dest */
550 RT_IDX_TYPE_NICQ | /* type */
551 (RT_IDX_IP_CSUM_ERR_SLOT <<
552 RT_IDX_IDX_SHIFT); /* index */
553 break;
554 }
555 case RT_IDX_TU_CSUM_ERR: /* Pass up TCP/UDP CSUM error frames. */
556 {
557 value = RT_IDX_DST_DFLT_Q | /* dest */
558 RT_IDX_TYPE_NICQ | /* type */
559 (RT_IDX_TCP_UDP_CSUM_ERR_SLOT <<
560 RT_IDX_IDX_SHIFT); /* index */
561 break;
562 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400563 case RT_IDX_BCAST: /* Pass up Broadcast frames to default Q. */
564 {
565 value = RT_IDX_DST_DFLT_Q | /* dest */
566 RT_IDX_TYPE_NICQ | /* type */
567 (RT_IDX_BCAST_SLOT << RT_IDX_IDX_SHIFT);/* index */
568 break;
569 }
570 case RT_IDX_MCAST: /* Pass up All Multicast frames. */
571 {
Ron Mercere163d7f2009-10-08 09:54:39 +0000572 value = RT_IDX_DST_DFLT_Q | /* dest */
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400573 RT_IDX_TYPE_NICQ | /* type */
574 (RT_IDX_ALLMULTI_SLOT << RT_IDX_IDX_SHIFT);/* index */
575 break;
576 }
577 case RT_IDX_MCAST_MATCH: /* Pass up matched Multicast frames. */
578 {
Ron Mercere163d7f2009-10-08 09:54:39 +0000579 value = RT_IDX_DST_DFLT_Q | /* dest */
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400580 RT_IDX_TYPE_NICQ | /* type */
581 (RT_IDX_MCAST_MATCH_SLOT << RT_IDX_IDX_SHIFT);/* index */
582 break;
583 }
584 case RT_IDX_RSS_MATCH: /* Pass up matched RSS frames. */
585 {
586 value = RT_IDX_DST_RSS | /* dest */
587 RT_IDX_TYPE_NICQ | /* type */
588 (RT_IDX_RSS_MATCH_SLOT << RT_IDX_IDX_SHIFT);/* index */
589 break;
590 }
591 case 0: /* Clear the E-bit on an entry. */
592 {
593 value = RT_IDX_DST_DFLT_Q | /* dest */
594 RT_IDX_TYPE_NICQ | /* type */
595 (index << RT_IDX_IDX_SHIFT);/* index */
596 break;
597 }
598 default:
Joe Perchesae9540f72010-02-09 11:49:52 +0000599 netif_err(qdev, ifup, qdev->ndev,
600 "Mask type %d not yet supported.\n", mask);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400601 status = -EPERM;
602 goto exit;
603 }
604
605 if (value) {
606 status = ql_wait_reg_rdy(qdev, RT_IDX, RT_IDX_MW, 0);
607 if (status)
608 goto exit;
609 value |= (enable ? RT_IDX_E : 0);
610 ql_write32(qdev, RT_IDX, value);
611 ql_write32(qdev, RT_DATA, enable ? mask : 0);
612 }
613exit:
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400614 return status;
615}
616
617static void ql_enable_interrupts(struct ql_adapter *qdev)
618{
619 ql_write32(qdev, INTR_EN, (INTR_EN_EI << 16) | INTR_EN_EI);
620}
621
622static void ql_disable_interrupts(struct ql_adapter *qdev)
623{
624 ql_write32(qdev, INTR_EN, (INTR_EN_EI << 16));
625}
626
627/* If we're running with multiple MSI-X vectors then we enable on the fly.
628 * Otherwise, we may have multiple outstanding workers and don't want to
629 * enable until the last one finishes. In this case, the irq_cnt gets
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300630 * incremented every time we queue a worker and decremented every time
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400631 * a worker finishes. Once it hits zero we enable the interrupt.
632 */
Ron Mercerbb0d2152008-10-20 10:30:26 -0700633u32 ql_enable_completion_interrupt(struct ql_adapter *qdev, u32 intr)
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400634{
Ron Mercerbb0d2152008-10-20 10:30:26 -0700635 u32 var = 0;
636 unsigned long hw_flags = 0;
637 struct intr_context *ctx = qdev->intr_context + intr;
638
639 if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags) && intr)) {
640 /* Always enable if we're MSIX multi interrupts and
641 * it's not the default (zeroeth) interrupt.
642 */
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400643 ql_write32(qdev, INTR_EN,
Ron Mercerbb0d2152008-10-20 10:30:26 -0700644 ctx->intr_en_mask);
645 var = ql_read32(qdev, STS);
646 return var;
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400647 }
Ron Mercerbb0d2152008-10-20 10:30:26 -0700648
649 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
650 if (atomic_dec_and_test(&ctx->irq_cnt)) {
651 ql_write32(qdev, INTR_EN,
652 ctx->intr_en_mask);
653 var = ql_read32(qdev, STS);
654 }
655 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
656 return var;
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400657}
658
659static u32 ql_disable_completion_interrupt(struct ql_adapter *qdev, u32 intr)
660{
661 u32 var = 0;
Ron Mercerbb0d2152008-10-20 10:30:26 -0700662 struct intr_context *ctx;
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400663
Ron Mercerbb0d2152008-10-20 10:30:26 -0700664 /* HW disables for us if we're MSIX multi interrupts and
665 * it's not the default (zeroeth) interrupt.
666 */
667 if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags) && intr))
668 return 0;
669
670 ctx = qdev->intr_context + intr;
Ron Mercer08b1bc82009-03-09 10:59:23 +0000671 spin_lock(&qdev->hw_lock);
Ron Mercerbb0d2152008-10-20 10:30:26 -0700672 if (!atomic_read(&ctx->irq_cnt)) {
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400673 ql_write32(qdev, INTR_EN,
Ron Mercerbb0d2152008-10-20 10:30:26 -0700674 ctx->intr_dis_mask);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400675 var = ql_read32(qdev, STS);
676 }
Ron Mercerbb0d2152008-10-20 10:30:26 -0700677 atomic_inc(&ctx->irq_cnt);
Ron Mercer08b1bc82009-03-09 10:59:23 +0000678 spin_unlock(&qdev->hw_lock);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400679 return var;
680}
681
682static void ql_enable_all_completion_interrupts(struct ql_adapter *qdev)
683{
684 int i;
685 for (i = 0; i < qdev->intr_count; i++) {
686 /* The enable call does a atomic_dec_and_test
687 * and enables only if the result is zero.
688 * So we precharge it here.
689 */
Ron Mercerbb0d2152008-10-20 10:30:26 -0700690 if (unlikely(!test_bit(QL_MSIX_ENABLED, &qdev->flags) ||
691 i == 0))
692 atomic_set(&qdev->intr_context[i].irq_cnt, 1);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400693 ql_enable_completion_interrupt(qdev, i);
694 }
695
696}
697
Ron Mercerb0c2aad2009-02-26 10:08:35 +0000698static int ql_validate_flash(struct ql_adapter *qdev, u32 size, const char *str)
699{
700 int status, i;
701 u16 csum = 0;
702 __le16 *flash = (__le16 *)&qdev->flash;
703
704 status = strncmp((char *)&qdev->flash, str, 4);
705 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +0000706 netif_err(qdev, ifup, qdev->ndev, "Invalid flash signature.\n");
Ron Mercerb0c2aad2009-02-26 10:08:35 +0000707 return status;
708 }
709
710 for (i = 0; i < size; i++)
711 csum += le16_to_cpu(*flash++);
712
713 if (csum)
Joe Perchesae9540f72010-02-09 11:49:52 +0000714 netif_err(qdev, ifup, qdev->ndev,
715 "Invalid flash checksum, csum = 0x%.04x.\n", csum);
Ron Mercerb0c2aad2009-02-26 10:08:35 +0000716
717 return csum;
718}
719
Ron Mercer26351472009-02-02 13:53:57 -0800720static int ql_read_flash_word(struct ql_adapter *qdev, int offset, __le32 *data)
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400721{
722 int status = 0;
723 /* wait for reg to come ready */
724 status = ql_wait_reg_rdy(qdev,
725 FLASH_ADDR, FLASH_ADDR_RDY, FLASH_ADDR_ERR);
726 if (status)
727 goto exit;
728 /* set up for reg read */
729 ql_write32(qdev, FLASH_ADDR, FLASH_ADDR_R | offset);
730 /* wait for reg to come ready */
731 status = ql_wait_reg_rdy(qdev,
732 FLASH_ADDR, FLASH_ADDR_RDY, FLASH_ADDR_ERR);
733 if (status)
734 goto exit;
Ron Mercer26351472009-02-02 13:53:57 -0800735 /* This data is stored on flash as an array of
736 * __le32. Since ql_read32() returns cpu endian
737 * we need to swap it back.
738 */
739 *data = cpu_to_le32(ql_read32(qdev, FLASH_DATA));
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400740exit:
741 return status;
742}
743
Ron Mercercdca8d02009-03-02 08:07:31 +0000744static int ql_get_8000_flash_params(struct ql_adapter *qdev)
745{
746 u32 i, size;
747 int status;
748 __le32 *p = (__le32 *)&qdev->flash;
749 u32 offset;
Ron Mercer542512e2009-06-09 05:39:33 +0000750 u8 mac_addr[6];
Ron Mercercdca8d02009-03-02 08:07:31 +0000751
752 /* Get flash offset for function and adjust
753 * for dword access.
754 */
Ron Mercere4552f52009-06-09 05:39:32 +0000755 if (!qdev->port)
Ron Mercercdca8d02009-03-02 08:07:31 +0000756 offset = FUNC0_FLASH_OFFSET / sizeof(u32);
757 else
758 offset = FUNC1_FLASH_OFFSET / sizeof(u32);
759
760 if (ql_sem_spinlock(qdev, SEM_FLASH_MASK))
761 return -ETIMEDOUT;
762
763 size = sizeof(struct flash_params_8000) / sizeof(u32);
764 for (i = 0; i < size; i++, p++) {
765 status = ql_read_flash_word(qdev, i+offset, p);
766 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +0000767 netif_err(qdev, ifup, qdev->ndev,
768 "Error reading flash.\n");
Ron Mercercdca8d02009-03-02 08:07:31 +0000769 goto exit;
770 }
771 }
772
773 status = ql_validate_flash(qdev,
774 sizeof(struct flash_params_8000) / sizeof(u16),
775 "8000");
776 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +0000777 netif_err(qdev, ifup, qdev->ndev, "Invalid flash.\n");
Ron Mercercdca8d02009-03-02 08:07:31 +0000778 status = -EINVAL;
779 goto exit;
780 }
781
Ron Mercer542512e2009-06-09 05:39:33 +0000782 /* Extract either manufacturer or BOFM modified
783 * MAC address.
784 */
785 if (qdev->flash.flash_params_8000.data_type1 == 2)
786 memcpy(mac_addr,
787 qdev->flash.flash_params_8000.mac_addr1,
788 qdev->ndev->addr_len);
789 else
790 memcpy(mac_addr,
791 qdev->flash.flash_params_8000.mac_addr,
792 qdev->ndev->addr_len);
793
794 if (!is_valid_ether_addr(mac_addr)) {
Joe Perchesae9540f72010-02-09 11:49:52 +0000795 netif_err(qdev, ifup, qdev->ndev, "Invalid MAC address.\n");
Ron Mercercdca8d02009-03-02 08:07:31 +0000796 status = -EINVAL;
797 goto exit;
798 }
799
800 memcpy(qdev->ndev->dev_addr,
Ron Mercer542512e2009-06-09 05:39:33 +0000801 mac_addr,
Ron Mercercdca8d02009-03-02 08:07:31 +0000802 qdev->ndev->addr_len);
803
804exit:
805 ql_sem_unlock(qdev, SEM_FLASH_MASK);
806 return status;
807}
808
Ron Mercerb0c2aad2009-02-26 10:08:35 +0000809static int ql_get_8012_flash_params(struct ql_adapter *qdev)
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400810{
811 int i;
812 int status;
Ron Mercer26351472009-02-02 13:53:57 -0800813 __le32 *p = (__le32 *)&qdev->flash;
Ron Mercere78f5fa72009-02-02 13:54:15 -0800814 u32 offset = 0;
Ron Mercerb0c2aad2009-02-26 10:08:35 +0000815 u32 size = sizeof(struct flash_params_8012) / sizeof(u32);
Ron Mercere78f5fa72009-02-02 13:54:15 -0800816
817 /* Second function's parameters follow the first
818 * function's.
819 */
Ron Mercere4552f52009-06-09 05:39:32 +0000820 if (qdev->port)
Ron Mercerb0c2aad2009-02-26 10:08:35 +0000821 offset = size;
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400822
823 if (ql_sem_spinlock(qdev, SEM_FLASH_MASK))
824 return -ETIMEDOUT;
825
Ron Mercerb0c2aad2009-02-26 10:08:35 +0000826 for (i = 0; i < size; i++, p++) {
Ron Mercere78f5fa72009-02-02 13:54:15 -0800827 status = ql_read_flash_word(qdev, i+offset, p);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400828 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +0000829 netif_err(qdev, ifup, qdev->ndev,
830 "Error reading flash.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400831 goto exit;
832 }
833
834 }
Ron Mercerb0c2aad2009-02-26 10:08:35 +0000835
836 status = ql_validate_flash(qdev,
837 sizeof(struct flash_params_8012) / sizeof(u16),
838 "8012");
839 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +0000840 netif_err(qdev, ifup, qdev->ndev, "Invalid flash.\n");
Ron Mercerb0c2aad2009-02-26 10:08:35 +0000841 status = -EINVAL;
842 goto exit;
843 }
844
845 if (!is_valid_ether_addr(qdev->flash.flash_params_8012.mac_addr)) {
846 status = -EINVAL;
847 goto exit;
848 }
849
850 memcpy(qdev->ndev->dev_addr,
851 qdev->flash.flash_params_8012.mac_addr,
852 qdev->ndev->addr_len);
853
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400854exit:
855 ql_sem_unlock(qdev, SEM_FLASH_MASK);
856 return status;
857}
858
859/* xgmac register are located behind the xgmac_addr and xgmac_data
860 * register pair. Each read/write requires us to wait for the ready
861 * bit before reading/writing the data.
862 */
863static int ql_write_xgmac_reg(struct ql_adapter *qdev, u32 reg, u32 data)
864{
865 int status;
866 /* wait for reg to come ready */
867 status = ql_wait_reg_rdy(qdev,
868 XGMAC_ADDR, XGMAC_ADDR_RDY, XGMAC_ADDR_XME);
869 if (status)
870 return status;
871 /* write the data to the data reg */
872 ql_write32(qdev, XGMAC_DATA, data);
873 /* trigger the write */
874 ql_write32(qdev, XGMAC_ADDR, reg);
875 return status;
876}
877
878/* xgmac register are located behind the xgmac_addr and xgmac_data
879 * register pair. Each read/write requires us to wait for the ready
880 * bit before reading/writing the data.
881 */
882int ql_read_xgmac_reg(struct ql_adapter *qdev, u32 reg, u32 *data)
883{
884 int status = 0;
885 /* wait for reg to come ready */
886 status = ql_wait_reg_rdy(qdev,
887 XGMAC_ADDR, XGMAC_ADDR_RDY, XGMAC_ADDR_XME);
888 if (status)
889 goto exit;
890 /* set up for reg read */
891 ql_write32(qdev, XGMAC_ADDR, reg | XGMAC_ADDR_R);
892 /* wait for reg to come ready */
893 status = ql_wait_reg_rdy(qdev,
894 XGMAC_ADDR, XGMAC_ADDR_RDY, XGMAC_ADDR_XME);
895 if (status)
896 goto exit;
897 /* get the data */
898 *data = ql_read32(qdev, XGMAC_DATA);
899exit:
900 return status;
901}
902
903/* This is used for reading the 64-bit statistics regs. */
904int ql_read_xgmac_reg64(struct ql_adapter *qdev, u32 reg, u64 *data)
905{
906 int status = 0;
907 u32 hi = 0;
908 u32 lo = 0;
909
910 status = ql_read_xgmac_reg(qdev, reg, &lo);
911 if (status)
912 goto exit;
913
914 status = ql_read_xgmac_reg(qdev, reg + 4, &hi);
915 if (status)
916 goto exit;
917
918 *data = (u64) lo | ((u64) hi << 32);
919
920exit:
921 return status;
922}
923
Ron Mercercdca8d02009-03-02 08:07:31 +0000924static int ql_8000_port_initialize(struct ql_adapter *qdev)
925{
Ron Mercerbcc2cb32009-03-02 08:07:32 +0000926 int status;
Ron Mercercfec0cb2009-06-09 05:39:29 +0000927 /*
928 * Get MPI firmware version for driver banner
929 * and ethool info.
930 */
931 status = ql_mb_about_fw(qdev);
932 if (status)
933 goto exit;
Ron Mercerbcc2cb32009-03-02 08:07:32 +0000934 status = ql_mb_get_fw_state(qdev);
935 if (status)
936 goto exit;
937 /* Wake up a worker to get/set the TX/RX frame sizes. */
938 queue_delayed_work(qdev->workqueue, &qdev->mpi_port_cfg_work, 0);
939exit:
940 return status;
Ron Mercercdca8d02009-03-02 08:07:31 +0000941}
942
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400943/* Take the MAC Core out of reset.
944 * Enable statistics counting.
945 * Take the transmitter/receiver out of reset.
946 * This functionality may be done in the MPI firmware at a
947 * later date.
948 */
Ron Mercerb0c2aad2009-02-26 10:08:35 +0000949static int ql_8012_port_initialize(struct ql_adapter *qdev)
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400950{
951 int status = 0;
952 u32 data;
953
954 if (ql_sem_trylock(qdev, qdev->xg_sem_mask)) {
955 /* Another function has the semaphore, so
956 * wait for the port init bit to come ready.
957 */
Joe Perchesae9540f72010-02-09 11:49:52 +0000958 netif_info(qdev, link, qdev->ndev,
959 "Another function has the semaphore, so wait for the port init bit to come ready.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400960 status = ql_wait_reg_rdy(qdev, STS, qdev->port_init, 0);
961 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +0000962 netif_crit(qdev, link, qdev->ndev,
963 "Port initialize timed out.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400964 }
965 return status;
966 }
967
Joe Perchesae9540f72010-02-09 11:49:52 +0000968 netif_info(qdev, link, qdev->ndev, "Got xgmac semaphore!.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400969 /* Set the core reset. */
970 status = ql_read_xgmac_reg(qdev, GLOBAL_CFG, &data);
971 if (status)
972 goto end;
973 data |= GLOBAL_CFG_RESET;
974 status = ql_write_xgmac_reg(qdev, GLOBAL_CFG, data);
975 if (status)
976 goto end;
977
978 /* Clear the core reset and turn on jumbo for receiver. */
979 data &= ~GLOBAL_CFG_RESET; /* Clear core reset. */
980 data |= GLOBAL_CFG_JUMBO; /* Turn on jumbo. */
981 data |= GLOBAL_CFG_TX_STAT_EN;
982 data |= GLOBAL_CFG_RX_STAT_EN;
983 status = ql_write_xgmac_reg(qdev, GLOBAL_CFG, data);
984 if (status)
985 goto end;
986
987 /* Enable transmitter, and clear it's reset. */
988 status = ql_read_xgmac_reg(qdev, TX_CFG, &data);
989 if (status)
990 goto end;
991 data &= ~TX_CFG_RESET; /* Clear the TX MAC reset. */
992 data |= TX_CFG_EN; /* Enable the transmitter. */
993 status = ql_write_xgmac_reg(qdev, TX_CFG, data);
994 if (status)
995 goto end;
996
997 /* Enable receiver and clear it's reset. */
998 status = ql_read_xgmac_reg(qdev, RX_CFG, &data);
999 if (status)
1000 goto end;
1001 data &= ~RX_CFG_RESET; /* Clear the RX MAC reset. */
1002 data |= RX_CFG_EN; /* Enable the receiver. */
1003 status = ql_write_xgmac_reg(qdev, RX_CFG, data);
1004 if (status)
1005 goto end;
1006
1007 /* Turn on jumbo. */
1008 status =
1009 ql_write_xgmac_reg(qdev, MAC_TX_PARAMS, MAC_TX_PARAMS_JUMBO | (0x2580 << 16));
1010 if (status)
1011 goto end;
1012 status =
1013 ql_write_xgmac_reg(qdev, MAC_RX_PARAMS, 0x2580);
1014 if (status)
1015 goto end;
1016
1017 /* Signal to the world that the port is enabled. */
1018 ql_write32(qdev, STS, ((qdev->port_init << 16) | qdev->port_init));
1019end:
1020 ql_sem_unlock(qdev, qdev->xg_sem_mask);
1021 return status;
1022}
1023
Ron Mercer7c734352009-10-19 03:32:19 +00001024static inline unsigned int ql_lbq_block_size(struct ql_adapter *qdev)
1025{
1026 return PAGE_SIZE << qdev->lbq_buf_order;
1027}
1028
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001029/* Get the next large buffer. */
Stephen Hemminger8668ae92008-11-21 17:29:50 -08001030static struct bq_desc *ql_get_curr_lbuf(struct rx_ring *rx_ring)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001031{
1032 struct bq_desc *lbq_desc = &rx_ring->lbq[rx_ring->lbq_curr_idx];
1033 rx_ring->lbq_curr_idx++;
1034 if (rx_ring->lbq_curr_idx == rx_ring->lbq_len)
1035 rx_ring->lbq_curr_idx = 0;
1036 rx_ring->lbq_free_cnt++;
1037 return lbq_desc;
1038}
1039
Ron Mercer7c734352009-10-19 03:32:19 +00001040static struct bq_desc *ql_get_curr_lchunk(struct ql_adapter *qdev,
1041 struct rx_ring *rx_ring)
1042{
1043 struct bq_desc *lbq_desc = ql_get_curr_lbuf(rx_ring);
1044
1045 pci_dma_sync_single_for_cpu(qdev->pdev,
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001046 dma_unmap_addr(lbq_desc, mapaddr),
Ron Mercer7c734352009-10-19 03:32:19 +00001047 rx_ring->lbq_buf_size,
1048 PCI_DMA_FROMDEVICE);
1049
1050 /* If it's the last chunk of our master page then
1051 * we unmap it.
1052 */
1053 if ((lbq_desc->p.pg_chunk.offset + rx_ring->lbq_buf_size)
1054 == ql_lbq_block_size(qdev))
1055 pci_unmap_page(qdev->pdev,
1056 lbq_desc->p.pg_chunk.map,
1057 ql_lbq_block_size(qdev),
1058 PCI_DMA_FROMDEVICE);
1059 return lbq_desc;
1060}
1061
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001062/* Get the next small buffer. */
Stephen Hemminger8668ae92008-11-21 17:29:50 -08001063static struct bq_desc *ql_get_curr_sbuf(struct rx_ring *rx_ring)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001064{
1065 struct bq_desc *sbq_desc = &rx_ring->sbq[rx_ring->sbq_curr_idx];
1066 rx_ring->sbq_curr_idx++;
1067 if (rx_ring->sbq_curr_idx == rx_ring->sbq_len)
1068 rx_ring->sbq_curr_idx = 0;
1069 rx_ring->sbq_free_cnt++;
1070 return sbq_desc;
1071}
1072
1073/* Update an rx ring index. */
1074static void ql_update_cq(struct rx_ring *rx_ring)
1075{
1076 rx_ring->cnsmr_idx++;
1077 rx_ring->curr_entry++;
1078 if (unlikely(rx_ring->cnsmr_idx == rx_ring->cq_len)) {
1079 rx_ring->cnsmr_idx = 0;
1080 rx_ring->curr_entry = rx_ring->cq_base;
1081 }
1082}
1083
1084static void ql_write_cq_idx(struct rx_ring *rx_ring)
1085{
1086 ql_write_db_reg(rx_ring->cnsmr_idx, rx_ring->cnsmr_idx_db_reg);
1087}
1088
Ron Mercer7c734352009-10-19 03:32:19 +00001089static int ql_get_next_chunk(struct ql_adapter *qdev, struct rx_ring *rx_ring,
1090 struct bq_desc *lbq_desc)
1091{
1092 if (!rx_ring->pg_chunk.page) {
1093 u64 map;
1094 rx_ring->pg_chunk.page = alloc_pages(__GFP_COLD | __GFP_COMP |
1095 GFP_ATOMIC,
1096 qdev->lbq_buf_order);
1097 if (unlikely(!rx_ring->pg_chunk.page)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001098 netif_err(qdev, drv, qdev->ndev,
1099 "page allocation failed.\n");
Ron Mercer7c734352009-10-19 03:32:19 +00001100 return -ENOMEM;
1101 }
1102 rx_ring->pg_chunk.offset = 0;
1103 map = pci_map_page(qdev->pdev, rx_ring->pg_chunk.page,
1104 0, ql_lbq_block_size(qdev),
1105 PCI_DMA_FROMDEVICE);
1106 if (pci_dma_mapping_error(qdev->pdev, map)) {
1107 __free_pages(rx_ring->pg_chunk.page,
1108 qdev->lbq_buf_order);
Joe Perchesae9540f72010-02-09 11:49:52 +00001109 netif_err(qdev, drv, qdev->ndev,
1110 "PCI mapping failed.\n");
Ron Mercer7c734352009-10-19 03:32:19 +00001111 return -ENOMEM;
1112 }
1113 rx_ring->pg_chunk.map = map;
1114 rx_ring->pg_chunk.va = page_address(rx_ring->pg_chunk.page);
1115 }
1116
1117 /* Copy the current master pg_chunk info
1118 * to the current descriptor.
1119 */
1120 lbq_desc->p.pg_chunk = rx_ring->pg_chunk;
1121
1122 /* Adjust the master page chunk for next
1123 * buffer get.
1124 */
1125 rx_ring->pg_chunk.offset += rx_ring->lbq_buf_size;
1126 if (rx_ring->pg_chunk.offset == ql_lbq_block_size(qdev)) {
1127 rx_ring->pg_chunk.page = NULL;
1128 lbq_desc->p.pg_chunk.last_flag = 1;
1129 } else {
1130 rx_ring->pg_chunk.va += rx_ring->lbq_buf_size;
1131 get_page(rx_ring->pg_chunk.page);
1132 lbq_desc->p.pg_chunk.last_flag = 0;
1133 }
1134 return 0;
1135}
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001136/* Process (refill) a large buffer queue. */
1137static void ql_update_lbq(struct ql_adapter *qdev, struct rx_ring *rx_ring)
1138{
Ron Mercer49f21862009-02-23 10:42:16 +00001139 u32 clean_idx = rx_ring->lbq_clean_idx;
1140 u32 start_idx = clean_idx;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001141 struct bq_desc *lbq_desc;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001142 u64 map;
1143 int i;
1144
Ron Mercer7c734352009-10-19 03:32:19 +00001145 while (rx_ring->lbq_free_cnt > 32) {
Jitendra Kalsaria81f25d92012-02-03 14:06:51 +00001146 for (i = (rx_ring->lbq_clean_idx % 16); i < 16; i++) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001147 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1148 "lbq: try cleaning clean_idx = %d.\n",
1149 clean_idx);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001150 lbq_desc = &rx_ring->lbq[clean_idx];
Ron Mercer7c734352009-10-19 03:32:19 +00001151 if (ql_get_next_chunk(qdev, rx_ring, lbq_desc)) {
Jitendra Kalsaria81f25d92012-02-03 14:06:51 +00001152 rx_ring->lbq_clean_idx = clean_idx;
Joe Perchesae9540f72010-02-09 11:49:52 +00001153 netif_err(qdev, ifup, qdev->ndev,
Jitendra Kalsaria81f25d92012-02-03 14:06:51 +00001154 "Could not get a page chunk, i=%d, clean_idx =%d .\n",
1155 i, clean_idx);
Joe Perchesae9540f72010-02-09 11:49:52 +00001156 return;
1157 }
Ron Mercer7c734352009-10-19 03:32:19 +00001158
1159 map = lbq_desc->p.pg_chunk.map +
1160 lbq_desc->p.pg_chunk.offset;
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001161 dma_unmap_addr_set(lbq_desc, mapaddr, map);
1162 dma_unmap_len_set(lbq_desc, maplen,
Ron Mercer7c734352009-10-19 03:32:19 +00001163 rx_ring->lbq_buf_size);
Ron Mercer2c9a0d42009-01-05 18:19:20 -08001164 *lbq_desc->addr = cpu_to_le64(map);
Ron Mercer7c734352009-10-19 03:32:19 +00001165
1166 pci_dma_sync_single_for_device(qdev->pdev, map,
1167 rx_ring->lbq_buf_size,
1168 PCI_DMA_FROMDEVICE);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001169 clean_idx++;
1170 if (clean_idx == rx_ring->lbq_len)
1171 clean_idx = 0;
1172 }
1173
1174 rx_ring->lbq_clean_idx = clean_idx;
1175 rx_ring->lbq_prod_idx += 16;
1176 if (rx_ring->lbq_prod_idx == rx_ring->lbq_len)
1177 rx_ring->lbq_prod_idx = 0;
Ron Mercer49f21862009-02-23 10:42:16 +00001178 rx_ring->lbq_free_cnt -= 16;
1179 }
1180
1181 if (start_idx != clean_idx) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001182 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1183 "lbq: updating prod idx = %d.\n",
1184 rx_ring->lbq_prod_idx);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001185 ql_write_db_reg(rx_ring->lbq_prod_idx,
1186 rx_ring->lbq_prod_idx_db_reg);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001187 }
1188}
1189
1190/* Process (refill) a small buffer queue. */
1191static void ql_update_sbq(struct ql_adapter *qdev, struct rx_ring *rx_ring)
1192{
Ron Mercer49f21862009-02-23 10:42:16 +00001193 u32 clean_idx = rx_ring->sbq_clean_idx;
1194 u32 start_idx = clean_idx;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001195 struct bq_desc *sbq_desc;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001196 u64 map;
1197 int i;
1198
1199 while (rx_ring->sbq_free_cnt > 16) {
Jitendra Kalsaria81f25d92012-02-03 14:06:51 +00001200 for (i = (rx_ring->sbq_clean_idx % 16); i < 16; i++) {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001201 sbq_desc = &rx_ring->sbq[clean_idx];
Joe Perchesae9540f72010-02-09 11:49:52 +00001202 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1203 "sbq: try cleaning clean_idx = %d.\n",
1204 clean_idx);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001205 if (sbq_desc->p.skb == NULL) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001206 netif_printk(qdev, rx_status, KERN_DEBUG,
1207 qdev->ndev,
1208 "sbq: getting new skb for index %d.\n",
1209 sbq_desc->index);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001210 sbq_desc->p.skb =
1211 netdev_alloc_skb(qdev->ndev,
Ron Mercer52e55f32009-10-10 09:35:07 +00001212 SMALL_BUFFER_SIZE);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001213 if (sbq_desc->p.skb == NULL) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001214 netif_err(qdev, probe, qdev->ndev,
1215 "Couldn't get an skb.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001216 rx_ring->sbq_clean_idx = clean_idx;
1217 return;
1218 }
1219 skb_reserve(sbq_desc->p.skb, QLGE_SB_PAD);
1220 map = pci_map_single(qdev->pdev,
1221 sbq_desc->p.skb->data,
Ron Mercer52e55f32009-10-10 09:35:07 +00001222 rx_ring->sbq_buf_size,
1223 PCI_DMA_FROMDEVICE);
Ron Mercerc907a352009-01-04 17:06:46 -08001224 if (pci_dma_mapping_error(qdev->pdev, map)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001225 netif_err(qdev, ifup, qdev->ndev,
1226 "PCI mapping failed.\n");
Ron Mercerc907a352009-01-04 17:06:46 -08001227 rx_ring->sbq_clean_idx = clean_idx;
Ron Mercer06a3d512009-02-12 16:37:48 -08001228 dev_kfree_skb_any(sbq_desc->p.skb);
1229 sbq_desc->p.skb = NULL;
Ron Mercerc907a352009-01-04 17:06:46 -08001230 return;
1231 }
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001232 dma_unmap_addr_set(sbq_desc, mapaddr, map);
1233 dma_unmap_len_set(sbq_desc, maplen,
Ron Mercer52e55f32009-10-10 09:35:07 +00001234 rx_ring->sbq_buf_size);
Ron Mercer2c9a0d42009-01-05 18:19:20 -08001235 *sbq_desc->addr = cpu_to_le64(map);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001236 }
1237
1238 clean_idx++;
1239 if (clean_idx == rx_ring->sbq_len)
1240 clean_idx = 0;
1241 }
1242 rx_ring->sbq_clean_idx = clean_idx;
1243 rx_ring->sbq_prod_idx += 16;
1244 if (rx_ring->sbq_prod_idx == rx_ring->sbq_len)
1245 rx_ring->sbq_prod_idx = 0;
Ron Mercer49f21862009-02-23 10:42:16 +00001246 rx_ring->sbq_free_cnt -= 16;
1247 }
1248
1249 if (start_idx != clean_idx) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001250 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1251 "sbq: updating prod idx = %d.\n",
1252 rx_ring->sbq_prod_idx);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001253 ql_write_db_reg(rx_ring->sbq_prod_idx,
1254 rx_ring->sbq_prod_idx_db_reg);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001255 }
1256}
1257
1258static void ql_update_buffer_queues(struct ql_adapter *qdev,
1259 struct rx_ring *rx_ring)
1260{
1261 ql_update_sbq(qdev, rx_ring);
1262 ql_update_lbq(qdev, rx_ring);
1263}
1264
1265/* Unmaps tx buffers. Can be called from send() if a pci mapping
1266 * fails at some stage, or from the interrupt when a tx completes.
1267 */
1268static void ql_unmap_send(struct ql_adapter *qdev,
1269 struct tx_ring_desc *tx_ring_desc, int mapped)
1270{
1271 int i;
1272 for (i = 0; i < mapped; i++) {
1273 if (i == 0 || (i == 7 && mapped > 7)) {
1274 /*
1275 * Unmap the skb->data area, or the
1276 * external sglist (AKA the Outbound
1277 * Address List (OAL)).
1278 * If its the zeroeth element, then it's
1279 * the skb->data area. If it's the 7th
1280 * element and there is more than 6 frags,
1281 * then its an OAL.
1282 */
1283 if (i == 7) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001284 netif_printk(qdev, tx_done, KERN_DEBUG,
1285 qdev->ndev,
1286 "unmapping OAL area.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001287 }
1288 pci_unmap_single(qdev->pdev,
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001289 dma_unmap_addr(&tx_ring_desc->map[i],
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001290 mapaddr),
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001291 dma_unmap_len(&tx_ring_desc->map[i],
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001292 maplen),
1293 PCI_DMA_TODEVICE);
1294 } else {
Joe Perchesae9540f72010-02-09 11:49:52 +00001295 netif_printk(qdev, tx_done, KERN_DEBUG, qdev->ndev,
1296 "unmapping frag %d.\n", i);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001297 pci_unmap_page(qdev->pdev,
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001298 dma_unmap_addr(&tx_ring_desc->map[i],
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001299 mapaddr),
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001300 dma_unmap_len(&tx_ring_desc->map[i],
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001301 maplen), PCI_DMA_TODEVICE);
1302 }
1303 }
1304
1305}
1306
1307/* Map the buffers for this transmit. This will return
1308 * NETDEV_TX_BUSY or NETDEV_TX_OK based on success.
1309 */
1310static int ql_map_send(struct ql_adapter *qdev,
1311 struct ob_mac_iocb_req *mac_iocb_ptr,
1312 struct sk_buff *skb, struct tx_ring_desc *tx_ring_desc)
1313{
1314 int len = skb_headlen(skb);
1315 dma_addr_t map;
1316 int frag_idx, err, map_idx = 0;
1317 struct tx_buf_desc *tbd = mac_iocb_ptr->tbd;
1318 int frag_cnt = skb_shinfo(skb)->nr_frags;
1319
1320 if (frag_cnt) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001321 netif_printk(qdev, tx_queued, KERN_DEBUG, qdev->ndev,
1322 "frag_cnt = %d.\n", frag_cnt);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001323 }
1324 /*
1325 * Map the skb buffer first.
1326 */
1327 map = pci_map_single(qdev->pdev, skb->data, len, PCI_DMA_TODEVICE);
1328
1329 err = pci_dma_mapping_error(qdev->pdev, map);
1330 if (err) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001331 netif_err(qdev, tx_queued, qdev->ndev,
1332 "PCI mapping failed with error: %d\n", err);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001333
1334 return NETDEV_TX_BUSY;
1335 }
1336
1337 tbd->len = cpu_to_le32(len);
1338 tbd->addr = cpu_to_le64(map);
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001339 dma_unmap_addr_set(&tx_ring_desc->map[map_idx], mapaddr, map);
1340 dma_unmap_len_set(&tx_ring_desc->map[map_idx], maplen, len);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001341 map_idx++;
1342
1343 /*
1344 * This loop fills the remainder of the 8 address descriptors
1345 * in the IOCB. If there are more than 7 fragments, then the
1346 * eighth address desc will point to an external list (OAL).
1347 * When this happens, the remainder of the frags will be stored
1348 * in this list.
1349 */
1350 for (frag_idx = 0; frag_idx < frag_cnt; frag_idx++, map_idx++) {
1351 skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_idx];
1352 tbd++;
1353 if (frag_idx == 6 && frag_cnt > 7) {
1354 /* Let's tack on an sglist.
1355 * Our control block will now
1356 * look like this:
1357 * iocb->seg[0] = skb->data
1358 * iocb->seg[1] = frag[0]
1359 * iocb->seg[2] = frag[1]
1360 * iocb->seg[3] = frag[2]
1361 * iocb->seg[4] = frag[3]
1362 * iocb->seg[5] = frag[4]
1363 * iocb->seg[6] = frag[5]
1364 * iocb->seg[7] = ptr to OAL (external sglist)
1365 * oal->seg[0] = frag[6]
1366 * oal->seg[1] = frag[7]
1367 * oal->seg[2] = frag[8]
1368 * oal->seg[3] = frag[9]
1369 * oal->seg[4] = frag[10]
1370 * etc...
1371 */
1372 /* Tack on the OAL in the eighth segment of IOCB. */
1373 map = pci_map_single(qdev->pdev, &tx_ring_desc->oal,
1374 sizeof(struct oal),
1375 PCI_DMA_TODEVICE);
1376 err = pci_dma_mapping_error(qdev->pdev, map);
1377 if (err) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001378 netif_err(qdev, tx_queued, qdev->ndev,
1379 "PCI mapping outbound address list with error: %d\n",
1380 err);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001381 goto map_error;
1382 }
1383
1384 tbd->addr = cpu_to_le64(map);
1385 /*
1386 * The length is the number of fragments
1387 * that remain to be mapped times the length
1388 * of our sglist (OAL).
1389 */
1390 tbd->len =
1391 cpu_to_le32((sizeof(struct tx_buf_desc) *
1392 (frag_cnt - frag_idx)) | TX_DESC_C);
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001393 dma_unmap_addr_set(&tx_ring_desc->map[map_idx], mapaddr,
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001394 map);
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001395 dma_unmap_len_set(&tx_ring_desc->map[map_idx], maplen,
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001396 sizeof(struct oal));
1397 tbd = (struct tx_buf_desc *)&tx_ring_desc->oal;
1398 map_idx++;
1399 }
1400
Eric Dumazet9e903e02011-10-18 21:00:24 +00001401 map = skb_frag_dma_map(&qdev->pdev->dev, frag, 0, skb_frag_size(frag),
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01001402 DMA_TO_DEVICE);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001403
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01001404 err = dma_mapping_error(&qdev->pdev->dev, map);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001405 if (err) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001406 netif_err(qdev, tx_queued, qdev->ndev,
1407 "PCI mapping frags failed with error: %d.\n",
1408 err);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001409 goto map_error;
1410 }
1411
1412 tbd->addr = cpu_to_le64(map);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001413 tbd->len = cpu_to_le32(skb_frag_size(frag));
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001414 dma_unmap_addr_set(&tx_ring_desc->map[map_idx], mapaddr, map);
1415 dma_unmap_len_set(&tx_ring_desc->map[map_idx], maplen,
Eric Dumazet9e903e02011-10-18 21:00:24 +00001416 skb_frag_size(frag));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001417
1418 }
1419 /* Save the number of segments we've mapped. */
1420 tx_ring_desc->map_cnt = map_idx;
1421 /* Terminate the last segment. */
1422 tbd->len = cpu_to_le32(le32_to_cpu(tbd->len) | TX_DESC_E);
1423 return NETDEV_TX_OK;
1424
1425map_error:
1426 /*
1427 * If the first frag mapping failed, then i will be zero.
1428 * This causes the unmap of the skb->data area. Otherwise
1429 * we pass in the number of frags that mapped successfully
1430 * so they can be umapped.
1431 */
1432 ql_unmap_send(qdev, tx_ring_desc, map_idx);
1433 return NETDEV_TX_BUSY;
1434}
1435
Jitendra Kalsaria433c88e2012-07-10 14:57:37 +00001436/* Categorizing receive firmware frame errors */
Sritej Velagaae721f32013-04-18 19:49:52 +00001437static void ql_categorize_rx_err(struct ql_adapter *qdev, u8 rx_err,
1438 struct rx_ring *rx_ring)
Jitendra Kalsaria433c88e2012-07-10 14:57:37 +00001439{
1440 struct nic_stats *stats = &qdev->nic_stats;
1441
1442 stats->rx_err_count++;
Sritej Velagaae721f32013-04-18 19:49:52 +00001443 rx_ring->rx_errors++;
Jitendra Kalsaria433c88e2012-07-10 14:57:37 +00001444
1445 switch (rx_err & IB_MAC_IOCB_RSP_ERR_MASK) {
1446 case IB_MAC_IOCB_RSP_ERR_CODE_ERR:
1447 stats->rx_code_err++;
1448 break;
1449 case IB_MAC_IOCB_RSP_ERR_OVERSIZE:
1450 stats->rx_oversize_err++;
1451 break;
1452 case IB_MAC_IOCB_RSP_ERR_UNDERSIZE:
1453 stats->rx_undersize_err++;
1454 break;
1455 case IB_MAC_IOCB_RSP_ERR_PREAMBLE:
1456 stats->rx_preamble_err++;
1457 break;
1458 case IB_MAC_IOCB_RSP_ERR_FRAME_LEN:
1459 stats->rx_frame_len_err++;
1460 break;
1461 case IB_MAC_IOCB_RSP_ERR_CRC:
1462 stats->rx_crc_err++;
1463 default:
1464 break;
1465 }
1466}
1467
Ron Mercer4f848c02010-01-02 10:37:43 +00001468/* Process an inbound completion from an rx ring. */
Ron Mercer63526712010-01-02 10:37:44 +00001469static void ql_process_mac_rx_gro_page(struct ql_adapter *qdev,
1470 struct rx_ring *rx_ring,
1471 struct ib_mac_iocb_rsp *ib_mac_rsp,
1472 u32 length,
1473 u16 vlan_id)
1474{
1475 struct sk_buff *skb;
1476 struct bq_desc *lbq_desc = ql_get_curr_lchunk(qdev, rx_ring);
Ron Mercer63526712010-01-02 10:37:44 +00001477 struct napi_struct *napi = &rx_ring->napi;
1478
Sritej Velagaae721f32013-04-18 19:49:52 +00001479 /* Frame error, so drop the packet. */
1480 if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_ERR_MASK) {
1481 ql_categorize_rx_err(qdev, ib_mac_rsp->flags2, rx_ring);
1482 put_page(lbq_desc->p.pg_chunk.page);
1483 return;
1484 }
Ron Mercer63526712010-01-02 10:37:44 +00001485 napi->dev = qdev->ndev;
1486
1487 skb = napi_get_frags(napi);
1488 if (!skb) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001489 netif_err(qdev, drv, qdev->ndev,
1490 "Couldn't get an skb, exiting.\n");
Ron Mercer63526712010-01-02 10:37:44 +00001491 rx_ring->rx_dropped++;
1492 put_page(lbq_desc->p.pg_chunk.page);
1493 return;
1494 }
1495 prefetch(lbq_desc->p.pg_chunk.va);
Ian Campbellda7ebfd2011-08-31 00:47:05 +00001496 __skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
1497 lbq_desc->p.pg_chunk.page,
1498 lbq_desc->p.pg_chunk.offset,
1499 length);
Ron Mercer63526712010-01-02 10:37:44 +00001500
1501 skb->len += length;
1502 skb->data_len += length;
1503 skb->truesize += length;
1504 skb_shinfo(skb)->nr_frags++;
1505
1506 rx_ring->rx_packets++;
1507 rx_ring->rx_bytes += length;
1508 skb->ip_summed = CHECKSUM_UNNECESSARY;
1509 skb_record_rx_queue(skb, rx_ring->cq_id);
Jiri Pirko18c49b92011-07-21 03:24:11 +00001510 if (vlan_id != 0xffff)
1511 __vlan_hwaccel_put_tag(skb, vlan_id);
1512 napi_gro_frags(napi);
Ron Mercer63526712010-01-02 10:37:44 +00001513}
1514
1515/* Process an inbound completion from an rx ring. */
Ron Mercer4f848c02010-01-02 10:37:43 +00001516static void ql_process_mac_rx_page(struct ql_adapter *qdev,
1517 struct rx_ring *rx_ring,
1518 struct ib_mac_iocb_rsp *ib_mac_rsp,
1519 u32 length,
1520 u16 vlan_id)
1521{
1522 struct net_device *ndev = qdev->ndev;
1523 struct sk_buff *skb = NULL;
1524 void *addr;
1525 struct bq_desc *lbq_desc = ql_get_curr_lchunk(qdev, rx_ring);
1526 struct napi_struct *napi = &rx_ring->napi;
1527
1528 skb = netdev_alloc_skb(ndev, length);
1529 if (!skb) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001530 netif_err(qdev, drv, qdev->ndev,
1531 "Couldn't get an skb, need to unwind!.\n");
Ron Mercer4f848c02010-01-02 10:37:43 +00001532 rx_ring->rx_dropped++;
1533 put_page(lbq_desc->p.pg_chunk.page);
1534 return;
1535 }
1536
1537 addr = lbq_desc->p.pg_chunk.va;
1538 prefetch(addr);
1539
Sritej Velagaae721f32013-04-18 19:49:52 +00001540 /* Frame error, so drop the packet. */
1541 if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_ERR_MASK) {
1542 ql_categorize_rx_err(qdev, ib_mac_rsp->flags2, rx_ring);
1543 goto err_out;
1544 }
1545
Ron Mercer4f848c02010-01-02 10:37:43 +00001546 /* The max framesize filter on this chip is set higher than
1547 * MTU since FCoE uses 2k frames.
1548 */
1549 if (skb->len > ndev->mtu + ETH_HLEN) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001550 netif_err(qdev, drv, qdev->ndev,
1551 "Segment too small, dropping.\n");
Ron Mercer4f848c02010-01-02 10:37:43 +00001552 rx_ring->rx_dropped++;
1553 goto err_out;
1554 }
1555 memcpy(skb_put(skb, ETH_HLEN), addr, ETH_HLEN);
Joe Perchesae9540f72010-02-09 11:49:52 +00001556 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1557 "%d bytes of headers and data in large. Chain page to new skb and pull tail.\n",
1558 length);
Ron Mercer4f848c02010-01-02 10:37:43 +00001559 skb_fill_page_desc(skb, 0, lbq_desc->p.pg_chunk.page,
1560 lbq_desc->p.pg_chunk.offset+ETH_HLEN,
1561 length-ETH_HLEN);
1562 skb->len += length-ETH_HLEN;
1563 skb->data_len += length-ETH_HLEN;
1564 skb->truesize += length-ETH_HLEN;
1565
1566 rx_ring->rx_packets++;
1567 rx_ring->rx_bytes += skb->len;
1568 skb->protocol = eth_type_trans(skb, ndev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001569 skb_checksum_none_assert(skb);
Ron Mercer4f848c02010-01-02 10:37:43 +00001570
Michał Mirosław88230fd2011-04-18 13:31:21 +00001571 if ((ndev->features & NETIF_F_RXCSUM) &&
Ron Mercer4f848c02010-01-02 10:37:43 +00001572 !(ib_mac_rsp->flags1 & IB_MAC_CSUM_ERR_MASK)) {
1573 /* TCP frame. */
1574 if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_T) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001575 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1576 "TCP checksum done!\n");
Ron Mercer4f848c02010-01-02 10:37:43 +00001577 skb->ip_summed = CHECKSUM_UNNECESSARY;
1578 } else if ((ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_U) &&
1579 (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_V4)) {
1580 /* Unfragmented ipv4 UDP frame. */
Jitendra Kalsariae02ef3312012-02-03 14:06:49 +00001581 struct iphdr *iph =
1582 (struct iphdr *) ((u8 *)addr + ETH_HLEN);
Ron Mercer4f848c02010-01-02 10:37:43 +00001583 if (!(iph->frag_off &
Li RongQing0d653ed82012-07-09 22:02:42 +00001584 htons(IP_MF|IP_OFFSET))) {
Ron Mercer4f848c02010-01-02 10:37:43 +00001585 skb->ip_summed = CHECKSUM_UNNECESSARY;
Joe Perchesae9540f72010-02-09 11:49:52 +00001586 netif_printk(qdev, rx_status, KERN_DEBUG,
1587 qdev->ndev,
Jitendra Kalsariae02ef3312012-02-03 14:06:49 +00001588 "UDP checksum done!\n");
Ron Mercer4f848c02010-01-02 10:37:43 +00001589 }
1590 }
1591 }
1592
1593 skb_record_rx_queue(skb, rx_ring->cq_id);
Jiri Pirko18c49b92011-07-21 03:24:11 +00001594 if (vlan_id != 0xffff)
1595 __vlan_hwaccel_put_tag(skb, vlan_id);
1596 if (skb->ip_summed == CHECKSUM_UNNECESSARY)
1597 napi_gro_receive(napi, skb);
1598 else
1599 netif_receive_skb(skb);
Ron Mercer4f848c02010-01-02 10:37:43 +00001600 return;
1601err_out:
1602 dev_kfree_skb_any(skb);
1603 put_page(lbq_desc->p.pg_chunk.page);
1604}
1605
1606/* Process an inbound completion from an rx ring. */
1607static void ql_process_mac_rx_skb(struct ql_adapter *qdev,
1608 struct rx_ring *rx_ring,
1609 struct ib_mac_iocb_rsp *ib_mac_rsp,
1610 u32 length,
1611 u16 vlan_id)
1612{
1613 struct net_device *ndev = qdev->ndev;
1614 struct sk_buff *skb = NULL;
1615 struct sk_buff *new_skb = NULL;
1616 struct bq_desc *sbq_desc = ql_get_curr_sbuf(rx_ring);
1617
1618 skb = sbq_desc->p.skb;
1619 /* Allocate new_skb and copy */
1620 new_skb = netdev_alloc_skb(qdev->ndev, length + NET_IP_ALIGN);
1621 if (new_skb == NULL) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001622 netif_err(qdev, probe, qdev->ndev,
1623 "No skb available, drop the packet.\n");
Ron Mercer4f848c02010-01-02 10:37:43 +00001624 rx_ring->rx_dropped++;
1625 return;
1626 }
1627 skb_reserve(new_skb, NET_IP_ALIGN);
1628 memcpy(skb_put(new_skb, length), skb->data, length);
1629 skb = new_skb;
1630
Sritej Velagaae721f32013-04-18 19:49:52 +00001631 /* Frame error, so drop the packet. */
1632 if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_ERR_MASK) {
1633 ql_categorize_rx_err(qdev, ib_mac_rsp->flags2, rx_ring);
1634 dev_kfree_skb_any(skb);
1635 return;
1636 }
1637
Ron Mercer4f848c02010-01-02 10:37:43 +00001638 /* loopback self test for ethtool */
1639 if (test_bit(QL_SELFTEST, &qdev->flags)) {
1640 ql_check_lb_frame(qdev, skb);
1641 dev_kfree_skb_any(skb);
1642 return;
1643 }
1644
1645 /* The max framesize filter on this chip is set higher than
1646 * MTU since FCoE uses 2k frames.
1647 */
1648 if (skb->len > ndev->mtu + ETH_HLEN) {
1649 dev_kfree_skb_any(skb);
1650 rx_ring->rx_dropped++;
1651 return;
1652 }
1653
1654 prefetch(skb->data);
Ron Mercer4f848c02010-01-02 10:37:43 +00001655 if (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001656 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1657 "%s Multicast.\n",
1658 (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
1659 IB_MAC_IOCB_RSP_M_HASH ? "Hash" :
1660 (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
1661 IB_MAC_IOCB_RSP_M_REG ? "Registered" :
1662 (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
1663 IB_MAC_IOCB_RSP_M_PROM ? "Promiscuous" : "");
Ron Mercer4f848c02010-01-02 10:37:43 +00001664 }
1665 if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_P)
Joe Perchesae9540f72010-02-09 11:49:52 +00001666 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1667 "Promiscuous Packet.\n");
Ron Mercer4f848c02010-01-02 10:37:43 +00001668
1669 rx_ring->rx_packets++;
1670 rx_ring->rx_bytes += skb->len;
1671 skb->protocol = eth_type_trans(skb, ndev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001672 skb_checksum_none_assert(skb);
Ron Mercer4f848c02010-01-02 10:37:43 +00001673
1674 /* If rx checksum is on, and there are no
1675 * csum or frame errors.
1676 */
Michał Mirosław88230fd2011-04-18 13:31:21 +00001677 if ((ndev->features & NETIF_F_RXCSUM) &&
Ron Mercer4f848c02010-01-02 10:37:43 +00001678 !(ib_mac_rsp->flags1 & IB_MAC_CSUM_ERR_MASK)) {
1679 /* TCP frame. */
1680 if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_T) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001681 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1682 "TCP checksum done!\n");
Ron Mercer4f848c02010-01-02 10:37:43 +00001683 skb->ip_summed = CHECKSUM_UNNECESSARY;
1684 } else if ((ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_U) &&
1685 (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_V4)) {
1686 /* Unfragmented ipv4 UDP frame. */
1687 struct iphdr *iph = (struct iphdr *) skb->data;
1688 if (!(iph->frag_off &
Li RongQing0d653ed82012-07-09 22:02:42 +00001689 htons(IP_MF|IP_OFFSET))) {
Ron Mercer4f848c02010-01-02 10:37:43 +00001690 skb->ip_summed = CHECKSUM_UNNECESSARY;
Joe Perchesae9540f72010-02-09 11:49:52 +00001691 netif_printk(qdev, rx_status, KERN_DEBUG,
1692 qdev->ndev,
Jitendra Kalsariae02ef3312012-02-03 14:06:49 +00001693 "UDP checksum done!\n");
Ron Mercer4f848c02010-01-02 10:37:43 +00001694 }
1695 }
1696 }
1697
1698 skb_record_rx_queue(skb, rx_ring->cq_id);
Jiri Pirko18c49b92011-07-21 03:24:11 +00001699 if (vlan_id != 0xffff)
1700 __vlan_hwaccel_put_tag(skb, vlan_id);
1701 if (skb->ip_summed == CHECKSUM_UNNECESSARY)
1702 napi_gro_receive(&rx_ring->napi, skb);
1703 else
1704 netif_receive_skb(skb);
Ron Mercer4f848c02010-01-02 10:37:43 +00001705}
1706
Stephen Hemminger8668ae92008-11-21 17:29:50 -08001707static void ql_realign_skb(struct sk_buff *skb, int len)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001708{
1709 void *temp_addr = skb->data;
1710
1711 /* Undo the skb_reserve(skb,32) we did before
1712 * giving to hardware, and realign data on
1713 * a 2-byte boundary.
1714 */
1715 skb->data -= QLGE_SB_PAD - NET_IP_ALIGN;
1716 skb->tail -= QLGE_SB_PAD - NET_IP_ALIGN;
1717 skb_copy_to_linear_data(skb, temp_addr,
1718 (unsigned int)len);
1719}
1720
1721/*
1722 * This function builds an skb for the given inbound
1723 * completion. It will be rewritten for readability in the near
1724 * future, but for not it works well.
1725 */
1726static struct sk_buff *ql_build_rx_skb(struct ql_adapter *qdev,
1727 struct rx_ring *rx_ring,
1728 struct ib_mac_iocb_rsp *ib_mac_rsp)
1729{
1730 struct bq_desc *lbq_desc;
1731 struct bq_desc *sbq_desc;
1732 struct sk_buff *skb = NULL;
1733 u32 length = le32_to_cpu(ib_mac_rsp->data_len);
1734 u32 hdr_len = le32_to_cpu(ib_mac_rsp->hdr_len);
1735
1736 /*
1737 * Handle the header buffer if present.
1738 */
1739 if (ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HV &&
1740 ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001741 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1742 "Header of %d bytes in small buffer.\n", hdr_len);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001743 /*
1744 * Headers fit nicely into a small buffer.
1745 */
1746 sbq_desc = ql_get_curr_sbuf(rx_ring);
1747 pci_unmap_single(qdev->pdev,
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001748 dma_unmap_addr(sbq_desc, mapaddr),
1749 dma_unmap_len(sbq_desc, maplen),
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001750 PCI_DMA_FROMDEVICE);
1751 skb = sbq_desc->p.skb;
1752 ql_realign_skb(skb, hdr_len);
1753 skb_put(skb, hdr_len);
1754 sbq_desc->p.skb = NULL;
1755 }
1756
1757 /*
1758 * Handle the data buffer(s).
1759 */
1760 if (unlikely(!length)) { /* Is there data too? */
Joe Perchesae9540f72010-02-09 11:49:52 +00001761 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1762 "No Data buffer in this packet.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001763 return skb;
1764 }
1765
1766 if (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_DS) {
1767 if (ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001768 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1769 "Headers in small, data of %d bytes in small, combine them.\n",
1770 length);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001771 /*
1772 * Data is less than small buffer size so it's
1773 * stuffed in a small buffer.
1774 * For this case we append the data
1775 * from the "data" small buffer to the "header" small
1776 * buffer.
1777 */
1778 sbq_desc = ql_get_curr_sbuf(rx_ring);
1779 pci_dma_sync_single_for_cpu(qdev->pdev,
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001780 dma_unmap_addr
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001781 (sbq_desc, mapaddr),
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001782 dma_unmap_len
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001783 (sbq_desc, maplen),
1784 PCI_DMA_FROMDEVICE);
1785 memcpy(skb_put(skb, length),
1786 sbq_desc->p.skb->data, length);
1787 pci_dma_sync_single_for_device(qdev->pdev,
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001788 dma_unmap_addr
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001789 (sbq_desc,
1790 mapaddr),
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001791 dma_unmap_len
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001792 (sbq_desc,
1793 maplen),
1794 PCI_DMA_FROMDEVICE);
1795 } else {
Joe Perchesae9540f72010-02-09 11:49:52 +00001796 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1797 "%d bytes in a single small buffer.\n",
1798 length);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001799 sbq_desc = ql_get_curr_sbuf(rx_ring);
1800 skb = sbq_desc->p.skb;
1801 ql_realign_skb(skb, length);
1802 skb_put(skb, length);
1803 pci_unmap_single(qdev->pdev,
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001804 dma_unmap_addr(sbq_desc,
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001805 mapaddr),
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001806 dma_unmap_len(sbq_desc,
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001807 maplen),
1808 PCI_DMA_FROMDEVICE);
1809 sbq_desc->p.skb = NULL;
1810 }
1811 } else if (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_DL) {
1812 if (ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001813 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1814 "Header in small, %d bytes in large. Chain large to small!\n",
1815 length);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001816 /*
1817 * The data is in a single large buffer. We
1818 * chain it to the header buffer's skb and let
1819 * it rip.
1820 */
Ron Mercer7c734352009-10-19 03:32:19 +00001821 lbq_desc = ql_get_curr_lchunk(qdev, rx_ring);
Joe Perchesae9540f72010-02-09 11:49:52 +00001822 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1823 "Chaining page at offset = %d, for %d bytes to skb.\n",
1824 lbq_desc->p.pg_chunk.offset, length);
Ron Mercer7c734352009-10-19 03:32:19 +00001825 skb_fill_page_desc(skb, 0, lbq_desc->p.pg_chunk.page,
1826 lbq_desc->p.pg_chunk.offset,
1827 length);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001828 skb->len += length;
1829 skb->data_len += length;
1830 skb->truesize += length;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001831 } else {
1832 /*
1833 * The headers and data are in a single large buffer. We
1834 * copy it to a new skb and let it go. This can happen with
1835 * jumbo mtu on a non-TCP/UDP frame.
1836 */
Ron Mercer7c734352009-10-19 03:32:19 +00001837 lbq_desc = ql_get_curr_lchunk(qdev, rx_ring);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001838 skb = netdev_alloc_skb(qdev->ndev, length);
1839 if (skb == NULL) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001840 netif_printk(qdev, probe, KERN_DEBUG, qdev->ndev,
1841 "No skb available, drop the packet.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001842 return NULL;
1843 }
Ron Mercer4055c7d42009-01-04 17:07:09 -08001844 pci_unmap_page(qdev->pdev,
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001845 dma_unmap_addr(lbq_desc,
Ron Mercer4055c7d42009-01-04 17:07:09 -08001846 mapaddr),
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001847 dma_unmap_len(lbq_desc, maplen),
Ron Mercer4055c7d42009-01-04 17:07:09 -08001848 PCI_DMA_FROMDEVICE);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001849 skb_reserve(skb, NET_IP_ALIGN);
Joe Perchesae9540f72010-02-09 11:49:52 +00001850 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1851 "%d bytes of headers and data in large. Chain page to new skb and pull tail.\n",
1852 length);
Ron Mercer7c734352009-10-19 03:32:19 +00001853 skb_fill_page_desc(skb, 0,
1854 lbq_desc->p.pg_chunk.page,
1855 lbq_desc->p.pg_chunk.offset,
1856 length);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001857 skb->len += length;
1858 skb->data_len += length;
1859 skb->truesize += length;
1860 length -= length;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001861 __pskb_pull_tail(skb,
1862 (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_V) ?
1863 VLAN_ETH_HLEN : ETH_HLEN);
1864 }
1865 } else {
1866 /*
1867 * The data is in a chain of large buffers
1868 * pointed to by a small buffer. We loop
1869 * thru and chain them to the our small header
1870 * buffer's skb.
1871 * frags: There are 18 max frags and our small
1872 * buffer will hold 32 of them. The thing is,
1873 * we'll use 3 max for our 9000 byte jumbo
1874 * frames. If the MTU goes up we could
1875 * eventually be in trouble.
1876 */
Ron Mercer7c734352009-10-19 03:32:19 +00001877 int size, i = 0;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001878 sbq_desc = ql_get_curr_sbuf(rx_ring);
1879 pci_unmap_single(qdev->pdev,
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00001880 dma_unmap_addr(sbq_desc, mapaddr),
1881 dma_unmap_len(sbq_desc, maplen),
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001882 PCI_DMA_FROMDEVICE);
1883 if (!(ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS)) {
1884 /*
1885 * This is an non TCP/UDP IP frame, so
1886 * the headers aren't split into a small
1887 * buffer. We have to use the small buffer
1888 * that contains our sg list as our skb to
1889 * send upstairs. Copy the sg list here to
1890 * a local buffer and use it to find the
1891 * pages to chain.
1892 */
Joe Perchesae9540f72010-02-09 11:49:52 +00001893 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1894 "%d bytes of headers & data in chain of large.\n",
1895 length);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001896 skb = sbq_desc->p.skb;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001897 sbq_desc->p.skb = NULL;
1898 skb_reserve(skb, NET_IP_ALIGN);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001899 }
1900 while (length > 0) {
Ron Mercer7c734352009-10-19 03:32:19 +00001901 lbq_desc = ql_get_curr_lchunk(qdev, rx_ring);
1902 size = (length < rx_ring->lbq_buf_size) ? length :
1903 rx_ring->lbq_buf_size;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001904
Joe Perchesae9540f72010-02-09 11:49:52 +00001905 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1906 "Adding page %d to skb for %d bytes.\n",
1907 i, size);
Ron Mercer7c734352009-10-19 03:32:19 +00001908 skb_fill_page_desc(skb, i,
1909 lbq_desc->p.pg_chunk.page,
1910 lbq_desc->p.pg_chunk.offset,
1911 size);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001912 skb->len += size;
1913 skb->data_len += size;
1914 skb->truesize += size;
1915 length -= size;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001916 i++;
1917 }
1918 __pskb_pull_tail(skb, (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_V) ?
1919 VLAN_ETH_HLEN : ETH_HLEN);
1920 }
1921 return skb;
1922}
1923
1924/* Process an inbound completion from an rx ring. */
Ron Mercer4f848c02010-01-02 10:37:43 +00001925static void ql_process_mac_split_rx_intr(struct ql_adapter *qdev,
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001926 struct rx_ring *rx_ring,
Ron Mercer4f848c02010-01-02 10:37:43 +00001927 struct ib_mac_iocb_rsp *ib_mac_rsp,
1928 u16 vlan_id)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001929{
1930 struct net_device *ndev = qdev->ndev;
1931 struct sk_buff *skb = NULL;
1932
1933 QL_DUMP_IB_MAC_RSP(ib_mac_rsp);
1934
1935 skb = ql_build_rx_skb(qdev, rx_ring, ib_mac_rsp);
1936 if (unlikely(!skb)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001937 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1938 "No skb available, drop packet.\n");
Ron Mercer885ee392009-11-03 13:49:31 +00001939 rx_ring->rx_dropped++;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001940 return;
1941 }
1942
Sritej Velagaae721f32013-04-18 19:49:52 +00001943 /* Frame error, so drop the packet. */
1944 if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_ERR_MASK) {
1945 ql_categorize_rx_err(qdev, ib_mac_rsp->flags2, rx_ring);
1946 dev_kfree_skb_any(skb);
1947 return;
1948 }
1949
Ron Mercerec33a492009-06-09 05:39:28 +00001950 /* The max framesize filter on this chip is set higher than
1951 * MTU since FCoE uses 2k frames.
1952 */
1953 if (skb->len > ndev->mtu + ETH_HLEN) {
1954 dev_kfree_skb_any(skb);
Ron Mercer885ee392009-11-03 13:49:31 +00001955 rx_ring->rx_dropped++;
Ron Mercerec33a492009-06-09 05:39:28 +00001956 return;
1957 }
1958
Ron Mercer9dfbbaa2009-10-30 12:13:33 +00001959 /* loopback self test for ethtool */
1960 if (test_bit(QL_SELFTEST, &qdev->flags)) {
1961 ql_check_lb_frame(qdev, skb);
1962 dev_kfree_skb_any(skb);
1963 return;
1964 }
1965
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001966 prefetch(skb->data);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001967 if (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001968 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev, "%s Multicast.\n",
1969 (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
1970 IB_MAC_IOCB_RSP_M_HASH ? "Hash" :
1971 (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
1972 IB_MAC_IOCB_RSP_M_REG ? "Registered" :
1973 (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
1974 IB_MAC_IOCB_RSP_M_PROM ? "Promiscuous" : "");
Ron Mercer885ee392009-11-03 13:49:31 +00001975 rx_ring->rx_multicast++;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001976 }
1977 if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_P) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001978 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1979 "Promiscuous Packet.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001980 }
Ron Mercerd555f592009-03-09 10:59:19 +00001981
Ron Mercerd555f592009-03-09 10:59:19 +00001982 skb->protocol = eth_type_trans(skb, ndev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001983 skb_checksum_none_assert(skb);
Ron Mercerd555f592009-03-09 10:59:19 +00001984
1985 /* If rx checksum is on, and there are no
1986 * csum or frame errors.
1987 */
Michał Mirosław88230fd2011-04-18 13:31:21 +00001988 if ((ndev->features & NETIF_F_RXCSUM) &&
Ron Mercerd555f592009-03-09 10:59:19 +00001989 !(ib_mac_rsp->flags1 & IB_MAC_CSUM_ERR_MASK)) {
1990 /* TCP frame. */
1991 if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_T) {
Joe Perchesae9540f72010-02-09 11:49:52 +00001992 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
1993 "TCP checksum done!\n");
Ron Mercerd555f592009-03-09 10:59:19 +00001994 skb->ip_summed = CHECKSUM_UNNECESSARY;
1995 } else if ((ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_U) &&
1996 (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_V4)) {
1997 /* Unfragmented ipv4 UDP frame. */
1998 struct iphdr *iph = (struct iphdr *) skb->data;
1999 if (!(iph->frag_off &
Li RongQing0d653ed82012-07-09 22:02:42 +00002000 htons(IP_MF|IP_OFFSET))) {
Ron Mercerd555f592009-03-09 10:59:19 +00002001 skb->ip_summed = CHECKSUM_UNNECESSARY;
Joe Perchesae9540f72010-02-09 11:49:52 +00002002 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
2003 "TCP checksum done!\n");
Ron Mercerd555f592009-03-09 10:59:19 +00002004 }
2005 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002006 }
Ron Mercerd555f592009-03-09 10:59:19 +00002007
Ron Mercer885ee392009-11-03 13:49:31 +00002008 rx_ring->rx_packets++;
2009 rx_ring->rx_bytes += skb->len;
Ron Mercerb2014ff2009-08-27 11:02:09 +00002010 skb_record_rx_queue(skb, rx_ring->cq_id);
Jiri Pirko18c49b92011-07-21 03:24:11 +00002011 if ((ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_V) && (vlan_id != 0))
2012 __vlan_hwaccel_put_tag(skb, vlan_id);
2013 if (skb->ip_summed == CHECKSUM_UNNECESSARY)
2014 napi_gro_receive(&rx_ring->napi, skb);
2015 else
2016 netif_receive_skb(skb);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002017}
2018
Ron Mercer4f848c02010-01-02 10:37:43 +00002019/* Process an inbound completion from an rx ring. */
2020static unsigned long ql_process_mac_rx_intr(struct ql_adapter *qdev,
2021 struct rx_ring *rx_ring,
2022 struct ib_mac_iocb_rsp *ib_mac_rsp)
2023{
2024 u32 length = le32_to_cpu(ib_mac_rsp->data_len);
2025 u16 vlan_id = (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_V) ?
2026 ((le16_to_cpu(ib_mac_rsp->vlan_id) &
2027 IB_MAC_IOCB_RSP_VLAN_MASK)) : 0xffff;
2028
2029 QL_DUMP_IB_MAC_RSP(ib_mac_rsp);
2030
2031 if (ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HV) {
2032 /* The data and headers are split into
2033 * separate buffers.
2034 */
2035 ql_process_mac_split_rx_intr(qdev, rx_ring, ib_mac_rsp,
2036 vlan_id);
2037 } else if (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_DS) {
2038 /* The data fit in a single small buffer.
2039 * Allocate a new skb, copy the data and
2040 * return the buffer to the free pool.
2041 */
2042 ql_process_mac_rx_skb(qdev, rx_ring, ib_mac_rsp,
2043 length, vlan_id);
Ron Mercer63526712010-01-02 10:37:44 +00002044 } else if ((ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_DL) &&
2045 !(ib_mac_rsp->flags1 & IB_MAC_CSUM_ERR_MASK) &&
2046 (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_T)) {
2047 /* TCP packet in a page chunk that's been checksummed.
2048 * Tack it on to our GRO skb and let it go.
2049 */
2050 ql_process_mac_rx_gro_page(qdev, rx_ring, ib_mac_rsp,
2051 length, vlan_id);
Ron Mercer4f848c02010-01-02 10:37:43 +00002052 } else if (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_DL) {
2053 /* Non-TCP packet in a page chunk. Allocate an
2054 * skb, tack it on frags, and send it up.
2055 */
2056 ql_process_mac_rx_page(qdev, rx_ring, ib_mac_rsp,
2057 length, vlan_id);
2058 } else {
Ron Mercerc0c56952010-02-17 06:41:21 +00002059 /* Non-TCP/UDP large frames that span multiple buffers
2060 * can be processed corrrectly by the split frame logic.
2061 */
2062 ql_process_mac_split_rx_intr(qdev, rx_ring, ib_mac_rsp,
2063 vlan_id);
Ron Mercer4f848c02010-01-02 10:37:43 +00002064 }
2065
2066 return (unsigned long)length;
2067}
2068
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002069/* Process an outbound completion from an rx ring. */
2070static void ql_process_mac_tx_intr(struct ql_adapter *qdev,
2071 struct ob_mac_iocb_rsp *mac_rsp)
2072{
2073 struct tx_ring *tx_ring;
2074 struct tx_ring_desc *tx_ring_desc;
2075
2076 QL_DUMP_OB_MAC_RSP(mac_rsp);
2077 tx_ring = &qdev->tx_ring[mac_rsp->txq_idx];
2078 tx_ring_desc = &tx_ring->q[mac_rsp->tid];
2079 ql_unmap_send(qdev, tx_ring_desc, tx_ring_desc->map_cnt);
Ron Mercer885ee392009-11-03 13:49:31 +00002080 tx_ring->tx_bytes += (tx_ring_desc->skb)->len;
2081 tx_ring->tx_packets++;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002082 dev_kfree_skb(tx_ring_desc->skb);
2083 tx_ring_desc->skb = NULL;
2084
2085 if (unlikely(mac_rsp->flags1 & (OB_MAC_IOCB_RSP_E |
2086 OB_MAC_IOCB_RSP_S |
2087 OB_MAC_IOCB_RSP_L |
2088 OB_MAC_IOCB_RSP_P | OB_MAC_IOCB_RSP_B))) {
2089 if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_E) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002090 netif_warn(qdev, tx_done, qdev->ndev,
2091 "Total descriptor length did not match transfer length.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002092 }
2093 if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_S) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002094 netif_warn(qdev, tx_done, qdev->ndev,
2095 "Frame too short to be valid, not sent.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002096 }
2097 if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_L) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002098 netif_warn(qdev, tx_done, qdev->ndev,
2099 "Frame too long, but sent anyway.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002100 }
2101 if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_B) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002102 netif_warn(qdev, tx_done, qdev->ndev,
2103 "PCI backplane error. Frame not sent.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002104 }
2105 }
2106 atomic_inc(&tx_ring->tx_count);
2107}
2108
2109/* Fire up a handler to reset the MPI processor. */
2110void ql_queue_fw_error(struct ql_adapter *qdev)
2111{
Ron Mercer6a473302009-07-02 06:06:12 +00002112 ql_link_off(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002113 queue_delayed_work(qdev->workqueue, &qdev->mpi_reset_work, 0);
2114}
2115
2116void ql_queue_asic_error(struct ql_adapter *qdev)
2117{
Ron Mercer6a473302009-07-02 06:06:12 +00002118 ql_link_off(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002119 ql_disable_interrupts(qdev);
Ron Mercer6497b602009-02-12 16:37:13 -08002120 /* Clear adapter up bit to signal the recovery
2121 * process that it shouldn't kill the reset worker
2122 * thread
2123 */
2124 clear_bit(QL_ADAPTER_UP, &qdev->flags);
Jitendra Kalsariada92b392011-06-30 10:02:05 +00002125 /* Set asic recovery bit to indicate reset process that we are
2126 * in fatal error recovery process rather than normal close
2127 */
2128 set_bit(QL_ASIC_RECOVERY, &qdev->flags);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002129 queue_delayed_work(qdev->workqueue, &qdev->asic_reset_work, 0);
2130}
2131
2132static void ql_process_chip_ae_intr(struct ql_adapter *qdev,
2133 struct ib_ae_iocb_rsp *ib_ae_rsp)
2134{
2135 switch (ib_ae_rsp->event) {
2136 case MGMT_ERR_EVENT:
Joe Perchesae9540f72010-02-09 11:49:52 +00002137 netif_err(qdev, rx_err, qdev->ndev,
2138 "Management Processor Fatal Error.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002139 ql_queue_fw_error(qdev);
2140 return;
2141
2142 case CAM_LOOKUP_ERR_EVENT:
Jitendra Kalsaria5069ee52011-06-30 10:02:06 +00002143 netdev_err(qdev->ndev, "Multiple CAM hits lookup occurred.\n");
2144 netdev_err(qdev->ndev, "This event shouldn't occur.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002145 ql_queue_asic_error(qdev);
2146 return;
2147
2148 case SOFT_ECC_ERROR_EVENT:
Jitendra Kalsaria5069ee52011-06-30 10:02:06 +00002149 netdev_err(qdev->ndev, "Soft ECC error detected.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002150 ql_queue_asic_error(qdev);
2151 break;
2152
2153 case PCI_ERR_ANON_BUF_RD:
Jitendra Kalsaria5069ee52011-06-30 10:02:06 +00002154 netdev_err(qdev->ndev, "PCI error occurred when reading "
2155 "anonymous buffers from rx_ring %d.\n",
2156 ib_ae_rsp->q_id);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002157 ql_queue_asic_error(qdev);
2158 break;
2159
2160 default:
Joe Perchesae9540f72010-02-09 11:49:52 +00002161 netif_err(qdev, drv, qdev->ndev, "Unexpected event %d.\n",
2162 ib_ae_rsp->event);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002163 ql_queue_asic_error(qdev);
2164 break;
2165 }
2166}
2167
2168static int ql_clean_outbound_rx_ring(struct rx_ring *rx_ring)
2169{
2170 struct ql_adapter *qdev = rx_ring->qdev;
Ron Mercerba7cd3b2009-01-09 11:31:49 +00002171 u32 prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002172 struct ob_mac_iocb_rsp *net_rsp = NULL;
2173 int count = 0;
2174
Ron Mercer1e213302009-03-09 10:59:21 +00002175 struct tx_ring *tx_ring;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002176 /* While there are entries in the completion queue. */
2177 while (prod != rx_ring->cnsmr_idx) {
2178
Joe Perchesae9540f72010-02-09 11:49:52 +00002179 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
2180 "cq_id = %d, prod = %d, cnsmr = %d.\n.",
2181 rx_ring->cq_id, prod, rx_ring->cnsmr_idx);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002182
2183 net_rsp = (struct ob_mac_iocb_rsp *)rx_ring->curr_entry;
2184 rmb();
2185 switch (net_rsp->opcode) {
2186
2187 case OPCODE_OB_MAC_TSO_IOCB:
2188 case OPCODE_OB_MAC_IOCB:
2189 ql_process_mac_tx_intr(qdev, net_rsp);
2190 break;
2191 default:
Joe Perchesae9540f72010-02-09 11:49:52 +00002192 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
2193 "Hit default case, not handled! dropping the packet, opcode = %x.\n",
2194 net_rsp->opcode);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002195 }
2196 count++;
2197 ql_update_cq(rx_ring);
Ron Mercerba7cd3b2009-01-09 11:31:49 +00002198 prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002199 }
Dan Carpenter4da79502010-08-19 08:52:44 +00002200 if (!net_rsp)
2201 return 0;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002202 ql_write_cq_idx(rx_ring);
Ron Mercer1e213302009-03-09 10:59:21 +00002203 tx_ring = &qdev->tx_ring[net_rsp->txq_idx];
Dan Carpenter4da79502010-08-19 08:52:44 +00002204 if (__netif_subqueue_stopped(qdev->ndev, tx_ring->wq_id)) {
Jitendra Kalsariad0de7302012-07-10 14:57:32 +00002205 if ((atomic_read(&tx_ring->tx_count) > (tx_ring->wq_len / 4)))
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002206 /*
2207 * The queue got stopped because the tx_ring was full.
2208 * Wake it up, because it's now at least 25% empty.
2209 */
Ron Mercer1e213302009-03-09 10:59:21 +00002210 netif_wake_subqueue(qdev->ndev, tx_ring->wq_id);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002211 }
2212
2213 return count;
2214}
2215
2216static int ql_clean_inbound_rx_ring(struct rx_ring *rx_ring, int budget)
2217{
2218 struct ql_adapter *qdev = rx_ring->qdev;
Ron Mercerba7cd3b2009-01-09 11:31:49 +00002219 u32 prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002220 struct ql_net_rsp_iocb *net_rsp;
2221 int count = 0;
2222
2223 /* While there are entries in the completion queue. */
2224 while (prod != rx_ring->cnsmr_idx) {
2225
Joe Perchesae9540f72010-02-09 11:49:52 +00002226 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
2227 "cq_id = %d, prod = %d, cnsmr = %d.\n.",
2228 rx_ring->cq_id, prod, rx_ring->cnsmr_idx);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002229
2230 net_rsp = rx_ring->curr_entry;
2231 rmb();
2232 switch (net_rsp->opcode) {
2233 case OPCODE_IB_MAC_IOCB:
2234 ql_process_mac_rx_intr(qdev, rx_ring,
2235 (struct ib_mac_iocb_rsp *)
2236 net_rsp);
2237 break;
2238
2239 case OPCODE_IB_AE_IOCB:
2240 ql_process_chip_ae_intr(qdev, (struct ib_ae_iocb_rsp *)
2241 net_rsp);
2242 break;
2243 default:
Joe Perchesae9540f72010-02-09 11:49:52 +00002244 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
2245 "Hit default case, not handled! dropping the packet, opcode = %x.\n",
2246 net_rsp->opcode);
2247 break;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002248 }
2249 count++;
2250 ql_update_cq(rx_ring);
Ron Mercerba7cd3b2009-01-09 11:31:49 +00002251 prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002252 if (count == budget)
2253 break;
2254 }
2255 ql_update_buffer_queues(qdev, rx_ring);
2256 ql_write_cq_idx(rx_ring);
2257 return count;
2258}
2259
2260static int ql_napi_poll_msix(struct napi_struct *napi, int budget)
2261{
2262 struct rx_ring *rx_ring = container_of(napi, struct rx_ring, napi);
2263 struct ql_adapter *qdev = rx_ring->qdev;
Ron Mercer39aa8162009-08-27 11:02:11 +00002264 struct rx_ring *trx_ring;
2265 int i, work_done = 0;
2266 struct intr_context *ctx = &qdev->intr_context[rx_ring->cq_id];
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002267
Joe Perchesae9540f72010-02-09 11:49:52 +00002268 netif_printk(qdev, rx_status, KERN_DEBUG, qdev->ndev,
2269 "Enter, NAPI POLL cq_id = %d.\n", rx_ring->cq_id);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002270
Ron Mercer39aa8162009-08-27 11:02:11 +00002271 /* Service the TX rings first. They start
2272 * right after the RSS rings. */
2273 for (i = qdev->rss_ring_count; i < qdev->rx_ring_count; i++) {
2274 trx_ring = &qdev->rx_ring[i];
2275 /* If this TX completion ring belongs to this vector and
2276 * it's not empty then service it.
2277 */
2278 if ((ctx->irq_mask & (1 << trx_ring->cq_id)) &&
2279 (ql_read_sh_reg(trx_ring->prod_idx_sh_reg) !=
2280 trx_ring->cnsmr_idx)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002281 netif_printk(qdev, intr, KERN_DEBUG, qdev->ndev,
2282 "%s: Servicing TX completion ring %d.\n",
2283 __func__, trx_ring->cq_id);
Ron Mercer39aa8162009-08-27 11:02:11 +00002284 ql_clean_outbound_rx_ring(trx_ring);
2285 }
2286 }
2287
2288 /*
2289 * Now service the RSS ring if it's active.
2290 */
2291 if (ql_read_sh_reg(rx_ring->prod_idx_sh_reg) !=
2292 rx_ring->cnsmr_idx) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002293 netif_printk(qdev, intr, KERN_DEBUG, qdev->ndev,
2294 "%s: Servicing RX completion ring %d.\n",
2295 __func__, rx_ring->cq_id);
Ron Mercer39aa8162009-08-27 11:02:11 +00002296 work_done = ql_clean_inbound_rx_ring(rx_ring, budget);
2297 }
2298
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002299 if (work_done < budget) {
Ron Mercer22bdd4f2009-03-09 10:59:20 +00002300 napi_complete(napi);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002301 ql_enable_completion_interrupt(qdev, rx_ring->irq);
2302 }
2303 return work_done;
2304}
2305
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002306static void qlge_vlan_mode(struct net_device *ndev, netdev_features_t features)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002307{
2308 struct ql_adapter *qdev = netdev_priv(ndev);
2309
Jiri Pirko18c49b92011-07-21 03:24:11 +00002310 if (features & NETIF_F_HW_VLAN_RX) {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002311 ql_write32(qdev, NIC_RCV_CFG, NIC_RCV_CFG_VLAN_MASK |
Jiri Pirko18c49b92011-07-21 03:24:11 +00002312 NIC_RCV_CFG_VLAN_MATCH_AND_NON);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002313 } else {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002314 ql_write32(qdev, NIC_RCV_CFG, NIC_RCV_CFG_VLAN_MASK);
2315 }
2316}
2317
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002318static netdev_features_t qlge_fix_features(struct net_device *ndev,
2319 netdev_features_t features)
Jiri Pirko18c49b92011-07-21 03:24:11 +00002320{
2321 /*
2322 * Since there is no support for separate rx/tx vlan accel
2323 * enable/disable make sure tx flag is always in same state as rx.
2324 */
2325 if (features & NETIF_F_HW_VLAN_RX)
2326 features |= NETIF_F_HW_VLAN_TX;
2327 else
2328 features &= ~NETIF_F_HW_VLAN_TX;
2329
2330 return features;
2331}
2332
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002333static int qlge_set_features(struct net_device *ndev,
2334 netdev_features_t features)
Jiri Pirko18c49b92011-07-21 03:24:11 +00002335{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002336 netdev_features_t changed = ndev->features ^ features;
Jiri Pirko18c49b92011-07-21 03:24:11 +00002337
2338 if (changed & NETIF_F_HW_VLAN_RX)
2339 qlge_vlan_mode(ndev, features);
2340
2341 return 0;
2342}
2343
Jiri Pirko8e586132011-12-08 19:52:37 -05002344static int __qlge_vlan_rx_add_vid(struct ql_adapter *qdev, u16 vid)
Jiri Pirko18c49b92011-07-21 03:24:11 +00002345{
2346 u32 enable_bit = MAC_ADDR_E;
Jiri Pirko8e586132011-12-08 19:52:37 -05002347 int err;
Jiri Pirko18c49b92011-07-21 03:24:11 +00002348
Jiri Pirko8e586132011-12-08 19:52:37 -05002349 err = ql_set_mac_addr_reg(qdev, (u8 *) &enable_bit,
2350 MAC_ADDR_TYPE_VLAN, vid);
2351 if (err)
Jiri Pirko18c49b92011-07-21 03:24:11 +00002352 netif_err(qdev, ifup, qdev->ndev,
2353 "Failed to init vlan address.\n");
Jiri Pirko8e586132011-12-08 19:52:37 -05002354 return err;
Jiri Pirko18c49b92011-07-21 03:24:11 +00002355}
2356
Jiri Pirko8e586132011-12-08 19:52:37 -05002357static int qlge_vlan_rx_add_vid(struct net_device *ndev, u16 vid)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002358{
2359 struct ql_adapter *qdev = netdev_priv(ndev);
Ron Mercercc288f52009-02-23 10:42:14 +00002360 int status;
Jiri Pirko8e586132011-12-08 19:52:37 -05002361 int err;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002362
Ron Mercercc288f52009-02-23 10:42:14 +00002363 status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
2364 if (status)
Jiri Pirko8e586132011-12-08 19:52:37 -05002365 return status;
Jiri Pirko18c49b92011-07-21 03:24:11 +00002366
Jiri Pirko8e586132011-12-08 19:52:37 -05002367 err = __qlge_vlan_rx_add_vid(qdev, vid);
Jiri Pirko18c49b92011-07-21 03:24:11 +00002368 set_bit(vid, qdev->active_vlans);
2369
Ron Mercercc288f52009-02-23 10:42:14 +00002370 ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
Jiri Pirko8e586132011-12-08 19:52:37 -05002371
2372 return err;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002373}
2374
Jiri Pirko8e586132011-12-08 19:52:37 -05002375static int __qlge_vlan_rx_kill_vid(struct ql_adapter *qdev, u16 vid)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002376{
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002377 u32 enable_bit = 0;
Jiri Pirko8e586132011-12-08 19:52:37 -05002378 int err;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002379
Jiri Pirko8e586132011-12-08 19:52:37 -05002380 err = ql_set_mac_addr_reg(qdev, (u8 *) &enable_bit,
2381 MAC_ADDR_TYPE_VLAN, vid);
2382 if (err)
Joe Perchesae9540f72010-02-09 11:49:52 +00002383 netif_err(qdev, ifup, qdev->ndev,
2384 "Failed to clear vlan address.\n");
Jiri Pirko8e586132011-12-08 19:52:37 -05002385 return err;
Jiri Pirko18c49b92011-07-21 03:24:11 +00002386}
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002387
Jiri Pirko8e586132011-12-08 19:52:37 -05002388static int qlge_vlan_rx_kill_vid(struct net_device *ndev, u16 vid)
Jiri Pirko18c49b92011-07-21 03:24:11 +00002389{
2390 struct ql_adapter *qdev = netdev_priv(ndev);
2391 int status;
Jiri Pirko8e586132011-12-08 19:52:37 -05002392 int err;
Jiri Pirko18c49b92011-07-21 03:24:11 +00002393
2394 status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
2395 if (status)
Jiri Pirko8e586132011-12-08 19:52:37 -05002396 return status;
Jiri Pirko18c49b92011-07-21 03:24:11 +00002397
Jiri Pirko8e586132011-12-08 19:52:37 -05002398 err = __qlge_vlan_rx_kill_vid(qdev, vid);
Jiri Pirko18c49b92011-07-21 03:24:11 +00002399 clear_bit(vid, qdev->active_vlans);
2400
2401 ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
Jiri Pirko8e586132011-12-08 19:52:37 -05002402
2403 return err;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002404}
2405
Ron Mercerc1b60092010-10-27 04:58:12 +00002406static void qlge_restore_vlan(struct ql_adapter *qdev)
2407{
Jiri Pirko18c49b92011-07-21 03:24:11 +00002408 int status;
2409 u16 vid;
Ron Mercerc1b60092010-10-27 04:58:12 +00002410
Jiri Pirko18c49b92011-07-21 03:24:11 +00002411 status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
2412 if (status)
2413 return;
2414
2415 for_each_set_bit(vid, qdev->active_vlans, VLAN_N_VID)
2416 __qlge_vlan_rx_add_vid(qdev, vid);
2417
2418 ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
Ron Mercerc1b60092010-10-27 04:58:12 +00002419}
2420
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002421/* MSI-X Multiple Vector Interrupt Handler for inbound completions. */
2422static irqreturn_t qlge_msix_rx_isr(int irq, void *dev_id)
2423{
2424 struct rx_ring *rx_ring = dev_id;
Ben Hutchings288379f2009-01-19 16:43:59 -08002425 napi_schedule(&rx_ring->napi);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002426 return IRQ_HANDLED;
2427}
2428
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002429/* This handles a fatal error, MPI activity, and the default
2430 * rx_ring in an MSI-X multiple vector environment.
2431 * In MSI/Legacy environment it also process the rest of
2432 * the rx_rings.
2433 */
2434static irqreturn_t qlge_isr(int irq, void *dev_id)
2435{
2436 struct rx_ring *rx_ring = dev_id;
2437 struct ql_adapter *qdev = rx_ring->qdev;
2438 struct intr_context *intr_context = &qdev->intr_context[0];
2439 u32 var;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002440 int work_done = 0;
2441
Ron Mercerbb0d2152008-10-20 10:30:26 -07002442 spin_lock(&qdev->hw_lock);
2443 if (atomic_read(&qdev->intr_context[0].irq_cnt)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002444 netif_printk(qdev, intr, KERN_DEBUG, qdev->ndev,
2445 "Shared Interrupt, Not ours!\n");
Ron Mercerbb0d2152008-10-20 10:30:26 -07002446 spin_unlock(&qdev->hw_lock);
2447 return IRQ_NONE;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002448 }
Ron Mercerbb0d2152008-10-20 10:30:26 -07002449 spin_unlock(&qdev->hw_lock);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002450
Ron Mercerbb0d2152008-10-20 10:30:26 -07002451 var = ql_disable_completion_interrupt(qdev, intr_context->intr);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002452
2453 /*
2454 * Check for fatal error.
2455 */
2456 if (var & STS_FE) {
2457 ql_queue_asic_error(qdev);
Jitendra Kalsaria5069ee52011-06-30 10:02:06 +00002458 netdev_err(qdev->ndev, "Got fatal error, STS = %x.\n", var);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002459 var = ql_read32(qdev, ERR_STS);
Jitendra Kalsaria5069ee52011-06-30 10:02:06 +00002460 netdev_err(qdev->ndev, "Resetting chip. "
2461 "Error Status Register = 0x%x\n", var);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002462 return IRQ_HANDLED;
2463 }
2464
2465 /*
2466 * Check MPI processor activity.
2467 */
Ron Mercer5ee22a52009-10-05 11:46:48 +00002468 if ((var & STS_PI) &&
2469 (ql_read32(qdev, INTR_MASK) & INTR_MASK_PI)) {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002470 /*
2471 * We've got an async event or mailbox completion.
2472 * Handle it and clear the source of the interrupt.
2473 */
Joe Perchesae9540f72010-02-09 11:49:52 +00002474 netif_err(qdev, intr, qdev->ndev,
2475 "Got MPI processor interrupt.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002476 ql_disable_completion_interrupt(qdev, intr_context->intr);
Ron Mercer5ee22a52009-10-05 11:46:48 +00002477 ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16));
2478 queue_delayed_work_on(smp_processor_id(),
2479 qdev->workqueue, &qdev->mpi_work, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002480 work_done++;
2481 }
2482
2483 /*
Ron Mercer39aa8162009-08-27 11:02:11 +00002484 * Get the bit-mask that shows the active queues for this
2485 * pass. Compare it to the queues that this irq services
2486 * and call napi if there's a match.
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002487 */
Ron Mercer39aa8162009-08-27 11:02:11 +00002488 var = ql_read32(qdev, ISR1);
2489 if (var & intr_context->irq_mask) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002490 netif_info(qdev, intr, qdev->ndev,
2491 "Waking handler for rx_ring[0].\n");
Ron Mercer39aa8162009-08-27 11:02:11 +00002492 ql_disable_completion_interrupt(qdev, intr_context->intr);
Ron Mercer32a5b2a2009-11-03 13:49:29 +00002493 napi_schedule(&rx_ring->napi);
2494 work_done++;
2495 }
Ron Mercerbb0d2152008-10-20 10:30:26 -07002496 ql_enable_completion_interrupt(qdev, intr_context->intr);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002497 return work_done ? IRQ_HANDLED : IRQ_NONE;
2498}
2499
2500static int ql_tso(struct sk_buff *skb, struct ob_mac_tso_iocb_req *mac_iocb_ptr)
2501{
2502
2503 if (skb_is_gso(skb)) {
2504 int err;
2505 if (skb_header_cloned(skb)) {
2506 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2507 if (err)
2508 return err;
2509 }
2510
2511 mac_iocb_ptr->opcode = OPCODE_OB_MAC_TSO_IOCB;
2512 mac_iocb_ptr->flags3 |= OB_MAC_TSO_IOCB_IC;
2513 mac_iocb_ptr->frame_len = cpu_to_le32((u32) skb->len);
2514 mac_iocb_ptr->total_hdrs_len =
2515 cpu_to_le16(skb_transport_offset(skb) + tcp_hdrlen(skb));
2516 mac_iocb_ptr->net_trans_offset =
2517 cpu_to_le16(skb_network_offset(skb) |
2518 skb_transport_offset(skb)
2519 << OB_MAC_TRANSPORT_HDR_SHIFT);
2520 mac_iocb_ptr->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
2521 mac_iocb_ptr->flags2 |= OB_MAC_TSO_IOCB_LSO;
2522 if (likely(skb->protocol == htons(ETH_P_IP))) {
2523 struct iphdr *iph = ip_hdr(skb);
2524 iph->check = 0;
2525 mac_iocb_ptr->flags1 |= OB_MAC_TSO_IOCB_IP4;
2526 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
2527 iph->daddr, 0,
2528 IPPROTO_TCP,
2529 0);
2530 } else if (skb->protocol == htons(ETH_P_IPV6)) {
2531 mac_iocb_ptr->flags1 |= OB_MAC_TSO_IOCB_IP6;
2532 tcp_hdr(skb)->check =
2533 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2534 &ipv6_hdr(skb)->daddr,
2535 0, IPPROTO_TCP, 0);
2536 }
2537 return 1;
2538 }
2539 return 0;
2540}
2541
2542static void ql_hw_csum_setup(struct sk_buff *skb,
2543 struct ob_mac_tso_iocb_req *mac_iocb_ptr)
2544{
2545 int len;
2546 struct iphdr *iph = ip_hdr(skb);
Ron Mercerfd2df4f2009-01-05 18:18:45 -08002547 __sum16 *check;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002548 mac_iocb_ptr->opcode = OPCODE_OB_MAC_TSO_IOCB;
2549 mac_iocb_ptr->frame_len = cpu_to_le32((u32) skb->len);
2550 mac_iocb_ptr->net_trans_offset =
2551 cpu_to_le16(skb_network_offset(skb) |
2552 skb_transport_offset(skb) << OB_MAC_TRANSPORT_HDR_SHIFT);
2553
2554 mac_iocb_ptr->flags1 |= OB_MAC_TSO_IOCB_IP4;
2555 len = (ntohs(iph->tot_len) - (iph->ihl << 2));
2556 if (likely(iph->protocol == IPPROTO_TCP)) {
2557 check = &(tcp_hdr(skb)->check);
2558 mac_iocb_ptr->flags2 |= OB_MAC_TSO_IOCB_TC;
2559 mac_iocb_ptr->total_hdrs_len =
2560 cpu_to_le16(skb_transport_offset(skb) +
2561 (tcp_hdr(skb)->doff << 2));
2562 } else {
2563 check = &(udp_hdr(skb)->check);
2564 mac_iocb_ptr->flags2 |= OB_MAC_TSO_IOCB_UC;
2565 mac_iocb_ptr->total_hdrs_len =
2566 cpu_to_le16(skb_transport_offset(skb) +
2567 sizeof(struct udphdr));
2568 }
2569 *check = ~csum_tcpudp_magic(iph->saddr,
2570 iph->daddr, len, iph->protocol, 0);
2571}
2572
Stephen Hemminger613573252009-08-31 19:50:58 +00002573static netdev_tx_t qlge_send(struct sk_buff *skb, struct net_device *ndev)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002574{
2575 struct tx_ring_desc *tx_ring_desc;
2576 struct ob_mac_iocb_req *mac_iocb_ptr;
2577 struct ql_adapter *qdev = netdev_priv(ndev);
2578 int tso;
2579 struct tx_ring *tx_ring;
Ron Mercer1e213302009-03-09 10:59:21 +00002580 u32 tx_ring_idx = (u32) skb->queue_mapping;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002581
2582 tx_ring = &qdev->tx_ring[tx_ring_idx];
2583
Ron Mercer74c50b42009-03-09 10:59:27 +00002584 if (skb_padto(skb, ETH_ZLEN))
2585 return NETDEV_TX_OK;
2586
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002587 if (unlikely(atomic_read(&tx_ring->tx_count) < 2)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002588 netif_info(qdev, tx_queued, qdev->ndev,
Jitendra Kalsaria41812db2012-07-10 14:57:31 +00002589 "%s: BUG! shutting down tx queue %d due to lack of resources.\n",
Joe Perchesae9540f72010-02-09 11:49:52 +00002590 __func__, tx_ring_idx);
Ron Mercer1e213302009-03-09 10:59:21 +00002591 netif_stop_subqueue(ndev, tx_ring->wq_id);
Ron Mercer885ee392009-11-03 13:49:31 +00002592 tx_ring->tx_errors++;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002593 return NETDEV_TX_BUSY;
2594 }
2595 tx_ring_desc = &tx_ring->q[tx_ring->prod_idx];
2596 mac_iocb_ptr = tx_ring_desc->queue_entry;
Ron Mercere3324712009-07-02 06:06:13 +00002597 memset((void *)mac_iocb_ptr, 0, sizeof(*mac_iocb_ptr));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002598
2599 mac_iocb_ptr->opcode = OPCODE_OB_MAC_IOCB;
2600 mac_iocb_ptr->tid = tx_ring_desc->index;
2601 /* We use the upper 32-bits to store the tx queue for this IO.
2602 * When we get the completion we can use it to establish the context.
2603 */
2604 mac_iocb_ptr->txq_idx = tx_ring_idx;
2605 tx_ring_desc->skb = skb;
2606
2607 mac_iocb_ptr->frame_len = cpu_to_le16((u16) skb->len);
2608
Jesse Grosseab6d182010-10-20 13:56:03 +00002609 if (vlan_tx_tag_present(skb)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002610 netif_printk(qdev, tx_queued, KERN_DEBUG, qdev->ndev,
2611 "Adding a vlan tag %d.\n", vlan_tx_tag_get(skb));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002612 mac_iocb_ptr->flags3 |= OB_MAC_IOCB_V;
2613 mac_iocb_ptr->vlan_tci = cpu_to_le16(vlan_tx_tag_get(skb));
2614 }
2615 tso = ql_tso(skb, (struct ob_mac_tso_iocb_req *)mac_iocb_ptr);
2616 if (tso < 0) {
2617 dev_kfree_skb_any(skb);
2618 return NETDEV_TX_OK;
2619 } else if (unlikely(!tso) && (skb->ip_summed == CHECKSUM_PARTIAL)) {
2620 ql_hw_csum_setup(skb,
2621 (struct ob_mac_tso_iocb_req *)mac_iocb_ptr);
2622 }
Ron Mercer0d979f72009-02-12 16:38:03 -08002623 if (ql_map_send(qdev, mac_iocb_ptr, skb, tx_ring_desc) !=
2624 NETDEV_TX_OK) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002625 netif_err(qdev, tx_queued, qdev->ndev,
2626 "Could not map the segments.\n");
Ron Mercer885ee392009-11-03 13:49:31 +00002627 tx_ring->tx_errors++;
Ron Mercer0d979f72009-02-12 16:38:03 -08002628 return NETDEV_TX_BUSY;
2629 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002630 QL_DUMP_OB_MAC_IOCB(mac_iocb_ptr);
2631 tx_ring->prod_idx++;
2632 if (tx_ring->prod_idx == tx_ring->wq_len)
2633 tx_ring->prod_idx = 0;
2634 wmb();
2635
2636 ql_write_db_reg(tx_ring->prod_idx, tx_ring->prod_idx_db_reg);
Joe Perchesae9540f72010-02-09 11:49:52 +00002637 netif_printk(qdev, tx_queued, KERN_DEBUG, qdev->ndev,
2638 "tx queued, slot %d, len %d\n",
2639 tx_ring->prod_idx, skb->len);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002640
2641 atomic_dec(&tx_ring->tx_count);
Jitendra Kalsaria41812db2012-07-10 14:57:31 +00002642
2643 if (unlikely(atomic_read(&tx_ring->tx_count) < 2)) {
2644 netif_stop_subqueue(ndev, tx_ring->wq_id);
2645 if ((atomic_read(&tx_ring->tx_count) > (tx_ring->wq_len / 4)))
2646 /*
2647 * The queue got stopped because the tx_ring was full.
2648 * Wake it up, because it's now at least 25% empty.
2649 */
2650 netif_wake_subqueue(qdev->ndev, tx_ring->wq_id);
2651 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002652 return NETDEV_TX_OK;
2653}
2654
Ron Mercer9dfbbaa2009-10-30 12:13:33 +00002655
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002656static void ql_free_shadow_space(struct ql_adapter *qdev)
2657{
2658 if (qdev->rx_ring_shadow_reg_area) {
2659 pci_free_consistent(qdev->pdev,
2660 PAGE_SIZE,
2661 qdev->rx_ring_shadow_reg_area,
2662 qdev->rx_ring_shadow_reg_dma);
2663 qdev->rx_ring_shadow_reg_area = NULL;
2664 }
2665 if (qdev->tx_ring_shadow_reg_area) {
2666 pci_free_consistent(qdev->pdev,
2667 PAGE_SIZE,
2668 qdev->tx_ring_shadow_reg_area,
2669 qdev->tx_ring_shadow_reg_dma);
2670 qdev->tx_ring_shadow_reg_area = NULL;
2671 }
2672}
2673
2674static int ql_alloc_shadow_space(struct ql_adapter *qdev)
2675{
2676 qdev->rx_ring_shadow_reg_area =
2677 pci_alloc_consistent(qdev->pdev,
2678 PAGE_SIZE, &qdev->rx_ring_shadow_reg_dma);
2679 if (qdev->rx_ring_shadow_reg_area == NULL) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002680 netif_err(qdev, ifup, qdev->ndev,
2681 "Allocation of RX shadow space failed.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002682 return -ENOMEM;
2683 }
Ron Mercerb25215d2009-03-09 10:59:24 +00002684 memset(qdev->rx_ring_shadow_reg_area, 0, PAGE_SIZE);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002685 qdev->tx_ring_shadow_reg_area =
2686 pci_alloc_consistent(qdev->pdev, PAGE_SIZE,
2687 &qdev->tx_ring_shadow_reg_dma);
2688 if (qdev->tx_ring_shadow_reg_area == NULL) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002689 netif_err(qdev, ifup, qdev->ndev,
2690 "Allocation of TX shadow space failed.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002691 goto err_wqp_sh_area;
2692 }
Ron Mercerb25215d2009-03-09 10:59:24 +00002693 memset(qdev->tx_ring_shadow_reg_area, 0, PAGE_SIZE);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002694 return 0;
2695
2696err_wqp_sh_area:
2697 pci_free_consistent(qdev->pdev,
2698 PAGE_SIZE,
2699 qdev->rx_ring_shadow_reg_area,
2700 qdev->rx_ring_shadow_reg_dma);
2701 return -ENOMEM;
2702}
2703
2704static void ql_init_tx_ring(struct ql_adapter *qdev, struct tx_ring *tx_ring)
2705{
2706 struct tx_ring_desc *tx_ring_desc;
2707 int i;
2708 struct ob_mac_iocb_req *mac_iocb_ptr;
2709
2710 mac_iocb_ptr = tx_ring->wq_base;
2711 tx_ring_desc = tx_ring->q;
2712 for (i = 0; i < tx_ring->wq_len; i++) {
2713 tx_ring_desc->index = i;
2714 tx_ring_desc->skb = NULL;
2715 tx_ring_desc->queue_entry = mac_iocb_ptr;
2716 mac_iocb_ptr++;
2717 tx_ring_desc++;
2718 }
2719 atomic_set(&tx_ring->tx_count, tx_ring->wq_len);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002720}
2721
2722static void ql_free_tx_resources(struct ql_adapter *qdev,
2723 struct tx_ring *tx_ring)
2724{
2725 if (tx_ring->wq_base) {
2726 pci_free_consistent(qdev->pdev, tx_ring->wq_size,
2727 tx_ring->wq_base, tx_ring->wq_base_dma);
2728 tx_ring->wq_base = NULL;
2729 }
2730 kfree(tx_ring->q);
2731 tx_ring->q = NULL;
2732}
2733
2734static int ql_alloc_tx_resources(struct ql_adapter *qdev,
2735 struct tx_ring *tx_ring)
2736{
2737 tx_ring->wq_base =
2738 pci_alloc_consistent(qdev->pdev, tx_ring->wq_size,
2739 &tx_ring->wq_base_dma);
2740
Joe Perches8e95a202009-12-03 07:58:21 +00002741 if ((tx_ring->wq_base == NULL) ||
Jitendra Kalsariaf5c44412012-07-10 14:57:36 +00002742 tx_ring->wq_base_dma & WQ_ADDR_ALIGN)
2743 goto pci_alloc_err;
2744
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002745 tx_ring->q =
2746 kmalloc(tx_ring->wq_len * sizeof(struct tx_ring_desc), GFP_KERNEL);
2747 if (tx_ring->q == NULL)
2748 goto err;
2749
2750 return 0;
2751err:
2752 pci_free_consistent(qdev->pdev, tx_ring->wq_size,
2753 tx_ring->wq_base, tx_ring->wq_base_dma);
Jitendra Kalsariaf5c44412012-07-10 14:57:36 +00002754 tx_ring->wq_base = NULL;
2755pci_alloc_err:
2756 netif_err(qdev, ifup, qdev->ndev, "tx_ring alloc failed.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002757 return -ENOMEM;
2758}
2759
Stephen Hemminger8668ae92008-11-21 17:29:50 -08002760static void ql_free_lbq_buffers(struct ql_adapter *qdev, struct rx_ring *rx_ring)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002761{
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002762 struct bq_desc *lbq_desc;
2763
Ron Mercer7c734352009-10-19 03:32:19 +00002764 uint32_t curr_idx, clean_idx;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002765
Ron Mercer7c734352009-10-19 03:32:19 +00002766 curr_idx = rx_ring->lbq_curr_idx;
2767 clean_idx = rx_ring->lbq_clean_idx;
2768 while (curr_idx != clean_idx) {
2769 lbq_desc = &rx_ring->lbq[curr_idx];
2770
2771 if (lbq_desc->p.pg_chunk.last_flag) {
2772 pci_unmap_page(qdev->pdev,
2773 lbq_desc->p.pg_chunk.map,
2774 ql_lbq_block_size(qdev),
2775 PCI_DMA_FROMDEVICE);
2776 lbq_desc->p.pg_chunk.last_flag = 0;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002777 }
Ron Mercer7c734352009-10-19 03:32:19 +00002778
2779 put_page(lbq_desc->p.pg_chunk.page);
2780 lbq_desc->p.pg_chunk.page = NULL;
2781
2782 if (++curr_idx == rx_ring->lbq_len)
2783 curr_idx = 0;
2784
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002785 }
2786}
2787
Stephen Hemminger8668ae92008-11-21 17:29:50 -08002788static void ql_free_sbq_buffers(struct ql_adapter *qdev, struct rx_ring *rx_ring)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002789{
2790 int i;
2791 struct bq_desc *sbq_desc;
2792
2793 for (i = 0; i < rx_ring->sbq_len; i++) {
2794 sbq_desc = &rx_ring->sbq[i];
2795 if (sbq_desc == NULL) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002796 netif_err(qdev, ifup, qdev->ndev,
2797 "sbq_desc %d is NULL.\n", i);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002798 return;
2799 }
2800 if (sbq_desc->p.skb) {
2801 pci_unmap_single(qdev->pdev,
FUJITA Tomonori64b9b412010-04-12 14:32:14 +00002802 dma_unmap_addr(sbq_desc, mapaddr),
2803 dma_unmap_len(sbq_desc, maplen),
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002804 PCI_DMA_FROMDEVICE);
2805 dev_kfree_skb(sbq_desc->p.skb);
2806 sbq_desc->p.skb = NULL;
2807 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002808 }
2809}
2810
Ron Mercer4545a3f2009-02-23 10:42:17 +00002811/* Free all large and small rx buffers associated
2812 * with the completion queues for this device.
2813 */
2814static void ql_free_rx_buffers(struct ql_adapter *qdev)
2815{
2816 int i;
2817 struct rx_ring *rx_ring;
2818
2819 for (i = 0; i < qdev->rx_ring_count; i++) {
2820 rx_ring = &qdev->rx_ring[i];
2821 if (rx_ring->lbq)
2822 ql_free_lbq_buffers(qdev, rx_ring);
2823 if (rx_ring->sbq)
2824 ql_free_sbq_buffers(qdev, rx_ring);
2825 }
2826}
2827
2828static void ql_alloc_rx_buffers(struct ql_adapter *qdev)
2829{
2830 struct rx_ring *rx_ring;
2831 int i;
2832
2833 for (i = 0; i < qdev->rx_ring_count; i++) {
2834 rx_ring = &qdev->rx_ring[i];
2835 if (rx_ring->type != TX_Q)
2836 ql_update_buffer_queues(qdev, rx_ring);
2837 }
2838}
2839
2840static void ql_init_lbq_ring(struct ql_adapter *qdev,
2841 struct rx_ring *rx_ring)
2842{
2843 int i;
2844 struct bq_desc *lbq_desc;
2845 __le64 *bq = rx_ring->lbq_base;
2846
2847 memset(rx_ring->lbq, 0, rx_ring->lbq_len * sizeof(struct bq_desc));
2848 for (i = 0; i < rx_ring->lbq_len; i++) {
2849 lbq_desc = &rx_ring->lbq[i];
2850 memset(lbq_desc, 0, sizeof(*lbq_desc));
2851 lbq_desc->index = i;
2852 lbq_desc->addr = bq;
2853 bq++;
2854 }
2855}
2856
2857static void ql_init_sbq_ring(struct ql_adapter *qdev,
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002858 struct rx_ring *rx_ring)
2859{
2860 int i;
2861 struct bq_desc *sbq_desc;
Ron Mercer2c9a0d42009-01-05 18:19:20 -08002862 __le64 *bq = rx_ring->sbq_base;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002863
Ron Mercer4545a3f2009-02-23 10:42:17 +00002864 memset(rx_ring->sbq, 0, rx_ring->sbq_len * sizeof(struct bq_desc));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002865 for (i = 0; i < rx_ring->sbq_len; i++) {
2866 sbq_desc = &rx_ring->sbq[i];
Ron Mercer4545a3f2009-02-23 10:42:17 +00002867 memset(sbq_desc, 0, sizeof(*sbq_desc));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002868 sbq_desc->index = i;
Ron Mercer2c9a0d42009-01-05 18:19:20 -08002869 sbq_desc->addr = bq;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002870 bq++;
2871 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002872}
2873
2874static void ql_free_rx_resources(struct ql_adapter *qdev,
2875 struct rx_ring *rx_ring)
2876{
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002877 /* Free the small buffer queue. */
2878 if (rx_ring->sbq_base) {
2879 pci_free_consistent(qdev->pdev,
2880 rx_ring->sbq_size,
2881 rx_ring->sbq_base, rx_ring->sbq_base_dma);
2882 rx_ring->sbq_base = NULL;
2883 }
2884
2885 /* Free the small buffer queue control blocks. */
2886 kfree(rx_ring->sbq);
2887 rx_ring->sbq = NULL;
2888
2889 /* Free the large buffer queue. */
2890 if (rx_ring->lbq_base) {
2891 pci_free_consistent(qdev->pdev,
2892 rx_ring->lbq_size,
2893 rx_ring->lbq_base, rx_ring->lbq_base_dma);
2894 rx_ring->lbq_base = NULL;
2895 }
2896
2897 /* Free the large buffer queue control blocks. */
2898 kfree(rx_ring->lbq);
2899 rx_ring->lbq = NULL;
2900
2901 /* Free the rx queue. */
2902 if (rx_ring->cq_base) {
2903 pci_free_consistent(qdev->pdev,
2904 rx_ring->cq_size,
2905 rx_ring->cq_base, rx_ring->cq_base_dma);
2906 rx_ring->cq_base = NULL;
2907 }
2908}
2909
2910/* Allocate queues and buffers for this completions queue based
2911 * on the values in the parameter structure. */
2912static int ql_alloc_rx_resources(struct ql_adapter *qdev,
2913 struct rx_ring *rx_ring)
2914{
2915
2916 /*
2917 * Allocate the completion queue for this rx_ring.
2918 */
2919 rx_ring->cq_base =
2920 pci_alloc_consistent(qdev->pdev, rx_ring->cq_size,
2921 &rx_ring->cq_base_dma);
2922
2923 if (rx_ring->cq_base == NULL) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002924 netif_err(qdev, ifup, qdev->ndev, "rx_ring alloc failed.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002925 return -ENOMEM;
2926 }
2927
2928 if (rx_ring->sbq_len) {
2929 /*
2930 * Allocate small buffer queue.
2931 */
2932 rx_ring->sbq_base =
2933 pci_alloc_consistent(qdev->pdev, rx_ring->sbq_size,
2934 &rx_ring->sbq_base_dma);
2935
2936 if (rx_ring->sbq_base == NULL) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002937 netif_err(qdev, ifup, qdev->ndev,
2938 "Small buffer queue allocation failed.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002939 goto err_mem;
2940 }
2941
2942 /*
2943 * Allocate small buffer queue control blocks.
2944 */
Joe Perches14f8dc42013-02-07 11:46:27 +00002945 rx_ring->sbq = kmalloc_array(rx_ring->sbq_len,
2946 sizeof(struct bq_desc),
2947 GFP_KERNEL);
2948 if (rx_ring->sbq == NULL)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002949 goto err_mem;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002950
Ron Mercer4545a3f2009-02-23 10:42:17 +00002951 ql_init_sbq_ring(qdev, rx_ring);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002952 }
2953
2954 if (rx_ring->lbq_len) {
2955 /*
2956 * Allocate large buffer queue.
2957 */
2958 rx_ring->lbq_base =
2959 pci_alloc_consistent(qdev->pdev, rx_ring->lbq_size,
2960 &rx_ring->lbq_base_dma);
2961
2962 if (rx_ring->lbq_base == NULL) {
Joe Perchesae9540f72010-02-09 11:49:52 +00002963 netif_err(qdev, ifup, qdev->ndev,
2964 "Large buffer queue allocation failed.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002965 goto err_mem;
2966 }
2967 /*
2968 * Allocate large buffer queue control blocks.
2969 */
Joe Perches14f8dc42013-02-07 11:46:27 +00002970 rx_ring->lbq = kmalloc_array(rx_ring->lbq_len,
2971 sizeof(struct bq_desc),
2972 GFP_KERNEL);
2973 if (rx_ring->lbq == NULL)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002974 goto err_mem;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002975
Ron Mercer4545a3f2009-02-23 10:42:17 +00002976 ql_init_lbq_ring(qdev, rx_ring);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002977 }
2978
2979 return 0;
2980
2981err_mem:
2982 ql_free_rx_resources(qdev, rx_ring);
2983 return -ENOMEM;
2984}
2985
2986static void ql_tx_ring_clean(struct ql_adapter *qdev)
2987{
2988 struct tx_ring *tx_ring;
2989 struct tx_ring_desc *tx_ring_desc;
2990 int i, j;
2991
2992 /*
2993 * Loop through all queues and free
2994 * any resources.
2995 */
2996 for (j = 0; j < qdev->tx_ring_count; j++) {
2997 tx_ring = &qdev->tx_ring[j];
2998 for (i = 0; i < tx_ring->wq_len; i++) {
2999 tx_ring_desc = &tx_ring->q[i];
3000 if (tx_ring_desc && tx_ring_desc->skb) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003001 netif_err(qdev, ifdown, qdev->ndev,
3002 "Freeing lost SKB %p, from queue %d, index %d.\n",
3003 tx_ring_desc->skb, j,
3004 tx_ring_desc->index);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003005 ql_unmap_send(qdev, tx_ring_desc,
3006 tx_ring_desc->map_cnt);
3007 dev_kfree_skb(tx_ring_desc->skb);
3008 tx_ring_desc->skb = NULL;
3009 }
3010 }
3011 }
3012}
3013
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003014static void ql_free_mem_resources(struct ql_adapter *qdev)
3015{
3016 int i;
3017
3018 for (i = 0; i < qdev->tx_ring_count; i++)
3019 ql_free_tx_resources(qdev, &qdev->tx_ring[i]);
3020 for (i = 0; i < qdev->rx_ring_count; i++)
3021 ql_free_rx_resources(qdev, &qdev->rx_ring[i]);
3022 ql_free_shadow_space(qdev);
3023}
3024
3025static int ql_alloc_mem_resources(struct ql_adapter *qdev)
3026{
3027 int i;
3028
3029 /* Allocate space for our shadow registers and such. */
3030 if (ql_alloc_shadow_space(qdev))
3031 return -ENOMEM;
3032
3033 for (i = 0; i < qdev->rx_ring_count; i++) {
3034 if (ql_alloc_rx_resources(qdev, &qdev->rx_ring[i]) != 0) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003035 netif_err(qdev, ifup, qdev->ndev,
3036 "RX resource allocation failed.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003037 goto err_mem;
3038 }
3039 }
3040 /* Allocate tx queue resources */
3041 for (i = 0; i < qdev->tx_ring_count; i++) {
3042 if (ql_alloc_tx_resources(qdev, &qdev->tx_ring[i]) != 0) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003043 netif_err(qdev, ifup, qdev->ndev,
3044 "TX resource allocation failed.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003045 goto err_mem;
3046 }
3047 }
3048 return 0;
3049
3050err_mem:
3051 ql_free_mem_resources(qdev);
3052 return -ENOMEM;
3053}
3054
3055/* Set up the rx ring control block and pass it to the chip.
3056 * The control block is defined as
3057 * "Completion Queue Initialization Control Block", or cqicb.
3058 */
3059static int ql_start_rx_ring(struct ql_adapter *qdev, struct rx_ring *rx_ring)
3060{
3061 struct cqicb *cqicb = &rx_ring->cqicb;
3062 void *shadow_reg = qdev->rx_ring_shadow_reg_area +
Ron Mercerb8facca2009-06-10 15:49:34 +00003063 (rx_ring->cq_id * RX_RING_SHADOW_SPACE);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003064 u64 shadow_reg_dma = qdev->rx_ring_shadow_reg_dma +
Ron Mercerb8facca2009-06-10 15:49:34 +00003065 (rx_ring->cq_id * RX_RING_SHADOW_SPACE);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003066 void __iomem *doorbell_area =
3067 qdev->doorbell_area + (DB_PAGE_SIZE * (128 + rx_ring->cq_id));
3068 int err = 0;
3069 u16 bq_len;
Ron Mercerd4a4aba2009-03-09 10:59:28 +00003070 u64 tmp;
Ron Mercerb8facca2009-06-10 15:49:34 +00003071 __le64 *base_indirect_ptr;
3072 int page_entries;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003073
3074 /* Set up the shadow registers for this ring. */
3075 rx_ring->prod_idx_sh_reg = shadow_reg;
3076 rx_ring->prod_idx_sh_reg_dma = shadow_reg_dma;
Ron Mercer7c734352009-10-19 03:32:19 +00003077 *rx_ring->prod_idx_sh_reg = 0;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003078 shadow_reg += sizeof(u64);
3079 shadow_reg_dma += sizeof(u64);
3080 rx_ring->lbq_base_indirect = shadow_reg;
3081 rx_ring->lbq_base_indirect_dma = shadow_reg_dma;
Ron Mercerb8facca2009-06-10 15:49:34 +00003082 shadow_reg += (sizeof(u64) * MAX_DB_PAGES_PER_BQ(rx_ring->lbq_len));
3083 shadow_reg_dma += (sizeof(u64) * MAX_DB_PAGES_PER_BQ(rx_ring->lbq_len));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003084 rx_ring->sbq_base_indirect = shadow_reg;
3085 rx_ring->sbq_base_indirect_dma = shadow_reg_dma;
3086
3087 /* PCI doorbell mem area + 0x00 for consumer index register */
Stephen Hemminger8668ae92008-11-21 17:29:50 -08003088 rx_ring->cnsmr_idx_db_reg = (u32 __iomem *) doorbell_area;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003089 rx_ring->cnsmr_idx = 0;
3090 rx_ring->curr_entry = rx_ring->cq_base;
3091
3092 /* PCI doorbell mem area + 0x04 for valid register */
3093 rx_ring->valid_db_reg = doorbell_area + 0x04;
3094
3095 /* PCI doorbell mem area + 0x18 for large buffer consumer */
Stephen Hemminger8668ae92008-11-21 17:29:50 -08003096 rx_ring->lbq_prod_idx_db_reg = (u32 __iomem *) (doorbell_area + 0x18);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003097
3098 /* PCI doorbell mem area + 0x1c */
Stephen Hemminger8668ae92008-11-21 17:29:50 -08003099 rx_ring->sbq_prod_idx_db_reg = (u32 __iomem *) (doorbell_area + 0x1c);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003100
3101 memset((void *)cqicb, 0, sizeof(struct cqicb));
3102 cqicb->msix_vect = rx_ring->irq;
3103
Ron Mercer459caf52009-01-04 17:08:11 -08003104 bq_len = (rx_ring->cq_len == 65536) ? 0 : (u16) rx_ring->cq_len;
3105 cqicb->len = cpu_to_le16(bq_len | LEN_V | LEN_CPP_CONT);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003106
Ron Mercer97345522009-01-09 11:31:50 +00003107 cqicb->addr = cpu_to_le64(rx_ring->cq_base_dma);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003108
Ron Mercer97345522009-01-09 11:31:50 +00003109 cqicb->prod_idx_addr = cpu_to_le64(rx_ring->prod_idx_sh_reg_dma);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003110
3111 /*
3112 * Set up the control block load flags.
3113 */
3114 cqicb->flags = FLAGS_LC | /* Load queue base address */
3115 FLAGS_LV | /* Load MSI-X vector */
3116 FLAGS_LI; /* Load irq delay values */
3117 if (rx_ring->lbq_len) {
3118 cqicb->flags |= FLAGS_LL; /* Load lbq values */
Joe Perchesa419aef2009-08-18 11:18:35 -07003119 tmp = (u64)rx_ring->lbq_base_dma;
Joe Perches43d620c2011-06-16 19:08:06 +00003120 base_indirect_ptr = rx_ring->lbq_base_indirect;
Ron Mercerb8facca2009-06-10 15:49:34 +00003121 page_entries = 0;
3122 do {
3123 *base_indirect_ptr = cpu_to_le64(tmp);
3124 tmp += DB_PAGE_SIZE;
3125 base_indirect_ptr++;
3126 page_entries++;
3127 } while (page_entries < MAX_DB_PAGES_PER_BQ(rx_ring->lbq_len));
Ron Mercer97345522009-01-09 11:31:50 +00003128 cqicb->lbq_addr =
3129 cpu_to_le64(rx_ring->lbq_base_indirect_dma);
Ron Mercer459caf52009-01-04 17:08:11 -08003130 bq_len = (rx_ring->lbq_buf_size == 65536) ? 0 :
3131 (u16) rx_ring->lbq_buf_size;
3132 cqicb->lbq_buf_size = cpu_to_le16(bq_len);
3133 bq_len = (rx_ring->lbq_len == 65536) ? 0 :
3134 (u16) rx_ring->lbq_len;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003135 cqicb->lbq_len = cpu_to_le16(bq_len);
Ron Mercer4545a3f2009-02-23 10:42:17 +00003136 rx_ring->lbq_prod_idx = 0;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003137 rx_ring->lbq_curr_idx = 0;
Ron Mercer4545a3f2009-02-23 10:42:17 +00003138 rx_ring->lbq_clean_idx = 0;
3139 rx_ring->lbq_free_cnt = rx_ring->lbq_len;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003140 }
3141 if (rx_ring->sbq_len) {
3142 cqicb->flags |= FLAGS_LS; /* Load sbq values */
Joe Perchesa419aef2009-08-18 11:18:35 -07003143 tmp = (u64)rx_ring->sbq_base_dma;
Joe Perches43d620c2011-06-16 19:08:06 +00003144 base_indirect_ptr = rx_ring->sbq_base_indirect;
Ron Mercerb8facca2009-06-10 15:49:34 +00003145 page_entries = 0;
3146 do {
3147 *base_indirect_ptr = cpu_to_le64(tmp);
3148 tmp += DB_PAGE_SIZE;
3149 base_indirect_ptr++;
3150 page_entries++;
3151 } while (page_entries < MAX_DB_PAGES_PER_BQ(rx_ring->sbq_len));
Ron Mercer97345522009-01-09 11:31:50 +00003152 cqicb->sbq_addr =
3153 cpu_to_le64(rx_ring->sbq_base_indirect_dma);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003154 cqicb->sbq_buf_size =
Ron Mercer52e55f32009-10-10 09:35:07 +00003155 cpu_to_le16((u16)(rx_ring->sbq_buf_size));
Ron Mercer459caf52009-01-04 17:08:11 -08003156 bq_len = (rx_ring->sbq_len == 65536) ? 0 :
3157 (u16) rx_ring->sbq_len;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003158 cqicb->sbq_len = cpu_to_le16(bq_len);
Ron Mercer4545a3f2009-02-23 10:42:17 +00003159 rx_ring->sbq_prod_idx = 0;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003160 rx_ring->sbq_curr_idx = 0;
Ron Mercer4545a3f2009-02-23 10:42:17 +00003161 rx_ring->sbq_clean_idx = 0;
3162 rx_ring->sbq_free_cnt = rx_ring->sbq_len;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003163 }
3164 switch (rx_ring->type) {
3165 case TX_Q:
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003166 cqicb->irq_delay = cpu_to_le16(qdev->tx_coalesce_usecs);
3167 cqicb->pkt_delay = cpu_to_le16(qdev->tx_max_coalesced_frames);
3168 break;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003169 case RX_Q:
3170 /* Inbound completion handling rx_rings run in
3171 * separate NAPI contexts.
3172 */
3173 netif_napi_add(qdev->ndev, &rx_ring->napi, ql_napi_poll_msix,
3174 64);
3175 cqicb->irq_delay = cpu_to_le16(qdev->rx_coalesce_usecs);
3176 cqicb->pkt_delay = cpu_to_le16(qdev->rx_max_coalesced_frames);
3177 break;
3178 default:
Joe Perchesae9540f72010-02-09 11:49:52 +00003179 netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
3180 "Invalid rx_ring->type = %d.\n", rx_ring->type);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003181 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003182 err = ql_write_cfg(qdev, cqicb, sizeof(struct cqicb),
3183 CFG_LCQ, rx_ring->cq_id);
3184 if (err) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003185 netif_err(qdev, ifup, qdev->ndev, "Failed to load CQICB.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003186 return err;
3187 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003188 return err;
3189}
3190
3191static int ql_start_tx_ring(struct ql_adapter *qdev, struct tx_ring *tx_ring)
3192{
3193 struct wqicb *wqicb = (struct wqicb *)tx_ring;
3194 void __iomem *doorbell_area =
3195 qdev->doorbell_area + (DB_PAGE_SIZE * tx_ring->wq_id);
3196 void *shadow_reg = qdev->tx_ring_shadow_reg_area +
3197 (tx_ring->wq_id * sizeof(u64));
3198 u64 shadow_reg_dma = qdev->tx_ring_shadow_reg_dma +
3199 (tx_ring->wq_id * sizeof(u64));
3200 int err = 0;
3201
3202 /*
3203 * Assign doorbell registers for this tx_ring.
3204 */
3205 /* TX PCI doorbell mem area for tx producer index */
Stephen Hemminger8668ae92008-11-21 17:29:50 -08003206 tx_ring->prod_idx_db_reg = (u32 __iomem *) doorbell_area;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003207 tx_ring->prod_idx = 0;
3208 /* TX PCI doorbell mem area + 0x04 */
3209 tx_ring->valid_db_reg = doorbell_area + 0x04;
3210
3211 /*
3212 * Assign shadow registers for this tx_ring.
3213 */
3214 tx_ring->cnsmr_idx_sh_reg = shadow_reg;
3215 tx_ring->cnsmr_idx_sh_reg_dma = shadow_reg_dma;
3216
3217 wqicb->len = cpu_to_le16(tx_ring->wq_len | Q_LEN_V | Q_LEN_CPP_CONT);
3218 wqicb->flags = cpu_to_le16(Q_FLAGS_LC |
3219 Q_FLAGS_LB | Q_FLAGS_LI | Q_FLAGS_LO);
3220 wqicb->cq_id_rss = cpu_to_le16(tx_ring->cq_id);
3221 wqicb->rid = 0;
Ron Mercer97345522009-01-09 11:31:50 +00003222 wqicb->addr = cpu_to_le64(tx_ring->wq_base_dma);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003223
Ron Mercer97345522009-01-09 11:31:50 +00003224 wqicb->cnsmr_idx_addr = cpu_to_le64(tx_ring->cnsmr_idx_sh_reg_dma);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003225
3226 ql_init_tx_ring(qdev, tx_ring);
3227
Ron Mercere3324712009-07-02 06:06:13 +00003228 err = ql_write_cfg(qdev, wqicb, sizeof(*wqicb), CFG_LRQ,
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003229 (u16) tx_ring->wq_id);
3230 if (err) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003231 netif_err(qdev, ifup, qdev->ndev, "Failed to load tx_ring.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003232 return err;
3233 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003234 return err;
3235}
3236
3237static void ql_disable_msix(struct ql_adapter *qdev)
3238{
3239 if (test_bit(QL_MSIX_ENABLED, &qdev->flags)) {
3240 pci_disable_msix(qdev->pdev);
3241 clear_bit(QL_MSIX_ENABLED, &qdev->flags);
3242 kfree(qdev->msi_x_entry);
3243 qdev->msi_x_entry = NULL;
3244 } else if (test_bit(QL_MSI_ENABLED, &qdev->flags)) {
3245 pci_disable_msi(qdev->pdev);
3246 clear_bit(QL_MSI_ENABLED, &qdev->flags);
3247 }
3248}
3249
Ron Mercera4ab6132009-08-27 11:02:10 +00003250/* We start by trying to get the number of vectors
3251 * stored in qdev->intr_count. If we don't get that
3252 * many then we reduce the count and try again.
3253 */
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003254static void ql_enable_msix(struct ql_adapter *qdev)
3255{
Ron Mercera4ab6132009-08-27 11:02:10 +00003256 int i, err;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003257
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003258 /* Get the MSIX vectors. */
Ron Mercera5a62a12009-11-11 12:54:05 +00003259 if (qlge_irq_type == MSIX_IRQ) {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003260 /* Try to alloc space for the msix struct,
3261 * if it fails then go to MSI/legacy.
3262 */
Ron Mercera4ab6132009-08-27 11:02:10 +00003263 qdev->msi_x_entry = kcalloc(qdev->intr_count,
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003264 sizeof(struct msix_entry),
3265 GFP_KERNEL);
3266 if (!qdev->msi_x_entry) {
Ron Mercera5a62a12009-11-11 12:54:05 +00003267 qlge_irq_type = MSI_IRQ;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003268 goto msi;
3269 }
3270
Ron Mercera4ab6132009-08-27 11:02:10 +00003271 for (i = 0; i < qdev->intr_count; i++)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003272 qdev->msi_x_entry[i].entry = i;
3273
Ron Mercera4ab6132009-08-27 11:02:10 +00003274 /* Loop to get our vectors. We start with
3275 * what we want and settle for what we get.
3276 */
3277 do {
3278 err = pci_enable_msix(qdev->pdev,
3279 qdev->msi_x_entry, qdev->intr_count);
3280 if (err > 0)
3281 qdev->intr_count = err;
3282 } while (err > 0);
3283
3284 if (err < 0) {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003285 kfree(qdev->msi_x_entry);
3286 qdev->msi_x_entry = NULL;
Joe Perchesae9540f72010-02-09 11:49:52 +00003287 netif_warn(qdev, ifup, qdev->ndev,
3288 "MSI-X Enable failed, trying MSI.\n");
Ron Mercera4ab6132009-08-27 11:02:10 +00003289 qdev->intr_count = 1;
Ron Mercera5a62a12009-11-11 12:54:05 +00003290 qlge_irq_type = MSI_IRQ;
Ron Mercera4ab6132009-08-27 11:02:10 +00003291 } else if (err == 0) {
3292 set_bit(QL_MSIX_ENABLED, &qdev->flags);
Joe Perchesae9540f72010-02-09 11:49:52 +00003293 netif_info(qdev, ifup, qdev->ndev,
3294 "MSI-X Enabled, got %d vectors.\n",
3295 qdev->intr_count);
Ron Mercera4ab6132009-08-27 11:02:10 +00003296 return;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003297 }
3298 }
3299msi:
Ron Mercera4ab6132009-08-27 11:02:10 +00003300 qdev->intr_count = 1;
Ron Mercera5a62a12009-11-11 12:54:05 +00003301 if (qlge_irq_type == MSI_IRQ) {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003302 if (!pci_enable_msi(qdev->pdev)) {
3303 set_bit(QL_MSI_ENABLED, &qdev->flags);
Joe Perchesae9540f72010-02-09 11:49:52 +00003304 netif_info(qdev, ifup, qdev->ndev,
3305 "Running with MSI interrupts.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003306 return;
3307 }
3308 }
Ron Mercera5a62a12009-11-11 12:54:05 +00003309 qlge_irq_type = LEG_IRQ;
Joe Perchesae9540f72010-02-09 11:49:52 +00003310 netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
3311 "Running with legacy interrupts.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003312}
3313
Ron Mercer39aa8162009-08-27 11:02:11 +00003314/* Each vector services 1 RSS ring and and 1 or more
3315 * TX completion rings. This function loops through
3316 * the TX completion rings and assigns the vector that
3317 * will service it. An example would be if there are
3318 * 2 vectors (so 2 RSS rings) and 8 TX completion rings.
3319 * This would mean that vector 0 would service RSS ring 0
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003320 * and TX completion rings 0,1,2 and 3. Vector 1 would
Ron Mercer39aa8162009-08-27 11:02:11 +00003321 * service RSS ring 1 and TX completion rings 4,5,6 and 7.
3322 */
3323static void ql_set_tx_vect(struct ql_adapter *qdev)
3324{
3325 int i, j, vect;
3326 u32 tx_rings_per_vector = qdev->tx_ring_count / qdev->intr_count;
3327
3328 if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags))) {
3329 /* Assign irq vectors to TX rx_rings.*/
3330 for (vect = 0, j = 0, i = qdev->rss_ring_count;
3331 i < qdev->rx_ring_count; i++) {
3332 if (j == tx_rings_per_vector) {
3333 vect++;
3334 j = 0;
3335 }
3336 qdev->rx_ring[i].irq = vect;
3337 j++;
3338 }
3339 } else {
3340 /* For single vector all rings have an irq
3341 * of zero.
3342 */
3343 for (i = 0; i < qdev->rx_ring_count; i++)
3344 qdev->rx_ring[i].irq = 0;
3345 }
3346}
3347
3348/* Set the interrupt mask for this vector. Each vector
3349 * will service 1 RSS ring and 1 or more TX completion
3350 * rings. This function sets up a bit mask per vector
3351 * that indicates which rings it services.
3352 */
3353static void ql_set_irq_mask(struct ql_adapter *qdev, struct intr_context *ctx)
3354{
3355 int j, vect = ctx->intr;
3356 u32 tx_rings_per_vector = qdev->tx_ring_count / qdev->intr_count;
3357
3358 if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags))) {
3359 /* Add the RSS ring serviced by this vector
3360 * to the mask.
3361 */
3362 ctx->irq_mask = (1 << qdev->rx_ring[vect].cq_id);
3363 /* Add the TX ring(s) serviced by this vector
3364 * to the mask. */
3365 for (j = 0; j < tx_rings_per_vector; j++) {
3366 ctx->irq_mask |=
3367 (1 << qdev->rx_ring[qdev->rss_ring_count +
3368 (vect * tx_rings_per_vector) + j].cq_id);
3369 }
3370 } else {
3371 /* For single vector we just shift each queue's
3372 * ID into the mask.
3373 */
3374 for (j = 0; j < qdev->rx_ring_count; j++)
3375 ctx->irq_mask |= (1 << qdev->rx_ring[j].cq_id);
3376 }
3377}
3378
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003379/*
3380 * Here we build the intr_context structures based on
3381 * our rx_ring count and intr vector count.
3382 * The intr_context structure is used to hook each vector
3383 * to possibly different handlers.
3384 */
3385static void ql_resolve_queues_to_irqs(struct ql_adapter *qdev)
3386{
3387 int i = 0;
3388 struct intr_context *intr_context = &qdev->intr_context[0];
3389
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003390 if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags))) {
3391 /* Each rx_ring has it's
3392 * own intr_context since we have separate
3393 * vectors for each queue.
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003394 */
3395 for (i = 0; i < qdev->intr_count; i++, intr_context++) {
3396 qdev->rx_ring[i].irq = i;
3397 intr_context->intr = i;
3398 intr_context->qdev = qdev;
Ron Mercer39aa8162009-08-27 11:02:11 +00003399 /* Set up this vector's bit-mask that indicates
3400 * which queues it services.
3401 */
3402 ql_set_irq_mask(qdev, intr_context);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003403 /*
3404 * We set up each vectors enable/disable/read bits so
3405 * there's no bit/mask calculations in the critical path.
3406 */
3407 intr_context->intr_en_mask =
3408 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
3409 INTR_EN_TYPE_ENABLE | INTR_EN_IHD_MASK | INTR_EN_IHD
3410 | i;
3411 intr_context->intr_dis_mask =
3412 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
3413 INTR_EN_TYPE_DISABLE | INTR_EN_IHD_MASK |
3414 INTR_EN_IHD | i;
3415 intr_context->intr_read_mask =
3416 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
3417 INTR_EN_TYPE_READ | INTR_EN_IHD_MASK | INTR_EN_IHD |
3418 i;
Ron Mercer39aa8162009-08-27 11:02:11 +00003419 if (i == 0) {
3420 /* The first vector/queue handles
3421 * broadcast/multicast, fatal errors,
3422 * and firmware events. This in addition
3423 * to normal inbound NAPI processing.
3424 */
3425 intr_context->handler = qlge_isr;
3426 sprintf(intr_context->name, "%s-rx-%d",
3427 qdev->ndev->name, i);
3428 } else {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003429 /*
3430 * Inbound queues handle unicast frames only.
3431 */
3432 intr_context->handler = qlge_msix_rx_isr;
Jesper Dangaard Brouerc2249692009-01-09 03:14:47 +00003433 sprintf(intr_context->name, "%s-rx-%d",
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003434 qdev->ndev->name, i);
3435 }
3436 }
3437 } else {
3438 /*
3439 * All rx_rings use the same intr_context since
3440 * there is only one vector.
3441 */
3442 intr_context->intr = 0;
3443 intr_context->qdev = qdev;
3444 /*
3445 * We set up each vectors enable/disable/read bits so
3446 * there's no bit/mask calculations in the critical path.
3447 */
3448 intr_context->intr_en_mask =
3449 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK | INTR_EN_TYPE_ENABLE;
3450 intr_context->intr_dis_mask =
3451 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
3452 INTR_EN_TYPE_DISABLE;
3453 intr_context->intr_read_mask =
3454 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK | INTR_EN_TYPE_READ;
3455 /*
3456 * Single interrupt means one handler for all rings.
3457 */
3458 intr_context->handler = qlge_isr;
3459 sprintf(intr_context->name, "%s-single_irq", qdev->ndev->name);
Ron Mercer39aa8162009-08-27 11:02:11 +00003460 /* Set up this vector's bit-mask that indicates
3461 * which queues it services. In this case there is
3462 * a single vector so it will service all RSS and
3463 * TX completion rings.
3464 */
3465 ql_set_irq_mask(qdev, intr_context);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003466 }
Ron Mercer39aa8162009-08-27 11:02:11 +00003467 /* Tell the TX completion rings which MSIx vector
3468 * they will be using.
3469 */
3470 ql_set_tx_vect(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003471}
3472
3473static void ql_free_irq(struct ql_adapter *qdev)
3474{
3475 int i;
3476 struct intr_context *intr_context = &qdev->intr_context[0];
3477
3478 for (i = 0; i < qdev->intr_count; i++, intr_context++) {
3479 if (intr_context->hooked) {
3480 if (test_bit(QL_MSIX_ENABLED, &qdev->flags)) {
3481 free_irq(qdev->msi_x_entry[i].vector,
3482 &qdev->rx_ring[i]);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003483 } else {
3484 free_irq(qdev->pdev->irq, &qdev->rx_ring[0]);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003485 }
3486 }
3487 }
3488 ql_disable_msix(qdev);
3489}
3490
3491static int ql_request_irq(struct ql_adapter *qdev)
3492{
3493 int i;
3494 int status = 0;
3495 struct pci_dev *pdev = qdev->pdev;
3496 struct intr_context *intr_context = &qdev->intr_context[0];
3497
3498 ql_resolve_queues_to_irqs(qdev);
3499
3500 for (i = 0; i < qdev->intr_count; i++, intr_context++) {
3501 atomic_set(&intr_context->irq_cnt, 0);
3502 if (test_bit(QL_MSIX_ENABLED, &qdev->flags)) {
3503 status = request_irq(qdev->msi_x_entry[i].vector,
3504 intr_context->handler,
3505 0,
3506 intr_context->name,
3507 &qdev->rx_ring[i]);
3508 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003509 netif_err(qdev, ifup, qdev->ndev,
3510 "Failed request for MSIX interrupt %d.\n",
3511 i);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003512 goto err_irq;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003513 }
3514 } else {
Joe Perchesae9540f72010-02-09 11:49:52 +00003515 netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
3516 "trying msi or legacy interrupts.\n");
3517 netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
3518 "%s: irq = %d.\n", __func__, pdev->irq);
3519 netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
3520 "%s: context->name = %s.\n", __func__,
3521 intr_context->name);
3522 netif_printk(qdev, ifup, KERN_DEBUG, qdev->ndev,
3523 "%s: dev_id = 0x%p.\n", __func__,
3524 &qdev->rx_ring[0]);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003525 status =
3526 request_irq(pdev->irq, qlge_isr,
3527 test_bit(QL_MSI_ENABLED,
3528 &qdev->
3529 flags) ? 0 : IRQF_SHARED,
3530 intr_context->name, &qdev->rx_ring[0]);
3531 if (status)
3532 goto err_irq;
3533
Joe Perchesae9540f72010-02-09 11:49:52 +00003534 netif_err(qdev, ifup, qdev->ndev,
3535 "Hooked intr %d, queue type %s, with name %s.\n",
3536 i,
3537 qdev->rx_ring[0].type == DEFAULT_Q ?
3538 "DEFAULT_Q" :
3539 qdev->rx_ring[0].type == TX_Q ? "TX_Q" :
3540 qdev->rx_ring[0].type == RX_Q ? "RX_Q" : "",
3541 intr_context->name);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003542 }
3543 intr_context->hooked = 1;
3544 }
3545 return status;
3546err_irq:
Joe Perchesae9540f72010-02-09 11:49:52 +00003547 netif_err(qdev, ifup, qdev->ndev, "Failed to get the interrupts!!!/n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003548 ql_free_irq(qdev);
3549 return status;
3550}
3551
3552static int ql_start_rss(struct ql_adapter *qdev)
3553{
Joe Perches215faf92010-12-21 02:16:10 -08003554 static const u8 init_hash_seed[] = {
3555 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
3556 0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
3557 0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
3558 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
3559 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa
3560 };
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003561 struct ricb *ricb = &qdev->ricb;
3562 int status = 0;
3563 int i;
3564 u8 *hash_id = (u8 *) ricb->hash_cq_id;
3565
Ron Mercere3324712009-07-02 06:06:13 +00003566 memset((void *)ricb, 0, sizeof(*ricb));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003567
Ron Mercerb2014ff2009-08-27 11:02:09 +00003568 ricb->base_cq = RSS_L4K;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003569 ricb->flags =
Ron Mercer541ae282009-10-08 09:54:37 +00003570 (RSS_L6K | RSS_LI | RSS_LB | RSS_LM | RSS_RT4 | RSS_RT6);
3571 ricb->mask = cpu_to_le16((u16)(0x3ff));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003572
3573 /*
3574 * Fill out the Indirection Table.
3575 */
Ron Mercer541ae282009-10-08 09:54:37 +00003576 for (i = 0; i < 1024; i++)
3577 hash_id[i] = (i & (qdev->rss_ring_count - 1));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003578
Ron Mercer541ae282009-10-08 09:54:37 +00003579 memcpy((void *)&ricb->ipv6_hash_key[0], init_hash_seed, 40);
3580 memcpy((void *)&ricb->ipv4_hash_key[0], init_hash_seed, 16);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003581
Ron Mercere3324712009-07-02 06:06:13 +00003582 status = ql_write_cfg(qdev, ricb, sizeof(*ricb), CFG_LR, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003583 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003584 netif_err(qdev, ifup, qdev->ndev, "Failed to load RICB.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003585 return status;
3586 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003587 return status;
3588}
3589
Ron Mercera5f59dc2009-07-02 06:06:07 +00003590static int ql_clear_routing_entries(struct ql_adapter *qdev)
3591{
3592 int i, status = 0;
3593
3594 status = ql_sem_spinlock(qdev, SEM_RT_IDX_MASK);
3595 if (status)
3596 return status;
3597 /* Clear all the entries in the routing table. */
3598 for (i = 0; i < 16; i++) {
3599 status = ql_set_routing_reg(qdev, i, 0, 0);
3600 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003601 netif_err(qdev, ifup, qdev->ndev,
3602 "Failed to init routing register for CAM packets.\n");
Ron Mercera5f59dc2009-07-02 06:06:07 +00003603 break;
3604 }
3605 }
3606 ql_sem_unlock(qdev, SEM_RT_IDX_MASK);
3607 return status;
3608}
3609
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003610/* Initialize the frame-to-queue routing. */
3611static int ql_route_initialize(struct ql_adapter *qdev)
3612{
3613 int status = 0;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003614
3615 /* Clear all the entries in the routing table. */
Ron Mercera5f59dc2009-07-02 06:06:07 +00003616 status = ql_clear_routing_entries(qdev);
3617 if (status)
Ron Mercerfd21cf52009-09-29 08:39:22 +00003618 return status;
3619
3620 status = ql_sem_spinlock(qdev, SEM_RT_IDX_MASK);
3621 if (status)
3622 return status;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003623
Ron Mercerfbc2ac32010-07-05 12:19:41 +00003624 status = ql_set_routing_reg(qdev, RT_IDX_IP_CSUM_ERR_SLOT,
3625 RT_IDX_IP_CSUM_ERR, 1);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003626 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003627 netif_err(qdev, ifup, qdev->ndev,
Ron Mercerfbc2ac32010-07-05 12:19:41 +00003628 "Failed to init routing register "
3629 "for IP CSUM error packets.\n");
3630 goto exit;
3631 }
3632 status = ql_set_routing_reg(qdev, RT_IDX_TCP_UDP_CSUM_ERR_SLOT,
3633 RT_IDX_TU_CSUM_ERR, 1);
3634 if (status) {
3635 netif_err(qdev, ifup, qdev->ndev,
3636 "Failed to init routing register "
3637 "for TCP/UDP CSUM error packets.\n");
Ron Mercer8587ea32009-02-23 10:42:15 +00003638 goto exit;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003639 }
3640 status = ql_set_routing_reg(qdev, RT_IDX_BCAST_SLOT, RT_IDX_BCAST, 1);
3641 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003642 netif_err(qdev, ifup, qdev->ndev,
3643 "Failed to init routing register for broadcast packets.\n");
Ron Mercer8587ea32009-02-23 10:42:15 +00003644 goto exit;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003645 }
3646 /* If we have more than one inbound queue, then turn on RSS in the
3647 * routing block.
3648 */
3649 if (qdev->rss_ring_count > 1) {
3650 status = ql_set_routing_reg(qdev, RT_IDX_RSS_MATCH_SLOT,
3651 RT_IDX_RSS_MATCH, 1);
3652 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003653 netif_err(qdev, ifup, qdev->ndev,
3654 "Failed to init routing register for MATCH RSS packets.\n");
Ron Mercer8587ea32009-02-23 10:42:15 +00003655 goto exit;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003656 }
3657 }
3658
3659 status = ql_set_routing_reg(qdev, RT_IDX_CAM_HIT_SLOT,
3660 RT_IDX_CAM_HIT, 1);
Ron Mercer8587ea32009-02-23 10:42:15 +00003661 if (status)
Joe Perchesae9540f72010-02-09 11:49:52 +00003662 netif_err(qdev, ifup, qdev->ndev,
3663 "Failed to init routing register for CAM packets.\n");
Ron Mercer8587ea32009-02-23 10:42:15 +00003664exit:
3665 ql_sem_unlock(qdev, SEM_RT_IDX_MASK);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003666 return status;
3667}
3668
Ron Mercer2ee1e272009-03-03 12:10:33 +00003669int ql_cam_route_initialize(struct ql_adapter *qdev)
Ron Mercerbb58b5b2009-02-23 10:42:13 +00003670{
Ron Mercer7fab3bfe2009-07-02 06:06:11 +00003671 int status, set;
Ron Mercerbb58b5b2009-02-23 10:42:13 +00003672
Ron Mercer7fab3bfe2009-07-02 06:06:11 +00003673 /* If check if the link is up and use to
3674 * determine if we are setting or clearing
3675 * the MAC address in the CAM.
3676 */
3677 set = ql_read32(qdev, STS);
3678 set &= qdev->port_link_up;
3679 status = ql_set_mac_addr(qdev, set);
Ron Mercerbb58b5b2009-02-23 10:42:13 +00003680 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003681 netif_err(qdev, ifup, qdev->ndev, "Failed to init mac address.\n");
Ron Mercerbb58b5b2009-02-23 10:42:13 +00003682 return status;
3683 }
3684
3685 status = ql_route_initialize(qdev);
3686 if (status)
Joe Perchesae9540f72010-02-09 11:49:52 +00003687 netif_err(qdev, ifup, qdev->ndev, "Failed to init routing table.\n");
Ron Mercerbb58b5b2009-02-23 10:42:13 +00003688
3689 return status;
3690}
3691
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003692static int ql_adapter_initialize(struct ql_adapter *qdev)
3693{
3694 u32 value, mask;
3695 int i;
3696 int status = 0;
3697
3698 /*
3699 * Set up the System register to halt on errors.
3700 */
3701 value = SYS_EFE | SYS_FAE;
3702 mask = value << 16;
3703 ql_write32(qdev, SYS, mask | value);
3704
Ron Mercerc9cf0a02009-03-09 10:59:22 +00003705 /* Set the default queue, and VLAN behavior. */
3706 value = NIC_RCV_CFG_DFQ | NIC_RCV_CFG_RV;
3707 mask = NIC_RCV_CFG_DFQ_MASK | (NIC_RCV_CFG_RV << 16);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003708 ql_write32(qdev, NIC_RCV_CFG, (mask | value));
3709
3710 /* Set the MPI interrupt to enabled. */
3711 ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16) | INTR_MASK_PI);
3712
3713 /* Enable the function, set pagesize, enable error checking. */
3714 value = FSC_FE | FSC_EPC_INBOUND | FSC_EPC_OUTBOUND |
Ron Mercer572c5262010-01-02 10:37:42 +00003715 FSC_EC | FSC_VM_PAGE_4K;
3716 value |= SPLT_SETTING;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003717
3718 /* Set/clear header splitting. */
3719 mask = FSC_VM_PAGESIZE_MASK |
3720 FSC_DBL_MASK | FSC_DBRST_MASK | (value << 16);
3721 ql_write32(qdev, FSC, mask | value);
3722
Ron Mercer572c5262010-01-02 10:37:42 +00003723 ql_write32(qdev, SPLT_HDR, SPLT_LEN);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003724
Ron Mercera3b71932009-10-08 09:54:38 +00003725 /* Set RX packet routing to use port/pci function on which the
3726 * packet arrived on in addition to usual frame routing.
3727 * This is helpful on bonding where both interfaces can have
3728 * the same MAC address.
3729 */
3730 ql_write32(qdev, RST_FO, RST_FO_RR_MASK | RST_FO_RR_RCV_FUNC_CQ);
Ron Mercerbc083ce2009-10-21 11:07:40 +00003731 /* Reroute all packets to our Interface.
3732 * They may have been routed to MPI firmware
3733 * due to WOL.
3734 */
3735 value = ql_read32(qdev, MGMT_RCV_CFG);
3736 value &= ~MGMT_RCV_CFG_RM;
3737 mask = 0xffff0000;
3738
3739 /* Sticky reg needs clearing due to WOL. */
3740 ql_write32(qdev, MGMT_RCV_CFG, mask);
3741 ql_write32(qdev, MGMT_RCV_CFG, mask | value);
3742
3743 /* Default WOL is enable on Mezz cards */
3744 if (qdev->pdev->subsystem_device == 0x0068 ||
3745 qdev->pdev->subsystem_device == 0x0180)
3746 qdev->wol = WAKE_MAGIC;
Ron Mercera3b71932009-10-08 09:54:38 +00003747
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003748 /* Start up the rx queues. */
3749 for (i = 0; i < qdev->rx_ring_count; i++) {
3750 status = ql_start_rx_ring(qdev, &qdev->rx_ring[i]);
3751 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003752 netif_err(qdev, ifup, qdev->ndev,
3753 "Failed to start rx ring[%d].\n", i);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003754 return status;
3755 }
3756 }
3757
3758 /* If there is more than one inbound completion queue
3759 * then download a RICB to configure RSS.
3760 */
3761 if (qdev->rss_ring_count > 1) {
3762 status = ql_start_rss(qdev);
3763 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003764 netif_err(qdev, ifup, qdev->ndev, "Failed to start RSS.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003765 return status;
3766 }
3767 }
3768
3769 /* Start up the tx queues. */
3770 for (i = 0; i < qdev->tx_ring_count; i++) {
3771 status = ql_start_tx_ring(qdev, &qdev->tx_ring[i]);
3772 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003773 netif_err(qdev, ifup, qdev->ndev,
3774 "Failed to start tx ring[%d].\n", i);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003775 return status;
3776 }
3777 }
3778
Ron Mercerb0c2aad2009-02-26 10:08:35 +00003779 /* Initialize the port and set the max framesize. */
3780 status = qdev->nic_ops->port_initialize(qdev);
Ron Mercer80928862009-10-10 09:35:09 +00003781 if (status)
Joe Perchesae9540f72010-02-09 11:49:52 +00003782 netif_err(qdev, ifup, qdev->ndev, "Failed to start port.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003783
Ron Mercerbb58b5b2009-02-23 10:42:13 +00003784 /* Set up the MAC address and frame routing filter. */
3785 status = ql_cam_route_initialize(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003786 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003787 netif_err(qdev, ifup, qdev->ndev,
3788 "Failed to init CAM/Routing tables.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003789 return status;
3790 }
3791
3792 /* Start NAPI for the RSS queues. */
Jitendra Kalsaria19257f52012-02-03 14:06:50 +00003793 for (i = 0; i < qdev->rss_ring_count; i++)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003794 napi_enable(&qdev->rx_ring[i].napi);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003795
3796 return status;
3797}
3798
3799/* Issue soft reset to chip. */
3800static int ql_adapter_reset(struct ql_adapter *qdev)
3801{
3802 u32 value;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003803 int status = 0;
Ron Mercera5f59dc2009-07-02 06:06:07 +00003804 unsigned long end_jiffies;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003805
Ron Mercera5f59dc2009-07-02 06:06:07 +00003806 /* Clear all the entries in the routing table. */
3807 status = ql_clear_routing_entries(qdev);
3808 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003809 netif_err(qdev, ifup, qdev->ndev, "Failed to clear routing bits.\n");
Ron Mercera5f59dc2009-07-02 06:06:07 +00003810 return status;
3811 }
3812
3813 end_jiffies = jiffies +
3814 max((unsigned long)1, usecs_to_jiffies(30));
Ron Mercer84087f42009-10-08 09:54:41 +00003815
Jitendra Kalsariada92b392011-06-30 10:02:05 +00003816 /* Check if bit is set then skip the mailbox command and
3817 * clear the bit, else we are in normal reset process.
3818 */
3819 if (!test_bit(QL_ASIC_RECOVERY, &qdev->flags)) {
3820 /* Stop management traffic. */
3821 ql_mb_set_mgmnt_traffic_ctl(qdev, MB_SET_MPI_TFK_STOP);
Ron Mercer84087f42009-10-08 09:54:41 +00003822
Jitendra Kalsariada92b392011-06-30 10:02:05 +00003823 /* Wait for the NIC and MGMNT FIFOs to empty. */
3824 ql_wait_fifo_empty(qdev);
3825 } else
3826 clear_bit(QL_ASIC_RECOVERY, &qdev->flags);
Ron Mercer84087f42009-10-08 09:54:41 +00003827
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003828 ql_write32(qdev, RST_FO, (RST_FO_FR << 16) | RST_FO_FR);
Ron Mercera75ee7f2009-03-09 10:59:18 +00003829
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003830 do {
3831 value = ql_read32(qdev, RST_FO);
3832 if ((value & RST_FO_FR) == 0)
3833 break;
Ron Mercera75ee7f2009-03-09 10:59:18 +00003834 cpu_relax();
3835 } while (time_before(jiffies, end_jiffies));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003836
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003837 if (value & RST_FO_FR) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003838 netif_err(qdev, ifdown, qdev->ndev,
3839 "ETIMEDOUT!!! errored out of resetting the chip!\n");
Ron Mercera75ee7f2009-03-09 10:59:18 +00003840 status = -ETIMEDOUT;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003841 }
3842
Ron Mercer84087f42009-10-08 09:54:41 +00003843 /* Resume management traffic. */
3844 ql_mb_set_mgmnt_traffic_ctl(qdev, MB_SET_MPI_TFK_RESUME);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003845 return status;
3846}
3847
3848static void ql_display_dev_info(struct net_device *ndev)
3849{
Joe Perchesb16fed02010-11-15 11:12:28 +00003850 struct ql_adapter *qdev = netdev_priv(ndev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003851
Joe Perchesae9540f72010-02-09 11:49:52 +00003852 netif_info(qdev, probe, qdev->ndev,
3853 "Function #%d, Port %d, NIC Roll %d, NIC Rev = %d, "
3854 "XG Roll = %d, XG Rev = %d.\n",
3855 qdev->func,
3856 qdev->port,
3857 qdev->chip_rev_id & 0x0000000f,
3858 qdev->chip_rev_id >> 4 & 0x0000000f,
3859 qdev->chip_rev_id >> 8 & 0x0000000f,
3860 qdev->chip_rev_id >> 12 & 0x0000000f);
3861 netif_info(qdev, probe, qdev->ndev,
3862 "MAC address %pM\n", ndev->dev_addr);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003863}
3864
stephen hemmingerac409212010-10-21 07:50:54 +00003865static int ql_wol(struct ql_adapter *qdev)
Ron Mercerbc083ce2009-10-21 11:07:40 +00003866{
3867 int status = 0;
3868 u32 wol = MB_WOL_DISABLE;
3869
3870 /* The CAM is still intact after a reset, but if we
3871 * are doing WOL, then we may need to program the
3872 * routing regs. We would also need to issue the mailbox
3873 * commands to instruct the MPI what to do per the ethtool
3874 * settings.
3875 */
3876
3877 if (qdev->wol & (WAKE_ARP | WAKE_MAGICSECURE | WAKE_PHY | WAKE_UCAST |
3878 WAKE_MCAST | WAKE_BCAST)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003879 netif_err(qdev, ifdown, qdev->ndev,
Masanari Iidafd9071e2012-04-13 04:33:20 +00003880 "Unsupported WOL parameter. qdev->wol = 0x%x.\n",
Joe Perchesae9540f72010-02-09 11:49:52 +00003881 qdev->wol);
Ron Mercerbc083ce2009-10-21 11:07:40 +00003882 return -EINVAL;
3883 }
3884
3885 if (qdev->wol & WAKE_MAGIC) {
3886 status = ql_mb_wol_set_magic(qdev, 1);
3887 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003888 netif_err(qdev, ifdown, qdev->ndev,
3889 "Failed to set magic packet on %s.\n",
3890 qdev->ndev->name);
Ron Mercerbc083ce2009-10-21 11:07:40 +00003891 return status;
3892 } else
Joe Perchesae9540f72010-02-09 11:49:52 +00003893 netif_info(qdev, drv, qdev->ndev,
3894 "Enabled magic packet successfully on %s.\n",
3895 qdev->ndev->name);
Ron Mercerbc083ce2009-10-21 11:07:40 +00003896
3897 wol |= MB_WOL_MAGIC_PKT;
3898 }
3899
3900 if (qdev->wol) {
Ron Mercerbc083ce2009-10-21 11:07:40 +00003901 wol |= MB_WOL_MODE_ON;
3902 status = ql_mb_wol_mode(qdev, wol);
Joe Perchesae9540f72010-02-09 11:49:52 +00003903 netif_err(qdev, drv, qdev->ndev,
3904 "WOL %s (wol code 0x%x) on %s\n",
Jiri Kosina318ae2e2010-03-08 16:55:37 +01003905 (status == 0) ? "Successfully set" : "Failed",
Joe Perchesae9540f72010-02-09 11:49:52 +00003906 wol, qdev->ndev->name);
Ron Mercerbc083ce2009-10-21 11:07:40 +00003907 }
3908
3909 return status;
3910}
3911
Breno Leitaoc5daddd2010-08-24 12:50:40 +00003912static void ql_cancel_all_work_sync(struct ql_adapter *qdev)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003913{
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003914
Ron Mercer6497b602009-02-12 16:37:13 -08003915 /* Don't kill the reset worker thread if we
3916 * are in the process of recovery.
3917 */
3918 if (test_bit(QL_ADAPTER_UP, &qdev->flags))
3919 cancel_delayed_work_sync(&qdev->asic_reset_work);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003920 cancel_delayed_work_sync(&qdev->mpi_reset_work);
3921 cancel_delayed_work_sync(&qdev->mpi_work);
Ron Mercer2ee1e272009-03-03 12:10:33 +00003922 cancel_delayed_work_sync(&qdev->mpi_idc_work);
Ron Mercer8aae2602010-01-15 13:31:28 +00003923 cancel_delayed_work_sync(&qdev->mpi_core_to_log);
Ron Mercerbcc2cb32009-03-02 08:07:32 +00003924 cancel_delayed_work_sync(&qdev->mpi_port_cfg_work);
Breno Leitaoc5daddd2010-08-24 12:50:40 +00003925}
3926
3927static int ql_adapter_down(struct ql_adapter *qdev)
3928{
3929 int i, status = 0;
3930
3931 ql_link_off(qdev);
3932
3933 ql_cancel_all_work_sync(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003934
Ron Mercer39aa8162009-08-27 11:02:11 +00003935 for (i = 0; i < qdev->rss_ring_count; i++)
3936 napi_disable(&qdev->rx_ring[i].napi);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003937
3938 clear_bit(QL_ADAPTER_UP, &qdev->flags);
3939
3940 ql_disable_interrupts(qdev);
3941
3942 ql_tx_ring_clean(qdev);
3943
Ron Mercer6b318cb2009-03-09 10:59:26 +00003944 /* Call netif_napi_del() from common point.
3945 */
Ron Mercerb2014ff2009-08-27 11:02:09 +00003946 for (i = 0; i < qdev->rss_ring_count; i++)
Ron Mercer6b318cb2009-03-09 10:59:26 +00003947 netif_napi_del(&qdev->rx_ring[i].napi);
3948
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003949 status = ql_adapter_reset(qdev);
3950 if (status)
Joe Perchesae9540f72010-02-09 11:49:52 +00003951 netif_err(qdev, ifdown, qdev->ndev, "reset(func #%d) FAILED!\n",
3952 qdev->func);
Breno Leitaofe5f0982010-08-26 08:27:58 +00003953 ql_free_rx_buffers(qdev);
3954
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003955 return status;
3956}
3957
3958static int ql_adapter_up(struct ql_adapter *qdev)
3959{
3960 int err = 0;
3961
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003962 err = ql_adapter_initialize(qdev);
3963 if (err) {
Joe Perchesae9540f72010-02-09 11:49:52 +00003964 netif_info(qdev, ifup, qdev->ndev, "Unable to initialize adapter.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003965 goto err_init;
3966 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003967 set_bit(QL_ADAPTER_UP, &qdev->flags);
Ron Mercer4545a3f2009-02-23 10:42:17 +00003968 ql_alloc_rx_buffers(qdev);
Ron Mercer8b007de2009-07-02 06:06:08 +00003969 /* If the port is initialized and the
3970 * link is up the turn on the carrier.
3971 */
3972 if ((ql_read32(qdev, STS) & qdev->port_init) &&
3973 (ql_read32(qdev, STS) & qdev->port_link_up))
Ron Mercer6a473302009-07-02 06:06:12 +00003974 ql_link_on(qdev);
Ron Mercerf2c05002010-07-05 12:19:37 +00003975 /* Restore rx mode. */
3976 clear_bit(QL_ALLMULTI, &qdev->flags);
3977 clear_bit(QL_PROMISCUOUS, &qdev->flags);
3978 qlge_set_multicast_list(qdev->ndev);
3979
Ron Mercerc1b60092010-10-27 04:58:12 +00003980 /* Restore vlan setting. */
3981 qlge_restore_vlan(qdev);
3982
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003983 ql_enable_interrupts(qdev);
3984 ql_enable_all_completion_interrupts(qdev);
Ron Mercer1e213302009-03-09 10:59:21 +00003985 netif_tx_start_all_queues(qdev->ndev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003986
3987 return 0;
3988err_init:
3989 ql_adapter_reset(qdev);
3990 return err;
3991}
3992
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003993static void ql_release_adapter_resources(struct ql_adapter *qdev)
3994{
3995 ql_free_mem_resources(qdev);
3996 ql_free_irq(qdev);
3997}
3998
3999static int ql_get_adapter_resources(struct ql_adapter *qdev)
4000{
4001 int status = 0;
4002
4003 if (ql_alloc_mem_resources(qdev)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004004 netif_err(qdev, ifup, qdev->ndev, "Unable to allocate memory.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004005 return -ENOMEM;
4006 }
4007 status = ql_request_irq(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004008 return status;
4009}
4010
4011static int qlge_close(struct net_device *ndev)
4012{
4013 struct ql_adapter *qdev = netdev_priv(ndev);
4014
Ron Mercer4bbd1a12010-02-03 07:24:12 +00004015 /* If we hit pci_channel_io_perm_failure
4016 * failure condition, then we already
4017 * brought the adapter down.
4018 */
4019 if (test_bit(QL_EEH_FATAL, &qdev->flags)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004020 netif_err(qdev, drv, qdev->ndev, "EEH fatal did unload.\n");
Ron Mercer4bbd1a12010-02-03 07:24:12 +00004021 clear_bit(QL_EEH_FATAL, &qdev->flags);
4022 return 0;
4023 }
4024
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004025 /*
4026 * Wait for device to recover from a reset.
4027 * (Rarely happens, but possible.)
4028 */
4029 while (!test_bit(QL_ADAPTER_UP, &qdev->flags))
4030 msleep(1);
4031 ql_adapter_down(qdev);
4032 ql_release_adapter_resources(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004033 return 0;
4034}
4035
4036static int ql_configure_rings(struct ql_adapter *qdev)
4037{
4038 int i;
4039 struct rx_ring *rx_ring;
4040 struct tx_ring *tx_ring;
Ron Mercera4ab6132009-08-27 11:02:10 +00004041 int cpu_cnt = min(MAX_CPUS, (int)num_online_cpus());
Ron Mercer7c734352009-10-19 03:32:19 +00004042 unsigned int lbq_buf_len = (qdev->ndev->mtu > 1500) ?
4043 LARGE_BUFFER_MAX_SIZE : LARGE_BUFFER_MIN_SIZE;
4044
4045 qdev->lbq_buf_order = get_order(lbq_buf_len);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004046
Ron Mercera4ab6132009-08-27 11:02:10 +00004047 /* In a perfect world we have one RSS ring for each CPU
4048 * and each has it's own vector. To do that we ask for
4049 * cpu_cnt vectors. ql_enable_msix() will adjust the
4050 * vector count to what we actually get. We then
4051 * allocate an RSS ring for each.
4052 * Essentially, we are doing min(cpu_count, msix_vector_count).
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004053 */
Ron Mercera4ab6132009-08-27 11:02:10 +00004054 qdev->intr_count = cpu_cnt;
4055 ql_enable_msix(qdev);
4056 /* Adjust the RSS ring count to the actual vector count. */
4057 qdev->rss_ring_count = qdev->intr_count;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004058 qdev->tx_ring_count = cpu_cnt;
Ron Mercerb2014ff2009-08-27 11:02:09 +00004059 qdev->rx_ring_count = qdev->tx_ring_count + qdev->rss_ring_count;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004060
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004061 for (i = 0; i < qdev->tx_ring_count; i++) {
4062 tx_ring = &qdev->tx_ring[i];
Ron Mercere3324712009-07-02 06:06:13 +00004063 memset((void *)tx_ring, 0, sizeof(*tx_ring));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004064 tx_ring->qdev = qdev;
4065 tx_ring->wq_id = i;
4066 tx_ring->wq_len = qdev->tx_ring_size;
4067 tx_ring->wq_size =
4068 tx_ring->wq_len * sizeof(struct ob_mac_iocb_req);
4069
4070 /*
4071 * The completion queue ID for the tx rings start
Ron Mercer39aa8162009-08-27 11:02:11 +00004072 * immediately after the rss rings.
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004073 */
Ron Mercer39aa8162009-08-27 11:02:11 +00004074 tx_ring->cq_id = qdev->rss_ring_count + i;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004075 }
4076
4077 for (i = 0; i < qdev->rx_ring_count; i++) {
4078 rx_ring = &qdev->rx_ring[i];
Ron Mercere3324712009-07-02 06:06:13 +00004079 memset((void *)rx_ring, 0, sizeof(*rx_ring));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004080 rx_ring->qdev = qdev;
4081 rx_ring->cq_id = i;
4082 rx_ring->cpu = i % cpu_cnt; /* CPU to run handler on. */
Ron Mercerb2014ff2009-08-27 11:02:09 +00004083 if (i < qdev->rss_ring_count) {
Ron Mercer39aa8162009-08-27 11:02:11 +00004084 /*
4085 * Inbound (RSS) queues.
4086 */
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004087 rx_ring->cq_len = qdev->rx_ring_size;
4088 rx_ring->cq_size =
4089 rx_ring->cq_len * sizeof(struct ql_net_rsp_iocb);
4090 rx_ring->lbq_len = NUM_LARGE_BUFFERS;
4091 rx_ring->lbq_size =
Ron Mercer2c9a0d42009-01-05 18:19:20 -08004092 rx_ring->lbq_len * sizeof(__le64);
Ron Mercer7c734352009-10-19 03:32:19 +00004093 rx_ring->lbq_buf_size = (u16)lbq_buf_len;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004094 rx_ring->sbq_len = NUM_SMALL_BUFFERS;
4095 rx_ring->sbq_size =
Ron Mercer2c9a0d42009-01-05 18:19:20 -08004096 rx_ring->sbq_len * sizeof(__le64);
Ron Mercer52e55f32009-10-10 09:35:07 +00004097 rx_ring->sbq_buf_size = SMALL_BUF_MAP_SIZE;
Ron Mercerb2014ff2009-08-27 11:02:09 +00004098 rx_ring->type = RX_Q;
4099 } else {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004100 /*
4101 * Outbound queue handles outbound completions only.
4102 */
4103 /* outbound cq is same size as tx_ring it services. */
4104 rx_ring->cq_len = qdev->tx_ring_size;
4105 rx_ring->cq_size =
4106 rx_ring->cq_len * sizeof(struct ql_net_rsp_iocb);
4107 rx_ring->lbq_len = 0;
4108 rx_ring->lbq_size = 0;
4109 rx_ring->lbq_buf_size = 0;
4110 rx_ring->sbq_len = 0;
4111 rx_ring->sbq_size = 0;
4112 rx_ring->sbq_buf_size = 0;
4113 rx_ring->type = TX_Q;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004114 }
4115 }
4116 return 0;
4117}
4118
4119static int qlge_open(struct net_device *ndev)
4120{
4121 int err = 0;
4122 struct ql_adapter *qdev = netdev_priv(ndev);
4123
Ron Mercer74e12432009-11-11 12:54:04 +00004124 err = ql_adapter_reset(qdev);
4125 if (err)
4126 return err;
4127
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004128 err = ql_configure_rings(qdev);
4129 if (err)
4130 return err;
4131
4132 err = ql_get_adapter_resources(qdev);
4133 if (err)
4134 goto error_up;
4135
4136 err = ql_adapter_up(qdev);
4137 if (err)
4138 goto error_up;
4139
4140 return err;
4141
4142error_up:
4143 ql_release_adapter_resources(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004144 return err;
4145}
4146
Ron Mercer7c734352009-10-19 03:32:19 +00004147static int ql_change_rx_buffers(struct ql_adapter *qdev)
4148{
4149 struct rx_ring *rx_ring;
4150 int i, status;
4151 u32 lbq_buf_len;
4152
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004153 /* Wait for an outstanding reset to complete. */
Ron Mercer7c734352009-10-19 03:32:19 +00004154 if (!test_bit(QL_ADAPTER_UP, &qdev->flags)) {
4155 int i = 3;
4156 while (i-- && !test_bit(QL_ADAPTER_UP, &qdev->flags)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004157 netif_err(qdev, ifup, qdev->ndev,
4158 "Waiting for adapter UP...\n");
Ron Mercer7c734352009-10-19 03:32:19 +00004159 ssleep(1);
4160 }
4161
4162 if (!i) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004163 netif_err(qdev, ifup, qdev->ndev,
4164 "Timed out waiting for adapter UP\n");
Ron Mercer7c734352009-10-19 03:32:19 +00004165 return -ETIMEDOUT;
4166 }
4167 }
4168
4169 status = ql_adapter_down(qdev);
4170 if (status)
4171 goto error;
4172
4173 /* Get the new rx buffer size. */
4174 lbq_buf_len = (qdev->ndev->mtu > 1500) ?
4175 LARGE_BUFFER_MAX_SIZE : LARGE_BUFFER_MIN_SIZE;
4176 qdev->lbq_buf_order = get_order(lbq_buf_len);
4177
4178 for (i = 0; i < qdev->rss_ring_count; i++) {
4179 rx_ring = &qdev->rx_ring[i];
4180 /* Set the new size. */
4181 rx_ring->lbq_buf_size = lbq_buf_len;
4182 }
4183
4184 status = ql_adapter_up(qdev);
4185 if (status)
4186 goto error;
4187
4188 return status;
4189error:
Joe Perchesae9540f72010-02-09 11:49:52 +00004190 netif_alert(qdev, ifup, qdev->ndev,
4191 "Driver up/down cycle failed, closing device.\n");
Ron Mercer7c734352009-10-19 03:32:19 +00004192 set_bit(QL_ADAPTER_UP, &qdev->flags);
4193 dev_close(qdev->ndev);
4194 return status;
4195}
4196
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004197static int qlge_change_mtu(struct net_device *ndev, int new_mtu)
4198{
4199 struct ql_adapter *qdev = netdev_priv(ndev);
Ron Mercer7c734352009-10-19 03:32:19 +00004200 int status;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004201
4202 if (ndev->mtu == 1500 && new_mtu == 9000) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004203 netif_err(qdev, ifup, qdev->ndev, "Changing to jumbo MTU.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004204 } else if (ndev->mtu == 9000 && new_mtu == 1500) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004205 netif_err(qdev, ifup, qdev->ndev, "Changing to normal MTU.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004206 } else
4207 return -EINVAL;
Ron Mercer7c734352009-10-19 03:32:19 +00004208
4209 queue_delayed_work(qdev->workqueue,
4210 &qdev->mpi_port_cfg_work, 3*HZ);
4211
Breno Leitao746079d2010-02-04 10:11:19 +00004212 ndev->mtu = new_mtu;
4213
Ron Mercer7c734352009-10-19 03:32:19 +00004214 if (!netif_running(qdev->ndev)) {
Ron Mercer7c734352009-10-19 03:32:19 +00004215 return 0;
4216 }
4217
Ron Mercer7c734352009-10-19 03:32:19 +00004218 status = ql_change_rx_buffers(qdev);
4219 if (status) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004220 netif_err(qdev, ifup, qdev->ndev,
4221 "Changing MTU failed.\n");
Ron Mercer7c734352009-10-19 03:32:19 +00004222 }
4223
4224 return status;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004225}
4226
4227static struct net_device_stats *qlge_get_stats(struct net_device
4228 *ndev)
4229{
Ron Mercer885ee392009-11-03 13:49:31 +00004230 struct ql_adapter *qdev = netdev_priv(ndev);
4231 struct rx_ring *rx_ring = &qdev->rx_ring[0];
4232 struct tx_ring *tx_ring = &qdev->tx_ring[0];
4233 unsigned long pkts, mcast, dropped, errors, bytes;
4234 int i;
4235
4236 /* Get RX stats. */
4237 pkts = mcast = dropped = errors = bytes = 0;
4238 for (i = 0; i < qdev->rss_ring_count; i++, rx_ring++) {
4239 pkts += rx_ring->rx_packets;
4240 bytes += rx_ring->rx_bytes;
4241 dropped += rx_ring->rx_dropped;
4242 errors += rx_ring->rx_errors;
4243 mcast += rx_ring->rx_multicast;
4244 }
4245 ndev->stats.rx_packets = pkts;
4246 ndev->stats.rx_bytes = bytes;
4247 ndev->stats.rx_dropped = dropped;
4248 ndev->stats.rx_errors = errors;
4249 ndev->stats.multicast = mcast;
4250
4251 /* Get TX stats. */
4252 pkts = errors = bytes = 0;
4253 for (i = 0; i < qdev->tx_ring_count; i++, tx_ring++) {
4254 pkts += tx_ring->tx_packets;
4255 bytes += tx_ring->tx_bytes;
4256 errors += tx_ring->tx_errors;
4257 }
4258 ndev->stats.tx_packets = pkts;
4259 ndev->stats.tx_bytes = bytes;
4260 ndev->stats.tx_errors = errors;
Ajit Khapardebcc90f52009-10-07 02:46:09 +00004261 return &ndev->stats;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004262}
4263
stephen hemmingerac409212010-10-21 07:50:54 +00004264static void qlge_set_multicast_list(struct net_device *ndev)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004265{
Joe Perchesb16fed02010-11-15 11:12:28 +00004266 struct ql_adapter *qdev = netdev_priv(ndev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004267 struct netdev_hw_addr *ha;
Ron Mercercc288f52009-02-23 10:42:14 +00004268 int i, status;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004269
Ron Mercercc288f52009-02-23 10:42:14 +00004270 status = ql_sem_spinlock(qdev, SEM_RT_IDX_MASK);
4271 if (status)
4272 return;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004273 /*
4274 * Set or clear promiscuous mode if a
4275 * transition is taking place.
4276 */
4277 if (ndev->flags & IFF_PROMISC) {
4278 if (!test_bit(QL_PROMISCUOUS, &qdev->flags)) {
4279 if (ql_set_routing_reg
4280 (qdev, RT_IDX_PROMISCUOUS_SLOT, RT_IDX_VALID, 1)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004281 netif_err(qdev, hw, qdev->ndev,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004282 "Failed to set promiscuous mode.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004283 } else {
4284 set_bit(QL_PROMISCUOUS, &qdev->flags);
4285 }
4286 }
4287 } else {
4288 if (test_bit(QL_PROMISCUOUS, &qdev->flags)) {
4289 if (ql_set_routing_reg
4290 (qdev, RT_IDX_PROMISCUOUS_SLOT, RT_IDX_VALID, 0)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004291 netif_err(qdev, hw, qdev->ndev,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004292 "Failed to clear promiscuous mode.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004293 } else {
4294 clear_bit(QL_PROMISCUOUS, &qdev->flags);
4295 }
4296 }
4297 }
4298
4299 /*
4300 * Set or clear all multicast mode if a
4301 * transition is taking place.
4302 */
4303 if ((ndev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00004304 (netdev_mc_count(ndev) > MAX_MULTICAST_ENTRIES)) {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004305 if (!test_bit(QL_ALLMULTI, &qdev->flags)) {
4306 if (ql_set_routing_reg
4307 (qdev, RT_IDX_ALLMULTI_SLOT, RT_IDX_MCAST, 1)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004308 netif_err(qdev, hw, qdev->ndev,
4309 "Failed to set all-multi mode.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004310 } else {
4311 set_bit(QL_ALLMULTI, &qdev->flags);
4312 }
4313 }
4314 } else {
4315 if (test_bit(QL_ALLMULTI, &qdev->flags)) {
4316 if (ql_set_routing_reg
4317 (qdev, RT_IDX_ALLMULTI_SLOT, RT_IDX_MCAST, 0)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004318 netif_err(qdev, hw, qdev->ndev,
4319 "Failed to clear all-multi mode.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004320 } else {
4321 clear_bit(QL_ALLMULTI, &qdev->flags);
4322 }
4323 }
4324 }
4325
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00004326 if (!netdev_mc_empty(ndev)) {
Ron Mercercc288f52009-02-23 10:42:14 +00004327 status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
4328 if (status)
4329 goto exit;
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00004330 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00004331 netdev_for_each_mc_addr(ha, ndev) {
4332 if (ql_set_mac_addr_reg(qdev, (u8 *) ha->addr,
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004333 MAC_ADDR_TYPE_MULTI_MAC, i)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004334 netif_err(qdev, hw, qdev->ndev,
4335 "Failed to loadmulticast address.\n");
Ron Mercercc288f52009-02-23 10:42:14 +00004336 ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004337 goto exit;
4338 }
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00004339 i++;
4340 }
Ron Mercercc288f52009-02-23 10:42:14 +00004341 ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004342 if (ql_set_routing_reg
4343 (qdev, RT_IDX_MCAST_MATCH_SLOT, RT_IDX_MCAST_MATCH, 1)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004344 netif_err(qdev, hw, qdev->ndev,
4345 "Failed to set multicast match mode.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004346 } else {
4347 set_bit(QL_ALLMULTI, &qdev->flags);
4348 }
4349 }
4350exit:
Ron Mercer8587ea32009-02-23 10:42:15 +00004351 ql_sem_unlock(qdev, SEM_RT_IDX_MASK);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004352}
4353
4354static int qlge_set_mac_address(struct net_device *ndev, void *p)
4355{
Joe Perchesb16fed02010-11-15 11:12:28 +00004356 struct ql_adapter *qdev = netdev_priv(ndev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004357 struct sockaddr *addr = p;
Ron Mercercc288f52009-02-23 10:42:14 +00004358 int status;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004359
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004360 if (!is_valid_ether_addr(addr->sa_data))
4361 return -EADDRNOTAVAIL;
4362 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
Ron Mercer801e9092010-02-17 06:41:22 +00004363 /* Update local copy of current mac address. */
4364 memcpy(qdev->current_mac_addr, ndev->dev_addr, ndev->addr_len);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004365
Ron Mercercc288f52009-02-23 10:42:14 +00004366 status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
4367 if (status)
4368 return status;
Ron Mercercc288f52009-02-23 10:42:14 +00004369 status = ql_set_mac_addr_reg(qdev, (u8 *) ndev->dev_addr,
4370 MAC_ADDR_TYPE_CAM_MAC, qdev->func * MAX_CQ);
Ron Mercercc288f52009-02-23 10:42:14 +00004371 if (status)
Joe Perchesae9540f72010-02-09 11:49:52 +00004372 netif_err(qdev, hw, qdev->ndev, "Failed to load MAC address.\n");
Ron Mercercc288f52009-02-23 10:42:14 +00004373 ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
4374 return status;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004375}
4376
4377static void qlge_tx_timeout(struct net_device *ndev)
4378{
Joe Perchesb16fed02010-11-15 11:12:28 +00004379 struct ql_adapter *qdev = netdev_priv(ndev);
Ron Mercer6497b602009-02-12 16:37:13 -08004380 ql_queue_asic_error(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004381}
4382
4383static void ql_asic_reset_work(struct work_struct *work)
4384{
4385 struct ql_adapter *qdev =
4386 container_of(work, struct ql_adapter, asic_reset_work.work);
Ron Mercerdb988122009-03-09 10:59:17 +00004387 int status;
Ron Mercerf2c0d8d2009-09-29 08:39:24 +00004388 rtnl_lock();
Ron Mercerdb988122009-03-09 10:59:17 +00004389 status = ql_adapter_down(qdev);
4390 if (status)
4391 goto error;
4392
4393 status = ql_adapter_up(qdev);
4394 if (status)
4395 goto error;
Ron Mercer2cd6dba2009-10-08 09:54:42 +00004396
4397 /* Restore rx mode. */
4398 clear_bit(QL_ALLMULTI, &qdev->flags);
4399 clear_bit(QL_PROMISCUOUS, &qdev->flags);
4400 qlge_set_multicast_list(qdev->ndev);
4401
Ron Mercerf2c0d8d2009-09-29 08:39:24 +00004402 rtnl_unlock();
Ron Mercerdb988122009-03-09 10:59:17 +00004403 return;
4404error:
Joe Perchesae9540f72010-02-09 11:49:52 +00004405 netif_alert(qdev, ifup, qdev->ndev,
4406 "Driver up/down cycle failed, closing device\n");
Ron Mercerf2c0d8d2009-09-29 08:39:24 +00004407
Ron Mercerdb988122009-03-09 10:59:17 +00004408 set_bit(QL_ADAPTER_UP, &qdev->flags);
4409 dev_close(qdev->ndev);
4410 rtnl_unlock();
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004411}
4412
stephen hemmingeref9c7ab2011-04-14 05:51:52 +00004413static const struct nic_operations qla8012_nic_ops = {
Ron Mercerb0c2aad2009-02-26 10:08:35 +00004414 .get_flash = ql_get_8012_flash_params,
4415 .port_initialize = ql_8012_port_initialize,
4416};
4417
stephen hemmingeref9c7ab2011-04-14 05:51:52 +00004418static const struct nic_operations qla8000_nic_ops = {
Ron Mercercdca8d02009-03-02 08:07:31 +00004419 .get_flash = ql_get_8000_flash_params,
4420 .port_initialize = ql_8000_port_initialize,
4421};
4422
Ron Mercere4552f52009-06-09 05:39:32 +00004423/* Find the pcie function number for the other NIC
4424 * on this chip. Since both NIC functions share a
4425 * common firmware we have the lowest enabled function
4426 * do any common work. Examples would be resetting
4427 * after a fatal firmware error, or doing a firmware
4428 * coredump.
4429 */
4430static int ql_get_alt_pcie_func(struct ql_adapter *qdev)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004431{
Ron Mercere4552f52009-06-09 05:39:32 +00004432 int status = 0;
4433 u32 temp;
4434 u32 nic_func1, nic_func2;
4435
4436 status = ql_read_mpi_reg(qdev, MPI_TEST_FUNC_PORT_CFG,
4437 &temp);
4438 if (status)
4439 return status;
4440
4441 nic_func1 = ((temp >> MPI_TEST_NIC1_FUNC_SHIFT) &
4442 MPI_TEST_NIC_FUNC_MASK);
4443 nic_func2 = ((temp >> MPI_TEST_NIC2_FUNC_SHIFT) &
4444 MPI_TEST_NIC_FUNC_MASK);
4445
4446 if (qdev->func == nic_func1)
4447 qdev->alt_func = nic_func2;
4448 else if (qdev->func == nic_func2)
4449 qdev->alt_func = nic_func1;
4450 else
4451 status = -EIO;
4452
4453 return status;
4454}
4455
4456static int ql_get_board_info(struct ql_adapter *qdev)
4457{
4458 int status;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004459 qdev->func =
4460 (ql_read32(qdev, STS) & STS_FUNC_ID_MASK) >> STS_FUNC_ID_SHIFT;
Ron Mercere4552f52009-06-09 05:39:32 +00004461 if (qdev->func > 3)
4462 return -EIO;
4463
4464 status = ql_get_alt_pcie_func(qdev);
4465 if (status)
4466 return status;
4467
4468 qdev->port = (qdev->func < qdev->alt_func) ? 0 : 1;
4469 if (qdev->port) {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004470 qdev->xg_sem_mask = SEM_XGMAC1_MASK;
4471 qdev->port_link_up = STS_PL1;
4472 qdev->port_init = STS_PI1;
4473 qdev->mailbox_in = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC2_MBI;
4474 qdev->mailbox_out = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC2_MBO;
4475 } else {
4476 qdev->xg_sem_mask = SEM_XGMAC0_MASK;
4477 qdev->port_link_up = STS_PL0;
4478 qdev->port_init = STS_PI0;
4479 qdev->mailbox_in = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC0_MBI;
4480 qdev->mailbox_out = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC0_MBO;
4481 }
4482 qdev->chip_rev_id = ql_read32(qdev, REV_ID);
Ron Mercerb0c2aad2009-02-26 10:08:35 +00004483 qdev->device_id = qdev->pdev->device;
4484 if (qdev->device_id == QLGE_DEVICE_ID_8012)
4485 qdev->nic_ops = &qla8012_nic_ops;
Ron Mercercdca8d02009-03-02 08:07:31 +00004486 else if (qdev->device_id == QLGE_DEVICE_ID_8000)
4487 qdev->nic_ops = &qla8000_nic_ops;
Ron Mercere4552f52009-06-09 05:39:32 +00004488 return status;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004489}
4490
4491static void ql_release_all(struct pci_dev *pdev)
4492{
4493 struct net_device *ndev = pci_get_drvdata(pdev);
4494 struct ql_adapter *qdev = netdev_priv(ndev);
4495
4496 if (qdev->workqueue) {
4497 destroy_workqueue(qdev->workqueue);
4498 qdev->workqueue = NULL;
4499 }
Ron Mercer39aa8162009-08-27 11:02:11 +00004500
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004501 if (qdev->reg_base)
Stephen Hemminger8668ae92008-11-21 17:29:50 -08004502 iounmap(qdev->reg_base);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004503 if (qdev->doorbell_area)
4504 iounmap(qdev->doorbell_area);
Ron Mercer8aae2602010-01-15 13:31:28 +00004505 vfree(qdev->mpi_coredump);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004506 pci_release_regions(pdev);
4507 pci_set_drvdata(pdev, NULL);
4508}
4509
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004510static int ql_init_device(struct pci_dev *pdev, struct net_device *ndev,
4511 int cards_found)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004512{
4513 struct ql_adapter *qdev = netdev_priv(ndev);
Ron Mercer1d1023d2009-10-10 09:35:03 +00004514 int err = 0;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004515
Ron Mercere3324712009-07-02 06:06:13 +00004516 memset((void *)qdev, 0, sizeof(*qdev));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004517 err = pci_enable_device(pdev);
4518 if (err) {
4519 dev_err(&pdev->dev, "PCI device enable failed.\n");
4520 return err;
4521 }
4522
Ron Mercerebd6e772009-09-29 08:39:25 +00004523 qdev->ndev = ndev;
4524 qdev->pdev = pdev;
4525 pci_set_drvdata(pdev, ndev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004526
Ron Mercerbc9167f2009-10-10 09:35:04 +00004527 /* Set PCIe read request size */
4528 err = pcie_set_readrq(pdev, 4096);
4529 if (err) {
4530 dev_err(&pdev->dev, "Set readrq failed.\n");
Breno Leitao4f9a91c2010-01-25 15:46:58 -08004531 goto err_out1;
Ron Mercerbc9167f2009-10-10 09:35:04 +00004532 }
4533
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004534 err = pci_request_regions(pdev, DRV_NAME);
4535 if (err) {
4536 dev_err(&pdev->dev, "PCI region request failed.\n");
Ron Mercerebd6e772009-09-29 08:39:25 +00004537 return err;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004538 }
4539
4540 pci_set_master(pdev);
Yang Hongyang6a355282009-04-06 19:01:13 -07004541 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004542 set_bit(QL_DMA64, &qdev->flags);
Yang Hongyang6a355282009-04-06 19:01:13 -07004543 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004544 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07004545 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004546 if (!err)
Yang Hongyang284901a2009-04-06 19:01:15 -07004547 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004548 }
4549
4550 if (err) {
4551 dev_err(&pdev->dev, "No usable DMA configuration.\n");
Breno Leitao4f9a91c2010-01-25 15:46:58 -08004552 goto err_out2;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004553 }
4554
Ron Mercer73475332009-11-06 07:44:58 +00004555 /* Set PCIe reset type for EEH to fundamental. */
4556 pdev->needs_freset = 1;
Ron Mercer6d190c62009-10-28 08:39:20 +00004557 pci_save_state(pdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004558 qdev->reg_base =
4559 ioremap_nocache(pci_resource_start(pdev, 1),
4560 pci_resource_len(pdev, 1));
4561 if (!qdev->reg_base) {
4562 dev_err(&pdev->dev, "Register mapping failed.\n");
4563 err = -ENOMEM;
Breno Leitao4f9a91c2010-01-25 15:46:58 -08004564 goto err_out2;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004565 }
4566
4567 qdev->doorbell_area_size = pci_resource_len(pdev, 3);
4568 qdev->doorbell_area =
4569 ioremap_nocache(pci_resource_start(pdev, 3),
4570 pci_resource_len(pdev, 3));
4571 if (!qdev->doorbell_area) {
4572 dev_err(&pdev->dev, "Doorbell register mapping failed.\n");
4573 err = -ENOMEM;
Breno Leitao4f9a91c2010-01-25 15:46:58 -08004574 goto err_out2;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004575 }
4576
Ron Mercere4552f52009-06-09 05:39:32 +00004577 err = ql_get_board_info(qdev);
4578 if (err) {
4579 dev_err(&pdev->dev, "Register access failed.\n");
4580 err = -EIO;
Breno Leitao4f9a91c2010-01-25 15:46:58 -08004581 goto err_out2;
Ron Mercere4552f52009-06-09 05:39:32 +00004582 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004583 qdev->msg_enable = netif_msg_init(debug, default_msg);
4584 spin_lock_init(&qdev->hw_lock);
4585 spin_lock_init(&qdev->stats_lock);
4586
Ron Mercer8aae2602010-01-15 13:31:28 +00004587 if (qlge_mpi_coredump) {
4588 qdev->mpi_coredump =
4589 vmalloc(sizeof(struct ql_mpi_coredump));
4590 if (qdev->mpi_coredump == NULL) {
Ron Mercer8aae2602010-01-15 13:31:28 +00004591 err = -ENOMEM;
Stephen Rothwellce96bc82010-01-28 06:13:13 -08004592 goto err_out2;
Ron Mercer8aae2602010-01-15 13:31:28 +00004593 }
Ron Mercerd5c1da52010-01-15 13:31:34 +00004594 if (qlge_force_coredump)
4595 set_bit(QL_FRC_COREDUMP, &qdev->flags);
Ron Mercer8aae2602010-01-15 13:31:28 +00004596 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004597 /* make sure the EEPROM is good */
Ron Mercerb0c2aad2009-02-26 10:08:35 +00004598 err = qdev->nic_ops->get_flash(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004599 if (err) {
4600 dev_err(&pdev->dev, "Invalid FLASH.\n");
Breno Leitao4f9a91c2010-01-25 15:46:58 -08004601 goto err_out2;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004602 }
4603
Ron Mercer801e9092010-02-17 06:41:22 +00004604 /* Keep local copy of current mac address. */
4605 memcpy(qdev->current_mac_addr, ndev->dev_addr, ndev->addr_len);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004606
4607 /* Set up the default ring sizes. */
4608 qdev->tx_ring_size = NUM_TX_RING_ENTRIES;
4609 qdev->rx_ring_size = NUM_RX_RING_ENTRIES;
4610
4611 /* Set up the coalescing parameters. */
4612 qdev->rx_coalesce_usecs = DFLT_COALESCE_WAIT;
4613 qdev->tx_coalesce_usecs = DFLT_COALESCE_WAIT;
4614 qdev->rx_max_coalesced_frames = DFLT_INTER_FRAME_WAIT;
4615 qdev->tx_max_coalesced_frames = DFLT_INTER_FRAME_WAIT;
4616
4617 /*
4618 * Set up the operating parameters.
4619 */
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004620 qdev->workqueue = create_singlethread_workqueue(ndev->name);
4621 INIT_DELAYED_WORK(&qdev->asic_reset_work, ql_asic_reset_work);
4622 INIT_DELAYED_WORK(&qdev->mpi_reset_work, ql_mpi_reset_work);
4623 INIT_DELAYED_WORK(&qdev->mpi_work, ql_mpi_work);
Ron Mercerbcc2cb32009-03-02 08:07:32 +00004624 INIT_DELAYED_WORK(&qdev->mpi_port_cfg_work, ql_mpi_port_cfg_work);
Ron Mercer2ee1e272009-03-03 12:10:33 +00004625 INIT_DELAYED_WORK(&qdev->mpi_idc_work, ql_mpi_idc_work);
Ron Mercer8aae2602010-01-15 13:31:28 +00004626 INIT_DELAYED_WORK(&qdev->mpi_core_to_log, ql_mpi_core_to_log);
Ron Mercerbcc2cb32009-03-02 08:07:32 +00004627 init_completion(&qdev->ide_completion);
Ron Mercer4d7b6b52010-12-11 11:06:50 +00004628 mutex_init(&qdev->mpi_mutex);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004629
4630 if (!cards_found) {
4631 dev_info(&pdev->dev, "%s\n", DRV_STRING);
4632 dev_info(&pdev->dev, "Driver name: %s, Version: %s.\n",
4633 DRV_NAME, DRV_VERSION);
4634 }
4635 return 0;
Breno Leitao4f9a91c2010-01-25 15:46:58 -08004636err_out2:
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004637 ql_release_all(pdev);
Breno Leitao4f9a91c2010-01-25 15:46:58 -08004638err_out1:
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004639 pci_disable_device(pdev);
4640 return err;
4641}
4642
Stephen Hemminger25ed7842008-11-21 17:29:16 -08004643static const struct net_device_ops qlge_netdev_ops = {
4644 .ndo_open = qlge_open,
4645 .ndo_stop = qlge_close,
4646 .ndo_start_xmit = qlge_send,
4647 .ndo_change_mtu = qlge_change_mtu,
4648 .ndo_get_stats = qlge_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00004649 .ndo_set_rx_mode = qlge_set_multicast_list,
Stephen Hemminger25ed7842008-11-21 17:29:16 -08004650 .ndo_set_mac_address = qlge_set_mac_address,
4651 .ndo_validate_addr = eth_validate_addr,
4652 .ndo_tx_timeout = qlge_tx_timeout,
Jiri Pirko18c49b92011-07-21 03:24:11 +00004653 .ndo_fix_features = qlge_fix_features,
4654 .ndo_set_features = qlge_set_features,
Ron Mercer01e6b952009-10-30 12:13:34 +00004655 .ndo_vlan_rx_add_vid = qlge_vlan_rx_add_vid,
4656 .ndo_vlan_rx_kill_vid = qlge_vlan_rx_kill_vid,
Stephen Hemminger25ed7842008-11-21 17:29:16 -08004657};
4658
Ron Mercer15c052f2010-02-04 13:32:46 -08004659static void ql_timer(unsigned long data)
4660{
4661 struct ql_adapter *qdev = (struct ql_adapter *)data;
4662 u32 var = 0;
4663
4664 var = ql_read32(qdev, STS);
4665 if (pci_channel_offline(qdev->pdev)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004666 netif_err(qdev, ifup, qdev->ndev, "EEH STS = 0x%.08x.\n", var);
Ron Mercer15c052f2010-02-04 13:32:46 -08004667 return;
4668 }
4669
Breno Leitao72046d82010-07-01 03:00:17 +00004670 mod_timer(&qdev->timer, jiffies + (5*HZ));
Ron Mercer15c052f2010-02-04 13:32:46 -08004671}
4672
Bill Pemberton5d8e8722012-12-03 09:23:27 -05004673static int qlge_probe(struct pci_dev *pdev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004674 const struct pci_device_id *pci_entry)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004675{
4676 struct net_device *ndev = NULL;
4677 struct ql_adapter *qdev = NULL;
4678 static int cards_found = 0;
4679 int err = 0;
4680
Ron Mercer1e213302009-03-09 10:59:21 +00004681 ndev = alloc_etherdev_mq(sizeof(struct ql_adapter),
Yuval Mintz9eb87382012-07-01 03:18:53 +00004682 min(MAX_CPUS, netif_get_num_default_rss_queues()));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004683 if (!ndev)
4684 return -ENOMEM;
4685
4686 err = ql_init_device(pdev, ndev, cards_found);
4687 if (err < 0) {
4688 free_netdev(ndev);
4689 return err;
4690 }
4691
4692 qdev = netdev_priv(ndev);
4693 SET_NETDEV_DEV(ndev, &pdev->dev);
Michał Mirosław88230fd2011-04-18 13:31:21 +00004694 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
Amerigo Wangf7e9e232013-01-10 22:52:54 +00004695 NETIF_F_TSO | NETIF_F_TSO_ECN |
Michał Mirosław88230fd2011-04-18 13:31:21 +00004696 NETIF_F_HW_VLAN_TX | NETIF_F_RXCSUM;
4697 ndev->features = ndev->hw_features |
4698 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
brenohl@br.ibm.com1a0150a92012-07-27 08:54:52 +00004699 ndev->vlan_features = ndev->hw_features;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004700
4701 if (test_bit(QL_DMA64, &qdev->flags))
4702 ndev->features |= NETIF_F_HIGHDMA;
4703
4704 /*
4705 * Set up net_device structure.
4706 */
4707 ndev->tx_queue_len = qdev->tx_ring_size;
4708 ndev->irq = pdev->irq;
Stephen Hemminger25ed7842008-11-21 17:29:16 -08004709
4710 ndev->netdev_ops = &qlge_netdev_ops;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004711 SET_ETHTOOL_OPS(ndev, &qlge_ethtool_ops);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004712 ndev->watchdog_timeo = 10 * HZ;
Stephen Hemminger25ed7842008-11-21 17:29:16 -08004713
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004714 err = register_netdev(ndev);
4715 if (err) {
4716 dev_err(&pdev->dev, "net device registration failed.\n");
4717 ql_release_all(pdev);
4718 pci_disable_device(pdev);
4719 return err;
4720 }
Ron Mercer15c052f2010-02-04 13:32:46 -08004721 /* Start up the timer to trigger EEH if
4722 * the bus goes dead
4723 */
4724 init_timer_deferrable(&qdev->timer);
4725 qdev->timer.data = (unsigned long)qdev;
4726 qdev->timer.function = ql_timer;
4727 qdev->timer.expires = jiffies + (5*HZ);
4728 add_timer(&qdev->timer);
Ron Mercer6a473302009-07-02 06:06:12 +00004729 ql_link_off(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004730 ql_display_dev_info(ndev);
Ron Mercer9dfbbaa2009-10-30 12:13:33 +00004731 atomic_set(&qdev->lb_count, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004732 cards_found++;
4733 return 0;
4734}
4735
Ron Mercer9dfbbaa2009-10-30 12:13:33 +00004736netdev_tx_t ql_lb_send(struct sk_buff *skb, struct net_device *ndev)
4737{
4738 return qlge_send(skb, ndev);
4739}
4740
4741int ql_clean_lb_rx_ring(struct rx_ring *rx_ring, int budget)
4742{
4743 return ql_clean_inbound_rx_ring(rx_ring, budget);
4744}
4745
Bill Pemberton5d8e8722012-12-03 09:23:27 -05004746static void qlge_remove(struct pci_dev *pdev)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004747{
4748 struct net_device *ndev = pci_get_drvdata(pdev);
Ron Mercer15c052f2010-02-04 13:32:46 -08004749 struct ql_adapter *qdev = netdev_priv(ndev);
4750 del_timer_sync(&qdev->timer);
Breno Leitaoc5daddd2010-08-24 12:50:40 +00004751 ql_cancel_all_work_sync(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004752 unregister_netdev(ndev);
4753 ql_release_all(pdev);
4754 pci_disable_device(pdev);
4755 free_netdev(ndev);
4756}
4757
Ron Mercer6d190c62009-10-28 08:39:20 +00004758/* Clean up resources without touching hardware. */
4759static void ql_eeh_close(struct net_device *ndev)
4760{
4761 int i;
4762 struct ql_adapter *qdev = netdev_priv(ndev);
4763
4764 if (netif_carrier_ok(ndev)) {
4765 netif_carrier_off(ndev);
4766 netif_stop_queue(ndev);
4767 }
4768
Breno Leitao7ae80ab2010-07-01 03:00:18 +00004769 /* Disabling the timer */
4770 del_timer_sync(&qdev->timer);
Breno Leitaoc5daddd2010-08-24 12:50:40 +00004771 ql_cancel_all_work_sync(qdev);
Ron Mercer6d190c62009-10-28 08:39:20 +00004772
4773 for (i = 0; i < qdev->rss_ring_count; i++)
4774 netif_napi_del(&qdev->rx_ring[i].napi);
4775
4776 clear_bit(QL_ADAPTER_UP, &qdev->flags);
4777 ql_tx_ring_clean(qdev);
4778 ql_free_rx_buffers(qdev);
4779 ql_release_adapter_resources(qdev);
4780}
4781
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004782/*
4783 * This callback is called by the PCI subsystem whenever
4784 * a PCI bus error is detected.
4785 */
4786static pci_ers_result_t qlge_io_error_detected(struct pci_dev *pdev,
4787 enum pci_channel_state state)
4788{
4789 struct net_device *ndev = pci_get_drvdata(pdev);
Ron Mercer4bbd1a12010-02-03 07:24:12 +00004790 struct ql_adapter *qdev = netdev_priv(ndev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004791
Ron Mercer6d190c62009-10-28 08:39:20 +00004792 switch (state) {
4793 case pci_channel_io_normal:
4794 return PCI_ERS_RESULT_CAN_RECOVER;
4795 case pci_channel_io_frozen:
4796 netif_device_detach(ndev);
4797 if (netif_running(ndev))
4798 ql_eeh_close(ndev);
4799 pci_disable_device(pdev);
4800 return PCI_ERS_RESULT_NEED_RESET;
4801 case pci_channel_io_perm_failure:
4802 dev_err(&pdev->dev,
4803 "%s: pci_channel_io_perm_failure.\n", __func__);
Ron Mercer4bbd1a12010-02-03 07:24:12 +00004804 ql_eeh_close(ndev);
4805 set_bit(QL_EEH_FATAL, &qdev->flags);
Dean Nelsonfbc663c2009-07-31 09:13:48 +00004806 return PCI_ERS_RESULT_DISCONNECT;
Ron Mercer6d190c62009-10-28 08:39:20 +00004807 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004808
4809 /* Request a slot reset. */
4810 return PCI_ERS_RESULT_NEED_RESET;
4811}
4812
4813/*
4814 * This callback is called after the PCI buss has been reset.
4815 * Basically, this tries to restart the card from scratch.
4816 * This is a shortened version of the device probe/discovery code,
4817 * it resembles the first-half of the () routine.
4818 */
4819static pci_ers_result_t qlge_io_slot_reset(struct pci_dev *pdev)
4820{
4821 struct net_device *ndev = pci_get_drvdata(pdev);
4822 struct ql_adapter *qdev = netdev_priv(ndev);
4823
Ron Mercer6d190c62009-10-28 08:39:20 +00004824 pdev->error_state = pci_channel_io_normal;
4825
4826 pci_restore_state(pdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004827 if (pci_enable_device(pdev)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004828 netif_err(qdev, ifup, qdev->ndev,
4829 "Cannot re-enable PCI device after reset.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004830 return PCI_ERS_RESULT_DISCONNECT;
4831 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004832 pci_set_master(pdev);
Ron Mercera112fd42010-02-03 07:24:11 +00004833
4834 if (ql_adapter_reset(qdev)) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004835 netif_err(qdev, drv, qdev->ndev, "reset FAILED!\n");
Ron Mercer4bbd1a12010-02-03 07:24:12 +00004836 set_bit(QL_EEH_FATAL, &qdev->flags);
Ron Mercera112fd42010-02-03 07:24:11 +00004837 return PCI_ERS_RESULT_DISCONNECT;
4838 }
4839
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004840 return PCI_ERS_RESULT_RECOVERED;
4841}
4842
4843static void qlge_io_resume(struct pci_dev *pdev)
4844{
4845 struct net_device *ndev = pci_get_drvdata(pdev);
4846 struct ql_adapter *qdev = netdev_priv(ndev);
Ron Mercer6d190c62009-10-28 08:39:20 +00004847 int err = 0;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004848
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004849 if (netif_running(ndev)) {
Ron Mercer6d190c62009-10-28 08:39:20 +00004850 err = qlge_open(ndev);
4851 if (err) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004852 netif_err(qdev, ifup, qdev->ndev,
4853 "Device initialization failed after reset.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004854 return;
4855 }
Ron Mercer6d190c62009-10-28 08:39:20 +00004856 } else {
Joe Perchesae9540f72010-02-09 11:49:52 +00004857 netif_err(qdev, ifup, qdev->ndev,
4858 "Device was not running prior to EEH.\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004859 }
Breno Leitao72046d82010-07-01 03:00:17 +00004860 mod_timer(&qdev->timer, jiffies + (5*HZ));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004861 netif_device_attach(ndev);
4862}
4863
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004864static const struct pci_error_handlers qlge_err_handler = {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004865 .error_detected = qlge_io_error_detected,
4866 .slot_reset = qlge_io_slot_reset,
4867 .resume = qlge_io_resume,
4868};
4869
4870static int qlge_suspend(struct pci_dev *pdev, pm_message_t state)
4871{
4872 struct net_device *ndev = pci_get_drvdata(pdev);
4873 struct ql_adapter *qdev = netdev_priv(ndev);
Ron Mercer6b318cb2009-03-09 10:59:26 +00004874 int err;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004875
4876 netif_device_detach(ndev);
Ron Mercer15c052f2010-02-04 13:32:46 -08004877 del_timer_sync(&qdev->timer);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004878
4879 if (netif_running(ndev)) {
4880 err = ql_adapter_down(qdev);
4881 if (!err)
4882 return err;
4883 }
4884
Ron Mercerbc083ce2009-10-21 11:07:40 +00004885 ql_wol(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004886 err = pci_save_state(pdev);
4887 if (err)
4888 return err;
4889
4890 pci_disable_device(pdev);
4891
4892 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4893
4894 return 0;
4895}
4896
David S. Miller04da2cf2008-09-19 16:14:24 -07004897#ifdef CONFIG_PM
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004898static int qlge_resume(struct pci_dev *pdev)
4899{
4900 struct net_device *ndev = pci_get_drvdata(pdev);
4901 struct ql_adapter *qdev = netdev_priv(ndev);
4902 int err;
4903
4904 pci_set_power_state(pdev, PCI_D0);
4905 pci_restore_state(pdev);
4906 err = pci_enable_device(pdev);
4907 if (err) {
Joe Perchesae9540f72010-02-09 11:49:52 +00004908 netif_err(qdev, ifup, qdev->ndev, "Cannot enable PCI device from suspend\n");
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004909 return err;
4910 }
4911 pci_set_master(pdev);
4912
4913 pci_enable_wake(pdev, PCI_D3hot, 0);
4914 pci_enable_wake(pdev, PCI_D3cold, 0);
4915
4916 if (netif_running(ndev)) {
4917 err = ql_adapter_up(qdev);
4918 if (err)
4919 return err;
4920 }
4921
Breno Leitao72046d82010-07-01 03:00:17 +00004922 mod_timer(&qdev->timer, jiffies + (5*HZ));
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004923 netif_device_attach(ndev);
4924
4925 return 0;
4926}
David S. Miller04da2cf2008-09-19 16:14:24 -07004927#endif /* CONFIG_PM */
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004928
4929static void qlge_shutdown(struct pci_dev *pdev)
4930{
4931 qlge_suspend(pdev, PMSG_SUSPEND);
4932}
4933
4934static struct pci_driver qlge_driver = {
4935 .name = DRV_NAME,
4936 .id_table = qlge_pci_tbl,
4937 .probe = qlge_probe,
Bill Pemberton5d8e8722012-12-03 09:23:27 -05004938 .remove = qlge_remove,
Ron Mercerc4e84bd2008-09-18 11:56:28 -04004939#ifdef CONFIG_PM
4940 .suspend = qlge_suspend,
4941 .resume = qlge_resume,
4942#endif
4943 .shutdown = qlge_shutdown,
4944 .err_handler = &qlge_err_handler
4945};
4946
4947static int __init qlge_init_module(void)
4948{
4949 return pci_register_driver(&qlge_driver);
4950}
4951
4952static void __exit qlge_exit(void)
4953{
4954 pci_unregister_driver(&qlge_driver);
4955}
4956
4957module_init(qlge_init_module);
4958module_exit(qlge_exit);