blob: 32df3a8bf59338265c288ff1eab4af63d5ee89b4 [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 */
Jiri Pirkof859d7c2011-07-20 04:54:14 +000018#include <linux/bitops.h>
Rasesh Mody8b230ed2010-08-23 20:24:12 -070019#include <linux/netdevice.h>
20#include <linux/skbuff.h>
21#include <linux/etherdevice.h>
22#include <linux/in.h>
23#include <linux/ethtool.h>
24#include <linux/if_vlan.h>
25#include <linux/if_ether.h>
26#include <linux/ip.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040027#include <linux/prefetch.h>
Rasesh Mody8b230ed2010-08-23 20:24:12 -070028
29#include "bnad.h"
30#include "bna.h"
31#include "cna.h"
32
Rasesh Modyb7ee31c52010-10-05 15:46:05 +000033static DEFINE_MUTEX(bnad_fwimg_mutex);
Rasesh Mody8b230ed2010-08-23 20:24:12 -070034
35/*
36 * Module params
37 */
38static uint bnad_msix_disable;
39module_param(bnad_msix_disable, uint, 0444);
40MODULE_PARM_DESC(bnad_msix_disable, "Disable MSIX mode");
41
42static uint bnad_ioc_auto_recover = 1;
43module_param(bnad_ioc_auto_recover, uint, 0444);
44MODULE_PARM_DESC(bnad_ioc_auto_recover, "Enable / Disable auto recovery");
45
46/*
47 * Global variables
48 */
49u32 bnad_rxqs_per_cq = 2;
50
Rasesh Modyb7ee31c52010-10-05 15:46:05 +000051static const u8 bnad_bcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
Rasesh Mody8b230ed2010-08-23 20:24:12 -070052
53/*
54 * Local MACROS
55 */
56#define BNAD_TX_UNMAPQ_DEPTH (bnad->txq_depth * 2)
57
58#define BNAD_RX_UNMAPQ_DEPTH (bnad->rxq_depth)
59
60#define BNAD_GET_MBOX_IRQ(_bnad) \
61 (((_bnad)->cfg_flags & BNAD_CF_MSIX) ? \
Rasesh Mody8811e262011-07-22 08:07:44 +000062 ((_bnad)->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector) : \
Rasesh Mody8b230ed2010-08-23 20:24:12 -070063 ((_bnad)->pcidev->irq))
64
65#define BNAD_FILL_UNMAPQ_MEM_REQ(_res_info, _num, _depth) \
66do { \
67 (_res_info)->res_type = BNA_RES_T_MEM; \
68 (_res_info)->res_u.mem_info.mem_type = BNA_MEM_T_KVA; \
69 (_res_info)->res_u.mem_info.num = (_num); \
70 (_res_info)->res_u.mem_info.len = \
71 sizeof(struct bnad_unmap_q) + \
72 (sizeof(struct bnad_skb_unmap) * ((_depth) - 1)); \
73} while (0)
74
Rasesh Modybe7fa322010-12-23 21:45:01 +000075#define BNAD_TXRX_SYNC_MDELAY 250 /* 250 msecs */
76
Rasesh Mody8b230ed2010-08-23 20:24:12 -070077/*
78 * Reinitialize completions in CQ, once Rx is taken down
79 */
80static void
81bnad_cq_cmpl_init(struct bnad *bnad, struct bna_ccb *ccb)
82{
83 struct bna_cq_entry *cmpl, *next_cmpl;
84 unsigned int wi_range, wis = 0, ccb_prod = 0;
85 int i;
86
87 BNA_CQ_QPGE_PTR_GET(ccb_prod, ccb->sw_qpt, cmpl,
88 wi_range);
89
90 for (i = 0; i < ccb->q_depth; i++) {
91 wis++;
92 if (likely(--wi_range))
93 next_cmpl = cmpl + 1;
94 else {
95 BNA_QE_INDX_ADD(ccb_prod, wis, ccb->q_depth);
96 wis = 0;
97 BNA_CQ_QPGE_PTR_GET(ccb_prod, ccb->sw_qpt,
98 next_cmpl, wi_range);
99 }
100 cmpl->valid = 0;
101 cmpl = next_cmpl;
102 }
103}
104
Rasesh Mody271e8b72011-08-30 15:27:40 +0000105static u32
106bnad_pci_unmap_skb(struct device *pdev, struct bnad_skb_unmap *array,
107 u32 index, u32 depth, struct sk_buff *skb, u32 frag)
108{
109 int j;
110 array[index].skb = NULL;
111
112 dma_unmap_single(pdev, dma_unmap_addr(&array[index], dma_addr),
113 skb_headlen(skb), DMA_TO_DEVICE);
114 dma_unmap_addr_set(&array[index], dma_addr, 0);
115 BNA_QE_INDX_ADD(index, 1, depth);
116
117 for (j = 0; j < frag; j++) {
118 dma_unmap_page(pdev, dma_unmap_addr(&array[index], dma_addr),
119 skb_shinfo(skb)->frags[j].size, DMA_TO_DEVICE);
120 dma_unmap_addr_set(&array[index], dma_addr, 0);
121 BNA_QE_INDX_ADD(index, 1, depth);
122 }
123
124 return index;
125}
126
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700127/*
128 * Frees all pending Tx Bufs
129 * At this point no activity is expected on the Q,
130 * so DMA unmap & freeing is fine.
131 */
132static void
133bnad_free_all_txbufs(struct bnad *bnad,
134 struct bna_tcb *tcb)
135{
Rasesh Mody0120b992011-07-22 08:07:41 +0000136 u32 unmap_cons;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700137 struct bnad_unmap_q *unmap_q = tcb->unmap_q;
138 struct bnad_skb_unmap *unmap_array;
Rasesh Mody0120b992011-07-22 08:07:41 +0000139 struct sk_buff *skb = NULL;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700140 int i;
141
142 unmap_array = unmap_q->unmap_array;
143
144 unmap_cons = 0;
145 while (unmap_cons < unmap_q->q_depth) {
146 skb = unmap_array[unmap_cons].skb;
147 if (!skb) {
148 unmap_cons++;
149 continue;
150 }
151 unmap_array[unmap_cons].skb = NULL;
152
Ivan Vecera5ea74312011-02-02 04:37:02 +0000153 dma_unmap_single(&bnad->pcidev->dev,
154 dma_unmap_addr(&unmap_array[unmap_cons],
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700155 dma_addr), skb_headlen(skb),
Ivan Vecera5ea74312011-02-02 04:37:02 +0000156 DMA_TO_DEVICE);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700157
Ivan Vecera5ea74312011-02-02 04:37:02 +0000158 dma_unmap_addr_set(&unmap_array[unmap_cons], dma_addr, 0);
Rasesh Modybe7fa322010-12-23 21:45:01 +0000159 if (++unmap_cons >= unmap_q->q_depth)
160 break;
161
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700162 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Ivan Vecera5ea74312011-02-02 04:37:02 +0000163 dma_unmap_page(&bnad->pcidev->dev,
164 dma_unmap_addr(&unmap_array[unmap_cons],
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700165 dma_addr),
166 skb_shinfo(skb)->frags[i].size,
Ivan Vecera5ea74312011-02-02 04:37:02 +0000167 DMA_TO_DEVICE);
168 dma_unmap_addr_set(&unmap_array[unmap_cons], dma_addr,
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700169 0);
Rasesh Modybe7fa322010-12-23 21:45:01 +0000170 if (++unmap_cons >= unmap_q->q_depth)
171 break;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700172 }
173 dev_kfree_skb_any(skb);
174 }
175}
176
177/* Data Path Handlers */
178
179/*
180 * bnad_free_txbufs : Frees the Tx bufs on Tx completion
181 * Can be called in a) Interrupt context
182 * b) Sending context
183 * c) Tasklet context
184 */
185static u32
186bnad_free_txbufs(struct bnad *bnad,
187 struct bna_tcb *tcb)
188{
Rasesh Mody271e8b72011-08-30 15:27:40 +0000189 u32 unmap_cons, sent_packets = 0, sent_bytes = 0;
190 u16 wis, updated_hw_cons;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700191 struct bnad_unmap_q *unmap_q = tcb->unmap_q;
192 struct bnad_skb_unmap *unmap_array;
Rasesh Mody0120b992011-07-22 08:07:41 +0000193 struct sk_buff *skb;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700194
195 /*
196 * Just return if TX is stopped. This check is useful
197 * when bnad_free_txbufs() runs out of a tasklet scheduled
Rasesh Modybe7fa322010-12-23 21:45:01 +0000198 * before bnad_cb_tx_cleanup() cleared BNAD_TXQ_TX_STARTED bit
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700199 * but this routine runs actually after the cleanup has been
200 * executed.
201 */
Rasesh Modybe7fa322010-12-23 21:45:01 +0000202 if (!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700203 return 0;
204
205 updated_hw_cons = *(tcb->hw_consumer_index);
206
207 wis = BNA_Q_INDEX_CHANGE(tcb->consumer_index,
208 updated_hw_cons, tcb->q_depth);
209
210 BUG_ON(!(wis <= BNA_QE_IN_USE_CNT(tcb, tcb->q_depth)));
211
212 unmap_array = unmap_q->unmap_array;
213 unmap_cons = unmap_q->consumer_index;
214
215 prefetch(&unmap_array[unmap_cons + 1]);
216 while (wis) {
217 skb = unmap_array[unmap_cons].skb;
218
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700219 sent_packets++;
220 sent_bytes += skb->len;
221 wis -= BNA_TXQ_WI_NEEDED(1 + skb_shinfo(skb)->nr_frags);
222
Rasesh Mody271e8b72011-08-30 15:27:40 +0000223 unmap_cons = bnad_pci_unmap_skb(&bnad->pcidev->dev, unmap_array,
224 unmap_cons, unmap_q->q_depth, skb,
225 skb_shinfo(skb)->nr_frags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700226
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700227 dev_kfree_skb_any(skb);
228 }
229
230 /* Update consumer pointers. */
231 tcb->consumer_index = updated_hw_cons;
232 unmap_q->consumer_index = unmap_cons;
233
234 tcb->txq->tx_packets += sent_packets;
235 tcb->txq->tx_bytes += sent_bytes;
236
237 return sent_packets;
238}
239
240/* Tx Free Tasklet function */
241/* Frees for all the tcb's in all the Tx's */
242/*
243 * Scheduled from sending context, so that
244 * the fat Tx lock is not held for too long
245 * in the sending context.
246 */
247static void
248bnad_tx_free_tasklet(unsigned long bnad_ptr)
249{
250 struct bnad *bnad = (struct bnad *)bnad_ptr;
251 struct bna_tcb *tcb;
Rasesh Mody0120b992011-07-22 08:07:41 +0000252 u32 acked = 0;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700253 int i, j;
254
255 for (i = 0; i < bnad->num_tx; i++) {
256 for (j = 0; j < bnad->num_txq_per_tx; j++) {
257 tcb = bnad->tx_info[i].tcb[j];
258 if (!tcb)
259 continue;
260 if (((u16) (*tcb->hw_consumer_index) !=
261 tcb->consumer_index) &&
262 (!test_and_set_bit(BNAD_TXQ_FREE_SENT,
263 &tcb->flags))) {
264 acked = bnad_free_txbufs(bnad, tcb);
Rasesh Modybe7fa322010-12-23 21:45:01 +0000265 if (likely(test_bit(BNAD_TXQ_TX_STARTED,
266 &tcb->flags)))
267 bna_ib_ack(tcb->i_dbell, acked);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700268 smp_mb__before_clear_bit();
269 clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
270 }
Rasesh Modyf7c0fa42010-12-23 21:45:05 +0000271 if (unlikely(!test_bit(BNAD_TXQ_TX_STARTED,
272 &tcb->flags)))
273 continue;
274 if (netif_queue_stopped(bnad->netdev)) {
275 if (acked && netif_carrier_ok(bnad->netdev) &&
276 BNA_QE_FREE_CNT(tcb, tcb->q_depth) >=
277 BNAD_NETIF_WAKE_THRESHOLD) {
278 netif_wake_queue(bnad->netdev);
279 /* TODO */
280 /* Counters for individual TxQs? */
281 BNAD_UPDATE_CTR(bnad,
282 netif_queue_wakeup);
283 }
284 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700285 }
286 }
287}
288
289static u32
290bnad_tx(struct bnad *bnad, struct bna_tcb *tcb)
291{
292 struct net_device *netdev = bnad->netdev;
Rasesh Modybe7fa322010-12-23 21:45:01 +0000293 u32 sent = 0;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700294
295 if (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags))
296 return 0;
297
298 sent = bnad_free_txbufs(bnad, tcb);
299 if (sent) {
300 if (netif_queue_stopped(netdev) &&
301 netif_carrier_ok(netdev) &&
302 BNA_QE_FREE_CNT(tcb, tcb->q_depth) >=
303 BNAD_NETIF_WAKE_THRESHOLD) {
Rasesh Modybe7fa322010-12-23 21:45:01 +0000304 if (test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)) {
305 netif_wake_queue(netdev);
306 BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
307 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700308 }
Rasesh Modybe7fa322010-12-23 21:45:01 +0000309 }
310
311 if (likely(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700312 bna_ib_ack(tcb->i_dbell, sent);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700313
314 smp_mb__before_clear_bit();
315 clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
316
317 return sent;
318}
319
320/* MSIX Tx Completion Handler */
321static irqreturn_t
322bnad_msix_tx(int irq, void *data)
323{
324 struct bna_tcb *tcb = (struct bna_tcb *)data;
325 struct bnad *bnad = tcb->bnad;
326
327 bnad_tx(bnad, tcb);
328
329 return IRQ_HANDLED;
330}
331
332static void
333bnad_reset_rcb(struct bnad *bnad, struct bna_rcb *rcb)
334{
335 struct bnad_unmap_q *unmap_q = rcb->unmap_q;
336
337 rcb->producer_index = 0;
338 rcb->consumer_index = 0;
339
340 unmap_q->producer_index = 0;
341 unmap_q->consumer_index = 0;
342}
343
344static void
Rasesh Modybe7fa322010-12-23 21:45:01 +0000345bnad_free_all_rxbufs(struct bnad *bnad, struct bna_rcb *rcb)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700346{
347 struct bnad_unmap_q *unmap_q;
Ivan Vecera5ea74312011-02-02 04:37:02 +0000348 struct bnad_skb_unmap *unmap_array;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700349 struct sk_buff *skb;
Rasesh Modybe7fa322010-12-23 21:45:01 +0000350 int unmap_cons;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700351
352 unmap_q = rcb->unmap_q;
Ivan Vecera5ea74312011-02-02 04:37:02 +0000353 unmap_array = unmap_q->unmap_array;
Rasesh Modybe7fa322010-12-23 21:45:01 +0000354 for (unmap_cons = 0; unmap_cons < unmap_q->q_depth; unmap_cons++) {
Ivan Vecera5ea74312011-02-02 04:37:02 +0000355 skb = unmap_array[unmap_cons].skb;
Rasesh Modybe7fa322010-12-23 21:45:01 +0000356 if (!skb)
357 continue;
Ivan Vecera5ea74312011-02-02 04:37:02 +0000358 unmap_array[unmap_cons].skb = NULL;
359 dma_unmap_single(&bnad->pcidev->dev,
360 dma_unmap_addr(&unmap_array[unmap_cons],
361 dma_addr),
362 rcb->rxq->buffer_size,
363 DMA_FROM_DEVICE);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700364 dev_kfree_skb(skb);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700365 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700366 bnad_reset_rcb(bnad, rcb);
367}
368
369static void
370bnad_alloc_n_post_rxbufs(struct bnad *bnad, struct bna_rcb *rcb)
371{
372 u16 to_alloc, alloced, unmap_prod, wi_range;
373 struct bnad_unmap_q *unmap_q = rcb->unmap_q;
374 struct bnad_skb_unmap *unmap_array;
375 struct bna_rxq_entry *rxent;
376 struct sk_buff *skb;
377 dma_addr_t dma_addr;
378
379 alloced = 0;
380 to_alloc =
381 BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth);
382
383 unmap_array = unmap_q->unmap_array;
384 unmap_prod = unmap_q->producer_index;
385
386 BNA_RXQ_QPGE_PTR_GET(unmap_prod, rcb->sw_qpt, rxent, wi_range);
387
388 while (to_alloc--) {
Rasesh Mody19dbff92011-08-30 15:27:41 +0000389 if (!wi_range)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700390 BNA_RXQ_QPGE_PTR_GET(unmap_prod, rcb->sw_qpt, rxent,
391 wi_range);
Eric Dumazet0a0e2342011-07-08 05:29:30 +0000392 skb = netdev_alloc_skb_ip_align(bnad->netdev,
393 rcb->rxq->buffer_size);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700394 if (unlikely(!skb)) {
395 BNAD_UPDATE_CTR(bnad, rxbuf_alloc_failed);
396 goto finishing;
397 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700398 unmap_array[unmap_prod].skb = skb;
Ivan Vecera5ea74312011-02-02 04:37:02 +0000399 dma_addr = dma_map_single(&bnad->pcidev->dev, skb->data,
400 rcb->rxq->buffer_size,
401 DMA_FROM_DEVICE);
402 dma_unmap_addr_set(&unmap_array[unmap_prod], dma_addr,
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700403 dma_addr);
404 BNA_SET_DMA_ADDR(dma_addr, &rxent->host_addr);
405 BNA_QE_INDX_ADD(unmap_prod, 1, unmap_q->q_depth);
406
407 rxent++;
408 wi_range--;
409 alloced++;
410 }
411
412finishing:
413 if (likely(alloced)) {
414 unmap_q->producer_index = unmap_prod;
415 rcb->producer_index = unmap_prod;
416 smp_mb();
Rasesh Modybe7fa322010-12-23 21:45:01 +0000417 if (likely(test_bit(BNAD_RXQ_STARTED, &rcb->flags)))
418 bna_rxq_prod_indx_doorbell(rcb);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700419 }
420}
421
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700422static inline void
423bnad_refill_rxq(struct bnad *bnad, struct bna_rcb *rcb)
424{
425 struct bnad_unmap_q *unmap_q = rcb->unmap_q;
426
427 if (!test_and_set_bit(BNAD_RXQ_REFILL, &rcb->flags)) {
428 if (BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth)
429 >> BNAD_RXQ_REFILL_THRESHOLD_SHIFT)
430 bnad_alloc_n_post_rxbufs(bnad, rcb);
431 smp_mb__before_clear_bit();
432 clear_bit(BNAD_RXQ_REFILL, &rcb->flags);
433 }
434}
435
436static u32
437bnad_poll_cq(struct bnad *bnad, struct bna_ccb *ccb, int budget)
438{
439 struct bna_cq_entry *cmpl, *next_cmpl;
440 struct bna_rcb *rcb = NULL;
441 unsigned int wi_range, packets = 0, wis = 0;
442 struct bnad_unmap_q *unmap_q;
Ivan Vecera5ea74312011-02-02 04:37:02 +0000443 struct bnad_skb_unmap *unmap_array;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700444 struct sk_buff *skb;
Ivan Vecera5ea74312011-02-02 04:37:02 +0000445 u32 flags, unmap_cons;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700446 struct bna_pkt_rate *pkt_rt = &ccb->pkt_rate;
Rasesh Mody078086f2011-08-08 16:21:39 +0000447 struct bnad_rx_ctrl *rx_ctrl = (struct bnad_rx_ctrl *)(ccb->ctrl);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700448
Rasesh Mody078086f2011-08-08 16:21:39 +0000449 set_bit(BNAD_FP_IN_RX_PATH, &rx_ctrl->flags);
450
451 if (!test_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags)) {
452 clear_bit(BNAD_FP_IN_RX_PATH, &rx_ctrl->flags);
Rasesh Modybe7fa322010-12-23 21:45:01 +0000453 return 0;
Rasesh Mody078086f2011-08-08 16:21:39 +0000454 }
Rasesh Modybe7fa322010-12-23 21:45:01 +0000455
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700456 prefetch(bnad->netdev);
457 BNA_CQ_QPGE_PTR_GET(ccb->producer_index, ccb->sw_qpt, cmpl,
458 wi_range);
459 BUG_ON(!(wi_range <= ccb->q_depth));
460 while (cmpl->valid && packets < budget) {
461 packets++;
462 BNA_UPDATE_PKT_CNT(pkt_rt, ntohs(cmpl->length));
463
Rasesh Mody078086f2011-08-08 16:21:39 +0000464 if (bna_is_small_rxq(cmpl->rxq_id))
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700465 rcb = ccb->rcb[1];
Rasesh Mody078086f2011-08-08 16:21:39 +0000466 else
467 rcb = ccb->rcb[0];
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700468
469 unmap_q = rcb->unmap_q;
Ivan Vecera5ea74312011-02-02 04:37:02 +0000470 unmap_array = unmap_q->unmap_array;
471 unmap_cons = unmap_q->consumer_index;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700472
Ivan Vecera5ea74312011-02-02 04:37:02 +0000473 skb = unmap_array[unmap_cons].skb;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700474 BUG_ON(!(skb));
Ivan Vecera5ea74312011-02-02 04:37:02 +0000475 unmap_array[unmap_cons].skb = NULL;
476 dma_unmap_single(&bnad->pcidev->dev,
477 dma_unmap_addr(&unmap_array[unmap_cons],
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700478 dma_addr),
Ivan Vecera5ea74312011-02-02 04:37:02 +0000479 rcb->rxq->buffer_size,
480 DMA_FROM_DEVICE);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700481 BNA_QE_INDX_ADD(unmap_q->consumer_index, 1, unmap_q->q_depth);
482
483 /* Should be more efficient ? Performance ? */
484 BNA_QE_INDX_ADD(rcb->consumer_index, 1, rcb->q_depth);
485
486 wis++;
487 if (likely(--wi_range))
488 next_cmpl = cmpl + 1;
489 else {
490 BNA_QE_INDX_ADD(ccb->producer_index, wis, ccb->q_depth);
491 wis = 0;
492 BNA_CQ_QPGE_PTR_GET(ccb->producer_index, ccb->sw_qpt,
493 next_cmpl, wi_range);
494 BUG_ON(!(wi_range <= ccb->q_depth));
495 }
496 prefetch(next_cmpl);
497
498 flags = ntohl(cmpl->flags);
499 if (unlikely
500 (flags &
501 (BNA_CQ_EF_MAC_ERROR | BNA_CQ_EF_FCS_ERROR |
502 BNA_CQ_EF_TOO_LONG))) {
503 dev_kfree_skb_any(skb);
504 rcb->rxq->rx_packets_with_error++;
505 goto next;
506 }
507
508 skb_put(skb, ntohs(cmpl->length));
509 if (likely
Michał Mirosławe5ee20e2011-04-12 09:38:23 +0000510 ((bnad->netdev->features & NETIF_F_RXCSUM) &&
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700511 (((flags & BNA_CQ_EF_IPV4) &&
512 (flags & BNA_CQ_EF_L3_CKSUM_OK)) ||
513 (flags & BNA_CQ_EF_IPV6)) &&
514 (flags & (BNA_CQ_EF_TCP | BNA_CQ_EF_UDP)) &&
515 (flags & BNA_CQ_EF_L4_CKSUM_OK)))
516 skb->ip_summed = CHECKSUM_UNNECESSARY;
517 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700518 skb_checksum_none_assert(skb);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700519
520 rcb->rxq->rx_packets++;
521 rcb->rxq->rx_bytes += skb->len;
522 skb->protocol = eth_type_trans(skb, bnad->netdev);
523
Jiri Pirkof859d7c2011-07-20 04:54:14 +0000524 if (flags & BNA_CQ_EF_VLAN)
525 __vlan_hwaccel_put_tag(skb, ntohs(cmpl->vlan_tag));
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700526
Rasesh Mody078086f2011-08-08 16:21:39 +0000527 if (skb->ip_summed == CHECKSUM_UNNECESSARY)
Jiri Pirkof859d7c2011-07-20 04:54:14 +0000528 napi_gro_receive(&rx_ctrl->napi, skb);
Rasesh Mody078086f2011-08-08 16:21:39 +0000529 else {
Jiri Pirkof859d7c2011-07-20 04:54:14 +0000530 netif_receive_skb(skb);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700531 }
532
533next:
534 cmpl->valid = 0;
535 cmpl = next_cmpl;
536 }
537
538 BNA_QE_INDX_ADD(ccb->producer_index, wis, ccb->q_depth);
539
Rasesh Mody2be67142011-08-30 15:27:39 +0000540 if (likely(test_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags)))
Rasesh Mody271e8b72011-08-30 15:27:40 +0000541 bna_ib_ack_disable_irq(ccb->i_dbell, packets);
542
Rasesh Mody2be67142011-08-30 15:27:39 +0000543 bnad_refill_rxq(bnad, ccb->rcb[0]);
544 if (ccb->rcb[1])
545 bnad_refill_rxq(bnad, ccb->rcb[1]);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700546
Rasesh Mody078086f2011-08-08 16:21:39 +0000547 clear_bit(BNAD_FP_IN_RX_PATH, &rx_ctrl->flags);
548
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700549 return packets;
550}
551
552static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700553bnad_netif_rx_schedule_poll(struct bnad *bnad, struct bna_ccb *ccb)
554{
555 struct bnad_rx_ctrl *rx_ctrl = (struct bnad_rx_ctrl *)(ccb->ctrl);
Rasesh Modybe7fa322010-12-23 21:45:01 +0000556 struct napi_struct *napi = &rx_ctrl->napi;
557
558 if (likely(napi_schedule_prep(napi))) {
Rasesh Modybe7fa322010-12-23 21:45:01 +0000559 __napi_schedule(napi);
Rasesh Mody271e8b72011-08-30 15:27:40 +0000560 rx_ctrl->rx_schedule++;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700561 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700562}
563
564/* MSIX Rx Path Handler */
565static irqreturn_t
566bnad_msix_rx(int irq, void *data)
567{
568 struct bna_ccb *ccb = (struct bna_ccb *)data;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700569
Rasesh Mody271e8b72011-08-30 15:27:40 +0000570 if (ccb) {
571 ((struct bnad_rx_ctrl *)(ccb->ctrl))->rx_intr_ctr++;
Rasesh Mody2be67142011-08-30 15:27:39 +0000572 bnad_netif_rx_schedule_poll(ccb->bnad, ccb);
Rasesh Mody271e8b72011-08-30 15:27:40 +0000573 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700574
575 return IRQ_HANDLED;
576}
577
578/* Interrupt handlers */
579
580/* Mbox Interrupt Handlers */
581static irqreturn_t
582bnad_msix_mbox_handler(int irq, void *data)
583{
584 u32 intr_status;
Rasesh Modye2fa6f22010-10-05 15:46:04 +0000585 unsigned long flags;
Rasesh Modybe7fa322010-12-23 21:45:01 +0000586 struct bnad *bnad = (struct bnad *)data;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700587
Rasesh Modybe7fa322010-12-23 21:45:01 +0000588 if (unlikely(test_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags)))
589 return IRQ_HANDLED;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700590
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700591 spin_lock_irqsave(&bnad->bna_lock, flags);
592
593 bna_intr_status_get(&bnad->bna, intr_status);
594
Rasesh Mody078086f2011-08-08 16:21:39 +0000595 if (BNA_IS_MBOX_ERR_INTR(&bnad->bna, intr_status))
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700596 bna_mbox_handler(&bnad->bna, intr_status);
597
598 spin_unlock_irqrestore(&bnad->bna_lock, flags);
599
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700600 return IRQ_HANDLED;
601}
602
603static irqreturn_t
604bnad_isr(int irq, void *data)
605{
606 int i, j;
607 u32 intr_status;
608 unsigned long flags;
Rasesh Modybe7fa322010-12-23 21:45:01 +0000609 struct bnad *bnad = (struct bnad *)data;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700610 struct bnad_rx_info *rx_info;
611 struct bnad_rx_ctrl *rx_ctrl;
Rasesh Mody078086f2011-08-08 16:21:39 +0000612 struct bna_tcb *tcb = NULL;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700613
Rasesh Modye2fa6f22010-10-05 15:46:04 +0000614 if (unlikely(test_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags)))
615 return IRQ_NONE;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700616
617 bna_intr_status_get(&bnad->bna, intr_status);
Rasesh Modye2fa6f22010-10-05 15:46:04 +0000618
619 if (unlikely(!intr_status))
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700620 return IRQ_NONE;
Rasesh Modye2fa6f22010-10-05 15:46:04 +0000621
622 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700623
Rasesh Mody078086f2011-08-08 16:21:39 +0000624 if (BNA_IS_MBOX_ERR_INTR(&bnad->bna, intr_status))
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700625 bna_mbox_handler(&bnad->bna, intr_status);
Rasesh Modybe7fa322010-12-23 21:45:01 +0000626
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700627 spin_unlock_irqrestore(&bnad->bna_lock, flags);
628
Rasesh Modybe7fa322010-12-23 21:45:01 +0000629 if (!BNA_IS_INTX_DATA_INTR(intr_status))
630 return IRQ_HANDLED;
631
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700632 /* Process data interrupts */
Rasesh Modybe7fa322010-12-23 21:45:01 +0000633 /* Tx processing */
634 for (i = 0; i < bnad->num_tx; i++) {
Rasesh Mody078086f2011-08-08 16:21:39 +0000635 for (j = 0; j < bnad->num_txq_per_tx; j++) {
636 tcb = bnad->tx_info[i].tcb[j];
637 if (tcb && test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))
638 bnad_tx(bnad, bnad->tx_info[i].tcb[j]);
639 }
Rasesh Modybe7fa322010-12-23 21:45:01 +0000640 }
641 /* Rx processing */
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700642 for (i = 0; i < bnad->num_rx; i++) {
643 rx_info = &bnad->rx_info[i];
644 if (!rx_info->rx)
645 continue;
646 for (j = 0; j < bnad->num_rxp_per_rx; j++) {
647 rx_ctrl = &rx_info->rx_ctrl[j];
648 if (rx_ctrl->ccb)
649 bnad_netif_rx_schedule_poll(bnad,
650 rx_ctrl->ccb);
651 }
652 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700653 return IRQ_HANDLED;
654}
655
656/*
657 * Called in interrupt / callback context
658 * with bna_lock held, so cfg_flags access is OK
659 */
660static void
661bnad_enable_mbox_irq(struct bnad *bnad)
662{
Rasesh Modybe7fa322010-12-23 21:45:01 +0000663 clear_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
Rasesh Modye2fa6f22010-10-05 15:46:04 +0000664
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700665 BNAD_UPDATE_CTR(bnad, mbox_intr_enabled);
666}
667
668/*
669 * Called with bnad->bna_lock held b'cos of
670 * bnad->cfg_flags access.
671 */
Rasesh Modyb7ee31c52010-10-05 15:46:05 +0000672static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700673bnad_disable_mbox_irq(struct bnad *bnad)
674{
Rasesh Modybe7fa322010-12-23 21:45:01 +0000675 set_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
Rasesh Modye2fa6f22010-10-05 15:46:04 +0000676
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700677 BNAD_UPDATE_CTR(bnad, mbox_intr_disabled);
678}
679
Rasesh Modybe7fa322010-12-23 21:45:01 +0000680static void
681bnad_set_netdev_perm_addr(struct bnad *bnad)
682{
683 struct net_device *netdev = bnad->netdev;
684
685 memcpy(netdev->perm_addr, &bnad->perm_addr, netdev->addr_len);
686 if (is_zero_ether_addr(netdev->dev_addr))
687 memcpy(netdev->dev_addr, &bnad->perm_addr, netdev->addr_len);
688}
689
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700690/* Control Path Handlers */
691
692/* Callbacks */
693void
Rasesh Mody078086f2011-08-08 16:21:39 +0000694bnad_cb_mbox_intr_enable(struct bnad *bnad)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700695{
696 bnad_enable_mbox_irq(bnad);
697}
698
699void
Rasesh Mody078086f2011-08-08 16:21:39 +0000700bnad_cb_mbox_intr_disable(struct bnad *bnad)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700701{
702 bnad_disable_mbox_irq(bnad);
703}
704
705void
Rasesh Mody078086f2011-08-08 16:21:39 +0000706bnad_cb_ioceth_ready(struct bnad *bnad)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700707{
Rasesh Mody078086f2011-08-08 16:21:39 +0000708 bnad->bnad_completions.ioc_comp_status = BNA_CB_SUCCESS;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700709 complete(&bnad->bnad_completions.ioc_comp);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700710}
711
712void
Rasesh Mody078086f2011-08-08 16:21:39 +0000713bnad_cb_ioceth_failed(struct bnad *bnad)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700714{
Rasesh Mody078086f2011-08-08 16:21:39 +0000715 bnad->bnad_completions.ioc_comp_status = BNA_CB_FAIL;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700716 complete(&bnad->bnad_completions.ioc_comp);
Rasesh Mody078086f2011-08-08 16:21:39 +0000717}
718
719void
720bnad_cb_ioceth_disabled(struct bnad *bnad)
721{
722 bnad->bnad_completions.ioc_comp_status = BNA_CB_SUCCESS;
723 complete(&bnad->bnad_completions.ioc_comp);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700724}
725
726static void
Rasesh Mody078086f2011-08-08 16:21:39 +0000727bnad_cb_enet_disabled(void *arg)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700728{
729 struct bnad *bnad = (struct bnad *)arg;
730
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700731 netif_carrier_off(bnad->netdev);
Rasesh Mody078086f2011-08-08 16:21:39 +0000732 complete(&bnad->bnad_completions.enet_comp);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700733}
734
735void
Rasesh Mody078086f2011-08-08 16:21:39 +0000736bnad_cb_ethport_link_status(struct bnad *bnad,
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700737 enum bna_link_status link_status)
738{
739 bool link_up = 0;
740
741 link_up = (link_status == BNA_LINK_UP) || (link_status == BNA_CEE_UP);
742
743 if (link_status == BNA_CEE_UP) {
Rasesh Mody078086f2011-08-08 16:21:39 +0000744 if (!test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags))
745 BNAD_UPDATE_CTR(bnad, cee_toggle);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700746 set_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags);
Rasesh Mody078086f2011-08-08 16:21:39 +0000747 } else {
748 if (test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags))
749 BNAD_UPDATE_CTR(bnad, cee_toggle);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700750 clear_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags);
Rasesh Mody078086f2011-08-08 16:21:39 +0000751 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700752
753 if (link_up) {
754 if (!netif_carrier_ok(bnad->netdev)) {
Rasesh Mody078086f2011-08-08 16:21:39 +0000755 uint tx_id, tcb_id;
756 printk(KERN_WARNING "bna: %s link up\n",
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700757 bnad->netdev->name);
758 netif_carrier_on(bnad->netdev);
759 BNAD_UPDATE_CTR(bnad, link_toggle);
Rasesh Mody078086f2011-08-08 16:21:39 +0000760 for (tx_id = 0; tx_id < bnad->num_tx; tx_id++) {
761 for (tcb_id = 0; tcb_id < bnad->num_txq_per_tx;
762 tcb_id++) {
763 struct bna_tcb *tcb =
764 bnad->tx_info[tx_id].tcb[tcb_id];
765 u32 txq_id;
766 if (!tcb)
767 continue;
768
769 txq_id = tcb->id;
770
771 if (test_bit(BNAD_TXQ_TX_STARTED,
772 &tcb->flags)) {
773 /*
774 * Force an immediate
775 * Transmit Schedule */
776 printk(KERN_INFO "bna: %s %d "
777 "TXQ_STARTED\n",
778 bnad->netdev->name,
779 txq_id);
780 netif_wake_subqueue(
781 bnad->netdev,
782 txq_id);
783 BNAD_UPDATE_CTR(bnad,
784 netif_queue_wakeup);
785 } else {
786 netif_stop_subqueue(
787 bnad->netdev,
788 txq_id);
789 BNAD_UPDATE_CTR(bnad,
790 netif_queue_stop);
791 }
792 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700793 }
794 }
795 } else {
796 if (netif_carrier_ok(bnad->netdev)) {
Rasesh Mody078086f2011-08-08 16:21:39 +0000797 printk(KERN_WARNING "bna: %s link down\n",
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700798 bnad->netdev->name);
799 netif_carrier_off(bnad->netdev);
800 BNAD_UPDATE_CTR(bnad, link_toggle);
801 }
802 }
803}
804
805static void
Rasesh Mody078086f2011-08-08 16:21:39 +0000806bnad_cb_tx_disabled(void *arg, struct bna_tx *tx)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700807{
808 struct bnad *bnad = (struct bnad *)arg;
809
810 complete(&bnad->bnad_completions.tx_comp);
811}
812
813static void
814bnad_cb_tcb_setup(struct bnad *bnad, struct bna_tcb *tcb)
815{
816 struct bnad_tx_info *tx_info =
817 (struct bnad_tx_info *)tcb->txq->tx->priv;
818 struct bnad_unmap_q *unmap_q = tcb->unmap_q;
819
820 tx_info->tcb[tcb->id] = tcb;
821 unmap_q->producer_index = 0;
822 unmap_q->consumer_index = 0;
823 unmap_q->q_depth = BNAD_TX_UNMAPQ_DEPTH;
824}
825
826static void
827bnad_cb_tcb_destroy(struct bnad *bnad, struct bna_tcb *tcb)
828{
829 struct bnad_tx_info *tx_info =
830 (struct bnad_tx_info *)tcb->txq->tx->priv;
Rasesh Modybe7fa322010-12-23 21:45:01 +0000831 struct bnad_unmap_q *unmap_q = tcb->unmap_q;
832
833 while (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags))
834 cpu_relax();
835
836 bnad_free_all_txbufs(bnad, tcb);
837
838 unmap_q->producer_index = 0;
839 unmap_q->consumer_index = 0;
840
841 smp_mb__before_clear_bit();
842 clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700843
844 tx_info->tcb[tcb->id] = NULL;
845}
846
847static void
848bnad_cb_rcb_setup(struct bnad *bnad, struct bna_rcb *rcb)
849{
850 struct bnad_unmap_q *unmap_q = rcb->unmap_q;
851
852 unmap_q->producer_index = 0;
853 unmap_q->consumer_index = 0;
854 unmap_q->q_depth = BNAD_RX_UNMAPQ_DEPTH;
855}
856
857static void
Rasesh Modybe7fa322010-12-23 21:45:01 +0000858bnad_cb_rcb_destroy(struct bnad *bnad, struct bna_rcb *rcb)
859{
860 bnad_free_all_rxbufs(bnad, rcb);
861}
862
863static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700864bnad_cb_ccb_setup(struct bnad *bnad, struct bna_ccb *ccb)
865{
866 struct bnad_rx_info *rx_info =
867 (struct bnad_rx_info *)ccb->cq->rx->priv;
868
869 rx_info->rx_ctrl[ccb->id].ccb = ccb;
870 ccb->ctrl = &rx_info->rx_ctrl[ccb->id];
871}
872
873static void
874bnad_cb_ccb_destroy(struct bnad *bnad, struct bna_ccb *ccb)
875{
876 struct bnad_rx_info *rx_info =
877 (struct bnad_rx_info *)ccb->cq->rx->priv;
878
879 rx_info->rx_ctrl[ccb->id].ccb = NULL;
880}
881
882static void
Rasesh Mody078086f2011-08-08 16:21:39 +0000883bnad_cb_tx_stall(struct bnad *bnad, struct bna_tx *tx)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700884{
885 struct bnad_tx_info *tx_info =
Rasesh Mody078086f2011-08-08 16:21:39 +0000886 (struct bnad_tx_info *)tx->priv;
887 struct bna_tcb *tcb;
888 u32 txq_id;
889 int i;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700890
Rasesh Mody078086f2011-08-08 16:21:39 +0000891 for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
892 tcb = tx_info->tcb[i];
893 if (!tcb)
894 continue;
895 txq_id = tcb->id;
896 clear_bit(BNAD_TXQ_TX_STARTED, &tcb->flags);
897 netif_stop_subqueue(bnad->netdev, txq_id);
898 printk(KERN_INFO "bna: %s %d TXQ_STOPPED\n",
899 bnad->netdev->name, txq_id);
900 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700901}
902
903static void
Rasesh Mody078086f2011-08-08 16:21:39 +0000904bnad_cb_tx_resume(struct bnad *bnad, struct bna_tx *tx)
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700905{
Rasesh Mody078086f2011-08-08 16:21:39 +0000906 struct bnad_tx_info *tx_info = (struct bnad_tx_info *)tx->priv;
907 struct bna_tcb *tcb;
908 struct bnad_unmap_q *unmap_q;
909 u32 txq_id;
910 int i;
Rasesh Modybe7fa322010-12-23 21:45:01 +0000911
Rasesh Mody078086f2011-08-08 16:21:39 +0000912 for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
913 tcb = tx_info->tcb[i];
914 if (!tcb)
915 continue;
916 txq_id = tcb->id;
Rasesh Mody8b230ed2010-08-23 20:24:12 -0700917
Rasesh Mody078086f2011-08-08 16:21:39 +0000918 unmap_q = tcb->unmap_q;
Rasesh Modybe7fa322010-12-23 21:45:01 +0000919
Rasesh Mody078086f2011-08-08 16:21:39 +0000920 if (test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))
921 continue;
Rasesh Modybe7fa322010-12-23 21:45:01 +0000922
Rasesh Mody078086f2011-08-08 16:21:39 +0000923 while (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags))
924 cpu_relax();
Rasesh Modybe7fa322010-12-23 21:45:01 +0000925
Rasesh Mody078086f2011-08-08 16:21:39 +0000926 bnad_free_all_txbufs(bnad, tcb);
Rasesh Modybe7fa322010-12-23 21:45:01 +0000927
Rasesh Mody078086f2011-08-08 16:21:39 +0000928 unmap_q->producer_index = 0;
929 unmap_q->consumer_index = 0;
930
931 smp_mb__before_clear_bit();
932 clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
933
934 set_bit(BNAD_TXQ_TX_STARTED, &tcb->flags);
935
936 if (netif_carrier_ok(bnad->netdev)) {
937 printk(KERN_INFO "bna: %s %d TXQ_STARTED\n",
938 bnad->netdev->name, txq_id);
939 netif_wake_subqueue(bnad->netdev, txq_id);
940 BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
941 }
942 }
Rasesh Modybe7fa322010-12-23 21:45:01 +0000943
944 /*
Rasesh Mody078086f2011-08-08 16:21:39 +0000945 * Workaround for first ioceth enable failure & we
Rasesh Modybe7fa322010-12-23 21:45:01 +0000946 * get a 0 MAC address. We try to get the MAC address
947 * again here.
948 */
949 if (is_zero_ether_addr(&bnad->perm_addr.mac[0])) {
Rasesh Mody078086f2011-08-08 16:21:39 +0000950 bna_enet_perm_mac_get(&bnad->bna.enet, &bnad->perm_addr);
Rasesh Modybe7fa322010-12-23 21:45:01 +0000951 bnad_set_netdev_perm_addr(bnad);
952 }
Rasesh Mody078086f2011-08-08 16:21:39 +0000953}
Rasesh Modybe7fa322010-12-23 21:45:01 +0000954
Rasesh Mody078086f2011-08-08 16:21:39 +0000955static void
956bnad_cb_tx_cleanup(struct bnad *bnad, struct bna_tx *tx)
957{
958 struct bnad_tx_info *tx_info = (struct bnad_tx_info *)tx->priv;
959 struct bna_tcb *tcb;
960 int i;
Rasesh Modybe7fa322010-12-23 21:45:01 +0000961
Rasesh Mody078086f2011-08-08 16:21:39 +0000962 for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
963 tcb = tx_info->tcb[i];
964 if (!tcb)
965 continue;
966 }
967
968 mdelay(BNAD_TXRX_SYNC_MDELAY);
969 bna_tx_cleanup_complete(tx);
970}
971
972static void
973bnad_cb_rx_cleanup(struct bnad *bnad, struct bna_rx *rx)
974{
975 struct bnad_rx_info *rx_info = (struct bnad_rx_info *)rx->priv;
976 struct bna_ccb *ccb;
977 struct bnad_rx_ctrl *rx_ctrl;
978 int i;
979
980 mdelay(BNAD_TXRX_SYNC_MDELAY);
981
Rasesh Mody772b5232011-08-30 15:27:37 +0000982 for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
Rasesh Mody078086f2011-08-08 16:21:39 +0000983 rx_ctrl = &rx_info->rx_ctrl[i];
984 ccb = rx_ctrl->ccb;
985 if (!ccb)
986 continue;
987
988 clear_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags);
989
990 if (ccb->rcb[1])
991 clear_bit(BNAD_RXQ_STARTED, &ccb->rcb[1]->flags);
992
993 while (test_bit(BNAD_FP_IN_RX_PATH, &rx_ctrl->flags))
994 cpu_relax();
995 }
996
997 bna_rx_cleanup_complete(rx);
998}
999
1000static void
1001bnad_cb_rx_post(struct bnad *bnad, struct bna_rx *rx)
1002{
1003 struct bnad_rx_info *rx_info = (struct bnad_rx_info *)rx->priv;
1004 struct bna_ccb *ccb;
1005 struct bna_rcb *rcb;
1006 struct bnad_rx_ctrl *rx_ctrl;
1007 struct bnad_unmap_q *unmap_q;
1008 int i;
1009 int j;
1010
Rasesh Mody772b5232011-08-30 15:27:37 +00001011 for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
Rasesh Mody078086f2011-08-08 16:21:39 +00001012 rx_ctrl = &rx_info->rx_ctrl[i];
1013 ccb = rx_ctrl->ccb;
1014 if (!ccb)
1015 continue;
1016
1017 bnad_cq_cmpl_init(bnad, ccb);
1018
1019 for (j = 0; j < BNAD_MAX_RXQ_PER_RXP; j++) {
1020 rcb = ccb->rcb[j];
1021 if (!rcb)
1022 continue;
1023 bnad_free_all_rxbufs(bnad, rcb);
1024
1025 set_bit(BNAD_RXQ_STARTED, &rcb->flags);
1026 unmap_q = rcb->unmap_q;
1027
1028 /* Now allocate & post buffers for this RCB */
1029 /* !!Allocation in callback context */
1030 if (!test_and_set_bit(BNAD_RXQ_REFILL, &rcb->flags)) {
1031 if (BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth)
1032 >> BNAD_RXQ_REFILL_THRESHOLD_SHIFT)
1033 bnad_alloc_n_post_rxbufs(bnad, rcb);
1034 smp_mb__before_clear_bit();
1035 clear_bit(BNAD_RXQ_REFILL, &rcb->flags);
1036 }
1037 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001038 }
1039}
1040
1041static void
Rasesh Mody078086f2011-08-08 16:21:39 +00001042bnad_cb_rx_disabled(void *arg, struct bna_rx *rx)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001043{
1044 struct bnad *bnad = (struct bnad *)arg;
1045
1046 complete(&bnad->bnad_completions.rx_comp);
1047}
1048
1049static void
Rasesh Mody078086f2011-08-08 16:21:39 +00001050bnad_cb_rx_mcast_add(struct bnad *bnad, struct bna_rx *rx)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001051{
Rasesh Mody078086f2011-08-08 16:21:39 +00001052 bnad->bnad_completions.mcast_comp_status = BNA_CB_SUCCESS;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001053 complete(&bnad->bnad_completions.mcast_comp);
1054}
1055
1056void
1057bnad_cb_stats_get(struct bnad *bnad, enum bna_cb_status status,
1058 struct bna_stats *stats)
1059{
1060 if (status == BNA_CB_SUCCESS)
1061 BNAD_UPDATE_CTR(bnad, hw_stats_updates);
1062
1063 if (!netif_running(bnad->netdev) ||
1064 !test_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
1065 return;
1066
1067 mod_timer(&bnad->stats_timer,
1068 jiffies + msecs_to_jiffies(BNAD_STATS_TIMER_FREQ));
1069}
1070
Rasesh Mody078086f2011-08-08 16:21:39 +00001071static void
1072bnad_cb_enet_mtu_set(struct bnad *bnad)
1073{
1074 bnad->bnad_completions.mtu_comp_status = BNA_CB_SUCCESS;
1075 complete(&bnad->bnad_completions.mtu_comp);
1076}
1077
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001078/* Resource allocation, free functions */
1079
1080static void
1081bnad_mem_free(struct bnad *bnad,
1082 struct bna_mem_info *mem_info)
1083{
1084 int i;
1085 dma_addr_t dma_pa;
1086
1087 if (mem_info->mdl == NULL)
1088 return;
1089
1090 for (i = 0; i < mem_info->num; i++) {
1091 if (mem_info->mdl[i].kva != NULL) {
1092 if (mem_info->mem_type == BNA_MEM_T_DMA) {
1093 BNA_GET_DMA_ADDR(&(mem_info->mdl[i].dma),
1094 dma_pa);
Ivan Vecera5ea74312011-02-02 04:37:02 +00001095 dma_free_coherent(&bnad->pcidev->dev,
1096 mem_info->mdl[i].len,
1097 mem_info->mdl[i].kva, dma_pa);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001098 } else
1099 kfree(mem_info->mdl[i].kva);
1100 }
1101 }
1102 kfree(mem_info->mdl);
1103 mem_info->mdl = NULL;
1104}
1105
1106static int
1107bnad_mem_alloc(struct bnad *bnad,
1108 struct bna_mem_info *mem_info)
1109{
1110 int i;
1111 dma_addr_t dma_pa;
1112
1113 if ((mem_info->num == 0) || (mem_info->len == 0)) {
1114 mem_info->mdl = NULL;
1115 return 0;
1116 }
1117
1118 mem_info->mdl = kcalloc(mem_info->num, sizeof(struct bna_mem_descr),
1119 GFP_KERNEL);
1120 if (mem_info->mdl == NULL)
1121 return -ENOMEM;
1122
1123 if (mem_info->mem_type == BNA_MEM_T_DMA) {
1124 for (i = 0; i < mem_info->num; i++) {
1125 mem_info->mdl[i].len = mem_info->len;
1126 mem_info->mdl[i].kva =
Ivan Vecera5ea74312011-02-02 04:37:02 +00001127 dma_alloc_coherent(&bnad->pcidev->dev,
1128 mem_info->len, &dma_pa,
1129 GFP_KERNEL);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001130
1131 if (mem_info->mdl[i].kva == NULL)
1132 goto err_return;
1133
1134 BNA_SET_DMA_ADDR(dma_pa,
1135 &(mem_info->mdl[i].dma));
1136 }
1137 } else {
1138 for (i = 0; i < mem_info->num; i++) {
1139 mem_info->mdl[i].len = mem_info->len;
1140 mem_info->mdl[i].kva = kzalloc(mem_info->len,
1141 GFP_KERNEL);
1142 if (mem_info->mdl[i].kva == NULL)
1143 goto err_return;
1144 }
1145 }
1146
1147 return 0;
1148
1149err_return:
1150 bnad_mem_free(bnad, mem_info);
1151 return -ENOMEM;
1152}
1153
1154/* Free IRQ for Mailbox */
1155static void
Rasesh Mody078086f2011-08-08 16:21:39 +00001156bnad_mbox_irq_free(struct bnad *bnad)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001157{
1158 int irq;
1159 unsigned long flags;
1160
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001161 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001162 bnad_disable_mbox_irq(bnad);
Rasesh Modye2fa6f22010-10-05 15:46:04 +00001163 spin_unlock_irqrestore(&bnad->bna_lock, flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001164
1165 irq = BNAD_GET_MBOX_IRQ(bnad);
Rasesh Modybe7fa322010-12-23 21:45:01 +00001166 free_irq(irq, bnad);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001167}
1168
1169/*
1170 * Allocates IRQ for Mailbox, but keep it disabled
1171 * This will be enabled once we get the mbox enable callback
1172 * from bna
1173 */
1174static int
Rasesh Mody078086f2011-08-08 16:21:39 +00001175bnad_mbox_irq_alloc(struct bnad *bnad)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001176{
Rasesh Mody0120b992011-07-22 08:07:41 +00001177 int err = 0;
1178 unsigned long irq_flags, flags;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001179 u32 irq;
Rasesh Mody0120b992011-07-22 08:07:41 +00001180 irq_handler_t irq_handler;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001181
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001182 spin_lock_irqsave(&bnad->bna_lock, flags);
1183 if (bnad->cfg_flags & BNAD_CF_MSIX) {
1184 irq_handler = (irq_handler_t)bnad_msix_mbox_handler;
Rasesh Mody8811e262011-07-22 08:07:44 +00001185 irq = bnad->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector;
Shyam Iyer82791712011-07-14 15:00:32 +00001186 irq_flags = 0;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001187 } else {
1188 irq_handler = (irq_handler_t)bnad_isr;
1189 irq = bnad->pcidev->irq;
Shyam Iyer5f778982011-06-28 08:58:05 +00001190 irq_flags = IRQF_SHARED;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001191 }
Rasesh Mody8811e262011-07-22 08:07:44 +00001192
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001193 spin_unlock_irqrestore(&bnad->bna_lock, flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001194 sprintf(bnad->mbox_irq_name, "%s", BNAD_NAME);
1195
Rasesh Modye2fa6f22010-10-05 15:46:04 +00001196 /*
1197 * Set the Mbox IRQ disable flag, so that the IRQ handler
1198 * called from request_irq() for SHARED IRQs do not execute
1199 */
1200 set_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
1201
Rasesh Modybe7fa322010-12-23 21:45:01 +00001202 BNAD_UPDATE_CTR(bnad, mbox_intr_disabled);
1203
Shyam Iyer82791712011-07-14 15:00:32 +00001204 err = request_irq(irq, irq_handler, irq_flags,
Rasesh Modybe7fa322010-12-23 21:45:01 +00001205 bnad->mbox_irq_name, bnad);
Rasesh Modye2fa6f22010-10-05 15:46:04 +00001206
Rasesh Modybe7fa322010-12-23 21:45:01 +00001207 return err;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001208}
1209
1210static void
1211bnad_txrx_irq_free(struct bnad *bnad, struct bna_intr_info *intr_info)
1212{
1213 kfree(intr_info->idl);
1214 intr_info->idl = NULL;
1215}
1216
1217/* Allocates Interrupt Descriptor List for MSIX/INT-X vectors */
1218static int
1219bnad_txrx_irq_alloc(struct bnad *bnad, enum bnad_intr_source src,
Rasesh Mody078086f2011-08-08 16:21:39 +00001220 u32 txrx_id, struct bna_intr_info *intr_info)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001221{
1222 int i, vector_start = 0;
1223 u32 cfg_flags;
1224 unsigned long flags;
1225
1226 spin_lock_irqsave(&bnad->bna_lock, flags);
1227 cfg_flags = bnad->cfg_flags;
1228 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1229
1230 if (cfg_flags & BNAD_CF_MSIX) {
1231 intr_info->intr_type = BNA_INTR_T_MSIX;
1232 intr_info->idl = kcalloc(intr_info->num,
1233 sizeof(struct bna_intr_descr),
1234 GFP_KERNEL);
1235 if (!intr_info->idl)
1236 return -ENOMEM;
1237
1238 switch (src) {
1239 case BNAD_INTR_TX:
Rasesh Mody8811e262011-07-22 08:07:44 +00001240 vector_start = BNAD_MAILBOX_MSIX_VECTORS + txrx_id;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001241 break;
1242
1243 case BNAD_INTR_RX:
Rasesh Mody8811e262011-07-22 08:07:44 +00001244 vector_start = BNAD_MAILBOX_MSIX_VECTORS +
1245 (bnad->num_tx * bnad->num_txq_per_tx) +
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001246 txrx_id;
1247 break;
1248
1249 default:
1250 BUG();
1251 }
1252
1253 for (i = 0; i < intr_info->num; i++)
1254 intr_info->idl[i].vector = vector_start + i;
1255 } else {
1256 intr_info->intr_type = BNA_INTR_T_INTX;
1257 intr_info->num = 1;
1258 intr_info->idl = kcalloc(intr_info->num,
1259 sizeof(struct bna_intr_descr),
1260 GFP_KERNEL);
1261 if (!intr_info->idl)
1262 return -ENOMEM;
1263
1264 switch (src) {
1265 case BNAD_INTR_TX:
Rasesh Mody8811e262011-07-22 08:07:44 +00001266 intr_info->idl[0].vector = BNAD_INTX_TX_IB_BITMASK;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001267 break;
1268
1269 case BNAD_INTR_RX:
Rasesh Mody8811e262011-07-22 08:07:44 +00001270 intr_info->idl[0].vector = BNAD_INTX_RX_IB_BITMASK;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001271 break;
1272 }
1273 }
1274 return 0;
1275}
1276
1277/**
1278 * NOTE: Should be called for MSIX only
1279 * Unregisters Tx MSIX vector(s) from the kernel
1280 */
1281static void
1282bnad_tx_msix_unregister(struct bnad *bnad, struct bnad_tx_info *tx_info,
1283 int num_txqs)
1284{
1285 int i;
1286 int vector_num;
1287
1288 for (i = 0; i < num_txqs; i++) {
1289 if (tx_info->tcb[i] == NULL)
1290 continue;
1291
1292 vector_num = tx_info->tcb[i]->intr_vector;
1293 free_irq(bnad->msix_table[vector_num].vector, tx_info->tcb[i]);
1294 }
1295}
1296
1297/**
1298 * NOTE: Should be called for MSIX only
1299 * Registers Tx MSIX vector(s) and ISR(s), cookie with the kernel
1300 */
1301static int
1302bnad_tx_msix_register(struct bnad *bnad, struct bnad_tx_info *tx_info,
Rasesh Mody078086f2011-08-08 16:21:39 +00001303 u32 tx_id, int num_txqs)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001304{
1305 int i;
1306 int err;
1307 int vector_num;
1308
1309 for (i = 0; i < num_txqs; i++) {
1310 vector_num = tx_info->tcb[i]->intr_vector;
1311 sprintf(tx_info->tcb[i]->name, "%s TXQ %d", bnad->netdev->name,
1312 tx_id + tx_info->tcb[i]->id);
1313 err = request_irq(bnad->msix_table[vector_num].vector,
1314 (irq_handler_t)bnad_msix_tx, 0,
1315 tx_info->tcb[i]->name,
1316 tx_info->tcb[i]);
1317 if (err)
1318 goto err_return;
1319 }
1320
1321 return 0;
1322
1323err_return:
1324 if (i > 0)
1325 bnad_tx_msix_unregister(bnad, tx_info, (i - 1));
1326 return -1;
1327}
1328
1329/**
1330 * NOTE: Should be called for MSIX only
1331 * Unregisters Rx MSIX vector(s) from the kernel
1332 */
1333static void
1334bnad_rx_msix_unregister(struct bnad *bnad, struct bnad_rx_info *rx_info,
1335 int num_rxps)
1336{
1337 int i;
1338 int vector_num;
1339
1340 for (i = 0; i < num_rxps; i++) {
1341 if (rx_info->rx_ctrl[i].ccb == NULL)
1342 continue;
1343
1344 vector_num = rx_info->rx_ctrl[i].ccb->intr_vector;
1345 free_irq(bnad->msix_table[vector_num].vector,
1346 rx_info->rx_ctrl[i].ccb);
1347 }
1348}
1349
1350/**
1351 * NOTE: Should be called for MSIX only
1352 * Registers Tx MSIX vector(s) and ISR(s), cookie with the kernel
1353 */
1354static int
1355bnad_rx_msix_register(struct bnad *bnad, struct bnad_rx_info *rx_info,
Rasesh Mody078086f2011-08-08 16:21:39 +00001356 u32 rx_id, int num_rxps)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001357{
1358 int i;
1359 int err;
1360 int vector_num;
1361
1362 for (i = 0; i < num_rxps; i++) {
1363 vector_num = rx_info->rx_ctrl[i].ccb->intr_vector;
1364 sprintf(rx_info->rx_ctrl[i].ccb->name, "%s CQ %d",
1365 bnad->netdev->name,
1366 rx_id + rx_info->rx_ctrl[i].ccb->id);
1367 err = request_irq(bnad->msix_table[vector_num].vector,
1368 (irq_handler_t)bnad_msix_rx, 0,
1369 rx_info->rx_ctrl[i].ccb->name,
1370 rx_info->rx_ctrl[i].ccb);
1371 if (err)
1372 goto err_return;
1373 }
1374
1375 return 0;
1376
1377err_return:
1378 if (i > 0)
1379 bnad_rx_msix_unregister(bnad, rx_info, (i - 1));
1380 return -1;
1381}
1382
1383/* Free Tx object Resources */
1384static void
1385bnad_tx_res_free(struct bnad *bnad, struct bna_res_info *res_info)
1386{
1387 int i;
1388
1389 for (i = 0; i < BNA_TX_RES_T_MAX; i++) {
1390 if (res_info[i].res_type == BNA_RES_T_MEM)
1391 bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
1392 else if (res_info[i].res_type == BNA_RES_T_INTR)
1393 bnad_txrx_irq_free(bnad, &res_info[i].res_u.intr_info);
1394 }
1395}
1396
1397/* Allocates memory and interrupt resources for Tx object */
1398static int
1399bnad_tx_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
Rasesh Mody078086f2011-08-08 16:21:39 +00001400 u32 tx_id)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001401{
1402 int i, err = 0;
1403
1404 for (i = 0; i < BNA_TX_RES_T_MAX; i++) {
1405 if (res_info[i].res_type == BNA_RES_T_MEM)
1406 err = bnad_mem_alloc(bnad,
1407 &res_info[i].res_u.mem_info);
1408 else if (res_info[i].res_type == BNA_RES_T_INTR)
1409 err = bnad_txrx_irq_alloc(bnad, BNAD_INTR_TX, tx_id,
1410 &res_info[i].res_u.intr_info);
1411 if (err)
1412 goto err_return;
1413 }
1414 return 0;
1415
1416err_return:
1417 bnad_tx_res_free(bnad, res_info);
1418 return err;
1419}
1420
1421/* Free Rx object Resources */
1422static void
1423bnad_rx_res_free(struct bnad *bnad, struct bna_res_info *res_info)
1424{
1425 int i;
1426
1427 for (i = 0; i < BNA_RX_RES_T_MAX; i++) {
1428 if (res_info[i].res_type == BNA_RES_T_MEM)
1429 bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
1430 else if (res_info[i].res_type == BNA_RES_T_INTR)
1431 bnad_txrx_irq_free(bnad, &res_info[i].res_u.intr_info);
1432 }
1433}
1434
1435/* Allocates memory and interrupt resources for Rx object */
1436static int
1437bnad_rx_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
1438 uint rx_id)
1439{
1440 int i, err = 0;
1441
1442 /* All memory needs to be allocated before setup_ccbs */
1443 for (i = 0; i < BNA_RX_RES_T_MAX; i++) {
1444 if (res_info[i].res_type == BNA_RES_T_MEM)
1445 err = bnad_mem_alloc(bnad,
1446 &res_info[i].res_u.mem_info);
1447 else if (res_info[i].res_type == BNA_RES_T_INTR)
1448 err = bnad_txrx_irq_alloc(bnad, BNAD_INTR_RX, rx_id,
1449 &res_info[i].res_u.intr_info);
1450 if (err)
1451 goto err_return;
1452 }
1453 return 0;
1454
1455err_return:
1456 bnad_rx_res_free(bnad, res_info);
1457 return err;
1458}
1459
1460/* Timer callbacks */
1461/* a) IOC timer */
1462static void
1463bnad_ioc_timeout(unsigned long data)
1464{
1465 struct bnad *bnad = (struct bnad *)data;
1466 unsigned long flags;
1467
1468 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody078086f2011-08-08 16:21:39 +00001469 bfa_nw_ioc_timeout((void *) &bnad->bna.ioceth.ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001470 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1471}
1472
1473static void
1474bnad_ioc_hb_check(unsigned long data)
1475{
1476 struct bnad *bnad = (struct bnad *)data;
1477 unsigned long flags;
1478
1479 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody078086f2011-08-08 16:21:39 +00001480 bfa_nw_ioc_hb_check((void *) &bnad->bna.ioceth.ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001481 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1482}
1483
1484static void
Rasesh Mody1d32f762010-12-23 21:45:09 +00001485bnad_iocpf_timeout(unsigned long data)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001486{
1487 struct bnad *bnad = (struct bnad *)data;
1488 unsigned long flags;
1489
1490 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody078086f2011-08-08 16:21:39 +00001491 bfa_nw_iocpf_timeout((void *) &bnad->bna.ioceth.ioc);
Rasesh Mody1d32f762010-12-23 21:45:09 +00001492 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1493}
1494
1495static void
1496bnad_iocpf_sem_timeout(unsigned long data)
1497{
1498 struct bnad *bnad = (struct bnad *)data;
1499 unsigned long flags;
1500
1501 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody078086f2011-08-08 16:21:39 +00001502 bfa_nw_iocpf_sem_timeout((void *) &bnad->bna.ioceth.ioc);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001503 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1504}
1505
1506/*
1507 * All timer routines use bnad->bna_lock to protect against
1508 * the following race, which may occur in case of no locking:
Rasesh Mody0120b992011-07-22 08:07:41 +00001509 * Time CPU m CPU n
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001510 * 0 1 = test_bit
1511 * 1 clear_bit
1512 * 2 del_timer_sync
1513 * 3 mod_timer
1514 */
1515
1516/* b) Dynamic Interrupt Moderation Timer */
1517static void
1518bnad_dim_timeout(unsigned long data)
1519{
1520 struct bnad *bnad = (struct bnad *)data;
1521 struct bnad_rx_info *rx_info;
1522 struct bnad_rx_ctrl *rx_ctrl;
1523 int i, j;
1524 unsigned long flags;
1525
1526 if (!netif_carrier_ok(bnad->netdev))
1527 return;
1528
1529 spin_lock_irqsave(&bnad->bna_lock, flags);
1530 for (i = 0; i < bnad->num_rx; i++) {
1531 rx_info = &bnad->rx_info[i];
1532 if (!rx_info->rx)
1533 continue;
1534 for (j = 0; j < bnad->num_rxp_per_rx; j++) {
1535 rx_ctrl = &rx_info->rx_ctrl[j];
1536 if (!rx_ctrl->ccb)
1537 continue;
1538 bna_rx_dim_update(rx_ctrl->ccb);
1539 }
1540 }
1541
1542 /* Check for BNAD_CF_DIM_ENABLED, does not eleminate a race */
1543 if (test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags))
1544 mod_timer(&bnad->dim_timer,
1545 jiffies + msecs_to_jiffies(BNAD_DIM_TIMER_FREQ));
1546 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1547}
1548
1549/* c) Statistics Timer */
1550static void
1551bnad_stats_timeout(unsigned long data)
1552{
1553 struct bnad *bnad = (struct bnad *)data;
1554 unsigned long flags;
1555
1556 if (!netif_running(bnad->netdev) ||
1557 !test_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
1558 return;
1559
1560 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody078086f2011-08-08 16:21:39 +00001561 bna_hw_stats_get(&bnad->bna);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001562 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1563}
1564
1565/*
1566 * Set up timer for DIM
1567 * Called with bnad->bna_lock held
1568 */
1569void
1570bnad_dim_timer_start(struct bnad *bnad)
1571{
1572 if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED &&
1573 !test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags)) {
1574 setup_timer(&bnad->dim_timer, bnad_dim_timeout,
1575 (unsigned long)bnad);
1576 set_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
1577 mod_timer(&bnad->dim_timer,
1578 jiffies + msecs_to_jiffies(BNAD_DIM_TIMER_FREQ));
1579 }
1580}
1581
1582/*
1583 * Set up timer for statistics
1584 * Called with mutex_lock(&bnad->conf_mutex) held
1585 */
1586static void
1587bnad_stats_timer_start(struct bnad *bnad)
1588{
1589 unsigned long flags;
1590
1591 spin_lock_irqsave(&bnad->bna_lock, flags);
1592 if (!test_and_set_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags)) {
1593 setup_timer(&bnad->stats_timer, bnad_stats_timeout,
1594 (unsigned long)bnad);
1595 mod_timer(&bnad->stats_timer,
1596 jiffies + msecs_to_jiffies(BNAD_STATS_TIMER_FREQ));
1597 }
1598 spin_unlock_irqrestore(&bnad->bna_lock, flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001599}
1600
1601/*
1602 * Stops the stats timer
1603 * Called with mutex_lock(&bnad->conf_mutex) held
1604 */
1605static void
1606bnad_stats_timer_stop(struct bnad *bnad)
1607{
1608 int to_del = 0;
1609 unsigned long flags;
1610
1611 spin_lock_irqsave(&bnad->bna_lock, flags);
1612 if (test_and_clear_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
1613 to_del = 1;
1614 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1615 if (to_del)
1616 del_timer_sync(&bnad->stats_timer);
1617}
1618
1619/* Utilities */
1620
1621static void
1622bnad_netdev_mc_list_get(struct net_device *netdev, u8 *mc_list)
1623{
1624 int i = 1; /* Index 0 has broadcast address */
1625 struct netdev_hw_addr *mc_addr;
1626
1627 netdev_for_each_mc_addr(mc_addr, netdev) {
1628 memcpy(&mc_list[i * ETH_ALEN], &mc_addr->addr[0],
1629 ETH_ALEN);
1630 i++;
1631 }
1632}
1633
1634static int
1635bnad_napi_poll_rx(struct napi_struct *napi, int budget)
1636{
1637 struct bnad_rx_ctrl *rx_ctrl =
1638 container_of(napi, struct bnad_rx_ctrl, napi);
Rasesh Mody2be67142011-08-30 15:27:39 +00001639 struct bnad *bnad = rx_ctrl->bnad;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001640 int rcvd = 0;
1641
Rasesh Mody271e8b72011-08-30 15:27:40 +00001642 rx_ctrl->rx_poll_ctr++;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001643
1644 if (!netif_carrier_ok(bnad->netdev))
1645 goto poll_exit;
1646
Rasesh Mody2be67142011-08-30 15:27:39 +00001647 rcvd = bnad_poll_cq(bnad, rx_ctrl->ccb, budget);
Rasesh Mody271e8b72011-08-30 15:27:40 +00001648 if (rcvd >= budget)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001649 return rcvd;
1650
1651poll_exit:
Rasesh Mody19dbff92011-08-30 15:27:41 +00001652 napi_complete(napi);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001653
Rasesh Mody271e8b72011-08-30 15:27:40 +00001654 rx_ctrl->rx_complete++;
Rasesh Mody2be67142011-08-30 15:27:39 +00001655
1656 if (rx_ctrl->ccb)
Rasesh Mody271e8b72011-08-30 15:27:40 +00001657 bnad_enable_rx_irq_unsafe(rx_ctrl->ccb);
1658
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001659 return rcvd;
1660}
1661
Rasesh Mody2be67142011-08-30 15:27:39 +00001662#define BNAD_NAPI_POLL_QUOTA 64
1663static void
1664bnad_napi_init(struct bnad *bnad, u32 rx_id)
1665{
1666 struct bnad_rx_ctrl *rx_ctrl;
1667 int i;
1668
1669 /* Initialize & enable NAPI */
1670 for (i = 0; i < bnad->num_rxp_per_rx; i++) {
1671 rx_ctrl = &bnad->rx_info[rx_id].rx_ctrl[i];
1672 netif_napi_add(bnad->netdev, &rx_ctrl->napi,
1673 bnad_napi_poll_rx, BNAD_NAPI_POLL_QUOTA);
1674 }
1675}
1676
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001677static void
1678bnad_napi_enable(struct bnad *bnad, u32 rx_id)
1679{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001680 struct bnad_rx_ctrl *rx_ctrl;
1681 int i;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001682
1683 /* Initialize & enable NAPI */
1684 for (i = 0; i < bnad->num_rxp_per_rx; i++) {
1685 rx_ctrl = &bnad->rx_info[rx_id].rx_ctrl[i];
Rasesh Modybe7fa322010-12-23 21:45:01 +00001686
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001687 napi_enable(&rx_ctrl->napi);
1688 }
1689}
1690
1691static void
1692bnad_napi_disable(struct bnad *bnad, u32 rx_id)
1693{
1694 int i;
1695
1696 /* First disable and then clean up */
1697 for (i = 0; i < bnad->num_rxp_per_rx; i++) {
1698 napi_disable(&bnad->rx_info[rx_id].rx_ctrl[i].napi);
1699 netif_napi_del(&bnad->rx_info[rx_id].rx_ctrl[i].napi);
1700 }
1701}
1702
1703/* Should be held with conf_lock held */
1704void
Rasesh Mody078086f2011-08-08 16:21:39 +00001705bnad_cleanup_tx(struct bnad *bnad, u32 tx_id)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001706{
1707 struct bnad_tx_info *tx_info = &bnad->tx_info[tx_id];
1708 struct bna_res_info *res_info = &bnad->tx_res_info[tx_id].res_info[0];
1709 unsigned long flags;
1710
1711 if (!tx_info->tx)
1712 return;
1713
1714 init_completion(&bnad->bnad_completions.tx_comp);
1715 spin_lock_irqsave(&bnad->bna_lock, flags);
1716 bna_tx_disable(tx_info->tx, BNA_HARD_CLEANUP, bnad_cb_tx_disabled);
1717 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1718 wait_for_completion(&bnad->bnad_completions.tx_comp);
1719
1720 if (tx_info->tcb[0]->intr_type == BNA_INTR_T_MSIX)
1721 bnad_tx_msix_unregister(bnad, tx_info,
1722 bnad->num_txq_per_tx);
1723
Rasesh Mody2be67142011-08-30 15:27:39 +00001724 if (0 == tx_id)
1725 tasklet_kill(&bnad->tx_free_tasklet);
1726
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001727 spin_lock_irqsave(&bnad->bna_lock, flags);
1728 bna_tx_destroy(tx_info->tx);
1729 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1730
1731 tx_info->tx = NULL;
Rasesh Mody078086f2011-08-08 16:21:39 +00001732 tx_info->tx_id = 0;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001733
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001734 bnad_tx_res_free(bnad, res_info);
1735}
1736
1737/* Should be held with conf_lock held */
1738int
Rasesh Mody078086f2011-08-08 16:21:39 +00001739bnad_setup_tx(struct bnad *bnad, u32 tx_id)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001740{
1741 int err;
1742 struct bnad_tx_info *tx_info = &bnad->tx_info[tx_id];
1743 struct bna_res_info *res_info = &bnad->tx_res_info[tx_id].res_info[0];
1744 struct bna_intr_info *intr_info =
1745 &res_info[BNA_TX_RES_INTR_T_TXCMPL].res_u.intr_info;
1746 struct bna_tx_config *tx_config = &bnad->tx_config[tx_id];
1747 struct bna_tx_event_cbfn tx_cbfn;
1748 struct bna_tx *tx;
1749 unsigned long flags;
1750
Rasesh Mody078086f2011-08-08 16:21:39 +00001751 tx_info->tx_id = tx_id;
1752
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001753 /* Initialize the Tx object configuration */
1754 tx_config->num_txq = bnad->num_txq_per_tx;
1755 tx_config->txq_depth = bnad->txq_depth;
1756 tx_config->tx_type = BNA_TX_T_REGULAR;
Rasesh Mody078086f2011-08-08 16:21:39 +00001757 tx_config->coalescing_timeo = bnad->tx_coalescing_timeo;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001758
1759 /* Initialize the tx event handlers */
1760 tx_cbfn.tcb_setup_cbfn = bnad_cb_tcb_setup;
1761 tx_cbfn.tcb_destroy_cbfn = bnad_cb_tcb_destroy;
1762 tx_cbfn.tx_stall_cbfn = bnad_cb_tx_stall;
1763 tx_cbfn.tx_resume_cbfn = bnad_cb_tx_resume;
1764 tx_cbfn.tx_cleanup_cbfn = bnad_cb_tx_cleanup;
1765
1766 /* Get BNA's resource requirement for one tx object */
1767 spin_lock_irqsave(&bnad->bna_lock, flags);
1768 bna_tx_res_req(bnad->num_txq_per_tx,
1769 bnad->txq_depth, res_info);
1770 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1771
1772 /* Fill Unmap Q memory requirements */
1773 BNAD_FILL_UNMAPQ_MEM_REQ(
1774 &res_info[BNA_TX_RES_MEM_T_UNMAPQ],
1775 bnad->num_txq_per_tx,
1776 BNAD_TX_UNMAPQ_DEPTH);
1777
1778 /* Allocate resources */
1779 err = bnad_tx_res_alloc(bnad, res_info, tx_id);
1780 if (err)
1781 return err;
1782
1783 /* Ask BNA to create one Tx object, supplying required resources */
1784 spin_lock_irqsave(&bnad->bna_lock, flags);
1785 tx = bna_tx_create(&bnad->bna, bnad, tx_config, &tx_cbfn, res_info,
1786 tx_info);
1787 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1788 if (!tx)
1789 goto err_return;
1790 tx_info->tx = tx;
1791
1792 /* Register ISR for the Tx object */
1793 if (intr_info->intr_type == BNA_INTR_T_MSIX) {
1794 err = bnad_tx_msix_register(bnad, tx_info,
1795 tx_id, bnad->num_txq_per_tx);
1796 if (err)
1797 goto err_return;
1798 }
1799
1800 spin_lock_irqsave(&bnad->bna_lock, flags);
1801 bna_tx_enable(tx);
1802 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1803
1804 return 0;
1805
1806err_return:
1807 bnad_tx_res_free(bnad, res_info);
1808 return err;
1809}
1810
1811/* Setup the rx config for bna_rx_create */
1812/* bnad decides the configuration */
1813static void
1814bnad_init_rx_config(struct bnad *bnad, struct bna_rx_config *rx_config)
1815{
1816 rx_config->rx_type = BNA_RX_T_REGULAR;
1817 rx_config->num_paths = bnad->num_rxp_per_rx;
Rasesh Mody078086f2011-08-08 16:21:39 +00001818 rx_config->coalescing_timeo = bnad->rx_coalescing_timeo;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001819
1820 if (bnad->num_rxp_per_rx > 1) {
1821 rx_config->rss_status = BNA_STATUS_T_ENABLED;
1822 rx_config->rss_config.hash_type =
Rasesh Mody078086f2011-08-08 16:21:39 +00001823 (BFI_ENET_RSS_IPV6 |
1824 BFI_ENET_RSS_IPV6_TCP |
1825 BFI_ENET_RSS_IPV4 |
1826 BFI_ENET_RSS_IPV4_TCP);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001827 rx_config->rss_config.hash_mask =
1828 bnad->num_rxp_per_rx - 1;
1829 get_random_bytes(rx_config->rss_config.toeplitz_hash_key,
1830 sizeof(rx_config->rss_config.toeplitz_hash_key));
1831 } else {
1832 rx_config->rss_status = BNA_STATUS_T_DISABLED;
1833 memset(&rx_config->rss_config, 0,
1834 sizeof(rx_config->rss_config));
1835 }
1836 rx_config->rxp_type = BNA_RXP_SLR;
1837 rx_config->q_depth = bnad->rxq_depth;
1838
1839 rx_config->small_buff_size = BFI_SMALL_RXBUF_SIZE;
1840
1841 rx_config->vlan_strip_status = BNA_STATUS_T_ENABLED;
1842}
1843
Rasesh Mody2be67142011-08-30 15:27:39 +00001844static void
1845bnad_rx_ctrl_init(struct bnad *bnad, u32 rx_id)
1846{
1847 struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
1848 int i;
1849
1850 for (i = 0; i < bnad->num_rxp_per_rx; i++)
1851 rx_info->rx_ctrl[i].bnad = bnad;
1852}
1853
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001854/* Called with mutex_lock(&bnad->conf_mutex) held */
1855void
Rasesh Mody078086f2011-08-08 16:21:39 +00001856bnad_cleanup_rx(struct bnad *bnad, u32 rx_id)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001857{
1858 struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
1859 struct bna_rx_config *rx_config = &bnad->rx_config[rx_id];
1860 struct bna_res_info *res_info = &bnad->rx_res_info[rx_id].res_info[0];
1861 unsigned long flags;
Rasesh Mody271e8b72011-08-30 15:27:40 +00001862 int to_del = 0;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001863
1864 if (!rx_info->rx)
1865 return;
1866
1867 if (0 == rx_id) {
1868 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody271e8b72011-08-30 15:27:40 +00001869 if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED &&
1870 test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags)) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001871 clear_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
Rasesh Mody271e8b72011-08-30 15:27:40 +00001872 to_del = 1;
1873 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001874 spin_unlock_irqrestore(&bnad->bna_lock, flags);
Rasesh Mody271e8b72011-08-30 15:27:40 +00001875 if (to_del)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001876 del_timer_sync(&bnad->dim_timer);
1877 }
1878
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001879 init_completion(&bnad->bnad_completions.rx_comp);
1880 spin_lock_irqsave(&bnad->bna_lock, flags);
1881 bna_rx_disable(rx_info->rx, BNA_HARD_CLEANUP, bnad_cb_rx_disabled);
1882 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1883 wait_for_completion(&bnad->bnad_completions.rx_comp);
1884
1885 if (rx_info->rx_ctrl[0].ccb->intr_type == BNA_INTR_T_MSIX)
1886 bnad_rx_msix_unregister(bnad, rx_info, rx_config->num_paths);
1887
Rasesh Mody2be67142011-08-30 15:27:39 +00001888 bnad_napi_disable(bnad, rx_id);
1889
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001890 spin_lock_irqsave(&bnad->bna_lock, flags);
1891 bna_rx_destroy(rx_info->rx);
1892 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1893
1894 rx_info->rx = NULL;
1895
1896 bnad_rx_res_free(bnad, res_info);
1897}
1898
1899/* Called with mutex_lock(&bnad->conf_mutex) held */
1900int
Rasesh Mody078086f2011-08-08 16:21:39 +00001901bnad_setup_rx(struct bnad *bnad, u32 rx_id)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001902{
1903 int err;
1904 struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
1905 struct bna_res_info *res_info = &bnad->rx_res_info[rx_id].res_info[0];
1906 struct bna_intr_info *intr_info =
1907 &res_info[BNA_RX_RES_T_INTR].res_u.intr_info;
1908 struct bna_rx_config *rx_config = &bnad->rx_config[rx_id];
1909 struct bna_rx_event_cbfn rx_cbfn;
1910 struct bna_rx *rx;
1911 unsigned long flags;
1912
Rasesh Mody078086f2011-08-08 16:21:39 +00001913 rx_info->rx_id = rx_id;
1914
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001915 /* Initialize the Rx object configuration */
1916 bnad_init_rx_config(bnad, rx_config);
1917
1918 /* Initialize the Rx event handlers */
1919 rx_cbfn.rcb_setup_cbfn = bnad_cb_rcb_setup;
Rasesh Modybe7fa322010-12-23 21:45:01 +00001920 rx_cbfn.rcb_destroy_cbfn = bnad_cb_rcb_destroy;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001921 rx_cbfn.ccb_setup_cbfn = bnad_cb_ccb_setup;
1922 rx_cbfn.ccb_destroy_cbfn = bnad_cb_ccb_destroy;
1923 rx_cbfn.rx_cleanup_cbfn = bnad_cb_rx_cleanup;
1924 rx_cbfn.rx_post_cbfn = bnad_cb_rx_post;
1925
1926 /* Get BNA's resource requirement for one Rx object */
1927 spin_lock_irqsave(&bnad->bna_lock, flags);
1928 bna_rx_res_req(rx_config, res_info);
1929 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1930
1931 /* Fill Unmap Q memory requirements */
1932 BNAD_FILL_UNMAPQ_MEM_REQ(
1933 &res_info[BNA_RX_RES_MEM_T_UNMAPQ],
1934 rx_config->num_paths +
1935 ((rx_config->rxp_type == BNA_RXP_SINGLE) ? 0 :
1936 rx_config->num_paths), BNAD_RX_UNMAPQ_DEPTH);
1937
1938 /* Allocate resource */
1939 err = bnad_rx_res_alloc(bnad, res_info, rx_id);
1940 if (err)
1941 return err;
1942
Rasesh Mody2be67142011-08-30 15:27:39 +00001943 bnad_rx_ctrl_init(bnad, rx_id);
1944
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001945 /* Ask BNA to create one Rx object, supplying required resources */
1946 spin_lock_irqsave(&bnad->bna_lock, flags);
1947 rx = bna_rx_create(&bnad->bna, bnad, rx_config, &rx_cbfn, res_info,
1948 rx_info);
1949 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1950 if (!rx)
1951 goto err_return;
1952 rx_info->rx = rx;
1953
Rasesh Mody2be67142011-08-30 15:27:39 +00001954 /*
1955 * Init NAPI, so that state is set to NAPI_STATE_SCHED,
1956 * so that IRQ handler cannot schedule NAPI at this point.
1957 */
1958 bnad_napi_init(bnad, rx_id);
1959
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001960 /* Register ISR for the Rx object */
1961 if (intr_info->intr_type == BNA_INTR_T_MSIX) {
1962 err = bnad_rx_msix_register(bnad, rx_info, rx_id,
1963 rx_config->num_paths);
1964 if (err)
1965 goto err_return;
1966 }
1967
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001968 spin_lock_irqsave(&bnad->bna_lock, flags);
1969 if (0 == rx_id) {
1970 /* Set up Dynamic Interrupt Moderation Vector */
1971 if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED)
1972 bna_rx_dim_reconfig(&bnad->bna, bna_napi_dim_vector);
1973
1974 /* Enable VLAN filtering only on the default Rx */
1975 bna_rx_vlanfilter_enable(rx);
1976
1977 /* Start the DIM timer */
1978 bnad_dim_timer_start(bnad);
1979 }
1980
1981 bna_rx_enable(rx);
1982 spin_unlock_irqrestore(&bnad->bna_lock, flags);
1983
Rasesh Mody2be67142011-08-30 15:27:39 +00001984 /* Enable scheduling of NAPI */
1985 bnad_napi_enable(bnad, rx_id);
1986
Rasesh Mody8b230ed2010-08-23 20:24:12 -07001987 return 0;
1988
1989err_return:
1990 bnad_cleanup_rx(bnad, rx_id);
1991 return err;
1992}
1993
1994/* Called with conf_lock & bnad->bna_lock held */
1995void
1996bnad_tx_coalescing_timeo_set(struct bnad *bnad)
1997{
1998 struct bnad_tx_info *tx_info;
1999
2000 tx_info = &bnad->tx_info[0];
2001 if (!tx_info->tx)
2002 return;
2003
2004 bna_tx_coalescing_timeo_set(tx_info->tx, bnad->tx_coalescing_timeo);
2005}
2006
2007/* Called with conf_lock & bnad->bna_lock held */
2008void
2009bnad_rx_coalescing_timeo_set(struct bnad *bnad)
2010{
2011 struct bnad_rx_info *rx_info;
Rasesh Mody0120b992011-07-22 08:07:41 +00002012 int i;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002013
2014 for (i = 0; i < bnad->num_rx; i++) {
2015 rx_info = &bnad->rx_info[i];
2016 if (!rx_info->rx)
2017 continue;
2018 bna_rx_coalescing_timeo_set(rx_info->rx,
2019 bnad->rx_coalescing_timeo);
2020 }
2021}
2022
2023/*
2024 * Called with bnad->bna_lock held
2025 */
2026static int
2027bnad_mac_addr_set_locked(struct bnad *bnad, u8 *mac_addr)
2028{
2029 int ret;
2030
2031 if (!is_valid_ether_addr(mac_addr))
2032 return -EADDRNOTAVAIL;
2033
2034 /* If datapath is down, pretend everything went through */
2035 if (!bnad->rx_info[0].rx)
2036 return 0;
2037
2038 ret = bna_rx_ucast_set(bnad->rx_info[0].rx, mac_addr, NULL);
2039 if (ret != BNA_CB_SUCCESS)
2040 return -EADDRNOTAVAIL;
2041
2042 return 0;
2043}
2044
2045/* Should be called with conf_lock held */
2046static int
2047bnad_enable_default_bcast(struct bnad *bnad)
2048{
2049 struct bnad_rx_info *rx_info = &bnad->rx_info[0];
2050 int ret;
2051 unsigned long flags;
2052
2053 init_completion(&bnad->bnad_completions.mcast_comp);
2054
2055 spin_lock_irqsave(&bnad->bna_lock, flags);
2056 ret = bna_rx_mcast_add(rx_info->rx, (u8 *)bnad_bcast_addr,
2057 bnad_cb_rx_mcast_add);
2058 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2059
2060 if (ret == BNA_CB_SUCCESS)
2061 wait_for_completion(&bnad->bnad_completions.mcast_comp);
2062 else
2063 return -ENODEV;
2064
2065 if (bnad->bnad_completions.mcast_comp_status != BNA_CB_SUCCESS)
2066 return -ENODEV;
2067
2068 return 0;
2069}
2070
Rasesh Mody19dbff92011-08-30 15:27:41 +00002071/* Called with mutex_lock(&bnad->conf_mutex) held */
Rasesh Modyaad75b62010-12-23 21:45:08 +00002072static void
2073bnad_restore_vlans(struct bnad *bnad, u32 rx_id)
2074{
Jiri Pirkof859d7c2011-07-20 04:54:14 +00002075 u16 vid;
Rasesh Modyaad75b62010-12-23 21:45:08 +00002076 unsigned long flags;
2077
Jiri Pirkof859d7c2011-07-20 04:54:14 +00002078 for_each_set_bit(vid, bnad->active_vlans, VLAN_N_VID) {
Rasesh Modyaad75b62010-12-23 21:45:08 +00002079 spin_lock_irqsave(&bnad->bna_lock, flags);
Jiri Pirkof859d7c2011-07-20 04:54:14 +00002080 bna_rx_vlan_add(bnad->rx_info[rx_id].rx, vid);
Rasesh Modyaad75b62010-12-23 21:45:08 +00002081 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2082 }
2083}
2084
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002085/* Statistics utilities */
2086void
Eric Dumazet250e0612010-09-02 12:45:02 -07002087bnad_netdev_qstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002088{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002089 int i, j;
2090
2091 for (i = 0; i < bnad->num_rx; i++) {
2092 for (j = 0; j < bnad->num_rxp_per_rx; j++) {
2093 if (bnad->rx_info[i].rx_ctrl[j].ccb) {
Eric Dumazet250e0612010-09-02 12:45:02 -07002094 stats->rx_packets += bnad->rx_info[i].
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002095 rx_ctrl[j].ccb->rcb[0]->rxq->rx_packets;
Eric Dumazet250e0612010-09-02 12:45:02 -07002096 stats->rx_bytes += bnad->rx_info[i].
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002097 rx_ctrl[j].ccb->rcb[0]->rxq->rx_bytes;
2098 if (bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1] &&
2099 bnad->rx_info[i].rx_ctrl[j].ccb->
2100 rcb[1]->rxq) {
Eric Dumazet250e0612010-09-02 12:45:02 -07002101 stats->rx_packets +=
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002102 bnad->rx_info[i].rx_ctrl[j].
2103 ccb->rcb[1]->rxq->rx_packets;
Eric Dumazet250e0612010-09-02 12:45:02 -07002104 stats->rx_bytes +=
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002105 bnad->rx_info[i].rx_ctrl[j].
2106 ccb->rcb[1]->rxq->rx_bytes;
2107 }
2108 }
2109 }
2110 }
2111 for (i = 0; i < bnad->num_tx; i++) {
2112 for (j = 0; j < bnad->num_txq_per_tx; j++) {
2113 if (bnad->tx_info[i].tcb[j]) {
Eric Dumazet250e0612010-09-02 12:45:02 -07002114 stats->tx_packets +=
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002115 bnad->tx_info[i].tcb[j]->txq->tx_packets;
Eric Dumazet250e0612010-09-02 12:45:02 -07002116 stats->tx_bytes +=
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002117 bnad->tx_info[i].tcb[j]->txq->tx_bytes;
2118 }
2119 }
2120 }
2121}
2122
2123/*
2124 * Must be called with the bna_lock held.
2125 */
2126void
Eric Dumazet250e0612010-09-02 12:45:02 -07002127bnad_netdev_hwstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002128{
Rasesh Mody078086f2011-08-08 16:21:39 +00002129 struct bfi_enet_stats_mac *mac_stats;
2130 u32 bmap;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002131 int i;
2132
Rasesh Mody078086f2011-08-08 16:21:39 +00002133 mac_stats = &bnad->stats.bna_stats->hw_stats.mac_stats;
Eric Dumazet250e0612010-09-02 12:45:02 -07002134 stats->rx_errors =
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002135 mac_stats->rx_fcs_error + mac_stats->rx_alignment_error +
2136 mac_stats->rx_frame_length_error + mac_stats->rx_code_error +
2137 mac_stats->rx_undersize;
Eric Dumazet250e0612010-09-02 12:45:02 -07002138 stats->tx_errors = mac_stats->tx_fcs_error +
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002139 mac_stats->tx_undersize;
Eric Dumazet250e0612010-09-02 12:45:02 -07002140 stats->rx_dropped = mac_stats->rx_drop;
2141 stats->tx_dropped = mac_stats->tx_drop;
2142 stats->multicast = mac_stats->rx_multicast;
2143 stats->collisions = mac_stats->tx_total_collision;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002144
Eric Dumazet250e0612010-09-02 12:45:02 -07002145 stats->rx_length_errors = mac_stats->rx_frame_length_error;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002146
2147 /* receive ring buffer overflow ?? */
2148
Eric Dumazet250e0612010-09-02 12:45:02 -07002149 stats->rx_crc_errors = mac_stats->rx_fcs_error;
2150 stats->rx_frame_errors = mac_stats->rx_alignment_error;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002151 /* recv'r fifo overrun */
Rasesh Mody078086f2011-08-08 16:21:39 +00002152 bmap = bna_rx_rid_mask(&bnad->bna);
2153 for (i = 0; bmap; i++) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002154 if (bmap & 1) {
Eric Dumazet250e0612010-09-02 12:45:02 -07002155 stats->rx_fifo_errors +=
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002156 bnad->stats.bna_stats->
Rasesh Mody078086f2011-08-08 16:21:39 +00002157 hw_stats.rxf_stats[i].frame_drops;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002158 break;
2159 }
2160 bmap >>= 1;
2161 }
2162}
2163
2164static void
2165bnad_mbox_irq_sync(struct bnad *bnad)
2166{
2167 u32 irq;
2168 unsigned long flags;
2169
2170 spin_lock_irqsave(&bnad->bna_lock, flags);
2171 if (bnad->cfg_flags & BNAD_CF_MSIX)
Rasesh Mody8811e262011-07-22 08:07:44 +00002172 irq = bnad->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002173 else
2174 irq = bnad->pcidev->irq;
2175 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2176
2177 synchronize_irq(irq);
2178}
2179
2180/* Utility used by bnad_start_xmit, for doing TSO */
2181static int
2182bnad_tso_prepare(struct bnad *bnad, struct sk_buff *skb)
2183{
2184 int err;
2185
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002186 if (skb_header_cloned(skb)) {
2187 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2188 if (err) {
2189 BNAD_UPDATE_CTR(bnad, tso_err);
2190 return err;
2191 }
2192 }
2193
2194 /*
2195 * For TSO, the TCP checksum field is seeded with pseudo-header sum
2196 * excluding the length field.
2197 */
2198 if (skb->protocol == htons(ETH_P_IP)) {
2199 struct iphdr *iph = ip_hdr(skb);
2200
2201 /* Do we really need these? */
2202 iph->tot_len = 0;
2203 iph->check = 0;
2204
2205 tcp_hdr(skb)->check =
2206 ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
2207 IPPROTO_TCP, 0);
2208 BNAD_UPDATE_CTR(bnad, tso4);
2209 } else {
2210 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
2211
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002212 ipv6h->payload_len = 0;
2213 tcp_hdr(skb)->check =
2214 ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, 0,
2215 IPPROTO_TCP, 0);
2216 BNAD_UPDATE_CTR(bnad, tso6);
2217 }
2218
2219 return 0;
2220}
2221
2222/*
2223 * Initialize Q numbers depending on Rx Paths
2224 * Called with bnad->bna_lock held, because of cfg_flags
2225 * access.
2226 */
2227static void
2228bnad_q_num_init(struct bnad *bnad)
2229{
2230 int rxps;
2231
2232 rxps = min((uint)num_online_cpus(),
Rasesh Mody772b5232011-08-30 15:27:37 +00002233 (uint)(BNAD_MAX_RX * BNAD_MAX_RXP_PER_RX));
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002234
2235 if (!(bnad->cfg_flags & BNAD_CF_MSIX))
2236 rxps = 1; /* INTx */
2237
2238 bnad->num_rx = 1;
2239 bnad->num_tx = 1;
2240 bnad->num_rxp_per_rx = rxps;
2241 bnad->num_txq_per_tx = BNAD_TXQ_NUM;
2242}
2243
2244/*
2245 * Adjusts the Q numbers, given a number of msix vectors
2246 * Give preference to RSS as opposed to Tx priority Queues,
2247 * in such a case, just use 1 Tx Q
2248 * Called with bnad->bna_lock held b'cos of cfg_flags access
2249 */
2250static void
Rasesh Mody078086f2011-08-08 16:21:39 +00002251bnad_q_num_adjust(struct bnad *bnad, int msix_vectors, int temp)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002252{
2253 bnad->num_txq_per_tx = 1;
2254 if ((msix_vectors >= (bnad->num_tx * bnad->num_txq_per_tx) +
2255 bnad_rxqs_per_cq + BNAD_MAILBOX_MSIX_VECTORS) &&
2256 (bnad->cfg_flags & BNAD_CF_MSIX)) {
2257 bnad->num_rxp_per_rx = msix_vectors -
2258 (bnad->num_tx * bnad->num_txq_per_tx) -
2259 BNAD_MAILBOX_MSIX_VECTORS;
2260 } else
2261 bnad->num_rxp_per_rx = 1;
2262}
2263
Rasesh Mody078086f2011-08-08 16:21:39 +00002264/* Enable / disable ioceth */
2265static int
2266bnad_ioceth_disable(struct bnad *bnad)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002267{
2268 unsigned long flags;
Rasesh Mody078086f2011-08-08 16:21:39 +00002269 int err = 0;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002270
2271 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody078086f2011-08-08 16:21:39 +00002272 init_completion(&bnad->bnad_completions.ioc_comp);
2273 bna_ioceth_disable(&bnad->bna.ioceth, BNA_HARD_CLEANUP);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002274 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2275
Rasesh Mody078086f2011-08-08 16:21:39 +00002276 wait_for_completion_timeout(&bnad->bnad_completions.ioc_comp,
2277 msecs_to_jiffies(BNAD_IOCETH_TIMEOUT));
2278
2279 err = bnad->bnad_completions.ioc_comp_status;
2280 return err;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002281}
2282
2283static int
Rasesh Mody078086f2011-08-08 16:21:39 +00002284bnad_ioceth_enable(struct bnad *bnad)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002285{
2286 int err = 0;
2287 unsigned long flags;
2288
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002289 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody078086f2011-08-08 16:21:39 +00002290 init_completion(&bnad->bnad_completions.ioc_comp);
2291 bnad->bnad_completions.ioc_comp_status = BNA_CB_WAITING;
2292 bna_ioceth_enable(&bnad->bna.ioceth);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002293 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2294
Rasesh Mody078086f2011-08-08 16:21:39 +00002295 wait_for_completion_timeout(&bnad->bnad_completions.ioc_comp,
2296 msecs_to_jiffies(BNAD_IOCETH_TIMEOUT));
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002297
Rasesh Mody078086f2011-08-08 16:21:39 +00002298 err = bnad->bnad_completions.ioc_comp_status;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002299
2300 return err;
2301}
2302
2303/* Free BNA resources */
2304static void
Rasesh Mody078086f2011-08-08 16:21:39 +00002305bnad_res_free(struct bnad *bnad, struct bna_res_info *res_info,
2306 u32 res_val_max)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002307{
2308 int i;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002309
Rasesh Mody078086f2011-08-08 16:21:39 +00002310 for (i = 0; i < res_val_max; i++)
2311 bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002312}
2313
2314/* Allocates memory and interrupt resources for BNA */
2315static int
Rasesh Mody078086f2011-08-08 16:21:39 +00002316bnad_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
2317 u32 res_val_max)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002318{
2319 int i, err;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002320
Rasesh Mody078086f2011-08-08 16:21:39 +00002321 for (i = 0; i < res_val_max; i++) {
2322 err = bnad_mem_alloc(bnad, &res_info[i].res_u.mem_info);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002323 if (err)
2324 goto err_return;
2325 }
2326 return 0;
2327
2328err_return:
Rasesh Mody078086f2011-08-08 16:21:39 +00002329 bnad_res_free(bnad, res_info, res_val_max);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002330 return err;
2331}
2332
2333/* Interrupt enable / disable */
2334static void
2335bnad_enable_msix(struct bnad *bnad)
2336{
2337 int i, ret;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002338 unsigned long flags;
2339
2340 spin_lock_irqsave(&bnad->bna_lock, flags);
2341 if (!(bnad->cfg_flags & BNAD_CF_MSIX)) {
2342 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2343 return;
2344 }
2345 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2346
2347 if (bnad->msix_table)
2348 return;
2349
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002350 bnad->msix_table =
Rasesh Modyb7ee31c52010-10-05 15:46:05 +00002351 kcalloc(bnad->msix_num, sizeof(struct msix_entry), GFP_KERNEL);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002352
2353 if (!bnad->msix_table)
2354 goto intx_mode;
2355
Rasesh Modyb7ee31c52010-10-05 15:46:05 +00002356 for (i = 0; i < bnad->msix_num; i++)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002357 bnad->msix_table[i].entry = i;
2358
Rasesh Modyb7ee31c52010-10-05 15:46:05 +00002359 ret = pci_enable_msix(bnad->pcidev, bnad->msix_table, bnad->msix_num);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002360 if (ret > 0) {
2361 /* Not enough MSI-X vectors. */
Rasesh Mody19dbff92011-08-30 15:27:41 +00002362 pr_warn("BNA: %d MSI-X vectors allocated < %d requested\n",
2363 ret, bnad->msix_num);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002364
2365 spin_lock_irqsave(&bnad->bna_lock, flags);
2366 /* ret = #of vectors that we got */
Rasesh Mody271e8b72011-08-30 15:27:40 +00002367 bnad_q_num_adjust(bnad, (ret - BNAD_MAILBOX_MSIX_VECTORS) / 2,
2368 (ret - BNAD_MAILBOX_MSIX_VECTORS) / 2);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002369 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2370
Rasesh Mody271e8b72011-08-30 15:27:40 +00002371 bnad->msix_num = BNAD_NUM_TXQ + BNAD_NUM_RXP +
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002372 BNAD_MAILBOX_MSIX_VECTORS;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002373
Rasesh Mody078086f2011-08-08 16:21:39 +00002374 if (bnad->msix_num > ret)
2375 goto intx_mode;
2376
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002377 /* Try once more with adjusted numbers */
2378 /* If this fails, fall back to INTx */
2379 ret = pci_enable_msix(bnad->pcidev, bnad->msix_table,
Rasesh Modyb7ee31c52010-10-05 15:46:05 +00002380 bnad->msix_num);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002381 if (ret)
2382 goto intx_mode;
2383
2384 } else if (ret < 0)
2385 goto intx_mode;
Rasesh Mody078086f2011-08-08 16:21:39 +00002386
2387 pci_intx(bnad->pcidev, 0);
2388
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002389 return;
2390
2391intx_mode:
Rasesh Mody19dbff92011-08-30 15:27:41 +00002392 pr_warn("BNA: MSI-X enable failed - operating in INTx mode\n");
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002393
2394 kfree(bnad->msix_table);
2395 bnad->msix_table = NULL;
2396 bnad->msix_num = 0;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002397 spin_lock_irqsave(&bnad->bna_lock, flags);
2398 bnad->cfg_flags &= ~BNAD_CF_MSIX;
2399 bnad_q_num_init(bnad);
2400 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2401}
2402
2403static void
2404bnad_disable_msix(struct bnad *bnad)
2405{
2406 u32 cfg_flags;
2407 unsigned long flags;
2408
2409 spin_lock_irqsave(&bnad->bna_lock, flags);
2410 cfg_flags = bnad->cfg_flags;
2411 if (bnad->cfg_flags & BNAD_CF_MSIX)
2412 bnad->cfg_flags &= ~BNAD_CF_MSIX;
2413 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2414
2415 if (cfg_flags & BNAD_CF_MSIX) {
2416 pci_disable_msix(bnad->pcidev);
2417 kfree(bnad->msix_table);
2418 bnad->msix_table = NULL;
2419 }
2420}
2421
2422/* Netdev entry points */
2423static int
2424bnad_open(struct net_device *netdev)
2425{
2426 int err;
2427 struct bnad *bnad = netdev_priv(netdev);
2428 struct bna_pause_config pause_config;
2429 int mtu;
2430 unsigned long flags;
2431
2432 mutex_lock(&bnad->conf_mutex);
2433
2434 /* Tx */
2435 err = bnad_setup_tx(bnad, 0);
2436 if (err)
2437 goto err_return;
2438
2439 /* Rx */
2440 err = bnad_setup_rx(bnad, 0);
2441 if (err)
2442 goto cleanup_tx;
2443
2444 /* Port */
2445 pause_config.tx_pause = 0;
2446 pause_config.rx_pause = 0;
2447
Rasesh Mody078086f2011-08-08 16:21:39 +00002448 mtu = ETH_HLEN + VLAN_HLEN + bnad->netdev->mtu + ETH_FCS_LEN;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002449
2450 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody078086f2011-08-08 16:21:39 +00002451 bna_enet_mtu_set(&bnad->bna.enet, mtu, NULL);
2452 bna_enet_pause_config(&bnad->bna.enet, &pause_config, NULL);
2453 bna_enet_enable(&bnad->bna.enet);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002454 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2455
2456 /* Enable broadcast */
2457 bnad_enable_default_bcast(bnad);
2458
Rasesh Modyaad75b62010-12-23 21:45:08 +00002459 /* Restore VLANs, if any */
2460 bnad_restore_vlans(bnad, 0);
2461
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002462 /* Set the UCAST address */
2463 spin_lock_irqsave(&bnad->bna_lock, flags);
2464 bnad_mac_addr_set_locked(bnad, netdev->dev_addr);
2465 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2466
2467 /* Start the stats timer */
2468 bnad_stats_timer_start(bnad);
2469
2470 mutex_unlock(&bnad->conf_mutex);
2471
2472 return 0;
2473
2474cleanup_tx:
2475 bnad_cleanup_tx(bnad, 0);
2476
2477err_return:
2478 mutex_unlock(&bnad->conf_mutex);
2479 return err;
2480}
2481
2482static int
2483bnad_stop(struct net_device *netdev)
2484{
2485 struct bnad *bnad = netdev_priv(netdev);
2486 unsigned long flags;
2487
2488 mutex_lock(&bnad->conf_mutex);
2489
2490 /* Stop the stats timer */
2491 bnad_stats_timer_stop(bnad);
2492
Rasesh Mody078086f2011-08-08 16:21:39 +00002493 init_completion(&bnad->bnad_completions.enet_comp);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002494
2495 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody078086f2011-08-08 16:21:39 +00002496 bna_enet_disable(&bnad->bna.enet, BNA_HARD_CLEANUP,
2497 bnad_cb_enet_disabled);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002498 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2499
Rasesh Mody078086f2011-08-08 16:21:39 +00002500 wait_for_completion(&bnad->bnad_completions.enet_comp);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002501
2502 bnad_cleanup_tx(bnad, 0);
2503 bnad_cleanup_rx(bnad, 0);
2504
2505 /* Synchronize mailbox IRQ */
2506 bnad_mbox_irq_sync(bnad);
2507
2508 mutex_unlock(&bnad->conf_mutex);
2509
2510 return 0;
2511}
2512
2513/* TX */
2514/*
2515 * bnad_start_xmit : Netdev entry point for Transmit
2516 * Called under lock held by net_device
2517 */
2518static netdev_tx_t
2519bnad_start_xmit(struct sk_buff *skb, struct net_device *netdev)
2520{
2521 struct bnad *bnad = netdev_priv(netdev);
Rasesh Mody078086f2011-08-08 16:21:39 +00002522 u32 txq_id = 0;
2523 struct bna_tcb *tcb = bnad->tx_info[0].tcb[txq_id];
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002524
Rasesh Mody0120b992011-07-22 08:07:41 +00002525 u16 txq_prod, vlan_tag = 0;
2526 u32 unmap_prod, wis, wis_used, wi_range;
2527 u32 vectors, vect_id, i, acked;
Rasesh Mody0120b992011-07-22 08:07:41 +00002528 int err;
Rasesh Mody271e8b72011-08-30 15:27:40 +00002529 unsigned int len;
2530 u32 gso_size;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002531
Rasesh Mody078086f2011-08-08 16:21:39 +00002532 struct bnad_unmap_q *unmap_q = tcb->unmap_q;
Rasesh Mody0120b992011-07-22 08:07:41 +00002533 dma_addr_t dma_addr;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002534 struct bna_txq_entry *txqent;
Rasesh Mody078086f2011-08-08 16:21:39 +00002535 u16 flags;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002536
Rasesh Mody271e8b72011-08-30 15:27:40 +00002537 if (unlikely(skb->len <= ETH_HLEN)) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002538 dev_kfree_skb(skb);
Rasesh Mody271e8b72011-08-30 15:27:40 +00002539 BNAD_UPDATE_CTR(bnad, tx_skb_too_short);
2540 return NETDEV_TX_OK;
2541 }
2542 if (unlikely(skb_headlen(skb) > BFI_TX_MAX_DATA_PER_VECTOR)) {
2543 dev_kfree_skb(skb);
2544 BNAD_UPDATE_CTR(bnad, tx_skb_headlen_too_long);
2545 return NETDEV_TX_OK;
2546 }
2547 if (unlikely(skb_headlen(skb) == 0)) {
2548 dev_kfree_skb(skb);
2549 BNAD_UPDATE_CTR(bnad, tx_skb_headlen_zero);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002550 return NETDEV_TX_OK;
2551 }
2552
Rasesh Modybe7fa322010-12-23 21:45:01 +00002553 /*
2554 * Takes care of the Tx that is scheduled between clearing the flag
Rasesh Mody19dbff92011-08-30 15:27:41 +00002555 * and the netif_tx_stop_all_queues() call.
Rasesh Modybe7fa322010-12-23 21:45:01 +00002556 */
2557 if (unlikely(!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))) {
2558 dev_kfree_skb(skb);
Rasesh Mody271e8b72011-08-30 15:27:40 +00002559 BNAD_UPDATE_CTR(bnad, tx_skb_stopping);
Rasesh Modybe7fa322010-12-23 21:45:01 +00002560 return NETDEV_TX_OK;
2561 }
2562
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002563 vectors = 1 + skb_shinfo(skb)->nr_frags;
Rasesh Mody271e8b72011-08-30 15:27:40 +00002564 if (unlikely(vectors > BFI_TX_MAX_VECTORS_PER_PKT)) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002565 dev_kfree_skb(skb);
Rasesh Mody271e8b72011-08-30 15:27:40 +00002566 BNAD_UPDATE_CTR(bnad, tx_skb_max_vectors);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002567 return NETDEV_TX_OK;
2568 }
2569 wis = BNA_TXQ_WI_NEEDED(vectors); /* 4 vectors per work item */
2570 acked = 0;
Rasesh Mody078086f2011-08-08 16:21:39 +00002571 if (unlikely(wis > BNA_QE_FREE_CNT(tcb, tcb->q_depth) ||
2572 vectors > BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth))) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002573 if ((u16) (*tcb->hw_consumer_index) !=
2574 tcb->consumer_index &&
2575 !test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags)) {
2576 acked = bnad_free_txbufs(bnad, tcb);
Rasesh Modybe7fa322010-12-23 21:45:01 +00002577 if (likely(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
2578 bna_ib_ack(tcb->i_dbell, acked);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002579 smp_mb__before_clear_bit();
2580 clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
2581 } else {
2582 netif_stop_queue(netdev);
2583 BNAD_UPDATE_CTR(bnad, netif_queue_stop);
2584 }
2585
2586 smp_mb();
2587 /*
2588 * Check again to deal with race condition between
2589 * netif_stop_queue here, and netif_wake_queue in
2590 * interrupt handler which is not inside netif tx lock.
2591 */
2592 if (likely
2593 (wis > BNA_QE_FREE_CNT(tcb, tcb->q_depth) ||
2594 vectors > BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth))) {
2595 BNAD_UPDATE_CTR(bnad, netif_queue_stop);
2596 return NETDEV_TX_BUSY;
2597 } else {
2598 netif_wake_queue(netdev);
2599 BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
2600 }
2601 }
2602
2603 unmap_prod = unmap_q->producer_index;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002604 flags = 0;
2605
2606 txq_prod = tcb->producer_index;
2607 BNA_TXQ_QPGE_PTR_GET(txq_prod, tcb->sw_qpt, txqent, wi_range);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002608 txqent->hdr.wi.reserved = 0;
2609 txqent->hdr.wi.num_vectors = vectors;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002610
Jesse Grosseab6d182010-10-20 13:56:03 +00002611 if (vlan_tx_tag_present(skb)) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002612 vlan_tag = (u16) vlan_tx_tag_get(skb);
2613 flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
2614 }
2615 if (test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags)) {
2616 vlan_tag =
2617 (tcb->priority & 0x7) << 13 | (vlan_tag & 0x1fff);
2618 flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
2619 }
2620
2621 txqent->hdr.wi.vlan_tag = htons(vlan_tag);
2622
2623 if (skb_is_gso(skb)) {
Rasesh Mody271e8b72011-08-30 15:27:40 +00002624 gso_size = skb_shinfo(skb)->gso_size;
2625
2626 if (unlikely(gso_size > netdev->mtu)) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002627 dev_kfree_skb(skb);
Rasesh Mody271e8b72011-08-30 15:27:40 +00002628 BNAD_UPDATE_CTR(bnad, tx_skb_mss_too_long);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002629 return NETDEV_TX_OK;
2630 }
Rasesh Mody271e8b72011-08-30 15:27:40 +00002631 if (unlikely((gso_size + skb_transport_offset(skb) +
2632 tcp_hdrlen(skb)) >= skb->len)) {
2633 txqent->hdr.wi.opcode =
2634 __constant_htons(BNA_TXQ_WI_SEND);
2635 txqent->hdr.wi.lso_mss = 0;
2636 BNAD_UPDATE_CTR(bnad, tx_skb_tso_too_short);
2637 } else {
2638 txqent->hdr.wi.opcode =
2639 __constant_htons(BNA_TXQ_WI_SEND_LSO);
2640 txqent->hdr.wi.lso_mss = htons(gso_size);
2641 }
2642
2643 err = bnad_tso_prepare(bnad, skb);
2644 if (unlikely(err)) {
2645 dev_kfree_skb(skb);
2646 BNAD_UPDATE_CTR(bnad, tx_skb_tso_prepare);
2647 return NETDEV_TX_OK;
2648 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002649 flags |= (BNA_TXQ_WI_CF_IP_CKSUM | BNA_TXQ_WI_CF_TCP_CKSUM);
2650 txqent->hdr.wi.l4_hdr_size_n_offset =
2651 htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
2652 (tcp_hdrlen(skb) >> 2,
2653 skb_transport_offset(skb)));
Rasesh Mody271e8b72011-08-30 15:27:40 +00002654 } else {
2655 txqent->hdr.wi.opcode = __constant_htons(BNA_TXQ_WI_SEND);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002656 txqent->hdr.wi.lso_mss = 0;
2657
Rasesh Mody271e8b72011-08-30 15:27:40 +00002658 if (unlikely(skb->len > (netdev->mtu + ETH_HLEN))) {
2659 dev_kfree_skb(skb);
2660 BNAD_UPDATE_CTR(bnad, tx_skb_non_tso_too_long);
2661 return NETDEV_TX_OK;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002662 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002663
Rasesh Mody271e8b72011-08-30 15:27:40 +00002664 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2665 u8 proto = 0;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002666
Rasesh Mody271e8b72011-08-30 15:27:40 +00002667 if (skb->protocol == __constant_htons(ETH_P_IP))
2668 proto = ip_hdr(skb)->protocol;
2669 else if (skb->protocol ==
2670 __constant_htons(ETH_P_IPV6)) {
2671 /* nexthdr may not be TCP immediately. */
2672 proto = ipv6_hdr(skb)->nexthdr;
2673 }
2674 if (proto == IPPROTO_TCP) {
2675 flags |= BNA_TXQ_WI_CF_TCP_CKSUM;
2676 txqent->hdr.wi.l4_hdr_size_n_offset =
2677 htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
2678 (0, skb_transport_offset(skb)));
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002679
Rasesh Mody271e8b72011-08-30 15:27:40 +00002680 BNAD_UPDATE_CTR(bnad, tcpcsum_offload);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002681
Rasesh Mody271e8b72011-08-30 15:27:40 +00002682 if (unlikely(skb_headlen(skb) <
2683 skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2684 dev_kfree_skb(skb);
2685 BNAD_UPDATE_CTR(bnad, tx_skb_tcp_hdr);
2686 return NETDEV_TX_OK;
2687 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002688
Rasesh Mody271e8b72011-08-30 15:27:40 +00002689 } else if (proto == IPPROTO_UDP) {
2690 flags |= BNA_TXQ_WI_CF_UDP_CKSUM;
2691 txqent->hdr.wi.l4_hdr_size_n_offset =
2692 htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
2693 (0, skb_transport_offset(skb)));
2694
2695 BNAD_UPDATE_CTR(bnad, udpcsum_offload);
2696 if (unlikely(skb_headlen(skb) <
2697 skb_transport_offset(skb) +
2698 sizeof(struct udphdr))) {
2699 dev_kfree_skb(skb);
2700 BNAD_UPDATE_CTR(bnad, tx_skb_udp_hdr);
2701 return NETDEV_TX_OK;
2702 }
2703 } else {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002704 dev_kfree_skb(skb);
Rasesh Mody271e8b72011-08-30 15:27:40 +00002705 BNAD_UPDATE_CTR(bnad, tx_skb_csum_err);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002706 return NETDEV_TX_OK;
2707 }
Rasesh Mody271e8b72011-08-30 15:27:40 +00002708 } else {
2709 txqent->hdr.wi.l4_hdr_size_n_offset = 0;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002710 }
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002711 }
2712
2713 txqent->hdr.wi.flags = htons(flags);
2714
2715 txqent->hdr.wi.frame_length = htonl(skb->len);
2716
2717 unmap_q->unmap_array[unmap_prod].skb = skb;
Rasesh Mody271e8b72011-08-30 15:27:40 +00002718 len = skb_headlen(skb);
2719 txqent->vector[0].length = htons(len);
Ivan Vecera5ea74312011-02-02 04:37:02 +00002720 dma_addr = dma_map_single(&bnad->pcidev->dev, skb->data,
2721 skb_headlen(skb), DMA_TO_DEVICE);
2722 dma_unmap_addr_set(&unmap_q->unmap_array[unmap_prod], dma_addr,
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002723 dma_addr);
2724
Rasesh Mody271e8b72011-08-30 15:27:40 +00002725 BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[0].host_addr);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002726 BNA_QE_INDX_ADD(unmap_prod, 1, unmap_q->q_depth);
2727
Rasesh Mody271e8b72011-08-30 15:27:40 +00002728 vect_id = 0;
2729 wis_used = 1;
2730
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002731 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2732 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
Rasesh Mody078086f2011-08-08 16:21:39 +00002733 u16 size = frag->size;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002734
Rasesh Mody271e8b72011-08-30 15:27:40 +00002735 if (unlikely(size == 0)) {
2736 unmap_prod = unmap_q->producer_index;
2737
2738 unmap_prod = bnad_pci_unmap_skb(&bnad->pcidev->dev,
2739 unmap_q->unmap_array,
2740 unmap_prod, unmap_q->q_depth, skb,
2741 i);
2742 dev_kfree_skb(skb);
2743 BNAD_UPDATE_CTR(bnad, tx_skb_frag_zero);
2744 return NETDEV_TX_OK;
2745 }
2746
2747 len += size;
2748
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002749 if (++vect_id == BFI_TX_MAX_VECTORS_PER_WI) {
2750 vect_id = 0;
2751 if (--wi_range)
2752 txqent++;
2753 else {
2754 BNA_QE_INDX_ADD(txq_prod, wis_used,
2755 tcb->q_depth);
2756 wis_used = 0;
2757 BNA_TXQ_QPGE_PTR_GET(txq_prod, tcb->sw_qpt,
2758 txqent, wi_range);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002759 }
2760 wis_used++;
Rasesh Mody271e8b72011-08-30 15:27:40 +00002761 txqent->hdr.wi_ext.opcode =
2762 __constant_htons(BNA_TXQ_WI_EXTENSION);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002763 }
2764
2765 BUG_ON(!(size <= BFI_TX_MAX_DATA_PER_VECTOR));
2766 txqent->vector[vect_id].length = htons(size);
Ivan Vecera5ea74312011-02-02 04:37:02 +00002767 dma_addr = dma_map_page(&bnad->pcidev->dev, frag->page,
2768 frag->page_offset, size, DMA_TO_DEVICE);
2769 dma_unmap_addr_set(&unmap_q->unmap_array[unmap_prod], dma_addr,
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002770 dma_addr);
2771 BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[vect_id].host_addr);
2772 BNA_QE_INDX_ADD(unmap_prod, 1, unmap_q->q_depth);
2773 }
2774
Rasesh Mody271e8b72011-08-30 15:27:40 +00002775 if (unlikely(len != skb->len)) {
2776 unmap_prod = unmap_q->producer_index;
2777
2778 unmap_prod = bnad_pci_unmap_skb(&bnad->pcidev->dev,
2779 unmap_q->unmap_array, unmap_prod,
2780 unmap_q->q_depth, skb,
2781 skb_shinfo(skb)->nr_frags);
2782 dev_kfree_skb(skb);
2783 BNAD_UPDATE_CTR(bnad, tx_skb_len_mismatch);
2784 return NETDEV_TX_OK;
2785 }
2786
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002787 unmap_q->producer_index = unmap_prod;
2788 BNA_QE_INDX_ADD(txq_prod, wis_used, tcb->q_depth);
2789 tcb->producer_index = txq_prod;
2790
2791 smp_mb();
Rasesh Modybe7fa322010-12-23 21:45:01 +00002792
2793 if (unlikely(!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
2794 return NETDEV_TX_OK;
2795
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002796 bna_txq_prod_indx_doorbell(tcb);
Rasesh Mody271e8b72011-08-30 15:27:40 +00002797 smp_mb();
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002798
2799 if ((u16) (*tcb->hw_consumer_index) != tcb->consumer_index)
2800 tasklet_schedule(&bnad->tx_free_tasklet);
2801
2802 return NETDEV_TX_OK;
2803}
2804
2805/*
2806 * Used spin_lock to synchronize reading of stats structures, which
2807 * is written by BNA under the same lock.
2808 */
Eric Dumazet250e0612010-09-02 12:45:02 -07002809static struct rtnl_link_stats64 *
2810bnad_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002811{
2812 struct bnad *bnad = netdev_priv(netdev);
2813 unsigned long flags;
2814
2815 spin_lock_irqsave(&bnad->bna_lock, flags);
2816
Eric Dumazet250e0612010-09-02 12:45:02 -07002817 bnad_netdev_qstats_fill(bnad, stats);
2818 bnad_netdev_hwstats_fill(bnad, stats);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002819
2820 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2821
Eric Dumazet250e0612010-09-02 12:45:02 -07002822 return stats;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002823}
2824
2825static void
2826bnad_set_rx_mode(struct net_device *netdev)
2827{
2828 struct bnad *bnad = netdev_priv(netdev);
2829 u32 new_mask, valid_mask;
2830 unsigned long flags;
2831
2832 spin_lock_irqsave(&bnad->bna_lock, flags);
2833
2834 new_mask = valid_mask = 0;
2835
2836 if (netdev->flags & IFF_PROMISC) {
2837 if (!(bnad->cfg_flags & BNAD_CF_PROMISC)) {
2838 new_mask = BNAD_RXMODE_PROMISC_DEFAULT;
2839 valid_mask = BNAD_RXMODE_PROMISC_DEFAULT;
2840 bnad->cfg_flags |= BNAD_CF_PROMISC;
2841 }
2842 } else {
2843 if (bnad->cfg_flags & BNAD_CF_PROMISC) {
2844 new_mask = ~BNAD_RXMODE_PROMISC_DEFAULT;
2845 valid_mask = BNAD_RXMODE_PROMISC_DEFAULT;
2846 bnad->cfg_flags &= ~BNAD_CF_PROMISC;
2847 }
2848 }
2849
2850 if (netdev->flags & IFF_ALLMULTI) {
2851 if (!(bnad->cfg_flags & BNAD_CF_ALLMULTI)) {
2852 new_mask |= BNA_RXMODE_ALLMULTI;
2853 valid_mask |= BNA_RXMODE_ALLMULTI;
2854 bnad->cfg_flags |= BNAD_CF_ALLMULTI;
2855 }
2856 } else {
2857 if (bnad->cfg_flags & BNAD_CF_ALLMULTI) {
2858 new_mask &= ~BNA_RXMODE_ALLMULTI;
2859 valid_mask |= BNA_RXMODE_ALLMULTI;
2860 bnad->cfg_flags &= ~BNAD_CF_ALLMULTI;
2861 }
2862 }
2863
Rasesh Mody271e8b72011-08-30 15:27:40 +00002864 if (bnad->rx_info[0].rx == NULL)
2865 goto unlock;
2866
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002867 bna_rx_mode_set(bnad->rx_info[0].rx, new_mask, valid_mask, NULL);
2868
2869 if (!netdev_mc_empty(netdev)) {
2870 u8 *mcaddr_list;
2871 int mc_count = netdev_mc_count(netdev);
2872
2873 /* Index 0 holds the broadcast address */
2874 mcaddr_list =
2875 kzalloc((mc_count + 1) * ETH_ALEN,
2876 GFP_ATOMIC);
2877 if (!mcaddr_list)
Jiri Slabyca1cef32010-09-04 02:08:41 +00002878 goto unlock;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002879
2880 memcpy(&mcaddr_list[0], &bnad_bcast_addr[0], ETH_ALEN);
2881
2882 /* Copy rest of the MC addresses */
2883 bnad_netdev_mc_list_get(netdev, mcaddr_list);
2884
2885 bna_rx_mcast_listset(bnad->rx_info[0].rx, mc_count + 1,
2886 mcaddr_list, NULL);
2887
2888 /* Should we enable BNAD_CF_ALLMULTI for err != 0 ? */
2889 kfree(mcaddr_list);
2890 }
Jiri Slabyca1cef32010-09-04 02:08:41 +00002891unlock:
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002892 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2893}
2894
2895/*
2896 * bna_lock is used to sync writes to netdev->addr
2897 * conf_lock cannot be used since this call may be made
2898 * in a non-blocking context.
2899 */
2900static int
2901bnad_set_mac_address(struct net_device *netdev, void *mac_addr)
2902{
2903 int err;
2904 struct bnad *bnad = netdev_priv(netdev);
2905 struct sockaddr *sa = (struct sockaddr *)mac_addr;
2906 unsigned long flags;
2907
2908 spin_lock_irqsave(&bnad->bna_lock, flags);
2909
2910 err = bnad_mac_addr_set_locked(bnad, sa->sa_data);
2911
2912 if (!err)
2913 memcpy(netdev->dev_addr, sa->sa_data, netdev->addr_len);
2914
2915 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2916
2917 return err;
2918}
2919
2920static int
Rasesh Mody078086f2011-08-08 16:21:39 +00002921bnad_mtu_set(struct bnad *bnad, int mtu)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002922{
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002923 unsigned long flags;
2924
Rasesh Mody078086f2011-08-08 16:21:39 +00002925 init_completion(&bnad->bnad_completions.mtu_comp);
2926
2927 spin_lock_irqsave(&bnad->bna_lock, flags);
2928 bna_enet_mtu_set(&bnad->bna.enet, mtu, bnad_cb_enet_mtu_set);
2929 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2930
2931 wait_for_completion(&bnad->bnad_completions.mtu_comp);
2932
2933 return bnad->bnad_completions.mtu_comp_status;
2934}
2935
2936static int
2937bnad_change_mtu(struct net_device *netdev, int new_mtu)
2938{
2939 int err, mtu = netdev->mtu;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002940 struct bnad *bnad = netdev_priv(netdev);
2941
2942 if (new_mtu + ETH_HLEN < ETH_ZLEN || new_mtu > BNAD_JUMBO_MTU)
2943 return -EINVAL;
2944
2945 mutex_lock(&bnad->conf_mutex);
2946
2947 netdev->mtu = new_mtu;
2948
Rasesh Mody078086f2011-08-08 16:21:39 +00002949 mtu = ETH_HLEN + VLAN_HLEN + new_mtu + ETH_FCS_LEN;
2950 err = bnad_mtu_set(bnad, mtu);
2951 if (err)
2952 err = -EBUSY;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002953
2954 mutex_unlock(&bnad->conf_mutex);
2955 return err;
2956}
2957
2958static void
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002959bnad_vlan_rx_add_vid(struct net_device *netdev,
2960 unsigned short vid)
2961{
2962 struct bnad *bnad = netdev_priv(netdev);
2963 unsigned long flags;
2964
2965 if (!bnad->rx_info[0].rx)
2966 return;
2967
2968 mutex_lock(&bnad->conf_mutex);
2969
2970 spin_lock_irqsave(&bnad->bna_lock, flags);
2971 bna_rx_vlan_add(bnad->rx_info[0].rx, vid);
Jiri Pirkof859d7c2011-07-20 04:54:14 +00002972 set_bit(vid, bnad->active_vlans);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002973 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2974
2975 mutex_unlock(&bnad->conf_mutex);
2976}
2977
2978static void
2979bnad_vlan_rx_kill_vid(struct net_device *netdev,
2980 unsigned short vid)
2981{
2982 struct bnad *bnad = netdev_priv(netdev);
2983 unsigned long flags;
2984
2985 if (!bnad->rx_info[0].rx)
2986 return;
2987
2988 mutex_lock(&bnad->conf_mutex);
2989
2990 spin_lock_irqsave(&bnad->bna_lock, flags);
Jiri Pirkof859d7c2011-07-20 04:54:14 +00002991 clear_bit(vid, bnad->active_vlans);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07002992 bna_rx_vlan_del(bnad->rx_info[0].rx, vid);
2993 spin_unlock_irqrestore(&bnad->bna_lock, flags);
2994
2995 mutex_unlock(&bnad->conf_mutex);
2996}
2997
2998#ifdef CONFIG_NET_POLL_CONTROLLER
2999static void
3000bnad_netpoll(struct net_device *netdev)
3001{
3002 struct bnad *bnad = netdev_priv(netdev);
3003 struct bnad_rx_info *rx_info;
3004 struct bnad_rx_ctrl *rx_ctrl;
3005 u32 curr_mask;
3006 int i, j;
3007
3008 if (!(bnad->cfg_flags & BNAD_CF_MSIX)) {
3009 bna_intx_disable(&bnad->bna, curr_mask);
3010 bnad_isr(bnad->pcidev->irq, netdev);
3011 bna_intx_enable(&bnad->bna, curr_mask);
3012 } else {
Rasesh Mody19dbff92011-08-30 15:27:41 +00003013 /*
3014 * Tx processing may happen in sending context, so no need
3015 * to explicitly process completions here
3016 */
3017
3018 /* Rx processing */
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003019 for (i = 0; i < bnad->num_rx; i++) {
3020 rx_info = &bnad->rx_info[i];
3021 if (!rx_info->rx)
3022 continue;
3023 for (j = 0; j < bnad->num_rxp_per_rx; j++) {
3024 rx_ctrl = &rx_info->rx_ctrl[j];
Rasesh Mody271e8b72011-08-30 15:27:40 +00003025 if (rx_ctrl->ccb)
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003026 bnad_netif_rx_schedule_poll(bnad,
3027 rx_ctrl->ccb);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003028 }
3029 }
3030 }
3031}
3032#endif
3033
3034static const struct net_device_ops bnad_netdev_ops = {
3035 .ndo_open = bnad_open,
3036 .ndo_stop = bnad_stop,
3037 .ndo_start_xmit = bnad_start_xmit,
Eric Dumazet250e0612010-09-02 12:45:02 -07003038 .ndo_get_stats64 = bnad_get_stats64,
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003039 .ndo_set_rx_mode = bnad_set_rx_mode,
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003040 .ndo_validate_addr = eth_validate_addr,
3041 .ndo_set_mac_address = bnad_set_mac_address,
3042 .ndo_change_mtu = bnad_change_mtu,
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003043 .ndo_vlan_rx_add_vid = bnad_vlan_rx_add_vid,
3044 .ndo_vlan_rx_kill_vid = bnad_vlan_rx_kill_vid,
3045#ifdef CONFIG_NET_POLL_CONTROLLER
3046 .ndo_poll_controller = bnad_netpoll
3047#endif
3048};
3049
3050static void
3051bnad_netdev_init(struct bnad *bnad, bool using_dac)
3052{
3053 struct net_device *netdev = bnad->netdev;
3054
Michał Mirosławe5ee20e2011-04-12 09:38:23 +00003055 netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
3056 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3057 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_HW_VLAN_TX;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003058
Michał Mirosławe5ee20e2011-04-12 09:38:23 +00003059 netdev->vlan_features = NETIF_F_SG | NETIF_F_HIGHDMA |
3060 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3061 NETIF_F_TSO | NETIF_F_TSO6;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003062
Michał Mirosławe5ee20e2011-04-12 09:38:23 +00003063 netdev->features |= netdev->hw_features |
3064 NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003065
3066 if (using_dac)
3067 netdev->features |= NETIF_F_HIGHDMA;
3068
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003069 netdev->mem_start = bnad->mmio_start;
3070 netdev->mem_end = bnad->mmio_start + bnad->mmio_len - 1;
3071
3072 netdev->netdev_ops = &bnad_netdev_ops;
3073 bnad_set_ethtool_ops(netdev);
3074}
3075
3076/*
3077 * 1. Initialize the bnad structure
3078 * 2. Setup netdev pointer in pci_dev
3079 * 3. Initialze Tx free tasklet
3080 * 4. Initialize no. of TxQ & CQs & MSIX vectors
3081 */
3082static int
3083bnad_init(struct bnad *bnad,
3084 struct pci_dev *pdev, struct net_device *netdev)
3085{
3086 unsigned long flags;
3087
3088 SET_NETDEV_DEV(netdev, &pdev->dev);
3089 pci_set_drvdata(pdev, netdev);
3090
3091 bnad->netdev = netdev;
3092 bnad->pcidev = pdev;
3093 bnad->mmio_start = pci_resource_start(pdev, 0);
3094 bnad->mmio_len = pci_resource_len(pdev, 0);
3095 bnad->bar0 = ioremap_nocache(bnad->mmio_start, bnad->mmio_len);
3096 if (!bnad->bar0) {
3097 dev_err(&pdev->dev, "ioremap for bar0 failed\n");
3098 pci_set_drvdata(pdev, NULL);
3099 return -ENOMEM;
3100 }
3101 pr_info("bar0 mapped to %p, len %llu\n", bnad->bar0,
3102 (unsigned long long) bnad->mmio_len);
3103
3104 spin_lock_irqsave(&bnad->bna_lock, flags);
3105 if (!bnad_msix_disable)
3106 bnad->cfg_flags = BNAD_CF_MSIX;
3107
3108 bnad->cfg_flags |= BNAD_CF_DIM_ENABLED;
3109
3110 bnad_q_num_init(bnad);
3111 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3112
3113 bnad->msix_num = (bnad->num_tx * bnad->num_txq_per_tx) +
3114 (bnad->num_rx * bnad->num_rxp_per_rx) +
3115 BNAD_MAILBOX_MSIX_VECTORS;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003116
3117 bnad->txq_depth = BNAD_TXQ_DEPTH;
3118 bnad->rxq_depth = BNAD_RXQ_DEPTH;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003119
3120 bnad->tx_coalescing_timeo = BFI_TX_COALESCING_TIMEO;
3121 bnad->rx_coalescing_timeo = BFI_RX_COALESCING_TIMEO;
3122
3123 tasklet_init(&bnad->tx_free_tasklet, bnad_tx_free_tasklet,
3124 (unsigned long)bnad);
3125
3126 return 0;
3127}
3128
3129/*
3130 * Must be called after bnad_pci_uninit()
3131 * so that iounmap() and pci_set_drvdata(NULL)
3132 * happens only after PCI uninitialization.
3133 */
3134static void
3135bnad_uninit(struct bnad *bnad)
3136{
3137 if (bnad->bar0)
3138 iounmap(bnad->bar0);
3139 pci_set_drvdata(bnad->pcidev, NULL);
3140}
3141
3142/*
3143 * Initialize locks
Rasesh Mody078086f2011-08-08 16:21:39 +00003144 a) Per ioceth mutes used for serializing configuration
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003145 changes from OS interface
3146 b) spin lock used to protect bna state machine
3147 */
3148static void
3149bnad_lock_init(struct bnad *bnad)
3150{
3151 spin_lock_init(&bnad->bna_lock);
3152 mutex_init(&bnad->conf_mutex);
3153}
3154
3155static void
3156bnad_lock_uninit(struct bnad *bnad)
3157{
3158 mutex_destroy(&bnad->conf_mutex);
3159}
3160
3161/* PCI Initialization */
3162static int
3163bnad_pci_init(struct bnad *bnad,
3164 struct pci_dev *pdev, bool *using_dac)
3165{
3166 int err;
3167
3168 err = pci_enable_device(pdev);
3169 if (err)
3170 return err;
3171 err = pci_request_regions(pdev, BNAD_NAME);
3172 if (err)
3173 goto disable_device;
Ivan Vecera5ea74312011-02-02 04:37:02 +00003174 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
3175 !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003176 *using_dac = 1;
3177 } else {
Ivan Vecera5ea74312011-02-02 04:37:02 +00003178 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003179 if (err) {
Ivan Vecera5ea74312011-02-02 04:37:02 +00003180 err = dma_set_coherent_mask(&pdev->dev,
3181 DMA_BIT_MASK(32));
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003182 if (err)
3183 goto release_regions;
3184 }
3185 *using_dac = 0;
3186 }
3187 pci_set_master(pdev);
3188 return 0;
3189
3190release_regions:
3191 pci_release_regions(pdev);
3192disable_device:
3193 pci_disable_device(pdev);
3194
3195 return err;
3196}
3197
3198static void
3199bnad_pci_uninit(struct pci_dev *pdev)
3200{
3201 pci_release_regions(pdev);
3202 pci_disable_device(pdev);
3203}
3204
3205static int __devinit
3206bnad_pci_probe(struct pci_dev *pdev,
3207 const struct pci_device_id *pcidev_id)
3208{
Rasesh Mody0120b992011-07-22 08:07:41 +00003209 bool using_dac = false;
3210 int err;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003211 struct bnad *bnad;
3212 struct bna *bna;
3213 struct net_device *netdev;
3214 struct bfa_pcidev pcidev_info;
3215 unsigned long flags;
3216
3217 pr_info("bnad_pci_probe : (0x%p, 0x%p) PCI Func : (%d)\n",
3218 pdev, pcidev_id, PCI_FUNC(pdev->devfn));
3219
3220 mutex_lock(&bnad_fwimg_mutex);
3221 if (!cna_get_firmware_buf(pdev)) {
3222 mutex_unlock(&bnad_fwimg_mutex);
3223 pr_warn("Failed to load Firmware Image!\n");
3224 return -ENODEV;
3225 }
3226 mutex_unlock(&bnad_fwimg_mutex);
3227
3228 /*
3229 * Allocates sizeof(struct net_device + struct bnad)
3230 * bnad = netdev->priv
3231 */
3232 netdev = alloc_etherdev(sizeof(struct bnad));
3233 if (!netdev) {
Rasesh Mody078086f2011-08-08 16:21:39 +00003234 dev_err(&pdev->dev, "netdev allocation failed\n");
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003235 err = -ENOMEM;
3236 return err;
3237 }
3238 bnad = netdev_priv(netdev);
3239
Rasesh Mody078086f2011-08-08 16:21:39 +00003240 bnad_lock_init(bnad);
3241
3242 mutex_lock(&bnad->conf_mutex);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003243 /*
3244 * PCI initialization
Rasesh Mody0120b992011-07-22 08:07:41 +00003245 * Output : using_dac = 1 for 64 bit DMA
Rasesh Modybe7fa322010-12-23 21:45:01 +00003246 * = 0 for 32 bit DMA
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003247 */
3248 err = bnad_pci_init(bnad, pdev, &using_dac);
3249 if (err)
Dan Carpenter44861f42011-08-24 01:29:22 +00003250 goto unlock_mutex;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003251
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003252 /*
3253 * Initialize bnad structure
3254 * Setup relation between pci_dev & netdev
3255 * Init Tx free tasklet
3256 */
3257 err = bnad_init(bnad, pdev, netdev);
3258 if (err)
3259 goto pci_uninit;
Rasesh Mody078086f2011-08-08 16:21:39 +00003260
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003261 /* Initialize netdev structure, set up ethtool ops */
3262 bnad_netdev_init(bnad, using_dac);
3263
Rasesh Mody815f41e2010-12-23 21:45:03 +00003264 /* Set link to down state */
3265 netif_carrier_off(netdev);
3266
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003267 /* Get resource requirement form bna */
Rasesh Mody078086f2011-08-08 16:21:39 +00003268 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003269 bna_res_req(&bnad->res_info[0]);
Rasesh Mody078086f2011-08-08 16:21:39 +00003270 spin_unlock_irqrestore(&bnad->bna_lock, flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003271
3272 /* Allocate resources from bna */
Rasesh Mody078086f2011-08-08 16:21:39 +00003273 err = bnad_res_alloc(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003274 if (err)
Rasesh Mody078086f2011-08-08 16:21:39 +00003275 goto drv_uninit;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003276
3277 bna = &bnad->bna;
3278
3279 /* Setup pcidev_info for bna_init() */
3280 pcidev_info.pci_slot = PCI_SLOT(bnad->pcidev->devfn);
3281 pcidev_info.pci_func = PCI_FUNC(bnad->pcidev->devfn);
3282 pcidev_info.device_id = bnad->pcidev->device;
3283 pcidev_info.pci_bar_kva = bnad->bar0;
3284
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003285 spin_lock_irqsave(&bnad->bna_lock, flags);
3286 bna_init(bna, bnad, &pcidev_info, &bnad->res_info[0]);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003287 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3288
3289 bnad->stats.bna_stats = &bna->stats;
3290
Rasesh Mody078086f2011-08-08 16:21:39 +00003291 bnad_enable_msix(bnad);
3292 err = bnad_mbox_irq_alloc(bnad);
3293 if (err)
3294 goto res_free;
3295
3296
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003297 /* Set up timers */
Rasesh Mody078086f2011-08-08 16:21:39 +00003298 setup_timer(&bnad->bna.ioceth.ioc.ioc_timer, bnad_ioc_timeout,
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003299 ((unsigned long)bnad));
Rasesh Mody078086f2011-08-08 16:21:39 +00003300 setup_timer(&bnad->bna.ioceth.ioc.hb_timer, bnad_ioc_hb_check,
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003301 ((unsigned long)bnad));
Rasesh Mody078086f2011-08-08 16:21:39 +00003302 setup_timer(&bnad->bna.ioceth.ioc.iocpf_timer, bnad_iocpf_timeout,
Rasesh Mody1d32f762010-12-23 21:45:09 +00003303 ((unsigned long)bnad));
Rasesh Mody078086f2011-08-08 16:21:39 +00003304 setup_timer(&bnad->bna.ioceth.ioc.sem_timer, bnad_iocpf_sem_timeout,
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003305 ((unsigned long)bnad));
3306
3307 /* Now start the timer before calling IOC */
Rasesh Mody078086f2011-08-08 16:21:39 +00003308 mod_timer(&bnad->bna.ioceth.ioc.iocpf_timer,
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003309 jiffies + msecs_to_jiffies(BNA_IOC_TIMER_FREQ));
3310
3311 /*
3312 * Start the chip
Rasesh Mody078086f2011-08-08 16:21:39 +00003313 * If the call back comes with error, we bail out.
3314 * This is a catastrophic error.
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003315 */
Rasesh Mody078086f2011-08-08 16:21:39 +00003316 err = bnad_ioceth_enable(bnad);
3317 if (err) {
3318 pr_err("BNA: Initialization failed err=%d\n",
3319 err);
3320 goto probe_success;
3321 }
3322
3323 spin_lock_irqsave(&bnad->bna_lock, flags);
3324 if (bna_num_txq_set(bna, BNAD_NUM_TXQ + 1) ||
3325 bna_num_rxp_set(bna, BNAD_NUM_RXP + 1)) {
3326 bnad_q_num_adjust(bnad, bna_attr(bna)->num_txq - 1,
3327 bna_attr(bna)->num_rxp - 1);
3328 if (bna_num_txq_set(bna, BNAD_NUM_TXQ + 1) ||
3329 bna_num_rxp_set(bna, BNAD_NUM_RXP + 1))
3330 err = -EIO;
3331 }
3332 bna_mod_res_req(&bnad->bna, &bnad->mod_res_info[0]);
3333 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3334
3335 err = bnad_res_alloc(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
Rasesh Mody0caa9aa2011-08-30 15:27:38 +00003336 if (err) {
3337 err = -EIO;
Rasesh Mody078086f2011-08-08 16:21:39 +00003338 goto disable_ioceth;
Rasesh Mody0caa9aa2011-08-30 15:27:38 +00003339 }
Rasesh Mody078086f2011-08-08 16:21:39 +00003340
3341 spin_lock_irqsave(&bnad->bna_lock, flags);
3342 bna_mod_init(&bnad->bna, &bnad->mod_res_info[0]);
3343 spin_unlock_irqrestore(&bnad->bna_lock, flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003344
3345 /* Get the burnt-in mac */
3346 spin_lock_irqsave(&bnad->bna_lock, flags);
Rasesh Mody078086f2011-08-08 16:21:39 +00003347 bna_enet_perm_mac_get(&bna->enet, &bnad->perm_addr);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003348 bnad_set_netdev_perm_addr(bnad);
3349 spin_unlock_irqrestore(&bnad->bna_lock, flags);
3350
Rasesh Mody0caa9aa2011-08-30 15:27:38 +00003351 mutex_unlock(&bnad->conf_mutex);
3352
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003353 /* Finally, reguister with net_device layer */
3354 err = register_netdev(netdev);
3355 if (err) {
3356 pr_err("BNA : Registering with netdev failed\n");
Rasesh Mody078086f2011-08-08 16:21:39 +00003357 goto probe_uninit;
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003358 }
Rasesh Mody078086f2011-08-08 16:21:39 +00003359 set_bit(BNAD_RF_NETDEV_REGISTERED, &bnad->run_flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003360
Rasesh Mody0caa9aa2011-08-30 15:27:38 +00003361 return 0;
3362
Rasesh Mody078086f2011-08-08 16:21:39 +00003363probe_success:
3364 mutex_unlock(&bnad->conf_mutex);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003365 return 0;
3366
Rasesh Mody078086f2011-08-08 16:21:39 +00003367probe_uninit:
3368 bnad_res_free(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
3369disable_ioceth:
3370 bnad_ioceth_disable(bnad);
3371 del_timer_sync(&bnad->bna.ioceth.ioc.ioc_timer);
3372 del_timer_sync(&bnad->bna.ioceth.ioc.sem_timer);
3373 del_timer_sync(&bnad->bna.ioceth.ioc.hb_timer);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003374 spin_lock_irqsave(&bnad->bna_lock, flags);
3375 bna_uninit(bna);
3376 spin_unlock_irqrestore(&bnad->bna_lock, flags);
Rasesh Mody078086f2011-08-08 16:21:39 +00003377 bnad_mbox_irq_free(bnad);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003378 bnad_disable_msix(bnad);
Rasesh Mody078086f2011-08-08 16:21:39 +00003379res_free:
3380 bnad_res_free(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
3381drv_uninit:
3382 bnad_uninit(bnad);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003383pci_uninit:
3384 bnad_pci_uninit(pdev);
Dan Carpenter44861f42011-08-24 01:29:22 +00003385unlock_mutex:
Rasesh Mody078086f2011-08-08 16:21:39 +00003386 mutex_unlock(&bnad->conf_mutex);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003387 bnad_lock_uninit(bnad);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003388 free_netdev(netdev);
3389 return err;
3390}
3391
3392static void __devexit
3393bnad_pci_remove(struct pci_dev *pdev)
3394{
3395 struct net_device *netdev = pci_get_drvdata(pdev);
3396 struct bnad *bnad;
3397 struct bna *bna;
3398 unsigned long flags;
3399
3400 if (!netdev)
3401 return;
3402
3403 pr_info("%s bnad_pci_remove\n", netdev->name);
3404 bnad = netdev_priv(netdev);
3405 bna = &bnad->bna;
3406
Rasesh Mody078086f2011-08-08 16:21:39 +00003407 if (test_and_clear_bit(BNAD_RF_NETDEV_REGISTERED, &bnad->run_flags))
3408 unregister_netdev(netdev);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003409
3410 mutex_lock(&bnad->conf_mutex);
Rasesh Mody078086f2011-08-08 16:21:39 +00003411 bnad_ioceth_disable(bnad);
3412 del_timer_sync(&bnad->bna.ioceth.ioc.ioc_timer);
3413 del_timer_sync(&bnad->bna.ioceth.ioc.sem_timer);
3414 del_timer_sync(&bnad->bna.ioceth.ioc.hb_timer);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003415 spin_lock_irqsave(&bnad->bna_lock, flags);
3416 bna_uninit(bna);
3417 spin_unlock_irqrestore(&bnad->bna_lock, flags);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003418
Rasesh Mody078086f2011-08-08 16:21:39 +00003419 bnad_res_free(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
3420 bnad_res_free(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
3421 bnad_mbox_irq_free(bnad);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003422 bnad_disable_msix(bnad);
3423 bnad_pci_uninit(pdev);
Rasesh Mody078086f2011-08-08 16:21:39 +00003424 mutex_unlock(&bnad->conf_mutex);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003425 bnad_lock_uninit(bnad);
3426 bnad_uninit(bnad);
3427 free_netdev(netdev);
3428}
3429
Rasesh Mody0120b992011-07-22 08:07:41 +00003430static DEFINE_PCI_DEVICE_TABLE(bnad_pci_id_table) = {
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003431 {
3432 PCI_DEVICE(PCI_VENDOR_ID_BROCADE,
3433 PCI_DEVICE_ID_BROCADE_CT),
3434 .class = PCI_CLASS_NETWORK_ETHERNET << 8,
3435 .class_mask = 0xffff00
3436 }, {0, }
3437};
3438
3439MODULE_DEVICE_TABLE(pci, bnad_pci_id_table);
3440
3441static struct pci_driver bnad_pci_driver = {
3442 .name = BNAD_NAME,
3443 .id_table = bnad_pci_id_table,
3444 .probe = bnad_pci_probe,
3445 .remove = __devexit_p(bnad_pci_remove),
3446};
3447
3448static int __init
3449bnad_module_init(void)
3450{
3451 int err;
3452
Rasesh Mody5aad0012011-07-22 08:07:40 +00003453 pr_info("Brocade 10G Ethernet driver - version: %s\n",
3454 BNAD_VERSION);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003455
Rasesh Mody8a891422010-08-25 23:00:27 -07003456 bfa_nw_ioc_auto_recover(bnad_ioc_auto_recover);
Rasesh Mody8b230ed2010-08-23 20:24:12 -07003457
3458 err = pci_register_driver(&bnad_pci_driver);
3459 if (err < 0) {
3460 pr_err("bna : PCI registration failed in module init "
3461 "(%d)\n", err);
3462 return err;
3463 }
3464
3465 return 0;
3466}
3467
3468static void __exit
3469bnad_module_exit(void)
3470{
3471 pci_unregister_driver(&bnad_pci_driver);
3472
3473 if (bfi_fw)
3474 release_firmware(bfi_fw);
3475}
3476
3477module_init(bnad_module_init);
3478module_exit(bnad_module_exit);
3479
3480MODULE_AUTHOR("Brocade");
3481MODULE_LICENSE("GPL");
3482MODULE_DESCRIPTION("Brocade 10G PCIe Ethernet driver");
3483MODULE_VERSION(BNAD_VERSION);
3484MODULE_FIRMWARE(CNA_FW_FILE_CT);