blob: 4ab6e72ea95c3be59075933cd988710928422eae [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>
10#include <linux/types.h>
11#include <linux/module.h>
12#include <linux/list.h>
13#include <linux/pci.h>
14#include <linux/dma-mapping.h>
15#include <linux/pagemap.h>
16#include <linux/sched.h>
17#include <linux/slab.h>
18#include <linux/dmapool.h>
19#include <linux/mempool.h>
20#include <linux/spinlock.h>
21#include <linux/kthread.h>
22#include <linux/interrupt.h>
23#include <linux/errno.h>
24#include <linux/ioport.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/ipv6.h>
28#include <net/ipv6.h>
29#include <linux/tcp.h>
30#include <linux/udp.h>
31#include <linux/if_arp.h>
32#include <linux/if_ether.h>
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/ethtool.h>
36#include <linux/skbuff.h>
37#include <linux/rtnetlink.h>
38#include <linux/if_vlan.h>
Ron Mercerc4e84bd2008-09-18 11:56:28 -040039#include <linux/delay.h>
40#include <linux/mm.h>
41#include <linux/vmalloc.h>
Kamalesh Babulalb7c6bfb2008-10-13 18:41:01 -070042#include <net/ip6_checksum.h>
Ron Mercerc4e84bd2008-09-18 11:56:28 -040043
44#include "qlge.h"
45
46char qlge_driver_name[] = DRV_NAME;
47const char qlge_driver_version[] = DRV_VERSION;
48
49MODULE_AUTHOR("Ron Mercer <ron.mercer@qlogic.com>");
50MODULE_DESCRIPTION(DRV_STRING " ");
51MODULE_LICENSE("GPL");
52MODULE_VERSION(DRV_VERSION);
53
54static const u32 default_msg =
55 NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK |
56/* NETIF_MSG_TIMER | */
57 NETIF_MSG_IFDOWN |
58 NETIF_MSG_IFUP |
59 NETIF_MSG_RX_ERR |
60 NETIF_MSG_TX_ERR |
61 NETIF_MSG_TX_QUEUED |
62 NETIF_MSG_INTR | NETIF_MSG_TX_DONE | NETIF_MSG_RX_STATUS |
63/* NETIF_MSG_PKTDATA | */
64 NETIF_MSG_HW | NETIF_MSG_WOL | 0;
65
66static int debug = 0x00007fff; /* defaults above */
67module_param(debug, int, 0);
68MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
69
70#define MSIX_IRQ 0
71#define MSI_IRQ 1
72#define LEG_IRQ 2
73static int irq_type = MSIX_IRQ;
74module_param(irq_type, int, MSIX_IRQ);
75MODULE_PARM_DESC(irq_type, "0 = MSI-X, 1 = MSI, 2 = Legacy.");
76
77static struct pci_device_id qlge_pci_tbl[] __devinitdata = {
78 {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, QLGE_DEVICE_ID)},
Ron Mercerc4e84bd2008-09-18 11:56:28 -040079 /* required last entry */
80 {0,}
81};
82
83MODULE_DEVICE_TABLE(pci, qlge_pci_tbl);
84
85/* This hardware semaphore causes exclusive access to
86 * resources shared between the NIC driver, MPI firmware,
87 * FCOE firmware and the FC driver.
88 */
89static int ql_sem_trylock(struct ql_adapter *qdev, u32 sem_mask)
90{
91 u32 sem_bits = 0;
92
93 switch (sem_mask) {
94 case SEM_XGMAC0_MASK:
95 sem_bits = SEM_SET << SEM_XGMAC0_SHIFT;
96 break;
97 case SEM_XGMAC1_MASK:
98 sem_bits = SEM_SET << SEM_XGMAC1_SHIFT;
99 break;
100 case SEM_ICB_MASK:
101 sem_bits = SEM_SET << SEM_ICB_SHIFT;
102 break;
103 case SEM_MAC_ADDR_MASK:
104 sem_bits = SEM_SET << SEM_MAC_ADDR_SHIFT;
105 break;
106 case SEM_FLASH_MASK:
107 sem_bits = SEM_SET << SEM_FLASH_SHIFT;
108 break;
109 case SEM_PROBE_MASK:
110 sem_bits = SEM_SET << SEM_PROBE_SHIFT;
111 break;
112 case SEM_RT_IDX_MASK:
113 sem_bits = SEM_SET << SEM_RT_IDX_SHIFT;
114 break;
115 case SEM_PROC_REG_MASK:
116 sem_bits = SEM_SET << SEM_PROC_REG_SHIFT;
117 break;
118 default:
119 QPRINTK(qdev, PROBE, ALERT, "Bad Semaphore mask!.\n");
120 return -EINVAL;
121 }
122
123 ql_write32(qdev, SEM, sem_bits | sem_mask);
124 return !(ql_read32(qdev, SEM) & sem_bits);
125}
126
127int ql_sem_spinlock(struct ql_adapter *qdev, u32 sem_mask)
128{
Ron Mercer0857e9d2009-01-09 11:31:52 +0000129 unsigned int wait_count = 30;
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400130 do {
131 if (!ql_sem_trylock(qdev, sem_mask))
132 return 0;
Ron Mercer0857e9d2009-01-09 11:31:52 +0000133 udelay(100);
134 } while (--wait_count);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400135 return -ETIMEDOUT;
136}
137
138void ql_sem_unlock(struct ql_adapter *qdev, u32 sem_mask)
139{
140 ql_write32(qdev, SEM, sem_mask);
141 ql_read32(qdev, SEM); /* flush */
142}
143
144/* This function waits for a specific bit to come ready
145 * in a given register. It is used mostly by the initialize
146 * process, but is also used in kernel thread API such as
147 * netdev->set_multi, netdev->set_mac_address, netdev->vlan_rx_add_vid.
148 */
149int ql_wait_reg_rdy(struct ql_adapter *qdev, u32 reg, u32 bit, u32 err_bit)
150{
151 u32 temp;
152 int count = UDELAY_COUNT;
153
154 while (count) {
155 temp = ql_read32(qdev, reg);
156
157 /* check for errors */
158 if (temp & err_bit) {
159 QPRINTK(qdev, PROBE, ALERT,
160 "register 0x%.08x access error, value = 0x%.08x!.\n",
161 reg, temp);
162 return -EIO;
163 } else if (temp & bit)
164 return 0;
165 udelay(UDELAY_DELAY);
166 count--;
167 }
168 QPRINTK(qdev, PROBE, ALERT,
169 "Timed out waiting for reg %x to come ready.\n", reg);
170 return -ETIMEDOUT;
171}
172
173/* The CFG register is used to download TX and RX control blocks
174 * to the chip. This function waits for an operation to complete.
175 */
176static int ql_wait_cfg(struct ql_adapter *qdev, u32 bit)
177{
178 int count = UDELAY_COUNT;
179 u32 temp;
180
181 while (count) {
182 temp = ql_read32(qdev, CFG);
183 if (temp & CFG_LE)
184 return -EIO;
185 if (!(temp & bit))
186 return 0;
187 udelay(UDELAY_DELAY);
188 count--;
189 }
190 return -ETIMEDOUT;
191}
192
193
194/* Used to issue init control blocks to hw. Maps control block,
195 * sets address, triggers download, waits for completion.
196 */
197int ql_write_cfg(struct ql_adapter *qdev, void *ptr, int size, u32 bit,
198 u16 q_id)
199{
200 u64 map;
201 int status = 0;
202 int direction;
203 u32 mask;
204 u32 value;
205
206 direction =
207 (bit & (CFG_LRQ | CFG_LR | CFG_LCQ)) ? PCI_DMA_TODEVICE :
208 PCI_DMA_FROMDEVICE;
209
210 map = pci_map_single(qdev->pdev, ptr, size, direction);
211 if (pci_dma_mapping_error(qdev->pdev, map)) {
212 QPRINTK(qdev, IFUP, ERR, "Couldn't map DMA area.\n");
213 return -ENOMEM;
214 }
215
216 status = ql_wait_cfg(qdev, bit);
217 if (status) {
218 QPRINTK(qdev, IFUP, ERR,
219 "Timed out waiting for CFG to come ready.\n");
220 goto exit;
221 }
222
223 status = ql_sem_spinlock(qdev, SEM_ICB_MASK);
224 if (status)
225 goto exit;
226 ql_write32(qdev, ICB_L, (u32) map);
227 ql_write32(qdev, ICB_H, (u32) (map >> 32));
228 ql_sem_unlock(qdev, SEM_ICB_MASK); /* does flush too */
229
230 mask = CFG_Q_MASK | (bit << 16);
231 value = bit | (q_id << CFG_Q_SHIFT);
232 ql_write32(qdev, CFG, (mask | value));
233
234 /*
235 * Wait for the bit to clear after signaling hw.
236 */
237 status = ql_wait_cfg(qdev, bit);
238exit:
239 pci_unmap_single(qdev->pdev, map, size, direction);
240 return status;
241}
242
243/* Get a specific MAC address from the CAM. Used for debug and reg dump. */
244int ql_get_mac_addr_reg(struct ql_adapter *qdev, u32 type, u16 index,
245 u32 *value)
246{
247 u32 offset = 0;
248 int status;
249
250 status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
251 if (status)
252 return status;
253 switch (type) {
254 case MAC_ADDR_TYPE_MULTI_MAC:
255 case MAC_ADDR_TYPE_CAM_MAC:
256 {
257 status =
258 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800259 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400260 if (status)
261 goto exit;
262 ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
263 (index << MAC_ADDR_IDX_SHIFT) | /* index */
264 MAC_ADDR_ADR | MAC_ADDR_RS | type); /* type */
265 status =
266 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800267 MAC_ADDR_IDX, MAC_ADDR_MR, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400268 if (status)
269 goto exit;
270 *value++ = ql_read32(qdev, MAC_ADDR_DATA);
271 status =
272 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800273 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400274 if (status)
275 goto exit;
276 ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
277 (index << MAC_ADDR_IDX_SHIFT) | /* index */
278 MAC_ADDR_ADR | MAC_ADDR_RS | type); /* type */
279 status =
280 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800281 MAC_ADDR_IDX, MAC_ADDR_MR, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400282 if (status)
283 goto exit;
284 *value++ = ql_read32(qdev, MAC_ADDR_DATA);
285 if (type == MAC_ADDR_TYPE_CAM_MAC) {
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, MAC_ADDR_IDX,
Ron Mercer939678f2009-01-04 17:08:29 -0800296 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 }
301 break;
302 }
303 case MAC_ADDR_TYPE_VLAN:
304 case MAC_ADDR_TYPE_MULTI_FLTR:
305 default:
306 QPRINTK(qdev, IFUP, CRIT,
307 "Address type %d not yet supported.\n", type);
308 status = -EPERM;
309 }
310exit:
311 ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
312 return status;
313}
314
315/* Set up a MAC, multicast or VLAN address for the
316 * inbound frame matching.
317 */
318static int ql_set_mac_addr_reg(struct ql_adapter *qdev, u8 *addr, u32 type,
319 u16 index)
320{
321 u32 offset = 0;
322 int status = 0;
323
324 status = ql_sem_spinlock(qdev, SEM_MAC_ADDR_MASK);
325 if (status)
326 return status;
327 switch (type) {
328 case MAC_ADDR_TYPE_MULTI_MAC:
329 case MAC_ADDR_TYPE_CAM_MAC:
330 {
331 u32 cam_output;
332 u32 upper = (addr[0] << 8) | addr[1];
333 u32 lower =
334 (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) |
335 (addr[5]);
336
337 QPRINTK(qdev, IFUP, INFO,
Johannes Berg7c510e42008-10-27 17:47:26 -0700338 "Adding %s address %pM"
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400339 " at index %d in the CAM.\n",
340 ((type ==
341 MAC_ADDR_TYPE_MULTI_MAC) ? "MULTICAST" :
Johannes Berg7c510e42008-10-27 17:47:26 -0700342 "UNICAST"), addr, index);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400343
344 status =
345 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800346 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400347 if (status)
348 goto exit;
349 ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
350 (index << MAC_ADDR_IDX_SHIFT) | /* index */
351 type); /* type */
352 ql_write32(qdev, MAC_ADDR_DATA, lower);
353 status =
354 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800355 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400356 if (status)
357 goto exit;
358 ql_write32(qdev, MAC_ADDR_IDX, (offset++) | /* offset */
359 (index << MAC_ADDR_IDX_SHIFT) | /* index */
360 type); /* type */
361 ql_write32(qdev, MAC_ADDR_DATA, upper);
362 status =
363 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800364 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400365 if (status)
366 goto exit;
367 ql_write32(qdev, MAC_ADDR_IDX, (offset) | /* offset */
368 (index << MAC_ADDR_IDX_SHIFT) | /* index */
369 type); /* type */
370 /* This field should also include the queue id
371 and possibly the function id. Right now we hardcode
372 the route field to NIC core.
373 */
374 if (type == MAC_ADDR_TYPE_CAM_MAC) {
375 cam_output = (CAM_OUT_ROUTE_NIC |
376 (qdev->
377 func << CAM_OUT_FUNC_SHIFT) |
378 (qdev->
379 rss_ring_first_cq_id <<
380 CAM_OUT_CQ_ID_SHIFT));
381 if (qdev->vlgrp)
382 cam_output |= CAM_OUT_RV;
383 /* route to NIC core */
384 ql_write32(qdev, MAC_ADDR_DATA, cam_output);
385 }
386 break;
387 }
388 case MAC_ADDR_TYPE_VLAN:
389 {
390 u32 enable_bit = *((u32 *) &addr[0]);
391 /* For VLAN, the addr actually holds a bit that
392 * either enables or disables the vlan id we are
393 * addressing. It's either MAC_ADDR_E on or off.
394 * That's bit-27 we're talking about.
395 */
396 QPRINTK(qdev, IFUP, INFO, "%s VLAN ID %d %s the CAM.\n",
397 (enable_bit ? "Adding" : "Removing"),
398 index, (enable_bit ? "to" : "from"));
399
400 status =
401 ql_wait_reg_rdy(qdev,
Ron Mercer939678f2009-01-04 17:08:29 -0800402 MAC_ADDR_IDX, MAC_ADDR_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400403 if (status)
404 goto exit;
405 ql_write32(qdev, MAC_ADDR_IDX, offset | /* offset */
406 (index << MAC_ADDR_IDX_SHIFT) | /* index */
407 type | /* type */
408 enable_bit); /* enable/disable */
409 break;
410 }
411 case MAC_ADDR_TYPE_MULTI_FLTR:
412 default:
413 QPRINTK(qdev, IFUP, CRIT,
414 "Address type %d not yet supported.\n", type);
415 status = -EPERM;
416 }
417exit:
418 ql_sem_unlock(qdev, SEM_MAC_ADDR_MASK);
419 return status;
420}
421
422/* Get a specific frame routing value from the CAM.
423 * Used for debug and reg dump.
424 */
425int ql_get_routing_reg(struct ql_adapter *qdev, u32 index, u32 *value)
426{
427 int status = 0;
428
429 status = ql_sem_spinlock(qdev, SEM_RT_IDX_MASK);
430 if (status)
431 goto exit;
432
Ron Mercer939678f2009-01-04 17:08:29 -0800433 status = ql_wait_reg_rdy(qdev, RT_IDX, RT_IDX_MW, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400434 if (status)
435 goto exit;
436
437 ql_write32(qdev, RT_IDX,
438 RT_IDX_TYPE_NICQ | RT_IDX_RS | (index << RT_IDX_IDX_SHIFT));
Ron Mercer939678f2009-01-04 17:08:29 -0800439 status = ql_wait_reg_rdy(qdev, RT_IDX, RT_IDX_MR, 0);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400440 if (status)
441 goto exit;
442 *value = ql_read32(qdev, RT_DATA);
443exit:
444 ql_sem_unlock(qdev, SEM_RT_IDX_MASK);
445 return status;
446}
447
448/* The NIC function for this chip has 16 routing indexes. Each one can be used
449 * to route different frame types to various inbound queues. We send broadcast/
450 * multicast/error frames to the default queue for slow handling,
451 * and CAM hit/RSS frames to the fast handling queues.
452 */
453static int ql_set_routing_reg(struct ql_adapter *qdev, u32 index, u32 mask,
454 int enable)
455{
456 int status;
457 u32 value = 0;
458
459 status = ql_sem_spinlock(qdev, SEM_RT_IDX_MASK);
460 if (status)
461 return status;
462
463 QPRINTK(qdev, IFUP, DEBUG,
464 "%s %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s mask %s the routing reg.\n",
465 (enable ? "Adding" : "Removing"),
466 ((index == RT_IDX_ALL_ERR_SLOT) ? "MAC ERROR/ALL ERROR" : ""),
467 ((index == RT_IDX_IP_CSUM_ERR_SLOT) ? "IP CSUM ERROR" : ""),
468 ((index ==
469 RT_IDX_TCP_UDP_CSUM_ERR_SLOT) ? "TCP/UDP CSUM ERROR" : ""),
470 ((index == RT_IDX_BCAST_SLOT) ? "BROADCAST" : ""),
471 ((index == RT_IDX_MCAST_MATCH_SLOT) ? "MULTICAST MATCH" : ""),
472 ((index == RT_IDX_ALLMULTI_SLOT) ? "ALL MULTICAST MATCH" : ""),
473 ((index == RT_IDX_UNUSED6_SLOT) ? "UNUSED6" : ""),
474 ((index == RT_IDX_UNUSED7_SLOT) ? "UNUSED7" : ""),
475 ((index == RT_IDX_RSS_MATCH_SLOT) ? "RSS ALL/IPV4 MATCH" : ""),
476 ((index == RT_IDX_RSS_IPV6_SLOT) ? "RSS IPV6" : ""),
477 ((index == RT_IDX_RSS_TCP4_SLOT) ? "RSS TCP4" : ""),
478 ((index == RT_IDX_RSS_TCP6_SLOT) ? "RSS TCP6" : ""),
479 ((index == RT_IDX_CAM_HIT_SLOT) ? "CAM HIT" : ""),
480 ((index == RT_IDX_UNUSED013) ? "UNUSED13" : ""),
481 ((index == RT_IDX_UNUSED014) ? "UNUSED14" : ""),
482 ((index == RT_IDX_PROMISCUOUS_SLOT) ? "PROMISCUOUS" : ""),
483 (enable ? "to" : "from"));
484
485 switch (mask) {
486 case RT_IDX_CAM_HIT:
487 {
488 value = RT_IDX_DST_CAM_Q | /* dest */
489 RT_IDX_TYPE_NICQ | /* type */
490 (RT_IDX_CAM_HIT_SLOT << RT_IDX_IDX_SHIFT);/* index */
491 break;
492 }
493 case RT_IDX_VALID: /* Promiscuous Mode frames. */
494 {
495 value = RT_IDX_DST_DFLT_Q | /* dest */
496 RT_IDX_TYPE_NICQ | /* type */
497 (RT_IDX_PROMISCUOUS_SLOT << RT_IDX_IDX_SHIFT);/* index */
498 break;
499 }
500 case RT_IDX_ERR: /* Pass up MAC,IP,TCP/UDP error frames. */
501 {
502 value = RT_IDX_DST_DFLT_Q | /* dest */
503 RT_IDX_TYPE_NICQ | /* type */
504 (RT_IDX_ALL_ERR_SLOT << RT_IDX_IDX_SHIFT);/* index */
505 break;
506 }
507 case RT_IDX_BCAST: /* Pass up Broadcast frames to default Q. */
508 {
509 value = RT_IDX_DST_DFLT_Q | /* dest */
510 RT_IDX_TYPE_NICQ | /* type */
511 (RT_IDX_BCAST_SLOT << RT_IDX_IDX_SHIFT);/* index */
512 break;
513 }
514 case RT_IDX_MCAST: /* Pass up All Multicast frames. */
515 {
516 value = RT_IDX_DST_CAM_Q | /* dest */
517 RT_IDX_TYPE_NICQ | /* type */
518 (RT_IDX_ALLMULTI_SLOT << RT_IDX_IDX_SHIFT);/* index */
519 break;
520 }
521 case RT_IDX_MCAST_MATCH: /* Pass up matched Multicast frames. */
522 {
523 value = RT_IDX_DST_CAM_Q | /* dest */
524 RT_IDX_TYPE_NICQ | /* type */
525 (RT_IDX_MCAST_MATCH_SLOT << RT_IDX_IDX_SHIFT);/* index */
526 break;
527 }
528 case RT_IDX_RSS_MATCH: /* Pass up matched RSS frames. */
529 {
530 value = RT_IDX_DST_RSS | /* dest */
531 RT_IDX_TYPE_NICQ | /* type */
532 (RT_IDX_RSS_MATCH_SLOT << RT_IDX_IDX_SHIFT);/* index */
533 break;
534 }
535 case 0: /* Clear the E-bit on an entry. */
536 {
537 value = RT_IDX_DST_DFLT_Q | /* dest */
538 RT_IDX_TYPE_NICQ | /* type */
539 (index << RT_IDX_IDX_SHIFT);/* index */
540 break;
541 }
542 default:
543 QPRINTK(qdev, IFUP, ERR, "Mask type %d not yet supported.\n",
544 mask);
545 status = -EPERM;
546 goto exit;
547 }
548
549 if (value) {
550 status = ql_wait_reg_rdy(qdev, RT_IDX, RT_IDX_MW, 0);
551 if (status)
552 goto exit;
553 value |= (enable ? RT_IDX_E : 0);
554 ql_write32(qdev, RT_IDX, value);
555 ql_write32(qdev, RT_DATA, enable ? mask : 0);
556 }
557exit:
558 ql_sem_unlock(qdev, SEM_RT_IDX_MASK);
559 return status;
560}
561
562static void ql_enable_interrupts(struct ql_adapter *qdev)
563{
564 ql_write32(qdev, INTR_EN, (INTR_EN_EI << 16) | INTR_EN_EI);
565}
566
567static void ql_disable_interrupts(struct ql_adapter *qdev)
568{
569 ql_write32(qdev, INTR_EN, (INTR_EN_EI << 16));
570}
571
572/* If we're running with multiple MSI-X vectors then we enable on the fly.
573 * Otherwise, we may have multiple outstanding workers and don't want to
574 * enable until the last one finishes. In this case, the irq_cnt gets
575 * incremented everytime we queue a worker and decremented everytime
576 * a worker finishes. Once it hits zero we enable the interrupt.
577 */
Ron Mercerbb0d2152008-10-20 10:30:26 -0700578u32 ql_enable_completion_interrupt(struct ql_adapter *qdev, u32 intr)
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400579{
Ron Mercerbb0d2152008-10-20 10:30:26 -0700580 u32 var = 0;
581 unsigned long hw_flags = 0;
582 struct intr_context *ctx = qdev->intr_context + intr;
583
584 if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags) && intr)) {
585 /* Always enable if we're MSIX multi interrupts and
586 * it's not the default (zeroeth) interrupt.
587 */
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400588 ql_write32(qdev, INTR_EN,
Ron Mercerbb0d2152008-10-20 10:30:26 -0700589 ctx->intr_en_mask);
590 var = ql_read32(qdev, STS);
591 return var;
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400592 }
Ron Mercerbb0d2152008-10-20 10:30:26 -0700593
594 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
595 if (atomic_dec_and_test(&ctx->irq_cnt)) {
596 ql_write32(qdev, INTR_EN,
597 ctx->intr_en_mask);
598 var = ql_read32(qdev, STS);
599 }
600 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
601 return var;
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400602}
603
604static u32 ql_disable_completion_interrupt(struct ql_adapter *qdev, u32 intr)
605{
606 u32 var = 0;
Ron Mercerbb0d2152008-10-20 10:30:26 -0700607 unsigned long hw_flags;
608 struct intr_context *ctx;
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400609
Ron Mercerbb0d2152008-10-20 10:30:26 -0700610 /* HW disables for us if we're MSIX multi interrupts and
611 * it's not the default (zeroeth) interrupt.
612 */
613 if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags) && intr))
614 return 0;
615
616 ctx = qdev->intr_context + intr;
617 spin_lock_irqsave(&qdev->hw_lock, hw_flags);
618 if (!atomic_read(&ctx->irq_cnt)) {
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400619 ql_write32(qdev, INTR_EN,
Ron Mercerbb0d2152008-10-20 10:30:26 -0700620 ctx->intr_dis_mask);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400621 var = ql_read32(qdev, STS);
622 }
Ron Mercerbb0d2152008-10-20 10:30:26 -0700623 atomic_inc(&ctx->irq_cnt);
624 spin_unlock_irqrestore(&qdev->hw_lock, hw_flags);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400625 return var;
626}
627
628static void ql_enable_all_completion_interrupts(struct ql_adapter *qdev)
629{
630 int i;
631 for (i = 0; i < qdev->intr_count; i++) {
632 /* The enable call does a atomic_dec_and_test
633 * and enables only if the result is zero.
634 * So we precharge it here.
635 */
Ron Mercerbb0d2152008-10-20 10:30:26 -0700636 if (unlikely(!test_bit(QL_MSIX_ENABLED, &qdev->flags) ||
637 i == 0))
638 atomic_set(&qdev->intr_context[i].irq_cnt, 1);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400639 ql_enable_completion_interrupt(qdev, i);
640 }
641
642}
643
Stephen Hemminger8668ae92008-11-21 17:29:50 -0800644static int ql_read_flash_word(struct ql_adapter *qdev, int offset, u32 *data)
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400645{
646 int status = 0;
647 /* wait for reg to come ready */
648 status = ql_wait_reg_rdy(qdev,
649 FLASH_ADDR, FLASH_ADDR_RDY, FLASH_ADDR_ERR);
650 if (status)
651 goto exit;
652 /* set up for reg read */
653 ql_write32(qdev, FLASH_ADDR, FLASH_ADDR_R | offset);
654 /* wait for reg to come ready */
655 status = ql_wait_reg_rdy(qdev,
656 FLASH_ADDR, FLASH_ADDR_RDY, FLASH_ADDR_ERR);
657 if (status)
658 goto exit;
659 /* get the data */
660 *data = ql_read32(qdev, FLASH_DATA);
661exit:
662 return status;
663}
664
665static int ql_get_flash_params(struct ql_adapter *qdev)
666{
667 int i;
668 int status;
669 u32 *p = (u32 *)&qdev->flash;
670
671 if (ql_sem_spinlock(qdev, SEM_FLASH_MASK))
672 return -ETIMEDOUT;
673
674 for (i = 0; i < sizeof(qdev->flash) / sizeof(u32); i++, p++) {
675 status = ql_read_flash_word(qdev, i, p);
676 if (status) {
677 QPRINTK(qdev, IFUP, ERR, "Error reading flash.\n");
678 goto exit;
679 }
680
681 }
682exit:
683 ql_sem_unlock(qdev, SEM_FLASH_MASK);
684 return status;
685}
686
687/* xgmac register are located behind the xgmac_addr and xgmac_data
688 * register pair. Each read/write requires us to wait for the ready
689 * bit before reading/writing the data.
690 */
691static int ql_write_xgmac_reg(struct ql_adapter *qdev, u32 reg, u32 data)
692{
693 int status;
694 /* wait for reg to come ready */
695 status = ql_wait_reg_rdy(qdev,
696 XGMAC_ADDR, XGMAC_ADDR_RDY, XGMAC_ADDR_XME);
697 if (status)
698 return status;
699 /* write the data to the data reg */
700 ql_write32(qdev, XGMAC_DATA, data);
701 /* trigger the write */
702 ql_write32(qdev, XGMAC_ADDR, reg);
703 return status;
704}
705
706/* xgmac register are located behind the xgmac_addr and xgmac_data
707 * register pair. Each read/write requires us to wait for the ready
708 * bit before reading/writing the data.
709 */
710int ql_read_xgmac_reg(struct ql_adapter *qdev, u32 reg, u32 *data)
711{
712 int status = 0;
713 /* wait for reg to come ready */
714 status = ql_wait_reg_rdy(qdev,
715 XGMAC_ADDR, XGMAC_ADDR_RDY, XGMAC_ADDR_XME);
716 if (status)
717 goto exit;
718 /* set up for reg read */
719 ql_write32(qdev, XGMAC_ADDR, reg | XGMAC_ADDR_R);
720 /* wait for reg to come ready */
721 status = ql_wait_reg_rdy(qdev,
722 XGMAC_ADDR, XGMAC_ADDR_RDY, XGMAC_ADDR_XME);
723 if (status)
724 goto exit;
725 /* get the data */
726 *data = ql_read32(qdev, XGMAC_DATA);
727exit:
728 return status;
729}
730
731/* This is used for reading the 64-bit statistics regs. */
732int ql_read_xgmac_reg64(struct ql_adapter *qdev, u32 reg, u64 *data)
733{
734 int status = 0;
735 u32 hi = 0;
736 u32 lo = 0;
737
738 status = ql_read_xgmac_reg(qdev, reg, &lo);
739 if (status)
740 goto exit;
741
742 status = ql_read_xgmac_reg(qdev, reg + 4, &hi);
743 if (status)
744 goto exit;
745
746 *data = (u64) lo | ((u64) hi << 32);
747
748exit:
749 return status;
750}
751
752/* Take the MAC Core out of reset.
753 * Enable statistics counting.
754 * Take the transmitter/receiver out of reset.
755 * This functionality may be done in the MPI firmware at a
756 * later date.
757 */
758static int ql_port_initialize(struct ql_adapter *qdev)
759{
760 int status = 0;
761 u32 data;
762
763 if (ql_sem_trylock(qdev, qdev->xg_sem_mask)) {
764 /* Another function has the semaphore, so
765 * wait for the port init bit to come ready.
766 */
767 QPRINTK(qdev, LINK, INFO,
768 "Another function has the semaphore, so wait for the port init bit to come ready.\n");
769 status = ql_wait_reg_rdy(qdev, STS, qdev->port_init, 0);
770 if (status) {
771 QPRINTK(qdev, LINK, CRIT,
772 "Port initialize timed out.\n");
773 }
774 return status;
775 }
776
777 QPRINTK(qdev, LINK, INFO, "Got xgmac semaphore!.\n");
778 /* Set the core reset. */
779 status = ql_read_xgmac_reg(qdev, GLOBAL_CFG, &data);
780 if (status)
781 goto end;
782 data |= GLOBAL_CFG_RESET;
783 status = ql_write_xgmac_reg(qdev, GLOBAL_CFG, data);
784 if (status)
785 goto end;
786
787 /* Clear the core reset and turn on jumbo for receiver. */
788 data &= ~GLOBAL_CFG_RESET; /* Clear core reset. */
789 data |= GLOBAL_CFG_JUMBO; /* Turn on jumbo. */
790 data |= GLOBAL_CFG_TX_STAT_EN;
791 data |= GLOBAL_CFG_RX_STAT_EN;
792 status = ql_write_xgmac_reg(qdev, GLOBAL_CFG, data);
793 if (status)
794 goto end;
795
796 /* Enable transmitter, and clear it's reset. */
797 status = ql_read_xgmac_reg(qdev, TX_CFG, &data);
798 if (status)
799 goto end;
800 data &= ~TX_CFG_RESET; /* Clear the TX MAC reset. */
801 data |= TX_CFG_EN; /* Enable the transmitter. */
802 status = ql_write_xgmac_reg(qdev, TX_CFG, data);
803 if (status)
804 goto end;
805
806 /* Enable receiver and clear it's reset. */
807 status = ql_read_xgmac_reg(qdev, RX_CFG, &data);
808 if (status)
809 goto end;
810 data &= ~RX_CFG_RESET; /* Clear the RX MAC reset. */
811 data |= RX_CFG_EN; /* Enable the receiver. */
812 status = ql_write_xgmac_reg(qdev, RX_CFG, data);
813 if (status)
814 goto end;
815
816 /* Turn on jumbo. */
817 status =
818 ql_write_xgmac_reg(qdev, MAC_TX_PARAMS, MAC_TX_PARAMS_JUMBO | (0x2580 << 16));
819 if (status)
820 goto end;
821 status =
822 ql_write_xgmac_reg(qdev, MAC_RX_PARAMS, 0x2580);
823 if (status)
824 goto end;
825
826 /* Signal to the world that the port is enabled. */
827 ql_write32(qdev, STS, ((qdev->port_init << 16) | qdev->port_init));
828end:
829 ql_sem_unlock(qdev, qdev->xg_sem_mask);
830 return status;
831}
832
833/* Get the next large buffer. */
Stephen Hemminger8668ae92008-11-21 17:29:50 -0800834static struct bq_desc *ql_get_curr_lbuf(struct rx_ring *rx_ring)
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400835{
836 struct bq_desc *lbq_desc = &rx_ring->lbq[rx_ring->lbq_curr_idx];
837 rx_ring->lbq_curr_idx++;
838 if (rx_ring->lbq_curr_idx == rx_ring->lbq_len)
839 rx_ring->lbq_curr_idx = 0;
840 rx_ring->lbq_free_cnt++;
841 return lbq_desc;
842}
843
844/* Get the next small buffer. */
Stephen Hemminger8668ae92008-11-21 17:29:50 -0800845static struct bq_desc *ql_get_curr_sbuf(struct rx_ring *rx_ring)
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400846{
847 struct bq_desc *sbq_desc = &rx_ring->sbq[rx_ring->sbq_curr_idx];
848 rx_ring->sbq_curr_idx++;
849 if (rx_ring->sbq_curr_idx == rx_ring->sbq_len)
850 rx_ring->sbq_curr_idx = 0;
851 rx_ring->sbq_free_cnt++;
852 return sbq_desc;
853}
854
855/* Update an rx ring index. */
856static void ql_update_cq(struct rx_ring *rx_ring)
857{
858 rx_ring->cnsmr_idx++;
859 rx_ring->curr_entry++;
860 if (unlikely(rx_ring->cnsmr_idx == rx_ring->cq_len)) {
861 rx_ring->cnsmr_idx = 0;
862 rx_ring->curr_entry = rx_ring->cq_base;
863 }
864}
865
866static void ql_write_cq_idx(struct rx_ring *rx_ring)
867{
868 ql_write_db_reg(rx_ring->cnsmr_idx, rx_ring->cnsmr_idx_db_reg);
869}
870
871/* Process (refill) a large buffer queue. */
872static void ql_update_lbq(struct ql_adapter *qdev, struct rx_ring *rx_ring)
873{
874 int clean_idx = rx_ring->lbq_clean_idx;
875 struct bq_desc *lbq_desc;
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400876 u64 map;
877 int i;
878
879 while (rx_ring->lbq_free_cnt > 16) {
880 for (i = 0; i < 16; i++) {
881 QPRINTK(qdev, RX_STATUS, DEBUG,
882 "lbq: try cleaning clean_idx = %d.\n",
883 clean_idx);
884 lbq_desc = &rx_ring->lbq[clean_idx];
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400885 if (lbq_desc->p.lbq_page == NULL) {
886 QPRINTK(qdev, RX_STATUS, DEBUG,
887 "lbq: getting new page for index %d.\n",
888 lbq_desc->index);
889 lbq_desc->p.lbq_page = alloc_page(GFP_ATOMIC);
890 if (lbq_desc->p.lbq_page == NULL) {
891 QPRINTK(qdev, RX_STATUS, ERR,
892 "Couldn't get a page.\n");
893 return;
894 }
895 map = pci_map_page(qdev->pdev,
896 lbq_desc->p.lbq_page,
897 0, PAGE_SIZE,
898 PCI_DMA_FROMDEVICE);
899 if (pci_dma_mapping_error(qdev->pdev, map)) {
900 QPRINTK(qdev, RX_STATUS, ERR,
901 "PCI mapping failed.\n");
902 return;
903 }
904 pci_unmap_addr_set(lbq_desc, mapaddr, map);
905 pci_unmap_len_set(lbq_desc, maplen, PAGE_SIZE);
Ron Mercer2c9a0d42009-01-05 18:19:20 -0800906 *lbq_desc->addr = cpu_to_le64(map);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400907 }
908 clean_idx++;
909 if (clean_idx == rx_ring->lbq_len)
910 clean_idx = 0;
911 }
912
913 rx_ring->lbq_clean_idx = clean_idx;
914 rx_ring->lbq_prod_idx += 16;
915 if (rx_ring->lbq_prod_idx == rx_ring->lbq_len)
916 rx_ring->lbq_prod_idx = 0;
917 QPRINTK(qdev, RX_STATUS, DEBUG,
918 "lbq: updating prod idx = %d.\n",
919 rx_ring->lbq_prod_idx);
920 ql_write_db_reg(rx_ring->lbq_prod_idx,
921 rx_ring->lbq_prod_idx_db_reg);
922 rx_ring->lbq_free_cnt -= 16;
923 }
924}
925
926/* Process (refill) a small buffer queue. */
927static void ql_update_sbq(struct ql_adapter *qdev, struct rx_ring *rx_ring)
928{
929 int clean_idx = rx_ring->sbq_clean_idx;
930 struct bq_desc *sbq_desc;
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400931 u64 map;
932 int i;
933
934 while (rx_ring->sbq_free_cnt > 16) {
935 for (i = 0; i < 16; i++) {
936 sbq_desc = &rx_ring->sbq[clean_idx];
937 QPRINTK(qdev, RX_STATUS, DEBUG,
938 "sbq: try cleaning clean_idx = %d.\n",
939 clean_idx);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400940 if (sbq_desc->p.skb == NULL) {
941 QPRINTK(qdev, RX_STATUS, DEBUG,
942 "sbq: getting new skb for index %d.\n",
943 sbq_desc->index);
944 sbq_desc->p.skb =
945 netdev_alloc_skb(qdev->ndev,
946 rx_ring->sbq_buf_size);
947 if (sbq_desc->p.skb == NULL) {
948 QPRINTK(qdev, PROBE, ERR,
949 "Couldn't get an skb.\n");
950 rx_ring->sbq_clean_idx = clean_idx;
951 return;
952 }
953 skb_reserve(sbq_desc->p.skb, QLGE_SB_PAD);
954 map = pci_map_single(qdev->pdev,
955 sbq_desc->p.skb->data,
956 rx_ring->sbq_buf_size /
957 2, PCI_DMA_FROMDEVICE);
Ron Mercerc907a352009-01-04 17:06:46 -0800958 if (pci_dma_mapping_error(qdev->pdev, map)) {
959 QPRINTK(qdev, IFUP, ERR, "PCI mapping failed.\n");
960 rx_ring->sbq_clean_idx = clean_idx;
961 return;
962 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400963 pci_unmap_addr_set(sbq_desc, mapaddr, map);
964 pci_unmap_len_set(sbq_desc, maplen,
965 rx_ring->sbq_buf_size / 2);
Ron Mercer2c9a0d42009-01-05 18:19:20 -0800966 *sbq_desc->addr = cpu_to_le64(map);
Ron Mercerc4e84bd2008-09-18 11:56:28 -0400967 }
968
969 clean_idx++;
970 if (clean_idx == rx_ring->sbq_len)
971 clean_idx = 0;
972 }
973 rx_ring->sbq_clean_idx = clean_idx;
974 rx_ring->sbq_prod_idx += 16;
975 if (rx_ring->sbq_prod_idx == rx_ring->sbq_len)
976 rx_ring->sbq_prod_idx = 0;
977 QPRINTK(qdev, RX_STATUS, DEBUG,
978 "sbq: updating prod idx = %d.\n",
979 rx_ring->sbq_prod_idx);
980 ql_write_db_reg(rx_ring->sbq_prod_idx,
981 rx_ring->sbq_prod_idx_db_reg);
982
983 rx_ring->sbq_free_cnt -= 16;
984 }
985}
986
987static void ql_update_buffer_queues(struct ql_adapter *qdev,
988 struct rx_ring *rx_ring)
989{
990 ql_update_sbq(qdev, rx_ring);
991 ql_update_lbq(qdev, rx_ring);
992}
993
994/* Unmaps tx buffers. Can be called from send() if a pci mapping
995 * fails at some stage, or from the interrupt when a tx completes.
996 */
997static void ql_unmap_send(struct ql_adapter *qdev,
998 struct tx_ring_desc *tx_ring_desc, int mapped)
999{
1000 int i;
1001 for (i = 0; i < mapped; i++) {
1002 if (i == 0 || (i == 7 && mapped > 7)) {
1003 /*
1004 * Unmap the skb->data area, or the
1005 * external sglist (AKA the Outbound
1006 * Address List (OAL)).
1007 * If its the zeroeth element, then it's
1008 * the skb->data area. If it's the 7th
1009 * element and there is more than 6 frags,
1010 * then its an OAL.
1011 */
1012 if (i == 7) {
1013 QPRINTK(qdev, TX_DONE, DEBUG,
1014 "unmapping OAL area.\n");
1015 }
1016 pci_unmap_single(qdev->pdev,
1017 pci_unmap_addr(&tx_ring_desc->map[i],
1018 mapaddr),
1019 pci_unmap_len(&tx_ring_desc->map[i],
1020 maplen),
1021 PCI_DMA_TODEVICE);
1022 } else {
1023 QPRINTK(qdev, TX_DONE, DEBUG, "unmapping frag %d.\n",
1024 i);
1025 pci_unmap_page(qdev->pdev,
1026 pci_unmap_addr(&tx_ring_desc->map[i],
1027 mapaddr),
1028 pci_unmap_len(&tx_ring_desc->map[i],
1029 maplen), PCI_DMA_TODEVICE);
1030 }
1031 }
1032
1033}
1034
1035/* Map the buffers for this transmit. This will return
1036 * NETDEV_TX_BUSY or NETDEV_TX_OK based on success.
1037 */
1038static int ql_map_send(struct ql_adapter *qdev,
1039 struct ob_mac_iocb_req *mac_iocb_ptr,
1040 struct sk_buff *skb, struct tx_ring_desc *tx_ring_desc)
1041{
1042 int len = skb_headlen(skb);
1043 dma_addr_t map;
1044 int frag_idx, err, map_idx = 0;
1045 struct tx_buf_desc *tbd = mac_iocb_ptr->tbd;
1046 int frag_cnt = skb_shinfo(skb)->nr_frags;
1047
1048 if (frag_cnt) {
1049 QPRINTK(qdev, TX_QUEUED, DEBUG, "frag_cnt = %d.\n", frag_cnt);
1050 }
1051 /*
1052 * Map the skb buffer first.
1053 */
1054 map = pci_map_single(qdev->pdev, skb->data, len, PCI_DMA_TODEVICE);
1055
1056 err = pci_dma_mapping_error(qdev->pdev, map);
1057 if (err) {
1058 QPRINTK(qdev, TX_QUEUED, ERR,
1059 "PCI mapping failed with error: %d\n", err);
1060
1061 return NETDEV_TX_BUSY;
1062 }
1063
1064 tbd->len = cpu_to_le32(len);
1065 tbd->addr = cpu_to_le64(map);
1066 pci_unmap_addr_set(&tx_ring_desc->map[map_idx], mapaddr, map);
1067 pci_unmap_len_set(&tx_ring_desc->map[map_idx], maplen, len);
1068 map_idx++;
1069
1070 /*
1071 * This loop fills the remainder of the 8 address descriptors
1072 * in the IOCB. If there are more than 7 fragments, then the
1073 * eighth address desc will point to an external list (OAL).
1074 * When this happens, the remainder of the frags will be stored
1075 * in this list.
1076 */
1077 for (frag_idx = 0; frag_idx < frag_cnt; frag_idx++, map_idx++) {
1078 skb_frag_t *frag = &skb_shinfo(skb)->frags[frag_idx];
1079 tbd++;
1080 if (frag_idx == 6 && frag_cnt > 7) {
1081 /* Let's tack on an sglist.
1082 * Our control block will now
1083 * look like this:
1084 * iocb->seg[0] = skb->data
1085 * iocb->seg[1] = frag[0]
1086 * iocb->seg[2] = frag[1]
1087 * iocb->seg[3] = frag[2]
1088 * iocb->seg[4] = frag[3]
1089 * iocb->seg[5] = frag[4]
1090 * iocb->seg[6] = frag[5]
1091 * iocb->seg[7] = ptr to OAL (external sglist)
1092 * oal->seg[0] = frag[6]
1093 * oal->seg[1] = frag[7]
1094 * oal->seg[2] = frag[8]
1095 * oal->seg[3] = frag[9]
1096 * oal->seg[4] = frag[10]
1097 * etc...
1098 */
1099 /* Tack on the OAL in the eighth segment of IOCB. */
1100 map = pci_map_single(qdev->pdev, &tx_ring_desc->oal,
1101 sizeof(struct oal),
1102 PCI_DMA_TODEVICE);
1103 err = pci_dma_mapping_error(qdev->pdev, map);
1104 if (err) {
1105 QPRINTK(qdev, TX_QUEUED, ERR,
1106 "PCI mapping outbound address list with error: %d\n",
1107 err);
1108 goto map_error;
1109 }
1110
1111 tbd->addr = cpu_to_le64(map);
1112 /*
1113 * The length is the number of fragments
1114 * that remain to be mapped times the length
1115 * of our sglist (OAL).
1116 */
1117 tbd->len =
1118 cpu_to_le32((sizeof(struct tx_buf_desc) *
1119 (frag_cnt - frag_idx)) | TX_DESC_C);
1120 pci_unmap_addr_set(&tx_ring_desc->map[map_idx], mapaddr,
1121 map);
1122 pci_unmap_len_set(&tx_ring_desc->map[map_idx], maplen,
1123 sizeof(struct oal));
1124 tbd = (struct tx_buf_desc *)&tx_ring_desc->oal;
1125 map_idx++;
1126 }
1127
1128 map =
1129 pci_map_page(qdev->pdev, frag->page,
1130 frag->page_offset, frag->size,
1131 PCI_DMA_TODEVICE);
1132
1133 err = pci_dma_mapping_error(qdev->pdev, map);
1134 if (err) {
1135 QPRINTK(qdev, TX_QUEUED, ERR,
1136 "PCI mapping frags failed with error: %d.\n",
1137 err);
1138 goto map_error;
1139 }
1140
1141 tbd->addr = cpu_to_le64(map);
1142 tbd->len = cpu_to_le32(frag->size);
1143 pci_unmap_addr_set(&tx_ring_desc->map[map_idx], mapaddr, map);
1144 pci_unmap_len_set(&tx_ring_desc->map[map_idx], maplen,
1145 frag->size);
1146
1147 }
1148 /* Save the number of segments we've mapped. */
1149 tx_ring_desc->map_cnt = map_idx;
1150 /* Terminate the last segment. */
1151 tbd->len = cpu_to_le32(le32_to_cpu(tbd->len) | TX_DESC_E);
1152 return NETDEV_TX_OK;
1153
1154map_error:
1155 /*
1156 * If the first frag mapping failed, then i will be zero.
1157 * This causes the unmap of the skb->data area. Otherwise
1158 * we pass in the number of frags that mapped successfully
1159 * so they can be umapped.
1160 */
1161 ql_unmap_send(qdev, tx_ring_desc, map_idx);
1162 return NETDEV_TX_BUSY;
1163}
1164
Stephen Hemminger8668ae92008-11-21 17:29:50 -08001165static void ql_realign_skb(struct sk_buff *skb, int len)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001166{
1167 void *temp_addr = skb->data;
1168
1169 /* Undo the skb_reserve(skb,32) we did before
1170 * giving to hardware, and realign data on
1171 * a 2-byte boundary.
1172 */
1173 skb->data -= QLGE_SB_PAD - NET_IP_ALIGN;
1174 skb->tail -= QLGE_SB_PAD - NET_IP_ALIGN;
1175 skb_copy_to_linear_data(skb, temp_addr,
1176 (unsigned int)len);
1177}
1178
1179/*
1180 * This function builds an skb for the given inbound
1181 * completion. It will be rewritten for readability in the near
1182 * future, but for not it works well.
1183 */
1184static struct sk_buff *ql_build_rx_skb(struct ql_adapter *qdev,
1185 struct rx_ring *rx_ring,
1186 struct ib_mac_iocb_rsp *ib_mac_rsp)
1187{
1188 struct bq_desc *lbq_desc;
1189 struct bq_desc *sbq_desc;
1190 struct sk_buff *skb = NULL;
1191 u32 length = le32_to_cpu(ib_mac_rsp->data_len);
1192 u32 hdr_len = le32_to_cpu(ib_mac_rsp->hdr_len);
1193
1194 /*
1195 * Handle the header buffer if present.
1196 */
1197 if (ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HV &&
1198 ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS) {
1199 QPRINTK(qdev, RX_STATUS, DEBUG, "Header of %d bytes in small buffer.\n", hdr_len);
1200 /*
1201 * Headers fit nicely into a small buffer.
1202 */
1203 sbq_desc = ql_get_curr_sbuf(rx_ring);
1204 pci_unmap_single(qdev->pdev,
1205 pci_unmap_addr(sbq_desc, mapaddr),
1206 pci_unmap_len(sbq_desc, maplen),
1207 PCI_DMA_FROMDEVICE);
1208 skb = sbq_desc->p.skb;
1209 ql_realign_skb(skb, hdr_len);
1210 skb_put(skb, hdr_len);
1211 sbq_desc->p.skb = NULL;
1212 }
1213
1214 /*
1215 * Handle the data buffer(s).
1216 */
1217 if (unlikely(!length)) { /* Is there data too? */
1218 QPRINTK(qdev, RX_STATUS, DEBUG,
1219 "No Data buffer in this packet.\n");
1220 return skb;
1221 }
1222
1223 if (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_DS) {
1224 if (ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS) {
1225 QPRINTK(qdev, RX_STATUS, DEBUG,
1226 "Headers in small, data of %d bytes in small, combine them.\n", length);
1227 /*
1228 * Data is less than small buffer size so it's
1229 * stuffed in a small buffer.
1230 * For this case we append the data
1231 * from the "data" small buffer to the "header" small
1232 * buffer.
1233 */
1234 sbq_desc = ql_get_curr_sbuf(rx_ring);
1235 pci_dma_sync_single_for_cpu(qdev->pdev,
1236 pci_unmap_addr
1237 (sbq_desc, mapaddr),
1238 pci_unmap_len
1239 (sbq_desc, maplen),
1240 PCI_DMA_FROMDEVICE);
1241 memcpy(skb_put(skb, length),
1242 sbq_desc->p.skb->data, length);
1243 pci_dma_sync_single_for_device(qdev->pdev,
1244 pci_unmap_addr
1245 (sbq_desc,
1246 mapaddr),
1247 pci_unmap_len
1248 (sbq_desc,
1249 maplen),
1250 PCI_DMA_FROMDEVICE);
1251 } else {
1252 QPRINTK(qdev, RX_STATUS, DEBUG,
1253 "%d bytes in a single small buffer.\n", length);
1254 sbq_desc = ql_get_curr_sbuf(rx_ring);
1255 skb = sbq_desc->p.skb;
1256 ql_realign_skb(skb, length);
1257 skb_put(skb, length);
1258 pci_unmap_single(qdev->pdev,
1259 pci_unmap_addr(sbq_desc,
1260 mapaddr),
1261 pci_unmap_len(sbq_desc,
1262 maplen),
1263 PCI_DMA_FROMDEVICE);
1264 sbq_desc->p.skb = NULL;
1265 }
1266 } else if (ib_mac_rsp->flags3 & IB_MAC_IOCB_RSP_DL) {
1267 if (ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS) {
1268 QPRINTK(qdev, RX_STATUS, DEBUG,
1269 "Header in small, %d bytes in large. Chain large to small!\n", length);
1270 /*
1271 * The data is in a single large buffer. We
1272 * chain it to the header buffer's skb and let
1273 * it rip.
1274 */
1275 lbq_desc = ql_get_curr_lbuf(rx_ring);
1276 pci_unmap_page(qdev->pdev,
1277 pci_unmap_addr(lbq_desc,
1278 mapaddr),
1279 pci_unmap_len(lbq_desc, maplen),
1280 PCI_DMA_FROMDEVICE);
1281 QPRINTK(qdev, RX_STATUS, DEBUG,
1282 "Chaining page to skb.\n");
1283 skb_fill_page_desc(skb, 0, lbq_desc->p.lbq_page,
1284 0, length);
1285 skb->len += length;
1286 skb->data_len += length;
1287 skb->truesize += length;
1288 lbq_desc->p.lbq_page = NULL;
1289 } else {
1290 /*
1291 * The headers and data are in a single large buffer. We
1292 * copy it to a new skb and let it go. This can happen with
1293 * jumbo mtu on a non-TCP/UDP frame.
1294 */
1295 lbq_desc = ql_get_curr_lbuf(rx_ring);
1296 skb = netdev_alloc_skb(qdev->ndev, length);
1297 if (skb == NULL) {
1298 QPRINTK(qdev, PROBE, DEBUG,
1299 "No skb available, drop the packet.\n");
1300 return NULL;
1301 }
Ron Mercer4055c7d42009-01-04 17:07:09 -08001302 pci_unmap_page(qdev->pdev,
1303 pci_unmap_addr(lbq_desc,
1304 mapaddr),
1305 pci_unmap_len(lbq_desc, maplen),
1306 PCI_DMA_FROMDEVICE);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001307 skb_reserve(skb, NET_IP_ALIGN);
1308 QPRINTK(qdev, RX_STATUS, DEBUG,
1309 "%d bytes of headers and data in large. Chain page to new skb and pull tail.\n", length);
1310 skb_fill_page_desc(skb, 0, lbq_desc->p.lbq_page,
1311 0, length);
1312 skb->len += length;
1313 skb->data_len += length;
1314 skb->truesize += length;
1315 length -= length;
1316 lbq_desc->p.lbq_page = NULL;
1317 __pskb_pull_tail(skb,
1318 (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_V) ?
1319 VLAN_ETH_HLEN : ETH_HLEN);
1320 }
1321 } else {
1322 /*
1323 * The data is in a chain of large buffers
1324 * pointed to by a small buffer. We loop
1325 * thru and chain them to the our small header
1326 * buffer's skb.
1327 * frags: There are 18 max frags and our small
1328 * buffer will hold 32 of them. The thing is,
1329 * we'll use 3 max for our 9000 byte jumbo
1330 * frames. If the MTU goes up we could
1331 * eventually be in trouble.
1332 */
1333 int size, offset, i = 0;
Ron Mercer2c9a0d42009-01-05 18:19:20 -08001334 __le64 *bq, bq_array[8];
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001335 sbq_desc = ql_get_curr_sbuf(rx_ring);
1336 pci_unmap_single(qdev->pdev,
1337 pci_unmap_addr(sbq_desc, mapaddr),
1338 pci_unmap_len(sbq_desc, maplen),
1339 PCI_DMA_FROMDEVICE);
1340 if (!(ib_mac_rsp->flags4 & IB_MAC_IOCB_RSP_HS)) {
1341 /*
1342 * This is an non TCP/UDP IP frame, so
1343 * the headers aren't split into a small
1344 * buffer. We have to use the small buffer
1345 * that contains our sg list as our skb to
1346 * send upstairs. Copy the sg list here to
1347 * a local buffer and use it to find the
1348 * pages to chain.
1349 */
1350 QPRINTK(qdev, RX_STATUS, DEBUG,
1351 "%d bytes of headers & data in chain of large.\n", length);
1352 skb = sbq_desc->p.skb;
1353 bq = &bq_array[0];
1354 memcpy(bq, skb->data, sizeof(bq_array));
1355 sbq_desc->p.skb = NULL;
1356 skb_reserve(skb, NET_IP_ALIGN);
1357 } else {
1358 QPRINTK(qdev, RX_STATUS, DEBUG,
1359 "Headers in small, %d bytes of data in chain of large.\n", length);
Ron Mercer2c9a0d42009-01-05 18:19:20 -08001360 bq = (__le64 *)sbq_desc->p.skb->data;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001361 }
1362 while (length > 0) {
1363 lbq_desc = ql_get_curr_lbuf(rx_ring);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001364 pci_unmap_page(qdev->pdev,
1365 pci_unmap_addr(lbq_desc,
1366 mapaddr),
1367 pci_unmap_len(lbq_desc,
1368 maplen),
1369 PCI_DMA_FROMDEVICE);
1370 size = (length < PAGE_SIZE) ? length : PAGE_SIZE;
1371 offset = 0;
1372
1373 QPRINTK(qdev, RX_STATUS, DEBUG,
1374 "Adding page %d to skb for %d bytes.\n",
1375 i, size);
1376 skb_fill_page_desc(skb, i, lbq_desc->p.lbq_page,
1377 offset, size);
1378 skb->len += size;
1379 skb->data_len += size;
1380 skb->truesize += size;
1381 length -= size;
1382 lbq_desc->p.lbq_page = NULL;
1383 bq++;
1384 i++;
1385 }
1386 __pskb_pull_tail(skb, (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_V) ?
1387 VLAN_ETH_HLEN : ETH_HLEN);
1388 }
1389 return skb;
1390}
1391
1392/* Process an inbound completion from an rx ring. */
1393static void ql_process_mac_rx_intr(struct ql_adapter *qdev,
1394 struct rx_ring *rx_ring,
1395 struct ib_mac_iocb_rsp *ib_mac_rsp)
1396{
1397 struct net_device *ndev = qdev->ndev;
1398 struct sk_buff *skb = NULL;
1399
1400 QL_DUMP_IB_MAC_RSP(ib_mac_rsp);
1401
1402 skb = ql_build_rx_skb(qdev, rx_ring, ib_mac_rsp);
1403 if (unlikely(!skb)) {
1404 QPRINTK(qdev, RX_STATUS, DEBUG,
1405 "No skb available, drop packet.\n");
1406 return;
1407 }
1408
1409 prefetch(skb->data);
1410 skb->dev = ndev;
1411 if (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) {
1412 QPRINTK(qdev, RX_STATUS, DEBUG, "%s%s%s Multicast.\n",
1413 (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
1414 IB_MAC_IOCB_RSP_M_HASH ? "Hash" : "",
1415 (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
1416 IB_MAC_IOCB_RSP_M_REG ? "Registered" : "",
1417 (ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_M_MASK) ==
1418 IB_MAC_IOCB_RSP_M_PROM ? "Promiscuous" : "");
1419 }
1420 if (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_P) {
1421 QPRINTK(qdev, RX_STATUS, DEBUG, "Promiscuous Packet.\n");
1422 }
1423 if (ib_mac_rsp->flags1 & (IB_MAC_IOCB_RSP_IE | IB_MAC_IOCB_RSP_TE)) {
1424 QPRINTK(qdev, RX_STATUS, ERR,
1425 "Bad checksum for this %s packet.\n",
1426 ((ib_mac_rsp->
1427 flags2 & IB_MAC_IOCB_RSP_T) ? "TCP" : "UDP"));
1428 skb->ip_summed = CHECKSUM_NONE;
1429 } else if (qdev->rx_csum &&
1430 ((ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_T) ||
1431 ((ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_U) &&
1432 !(ib_mac_rsp->flags1 & IB_MAC_IOCB_RSP_NU)))) {
1433 QPRINTK(qdev, RX_STATUS, DEBUG, "RX checksum done!\n");
1434 skb->ip_summed = CHECKSUM_UNNECESSARY;
1435 }
1436 qdev->stats.rx_packets++;
1437 qdev->stats.rx_bytes += skb->len;
1438 skb->protocol = eth_type_trans(skb, ndev);
David S. Miller0c8dfc82009-01-27 16:22:32 -08001439 skb_record_rx_queue(skb, rx_ring - &qdev->rx_ring[0]);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001440 if (qdev->vlgrp && (ib_mac_rsp->flags2 & IB_MAC_IOCB_RSP_V)) {
1441 QPRINTK(qdev, RX_STATUS, DEBUG,
1442 "Passing a VLAN packet upstream.\n");
1443 vlan_hwaccel_rx(skb, qdev->vlgrp,
1444 le16_to_cpu(ib_mac_rsp->vlan_id));
1445 } else {
1446 QPRINTK(qdev, RX_STATUS, DEBUG,
1447 "Passing a normal packet upstream.\n");
1448 netif_rx(skb);
1449 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001450}
1451
1452/* Process an outbound completion from an rx ring. */
1453static void ql_process_mac_tx_intr(struct ql_adapter *qdev,
1454 struct ob_mac_iocb_rsp *mac_rsp)
1455{
1456 struct tx_ring *tx_ring;
1457 struct tx_ring_desc *tx_ring_desc;
1458
1459 QL_DUMP_OB_MAC_RSP(mac_rsp);
1460 tx_ring = &qdev->tx_ring[mac_rsp->txq_idx];
1461 tx_ring_desc = &tx_ring->q[mac_rsp->tid];
1462 ql_unmap_send(qdev, tx_ring_desc, tx_ring_desc->map_cnt);
1463 qdev->stats.tx_bytes += tx_ring_desc->map_cnt;
1464 qdev->stats.tx_packets++;
1465 dev_kfree_skb(tx_ring_desc->skb);
1466 tx_ring_desc->skb = NULL;
1467
1468 if (unlikely(mac_rsp->flags1 & (OB_MAC_IOCB_RSP_E |
1469 OB_MAC_IOCB_RSP_S |
1470 OB_MAC_IOCB_RSP_L |
1471 OB_MAC_IOCB_RSP_P | OB_MAC_IOCB_RSP_B))) {
1472 if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_E) {
1473 QPRINTK(qdev, TX_DONE, WARNING,
1474 "Total descriptor length did not match transfer length.\n");
1475 }
1476 if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_S) {
1477 QPRINTK(qdev, TX_DONE, WARNING,
1478 "Frame too short to be legal, not sent.\n");
1479 }
1480 if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_L) {
1481 QPRINTK(qdev, TX_DONE, WARNING,
1482 "Frame too long, but sent anyway.\n");
1483 }
1484 if (mac_rsp->flags1 & OB_MAC_IOCB_RSP_B) {
1485 QPRINTK(qdev, TX_DONE, WARNING,
1486 "PCI backplane error. Frame not sent.\n");
1487 }
1488 }
1489 atomic_inc(&tx_ring->tx_count);
1490}
1491
1492/* Fire up a handler to reset the MPI processor. */
1493void ql_queue_fw_error(struct ql_adapter *qdev)
1494{
1495 netif_stop_queue(qdev->ndev);
1496 netif_carrier_off(qdev->ndev);
1497 queue_delayed_work(qdev->workqueue, &qdev->mpi_reset_work, 0);
1498}
1499
1500void ql_queue_asic_error(struct ql_adapter *qdev)
1501{
1502 netif_stop_queue(qdev->ndev);
1503 netif_carrier_off(qdev->ndev);
1504 ql_disable_interrupts(qdev);
1505 queue_delayed_work(qdev->workqueue, &qdev->asic_reset_work, 0);
1506}
1507
1508static void ql_process_chip_ae_intr(struct ql_adapter *qdev,
1509 struct ib_ae_iocb_rsp *ib_ae_rsp)
1510{
1511 switch (ib_ae_rsp->event) {
1512 case MGMT_ERR_EVENT:
1513 QPRINTK(qdev, RX_ERR, ERR,
1514 "Management Processor Fatal Error.\n");
1515 ql_queue_fw_error(qdev);
1516 return;
1517
1518 case CAM_LOOKUP_ERR_EVENT:
1519 QPRINTK(qdev, LINK, ERR,
1520 "Multiple CAM hits lookup occurred.\n");
1521 QPRINTK(qdev, DRV, ERR, "This event shouldn't occur.\n");
1522 ql_queue_asic_error(qdev);
1523 return;
1524
1525 case SOFT_ECC_ERROR_EVENT:
1526 QPRINTK(qdev, RX_ERR, ERR, "Soft ECC error detected.\n");
1527 ql_queue_asic_error(qdev);
1528 break;
1529
1530 case PCI_ERR_ANON_BUF_RD:
1531 QPRINTK(qdev, RX_ERR, ERR,
1532 "PCI error occurred when reading anonymous buffers from rx_ring %d.\n",
1533 ib_ae_rsp->q_id);
1534 ql_queue_asic_error(qdev);
1535 break;
1536
1537 default:
1538 QPRINTK(qdev, DRV, ERR, "Unexpected event %d.\n",
1539 ib_ae_rsp->event);
1540 ql_queue_asic_error(qdev);
1541 break;
1542 }
1543}
1544
1545static int ql_clean_outbound_rx_ring(struct rx_ring *rx_ring)
1546{
1547 struct ql_adapter *qdev = rx_ring->qdev;
Ron Mercerba7cd3b2009-01-09 11:31:49 +00001548 u32 prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001549 struct ob_mac_iocb_rsp *net_rsp = NULL;
1550 int count = 0;
1551
1552 /* While there are entries in the completion queue. */
1553 while (prod != rx_ring->cnsmr_idx) {
1554
1555 QPRINTK(qdev, RX_STATUS, DEBUG,
1556 "cq_id = %d, prod = %d, cnsmr = %d.\n.", rx_ring->cq_id,
1557 prod, rx_ring->cnsmr_idx);
1558
1559 net_rsp = (struct ob_mac_iocb_rsp *)rx_ring->curr_entry;
1560 rmb();
1561 switch (net_rsp->opcode) {
1562
1563 case OPCODE_OB_MAC_TSO_IOCB:
1564 case OPCODE_OB_MAC_IOCB:
1565 ql_process_mac_tx_intr(qdev, net_rsp);
1566 break;
1567 default:
1568 QPRINTK(qdev, RX_STATUS, DEBUG,
1569 "Hit default case, not handled! dropping the packet, opcode = %x.\n",
1570 net_rsp->opcode);
1571 }
1572 count++;
1573 ql_update_cq(rx_ring);
Ron Mercerba7cd3b2009-01-09 11:31:49 +00001574 prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001575 }
1576 ql_write_cq_idx(rx_ring);
1577 if (netif_queue_stopped(qdev->ndev) && net_rsp != NULL) {
1578 struct tx_ring *tx_ring = &qdev->tx_ring[net_rsp->txq_idx];
1579 if (atomic_read(&tx_ring->queue_stopped) &&
1580 (atomic_read(&tx_ring->tx_count) > (tx_ring->wq_len / 4)))
1581 /*
1582 * The queue got stopped because the tx_ring was full.
1583 * Wake it up, because it's now at least 25% empty.
1584 */
1585 netif_wake_queue(qdev->ndev);
1586 }
1587
1588 return count;
1589}
1590
1591static int ql_clean_inbound_rx_ring(struct rx_ring *rx_ring, int budget)
1592{
1593 struct ql_adapter *qdev = rx_ring->qdev;
Ron Mercerba7cd3b2009-01-09 11:31:49 +00001594 u32 prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001595 struct ql_net_rsp_iocb *net_rsp;
1596 int count = 0;
1597
1598 /* While there are entries in the completion queue. */
1599 while (prod != rx_ring->cnsmr_idx) {
1600
1601 QPRINTK(qdev, RX_STATUS, DEBUG,
1602 "cq_id = %d, prod = %d, cnsmr = %d.\n.", rx_ring->cq_id,
1603 prod, rx_ring->cnsmr_idx);
1604
1605 net_rsp = rx_ring->curr_entry;
1606 rmb();
1607 switch (net_rsp->opcode) {
1608 case OPCODE_IB_MAC_IOCB:
1609 ql_process_mac_rx_intr(qdev, rx_ring,
1610 (struct ib_mac_iocb_rsp *)
1611 net_rsp);
1612 break;
1613
1614 case OPCODE_IB_AE_IOCB:
1615 ql_process_chip_ae_intr(qdev, (struct ib_ae_iocb_rsp *)
1616 net_rsp);
1617 break;
1618 default:
1619 {
1620 QPRINTK(qdev, RX_STATUS, DEBUG,
1621 "Hit default case, not handled! dropping the packet, opcode = %x.\n",
1622 net_rsp->opcode);
1623 }
1624 }
1625 count++;
1626 ql_update_cq(rx_ring);
Ron Mercerba7cd3b2009-01-09 11:31:49 +00001627 prod = ql_read_sh_reg(rx_ring->prod_idx_sh_reg);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001628 if (count == budget)
1629 break;
1630 }
1631 ql_update_buffer_queues(qdev, rx_ring);
1632 ql_write_cq_idx(rx_ring);
1633 return count;
1634}
1635
1636static int ql_napi_poll_msix(struct napi_struct *napi, int budget)
1637{
1638 struct rx_ring *rx_ring = container_of(napi, struct rx_ring, napi);
1639 struct ql_adapter *qdev = rx_ring->qdev;
1640 int work_done = ql_clean_inbound_rx_ring(rx_ring, budget);
1641
1642 QPRINTK(qdev, RX_STATUS, DEBUG, "Enter, NAPI POLL cq_id = %d.\n",
1643 rx_ring->cq_id);
1644
1645 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001646 __napi_complete(napi);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001647 ql_enable_completion_interrupt(qdev, rx_ring->irq);
1648 }
1649 return work_done;
1650}
1651
1652static void ql_vlan_rx_register(struct net_device *ndev, struct vlan_group *grp)
1653{
1654 struct ql_adapter *qdev = netdev_priv(ndev);
1655
1656 qdev->vlgrp = grp;
1657 if (grp) {
1658 QPRINTK(qdev, IFUP, DEBUG, "Turning on VLAN in NIC_RCV_CFG.\n");
1659 ql_write32(qdev, NIC_RCV_CFG, NIC_RCV_CFG_VLAN_MASK |
1660 NIC_RCV_CFG_VLAN_MATCH_AND_NON);
1661 } else {
1662 QPRINTK(qdev, IFUP, DEBUG,
1663 "Turning off VLAN in NIC_RCV_CFG.\n");
1664 ql_write32(qdev, NIC_RCV_CFG, NIC_RCV_CFG_VLAN_MASK);
1665 }
1666}
1667
1668static void ql_vlan_rx_add_vid(struct net_device *ndev, u16 vid)
1669{
1670 struct ql_adapter *qdev = netdev_priv(ndev);
1671 u32 enable_bit = MAC_ADDR_E;
1672
1673 spin_lock(&qdev->hw_lock);
1674 if (ql_set_mac_addr_reg
1675 (qdev, (u8 *) &enable_bit, MAC_ADDR_TYPE_VLAN, vid)) {
1676 QPRINTK(qdev, IFUP, ERR, "Failed to init vlan address.\n");
1677 }
1678 spin_unlock(&qdev->hw_lock);
1679}
1680
1681static void ql_vlan_rx_kill_vid(struct net_device *ndev, u16 vid)
1682{
1683 struct ql_adapter *qdev = netdev_priv(ndev);
1684 u32 enable_bit = 0;
1685
1686 spin_lock(&qdev->hw_lock);
1687 if (ql_set_mac_addr_reg
1688 (qdev, (u8 *) &enable_bit, MAC_ADDR_TYPE_VLAN, vid)) {
1689 QPRINTK(qdev, IFUP, ERR, "Failed to clear vlan address.\n");
1690 }
1691 spin_unlock(&qdev->hw_lock);
1692
1693}
1694
1695/* Worker thread to process a given rx_ring that is dedicated
1696 * to outbound completions.
1697 */
1698static void ql_tx_clean(struct work_struct *work)
1699{
1700 struct rx_ring *rx_ring =
1701 container_of(work, struct rx_ring, rx_work.work);
1702 ql_clean_outbound_rx_ring(rx_ring);
1703 ql_enable_completion_interrupt(rx_ring->qdev, rx_ring->irq);
1704
1705}
1706
1707/* Worker thread to process a given rx_ring that is dedicated
1708 * to inbound completions.
1709 */
1710static void ql_rx_clean(struct work_struct *work)
1711{
1712 struct rx_ring *rx_ring =
1713 container_of(work, struct rx_ring, rx_work.work);
1714 ql_clean_inbound_rx_ring(rx_ring, 64);
1715 ql_enable_completion_interrupt(rx_ring->qdev, rx_ring->irq);
1716}
1717
1718/* MSI-X Multiple Vector Interrupt Handler for outbound completions. */
1719static irqreturn_t qlge_msix_tx_isr(int irq, void *dev_id)
1720{
1721 struct rx_ring *rx_ring = dev_id;
1722 queue_delayed_work_on(rx_ring->cpu, rx_ring->qdev->q_workqueue,
1723 &rx_ring->rx_work, 0);
1724 return IRQ_HANDLED;
1725}
1726
1727/* MSI-X Multiple Vector Interrupt Handler for inbound completions. */
1728static irqreturn_t qlge_msix_rx_isr(int irq, void *dev_id)
1729{
1730 struct rx_ring *rx_ring = dev_id;
Ben Hutchings288379f2009-01-19 16:43:59 -08001731 napi_schedule(&rx_ring->napi);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001732 return IRQ_HANDLED;
1733}
1734
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001735/* This handles a fatal error, MPI activity, and the default
1736 * rx_ring in an MSI-X multiple vector environment.
1737 * In MSI/Legacy environment it also process the rest of
1738 * the rx_rings.
1739 */
1740static irqreturn_t qlge_isr(int irq, void *dev_id)
1741{
1742 struct rx_ring *rx_ring = dev_id;
1743 struct ql_adapter *qdev = rx_ring->qdev;
1744 struct intr_context *intr_context = &qdev->intr_context[0];
1745 u32 var;
1746 int i;
1747 int work_done = 0;
1748
Ron Mercerbb0d2152008-10-20 10:30:26 -07001749 spin_lock(&qdev->hw_lock);
1750 if (atomic_read(&qdev->intr_context[0].irq_cnt)) {
1751 QPRINTK(qdev, INTR, DEBUG, "Shared Interrupt, Not ours!\n");
1752 spin_unlock(&qdev->hw_lock);
1753 return IRQ_NONE;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001754 }
Ron Mercerbb0d2152008-10-20 10:30:26 -07001755 spin_unlock(&qdev->hw_lock);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001756
Ron Mercerbb0d2152008-10-20 10:30:26 -07001757 var = ql_disable_completion_interrupt(qdev, intr_context->intr);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001758
1759 /*
1760 * Check for fatal error.
1761 */
1762 if (var & STS_FE) {
1763 ql_queue_asic_error(qdev);
1764 QPRINTK(qdev, INTR, ERR, "Got fatal error, STS = %x.\n", var);
1765 var = ql_read32(qdev, ERR_STS);
1766 QPRINTK(qdev, INTR, ERR,
1767 "Resetting chip. Error Status Register = 0x%x\n", var);
1768 return IRQ_HANDLED;
1769 }
1770
1771 /*
1772 * Check MPI processor activity.
1773 */
1774 if (var & STS_PI) {
1775 /*
1776 * We've got an async event or mailbox completion.
1777 * Handle it and clear the source of the interrupt.
1778 */
1779 QPRINTK(qdev, INTR, ERR, "Got MPI processor interrupt.\n");
1780 ql_disable_completion_interrupt(qdev, intr_context->intr);
1781 queue_delayed_work_on(smp_processor_id(), qdev->workqueue,
1782 &qdev->mpi_work, 0);
1783 work_done++;
1784 }
1785
1786 /*
1787 * Check the default queue and wake handler if active.
1788 */
1789 rx_ring = &qdev->rx_ring[0];
Ron Mercerba7cd3b2009-01-09 11:31:49 +00001790 if (ql_read_sh_reg(rx_ring->prod_idx_sh_reg) != rx_ring->cnsmr_idx) {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001791 QPRINTK(qdev, INTR, INFO, "Waking handler for rx_ring[0].\n");
1792 ql_disable_completion_interrupt(qdev, intr_context->intr);
1793 queue_delayed_work_on(smp_processor_id(), qdev->q_workqueue,
1794 &rx_ring->rx_work, 0);
1795 work_done++;
1796 }
1797
1798 if (!test_bit(QL_MSIX_ENABLED, &qdev->flags)) {
1799 /*
1800 * Start the DPC for each active queue.
1801 */
1802 for (i = 1; i < qdev->rx_ring_count; i++) {
1803 rx_ring = &qdev->rx_ring[i];
Ron Mercerba7cd3b2009-01-09 11:31:49 +00001804 if (ql_read_sh_reg(rx_ring->prod_idx_sh_reg) !=
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001805 rx_ring->cnsmr_idx) {
1806 QPRINTK(qdev, INTR, INFO,
1807 "Waking handler for rx_ring[%d].\n", i);
1808 ql_disable_completion_interrupt(qdev,
1809 intr_context->
1810 intr);
1811 if (i < qdev->rss_ring_first_cq_id)
1812 queue_delayed_work_on(rx_ring->cpu,
1813 qdev->q_workqueue,
1814 &rx_ring->rx_work,
1815 0);
1816 else
Ben Hutchings288379f2009-01-19 16:43:59 -08001817 napi_schedule(&rx_ring->napi);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001818 work_done++;
1819 }
1820 }
1821 }
Ron Mercerbb0d2152008-10-20 10:30:26 -07001822 ql_enable_completion_interrupt(qdev, intr_context->intr);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001823 return work_done ? IRQ_HANDLED : IRQ_NONE;
1824}
1825
1826static int ql_tso(struct sk_buff *skb, struct ob_mac_tso_iocb_req *mac_iocb_ptr)
1827{
1828
1829 if (skb_is_gso(skb)) {
1830 int err;
1831 if (skb_header_cloned(skb)) {
1832 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1833 if (err)
1834 return err;
1835 }
1836
1837 mac_iocb_ptr->opcode = OPCODE_OB_MAC_TSO_IOCB;
1838 mac_iocb_ptr->flags3 |= OB_MAC_TSO_IOCB_IC;
1839 mac_iocb_ptr->frame_len = cpu_to_le32((u32) skb->len);
1840 mac_iocb_ptr->total_hdrs_len =
1841 cpu_to_le16(skb_transport_offset(skb) + tcp_hdrlen(skb));
1842 mac_iocb_ptr->net_trans_offset =
1843 cpu_to_le16(skb_network_offset(skb) |
1844 skb_transport_offset(skb)
1845 << OB_MAC_TRANSPORT_HDR_SHIFT);
1846 mac_iocb_ptr->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
1847 mac_iocb_ptr->flags2 |= OB_MAC_TSO_IOCB_LSO;
1848 if (likely(skb->protocol == htons(ETH_P_IP))) {
1849 struct iphdr *iph = ip_hdr(skb);
1850 iph->check = 0;
1851 mac_iocb_ptr->flags1 |= OB_MAC_TSO_IOCB_IP4;
1852 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
1853 iph->daddr, 0,
1854 IPPROTO_TCP,
1855 0);
1856 } else if (skb->protocol == htons(ETH_P_IPV6)) {
1857 mac_iocb_ptr->flags1 |= OB_MAC_TSO_IOCB_IP6;
1858 tcp_hdr(skb)->check =
1859 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1860 &ipv6_hdr(skb)->daddr,
1861 0, IPPROTO_TCP, 0);
1862 }
1863 return 1;
1864 }
1865 return 0;
1866}
1867
1868static void ql_hw_csum_setup(struct sk_buff *skb,
1869 struct ob_mac_tso_iocb_req *mac_iocb_ptr)
1870{
1871 int len;
1872 struct iphdr *iph = ip_hdr(skb);
Ron Mercerfd2df4f2009-01-05 18:18:45 -08001873 __sum16 *check;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04001874 mac_iocb_ptr->opcode = OPCODE_OB_MAC_TSO_IOCB;
1875 mac_iocb_ptr->frame_len = cpu_to_le32((u32) skb->len);
1876 mac_iocb_ptr->net_trans_offset =
1877 cpu_to_le16(skb_network_offset(skb) |
1878 skb_transport_offset(skb) << OB_MAC_TRANSPORT_HDR_SHIFT);
1879
1880 mac_iocb_ptr->flags1 |= OB_MAC_TSO_IOCB_IP4;
1881 len = (ntohs(iph->tot_len) - (iph->ihl << 2));
1882 if (likely(iph->protocol == IPPROTO_TCP)) {
1883 check = &(tcp_hdr(skb)->check);
1884 mac_iocb_ptr->flags2 |= OB_MAC_TSO_IOCB_TC;
1885 mac_iocb_ptr->total_hdrs_len =
1886 cpu_to_le16(skb_transport_offset(skb) +
1887 (tcp_hdr(skb)->doff << 2));
1888 } else {
1889 check = &(udp_hdr(skb)->check);
1890 mac_iocb_ptr->flags2 |= OB_MAC_TSO_IOCB_UC;
1891 mac_iocb_ptr->total_hdrs_len =
1892 cpu_to_le16(skb_transport_offset(skb) +
1893 sizeof(struct udphdr));
1894 }
1895 *check = ~csum_tcpudp_magic(iph->saddr,
1896 iph->daddr, len, iph->protocol, 0);
1897}
1898
1899static int qlge_send(struct sk_buff *skb, struct net_device *ndev)
1900{
1901 struct tx_ring_desc *tx_ring_desc;
1902 struct ob_mac_iocb_req *mac_iocb_ptr;
1903 struct ql_adapter *qdev = netdev_priv(ndev);
1904 int tso;
1905 struct tx_ring *tx_ring;
1906 u32 tx_ring_idx = (u32) QL_TXQ_IDX(qdev, skb);
1907
1908 tx_ring = &qdev->tx_ring[tx_ring_idx];
1909
1910 if (unlikely(atomic_read(&tx_ring->tx_count) < 2)) {
1911 QPRINTK(qdev, TX_QUEUED, INFO,
1912 "%s: shutting down tx queue %d du to lack of resources.\n",
1913 __func__, tx_ring_idx);
1914 netif_stop_queue(ndev);
1915 atomic_inc(&tx_ring->queue_stopped);
1916 return NETDEV_TX_BUSY;
1917 }
1918 tx_ring_desc = &tx_ring->q[tx_ring->prod_idx];
1919 mac_iocb_ptr = tx_ring_desc->queue_entry;
1920 memset((void *)mac_iocb_ptr, 0, sizeof(mac_iocb_ptr));
1921 if (ql_map_send(qdev, mac_iocb_ptr, skb, tx_ring_desc) != NETDEV_TX_OK) {
1922 QPRINTK(qdev, TX_QUEUED, ERR, "Could not map the segments.\n");
1923 return NETDEV_TX_BUSY;
1924 }
1925
1926 mac_iocb_ptr->opcode = OPCODE_OB_MAC_IOCB;
1927 mac_iocb_ptr->tid = tx_ring_desc->index;
1928 /* We use the upper 32-bits to store the tx queue for this IO.
1929 * When we get the completion we can use it to establish the context.
1930 */
1931 mac_iocb_ptr->txq_idx = tx_ring_idx;
1932 tx_ring_desc->skb = skb;
1933
1934 mac_iocb_ptr->frame_len = cpu_to_le16((u16) skb->len);
1935
1936 if (qdev->vlgrp && vlan_tx_tag_present(skb)) {
1937 QPRINTK(qdev, TX_QUEUED, DEBUG, "Adding a vlan tag %d.\n",
1938 vlan_tx_tag_get(skb));
1939 mac_iocb_ptr->flags3 |= OB_MAC_IOCB_V;
1940 mac_iocb_ptr->vlan_tci = cpu_to_le16(vlan_tx_tag_get(skb));
1941 }
1942 tso = ql_tso(skb, (struct ob_mac_tso_iocb_req *)mac_iocb_ptr);
1943 if (tso < 0) {
1944 dev_kfree_skb_any(skb);
1945 return NETDEV_TX_OK;
1946 } else if (unlikely(!tso) && (skb->ip_summed == CHECKSUM_PARTIAL)) {
1947 ql_hw_csum_setup(skb,
1948 (struct ob_mac_tso_iocb_req *)mac_iocb_ptr);
1949 }
1950 QL_DUMP_OB_MAC_IOCB(mac_iocb_ptr);
1951 tx_ring->prod_idx++;
1952 if (tx_ring->prod_idx == tx_ring->wq_len)
1953 tx_ring->prod_idx = 0;
1954 wmb();
1955
1956 ql_write_db_reg(tx_ring->prod_idx, tx_ring->prod_idx_db_reg);
1957 ndev->trans_start = jiffies;
1958 QPRINTK(qdev, TX_QUEUED, DEBUG, "tx queued, slot %d, len %d\n",
1959 tx_ring->prod_idx, skb->len);
1960
1961 atomic_dec(&tx_ring->tx_count);
1962 return NETDEV_TX_OK;
1963}
1964
1965static void ql_free_shadow_space(struct ql_adapter *qdev)
1966{
1967 if (qdev->rx_ring_shadow_reg_area) {
1968 pci_free_consistent(qdev->pdev,
1969 PAGE_SIZE,
1970 qdev->rx_ring_shadow_reg_area,
1971 qdev->rx_ring_shadow_reg_dma);
1972 qdev->rx_ring_shadow_reg_area = NULL;
1973 }
1974 if (qdev->tx_ring_shadow_reg_area) {
1975 pci_free_consistent(qdev->pdev,
1976 PAGE_SIZE,
1977 qdev->tx_ring_shadow_reg_area,
1978 qdev->tx_ring_shadow_reg_dma);
1979 qdev->tx_ring_shadow_reg_area = NULL;
1980 }
1981}
1982
1983static int ql_alloc_shadow_space(struct ql_adapter *qdev)
1984{
1985 qdev->rx_ring_shadow_reg_area =
1986 pci_alloc_consistent(qdev->pdev,
1987 PAGE_SIZE, &qdev->rx_ring_shadow_reg_dma);
1988 if (qdev->rx_ring_shadow_reg_area == NULL) {
1989 QPRINTK(qdev, IFUP, ERR,
1990 "Allocation of RX shadow space failed.\n");
1991 return -ENOMEM;
1992 }
1993 qdev->tx_ring_shadow_reg_area =
1994 pci_alloc_consistent(qdev->pdev, PAGE_SIZE,
1995 &qdev->tx_ring_shadow_reg_dma);
1996 if (qdev->tx_ring_shadow_reg_area == NULL) {
1997 QPRINTK(qdev, IFUP, ERR,
1998 "Allocation of TX shadow space failed.\n");
1999 goto err_wqp_sh_area;
2000 }
2001 return 0;
2002
2003err_wqp_sh_area:
2004 pci_free_consistent(qdev->pdev,
2005 PAGE_SIZE,
2006 qdev->rx_ring_shadow_reg_area,
2007 qdev->rx_ring_shadow_reg_dma);
2008 return -ENOMEM;
2009}
2010
2011static void ql_init_tx_ring(struct ql_adapter *qdev, struct tx_ring *tx_ring)
2012{
2013 struct tx_ring_desc *tx_ring_desc;
2014 int i;
2015 struct ob_mac_iocb_req *mac_iocb_ptr;
2016
2017 mac_iocb_ptr = tx_ring->wq_base;
2018 tx_ring_desc = tx_ring->q;
2019 for (i = 0; i < tx_ring->wq_len; i++) {
2020 tx_ring_desc->index = i;
2021 tx_ring_desc->skb = NULL;
2022 tx_ring_desc->queue_entry = mac_iocb_ptr;
2023 mac_iocb_ptr++;
2024 tx_ring_desc++;
2025 }
2026 atomic_set(&tx_ring->tx_count, tx_ring->wq_len);
2027 atomic_set(&tx_ring->queue_stopped, 0);
2028}
2029
2030static void ql_free_tx_resources(struct ql_adapter *qdev,
2031 struct tx_ring *tx_ring)
2032{
2033 if (tx_ring->wq_base) {
2034 pci_free_consistent(qdev->pdev, tx_ring->wq_size,
2035 tx_ring->wq_base, tx_ring->wq_base_dma);
2036 tx_ring->wq_base = NULL;
2037 }
2038 kfree(tx_ring->q);
2039 tx_ring->q = NULL;
2040}
2041
2042static int ql_alloc_tx_resources(struct ql_adapter *qdev,
2043 struct tx_ring *tx_ring)
2044{
2045 tx_ring->wq_base =
2046 pci_alloc_consistent(qdev->pdev, tx_ring->wq_size,
2047 &tx_ring->wq_base_dma);
2048
2049 if ((tx_ring->wq_base == NULL)
2050 || tx_ring->wq_base_dma & (tx_ring->wq_size - 1)) {
2051 QPRINTK(qdev, IFUP, ERR, "tx_ring alloc failed.\n");
2052 return -ENOMEM;
2053 }
2054 tx_ring->q =
2055 kmalloc(tx_ring->wq_len * sizeof(struct tx_ring_desc), GFP_KERNEL);
2056 if (tx_ring->q == NULL)
2057 goto err;
2058
2059 return 0;
2060err:
2061 pci_free_consistent(qdev->pdev, tx_ring->wq_size,
2062 tx_ring->wq_base, tx_ring->wq_base_dma);
2063 return -ENOMEM;
2064}
2065
Stephen Hemminger8668ae92008-11-21 17:29:50 -08002066static void ql_free_lbq_buffers(struct ql_adapter *qdev, struct rx_ring *rx_ring)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002067{
2068 int i;
2069 struct bq_desc *lbq_desc;
2070
2071 for (i = 0; i < rx_ring->lbq_len; i++) {
2072 lbq_desc = &rx_ring->lbq[i];
2073 if (lbq_desc->p.lbq_page) {
2074 pci_unmap_page(qdev->pdev,
2075 pci_unmap_addr(lbq_desc, mapaddr),
2076 pci_unmap_len(lbq_desc, maplen),
2077 PCI_DMA_FROMDEVICE);
2078
2079 put_page(lbq_desc->p.lbq_page);
2080 lbq_desc->p.lbq_page = NULL;
2081 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002082 }
2083}
2084
2085/*
2086 * Allocate and map a page for each element of the lbq.
2087 */
2088static int ql_alloc_lbq_buffers(struct ql_adapter *qdev,
2089 struct rx_ring *rx_ring)
2090{
2091 int i;
2092 struct bq_desc *lbq_desc;
2093 u64 map;
Ron Mercer2c9a0d42009-01-05 18:19:20 -08002094 __le64 *bq = rx_ring->lbq_base;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002095
2096 for (i = 0; i < rx_ring->lbq_len; i++) {
2097 lbq_desc = &rx_ring->lbq[i];
2098 memset(lbq_desc, 0, sizeof(lbq_desc));
Ron Mercer2c9a0d42009-01-05 18:19:20 -08002099 lbq_desc->addr = bq;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002100 lbq_desc->index = i;
2101 lbq_desc->p.lbq_page = alloc_page(GFP_ATOMIC);
2102 if (unlikely(!lbq_desc->p.lbq_page)) {
2103 QPRINTK(qdev, IFUP, ERR, "failed alloc_page().\n");
2104 goto mem_error;
2105 } else {
2106 map = pci_map_page(qdev->pdev,
2107 lbq_desc->p.lbq_page,
2108 0, PAGE_SIZE, PCI_DMA_FROMDEVICE);
2109 if (pci_dma_mapping_error(qdev->pdev, map)) {
2110 QPRINTK(qdev, IFUP, ERR,
2111 "PCI mapping failed.\n");
2112 goto mem_error;
2113 }
2114 pci_unmap_addr_set(lbq_desc, mapaddr, map);
2115 pci_unmap_len_set(lbq_desc, maplen, PAGE_SIZE);
Ron Mercer2c9a0d42009-01-05 18:19:20 -08002116 *lbq_desc->addr = cpu_to_le64(map);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002117 }
2118 bq++;
2119 }
2120 return 0;
2121mem_error:
2122 ql_free_lbq_buffers(qdev, rx_ring);
2123 return -ENOMEM;
2124}
2125
Stephen Hemminger8668ae92008-11-21 17:29:50 -08002126static void ql_free_sbq_buffers(struct ql_adapter *qdev, struct rx_ring *rx_ring)
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002127{
2128 int i;
2129 struct bq_desc *sbq_desc;
2130
2131 for (i = 0; i < rx_ring->sbq_len; i++) {
2132 sbq_desc = &rx_ring->sbq[i];
2133 if (sbq_desc == NULL) {
2134 QPRINTK(qdev, IFUP, ERR, "sbq_desc %d is NULL.\n", i);
2135 return;
2136 }
2137 if (sbq_desc->p.skb) {
2138 pci_unmap_single(qdev->pdev,
2139 pci_unmap_addr(sbq_desc, mapaddr),
2140 pci_unmap_len(sbq_desc, maplen),
2141 PCI_DMA_FROMDEVICE);
2142 dev_kfree_skb(sbq_desc->p.skb);
2143 sbq_desc->p.skb = NULL;
2144 }
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002145 }
2146}
2147
2148/* Allocate and map an skb for each element of the sbq. */
2149static int ql_alloc_sbq_buffers(struct ql_adapter *qdev,
2150 struct rx_ring *rx_ring)
2151{
2152 int i;
2153 struct bq_desc *sbq_desc;
2154 struct sk_buff *skb;
2155 u64 map;
Ron Mercer2c9a0d42009-01-05 18:19:20 -08002156 __le64 *bq = rx_ring->sbq_base;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002157
2158 for (i = 0; i < rx_ring->sbq_len; i++) {
2159 sbq_desc = &rx_ring->sbq[i];
2160 memset(sbq_desc, 0, sizeof(sbq_desc));
2161 sbq_desc->index = i;
Ron Mercer2c9a0d42009-01-05 18:19:20 -08002162 sbq_desc->addr = bq;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002163 skb = netdev_alloc_skb(qdev->ndev, rx_ring->sbq_buf_size);
2164 if (unlikely(!skb)) {
2165 /* Better luck next round */
2166 QPRINTK(qdev, IFUP, ERR,
2167 "small buff alloc failed for %d bytes at index %d.\n",
2168 rx_ring->sbq_buf_size, i);
2169 goto mem_err;
2170 }
2171 skb_reserve(skb, QLGE_SB_PAD);
2172 sbq_desc->p.skb = skb;
2173 /*
2174 * Map only half the buffer. Because the
2175 * other half may get some data copied to it
2176 * when the completion arrives.
2177 */
2178 map = pci_map_single(qdev->pdev,
2179 skb->data,
2180 rx_ring->sbq_buf_size / 2,
2181 PCI_DMA_FROMDEVICE);
2182 if (pci_dma_mapping_error(qdev->pdev, map)) {
2183 QPRINTK(qdev, IFUP, ERR, "PCI mapping failed.\n");
2184 goto mem_err;
2185 }
2186 pci_unmap_addr_set(sbq_desc, mapaddr, map);
2187 pci_unmap_len_set(sbq_desc, maplen, rx_ring->sbq_buf_size / 2);
Ron Mercer2c9a0d42009-01-05 18:19:20 -08002188 *sbq_desc->addr = cpu_to_le64(map);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002189 bq++;
2190 }
2191 return 0;
2192mem_err:
2193 ql_free_sbq_buffers(qdev, rx_ring);
2194 return -ENOMEM;
2195}
2196
2197static void ql_free_rx_resources(struct ql_adapter *qdev,
2198 struct rx_ring *rx_ring)
2199{
2200 if (rx_ring->sbq_len)
2201 ql_free_sbq_buffers(qdev, rx_ring);
2202 if (rx_ring->lbq_len)
2203 ql_free_lbq_buffers(qdev, rx_ring);
2204
2205 /* Free the small buffer queue. */
2206 if (rx_ring->sbq_base) {
2207 pci_free_consistent(qdev->pdev,
2208 rx_ring->sbq_size,
2209 rx_ring->sbq_base, rx_ring->sbq_base_dma);
2210 rx_ring->sbq_base = NULL;
2211 }
2212
2213 /* Free the small buffer queue control blocks. */
2214 kfree(rx_ring->sbq);
2215 rx_ring->sbq = NULL;
2216
2217 /* Free the large buffer queue. */
2218 if (rx_ring->lbq_base) {
2219 pci_free_consistent(qdev->pdev,
2220 rx_ring->lbq_size,
2221 rx_ring->lbq_base, rx_ring->lbq_base_dma);
2222 rx_ring->lbq_base = NULL;
2223 }
2224
2225 /* Free the large buffer queue control blocks. */
2226 kfree(rx_ring->lbq);
2227 rx_ring->lbq = NULL;
2228
2229 /* Free the rx queue. */
2230 if (rx_ring->cq_base) {
2231 pci_free_consistent(qdev->pdev,
2232 rx_ring->cq_size,
2233 rx_ring->cq_base, rx_ring->cq_base_dma);
2234 rx_ring->cq_base = NULL;
2235 }
2236}
2237
2238/* Allocate queues and buffers for this completions queue based
2239 * on the values in the parameter structure. */
2240static int ql_alloc_rx_resources(struct ql_adapter *qdev,
2241 struct rx_ring *rx_ring)
2242{
2243
2244 /*
2245 * Allocate the completion queue for this rx_ring.
2246 */
2247 rx_ring->cq_base =
2248 pci_alloc_consistent(qdev->pdev, rx_ring->cq_size,
2249 &rx_ring->cq_base_dma);
2250
2251 if (rx_ring->cq_base == NULL) {
2252 QPRINTK(qdev, IFUP, ERR, "rx_ring alloc failed.\n");
2253 return -ENOMEM;
2254 }
2255
2256 if (rx_ring->sbq_len) {
2257 /*
2258 * Allocate small buffer queue.
2259 */
2260 rx_ring->sbq_base =
2261 pci_alloc_consistent(qdev->pdev, rx_ring->sbq_size,
2262 &rx_ring->sbq_base_dma);
2263
2264 if (rx_ring->sbq_base == NULL) {
2265 QPRINTK(qdev, IFUP, ERR,
2266 "Small buffer queue allocation failed.\n");
2267 goto err_mem;
2268 }
2269
2270 /*
2271 * Allocate small buffer queue control blocks.
2272 */
2273 rx_ring->sbq =
2274 kmalloc(rx_ring->sbq_len * sizeof(struct bq_desc),
2275 GFP_KERNEL);
2276 if (rx_ring->sbq == NULL) {
2277 QPRINTK(qdev, IFUP, ERR,
2278 "Small buffer queue control block allocation failed.\n");
2279 goto err_mem;
2280 }
2281
2282 if (ql_alloc_sbq_buffers(qdev, rx_ring)) {
2283 QPRINTK(qdev, IFUP, ERR,
2284 "Small buffer allocation failed.\n");
2285 goto err_mem;
2286 }
2287 }
2288
2289 if (rx_ring->lbq_len) {
2290 /*
2291 * Allocate large buffer queue.
2292 */
2293 rx_ring->lbq_base =
2294 pci_alloc_consistent(qdev->pdev, rx_ring->lbq_size,
2295 &rx_ring->lbq_base_dma);
2296
2297 if (rx_ring->lbq_base == NULL) {
2298 QPRINTK(qdev, IFUP, ERR,
2299 "Large buffer queue allocation failed.\n");
2300 goto err_mem;
2301 }
2302 /*
2303 * Allocate large buffer queue control blocks.
2304 */
2305 rx_ring->lbq =
2306 kmalloc(rx_ring->lbq_len * sizeof(struct bq_desc),
2307 GFP_KERNEL);
2308 if (rx_ring->lbq == NULL) {
2309 QPRINTK(qdev, IFUP, ERR,
2310 "Large buffer queue control block allocation failed.\n");
2311 goto err_mem;
2312 }
2313
2314 /*
2315 * Allocate the buffers.
2316 */
2317 if (ql_alloc_lbq_buffers(qdev, rx_ring)) {
2318 QPRINTK(qdev, IFUP, ERR,
2319 "Large buffer allocation failed.\n");
2320 goto err_mem;
2321 }
2322 }
2323
2324 return 0;
2325
2326err_mem:
2327 ql_free_rx_resources(qdev, rx_ring);
2328 return -ENOMEM;
2329}
2330
2331static void ql_tx_ring_clean(struct ql_adapter *qdev)
2332{
2333 struct tx_ring *tx_ring;
2334 struct tx_ring_desc *tx_ring_desc;
2335 int i, j;
2336
2337 /*
2338 * Loop through all queues and free
2339 * any resources.
2340 */
2341 for (j = 0; j < qdev->tx_ring_count; j++) {
2342 tx_ring = &qdev->tx_ring[j];
2343 for (i = 0; i < tx_ring->wq_len; i++) {
2344 tx_ring_desc = &tx_ring->q[i];
2345 if (tx_ring_desc && tx_ring_desc->skb) {
2346 QPRINTK(qdev, IFDOWN, ERR,
2347 "Freeing lost SKB %p, from queue %d, index %d.\n",
2348 tx_ring_desc->skb, j,
2349 tx_ring_desc->index);
2350 ql_unmap_send(qdev, tx_ring_desc,
2351 tx_ring_desc->map_cnt);
2352 dev_kfree_skb(tx_ring_desc->skb);
2353 tx_ring_desc->skb = NULL;
2354 }
2355 }
2356 }
2357}
2358
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002359static void ql_free_mem_resources(struct ql_adapter *qdev)
2360{
2361 int i;
2362
2363 for (i = 0; i < qdev->tx_ring_count; i++)
2364 ql_free_tx_resources(qdev, &qdev->tx_ring[i]);
2365 for (i = 0; i < qdev->rx_ring_count; i++)
2366 ql_free_rx_resources(qdev, &qdev->rx_ring[i]);
2367 ql_free_shadow_space(qdev);
2368}
2369
2370static int ql_alloc_mem_resources(struct ql_adapter *qdev)
2371{
2372 int i;
2373
2374 /* Allocate space for our shadow registers and such. */
2375 if (ql_alloc_shadow_space(qdev))
2376 return -ENOMEM;
2377
2378 for (i = 0; i < qdev->rx_ring_count; i++) {
2379 if (ql_alloc_rx_resources(qdev, &qdev->rx_ring[i]) != 0) {
2380 QPRINTK(qdev, IFUP, ERR,
2381 "RX resource allocation failed.\n");
2382 goto err_mem;
2383 }
2384 }
2385 /* Allocate tx queue resources */
2386 for (i = 0; i < qdev->tx_ring_count; i++) {
2387 if (ql_alloc_tx_resources(qdev, &qdev->tx_ring[i]) != 0) {
2388 QPRINTK(qdev, IFUP, ERR,
2389 "TX resource allocation failed.\n");
2390 goto err_mem;
2391 }
2392 }
2393 return 0;
2394
2395err_mem:
2396 ql_free_mem_resources(qdev);
2397 return -ENOMEM;
2398}
2399
2400/* Set up the rx ring control block and pass it to the chip.
2401 * The control block is defined as
2402 * "Completion Queue Initialization Control Block", or cqicb.
2403 */
2404static int ql_start_rx_ring(struct ql_adapter *qdev, struct rx_ring *rx_ring)
2405{
2406 struct cqicb *cqicb = &rx_ring->cqicb;
2407 void *shadow_reg = qdev->rx_ring_shadow_reg_area +
2408 (rx_ring->cq_id * sizeof(u64) * 4);
2409 u64 shadow_reg_dma = qdev->rx_ring_shadow_reg_dma +
2410 (rx_ring->cq_id * sizeof(u64) * 4);
2411 void __iomem *doorbell_area =
2412 qdev->doorbell_area + (DB_PAGE_SIZE * (128 + rx_ring->cq_id));
2413 int err = 0;
2414 u16 bq_len;
2415
2416 /* Set up the shadow registers for this ring. */
2417 rx_ring->prod_idx_sh_reg = shadow_reg;
2418 rx_ring->prod_idx_sh_reg_dma = shadow_reg_dma;
2419 shadow_reg += sizeof(u64);
2420 shadow_reg_dma += sizeof(u64);
2421 rx_ring->lbq_base_indirect = shadow_reg;
2422 rx_ring->lbq_base_indirect_dma = shadow_reg_dma;
2423 shadow_reg += sizeof(u64);
2424 shadow_reg_dma += sizeof(u64);
2425 rx_ring->sbq_base_indirect = shadow_reg;
2426 rx_ring->sbq_base_indirect_dma = shadow_reg_dma;
2427
2428 /* PCI doorbell mem area + 0x00 for consumer index register */
Stephen Hemminger8668ae92008-11-21 17:29:50 -08002429 rx_ring->cnsmr_idx_db_reg = (u32 __iomem *) doorbell_area;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002430 rx_ring->cnsmr_idx = 0;
2431 rx_ring->curr_entry = rx_ring->cq_base;
2432
2433 /* PCI doorbell mem area + 0x04 for valid register */
2434 rx_ring->valid_db_reg = doorbell_area + 0x04;
2435
2436 /* PCI doorbell mem area + 0x18 for large buffer consumer */
Stephen Hemminger8668ae92008-11-21 17:29:50 -08002437 rx_ring->lbq_prod_idx_db_reg = (u32 __iomem *) (doorbell_area + 0x18);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002438
2439 /* PCI doorbell mem area + 0x1c */
Stephen Hemminger8668ae92008-11-21 17:29:50 -08002440 rx_ring->sbq_prod_idx_db_reg = (u32 __iomem *) (doorbell_area + 0x1c);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002441
2442 memset((void *)cqicb, 0, sizeof(struct cqicb));
2443 cqicb->msix_vect = rx_ring->irq;
2444
Ron Mercer459caf52009-01-04 17:08:11 -08002445 bq_len = (rx_ring->cq_len == 65536) ? 0 : (u16) rx_ring->cq_len;
2446 cqicb->len = cpu_to_le16(bq_len | LEN_V | LEN_CPP_CONT);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002447
Ron Mercer97345522009-01-09 11:31:50 +00002448 cqicb->addr = cpu_to_le64(rx_ring->cq_base_dma);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002449
Ron Mercer97345522009-01-09 11:31:50 +00002450 cqicb->prod_idx_addr = cpu_to_le64(rx_ring->prod_idx_sh_reg_dma);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002451
2452 /*
2453 * Set up the control block load flags.
2454 */
2455 cqicb->flags = FLAGS_LC | /* Load queue base address */
2456 FLAGS_LV | /* Load MSI-X vector */
2457 FLAGS_LI; /* Load irq delay values */
2458 if (rx_ring->lbq_len) {
2459 cqicb->flags |= FLAGS_LL; /* Load lbq values */
2460 *((u64 *) rx_ring->lbq_base_indirect) = rx_ring->lbq_base_dma;
Ron Mercer97345522009-01-09 11:31:50 +00002461 cqicb->lbq_addr =
2462 cpu_to_le64(rx_ring->lbq_base_indirect_dma);
Ron Mercer459caf52009-01-04 17:08:11 -08002463 bq_len = (rx_ring->lbq_buf_size == 65536) ? 0 :
2464 (u16) rx_ring->lbq_buf_size;
2465 cqicb->lbq_buf_size = cpu_to_le16(bq_len);
2466 bq_len = (rx_ring->lbq_len == 65536) ? 0 :
2467 (u16) rx_ring->lbq_len;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002468 cqicb->lbq_len = cpu_to_le16(bq_len);
2469 rx_ring->lbq_prod_idx = rx_ring->lbq_len - 16;
2470 rx_ring->lbq_curr_idx = 0;
2471 rx_ring->lbq_clean_idx = rx_ring->lbq_prod_idx;
2472 rx_ring->lbq_free_cnt = 16;
2473 }
2474 if (rx_ring->sbq_len) {
2475 cqicb->flags |= FLAGS_LS; /* Load sbq values */
2476 *((u64 *) rx_ring->sbq_base_indirect) = rx_ring->sbq_base_dma;
Ron Mercer97345522009-01-09 11:31:50 +00002477 cqicb->sbq_addr =
2478 cpu_to_le64(rx_ring->sbq_base_indirect_dma);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002479 cqicb->sbq_buf_size =
2480 cpu_to_le16(((rx_ring->sbq_buf_size / 2) + 8) & 0xfffffff8);
Ron Mercer459caf52009-01-04 17:08:11 -08002481 bq_len = (rx_ring->sbq_len == 65536) ? 0 :
2482 (u16) rx_ring->sbq_len;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002483 cqicb->sbq_len = cpu_to_le16(bq_len);
2484 rx_ring->sbq_prod_idx = rx_ring->sbq_len - 16;
2485 rx_ring->sbq_curr_idx = 0;
2486 rx_ring->sbq_clean_idx = rx_ring->sbq_prod_idx;
2487 rx_ring->sbq_free_cnt = 16;
2488 }
2489 switch (rx_ring->type) {
2490 case TX_Q:
2491 /* If there's only one interrupt, then we use
2492 * worker threads to process the outbound
2493 * completion handling rx_rings. We do this so
2494 * they can be run on multiple CPUs. There is
2495 * room to play with this more where we would only
2496 * run in a worker if there are more than x number
2497 * of outbound completions on the queue and more
2498 * than one queue active. Some threshold that
2499 * would indicate a benefit in spite of the cost
2500 * of a context switch.
2501 * If there's more than one interrupt, then the
2502 * outbound completions are processed in the ISR.
2503 */
2504 if (!test_bit(QL_MSIX_ENABLED, &qdev->flags))
2505 INIT_DELAYED_WORK(&rx_ring->rx_work, ql_tx_clean);
2506 else {
2507 /* With all debug warnings on we see a WARN_ON message
2508 * when we free the skb in the interrupt context.
2509 */
2510 INIT_DELAYED_WORK(&rx_ring->rx_work, ql_tx_clean);
2511 }
2512 cqicb->irq_delay = cpu_to_le16(qdev->tx_coalesce_usecs);
2513 cqicb->pkt_delay = cpu_to_le16(qdev->tx_max_coalesced_frames);
2514 break;
2515 case DEFAULT_Q:
2516 INIT_DELAYED_WORK(&rx_ring->rx_work, ql_rx_clean);
2517 cqicb->irq_delay = 0;
2518 cqicb->pkt_delay = 0;
2519 break;
2520 case RX_Q:
2521 /* Inbound completion handling rx_rings run in
2522 * separate NAPI contexts.
2523 */
2524 netif_napi_add(qdev->ndev, &rx_ring->napi, ql_napi_poll_msix,
2525 64);
2526 cqicb->irq_delay = cpu_to_le16(qdev->rx_coalesce_usecs);
2527 cqicb->pkt_delay = cpu_to_le16(qdev->rx_max_coalesced_frames);
2528 break;
2529 default:
2530 QPRINTK(qdev, IFUP, DEBUG, "Invalid rx_ring->type = %d.\n",
2531 rx_ring->type);
2532 }
2533 QPRINTK(qdev, IFUP, INFO, "Initializing rx work queue.\n");
2534 err = ql_write_cfg(qdev, cqicb, sizeof(struct cqicb),
2535 CFG_LCQ, rx_ring->cq_id);
2536 if (err) {
2537 QPRINTK(qdev, IFUP, ERR, "Failed to load CQICB.\n");
2538 return err;
2539 }
2540 QPRINTK(qdev, IFUP, INFO, "Successfully loaded CQICB.\n");
2541 /*
2542 * Advance the producer index for the buffer queues.
2543 */
2544 wmb();
2545 if (rx_ring->lbq_len)
2546 ql_write_db_reg(rx_ring->lbq_prod_idx,
2547 rx_ring->lbq_prod_idx_db_reg);
2548 if (rx_ring->sbq_len)
2549 ql_write_db_reg(rx_ring->sbq_prod_idx,
2550 rx_ring->sbq_prod_idx_db_reg);
2551 return err;
2552}
2553
2554static int ql_start_tx_ring(struct ql_adapter *qdev, struct tx_ring *tx_ring)
2555{
2556 struct wqicb *wqicb = (struct wqicb *)tx_ring;
2557 void __iomem *doorbell_area =
2558 qdev->doorbell_area + (DB_PAGE_SIZE * tx_ring->wq_id);
2559 void *shadow_reg = qdev->tx_ring_shadow_reg_area +
2560 (tx_ring->wq_id * sizeof(u64));
2561 u64 shadow_reg_dma = qdev->tx_ring_shadow_reg_dma +
2562 (tx_ring->wq_id * sizeof(u64));
2563 int err = 0;
2564
2565 /*
2566 * Assign doorbell registers for this tx_ring.
2567 */
2568 /* TX PCI doorbell mem area for tx producer index */
Stephen Hemminger8668ae92008-11-21 17:29:50 -08002569 tx_ring->prod_idx_db_reg = (u32 __iomem *) doorbell_area;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002570 tx_ring->prod_idx = 0;
2571 /* TX PCI doorbell mem area + 0x04 */
2572 tx_ring->valid_db_reg = doorbell_area + 0x04;
2573
2574 /*
2575 * Assign shadow registers for this tx_ring.
2576 */
2577 tx_ring->cnsmr_idx_sh_reg = shadow_reg;
2578 tx_ring->cnsmr_idx_sh_reg_dma = shadow_reg_dma;
2579
2580 wqicb->len = cpu_to_le16(tx_ring->wq_len | Q_LEN_V | Q_LEN_CPP_CONT);
2581 wqicb->flags = cpu_to_le16(Q_FLAGS_LC |
2582 Q_FLAGS_LB | Q_FLAGS_LI | Q_FLAGS_LO);
2583 wqicb->cq_id_rss = cpu_to_le16(tx_ring->cq_id);
2584 wqicb->rid = 0;
Ron Mercer97345522009-01-09 11:31:50 +00002585 wqicb->addr = cpu_to_le64(tx_ring->wq_base_dma);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002586
Ron Mercer97345522009-01-09 11:31:50 +00002587 wqicb->cnsmr_idx_addr = cpu_to_le64(tx_ring->cnsmr_idx_sh_reg_dma);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002588
2589 ql_init_tx_ring(qdev, tx_ring);
2590
2591 err = ql_write_cfg(qdev, wqicb, sizeof(wqicb), CFG_LRQ,
2592 (u16) tx_ring->wq_id);
2593 if (err) {
2594 QPRINTK(qdev, IFUP, ERR, "Failed to load tx_ring.\n");
2595 return err;
2596 }
2597 QPRINTK(qdev, IFUP, INFO, "Successfully loaded WQICB.\n");
2598 return err;
2599}
2600
2601static void ql_disable_msix(struct ql_adapter *qdev)
2602{
2603 if (test_bit(QL_MSIX_ENABLED, &qdev->flags)) {
2604 pci_disable_msix(qdev->pdev);
2605 clear_bit(QL_MSIX_ENABLED, &qdev->flags);
2606 kfree(qdev->msi_x_entry);
2607 qdev->msi_x_entry = NULL;
2608 } else if (test_bit(QL_MSI_ENABLED, &qdev->flags)) {
2609 pci_disable_msi(qdev->pdev);
2610 clear_bit(QL_MSI_ENABLED, &qdev->flags);
2611 }
2612}
2613
2614static void ql_enable_msix(struct ql_adapter *qdev)
2615{
2616 int i;
2617
2618 qdev->intr_count = 1;
2619 /* Get the MSIX vectors. */
2620 if (irq_type == MSIX_IRQ) {
2621 /* Try to alloc space for the msix struct,
2622 * if it fails then go to MSI/legacy.
2623 */
2624 qdev->msi_x_entry = kcalloc(qdev->rx_ring_count,
2625 sizeof(struct msix_entry),
2626 GFP_KERNEL);
2627 if (!qdev->msi_x_entry) {
2628 irq_type = MSI_IRQ;
2629 goto msi;
2630 }
2631
2632 for (i = 0; i < qdev->rx_ring_count; i++)
2633 qdev->msi_x_entry[i].entry = i;
2634
2635 if (!pci_enable_msix
2636 (qdev->pdev, qdev->msi_x_entry, qdev->rx_ring_count)) {
2637 set_bit(QL_MSIX_ENABLED, &qdev->flags);
2638 qdev->intr_count = qdev->rx_ring_count;
2639 QPRINTK(qdev, IFUP, INFO,
2640 "MSI-X Enabled, got %d vectors.\n",
2641 qdev->intr_count);
2642 return;
2643 } else {
2644 kfree(qdev->msi_x_entry);
2645 qdev->msi_x_entry = NULL;
2646 QPRINTK(qdev, IFUP, WARNING,
2647 "MSI-X Enable failed, trying MSI.\n");
2648 irq_type = MSI_IRQ;
2649 }
2650 }
2651msi:
2652 if (irq_type == MSI_IRQ) {
2653 if (!pci_enable_msi(qdev->pdev)) {
2654 set_bit(QL_MSI_ENABLED, &qdev->flags);
2655 QPRINTK(qdev, IFUP, INFO,
2656 "Running with MSI interrupts.\n");
2657 return;
2658 }
2659 }
2660 irq_type = LEG_IRQ;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002661 QPRINTK(qdev, IFUP, DEBUG, "Running with legacy interrupts.\n");
2662}
2663
2664/*
2665 * Here we build the intr_context structures based on
2666 * our rx_ring count and intr vector count.
2667 * The intr_context structure is used to hook each vector
2668 * to possibly different handlers.
2669 */
2670static void ql_resolve_queues_to_irqs(struct ql_adapter *qdev)
2671{
2672 int i = 0;
2673 struct intr_context *intr_context = &qdev->intr_context[0];
2674
2675 ql_enable_msix(qdev);
2676
2677 if (likely(test_bit(QL_MSIX_ENABLED, &qdev->flags))) {
2678 /* Each rx_ring has it's
2679 * own intr_context since we have separate
2680 * vectors for each queue.
2681 * This only true when MSI-X is enabled.
2682 */
2683 for (i = 0; i < qdev->intr_count; i++, intr_context++) {
2684 qdev->rx_ring[i].irq = i;
2685 intr_context->intr = i;
2686 intr_context->qdev = qdev;
2687 /*
2688 * We set up each vectors enable/disable/read bits so
2689 * there's no bit/mask calculations in the critical path.
2690 */
2691 intr_context->intr_en_mask =
2692 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
2693 INTR_EN_TYPE_ENABLE | INTR_EN_IHD_MASK | INTR_EN_IHD
2694 | i;
2695 intr_context->intr_dis_mask =
2696 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
2697 INTR_EN_TYPE_DISABLE | INTR_EN_IHD_MASK |
2698 INTR_EN_IHD | i;
2699 intr_context->intr_read_mask =
2700 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
2701 INTR_EN_TYPE_READ | INTR_EN_IHD_MASK | INTR_EN_IHD |
2702 i;
2703
2704 if (i == 0) {
2705 /*
2706 * Default queue handles bcast/mcast plus
2707 * async events. Needs buffers.
2708 */
2709 intr_context->handler = qlge_isr;
2710 sprintf(intr_context->name, "%s-default-queue",
2711 qdev->ndev->name);
2712 } else if (i < qdev->rss_ring_first_cq_id) {
2713 /*
2714 * Outbound queue is for outbound completions only.
2715 */
2716 intr_context->handler = qlge_msix_tx_isr;
Jesper Dangaard Brouerc2249692009-01-09 03:14:47 +00002717 sprintf(intr_context->name, "%s-tx-%d",
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002718 qdev->ndev->name, i);
2719 } else {
2720 /*
2721 * Inbound queues handle unicast frames only.
2722 */
2723 intr_context->handler = qlge_msix_rx_isr;
Jesper Dangaard Brouerc2249692009-01-09 03:14:47 +00002724 sprintf(intr_context->name, "%s-rx-%d",
Ron Mercerc4e84bd2008-09-18 11:56:28 -04002725 qdev->ndev->name, i);
2726 }
2727 }
2728 } else {
2729 /*
2730 * All rx_rings use the same intr_context since
2731 * there is only one vector.
2732 */
2733 intr_context->intr = 0;
2734 intr_context->qdev = qdev;
2735 /*
2736 * We set up each vectors enable/disable/read bits so
2737 * there's no bit/mask calculations in the critical path.
2738 */
2739 intr_context->intr_en_mask =
2740 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK | INTR_EN_TYPE_ENABLE;
2741 intr_context->intr_dis_mask =
2742 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK |
2743 INTR_EN_TYPE_DISABLE;
2744 intr_context->intr_read_mask =
2745 INTR_EN_TYPE_MASK | INTR_EN_INTR_MASK | INTR_EN_TYPE_READ;
2746 /*
2747 * Single interrupt means one handler for all rings.
2748 */
2749 intr_context->handler = qlge_isr;
2750 sprintf(intr_context->name, "%s-single_irq", qdev->ndev->name);
2751 for (i = 0; i < qdev->rx_ring_count; i++)
2752 qdev->rx_ring[i].irq = 0;
2753 }
2754}
2755
2756static void ql_free_irq(struct ql_adapter *qdev)
2757{
2758 int i;
2759 struct intr_context *intr_context = &qdev->intr_context[0];
2760
2761 for (i = 0; i < qdev->intr_count; i++, intr_context++) {
2762 if (intr_context->hooked) {
2763 if (test_bit(QL_MSIX_ENABLED, &qdev->flags)) {
2764 free_irq(qdev->msi_x_entry[i].vector,
2765 &qdev->rx_ring[i]);
2766 QPRINTK(qdev, IFDOWN, ERR,
2767 "freeing msix interrupt %d.\n", i);
2768 } else {
2769 free_irq(qdev->pdev->irq, &qdev->rx_ring[0]);
2770 QPRINTK(qdev, IFDOWN, ERR,
2771 "freeing msi interrupt %d.\n", i);
2772 }
2773 }
2774 }
2775 ql_disable_msix(qdev);
2776}
2777
2778static int ql_request_irq(struct ql_adapter *qdev)
2779{
2780 int i;
2781 int status = 0;
2782 struct pci_dev *pdev = qdev->pdev;
2783 struct intr_context *intr_context = &qdev->intr_context[0];
2784
2785 ql_resolve_queues_to_irqs(qdev);
2786
2787 for (i = 0; i < qdev->intr_count; i++, intr_context++) {
2788 atomic_set(&intr_context->irq_cnt, 0);
2789 if (test_bit(QL_MSIX_ENABLED, &qdev->flags)) {
2790 status = request_irq(qdev->msi_x_entry[i].vector,
2791 intr_context->handler,
2792 0,
2793 intr_context->name,
2794 &qdev->rx_ring[i]);
2795 if (status) {
2796 QPRINTK(qdev, IFUP, ERR,
2797 "Failed request for MSIX interrupt %d.\n",
2798 i);
2799 goto err_irq;
2800 } else {
2801 QPRINTK(qdev, IFUP, INFO,
2802 "Hooked intr %d, queue type %s%s%s, with name %s.\n",
2803 i,
2804 qdev->rx_ring[i].type ==
2805 DEFAULT_Q ? "DEFAULT_Q" : "",
2806 qdev->rx_ring[i].type ==
2807 TX_Q ? "TX_Q" : "",
2808 qdev->rx_ring[i].type ==
2809 RX_Q ? "RX_Q" : "", intr_context->name);
2810 }
2811 } else {
2812 QPRINTK(qdev, IFUP, DEBUG,
2813 "trying msi or legacy interrupts.\n");
2814 QPRINTK(qdev, IFUP, DEBUG,
2815 "%s: irq = %d.\n", __func__, pdev->irq);
2816 QPRINTK(qdev, IFUP, DEBUG,
2817 "%s: context->name = %s.\n", __func__,
2818 intr_context->name);
2819 QPRINTK(qdev, IFUP, DEBUG,
2820 "%s: dev_id = 0x%p.\n", __func__,
2821 &qdev->rx_ring[0]);
2822 status =
2823 request_irq(pdev->irq, qlge_isr,
2824 test_bit(QL_MSI_ENABLED,
2825 &qdev->
2826 flags) ? 0 : IRQF_SHARED,
2827 intr_context->name, &qdev->rx_ring[0]);
2828 if (status)
2829 goto err_irq;
2830
2831 QPRINTK(qdev, IFUP, ERR,
2832 "Hooked intr %d, queue type %s%s%s, with name %s.\n",
2833 i,
2834 qdev->rx_ring[0].type ==
2835 DEFAULT_Q ? "DEFAULT_Q" : "",
2836 qdev->rx_ring[0].type == TX_Q ? "TX_Q" : "",
2837 qdev->rx_ring[0].type == RX_Q ? "RX_Q" : "",
2838 intr_context->name);
2839 }
2840 intr_context->hooked = 1;
2841 }
2842 return status;
2843err_irq:
2844 QPRINTK(qdev, IFUP, ERR, "Failed to get the interrupts!!!/n");
2845 ql_free_irq(qdev);
2846 return status;
2847}
2848
2849static int ql_start_rss(struct ql_adapter *qdev)
2850{
2851 struct ricb *ricb = &qdev->ricb;
2852 int status = 0;
2853 int i;
2854 u8 *hash_id = (u8 *) ricb->hash_cq_id;
2855
2856 memset((void *)ricb, 0, sizeof(ricb));
2857
2858 ricb->base_cq = qdev->rss_ring_first_cq_id | RSS_L4K;
2859 ricb->flags =
2860 (RSS_L6K | RSS_LI | RSS_LB | RSS_LM | RSS_RI4 | RSS_RI6 | RSS_RT4 |
2861 RSS_RT6);
2862 ricb->mask = cpu_to_le16(qdev->rss_ring_count - 1);
2863
2864 /*
2865 * Fill out the Indirection Table.
2866 */
2867 for (i = 0; i < 32; i++)
2868 hash_id[i] = i & 1;
2869
2870 /*
2871 * Random values for the IPv6 and IPv4 Hash Keys.
2872 */
2873 get_random_bytes((void *)&ricb->ipv6_hash_key[0], 40);
2874 get_random_bytes((void *)&ricb->ipv4_hash_key[0], 16);
2875
2876 QPRINTK(qdev, IFUP, INFO, "Initializing RSS.\n");
2877
2878 status = ql_write_cfg(qdev, ricb, sizeof(ricb), CFG_LR, 0);
2879 if (status) {
2880 QPRINTK(qdev, IFUP, ERR, "Failed to load RICB.\n");
2881 return status;
2882 }
2883 QPRINTK(qdev, IFUP, INFO, "Successfully loaded RICB.\n");
2884 return status;
2885}
2886
2887/* Initialize the frame-to-queue routing. */
2888static int ql_route_initialize(struct ql_adapter *qdev)
2889{
2890 int status = 0;
2891 int i;
2892
2893 /* Clear all the entries in the routing table. */
2894 for (i = 0; i < 16; i++) {
2895 status = ql_set_routing_reg(qdev, i, 0, 0);
2896 if (status) {
2897 QPRINTK(qdev, IFUP, ERR,
2898 "Failed to init routing register for CAM packets.\n");
2899 return status;
2900 }
2901 }
2902
2903 status = ql_set_routing_reg(qdev, RT_IDX_ALL_ERR_SLOT, RT_IDX_ERR, 1);
2904 if (status) {
2905 QPRINTK(qdev, IFUP, ERR,
2906 "Failed to init routing register for error packets.\n");
2907 return status;
2908 }
2909 status = ql_set_routing_reg(qdev, RT_IDX_BCAST_SLOT, RT_IDX_BCAST, 1);
2910 if (status) {
2911 QPRINTK(qdev, IFUP, ERR,
2912 "Failed to init routing register for broadcast packets.\n");
2913 return status;
2914 }
2915 /* If we have more than one inbound queue, then turn on RSS in the
2916 * routing block.
2917 */
2918 if (qdev->rss_ring_count > 1) {
2919 status = ql_set_routing_reg(qdev, RT_IDX_RSS_MATCH_SLOT,
2920 RT_IDX_RSS_MATCH, 1);
2921 if (status) {
2922 QPRINTK(qdev, IFUP, ERR,
2923 "Failed to init routing register for MATCH RSS packets.\n");
2924 return status;
2925 }
2926 }
2927
2928 status = ql_set_routing_reg(qdev, RT_IDX_CAM_HIT_SLOT,
2929 RT_IDX_CAM_HIT, 1);
2930 if (status) {
2931 QPRINTK(qdev, IFUP, ERR,
2932 "Failed to init routing register for CAM packets.\n");
2933 return status;
2934 }
2935 return status;
2936}
2937
2938static int ql_adapter_initialize(struct ql_adapter *qdev)
2939{
2940 u32 value, mask;
2941 int i;
2942 int status = 0;
2943
2944 /*
2945 * Set up the System register to halt on errors.
2946 */
2947 value = SYS_EFE | SYS_FAE;
2948 mask = value << 16;
2949 ql_write32(qdev, SYS, mask | value);
2950
2951 /* Set the default queue. */
2952 value = NIC_RCV_CFG_DFQ;
2953 mask = NIC_RCV_CFG_DFQ_MASK;
2954 ql_write32(qdev, NIC_RCV_CFG, (mask | value));
2955
2956 /* Set the MPI interrupt to enabled. */
2957 ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16) | INTR_MASK_PI);
2958
2959 /* Enable the function, set pagesize, enable error checking. */
2960 value = FSC_FE | FSC_EPC_INBOUND | FSC_EPC_OUTBOUND |
2961 FSC_EC | FSC_VM_PAGE_4K | FSC_SH;
2962
2963 /* Set/clear header splitting. */
2964 mask = FSC_VM_PAGESIZE_MASK |
2965 FSC_DBL_MASK | FSC_DBRST_MASK | (value << 16);
2966 ql_write32(qdev, FSC, mask | value);
2967
2968 ql_write32(qdev, SPLT_HDR, SPLT_HDR_EP |
2969 min(SMALL_BUFFER_SIZE, MAX_SPLIT_SIZE));
2970
2971 /* Start up the rx queues. */
2972 for (i = 0; i < qdev->rx_ring_count; i++) {
2973 status = ql_start_rx_ring(qdev, &qdev->rx_ring[i]);
2974 if (status) {
2975 QPRINTK(qdev, IFUP, ERR,
2976 "Failed to start rx ring[%d].\n", i);
2977 return status;
2978 }
2979 }
2980
2981 /* If there is more than one inbound completion queue
2982 * then download a RICB to configure RSS.
2983 */
2984 if (qdev->rss_ring_count > 1) {
2985 status = ql_start_rss(qdev);
2986 if (status) {
2987 QPRINTK(qdev, IFUP, ERR, "Failed to start RSS.\n");
2988 return status;
2989 }
2990 }
2991
2992 /* Start up the tx queues. */
2993 for (i = 0; i < qdev->tx_ring_count; i++) {
2994 status = ql_start_tx_ring(qdev, &qdev->tx_ring[i]);
2995 if (status) {
2996 QPRINTK(qdev, IFUP, ERR,
2997 "Failed to start tx ring[%d].\n", i);
2998 return status;
2999 }
3000 }
3001
3002 status = ql_port_initialize(qdev);
3003 if (status) {
3004 QPRINTK(qdev, IFUP, ERR, "Failed to start port.\n");
3005 return status;
3006 }
3007
3008 status = ql_set_mac_addr_reg(qdev, (u8 *) qdev->ndev->perm_addr,
3009 MAC_ADDR_TYPE_CAM_MAC, qdev->func);
3010 if (status) {
3011 QPRINTK(qdev, IFUP, ERR, "Failed to init mac address.\n");
3012 return status;
3013 }
3014
3015 status = ql_route_initialize(qdev);
3016 if (status) {
3017 QPRINTK(qdev, IFUP, ERR, "Failed to init routing table.\n");
3018 return status;
3019 }
3020
3021 /* Start NAPI for the RSS queues. */
3022 for (i = qdev->rss_ring_first_cq_id; i < qdev->rx_ring_count; i++) {
3023 QPRINTK(qdev, IFUP, INFO, "Enabling NAPI for rx_ring[%d].\n",
3024 i);
3025 napi_enable(&qdev->rx_ring[i].napi);
3026 }
3027
3028 return status;
3029}
3030
3031/* Issue soft reset to chip. */
3032static int ql_adapter_reset(struct ql_adapter *qdev)
3033{
3034 u32 value;
3035 int max_wait_time;
3036 int status = 0;
3037 int resetCnt = 0;
3038
3039#define MAX_RESET_CNT 1
3040issueReset:
3041 resetCnt++;
3042 QPRINTK(qdev, IFDOWN, DEBUG, "Issue soft reset to chip.\n");
3043 ql_write32(qdev, RST_FO, (RST_FO_FR << 16) | RST_FO_FR);
3044 /* Wait for reset to complete. */
3045 max_wait_time = 3;
3046 QPRINTK(qdev, IFDOWN, DEBUG, "Wait %d seconds for reset to complete.\n",
3047 max_wait_time);
3048 do {
3049 value = ql_read32(qdev, RST_FO);
3050 if ((value & RST_FO_FR) == 0)
3051 break;
3052
3053 ssleep(1);
3054 } while ((--max_wait_time));
3055 if (value & RST_FO_FR) {
3056 QPRINTK(qdev, IFDOWN, ERR,
3057 "Stuck in SoftReset: FSC_SR:0x%08x\n", value);
3058 if (resetCnt < MAX_RESET_CNT)
3059 goto issueReset;
3060 }
3061 if (max_wait_time == 0) {
3062 status = -ETIMEDOUT;
3063 QPRINTK(qdev, IFDOWN, ERR,
3064 "ETIMEOUT!!! errored out of resetting the chip!\n");
3065 }
3066
3067 return status;
3068}
3069
3070static void ql_display_dev_info(struct net_device *ndev)
3071{
3072 struct ql_adapter *qdev = (struct ql_adapter *)netdev_priv(ndev);
3073
3074 QPRINTK(qdev, PROBE, INFO,
3075 "Function #%d, NIC Roll %d, NIC Rev = %d, "
3076 "XG Roll = %d, XG Rev = %d.\n",
3077 qdev->func,
3078 qdev->chip_rev_id & 0x0000000f,
3079 qdev->chip_rev_id >> 4 & 0x0000000f,
3080 qdev->chip_rev_id >> 8 & 0x0000000f,
3081 qdev->chip_rev_id >> 12 & 0x0000000f);
Johannes Berg7c510e42008-10-27 17:47:26 -07003082 QPRINTK(qdev, PROBE, INFO, "MAC address %pM\n", ndev->dev_addr);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003083}
3084
3085static int ql_adapter_down(struct ql_adapter *qdev)
3086{
3087 struct net_device *ndev = qdev->ndev;
3088 int i, status = 0;
3089 struct rx_ring *rx_ring;
3090
3091 netif_stop_queue(ndev);
3092 netif_carrier_off(ndev);
3093
3094 cancel_delayed_work_sync(&qdev->asic_reset_work);
3095 cancel_delayed_work_sync(&qdev->mpi_reset_work);
3096 cancel_delayed_work_sync(&qdev->mpi_work);
3097
3098 /* The default queue at index 0 is always processed in
3099 * a workqueue.
3100 */
3101 cancel_delayed_work_sync(&qdev->rx_ring[0].rx_work);
3102
3103 /* The rest of the rx_rings are processed in
3104 * a workqueue only if it's a single interrupt
3105 * environment (MSI/Legacy).
3106 */
Roel Kluinc0620762008-12-25 17:23:50 -08003107 for (i = 1; i < qdev->rx_ring_count; i++) {
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003108 rx_ring = &qdev->rx_ring[i];
3109 /* Only the RSS rings use NAPI on multi irq
3110 * environment. Outbound completion processing
3111 * is done in interrupt context.
3112 */
3113 if (i >= qdev->rss_ring_first_cq_id) {
3114 napi_disable(&rx_ring->napi);
3115 } else {
3116 cancel_delayed_work_sync(&rx_ring->rx_work);
3117 }
3118 }
3119
3120 clear_bit(QL_ADAPTER_UP, &qdev->flags);
3121
3122 ql_disable_interrupts(qdev);
3123
3124 ql_tx_ring_clean(qdev);
3125
3126 spin_lock(&qdev->hw_lock);
3127 status = ql_adapter_reset(qdev);
3128 if (status)
3129 QPRINTK(qdev, IFDOWN, ERR, "reset(func #%d) FAILED!\n",
3130 qdev->func);
3131 spin_unlock(&qdev->hw_lock);
3132 return status;
3133}
3134
3135static int ql_adapter_up(struct ql_adapter *qdev)
3136{
3137 int err = 0;
3138
3139 spin_lock(&qdev->hw_lock);
3140 err = ql_adapter_initialize(qdev);
3141 if (err) {
3142 QPRINTK(qdev, IFUP, INFO, "Unable to initialize adapter.\n");
3143 spin_unlock(&qdev->hw_lock);
3144 goto err_init;
3145 }
3146 spin_unlock(&qdev->hw_lock);
3147 set_bit(QL_ADAPTER_UP, &qdev->flags);
3148 ql_enable_interrupts(qdev);
3149 ql_enable_all_completion_interrupts(qdev);
3150 if ((ql_read32(qdev, STS) & qdev->port_init)) {
3151 netif_carrier_on(qdev->ndev);
3152 netif_start_queue(qdev->ndev);
3153 }
3154
3155 return 0;
3156err_init:
3157 ql_adapter_reset(qdev);
3158 return err;
3159}
3160
3161static int ql_cycle_adapter(struct ql_adapter *qdev)
3162{
3163 int status;
3164
3165 status = ql_adapter_down(qdev);
3166 if (status)
3167 goto error;
3168
3169 status = ql_adapter_up(qdev);
3170 if (status)
3171 goto error;
3172
3173 return status;
3174error:
3175 QPRINTK(qdev, IFUP, ALERT,
3176 "Driver up/down cycle failed, closing device\n");
3177 rtnl_lock();
3178 dev_close(qdev->ndev);
3179 rtnl_unlock();
3180 return status;
3181}
3182
3183static void ql_release_adapter_resources(struct ql_adapter *qdev)
3184{
3185 ql_free_mem_resources(qdev);
3186 ql_free_irq(qdev);
3187}
3188
3189static int ql_get_adapter_resources(struct ql_adapter *qdev)
3190{
3191 int status = 0;
3192
3193 if (ql_alloc_mem_resources(qdev)) {
3194 QPRINTK(qdev, IFUP, ERR, "Unable to allocate memory.\n");
3195 return -ENOMEM;
3196 }
3197 status = ql_request_irq(qdev);
3198 if (status)
3199 goto err_irq;
3200 return status;
3201err_irq:
3202 ql_free_mem_resources(qdev);
3203 return status;
3204}
3205
3206static int qlge_close(struct net_device *ndev)
3207{
3208 struct ql_adapter *qdev = netdev_priv(ndev);
3209
3210 /*
3211 * Wait for device to recover from a reset.
3212 * (Rarely happens, but possible.)
3213 */
3214 while (!test_bit(QL_ADAPTER_UP, &qdev->flags))
3215 msleep(1);
3216 ql_adapter_down(qdev);
3217 ql_release_adapter_resources(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003218 return 0;
3219}
3220
3221static int ql_configure_rings(struct ql_adapter *qdev)
3222{
3223 int i;
3224 struct rx_ring *rx_ring;
3225 struct tx_ring *tx_ring;
3226 int cpu_cnt = num_online_cpus();
3227
3228 /*
3229 * For each processor present we allocate one
3230 * rx_ring for outbound completions, and one
3231 * rx_ring for inbound completions. Plus there is
3232 * always the one default queue. For the CPU
3233 * counts we end up with the following rx_rings:
3234 * rx_ring count =
3235 * one default queue +
3236 * (CPU count * outbound completion rx_ring) +
3237 * (CPU count * inbound (RSS) completion rx_ring)
3238 * To keep it simple we limit the total number of
3239 * queues to < 32, so we truncate CPU to 8.
3240 * This limitation can be removed when requested.
3241 */
3242
Ron Mercer683d46a2009-01-09 11:31:53 +00003243 if (cpu_cnt > MAX_CPUS)
3244 cpu_cnt = MAX_CPUS;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003245
3246 /*
3247 * rx_ring[0] is always the default queue.
3248 */
3249 /* Allocate outbound completion ring for each CPU. */
3250 qdev->tx_ring_count = cpu_cnt;
3251 /* Allocate inbound completion (RSS) ring for each CPU. */
3252 qdev->rss_ring_count = cpu_cnt;
3253 /* cq_id for the first inbound ring handler. */
3254 qdev->rss_ring_first_cq_id = cpu_cnt + 1;
3255 /*
3256 * qdev->rx_ring_count:
3257 * Total number of rx_rings. This includes the one
3258 * default queue, a number of outbound completion
3259 * handler rx_rings, and the number of inbound
3260 * completion handler rx_rings.
3261 */
3262 qdev->rx_ring_count = qdev->tx_ring_count + qdev->rss_ring_count + 1;
3263
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003264 for (i = 0; i < qdev->tx_ring_count; i++) {
3265 tx_ring = &qdev->tx_ring[i];
3266 memset((void *)tx_ring, 0, sizeof(tx_ring));
3267 tx_ring->qdev = qdev;
3268 tx_ring->wq_id = i;
3269 tx_ring->wq_len = qdev->tx_ring_size;
3270 tx_ring->wq_size =
3271 tx_ring->wq_len * sizeof(struct ob_mac_iocb_req);
3272
3273 /*
3274 * The completion queue ID for the tx rings start
3275 * immediately after the default Q ID, which is zero.
3276 */
3277 tx_ring->cq_id = i + 1;
3278 }
3279
3280 for (i = 0; i < qdev->rx_ring_count; i++) {
3281 rx_ring = &qdev->rx_ring[i];
3282 memset((void *)rx_ring, 0, sizeof(rx_ring));
3283 rx_ring->qdev = qdev;
3284 rx_ring->cq_id = i;
3285 rx_ring->cpu = i % cpu_cnt; /* CPU to run handler on. */
3286 if (i == 0) { /* Default queue at index 0. */
3287 /*
3288 * Default queue handles bcast/mcast plus
3289 * async events. Needs buffers.
3290 */
3291 rx_ring->cq_len = qdev->rx_ring_size;
3292 rx_ring->cq_size =
3293 rx_ring->cq_len * sizeof(struct ql_net_rsp_iocb);
3294 rx_ring->lbq_len = NUM_LARGE_BUFFERS;
3295 rx_ring->lbq_size =
Ron Mercer2c9a0d42009-01-05 18:19:20 -08003296 rx_ring->lbq_len * sizeof(__le64);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003297 rx_ring->lbq_buf_size = LARGE_BUFFER_SIZE;
3298 rx_ring->sbq_len = NUM_SMALL_BUFFERS;
3299 rx_ring->sbq_size =
Ron Mercer2c9a0d42009-01-05 18:19:20 -08003300 rx_ring->sbq_len * sizeof(__le64);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003301 rx_ring->sbq_buf_size = SMALL_BUFFER_SIZE * 2;
3302 rx_ring->type = DEFAULT_Q;
3303 } else if (i < qdev->rss_ring_first_cq_id) {
3304 /*
3305 * Outbound queue handles outbound completions only.
3306 */
3307 /* outbound cq is same size as tx_ring it services. */
3308 rx_ring->cq_len = qdev->tx_ring_size;
3309 rx_ring->cq_size =
3310 rx_ring->cq_len * sizeof(struct ql_net_rsp_iocb);
3311 rx_ring->lbq_len = 0;
3312 rx_ring->lbq_size = 0;
3313 rx_ring->lbq_buf_size = 0;
3314 rx_ring->sbq_len = 0;
3315 rx_ring->sbq_size = 0;
3316 rx_ring->sbq_buf_size = 0;
3317 rx_ring->type = TX_Q;
3318 } else { /* Inbound completions (RSS) queues */
3319 /*
3320 * Inbound queues handle unicast frames only.
3321 */
3322 rx_ring->cq_len = qdev->rx_ring_size;
3323 rx_ring->cq_size =
3324 rx_ring->cq_len * sizeof(struct ql_net_rsp_iocb);
3325 rx_ring->lbq_len = NUM_LARGE_BUFFERS;
3326 rx_ring->lbq_size =
Ron Mercer2c9a0d42009-01-05 18:19:20 -08003327 rx_ring->lbq_len * sizeof(__le64);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003328 rx_ring->lbq_buf_size = LARGE_BUFFER_SIZE;
3329 rx_ring->sbq_len = NUM_SMALL_BUFFERS;
3330 rx_ring->sbq_size =
Ron Mercer2c9a0d42009-01-05 18:19:20 -08003331 rx_ring->sbq_len * sizeof(__le64);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003332 rx_ring->sbq_buf_size = SMALL_BUFFER_SIZE * 2;
3333 rx_ring->type = RX_Q;
3334 }
3335 }
3336 return 0;
3337}
3338
3339static int qlge_open(struct net_device *ndev)
3340{
3341 int err = 0;
3342 struct ql_adapter *qdev = netdev_priv(ndev);
3343
3344 err = ql_configure_rings(qdev);
3345 if (err)
3346 return err;
3347
3348 err = ql_get_adapter_resources(qdev);
3349 if (err)
3350 goto error_up;
3351
3352 err = ql_adapter_up(qdev);
3353 if (err)
3354 goto error_up;
3355
3356 return err;
3357
3358error_up:
3359 ql_release_adapter_resources(qdev);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003360 return err;
3361}
3362
3363static int qlge_change_mtu(struct net_device *ndev, int new_mtu)
3364{
3365 struct ql_adapter *qdev = netdev_priv(ndev);
3366
3367 if (ndev->mtu == 1500 && new_mtu == 9000) {
3368 QPRINTK(qdev, IFUP, ERR, "Changing to jumbo MTU.\n");
3369 } else if (ndev->mtu == 9000 && new_mtu == 1500) {
3370 QPRINTK(qdev, IFUP, ERR, "Changing to normal MTU.\n");
3371 } else if ((ndev->mtu == 1500 && new_mtu == 1500) ||
3372 (ndev->mtu == 9000 && new_mtu == 9000)) {
3373 return 0;
3374 } else
3375 return -EINVAL;
3376 ndev->mtu = new_mtu;
3377 return 0;
3378}
3379
3380static struct net_device_stats *qlge_get_stats(struct net_device
3381 *ndev)
3382{
3383 struct ql_adapter *qdev = netdev_priv(ndev);
3384 return &qdev->stats;
3385}
3386
3387static void qlge_set_multicast_list(struct net_device *ndev)
3388{
3389 struct ql_adapter *qdev = (struct ql_adapter *)netdev_priv(ndev);
3390 struct dev_mc_list *mc_ptr;
3391 int i;
3392
3393 spin_lock(&qdev->hw_lock);
3394 /*
3395 * Set or clear promiscuous mode if a
3396 * transition is taking place.
3397 */
3398 if (ndev->flags & IFF_PROMISC) {
3399 if (!test_bit(QL_PROMISCUOUS, &qdev->flags)) {
3400 if (ql_set_routing_reg
3401 (qdev, RT_IDX_PROMISCUOUS_SLOT, RT_IDX_VALID, 1)) {
3402 QPRINTK(qdev, HW, ERR,
3403 "Failed to set promiscous mode.\n");
3404 } else {
3405 set_bit(QL_PROMISCUOUS, &qdev->flags);
3406 }
3407 }
3408 } else {
3409 if (test_bit(QL_PROMISCUOUS, &qdev->flags)) {
3410 if (ql_set_routing_reg
3411 (qdev, RT_IDX_PROMISCUOUS_SLOT, RT_IDX_VALID, 0)) {
3412 QPRINTK(qdev, HW, ERR,
3413 "Failed to clear promiscous mode.\n");
3414 } else {
3415 clear_bit(QL_PROMISCUOUS, &qdev->flags);
3416 }
3417 }
3418 }
3419
3420 /*
3421 * Set or clear all multicast mode if a
3422 * transition is taking place.
3423 */
3424 if ((ndev->flags & IFF_ALLMULTI) ||
3425 (ndev->mc_count > MAX_MULTICAST_ENTRIES)) {
3426 if (!test_bit(QL_ALLMULTI, &qdev->flags)) {
3427 if (ql_set_routing_reg
3428 (qdev, RT_IDX_ALLMULTI_SLOT, RT_IDX_MCAST, 1)) {
3429 QPRINTK(qdev, HW, ERR,
3430 "Failed to set all-multi mode.\n");
3431 } else {
3432 set_bit(QL_ALLMULTI, &qdev->flags);
3433 }
3434 }
3435 } else {
3436 if (test_bit(QL_ALLMULTI, &qdev->flags)) {
3437 if (ql_set_routing_reg
3438 (qdev, RT_IDX_ALLMULTI_SLOT, RT_IDX_MCAST, 0)) {
3439 QPRINTK(qdev, HW, ERR,
3440 "Failed to clear all-multi mode.\n");
3441 } else {
3442 clear_bit(QL_ALLMULTI, &qdev->flags);
3443 }
3444 }
3445 }
3446
3447 if (ndev->mc_count) {
3448 for (i = 0, mc_ptr = ndev->mc_list; mc_ptr;
3449 i++, mc_ptr = mc_ptr->next)
3450 if (ql_set_mac_addr_reg(qdev, (u8 *) mc_ptr->dmi_addr,
3451 MAC_ADDR_TYPE_MULTI_MAC, i)) {
3452 QPRINTK(qdev, HW, ERR,
3453 "Failed to loadmulticast address.\n");
3454 goto exit;
3455 }
3456 if (ql_set_routing_reg
3457 (qdev, RT_IDX_MCAST_MATCH_SLOT, RT_IDX_MCAST_MATCH, 1)) {
3458 QPRINTK(qdev, HW, ERR,
3459 "Failed to set multicast match mode.\n");
3460 } else {
3461 set_bit(QL_ALLMULTI, &qdev->flags);
3462 }
3463 }
3464exit:
3465 spin_unlock(&qdev->hw_lock);
3466}
3467
3468static int qlge_set_mac_address(struct net_device *ndev, void *p)
3469{
3470 struct ql_adapter *qdev = (struct ql_adapter *)netdev_priv(ndev);
3471 struct sockaddr *addr = p;
Stephen Hemminger8668ae92008-11-21 17:29:50 -08003472 int ret = 0;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003473
3474 if (netif_running(ndev))
3475 return -EBUSY;
3476
3477 if (!is_valid_ether_addr(addr->sa_data))
3478 return -EADDRNOTAVAIL;
3479 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
3480
3481 spin_lock(&qdev->hw_lock);
3482 if (ql_set_mac_addr_reg(qdev, (u8 *) ndev->dev_addr,
3483 MAC_ADDR_TYPE_CAM_MAC, qdev->func)) {/* Unicast */
3484 QPRINTK(qdev, HW, ERR, "Failed to load MAC address.\n");
Stephen Hemminger8668ae92008-11-21 17:29:50 -08003485 ret = -1;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003486 }
3487 spin_unlock(&qdev->hw_lock);
3488
Stephen Hemminger8668ae92008-11-21 17:29:50 -08003489 return ret;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003490}
3491
3492static void qlge_tx_timeout(struct net_device *ndev)
3493{
3494 struct ql_adapter *qdev = (struct ql_adapter *)netdev_priv(ndev);
3495 queue_delayed_work(qdev->workqueue, &qdev->asic_reset_work, 0);
3496}
3497
3498static void ql_asic_reset_work(struct work_struct *work)
3499{
3500 struct ql_adapter *qdev =
3501 container_of(work, struct ql_adapter, asic_reset_work.work);
3502 ql_cycle_adapter(qdev);
3503}
3504
3505static void ql_get_board_info(struct ql_adapter *qdev)
3506{
3507 qdev->func =
3508 (ql_read32(qdev, STS) & STS_FUNC_ID_MASK) >> STS_FUNC_ID_SHIFT;
3509 if (qdev->func) {
3510 qdev->xg_sem_mask = SEM_XGMAC1_MASK;
3511 qdev->port_link_up = STS_PL1;
3512 qdev->port_init = STS_PI1;
3513 qdev->mailbox_in = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC2_MBI;
3514 qdev->mailbox_out = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC2_MBO;
3515 } else {
3516 qdev->xg_sem_mask = SEM_XGMAC0_MASK;
3517 qdev->port_link_up = STS_PL0;
3518 qdev->port_init = STS_PI0;
3519 qdev->mailbox_in = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC0_MBI;
3520 qdev->mailbox_out = PROC_ADDR_MPI_RISC | PROC_ADDR_FUNC0_MBO;
3521 }
3522 qdev->chip_rev_id = ql_read32(qdev, REV_ID);
3523}
3524
3525static void ql_release_all(struct pci_dev *pdev)
3526{
3527 struct net_device *ndev = pci_get_drvdata(pdev);
3528 struct ql_adapter *qdev = netdev_priv(ndev);
3529
3530 if (qdev->workqueue) {
3531 destroy_workqueue(qdev->workqueue);
3532 qdev->workqueue = NULL;
3533 }
3534 if (qdev->q_workqueue) {
3535 destroy_workqueue(qdev->q_workqueue);
3536 qdev->q_workqueue = NULL;
3537 }
3538 if (qdev->reg_base)
Stephen Hemminger8668ae92008-11-21 17:29:50 -08003539 iounmap(qdev->reg_base);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003540 if (qdev->doorbell_area)
3541 iounmap(qdev->doorbell_area);
3542 pci_release_regions(pdev);
3543 pci_set_drvdata(pdev, NULL);
3544}
3545
3546static int __devinit ql_init_device(struct pci_dev *pdev,
3547 struct net_device *ndev, int cards_found)
3548{
3549 struct ql_adapter *qdev = netdev_priv(ndev);
3550 int pos, err = 0;
3551 u16 val16;
3552
3553 memset((void *)qdev, 0, sizeof(qdev));
3554 err = pci_enable_device(pdev);
3555 if (err) {
3556 dev_err(&pdev->dev, "PCI device enable failed.\n");
3557 return err;
3558 }
3559
3560 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3561 if (pos <= 0) {
3562 dev_err(&pdev->dev, PFX "Cannot find PCI Express capability, "
3563 "aborting.\n");
3564 goto err_out;
3565 } else {
3566 pci_read_config_word(pdev, pos + PCI_EXP_DEVCTL, &val16);
3567 val16 &= ~PCI_EXP_DEVCTL_NOSNOOP_EN;
3568 val16 |= (PCI_EXP_DEVCTL_CERE |
3569 PCI_EXP_DEVCTL_NFERE |
3570 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE);
3571 pci_write_config_word(pdev, pos + PCI_EXP_DEVCTL, val16);
3572 }
3573
3574 err = pci_request_regions(pdev, DRV_NAME);
3575 if (err) {
3576 dev_err(&pdev->dev, "PCI region request failed.\n");
3577 goto err_out;
3578 }
3579
3580 pci_set_master(pdev);
3581 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
3582 set_bit(QL_DMA64, &qdev->flags);
3583 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
3584 } else {
3585 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3586 if (!err)
3587 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
3588 }
3589
3590 if (err) {
3591 dev_err(&pdev->dev, "No usable DMA configuration.\n");
3592 goto err_out;
3593 }
3594
3595 pci_set_drvdata(pdev, ndev);
3596 qdev->reg_base =
3597 ioremap_nocache(pci_resource_start(pdev, 1),
3598 pci_resource_len(pdev, 1));
3599 if (!qdev->reg_base) {
3600 dev_err(&pdev->dev, "Register mapping failed.\n");
3601 err = -ENOMEM;
3602 goto err_out;
3603 }
3604
3605 qdev->doorbell_area_size = pci_resource_len(pdev, 3);
3606 qdev->doorbell_area =
3607 ioremap_nocache(pci_resource_start(pdev, 3),
3608 pci_resource_len(pdev, 3));
3609 if (!qdev->doorbell_area) {
3610 dev_err(&pdev->dev, "Doorbell register mapping failed.\n");
3611 err = -ENOMEM;
3612 goto err_out;
3613 }
3614
3615 ql_get_board_info(qdev);
3616 qdev->ndev = ndev;
3617 qdev->pdev = pdev;
3618 qdev->msg_enable = netif_msg_init(debug, default_msg);
3619 spin_lock_init(&qdev->hw_lock);
3620 spin_lock_init(&qdev->stats_lock);
3621
3622 /* make sure the EEPROM is good */
3623 err = ql_get_flash_params(qdev);
3624 if (err) {
3625 dev_err(&pdev->dev, "Invalid FLASH.\n");
3626 goto err_out;
3627 }
3628
3629 if (!is_valid_ether_addr(qdev->flash.mac_addr))
3630 goto err_out;
3631
3632 memcpy(ndev->dev_addr, qdev->flash.mac_addr, ndev->addr_len);
3633 memcpy(ndev->perm_addr, ndev->dev_addr, ndev->addr_len);
3634
3635 /* Set up the default ring sizes. */
3636 qdev->tx_ring_size = NUM_TX_RING_ENTRIES;
3637 qdev->rx_ring_size = NUM_RX_RING_ENTRIES;
3638
3639 /* Set up the coalescing parameters. */
3640 qdev->rx_coalesce_usecs = DFLT_COALESCE_WAIT;
3641 qdev->tx_coalesce_usecs = DFLT_COALESCE_WAIT;
3642 qdev->rx_max_coalesced_frames = DFLT_INTER_FRAME_WAIT;
3643 qdev->tx_max_coalesced_frames = DFLT_INTER_FRAME_WAIT;
3644
3645 /*
3646 * Set up the operating parameters.
3647 */
3648 qdev->rx_csum = 1;
3649
3650 qdev->q_workqueue = create_workqueue(ndev->name);
3651 qdev->workqueue = create_singlethread_workqueue(ndev->name);
3652 INIT_DELAYED_WORK(&qdev->asic_reset_work, ql_asic_reset_work);
3653 INIT_DELAYED_WORK(&qdev->mpi_reset_work, ql_mpi_reset_work);
3654 INIT_DELAYED_WORK(&qdev->mpi_work, ql_mpi_work);
3655
3656 if (!cards_found) {
3657 dev_info(&pdev->dev, "%s\n", DRV_STRING);
3658 dev_info(&pdev->dev, "Driver name: %s, Version: %s.\n",
3659 DRV_NAME, DRV_VERSION);
3660 }
3661 return 0;
3662err_out:
3663 ql_release_all(pdev);
3664 pci_disable_device(pdev);
3665 return err;
3666}
3667
Stephen Hemminger25ed7842008-11-21 17:29:16 -08003668
3669static const struct net_device_ops qlge_netdev_ops = {
3670 .ndo_open = qlge_open,
3671 .ndo_stop = qlge_close,
3672 .ndo_start_xmit = qlge_send,
3673 .ndo_change_mtu = qlge_change_mtu,
3674 .ndo_get_stats = qlge_get_stats,
3675 .ndo_set_multicast_list = qlge_set_multicast_list,
3676 .ndo_set_mac_address = qlge_set_mac_address,
3677 .ndo_validate_addr = eth_validate_addr,
3678 .ndo_tx_timeout = qlge_tx_timeout,
3679 .ndo_vlan_rx_register = ql_vlan_rx_register,
3680 .ndo_vlan_rx_add_vid = ql_vlan_rx_add_vid,
3681 .ndo_vlan_rx_kill_vid = ql_vlan_rx_kill_vid,
3682};
3683
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003684static int __devinit qlge_probe(struct pci_dev *pdev,
3685 const struct pci_device_id *pci_entry)
3686{
3687 struct net_device *ndev = NULL;
3688 struct ql_adapter *qdev = NULL;
3689 static int cards_found = 0;
3690 int err = 0;
3691
3692 ndev = alloc_etherdev(sizeof(struct ql_adapter));
3693 if (!ndev)
3694 return -ENOMEM;
3695
3696 err = ql_init_device(pdev, ndev, cards_found);
3697 if (err < 0) {
3698 free_netdev(ndev);
3699 return err;
3700 }
3701
3702 qdev = netdev_priv(ndev);
3703 SET_NETDEV_DEV(ndev, &pdev->dev);
3704 ndev->features = (0
3705 | NETIF_F_IP_CSUM
3706 | NETIF_F_SG
3707 | NETIF_F_TSO
3708 | NETIF_F_TSO6
3709 | NETIF_F_TSO_ECN
3710 | NETIF_F_HW_VLAN_TX
3711 | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER);
3712
3713 if (test_bit(QL_DMA64, &qdev->flags))
3714 ndev->features |= NETIF_F_HIGHDMA;
3715
3716 /*
3717 * Set up net_device structure.
3718 */
3719 ndev->tx_queue_len = qdev->tx_ring_size;
3720 ndev->irq = pdev->irq;
Stephen Hemminger25ed7842008-11-21 17:29:16 -08003721
3722 ndev->netdev_ops = &qlge_netdev_ops;
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003723 SET_ETHTOOL_OPS(ndev, &qlge_ethtool_ops);
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003724 ndev->watchdog_timeo = 10 * HZ;
Stephen Hemminger25ed7842008-11-21 17:29:16 -08003725
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003726 err = register_netdev(ndev);
3727 if (err) {
3728 dev_err(&pdev->dev, "net device registration failed.\n");
3729 ql_release_all(pdev);
3730 pci_disable_device(pdev);
3731 return err;
3732 }
3733 netif_carrier_off(ndev);
3734 netif_stop_queue(ndev);
3735 ql_display_dev_info(ndev);
3736 cards_found++;
3737 return 0;
3738}
3739
3740static void __devexit qlge_remove(struct pci_dev *pdev)
3741{
3742 struct net_device *ndev = pci_get_drvdata(pdev);
3743 unregister_netdev(ndev);
3744 ql_release_all(pdev);
3745 pci_disable_device(pdev);
3746 free_netdev(ndev);
3747}
3748
3749/*
3750 * This callback is called by the PCI subsystem whenever
3751 * a PCI bus error is detected.
3752 */
3753static pci_ers_result_t qlge_io_error_detected(struct pci_dev *pdev,
3754 enum pci_channel_state state)
3755{
3756 struct net_device *ndev = pci_get_drvdata(pdev);
3757 struct ql_adapter *qdev = netdev_priv(ndev);
3758
3759 if (netif_running(ndev))
3760 ql_adapter_down(qdev);
3761
3762 pci_disable_device(pdev);
3763
3764 /* Request a slot reset. */
3765 return PCI_ERS_RESULT_NEED_RESET;
3766}
3767
3768/*
3769 * This callback is called after the PCI buss has been reset.
3770 * Basically, this tries to restart the card from scratch.
3771 * This is a shortened version of the device probe/discovery code,
3772 * it resembles the first-half of the () routine.
3773 */
3774static pci_ers_result_t qlge_io_slot_reset(struct pci_dev *pdev)
3775{
3776 struct net_device *ndev = pci_get_drvdata(pdev);
3777 struct ql_adapter *qdev = netdev_priv(ndev);
3778
3779 if (pci_enable_device(pdev)) {
3780 QPRINTK(qdev, IFUP, ERR,
3781 "Cannot re-enable PCI device after reset.\n");
3782 return PCI_ERS_RESULT_DISCONNECT;
3783 }
3784
3785 pci_set_master(pdev);
3786
3787 netif_carrier_off(ndev);
3788 netif_stop_queue(ndev);
3789 ql_adapter_reset(qdev);
3790
3791 /* Make sure the EEPROM is good */
3792 memcpy(ndev->perm_addr, ndev->dev_addr, ndev->addr_len);
3793
3794 if (!is_valid_ether_addr(ndev->perm_addr)) {
3795 QPRINTK(qdev, IFUP, ERR, "After reset, invalid MAC address.\n");
3796 return PCI_ERS_RESULT_DISCONNECT;
3797 }
3798
3799 return PCI_ERS_RESULT_RECOVERED;
3800}
3801
3802static void qlge_io_resume(struct pci_dev *pdev)
3803{
3804 struct net_device *ndev = pci_get_drvdata(pdev);
3805 struct ql_adapter *qdev = netdev_priv(ndev);
3806
3807 pci_set_master(pdev);
3808
3809 if (netif_running(ndev)) {
3810 if (ql_adapter_up(qdev)) {
3811 QPRINTK(qdev, IFUP, ERR,
3812 "Device initialization failed after reset.\n");
3813 return;
3814 }
3815 }
3816
3817 netif_device_attach(ndev);
3818}
3819
3820static struct pci_error_handlers qlge_err_handler = {
3821 .error_detected = qlge_io_error_detected,
3822 .slot_reset = qlge_io_slot_reset,
3823 .resume = qlge_io_resume,
3824};
3825
3826static int qlge_suspend(struct pci_dev *pdev, pm_message_t state)
3827{
3828 struct net_device *ndev = pci_get_drvdata(pdev);
3829 struct ql_adapter *qdev = netdev_priv(ndev);
3830 int err;
3831
3832 netif_device_detach(ndev);
3833
3834 if (netif_running(ndev)) {
3835 err = ql_adapter_down(qdev);
3836 if (!err)
3837 return err;
3838 }
3839
3840 err = pci_save_state(pdev);
3841 if (err)
3842 return err;
3843
3844 pci_disable_device(pdev);
3845
3846 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3847
3848 return 0;
3849}
3850
David S. Miller04da2cf2008-09-19 16:14:24 -07003851#ifdef CONFIG_PM
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003852static int qlge_resume(struct pci_dev *pdev)
3853{
3854 struct net_device *ndev = pci_get_drvdata(pdev);
3855 struct ql_adapter *qdev = netdev_priv(ndev);
3856 int err;
3857
3858 pci_set_power_state(pdev, PCI_D0);
3859 pci_restore_state(pdev);
3860 err = pci_enable_device(pdev);
3861 if (err) {
3862 QPRINTK(qdev, IFUP, ERR, "Cannot enable PCI device from suspend\n");
3863 return err;
3864 }
3865 pci_set_master(pdev);
3866
3867 pci_enable_wake(pdev, PCI_D3hot, 0);
3868 pci_enable_wake(pdev, PCI_D3cold, 0);
3869
3870 if (netif_running(ndev)) {
3871 err = ql_adapter_up(qdev);
3872 if (err)
3873 return err;
3874 }
3875
3876 netif_device_attach(ndev);
3877
3878 return 0;
3879}
David S. Miller04da2cf2008-09-19 16:14:24 -07003880#endif /* CONFIG_PM */
Ron Mercerc4e84bd2008-09-18 11:56:28 -04003881
3882static void qlge_shutdown(struct pci_dev *pdev)
3883{
3884 qlge_suspend(pdev, PMSG_SUSPEND);
3885}
3886
3887static struct pci_driver qlge_driver = {
3888 .name = DRV_NAME,
3889 .id_table = qlge_pci_tbl,
3890 .probe = qlge_probe,
3891 .remove = __devexit_p(qlge_remove),
3892#ifdef CONFIG_PM
3893 .suspend = qlge_suspend,
3894 .resume = qlge_resume,
3895#endif
3896 .shutdown = qlge_shutdown,
3897 .err_handler = &qlge_err_handler
3898};
3899
3900static int __init qlge_init_module(void)
3901{
3902 return pci_register_driver(&qlge_driver);
3903}
3904
3905static void __exit qlge_exit(void)
3906{
3907 pci_unregister_driver(&qlge_driver);
3908}
3909
3910module_init(qlge_init_module);
3911module_exit(qlge_exit);