2 * Linux network driver for Brocade Converged Network Adapter.
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
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.
14 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
18 #include <linux/bitops.h>
19 #include <linux/netdevice.h>
20 #include <linux/skbuff.h>
21 #include <linux/etherdevice.h>
23 #include <linux/ethtool.h>
24 #include <linux/if_vlan.h>
25 #include <linux/if_ether.h>
27 #include <linux/prefetch.h>
28 #include <linux/module.h>
34 static DEFINE_MUTEX(bnad_fwimg_mutex);
39 static uint bnad_msix_disable;
40 module_param(bnad_msix_disable, uint, 0444);
41 MODULE_PARM_DESC(bnad_msix_disable, "Disable MSIX mode");
43 static uint bnad_ioc_auto_recover = 1;
44 module_param(bnad_ioc_auto_recover, uint, 0444);
45 MODULE_PARM_DESC(bnad_ioc_auto_recover, "Enable / Disable auto recovery");
47 static uint bna_debugfs_enable = 1;
48 module_param(bna_debugfs_enable, uint, S_IRUGO | S_IWUSR);
49 MODULE_PARM_DESC(bna_debugfs_enable, "Enables debugfs feature, default=1,"
50 " Range[false:0|true:1]");
55 u32 bnad_rxqs_per_cq = 2;
57 static struct mutex bnad_list_mutex;
58 static LIST_HEAD(bnad_list);
59 static const u8 bnad_bcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
64 #define BNAD_TX_UNMAPQ_DEPTH (bnad->txq_depth * 2)
66 #define BNAD_RX_UNMAPQ_DEPTH (bnad->rxq_depth)
68 #define BNAD_GET_MBOX_IRQ(_bnad) \
69 (((_bnad)->cfg_flags & BNAD_CF_MSIX) ? \
70 ((_bnad)->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector) : \
71 ((_bnad)->pcidev->irq))
73 #define BNAD_FILL_UNMAPQ_MEM_REQ(_res_info, _num, _depth) \
75 (_res_info)->res_type = BNA_RES_T_MEM; \
76 (_res_info)->res_u.mem_info.mem_type = BNA_MEM_T_KVA; \
77 (_res_info)->res_u.mem_info.num = (_num); \
78 (_res_info)->res_u.mem_info.len = \
79 sizeof(struct bnad_unmap_q) + \
80 (sizeof(struct bnad_skb_unmap) * ((_depth) - 1)); \
84 bnad_add_to_list(struct bnad *bnad)
86 mutex_lock(&bnad_list_mutex);
87 list_add_tail(&bnad->list_entry, &bnad_list);
89 mutex_unlock(&bnad_list_mutex);
93 bnad_remove_from_list(struct bnad *bnad)
95 mutex_lock(&bnad_list_mutex);
96 list_del(&bnad->list_entry);
97 mutex_unlock(&bnad_list_mutex);
101 * Reinitialize completions in CQ, once Rx is taken down
104 bnad_cq_cleanup(struct bnad *bnad, struct bna_ccb *ccb)
106 struct bna_cq_entry *cmpl, *next_cmpl;
107 unsigned int wi_range, wis = 0, ccb_prod = 0;
110 BNA_CQ_QPGE_PTR_GET(ccb_prod, ccb->sw_qpt, cmpl,
113 for (i = 0; i < ccb->q_depth; i++) {
115 if (likely(--wi_range))
116 next_cmpl = cmpl + 1;
118 BNA_QE_INDX_ADD(ccb_prod, wis, ccb->q_depth);
120 BNA_CQ_QPGE_PTR_GET(ccb_prod, ccb->sw_qpt,
121 next_cmpl, wi_range);
129 bnad_pci_unmap_skb(struct device *pdev, struct bnad_skb_unmap *array,
130 u32 index, u32 depth, struct sk_buff *skb, u32 frag)
133 array[index].skb = NULL;
135 dma_unmap_single(pdev, dma_unmap_addr(&array[index], dma_addr),
136 skb_headlen(skb), DMA_TO_DEVICE);
137 dma_unmap_addr_set(&array[index], dma_addr, 0);
138 BNA_QE_INDX_ADD(index, 1, depth);
140 for (j = 0; j < frag; j++) {
141 dma_unmap_page(pdev, dma_unmap_addr(&array[index], dma_addr),
142 skb_frag_size(&skb_shinfo(skb)->frags[j]),
144 dma_unmap_addr_set(&array[index], dma_addr, 0);
145 BNA_QE_INDX_ADD(index, 1, depth);
152 * Frees all pending Tx Bufs
153 * At this point no activity is expected on the Q,
154 * so DMA unmap & freeing is fine.
157 bnad_txq_cleanup(struct bnad *bnad,
161 struct bnad_unmap_q *unmap_q = tcb->unmap_q;
162 struct bnad_skb_unmap *unmap_array;
163 struct sk_buff *skb = NULL;
166 unmap_array = unmap_q->unmap_array;
168 for (q = 0; q < unmap_q->q_depth; q++) {
169 skb = unmap_array[q].skb;
174 unmap_cons = bnad_pci_unmap_skb(&bnad->pcidev->dev, unmap_array,
175 unmap_cons, unmap_q->q_depth, skb,
176 skb_shinfo(skb)->nr_frags);
178 dev_kfree_skb_any(skb);
182 /* Data Path Handlers */
185 * bnad_txcmpl_process : Frees the Tx bufs on Tx completion
186 * Can be called in a) Interrupt context
190 bnad_txcmpl_process(struct bnad *bnad,
193 u32 unmap_cons, sent_packets = 0, sent_bytes = 0;
194 u16 wis, updated_hw_cons;
195 struct bnad_unmap_q *unmap_q = tcb->unmap_q;
196 struct bnad_skb_unmap *unmap_array;
199 /* Just return if TX is stopped */
200 if (!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))
203 updated_hw_cons = *(tcb->hw_consumer_index);
205 wis = BNA_Q_INDEX_CHANGE(tcb->consumer_index,
206 updated_hw_cons, tcb->q_depth);
208 BUG_ON(!(wis <= BNA_QE_IN_USE_CNT(tcb, tcb->q_depth)));
210 unmap_array = unmap_q->unmap_array;
211 unmap_cons = unmap_q->consumer_index;
213 prefetch(&unmap_array[unmap_cons + 1]);
215 skb = unmap_array[unmap_cons].skb;
218 sent_bytes += skb->len;
219 wis -= BNA_TXQ_WI_NEEDED(1 + skb_shinfo(skb)->nr_frags);
221 unmap_cons = bnad_pci_unmap_skb(&bnad->pcidev->dev, unmap_array,
222 unmap_cons, unmap_q->q_depth, skb,
223 skb_shinfo(skb)->nr_frags);
225 dev_kfree_skb_any(skb);
228 /* Update consumer pointers. */
229 tcb->consumer_index = updated_hw_cons;
230 unmap_q->consumer_index = unmap_cons;
232 tcb->txq->tx_packets += sent_packets;
233 tcb->txq->tx_bytes += sent_bytes;
239 bnad_tx_complete(struct bnad *bnad, struct bna_tcb *tcb)
241 struct net_device *netdev = bnad->netdev;
244 if (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags))
247 sent = bnad_txcmpl_process(bnad, tcb);
249 if (netif_queue_stopped(netdev) &&
250 netif_carrier_ok(netdev) &&
251 BNA_QE_FREE_CNT(tcb, tcb->q_depth) >=
252 BNAD_NETIF_WAKE_THRESHOLD) {
253 if (test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)) {
254 netif_wake_queue(netdev);
255 BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
260 if (likely(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
261 bna_ib_ack(tcb->i_dbell, sent);
263 smp_mb__before_clear_bit();
264 clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
269 /* MSIX Tx Completion Handler */
271 bnad_msix_tx(int irq, void *data)
273 struct bna_tcb *tcb = (struct bna_tcb *)data;
274 struct bnad *bnad = tcb->bnad;
276 bnad_tx_complete(bnad, tcb);
282 bnad_rcb_cleanup(struct bnad *bnad, struct bna_rcb *rcb)
284 struct bnad_unmap_q *unmap_q = rcb->unmap_q;
286 rcb->producer_index = 0;
287 rcb->consumer_index = 0;
289 unmap_q->producer_index = 0;
290 unmap_q->consumer_index = 0;
294 bnad_rxq_cleanup(struct bnad *bnad, struct bna_rcb *rcb)
296 struct bnad_unmap_q *unmap_q;
297 struct bnad_skb_unmap *unmap_array;
301 unmap_q = rcb->unmap_q;
302 unmap_array = unmap_q->unmap_array;
303 for (unmap_cons = 0; unmap_cons < unmap_q->q_depth; unmap_cons++) {
304 skb = unmap_array[unmap_cons].skb;
307 unmap_array[unmap_cons].skb = NULL;
308 dma_unmap_single(&bnad->pcidev->dev,
309 dma_unmap_addr(&unmap_array[unmap_cons],
311 rcb->rxq->buffer_size,
315 bnad_rcb_cleanup(bnad, rcb);
319 bnad_rxq_post(struct bnad *bnad, struct bna_rcb *rcb)
321 u16 to_alloc, alloced, unmap_prod, wi_range;
322 struct bnad_unmap_q *unmap_q = rcb->unmap_q;
323 struct bnad_skb_unmap *unmap_array;
324 struct bna_rxq_entry *rxent;
330 BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth);
332 unmap_array = unmap_q->unmap_array;
333 unmap_prod = unmap_q->producer_index;
335 BNA_RXQ_QPGE_PTR_GET(unmap_prod, rcb->sw_qpt, rxent, wi_range);
339 BNA_RXQ_QPGE_PTR_GET(unmap_prod, rcb->sw_qpt, rxent,
341 skb = netdev_alloc_skb_ip_align(bnad->netdev,
342 rcb->rxq->buffer_size);
343 if (unlikely(!skb)) {
344 BNAD_UPDATE_CTR(bnad, rxbuf_alloc_failed);
345 rcb->rxq->rxbuf_alloc_failed++;
348 unmap_array[unmap_prod].skb = skb;
349 dma_addr = dma_map_single(&bnad->pcidev->dev, skb->data,
350 rcb->rxq->buffer_size,
352 dma_unmap_addr_set(&unmap_array[unmap_prod], dma_addr,
354 BNA_SET_DMA_ADDR(dma_addr, &rxent->host_addr);
355 BNA_QE_INDX_ADD(unmap_prod, 1, unmap_q->q_depth);
363 if (likely(alloced)) {
364 unmap_q->producer_index = unmap_prod;
365 rcb->producer_index = unmap_prod;
367 if (likely(test_bit(BNAD_RXQ_POST_OK, &rcb->flags)))
368 bna_rxq_prod_indx_doorbell(rcb);
373 bnad_refill_rxq(struct bnad *bnad, struct bna_rcb *rcb)
375 struct bnad_unmap_q *unmap_q = rcb->unmap_q;
377 if (!test_and_set_bit(BNAD_RXQ_REFILL, &rcb->flags)) {
378 if (BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth)
379 >> BNAD_RXQ_REFILL_THRESHOLD_SHIFT)
380 bnad_rxq_post(bnad, rcb);
381 smp_mb__before_clear_bit();
382 clear_bit(BNAD_RXQ_REFILL, &rcb->flags);
387 bnad_cq_process(struct bnad *bnad, struct bna_ccb *ccb, int budget)
389 struct bna_cq_entry *cmpl, *next_cmpl;
390 struct bna_rcb *rcb = NULL;
391 unsigned int wi_range, packets = 0, wis = 0;
392 struct bnad_unmap_q *unmap_q;
393 struct bnad_skb_unmap *unmap_array;
395 u32 flags, unmap_cons;
396 struct bna_pkt_rate *pkt_rt = &ccb->pkt_rate;
397 struct bnad_rx_ctrl *rx_ctrl = (struct bnad_rx_ctrl *)(ccb->ctrl);
399 if (!test_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags))
402 prefetch(bnad->netdev);
403 BNA_CQ_QPGE_PTR_GET(ccb->producer_index, ccb->sw_qpt, cmpl,
405 BUG_ON(!(wi_range <= ccb->q_depth));
406 while (cmpl->valid && packets < budget) {
408 BNA_UPDATE_PKT_CNT(pkt_rt, ntohs(cmpl->length));
410 if (bna_is_small_rxq(cmpl->rxq_id))
415 unmap_q = rcb->unmap_q;
416 unmap_array = unmap_q->unmap_array;
417 unmap_cons = unmap_q->consumer_index;
419 skb = unmap_array[unmap_cons].skb;
421 unmap_array[unmap_cons].skb = NULL;
422 dma_unmap_single(&bnad->pcidev->dev,
423 dma_unmap_addr(&unmap_array[unmap_cons],
425 rcb->rxq->buffer_size,
427 BNA_QE_INDX_ADD(unmap_q->consumer_index, 1, unmap_q->q_depth);
429 /* Should be more efficient ? Performance ? */
430 BNA_QE_INDX_ADD(rcb->consumer_index, 1, rcb->q_depth);
433 if (likely(--wi_range))
434 next_cmpl = cmpl + 1;
436 BNA_QE_INDX_ADD(ccb->producer_index, wis, ccb->q_depth);
438 BNA_CQ_QPGE_PTR_GET(ccb->producer_index, ccb->sw_qpt,
439 next_cmpl, wi_range);
440 BUG_ON(!(wi_range <= ccb->q_depth));
444 flags = ntohl(cmpl->flags);
447 (BNA_CQ_EF_MAC_ERROR | BNA_CQ_EF_FCS_ERROR |
448 BNA_CQ_EF_TOO_LONG))) {
449 dev_kfree_skb_any(skb);
450 rcb->rxq->rx_packets_with_error++;
454 skb_put(skb, ntohs(cmpl->length));
456 ((bnad->netdev->features & NETIF_F_RXCSUM) &&
457 (((flags & BNA_CQ_EF_IPV4) &&
458 (flags & BNA_CQ_EF_L3_CKSUM_OK)) ||
459 (flags & BNA_CQ_EF_IPV6)) &&
460 (flags & (BNA_CQ_EF_TCP | BNA_CQ_EF_UDP)) &&
461 (flags & BNA_CQ_EF_L4_CKSUM_OK)))
462 skb->ip_summed = CHECKSUM_UNNECESSARY;
464 skb_checksum_none_assert(skb);
466 rcb->rxq->rx_packets++;
467 rcb->rxq->rx_bytes += skb->len;
468 skb->protocol = eth_type_trans(skb, bnad->netdev);
470 if (flags & BNA_CQ_EF_VLAN)
471 __vlan_hwaccel_put_tag(skb, ntohs(cmpl->vlan_tag));
473 if (skb->ip_summed == CHECKSUM_UNNECESSARY)
474 napi_gro_receive(&rx_ctrl->napi, skb);
476 netif_receive_skb(skb);
483 BNA_QE_INDX_ADD(ccb->producer_index, wis, ccb->q_depth);
485 if (likely(test_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags)))
486 bna_ib_ack_disable_irq(ccb->i_dbell, packets);
488 bnad_refill_rxq(bnad, ccb->rcb[0]);
490 bnad_refill_rxq(bnad, ccb->rcb[1]);
492 clear_bit(BNAD_FP_IN_RX_PATH, &rx_ctrl->flags);
498 bnad_netif_rx_schedule_poll(struct bnad *bnad, struct bna_ccb *ccb)
500 struct bnad_rx_ctrl *rx_ctrl = (struct bnad_rx_ctrl *)(ccb->ctrl);
501 struct napi_struct *napi = &rx_ctrl->napi;
503 if (likely(napi_schedule_prep(napi))) {
504 __napi_schedule(napi);
505 rx_ctrl->rx_schedule++;
509 /* MSIX Rx Path Handler */
511 bnad_msix_rx(int irq, void *data)
513 struct bna_ccb *ccb = (struct bna_ccb *)data;
516 ((struct bnad_rx_ctrl *)(ccb->ctrl))->rx_intr_ctr++;
517 bnad_netif_rx_schedule_poll(ccb->bnad, ccb);
523 /* Interrupt handlers */
525 /* Mbox Interrupt Handlers */
527 bnad_msix_mbox_handler(int irq, void *data)
531 struct bnad *bnad = (struct bnad *)data;
533 spin_lock_irqsave(&bnad->bna_lock, flags);
534 if (unlikely(test_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags))) {
535 spin_unlock_irqrestore(&bnad->bna_lock, flags);
539 bna_intr_status_get(&bnad->bna, intr_status);
541 if (BNA_IS_MBOX_ERR_INTR(&bnad->bna, intr_status))
542 bna_mbox_handler(&bnad->bna, intr_status);
544 spin_unlock_irqrestore(&bnad->bna_lock, flags);
550 bnad_isr(int irq, void *data)
555 struct bnad *bnad = (struct bnad *)data;
556 struct bnad_rx_info *rx_info;
557 struct bnad_rx_ctrl *rx_ctrl;
558 struct bna_tcb *tcb = NULL;
560 spin_lock_irqsave(&bnad->bna_lock, flags);
561 if (unlikely(test_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags))) {
562 spin_unlock_irqrestore(&bnad->bna_lock, flags);
566 bna_intr_status_get(&bnad->bna, intr_status);
568 if (unlikely(!intr_status)) {
569 spin_unlock_irqrestore(&bnad->bna_lock, flags);
573 if (BNA_IS_MBOX_ERR_INTR(&bnad->bna, intr_status))
574 bna_mbox_handler(&bnad->bna, intr_status);
576 spin_unlock_irqrestore(&bnad->bna_lock, flags);
578 if (!BNA_IS_INTX_DATA_INTR(intr_status))
581 /* Process data interrupts */
583 for (i = 0; i < bnad->num_tx; i++) {
584 for (j = 0; j < bnad->num_txq_per_tx; j++) {
585 tcb = bnad->tx_info[i].tcb[j];
586 if (tcb && test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))
587 bnad_tx_complete(bnad, bnad->tx_info[i].tcb[j]);
591 for (i = 0; i < bnad->num_rx; i++) {
592 rx_info = &bnad->rx_info[i];
595 for (j = 0; j < bnad->num_rxp_per_rx; j++) {
596 rx_ctrl = &rx_info->rx_ctrl[j];
598 bnad_netif_rx_schedule_poll(bnad,
606 * Called in interrupt / callback context
607 * with bna_lock held, so cfg_flags access is OK
610 bnad_enable_mbox_irq(struct bnad *bnad)
612 clear_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
614 BNAD_UPDATE_CTR(bnad, mbox_intr_enabled);
618 * Called with bnad->bna_lock held b'cos of
619 * bnad->cfg_flags access.
622 bnad_disable_mbox_irq(struct bnad *bnad)
624 set_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
626 BNAD_UPDATE_CTR(bnad, mbox_intr_disabled);
630 bnad_set_netdev_perm_addr(struct bnad *bnad)
632 struct net_device *netdev = bnad->netdev;
634 memcpy(netdev->perm_addr, &bnad->perm_addr, netdev->addr_len);
635 if (is_zero_ether_addr(netdev->dev_addr))
636 memcpy(netdev->dev_addr, &bnad->perm_addr, netdev->addr_len);
639 /* Control Path Handlers */
643 bnad_cb_mbox_intr_enable(struct bnad *bnad)
645 bnad_enable_mbox_irq(bnad);
649 bnad_cb_mbox_intr_disable(struct bnad *bnad)
651 bnad_disable_mbox_irq(bnad);
655 bnad_cb_ioceth_ready(struct bnad *bnad)
657 bnad->bnad_completions.ioc_comp_status = BNA_CB_SUCCESS;
658 complete(&bnad->bnad_completions.ioc_comp);
662 bnad_cb_ioceth_failed(struct bnad *bnad)
664 bnad->bnad_completions.ioc_comp_status = BNA_CB_FAIL;
665 complete(&bnad->bnad_completions.ioc_comp);
669 bnad_cb_ioceth_disabled(struct bnad *bnad)
671 bnad->bnad_completions.ioc_comp_status = BNA_CB_SUCCESS;
672 complete(&bnad->bnad_completions.ioc_comp);
676 bnad_cb_enet_disabled(void *arg)
678 struct bnad *bnad = (struct bnad *)arg;
680 netif_carrier_off(bnad->netdev);
681 complete(&bnad->bnad_completions.enet_comp);
685 bnad_cb_ethport_link_status(struct bnad *bnad,
686 enum bna_link_status link_status)
688 bool link_up = false;
690 link_up = (link_status == BNA_LINK_UP) || (link_status == BNA_CEE_UP);
692 if (link_status == BNA_CEE_UP) {
693 if (!test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags))
694 BNAD_UPDATE_CTR(bnad, cee_toggle);
695 set_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags);
697 if (test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags))
698 BNAD_UPDATE_CTR(bnad, cee_toggle);
699 clear_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags);
703 if (!netif_carrier_ok(bnad->netdev)) {
705 printk(KERN_WARNING "bna: %s link up\n",
707 netif_carrier_on(bnad->netdev);
708 BNAD_UPDATE_CTR(bnad, link_toggle);
709 for (tx_id = 0; tx_id < bnad->num_tx; tx_id++) {
710 for (tcb_id = 0; tcb_id < bnad->num_txq_per_tx;
712 struct bna_tcb *tcb =
713 bnad->tx_info[tx_id].tcb[tcb_id];
720 if (test_bit(BNAD_TXQ_TX_STARTED,
724 * Transmit Schedule */
725 printk(KERN_INFO "bna: %s %d "
732 BNAD_UPDATE_CTR(bnad,
738 BNAD_UPDATE_CTR(bnad,
745 if (netif_carrier_ok(bnad->netdev)) {
746 printk(KERN_WARNING "bna: %s link down\n",
748 netif_carrier_off(bnad->netdev);
749 BNAD_UPDATE_CTR(bnad, link_toggle);
755 bnad_cb_tx_disabled(void *arg, struct bna_tx *tx)
757 struct bnad *bnad = (struct bnad *)arg;
759 complete(&bnad->bnad_completions.tx_comp);
763 bnad_cb_tcb_setup(struct bnad *bnad, struct bna_tcb *tcb)
765 struct bnad_tx_info *tx_info =
766 (struct bnad_tx_info *)tcb->txq->tx->priv;
767 struct bnad_unmap_q *unmap_q = tcb->unmap_q;
769 tx_info->tcb[tcb->id] = tcb;
770 unmap_q->producer_index = 0;
771 unmap_q->consumer_index = 0;
772 unmap_q->q_depth = BNAD_TX_UNMAPQ_DEPTH;
776 bnad_cb_tcb_destroy(struct bnad *bnad, struct bna_tcb *tcb)
778 struct bnad_tx_info *tx_info =
779 (struct bnad_tx_info *)tcb->txq->tx->priv;
781 tx_info->tcb[tcb->id] = NULL;
786 bnad_cb_rcb_setup(struct bnad *bnad, struct bna_rcb *rcb)
788 struct bnad_unmap_q *unmap_q = rcb->unmap_q;
790 unmap_q->producer_index = 0;
791 unmap_q->consumer_index = 0;
792 unmap_q->q_depth = BNAD_RX_UNMAPQ_DEPTH;
796 bnad_cb_ccb_setup(struct bnad *bnad, struct bna_ccb *ccb)
798 struct bnad_rx_info *rx_info =
799 (struct bnad_rx_info *)ccb->cq->rx->priv;
801 rx_info->rx_ctrl[ccb->id].ccb = ccb;
802 ccb->ctrl = &rx_info->rx_ctrl[ccb->id];
806 bnad_cb_ccb_destroy(struct bnad *bnad, struct bna_ccb *ccb)
808 struct bnad_rx_info *rx_info =
809 (struct bnad_rx_info *)ccb->cq->rx->priv;
811 rx_info->rx_ctrl[ccb->id].ccb = NULL;
815 bnad_cb_tx_stall(struct bnad *bnad, struct bna_tx *tx)
817 struct bnad_tx_info *tx_info =
818 (struct bnad_tx_info *)tx->priv;
823 for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
824 tcb = tx_info->tcb[i];
828 clear_bit(BNAD_TXQ_TX_STARTED, &tcb->flags);
829 netif_stop_subqueue(bnad->netdev, txq_id);
830 printk(KERN_INFO "bna: %s %d TXQ_STOPPED\n",
831 bnad->netdev->name, txq_id);
836 bnad_cb_tx_resume(struct bnad *bnad, struct bna_tx *tx)
838 struct bnad_tx_info *tx_info = (struct bnad_tx_info *)tx->priv;
843 for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
844 tcb = tx_info->tcb[i];
849 BUG_ON(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags));
850 set_bit(BNAD_TXQ_TX_STARTED, &tcb->flags);
851 BUG_ON(*(tcb->hw_consumer_index) != 0);
853 if (netif_carrier_ok(bnad->netdev)) {
854 printk(KERN_INFO "bna: %s %d TXQ_STARTED\n",
855 bnad->netdev->name, txq_id);
856 netif_wake_subqueue(bnad->netdev, txq_id);
857 BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
862 * Workaround for first ioceth enable failure & we
863 * get a 0 MAC address. We try to get the MAC address
866 if (is_zero_ether_addr(&bnad->perm_addr.mac[0])) {
867 bna_enet_perm_mac_get(&bnad->bna.enet, &bnad->perm_addr);
868 bnad_set_netdev_perm_addr(bnad);
873 * Free all TxQs buffers and then notify TX_E_CLEANUP_DONE to Tx fsm.
876 bnad_tx_cleanup(struct delayed_work *work)
878 struct bnad_tx_info *tx_info =
879 container_of(work, struct bnad_tx_info, tx_cleanup_work);
880 struct bnad *bnad = NULL;
881 struct bnad_unmap_q *unmap_q;
884 uint32_t i, pending = 0;
886 for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
887 tcb = tx_info->tcb[i];
893 if (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags)) {
898 bnad_txq_cleanup(bnad, tcb);
900 unmap_q = tcb->unmap_q;
901 unmap_q->producer_index = 0;
902 unmap_q->consumer_index = 0;
904 smp_mb__before_clear_bit();
905 clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
909 queue_delayed_work(bnad->work_q, &tx_info->tx_cleanup_work,
910 msecs_to_jiffies(1));
914 spin_lock_irqsave(&bnad->bna_lock, flags);
915 bna_tx_cleanup_complete(tx_info->tx);
916 spin_unlock_irqrestore(&bnad->bna_lock, flags);
921 bnad_cb_tx_cleanup(struct bnad *bnad, struct bna_tx *tx)
923 struct bnad_tx_info *tx_info = (struct bnad_tx_info *)tx->priv;
927 for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
928 tcb = tx_info->tcb[i];
933 queue_delayed_work(bnad->work_q, &tx_info->tx_cleanup_work, 0);
937 bnad_cb_rx_stall(struct bnad *bnad, struct bna_rx *rx)
939 struct bnad_rx_info *rx_info = (struct bnad_rx_info *)rx->priv;
941 struct bnad_rx_ctrl *rx_ctrl;
944 for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
945 rx_ctrl = &rx_info->rx_ctrl[i];
950 clear_bit(BNAD_RXQ_POST_OK, &ccb->rcb[0]->flags);
953 clear_bit(BNAD_RXQ_POST_OK, &ccb->rcb[1]->flags);
958 * Free all RxQs buffers and then notify RX_E_CLEANUP_DONE to Rx fsm.
961 bnad_rx_cleanup(void *work)
963 struct bnad_rx_info *rx_info =
964 container_of(work, struct bnad_rx_info, rx_cleanup_work);
965 struct bnad_rx_ctrl *rx_ctrl;
966 struct bnad *bnad = NULL;
970 for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
971 rx_ctrl = &rx_info->rx_ctrl[i];
976 bnad = rx_ctrl->ccb->bnad;
979 * Wait till the poll handler has exited
980 * and nothing can be scheduled anymore
982 napi_disable(&rx_ctrl->napi);
984 bnad_cq_cleanup(bnad, rx_ctrl->ccb);
985 bnad_rxq_cleanup(bnad, rx_ctrl->ccb->rcb[0]);
986 if (rx_ctrl->ccb->rcb[1])
987 bnad_rxq_cleanup(bnad, rx_ctrl->ccb->rcb[1]);
990 spin_lock_irqsave(&bnad->bna_lock, flags);
991 bna_rx_cleanup_complete(rx_info->rx);
992 spin_unlock_irqrestore(&bnad->bna_lock, flags);
996 bnad_cb_rx_cleanup(struct bnad *bnad, struct bna_rx *rx)
998 struct bnad_rx_info *rx_info = (struct bnad_rx_info *)rx->priv;
1000 struct bnad_rx_ctrl *rx_ctrl;
1003 for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
1004 rx_ctrl = &rx_info->rx_ctrl[i];
1009 clear_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags);
1012 clear_bit(BNAD_RXQ_STARTED, &ccb->rcb[1]->flags);
1015 queue_work(bnad->work_q, &rx_info->rx_cleanup_work);
1019 bnad_cb_rx_post(struct bnad *bnad, struct bna_rx *rx)
1021 struct bnad_rx_info *rx_info = (struct bnad_rx_info *)rx->priv;
1022 struct bna_ccb *ccb;
1023 struct bna_rcb *rcb;
1024 struct bnad_rx_ctrl *rx_ctrl;
1025 struct bnad_unmap_q *unmap_q;
1029 for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
1030 rx_ctrl = &rx_info->rx_ctrl[i];
1035 napi_enable(&rx_ctrl->napi);
1037 for (j = 0; j < BNAD_MAX_RXQ_PER_RXP; j++) {
1042 set_bit(BNAD_RXQ_STARTED, &rcb->flags);
1043 set_bit(BNAD_RXQ_POST_OK, &rcb->flags);
1044 unmap_q = rcb->unmap_q;
1046 /* Now allocate & post buffers for this RCB */
1047 /* !!Allocation in callback context */
1048 if (!test_and_set_bit(BNAD_RXQ_REFILL, &rcb->flags)) {
1049 if (BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth)
1050 >> BNAD_RXQ_REFILL_THRESHOLD_SHIFT)
1051 bnad_rxq_post(bnad, rcb);
1052 smp_mb__before_clear_bit();
1053 clear_bit(BNAD_RXQ_REFILL, &rcb->flags);
1060 bnad_cb_rx_disabled(void *arg, struct bna_rx *rx)
1062 struct bnad *bnad = (struct bnad *)arg;
1064 complete(&bnad->bnad_completions.rx_comp);
1068 bnad_cb_rx_mcast_add(struct bnad *bnad, struct bna_rx *rx)
1070 bnad->bnad_completions.mcast_comp_status = BNA_CB_SUCCESS;
1071 complete(&bnad->bnad_completions.mcast_comp);
1075 bnad_cb_stats_get(struct bnad *bnad, enum bna_cb_status status,
1076 struct bna_stats *stats)
1078 if (status == BNA_CB_SUCCESS)
1079 BNAD_UPDATE_CTR(bnad, hw_stats_updates);
1081 if (!netif_running(bnad->netdev) ||
1082 !test_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
1085 mod_timer(&bnad->stats_timer,
1086 jiffies + msecs_to_jiffies(BNAD_STATS_TIMER_FREQ));
1090 bnad_cb_enet_mtu_set(struct bnad *bnad)
1092 bnad->bnad_completions.mtu_comp_status = BNA_CB_SUCCESS;
1093 complete(&bnad->bnad_completions.mtu_comp);
1097 bnad_cb_completion(void *arg, enum bfa_status status)
1099 struct bnad_iocmd_comp *iocmd_comp =
1100 (struct bnad_iocmd_comp *)arg;
1102 iocmd_comp->comp_status = (u32) status;
1103 complete(&iocmd_comp->comp);
1106 /* Resource allocation, free functions */
1109 bnad_mem_free(struct bnad *bnad,
1110 struct bna_mem_info *mem_info)
1115 if (mem_info->mdl == NULL)
1118 for (i = 0; i < mem_info->num; i++) {
1119 if (mem_info->mdl[i].kva != NULL) {
1120 if (mem_info->mem_type == BNA_MEM_T_DMA) {
1121 BNA_GET_DMA_ADDR(&(mem_info->mdl[i].dma),
1123 dma_free_coherent(&bnad->pcidev->dev,
1124 mem_info->mdl[i].len,
1125 mem_info->mdl[i].kva, dma_pa);
1127 kfree(mem_info->mdl[i].kva);
1130 kfree(mem_info->mdl);
1131 mem_info->mdl = NULL;
1135 bnad_mem_alloc(struct bnad *bnad,
1136 struct bna_mem_info *mem_info)
1141 if ((mem_info->num == 0) || (mem_info->len == 0)) {
1142 mem_info->mdl = NULL;
1146 mem_info->mdl = kcalloc(mem_info->num, sizeof(struct bna_mem_descr),
1148 if (mem_info->mdl == NULL)
1151 if (mem_info->mem_type == BNA_MEM_T_DMA) {
1152 for (i = 0; i < mem_info->num; i++) {
1153 mem_info->mdl[i].len = mem_info->len;
1154 mem_info->mdl[i].kva =
1155 dma_alloc_coherent(&bnad->pcidev->dev,
1156 mem_info->len, &dma_pa,
1159 if (mem_info->mdl[i].kva == NULL)
1162 BNA_SET_DMA_ADDR(dma_pa,
1163 &(mem_info->mdl[i].dma));
1166 for (i = 0; i < mem_info->num; i++) {
1167 mem_info->mdl[i].len = mem_info->len;
1168 mem_info->mdl[i].kva = kzalloc(mem_info->len,
1170 if (mem_info->mdl[i].kva == NULL)
1178 bnad_mem_free(bnad, mem_info);
1182 /* Free IRQ for Mailbox */
1184 bnad_mbox_irq_free(struct bnad *bnad)
1187 unsigned long flags;
1189 spin_lock_irqsave(&bnad->bna_lock, flags);
1190 bnad_disable_mbox_irq(bnad);
1191 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1193 irq = BNAD_GET_MBOX_IRQ(bnad);
1194 free_irq(irq, bnad);
1198 * Allocates IRQ for Mailbox, but keep it disabled
1199 * This will be enabled once we get the mbox enable callback
1203 bnad_mbox_irq_alloc(struct bnad *bnad)
1206 unsigned long irq_flags, flags;
1208 irq_handler_t irq_handler;
1210 spin_lock_irqsave(&bnad->bna_lock, flags);
1211 if (bnad->cfg_flags & BNAD_CF_MSIX) {
1212 irq_handler = (irq_handler_t)bnad_msix_mbox_handler;
1213 irq = bnad->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector;
1216 irq_handler = (irq_handler_t)bnad_isr;
1217 irq = bnad->pcidev->irq;
1218 irq_flags = IRQF_SHARED;
1221 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1222 sprintf(bnad->mbox_irq_name, "%s", BNAD_NAME);
1225 * Set the Mbox IRQ disable flag, so that the IRQ handler
1226 * called from request_irq() for SHARED IRQs do not execute
1228 set_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
1230 BNAD_UPDATE_CTR(bnad, mbox_intr_disabled);
1232 err = request_irq(irq, irq_handler, irq_flags,
1233 bnad->mbox_irq_name, bnad);
1239 bnad_txrx_irq_free(struct bnad *bnad, struct bna_intr_info *intr_info)
1241 kfree(intr_info->idl);
1242 intr_info->idl = NULL;
1245 /* Allocates Interrupt Descriptor List for MSIX/INT-X vectors */
1247 bnad_txrx_irq_alloc(struct bnad *bnad, enum bnad_intr_source src,
1248 u32 txrx_id, struct bna_intr_info *intr_info)
1250 int i, vector_start = 0;
1252 unsigned long flags;
1254 spin_lock_irqsave(&bnad->bna_lock, flags);
1255 cfg_flags = bnad->cfg_flags;
1256 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1258 if (cfg_flags & BNAD_CF_MSIX) {
1259 intr_info->intr_type = BNA_INTR_T_MSIX;
1260 intr_info->idl = kcalloc(intr_info->num,
1261 sizeof(struct bna_intr_descr),
1263 if (!intr_info->idl)
1268 vector_start = BNAD_MAILBOX_MSIX_VECTORS + txrx_id;
1272 vector_start = BNAD_MAILBOX_MSIX_VECTORS +
1273 (bnad->num_tx * bnad->num_txq_per_tx) +
1281 for (i = 0; i < intr_info->num; i++)
1282 intr_info->idl[i].vector = vector_start + i;
1284 intr_info->intr_type = BNA_INTR_T_INTX;
1286 intr_info->idl = kcalloc(intr_info->num,
1287 sizeof(struct bna_intr_descr),
1289 if (!intr_info->idl)
1294 intr_info->idl[0].vector = BNAD_INTX_TX_IB_BITMASK;
1298 intr_info->idl[0].vector = BNAD_INTX_RX_IB_BITMASK;
1305 /* NOTE: Should be called for MSIX only
1306 * Unregisters Tx MSIX vector(s) from the kernel
1309 bnad_tx_msix_unregister(struct bnad *bnad, struct bnad_tx_info *tx_info,
1315 for (i = 0; i < num_txqs; i++) {
1316 if (tx_info->tcb[i] == NULL)
1319 vector_num = tx_info->tcb[i]->intr_vector;
1320 free_irq(bnad->msix_table[vector_num].vector, tx_info->tcb[i]);
1324 /* NOTE: Should be called for MSIX only
1325 * Registers Tx MSIX vector(s) and ISR(s), cookie with the kernel
1328 bnad_tx_msix_register(struct bnad *bnad, struct bnad_tx_info *tx_info,
1329 u32 tx_id, int num_txqs)
1335 for (i = 0; i < num_txqs; i++) {
1336 vector_num = tx_info->tcb[i]->intr_vector;
1337 sprintf(tx_info->tcb[i]->name, "%s TXQ %d", bnad->netdev->name,
1338 tx_id + tx_info->tcb[i]->id);
1339 err = request_irq(bnad->msix_table[vector_num].vector,
1340 (irq_handler_t)bnad_msix_tx, 0,
1341 tx_info->tcb[i]->name,
1351 bnad_tx_msix_unregister(bnad, tx_info, (i - 1));
1355 /* NOTE: Should be called for MSIX only
1356 * Unregisters Rx MSIX vector(s) from the kernel
1359 bnad_rx_msix_unregister(struct bnad *bnad, struct bnad_rx_info *rx_info,
1365 for (i = 0; i < num_rxps; i++) {
1366 if (rx_info->rx_ctrl[i].ccb == NULL)
1369 vector_num = rx_info->rx_ctrl[i].ccb->intr_vector;
1370 free_irq(bnad->msix_table[vector_num].vector,
1371 rx_info->rx_ctrl[i].ccb);
1375 /* NOTE: Should be called for MSIX only
1376 * Registers Tx MSIX vector(s) and ISR(s), cookie with the kernel
1379 bnad_rx_msix_register(struct bnad *bnad, struct bnad_rx_info *rx_info,
1380 u32 rx_id, int num_rxps)
1386 for (i = 0; i < num_rxps; i++) {
1387 vector_num = rx_info->rx_ctrl[i].ccb->intr_vector;
1388 sprintf(rx_info->rx_ctrl[i].ccb->name, "%s CQ %d",
1390 rx_id + rx_info->rx_ctrl[i].ccb->id);
1391 err = request_irq(bnad->msix_table[vector_num].vector,
1392 (irq_handler_t)bnad_msix_rx, 0,
1393 rx_info->rx_ctrl[i].ccb->name,
1394 rx_info->rx_ctrl[i].ccb);
1403 bnad_rx_msix_unregister(bnad, rx_info, (i - 1));
1407 /* Free Tx object Resources */
1409 bnad_tx_res_free(struct bnad *bnad, struct bna_res_info *res_info)
1413 for (i = 0; i < BNA_TX_RES_T_MAX; i++) {
1414 if (res_info[i].res_type == BNA_RES_T_MEM)
1415 bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
1416 else if (res_info[i].res_type == BNA_RES_T_INTR)
1417 bnad_txrx_irq_free(bnad, &res_info[i].res_u.intr_info);
1421 /* Allocates memory and interrupt resources for Tx object */
1423 bnad_tx_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
1428 for (i = 0; i < BNA_TX_RES_T_MAX; i++) {
1429 if (res_info[i].res_type == BNA_RES_T_MEM)
1430 err = bnad_mem_alloc(bnad,
1431 &res_info[i].res_u.mem_info);
1432 else if (res_info[i].res_type == BNA_RES_T_INTR)
1433 err = bnad_txrx_irq_alloc(bnad, BNAD_INTR_TX, tx_id,
1434 &res_info[i].res_u.intr_info);
1441 bnad_tx_res_free(bnad, res_info);
1445 /* Free Rx object Resources */
1447 bnad_rx_res_free(struct bnad *bnad, struct bna_res_info *res_info)
1451 for (i = 0; i < BNA_RX_RES_T_MAX; i++) {
1452 if (res_info[i].res_type == BNA_RES_T_MEM)
1453 bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
1454 else if (res_info[i].res_type == BNA_RES_T_INTR)
1455 bnad_txrx_irq_free(bnad, &res_info[i].res_u.intr_info);
1459 /* Allocates memory and interrupt resources for Rx object */
1461 bnad_rx_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
1466 /* All memory needs to be allocated before setup_ccbs */
1467 for (i = 0; i < BNA_RX_RES_T_MAX; i++) {
1468 if (res_info[i].res_type == BNA_RES_T_MEM)
1469 err = bnad_mem_alloc(bnad,
1470 &res_info[i].res_u.mem_info);
1471 else if (res_info[i].res_type == BNA_RES_T_INTR)
1472 err = bnad_txrx_irq_alloc(bnad, BNAD_INTR_RX, rx_id,
1473 &res_info[i].res_u.intr_info);
1480 bnad_rx_res_free(bnad, res_info);
1484 /* Timer callbacks */
1487 bnad_ioc_timeout(unsigned long data)
1489 struct bnad *bnad = (struct bnad *)data;
1490 unsigned long flags;
1492 spin_lock_irqsave(&bnad->bna_lock, flags);
1493 bfa_nw_ioc_timeout((void *) &bnad->bna.ioceth.ioc);
1494 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1498 bnad_ioc_hb_check(unsigned long data)
1500 struct bnad *bnad = (struct bnad *)data;
1501 unsigned long flags;
1503 spin_lock_irqsave(&bnad->bna_lock, flags);
1504 bfa_nw_ioc_hb_check((void *) &bnad->bna.ioceth.ioc);
1505 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1509 bnad_iocpf_timeout(unsigned long data)
1511 struct bnad *bnad = (struct bnad *)data;
1512 unsigned long flags;
1514 spin_lock_irqsave(&bnad->bna_lock, flags);
1515 bfa_nw_iocpf_timeout((void *) &bnad->bna.ioceth.ioc);
1516 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1520 bnad_iocpf_sem_timeout(unsigned long data)
1522 struct bnad *bnad = (struct bnad *)data;
1523 unsigned long flags;
1525 spin_lock_irqsave(&bnad->bna_lock, flags);
1526 bfa_nw_iocpf_sem_timeout((void *) &bnad->bna.ioceth.ioc);
1527 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1531 * All timer routines use bnad->bna_lock to protect against
1532 * the following race, which may occur in case of no locking:
1540 /* b) Dynamic Interrupt Moderation Timer */
1542 bnad_dim_timeout(unsigned long data)
1544 struct bnad *bnad = (struct bnad *)data;
1545 struct bnad_rx_info *rx_info;
1546 struct bnad_rx_ctrl *rx_ctrl;
1548 unsigned long flags;
1550 if (!netif_carrier_ok(bnad->netdev))
1553 spin_lock_irqsave(&bnad->bna_lock, flags);
1554 for (i = 0; i < bnad->num_rx; i++) {
1555 rx_info = &bnad->rx_info[i];
1558 for (j = 0; j < bnad->num_rxp_per_rx; j++) {
1559 rx_ctrl = &rx_info->rx_ctrl[j];
1562 bna_rx_dim_update(rx_ctrl->ccb);
1566 /* Check for BNAD_CF_DIM_ENABLED, does not eleminate a race */
1567 if (test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags))
1568 mod_timer(&bnad->dim_timer,
1569 jiffies + msecs_to_jiffies(BNAD_DIM_TIMER_FREQ));
1570 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1573 /* c) Statistics Timer */
1575 bnad_stats_timeout(unsigned long data)
1577 struct bnad *bnad = (struct bnad *)data;
1578 unsigned long flags;
1580 if (!netif_running(bnad->netdev) ||
1581 !test_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
1584 spin_lock_irqsave(&bnad->bna_lock, flags);
1585 bna_hw_stats_get(&bnad->bna);
1586 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1590 * Set up timer for DIM
1591 * Called with bnad->bna_lock held
1594 bnad_dim_timer_start(struct bnad *bnad)
1596 if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED &&
1597 !test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags)) {
1598 setup_timer(&bnad->dim_timer, bnad_dim_timeout,
1599 (unsigned long)bnad);
1600 set_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
1601 mod_timer(&bnad->dim_timer,
1602 jiffies + msecs_to_jiffies(BNAD_DIM_TIMER_FREQ));
1607 * Set up timer for statistics
1608 * Called with mutex_lock(&bnad->conf_mutex) held
1611 bnad_stats_timer_start(struct bnad *bnad)
1613 unsigned long flags;
1615 spin_lock_irqsave(&bnad->bna_lock, flags);
1616 if (!test_and_set_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags)) {
1617 setup_timer(&bnad->stats_timer, bnad_stats_timeout,
1618 (unsigned long)bnad);
1619 mod_timer(&bnad->stats_timer,
1620 jiffies + msecs_to_jiffies(BNAD_STATS_TIMER_FREQ));
1622 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1626 * Stops the stats timer
1627 * Called with mutex_lock(&bnad->conf_mutex) held
1630 bnad_stats_timer_stop(struct bnad *bnad)
1633 unsigned long flags;
1635 spin_lock_irqsave(&bnad->bna_lock, flags);
1636 if (test_and_clear_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
1638 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1640 del_timer_sync(&bnad->stats_timer);
1646 bnad_netdev_mc_list_get(struct net_device *netdev, u8 *mc_list)
1648 int i = 1; /* Index 0 has broadcast address */
1649 struct netdev_hw_addr *mc_addr;
1651 netdev_for_each_mc_addr(mc_addr, netdev) {
1652 memcpy(&mc_list[i * ETH_ALEN], &mc_addr->addr[0],
1659 bnad_napi_poll_rx(struct napi_struct *napi, int budget)
1661 struct bnad_rx_ctrl *rx_ctrl =
1662 container_of(napi, struct bnad_rx_ctrl, napi);
1663 struct bnad *bnad = rx_ctrl->bnad;
1666 rx_ctrl->rx_poll_ctr++;
1668 if (!netif_carrier_ok(bnad->netdev))
1671 rcvd = bnad_cq_process(bnad, rx_ctrl->ccb, budget);
1676 napi_complete(napi);
1678 rx_ctrl->rx_complete++;
1681 bnad_enable_rx_irq_unsafe(rx_ctrl->ccb);
1686 #define BNAD_NAPI_POLL_QUOTA 64
1688 bnad_napi_add(struct bnad *bnad, u32 rx_id)
1690 struct bnad_rx_ctrl *rx_ctrl;
1693 /* Initialize & enable NAPI */
1694 for (i = 0; i < bnad->num_rxp_per_rx; i++) {
1695 rx_ctrl = &bnad->rx_info[rx_id].rx_ctrl[i];
1696 netif_napi_add(bnad->netdev, &rx_ctrl->napi,
1697 bnad_napi_poll_rx, BNAD_NAPI_POLL_QUOTA);
1702 bnad_napi_delete(struct bnad *bnad, u32 rx_id)
1706 /* First disable and then clean up */
1707 for (i = 0; i < bnad->num_rxp_per_rx; i++)
1708 netif_napi_del(&bnad->rx_info[rx_id].rx_ctrl[i].napi);
1711 /* Should be held with conf_lock held */
1713 bnad_destroy_tx(struct bnad *bnad, u32 tx_id)
1715 struct bnad_tx_info *tx_info = &bnad->tx_info[tx_id];
1716 struct bna_res_info *res_info = &bnad->tx_res_info[tx_id].res_info[0];
1717 unsigned long flags;
1722 init_completion(&bnad->bnad_completions.tx_comp);
1723 spin_lock_irqsave(&bnad->bna_lock, flags);
1724 bna_tx_disable(tx_info->tx, BNA_HARD_CLEANUP, bnad_cb_tx_disabled);
1725 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1726 wait_for_completion(&bnad->bnad_completions.tx_comp);
1728 if (tx_info->tcb[0]->intr_type == BNA_INTR_T_MSIX)
1729 bnad_tx_msix_unregister(bnad, tx_info,
1730 bnad->num_txq_per_tx);
1732 spin_lock_irqsave(&bnad->bna_lock, flags);
1733 bna_tx_destroy(tx_info->tx);
1734 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1739 bnad_tx_res_free(bnad, res_info);
1742 /* Should be held with conf_lock held */
1744 bnad_setup_tx(struct bnad *bnad, u32 tx_id)
1747 struct bnad_tx_info *tx_info = &bnad->tx_info[tx_id];
1748 struct bna_res_info *res_info = &bnad->tx_res_info[tx_id].res_info[0];
1749 struct bna_intr_info *intr_info =
1750 &res_info[BNA_TX_RES_INTR_T_TXCMPL].res_u.intr_info;
1751 struct bna_tx_config *tx_config = &bnad->tx_config[tx_id];
1752 static const struct bna_tx_event_cbfn tx_cbfn = {
1753 .tcb_setup_cbfn = bnad_cb_tcb_setup,
1754 .tcb_destroy_cbfn = bnad_cb_tcb_destroy,
1755 .tx_stall_cbfn = bnad_cb_tx_stall,
1756 .tx_resume_cbfn = bnad_cb_tx_resume,
1757 .tx_cleanup_cbfn = bnad_cb_tx_cleanup,
1761 unsigned long flags;
1763 tx_info->tx_id = tx_id;
1765 /* Initialize the Tx object configuration */
1766 tx_config->num_txq = bnad->num_txq_per_tx;
1767 tx_config->txq_depth = bnad->txq_depth;
1768 tx_config->tx_type = BNA_TX_T_REGULAR;
1769 tx_config->coalescing_timeo = bnad->tx_coalescing_timeo;
1771 /* Get BNA's resource requirement for one tx object */
1772 spin_lock_irqsave(&bnad->bna_lock, flags);
1773 bna_tx_res_req(bnad->num_txq_per_tx,
1774 bnad->txq_depth, res_info);
1775 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1777 /* Fill Unmap Q memory requirements */
1778 BNAD_FILL_UNMAPQ_MEM_REQ(
1779 &res_info[BNA_TX_RES_MEM_T_UNMAPQ],
1780 bnad->num_txq_per_tx,
1781 BNAD_TX_UNMAPQ_DEPTH);
1783 /* Allocate resources */
1784 err = bnad_tx_res_alloc(bnad, res_info, tx_id);
1788 /* Ask BNA to create one Tx object, supplying required resources */
1789 spin_lock_irqsave(&bnad->bna_lock, flags);
1790 tx = bna_tx_create(&bnad->bna, bnad, tx_config, &tx_cbfn, res_info,
1792 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1797 INIT_DELAYED_WORK(&tx_info->tx_cleanup_work,
1798 (work_func_t)bnad_tx_cleanup);
1800 /* Register ISR for the Tx object */
1801 if (intr_info->intr_type == BNA_INTR_T_MSIX) {
1802 err = bnad_tx_msix_register(bnad, tx_info,
1803 tx_id, bnad->num_txq_per_tx);
1808 spin_lock_irqsave(&bnad->bna_lock, flags);
1810 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1815 bnad_tx_res_free(bnad, res_info);
1819 /* Setup the rx config for bna_rx_create */
1820 /* bnad decides the configuration */
1822 bnad_init_rx_config(struct bnad *bnad, struct bna_rx_config *rx_config)
1824 rx_config->rx_type = BNA_RX_T_REGULAR;
1825 rx_config->num_paths = bnad->num_rxp_per_rx;
1826 rx_config->coalescing_timeo = bnad->rx_coalescing_timeo;
1828 if (bnad->num_rxp_per_rx > 1) {
1829 rx_config->rss_status = BNA_STATUS_T_ENABLED;
1830 rx_config->rss_config.hash_type =
1831 (BFI_ENET_RSS_IPV6 |
1832 BFI_ENET_RSS_IPV6_TCP |
1834 BFI_ENET_RSS_IPV4_TCP);
1835 rx_config->rss_config.hash_mask =
1836 bnad->num_rxp_per_rx - 1;
1837 get_random_bytes(rx_config->rss_config.toeplitz_hash_key,
1838 sizeof(rx_config->rss_config.toeplitz_hash_key));
1840 rx_config->rss_status = BNA_STATUS_T_DISABLED;
1841 memset(&rx_config->rss_config, 0,
1842 sizeof(rx_config->rss_config));
1844 rx_config->rxp_type = BNA_RXP_SLR;
1845 rx_config->q_depth = bnad->rxq_depth;
1847 rx_config->small_buff_size = BFI_SMALL_RXBUF_SIZE;
1849 rx_config->vlan_strip_status = BNA_STATUS_T_ENABLED;
1853 bnad_rx_ctrl_init(struct bnad *bnad, u32 rx_id)
1855 struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
1858 for (i = 0; i < bnad->num_rxp_per_rx; i++)
1859 rx_info->rx_ctrl[i].bnad = bnad;
1862 /* Called with mutex_lock(&bnad->conf_mutex) held */
1864 bnad_destroy_rx(struct bnad *bnad, u32 rx_id)
1866 struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
1867 struct bna_rx_config *rx_config = &bnad->rx_config[rx_id];
1868 struct bna_res_info *res_info = &bnad->rx_res_info[rx_id].res_info[0];
1869 unsigned long flags;
1876 spin_lock_irqsave(&bnad->bna_lock, flags);
1877 if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED &&
1878 test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags)) {
1879 clear_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
1882 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1884 del_timer_sync(&bnad->dim_timer);
1887 init_completion(&bnad->bnad_completions.rx_comp);
1888 spin_lock_irqsave(&bnad->bna_lock, flags);
1889 bna_rx_disable(rx_info->rx, BNA_HARD_CLEANUP, bnad_cb_rx_disabled);
1890 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1891 wait_for_completion(&bnad->bnad_completions.rx_comp);
1893 if (rx_info->rx_ctrl[0].ccb->intr_type == BNA_INTR_T_MSIX)
1894 bnad_rx_msix_unregister(bnad, rx_info, rx_config->num_paths);
1896 bnad_napi_delete(bnad, rx_id);
1898 spin_lock_irqsave(&bnad->bna_lock, flags);
1899 bna_rx_destroy(rx_info->rx);
1903 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1905 bnad_rx_res_free(bnad, res_info);
1908 /* Called with mutex_lock(&bnad->conf_mutex) held */
1910 bnad_setup_rx(struct bnad *bnad, u32 rx_id)
1913 struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
1914 struct bna_res_info *res_info = &bnad->rx_res_info[rx_id].res_info[0];
1915 struct bna_intr_info *intr_info =
1916 &res_info[BNA_RX_RES_T_INTR].res_u.intr_info;
1917 struct bna_rx_config *rx_config = &bnad->rx_config[rx_id];
1918 static const struct bna_rx_event_cbfn rx_cbfn = {
1919 .rcb_setup_cbfn = bnad_cb_rcb_setup,
1920 .rcb_destroy_cbfn = NULL,
1921 .ccb_setup_cbfn = bnad_cb_ccb_setup,
1922 .ccb_destroy_cbfn = bnad_cb_ccb_destroy,
1923 .rx_stall_cbfn = bnad_cb_rx_stall,
1924 .rx_cleanup_cbfn = bnad_cb_rx_cleanup,
1925 .rx_post_cbfn = bnad_cb_rx_post,
1928 unsigned long flags;
1930 rx_info->rx_id = rx_id;
1932 /* Initialize the Rx object configuration */
1933 bnad_init_rx_config(bnad, rx_config);
1935 /* Get BNA's resource requirement for one Rx object */
1936 spin_lock_irqsave(&bnad->bna_lock, flags);
1937 bna_rx_res_req(rx_config, res_info);
1938 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1940 /* Fill Unmap Q memory requirements */
1941 BNAD_FILL_UNMAPQ_MEM_REQ(
1942 &res_info[BNA_RX_RES_MEM_T_UNMAPQ],
1943 rx_config->num_paths +
1944 ((rx_config->rxp_type == BNA_RXP_SINGLE) ? 0 :
1945 rx_config->num_paths), BNAD_RX_UNMAPQ_DEPTH);
1947 /* Allocate resource */
1948 err = bnad_rx_res_alloc(bnad, res_info, rx_id);
1952 bnad_rx_ctrl_init(bnad, rx_id);
1954 /* Ask BNA to create one Rx object, supplying required resources */
1955 spin_lock_irqsave(&bnad->bna_lock, flags);
1956 rx = bna_rx_create(&bnad->bna, bnad, rx_config, &rx_cbfn, res_info,
1960 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1964 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1966 INIT_WORK(&rx_info->rx_cleanup_work,
1967 (work_func_t)(bnad_rx_cleanup));
1970 * Init NAPI, so that state is set to NAPI_STATE_SCHED,
1971 * so that IRQ handler cannot schedule NAPI at this point.
1973 bnad_napi_add(bnad, rx_id);
1975 /* Register ISR for the Rx object */
1976 if (intr_info->intr_type == BNA_INTR_T_MSIX) {
1977 err = bnad_rx_msix_register(bnad, rx_info, rx_id,
1978 rx_config->num_paths);
1983 spin_lock_irqsave(&bnad->bna_lock, flags);
1985 /* Set up Dynamic Interrupt Moderation Vector */
1986 if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED)
1987 bna_rx_dim_reconfig(&bnad->bna, bna_napi_dim_vector);
1989 /* Enable VLAN filtering only on the default Rx */
1990 bna_rx_vlanfilter_enable(rx);
1992 /* Start the DIM timer */
1993 bnad_dim_timer_start(bnad);
1997 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2002 bnad_destroy_rx(bnad, rx_id);
2006 /* Called with conf_lock & bnad->bna_lock held */
2008 bnad_tx_coalescing_timeo_set(struct bnad *bnad)
2010 struct bnad_tx_info *tx_info;
2012 tx_info = &bnad->tx_info[0];
2016 bna_tx_coalescing_timeo_set(tx_info->tx, bnad->tx_coalescing_timeo);
2019 /* Called with conf_lock & bnad->bna_lock held */
2021 bnad_rx_coalescing_timeo_set(struct bnad *bnad)
2023 struct bnad_rx_info *rx_info;
2026 for (i = 0; i < bnad->num_rx; i++) {
2027 rx_info = &bnad->rx_info[i];
2030 bna_rx_coalescing_timeo_set(rx_info->rx,
2031 bnad->rx_coalescing_timeo);
2036 * Called with bnad->bna_lock held
2039 bnad_mac_addr_set_locked(struct bnad *bnad, u8 *mac_addr)
2043 if (!is_valid_ether_addr(mac_addr))
2044 return -EADDRNOTAVAIL;
2046 /* If datapath is down, pretend everything went through */
2047 if (!bnad->rx_info[0].rx)
2050 ret = bna_rx_ucast_set(bnad->rx_info[0].rx, mac_addr, NULL);
2051 if (ret != BNA_CB_SUCCESS)
2052 return -EADDRNOTAVAIL;
2057 /* Should be called with conf_lock held */
2059 bnad_enable_default_bcast(struct bnad *bnad)
2061 struct bnad_rx_info *rx_info = &bnad->rx_info[0];
2063 unsigned long flags;
2065 init_completion(&bnad->bnad_completions.mcast_comp);
2067 spin_lock_irqsave(&bnad->bna_lock, flags);
2068 ret = bna_rx_mcast_add(rx_info->rx, (u8 *)bnad_bcast_addr,
2069 bnad_cb_rx_mcast_add);
2070 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2072 if (ret == BNA_CB_SUCCESS)
2073 wait_for_completion(&bnad->bnad_completions.mcast_comp);
2077 if (bnad->bnad_completions.mcast_comp_status != BNA_CB_SUCCESS)
2083 /* Called with mutex_lock(&bnad->conf_mutex) held */
2085 bnad_restore_vlans(struct bnad *bnad, u32 rx_id)
2088 unsigned long flags;
2090 for_each_set_bit(vid, bnad->active_vlans, VLAN_N_VID) {
2091 spin_lock_irqsave(&bnad->bna_lock, flags);
2092 bna_rx_vlan_add(bnad->rx_info[rx_id].rx, vid);
2093 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2097 /* Statistics utilities */
2099 bnad_netdev_qstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
2103 for (i = 0; i < bnad->num_rx; i++) {
2104 for (j = 0; j < bnad->num_rxp_per_rx; j++) {
2105 if (bnad->rx_info[i].rx_ctrl[j].ccb) {
2106 stats->rx_packets += bnad->rx_info[i].
2107 rx_ctrl[j].ccb->rcb[0]->rxq->rx_packets;
2108 stats->rx_bytes += bnad->rx_info[i].
2109 rx_ctrl[j].ccb->rcb[0]->rxq->rx_bytes;
2110 if (bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1] &&
2111 bnad->rx_info[i].rx_ctrl[j].ccb->
2113 stats->rx_packets +=
2114 bnad->rx_info[i].rx_ctrl[j].
2115 ccb->rcb[1]->rxq->rx_packets;
2117 bnad->rx_info[i].rx_ctrl[j].
2118 ccb->rcb[1]->rxq->rx_bytes;
2123 for (i = 0; i < bnad->num_tx; i++) {
2124 for (j = 0; j < bnad->num_txq_per_tx; j++) {
2125 if (bnad->tx_info[i].tcb[j]) {
2126 stats->tx_packets +=
2127 bnad->tx_info[i].tcb[j]->txq->tx_packets;
2129 bnad->tx_info[i].tcb[j]->txq->tx_bytes;
2136 * Must be called with the bna_lock held.
2139 bnad_netdev_hwstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
2141 struct bfi_enet_stats_mac *mac_stats;
2145 mac_stats = &bnad->stats.bna_stats->hw_stats.mac_stats;
2147 mac_stats->rx_fcs_error + mac_stats->rx_alignment_error +
2148 mac_stats->rx_frame_length_error + mac_stats->rx_code_error +
2149 mac_stats->rx_undersize;
2150 stats->tx_errors = mac_stats->tx_fcs_error +
2151 mac_stats->tx_undersize;
2152 stats->rx_dropped = mac_stats->rx_drop;
2153 stats->tx_dropped = mac_stats->tx_drop;
2154 stats->multicast = mac_stats->rx_multicast;
2155 stats->collisions = mac_stats->tx_total_collision;
2157 stats->rx_length_errors = mac_stats->rx_frame_length_error;
2159 /* receive ring buffer overflow ?? */
2161 stats->rx_crc_errors = mac_stats->rx_fcs_error;
2162 stats->rx_frame_errors = mac_stats->rx_alignment_error;
2163 /* recv'r fifo overrun */
2164 bmap = bna_rx_rid_mask(&bnad->bna);
2165 for (i = 0; bmap; i++) {
2167 stats->rx_fifo_errors +=
2168 bnad->stats.bna_stats->
2169 hw_stats.rxf_stats[i].frame_drops;
2177 bnad_mbox_irq_sync(struct bnad *bnad)
2180 unsigned long flags;
2182 spin_lock_irqsave(&bnad->bna_lock, flags);
2183 if (bnad->cfg_flags & BNAD_CF_MSIX)
2184 irq = bnad->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector;
2186 irq = bnad->pcidev->irq;
2187 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2189 synchronize_irq(irq);
2192 /* Utility used by bnad_start_xmit, for doing TSO */
2194 bnad_tso_prepare(struct bnad *bnad, struct sk_buff *skb)
2198 if (skb_header_cloned(skb)) {
2199 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2201 BNAD_UPDATE_CTR(bnad, tso_err);
2207 * For TSO, the TCP checksum field is seeded with pseudo-header sum
2208 * excluding the length field.
2210 if (skb->protocol == htons(ETH_P_IP)) {
2211 struct iphdr *iph = ip_hdr(skb);
2213 /* Do we really need these? */
2217 tcp_hdr(skb)->check =
2218 ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
2220 BNAD_UPDATE_CTR(bnad, tso4);
2222 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
2224 ipv6h->payload_len = 0;
2225 tcp_hdr(skb)->check =
2226 ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, 0,
2228 BNAD_UPDATE_CTR(bnad, tso6);
2235 * Initialize Q numbers depending on Rx Paths
2236 * Called with bnad->bna_lock held, because of cfg_flags
2240 bnad_q_num_init(struct bnad *bnad)
2244 rxps = min((uint)num_online_cpus(),
2245 (uint)(BNAD_MAX_RX * BNAD_MAX_RXP_PER_RX));
2247 if (!(bnad->cfg_flags & BNAD_CF_MSIX))
2248 rxps = 1; /* INTx */
2252 bnad->num_rxp_per_rx = rxps;
2253 bnad->num_txq_per_tx = BNAD_TXQ_NUM;
2257 * Adjusts the Q numbers, given a number of msix vectors
2258 * Give preference to RSS as opposed to Tx priority Queues,
2259 * in such a case, just use 1 Tx Q
2260 * Called with bnad->bna_lock held b'cos of cfg_flags access
2263 bnad_q_num_adjust(struct bnad *bnad, int msix_vectors, int temp)
2265 bnad->num_txq_per_tx = 1;
2266 if ((msix_vectors >= (bnad->num_tx * bnad->num_txq_per_tx) +
2267 bnad_rxqs_per_cq + BNAD_MAILBOX_MSIX_VECTORS) &&
2268 (bnad->cfg_flags & BNAD_CF_MSIX)) {
2269 bnad->num_rxp_per_rx = msix_vectors -
2270 (bnad->num_tx * bnad->num_txq_per_tx) -
2271 BNAD_MAILBOX_MSIX_VECTORS;
2273 bnad->num_rxp_per_rx = 1;
2276 /* Enable / disable ioceth */
2278 bnad_ioceth_disable(struct bnad *bnad)
2280 unsigned long flags;
2283 spin_lock_irqsave(&bnad->bna_lock, flags);
2284 init_completion(&bnad->bnad_completions.ioc_comp);
2285 bna_ioceth_disable(&bnad->bna.ioceth, BNA_HARD_CLEANUP);
2286 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2288 wait_for_completion_timeout(&bnad->bnad_completions.ioc_comp,
2289 msecs_to_jiffies(BNAD_IOCETH_TIMEOUT));
2291 err = bnad->bnad_completions.ioc_comp_status;
2296 bnad_ioceth_enable(struct bnad *bnad)
2299 unsigned long flags;
2301 spin_lock_irqsave(&bnad->bna_lock, flags);
2302 init_completion(&bnad->bnad_completions.ioc_comp);
2303 bnad->bnad_completions.ioc_comp_status = BNA_CB_WAITING;
2304 bna_ioceth_enable(&bnad->bna.ioceth);
2305 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2307 wait_for_completion_timeout(&bnad->bnad_completions.ioc_comp,
2308 msecs_to_jiffies(BNAD_IOCETH_TIMEOUT));
2310 err = bnad->bnad_completions.ioc_comp_status;
2315 /* Free BNA resources */
2317 bnad_res_free(struct bnad *bnad, struct bna_res_info *res_info,
2322 for (i = 0; i < res_val_max; i++)
2323 bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
2326 /* Allocates memory and interrupt resources for BNA */
2328 bnad_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
2333 for (i = 0; i < res_val_max; i++) {
2334 err = bnad_mem_alloc(bnad, &res_info[i].res_u.mem_info);
2341 bnad_res_free(bnad, res_info, res_val_max);
2345 /* Interrupt enable / disable */
2347 bnad_enable_msix(struct bnad *bnad)
2350 unsigned long flags;
2352 spin_lock_irqsave(&bnad->bna_lock, flags);
2353 if (!(bnad->cfg_flags & BNAD_CF_MSIX)) {
2354 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2357 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2359 if (bnad->msix_table)
2363 kcalloc(bnad->msix_num, sizeof(struct msix_entry), GFP_KERNEL);
2365 if (!bnad->msix_table)
2368 for (i = 0; i < bnad->msix_num; i++)
2369 bnad->msix_table[i].entry = i;
2371 ret = pci_enable_msix(bnad->pcidev, bnad->msix_table, bnad->msix_num);
2373 /* Not enough MSI-X vectors. */
2374 pr_warn("BNA: %d MSI-X vectors allocated < %d requested\n",
2375 ret, bnad->msix_num);
2377 spin_lock_irqsave(&bnad->bna_lock, flags);
2378 /* ret = #of vectors that we got */
2379 bnad_q_num_adjust(bnad, (ret - BNAD_MAILBOX_MSIX_VECTORS) / 2,
2380 (ret - BNAD_MAILBOX_MSIX_VECTORS) / 2);
2381 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2383 bnad->msix_num = BNAD_NUM_TXQ + BNAD_NUM_RXP +
2384 BNAD_MAILBOX_MSIX_VECTORS;
2386 if (bnad->msix_num > ret)
2389 /* Try once more with adjusted numbers */
2390 /* If this fails, fall back to INTx */
2391 ret = pci_enable_msix(bnad->pcidev, bnad->msix_table,
2399 pci_intx(bnad->pcidev, 0);
2404 pr_warn("BNA: MSI-X enable failed - operating in INTx mode\n");
2406 kfree(bnad->msix_table);
2407 bnad->msix_table = NULL;
2409 spin_lock_irqsave(&bnad->bna_lock, flags);
2410 bnad->cfg_flags &= ~BNAD_CF_MSIX;
2411 bnad_q_num_init(bnad);
2412 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2416 bnad_disable_msix(struct bnad *bnad)
2419 unsigned long flags;
2421 spin_lock_irqsave(&bnad->bna_lock, flags);
2422 cfg_flags = bnad->cfg_flags;
2423 if (bnad->cfg_flags & BNAD_CF_MSIX)
2424 bnad->cfg_flags &= ~BNAD_CF_MSIX;
2425 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2427 if (cfg_flags & BNAD_CF_MSIX) {
2428 pci_disable_msix(bnad->pcidev);
2429 kfree(bnad->msix_table);
2430 bnad->msix_table = NULL;
2434 /* Netdev entry points */
2436 bnad_open(struct net_device *netdev)
2439 struct bnad *bnad = netdev_priv(netdev);
2440 struct bna_pause_config pause_config;
2442 unsigned long flags;
2444 mutex_lock(&bnad->conf_mutex);
2447 err = bnad_setup_tx(bnad, 0);
2452 err = bnad_setup_rx(bnad, 0);
2457 pause_config.tx_pause = 0;
2458 pause_config.rx_pause = 0;
2460 mtu = ETH_HLEN + VLAN_HLEN + bnad->netdev->mtu + ETH_FCS_LEN;
2462 spin_lock_irqsave(&bnad->bna_lock, flags);
2463 bna_enet_mtu_set(&bnad->bna.enet, mtu, NULL);
2464 bna_enet_pause_config(&bnad->bna.enet, &pause_config, NULL);
2465 bna_enet_enable(&bnad->bna.enet);
2466 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2468 /* Enable broadcast */
2469 bnad_enable_default_bcast(bnad);
2471 /* Restore VLANs, if any */
2472 bnad_restore_vlans(bnad, 0);
2474 /* Set the UCAST address */
2475 spin_lock_irqsave(&bnad->bna_lock, flags);
2476 bnad_mac_addr_set_locked(bnad, netdev->dev_addr);
2477 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2479 /* Start the stats timer */
2480 bnad_stats_timer_start(bnad);
2482 mutex_unlock(&bnad->conf_mutex);
2487 bnad_destroy_tx(bnad, 0);
2490 mutex_unlock(&bnad->conf_mutex);
2495 bnad_stop(struct net_device *netdev)
2497 struct bnad *bnad = netdev_priv(netdev);
2498 unsigned long flags;
2500 mutex_lock(&bnad->conf_mutex);
2502 /* Stop the stats timer */
2503 bnad_stats_timer_stop(bnad);
2505 init_completion(&bnad->bnad_completions.enet_comp);
2507 spin_lock_irqsave(&bnad->bna_lock, flags);
2508 bna_enet_disable(&bnad->bna.enet, BNA_HARD_CLEANUP,
2509 bnad_cb_enet_disabled);
2510 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2512 wait_for_completion(&bnad->bnad_completions.enet_comp);
2514 bnad_destroy_tx(bnad, 0);
2515 bnad_destroy_rx(bnad, 0);
2517 /* Synchronize mailbox IRQ */
2518 bnad_mbox_irq_sync(bnad);
2520 mutex_unlock(&bnad->conf_mutex);
2527 * bnad_start_xmit : Netdev entry point for Transmit
2528 * Called under lock held by net_device
2531 bnad_start_xmit(struct sk_buff *skb, struct net_device *netdev)
2533 struct bnad *bnad = netdev_priv(netdev);
2535 struct bna_tcb *tcb = bnad->tx_info[0].tcb[txq_id];
2537 u16 txq_prod, vlan_tag = 0;
2538 u32 unmap_prod, wis, wis_used, wi_range;
2539 u32 vectors, vect_id, i, acked;
2544 struct bnad_unmap_q *unmap_q = tcb->unmap_q;
2545 dma_addr_t dma_addr;
2546 struct bna_txq_entry *txqent;
2549 if (unlikely(skb->len <= ETH_HLEN)) {
2551 BNAD_UPDATE_CTR(bnad, tx_skb_too_short);
2552 return NETDEV_TX_OK;
2554 if (unlikely(skb_headlen(skb) > BFI_TX_MAX_DATA_PER_VECTOR)) {
2556 BNAD_UPDATE_CTR(bnad, tx_skb_headlen_too_long);
2557 return NETDEV_TX_OK;
2559 if (unlikely(skb_headlen(skb) == 0)) {
2561 BNAD_UPDATE_CTR(bnad, tx_skb_headlen_zero);
2562 return NETDEV_TX_OK;
2566 * Takes care of the Tx that is scheduled between clearing the flag
2567 * and the netif_tx_stop_all_queues() call.
2569 if (unlikely(!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))) {
2571 BNAD_UPDATE_CTR(bnad, tx_skb_stopping);
2572 return NETDEV_TX_OK;
2575 vectors = 1 + skb_shinfo(skb)->nr_frags;
2576 if (unlikely(vectors > BFI_TX_MAX_VECTORS_PER_PKT)) {
2578 BNAD_UPDATE_CTR(bnad, tx_skb_max_vectors);
2579 return NETDEV_TX_OK;
2581 wis = BNA_TXQ_WI_NEEDED(vectors); /* 4 vectors per work item */
2583 if (unlikely(wis > BNA_QE_FREE_CNT(tcb, tcb->q_depth) ||
2584 vectors > BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth))) {
2585 if ((u16) (*tcb->hw_consumer_index) !=
2586 tcb->consumer_index &&
2587 !test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags)) {
2588 acked = bnad_txcmpl_process(bnad, tcb);
2589 if (likely(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
2590 bna_ib_ack(tcb->i_dbell, acked);
2591 smp_mb__before_clear_bit();
2592 clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
2594 netif_stop_queue(netdev);
2595 BNAD_UPDATE_CTR(bnad, netif_queue_stop);
2600 * Check again to deal with race condition between
2601 * netif_stop_queue here, and netif_wake_queue in
2602 * interrupt handler which is not inside netif tx lock.
2605 (wis > BNA_QE_FREE_CNT(tcb, tcb->q_depth) ||
2606 vectors > BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth))) {
2607 BNAD_UPDATE_CTR(bnad, netif_queue_stop);
2608 return NETDEV_TX_BUSY;
2610 netif_wake_queue(netdev);
2611 BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
2615 unmap_prod = unmap_q->producer_index;
2618 txq_prod = tcb->producer_index;
2619 BNA_TXQ_QPGE_PTR_GET(txq_prod, tcb->sw_qpt, txqent, wi_range);
2620 txqent->hdr.wi.reserved = 0;
2621 txqent->hdr.wi.num_vectors = vectors;
2623 if (vlan_tx_tag_present(skb)) {
2624 vlan_tag = (u16) vlan_tx_tag_get(skb);
2625 flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
2627 if (test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags)) {
2629 (tcb->priority & 0x7) << 13 | (vlan_tag & 0x1fff);
2630 flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
2633 txqent->hdr.wi.vlan_tag = htons(vlan_tag);
2635 if (skb_is_gso(skb)) {
2636 gso_size = skb_shinfo(skb)->gso_size;
2638 if (unlikely(gso_size > netdev->mtu)) {
2640 BNAD_UPDATE_CTR(bnad, tx_skb_mss_too_long);
2641 return NETDEV_TX_OK;
2643 if (unlikely((gso_size + skb_transport_offset(skb) +
2644 tcp_hdrlen(skb)) >= skb->len)) {
2645 txqent->hdr.wi.opcode =
2646 __constant_htons(BNA_TXQ_WI_SEND);
2647 txqent->hdr.wi.lso_mss = 0;
2648 BNAD_UPDATE_CTR(bnad, tx_skb_tso_too_short);
2650 txqent->hdr.wi.opcode =
2651 __constant_htons(BNA_TXQ_WI_SEND_LSO);
2652 txqent->hdr.wi.lso_mss = htons(gso_size);
2655 err = bnad_tso_prepare(bnad, skb);
2656 if (unlikely(err)) {
2658 BNAD_UPDATE_CTR(bnad, tx_skb_tso_prepare);
2659 return NETDEV_TX_OK;
2661 flags |= (BNA_TXQ_WI_CF_IP_CKSUM | BNA_TXQ_WI_CF_TCP_CKSUM);
2662 txqent->hdr.wi.l4_hdr_size_n_offset =
2663 htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
2664 (tcp_hdrlen(skb) >> 2,
2665 skb_transport_offset(skb)));
2667 txqent->hdr.wi.opcode = __constant_htons(BNA_TXQ_WI_SEND);
2668 txqent->hdr.wi.lso_mss = 0;
2670 if (unlikely(skb->len > (netdev->mtu + ETH_HLEN))) {
2672 BNAD_UPDATE_CTR(bnad, tx_skb_non_tso_too_long);
2673 return NETDEV_TX_OK;
2676 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2679 if (skb->protocol == __constant_htons(ETH_P_IP))
2680 proto = ip_hdr(skb)->protocol;
2681 else if (skb->protocol ==
2682 __constant_htons(ETH_P_IPV6)) {
2683 /* nexthdr may not be TCP immediately. */
2684 proto = ipv6_hdr(skb)->nexthdr;
2686 if (proto == IPPROTO_TCP) {
2687 flags |= BNA_TXQ_WI_CF_TCP_CKSUM;
2688 txqent->hdr.wi.l4_hdr_size_n_offset =
2689 htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
2690 (0, skb_transport_offset(skb)));
2692 BNAD_UPDATE_CTR(bnad, tcpcsum_offload);
2694 if (unlikely(skb_headlen(skb) <
2695 skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2697 BNAD_UPDATE_CTR(bnad, tx_skb_tcp_hdr);
2698 return NETDEV_TX_OK;
2701 } else if (proto == IPPROTO_UDP) {
2702 flags |= BNA_TXQ_WI_CF_UDP_CKSUM;
2703 txqent->hdr.wi.l4_hdr_size_n_offset =
2704 htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
2705 (0, skb_transport_offset(skb)));
2707 BNAD_UPDATE_CTR(bnad, udpcsum_offload);
2708 if (unlikely(skb_headlen(skb) <
2709 skb_transport_offset(skb) +
2710 sizeof(struct udphdr))) {
2712 BNAD_UPDATE_CTR(bnad, tx_skb_udp_hdr);
2713 return NETDEV_TX_OK;
2717 BNAD_UPDATE_CTR(bnad, tx_skb_csum_err);
2718 return NETDEV_TX_OK;
2721 txqent->hdr.wi.l4_hdr_size_n_offset = 0;
2725 txqent->hdr.wi.flags = htons(flags);
2727 txqent->hdr.wi.frame_length = htonl(skb->len);
2729 unmap_q->unmap_array[unmap_prod].skb = skb;
2730 len = skb_headlen(skb);
2731 txqent->vector[0].length = htons(len);
2732 dma_addr = dma_map_single(&bnad->pcidev->dev, skb->data,
2733 skb_headlen(skb), DMA_TO_DEVICE);
2734 dma_unmap_addr_set(&unmap_q->unmap_array[unmap_prod], dma_addr,
2737 BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[0].host_addr);
2738 BNA_QE_INDX_ADD(unmap_prod, 1, unmap_q->q_depth);
2743 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2744 const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
2745 u16 size = skb_frag_size(frag);
2747 if (unlikely(size == 0)) {
2748 unmap_prod = unmap_q->producer_index;
2750 unmap_prod = bnad_pci_unmap_skb(&bnad->pcidev->dev,
2751 unmap_q->unmap_array,
2752 unmap_prod, unmap_q->q_depth, skb,
2755 BNAD_UPDATE_CTR(bnad, tx_skb_frag_zero);
2756 return NETDEV_TX_OK;
2761 if (++vect_id == BFI_TX_MAX_VECTORS_PER_WI) {
2766 BNA_QE_INDX_ADD(txq_prod, wis_used,
2769 BNA_TXQ_QPGE_PTR_GET(txq_prod, tcb->sw_qpt,
2773 txqent->hdr.wi_ext.opcode =
2774 __constant_htons(BNA_TXQ_WI_EXTENSION);
2777 BUG_ON(!(size <= BFI_TX_MAX_DATA_PER_VECTOR));
2778 txqent->vector[vect_id].length = htons(size);
2779 dma_addr = skb_frag_dma_map(&bnad->pcidev->dev, frag,
2780 0, size, DMA_TO_DEVICE);
2781 dma_unmap_addr_set(&unmap_q->unmap_array[unmap_prod], dma_addr,
2783 BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[vect_id].host_addr);
2784 BNA_QE_INDX_ADD(unmap_prod, 1, unmap_q->q_depth);
2787 if (unlikely(len != skb->len)) {
2788 unmap_prod = unmap_q->producer_index;
2790 unmap_prod = bnad_pci_unmap_skb(&bnad->pcidev->dev,
2791 unmap_q->unmap_array, unmap_prod,
2792 unmap_q->q_depth, skb,
2793 skb_shinfo(skb)->nr_frags);
2795 BNAD_UPDATE_CTR(bnad, tx_skb_len_mismatch);
2796 return NETDEV_TX_OK;
2799 unmap_q->producer_index = unmap_prod;
2800 BNA_QE_INDX_ADD(txq_prod, wis_used, tcb->q_depth);
2801 tcb->producer_index = txq_prod;
2805 if (unlikely(!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
2806 return NETDEV_TX_OK;
2808 bna_txq_prod_indx_doorbell(tcb);
2811 return NETDEV_TX_OK;
2815 * Used spin_lock to synchronize reading of stats structures, which
2816 * is written by BNA under the same lock.
2818 static struct rtnl_link_stats64 *
2819 bnad_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
2821 struct bnad *bnad = netdev_priv(netdev);
2822 unsigned long flags;
2824 spin_lock_irqsave(&bnad->bna_lock, flags);
2826 bnad_netdev_qstats_fill(bnad, stats);
2827 bnad_netdev_hwstats_fill(bnad, stats);
2829 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2835 bnad_set_rx_mode(struct net_device *netdev)
2837 struct bnad *bnad = netdev_priv(netdev);
2838 u32 new_mask, valid_mask;
2839 unsigned long flags;
2841 spin_lock_irqsave(&bnad->bna_lock, flags);
2843 new_mask = valid_mask = 0;
2845 if (netdev->flags & IFF_PROMISC) {
2846 if (!(bnad->cfg_flags & BNAD_CF_PROMISC)) {
2847 new_mask = BNAD_RXMODE_PROMISC_DEFAULT;
2848 valid_mask = BNAD_RXMODE_PROMISC_DEFAULT;
2849 bnad->cfg_flags |= BNAD_CF_PROMISC;
2852 if (bnad->cfg_flags & BNAD_CF_PROMISC) {
2853 new_mask = ~BNAD_RXMODE_PROMISC_DEFAULT;
2854 valid_mask = BNAD_RXMODE_PROMISC_DEFAULT;
2855 bnad->cfg_flags &= ~BNAD_CF_PROMISC;
2859 if (netdev->flags & IFF_ALLMULTI) {
2860 if (!(bnad->cfg_flags & BNAD_CF_ALLMULTI)) {
2861 new_mask |= BNA_RXMODE_ALLMULTI;
2862 valid_mask |= BNA_RXMODE_ALLMULTI;
2863 bnad->cfg_flags |= BNAD_CF_ALLMULTI;
2866 if (bnad->cfg_flags & BNAD_CF_ALLMULTI) {
2867 new_mask &= ~BNA_RXMODE_ALLMULTI;
2868 valid_mask |= BNA_RXMODE_ALLMULTI;
2869 bnad->cfg_flags &= ~BNAD_CF_ALLMULTI;
2873 if (bnad->rx_info[0].rx == NULL)
2876 bna_rx_mode_set(bnad->rx_info[0].rx, new_mask, valid_mask, NULL);
2878 if (!netdev_mc_empty(netdev)) {
2880 int mc_count = netdev_mc_count(netdev);
2882 /* Index 0 holds the broadcast address */
2884 kzalloc((mc_count + 1) * ETH_ALEN,
2889 memcpy(&mcaddr_list[0], &bnad_bcast_addr[0], ETH_ALEN);
2891 /* Copy rest of the MC addresses */
2892 bnad_netdev_mc_list_get(netdev, mcaddr_list);
2894 bna_rx_mcast_listset(bnad->rx_info[0].rx, mc_count + 1,
2897 /* Should we enable BNAD_CF_ALLMULTI for err != 0 ? */
2901 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2905 * bna_lock is used to sync writes to netdev->addr
2906 * conf_lock cannot be used since this call may be made
2907 * in a non-blocking context.
2910 bnad_set_mac_address(struct net_device *netdev, void *mac_addr)
2913 struct bnad *bnad = netdev_priv(netdev);
2914 struct sockaddr *sa = (struct sockaddr *)mac_addr;
2915 unsigned long flags;
2917 spin_lock_irqsave(&bnad->bna_lock, flags);
2919 err = bnad_mac_addr_set_locked(bnad, sa->sa_data);
2922 memcpy(netdev->dev_addr, sa->sa_data, netdev->addr_len);
2924 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2930 bnad_mtu_set(struct bnad *bnad, int mtu)
2932 unsigned long flags;
2934 init_completion(&bnad->bnad_completions.mtu_comp);
2936 spin_lock_irqsave(&bnad->bna_lock, flags);
2937 bna_enet_mtu_set(&bnad->bna.enet, mtu, bnad_cb_enet_mtu_set);
2938 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2940 wait_for_completion(&bnad->bnad_completions.mtu_comp);
2942 return bnad->bnad_completions.mtu_comp_status;
2946 bnad_change_mtu(struct net_device *netdev, int new_mtu)
2948 int err, mtu = netdev->mtu;
2949 struct bnad *bnad = netdev_priv(netdev);
2951 if (new_mtu + ETH_HLEN < ETH_ZLEN || new_mtu > BNAD_JUMBO_MTU)
2954 mutex_lock(&bnad->conf_mutex);
2956 netdev->mtu = new_mtu;
2958 mtu = ETH_HLEN + VLAN_HLEN + new_mtu + ETH_FCS_LEN;
2959 err = bnad_mtu_set(bnad, mtu);
2963 mutex_unlock(&bnad->conf_mutex);
2968 bnad_vlan_rx_add_vid(struct net_device *netdev,
2971 struct bnad *bnad = netdev_priv(netdev);
2972 unsigned long flags;
2974 if (!bnad->rx_info[0].rx)
2977 mutex_lock(&bnad->conf_mutex);
2979 spin_lock_irqsave(&bnad->bna_lock, flags);
2980 bna_rx_vlan_add(bnad->rx_info[0].rx, vid);
2981 set_bit(vid, bnad->active_vlans);
2982 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2984 mutex_unlock(&bnad->conf_mutex);
2990 bnad_vlan_rx_kill_vid(struct net_device *netdev,
2993 struct bnad *bnad = netdev_priv(netdev);
2994 unsigned long flags;
2996 if (!bnad->rx_info[0].rx)
2999 mutex_lock(&bnad->conf_mutex);
3001 spin_lock_irqsave(&bnad->bna_lock, flags);
3002 clear_bit(vid, bnad->active_vlans);
3003 bna_rx_vlan_del(bnad->rx_info[0].rx, vid);
3004 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3006 mutex_unlock(&bnad->conf_mutex);
3011 #ifdef CONFIG_NET_POLL_CONTROLLER
3013 bnad_netpoll(struct net_device *netdev)
3015 struct bnad *bnad = netdev_priv(netdev);
3016 struct bnad_rx_info *rx_info;
3017 struct bnad_rx_ctrl *rx_ctrl;
3021 if (!(bnad->cfg_flags & BNAD_CF_MSIX)) {
3022 bna_intx_disable(&bnad->bna, curr_mask);
3023 bnad_isr(bnad->pcidev->irq, netdev);
3024 bna_intx_enable(&bnad->bna, curr_mask);
3027 * Tx processing may happen in sending context, so no need
3028 * to explicitly process completions here
3032 for (i = 0; i < bnad->num_rx; i++) {
3033 rx_info = &bnad->rx_info[i];
3036 for (j = 0; j < bnad->num_rxp_per_rx; j++) {
3037 rx_ctrl = &rx_info->rx_ctrl[j];
3039 bnad_netif_rx_schedule_poll(bnad,
3047 static const struct net_device_ops bnad_netdev_ops = {
3048 .ndo_open = bnad_open,
3049 .ndo_stop = bnad_stop,
3050 .ndo_start_xmit = bnad_start_xmit,
3051 .ndo_get_stats64 = bnad_get_stats64,
3052 .ndo_set_rx_mode = bnad_set_rx_mode,
3053 .ndo_validate_addr = eth_validate_addr,
3054 .ndo_set_mac_address = bnad_set_mac_address,
3055 .ndo_change_mtu = bnad_change_mtu,
3056 .ndo_vlan_rx_add_vid = bnad_vlan_rx_add_vid,
3057 .ndo_vlan_rx_kill_vid = bnad_vlan_rx_kill_vid,
3058 #ifdef CONFIG_NET_POLL_CONTROLLER
3059 .ndo_poll_controller = bnad_netpoll
3064 bnad_netdev_init(struct bnad *bnad, bool using_dac)
3066 struct net_device *netdev = bnad->netdev;
3068 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
3069 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3070 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_HW_VLAN_TX;
3072 netdev->vlan_features = NETIF_F_SG | NETIF_F_HIGHDMA |
3073 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3074 NETIF_F_TSO | NETIF_F_TSO6;
3076 netdev->features |= netdev->hw_features |
3077 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
3080 netdev->features |= NETIF_F_HIGHDMA;
3082 netdev->mem_start = bnad->mmio_start;
3083 netdev->mem_end = bnad->mmio_start + bnad->mmio_len - 1;
3085 netdev->netdev_ops = &bnad_netdev_ops;
3086 bnad_set_ethtool_ops(netdev);
3090 * 1. Initialize the bnad structure
3091 * 2. Setup netdev pointer in pci_dev
3092 * 3. Initialize no. of TxQ & CQs & MSIX vectors
3093 * 4. Initialize work queue.
3096 bnad_init(struct bnad *bnad,
3097 struct pci_dev *pdev, struct net_device *netdev)
3099 unsigned long flags;
3101 SET_NETDEV_DEV(netdev, &pdev->dev);
3102 pci_set_drvdata(pdev, netdev);
3104 bnad->netdev = netdev;
3105 bnad->pcidev = pdev;
3106 bnad->mmio_start = pci_resource_start(pdev, 0);
3107 bnad->mmio_len = pci_resource_len(pdev, 0);
3108 bnad->bar0 = ioremap_nocache(bnad->mmio_start, bnad->mmio_len);
3110 dev_err(&pdev->dev, "ioremap for bar0 failed\n");
3111 pci_set_drvdata(pdev, NULL);
3114 pr_info("bar0 mapped to %p, len %llu\n", bnad->bar0,
3115 (unsigned long long) bnad->mmio_len);
3117 spin_lock_irqsave(&bnad->bna_lock, flags);
3118 if (!bnad_msix_disable)
3119 bnad->cfg_flags = BNAD_CF_MSIX;
3121 bnad->cfg_flags |= BNAD_CF_DIM_ENABLED;
3123 bnad_q_num_init(bnad);
3124 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3126 bnad->msix_num = (bnad->num_tx * bnad->num_txq_per_tx) +
3127 (bnad->num_rx * bnad->num_rxp_per_rx) +
3128 BNAD_MAILBOX_MSIX_VECTORS;
3130 bnad->txq_depth = BNAD_TXQ_DEPTH;
3131 bnad->rxq_depth = BNAD_RXQ_DEPTH;
3133 bnad->tx_coalescing_timeo = BFI_TX_COALESCING_TIMEO;
3134 bnad->rx_coalescing_timeo = BFI_RX_COALESCING_TIMEO;
3136 sprintf(bnad->wq_name, "%s_wq_%d", BNAD_NAME, bnad->id);
3137 bnad->work_q = create_singlethread_workqueue(bnad->wq_name);
3146 * Must be called after bnad_pci_uninit()
3147 * so that iounmap() and pci_set_drvdata(NULL)
3148 * happens only after PCI uninitialization.
3151 bnad_uninit(struct bnad *bnad)
3154 flush_workqueue(bnad->work_q);
3155 destroy_workqueue(bnad->work_q);
3156 bnad->work_q = NULL;
3160 iounmap(bnad->bar0);
3161 pci_set_drvdata(bnad->pcidev, NULL);
3166 a) Per ioceth mutes used for serializing configuration
3167 changes from OS interface
3168 b) spin lock used to protect bna state machine
3171 bnad_lock_init(struct bnad *bnad)
3173 spin_lock_init(&bnad->bna_lock);
3174 mutex_init(&bnad->conf_mutex);
3175 mutex_init(&bnad_list_mutex);
3179 bnad_lock_uninit(struct bnad *bnad)
3181 mutex_destroy(&bnad->conf_mutex);
3182 mutex_destroy(&bnad_list_mutex);
3185 /* PCI Initialization */
3187 bnad_pci_init(struct bnad *bnad,
3188 struct pci_dev *pdev, bool *using_dac)
3192 err = pci_enable_device(pdev);
3195 err = pci_request_regions(pdev, BNAD_NAME);
3197 goto disable_device;
3198 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
3199 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
3202 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3204 err = dma_set_coherent_mask(&pdev->dev,
3207 goto release_regions;
3211 pci_set_master(pdev);
3215 pci_release_regions(pdev);
3217 pci_disable_device(pdev);
3223 bnad_pci_uninit(struct pci_dev *pdev)
3225 pci_release_regions(pdev);
3226 pci_disable_device(pdev);
3229 static int __devinit
3230 bnad_pci_probe(struct pci_dev *pdev,
3231 const struct pci_device_id *pcidev_id)
3237 struct net_device *netdev;
3238 struct bfa_pcidev pcidev_info;
3239 unsigned long flags;
3241 pr_info("bnad_pci_probe : (0x%p, 0x%p) PCI Func : (%d)\n",
3242 pdev, pcidev_id, PCI_FUNC(pdev->devfn));
3244 mutex_lock(&bnad_fwimg_mutex);
3245 if (!cna_get_firmware_buf(pdev)) {
3246 mutex_unlock(&bnad_fwimg_mutex);
3247 pr_warn("Failed to load Firmware Image!\n");
3250 mutex_unlock(&bnad_fwimg_mutex);
3253 * Allocates sizeof(struct net_device + struct bnad)
3254 * bnad = netdev->priv
3256 netdev = alloc_etherdev(sizeof(struct bnad));
3261 bnad = netdev_priv(netdev);
3262 bnad_lock_init(bnad);
3263 bnad_add_to_list(bnad);
3265 mutex_lock(&bnad->conf_mutex);
3267 * PCI initialization
3268 * Output : using_dac = 1 for 64 bit DMA
3269 * = 0 for 32 bit DMA
3271 err = bnad_pci_init(bnad, pdev, &using_dac);
3276 * Initialize bnad structure
3277 * Setup relation between pci_dev & netdev
3279 err = bnad_init(bnad, pdev, netdev);
3283 /* Initialize netdev structure, set up ethtool ops */
3284 bnad_netdev_init(bnad, using_dac);
3286 /* Set link to down state */
3287 netif_carrier_off(netdev);
3289 /* Setup the debugfs node for this bfad */
3290 if (bna_debugfs_enable)
3291 bnad_debugfs_init(bnad);
3293 /* Get resource requirement form bna */
3294 spin_lock_irqsave(&bnad->bna_lock, flags);
3295 bna_res_req(&bnad->res_info[0]);
3296 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3298 /* Allocate resources from bna */
3299 err = bnad_res_alloc(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
3305 /* Setup pcidev_info for bna_init() */
3306 pcidev_info.pci_slot = PCI_SLOT(bnad->pcidev->devfn);
3307 pcidev_info.pci_func = PCI_FUNC(bnad->pcidev->devfn);
3308 pcidev_info.device_id = bnad->pcidev->device;
3309 pcidev_info.pci_bar_kva = bnad->bar0;
3311 spin_lock_irqsave(&bnad->bna_lock, flags);
3312 bna_init(bna, bnad, &pcidev_info, &bnad->res_info[0]);
3313 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3315 bnad->stats.bna_stats = &bna->stats;
3317 bnad_enable_msix(bnad);
3318 err = bnad_mbox_irq_alloc(bnad);
3324 setup_timer(&bnad->bna.ioceth.ioc.ioc_timer, bnad_ioc_timeout,
3325 ((unsigned long)bnad));
3326 setup_timer(&bnad->bna.ioceth.ioc.hb_timer, bnad_ioc_hb_check,
3327 ((unsigned long)bnad));
3328 setup_timer(&bnad->bna.ioceth.ioc.iocpf_timer, bnad_iocpf_timeout,
3329 ((unsigned long)bnad));
3330 setup_timer(&bnad->bna.ioceth.ioc.sem_timer, bnad_iocpf_sem_timeout,
3331 ((unsigned long)bnad));
3333 /* Now start the timer before calling IOC */
3334 mod_timer(&bnad->bna.ioceth.ioc.iocpf_timer,
3335 jiffies + msecs_to_jiffies(BNA_IOC_TIMER_FREQ));
3339 * If the call back comes with error, we bail out.
3340 * This is a catastrophic error.
3342 err = bnad_ioceth_enable(bnad);
3344 pr_err("BNA: Initialization failed err=%d\n",
3349 spin_lock_irqsave(&bnad->bna_lock, flags);
3350 if (bna_num_txq_set(bna, BNAD_NUM_TXQ + 1) ||
3351 bna_num_rxp_set(bna, BNAD_NUM_RXP + 1)) {
3352 bnad_q_num_adjust(bnad, bna_attr(bna)->num_txq - 1,
3353 bna_attr(bna)->num_rxp - 1);
3354 if (bna_num_txq_set(bna, BNAD_NUM_TXQ + 1) ||
3355 bna_num_rxp_set(bna, BNAD_NUM_RXP + 1))
3358 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3360 goto disable_ioceth;
3362 spin_lock_irqsave(&bnad->bna_lock, flags);
3363 bna_mod_res_req(&bnad->bna, &bnad->mod_res_info[0]);
3364 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3366 err = bnad_res_alloc(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
3369 goto disable_ioceth;
3372 spin_lock_irqsave(&bnad->bna_lock, flags);
3373 bna_mod_init(&bnad->bna, &bnad->mod_res_info[0]);
3374 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3376 /* Get the burnt-in mac */
3377 spin_lock_irqsave(&bnad->bna_lock, flags);
3378 bna_enet_perm_mac_get(&bna->enet, &bnad->perm_addr);
3379 bnad_set_netdev_perm_addr(bnad);
3380 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3382 mutex_unlock(&bnad->conf_mutex);
3384 /* Finally, reguister with net_device layer */
3385 err = register_netdev(netdev);
3387 pr_err("BNA : Registering with netdev failed\n");
3390 set_bit(BNAD_RF_NETDEV_REGISTERED, &bnad->run_flags);
3395 mutex_unlock(&bnad->conf_mutex);
3399 mutex_lock(&bnad->conf_mutex);
3400 bnad_res_free(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
3402 bnad_ioceth_disable(bnad);
3403 del_timer_sync(&bnad->bna.ioceth.ioc.ioc_timer);
3404 del_timer_sync(&bnad->bna.ioceth.ioc.sem_timer);
3405 del_timer_sync(&bnad->bna.ioceth.ioc.hb_timer);
3406 spin_lock_irqsave(&bnad->bna_lock, flags);
3408 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3409 bnad_mbox_irq_free(bnad);
3410 bnad_disable_msix(bnad);
3412 bnad_res_free(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
3414 /* Remove the debugfs node for this bnad */
3415 kfree(bnad->regdata);
3416 bnad_debugfs_uninit(bnad);
3419 bnad_pci_uninit(pdev);
3421 mutex_unlock(&bnad->conf_mutex);
3422 bnad_remove_from_list(bnad);
3423 bnad_lock_uninit(bnad);
3424 free_netdev(netdev);
3428 static void __devexit
3429 bnad_pci_remove(struct pci_dev *pdev)
3431 struct net_device *netdev = pci_get_drvdata(pdev);
3434 unsigned long flags;
3439 pr_info("%s bnad_pci_remove\n", netdev->name);
3440 bnad = netdev_priv(netdev);
3443 if (test_and_clear_bit(BNAD_RF_NETDEV_REGISTERED, &bnad->run_flags))
3444 unregister_netdev(netdev);
3446 mutex_lock(&bnad->conf_mutex);
3447 bnad_ioceth_disable(bnad);
3448 del_timer_sync(&bnad->bna.ioceth.ioc.ioc_timer);
3449 del_timer_sync(&bnad->bna.ioceth.ioc.sem_timer);
3450 del_timer_sync(&bnad->bna.ioceth.ioc.hb_timer);
3451 spin_lock_irqsave(&bnad->bna_lock, flags);
3453 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3455 bnad_res_free(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
3456 bnad_res_free(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
3457 bnad_mbox_irq_free(bnad);
3458 bnad_disable_msix(bnad);
3459 bnad_pci_uninit(pdev);
3460 mutex_unlock(&bnad->conf_mutex);
3461 bnad_remove_from_list(bnad);
3462 bnad_lock_uninit(bnad);
3463 /* Remove the debugfs node for this bnad */
3464 kfree(bnad->regdata);
3465 bnad_debugfs_uninit(bnad);
3467 free_netdev(netdev);
3470 static DEFINE_PCI_DEVICE_TABLE(bnad_pci_id_table) = {
3472 PCI_DEVICE(PCI_VENDOR_ID_BROCADE,
3473 PCI_DEVICE_ID_BROCADE_CT),
3474 .class = PCI_CLASS_NETWORK_ETHERNET << 8,
3475 .class_mask = 0xffff00
3478 PCI_DEVICE(PCI_VENDOR_ID_BROCADE,
3479 BFA_PCI_DEVICE_ID_CT2),
3480 .class = PCI_CLASS_NETWORK_ETHERNET << 8,
3481 .class_mask = 0xffff00
3486 MODULE_DEVICE_TABLE(pci, bnad_pci_id_table);
3488 static struct pci_driver bnad_pci_driver = {
3490 .id_table = bnad_pci_id_table,
3491 .probe = bnad_pci_probe,
3492 .remove = __devexit_p(bnad_pci_remove),
3496 bnad_module_init(void)
3500 pr_info("Brocade 10G Ethernet driver - version: %s\n",
3503 bfa_nw_ioc_auto_recover(bnad_ioc_auto_recover);
3505 err = pci_register_driver(&bnad_pci_driver);
3507 pr_err("bna : PCI registration failed in module init "
3516 bnad_module_exit(void)
3518 pci_unregister_driver(&bnad_pci_driver);
3519 release_firmware(bfi_fw);
3522 module_init(bnad_module_init);
3523 module_exit(bnad_module_exit);
3525 MODULE_AUTHOR("Brocade");
3526 MODULE_LICENSE("GPL");
3527 MODULE_DESCRIPTION("Brocade 10G PCIe Ethernet driver");
3528 MODULE_VERSION(BNAD_VERSION);
3529 MODULE_FIRMWARE(CNA_FW_FILE_CT);
3530 MODULE_FIRMWARE(CNA_FW_FILE_CT2);