blob: 7210c34d2d5b25baf3fb2f78cf277a9512df5ce9 [file] [log] [blame]
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001/*
2 * Linux network driver for Brocade Converged Network Adapter.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License (GPL) Version 2 as
6 * published by the Free Software Foundation
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13/*
14 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
15 * All rights reserved
16 * www.brocade.com
17 */
18#include <linux/netdevice.h>
19#include <linux/skbuff.h>
20#include <linux/etherdevice.h>
21#include <linux/in.h>
22#include <linux/ethtool.h>
23#include <linux/if_vlan.h>
24#include <linux/if_ether.h>
25#include <linux/ip.h>
26
27#include "bnad.h"
28#include "bna.h"
29#include "cna.h"
30
31DEFINE_MUTEX(bnad_fwimg_mutex);
32
33/*
34 * Module params
35 */
36static uint bnad_msix_disable;
37module_param(bnad_msix_disable, uint, 0444);
38MODULE_PARM_DESC(bnad_msix_disable, "Disable MSIX mode");
39
40static uint bnad_ioc_auto_recover = 1;
41module_param(bnad_ioc_auto_recover, uint, 0444);
42MODULE_PARM_DESC(bnad_ioc_auto_recover, "Enable / Disable auto recovery");
43
44/*
45 * Global variables
46 */
47u32 bnad_rxqs_per_cq = 2;
48
49const u8 bnad_bcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
50
51/*
52 * Local MACROS
53 */
54#define BNAD_TX_UNMAPQ_DEPTH (bnad->txq_depth * 2)
55
56#define BNAD_RX_UNMAPQ_DEPTH (bnad->rxq_depth)
57
58#define BNAD_GET_MBOX_IRQ(_bnad) \
59 (((_bnad)->cfg_flags & BNAD_CF_MSIX) ? \
60 ((_bnad)->msix_table[(_bnad)->msix_num - 1].vector) : \
61 ((_bnad)->pcidev->irq))
62
63#define BNAD_FILL_UNMAPQ_MEM_REQ(_res_info, _num, _depth) \
64do { \
65 (_res_info)->res_type = BNA_RES_T_MEM; \
66 (_res_info)->res_u.mem_info.mem_type = BNA_MEM_T_KVA; \
67 (_res_info)->res_u.mem_info.num = (_num); \
68 (_res_info)->res_u.mem_info.len = \
69 sizeof(struct bnad_unmap_q) + \
70 (sizeof(struct bnad_skb_unmap) * ((_depth) - 1)); \
71} while (0)
72
73/*
74 * Reinitialize completions in CQ, once Rx is taken down
75 */
76static void
77bnad_cq_cmpl_init(struct bnad *bnad, struct bna_ccb *ccb)
78{
79 struct bna_cq_entry *cmpl, *next_cmpl;
80 unsigned int wi_range, wis = 0, ccb_prod = 0;
81 int i;
82
83 BNA_CQ_QPGE_PTR_GET(ccb_prod, ccb->sw_qpt, cmpl,
84 wi_range);
85
86 for (i = 0; i < ccb->q_depth; i++) {
87 wis++;
88 if (likely(--wi_range))
89 next_cmpl = cmpl + 1;
90 else {
91 BNA_QE_INDX_ADD(ccb_prod, wis, ccb->q_depth);
92 wis = 0;
93 BNA_CQ_QPGE_PTR_GET(ccb_prod, ccb->sw_qpt,
94 next_cmpl, wi_range);
95 }
96 cmpl->valid = 0;
97 cmpl = next_cmpl;
98 }
99}
100
101/*
102 * Frees all pending Tx Bufs
103 * At this point no activity is expected on the Q,
104 * so DMA unmap & freeing is fine.
105 */
106static void
107bnad_free_all_txbufs(struct bnad *bnad,
108 struct bna_tcb *tcb)
109{
110 u16 unmap_cons;
111 struct bnad_unmap_q *unmap_q = tcb->unmap_q;
112 struct bnad_skb_unmap *unmap_array;
113 struct sk_buff *skb = NULL;
114 int i;
115
116 unmap_array = unmap_q->unmap_array;
117
118 unmap_cons = 0;
119 while (unmap_cons < unmap_q->q_depth) {
120 skb = unmap_array[unmap_cons].skb;
121 if (!skb) {
122 unmap_cons++;
123 continue;
124 }
125 unmap_array[unmap_cons].skb = NULL;
126
127 pci_unmap_single(bnad->pcidev,
128 pci_unmap_addr(&unmap_array[unmap_cons],
129 dma_addr), skb_headlen(skb),
130 PCI_DMA_TODEVICE);
131
132 pci_unmap_addr_set(&unmap_array[unmap_cons], dma_addr, 0);
133 unmap_cons++;
134 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
135 pci_unmap_page(bnad->pcidev,
136 pci_unmap_addr(&unmap_array[unmap_cons],
137 dma_addr),
138 skb_shinfo(skb)->frags[i].size,
139 PCI_DMA_TODEVICE);
140 pci_unmap_addr_set(&unmap_array[unmap_cons], dma_addr,
141 0);
142 unmap_cons++;
143 }
144 dev_kfree_skb_any(skb);
145 }
146}
147
148/* Data Path Handlers */
149
150/*
151 * bnad_free_txbufs : Frees the Tx bufs on Tx completion
152 * Can be called in a) Interrupt context
153 * b) Sending context
154 * c) Tasklet context
155 */
156static u32
157bnad_free_txbufs(struct bnad *bnad,
158 struct bna_tcb *tcb)
159{
160 u32 sent_packets = 0, sent_bytes = 0;
161 u16 wis, unmap_cons, updated_hw_cons;
162 struct bnad_unmap_q *unmap_q = tcb->unmap_q;
163 struct bnad_skb_unmap *unmap_array;
164 struct sk_buff *skb;
165 int i;
166
167 /*
168 * Just return if TX is stopped. This check is useful
169 * when bnad_free_txbufs() runs out of a tasklet scheduled
170 * before bnad_cb_tx_cleanup() cleared BNAD_RF_TX_STARTED bit
171 * but this routine runs actually after the cleanup has been
172 * executed.
173 */
174 if (!test_bit(BNAD_RF_TX_STARTED, &bnad->run_flags))
175 return 0;
176
177 updated_hw_cons = *(tcb->hw_consumer_index);
178
179 wis = BNA_Q_INDEX_CHANGE(tcb->consumer_index,
180 updated_hw_cons, tcb->q_depth);
181
182 BUG_ON(!(wis <= BNA_QE_IN_USE_CNT(tcb, tcb->q_depth)));
183
184 unmap_array = unmap_q->unmap_array;
185 unmap_cons = unmap_q->consumer_index;
186
187 prefetch(&unmap_array[unmap_cons + 1]);
188 while (wis) {
189 skb = unmap_array[unmap_cons].skb;
190
191 unmap_array[unmap_cons].skb = NULL;
192
193 sent_packets++;
194 sent_bytes += skb->len;
195 wis -= BNA_TXQ_WI_NEEDED(1 + skb_shinfo(skb)->nr_frags);
196
197 pci_unmap_single(bnad->pcidev,
198 pci_unmap_addr(&unmap_array[unmap_cons],
199 dma_addr), skb_headlen(skb),
200 PCI_DMA_TODEVICE);
201 pci_unmap_addr_set(&unmap_array[unmap_cons], dma_addr, 0);
202 BNA_QE_INDX_ADD(unmap_cons, 1, unmap_q->q_depth);
203
204 prefetch(&unmap_array[unmap_cons + 1]);
205 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
206 prefetch(&unmap_array[unmap_cons + 1]);
207
208 pci_unmap_page(bnad->pcidev,
209 pci_unmap_addr(&unmap_array[unmap_cons],
210 dma_addr),
211 skb_shinfo(skb)->frags[i].size,
212 PCI_DMA_TODEVICE);
213 pci_unmap_addr_set(&unmap_array[unmap_cons], dma_addr,
214 0);
215 BNA_QE_INDX_ADD(unmap_cons, 1, unmap_q->q_depth);
216 }
217 dev_kfree_skb_any(skb);
218 }
219
220 /* Update consumer pointers. */
221 tcb->consumer_index = updated_hw_cons;
222 unmap_q->consumer_index = unmap_cons;
223
224 tcb->txq->tx_packets += sent_packets;
225 tcb->txq->tx_bytes += sent_bytes;
226
227 return sent_packets;
228}
229
230/* Tx Free Tasklet function */
231/* Frees for all the tcb's in all the Tx's */
232/*
233 * Scheduled from sending context, so that
234 * the fat Tx lock is not held for too long
235 * in the sending context.
236 */
237static void
238bnad_tx_free_tasklet(unsigned long bnad_ptr)
239{
240 struct bnad *bnad = (struct bnad *)bnad_ptr;
241 struct bna_tcb *tcb;
242 u32 acked;
243 int i, j;
244
245 for (i = 0; i < bnad->num_tx; i++) {
246 for (j = 0; j < bnad->num_txq_per_tx; j++) {
247 tcb = bnad->tx_info[i].tcb[j];
248 if (!tcb)
249 continue;
250 if (((u16) (*tcb->hw_consumer_index) !=
251 tcb->consumer_index) &&
252 (!test_and_set_bit(BNAD_TXQ_FREE_SENT,
253 &tcb->flags))) {
254 acked = bnad_free_txbufs(bnad, tcb);
255 bna_ib_ack(tcb->i_dbell, acked);
256 smp_mb__before_clear_bit();
257 clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
258 }
259 }
260 }
261}
262
263static u32
264bnad_tx(struct bnad *bnad, struct bna_tcb *tcb)
265{
266 struct net_device *netdev = bnad->netdev;
267 u32 sent;
268
269 if (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags))
270 return 0;
271
272 sent = bnad_free_txbufs(bnad, tcb);
273 if (sent) {
274 if (netif_queue_stopped(netdev) &&
275 netif_carrier_ok(netdev) &&
276 BNA_QE_FREE_CNT(tcb, tcb->q_depth) >=
277 BNAD_NETIF_WAKE_THRESHOLD) {
278 netif_wake_queue(netdev);
279 BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
280 }
281 bna_ib_ack(tcb->i_dbell, sent);
282 } else
283 bna_ib_ack(tcb->i_dbell, 0);
284
285 smp_mb__before_clear_bit();
286 clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
287
288 return sent;
289}
290
291/* MSIX Tx Completion Handler */
292static irqreturn_t
293bnad_msix_tx(int irq, void *data)
294{
295 struct bna_tcb *tcb = (struct bna_tcb *)data;
296 struct bnad *bnad = tcb->bnad;
297
298 bnad_tx(bnad, tcb);
299
300 return IRQ_HANDLED;
301}
302
303static void
304bnad_reset_rcb(struct bnad *bnad, struct bna_rcb *rcb)
305{
306 struct bnad_unmap_q *unmap_q = rcb->unmap_q;
307
308 rcb->producer_index = 0;
309 rcb->consumer_index = 0;
310
311 unmap_q->producer_index = 0;
312 unmap_q->consumer_index = 0;
313}
314
315static void
316bnad_free_rxbufs(struct bnad *bnad, struct bna_rcb *rcb)
317{
318 struct bnad_unmap_q *unmap_q;
319 struct sk_buff *skb;
320
321 unmap_q = rcb->unmap_q;
322 while (BNA_QE_IN_USE_CNT(unmap_q, unmap_q->q_depth)) {
323 skb = unmap_q->unmap_array[unmap_q->consumer_index].skb;
324 BUG_ON(!(skb));
325 unmap_q->unmap_array[unmap_q->consumer_index].skb = NULL;
326 pci_unmap_single(bnad->pcidev, pci_unmap_addr(&unmap_q->
327 unmap_array[unmap_q->consumer_index],
328 dma_addr), rcb->rxq->buffer_size +
329 NET_IP_ALIGN, PCI_DMA_FROMDEVICE);
330 dev_kfree_skb(skb);
331 BNA_QE_INDX_ADD(unmap_q->consumer_index, 1, unmap_q->q_depth);
332 BNA_QE_INDX_ADD(rcb->consumer_index, 1, rcb->q_depth);
333 }
334
335 bnad_reset_rcb(bnad, rcb);
336}
337
338static void
339bnad_alloc_n_post_rxbufs(struct bnad *bnad, struct bna_rcb *rcb)
340{
341 u16 to_alloc, alloced, unmap_prod, wi_range;
342 struct bnad_unmap_q *unmap_q = rcb->unmap_q;
343 struct bnad_skb_unmap *unmap_array;
344 struct bna_rxq_entry *rxent;
345 struct sk_buff *skb;
346 dma_addr_t dma_addr;
347
348 alloced = 0;
349 to_alloc =
350 BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth);
351
352 unmap_array = unmap_q->unmap_array;
353 unmap_prod = unmap_q->producer_index;
354
355 BNA_RXQ_QPGE_PTR_GET(unmap_prod, rcb->sw_qpt, rxent, wi_range);
356
357 while (to_alloc--) {
358 if (!wi_range) {
359 BNA_RXQ_QPGE_PTR_GET(unmap_prod, rcb->sw_qpt, rxent,
360 wi_range);
361 }
362 skb = alloc_skb(rcb->rxq->buffer_size + NET_IP_ALIGN,
363 GFP_ATOMIC);
364 if (unlikely(!skb)) {
365 BNAD_UPDATE_CTR(bnad, rxbuf_alloc_failed);
366 goto finishing;
367 }
368 skb->dev = bnad->netdev;
369 skb_reserve(skb, NET_IP_ALIGN);
370 unmap_array[unmap_prod].skb = skb;
371 dma_addr = pci_map_single(bnad->pcidev, skb->data,
372 rcb->rxq->buffer_size, PCI_DMA_FROMDEVICE);
373 pci_unmap_addr_set(&unmap_array[unmap_prod], dma_addr,
374 dma_addr);
375 BNA_SET_DMA_ADDR(dma_addr, &rxent->host_addr);
376 BNA_QE_INDX_ADD(unmap_prod, 1, unmap_q->q_depth);
377
378 rxent++;
379 wi_range--;
380 alloced++;
381 }
382
383finishing:
384 if (likely(alloced)) {
385 unmap_q->producer_index = unmap_prod;
386 rcb->producer_index = unmap_prod;
387 smp_mb();
388 bna_rxq_prod_indx_doorbell(rcb);
389 }
390}
391
392/*
393 * Locking is required in the enable path
394 * because it is called from a napi poll
395 * context, where the bna_lock is not held
396 * unlike the IRQ context.
397 */
398static void
399bnad_enable_txrx_irqs(struct bnad *bnad)
400{
401 struct bna_tcb *tcb;
402 struct bna_ccb *ccb;
403 int i, j;
404 unsigned long flags;
405
406 spin_lock_irqsave(&bnad->bna_lock, flags);
407 for (i = 0; i < bnad->num_tx; i++) {
408 for (j = 0; j < bnad->num_txq_per_tx; j++) {
409 tcb = bnad->tx_info[i].tcb[j];
410 bna_ib_coalescing_timer_set(tcb->i_dbell,
411 tcb->txq->ib->ib_config.coalescing_timeo);
412 bna_ib_ack(tcb->i_dbell, 0);
413 }
414 }
415
416 for (i = 0; i < bnad->num_rx; i++) {
417 for (j = 0; j < bnad->num_rxp_per_rx; j++) {
418 ccb = bnad->rx_info[i].rx_ctrl[j].ccb;
419 bnad_enable_rx_irq_unsafe(ccb);
420 }
421 }
422 spin_unlock_irqrestore(&bnad->bna_lock, flags);
423}
424
425static inline void
426bnad_refill_rxq(struct bnad *bnad, struct bna_rcb *rcb)
427{
428 struct bnad_unmap_q *unmap_q = rcb->unmap_q;
429
430 if (!test_and_set_bit(BNAD_RXQ_REFILL, &rcb->flags)) {
431 if (BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth)
432 >> BNAD_RXQ_REFILL_THRESHOLD_SHIFT)
433 bnad_alloc_n_post_rxbufs(bnad, rcb);
434 smp_mb__before_clear_bit();
435 clear_bit(BNAD_RXQ_REFILL, &rcb->flags);
436 }
437}
438
439static u32
440bnad_poll_cq(struct bnad *bnad, struct bna_ccb *ccb, int budget)
441{
442 struct bna_cq_entry *cmpl, *next_cmpl;
443 struct bna_rcb *rcb = NULL;
444 unsigned int wi_range, packets = 0, wis = 0;
445 struct bnad_unmap_q *unmap_q;
446 struct sk_buff *skb;
447 u32 flags;
448 u32 qid0 = ccb->rcb[0]->rxq->rxq_id;
449 struct bna_pkt_rate *pkt_rt = &ccb->pkt_rate;
450
451 prefetch(bnad->netdev);
452 BNA_CQ_QPGE_PTR_GET(ccb->producer_index, ccb->sw_qpt, cmpl,
453 wi_range);
454 BUG_ON(!(wi_range <= ccb->q_depth));
455 while (cmpl->valid && packets < budget) {
456 packets++;
457 BNA_UPDATE_PKT_CNT(pkt_rt, ntohs(cmpl->length));
458
459 if (qid0 == cmpl->rxq_id)
460 rcb = ccb->rcb[0];
461 else
462 rcb = ccb->rcb[1];
463
464 unmap_q = rcb->unmap_q;
465
466 skb = unmap_q->unmap_array[unmap_q->consumer_index].skb;
467 BUG_ON(!(skb));
468 unmap_q->unmap_array[unmap_q->consumer_index].skb = NULL;
469 pci_unmap_single(bnad->pcidev,
470 pci_unmap_addr(&unmap_q->
471 unmap_array[unmap_q->
472 consumer_index],
473 dma_addr),
474 rcb->rxq->buffer_size,
475 PCI_DMA_FROMDEVICE);
476 BNA_QE_INDX_ADD(unmap_q->consumer_index, 1, unmap_q->q_depth);
477
478 /* Should be more efficient ? Performance ? */
479 BNA_QE_INDX_ADD(rcb->consumer_index, 1, rcb->q_depth);
480
481 wis++;
482 if (likely(--wi_range))
483 next_cmpl = cmpl + 1;
484 else {
485 BNA_QE_INDX_ADD(ccb->producer_index, wis, ccb->q_depth);
486 wis = 0;
487 BNA_CQ_QPGE_PTR_GET(ccb->producer_index, ccb->sw_qpt,
488 next_cmpl, wi_range);
489 BUG_ON(!(wi_range <= ccb->q_depth));
490 }
491 prefetch(next_cmpl);
492
493 flags = ntohl(cmpl->flags);
494 if (unlikely
495 (flags &
496 (BNA_CQ_EF_MAC_ERROR | BNA_CQ_EF_FCS_ERROR |
497 BNA_CQ_EF_TOO_LONG))) {
498 dev_kfree_skb_any(skb);
499 rcb->rxq->rx_packets_with_error++;
500 goto next;
501 }
502
503 skb_put(skb, ntohs(cmpl->length));
504 if (likely
505 (bnad->rx_csum &&
506 (((flags & BNA_CQ_EF_IPV4) &&
507 (flags & BNA_CQ_EF_L3_CKSUM_OK)) ||
508 (flags & BNA_CQ_EF_IPV6)) &&
509 (flags & (BNA_CQ_EF_TCP | BNA_CQ_EF_UDP)) &&
510 (flags & BNA_CQ_EF_L4_CKSUM_OK)))
511 skb->ip_summed = CHECKSUM_UNNECESSARY;
512 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700513 skb_checksum_none_assert(skb);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700514
515 rcb->rxq->rx_packets++;
516 rcb->rxq->rx_bytes += skb->len;
517 skb->protocol = eth_type_trans(skb, bnad->netdev);
518
519 if (bnad->vlan_grp && (flags & BNA_CQ_EF_VLAN)) {
520 struct bnad_rx_ctrl *rx_ctrl =
521 (struct bnad_rx_ctrl *)ccb->ctrl;
522 if (skb->ip_summed == CHECKSUM_UNNECESSARY)
523 vlan_gro_receive(&rx_ctrl->napi, bnad->vlan_grp,
524 ntohs(cmpl->vlan_tag), skb);
525 else
526 vlan_hwaccel_receive_skb(skb,
527 bnad->vlan_grp,
528 ntohs(cmpl->vlan_tag));
529
530 } else { /* Not VLAN tagged/stripped */
531 struct bnad_rx_ctrl *rx_ctrl =
532 (struct bnad_rx_ctrl *)ccb->ctrl;
533 if (skb->ip_summed == CHECKSUM_UNNECESSARY)
534 napi_gro_receive(&rx_ctrl->napi, skb);
535 else
536 netif_receive_skb(skb);
537 }
538
539next:
540 cmpl->valid = 0;
541 cmpl = next_cmpl;
542 }
543
544 BNA_QE_INDX_ADD(ccb->producer_index, wis, ccb->q_depth);
545
546 if (likely(ccb)) {
547 bna_ib_ack(ccb->i_dbell, packets);
548 bnad_refill_rxq(bnad, ccb->rcb[0]);
549 if (ccb->rcb[1])
550 bnad_refill_rxq(bnad, ccb->rcb[1]);
551 } else
552 bna_ib_ack(ccb->i_dbell, 0);
553
554 return packets;
555}
556
557static void
558bnad_disable_rx_irq(struct bnad *bnad, struct bna_ccb *ccb)
559{
560 bna_ib_coalescing_timer_set(ccb->i_dbell, 0);
561 bna_ib_ack(ccb->i_dbell, 0);
562}
563
564static void
565bnad_enable_rx_irq(struct bnad *bnad, struct bna_ccb *ccb)
566{
Rasesh Modye2fa6f22010-10-05 15:46:04 +0000567 unsigned long flags;
568
569 spin_lock_irqsave(&bnad->bna_lock, flags); /* Because of polling context */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700570 bnad_enable_rx_irq_unsafe(ccb);
Rasesh Modye2fa6f22010-10-05 15:46:04 +0000571 spin_unlock_irqrestore(&bnad->bna_lock, flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700572}
573
574static void
575bnad_netif_rx_schedule_poll(struct bnad *bnad, struct bna_ccb *ccb)
576{
577 struct bnad_rx_ctrl *rx_ctrl = (struct bnad_rx_ctrl *)(ccb->ctrl);
578 if (likely(napi_schedule_prep((&rx_ctrl->napi)))) {
579 bnad_disable_rx_irq(bnad, ccb);
580 __napi_schedule((&rx_ctrl->napi));
581 }
582 BNAD_UPDATE_CTR(bnad, netif_rx_schedule);
583}
584
585/* MSIX Rx Path Handler */
586static irqreturn_t
587bnad_msix_rx(int irq, void *data)
588{
589 struct bna_ccb *ccb = (struct bna_ccb *)data;
590 struct bnad *bnad = ccb->bnad;
591
592 bnad_netif_rx_schedule_poll(bnad, ccb);
593
594 return IRQ_HANDLED;
595}
596
597/* Interrupt handlers */
598
599/* Mbox Interrupt Handlers */
600static irqreturn_t
601bnad_msix_mbox_handler(int irq, void *data)
602{
603 u32 intr_status;
Rasesh Modye2fa6f22010-10-05 15:46:04 +0000604 unsigned long flags;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700605 struct net_device *netdev = data;
606 struct bnad *bnad;
607
608 bnad = netdev_priv(netdev);
609
610 /* BNA_ISR_GET(bnad); Inc Ref count */
611 spin_lock_irqsave(&bnad->bna_lock, flags);
612
613 bna_intr_status_get(&bnad->bna, intr_status);
614
615 if (BNA_IS_MBOX_ERR_INTR(intr_status))
616 bna_mbox_handler(&bnad->bna, intr_status);
617
618 spin_unlock_irqrestore(&bnad->bna_lock, flags);
619
620 /* BNAD_ISR_PUT(bnad); Dec Ref count */
621 return IRQ_HANDLED;
622}
623
624static irqreturn_t
625bnad_isr(int irq, void *data)
626{
627 int i, j;
628 u32 intr_status;
629 unsigned long flags;
630 struct net_device *netdev = data;
631 struct bnad *bnad = netdev_priv(netdev);
632 struct bnad_rx_info *rx_info;
633 struct bnad_rx_ctrl *rx_ctrl;
634
Rasesh Modye2fa6f22010-10-05 15:46:04 +0000635 if (unlikely(test_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags)))
636 return IRQ_NONE;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700637
638 bna_intr_status_get(&bnad->bna, intr_status);
Rasesh Modye2fa6f22010-10-05 15:46:04 +0000639
640 if (unlikely(!intr_status))
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700641 return IRQ_NONE;
Rasesh Modye2fa6f22010-10-05 15:46:04 +0000642
643 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700644
645 if (BNA_IS_MBOX_ERR_INTR(intr_status)) {
646 bna_mbox_handler(&bnad->bna, intr_status);
647 if (!BNA_IS_INTX_DATA_INTR(intr_status)) {
648 spin_unlock_irqrestore(&bnad->bna_lock, flags);
649 goto done;
650 }
651 }
652 spin_unlock_irqrestore(&bnad->bna_lock, flags);
653
654 /* Process data interrupts */
655 for (i = 0; i < bnad->num_rx; i++) {
656 rx_info = &bnad->rx_info[i];
657 if (!rx_info->rx)
658 continue;
659 for (j = 0; j < bnad->num_rxp_per_rx; j++) {
660 rx_ctrl = &rx_info->rx_ctrl[j];
661 if (rx_ctrl->ccb)
662 bnad_netif_rx_schedule_poll(bnad,
663 rx_ctrl->ccb);
664 }
665 }
666done:
667 return IRQ_HANDLED;
668}
669
670/*
671 * Called in interrupt / callback context
672 * with bna_lock held, so cfg_flags access is OK
673 */
674static void
675bnad_enable_mbox_irq(struct bnad *bnad)
676{
677 int irq = BNAD_GET_MBOX_IRQ(bnad);
678
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700679 if (test_and_clear_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags))
Rasesh Modye2fa6f22010-10-05 15:46:04 +0000680 if (bnad->cfg_flags & BNAD_CF_MSIX)
681 enable_irq(irq);
682
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700683 BNAD_UPDATE_CTR(bnad, mbox_intr_enabled);
684}
685
686/*
687 * Called with bnad->bna_lock held b'cos of
688 * bnad->cfg_flags access.
689 */
690void
691bnad_disable_mbox_irq(struct bnad *bnad)
692{
693 int irq = BNAD_GET_MBOX_IRQ(bnad);
694
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700695
696 if (!test_and_set_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags))
Rasesh Modye2fa6f22010-10-05 15:46:04 +0000697 if (bnad->cfg_flags & BNAD_CF_MSIX)
698 disable_irq_nosync(irq);
699
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700700 BNAD_UPDATE_CTR(bnad, mbox_intr_disabled);
701}
702
703/* Control Path Handlers */
704
705/* Callbacks */
706void
707bnad_cb_device_enable_mbox_intr(struct bnad *bnad)
708{
709 bnad_enable_mbox_irq(bnad);
710}
711
712void
713bnad_cb_device_disable_mbox_intr(struct bnad *bnad)
714{
715 bnad_disable_mbox_irq(bnad);
716}
717
718void
719bnad_cb_device_enabled(struct bnad *bnad, enum bna_cb_status status)
720{
721 complete(&bnad->bnad_completions.ioc_comp);
722 bnad->bnad_completions.ioc_comp_status = status;
723}
724
725void
726bnad_cb_device_disabled(struct bnad *bnad, enum bna_cb_status status)
727{
728 complete(&bnad->bnad_completions.ioc_comp);
729 bnad->bnad_completions.ioc_comp_status = status;
730}
731
732static void
733bnad_cb_port_disabled(void *arg, enum bna_cb_status status)
734{
735 struct bnad *bnad = (struct bnad *)arg;
736
737 complete(&bnad->bnad_completions.port_comp);
738
739 netif_carrier_off(bnad->netdev);
740}
741
742void
743bnad_cb_port_link_status(struct bnad *bnad,
744 enum bna_link_status link_status)
745{
746 bool link_up = 0;
747
748 link_up = (link_status == BNA_LINK_UP) || (link_status == BNA_CEE_UP);
749
750 if (link_status == BNA_CEE_UP) {
751 set_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags);
752 BNAD_UPDATE_CTR(bnad, cee_up);
753 } else
754 clear_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags);
755
756 if (link_up) {
757 if (!netif_carrier_ok(bnad->netdev)) {
758 pr_warn("bna: %s link up\n",
759 bnad->netdev->name);
760 netif_carrier_on(bnad->netdev);
761 BNAD_UPDATE_CTR(bnad, link_toggle);
762 if (test_bit(BNAD_RF_TX_STARTED, &bnad->run_flags)) {
763 /* Force an immediate Transmit Schedule */
764 pr_info("bna: %s TX_STARTED\n",
765 bnad->netdev->name);
766 netif_wake_queue(bnad->netdev);
767 BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
768 } else {
769 netif_stop_queue(bnad->netdev);
770 BNAD_UPDATE_CTR(bnad, netif_queue_stop);
771 }
772 }
773 } else {
774 if (netif_carrier_ok(bnad->netdev)) {
775 pr_warn("bna: %s link down\n",
776 bnad->netdev->name);
777 netif_carrier_off(bnad->netdev);
778 BNAD_UPDATE_CTR(bnad, link_toggle);
779 }
780 }
781}
782
783static void
784bnad_cb_tx_disabled(void *arg, struct bna_tx *tx,
785 enum bna_cb_status status)
786{
787 struct bnad *bnad = (struct bnad *)arg;
788
789 complete(&bnad->bnad_completions.tx_comp);
790}
791
792static void
793bnad_cb_tcb_setup(struct bnad *bnad, struct bna_tcb *tcb)
794{
795 struct bnad_tx_info *tx_info =
796 (struct bnad_tx_info *)tcb->txq->tx->priv;
797 struct bnad_unmap_q *unmap_q = tcb->unmap_q;
798
799 tx_info->tcb[tcb->id] = tcb;
800 unmap_q->producer_index = 0;
801 unmap_q->consumer_index = 0;
802 unmap_q->q_depth = BNAD_TX_UNMAPQ_DEPTH;
803}
804
805static void
806bnad_cb_tcb_destroy(struct bnad *bnad, struct bna_tcb *tcb)
807{
808 struct bnad_tx_info *tx_info =
809 (struct bnad_tx_info *)tcb->txq->tx->priv;
810
811 tx_info->tcb[tcb->id] = NULL;
812}
813
814static void
815bnad_cb_rcb_setup(struct bnad *bnad, struct bna_rcb *rcb)
816{
817 struct bnad_unmap_q *unmap_q = rcb->unmap_q;
818
819 unmap_q->producer_index = 0;
820 unmap_q->consumer_index = 0;
821 unmap_q->q_depth = BNAD_RX_UNMAPQ_DEPTH;
822}
823
824static void
825bnad_cb_ccb_setup(struct bnad *bnad, struct bna_ccb *ccb)
826{
827 struct bnad_rx_info *rx_info =
828 (struct bnad_rx_info *)ccb->cq->rx->priv;
829
830 rx_info->rx_ctrl[ccb->id].ccb = ccb;
831 ccb->ctrl = &rx_info->rx_ctrl[ccb->id];
832}
833
834static void
835bnad_cb_ccb_destroy(struct bnad *bnad, struct bna_ccb *ccb)
836{
837 struct bnad_rx_info *rx_info =
838 (struct bnad_rx_info *)ccb->cq->rx->priv;
839
840 rx_info->rx_ctrl[ccb->id].ccb = NULL;
841}
842
843static void
844bnad_cb_tx_stall(struct bnad *bnad, struct bna_tcb *tcb)
845{
846 struct bnad_tx_info *tx_info =
847 (struct bnad_tx_info *)tcb->txq->tx->priv;
848
849 if (tx_info != &bnad->tx_info[0])
850 return;
851
852 clear_bit(BNAD_RF_TX_STARTED, &bnad->run_flags);
853 netif_stop_queue(bnad->netdev);
854 pr_info("bna: %s TX_STOPPED\n", bnad->netdev->name);
855}
856
857static void
858bnad_cb_tx_resume(struct bnad *bnad, struct bna_tcb *tcb)
859{
860 if (test_and_set_bit(BNAD_RF_TX_STARTED, &bnad->run_flags))
861 return;
862
863 if (netif_carrier_ok(bnad->netdev)) {
864 pr_info("bna: %s TX_STARTED\n", bnad->netdev->name);
865 netif_wake_queue(bnad->netdev);
866 BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
867 }
868}
869
870static void
871bnad_cb_tx_cleanup(struct bnad *bnad, struct bna_tcb *tcb)
872{
David S. Miller0ea05ce2010-09-12 12:06:00 -0700873 struct bnad_unmap_q *unmap_q;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700874
875 if (!tcb || (!tcb->unmap_q))
876 return;
877
David S. Miller0ea05ce2010-09-12 12:06:00 -0700878 unmap_q = tcb->unmap_q;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700879 if (!unmap_q->unmap_array)
880 return;
881
882 if (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags))
883 return;
884
885 bnad_free_all_txbufs(bnad, tcb);
886
887 unmap_q->producer_index = 0;
888 unmap_q->consumer_index = 0;
889
890 smp_mb__before_clear_bit();
891 clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
892}
893
894static void
895bnad_cb_rx_cleanup(struct bnad *bnad,
896 struct bna_ccb *ccb)
897{
898 bnad_cq_cmpl_init(bnad, ccb);
899
900 bnad_free_rxbufs(bnad, ccb->rcb[0]);
901 clear_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags);
902
903 if (ccb->rcb[1]) {
904 bnad_free_rxbufs(bnad, ccb->rcb[1]);
905 clear_bit(BNAD_RXQ_STARTED, &ccb->rcb[1]->flags);
906 }
907}
908
909static void
910bnad_cb_rx_post(struct bnad *bnad, struct bna_rcb *rcb)
911{
912 struct bnad_unmap_q *unmap_q = rcb->unmap_q;
913
914 set_bit(BNAD_RXQ_STARTED, &rcb->flags);
915
916 /* Now allocate & post buffers for this RCB */
917 /* !!Allocation in callback context */
918 if (!test_and_set_bit(BNAD_RXQ_REFILL, &rcb->flags)) {
919 if (BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth)
920 >> BNAD_RXQ_REFILL_THRESHOLD_SHIFT)
921 bnad_alloc_n_post_rxbufs(bnad, rcb);
922 smp_mb__before_clear_bit();
923 clear_bit(BNAD_RXQ_REFILL, &rcb->flags);
924 }
925}
926
927static void
928bnad_cb_rx_disabled(void *arg, struct bna_rx *rx,
929 enum bna_cb_status status)
930{
931 struct bnad *bnad = (struct bnad *)arg;
932
933 complete(&bnad->bnad_completions.rx_comp);
934}
935
936static void
937bnad_cb_rx_mcast_add(struct bnad *bnad, struct bna_rx *rx,
938 enum bna_cb_status status)
939{
940 bnad->bnad_completions.mcast_comp_status = status;
941 complete(&bnad->bnad_completions.mcast_comp);
942}
943
944void
945bnad_cb_stats_get(struct bnad *bnad, enum bna_cb_status status,
946 struct bna_stats *stats)
947{
948 if (status == BNA_CB_SUCCESS)
949 BNAD_UPDATE_CTR(bnad, hw_stats_updates);
950
951 if (!netif_running(bnad->netdev) ||
952 !test_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
953 return;
954
955 mod_timer(&bnad->stats_timer,
956 jiffies + msecs_to_jiffies(BNAD_STATS_TIMER_FREQ));
957}
958
959void
960bnad_cb_stats_clr(struct bnad *bnad)
961{
962}
963
964/* Resource allocation, free functions */
965
966static void
967bnad_mem_free(struct bnad *bnad,
968 struct bna_mem_info *mem_info)
969{
970 int i;
971 dma_addr_t dma_pa;
972
973 if (mem_info->mdl == NULL)
974 return;
975
976 for (i = 0; i < mem_info->num; i++) {
977 if (mem_info->mdl[i].kva != NULL) {
978 if (mem_info->mem_type == BNA_MEM_T_DMA) {
979 BNA_GET_DMA_ADDR(&(mem_info->mdl[i].dma),
980 dma_pa);
981 pci_free_consistent(bnad->pcidev,
982 mem_info->mdl[i].len,
983 mem_info->mdl[i].kva, dma_pa);
984 } else
985 kfree(mem_info->mdl[i].kva);
986 }
987 }
988 kfree(mem_info->mdl);
989 mem_info->mdl = NULL;
990}
991
992static int
993bnad_mem_alloc(struct bnad *bnad,
994 struct bna_mem_info *mem_info)
995{
996 int i;
997 dma_addr_t dma_pa;
998
999 if ((mem_info->num == 0) || (mem_info->len == 0)) {
1000 mem_info->mdl = NULL;
1001 return 0;
1002 }
1003
1004 mem_info->mdl = kcalloc(mem_info->num, sizeof(struct bna_mem_descr),
1005 GFP_KERNEL);
1006 if (mem_info->mdl == NULL)
1007 return -ENOMEM;
1008
1009 if (mem_info->mem_type == BNA_MEM_T_DMA) {
1010 for (i = 0; i < mem_info->num; i++) {
1011 mem_info->mdl[i].len = mem_info->len;
1012 mem_info->mdl[i].kva =
1013 pci_alloc_consistent(bnad->pcidev,
1014 mem_info->len, &dma_pa);
1015
1016 if (mem_info->mdl[i].kva == NULL)
1017 goto err_return;
1018
1019 BNA_SET_DMA_ADDR(dma_pa,
1020 &(mem_info->mdl[i].dma));
1021 }
1022 } else {
1023 for (i = 0; i < mem_info->num; i++) {
1024 mem_info->mdl[i].len = mem_info->len;
1025 mem_info->mdl[i].kva = kzalloc(mem_info->len,
1026 GFP_KERNEL);
1027 if (mem_info->mdl[i].kva == NULL)
1028 goto err_return;
1029 }
1030 }
1031
1032 return 0;
1033
1034err_return:
1035 bnad_mem_free(bnad, mem_info);
1036 return -ENOMEM;
1037}
1038
1039/* Free IRQ for Mailbox */
1040static void
1041bnad_mbox_irq_free(struct bnad *bnad,
1042 struct bna_intr_info *intr_info)
1043{
1044 int irq;
1045 unsigned long flags;
1046
1047 if (intr_info->idl == NULL)
1048 return;
1049
1050 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001051 bnad_disable_mbox_irq(bnad);
Rasesh Modye2fa6f22010-10-05 15:46:04 +00001052 spin_unlock_irqrestore(&bnad->bna_lock, flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001053
1054 irq = BNAD_GET_MBOX_IRQ(bnad);
1055 free_irq(irq, bnad->netdev);
1056
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001057 kfree(intr_info->idl);
1058}
1059
1060/*
1061 * Allocates IRQ for Mailbox, but keep it disabled
1062 * This will be enabled once we get the mbox enable callback
1063 * from bna
1064 */
1065static int
1066bnad_mbox_irq_alloc(struct bnad *bnad,
1067 struct bna_intr_info *intr_info)
1068{
1069 int err;
1070 unsigned long flags;
1071 u32 irq;
1072 irq_handler_t irq_handler;
1073
1074 /* Mbox should use only 1 vector */
1075
1076 intr_info->idl = kzalloc(sizeof(*(intr_info->idl)), GFP_KERNEL);
1077 if (!intr_info->idl)
1078 return -ENOMEM;
1079
1080 spin_lock_irqsave(&bnad->bna_lock, flags);
1081 if (bnad->cfg_flags & BNAD_CF_MSIX) {
1082 irq_handler = (irq_handler_t)bnad_msix_mbox_handler;
1083 irq = bnad->msix_table[bnad->msix_num - 1].vector;
1084 flags = 0;
1085 intr_info->intr_type = BNA_INTR_T_MSIX;
1086 intr_info->idl[0].vector = bnad->msix_num - 1;
1087 } else {
1088 irq_handler = (irq_handler_t)bnad_isr;
1089 irq = bnad->pcidev->irq;
1090 flags = IRQF_SHARED;
1091 intr_info->intr_type = BNA_INTR_T_INTX;
1092 /* intr_info->idl.vector = 0 ? */
1093 }
1094 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1095
1096 sprintf(bnad->mbox_irq_name, "%s", BNAD_NAME);
1097
Rasesh Modye2fa6f22010-10-05 15:46:04 +00001098 /*
1099 * Set the Mbox IRQ disable flag, so that the IRQ handler
1100 * called from request_irq() for SHARED IRQs do not execute
1101 */
1102 set_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
1103
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001104 err = request_irq(irq, irq_handler, flags,
1105 bnad->mbox_irq_name, bnad->netdev);
Rasesh Modye2fa6f22010-10-05 15:46:04 +00001106
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001107 if (err) {
1108 kfree(intr_info->idl);
1109 intr_info->idl = NULL;
1110 return err;
1111 }
1112
1113 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Modye2fa6f22010-10-05 15:46:04 +00001114 if (bnad->cfg_flags & BNAD_CF_MSIX)
1115 disable_irq_nosync(irq);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001116 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1117 return 0;
1118}
1119
1120static void
1121bnad_txrx_irq_free(struct bnad *bnad, struct bna_intr_info *intr_info)
1122{
1123 kfree(intr_info->idl);
1124 intr_info->idl = NULL;
1125}
1126
1127/* Allocates Interrupt Descriptor List for MSIX/INT-X vectors */
1128static int
1129bnad_txrx_irq_alloc(struct bnad *bnad, enum bnad_intr_source src,
1130 uint txrx_id, struct bna_intr_info *intr_info)
1131{
1132 int i, vector_start = 0;
1133 u32 cfg_flags;
1134 unsigned long flags;
1135
1136 spin_lock_irqsave(&bnad->bna_lock, flags);
1137 cfg_flags = bnad->cfg_flags;
1138 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1139
1140 if (cfg_flags & BNAD_CF_MSIX) {
1141 intr_info->intr_type = BNA_INTR_T_MSIX;
1142 intr_info->idl = kcalloc(intr_info->num,
1143 sizeof(struct bna_intr_descr),
1144 GFP_KERNEL);
1145 if (!intr_info->idl)
1146 return -ENOMEM;
1147
1148 switch (src) {
1149 case BNAD_INTR_TX:
1150 vector_start = txrx_id;
1151 break;
1152
1153 case BNAD_INTR_RX:
1154 vector_start = bnad->num_tx * bnad->num_txq_per_tx +
1155 txrx_id;
1156 break;
1157
1158 default:
1159 BUG();
1160 }
1161
1162 for (i = 0; i < intr_info->num; i++)
1163 intr_info->idl[i].vector = vector_start + i;
1164 } else {
1165 intr_info->intr_type = BNA_INTR_T_INTX;
1166 intr_info->num = 1;
1167 intr_info->idl = kcalloc(intr_info->num,
1168 sizeof(struct bna_intr_descr),
1169 GFP_KERNEL);
1170 if (!intr_info->idl)
1171 return -ENOMEM;
1172
1173 switch (src) {
1174 case BNAD_INTR_TX:
1175 intr_info->idl[0].vector = 0x1; /* Bit mask : Tx IB */
1176 break;
1177
1178 case BNAD_INTR_RX:
1179 intr_info->idl[0].vector = 0x2; /* Bit mask : Rx IB */
1180 break;
1181 }
1182 }
1183 return 0;
1184}
1185
1186/**
1187 * NOTE: Should be called for MSIX only
1188 * Unregisters Tx MSIX vector(s) from the kernel
1189 */
1190static void
1191bnad_tx_msix_unregister(struct bnad *bnad, struct bnad_tx_info *tx_info,
1192 int num_txqs)
1193{
1194 int i;
1195 int vector_num;
1196
1197 for (i = 0; i < num_txqs; i++) {
1198 if (tx_info->tcb[i] == NULL)
1199 continue;
1200
1201 vector_num = tx_info->tcb[i]->intr_vector;
1202 free_irq(bnad->msix_table[vector_num].vector, tx_info->tcb[i]);
1203 }
1204}
1205
1206/**
1207 * NOTE: Should be called for MSIX only
1208 * Registers Tx MSIX vector(s) and ISR(s), cookie with the kernel
1209 */
1210static int
1211bnad_tx_msix_register(struct bnad *bnad, struct bnad_tx_info *tx_info,
1212 uint tx_id, int num_txqs)
1213{
1214 int i;
1215 int err;
1216 int vector_num;
1217
1218 for (i = 0; i < num_txqs; i++) {
1219 vector_num = tx_info->tcb[i]->intr_vector;
1220 sprintf(tx_info->tcb[i]->name, "%s TXQ %d", bnad->netdev->name,
1221 tx_id + tx_info->tcb[i]->id);
1222 err = request_irq(bnad->msix_table[vector_num].vector,
1223 (irq_handler_t)bnad_msix_tx, 0,
1224 tx_info->tcb[i]->name,
1225 tx_info->tcb[i]);
1226 if (err)
1227 goto err_return;
1228 }
1229
1230 return 0;
1231
1232err_return:
1233 if (i > 0)
1234 bnad_tx_msix_unregister(bnad, tx_info, (i - 1));
1235 return -1;
1236}
1237
1238/**
1239 * NOTE: Should be called for MSIX only
1240 * Unregisters Rx MSIX vector(s) from the kernel
1241 */
1242static void
1243bnad_rx_msix_unregister(struct bnad *bnad, struct bnad_rx_info *rx_info,
1244 int num_rxps)
1245{
1246 int i;
1247 int vector_num;
1248
1249 for (i = 0; i < num_rxps; i++) {
1250 if (rx_info->rx_ctrl[i].ccb == NULL)
1251 continue;
1252
1253 vector_num = rx_info->rx_ctrl[i].ccb->intr_vector;
1254 free_irq(bnad->msix_table[vector_num].vector,
1255 rx_info->rx_ctrl[i].ccb);
1256 }
1257}
1258
1259/**
1260 * NOTE: Should be called for MSIX only
1261 * Registers Tx MSIX vector(s) and ISR(s), cookie with the kernel
1262 */
1263static int
1264bnad_rx_msix_register(struct bnad *bnad, struct bnad_rx_info *rx_info,
1265 uint rx_id, int num_rxps)
1266{
1267 int i;
1268 int err;
1269 int vector_num;
1270
1271 for (i = 0; i < num_rxps; i++) {
1272 vector_num = rx_info->rx_ctrl[i].ccb->intr_vector;
1273 sprintf(rx_info->rx_ctrl[i].ccb->name, "%s CQ %d",
1274 bnad->netdev->name,
1275 rx_id + rx_info->rx_ctrl[i].ccb->id);
1276 err = request_irq(bnad->msix_table[vector_num].vector,
1277 (irq_handler_t)bnad_msix_rx, 0,
1278 rx_info->rx_ctrl[i].ccb->name,
1279 rx_info->rx_ctrl[i].ccb);
1280 if (err)
1281 goto err_return;
1282 }
1283
1284 return 0;
1285
1286err_return:
1287 if (i > 0)
1288 bnad_rx_msix_unregister(bnad, rx_info, (i - 1));
1289 return -1;
1290}
1291
1292/* Free Tx object Resources */
1293static void
1294bnad_tx_res_free(struct bnad *bnad, struct bna_res_info *res_info)
1295{
1296 int i;
1297
1298 for (i = 0; i < BNA_TX_RES_T_MAX; i++) {
1299 if (res_info[i].res_type == BNA_RES_T_MEM)
1300 bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
1301 else if (res_info[i].res_type == BNA_RES_T_INTR)
1302 bnad_txrx_irq_free(bnad, &res_info[i].res_u.intr_info);
1303 }
1304}
1305
1306/* Allocates memory and interrupt resources for Tx object */
1307static int
1308bnad_tx_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
1309 uint tx_id)
1310{
1311 int i, err = 0;
1312
1313 for (i = 0; i < BNA_TX_RES_T_MAX; i++) {
1314 if (res_info[i].res_type == BNA_RES_T_MEM)
1315 err = bnad_mem_alloc(bnad,
1316 &res_info[i].res_u.mem_info);
1317 else if (res_info[i].res_type == BNA_RES_T_INTR)
1318 err = bnad_txrx_irq_alloc(bnad, BNAD_INTR_TX, tx_id,
1319 &res_info[i].res_u.intr_info);
1320 if (err)
1321 goto err_return;
1322 }
1323 return 0;
1324
1325err_return:
1326 bnad_tx_res_free(bnad, res_info);
1327 return err;
1328}
1329
1330/* Free Rx object Resources */
1331static void
1332bnad_rx_res_free(struct bnad *bnad, struct bna_res_info *res_info)
1333{
1334 int i;
1335
1336 for (i = 0; i < BNA_RX_RES_T_MAX; i++) {
1337 if (res_info[i].res_type == BNA_RES_T_MEM)
1338 bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
1339 else if (res_info[i].res_type == BNA_RES_T_INTR)
1340 bnad_txrx_irq_free(bnad, &res_info[i].res_u.intr_info);
1341 }
1342}
1343
1344/* Allocates memory and interrupt resources for Rx object */
1345static int
1346bnad_rx_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
1347 uint rx_id)
1348{
1349 int i, err = 0;
1350
1351 /* All memory needs to be allocated before setup_ccbs */
1352 for (i = 0; i < BNA_RX_RES_T_MAX; i++) {
1353 if (res_info[i].res_type == BNA_RES_T_MEM)
1354 err = bnad_mem_alloc(bnad,
1355 &res_info[i].res_u.mem_info);
1356 else if (res_info[i].res_type == BNA_RES_T_INTR)
1357 err = bnad_txrx_irq_alloc(bnad, BNAD_INTR_RX, rx_id,
1358 &res_info[i].res_u.intr_info);
1359 if (err)
1360 goto err_return;
1361 }
1362 return 0;
1363
1364err_return:
1365 bnad_rx_res_free(bnad, res_info);
1366 return err;
1367}
1368
1369/* Timer callbacks */
1370/* a) IOC timer */
1371static void
1372bnad_ioc_timeout(unsigned long data)
1373{
1374 struct bnad *bnad = (struct bnad *)data;
1375 unsigned long flags;
1376
1377 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody8a891422010-08-25 23:00:27 -07001378 bfa_nw_ioc_timeout((void *) &bnad->bna.device.ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001379 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1380}
1381
1382static void
1383bnad_ioc_hb_check(unsigned long data)
1384{
1385 struct bnad *bnad = (struct bnad *)data;
1386 unsigned long flags;
1387
1388 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody8a891422010-08-25 23:00:27 -07001389 bfa_nw_ioc_hb_check((void *) &bnad->bna.device.ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001390 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1391}
1392
1393static void
1394bnad_ioc_sem_timeout(unsigned long data)
1395{
1396 struct bnad *bnad = (struct bnad *)data;
1397 unsigned long flags;
1398
1399 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody8a891422010-08-25 23:00:27 -07001400 bfa_nw_ioc_sem_timeout((void *) &bnad->bna.device.ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001401 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1402}
1403
1404/*
1405 * All timer routines use bnad->bna_lock to protect against
1406 * the following race, which may occur in case of no locking:
1407 * Time CPU m CPU n
1408 * 0 1 = test_bit
1409 * 1 clear_bit
1410 * 2 del_timer_sync
1411 * 3 mod_timer
1412 */
1413
1414/* b) Dynamic Interrupt Moderation Timer */
1415static void
1416bnad_dim_timeout(unsigned long data)
1417{
1418 struct bnad *bnad = (struct bnad *)data;
1419 struct bnad_rx_info *rx_info;
1420 struct bnad_rx_ctrl *rx_ctrl;
1421 int i, j;
1422 unsigned long flags;
1423
1424 if (!netif_carrier_ok(bnad->netdev))
1425 return;
1426
1427 spin_lock_irqsave(&bnad->bna_lock, flags);
1428 for (i = 0; i < bnad->num_rx; i++) {
1429 rx_info = &bnad->rx_info[i];
1430 if (!rx_info->rx)
1431 continue;
1432 for (j = 0; j < bnad->num_rxp_per_rx; j++) {
1433 rx_ctrl = &rx_info->rx_ctrl[j];
1434 if (!rx_ctrl->ccb)
1435 continue;
1436 bna_rx_dim_update(rx_ctrl->ccb);
1437 }
1438 }
1439
1440 /* Check for BNAD_CF_DIM_ENABLED, does not eleminate a race */
1441 if (test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags))
1442 mod_timer(&bnad->dim_timer,
1443 jiffies + msecs_to_jiffies(BNAD_DIM_TIMER_FREQ));
1444 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1445}
1446
1447/* c) Statistics Timer */
1448static void
1449bnad_stats_timeout(unsigned long data)
1450{
1451 struct bnad *bnad = (struct bnad *)data;
1452 unsigned long flags;
1453
1454 if (!netif_running(bnad->netdev) ||
1455 !test_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
1456 return;
1457
1458 spin_lock_irqsave(&bnad->bna_lock, flags);
1459 bna_stats_get(&bnad->bna);
1460 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1461}
1462
1463/*
1464 * Set up timer for DIM
1465 * Called with bnad->bna_lock held
1466 */
1467void
1468bnad_dim_timer_start(struct bnad *bnad)
1469{
1470 if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED &&
1471 !test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags)) {
1472 setup_timer(&bnad->dim_timer, bnad_dim_timeout,
1473 (unsigned long)bnad);
1474 set_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
1475 mod_timer(&bnad->dim_timer,
1476 jiffies + msecs_to_jiffies(BNAD_DIM_TIMER_FREQ));
1477 }
1478}
1479
1480/*
1481 * Set up timer for statistics
1482 * Called with mutex_lock(&bnad->conf_mutex) held
1483 */
1484static void
1485bnad_stats_timer_start(struct bnad *bnad)
1486{
1487 unsigned long flags;
1488
1489 spin_lock_irqsave(&bnad->bna_lock, flags);
1490 if (!test_and_set_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags)) {
1491 setup_timer(&bnad->stats_timer, bnad_stats_timeout,
1492 (unsigned long)bnad);
1493 mod_timer(&bnad->stats_timer,
1494 jiffies + msecs_to_jiffies(BNAD_STATS_TIMER_FREQ));
1495 }
1496 spin_unlock_irqrestore(&bnad->bna_lock, flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001497}
1498
1499/*
1500 * Stops the stats timer
1501 * Called with mutex_lock(&bnad->conf_mutex) held
1502 */
1503static void
1504bnad_stats_timer_stop(struct bnad *bnad)
1505{
1506 int to_del = 0;
1507 unsigned long flags;
1508
1509 spin_lock_irqsave(&bnad->bna_lock, flags);
1510 if (test_and_clear_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
1511 to_del = 1;
1512 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1513 if (to_del)
1514 del_timer_sync(&bnad->stats_timer);
1515}
1516
1517/* Utilities */
1518
1519static void
1520bnad_netdev_mc_list_get(struct net_device *netdev, u8 *mc_list)
1521{
1522 int i = 1; /* Index 0 has broadcast address */
1523 struct netdev_hw_addr *mc_addr;
1524
1525 netdev_for_each_mc_addr(mc_addr, netdev) {
1526 memcpy(&mc_list[i * ETH_ALEN], &mc_addr->addr[0],
1527 ETH_ALEN);
1528 i++;
1529 }
1530}
1531
1532static int
1533bnad_napi_poll_rx(struct napi_struct *napi, int budget)
1534{
1535 struct bnad_rx_ctrl *rx_ctrl =
1536 container_of(napi, struct bnad_rx_ctrl, napi);
1537 struct bna_ccb *ccb;
1538 struct bnad *bnad;
1539 int rcvd = 0;
1540
1541 ccb = rx_ctrl->ccb;
1542
1543 bnad = ccb->bnad;
1544
1545 if (!netif_carrier_ok(bnad->netdev))
1546 goto poll_exit;
1547
1548 rcvd = bnad_poll_cq(bnad, ccb, budget);
1549 if (rcvd == budget)
1550 return rcvd;
1551
1552poll_exit:
1553 napi_complete((napi));
1554
1555 BNAD_UPDATE_CTR(bnad, netif_rx_complete);
1556
1557 bnad_enable_rx_irq(bnad, ccb);
1558 return rcvd;
1559}
1560
1561static int
1562bnad_napi_poll_txrx(struct napi_struct *napi, int budget)
1563{
1564 struct bnad_rx_ctrl *rx_ctrl =
1565 container_of(napi, struct bnad_rx_ctrl, napi);
1566 struct bna_ccb *ccb;
1567 struct bnad *bnad;
1568 int rcvd = 0;
1569 int i, j;
1570
1571 ccb = rx_ctrl->ccb;
1572
1573 bnad = ccb->bnad;
1574
1575 if (!netif_carrier_ok(bnad->netdev))
1576 goto poll_exit;
1577
1578 /* Handle Tx Completions, if any */
1579 for (i = 0; i < bnad->num_tx; i++) {
1580 for (j = 0; j < bnad->num_txq_per_tx; j++)
1581 bnad_tx(bnad, bnad->tx_info[i].tcb[j]);
1582 }
1583
1584 /* Handle Rx Completions */
1585 rcvd = bnad_poll_cq(bnad, ccb, budget);
1586 if (rcvd == budget)
1587 return rcvd;
1588poll_exit:
1589 napi_complete((napi));
1590
1591 BNAD_UPDATE_CTR(bnad, netif_rx_complete);
1592
1593 bnad_enable_txrx_irqs(bnad);
1594 return rcvd;
1595}
1596
1597static void
1598bnad_napi_enable(struct bnad *bnad, u32 rx_id)
1599{
1600 int (*napi_poll) (struct napi_struct *, int);
1601 struct bnad_rx_ctrl *rx_ctrl;
1602 int i;
1603 unsigned long flags;
1604
1605 spin_lock_irqsave(&bnad->bna_lock, flags);
1606 if (bnad->cfg_flags & BNAD_CF_MSIX)
1607 napi_poll = bnad_napi_poll_rx;
1608 else
1609 napi_poll = bnad_napi_poll_txrx;
1610 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1611
1612 /* Initialize & enable NAPI */
1613 for (i = 0; i < bnad->num_rxp_per_rx; i++) {
1614 rx_ctrl = &bnad->rx_info[rx_id].rx_ctrl[i];
1615 netif_napi_add(bnad->netdev, &rx_ctrl->napi,
1616 napi_poll, 64);
1617 napi_enable(&rx_ctrl->napi);
1618 }
1619}
1620
1621static void
1622bnad_napi_disable(struct bnad *bnad, u32 rx_id)
1623{
1624 int i;
1625
1626 /* First disable and then clean up */
1627 for (i = 0; i < bnad->num_rxp_per_rx; i++) {
1628 napi_disable(&bnad->rx_info[rx_id].rx_ctrl[i].napi);
1629 netif_napi_del(&bnad->rx_info[rx_id].rx_ctrl[i].napi);
1630 }
1631}
1632
1633/* Should be held with conf_lock held */
1634void
1635bnad_cleanup_tx(struct bnad *bnad, uint tx_id)
1636{
1637 struct bnad_tx_info *tx_info = &bnad->tx_info[tx_id];
1638 struct bna_res_info *res_info = &bnad->tx_res_info[tx_id].res_info[0];
1639 unsigned long flags;
1640
1641 if (!tx_info->tx)
1642 return;
1643
1644 init_completion(&bnad->bnad_completions.tx_comp);
1645 spin_lock_irqsave(&bnad->bna_lock, flags);
1646 bna_tx_disable(tx_info->tx, BNA_HARD_CLEANUP, bnad_cb_tx_disabled);
1647 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1648 wait_for_completion(&bnad->bnad_completions.tx_comp);
1649
1650 if (tx_info->tcb[0]->intr_type == BNA_INTR_T_MSIX)
1651 bnad_tx_msix_unregister(bnad, tx_info,
1652 bnad->num_txq_per_tx);
1653
1654 spin_lock_irqsave(&bnad->bna_lock, flags);
1655 bna_tx_destroy(tx_info->tx);
1656 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1657
1658 tx_info->tx = NULL;
1659
1660 if (0 == tx_id)
1661 tasklet_kill(&bnad->tx_free_tasklet);
1662
1663 bnad_tx_res_free(bnad, res_info);
1664}
1665
1666/* Should be held with conf_lock held */
1667int
1668bnad_setup_tx(struct bnad *bnad, uint tx_id)
1669{
1670 int err;
1671 struct bnad_tx_info *tx_info = &bnad->tx_info[tx_id];
1672 struct bna_res_info *res_info = &bnad->tx_res_info[tx_id].res_info[0];
1673 struct bna_intr_info *intr_info =
1674 &res_info[BNA_TX_RES_INTR_T_TXCMPL].res_u.intr_info;
1675 struct bna_tx_config *tx_config = &bnad->tx_config[tx_id];
1676 struct bna_tx_event_cbfn tx_cbfn;
1677 struct bna_tx *tx;
1678 unsigned long flags;
1679
1680 /* Initialize the Tx object configuration */
1681 tx_config->num_txq = bnad->num_txq_per_tx;
1682 tx_config->txq_depth = bnad->txq_depth;
1683 tx_config->tx_type = BNA_TX_T_REGULAR;
1684
1685 /* Initialize the tx event handlers */
1686 tx_cbfn.tcb_setup_cbfn = bnad_cb_tcb_setup;
1687 tx_cbfn.tcb_destroy_cbfn = bnad_cb_tcb_destroy;
1688 tx_cbfn.tx_stall_cbfn = bnad_cb_tx_stall;
1689 tx_cbfn.tx_resume_cbfn = bnad_cb_tx_resume;
1690 tx_cbfn.tx_cleanup_cbfn = bnad_cb_tx_cleanup;
1691
1692 /* Get BNA's resource requirement for one tx object */
1693 spin_lock_irqsave(&bnad->bna_lock, flags);
1694 bna_tx_res_req(bnad->num_txq_per_tx,
1695 bnad->txq_depth, res_info);
1696 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1697
1698 /* Fill Unmap Q memory requirements */
1699 BNAD_FILL_UNMAPQ_MEM_REQ(
1700 &res_info[BNA_TX_RES_MEM_T_UNMAPQ],
1701 bnad->num_txq_per_tx,
1702 BNAD_TX_UNMAPQ_DEPTH);
1703
1704 /* Allocate resources */
1705 err = bnad_tx_res_alloc(bnad, res_info, tx_id);
1706 if (err)
1707 return err;
1708
1709 /* Ask BNA to create one Tx object, supplying required resources */
1710 spin_lock_irqsave(&bnad->bna_lock, flags);
1711 tx = bna_tx_create(&bnad->bna, bnad, tx_config, &tx_cbfn, res_info,
1712 tx_info);
1713 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1714 if (!tx)
1715 goto err_return;
1716 tx_info->tx = tx;
1717
1718 /* Register ISR for the Tx object */
1719 if (intr_info->intr_type == BNA_INTR_T_MSIX) {
1720 err = bnad_tx_msix_register(bnad, tx_info,
1721 tx_id, bnad->num_txq_per_tx);
1722 if (err)
1723 goto err_return;
1724 }
1725
1726 spin_lock_irqsave(&bnad->bna_lock, flags);
1727 bna_tx_enable(tx);
1728 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1729
1730 return 0;
1731
1732err_return:
1733 bnad_tx_res_free(bnad, res_info);
1734 return err;
1735}
1736
1737/* Setup the rx config for bna_rx_create */
1738/* bnad decides the configuration */
1739static void
1740bnad_init_rx_config(struct bnad *bnad, struct bna_rx_config *rx_config)
1741{
1742 rx_config->rx_type = BNA_RX_T_REGULAR;
1743 rx_config->num_paths = bnad->num_rxp_per_rx;
1744
1745 if (bnad->num_rxp_per_rx > 1) {
1746 rx_config->rss_status = BNA_STATUS_T_ENABLED;
1747 rx_config->rss_config.hash_type =
1748 (BFI_RSS_T_V4_TCP |
1749 BFI_RSS_T_V6_TCP |
1750 BFI_RSS_T_V4_IP |
1751 BFI_RSS_T_V6_IP);
1752 rx_config->rss_config.hash_mask =
1753 bnad->num_rxp_per_rx - 1;
1754 get_random_bytes(rx_config->rss_config.toeplitz_hash_key,
1755 sizeof(rx_config->rss_config.toeplitz_hash_key));
1756 } else {
1757 rx_config->rss_status = BNA_STATUS_T_DISABLED;
1758 memset(&rx_config->rss_config, 0,
1759 sizeof(rx_config->rss_config));
1760 }
1761 rx_config->rxp_type = BNA_RXP_SLR;
1762 rx_config->q_depth = bnad->rxq_depth;
1763
1764 rx_config->small_buff_size = BFI_SMALL_RXBUF_SIZE;
1765
1766 rx_config->vlan_strip_status = BNA_STATUS_T_ENABLED;
1767}
1768
1769/* Called with mutex_lock(&bnad->conf_mutex) held */
1770void
1771bnad_cleanup_rx(struct bnad *bnad, uint rx_id)
1772{
1773 struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
1774 struct bna_rx_config *rx_config = &bnad->rx_config[rx_id];
1775 struct bna_res_info *res_info = &bnad->rx_res_info[rx_id].res_info[0];
1776 unsigned long flags;
1777 int dim_timer_del = 0;
1778
1779 if (!rx_info->rx)
1780 return;
1781
1782 if (0 == rx_id) {
1783 spin_lock_irqsave(&bnad->bna_lock, flags);
1784 dim_timer_del = bnad_dim_timer_running(bnad);
1785 if (dim_timer_del)
1786 clear_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
1787 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1788 if (dim_timer_del)
1789 del_timer_sync(&bnad->dim_timer);
1790 }
1791
1792 bnad_napi_disable(bnad, rx_id);
1793
1794 init_completion(&bnad->bnad_completions.rx_comp);
1795 spin_lock_irqsave(&bnad->bna_lock, flags);
1796 bna_rx_disable(rx_info->rx, BNA_HARD_CLEANUP, bnad_cb_rx_disabled);
1797 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1798 wait_for_completion(&bnad->bnad_completions.rx_comp);
1799
1800 if (rx_info->rx_ctrl[0].ccb->intr_type == BNA_INTR_T_MSIX)
1801 bnad_rx_msix_unregister(bnad, rx_info, rx_config->num_paths);
1802
1803 spin_lock_irqsave(&bnad->bna_lock, flags);
1804 bna_rx_destroy(rx_info->rx);
1805 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1806
1807 rx_info->rx = NULL;
1808
1809 bnad_rx_res_free(bnad, res_info);
1810}
1811
1812/* Called with mutex_lock(&bnad->conf_mutex) held */
1813int
1814bnad_setup_rx(struct bnad *bnad, uint rx_id)
1815{
1816 int err;
1817 struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
1818 struct bna_res_info *res_info = &bnad->rx_res_info[rx_id].res_info[0];
1819 struct bna_intr_info *intr_info =
1820 &res_info[BNA_RX_RES_T_INTR].res_u.intr_info;
1821 struct bna_rx_config *rx_config = &bnad->rx_config[rx_id];
1822 struct bna_rx_event_cbfn rx_cbfn;
1823 struct bna_rx *rx;
1824 unsigned long flags;
1825
1826 /* Initialize the Rx object configuration */
1827 bnad_init_rx_config(bnad, rx_config);
1828
1829 /* Initialize the Rx event handlers */
1830 rx_cbfn.rcb_setup_cbfn = bnad_cb_rcb_setup;
1831 rx_cbfn.rcb_destroy_cbfn = NULL;
1832 rx_cbfn.ccb_setup_cbfn = bnad_cb_ccb_setup;
1833 rx_cbfn.ccb_destroy_cbfn = bnad_cb_ccb_destroy;
1834 rx_cbfn.rx_cleanup_cbfn = bnad_cb_rx_cleanup;
1835 rx_cbfn.rx_post_cbfn = bnad_cb_rx_post;
1836
1837 /* Get BNA's resource requirement for one Rx object */
1838 spin_lock_irqsave(&bnad->bna_lock, flags);
1839 bna_rx_res_req(rx_config, res_info);
1840 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1841
1842 /* Fill Unmap Q memory requirements */
1843 BNAD_FILL_UNMAPQ_MEM_REQ(
1844 &res_info[BNA_RX_RES_MEM_T_UNMAPQ],
1845 rx_config->num_paths +
1846 ((rx_config->rxp_type == BNA_RXP_SINGLE) ? 0 :
1847 rx_config->num_paths), BNAD_RX_UNMAPQ_DEPTH);
1848
1849 /* Allocate resource */
1850 err = bnad_rx_res_alloc(bnad, res_info, rx_id);
1851 if (err)
1852 return err;
1853
1854 /* Ask BNA to create one Rx object, supplying required resources */
1855 spin_lock_irqsave(&bnad->bna_lock, flags);
1856 rx = bna_rx_create(&bnad->bna, bnad, rx_config, &rx_cbfn, res_info,
1857 rx_info);
1858 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1859 if (!rx)
1860 goto err_return;
1861 rx_info->rx = rx;
1862
1863 /* Register ISR for the Rx object */
1864 if (intr_info->intr_type == BNA_INTR_T_MSIX) {
1865 err = bnad_rx_msix_register(bnad, rx_info, rx_id,
1866 rx_config->num_paths);
1867 if (err)
1868 goto err_return;
1869 }
1870
1871 /* Enable NAPI */
1872 bnad_napi_enable(bnad, rx_id);
1873
1874 spin_lock_irqsave(&bnad->bna_lock, flags);
1875 if (0 == rx_id) {
1876 /* Set up Dynamic Interrupt Moderation Vector */
1877 if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED)
1878 bna_rx_dim_reconfig(&bnad->bna, bna_napi_dim_vector);
1879
1880 /* Enable VLAN filtering only on the default Rx */
1881 bna_rx_vlanfilter_enable(rx);
1882
1883 /* Start the DIM timer */
1884 bnad_dim_timer_start(bnad);
1885 }
1886
1887 bna_rx_enable(rx);
1888 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1889
1890 return 0;
1891
1892err_return:
1893 bnad_cleanup_rx(bnad, rx_id);
1894 return err;
1895}
1896
1897/* Called with conf_lock & bnad->bna_lock held */
1898void
1899bnad_tx_coalescing_timeo_set(struct bnad *bnad)
1900{
1901 struct bnad_tx_info *tx_info;
1902
1903 tx_info = &bnad->tx_info[0];
1904 if (!tx_info->tx)
1905 return;
1906
1907 bna_tx_coalescing_timeo_set(tx_info->tx, bnad->tx_coalescing_timeo);
1908}
1909
1910/* Called with conf_lock & bnad->bna_lock held */
1911void
1912bnad_rx_coalescing_timeo_set(struct bnad *bnad)
1913{
1914 struct bnad_rx_info *rx_info;
1915 int i;
1916
1917 for (i = 0; i < bnad->num_rx; i++) {
1918 rx_info = &bnad->rx_info[i];
1919 if (!rx_info->rx)
1920 continue;
1921 bna_rx_coalescing_timeo_set(rx_info->rx,
1922 bnad->rx_coalescing_timeo);
1923 }
1924}
1925
1926/*
1927 * Called with bnad->bna_lock held
1928 */
1929static int
1930bnad_mac_addr_set_locked(struct bnad *bnad, u8 *mac_addr)
1931{
1932 int ret;
1933
1934 if (!is_valid_ether_addr(mac_addr))
1935 return -EADDRNOTAVAIL;
1936
1937 /* If datapath is down, pretend everything went through */
1938 if (!bnad->rx_info[0].rx)
1939 return 0;
1940
1941 ret = bna_rx_ucast_set(bnad->rx_info[0].rx, mac_addr, NULL);
1942 if (ret != BNA_CB_SUCCESS)
1943 return -EADDRNOTAVAIL;
1944
1945 return 0;
1946}
1947
1948/* Should be called with conf_lock held */
1949static int
1950bnad_enable_default_bcast(struct bnad *bnad)
1951{
1952 struct bnad_rx_info *rx_info = &bnad->rx_info[0];
1953 int ret;
1954 unsigned long flags;
1955
1956 init_completion(&bnad->bnad_completions.mcast_comp);
1957
1958 spin_lock_irqsave(&bnad->bna_lock, flags);
1959 ret = bna_rx_mcast_add(rx_info->rx, (u8 *)bnad_bcast_addr,
1960 bnad_cb_rx_mcast_add);
1961 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1962
1963 if (ret == BNA_CB_SUCCESS)
1964 wait_for_completion(&bnad->bnad_completions.mcast_comp);
1965 else
1966 return -ENODEV;
1967
1968 if (bnad->bnad_completions.mcast_comp_status != BNA_CB_SUCCESS)
1969 return -ENODEV;
1970
1971 return 0;
1972}
1973
1974/* Statistics utilities */
1975void
Eric Dumazet250e0612010-09-02 12:45:02 -07001976bnad_netdev_qstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001977{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001978 int i, j;
1979
1980 for (i = 0; i < bnad->num_rx; i++) {
1981 for (j = 0; j < bnad->num_rxp_per_rx; j++) {
1982 if (bnad->rx_info[i].rx_ctrl[j].ccb) {
Eric Dumazet250e0612010-09-02 12:45:02 -07001983 stats->rx_packets += bnad->rx_info[i].
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001984 rx_ctrl[j].ccb->rcb[0]->rxq->rx_packets;
Eric Dumazet250e0612010-09-02 12:45:02 -07001985 stats->rx_bytes += bnad->rx_info[i].
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001986 rx_ctrl[j].ccb->rcb[0]->rxq->rx_bytes;
1987 if (bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1] &&
1988 bnad->rx_info[i].rx_ctrl[j].ccb->
1989 rcb[1]->rxq) {
Eric Dumazet250e0612010-09-02 12:45:02 -07001990 stats->rx_packets +=
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001991 bnad->rx_info[i].rx_ctrl[j].
1992 ccb->rcb[1]->rxq->rx_packets;
Eric Dumazet250e0612010-09-02 12:45:02 -07001993 stats->rx_bytes +=
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001994 bnad->rx_info[i].rx_ctrl[j].
1995 ccb->rcb[1]->rxq->rx_bytes;
1996 }
1997 }
1998 }
1999 }
2000 for (i = 0; i < bnad->num_tx; i++) {
2001 for (j = 0; j < bnad->num_txq_per_tx; j++) {
2002 if (bnad->tx_info[i].tcb[j]) {
Eric Dumazet250e0612010-09-02 12:45:02 -07002003 stats->tx_packets +=
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002004 bnad->tx_info[i].tcb[j]->txq->tx_packets;
Eric Dumazet250e0612010-09-02 12:45:02 -07002005 stats->tx_bytes +=
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002006 bnad->tx_info[i].tcb[j]->txq->tx_bytes;
2007 }
2008 }
2009 }
2010}
2011
2012/*
2013 * Must be called with the bna_lock held.
2014 */
2015void
Eric Dumazet250e0612010-09-02 12:45:02 -07002016bnad_netdev_hwstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002017{
2018 struct bfi_ll_stats_mac *mac_stats;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002019 u64 bmap;
2020 int i;
2021
2022 mac_stats = &bnad->stats.bna_stats->hw_stats->mac_stats;
Eric Dumazet250e0612010-09-02 12:45:02 -07002023 stats->rx_errors =
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002024 mac_stats->rx_fcs_error + mac_stats->rx_alignment_error +
2025 mac_stats->rx_frame_length_error + mac_stats->rx_code_error +
2026 mac_stats->rx_undersize;
Eric Dumazet250e0612010-09-02 12:45:02 -07002027 stats->tx_errors = mac_stats->tx_fcs_error +
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002028 mac_stats->tx_undersize;
Eric Dumazet250e0612010-09-02 12:45:02 -07002029 stats->rx_dropped = mac_stats->rx_drop;
2030 stats->tx_dropped = mac_stats->tx_drop;
2031 stats->multicast = mac_stats->rx_multicast;
2032 stats->collisions = mac_stats->tx_total_collision;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002033
Eric Dumazet250e0612010-09-02 12:45:02 -07002034 stats->rx_length_errors = mac_stats->rx_frame_length_error;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002035
2036 /* receive ring buffer overflow ?? */
2037
Eric Dumazet250e0612010-09-02 12:45:02 -07002038 stats->rx_crc_errors = mac_stats->rx_fcs_error;
2039 stats->rx_frame_errors = mac_stats->rx_alignment_error;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002040 /* recv'r fifo overrun */
2041 bmap = (u64)bnad->stats.bna_stats->rxf_bmap[0] |
2042 ((u64)bnad->stats.bna_stats->rxf_bmap[1] << 32);
2043 for (i = 0; bmap && (i < BFI_LL_RXF_ID_MAX); i++) {
2044 if (bmap & 1) {
Eric Dumazet250e0612010-09-02 12:45:02 -07002045 stats->rx_fifo_errors +=
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002046 bnad->stats.bna_stats->
2047 hw_stats->rxf_stats[i].frame_drops;
2048 break;
2049 }
2050 bmap >>= 1;
2051 }
2052}
2053
2054static void
2055bnad_mbox_irq_sync(struct bnad *bnad)
2056{
2057 u32 irq;
2058 unsigned long flags;
2059
2060 spin_lock_irqsave(&bnad->bna_lock, flags);
2061 if (bnad->cfg_flags & BNAD_CF_MSIX)
2062 irq = bnad->msix_table[bnad->msix_num - 1].vector;
2063 else
2064 irq = bnad->pcidev->irq;
2065 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2066
2067 synchronize_irq(irq);
2068}
2069
2070/* Utility used by bnad_start_xmit, for doing TSO */
2071static int
2072bnad_tso_prepare(struct bnad *bnad, struct sk_buff *skb)
2073{
2074 int err;
2075
2076 /* SKB_GSO_TCPV4 and SKB_GSO_TCPV6 is defined since 2.6.18. */
2077 BUG_ON(!(skb_shinfo(skb)->gso_type == SKB_GSO_TCPV4 ||
2078 skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6));
2079 if (skb_header_cloned(skb)) {
2080 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2081 if (err) {
2082 BNAD_UPDATE_CTR(bnad, tso_err);
2083 return err;
2084 }
2085 }
2086
2087 /*
2088 * For TSO, the TCP checksum field is seeded with pseudo-header sum
2089 * excluding the length field.
2090 */
2091 if (skb->protocol == htons(ETH_P_IP)) {
2092 struct iphdr *iph = ip_hdr(skb);
2093
2094 /* Do we really need these? */
2095 iph->tot_len = 0;
2096 iph->check = 0;
2097
2098 tcp_hdr(skb)->check =
2099 ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
2100 IPPROTO_TCP, 0);
2101 BNAD_UPDATE_CTR(bnad, tso4);
2102 } else {
2103 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
2104
2105 BUG_ON(!(skb->protocol == htons(ETH_P_IPV6)));
2106 ipv6h->payload_len = 0;
2107 tcp_hdr(skb)->check =
2108 ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, 0,
2109 IPPROTO_TCP, 0);
2110 BNAD_UPDATE_CTR(bnad, tso6);
2111 }
2112
2113 return 0;
2114}
2115
2116/*
2117 * Initialize Q numbers depending on Rx Paths
2118 * Called with bnad->bna_lock held, because of cfg_flags
2119 * access.
2120 */
2121static void
2122bnad_q_num_init(struct bnad *bnad)
2123{
2124 int rxps;
2125
2126 rxps = min((uint)num_online_cpus(),
2127 (uint)(BNAD_MAX_RXS * BNAD_MAX_RXPS_PER_RX));
2128
2129 if (!(bnad->cfg_flags & BNAD_CF_MSIX))
2130 rxps = 1; /* INTx */
2131
2132 bnad->num_rx = 1;
2133 bnad->num_tx = 1;
2134 bnad->num_rxp_per_rx = rxps;
2135 bnad->num_txq_per_tx = BNAD_TXQ_NUM;
2136}
2137
2138/*
2139 * Adjusts the Q numbers, given a number of msix vectors
2140 * Give preference to RSS as opposed to Tx priority Queues,
2141 * in such a case, just use 1 Tx Q
2142 * Called with bnad->bna_lock held b'cos of cfg_flags access
2143 */
2144static void
2145bnad_q_num_adjust(struct bnad *bnad, int msix_vectors)
2146{
2147 bnad->num_txq_per_tx = 1;
2148 if ((msix_vectors >= (bnad->num_tx * bnad->num_txq_per_tx) +
2149 bnad_rxqs_per_cq + BNAD_MAILBOX_MSIX_VECTORS) &&
2150 (bnad->cfg_flags & BNAD_CF_MSIX)) {
2151 bnad->num_rxp_per_rx = msix_vectors -
2152 (bnad->num_tx * bnad->num_txq_per_tx) -
2153 BNAD_MAILBOX_MSIX_VECTORS;
2154 } else
2155 bnad->num_rxp_per_rx = 1;
2156}
2157
2158static void
2159bnad_set_netdev_perm_addr(struct bnad *bnad)
2160{
2161 struct net_device *netdev = bnad->netdev;
2162
2163 memcpy(netdev->perm_addr, &bnad->perm_addr, netdev->addr_len);
2164 if (is_zero_ether_addr(netdev->dev_addr))
2165 memcpy(netdev->dev_addr, &bnad->perm_addr, netdev->addr_len);
2166}
2167
2168/* Enable / disable device */
2169static void
2170bnad_device_disable(struct bnad *bnad)
2171{
2172 unsigned long flags;
2173
2174 init_completion(&bnad->bnad_completions.ioc_comp);
2175
2176 spin_lock_irqsave(&bnad->bna_lock, flags);
2177 bna_device_disable(&bnad->bna.device, BNA_HARD_CLEANUP);
2178 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2179
2180 wait_for_completion(&bnad->bnad_completions.ioc_comp);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002181}
2182
2183static int
2184bnad_device_enable(struct bnad *bnad)
2185{
2186 int err = 0;
2187 unsigned long flags;
2188
2189 init_completion(&bnad->bnad_completions.ioc_comp);
2190
2191 spin_lock_irqsave(&bnad->bna_lock, flags);
2192 bna_device_enable(&bnad->bna.device);
2193 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2194
2195 wait_for_completion(&bnad->bnad_completions.ioc_comp);
2196
2197 if (bnad->bnad_completions.ioc_comp_status)
2198 err = bnad->bnad_completions.ioc_comp_status;
2199
2200 return err;
2201}
2202
2203/* Free BNA resources */
2204static void
2205bnad_res_free(struct bnad *bnad)
2206{
2207 int i;
2208 struct bna_res_info *res_info = &bnad->res_info[0];
2209
2210 for (i = 0; i < BNA_RES_T_MAX; i++) {
2211 if (res_info[i].res_type == BNA_RES_T_MEM)
2212 bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
2213 else
2214 bnad_mbox_irq_free(bnad, &res_info[i].res_u.intr_info);
2215 }
2216}
2217
2218/* Allocates memory and interrupt resources for BNA */
2219static int
2220bnad_res_alloc(struct bnad *bnad)
2221{
2222 int i, err;
2223 struct bna_res_info *res_info = &bnad->res_info[0];
2224
2225 for (i = 0; i < BNA_RES_T_MAX; i++) {
2226 if (res_info[i].res_type == BNA_RES_T_MEM)
2227 err = bnad_mem_alloc(bnad, &res_info[i].res_u.mem_info);
2228 else
2229 err = bnad_mbox_irq_alloc(bnad,
2230 &res_info[i].res_u.intr_info);
2231 if (err)
2232 goto err_return;
2233 }
2234 return 0;
2235
2236err_return:
2237 bnad_res_free(bnad);
2238 return err;
2239}
2240
2241/* Interrupt enable / disable */
2242static void
2243bnad_enable_msix(struct bnad *bnad)
2244{
2245 int i, ret;
2246 u32 tot_msix_num;
2247 unsigned long flags;
2248
2249 spin_lock_irqsave(&bnad->bna_lock, flags);
2250 if (!(bnad->cfg_flags & BNAD_CF_MSIX)) {
2251 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2252 return;
2253 }
2254 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2255
2256 if (bnad->msix_table)
2257 return;
2258
2259 tot_msix_num = bnad->msix_num + bnad->msix_diag_num;
2260
2261 bnad->msix_table =
2262 kcalloc(tot_msix_num, sizeof(struct msix_entry), GFP_KERNEL);
2263
2264 if (!bnad->msix_table)
2265 goto intx_mode;
2266
2267 for (i = 0; i < tot_msix_num; i++)
2268 bnad->msix_table[i].entry = i;
2269
2270 ret = pci_enable_msix(bnad->pcidev, bnad->msix_table, tot_msix_num);
2271 if (ret > 0) {
2272 /* Not enough MSI-X vectors. */
2273
2274 spin_lock_irqsave(&bnad->bna_lock, flags);
2275 /* ret = #of vectors that we got */
2276 bnad_q_num_adjust(bnad, ret);
2277 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2278
2279 bnad->msix_num = (bnad->num_tx * bnad->num_txq_per_tx)
2280 + (bnad->num_rx
2281 * bnad->num_rxp_per_rx) +
2282 BNAD_MAILBOX_MSIX_VECTORS;
2283 tot_msix_num = bnad->msix_num + bnad->msix_diag_num;
2284
2285 /* Try once more with adjusted numbers */
2286 /* If this fails, fall back to INTx */
2287 ret = pci_enable_msix(bnad->pcidev, bnad->msix_table,
2288 tot_msix_num);
2289 if (ret)
2290 goto intx_mode;
2291
2292 } else if (ret < 0)
2293 goto intx_mode;
2294 return;
2295
2296intx_mode:
2297
2298 kfree(bnad->msix_table);
2299 bnad->msix_table = NULL;
2300 bnad->msix_num = 0;
2301 bnad->msix_diag_num = 0;
2302 spin_lock_irqsave(&bnad->bna_lock, flags);
2303 bnad->cfg_flags &= ~BNAD_CF_MSIX;
2304 bnad_q_num_init(bnad);
2305 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2306}
2307
2308static void
2309bnad_disable_msix(struct bnad *bnad)
2310{
2311 u32 cfg_flags;
2312 unsigned long flags;
2313
2314 spin_lock_irqsave(&bnad->bna_lock, flags);
2315 cfg_flags = bnad->cfg_flags;
2316 if (bnad->cfg_flags & BNAD_CF_MSIX)
2317 bnad->cfg_flags &= ~BNAD_CF_MSIX;
2318 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2319
2320 if (cfg_flags & BNAD_CF_MSIX) {
2321 pci_disable_msix(bnad->pcidev);
2322 kfree(bnad->msix_table);
2323 bnad->msix_table = NULL;
2324 }
2325}
2326
2327/* Netdev entry points */
2328static int
2329bnad_open(struct net_device *netdev)
2330{
2331 int err;
2332 struct bnad *bnad = netdev_priv(netdev);
2333 struct bna_pause_config pause_config;
2334 int mtu;
2335 unsigned long flags;
2336
2337 mutex_lock(&bnad->conf_mutex);
2338
2339 /* Tx */
2340 err = bnad_setup_tx(bnad, 0);
2341 if (err)
2342 goto err_return;
2343
2344 /* Rx */
2345 err = bnad_setup_rx(bnad, 0);
2346 if (err)
2347 goto cleanup_tx;
2348
2349 /* Port */
2350 pause_config.tx_pause = 0;
2351 pause_config.rx_pause = 0;
2352
2353 mtu = ETH_HLEN + bnad->netdev->mtu + ETH_FCS_LEN;
2354
2355 spin_lock_irqsave(&bnad->bna_lock, flags);
2356 bna_port_mtu_set(&bnad->bna.port, mtu, NULL);
2357 bna_port_pause_config(&bnad->bna.port, &pause_config, NULL);
2358 bna_port_enable(&bnad->bna.port);
2359 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2360
2361 /* Enable broadcast */
2362 bnad_enable_default_bcast(bnad);
2363
2364 /* Set the UCAST address */
2365 spin_lock_irqsave(&bnad->bna_lock, flags);
2366 bnad_mac_addr_set_locked(bnad, netdev->dev_addr);
2367 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2368
2369 /* Start the stats timer */
2370 bnad_stats_timer_start(bnad);
2371
2372 mutex_unlock(&bnad->conf_mutex);
2373
2374 return 0;
2375
2376cleanup_tx:
2377 bnad_cleanup_tx(bnad, 0);
2378
2379err_return:
2380 mutex_unlock(&bnad->conf_mutex);
2381 return err;
2382}
2383
2384static int
2385bnad_stop(struct net_device *netdev)
2386{
2387 struct bnad *bnad = netdev_priv(netdev);
2388 unsigned long flags;
2389
2390 mutex_lock(&bnad->conf_mutex);
2391
2392 /* Stop the stats timer */
2393 bnad_stats_timer_stop(bnad);
2394
2395 init_completion(&bnad->bnad_completions.port_comp);
2396
2397 spin_lock_irqsave(&bnad->bna_lock, flags);
2398 bna_port_disable(&bnad->bna.port, BNA_HARD_CLEANUP,
2399 bnad_cb_port_disabled);
2400 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2401
2402 wait_for_completion(&bnad->bnad_completions.port_comp);
2403
2404 bnad_cleanup_tx(bnad, 0);
2405 bnad_cleanup_rx(bnad, 0);
2406
2407 /* Synchronize mailbox IRQ */
2408 bnad_mbox_irq_sync(bnad);
2409
2410 mutex_unlock(&bnad->conf_mutex);
2411
2412 return 0;
2413}
2414
2415/* TX */
2416/*
2417 * bnad_start_xmit : Netdev entry point for Transmit
2418 * Called under lock held by net_device
2419 */
2420static netdev_tx_t
2421bnad_start_xmit(struct sk_buff *skb, struct net_device *netdev)
2422{
2423 struct bnad *bnad = netdev_priv(netdev);
2424
2425 u16 txq_prod, vlan_tag = 0;
2426 u32 unmap_prod, wis, wis_used, wi_range;
2427 u32 vectors, vect_id, i, acked;
2428 u32 tx_id;
2429 int err;
2430
2431 struct bnad_tx_info *tx_info;
2432 struct bna_tcb *tcb;
2433 struct bnad_unmap_q *unmap_q;
2434 dma_addr_t dma_addr;
2435 struct bna_txq_entry *txqent;
2436 bna_txq_wi_ctrl_flag_t flags;
2437
2438 if (unlikely
2439 (skb->len <= ETH_HLEN || skb->len > BFI_TX_MAX_DATA_PER_PKT)) {
2440 dev_kfree_skb(skb);
2441 return NETDEV_TX_OK;
2442 }
2443
2444 /*
2445 * Takes care of the Tx that is scheduled between clearing the flag
2446 * and the netif_stop_queue() call.
2447 */
2448 if (unlikely(!test_bit(BNAD_RF_TX_STARTED, &bnad->run_flags))) {
2449 dev_kfree_skb(skb);
2450 return NETDEV_TX_OK;
2451 }
2452
2453 tx_id = 0;
2454
2455 tx_info = &bnad->tx_info[tx_id];
2456 tcb = tx_info->tcb[tx_id];
2457 unmap_q = tcb->unmap_q;
2458
2459 vectors = 1 + skb_shinfo(skb)->nr_frags;
2460 if (vectors > BFI_TX_MAX_VECTORS_PER_PKT) {
2461 dev_kfree_skb(skb);
2462 return NETDEV_TX_OK;
2463 }
2464 wis = BNA_TXQ_WI_NEEDED(vectors); /* 4 vectors per work item */
2465 acked = 0;
2466 if (unlikely
2467 (wis > BNA_QE_FREE_CNT(tcb, tcb->q_depth) ||
2468 vectors > BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth))) {
2469 if ((u16) (*tcb->hw_consumer_index) !=
2470 tcb->consumer_index &&
2471 !test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags)) {
2472 acked = bnad_free_txbufs(bnad, tcb);
2473 bna_ib_ack(tcb->i_dbell, acked);
2474 smp_mb__before_clear_bit();
2475 clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
2476 } else {
2477 netif_stop_queue(netdev);
2478 BNAD_UPDATE_CTR(bnad, netif_queue_stop);
2479 }
2480
2481 smp_mb();
2482 /*
2483 * Check again to deal with race condition between
2484 * netif_stop_queue here, and netif_wake_queue in
2485 * interrupt handler which is not inside netif tx lock.
2486 */
2487 if (likely
2488 (wis > BNA_QE_FREE_CNT(tcb, tcb->q_depth) ||
2489 vectors > BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth))) {
2490 BNAD_UPDATE_CTR(bnad, netif_queue_stop);
2491 return NETDEV_TX_BUSY;
2492 } else {
2493 netif_wake_queue(netdev);
2494 BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
2495 }
2496 }
2497
2498 unmap_prod = unmap_q->producer_index;
2499 wis_used = 1;
2500 vect_id = 0;
2501 flags = 0;
2502
2503 txq_prod = tcb->producer_index;
2504 BNA_TXQ_QPGE_PTR_GET(txq_prod, tcb->sw_qpt, txqent, wi_range);
2505 BUG_ON(!(wi_range <= tcb->q_depth));
2506 txqent->hdr.wi.reserved = 0;
2507 txqent->hdr.wi.num_vectors = vectors;
2508 txqent->hdr.wi.opcode =
2509 htons((skb_is_gso(skb) ? BNA_TXQ_WI_SEND_LSO :
2510 BNA_TXQ_WI_SEND));
2511
2512 if (bnad->vlan_grp && vlan_tx_tag_present(skb)) {
2513 vlan_tag = (u16) vlan_tx_tag_get(skb);
2514 flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
2515 }
2516 if (test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags)) {
2517 vlan_tag =
2518 (tcb->priority & 0x7) << 13 | (vlan_tag & 0x1fff);
2519 flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
2520 }
2521
2522 txqent->hdr.wi.vlan_tag = htons(vlan_tag);
2523
2524 if (skb_is_gso(skb)) {
2525 err = bnad_tso_prepare(bnad, skb);
2526 if (err) {
2527 dev_kfree_skb(skb);
2528 return NETDEV_TX_OK;
2529 }
2530 txqent->hdr.wi.lso_mss = htons(skb_is_gso(skb));
2531 flags |= (BNA_TXQ_WI_CF_IP_CKSUM | BNA_TXQ_WI_CF_TCP_CKSUM);
2532 txqent->hdr.wi.l4_hdr_size_n_offset =
2533 htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
2534 (tcp_hdrlen(skb) >> 2,
2535 skb_transport_offset(skb)));
2536 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2537 u8 proto = 0;
2538
2539 txqent->hdr.wi.lso_mss = 0;
2540
2541 if (skb->protocol == htons(ETH_P_IP))
2542 proto = ip_hdr(skb)->protocol;
2543 else if (skb->protocol == htons(ETH_P_IPV6)) {
2544 /* nexthdr may not be TCP immediately. */
2545 proto = ipv6_hdr(skb)->nexthdr;
2546 }
2547 if (proto == IPPROTO_TCP) {
2548 flags |= BNA_TXQ_WI_CF_TCP_CKSUM;
2549 txqent->hdr.wi.l4_hdr_size_n_offset =
2550 htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
2551 (0, skb_transport_offset(skb)));
2552
2553 BNAD_UPDATE_CTR(bnad, tcpcsum_offload);
2554
2555 BUG_ON(!(skb_headlen(skb) >=
2556 skb_transport_offset(skb) + tcp_hdrlen(skb)));
2557
2558 } else if (proto == IPPROTO_UDP) {
2559 flags |= BNA_TXQ_WI_CF_UDP_CKSUM;
2560 txqent->hdr.wi.l4_hdr_size_n_offset =
2561 htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
2562 (0, skb_transport_offset(skb)));
2563
2564 BNAD_UPDATE_CTR(bnad, udpcsum_offload);
2565
2566 BUG_ON(!(skb_headlen(skb) >=
2567 skb_transport_offset(skb) +
2568 sizeof(struct udphdr)));
2569 } else {
2570 err = skb_checksum_help(skb);
2571 BNAD_UPDATE_CTR(bnad, csum_help);
2572 if (err) {
2573 dev_kfree_skb(skb);
2574 BNAD_UPDATE_CTR(bnad, csum_help_err);
2575 return NETDEV_TX_OK;
2576 }
2577 }
2578 } else {
2579 txqent->hdr.wi.lso_mss = 0;
2580 txqent->hdr.wi.l4_hdr_size_n_offset = 0;
2581 }
2582
2583 txqent->hdr.wi.flags = htons(flags);
2584
2585 txqent->hdr.wi.frame_length = htonl(skb->len);
2586
2587 unmap_q->unmap_array[unmap_prod].skb = skb;
2588 BUG_ON(!(skb_headlen(skb) <= BFI_TX_MAX_DATA_PER_VECTOR));
2589 txqent->vector[vect_id].length = htons(skb_headlen(skb));
2590 dma_addr = pci_map_single(bnad->pcidev, skb->data, skb_headlen(skb),
2591 PCI_DMA_TODEVICE);
2592 pci_unmap_addr_set(&unmap_q->unmap_array[unmap_prod], dma_addr,
2593 dma_addr);
2594
2595 BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[vect_id].host_addr);
2596 BNA_QE_INDX_ADD(unmap_prod, 1, unmap_q->q_depth);
2597
2598 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2599 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
2600 u32 size = frag->size;
2601
2602 if (++vect_id == BFI_TX_MAX_VECTORS_PER_WI) {
2603 vect_id = 0;
2604 if (--wi_range)
2605 txqent++;
2606 else {
2607 BNA_QE_INDX_ADD(txq_prod, wis_used,
2608 tcb->q_depth);
2609 wis_used = 0;
2610 BNA_TXQ_QPGE_PTR_GET(txq_prod, tcb->sw_qpt,
2611 txqent, wi_range);
2612 BUG_ON(!(wi_range <= tcb->q_depth));
2613 }
2614 wis_used++;
2615 txqent->hdr.wi_ext.opcode = htons(BNA_TXQ_WI_EXTENSION);
2616 }
2617
2618 BUG_ON(!(size <= BFI_TX_MAX_DATA_PER_VECTOR));
2619 txqent->vector[vect_id].length = htons(size);
2620 dma_addr =
2621 pci_map_page(bnad->pcidev, frag->page,
2622 frag->page_offset, size,
2623 PCI_DMA_TODEVICE);
2624 pci_unmap_addr_set(&unmap_q->unmap_array[unmap_prod], dma_addr,
2625 dma_addr);
2626 BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[vect_id].host_addr);
2627 BNA_QE_INDX_ADD(unmap_prod, 1, unmap_q->q_depth);
2628 }
2629
2630 unmap_q->producer_index = unmap_prod;
2631 BNA_QE_INDX_ADD(txq_prod, wis_used, tcb->q_depth);
2632 tcb->producer_index = txq_prod;
2633
2634 smp_mb();
2635 bna_txq_prod_indx_doorbell(tcb);
2636
2637 if ((u16) (*tcb->hw_consumer_index) != tcb->consumer_index)
2638 tasklet_schedule(&bnad->tx_free_tasklet);
2639
2640 return NETDEV_TX_OK;
2641}
2642
2643/*
2644 * Used spin_lock to synchronize reading of stats structures, which
2645 * is written by BNA under the same lock.
2646 */
Eric Dumazet250e0612010-09-02 12:45:02 -07002647static struct rtnl_link_stats64 *
2648bnad_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002649{
2650 struct bnad *bnad = netdev_priv(netdev);
2651 unsigned long flags;
2652
2653 spin_lock_irqsave(&bnad->bna_lock, flags);
2654
Eric Dumazet250e0612010-09-02 12:45:02 -07002655 bnad_netdev_qstats_fill(bnad, stats);
2656 bnad_netdev_hwstats_fill(bnad, stats);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002657
2658 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2659
Eric Dumazet250e0612010-09-02 12:45:02 -07002660 return stats;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002661}
2662
2663static void
2664bnad_set_rx_mode(struct net_device *netdev)
2665{
2666 struct bnad *bnad = netdev_priv(netdev);
2667 u32 new_mask, valid_mask;
2668 unsigned long flags;
2669
2670 spin_lock_irqsave(&bnad->bna_lock, flags);
2671
2672 new_mask = valid_mask = 0;
2673
2674 if (netdev->flags & IFF_PROMISC) {
2675 if (!(bnad->cfg_flags & BNAD_CF_PROMISC)) {
2676 new_mask = BNAD_RXMODE_PROMISC_DEFAULT;
2677 valid_mask = BNAD_RXMODE_PROMISC_DEFAULT;
2678 bnad->cfg_flags |= BNAD_CF_PROMISC;
2679 }
2680 } else {
2681 if (bnad->cfg_flags & BNAD_CF_PROMISC) {
2682 new_mask = ~BNAD_RXMODE_PROMISC_DEFAULT;
2683 valid_mask = BNAD_RXMODE_PROMISC_DEFAULT;
2684 bnad->cfg_flags &= ~BNAD_CF_PROMISC;
2685 }
2686 }
2687
2688 if (netdev->flags & IFF_ALLMULTI) {
2689 if (!(bnad->cfg_flags & BNAD_CF_ALLMULTI)) {
2690 new_mask |= BNA_RXMODE_ALLMULTI;
2691 valid_mask |= BNA_RXMODE_ALLMULTI;
2692 bnad->cfg_flags |= BNAD_CF_ALLMULTI;
2693 }
2694 } else {
2695 if (bnad->cfg_flags & BNAD_CF_ALLMULTI) {
2696 new_mask &= ~BNA_RXMODE_ALLMULTI;
2697 valid_mask |= BNA_RXMODE_ALLMULTI;
2698 bnad->cfg_flags &= ~BNAD_CF_ALLMULTI;
2699 }
2700 }
2701
2702 bna_rx_mode_set(bnad->rx_info[0].rx, new_mask, valid_mask, NULL);
2703
2704 if (!netdev_mc_empty(netdev)) {
2705 u8 *mcaddr_list;
2706 int mc_count = netdev_mc_count(netdev);
2707
2708 /* Index 0 holds the broadcast address */
2709 mcaddr_list =
2710 kzalloc((mc_count + 1) * ETH_ALEN,
2711 GFP_ATOMIC);
2712 if (!mcaddr_list)
Jiri Slabyca1cef32010-09-04 02:08:41 +00002713 goto unlock;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002714
2715 memcpy(&mcaddr_list[0], &bnad_bcast_addr[0], ETH_ALEN);
2716
2717 /* Copy rest of the MC addresses */
2718 bnad_netdev_mc_list_get(netdev, mcaddr_list);
2719
2720 bna_rx_mcast_listset(bnad->rx_info[0].rx, mc_count + 1,
2721 mcaddr_list, NULL);
2722
2723 /* Should we enable BNAD_CF_ALLMULTI for err != 0 ? */
2724 kfree(mcaddr_list);
2725 }
Jiri Slabyca1cef32010-09-04 02:08:41 +00002726unlock:
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002727 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2728}
2729
2730/*
2731 * bna_lock is used to sync writes to netdev->addr
2732 * conf_lock cannot be used since this call may be made
2733 * in a non-blocking context.
2734 */
2735static int
2736bnad_set_mac_address(struct net_device *netdev, void *mac_addr)
2737{
2738 int err;
2739 struct bnad *bnad = netdev_priv(netdev);
2740 struct sockaddr *sa = (struct sockaddr *)mac_addr;
2741 unsigned long flags;
2742
2743 spin_lock_irqsave(&bnad->bna_lock, flags);
2744
2745 err = bnad_mac_addr_set_locked(bnad, sa->sa_data);
2746
2747 if (!err)
2748 memcpy(netdev->dev_addr, sa->sa_data, netdev->addr_len);
2749
2750 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2751
2752 return err;
2753}
2754
2755static int
2756bnad_change_mtu(struct net_device *netdev, int new_mtu)
2757{
2758 int mtu, err = 0;
2759 unsigned long flags;
2760
2761 struct bnad *bnad = netdev_priv(netdev);
2762
2763 if (new_mtu + ETH_HLEN < ETH_ZLEN || new_mtu > BNAD_JUMBO_MTU)
2764 return -EINVAL;
2765
2766 mutex_lock(&bnad->conf_mutex);
2767
2768 netdev->mtu = new_mtu;
2769
2770 mtu = ETH_HLEN + new_mtu + ETH_FCS_LEN;
2771
2772 spin_lock_irqsave(&bnad->bna_lock, flags);
2773 bna_port_mtu_set(&bnad->bna.port, mtu, NULL);
2774 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2775
2776 mutex_unlock(&bnad->conf_mutex);
2777 return err;
2778}
2779
2780static void
2781bnad_vlan_rx_register(struct net_device *netdev,
2782 struct vlan_group *vlan_grp)
2783{
2784 struct bnad *bnad = netdev_priv(netdev);
2785
2786 mutex_lock(&bnad->conf_mutex);
2787 bnad->vlan_grp = vlan_grp;
2788 mutex_unlock(&bnad->conf_mutex);
2789}
2790
2791static void
2792bnad_vlan_rx_add_vid(struct net_device *netdev,
2793 unsigned short vid)
2794{
2795 struct bnad *bnad = netdev_priv(netdev);
2796 unsigned long flags;
2797
2798 if (!bnad->rx_info[0].rx)
2799 return;
2800
2801 mutex_lock(&bnad->conf_mutex);
2802
2803 spin_lock_irqsave(&bnad->bna_lock, flags);
2804 bna_rx_vlan_add(bnad->rx_info[0].rx, vid);
2805 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2806
2807 mutex_unlock(&bnad->conf_mutex);
2808}
2809
2810static void
2811bnad_vlan_rx_kill_vid(struct net_device *netdev,
2812 unsigned short vid)
2813{
2814 struct bnad *bnad = netdev_priv(netdev);
2815 unsigned long flags;
2816
2817 if (!bnad->rx_info[0].rx)
2818 return;
2819
2820 mutex_lock(&bnad->conf_mutex);
2821
2822 spin_lock_irqsave(&bnad->bna_lock, flags);
2823 bna_rx_vlan_del(bnad->rx_info[0].rx, vid);
2824 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2825
2826 mutex_unlock(&bnad->conf_mutex);
2827}
2828
2829#ifdef CONFIG_NET_POLL_CONTROLLER
2830static void
2831bnad_netpoll(struct net_device *netdev)
2832{
2833 struct bnad *bnad = netdev_priv(netdev);
2834 struct bnad_rx_info *rx_info;
2835 struct bnad_rx_ctrl *rx_ctrl;
2836 u32 curr_mask;
2837 int i, j;
2838
2839 if (!(bnad->cfg_flags & BNAD_CF_MSIX)) {
2840 bna_intx_disable(&bnad->bna, curr_mask);
2841 bnad_isr(bnad->pcidev->irq, netdev);
2842 bna_intx_enable(&bnad->bna, curr_mask);
2843 } else {
2844 for (i = 0; i < bnad->num_rx; i++) {
2845 rx_info = &bnad->rx_info[i];
2846 if (!rx_info->rx)
2847 continue;
2848 for (j = 0; j < bnad->num_rxp_per_rx; j++) {
2849 rx_ctrl = &rx_info->rx_ctrl[j];
2850 if (rx_ctrl->ccb) {
2851 bnad_disable_rx_irq(bnad,
2852 rx_ctrl->ccb);
2853 bnad_netif_rx_schedule_poll(bnad,
2854 rx_ctrl->ccb);
2855 }
2856 }
2857 }
2858 }
2859}
2860#endif
2861
2862static const struct net_device_ops bnad_netdev_ops = {
2863 .ndo_open = bnad_open,
2864 .ndo_stop = bnad_stop,
2865 .ndo_start_xmit = bnad_start_xmit,
Eric Dumazet250e0612010-09-02 12:45:02 -07002866 .ndo_get_stats64 = bnad_get_stats64,
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002867 .ndo_set_rx_mode = bnad_set_rx_mode,
2868 .ndo_set_multicast_list = bnad_set_rx_mode,
2869 .ndo_validate_addr = eth_validate_addr,
2870 .ndo_set_mac_address = bnad_set_mac_address,
2871 .ndo_change_mtu = bnad_change_mtu,
2872 .ndo_vlan_rx_register = bnad_vlan_rx_register,
2873 .ndo_vlan_rx_add_vid = bnad_vlan_rx_add_vid,
2874 .ndo_vlan_rx_kill_vid = bnad_vlan_rx_kill_vid,
2875#ifdef CONFIG_NET_POLL_CONTROLLER
2876 .ndo_poll_controller = bnad_netpoll
2877#endif
2878};
2879
2880static void
2881bnad_netdev_init(struct bnad *bnad, bool using_dac)
2882{
2883 struct net_device *netdev = bnad->netdev;
2884
2885 netdev->features |= NETIF_F_IPV6_CSUM;
2886 netdev->features |= NETIF_F_TSO;
2887 netdev->features |= NETIF_F_TSO6;
2888
2889 netdev->features |= NETIF_F_GRO;
2890 pr_warn("bna: GRO enabled, using kernel stack GRO\n");
2891
2892 netdev->features |= NETIF_F_SG | NETIF_F_IP_CSUM;
2893
2894 if (using_dac)
2895 netdev->features |= NETIF_F_HIGHDMA;
2896
2897 netdev->features |=
2898 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
2899 NETIF_F_HW_VLAN_FILTER;
2900
2901 netdev->vlan_features = netdev->features;
2902 netdev->mem_start = bnad->mmio_start;
2903 netdev->mem_end = bnad->mmio_start + bnad->mmio_len - 1;
2904
2905 netdev->netdev_ops = &bnad_netdev_ops;
2906 bnad_set_ethtool_ops(netdev);
2907}
2908
2909/*
2910 * 1. Initialize the bnad structure
2911 * 2. Setup netdev pointer in pci_dev
2912 * 3. Initialze Tx free tasklet
2913 * 4. Initialize no. of TxQ & CQs & MSIX vectors
2914 */
2915static int
2916bnad_init(struct bnad *bnad,
2917 struct pci_dev *pdev, struct net_device *netdev)
2918{
2919 unsigned long flags;
2920
2921 SET_NETDEV_DEV(netdev, &pdev->dev);
2922 pci_set_drvdata(pdev, netdev);
2923
2924 bnad->netdev = netdev;
2925 bnad->pcidev = pdev;
2926 bnad->mmio_start = pci_resource_start(pdev, 0);
2927 bnad->mmio_len = pci_resource_len(pdev, 0);
2928 bnad->bar0 = ioremap_nocache(bnad->mmio_start, bnad->mmio_len);
2929 if (!bnad->bar0) {
2930 dev_err(&pdev->dev, "ioremap for bar0 failed\n");
2931 pci_set_drvdata(pdev, NULL);
2932 return -ENOMEM;
2933 }
2934 pr_info("bar0 mapped to %p, len %llu\n", bnad->bar0,
2935 (unsigned long long) bnad->mmio_len);
2936
2937 spin_lock_irqsave(&bnad->bna_lock, flags);
2938 if (!bnad_msix_disable)
2939 bnad->cfg_flags = BNAD_CF_MSIX;
2940
2941 bnad->cfg_flags |= BNAD_CF_DIM_ENABLED;
2942
2943 bnad_q_num_init(bnad);
2944 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2945
2946 bnad->msix_num = (bnad->num_tx * bnad->num_txq_per_tx) +
2947 (bnad->num_rx * bnad->num_rxp_per_rx) +
2948 BNAD_MAILBOX_MSIX_VECTORS;
2949 bnad->msix_diag_num = 2; /* 1 for Tx, 1 for Rx */
2950
2951 bnad->txq_depth = BNAD_TXQ_DEPTH;
2952 bnad->rxq_depth = BNAD_RXQ_DEPTH;
2953 bnad->rx_csum = true;
2954
2955 bnad->tx_coalescing_timeo = BFI_TX_COALESCING_TIMEO;
2956 bnad->rx_coalescing_timeo = BFI_RX_COALESCING_TIMEO;
2957
2958 tasklet_init(&bnad->tx_free_tasklet, bnad_tx_free_tasklet,
2959 (unsigned long)bnad);
2960
2961 return 0;
2962}
2963
2964/*
2965 * Must be called after bnad_pci_uninit()
2966 * so that iounmap() and pci_set_drvdata(NULL)
2967 * happens only after PCI uninitialization.
2968 */
2969static void
2970bnad_uninit(struct bnad *bnad)
2971{
2972 if (bnad->bar0)
2973 iounmap(bnad->bar0);
2974 pci_set_drvdata(bnad->pcidev, NULL);
2975}
2976
2977/*
2978 * Initialize locks
2979 a) Per device mutes used for serializing configuration
2980 changes from OS interface
2981 b) spin lock used to protect bna state machine
2982 */
2983static void
2984bnad_lock_init(struct bnad *bnad)
2985{
2986 spin_lock_init(&bnad->bna_lock);
2987 mutex_init(&bnad->conf_mutex);
2988}
2989
2990static void
2991bnad_lock_uninit(struct bnad *bnad)
2992{
2993 mutex_destroy(&bnad->conf_mutex);
2994}
2995
2996/* PCI Initialization */
2997static int
2998bnad_pci_init(struct bnad *bnad,
2999 struct pci_dev *pdev, bool *using_dac)
3000{
3001 int err;
3002
3003 err = pci_enable_device(pdev);
3004 if (err)
3005 return err;
3006 err = pci_request_regions(pdev, BNAD_NAME);
3007 if (err)
3008 goto disable_device;
3009 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
3010 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
3011 *using_dac = 1;
3012 } else {
3013 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3014 if (err) {
3015 err = pci_set_consistent_dma_mask(pdev,
3016 DMA_BIT_MASK(32));
3017 if (err)
3018 goto release_regions;
3019 }
3020 *using_dac = 0;
3021 }
3022 pci_set_master(pdev);
3023 return 0;
3024
3025release_regions:
3026 pci_release_regions(pdev);
3027disable_device:
3028 pci_disable_device(pdev);
3029
3030 return err;
3031}
3032
3033static void
3034bnad_pci_uninit(struct pci_dev *pdev)
3035{
3036 pci_release_regions(pdev);
3037 pci_disable_device(pdev);
3038}
3039
3040static int __devinit
3041bnad_pci_probe(struct pci_dev *pdev,
3042 const struct pci_device_id *pcidev_id)
3043{
3044 bool using_dac;
3045 int err;
3046 struct bnad *bnad;
3047 struct bna *bna;
3048 struct net_device *netdev;
3049 struct bfa_pcidev pcidev_info;
3050 unsigned long flags;
3051
3052 pr_info("bnad_pci_probe : (0x%p, 0x%p) PCI Func : (%d)\n",
3053 pdev, pcidev_id, PCI_FUNC(pdev->devfn));
3054
3055 mutex_lock(&bnad_fwimg_mutex);
3056 if (!cna_get_firmware_buf(pdev)) {
3057 mutex_unlock(&bnad_fwimg_mutex);
3058 pr_warn("Failed to load Firmware Image!\n");
3059 return -ENODEV;
3060 }
3061 mutex_unlock(&bnad_fwimg_mutex);
3062
3063 /*
3064 * Allocates sizeof(struct net_device + struct bnad)
3065 * bnad = netdev->priv
3066 */
3067 netdev = alloc_etherdev(sizeof(struct bnad));
3068 if (!netdev) {
3069 dev_err(&pdev->dev, "alloc_etherdev failed\n");
3070 err = -ENOMEM;
3071 return err;
3072 }
3073 bnad = netdev_priv(netdev);
3074
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003075 /*
3076 * PCI initialization
3077 * Output : using_dac = 1 for 64 bit DMA
3078 * = 0 for 32 bit DMA
3079 */
3080 err = bnad_pci_init(bnad, pdev, &using_dac);
3081 if (err)
3082 goto free_netdev;
3083
3084 bnad_lock_init(bnad);
3085 /*
3086 * Initialize bnad structure
3087 * Setup relation between pci_dev & netdev
3088 * Init Tx free tasklet
3089 */
3090 err = bnad_init(bnad, pdev, netdev);
3091 if (err)
3092 goto pci_uninit;
3093 /* Initialize netdev structure, set up ethtool ops */
3094 bnad_netdev_init(bnad, using_dac);
3095
3096 bnad_enable_msix(bnad);
3097
3098 /* Get resource requirement form bna */
3099 bna_res_req(&bnad->res_info[0]);
3100
3101 /* Allocate resources from bna */
3102 err = bnad_res_alloc(bnad);
3103 if (err)
3104 goto free_netdev;
3105
3106 bna = &bnad->bna;
3107
3108 /* Setup pcidev_info for bna_init() */
3109 pcidev_info.pci_slot = PCI_SLOT(bnad->pcidev->devfn);
3110 pcidev_info.pci_func = PCI_FUNC(bnad->pcidev->devfn);
3111 pcidev_info.device_id = bnad->pcidev->device;
3112 pcidev_info.pci_bar_kva = bnad->bar0;
3113
3114 mutex_lock(&bnad->conf_mutex);
3115
3116 spin_lock_irqsave(&bnad->bna_lock, flags);
3117 bna_init(bna, bnad, &pcidev_info, &bnad->res_info[0]);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003118 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3119
3120 bnad->stats.bna_stats = &bna->stats;
3121
3122 /* Set up timers */
3123 setup_timer(&bnad->bna.device.ioc.ioc_timer, bnad_ioc_timeout,
3124 ((unsigned long)bnad));
3125 setup_timer(&bnad->bna.device.ioc.hb_timer, bnad_ioc_hb_check,
3126 ((unsigned long)bnad));
3127 setup_timer(&bnad->bna.device.ioc.sem_timer, bnad_ioc_sem_timeout,
3128 ((unsigned long)bnad));
3129
3130 /* Now start the timer before calling IOC */
3131 mod_timer(&bnad->bna.device.ioc.ioc_timer,
3132 jiffies + msecs_to_jiffies(BNA_IOC_TIMER_FREQ));
3133
3134 /*
3135 * Start the chip
3136 * Don't care even if err != 0, bna state machine will
3137 * deal with it
3138 */
3139 err = bnad_device_enable(bnad);
3140
3141 /* Get the burnt-in mac */
3142 spin_lock_irqsave(&bnad->bna_lock, flags);
3143 bna_port_mac_get(&bna->port, &bnad->perm_addr);
3144 bnad_set_netdev_perm_addr(bnad);
3145 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3146
3147 mutex_unlock(&bnad->conf_mutex);
3148
3149 /*
3150 * Make sure the link appears down to the stack
3151 */
3152 netif_carrier_off(netdev);
3153
3154 /* Finally, reguister with net_device layer */
3155 err = register_netdev(netdev);
3156 if (err) {
3157 pr_err("BNA : Registering with netdev failed\n");
3158 goto disable_device;
3159 }
3160
3161 return 0;
3162
3163disable_device:
3164 mutex_lock(&bnad->conf_mutex);
3165 bnad_device_disable(bnad);
3166 del_timer_sync(&bnad->bna.device.ioc.ioc_timer);
3167 del_timer_sync(&bnad->bna.device.ioc.sem_timer);
3168 del_timer_sync(&bnad->bna.device.ioc.hb_timer);
3169 spin_lock_irqsave(&bnad->bna_lock, flags);
3170 bna_uninit(bna);
3171 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3172 mutex_unlock(&bnad->conf_mutex);
3173
3174 bnad_res_free(bnad);
3175 bnad_disable_msix(bnad);
3176pci_uninit:
3177 bnad_pci_uninit(pdev);
3178 bnad_lock_uninit(bnad);
3179 bnad_uninit(bnad);
3180free_netdev:
3181 free_netdev(netdev);
3182 return err;
3183}
3184
3185static void __devexit
3186bnad_pci_remove(struct pci_dev *pdev)
3187{
3188 struct net_device *netdev = pci_get_drvdata(pdev);
3189 struct bnad *bnad;
3190 struct bna *bna;
3191 unsigned long flags;
3192
3193 if (!netdev)
3194 return;
3195
3196 pr_info("%s bnad_pci_remove\n", netdev->name);
3197 bnad = netdev_priv(netdev);
3198 bna = &bnad->bna;
3199
3200 unregister_netdev(netdev);
3201
3202 mutex_lock(&bnad->conf_mutex);
3203 bnad_device_disable(bnad);
3204 del_timer_sync(&bnad->bna.device.ioc.ioc_timer);
3205 del_timer_sync(&bnad->bna.device.ioc.sem_timer);
3206 del_timer_sync(&bnad->bna.device.ioc.hb_timer);
3207 spin_lock_irqsave(&bnad->bna_lock, flags);
3208 bna_uninit(bna);
3209 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3210 mutex_unlock(&bnad->conf_mutex);
3211
3212 bnad_res_free(bnad);
3213 bnad_disable_msix(bnad);
3214 bnad_pci_uninit(pdev);
3215 bnad_lock_uninit(bnad);
3216 bnad_uninit(bnad);
3217 free_netdev(netdev);
3218}
3219
3220const struct pci_device_id bnad_pci_id_table[] = {
3221 {
3222 PCI_DEVICE(PCI_VENDOR_ID_BROCADE,
3223 PCI_DEVICE_ID_BROCADE_CT),
3224 .class = PCI_CLASS_NETWORK_ETHERNET << 8,
3225 .class_mask = 0xffff00
3226 }, {0, }
3227};
3228
3229MODULE_DEVICE_TABLE(pci, bnad_pci_id_table);
3230
3231static struct pci_driver bnad_pci_driver = {
3232 .name = BNAD_NAME,
3233 .id_table = bnad_pci_id_table,
3234 .probe = bnad_pci_probe,
3235 .remove = __devexit_p(bnad_pci_remove),
3236};
3237
3238static int __init
3239bnad_module_init(void)
3240{
3241 int err;
3242
3243 pr_info("Brocade 10G Ethernet driver\n");
3244
Rasesh Mody8a891422010-08-25 23:00:27 -07003245 bfa_nw_ioc_auto_recover(bnad_ioc_auto_recover);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003246
3247 err = pci_register_driver(&bnad_pci_driver);
3248 if (err < 0) {
3249 pr_err("bna : PCI registration failed in module init "
3250 "(%d)\n", err);
3251 return err;
3252 }
3253
3254 return 0;
3255}
3256
3257static void __exit
3258bnad_module_exit(void)
3259{
3260 pci_unregister_driver(&bnad_pci_driver);
3261
3262 if (bfi_fw)
3263 release_firmware(bfi_fw);
3264}
3265
3266module_init(bnad_module_init);
3267module_exit(bnad_module_exit);
3268
3269MODULE_AUTHOR("Brocade");
3270MODULE_LICENSE("GPL");
3271MODULE_DESCRIPTION("Brocade 10G PCIe Ethernet driver");
3272MODULE_VERSION(BNAD_VERSION);
3273MODULE_FIRMWARE(CNA_FW_FILE_CT);