bnad.c 93.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Linux network driver for Brocade Converged Network Adapter.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License (GPL) Version 2 as
 * published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */
/*
 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
 * All rights reserved
 * www.brocade.com
 */
J
Jiri Pirko 已提交
18
#include <linux/bitops.h>
19 20 21 22 23 24 25 26
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/etherdevice.h>
#include <linux/in.h>
#include <linux/ethtool.h>
#include <linux/if_vlan.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
27
#include <linux/prefetch.h>
28
#include <linux/module.h>
29 30 31 32 33

#include "bnad.h"
#include "bna.h"
#include "cna.h"

R
Rasesh Mody 已提交
34
static DEFINE_MUTEX(bnad_fwimg_mutex);
35 36 37 38 39 40 41 42 43 44 45 46

/*
 * Module params
 */
static uint bnad_msix_disable;
module_param(bnad_msix_disable, uint, 0444);
MODULE_PARM_DESC(bnad_msix_disable, "Disable MSIX mode");

static uint bnad_ioc_auto_recover = 1;
module_param(bnad_ioc_auto_recover, uint, 0444);
MODULE_PARM_DESC(bnad_ioc_auto_recover, "Enable / Disable auto recovery");

K
Krishna Gudipati 已提交
47 48 49 50 51
static uint bna_debugfs_enable = 1;
module_param(bna_debugfs_enable, uint, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(bna_debugfs_enable, "Enables debugfs feature, default=1,"
		 " Range[false:0|true:1]");

52 53 54
/*
 * Global variables
 */
55
static u32 bnad_rxqs_per_cq = 2;
56 57 58
static u32 bna_id;
static struct mutex bnad_list_mutex;
static LIST_HEAD(bnad_list);
R
Rasesh Mody 已提交
59
static const u8 bnad_bcast_addr[] =  {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
60 61 62 63 64 65

/*
 * Local MACROS
 */
#define BNAD_GET_MBOX_IRQ(_bnad)				\
	(((_bnad)->cfg_flags & BNAD_CF_MSIX) ?			\
66
	 ((_bnad)->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector) : \
67 68
	 ((_bnad)->pcidev->irq))

R
Rasesh Mody 已提交
69
#define BNAD_FILL_UNMAPQ_MEM_REQ(_res_info, _num, _size)	\
70 71 72 73
do {								\
	(_res_info)->res_type = BNA_RES_T_MEM;			\
	(_res_info)->res_u.mem_info.mem_type = BNA_MEM_T_KVA;	\
	(_res_info)->res_u.mem_info.num = (_num);		\
R
Rasesh Mody 已提交
74
	(_res_info)->res_u.mem_info.len = (_size);		\
75 76
} while (0)

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
static void
bnad_add_to_list(struct bnad *bnad)
{
	mutex_lock(&bnad_list_mutex);
	list_add_tail(&bnad->list_entry, &bnad_list);
	bnad->id = bna_id++;
	mutex_unlock(&bnad_list_mutex);
}

static void
bnad_remove_from_list(struct bnad *bnad)
{
	mutex_lock(&bnad_list_mutex);
	list_del(&bnad->list_entry);
	mutex_unlock(&bnad_list_mutex);
}

94 95 96 97
/*
 * Reinitialize completions in CQ, once Rx is taken down
 */
static void
98
bnad_cq_cleanup(struct bnad *bnad, struct bna_ccb *ccb)
99
{
R
Rasesh Mody 已提交
100
	struct bna_cq_entry *cmpl;
101 102 103
	int i;

	for (i = 0; i < ccb->q_depth; i++) {
R
Rasesh Mody 已提交
104
		cmpl = &((struct bna_cq_entry *)ccb->sw_q)[i];
105 106 107 108
		cmpl->valid = 0;
	}
}

R
Rasesh Mody 已提交
109 110 111 112
/* Tx Datapath functions */


/* Caller should ensure that the entry at unmap_q[index] is valid */
R
Rasesh Mody 已提交
113
static u32
R
Rasesh Mody 已提交
114 115 116
bnad_tx_buff_unmap(struct bnad *bnad,
			      struct bnad_tx_unmap *unmap_q,
			      u32 q_depth, u32 index)
R
Rasesh Mody 已提交
117
{
R
Rasesh Mody 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	struct bnad_tx_unmap *unmap;
	struct sk_buff *skb;
	int vector, nvecs;

	unmap = &unmap_q[index];
	nvecs = unmap->nvecs;

	skb = unmap->skb;
	unmap->skb = NULL;
	unmap->nvecs = 0;
	dma_unmap_single(&bnad->pcidev->dev,
		dma_unmap_addr(&unmap->vectors[0], dma_addr),
		skb_headlen(skb), DMA_TO_DEVICE);
	dma_unmap_addr_set(&unmap->vectors[0], dma_addr, 0);
	nvecs--;

	vector = 0;
	while (nvecs) {
		vector++;
		if (vector == BFI_TX_MAX_VECTORS_PER_WI) {
			vector = 0;
			BNA_QE_INDX_INC(index, q_depth);
			unmap = &unmap_q[index];
		}
R
Rasesh Mody 已提交
142

R
Rasesh Mody 已提交
143 144
		dma_unmap_page(&bnad->pcidev->dev,
			dma_unmap_addr(&unmap->vectors[vector], dma_addr),
R
Rasesh Mody 已提交
145 146
			dma_unmap_len(&unmap->vectors[vector], dma_len),
			DMA_TO_DEVICE);
R
Rasesh Mody 已提交
147 148
		dma_unmap_addr_set(&unmap->vectors[vector], dma_addr, 0);
		nvecs--;
R
Rasesh Mody 已提交
149 150
	}

R
Rasesh Mody 已提交
151 152
	BNA_QE_INDX_INC(index, q_depth);

R
Rasesh Mody 已提交
153 154 155
	return index;
}

156 157 158 159 160 161
/*
 * Frees all pending Tx Bufs
 * At this point no activity is expected on the Q,
 * so DMA unmap & freeing is fine.
 */
static void
R
Rasesh Mody 已提交
162
bnad_txq_cleanup(struct bnad *bnad, struct bna_tcb *tcb)
163
{
R
Rasesh Mody 已提交
164 165 166
	struct bnad_tx_unmap *unmap_q = tcb->unmap_q;
	struct sk_buff *skb;
	int i;
167

R
Rasesh Mody 已提交
168 169
	for (i = 0; i < tcb->q_depth; i++) {
		skb = unmap_q[i].skb;
R
Rasesh Mody 已提交
170
		if (!skb)
171
			continue;
R
Rasesh Mody 已提交
172
		bnad_tx_buff_unmap(bnad, unmap_q, tcb->q_depth, i);
R
Rasesh Mody 已提交
173

174 175 176 177 178
		dev_kfree_skb_any(skb);
	}
}

/*
179
 * bnad_txcmpl_process : Frees the Tx bufs on Tx completion
180 181 182 183
 * Can be called in a) Interrupt context
 *		    b) Sending context
 */
static u32
R
Rasesh Mody 已提交
184
bnad_txcmpl_process(struct bnad *bnad, struct bna_tcb *tcb)
185
{
R
Rasesh Mody 已提交
186 187 188 189 190
	u32 sent_packets = 0, sent_bytes = 0;
	u32 wis, unmap_wis, hw_cons, cons, q_depth;
	struct bnad_tx_unmap *unmap_q = tcb->unmap_q;
	struct bnad_tx_unmap *unmap;
	struct sk_buff *skb;
191

J
Jing Huang 已提交
192
	/* Just return if TX is stopped */
R
Rasesh Mody 已提交
193
	if (!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))
194 195
		return 0;

R
Rasesh Mody 已提交
196 197 198
	hw_cons = *(tcb->hw_consumer_index);
	cons = tcb->consumer_index;
	q_depth = tcb->q_depth;
199

R
Rasesh Mody 已提交
200
	wis = BNA_Q_INDEX_CHANGE(cons, hw_cons, q_depth);
201 202 203
	BUG_ON(!(wis <= BNA_QE_IN_USE_CNT(tcb, tcb->q_depth)));

	while (wis) {
R
Rasesh Mody 已提交
204 205 206
		unmap = &unmap_q[cons];

		skb = unmap->skb;
207 208 209 210

		sent_packets++;
		sent_bytes += skb->len;

R
Rasesh Mody 已提交
211 212
		unmap_wis = BNA_TXQ_WI_NEEDED(unmap->nvecs);
		wis -= unmap_wis;
213

R
Rasesh Mody 已提交
214
		cons = bnad_tx_buff_unmap(bnad, unmap_q, q_depth, cons);
215 216 217 218
		dev_kfree_skb_any(skb);
	}

	/* Update consumer pointers. */
R
Rasesh Mody 已提交
219
	tcb->consumer_index = hw_cons;
220 221 222 223 224 225 226 227

	tcb->txq->tx_packets += sent_packets;
	tcb->txq->tx_bytes += sent_bytes;

	return sent_packets;
}

static u32
228
bnad_tx_complete(struct bnad *bnad, struct bna_tcb *tcb)
229 230
{
	struct net_device *netdev = bnad->netdev;
R
Rasesh Mody 已提交
231
	u32 sent = 0;
232 233 234 235

	if (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags))
		return 0;

236
	sent = bnad_txcmpl_process(bnad, tcb);
237 238 239 240 241
	if (sent) {
		if (netif_queue_stopped(netdev) &&
		    netif_carrier_ok(netdev) &&
		    BNA_QE_FREE_CNT(tcb, tcb->q_depth) >=
				    BNAD_NETIF_WAKE_THRESHOLD) {
R
Rasesh Mody 已提交
242 243 244 245
			if (test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)) {
				netif_wake_queue(netdev);
				BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
			}
246
		}
R
Rasesh Mody 已提交
247 248 249
	}

	if (likely(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
		bna_ib_ack(tcb->i_dbell, sent);

	smp_mb__before_clear_bit();
	clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);

	return sent;
}

/* MSIX Tx Completion Handler */
static irqreturn_t
bnad_msix_tx(int irq, void *data)
{
	struct bna_tcb *tcb = (struct bna_tcb *)data;
	struct bnad *bnad = tcb->bnad;

265
	bnad_tx_complete(bnad, tcb);
266 267 268 269

	return IRQ_HANDLED;
}

R
Rasesh Mody 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
static inline void
bnad_rxq_alloc_uninit(struct bnad *bnad, struct bna_rcb *rcb)
{
	struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;

	unmap_q->reuse_pi = -1;
	unmap_q->alloc_order = -1;
	unmap_q->map_size = 0;
	unmap_q->type = BNAD_RXBUF_NONE;
}

/* Default is page-based allocation. Multi-buffer support - TBD */
static int
bnad_rxq_alloc_init(struct bnad *bnad, struct bna_rcb *rcb)
{
	struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
R
Rasesh Mody 已提交
286
	int order;
R
Rasesh Mody 已提交
287 288 289

	bnad_rxq_alloc_uninit(bnad, rcb);

R
Rasesh Mody 已提交
290 291 292
	order = get_order(rcb->rxq->buffer_size);

	unmap_q->type = BNAD_RXBUF_PAGE;
R
Rasesh Mody 已提交
293 294 295 296 297

	if (bna_is_small_rxq(rcb->id)) {
		unmap_q->alloc_order = 0;
		unmap_q->map_size = rcb->rxq->buffer_size;
	} else {
R
Rasesh Mody 已提交
298 299 300 301 302 303 304 305 306 307
		if (rcb->rxq->multi_buffer) {
			unmap_q->alloc_order = 0;
			unmap_q->map_size = rcb->rxq->buffer_size;
			unmap_q->type = BNAD_RXBUF_MULTI_BUFF;
		} else {
			unmap_q->alloc_order = order;
			unmap_q->map_size =
				(rcb->rxq->buffer_size > 2048) ?
				PAGE_SIZE << order : 2048;
		}
R
Rasesh Mody 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	}

	BUG_ON(((PAGE_SIZE << order) % unmap_q->map_size));

	return 0;
}

static inline void
bnad_rxq_cleanup_page(struct bnad *bnad, struct bnad_rx_unmap *unmap)
{
	if (!unmap->page)
		return;

	dma_unmap_page(&bnad->pcidev->dev,
			dma_unmap_addr(&unmap->vector, dma_addr),
			unmap->vector.len, DMA_FROM_DEVICE);
	put_page(unmap->page);
	unmap->page = NULL;
	dma_unmap_addr_set(&unmap->vector, dma_addr, 0);
	unmap->vector.len = 0;
}

static inline void
bnad_rxq_cleanup_skb(struct bnad *bnad, struct bnad_rx_unmap *unmap)
{
	if (!unmap->skb)
		return;

	dma_unmap_single(&bnad->pcidev->dev,
			dma_unmap_addr(&unmap->vector, dma_addr),
			unmap->vector.len, DMA_FROM_DEVICE);
	dev_kfree_skb_any(unmap->skb);
	unmap->skb = NULL;
	dma_unmap_addr_set(&unmap->vector, dma_addr, 0);
	unmap->vector.len = 0;
}

345
static void
346
bnad_rxq_cleanup(struct bnad *bnad, struct bna_rcb *rcb)
347
{
R
Rasesh Mody 已提交
348
	struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
R
Rasesh Mody 已提交
349 350 351
	int i;

	for (i = 0; i < rcb->q_depth; i++) {
R
Rasesh Mody 已提交
352
		struct bnad_rx_unmap *unmap = &unmap_q->unmap[i];
353

R
Rasesh Mody 已提交
354
		if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
R
Rasesh Mody 已提交
355
			bnad_rxq_cleanup_skb(bnad, unmap);
R
Rasesh Mody 已提交
356 357
		else
			bnad_rxq_cleanup_page(bnad, unmap);
R
Rasesh Mody 已提交
358 359 360
	}
	bnad_rxq_alloc_uninit(bnad, rcb);
}
R
Rasesh Mody 已提交
361

R
Rasesh Mody 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
static u32
bnad_rxq_refill_page(struct bnad *bnad, struct bna_rcb *rcb, u32 nalloc)
{
	u32 alloced, prod, q_depth;
	struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
	struct bnad_rx_unmap *unmap, *prev;
	struct bna_rxq_entry *rxent;
	struct page *page;
	u32 page_offset, alloc_size;
	dma_addr_t dma_addr;

	prod = rcb->producer_index;
	q_depth = rcb->q_depth;

	alloc_size = PAGE_SIZE << unmap_q->alloc_order;
	alloced = 0;

	while (nalloc--) {
		unmap = &unmap_q->unmap[prod];

		if (unmap_q->reuse_pi < 0) {
			page = alloc_pages(GFP_ATOMIC | __GFP_COMP,
					unmap_q->alloc_order);
			page_offset = 0;
		} else {
			prev = &unmap_q->unmap[unmap_q->reuse_pi];
			page = prev->page;
			page_offset = prev->page_offset + unmap_q->map_size;
			get_page(page);
		}

		if (unlikely(!page)) {
			BNAD_UPDATE_CTR(bnad, rxbuf_alloc_failed);
			rcb->rxq->rxbuf_alloc_failed++;
			goto finishing;
		}

		dma_addr = dma_map_page(&bnad->pcidev->dev, page, page_offset,
				unmap_q->map_size, DMA_FROM_DEVICE);

		unmap->page = page;
		unmap->page_offset = page_offset;
		dma_unmap_addr_set(&unmap->vector, dma_addr, dma_addr);
		unmap->vector.len = unmap_q->map_size;
		page_offset += unmap_q->map_size;

		if (page_offset < alloc_size)
			unmap_q->reuse_pi = prod;
		else
			unmap_q->reuse_pi = -1;

		rxent = &((struct bna_rxq_entry *)rcb->sw_q)[prod];
		BNA_SET_DMA_ADDR(dma_addr, &rxent->host_addr);
		BNA_QE_INDX_INC(prod, q_depth);
		alloced++;
	}

finishing:
	if (likely(alloced)) {
		rcb->producer_index = prod;
		smp_mb();
		if (likely(test_bit(BNAD_RXQ_POST_OK, &rcb->flags)))
			bna_rxq_prod_indx_doorbell(rcb);
425
	}
R
Rasesh Mody 已提交
426 427

	return alloced;
428 429
}

R
Rasesh Mody 已提交
430 431
static u32
bnad_rxq_refill_skb(struct bnad *bnad, struct bna_rcb *rcb, u32 nalloc)
432
{
R
Rasesh Mody 已提交
433 434
	u32 alloced, prod, q_depth, buff_sz;
	struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
R
Rasesh Mody 已提交
435
	struct bnad_rx_unmap *unmap;
436 437 438 439
	struct bna_rxq_entry *rxent;
	struct sk_buff *skb;
	dma_addr_t dma_addr;

R
Rasesh Mody 已提交
440 441 442
	buff_sz = rcb->rxq->buffer_size;
	prod = rcb->producer_index;
	q_depth = rcb->q_depth;
443

R
Rasesh Mody 已提交
444 445 446 447 448 449
	alloced = 0;
	while (nalloc--) {
		unmap = &unmap_q->unmap[prod];

		skb = netdev_alloc_skb_ip_align(bnad->netdev, buff_sz);

450 451
		if (unlikely(!skb)) {
			BNAD_UPDATE_CTR(bnad, rxbuf_alloc_failed);
452
			rcb->rxq->rxbuf_alloc_failed++;
453 454
			goto finishing;
		}
I
Ivan Vecera 已提交
455
		dma_addr = dma_map_single(&bnad->pcidev->dev, skb->data,
R
Rasesh Mody 已提交
456
					  buff_sz, DMA_FROM_DEVICE);
457

R
Rasesh Mody 已提交
458 459 460
		unmap->skb = skb;
		dma_unmap_addr_set(&unmap->vector, dma_addr, dma_addr);
		unmap->vector.len = buff_sz;
R
Rasesh Mody 已提交
461 462 463

		rxent = &((struct bna_rxq_entry *)rcb->sw_q)[prod];
		BNA_SET_DMA_ADDR(dma_addr, &rxent->host_addr);
R
Rasesh Mody 已提交
464
		BNA_QE_INDX_INC(prod, q_depth);
465 466 467 468 469
		alloced++;
	}

finishing:
	if (likely(alloced)) {
R
Rasesh Mody 已提交
470
		rcb->producer_index = prod;
471
		smp_mb();
R
Rasesh Mody 已提交
472
		if (likely(test_bit(BNAD_RXQ_POST_OK, &rcb->flags)))
R
Rasesh Mody 已提交
473
			bna_rxq_prod_indx_doorbell(rcb);
474
	}
R
Rasesh Mody 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488

	return alloced;
}

static inline void
bnad_rxq_post(struct bnad *bnad, struct bna_rcb *rcb)
{
	struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
	u32 to_alloc;

	to_alloc = BNA_QE_FREE_CNT(rcb, rcb->q_depth);
	if (!(to_alloc >> BNAD_RXQ_REFILL_THRESHOLD_SHIFT))
		return;

R
Rasesh Mody 已提交
489
	if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
R
Rasesh Mody 已提交
490
		bnad_rxq_refill_skb(bnad, rcb, to_alloc);
R
Rasesh Mody 已提交
491 492
	else
		bnad_rxq_refill_page(bnad, rcb, to_alloc);
493 494
}

R
Rasesh Mody 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508
#define flags_cksum_prot_mask (BNA_CQ_EF_IPV4 | BNA_CQ_EF_L3_CKSUM_OK | \
					BNA_CQ_EF_IPV6 | \
					BNA_CQ_EF_TCP | BNA_CQ_EF_UDP | \
					BNA_CQ_EF_L4_CKSUM_OK)

#define flags_tcp4 (BNA_CQ_EF_IPV4 | BNA_CQ_EF_L3_CKSUM_OK | \
				BNA_CQ_EF_TCP | BNA_CQ_EF_L4_CKSUM_OK)
#define flags_tcp6 (BNA_CQ_EF_IPV6 | \
				BNA_CQ_EF_TCP | BNA_CQ_EF_L4_CKSUM_OK)
#define flags_udp4 (BNA_CQ_EF_IPV4 | BNA_CQ_EF_L3_CKSUM_OK | \
				BNA_CQ_EF_UDP | BNA_CQ_EF_L4_CKSUM_OK)
#define flags_udp6 (BNA_CQ_EF_IPV6 | \
				BNA_CQ_EF_UDP | BNA_CQ_EF_L4_CKSUM_OK)

R
Rasesh Mody 已提交
509 510 511
static void
bnad_cq_drop_packet(struct bnad *bnad, struct bna_rcb *rcb,
		    u32 sop_ci, u32 nvecs)
R
Rasesh Mody 已提交
512
{
R
Rasesh Mody 已提交
513 514 515
	struct bnad_rx_unmap_q *unmap_q;
	struct bnad_rx_unmap *unmap;
	u32 ci, vec;
R
Rasesh Mody 已提交
516

R
Rasesh Mody 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
	unmap_q = rcb->unmap_q;
	for (vec = 0, ci = sop_ci; vec < nvecs; vec++) {
		unmap = &unmap_q->unmap[ci];
		BNA_QE_INDX_INC(ci, rcb->q_depth);

		if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
			bnad_rxq_cleanup_skb(bnad, unmap);
		else
			bnad_rxq_cleanup_page(bnad, unmap);
	}
}

static void
bnad_cq_setup_skb_frags(struct bna_rcb *rcb, struct sk_buff *skb,
			u32 sop_ci, u32 nvecs, u32 last_fraglen)
{
	struct bnad *bnad;
	u32 ci, vec, len, totlen = 0;
	struct bnad_rx_unmap_q *unmap_q;
	struct bnad_rx_unmap *unmap;

	unmap_q = rcb->unmap_q;
	bnad = rcb->bnad;
540 541 542 543 544

	/* prefetch header */
	prefetch(page_address(unmap_q->unmap[sop_ci].page) +
			unmap_q->unmap[sop_ci].page_offset);

R
Rasesh Mody 已提交
545 546 547
	for (vec = 1, ci = sop_ci; vec <= nvecs; vec++) {
		unmap = &unmap_q->unmap[ci];
		BNA_QE_INDX_INC(ci, rcb->q_depth);
R
Rasesh Mody 已提交
548 549 550 551

		dma_unmap_page(&bnad->pcidev->dev,
				dma_unmap_addr(&unmap->vector, dma_addr),
				unmap->vector.len, DMA_FROM_DEVICE);
R
Rasesh Mody 已提交
552 553 554 555 556

		len = (vec == nvecs) ?
			last_fraglen : unmap->vector.len;
		totlen += len;

R
Rasesh Mody 已提交
557
		skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
R
Rasesh Mody 已提交
558
				unmap->page, unmap->page_offset, len);
R
Rasesh Mody 已提交
559 560 561 562 563

		unmap->page = NULL;
		unmap->vector.len = 0;
	}

R
Rasesh Mody 已提交
564 565 566 567 568 569 570 571 572 573
	skb->len += totlen;
	skb->data_len += totlen;
	skb->truesize += totlen;
}

static inline void
bnad_cq_setup_skb(struct bnad *bnad, struct sk_buff *skb,
		  struct bnad_rx_unmap *unmap, u32 len)
{
	prefetch(skb->data);
R
Rasesh Mody 已提交
574 575 576 577 578

	dma_unmap_single(&bnad->pcidev->dev,
			dma_unmap_addr(&unmap->vector, dma_addr),
			unmap->vector.len, DMA_FROM_DEVICE);

R
Rasesh Mody 已提交
579
	skb_put(skb, len);
R
Rasesh Mody 已提交
580 581 582 583 584 585
	skb->protocol = eth_type_trans(skb, bnad->netdev);

	unmap->skb = NULL;
	unmap->vector.len = 0;
}

586
static u32
587
bnad_cq_process(struct bnad *bnad, struct bna_ccb *ccb, int budget)
588
{
R
Rasesh Mody 已提交
589
	struct bna_cq_entry *cq, *cmpl, *next_cmpl;
590
	struct bna_rcb *rcb = NULL;
R
Rasesh Mody 已提交
591
	struct bnad_rx_unmap_q *unmap_q;
R
Rasesh Mody 已提交
592 593
	struct bnad_rx_unmap *unmap = NULL;
	struct sk_buff *skb = NULL;
594
	struct bna_pkt_rate *pkt_rt = &ccb->pkt_rate;
R
Rasesh Mody 已提交
595
	struct bnad_rx_ctrl *rx_ctrl = ccb->ctrl;
R
Rasesh Mody 已提交
596 597 598
	u32 packets = 0, len = 0, totlen = 0;
	u32 pi, vec, sop_ci = 0, nvecs = 0;
	u32 flags, masked_flags;
599

600
	prefetch(bnad->netdev);
R
Rasesh Mody 已提交
601 602 603 604

	cq = ccb->sw_q;
	cmpl = &cq[ccb->producer_index];

R
Rasesh Mody 已提交
605 606 607 608 609 610 611 612 613 614 615 616
	while (packets < budget) {
		if (!cmpl->valid)
			break;
		/* The 'valid' field is set by the adapter, only after writing
		 * the other fields of completion entry. Hence, do not load
		 * other fields of completion entry *before* the 'valid' is
		 * loaded. Adding the rmb() here prevents the compiler and/or
		 * CPU from reordering the reads which would potentially result
		 * in reading stale values in completion entry.
		 */
		rmb();

617 618
		BNA_UPDATE_PKT_CNT(pkt_rt, ntohs(cmpl->length));

619
		if (bna_is_small_rxq(cmpl->rxq_id))
620
			rcb = ccb->rcb[1];
621 622
		else
			rcb = ccb->rcb[0];
623 624 625

		unmap_q = rcb->unmap_q;

R
Rasesh Mody 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
		/* start of packet ci */
		sop_ci = rcb->consumer_index;

		if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type)) {
			unmap = &unmap_q->unmap[sop_ci];
			skb = unmap->skb;
		} else {
			skb = napi_get_frags(&rx_ctrl->napi);
			if (unlikely(!skb))
				break;
		}
		prefetch(skb);

		flags = ntohl(cmpl->flags);
		len = ntohs(cmpl->length);
		totlen = len;
		nvecs = 1;

		/* Check all the completions for this frame.
		 * busy-wait doesn't help much, break here.
		 */
		if (BNAD_RXBUF_IS_MULTI_BUFF(unmap_q->type) &&
		    (flags & BNA_CQ_EF_EOP) == 0) {
			pi = ccb->producer_index;
			do {
				BNA_QE_INDX_INC(pi, ccb->q_depth);
				next_cmpl = &cq[pi];

				if (!next_cmpl->valid)
					break;
R
Rasesh Mody 已提交
656 657 658 659 660 661 662 663 664 665
				/* The 'valid' field is set by the adapter, only
				 * after writing the other fields of completion
				 * entry. Hence, do not load other fields of
				 * completion entry *before* the 'valid' is
				 * loaded. Adding the rmb() here prevents the
				 * compiler and/or CPU from reordering the reads
				 * which would potentially result in reading
				 * stale values in completion entry.
				 */
				rmb();
R
Rasesh Mody 已提交
666

R
Rasesh Mody 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
				len = ntohs(next_cmpl->length);
				flags = ntohl(next_cmpl->flags);

				nvecs++;
				totlen += len;
			} while ((flags & BNA_CQ_EF_EOP) == 0);

			if (!next_cmpl->valid)
				break;
		}

		/* TODO: BNA_CQ_EF_LOCAL ? */
		if (unlikely(flags & (BNA_CQ_EF_MAC_ERROR |
						BNA_CQ_EF_FCS_ERROR |
						BNA_CQ_EF_TOO_LONG))) {
			bnad_cq_drop_packet(bnad, rcb, sop_ci, nvecs);
683
			rcb->rxq->rx_packets_with_error++;
R
Rasesh Mody 已提交
684

685 686 687
			goto next;
		}

R
Rasesh Mody 已提交
688 689 690 691
		if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
			bnad_cq_setup_skb(bnad, skb, unmap, len);
		else
			bnad_cq_setup_skb_frags(rcb, skb, sop_ci, nvecs, len);
R
Rasesh Mody 已提交
692

R
Rasesh Mody 已提交
693 694 695 696
		packets++;
		rcb->rxq->rx_packets++;
		rcb->rxq->rx_bytes += totlen;
		ccb->bytes_per_intr += totlen;
R
Rasesh Mody 已提交
697 698 699

		masked_flags = flags & flags_cksum_prot_mask;

700
		if (likely
701
		    ((bnad->netdev->features & NETIF_F_RXCSUM) &&
R
Rasesh Mody 已提交
702 703 704 705
		     ((masked_flags == flags_tcp4) ||
		      (masked_flags == flags_udp4) ||
		      (masked_flags == flags_tcp6) ||
		      (masked_flags == flags_udp6))))
706 707
			skb->ip_summed = CHECKSUM_UNNECESSARY;
		else
708
			skb_checksum_none_assert(skb);
709

J
Jiri Pirko 已提交
710
		if (flags & BNA_CQ_EF_VLAN)
711
			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(cmpl->vlan_tag));
J
Jiri Pirko 已提交
712

R
Rasesh Mody 已提交
713
		if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
J
Jiri Pirko 已提交
714
			netif_receive_skb(skb);
R
Rasesh Mody 已提交
715 716
		else
			napi_gro_frags(&rx_ctrl->napi);
717 718

next:
R
Rasesh Mody 已提交
719 720 721 722 723 724
		BNA_QE_INDX_ADD(rcb->consumer_index, nvecs, rcb->q_depth);
		for (vec = 0; vec < nvecs; vec++) {
			cmpl = &cq[ccb->producer_index];
			cmpl->valid = 0;
			BNA_QE_INDX_INC(ccb->producer_index, ccb->q_depth);
		}
R
Rasesh Mody 已提交
725
		cmpl = &cq[ccb->producer_index];
726 727
	}

R
Rasesh Mody 已提交
728
	napi_gro_flush(&rx_ctrl->napi, false);
729
	if (likely(test_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags)))
R
Rasesh Mody 已提交
730 731
		bna_ib_ack_disable_irq(ccb->i_dbell, packets);

R
Rasesh Mody 已提交
732
	bnad_rxq_post(bnad, ccb->rcb[0]);
733
	if (ccb->rcb[1])
R
Rasesh Mody 已提交
734
		bnad_rxq_post(bnad, ccb->rcb[1]);
735

736 737 738 739 740 741 742
	return packets;
}

static void
bnad_netif_rx_schedule_poll(struct bnad *bnad, struct bna_ccb *ccb)
{
	struct bnad_rx_ctrl *rx_ctrl = (struct bnad_rx_ctrl *)(ccb->ctrl);
R
Rasesh Mody 已提交
743 744 745 746
	struct napi_struct *napi = &rx_ctrl->napi;

	if (likely(napi_schedule_prep(napi))) {
		__napi_schedule(napi);
R
Rasesh Mody 已提交
747
		rx_ctrl->rx_schedule++;
748 749 750 751 752 753 754 755 756
	}
}

/* MSIX Rx Path Handler */
static irqreturn_t
bnad_msix_rx(int irq, void *data)
{
	struct bna_ccb *ccb = (struct bna_ccb *)data;

R
Rasesh Mody 已提交
757 758
	if (ccb) {
		((struct bnad_rx_ctrl *)(ccb->ctrl))->rx_intr_ctr++;
759
		bnad_netif_rx_schedule_poll(ccb->bnad, ccb);
R
Rasesh Mody 已提交
760
	}
761 762 763 764 765 766 767 768 769 770 771

	return IRQ_HANDLED;
}

/* Interrupt handlers */

/* Mbox Interrupt Handlers */
static irqreturn_t
bnad_msix_mbox_handler(int irq, void *data)
{
	u32 intr_status;
R
Rasesh Mody 已提交
772
	unsigned long flags;
R
Rasesh Mody 已提交
773
	struct bnad *bnad = (struct bnad *)data;
774 775

	spin_lock_irqsave(&bnad->bna_lock, flags);
776 777 778 779
	if (unlikely(test_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags))) {
		spin_unlock_irqrestore(&bnad->bna_lock, flags);
		return IRQ_HANDLED;
	}
780 781 782

	bna_intr_status_get(&bnad->bna, intr_status);

783
	if (BNA_IS_MBOX_ERR_INTR(&bnad->bna, intr_status))
784 785 786 787 788 789 790 791 792 793 794 795 796
		bna_mbox_handler(&bnad->bna, intr_status);

	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	return IRQ_HANDLED;
}

static irqreturn_t
bnad_isr(int irq, void *data)
{
	int i, j;
	u32 intr_status;
	unsigned long flags;
R
Rasesh Mody 已提交
797
	struct bnad *bnad = (struct bnad *)data;
798 799
	struct bnad_rx_info *rx_info;
	struct bnad_rx_ctrl *rx_ctrl;
800
	struct bna_tcb *tcb = NULL;
801

802 803 804
	spin_lock_irqsave(&bnad->bna_lock, flags);
	if (unlikely(test_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags))) {
		spin_unlock_irqrestore(&bnad->bna_lock, flags);
R
Rasesh Mody 已提交
805
		return IRQ_NONE;
806
	}
807 808

	bna_intr_status_get(&bnad->bna, intr_status);
R
Rasesh Mody 已提交
809

810 811
	if (unlikely(!intr_status)) {
		spin_unlock_irqrestore(&bnad->bna_lock, flags);
812
		return IRQ_NONE;
813
	}
814

815
	if (BNA_IS_MBOX_ERR_INTR(&bnad->bna, intr_status))
816
		bna_mbox_handler(&bnad->bna, intr_status);
R
Rasesh Mody 已提交
817

818 819
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

R
Rasesh Mody 已提交
820 821 822
	if (!BNA_IS_INTX_DATA_INTR(intr_status))
		return IRQ_HANDLED;

823
	/* Process data interrupts */
R
Rasesh Mody 已提交
824 825
	/* Tx processing */
	for (i = 0; i < bnad->num_tx; i++) {
826 827 828
		for (j = 0; j < bnad->num_txq_per_tx; j++) {
			tcb = bnad->tx_info[i].tcb[j];
			if (tcb && test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))
829
				bnad_tx_complete(bnad, bnad->tx_info[i].tcb[j]);
830
		}
R
Rasesh Mody 已提交
831 832
	}
	/* Rx processing */
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
	for (i = 0; i < bnad->num_rx; i++) {
		rx_info = &bnad->rx_info[i];
		if (!rx_info->rx)
			continue;
		for (j = 0; j < bnad->num_rxp_per_rx; j++) {
			rx_ctrl = &rx_info->rx_ctrl[j];
			if (rx_ctrl->ccb)
				bnad_netif_rx_schedule_poll(bnad,
							    rx_ctrl->ccb);
		}
	}
	return IRQ_HANDLED;
}

/*
 * Called in interrupt / callback context
 * with bna_lock held, so cfg_flags access is OK
 */
static void
bnad_enable_mbox_irq(struct bnad *bnad)
{
R
Rasesh Mody 已提交
854
	clear_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
R
Rasesh Mody 已提交
855

856 857 858 859 860 861 862
	BNAD_UPDATE_CTR(bnad, mbox_intr_enabled);
}

/*
 * Called with bnad->bna_lock held b'cos of
 * bnad->cfg_flags access.
 */
R
Rasesh Mody 已提交
863
static void
864 865
bnad_disable_mbox_irq(struct bnad *bnad)
{
R
Rasesh Mody 已提交
866
	set_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
867

R
Rasesh Mody 已提交
868 869
	BNAD_UPDATE_CTR(bnad, mbox_intr_disabled);
}
870

R
Rasesh Mody 已提交
871 872 873 874
static void
bnad_set_netdev_perm_addr(struct bnad *bnad)
{
	struct net_device *netdev = bnad->netdev;
R
Rasesh Mody 已提交
875

R
Rasesh Mody 已提交
876 877 878
	memcpy(netdev->perm_addr, &bnad->perm_addr, netdev->addr_len);
	if (is_zero_ether_addr(netdev->dev_addr))
		memcpy(netdev->dev_addr, &bnad->perm_addr, netdev->addr_len);
879 880 881 882 883 884
}

/* Control Path Handlers */

/* Callbacks */
void
885
bnad_cb_mbox_intr_enable(struct bnad *bnad)
886 887 888 889 890
{
	bnad_enable_mbox_irq(bnad);
}

void
891
bnad_cb_mbox_intr_disable(struct bnad *bnad)
892 893 894 895 896
{
	bnad_disable_mbox_irq(bnad);
}

void
897 898 899 900 901 902 903 904
bnad_cb_ioceth_ready(struct bnad *bnad)
{
	bnad->bnad_completions.ioc_comp_status = BNA_CB_SUCCESS;
	complete(&bnad->bnad_completions.ioc_comp);
}

void
bnad_cb_ioceth_failed(struct bnad *bnad)
905
{
906
	bnad->bnad_completions.ioc_comp_status = BNA_CB_FAIL;
907 908 909 910
	complete(&bnad->bnad_completions.ioc_comp);
}

void
911
bnad_cb_ioceth_disabled(struct bnad *bnad)
912
{
913
	bnad->bnad_completions.ioc_comp_status = BNA_CB_SUCCESS;
914 915 916 917
	complete(&bnad->bnad_completions.ioc_comp);
}

static void
918
bnad_cb_enet_disabled(void *arg)
919 920 921 922
{
	struct bnad *bnad = (struct bnad *)arg;

	netif_carrier_off(bnad->netdev);
923
	complete(&bnad->bnad_completions.enet_comp);
924 925 926
}

void
927
bnad_cb_ethport_link_status(struct bnad *bnad,
928 929
			enum bna_link_status link_status)
{
930
	bool link_up = false;
931 932 933 934

	link_up = (link_status == BNA_LINK_UP) || (link_status == BNA_CEE_UP);

	if (link_status == BNA_CEE_UP) {
935 936
		if (!test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags))
			BNAD_UPDATE_CTR(bnad, cee_toggle);
937
		set_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags);
938 939 940
	} else {
		if (test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags))
			BNAD_UPDATE_CTR(bnad, cee_toggle);
941
		clear_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags);
942
	}
943 944 945

	if (link_up) {
		if (!netif_carrier_ok(bnad->netdev)) {
946 947
			uint tx_id, tcb_id;
			printk(KERN_WARNING "bna: %s link up\n",
948 949 950
				bnad->netdev->name);
			netif_carrier_on(bnad->netdev);
			BNAD_UPDATE_CTR(bnad, link_toggle);
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
			for (tx_id = 0; tx_id < bnad->num_tx; tx_id++) {
				for (tcb_id = 0; tcb_id < bnad->num_txq_per_tx;
				      tcb_id++) {
					struct bna_tcb *tcb =
					bnad->tx_info[tx_id].tcb[tcb_id];
					u32 txq_id;
					if (!tcb)
						continue;

					txq_id = tcb->id;

					if (test_bit(BNAD_TXQ_TX_STARTED,
						     &tcb->flags)) {
						/*
						 * Force an immediate
						 * Transmit Schedule */
						printk(KERN_INFO "bna: %s %d "
						      "TXQ_STARTED\n",
						       bnad->netdev->name,
						       txq_id);
						netif_wake_subqueue(
								bnad->netdev,
								txq_id);
						BNAD_UPDATE_CTR(bnad,
							netif_queue_wakeup);
					} else {
						netif_stop_subqueue(
								bnad->netdev,
								txq_id);
						BNAD_UPDATE_CTR(bnad,
							netif_queue_stop);
					}
				}
984 985 986 987
			}
		}
	} else {
		if (netif_carrier_ok(bnad->netdev)) {
988
			printk(KERN_WARNING "bna: %s link down\n",
989 990 991 992 993 994 995 996
				bnad->netdev->name);
			netif_carrier_off(bnad->netdev);
			BNAD_UPDATE_CTR(bnad, link_toggle);
		}
	}
}

static void
997
bnad_cb_tx_disabled(void *arg, struct bna_tx *tx)
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
{
	struct bnad *bnad = (struct bnad *)arg;

	complete(&bnad->bnad_completions.tx_comp);
}

static void
bnad_cb_tcb_setup(struct bnad *bnad, struct bna_tcb *tcb)
{
	struct bnad_tx_info *tx_info =
			(struct bnad_tx_info *)tcb->txq->tx->priv;

R
Rasesh Mody 已提交
1010
	tcb->priv = tcb;
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
	tx_info->tcb[tcb->id] = tcb;
}

static void
bnad_cb_tcb_destroy(struct bnad *bnad, struct bna_tcb *tcb)
{
	struct bnad_tx_info *tx_info =
			(struct bnad_tx_info *)tcb->txq->tx->priv;

	tx_info->tcb[tcb->id] = NULL;
J
Jing Huang 已提交
1021
	tcb->priv = NULL;
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
}

static void
bnad_cb_ccb_setup(struct bnad *bnad, struct bna_ccb *ccb)
{
	struct bnad_rx_info *rx_info =
			(struct bnad_rx_info *)ccb->cq->rx->priv;

	rx_info->rx_ctrl[ccb->id].ccb = ccb;
	ccb->ctrl = &rx_info->rx_ctrl[ccb->id];
}

static void
bnad_cb_ccb_destroy(struct bnad *bnad, struct bna_ccb *ccb)
{
	struct bnad_rx_info *rx_info =
			(struct bnad_rx_info *)ccb->cq->rx->priv;

	rx_info->rx_ctrl[ccb->id].ccb = NULL;
}

static void
1044
bnad_cb_tx_stall(struct bnad *bnad, struct bna_tx *tx)
1045 1046
{
	struct bnad_tx_info *tx_info =
1047 1048 1049 1050
			(struct bnad_tx_info *)tx->priv;
	struct bna_tcb *tcb;
	u32 txq_id;
	int i;
1051

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
	for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
		tcb = tx_info->tcb[i];
		if (!tcb)
			continue;
		txq_id = tcb->id;
		clear_bit(BNAD_TXQ_TX_STARTED, &tcb->flags);
		netif_stop_subqueue(bnad->netdev, txq_id);
		printk(KERN_INFO "bna: %s %d TXQ_STOPPED\n",
			bnad->netdev->name, txq_id);
	}
1062 1063 1064
}

static void
1065
bnad_cb_tx_resume(struct bnad *bnad, struct bna_tx *tx)
1066
{
1067 1068 1069 1070
	struct bnad_tx_info *tx_info = (struct bnad_tx_info *)tx->priv;
	struct bna_tcb *tcb;
	u32 txq_id;
	int i;
1071

1072 1073 1074 1075 1076
	for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
		tcb = tx_info->tcb[i];
		if (!tcb)
			continue;
		txq_id = tcb->id;
1077

J
Jing Huang 已提交
1078
		BUG_ON(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags));
1079
		set_bit(BNAD_TXQ_TX_STARTED, &tcb->flags);
J
Jing Huang 已提交
1080
		BUG_ON(*(tcb->hw_consumer_index) != 0);
1081 1082 1083 1084 1085 1086 1087 1088

		if (netif_carrier_ok(bnad->netdev)) {
			printk(KERN_INFO "bna: %s %d TXQ_STARTED\n",
				bnad->netdev->name, txq_id);
			netif_wake_subqueue(bnad->netdev, txq_id);
			BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
		}
	}
R
Rasesh Mody 已提交
1089 1090

	/*
1091
	 * Workaround for first ioceth enable failure & we
R
Rasesh Mody 已提交
1092 1093 1094 1095
	 * get a 0 MAC address. We try to get the MAC address
	 * again here.
	 */
	if (is_zero_ether_addr(&bnad->perm_addr.mac[0])) {
1096
		bna_enet_perm_mac_get(&bnad->bna.enet, &bnad->perm_addr);
R
Rasesh Mody 已提交
1097 1098 1099 1100
		bnad_set_netdev_perm_addr(bnad);
	}
}

J
Jing Huang 已提交
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
/*
 * Free all TxQs buffers and then notify TX_E_CLEANUP_DONE to Tx fsm.
 */
static void
bnad_tx_cleanup(struct delayed_work *work)
{
	struct bnad_tx_info *tx_info =
		container_of(work, struct bnad_tx_info, tx_cleanup_work);
	struct bnad *bnad = NULL;
	struct bna_tcb *tcb;
	unsigned long flags;
R
Rasesh Mody 已提交
1112
	u32 i, pending = 0;
J
Jing Huang 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125

	for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
		tcb = tx_info->tcb[i];
		if (!tcb)
			continue;

		bnad = tcb->bnad;

		if (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags)) {
			pending++;
			continue;
		}

1126
		bnad_txq_cleanup(bnad, tcb);
J
Jing Huang 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142

		smp_mb__before_clear_bit();
		clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
	}

	if (pending) {
		queue_delayed_work(bnad->work_q, &tx_info->tx_cleanup_work,
			msecs_to_jiffies(1));
		return;
	}

	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_tx_cleanup_complete(tx_info->tx);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
}

R
Rasesh Mody 已提交
1143
static void
1144
bnad_cb_tx_cleanup(struct bnad *bnad, struct bna_tx *tx)
R
Rasesh Mody 已提交
1145
{
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
	struct bnad_tx_info *tx_info = (struct bnad_tx_info *)tx->priv;
	struct bna_tcb *tcb;
	int i;

	for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
		tcb = tx_info->tcb[i];
		if (!tcb)
			continue;
	}

J
Jing Huang 已提交
1156
	queue_delayed_work(bnad->work_q, &tx_info->tx_cleanup_work, 0);
1157 1158
}

R
Rasesh Mody 已提交
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
static void
bnad_cb_rx_stall(struct bnad *bnad, struct bna_rx *rx)
{
	struct bnad_rx_info *rx_info = (struct bnad_rx_info *)rx->priv;
	struct bna_ccb *ccb;
	struct bnad_rx_ctrl *rx_ctrl;
	int i;

	for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
		rx_ctrl = &rx_info->rx_ctrl[i];
		ccb = rx_ctrl->ccb;
		if (!ccb)
			continue;

		clear_bit(BNAD_RXQ_POST_OK, &ccb->rcb[0]->flags);

		if (ccb->rcb[1])
			clear_bit(BNAD_RXQ_POST_OK, &ccb->rcb[1]->flags);
	}
}

J
Jing Huang 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
/*
 * Free all RxQs buffers and then notify RX_E_CLEANUP_DONE to Rx fsm.
 */
static void
bnad_rx_cleanup(void *work)
{
	struct bnad_rx_info *rx_info =
		container_of(work, struct bnad_rx_info, rx_cleanup_work);
	struct bnad_rx_ctrl *rx_ctrl;
	struct bnad *bnad = NULL;
	unsigned long flags;
R
Rasesh Mody 已提交
1191
	u32 i;
J
Jing Huang 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206

	for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
		rx_ctrl = &rx_info->rx_ctrl[i];

		if (!rx_ctrl->ccb)
			continue;

		bnad = rx_ctrl->ccb->bnad;

		/*
		 * Wait till the poll handler has exited
		 * and nothing can be scheduled anymore
		 */
		napi_disable(&rx_ctrl->napi);

1207 1208
		bnad_cq_cleanup(bnad, rx_ctrl->ccb);
		bnad_rxq_cleanup(bnad, rx_ctrl->ccb->rcb[0]);
J
Jing Huang 已提交
1209
		if (rx_ctrl->ccb->rcb[1])
1210
			bnad_rxq_cleanup(bnad, rx_ctrl->ccb->rcb[1]);
J
Jing Huang 已提交
1211 1212 1213 1214 1215 1216 1217
	}

	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_rx_cleanup_complete(rx_info->rx);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
}

1218
static void
1219
bnad_cb_rx_cleanup(struct bnad *bnad, struct bna_rx *rx)
1220
{
1221 1222 1223 1224 1225
	struct bnad_rx_info *rx_info = (struct bnad_rx_info *)rx->priv;
	struct bna_ccb *ccb;
	struct bnad_rx_ctrl *rx_ctrl;
	int i;

1226
	for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
		rx_ctrl = &rx_info->rx_ctrl[i];
		ccb = rx_ctrl->ccb;
		if (!ccb)
			continue;

		clear_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags);

		if (ccb->rcb[1])
			clear_bit(BNAD_RXQ_STARTED, &ccb->rcb[1]->flags);
	}
R
Rasesh Mody 已提交
1237

J
Jing Huang 已提交
1238
	queue_work(bnad->work_q, &rx_info->rx_cleanup_work);
1239 1240 1241
}

static void
1242
bnad_cb_rx_post(struct bnad *bnad, struct bna_rx *rx)
1243
{
1244 1245 1246 1247
	struct bnad_rx_info *rx_info = (struct bnad_rx_info *)rx->priv;
	struct bna_ccb *ccb;
	struct bna_rcb *rcb;
	struct bnad_rx_ctrl *rx_ctrl;
R
Rasesh Mody 已提交
1248
	int i, j;
R
Rasesh Mody 已提交
1249

1250
	for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
1251 1252 1253 1254
		rx_ctrl = &rx_info->rx_ctrl[i];
		ccb = rx_ctrl->ccb;
		if (!ccb)
			continue;
R
Rasesh Mody 已提交
1255

J
Jing Huang 已提交
1256
		napi_enable(&rx_ctrl->napi);
1257

1258 1259 1260 1261 1262
		for (j = 0; j < BNAD_MAX_RXQ_PER_RXP; j++) {
			rcb = ccb->rcb[j];
			if (!rcb)
				continue;

R
Rasesh Mody 已提交
1263
			bnad_rxq_alloc_init(bnad, rcb);
1264
			set_bit(BNAD_RXQ_STARTED, &rcb->flags);
R
Rasesh Mody 已提交
1265
			set_bit(BNAD_RXQ_POST_OK, &rcb->flags);
R
Rasesh Mody 已提交
1266
			bnad_rxq_post(bnad, rcb);
1267
		}
1268 1269 1270 1271
	}
}

static void
1272
bnad_cb_rx_disabled(void *arg, struct bna_rx *rx)
1273 1274 1275 1276 1277 1278 1279
{
	struct bnad *bnad = (struct bnad *)arg;

	complete(&bnad->bnad_completions.rx_comp);
}

static void
1280
bnad_cb_rx_mcast_add(struct bnad *bnad, struct bna_rx *rx)
1281
{
1282
	bnad->bnad_completions.mcast_comp_status = BNA_CB_SUCCESS;
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
	complete(&bnad->bnad_completions.mcast_comp);
}

void
bnad_cb_stats_get(struct bnad *bnad, enum bna_cb_status status,
		       struct bna_stats *stats)
{
	if (status == BNA_CB_SUCCESS)
		BNAD_UPDATE_CTR(bnad, hw_stats_updates);

	if (!netif_running(bnad->netdev) ||
		!test_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
		return;

	mod_timer(&bnad->stats_timer,
		  jiffies + msecs_to_jiffies(BNAD_STATS_TIMER_FREQ));
}

1301 1302 1303 1304 1305 1306 1307
static void
bnad_cb_enet_mtu_set(struct bnad *bnad)
{
	bnad->bnad_completions.mtu_comp_status = BNA_CB_SUCCESS;
	complete(&bnad->bnad_completions.mtu_comp);
}

1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
void
bnad_cb_completion(void *arg, enum bfa_status status)
{
	struct bnad_iocmd_comp *iocmd_comp =
			(struct bnad_iocmd_comp *)arg;

	iocmd_comp->comp_status = (u32) status;
	complete(&iocmd_comp->comp);
}

1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
/* Resource allocation, free functions */

static void
bnad_mem_free(struct bnad *bnad,
	      struct bna_mem_info *mem_info)
{
	int i;
	dma_addr_t dma_pa;

	if (mem_info->mdl == NULL)
		return;

	for (i = 0; i < mem_info->num; i++) {
		if (mem_info->mdl[i].kva != NULL) {
			if (mem_info->mem_type == BNA_MEM_T_DMA) {
				BNA_GET_DMA_ADDR(&(mem_info->mdl[i].dma),
						dma_pa);
I
Ivan Vecera 已提交
1335 1336 1337
				dma_free_coherent(&bnad->pcidev->dev,
						  mem_info->mdl[i].len,
						  mem_info->mdl[i].kva, dma_pa);
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
			} else
				kfree(mem_info->mdl[i].kva);
		}
	}
	kfree(mem_info->mdl);
	mem_info->mdl = NULL;
}

static int
bnad_mem_alloc(struct bnad *bnad,
	       struct bna_mem_info *mem_info)
{
	int i;
	dma_addr_t dma_pa;

	if ((mem_info->num == 0) || (mem_info->len == 0)) {
		mem_info->mdl = NULL;
		return 0;
	}

	mem_info->mdl = kcalloc(mem_info->num, sizeof(struct bna_mem_descr),
				GFP_KERNEL);
	if (mem_info->mdl == NULL)
		return -ENOMEM;

	if (mem_info->mem_type == BNA_MEM_T_DMA) {
		for (i = 0; i < mem_info->num; i++) {
			mem_info->mdl[i].len = mem_info->len;
			mem_info->mdl[i].kva =
I
Ivan Vecera 已提交
1367
				dma_alloc_coherent(&bnad->pcidev->dev,
1368 1369
						   mem_info->len, &dma_pa,
						   GFP_KERNEL);
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
			if (mem_info->mdl[i].kva == NULL)
				goto err_return;

			BNA_SET_DMA_ADDR(dma_pa,
					 &(mem_info->mdl[i].dma));
		}
	} else {
		for (i = 0; i < mem_info->num; i++) {
			mem_info->mdl[i].len = mem_info->len;
			mem_info->mdl[i].kva = kzalloc(mem_info->len,
							GFP_KERNEL);
			if (mem_info->mdl[i].kva == NULL)
				goto err_return;
		}
	}

	return 0;

err_return:
	bnad_mem_free(bnad, mem_info);
	return -ENOMEM;
}

/* Free IRQ for Mailbox */
static void
1395
bnad_mbox_irq_free(struct bnad *bnad)
1396 1397 1398 1399 1400 1401
{
	int irq;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
	bnad_disable_mbox_irq(bnad);
R
Rasesh Mody 已提交
1402
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
1403 1404

	irq = BNAD_GET_MBOX_IRQ(bnad);
R
Rasesh Mody 已提交
1405
	free_irq(irq, bnad);
1406 1407 1408 1409 1410 1411 1412 1413
}

/*
 * Allocates IRQ for Mailbox, but keep it disabled
 * This will be enabled once we get the mbox enable callback
 * from bna
 */
static int
1414
bnad_mbox_irq_alloc(struct bnad *bnad)
1415
{
R
Rasesh Mody 已提交
1416 1417
	int		err = 0;
	unsigned long	irq_flags, flags;
1418
	u32	irq;
R
Rasesh Mody 已提交
1419
	irq_handler_t	irq_handler;
1420 1421 1422 1423

	spin_lock_irqsave(&bnad->bna_lock, flags);
	if (bnad->cfg_flags & BNAD_CF_MSIX) {
		irq_handler = (irq_handler_t)bnad_msix_mbox_handler;
1424
		irq = bnad->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector;
1425
		irq_flags = 0;
1426 1427 1428
	} else {
		irq_handler = (irq_handler_t)bnad_isr;
		irq = bnad->pcidev->irq;
1429
		irq_flags = IRQF_SHARED;
1430
	}
1431

1432 1433 1434
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
	sprintf(bnad->mbox_irq_name, "%s", BNAD_NAME);

R
Rasesh Mody 已提交
1435 1436 1437 1438 1439 1440
	/*
	 * Set the Mbox IRQ disable flag, so that the IRQ handler
	 * called from request_irq() for SHARED IRQs do not execute
	 */
	set_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);

R
Rasesh Mody 已提交
1441 1442
	BNAD_UPDATE_CTR(bnad, mbox_intr_disabled);

1443
	err = request_irq(irq, irq_handler, irq_flags,
R
Rasesh Mody 已提交
1444
			  bnad->mbox_irq_name, bnad);
R
Rasesh Mody 已提交
1445

R
Rasesh Mody 已提交
1446
	return err;
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
}

static void
bnad_txrx_irq_free(struct bnad *bnad, struct bna_intr_info *intr_info)
{
	kfree(intr_info->idl);
	intr_info->idl = NULL;
}

/* Allocates Interrupt Descriptor List for MSIX/INT-X vectors */
static int
bnad_txrx_irq_alloc(struct bnad *bnad, enum bnad_intr_source src,
1459
		    u32 txrx_id, struct bna_intr_info *intr_info)
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
{
	int i, vector_start = 0;
	u32 cfg_flags;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
	cfg_flags = bnad->cfg_flags;
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	if (cfg_flags & BNAD_CF_MSIX) {
		intr_info->intr_type = BNA_INTR_T_MSIX;
		intr_info->idl = kcalloc(intr_info->num,
					sizeof(struct bna_intr_descr),
					GFP_KERNEL);
		if (!intr_info->idl)
			return -ENOMEM;

		switch (src) {
		case BNAD_INTR_TX:
1479
			vector_start = BNAD_MAILBOX_MSIX_VECTORS + txrx_id;
1480 1481 1482
			break;

		case BNAD_INTR_RX:
1483 1484
			vector_start = BNAD_MAILBOX_MSIX_VECTORS +
					(bnad->num_tx * bnad->num_txq_per_tx) +
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
					txrx_id;
			break;

		default:
			BUG();
		}

		for (i = 0; i < intr_info->num; i++)
			intr_info->idl[i].vector = vector_start + i;
	} else {
		intr_info->intr_type = BNA_INTR_T_INTX;
		intr_info->num = 1;
		intr_info->idl = kcalloc(intr_info->num,
					sizeof(struct bna_intr_descr),
					GFP_KERNEL);
		if (!intr_info->idl)
			return -ENOMEM;

		switch (src) {
		case BNAD_INTR_TX:
1505
			intr_info->idl[0].vector = BNAD_INTX_TX_IB_BITMASK;
1506 1507 1508
			break;

		case BNAD_INTR_RX:
1509
			intr_info->idl[0].vector = BNAD_INTX_RX_IB_BITMASK;
1510 1511 1512 1513 1514 1515
			break;
		}
	}
	return 0;
}

1516
/* NOTE: Should be called for MSIX only
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
 * Unregisters Tx MSIX vector(s) from the kernel
 */
static void
bnad_tx_msix_unregister(struct bnad *bnad, struct bnad_tx_info *tx_info,
			int num_txqs)
{
	int i;
	int vector_num;

	for (i = 0; i < num_txqs; i++) {
		if (tx_info->tcb[i] == NULL)
			continue;

		vector_num = tx_info->tcb[i]->intr_vector;
		free_irq(bnad->msix_table[vector_num].vector, tx_info->tcb[i]);
	}
}

1535
/* NOTE: Should be called for MSIX only
1536 1537 1538 1539
 * Registers Tx MSIX vector(s) and ISR(s), cookie with the kernel
 */
static int
bnad_tx_msix_register(struct bnad *bnad, struct bnad_tx_info *tx_info,
1540
			u32 tx_id, int num_txqs)
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
{
	int i;
	int err;
	int vector_num;

	for (i = 0; i < num_txqs; i++) {
		vector_num = tx_info->tcb[i]->intr_vector;
		sprintf(tx_info->tcb[i]->name, "%s TXQ %d", bnad->netdev->name,
				tx_id + tx_info->tcb[i]->id);
		err = request_irq(bnad->msix_table[vector_num].vector,
				  (irq_handler_t)bnad_msix_tx, 0,
				  tx_info->tcb[i]->name,
				  tx_info->tcb[i]);
		if (err)
			goto err_return;
	}

	return 0;

err_return:
	if (i > 0)
		bnad_tx_msix_unregister(bnad, tx_info, (i - 1));
	return -1;
}

1566
/* NOTE: Should be called for MSIX only
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
 * Unregisters Rx MSIX vector(s) from the kernel
 */
static void
bnad_rx_msix_unregister(struct bnad *bnad, struct bnad_rx_info *rx_info,
			int num_rxps)
{
	int i;
	int vector_num;

	for (i = 0; i < num_rxps; i++) {
		if (rx_info->rx_ctrl[i].ccb == NULL)
			continue;

		vector_num = rx_info->rx_ctrl[i].ccb->intr_vector;
		free_irq(bnad->msix_table[vector_num].vector,
			 rx_info->rx_ctrl[i].ccb);
	}
}

1586
/* NOTE: Should be called for MSIX only
1587 1588 1589 1590
 * Registers Tx MSIX vector(s) and ISR(s), cookie with the kernel
 */
static int
bnad_rx_msix_register(struct bnad *bnad, struct bnad_rx_info *rx_info,
1591
			u32 rx_id, int num_rxps)
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
{
	int i;
	int err;
	int vector_num;

	for (i = 0; i < num_rxps; i++) {
		vector_num = rx_info->rx_ctrl[i].ccb->intr_vector;
		sprintf(rx_info->rx_ctrl[i].ccb->name, "%s CQ %d",
			bnad->netdev->name,
			rx_id + rx_info->rx_ctrl[i].ccb->id);
		err = request_irq(bnad->msix_table[vector_num].vector,
				  (irq_handler_t)bnad_msix_rx, 0,
				  rx_info->rx_ctrl[i].ccb->name,
				  rx_info->rx_ctrl[i].ccb);
		if (err)
			goto err_return;
	}

	return 0;

err_return:
	if (i > 0)
		bnad_rx_msix_unregister(bnad, rx_info, (i - 1));
	return -1;
}

/* Free Tx object Resources */
static void
bnad_tx_res_free(struct bnad *bnad, struct bna_res_info *res_info)
{
	int i;

	for (i = 0; i < BNA_TX_RES_T_MAX; i++) {
		if (res_info[i].res_type == BNA_RES_T_MEM)
			bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
		else if (res_info[i].res_type == BNA_RES_T_INTR)
			bnad_txrx_irq_free(bnad, &res_info[i].res_u.intr_info);
	}
}

/* Allocates memory and interrupt resources for Tx object */
static int
bnad_tx_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
1635
		  u32 tx_id)
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
{
	int i, err = 0;

	for (i = 0; i < BNA_TX_RES_T_MAX; i++) {
		if (res_info[i].res_type == BNA_RES_T_MEM)
			err = bnad_mem_alloc(bnad,
					&res_info[i].res_u.mem_info);
		else if (res_info[i].res_type == BNA_RES_T_INTR)
			err = bnad_txrx_irq_alloc(bnad, BNAD_INTR_TX, tx_id,
					&res_info[i].res_u.intr_info);
		if (err)
			goto err_return;
	}
	return 0;

err_return:
	bnad_tx_res_free(bnad, res_info);
	return err;
}

/* Free Rx object Resources */
static void
bnad_rx_res_free(struct bnad *bnad, struct bna_res_info *res_info)
{
	int i;

	for (i = 0; i < BNA_RX_RES_T_MAX; i++) {
		if (res_info[i].res_type == BNA_RES_T_MEM)
			bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
		else if (res_info[i].res_type == BNA_RES_T_INTR)
			bnad_txrx_irq_free(bnad, &res_info[i].res_u.intr_info);
	}
}

/* Allocates memory and interrupt resources for Rx object */
static int
bnad_rx_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
		  uint rx_id)
{
	int i, err = 0;

	/* All memory needs to be allocated before setup_ccbs */
	for (i = 0; i < BNA_RX_RES_T_MAX; i++) {
		if (res_info[i].res_type == BNA_RES_T_MEM)
			err = bnad_mem_alloc(bnad,
					&res_info[i].res_u.mem_info);
		else if (res_info[i].res_type == BNA_RES_T_INTR)
			err = bnad_txrx_irq_alloc(bnad, BNAD_INTR_RX, rx_id,
					&res_info[i].res_u.intr_info);
		if (err)
			goto err_return;
	}
	return 0;

err_return:
	bnad_rx_res_free(bnad, res_info);
	return err;
}

/* Timer callbacks */
/* a) IOC timer */
static void
bnad_ioc_timeout(unsigned long data)
{
	struct bnad *bnad = (struct bnad *)data;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
1704
	bfa_nw_ioc_timeout((void *) &bnad->bna.ioceth.ioc);
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
}

static void
bnad_ioc_hb_check(unsigned long data)
{
	struct bnad *bnad = (struct bnad *)data;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
1715
	bfa_nw_ioc_hb_check((void *) &bnad->bna.ioceth.ioc);
1716 1717 1718 1719
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
}

static void
R
Rasesh Mody 已提交
1720
bnad_iocpf_timeout(unsigned long data)
1721 1722 1723 1724 1725
{
	struct bnad *bnad = (struct bnad *)data;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
1726
	bfa_nw_iocpf_timeout((void *) &bnad->bna.ioceth.ioc);
R
Rasesh Mody 已提交
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
}

static void
bnad_iocpf_sem_timeout(unsigned long data)
{
	struct bnad *bnad = (struct bnad *)data;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
1737
	bfa_nw_iocpf_sem_timeout((void *) &bnad->bna.ioceth.ioc);
1738 1739 1740 1741 1742 1743
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
}

/*
 * All timer routines use bnad->bna_lock to protect against
 * the following race, which may occur in case of no locking:
R
Rasesh Mody 已提交
1744
 *	Time	CPU m	CPU n
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
 *	0       1 = test_bit
 *	1			clear_bit
 *	2			del_timer_sync
 *	3	mod_timer
 */

/* b) Dynamic Interrupt Moderation Timer */
static void
bnad_dim_timeout(unsigned long data)
{
	struct bnad *bnad = (struct bnad *)data;
	struct bnad_rx_info *rx_info;
	struct bnad_rx_ctrl *rx_ctrl;
	int i, j;
	unsigned long flags;

	if (!netif_carrier_ok(bnad->netdev))
		return;

	spin_lock_irqsave(&bnad->bna_lock, flags);
	for (i = 0; i < bnad->num_rx; i++) {
		rx_info = &bnad->rx_info[i];
		if (!rx_info->rx)
			continue;
		for (j = 0; j < bnad->num_rxp_per_rx; j++) {
			rx_ctrl = &rx_info->rx_ctrl[j];
			if (!rx_ctrl->ccb)
				continue;
			bna_rx_dim_update(rx_ctrl->ccb);
		}
	}

	/* Check for BNAD_CF_DIM_ENABLED, does not eleminate a race */
	if (test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags))
		mod_timer(&bnad->dim_timer,
			  jiffies + msecs_to_jiffies(BNAD_DIM_TIMER_FREQ));
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
}

/* c)  Statistics Timer */
static void
bnad_stats_timeout(unsigned long data)
{
	struct bnad *bnad = (struct bnad *)data;
	unsigned long flags;

	if (!netif_running(bnad->netdev) ||
		!test_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
		return;

	spin_lock_irqsave(&bnad->bna_lock, flags);
1796
	bna_hw_stats_get(&bnad->bna);
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
}

/*
 * Set up timer for DIM
 * Called with bnad->bna_lock held
 */
void
bnad_dim_timer_start(struct bnad *bnad)
{
	if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED &&
	    !test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags)) {
		setup_timer(&bnad->dim_timer, bnad_dim_timeout,
			    (unsigned long)bnad);
		set_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
		mod_timer(&bnad->dim_timer,
			  jiffies + msecs_to_jiffies(BNAD_DIM_TIMER_FREQ));
	}
}

/*
 * Set up timer for statistics
 * Called with mutex_lock(&bnad->conf_mutex) held
 */
static void
bnad_stats_timer_start(struct bnad *bnad)
{
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
	if (!test_and_set_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags)) {
		setup_timer(&bnad->stats_timer, bnad_stats_timeout,
			    (unsigned long)bnad);
		mod_timer(&bnad->stats_timer,
			  jiffies + msecs_to_jiffies(BNAD_STATS_TIMER_FREQ));
	}
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
}

/*
 * Stops the stats timer
 * Called with mutex_lock(&bnad->conf_mutex) held
 */
static void
bnad_stats_timer_stop(struct bnad *bnad)
{
	int to_del = 0;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
	if (test_and_clear_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
		to_del = 1;
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
	if (to_del)
		del_timer_sync(&bnad->stats_timer);
}

/* Utilities */

static void
bnad_netdev_mc_list_get(struct net_device *netdev, u8 *mc_list)
{
	int i = 1; /* Index 0 has broadcast address */
	struct netdev_hw_addr *mc_addr;

	netdev_for_each_mc_addr(mc_addr, netdev) {
		memcpy(&mc_list[i * ETH_ALEN], &mc_addr->addr[0],
							ETH_ALEN);
		i++;
	}
}

static int
bnad_napi_poll_rx(struct napi_struct *napi, int budget)
{
	struct bnad_rx_ctrl *rx_ctrl =
		container_of(napi, struct bnad_rx_ctrl, napi);
1874
	struct bnad *bnad = rx_ctrl->bnad;
1875 1876
	int rcvd = 0;

R
Rasesh Mody 已提交
1877
	rx_ctrl->rx_poll_ctr++;
1878 1879 1880 1881

	if (!netif_carrier_ok(bnad->netdev))
		goto poll_exit;

1882
	rcvd = bnad_cq_process(bnad, rx_ctrl->ccb, budget);
R
Rasesh Mody 已提交
1883
	if (rcvd >= budget)
1884 1885 1886
		return rcvd;

poll_exit:
R
Rasesh Mody 已提交
1887
	napi_complete(napi);
1888

R
Rasesh Mody 已提交
1889
	rx_ctrl->rx_complete++;
1890 1891

	if (rx_ctrl->ccb)
R
Rasesh Mody 已提交
1892 1893
		bnad_enable_rx_irq_unsafe(rx_ctrl->ccb);

1894 1895 1896
	return rcvd;
}

1897
#define BNAD_NAPI_POLL_QUOTA		64
1898
static void
J
Jing Huang 已提交
1899
bnad_napi_add(struct bnad *bnad, u32 rx_id)
1900 1901 1902 1903 1904 1905 1906 1907
{
	struct bnad_rx_ctrl *rx_ctrl;
	int i;

	/* Initialize & enable NAPI */
	for (i = 0; i <	bnad->num_rxp_per_rx; i++) {
		rx_ctrl = &bnad->rx_info[rx_id].rx_ctrl[i];
		netif_napi_add(bnad->netdev, &rx_ctrl->napi,
1908 1909 1910 1911 1912
			       bnad_napi_poll_rx, BNAD_NAPI_POLL_QUOTA);
	}
}

static void
J
Jing Huang 已提交
1913
bnad_napi_delete(struct bnad *bnad, u32 rx_id)
1914 1915 1916 1917
{
	int i;

	/* First disable and then clean up */
J
Jing Huang 已提交
1918
	for (i = 0; i < bnad->num_rxp_per_rx; i++)
1919 1920 1921 1922 1923
		netif_napi_del(&bnad->rx_info[rx_id].rx_ctrl[i].napi);
}

/* Should be held with conf_lock held */
void
1924
bnad_destroy_tx(struct bnad *bnad, u32 tx_id)
1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
{
	struct bnad_tx_info *tx_info = &bnad->tx_info[tx_id];
	struct bna_res_info *res_info = &bnad->tx_res_info[tx_id].res_info[0];
	unsigned long flags;

	if (!tx_info->tx)
		return;

	init_completion(&bnad->bnad_completions.tx_comp);
	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_tx_disable(tx_info->tx, BNA_HARD_CLEANUP, bnad_cb_tx_disabled);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
	wait_for_completion(&bnad->bnad_completions.tx_comp);

	if (tx_info->tcb[0]->intr_type == BNA_INTR_T_MSIX)
		bnad_tx_msix_unregister(bnad, tx_info,
			bnad->num_txq_per_tx);

	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_tx_destroy(tx_info->tx);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	tx_info->tx = NULL;
1948
	tx_info->tx_id = 0;
1949 1950 1951 1952 1953 1954

	bnad_tx_res_free(bnad, res_info);
}

/* Should be held with conf_lock held */
int
1955
bnad_setup_tx(struct bnad *bnad, u32 tx_id)
1956 1957 1958 1959 1960 1961 1962
{
	int err;
	struct bnad_tx_info *tx_info = &bnad->tx_info[tx_id];
	struct bna_res_info *res_info = &bnad->tx_res_info[tx_id].res_info[0];
	struct bna_intr_info *intr_info =
			&res_info[BNA_TX_RES_INTR_T_TXCMPL].res_u.intr_info;
	struct bna_tx_config *tx_config = &bnad->tx_config[tx_id];
1963 1964 1965 1966 1967 1968 1969 1970
	static const struct bna_tx_event_cbfn tx_cbfn = {
		.tcb_setup_cbfn = bnad_cb_tcb_setup,
		.tcb_destroy_cbfn = bnad_cb_tcb_destroy,
		.tx_stall_cbfn = bnad_cb_tx_stall,
		.tx_resume_cbfn = bnad_cb_tx_resume,
		.tx_cleanup_cbfn = bnad_cb_tx_cleanup,
	};

1971 1972 1973
	struct bna_tx *tx;
	unsigned long flags;

1974 1975
	tx_info->tx_id = tx_id;

1976 1977 1978 1979
	/* Initialize the Tx object configuration */
	tx_config->num_txq = bnad->num_txq_per_tx;
	tx_config->txq_depth = bnad->txq_depth;
	tx_config->tx_type = BNA_TX_T_REGULAR;
1980
	tx_config->coalescing_timeo = bnad->tx_coalescing_timeo;
1981 1982 1983 1984 1985 1986 1987 1988

	/* Get BNA's resource requirement for one tx object */
	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_tx_res_req(bnad->num_txq_per_tx,
		bnad->txq_depth, res_info);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	/* Fill Unmap Q memory requirements */
R
Rasesh Mody 已提交
1989 1990 1991
	BNAD_FILL_UNMAPQ_MEM_REQ(&res_info[BNA_TX_RES_MEM_T_UNMAPQ],
			bnad->num_txq_per_tx, (sizeof(struct bnad_tx_unmap) *
			bnad->txq_depth));
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002

	/* Allocate resources */
	err = bnad_tx_res_alloc(bnad, res_info, tx_id);
	if (err)
		return err;

	/* Ask BNA to create one Tx object, supplying required resources */
	spin_lock_irqsave(&bnad->bna_lock, flags);
	tx = bna_tx_create(&bnad->bna, bnad, tx_config, &tx_cbfn, res_info,
			tx_info);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
R
Rasesh Mody 已提交
2003 2004
	if (!tx) {
		err = -ENOMEM;
2005
		goto err_return;
R
Rasesh Mody 已提交
2006
	}
2007 2008
	tx_info->tx = tx;

J
Jing Huang 已提交
2009 2010 2011
	INIT_DELAYED_WORK(&tx_info->tx_cleanup_work,
			(work_func_t)bnad_tx_cleanup);

2012 2013 2014 2015 2016
	/* Register ISR for the Tx object */
	if (intr_info->intr_type == BNA_INTR_T_MSIX) {
		err = bnad_tx_msix_register(bnad, tx_info,
			tx_id, bnad->num_txq_per_tx);
		if (err)
R
Rasesh Mody 已提交
2017
			goto cleanup_tx;
2018 2019 2020 2021 2022 2023 2024 2025
	}

	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_tx_enable(tx);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	return 0;

R
Rasesh Mody 已提交
2026 2027 2028 2029 2030 2031
cleanup_tx:
	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_tx_destroy(tx_info->tx);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
	tx_info->tx = NULL;
	tx_info->tx_id = 0;
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
err_return:
	bnad_tx_res_free(bnad, res_info);
	return err;
}

/* Setup the rx config for bna_rx_create */
/* bnad decides the configuration */
static void
bnad_init_rx_config(struct bnad *bnad, struct bna_rx_config *rx_config)
{
R
Rasesh Mody 已提交
2042
	memset(rx_config, 0, sizeof(*rx_config));
2043 2044
	rx_config->rx_type = BNA_RX_T_REGULAR;
	rx_config->num_paths = bnad->num_rxp_per_rx;
2045
	rx_config->coalescing_timeo = bnad->rx_coalescing_timeo;
2046 2047 2048 2049

	if (bnad->num_rxp_per_rx > 1) {
		rx_config->rss_status = BNA_STATUS_T_ENABLED;
		rx_config->rss_config.hash_type =
2050 2051 2052 2053
				(BFI_ENET_RSS_IPV6 |
				 BFI_ENET_RSS_IPV6_TCP |
				 BFI_ENET_RSS_IPV4 |
				 BFI_ENET_RSS_IPV4_TCP);
2054 2055 2056 2057 2058 2059 2060 2061 2062
		rx_config->rss_config.hash_mask =
				bnad->num_rxp_per_rx - 1;
		get_random_bytes(rx_config->rss_config.toeplitz_hash_key,
			sizeof(rx_config->rss_config.toeplitz_hash_key));
	} else {
		rx_config->rss_status = BNA_STATUS_T_DISABLED;
		memset(&rx_config->rss_config, 0,
		       sizeof(rx_config->rss_config));
	}
R
Rasesh Mody 已提交
2063 2064 2065 2066 2067 2068 2069 2070 2071

	rx_config->frame_size = BNAD_FRAME_SIZE(bnad->netdev->mtu);
	rx_config->q0_multi_buf = BNA_STATUS_T_DISABLED;

	/* BNA_RXP_SINGLE - one data-buffer queue
	 * BNA_RXP_SLR - one small-buffer and one large-buffer queues
	 * BNA_RXP_HDS - one header-buffer and one data-buffer queues
	 */
	/* TODO: configurable param for queue type */
2072 2073
	rx_config->rxp_type = BNA_RXP_SLR;

R
Rasesh Mody 已提交
2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
	if (BNAD_PCI_DEV_IS_CAT2(bnad) &&
	    rx_config->frame_size > 4096) {
		/* though size_routing_enable is set in SLR,
		 * small packets may get routed to same rxq.
		 * set buf_size to 2048 instead of PAGE_SIZE.
		 */
		rx_config->q0_buf_size = 2048;
		/* this should be in multiples of 2 */
		rx_config->q0_num_vecs = 4;
		rx_config->q0_depth = bnad->rxq_depth * rx_config->q0_num_vecs;
		rx_config->q0_multi_buf = BNA_STATUS_T_ENABLED;
	} else {
		rx_config->q0_buf_size = rx_config->frame_size;
		rx_config->q0_num_vecs = 1;
		rx_config->q0_depth = bnad->rxq_depth;
	}

	/* initialize for q1 for BNA_RXP_SLR/BNA_RXP_HDS */
	if (rx_config->rxp_type == BNA_RXP_SLR) {
		rx_config->q1_depth = bnad->rxq_depth;
		rx_config->q1_buf_size = BFI_SMALL_RXBUF_SIZE;
	}
2096 2097 2098 2099

	rx_config->vlan_strip_status = BNA_STATUS_T_ENABLED;
}

2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
static void
bnad_rx_ctrl_init(struct bnad *bnad, u32 rx_id)
{
	struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
	int i;

	for (i = 0; i < bnad->num_rxp_per_rx; i++)
		rx_info->rx_ctrl[i].bnad = bnad;
}

2110
/* Called with mutex_lock(&bnad->conf_mutex) held */
R
Rasesh Mody 已提交
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153
u32
bnad_reinit_rx(struct bnad *bnad)
{
	struct net_device *netdev = bnad->netdev;
	u32 err = 0, current_err = 0;
	u32 rx_id = 0, count = 0;
	unsigned long flags;

	/* destroy and create new rx objects */
	for (rx_id = 0; rx_id < bnad->num_rx; rx_id++) {
		if (!bnad->rx_info[rx_id].rx)
			continue;
		bnad_destroy_rx(bnad, rx_id);
	}

	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_enet_mtu_set(&bnad->bna.enet,
			 BNAD_FRAME_SIZE(bnad->netdev->mtu), NULL);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	for (rx_id = 0; rx_id < bnad->num_rx; rx_id++) {
		count++;
		current_err = bnad_setup_rx(bnad, rx_id);
		if (current_err && !err) {
			err = current_err;
			pr_err("RXQ:%u setup failed\n", rx_id);
		}
	}

	/* restore rx configuration */
	if (bnad->rx_info[0].rx && !err) {
		bnad_restore_vlans(bnad, 0);
		bnad_enable_default_bcast(bnad);
		spin_lock_irqsave(&bnad->bna_lock, flags);
		bnad_mac_addr_set_locked(bnad, netdev->dev_addr);
		spin_unlock_irqrestore(&bnad->bna_lock, flags);
		bnad_set_rx_mode(netdev);
	}

	return count;
}

/* Called with bnad_conf_lock() held */
2154
void
2155
bnad_destroy_rx(struct bnad *bnad, u32 rx_id)
2156 2157 2158 2159 2160
{
	struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
	struct bna_rx_config *rx_config = &bnad->rx_config[rx_id];
	struct bna_res_info *res_info = &bnad->rx_res_info[rx_id].res_info[0];
	unsigned long flags;
R
Rasesh Mody 已提交
2161
	int to_del = 0;
2162 2163 2164 2165 2166 2167

	if (!rx_info->rx)
		return;

	if (0 == rx_id) {
		spin_lock_irqsave(&bnad->bna_lock, flags);
R
Rasesh Mody 已提交
2168 2169
		if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED &&
		    test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags)) {
2170
			clear_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
R
Rasesh Mody 已提交
2171 2172
			to_del = 1;
		}
2173
		spin_unlock_irqrestore(&bnad->bna_lock, flags);
R
Rasesh Mody 已提交
2174
		if (to_del)
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
			del_timer_sync(&bnad->dim_timer);
	}

	init_completion(&bnad->bnad_completions.rx_comp);
	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_rx_disable(rx_info->rx, BNA_HARD_CLEANUP, bnad_cb_rx_disabled);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
	wait_for_completion(&bnad->bnad_completions.rx_comp);

	if (rx_info->rx_ctrl[0].ccb->intr_type == BNA_INTR_T_MSIX)
		bnad_rx_msix_unregister(bnad, rx_info, rx_config->num_paths);

J
Jing Huang 已提交
2187
	bnad_napi_delete(bnad, rx_id);
2188

2189 2190 2191 2192
	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_rx_destroy(rx_info->rx);

	rx_info->rx = NULL;
2193
	rx_info->rx_id = 0;
2194
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
2195 2196 2197 2198 2199 2200

	bnad_rx_res_free(bnad, res_info);
}

/* Called with mutex_lock(&bnad->conf_mutex) held */
int
2201
bnad_setup_rx(struct bnad *bnad, u32 rx_id)
2202 2203 2204 2205 2206 2207 2208
{
	int err;
	struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
	struct bna_res_info *res_info = &bnad->rx_res_info[rx_id].res_info[0];
	struct bna_intr_info *intr_info =
			&res_info[BNA_RX_RES_T_INTR].res_u.intr_info;
	struct bna_rx_config *rx_config = &bnad->rx_config[rx_id];
2209
	static const struct bna_rx_event_cbfn rx_cbfn = {
R
Rasesh Mody 已提交
2210
		.rcb_setup_cbfn = NULL,
J
Jing Huang 已提交
2211
		.rcb_destroy_cbfn = NULL,
2212 2213
		.ccb_setup_cbfn = bnad_cb_ccb_setup,
		.ccb_destroy_cbfn = bnad_cb_ccb_destroy,
R
Rasesh Mody 已提交
2214
		.rx_stall_cbfn = bnad_cb_rx_stall,
2215 2216 2217
		.rx_cleanup_cbfn = bnad_cb_rx_cleanup,
		.rx_post_cbfn = bnad_cb_rx_post,
	};
2218 2219 2220
	struct bna_rx *rx;
	unsigned long flags;

2221 2222
	rx_info->rx_id = rx_id;

2223 2224 2225 2226 2227 2228 2229 2230 2231
	/* Initialize the Rx object configuration */
	bnad_init_rx_config(bnad, rx_config);

	/* Get BNA's resource requirement for one Rx object */
	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_rx_res_req(rx_config, res_info);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	/* Fill Unmap Q memory requirements */
R
Rasesh Mody 已提交
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244
	BNAD_FILL_UNMAPQ_MEM_REQ(&res_info[BNA_RX_RES_MEM_T_UNMAPDQ],
				 rx_config->num_paths,
			(rx_config->q0_depth *
			 sizeof(struct bnad_rx_unmap)) +
			 sizeof(struct bnad_rx_unmap_q));

	if (rx_config->rxp_type != BNA_RXP_SINGLE) {
		BNAD_FILL_UNMAPQ_MEM_REQ(&res_info[BNA_RX_RES_MEM_T_UNMAPHQ],
					 rx_config->num_paths,
				(rx_config->q1_depth *
				 sizeof(struct bnad_rx_unmap) +
				 sizeof(struct bnad_rx_unmap_q)));
	}
2245 2246 2247 2248 2249
	/* Allocate resource */
	err = bnad_rx_res_alloc(bnad, res_info, rx_id);
	if (err)
		return err;

2250 2251
	bnad_rx_ctrl_init(bnad, rx_id);

2252 2253 2254 2255
	/* Ask BNA to create one Rx object, supplying required resources */
	spin_lock_irqsave(&bnad->bna_lock, flags);
	rx = bna_rx_create(&bnad->bna, bnad, rx_config, &rx_cbfn, res_info,
			rx_info);
2256 2257
	if (!rx) {
		err = -ENOMEM;
2258
		spin_unlock_irqrestore(&bnad->bna_lock, flags);
2259
		goto err_return;
2260
	}
2261
	rx_info->rx = rx;
2262
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
2263

J
Jing Huang 已提交
2264 2265 2266
	INIT_WORK(&rx_info->rx_cleanup_work,
			(work_func_t)(bnad_rx_cleanup));

2267 2268 2269 2270
	/*
	 * Init NAPI, so that state is set to NAPI_STATE_SCHED,
	 * so that IRQ handler cannot schedule NAPI at this point.
	 */
J
Jing Huang 已提交
2271
	bnad_napi_add(bnad, rx_id);
2272

2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
	/* Register ISR for the Rx object */
	if (intr_info->intr_type == BNA_INTR_T_MSIX) {
		err = bnad_rx_msix_register(bnad, rx_info, rx_id,
						rx_config->num_paths);
		if (err)
			goto err_return;
	}

	spin_lock_irqsave(&bnad->bna_lock, flags);
	if (0 == rx_id) {
		/* Set up Dynamic Interrupt Moderation Vector */
		if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED)
			bna_rx_dim_reconfig(&bnad->bna, bna_napi_dim_vector);

		/* Enable VLAN filtering only on the default Rx */
		bna_rx_vlanfilter_enable(rx);

		/* Start the DIM timer */
		bnad_dim_timer_start(bnad);
	}

	bna_rx_enable(rx);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	return 0;

err_return:
2300
	bnad_destroy_rx(bnad, rx_id);
2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321
	return err;
}

/* Called with conf_lock & bnad->bna_lock held */
void
bnad_tx_coalescing_timeo_set(struct bnad *bnad)
{
	struct bnad_tx_info *tx_info;

	tx_info = &bnad->tx_info[0];
	if (!tx_info->tx)
		return;

	bna_tx_coalescing_timeo_set(tx_info->tx, bnad->tx_coalescing_timeo);
}

/* Called with conf_lock & bnad->bna_lock held */
void
bnad_rx_coalescing_timeo_set(struct bnad *bnad)
{
	struct bnad_rx_info *rx_info;
R
Rasesh Mody 已提交
2322
	int	i;
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335

	for (i = 0; i < bnad->num_rx; i++) {
		rx_info = &bnad->rx_info[i];
		if (!rx_info->rx)
			continue;
		bna_rx_coalescing_timeo_set(rx_info->rx,
				bnad->rx_coalescing_timeo);
	}
}

/*
 * Called with bnad->bna_lock held
 */
R
Rasesh Mody 已提交
2336
int
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355
bnad_mac_addr_set_locked(struct bnad *bnad, u8 *mac_addr)
{
	int ret;

	if (!is_valid_ether_addr(mac_addr))
		return -EADDRNOTAVAIL;

	/* If datapath is down, pretend everything went through */
	if (!bnad->rx_info[0].rx)
		return 0;

	ret = bna_rx_ucast_set(bnad->rx_info[0].rx, mac_addr, NULL);
	if (ret != BNA_CB_SUCCESS)
		return -EADDRNOTAVAIL;

	return 0;
}

/* Should be called with conf_lock held */
R
Rasesh Mody 已提交
2356
int
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
bnad_enable_default_bcast(struct bnad *bnad)
{
	struct bnad_rx_info *rx_info = &bnad->rx_info[0];
	int ret;
	unsigned long flags;

	init_completion(&bnad->bnad_completions.mcast_comp);

	spin_lock_irqsave(&bnad->bna_lock, flags);
	ret = bna_rx_mcast_add(rx_info->rx, (u8 *)bnad_bcast_addr,
				bnad_cb_rx_mcast_add);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	if (ret == BNA_CB_SUCCESS)
		wait_for_completion(&bnad->bnad_completions.mcast_comp);
	else
		return -ENODEV;

	if (bnad->bnad_completions.mcast_comp_status != BNA_CB_SUCCESS)
		return -ENODEV;

	return 0;
}

R
Rasesh Mody 已提交
2381
/* Called with mutex_lock(&bnad->conf_mutex) held */
R
Rasesh Mody 已提交
2382
void
R
Rasesh Mody 已提交
2383 2384
bnad_restore_vlans(struct bnad *bnad, u32 rx_id)
{
J
Jiri Pirko 已提交
2385
	u16 vid;
R
Rasesh Mody 已提交
2386 2387
	unsigned long flags;

J
Jiri Pirko 已提交
2388
	for_each_set_bit(vid, bnad->active_vlans, VLAN_N_VID) {
R
Rasesh Mody 已提交
2389
		spin_lock_irqsave(&bnad->bna_lock, flags);
J
Jiri Pirko 已提交
2390
		bna_rx_vlan_add(bnad->rx_info[rx_id].rx, vid);
R
Rasesh Mody 已提交
2391 2392 2393 2394
		spin_unlock_irqrestore(&bnad->bna_lock, flags);
	}
}

2395 2396
/* Statistics utilities */
void
E
Eric Dumazet 已提交
2397
bnad_netdev_qstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
2398 2399 2400 2401 2402 2403
{
	int i, j;

	for (i = 0; i < bnad->num_rx; i++) {
		for (j = 0; j < bnad->num_rxp_per_rx; j++) {
			if (bnad->rx_info[i].rx_ctrl[j].ccb) {
E
Eric Dumazet 已提交
2404
				stats->rx_packets += bnad->rx_info[i].
2405
				rx_ctrl[j].ccb->rcb[0]->rxq->rx_packets;
E
Eric Dumazet 已提交
2406
				stats->rx_bytes += bnad->rx_info[i].
2407 2408 2409 2410
					rx_ctrl[j].ccb->rcb[0]->rxq->rx_bytes;
				if (bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1] &&
					bnad->rx_info[i].rx_ctrl[j].ccb->
					rcb[1]->rxq) {
E
Eric Dumazet 已提交
2411
					stats->rx_packets +=
2412 2413
						bnad->rx_info[i].rx_ctrl[j].
						ccb->rcb[1]->rxq->rx_packets;
E
Eric Dumazet 已提交
2414
					stats->rx_bytes +=
2415 2416 2417 2418 2419 2420 2421 2422 2423
						bnad->rx_info[i].rx_ctrl[j].
						ccb->rcb[1]->rxq->rx_bytes;
				}
			}
		}
	}
	for (i = 0; i < bnad->num_tx; i++) {
		for (j = 0; j < bnad->num_txq_per_tx; j++) {
			if (bnad->tx_info[i].tcb[j]) {
E
Eric Dumazet 已提交
2424
				stats->tx_packets +=
2425
				bnad->tx_info[i].tcb[j]->txq->tx_packets;
E
Eric Dumazet 已提交
2426
				stats->tx_bytes +=
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436
					bnad->tx_info[i].tcb[j]->txq->tx_bytes;
			}
		}
	}
}

/*
 * Must be called with the bna_lock held.
 */
void
E
Eric Dumazet 已提交
2437
bnad_netdev_hwstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
2438
{
2439 2440
	struct bfi_enet_stats_mac *mac_stats;
	u32 bmap;
2441 2442
	int i;

2443
	mac_stats = &bnad->stats.bna_stats->hw_stats.mac_stats;
E
Eric Dumazet 已提交
2444
	stats->rx_errors =
2445 2446 2447
		mac_stats->rx_fcs_error + mac_stats->rx_alignment_error +
		mac_stats->rx_frame_length_error + mac_stats->rx_code_error +
		mac_stats->rx_undersize;
E
Eric Dumazet 已提交
2448
	stats->tx_errors = mac_stats->tx_fcs_error +
2449
					mac_stats->tx_undersize;
E
Eric Dumazet 已提交
2450 2451 2452 2453
	stats->rx_dropped = mac_stats->rx_drop;
	stats->tx_dropped = mac_stats->tx_drop;
	stats->multicast = mac_stats->rx_multicast;
	stats->collisions = mac_stats->tx_total_collision;
2454

E
Eric Dumazet 已提交
2455
	stats->rx_length_errors = mac_stats->rx_frame_length_error;
2456 2457 2458

	/* receive ring buffer overflow  ?? */

E
Eric Dumazet 已提交
2459 2460
	stats->rx_crc_errors = mac_stats->rx_fcs_error;
	stats->rx_frame_errors = mac_stats->rx_alignment_error;
2461
	/* recv'r fifo overrun */
2462 2463
	bmap = bna_rx_rid_mask(&bnad->bna);
	for (i = 0; bmap; i++) {
2464
		if (bmap & 1) {
E
Eric Dumazet 已提交
2465
			stats->rx_fifo_errors +=
2466
				bnad->stats.bna_stats->
2467
					hw_stats.rxf_stats[i].frame_drops;
2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481
			break;
		}
		bmap >>= 1;
	}
}

static void
bnad_mbox_irq_sync(struct bnad *bnad)
{
	u32 irq;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
	if (bnad->cfg_flags & BNAD_CF_MSIX)
2482
		irq = bnad->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector;
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542
	else
		irq = bnad->pcidev->irq;
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	synchronize_irq(irq);
}

/* Utility used by bnad_start_xmit, for doing TSO */
static int
bnad_tso_prepare(struct bnad *bnad, struct sk_buff *skb)
{
	int err;

	if (skb_header_cloned(skb)) {
		err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
		if (err) {
			BNAD_UPDATE_CTR(bnad, tso_err);
			return err;
		}
	}

	/*
	 * For TSO, the TCP checksum field is seeded with pseudo-header sum
	 * excluding the length field.
	 */
	if (skb->protocol == htons(ETH_P_IP)) {
		struct iphdr *iph = ip_hdr(skb);

		/* Do we really need these? */
		iph->tot_len = 0;
		iph->check = 0;

		tcp_hdr(skb)->check =
			~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
					   IPPROTO_TCP, 0);
		BNAD_UPDATE_CTR(bnad, tso4);
	} else {
		struct ipv6hdr *ipv6h = ipv6_hdr(skb);

		ipv6h->payload_len = 0;
		tcp_hdr(skb)->check =
			~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, 0,
					 IPPROTO_TCP, 0);
		BNAD_UPDATE_CTR(bnad, tso6);
	}

	return 0;
}

/*
 * Initialize Q numbers depending on Rx Paths
 * Called with bnad->bna_lock held, because of cfg_flags
 * access.
 */
static void
bnad_q_num_init(struct bnad *bnad)
{
	int rxps;

	rxps = min((uint)num_online_cpus(),
2543
			(uint)(BNAD_MAX_RX * BNAD_MAX_RXP_PER_RX));
2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560

	if (!(bnad->cfg_flags & BNAD_CF_MSIX))
		rxps = 1;	/* INTx */

	bnad->num_rx = 1;
	bnad->num_tx = 1;
	bnad->num_rxp_per_rx = rxps;
	bnad->num_txq_per_tx = BNAD_TXQ_NUM;
}

/*
 * Adjusts the Q numbers, given a number of msix vectors
 * Give preference to RSS as opposed to Tx priority Queues,
 * in such a case, just use 1 Tx Q
 * Called with bnad->bna_lock held b'cos of cfg_flags access
 */
static void
2561
bnad_q_num_adjust(struct bnad *bnad, int msix_vectors, int temp)
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
{
	bnad->num_txq_per_tx = 1;
	if ((msix_vectors >= (bnad->num_tx * bnad->num_txq_per_tx)  +
	     bnad_rxqs_per_cq + BNAD_MAILBOX_MSIX_VECTORS) &&
	    (bnad->cfg_flags & BNAD_CF_MSIX)) {
		bnad->num_rxp_per_rx = msix_vectors -
			(bnad->num_tx * bnad->num_txq_per_tx) -
			BNAD_MAILBOX_MSIX_VECTORS;
	} else
		bnad->num_rxp_per_rx = 1;
}

2574 2575 2576
/* Enable / disable ioceth */
static int
bnad_ioceth_disable(struct bnad *bnad)
2577 2578
{
	unsigned long flags;
2579
	int err = 0;
2580 2581

	spin_lock_irqsave(&bnad->bna_lock, flags);
2582 2583
	init_completion(&bnad->bnad_completions.ioc_comp);
	bna_ioceth_disable(&bnad->bna.ioceth, BNA_HARD_CLEANUP);
2584 2585
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

2586 2587 2588 2589 2590
	wait_for_completion_timeout(&bnad->bnad_completions.ioc_comp,
		msecs_to_jiffies(BNAD_IOCETH_TIMEOUT));

	err = bnad->bnad_completions.ioc_comp_status;
	return err;
2591 2592 2593
}

static int
2594
bnad_ioceth_enable(struct bnad *bnad)
2595 2596 2597 2598 2599
{
	int err = 0;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
2600 2601 2602
	init_completion(&bnad->bnad_completions.ioc_comp);
	bnad->bnad_completions.ioc_comp_status = BNA_CB_WAITING;
	bna_ioceth_enable(&bnad->bna.ioceth);
2603 2604
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

2605 2606
	wait_for_completion_timeout(&bnad->bnad_completions.ioc_comp,
		msecs_to_jiffies(BNAD_IOCETH_TIMEOUT));
2607

2608
	err = bnad->bnad_completions.ioc_comp_status;
2609 2610 2611 2612 2613 2614

	return err;
}

/* Free BNA resources */
static void
2615 2616
bnad_res_free(struct bnad *bnad, struct bna_res_info *res_info,
		u32 res_val_max)
2617 2618 2619
{
	int i;

2620 2621
	for (i = 0; i < res_val_max; i++)
		bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
2622 2623 2624 2625
}

/* Allocates memory and interrupt resources for BNA */
static int
2626 2627
bnad_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
		u32 res_val_max)
2628 2629 2630
{
	int i, err;

2631 2632
	for (i = 0; i < res_val_max; i++) {
		err = bnad_mem_alloc(bnad, &res_info[i].res_u.mem_info);
2633 2634 2635 2636 2637 2638
		if (err)
			goto err_return;
	}
	return 0;

err_return:
2639
	bnad_res_free(bnad, res_info, res_val_max);
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660
	return err;
}

/* Interrupt enable / disable */
static void
bnad_enable_msix(struct bnad *bnad)
{
	int i, ret;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
	if (!(bnad->cfg_flags & BNAD_CF_MSIX)) {
		spin_unlock_irqrestore(&bnad->bna_lock, flags);
		return;
	}
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	if (bnad->msix_table)
		return;

	bnad->msix_table =
R
Rasesh Mody 已提交
2661
		kcalloc(bnad->msix_num, sizeof(struct msix_entry), GFP_KERNEL);
2662 2663 2664 2665

	if (!bnad->msix_table)
		goto intx_mode;

R
Rasesh Mody 已提交
2666
	for (i = 0; i < bnad->msix_num; i++)
2667 2668
		bnad->msix_table[i].entry = i;

R
Rasesh Mody 已提交
2669
	ret = pci_enable_msix(bnad->pcidev, bnad->msix_table, bnad->msix_num);
2670 2671
	if (ret > 0) {
		/* Not enough MSI-X vectors. */
R
Rasesh Mody 已提交
2672 2673
		pr_warn("BNA: %d MSI-X vectors allocated < %d requested\n",
			ret, bnad->msix_num);
2674 2675 2676

		spin_lock_irqsave(&bnad->bna_lock, flags);
		/* ret = #of vectors that we got */
R
Rasesh Mody 已提交
2677 2678
		bnad_q_num_adjust(bnad, (ret - BNAD_MAILBOX_MSIX_VECTORS) / 2,
			(ret - BNAD_MAILBOX_MSIX_VECTORS) / 2);
2679 2680
		spin_unlock_irqrestore(&bnad->bna_lock, flags);

R
Rasesh Mody 已提交
2681
		bnad->msix_num = BNAD_NUM_TXQ + BNAD_NUM_RXP +
2682 2683
			 BNAD_MAILBOX_MSIX_VECTORS;

2684 2685 2686
		if (bnad->msix_num > ret)
			goto intx_mode;

2687 2688 2689
		/* Try once more with adjusted numbers */
		/* If this fails, fall back to INTx */
		ret = pci_enable_msix(bnad->pcidev, bnad->msix_table,
R
Rasesh Mody 已提交
2690
				      bnad->msix_num);
2691 2692 2693 2694 2695
		if (ret)
			goto intx_mode;

	} else if (ret < 0)
		goto intx_mode;
2696 2697 2698

	pci_intx(bnad->pcidev, 0);

2699 2700 2701
	return;

intx_mode:
R
Rasesh Mody 已提交
2702
	pr_warn("BNA: MSI-X enable failed - operating in INTx mode\n");
2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757

	kfree(bnad->msix_table);
	bnad->msix_table = NULL;
	bnad->msix_num = 0;
	spin_lock_irqsave(&bnad->bna_lock, flags);
	bnad->cfg_flags &= ~BNAD_CF_MSIX;
	bnad_q_num_init(bnad);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
}

static void
bnad_disable_msix(struct bnad *bnad)
{
	u32 cfg_flags;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
	cfg_flags = bnad->cfg_flags;
	if (bnad->cfg_flags & BNAD_CF_MSIX)
		bnad->cfg_flags &= ~BNAD_CF_MSIX;
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	if (cfg_flags & BNAD_CF_MSIX) {
		pci_disable_msix(bnad->pcidev);
		kfree(bnad->msix_table);
		bnad->msix_table = NULL;
	}
}

/* Netdev entry points */
static int
bnad_open(struct net_device *netdev)
{
	int err;
	struct bnad *bnad = netdev_priv(netdev);
	struct bna_pause_config pause_config;
	unsigned long flags;

	mutex_lock(&bnad->conf_mutex);

	/* Tx */
	err = bnad_setup_tx(bnad, 0);
	if (err)
		goto err_return;

	/* Rx */
	err = bnad_setup_rx(bnad, 0);
	if (err)
		goto cleanup_tx;

	/* Port */
	pause_config.tx_pause = 0;
	pause_config.rx_pause = 0;

	spin_lock_irqsave(&bnad->bna_lock, flags);
R
Rasesh Mody 已提交
2758 2759
	bna_enet_mtu_set(&bnad->bna.enet,
			 BNAD_FRAME_SIZE(bnad->netdev->mtu), NULL);
2760 2761
	bna_enet_pause_config(&bnad->bna.enet, &pause_config, NULL);
	bna_enet_enable(&bnad->bna.enet);
2762 2763 2764 2765 2766
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	/* Enable broadcast */
	bnad_enable_default_bcast(bnad);

R
Rasesh Mody 已提交
2767 2768 2769
	/* Restore VLANs, if any */
	bnad_restore_vlans(bnad, 0);

2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782
	/* Set the UCAST address */
	spin_lock_irqsave(&bnad->bna_lock, flags);
	bnad_mac_addr_set_locked(bnad, netdev->dev_addr);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	/* Start the stats timer */
	bnad_stats_timer_start(bnad);

	mutex_unlock(&bnad->conf_mutex);

	return 0;

cleanup_tx:
2783
	bnad_destroy_tx(bnad, 0);
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800

err_return:
	mutex_unlock(&bnad->conf_mutex);
	return err;
}

static int
bnad_stop(struct net_device *netdev)
{
	struct bnad *bnad = netdev_priv(netdev);
	unsigned long flags;

	mutex_lock(&bnad->conf_mutex);

	/* Stop the stats timer */
	bnad_stats_timer_stop(bnad);

2801
	init_completion(&bnad->bnad_completions.enet_comp);
2802 2803

	spin_lock_irqsave(&bnad->bna_lock, flags);
2804 2805
	bna_enet_disable(&bnad->bna.enet, BNA_HARD_CLEANUP,
			bnad_cb_enet_disabled);
2806 2807
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

2808
	wait_for_completion(&bnad->bnad_completions.enet_comp);
2809

2810 2811
	bnad_destroy_tx(bnad, 0);
	bnad_destroy_rx(bnad, 0);
2812 2813 2814 2815 2816 2817 2818 2819 2820 2821

	/* Synchronize mailbox IRQ */
	bnad_mbox_irq_sync(bnad);

	mutex_unlock(&bnad->conf_mutex);

	return 0;
}

/* TX */
R
Rasesh Mody 已提交
2822 2823 2824 2825
/* Returns 0 for success */
static int
bnad_txq_wi_prepare(struct bnad *bnad, struct bna_tcb *tcb,
		    struct sk_buff *skb, struct bna_txq_entry *txqent)
2826
{
R
Rasesh Mody 已提交
2827 2828 2829
	u16 flags = 0;
	u32 gso_size;
	u16 vlan_tag = 0;
2830

2831
	if (vlan_tx_tag_present(skb)) {
R
Rasesh Mody 已提交
2832
		vlan_tag = (u16)vlan_tx_tag_get(skb);
2833 2834 2835
		flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
	}
	if (test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags)) {
R
Rasesh Mody 已提交
2836 2837
		vlan_tag = ((tcb->priority & 0x7) << VLAN_PRIO_SHIFT)
				| (vlan_tag & 0x1fff);
2838 2839 2840 2841 2842
		flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
	}
	txqent->hdr.wi.vlan_tag = htons(vlan_tag);

	if (skb_is_gso(skb)) {
R
Rasesh Mody 已提交
2843
		gso_size = skb_shinfo(skb)->gso_size;
R
Rasesh Mody 已提交
2844
		if (unlikely(gso_size > bnad->netdev->mtu)) {
R
Rasesh Mody 已提交
2845
			BNAD_UPDATE_CTR(bnad, tx_skb_mss_too_long);
R
Rasesh Mody 已提交
2846
			return -EINVAL;
R
Rasesh Mody 已提交
2847 2848
		}
		if (unlikely((gso_size + skb_transport_offset(skb) +
R
Rasesh Mody 已提交
2849
			      tcp_hdrlen(skb)) >= skb->len)) {
R
Rasesh Mody 已提交
2850 2851 2852 2853 2854 2855 2856 2857 2858 2859
			txqent->hdr.wi.opcode =
				__constant_htons(BNA_TXQ_WI_SEND);
			txqent->hdr.wi.lso_mss = 0;
			BNAD_UPDATE_CTR(bnad, tx_skb_tso_too_short);
		} else {
			txqent->hdr.wi.opcode =
				__constant_htons(BNA_TXQ_WI_SEND_LSO);
			txqent->hdr.wi.lso_mss = htons(gso_size);
		}

R
Rasesh Mody 已提交
2860
		if (bnad_tso_prepare(bnad, skb)) {
R
Rasesh Mody 已提交
2861
			BNAD_UPDATE_CTR(bnad, tx_skb_tso_prepare);
R
Rasesh Mody 已提交
2862
			return -EINVAL;
2863
		}
R
Rasesh Mody 已提交
2864

2865 2866
		flags |= (BNA_TXQ_WI_CF_IP_CKSUM | BNA_TXQ_WI_CF_TCP_CKSUM);
		txqent->hdr.wi.l4_hdr_size_n_offset =
R
Rasesh Mody 已提交
2867 2868 2869
			htons(BNA_TXQ_WI_L4_HDR_N_OFFSET(
			tcp_hdrlen(skb) >> 2, skb_transport_offset(skb)));
	} else  {
R
Rasesh Mody 已提交
2870
		txqent->hdr.wi.opcode =	__constant_htons(BNA_TXQ_WI_SEND);
2871 2872
		txqent->hdr.wi.lso_mss = 0;

R
Rasesh Mody 已提交
2873
		if (unlikely(skb->len > (bnad->netdev->mtu + ETH_HLEN))) {
R
Rasesh Mody 已提交
2874
			BNAD_UPDATE_CTR(bnad, tx_skb_non_tso_too_long);
R
Rasesh Mody 已提交
2875
			return -EINVAL;
2876 2877
		}

R
Rasesh Mody 已提交
2878 2879
		if (skb->ip_summed == CHECKSUM_PARTIAL) {
			u8 proto = 0;
2880

R
Rasesh Mody 已提交
2881 2882
			if (skb->protocol == __constant_htons(ETH_P_IP))
				proto = ip_hdr(skb)->protocol;
R
Rasesh Mody 已提交
2883
#ifdef NETIF_F_IPV6_CSUM
R
Rasesh Mody 已提交
2884 2885 2886 2887 2888
			else if (skb->protocol ==
				 __constant_htons(ETH_P_IPV6)) {
				/* nexthdr may not be TCP immediately. */
				proto = ipv6_hdr(skb)->nexthdr;
			}
R
Rasesh Mody 已提交
2889
#endif
R
Rasesh Mody 已提交
2890 2891 2892 2893 2894 2895 2896 2897 2898
			if (proto == IPPROTO_TCP) {
				flags |= BNA_TXQ_WI_CF_TCP_CKSUM;
				txqent->hdr.wi.l4_hdr_size_n_offset =
					htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
					      (0, skb_transport_offset(skb)));

				BNAD_UPDATE_CTR(bnad, tcpcsum_offload);

				if (unlikely(skb_headlen(skb) <
R
Rasesh Mody 已提交
2899 2900
					    skb_transport_offset(skb) +
				    tcp_hdrlen(skb))) {
R
Rasesh Mody 已提交
2901
					BNAD_UPDATE_CTR(bnad, tx_skb_tcp_hdr);
R
Rasesh Mody 已提交
2902
					return -EINVAL;
R
Rasesh Mody 已提交
2903 2904 2905 2906 2907 2908 2909 2910 2911
				}
			} else if (proto == IPPROTO_UDP) {
				flags |= BNA_TXQ_WI_CF_UDP_CKSUM;
				txqent->hdr.wi.l4_hdr_size_n_offset =
					htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
					      (0, skb_transport_offset(skb)));

				BNAD_UPDATE_CTR(bnad, udpcsum_offload);
				if (unlikely(skb_headlen(skb) <
R
Rasesh Mody 已提交
2912
					    skb_transport_offset(skb) +
R
Rasesh Mody 已提交
2913 2914
				    sizeof(struct udphdr))) {
					BNAD_UPDATE_CTR(bnad, tx_skb_udp_hdr);
R
Rasesh Mody 已提交
2915
					return -EINVAL;
R
Rasesh Mody 已提交
2916 2917
				}
			} else {
R
Rasesh Mody 已提交
2918

R
Rasesh Mody 已提交
2919
				BNAD_UPDATE_CTR(bnad, tx_skb_csum_err);
R
Rasesh Mody 已提交
2920
				return -EINVAL;
2921
			}
R
Rasesh Mody 已提交
2922
		} else
R
Rasesh Mody 已提交
2923
			txqent->hdr.wi.l4_hdr_size_n_offset = 0;
2924 2925 2926 2927 2928
	}

	txqent->hdr.wi.flags = htons(flags);
	txqent->hdr.wi.frame_length = htonl(skb->len);

R
Rasesh Mody 已提交
2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948
	return 0;
}

/*
 * bnad_start_xmit : Netdev entry point for Transmit
 *		     Called under lock held by net_device
 */
static netdev_tx_t
bnad_start_xmit(struct sk_buff *skb, struct net_device *netdev)
{
	struct bnad *bnad = netdev_priv(netdev);
	u32 txq_id = 0;
	struct bna_tcb *tcb = NULL;
	struct bnad_tx_unmap *unmap_q, *unmap, *head_unmap;
	u32		prod, q_depth, vect_id;
	u32		wis, vectors, len;
	int		i;
	dma_addr_t		dma_addr;
	struct bna_txq_entry *txqent;

R
Rasesh Mody 已提交
2949
	len = skb_headlen(skb);
2950

R
Rasesh Mody 已提交
2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969
	/* Sanity checks for the skb */

	if (unlikely(skb->len <= ETH_HLEN)) {
		dev_kfree_skb(skb);
		BNAD_UPDATE_CTR(bnad, tx_skb_too_short);
		return NETDEV_TX_OK;
	}
	if (unlikely(len > BFI_TX_MAX_DATA_PER_VECTOR)) {
		dev_kfree_skb(skb);
		BNAD_UPDATE_CTR(bnad, tx_skb_headlen_zero);
		return NETDEV_TX_OK;
	}
	if (unlikely(len == 0)) {
		dev_kfree_skb(skb);
		BNAD_UPDATE_CTR(bnad, tx_skb_headlen_zero);
		return NETDEV_TX_OK;
	}

	tcb = bnad->tx_info[0].tcb[txq_id];
R
Rasesh Mody 已提交
2970

R
Rasesh Mody 已提交
2971 2972 2973 2974
	/*
	 * Takes care of the Tx that is scheduled between clearing the flag
	 * and the netif_tx_stop_all_queues() call.
	 */
2975
	if (unlikely(!tcb || !test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))) {
R
Rasesh Mody 已提交
2976 2977 2978 2979 2980
		dev_kfree_skb(skb);
		BNAD_UPDATE_CTR(bnad, tx_skb_stopping);
		return NETDEV_TX_OK;
	}

2981 2982 2983 2984
	q_depth = tcb->q_depth;
	prod = tcb->producer_index;
	unmap_q = tcb->unmap_q;

R
Rasesh Mody 已提交
2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047
	vectors = 1 + skb_shinfo(skb)->nr_frags;
	wis = BNA_TXQ_WI_NEEDED(vectors);	/* 4 vectors per work item */

	if (unlikely(vectors > BFI_TX_MAX_VECTORS_PER_PKT)) {
		dev_kfree_skb(skb);
		BNAD_UPDATE_CTR(bnad, tx_skb_max_vectors);
		return NETDEV_TX_OK;
	}

	/* Check for available TxQ resources */
	if (unlikely(wis > BNA_QE_FREE_CNT(tcb, q_depth))) {
		if ((*tcb->hw_consumer_index != tcb->consumer_index) &&
		    !test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags)) {
			u32 sent;
			sent = bnad_txcmpl_process(bnad, tcb);
			if (likely(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
				bna_ib_ack(tcb->i_dbell, sent);
			smp_mb__before_clear_bit();
			clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
		} else {
			netif_stop_queue(netdev);
			BNAD_UPDATE_CTR(bnad, netif_queue_stop);
		}

		smp_mb();
		/*
		 * Check again to deal with race condition between
		 * netif_stop_queue here, and netif_wake_queue in
		 * interrupt handler which is not inside netif tx lock.
		 */
		if (likely(wis > BNA_QE_FREE_CNT(tcb, q_depth))) {
			BNAD_UPDATE_CTR(bnad, netif_queue_stop);
			return NETDEV_TX_BUSY;
		} else {
			netif_wake_queue(netdev);
			BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
		}
	}

	txqent = &((struct bna_txq_entry *)tcb->sw_q)[prod];
	head_unmap = &unmap_q[prod];

	/* Program the opcode, flags, frame_len, num_vectors in WI */
	if (bnad_txq_wi_prepare(bnad, tcb, skb, txqent)) {
		dev_kfree_skb(skb);
		return NETDEV_TX_OK;
	}
	txqent->hdr.wi.reserved = 0;
	txqent->hdr.wi.num_vectors = vectors;

	head_unmap->skb = skb;
	head_unmap->nvecs = 0;

	/* Program the vectors */
	unmap = head_unmap;
	dma_addr = dma_map_single(&bnad->pcidev->dev, skb->data,
				  len, DMA_TO_DEVICE);
	BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[0].host_addr);
	txqent->vector[0].length = htons(len);
	dma_unmap_addr_set(&unmap->vectors[0], dma_addr, dma_addr);
	head_unmap->nvecs++;

	for (i = 0, vect_id = 0; i < vectors - 1; i++) {
E
Eric Dumazet 已提交
3048
		const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
R
Rasesh Mody 已提交
3049
		u32		size = skb_frag_size(frag);
3050

R
Rasesh Mody 已提交
3051
		if (unlikely(size == 0)) {
R
Rasesh Mody 已提交
3052 3053 3054
			/* Undo the changes starting at tcb->producer_index */
			bnad_tx_buff_unmap(bnad, unmap_q, q_depth,
				tcb->producer_index);
R
Rasesh Mody 已提交
3055 3056 3057 3058 3059 3060 3061
			dev_kfree_skb(skb);
			BNAD_UPDATE_CTR(bnad, tx_skb_frag_zero);
			return NETDEV_TX_OK;
		}

		len += size;

R
Rasesh Mody 已提交
3062 3063
		vect_id++;
		if (vect_id == BFI_TX_MAX_VECTORS_PER_WI) {
3064
			vect_id = 0;
R
Rasesh Mody 已提交
3065 3066
			BNA_QE_INDX_INC(prod, q_depth);
			txqent = &((struct bna_txq_entry *)tcb->sw_q)[prod];
R
Rasesh Mody 已提交
3067 3068
			txqent->hdr.wi_ext.opcode =
				__constant_htons(BNA_TXQ_WI_EXTENSION);
R
Rasesh Mody 已提交
3069
			unmap = &unmap_q[prod];
3070 3071
		}

3072 3073
		dma_addr = skb_frag_dma_map(&bnad->pcidev->dev, frag,
					    0, size, DMA_TO_DEVICE);
3074
		dma_unmap_len_set(&unmap->vectors[vect_id], dma_len, size);
3075
		BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[vect_id].host_addr);
R
Rasesh Mody 已提交
3076 3077
		txqent->vector[vect_id].length = htons(size);
		dma_unmap_addr_set(&unmap->vectors[vect_id], dma_addr,
3078
				   dma_addr);
R
Rasesh Mody 已提交
3079
		head_unmap->nvecs++;
3080 3081
	}

R
Rasesh Mody 已提交
3082
	if (unlikely(len != skb->len)) {
R
Rasesh Mody 已提交
3083 3084
		/* Undo the changes starting at tcb->producer_index */
		bnad_tx_buff_unmap(bnad, unmap_q, q_depth, tcb->producer_index);
R
Rasesh Mody 已提交
3085 3086 3087 3088 3089
		dev_kfree_skb(skb);
		BNAD_UPDATE_CTR(bnad, tx_skb_len_mismatch);
		return NETDEV_TX_OK;
	}

R
Rasesh Mody 已提交
3090 3091
	BNA_QE_INDX_INC(prod, q_depth);
	tcb->producer_index = prod;
3092 3093

	smp_mb();
R
Rasesh Mody 已提交
3094 3095 3096 3097

	if (unlikely(!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
		return NETDEV_TX_OK;

3098 3099
	skb_tx_timestamp(skb);

3100
	bna_txq_prod_indx_doorbell(tcb);
R
Rasesh Mody 已提交
3101
	smp_mb();
3102 3103 3104 3105 3106 3107 3108 3109

	return NETDEV_TX_OK;
}

/*
 * Used spin_lock to synchronize reading of stats structures, which
 * is written by BNA under the same lock.
 */
E
Eric Dumazet 已提交
3110 3111
static struct rtnl_link_stats64 *
bnad_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
3112 3113 3114 3115 3116 3117
{
	struct bnad *bnad = netdev_priv(netdev);
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);

E
Eric Dumazet 已提交
3118 3119
	bnad_netdev_qstats_fill(bnad, stats);
	bnad_netdev_hwstats_fill(bnad, stats);
3120 3121 3122

	spin_unlock_irqrestore(&bnad->bna_lock, flags);

E
Eric Dumazet 已提交
3123
	return stats;
3124 3125
}

R
Rasesh Mody 已提交
3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209
static void
bnad_set_rx_ucast_fltr(struct bnad *bnad)
{
	struct net_device *netdev = bnad->netdev;
	int uc_count = netdev_uc_count(netdev);
	enum bna_cb_status ret;
	u8 *mac_list;
	struct netdev_hw_addr *ha;
	int entry;

	if (netdev_uc_empty(bnad->netdev)) {
		bna_rx_ucast_listset(bnad->rx_info[0].rx, 0, NULL, NULL);
		return;
	}

	if (uc_count > bna_attr(&bnad->bna)->num_ucmac)
		goto mode_default;

	mac_list = kzalloc(uc_count * ETH_ALEN, GFP_ATOMIC);
	if (mac_list == NULL)
		goto mode_default;

	entry = 0;
	netdev_for_each_uc_addr(ha, netdev) {
		memcpy(&mac_list[entry * ETH_ALEN],
		       &ha->addr[0], ETH_ALEN);
		entry++;
	}

	ret = bna_rx_ucast_listset(bnad->rx_info[0].rx, entry,
			mac_list, NULL);
	kfree(mac_list);

	if (ret != BNA_CB_SUCCESS)
		goto mode_default;

	return;

	/* ucast packets not in UCAM are routed to default function */
mode_default:
	bnad->cfg_flags |= BNAD_CF_DEFAULT;
	bna_rx_ucast_listset(bnad->rx_info[0].rx, 0, NULL, NULL);
}

static void
bnad_set_rx_mcast_fltr(struct bnad *bnad)
{
	struct net_device *netdev = bnad->netdev;
	int mc_count = netdev_mc_count(netdev);
	enum bna_cb_status ret;
	u8 *mac_list;

	if (netdev->flags & IFF_ALLMULTI)
		goto mode_allmulti;

	if (netdev_mc_empty(netdev))
		return;

	if (mc_count > bna_attr(&bnad->bna)->num_mcmac)
		goto mode_allmulti;

	mac_list = kzalloc((mc_count + 1) * ETH_ALEN, GFP_ATOMIC);

	if (mac_list == NULL)
		goto mode_allmulti;

	memcpy(&mac_list[0], &bnad_bcast_addr[0], ETH_ALEN);

	/* copy rest of the MCAST addresses */
	bnad_netdev_mc_list_get(netdev, mac_list);
	ret = bna_rx_mcast_listset(bnad->rx_info[0].rx, mc_count + 1,
			mac_list, NULL);
	kfree(mac_list);

	if (ret != BNA_CB_SUCCESS)
		goto mode_allmulti;

	return;

mode_allmulti:
	bnad->cfg_flags |= BNAD_CF_ALLMULTI;
	bna_rx_mcast_delall(bnad->rx_info[0].rx, NULL);
}

R
Rasesh Mody 已提交
3210
void
3211 3212 3213
bnad_set_rx_mode(struct net_device *netdev)
{
	struct bnad *bnad = netdev_priv(netdev);
R
Rasesh Mody 已提交
3214
	enum bna_rxmode new_mode, mode_mask;
3215 3216 3217 3218
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);

R
Rasesh Mody 已提交
3219 3220 3221
	if (bnad->rx_info[0].rx == NULL) {
		spin_unlock_irqrestore(&bnad->bna_lock, flags);
		return;
3222 3223
	}

R
Rasesh Mody 已提交
3224 3225 3226
	/* clear bnad flags to update it with new settings */
	bnad->cfg_flags &= ~(BNAD_CF_PROMISC | BNAD_CF_DEFAULT |
			BNAD_CF_ALLMULTI);
R
Rasesh Mody 已提交
3227

R
Rasesh Mody 已提交
3228 3229 3230 3231 3232 3233
	new_mode = 0;
	if (netdev->flags & IFF_PROMISC) {
		new_mode |= BNAD_RXMODE_PROMISC_DEFAULT;
		bnad->cfg_flags |= BNAD_CF_PROMISC;
	} else {
		bnad_set_rx_mcast_fltr(bnad);
3234

R
Rasesh Mody 已提交
3235 3236
		if (bnad->cfg_flags & BNAD_CF_ALLMULTI)
			new_mode |= BNA_RXMODE_ALLMULTI;
3237

R
Rasesh Mody 已提交
3238
		bnad_set_rx_ucast_fltr(bnad);
3239

R
Rasesh Mody 已提交
3240 3241 3242
		if (bnad->cfg_flags & BNAD_CF_DEFAULT)
			new_mode |= BNA_RXMODE_DEFAULT;
	}
3243

R
Rasesh Mody 已提交
3244 3245 3246
	mode_mask = BNA_RXMODE_PROMISC | BNA_RXMODE_DEFAULT |
			BNA_RXMODE_ALLMULTI;
	bna_rx_mode_set(bnad->rx_info[0].rx, new_mode, mode_mask, NULL);
3247

R
Rasesh Mody 已提交
3248 3249 3250 3251
	if (bnad->cfg_flags & BNAD_CF_PROMISC)
		bna_rx_vlan_strip_disable(bnad->rx_info[0].rx);
	else
		bna_rx_vlan_strip_enable(bnad->rx_info[0].rx);
3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281

	spin_unlock_irqrestore(&bnad->bna_lock, flags);
}

/*
 * bna_lock is used to sync writes to netdev->addr
 * conf_lock cannot be used since this call may be made
 * in a non-blocking context.
 */
static int
bnad_set_mac_address(struct net_device *netdev, void *mac_addr)
{
	int err;
	struct bnad *bnad = netdev_priv(netdev);
	struct sockaddr *sa = (struct sockaddr *)mac_addr;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);

	err = bnad_mac_addr_set_locked(bnad, sa->sa_data);

	if (!err)
		memcpy(netdev->dev_addr, sa->sa_data, netdev->addr_len);

	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	return err;
}

static int
R
Rasesh Mody 已提交
3282
bnad_mtu_set(struct bnad *bnad, int frame_size)
3283 3284 3285
{
	unsigned long flags;

3286 3287 3288
	init_completion(&bnad->bnad_completions.mtu_comp);

	spin_lock_irqsave(&bnad->bna_lock, flags);
R
Rasesh Mody 已提交
3289
	bna_enet_mtu_set(&bnad->bna.enet, frame_size, bnad_cb_enet_mtu_set);
3290 3291 3292 3293 3294 3295 3296 3297 3298 3299
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	wait_for_completion(&bnad->bnad_completions.mtu_comp);

	return bnad->bnad_completions.mtu_comp_status;
}

static int
bnad_change_mtu(struct net_device *netdev, int new_mtu)
{
R
Rasesh Mody 已提交
3300
	int err, mtu;
3301
	struct bnad *bnad = netdev_priv(netdev);
R
Rasesh Mody 已提交
3302
	u32 rx_count = 0, frame, new_frame;
3303 3304 3305 3306 3307 3308

	if (new_mtu + ETH_HLEN < ETH_ZLEN || new_mtu > BNAD_JUMBO_MTU)
		return -EINVAL;

	mutex_lock(&bnad->conf_mutex);

R
Rasesh Mody 已提交
3309
	mtu = netdev->mtu;
3310 3311
	netdev->mtu = new_mtu;

R
Rasesh Mody 已提交
3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327
	frame = BNAD_FRAME_SIZE(mtu);
	new_frame = BNAD_FRAME_SIZE(new_mtu);

	/* check if multi-buffer needs to be enabled */
	if (BNAD_PCI_DEV_IS_CAT2(bnad) &&
	    netif_running(bnad->netdev)) {
		/* only when transition is over 4K */
		if ((frame <= 4096 && new_frame > 4096) ||
		    (frame > 4096 && new_frame <= 4096))
			rx_count = bnad_reinit_rx(bnad);
	}

	/* rx_count > 0 - new rx created
	 *	- Linux set err = 0 and return
	 */
	err = bnad_mtu_set(bnad, new_frame);
3328 3329
	if (err)
		err = -EBUSY;
3330 3331 3332 3333 3334

	mutex_unlock(&bnad->conf_mutex);
	return err;
}

3335
static int
3336
bnad_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
3337 3338 3339 3340 3341
{
	struct bnad *bnad = netdev_priv(netdev);
	unsigned long flags;

	if (!bnad->rx_info[0].rx)
3342
		return 0;
3343 3344 3345 3346 3347

	mutex_lock(&bnad->conf_mutex);

	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_rx_vlan_add(bnad->rx_info[0].rx, vid);
J
Jiri Pirko 已提交
3348
	set_bit(vid, bnad->active_vlans);
3349 3350 3351
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	mutex_unlock(&bnad->conf_mutex);
3352 3353

	return 0;
3354 3355
}

3356
static int
3357
bnad_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
3358 3359 3360 3361 3362
{
	struct bnad *bnad = netdev_priv(netdev);
	unsigned long flags;

	if (!bnad->rx_info[0].rx)
3363
		return 0;
3364 3365 3366 3367

	mutex_lock(&bnad->conf_mutex);

	spin_lock_irqsave(&bnad->bna_lock, flags);
J
Jiri Pirko 已提交
3368
	clear_bit(vid, bnad->active_vlans);
3369 3370 3371 3372
	bna_rx_vlan_del(bnad->rx_info[0].rx, vid);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	mutex_unlock(&bnad->conf_mutex);
3373 3374

	return 0;
3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391
}

#ifdef CONFIG_NET_POLL_CONTROLLER
static void
bnad_netpoll(struct net_device *netdev)
{
	struct bnad *bnad = netdev_priv(netdev);
	struct bnad_rx_info *rx_info;
	struct bnad_rx_ctrl *rx_ctrl;
	u32 curr_mask;
	int i, j;

	if (!(bnad->cfg_flags & BNAD_CF_MSIX)) {
		bna_intx_disable(&bnad->bna, curr_mask);
		bnad_isr(bnad->pcidev->irq, netdev);
		bna_intx_enable(&bnad->bna, curr_mask);
	} else {
R
Rasesh Mody 已提交
3392 3393 3394 3395 3396 3397
		/*
		 * Tx processing may happen in sending context, so no need
		 * to explicitly process completions here
		 */

		/* Rx processing */
3398 3399 3400 3401 3402 3403
		for (i = 0; i < bnad->num_rx; i++) {
			rx_info = &bnad->rx_info[i];
			if (!rx_info->rx)
				continue;
			for (j = 0; j < bnad->num_rxp_per_rx; j++) {
				rx_ctrl = &rx_info->rx_ctrl[j];
R
Rasesh Mody 已提交
3404
				if (rx_ctrl->ccb)
3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416
					bnad_netif_rx_schedule_poll(bnad,
							    rx_ctrl->ccb);
			}
		}
	}
}
#endif

static const struct net_device_ops bnad_netdev_ops = {
	.ndo_open		= bnad_open,
	.ndo_stop		= bnad_stop,
	.ndo_start_xmit		= bnad_start_xmit,
E
Eric Dumazet 已提交
3417
	.ndo_get_stats64		= bnad_get_stats64,
3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433
	.ndo_set_rx_mode	= bnad_set_rx_mode,
	.ndo_validate_addr      = eth_validate_addr,
	.ndo_set_mac_address    = bnad_set_mac_address,
	.ndo_change_mtu		= bnad_change_mtu,
	.ndo_vlan_rx_add_vid    = bnad_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid   = bnad_vlan_rx_kill_vid,
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller    = bnad_netpoll
#endif
};

static void
bnad_netdev_init(struct bnad *bnad, bool using_dac)
{
	struct net_device *netdev = bnad->netdev;

3434 3435
	netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
		NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3436
		NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_TX;
3437

3438 3439 3440
	netdev->vlan_features = NETIF_F_SG | NETIF_F_HIGHDMA |
		NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
		NETIF_F_TSO | NETIF_F_TSO6;
3441

3442
	netdev->features |= netdev->hw_features |
3443
		NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_FILTER;
3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457

	if (using_dac)
		netdev->features |= NETIF_F_HIGHDMA;

	netdev->mem_start = bnad->mmio_start;
	netdev->mem_end = bnad->mmio_start + bnad->mmio_len - 1;

	netdev->netdev_ops = &bnad_netdev_ops;
	bnad_set_ethtool_ops(netdev);
}

/*
 * 1. Initialize the bnad structure
 * 2. Setup netdev pointer in pci_dev
J
Jing Huang 已提交
3458 3459
 * 3. Initialize no. of TxQ & CQs & MSIX vectors
 * 4. Initialize work queue.
3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500
 */
static int
bnad_init(struct bnad *bnad,
	  struct pci_dev *pdev, struct net_device *netdev)
{
	unsigned long flags;

	SET_NETDEV_DEV(netdev, &pdev->dev);
	pci_set_drvdata(pdev, netdev);

	bnad->netdev = netdev;
	bnad->pcidev = pdev;
	bnad->mmio_start = pci_resource_start(pdev, 0);
	bnad->mmio_len = pci_resource_len(pdev, 0);
	bnad->bar0 = ioremap_nocache(bnad->mmio_start, bnad->mmio_len);
	if (!bnad->bar0) {
		dev_err(&pdev->dev, "ioremap for bar0 failed\n");
		return -ENOMEM;
	}
	pr_info("bar0 mapped to %p, len %llu\n", bnad->bar0,
	       (unsigned long long) bnad->mmio_len);

	spin_lock_irqsave(&bnad->bna_lock, flags);
	if (!bnad_msix_disable)
		bnad->cfg_flags = BNAD_CF_MSIX;

	bnad->cfg_flags |= BNAD_CF_DIM_ENABLED;

	bnad_q_num_init(bnad);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	bnad->msix_num = (bnad->num_tx * bnad->num_txq_per_tx) +
		(bnad->num_rx * bnad->num_rxp_per_rx) +
			 BNAD_MAILBOX_MSIX_VECTORS;

	bnad->txq_depth = BNAD_TXQ_DEPTH;
	bnad->rxq_depth = BNAD_RXQ_DEPTH;

	bnad->tx_coalescing_timeo = BFI_TX_COALESCING_TIMEO;
	bnad->rx_coalescing_timeo = BFI_RX_COALESCING_TIMEO;

J
Jing Huang 已提交
3501 3502
	sprintf(bnad->wq_name, "%s_wq_%d", BNAD_NAME, bnad->id);
	bnad->work_q = create_singlethread_workqueue(bnad->wq_name);
3503 3504
	if (!bnad->work_q) {
		iounmap(bnad->bar0);
J
Jing Huang 已提交
3505
		return -ENOMEM;
3506
	}
J
Jing Huang 已提交
3507

3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518
	return 0;
}

/*
 * Must be called after bnad_pci_uninit()
 * so that iounmap() and pci_set_drvdata(NULL)
 * happens only after PCI uninitialization.
 */
static void
bnad_uninit(struct bnad *bnad)
{
J
Jing Huang 已提交
3519 3520 3521 3522 3523 3524
	if (bnad->work_q) {
		flush_workqueue(bnad->work_q);
		destroy_workqueue(bnad->work_q);
		bnad->work_q = NULL;
	}

3525 3526 3527 3528 3529 3530
	if (bnad->bar0)
		iounmap(bnad->bar0);
}

/*
 * Initialize locks
3531
	a) Per ioceth mutes used for serializing configuration
3532 3533 3534 3535 3536 3537 3538 3539
	   changes from OS interface
	b) spin lock used to protect bna state machine
 */
static void
bnad_lock_init(struct bnad *bnad)
{
	spin_lock_init(&bnad->bna_lock);
	mutex_init(&bnad->conf_mutex);
3540
	mutex_init(&bnad_list_mutex);
3541 3542 3543 3544 3545 3546
}

static void
bnad_lock_uninit(struct bnad *bnad)
{
	mutex_destroy(&bnad->conf_mutex);
3547
	mutex_destroy(&bnad_list_mutex);
3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562
}

/* PCI Initialization */
static int
bnad_pci_init(struct bnad *bnad,
	      struct pci_dev *pdev, bool *using_dac)
{
	int err;

	err = pci_enable_device(pdev);
	if (err)
		return err;
	err = pci_request_regions(pdev, BNAD_NAME);
	if (err)
		goto disable_device;
3563
	if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
3564
		*using_dac = true;
3565
	} else {
3566 3567 3568
		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
		if (err)
			goto release_regions;
3569
		*using_dac = false;
3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588
	}
	pci_set_master(pdev);
	return 0;

release_regions:
	pci_release_regions(pdev);
disable_device:
	pci_disable_device(pdev);

	return err;
}

static void
bnad_pci_uninit(struct pci_dev *pdev)
{
	pci_release_regions(pdev);
	pci_disable_device(pdev);
}

B
Bill Pemberton 已提交
3589
static int
3590 3591 3592
bnad_pci_probe(struct pci_dev *pdev,
		const struct pci_device_id *pcidev_id)
{
3593
	bool	using_dac;
R
Rasesh Mody 已提交
3594
	int	err;
3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621
	struct bnad *bnad;
	struct bna *bna;
	struct net_device *netdev;
	struct bfa_pcidev pcidev_info;
	unsigned long flags;

	pr_info("bnad_pci_probe : (0x%p, 0x%p) PCI Func : (%d)\n",
	       pdev, pcidev_id, PCI_FUNC(pdev->devfn));

	mutex_lock(&bnad_fwimg_mutex);
	if (!cna_get_firmware_buf(pdev)) {
		mutex_unlock(&bnad_fwimg_mutex);
		pr_warn("Failed to load Firmware Image!\n");
		return -ENODEV;
	}
	mutex_unlock(&bnad_fwimg_mutex);

	/*
	 * Allocates sizeof(struct net_device + struct bnad)
	 * bnad = netdev->priv
	 */
	netdev = alloc_etherdev(sizeof(struct bnad));
	if (!netdev) {
		err = -ENOMEM;
		return err;
	}
	bnad = netdev_priv(netdev);
3622
	bnad_lock_init(bnad);
3623
	bnad_add_to_list(bnad);
3624 3625

	mutex_lock(&bnad->conf_mutex);
3626 3627
	/*
	 * PCI initialization
R
Rasesh Mody 已提交
3628
	 *	Output : using_dac = 1 for 64 bit DMA
R
Rasesh Mody 已提交
3629
	 *			   = 0 for 32 bit DMA
3630
	 */
3631
	using_dac = false;
3632 3633
	err = bnad_pci_init(bnad, pdev, &using_dac);
	if (err)
3634
		goto unlock_mutex;
3635 3636 3637 3638 3639 3640 3641 3642

	/*
	 * Initialize bnad structure
	 * Setup relation between pci_dev & netdev
	 */
	err = bnad_init(bnad, pdev, netdev);
	if (err)
		goto pci_uninit;
3643

3644 3645 3646
	/* Initialize netdev structure, set up ethtool ops */
	bnad_netdev_init(bnad, using_dac);

3647 3648 3649
	/* Set link to down state */
	netif_carrier_off(netdev);

K
Krishna Gudipati 已提交
3650 3651 3652 3653
	/* Setup the debugfs node for this bfad */
	if (bna_debugfs_enable)
		bnad_debugfs_init(bnad);

3654
	/* Get resource requirement form bna */
3655
	spin_lock_irqsave(&bnad->bna_lock, flags);
3656
	bna_res_req(&bnad->res_info[0]);
3657
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
3658 3659

	/* Allocate resources from bna */
3660
	err = bnad_res_alloc(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
3661
	if (err)
3662
		goto drv_uninit;
3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677

	bna = &bnad->bna;

	/* Setup pcidev_info for bna_init() */
	pcidev_info.pci_slot = PCI_SLOT(bnad->pcidev->devfn);
	pcidev_info.pci_func = PCI_FUNC(bnad->pcidev->devfn);
	pcidev_info.device_id = bnad->pcidev->device;
	pcidev_info.pci_bar_kva = bnad->bar0;

	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_init(bna, bnad, &pcidev_info, &bnad->res_info[0]);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	bnad->stats.bna_stats = &bna->stats;

3678 3679 3680 3681 3682
	bnad_enable_msix(bnad);
	err = bnad_mbox_irq_alloc(bnad);
	if (err)
		goto res_free;

3683
	/* Set up timers */
3684
	setup_timer(&bnad->bna.ioceth.ioc.ioc_timer, bnad_ioc_timeout,
3685
				((unsigned long)bnad));
3686
	setup_timer(&bnad->bna.ioceth.ioc.hb_timer, bnad_ioc_hb_check,
3687
				((unsigned long)bnad));
3688
	setup_timer(&bnad->bna.ioceth.ioc.iocpf_timer, bnad_iocpf_timeout,
R
Rasesh Mody 已提交
3689
				((unsigned long)bnad));
3690
	setup_timer(&bnad->bna.ioceth.ioc.sem_timer, bnad_iocpf_sem_timeout,
3691 3692 3693
				((unsigned long)bnad));

	/* Now start the timer before calling IOC */
3694
	mod_timer(&bnad->bna.ioceth.ioc.iocpf_timer,
3695 3696 3697 3698
		  jiffies + msecs_to_jiffies(BNA_IOC_TIMER_FREQ));

	/*
	 * Start the chip
3699 3700
	 * If the call back comes with error, we bail out.
	 * This is a catastrophic error.
3701
	 */
3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717
	err = bnad_ioceth_enable(bnad);
	if (err) {
		pr_err("BNA: Initialization failed err=%d\n",
		       err);
		goto probe_success;
	}

	spin_lock_irqsave(&bnad->bna_lock, flags);
	if (bna_num_txq_set(bna, BNAD_NUM_TXQ + 1) ||
		bna_num_rxp_set(bna, BNAD_NUM_RXP + 1)) {
		bnad_q_num_adjust(bnad, bna_attr(bna)->num_txq - 1,
			bna_attr(bna)->num_rxp - 1);
		if (bna_num_txq_set(bna, BNAD_NUM_TXQ + 1) ||
			bna_num_rxp_set(bna, BNAD_NUM_RXP + 1))
			err = -EIO;
	}
3718 3719 3720 3721 3722
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
	if (err)
		goto disable_ioceth;

	spin_lock_irqsave(&bnad->bna_lock, flags);
3723 3724 3725 3726
	bna_mod_res_req(&bnad->bna, &bnad->mod_res_info[0]);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	err = bnad_res_alloc(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
R
Rasesh Mody 已提交
3727 3728
	if (err) {
		err = -EIO;
3729
		goto disable_ioceth;
R
Rasesh Mody 已提交
3730
	}
3731 3732 3733 3734

	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_mod_init(&bnad->bna, &bnad->mod_res_info[0]);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
3735 3736 3737

	/* Get the burnt-in mac */
	spin_lock_irqsave(&bnad->bna_lock, flags);
3738
	bna_enet_perm_mac_get(&bna->enet, &bnad->perm_addr);
3739 3740 3741
	bnad_set_netdev_perm_addr(bnad);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

R
Rasesh Mody 已提交
3742 3743
	mutex_unlock(&bnad->conf_mutex);

3744 3745 3746 3747
	/* Finally, reguister with net_device layer */
	err = register_netdev(netdev);
	if (err) {
		pr_err("BNA : Registering with netdev failed\n");
3748
		goto probe_uninit;
3749
	}
3750
	set_bit(BNAD_RF_NETDEV_REGISTERED, &bnad->run_flags);
3751

R
Rasesh Mody 已提交
3752 3753
	return 0;

3754 3755
probe_success:
	mutex_unlock(&bnad->conf_mutex);
3756 3757
	return 0;

3758
probe_uninit:
R
Rasesh Mody 已提交
3759
	mutex_lock(&bnad->conf_mutex);
3760 3761 3762 3763 3764 3765
	bnad_res_free(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
disable_ioceth:
	bnad_ioceth_disable(bnad);
	del_timer_sync(&bnad->bna.ioceth.ioc.ioc_timer);
	del_timer_sync(&bnad->bna.ioceth.ioc.sem_timer);
	del_timer_sync(&bnad->bna.ioceth.ioc.hb_timer);
3766 3767 3768
	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_uninit(bna);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
3769
	bnad_mbox_irq_free(bnad);
3770
	bnad_disable_msix(bnad);
3771 3772 3773
res_free:
	bnad_res_free(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
drv_uninit:
K
Krishna Gudipati 已提交
3774 3775 3776
	/* Remove the debugfs node for this bnad */
	kfree(bnad->regdata);
	bnad_debugfs_uninit(bnad);
3777
	bnad_uninit(bnad);
3778 3779
pci_uninit:
	bnad_pci_uninit(pdev);
3780
unlock_mutex:
3781
	mutex_unlock(&bnad->conf_mutex);
3782
	bnad_remove_from_list(bnad);
3783 3784 3785 3786 3787
	bnad_lock_uninit(bnad);
	free_netdev(netdev);
	return err;
}

B
Bill Pemberton 已提交
3788
static void
3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802
bnad_pci_remove(struct pci_dev *pdev)
{
	struct net_device *netdev = pci_get_drvdata(pdev);
	struct bnad *bnad;
	struct bna *bna;
	unsigned long flags;

	if (!netdev)
		return;

	pr_info("%s bnad_pci_remove\n", netdev->name);
	bnad = netdev_priv(netdev);
	bna = &bnad->bna;

3803 3804
	if (test_and_clear_bit(BNAD_RF_NETDEV_REGISTERED, &bnad->run_flags))
		unregister_netdev(netdev);
3805 3806

	mutex_lock(&bnad->conf_mutex);
3807 3808 3809 3810
	bnad_ioceth_disable(bnad);
	del_timer_sync(&bnad->bna.ioceth.ioc.ioc_timer);
	del_timer_sync(&bnad->bna.ioceth.ioc.sem_timer);
	del_timer_sync(&bnad->bna.ioceth.ioc.hb_timer);
3811 3812 3813 3814
	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_uninit(bna);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

3815 3816 3817
	bnad_res_free(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
	bnad_res_free(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
	bnad_mbox_irq_free(bnad);
3818 3819
	bnad_disable_msix(bnad);
	bnad_pci_uninit(pdev);
3820
	mutex_unlock(&bnad->conf_mutex);
3821
	bnad_remove_from_list(bnad);
3822
	bnad_lock_uninit(bnad);
K
Krishna Gudipati 已提交
3823 3824 3825
	/* Remove the debugfs node for this bnad */
	kfree(bnad->regdata);
	bnad_debugfs_uninit(bnad);
3826 3827 3828 3829
	bnad_uninit(bnad);
	free_netdev(netdev);
}

R
Rasesh Mody 已提交
3830
static DEFINE_PCI_DEVICE_TABLE(bnad_pci_id_table) = {
3831 3832 3833 3834 3835
	{
		PCI_DEVICE(PCI_VENDOR_ID_BROCADE,
			PCI_DEVICE_ID_BROCADE_CT),
		.class = PCI_CLASS_NETWORK_ETHERNET << 8,
		.class_mask =  0xffff00
R
Rasesh Mody 已提交
3836 3837 3838 3839 3840 3841 3842 3843
	},
	{
		PCI_DEVICE(PCI_VENDOR_ID_BROCADE,
			BFA_PCI_DEVICE_ID_CT2),
		.class = PCI_CLASS_NETWORK_ETHERNET << 8,
		.class_mask =  0xffff00
	},
	{0,  },
3844 3845 3846 3847 3848 3849 3850 3851
};

MODULE_DEVICE_TABLE(pci, bnad_pci_id_table);

static struct pci_driver bnad_pci_driver = {
	.name = BNAD_NAME,
	.id_table = bnad_pci_id_table,
	.probe = bnad_pci_probe,
B
Bill Pemberton 已提交
3852
	.remove = bnad_pci_remove,
3853 3854 3855 3856 3857 3858 3859
};

static int __init
bnad_module_init(void)
{
	int err;

R
Rasesh Mody 已提交
3860 3861
	pr_info("Brocade 10G Ethernet driver - version: %s\n",
			BNAD_VERSION);
3862

3863
	bfa_nw_ioc_auto_recover(bnad_ioc_auto_recover);
3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878

	err = pci_register_driver(&bnad_pci_driver);
	if (err < 0) {
		pr_err("bna : PCI registration failed in module init "
		       "(%d)\n", err);
		return err;
	}

	return 0;
}

static void __exit
bnad_module_exit(void)
{
	pci_unregister_driver(&bnad_pci_driver);
3879
	release_firmware(bfi_fw);
3880 3881 3882 3883 3884 3885 3886 3887 3888 3889
}

module_init(bnad_module_init);
module_exit(bnad_module_exit);

MODULE_AUTHOR("Brocade");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Brocade 10G PCIe Ethernet driver");
MODULE_VERSION(BNAD_VERSION);
MODULE_FIRMWARE(CNA_FW_FILE_CT);
3890
MODULE_FIRMWARE(CNA_FW_FILE_CT2);