bnad.c 85.5 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 55
/*
 * Global variables
 */
u32 bnad_rxqs_per_cq = 2;
56 57 58
u32 bna_id;
struct mutex bnad_list_mutex;
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 66 67 68 69

/*
 * Local MACROS
 */
#define BNAD_TX_UNMAPQ_DEPTH (bnad->txq_depth * 2)

#define BNAD_RX_UNMAPQ_DEPTH (bnad->rxq_depth)

#define BNAD_GET_MBOX_IRQ(_bnad)				\
	(((_bnad)->cfg_flags & BNAD_CF_MSIX) ?			\
70
	 ((_bnad)->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector) : \
71 72 73 74 75 76 77 78 79 80 81 82
	 ((_bnad)->pcidev->irq))

#define BNAD_FILL_UNMAPQ_MEM_REQ(_res_info, _num, _depth)	\
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);		\
	(_res_info)->res_u.mem_info.len =			\
	sizeof(struct bnad_unmap_q) +				\
	(sizeof(struct bnad_skb_unmap) * ((_depth) - 1));	\
} while (0)

R
Rasesh Mody 已提交
83 84
#define BNAD_TXRX_SYNC_MDELAY	250	/* 250 msecs */

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
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);
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
/*
 * Reinitialize completions in CQ, once Rx is taken down
 */
static void
bnad_cq_cmpl_init(struct bnad *bnad, struct bna_ccb *ccb)
{
	struct bna_cq_entry *cmpl, *next_cmpl;
	unsigned int wi_range, wis = 0, ccb_prod = 0;
	int i;

	BNA_CQ_QPGE_PTR_GET(ccb_prod, ccb->sw_qpt, cmpl,
			    wi_range);

	for (i = 0; i < ccb->q_depth; i++) {
		wis++;
		if (likely(--wi_range))
			next_cmpl = cmpl + 1;
		else {
			BNA_QE_INDX_ADD(ccb_prod, wis, ccb->q_depth);
			wis = 0;
			BNA_CQ_QPGE_PTR_GET(ccb_prod, ccb->sw_qpt,
						next_cmpl, wi_range);
		}
		cmpl->valid = 0;
		cmpl = next_cmpl;
	}
}

R
Rasesh Mody 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143
static u32
bnad_pci_unmap_skb(struct device *pdev, struct bnad_skb_unmap *array,
	u32 index, u32 depth, struct sk_buff *skb, u32 frag)
{
	int j;
	array[index].skb = NULL;

	dma_unmap_single(pdev, dma_unmap_addr(&array[index], dma_addr),
			skb_headlen(skb), DMA_TO_DEVICE);
	dma_unmap_addr_set(&array[index], dma_addr, 0);
	BNA_QE_INDX_ADD(index, 1, depth);

	for (j = 0; j < frag; j++) {
		dma_unmap_page(pdev, dma_unmap_addr(&array[index], dma_addr),
E
Eric Dumazet 已提交
144
			  skb_frag_size(&skb_shinfo(skb)->frags[j]), DMA_TO_DEVICE);
R
Rasesh Mody 已提交
145 146 147 148 149 150 151
		dma_unmap_addr_set(&array[index], dma_addr, 0);
		BNA_QE_INDX_ADD(index, 1, depth);
	}

	return index;
}

152 153 154 155 156 157 158 159 160
/*
 * Frees all pending Tx Bufs
 * At this point no activity is expected on the Q,
 * so DMA unmap & freeing is fine.
 */
static void
bnad_free_all_txbufs(struct bnad *bnad,
		 struct bna_tcb *tcb)
{
R
Rasesh Mody 已提交
161
	u32		unmap_cons;
162 163
	struct bnad_unmap_q *unmap_q = tcb->unmap_q;
	struct bnad_skb_unmap *unmap_array;
R
Rasesh Mody 已提交
164
	struct sk_buff		*skb = NULL;
R
Rasesh Mody 已提交
165
	int			q;
166 167 168

	unmap_array = unmap_q->unmap_array;

R
Rasesh Mody 已提交
169 170 171
	for (q = 0; q < unmap_q->q_depth; q++) {
		skb = unmap_array[q].skb;
		if (!skb)
172
			continue;
R
Rasesh Mody 已提交
173 174 175 176 177 178

		unmap_cons = q;
		unmap_cons = bnad_pci_unmap_skb(&bnad->pcidev->dev, unmap_array,
				unmap_cons, unmap_q->q_depth, skb,
				skb_shinfo(skb)->nr_frags);

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
		dev_kfree_skb_any(skb);
	}
}

/* Data Path Handlers */

/*
 * bnad_free_txbufs : Frees the Tx bufs on Tx completion
 * Can be called in a) Interrupt context
 *		    b) Sending context
 *		    c) Tasklet context
 */
static u32
bnad_free_txbufs(struct bnad *bnad,
		 struct bna_tcb *tcb)
{
R
Rasesh Mody 已提交
195 196
	u32		unmap_cons, sent_packets = 0, sent_bytes = 0;
	u16		wis, updated_hw_cons;
197 198
	struct bnad_unmap_q *unmap_q = tcb->unmap_q;
	struct bnad_skb_unmap *unmap_array;
R
Rasesh Mody 已提交
199
	struct sk_buff		*skb;
200 201 202 203

	/*
	 * Just return if TX is stopped. This check is useful
	 * when bnad_free_txbufs() runs out of a tasklet scheduled
R
Rasesh Mody 已提交
204
	 * before bnad_cb_tx_cleanup() cleared BNAD_TXQ_TX_STARTED bit
205 206 207
	 * but this routine runs actually after the cleanup has been
	 * executed.
	 */
R
Rasesh Mody 已提交
208
	if (!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
		return 0;

	updated_hw_cons = *(tcb->hw_consumer_index);

	wis = BNA_Q_INDEX_CHANGE(tcb->consumer_index,
				  updated_hw_cons, tcb->q_depth);

	BUG_ON(!(wis <= BNA_QE_IN_USE_CNT(tcb, tcb->q_depth)));

	unmap_array = unmap_q->unmap_array;
	unmap_cons = unmap_q->consumer_index;

	prefetch(&unmap_array[unmap_cons + 1]);
	while (wis) {
		skb = unmap_array[unmap_cons].skb;

		sent_packets++;
		sent_bytes += skb->len;
		wis -= BNA_TXQ_WI_NEEDED(1 + skb_shinfo(skb)->nr_frags);

R
Rasesh Mody 已提交
229 230 231
		unmap_cons = bnad_pci_unmap_skb(&bnad->pcidev->dev, unmap_array,
				unmap_cons, unmap_q->q_depth, skb,
				skb_shinfo(skb)->nr_frags);
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

		dev_kfree_skb_any(skb);
	}

	/* Update consumer pointers. */
	tcb->consumer_index = updated_hw_cons;
	unmap_q->consumer_index = unmap_cons;

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

	return sent_packets;
}

/* Tx Free Tasklet function */
/* Frees for all the tcb's in all the Tx's */
/*
 * Scheduled from sending context, so that
 * the fat Tx lock is not held for too long
 * in the sending context.
 */
static void
bnad_tx_free_tasklet(unsigned long bnad_ptr)
{
	struct bnad *bnad = (struct bnad *)bnad_ptr;
	struct bna_tcb *tcb;
R
Rasesh Mody 已提交
258
	u32		acked = 0;
259 260 261 262 263 264 265 266 267 268 269 270
	int			i, j;

	for (i = 0; i < bnad->num_tx; i++) {
		for (j = 0; j < bnad->num_txq_per_tx; j++) {
			tcb = bnad->tx_info[i].tcb[j];
			if (!tcb)
				continue;
			if (((u16) (*tcb->hw_consumer_index) !=
				tcb->consumer_index) &&
				(!test_and_set_bit(BNAD_TXQ_FREE_SENT,
						  &tcb->flags))) {
				acked = bnad_free_txbufs(bnad, tcb);
R
Rasesh Mody 已提交
271 272 273
				if (likely(test_bit(BNAD_TXQ_TX_STARTED,
					&tcb->flags)))
					bna_ib_ack(tcb->i_dbell, acked);
274 275 276
				smp_mb__before_clear_bit();
				clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
			}
R
Rasesh Mody 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290
			if (unlikely(!test_bit(BNAD_TXQ_TX_STARTED,
						&tcb->flags)))
				continue;
			if (netif_queue_stopped(bnad->netdev)) {
				if (acked && netif_carrier_ok(bnad->netdev) &&
					BNA_QE_FREE_CNT(tcb, tcb->q_depth) >=
						BNAD_NETIF_WAKE_THRESHOLD) {
					netif_wake_queue(bnad->netdev);
					/* TODO */
					/* Counters for individual TxQs? */
					BNAD_UPDATE_CTR(bnad,
						netif_queue_wakeup);
				}
			}
291 292 293 294 295 296 297 298
		}
	}
}

static u32
bnad_tx(struct bnad *bnad, struct bna_tcb *tcb)
{
	struct net_device *netdev = bnad->netdev;
R
Rasesh Mody 已提交
299
	u32 sent = 0;
300 301 302 303 304 305 306 307 308 309

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

	sent = bnad_free_txbufs(bnad, tcb);
	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 已提交
310 311 312 313
			if (test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)) {
				netif_wake_queue(netdev);
				BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
			}
314
		}
R
Rasesh Mody 已提交
315 316 317
	}

	if (likely(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
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 345 346 347 348 349 350
		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;

	bnad_tx(bnad, tcb);

	return IRQ_HANDLED;
}

static void
bnad_reset_rcb(struct bnad *bnad, struct bna_rcb *rcb)
{
	struct bnad_unmap_q *unmap_q = rcb->unmap_q;

	rcb->producer_index = 0;
	rcb->consumer_index = 0;

	unmap_q->producer_index = 0;
	unmap_q->consumer_index = 0;
}

static void
R
Rasesh Mody 已提交
351
bnad_free_all_rxbufs(struct bnad *bnad, struct bna_rcb *rcb)
352 353
{
	struct bnad_unmap_q *unmap_q;
I
Ivan Vecera 已提交
354
	struct bnad_skb_unmap *unmap_array;
355
	struct sk_buff *skb;
R
Rasesh Mody 已提交
356
	int unmap_cons;
357 358

	unmap_q = rcb->unmap_q;
I
Ivan Vecera 已提交
359
	unmap_array = unmap_q->unmap_array;
R
Rasesh Mody 已提交
360
	for (unmap_cons = 0; unmap_cons < unmap_q->q_depth; unmap_cons++) {
I
Ivan Vecera 已提交
361
		skb = unmap_array[unmap_cons].skb;
R
Rasesh Mody 已提交
362 363
		if (!skb)
			continue;
I
Ivan Vecera 已提交
364 365 366 367 368 369
		unmap_array[unmap_cons].skb = NULL;
		dma_unmap_single(&bnad->pcidev->dev,
				 dma_unmap_addr(&unmap_array[unmap_cons],
						dma_addr),
				 rcb->rxq->buffer_size,
				 DMA_FROM_DEVICE);
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
		dev_kfree_skb(skb);
	}
	bnad_reset_rcb(bnad, rcb);
}

static void
bnad_alloc_n_post_rxbufs(struct bnad *bnad, struct bna_rcb *rcb)
{
	u16 to_alloc, alloced, unmap_prod, wi_range;
	struct bnad_unmap_q *unmap_q = rcb->unmap_q;
	struct bnad_skb_unmap *unmap_array;
	struct bna_rxq_entry *rxent;
	struct sk_buff *skb;
	dma_addr_t dma_addr;

	alloced = 0;
	to_alloc =
		BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth);

	unmap_array = unmap_q->unmap_array;
	unmap_prod = unmap_q->producer_index;

	BNA_RXQ_QPGE_PTR_GET(unmap_prod, rcb->sw_qpt, rxent, wi_range);

	while (to_alloc--) {
R
Rasesh Mody 已提交
395
		if (!wi_range)
396 397
			BNA_RXQ_QPGE_PTR_GET(unmap_prod, rcb->sw_qpt, rxent,
					     wi_range);
398 399
		skb = netdev_alloc_skb_ip_align(bnad->netdev,
						rcb->rxq->buffer_size);
400 401
		if (unlikely(!skb)) {
			BNAD_UPDATE_CTR(bnad, rxbuf_alloc_failed);
402
			rcb->rxq->rxbuf_alloc_failed++;
403 404 405
			goto finishing;
		}
		unmap_array[unmap_prod].skb = skb;
I
Ivan Vecera 已提交
406 407 408 409
		dma_addr = dma_map_single(&bnad->pcidev->dev, skb->data,
					  rcb->rxq->buffer_size,
					  DMA_FROM_DEVICE);
		dma_unmap_addr_set(&unmap_array[unmap_prod], dma_addr,
410 411 412 413 414 415 416 417 418 419 420 421 422 423
				   dma_addr);
		BNA_SET_DMA_ADDR(dma_addr, &rxent->host_addr);
		BNA_QE_INDX_ADD(unmap_prod, 1, unmap_q->q_depth);

		rxent++;
		wi_range--;
		alloced++;
	}

finishing:
	if (likely(alloced)) {
		unmap_q->producer_index = unmap_prod;
		rcb->producer_index = unmap_prod;
		smp_mb();
R
Rasesh Mody 已提交
424
		if (likely(test_bit(BNAD_RXQ_POST_OK, &rcb->flags)))
R
Rasesh Mody 已提交
425
			bna_rxq_prod_indx_doorbell(rcb);
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
	}
}

static inline void
bnad_refill_rxq(struct bnad *bnad, struct bna_rcb *rcb)
{
	struct bnad_unmap_q *unmap_q = rcb->unmap_q;

	if (!test_and_set_bit(BNAD_RXQ_REFILL, &rcb->flags)) {
		if (BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth)
			 >> BNAD_RXQ_REFILL_THRESHOLD_SHIFT)
			bnad_alloc_n_post_rxbufs(bnad, rcb);
		smp_mb__before_clear_bit();
		clear_bit(BNAD_RXQ_REFILL, &rcb->flags);
	}
}

static u32
bnad_poll_cq(struct bnad *bnad, struct bna_ccb *ccb, int budget)
{
	struct bna_cq_entry *cmpl, *next_cmpl;
	struct bna_rcb *rcb = NULL;
	unsigned int wi_range, packets = 0, wis = 0;
	struct bnad_unmap_q *unmap_q;
I
Ivan Vecera 已提交
450
	struct bnad_skb_unmap *unmap_array;
451
	struct sk_buff *skb;
I
Ivan Vecera 已提交
452
	u32 flags, unmap_cons;
453
	struct bna_pkt_rate *pkt_rt = &ccb->pkt_rate;
454 455 456
	struct bnad_rx_ctrl *rx_ctrl = (struct bnad_rx_ctrl *)(ccb->ctrl);

	set_bit(BNAD_FP_IN_RX_PATH, &rx_ctrl->flags);
457

458 459
	if (!test_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags)) {
		clear_bit(BNAD_FP_IN_RX_PATH, &rx_ctrl->flags);
R
Rasesh Mody 已提交
460
		return 0;
461
	}
R
Rasesh Mody 已提交
462

463 464 465 466 467 468 469 470
	prefetch(bnad->netdev);
	BNA_CQ_QPGE_PTR_GET(ccb->producer_index, ccb->sw_qpt, cmpl,
			    wi_range);
	BUG_ON(!(wi_range <= ccb->q_depth));
	while (cmpl->valid && packets < budget) {
		packets++;
		BNA_UPDATE_PKT_CNT(pkt_rt, ntohs(cmpl->length));

471
		if (bna_is_small_rxq(cmpl->rxq_id))
472
			rcb = ccb->rcb[1];
473 474
		else
			rcb = ccb->rcb[0];
475 476

		unmap_q = rcb->unmap_q;
I
Ivan Vecera 已提交
477 478
		unmap_array = unmap_q->unmap_array;
		unmap_cons = unmap_q->consumer_index;
479

I
Ivan Vecera 已提交
480
		skb = unmap_array[unmap_cons].skb;
481
		BUG_ON(!(skb));
I
Ivan Vecera 已提交
482 483 484
		unmap_array[unmap_cons].skb = NULL;
		dma_unmap_single(&bnad->pcidev->dev,
				 dma_unmap_addr(&unmap_array[unmap_cons],
485
						dma_addr),
I
Ivan Vecera 已提交
486 487
				 rcb->rxq->buffer_size,
				 DMA_FROM_DEVICE);
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
		BNA_QE_INDX_ADD(unmap_q->consumer_index, 1, unmap_q->q_depth);

		/* Should be more efficient ? Performance ? */
		BNA_QE_INDX_ADD(rcb->consumer_index, 1, rcb->q_depth);

		wis++;
		if (likely(--wi_range))
			next_cmpl = cmpl + 1;
		else {
			BNA_QE_INDX_ADD(ccb->producer_index, wis, ccb->q_depth);
			wis = 0;
			BNA_CQ_QPGE_PTR_GET(ccb->producer_index, ccb->sw_qpt,
						next_cmpl, wi_range);
			BUG_ON(!(wi_range <= ccb->q_depth));
		}
		prefetch(next_cmpl);

		flags = ntohl(cmpl->flags);
		if (unlikely
		    (flags &
		     (BNA_CQ_EF_MAC_ERROR | BNA_CQ_EF_FCS_ERROR |
		      BNA_CQ_EF_TOO_LONG))) {
			dev_kfree_skb_any(skb);
			rcb->rxq->rx_packets_with_error++;
			goto next;
		}

		skb_put(skb, ntohs(cmpl->length));
		if (likely
517
		    ((bnad->netdev->features & NETIF_F_RXCSUM) &&
518 519 520 521 522 523 524
		     (((flags & BNA_CQ_EF_IPV4) &&
		      (flags & BNA_CQ_EF_L3_CKSUM_OK)) ||
		      (flags & BNA_CQ_EF_IPV6)) &&
		      (flags & (BNA_CQ_EF_TCP | BNA_CQ_EF_UDP)) &&
		      (flags & BNA_CQ_EF_L4_CKSUM_OK)))
			skb->ip_summed = CHECKSUM_UNNECESSARY;
		else
525
			skb_checksum_none_assert(skb);
526 527 528 529 530

		rcb->rxq->rx_packets++;
		rcb->rxq->rx_bytes += skb->len;
		skb->protocol = eth_type_trans(skb, bnad->netdev);

J
Jiri Pirko 已提交
531 532 533
		if (flags & BNA_CQ_EF_VLAN)
			__vlan_hwaccel_put_tag(skb, ntohs(cmpl->vlan_tag));

534
		if (skb->ip_summed == CHECKSUM_UNNECESSARY)
J
Jiri Pirko 已提交
535
			napi_gro_receive(&rx_ctrl->napi, skb);
536
		else {
J
Jiri Pirko 已提交
537
			netif_receive_skb(skb);
538 539 540 541 542 543 544 545 546
		}

next:
		cmpl->valid = 0;
		cmpl = next_cmpl;
	}

	BNA_QE_INDX_ADD(ccb->producer_index, wis, ccb->q_depth);

547
	if (likely(test_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags)))
R
Rasesh Mody 已提交
548 549
		bna_ib_ack_disable_irq(ccb->i_dbell, packets);

550 551 552
	bnad_refill_rxq(bnad, ccb->rcb[0]);
	if (ccb->rcb[1])
		bnad_refill_rxq(bnad, ccb->rcb[1]);
553

554 555
	clear_bit(BNAD_FP_IN_RX_PATH, &rx_ctrl->flags);

556 557 558 559 560 561 562
	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 已提交
563 564 565 566
	struct napi_struct *napi = &rx_ctrl->napi;

	if (likely(napi_schedule_prep(napi))) {
		__napi_schedule(napi);
R
Rasesh Mody 已提交
567
		rx_ctrl->rx_schedule++;
568 569 570 571 572 573 574 575 576
	}
}

/* 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 已提交
577 578
	if (ccb) {
		((struct bnad_rx_ctrl *)(ccb->ctrl))->rx_intr_ctr++;
579
		bnad_netif_rx_schedule_poll(ccb->bnad, ccb);
R
Rasesh Mody 已提交
580
	}
581 582 583 584 585 586 587 588 589 590 591

	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 已提交
592
	unsigned long flags;
R
Rasesh Mody 已提交
593
	struct bnad *bnad = (struct bnad *)data;
594 595

	spin_lock_irqsave(&bnad->bna_lock, flags);
596 597 598 599
	if (unlikely(test_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags))) {
		spin_unlock_irqrestore(&bnad->bna_lock, flags);
		return IRQ_HANDLED;
	}
600 601 602

	bna_intr_status_get(&bnad->bna, intr_status);

603
	if (BNA_IS_MBOX_ERR_INTR(&bnad->bna, intr_status))
604 605 606 607 608 609 610 611 612 613 614 615 616
		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 已提交
617
	struct bnad *bnad = (struct bnad *)data;
618 619
	struct bnad_rx_info *rx_info;
	struct bnad_rx_ctrl *rx_ctrl;
620
	struct bna_tcb *tcb = NULL;
621

622 623 624
	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 已提交
625
		return IRQ_NONE;
626
	}
627 628

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

630 631
	if (unlikely(!intr_status)) {
		spin_unlock_irqrestore(&bnad->bna_lock, flags);
632
		return IRQ_NONE;
633
	}
634

635
	if (BNA_IS_MBOX_ERR_INTR(&bnad->bna, intr_status))
636
		bna_mbox_handler(&bnad->bna, intr_status);
R
Rasesh Mody 已提交
637

638 639
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

R
Rasesh Mody 已提交
640 641 642
	if (!BNA_IS_INTX_DATA_INTR(intr_status))
		return IRQ_HANDLED;

643
	/* Process data interrupts */
R
Rasesh Mody 已提交
644 645
	/* Tx processing */
	for (i = 0; i < bnad->num_tx; i++) {
646 647 648 649 650
		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))
				bnad_tx(bnad, bnad->tx_info[i].tcb[j]);
		}
R
Rasesh Mody 已提交
651 652
	}
	/* Rx processing */
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
	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 已提交
674
	clear_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
R
Rasesh Mody 已提交
675

676 677 678 679 680 681 682
	BNAD_UPDATE_CTR(bnad, mbox_intr_enabled);
}

/*
 * Called with bnad->bna_lock held b'cos of
 * bnad->cfg_flags access.
 */
R
Rasesh Mody 已提交
683
static void
684 685
bnad_disable_mbox_irq(struct bnad *bnad)
{
R
Rasesh Mody 已提交
686
	set_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
687

R
Rasesh Mody 已提交
688 689
	BNAD_UPDATE_CTR(bnad, mbox_intr_disabled);
}
690

R
Rasesh Mody 已提交
691 692 693 694
static void
bnad_set_netdev_perm_addr(struct bnad *bnad)
{
	struct net_device *netdev = bnad->netdev;
R
Rasesh Mody 已提交
695

R
Rasesh Mody 已提交
696 697 698
	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);
699 700 701 702 703 704
}

/* Control Path Handlers */

/* Callbacks */
void
705
bnad_cb_mbox_intr_enable(struct bnad *bnad)
706 707 708 709 710
{
	bnad_enable_mbox_irq(bnad);
}

void
711
bnad_cb_mbox_intr_disable(struct bnad *bnad)
712 713 714 715 716
{
	bnad_disable_mbox_irq(bnad);
}

void
717 718 719 720 721 722 723 724
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)
725
{
726
	bnad->bnad_completions.ioc_comp_status = BNA_CB_FAIL;
727 728 729 730
	complete(&bnad->bnad_completions.ioc_comp);
}

void
731
bnad_cb_ioceth_disabled(struct bnad *bnad)
732
{
733
	bnad->bnad_completions.ioc_comp_status = BNA_CB_SUCCESS;
734 735 736 737
	complete(&bnad->bnad_completions.ioc_comp);
}

static void
738
bnad_cb_enet_disabled(void *arg)
739 740 741 742
{
	struct bnad *bnad = (struct bnad *)arg;

	netif_carrier_off(bnad->netdev);
743
	complete(&bnad->bnad_completions.enet_comp);
744 745 746
}

void
747
bnad_cb_ethport_link_status(struct bnad *bnad,
748 749
			enum bna_link_status link_status)
{
750
	bool link_up = false;
751 752 753 754

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

	if (link_status == BNA_CEE_UP) {
755 756
		if (!test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags))
			BNAD_UPDATE_CTR(bnad, cee_toggle);
757
		set_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags);
758 759 760
	} else {
		if (test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags))
			BNAD_UPDATE_CTR(bnad, cee_toggle);
761
		clear_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags);
762
	}
763 764 765

	if (link_up) {
		if (!netif_carrier_ok(bnad->netdev)) {
766 767
			uint tx_id, tcb_id;
			printk(KERN_WARNING "bna: %s link up\n",
768 769 770
				bnad->netdev->name);
			netif_carrier_on(bnad->netdev);
			BNAD_UPDATE_CTR(bnad, link_toggle);
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
			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);
					}
				}
804 805 806 807
			}
		}
	} else {
		if (netif_carrier_ok(bnad->netdev)) {
808
			printk(KERN_WARNING "bna: %s link down\n",
809 810 811 812 813 814 815 816
				bnad->netdev->name);
			netif_carrier_off(bnad->netdev);
			BNAD_UPDATE_CTR(bnad, link_toggle);
		}
	}
}

static void
817
bnad_cb_tx_disabled(void *arg, struct bna_tx *tx)
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
{
	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;
	struct bnad_unmap_q *unmap_q = tcb->unmap_q;

	tx_info->tcb[tcb->id] = tcb;
	unmap_q->producer_index = 0;
	unmap_q->consumer_index = 0;
	unmap_q->q_depth = BNAD_TX_UNMAPQ_DEPTH;
}

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;
R
Rasesh Mody 已提交
842 843 844 845 846 847 848 849 850 851 852 853
	struct bnad_unmap_q *unmap_q = tcb->unmap_q;

	while (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags))
		cpu_relax();

	bnad_free_all_txbufs(bnad, tcb);

	unmap_q->producer_index = 0;
	unmap_q->consumer_index = 0;

	smp_mb__before_clear_bit();
	clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
854 855 856 857 858 859 860 861 862 863 864 865 866 867

	tx_info->tcb[tcb->id] = NULL;
}

static void
bnad_cb_rcb_setup(struct bnad *bnad, struct bna_rcb *rcb)
{
	struct bnad_unmap_q *unmap_q = rcb->unmap_q;

	unmap_q->producer_index = 0;
	unmap_q->consumer_index = 0;
	unmap_q->q_depth = BNAD_RX_UNMAPQ_DEPTH;
}

R
Rasesh Mody 已提交
868 869 870 871 872 873
static void
bnad_cb_rcb_destroy(struct bnad *bnad, struct bna_rcb *rcb)
{
	bnad_free_all_rxbufs(bnad, rcb);
}

874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
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
894
bnad_cb_tx_stall(struct bnad *bnad, struct bna_tx *tx)
895 896
{
	struct bnad_tx_info *tx_info =
897 898 899 900
			(struct bnad_tx_info *)tx->priv;
	struct bna_tcb *tcb;
	u32 txq_id;
	int i;
901

902 903 904 905 906 907 908 909 910 911
	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);
	}
912 913 914
}

static void
915
bnad_cb_tx_resume(struct bnad *bnad, struct bna_tx *tx)
916
{
917 918 919 920 921
	struct bnad_tx_info *tx_info = (struct bnad_tx_info *)tx->priv;
	struct bna_tcb *tcb;
	struct bnad_unmap_q *unmap_q;
	u32 txq_id;
	int i;
922

923 924 925 926 927
	for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
		tcb = tx_info->tcb[i];
		if (!tcb)
			continue;
		txq_id = tcb->id;
928

929
		unmap_q = tcb->unmap_q;
930

931 932
		if (test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))
			continue;
933

934 935
		while (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags))
			cpu_relax();
936

937
		bnad_free_all_txbufs(bnad, tcb);
938

939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
		unmap_q->producer_index = 0;
		unmap_q->consumer_index = 0;

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

		set_bit(BNAD_TXQ_TX_STARTED, &tcb->flags);

		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 已提交
954 955

	/*
956
	 * Workaround for first ioceth enable failure & we
R
Rasesh Mody 已提交
957 958 959 960
	 * get a 0 MAC address. We try to get the MAC address
	 * again here.
	 */
	if (is_zero_ether_addr(&bnad->perm_addr.mac[0])) {
961
		bna_enet_perm_mac_get(&bnad->bna.enet, &bnad->perm_addr);
R
Rasesh Mody 已提交
962 963 964 965 966
		bnad_set_netdev_perm_addr(bnad);
	}
}

static void
967
bnad_cb_tx_cleanup(struct bnad *bnad, struct bna_tx *tx)
R
Rasesh Mody 已提交
968
{
969 970 971 972 973 974 975 976 977 978 979 980
	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;
	}

	mdelay(BNAD_TXRX_SYNC_MDELAY);
	bna_tx_cleanup_complete(tx);
981 982
}

R
Rasesh Mody 已提交
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
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);
	}
}

1004
static void
1005
bnad_cb_rx_cleanup(struct bnad *bnad, struct bna_rx *rx)
1006
{
1007 1008 1009 1010 1011 1012 1013
	struct bnad_rx_info *rx_info = (struct bnad_rx_info *)rx->priv;
	struct bna_ccb *ccb;
	struct bnad_rx_ctrl *rx_ctrl;
	int i;

	mdelay(BNAD_TXRX_SYNC_MDELAY);

1014
	for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
1015 1016 1017 1018 1019 1020 1021 1022 1023
		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);
1024

1025 1026 1027
		while (test_bit(BNAD_FP_IN_RX_PATH, &rx_ctrl->flags))
			cpu_relax();
	}
R
Rasesh Mody 已提交
1028

1029
	bna_rx_cleanup_complete(rx);
1030 1031 1032
}

static void
1033
bnad_cb_rx_post(struct bnad *bnad, struct bna_rx *rx)
1034
{
1035 1036 1037 1038 1039 1040 1041
	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;
	struct bnad_unmap_q *unmap_q;
	int i;
	int j;
R
Rasesh Mody 已提交
1042

1043
	for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
1044 1045 1046 1047
		rx_ctrl = &rx_info->rx_ctrl[i];
		ccb = rx_ctrl->ccb;
		if (!ccb)
			continue;
R
Rasesh Mody 已提交
1048

1049
		bnad_cq_cmpl_init(bnad, ccb);
1050

1051 1052 1053 1054 1055 1056 1057
		for (j = 0; j < BNAD_MAX_RXQ_PER_RXP; j++) {
			rcb = ccb->rcb[j];
			if (!rcb)
				continue;
			bnad_free_all_rxbufs(bnad, rcb);

			set_bit(BNAD_RXQ_STARTED, &rcb->flags);
R
Rasesh Mody 已提交
1058
			set_bit(BNAD_RXQ_POST_OK, &rcb->flags);
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
			unmap_q = rcb->unmap_q;

			/* Now allocate & post buffers for this RCB */
			/* !!Allocation in callback context */
			if (!test_and_set_bit(BNAD_RXQ_REFILL, &rcb->flags)) {
				if (BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth)
					>> BNAD_RXQ_REFILL_THRESHOLD_SHIFT)
					bnad_alloc_n_post_rxbufs(bnad, rcb);
					smp_mb__before_clear_bit();
				clear_bit(BNAD_RXQ_REFILL, &rcb->flags);
			}
		}
1071 1072 1073 1074
	}
}

static void
1075
bnad_cb_rx_disabled(void *arg, struct bna_rx *rx)
1076 1077 1078 1079 1080 1081 1082
{
	struct bnad *bnad = (struct bnad *)arg;

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

static void
1083
bnad_cb_rx_mcast_add(struct bnad *bnad, struct bna_rx *rx)
1084
{
1085
	bnad->bnad_completions.mcast_comp_status = BNA_CB_SUCCESS;
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
	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));
}

1104 1105 1106 1107 1108 1109 1110
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);
}

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
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);
}

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
/* 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 已提交
1138 1139 1140
				dma_free_coherent(&bnad->pcidev->dev,
						  mem_info->mdl[i].len,
						  mem_info->mdl[i].kva, dma_pa);
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
			} 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 已提交
1170 1171 1172
				dma_alloc_coherent(&bnad->pcidev->dev,
						mem_info->len, &dma_pa,
						GFP_KERNEL);
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198

			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
1199
bnad_mbox_irq_free(struct bnad *bnad)
1200 1201 1202 1203 1204 1205
{
	int irq;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
	bnad_disable_mbox_irq(bnad);
R
Rasesh Mody 已提交
1206
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
1207 1208

	irq = BNAD_GET_MBOX_IRQ(bnad);
R
Rasesh Mody 已提交
1209
	free_irq(irq, bnad);
1210 1211 1212 1213 1214 1215 1216 1217
}

/*
 * Allocates IRQ for Mailbox, but keep it disabled
 * This will be enabled once we get the mbox enable callback
 * from bna
 */
static int
1218
bnad_mbox_irq_alloc(struct bnad *bnad)
1219
{
R
Rasesh Mody 已提交
1220 1221
	int		err = 0;
	unsigned long	irq_flags, flags;
1222
	u32	irq;
R
Rasesh Mody 已提交
1223
	irq_handler_t	irq_handler;
1224 1225 1226 1227

	spin_lock_irqsave(&bnad->bna_lock, flags);
	if (bnad->cfg_flags & BNAD_CF_MSIX) {
		irq_handler = (irq_handler_t)bnad_msix_mbox_handler;
1228
		irq = bnad->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector;
1229
		irq_flags = 0;
1230 1231 1232
	} else {
		irq_handler = (irq_handler_t)bnad_isr;
		irq = bnad->pcidev->irq;
1233
		irq_flags = IRQF_SHARED;
1234
	}
1235

1236 1237 1238
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
	sprintf(bnad->mbox_irq_name, "%s", BNAD_NAME);

R
Rasesh Mody 已提交
1239 1240 1241 1242 1243 1244
	/*
	 * 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 已提交
1245 1246
	BNAD_UPDATE_CTR(bnad, mbox_intr_disabled);

1247
	err = request_irq(irq, irq_handler, irq_flags,
R
Rasesh Mody 已提交
1248
			  bnad->mbox_irq_name, bnad);
R
Rasesh Mody 已提交
1249

R
Rasesh Mody 已提交
1250
	return err;
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
}

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,
1263
		    u32 txrx_id, struct bna_intr_info *intr_info)
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
{
	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:
1283
			vector_start = BNAD_MAILBOX_MSIX_VECTORS + txrx_id;
1284 1285 1286
			break;

		case BNAD_INTR_RX:
1287 1288
			vector_start = BNAD_MAILBOX_MSIX_VECTORS +
					(bnad->num_tx * bnad->num_txq_per_tx) +
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
					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:
1309
			intr_info->idl[0].vector = BNAD_INTX_TX_IB_BITMASK;
1310 1311 1312
			break;

		case BNAD_INTR_RX:
1313
			intr_info->idl[0].vector = BNAD_INTX_RX_IB_BITMASK;
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
			break;
		}
	}
	return 0;
}

/**
 * NOTE: Should be called for MSIX only
 * 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]);
	}
}

/**
 * NOTE: Should be called for MSIX only
 * 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,
1346
			u32 tx_id, int num_txqs)
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 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 1395 1396 1397 1398
{
	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;
}

/**
 * NOTE: Should be called for MSIX only
 * 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);
	}
}

/**
 * NOTE: Should be called for MSIX only
 * 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,
1399
			u32 rx_id, int num_rxps)
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
{
	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,
1443
		  u32 tx_id)
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
{
	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);
1512
	bfa_nw_ioc_timeout((void *) &bnad->bna.ioceth.ioc);
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
	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);
1523
	bfa_nw_ioc_hb_check((void *) &bnad->bna.ioceth.ioc);
1524 1525 1526 1527
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
}

static void
R
Rasesh Mody 已提交
1528
bnad_iocpf_timeout(unsigned long data)
1529 1530 1531 1532 1533
{
	struct bnad *bnad = (struct bnad *)data;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
1534
	bfa_nw_iocpf_timeout((void *) &bnad->bna.ioceth.ioc);
R
Rasesh Mody 已提交
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
	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);
1545
	bfa_nw_iocpf_sem_timeout((void *) &bnad->bna.ioceth.ioc);
1546 1547 1548 1549 1550 1551
	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 已提交
1552
 *	Time	CPU m	CPU n
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
 *	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);
1604
	bna_hw_stats_get(&bnad->bna);
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 1635 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
	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);
1682
	struct bnad *bnad = rx_ctrl->bnad;
1683 1684
	int rcvd = 0;

R
Rasesh Mody 已提交
1685
	rx_ctrl->rx_poll_ctr++;
1686 1687 1688 1689

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

1690
	rcvd = bnad_poll_cq(bnad, rx_ctrl->ccb, budget);
R
Rasesh Mody 已提交
1691
	if (rcvd >= budget)
1692 1693 1694
		return rcvd;

poll_exit:
R
Rasesh Mody 已提交
1695
	napi_complete(napi);
1696

R
Rasesh Mody 已提交
1697
	rx_ctrl->rx_complete++;
1698 1699

	if (rx_ctrl->ccb)
R
Rasesh Mody 已提交
1700 1701
		bnad_enable_rx_irq_unsafe(rx_ctrl->ccb);

1702 1703 1704
	return rcvd;
}

1705
#define BNAD_NAPI_POLL_QUOTA		64
1706
static void
1707
bnad_napi_init(struct bnad *bnad, u32 rx_id)
1708 1709 1710 1711 1712 1713 1714 1715
{
	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,
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
			       bnad_napi_poll_rx, BNAD_NAPI_POLL_QUOTA);
	}
}

static void
bnad_napi_enable(struct bnad *bnad, u32 rx_id)
{
	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];
R
Rasesh Mody 已提交
1729

1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
		napi_enable(&rx_ctrl->napi);
	}
}

static void
bnad_napi_disable(struct bnad *bnad, u32 rx_id)
{
	int i;

	/* First disable and then clean up */
	for (i = 0; i < bnad->num_rxp_per_rx; i++) {
		napi_disable(&bnad->rx_info[rx_id].rx_ctrl[i].napi);
		netif_napi_del(&bnad->rx_info[rx_id].rx_ctrl[i].napi);
	}
}

/* Should be held with conf_lock held */
void
1748
bnad_cleanup_tx(struct bnad *bnad, u32 tx_id)
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
{
	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);

1767 1768 1769
	if (0 == tx_id)
		tasklet_kill(&bnad->tx_free_tasklet);

1770 1771 1772 1773 1774
	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_tx_destroy(tx_info->tx);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	tx_info->tx = NULL;
1775
	tx_info->tx_id = 0;
1776 1777 1778 1779 1780 1781

	bnad_tx_res_free(bnad, res_info);
}

/* Should be held with conf_lock held */
int
1782
bnad_setup_tx(struct bnad *bnad, u32 tx_id)
1783 1784 1785 1786 1787 1788 1789
{
	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];
1790 1791 1792 1793 1794 1795 1796 1797
	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,
	};

1798 1799 1800
	struct bna_tx *tx;
	unsigned long flags;

1801 1802
	tx_info->tx_id = tx_id;

1803 1804 1805 1806
	/* 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;
1807
	tx_config->coalescing_timeo = bnad->tx_coalescing_timeo;
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

	/* 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 */
	BNAD_FILL_UNMAPQ_MEM_REQ(
			&res_info[BNA_TX_RES_MEM_T_UNMAPQ],
			bnad->num_txq_per_tx,
			BNAD_TX_UNMAPQ_DEPTH);

	/* 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);
	if (!tx)
		goto err_return;
	tx_info->tx = tx;

	/* 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)
			goto err_return;
	}

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

	return 0;

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)
{
	rx_config->rx_type = BNA_RX_T_REGULAR;
	rx_config->num_paths = bnad->num_rxp_per_rx;
1861
	rx_config->coalescing_timeo = bnad->rx_coalescing_timeo;
1862 1863 1864 1865

	if (bnad->num_rxp_per_rx > 1) {
		rx_config->rss_status = BNA_STATUS_T_ENABLED;
		rx_config->rss_config.hash_type =
1866 1867 1868 1869
				(BFI_ENET_RSS_IPV6 |
				 BFI_ENET_RSS_IPV6_TCP |
				 BFI_ENET_RSS_IPV4 |
				 BFI_ENET_RSS_IPV4_TCP);
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
		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));
	}
	rx_config->rxp_type = BNA_RXP_SLR;
	rx_config->q_depth = bnad->rxq_depth;

	rx_config->small_buff_size = BFI_SMALL_RXBUF_SIZE;

	rx_config->vlan_strip_status = BNA_STATUS_T_ENABLED;
}

1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
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;
}

1897 1898
/* Called with mutex_lock(&bnad->conf_mutex) held */
void
1899
bnad_cleanup_rx(struct bnad *bnad, u32 rx_id)
1900 1901 1902 1903 1904
{
	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 已提交
1905
	int to_del = 0;
1906 1907 1908 1909 1910 1911

	if (!rx_info->rx)
		return;

	if (0 == rx_id) {
		spin_lock_irqsave(&bnad->bna_lock, flags);
R
Rasesh Mody 已提交
1912 1913
		if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED &&
		    test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags)) {
1914
			clear_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
R
Rasesh Mody 已提交
1915 1916
			to_del = 1;
		}
1917
		spin_unlock_irqrestore(&bnad->bna_lock, flags);
R
Rasesh Mody 已提交
1918
		if (to_del)
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
			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);

1931 1932
	bnad_napi_disable(bnad, rx_id);

1933 1934 1935 1936
	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_rx_destroy(rx_info->rx);

	rx_info->rx = NULL;
1937
	rx_info->rx_id = 0;
1938
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
1939 1940 1941 1942 1943 1944

	bnad_rx_res_free(bnad, res_info);
}

/* Called with mutex_lock(&bnad->conf_mutex) held */
int
1945
bnad_setup_rx(struct bnad *bnad, u32 rx_id)
1946 1947 1948 1949 1950 1951 1952
{
	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];
1953 1954 1955 1956 1957
	static const struct bna_rx_event_cbfn rx_cbfn = {
		.rcb_setup_cbfn = bnad_cb_rcb_setup,
		.rcb_destroy_cbfn = bnad_cb_rcb_destroy,
		.ccb_setup_cbfn = bnad_cb_ccb_setup,
		.ccb_destroy_cbfn = bnad_cb_ccb_destroy,
R
Rasesh Mody 已提交
1958
		.rx_stall_cbfn = bnad_cb_rx_stall,
1959 1960 1961
		.rx_cleanup_cbfn = bnad_cb_rx_cleanup,
		.rx_post_cbfn = bnad_cb_rx_post,
	};
1962 1963 1964
	struct bna_rx *rx;
	unsigned long flags;

1965 1966
	rx_info->rx_id = rx_id;

1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
	/* 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 */
	BNAD_FILL_UNMAPQ_MEM_REQ(
			&res_info[BNA_RX_RES_MEM_T_UNMAPQ],
			rx_config->num_paths +
			((rx_config->rxp_type == BNA_RXP_SINGLE) ? 0 :
				rx_config->num_paths), BNAD_RX_UNMAPQ_DEPTH);

	/* Allocate resource */
	err = bnad_rx_res_alloc(bnad, res_info, rx_id);
	if (err)
		return err;

1987 1988
	bnad_rx_ctrl_init(bnad, rx_id);

1989 1990 1991 1992
	/* 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);
1993 1994
	if (!rx) {
		err = -ENOMEM;
1995
		spin_unlock_irqrestore(&bnad->bna_lock, flags);
1996
		goto err_return;
1997
	}
1998
	rx_info->rx = rx;
1999
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
2000

2001 2002 2003 2004 2005 2006
	/*
	 * Init NAPI, so that state is set to NAPI_STATE_SCHED,
	 * so that IRQ handler cannot schedule NAPI at this point.
	 */
	bnad_napi_init(bnad, rx_id);

2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
	/* 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);

2031 2032 2033
	/* Enable scheduling of NAPI */
	bnad_napi_enable(bnad, rx_id);

2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058
	return 0;

err_return:
	bnad_cleanup_rx(bnad, rx_id);
	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 已提交
2059
	int	i;
2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072

	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 已提交
2073
int
2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
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 已提交
2093
int
2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
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 已提交
2118
/* Called with mutex_lock(&bnad->conf_mutex) held */
R
Rasesh Mody 已提交
2119
void
R
Rasesh Mody 已提交
2120 2121
bnad_restore_vlans(struct bnad *bnad, u32 rx_id)
{
J
Jiri Pirko 已提交
2122
	u16 vid;
R
Rasesh Mody 已提交
2123 2124
	unsigned long flags;

J
Jiri Pirko 已提交
2125
	for_each_set_bit(vid, bnad->active_vlans, VLAN_N_VID) {
R
Rasesh Mody 已提交
2126
		spin_lock_irqsave(&bnad->bna_lock, flags);
J
Jiri Pirko 已提交
2127
		bna_rx_vlan_add(bnad->rx_info[rx_id].rx, vid);
R
Rasesh Mody 已提交
2128 2129 2130 2131
		spin_unlock_irqrestore(&bnad->bna_lock, flags);
	}
}

2132 2133
/* Statistics utilities */
void
E
Eric Dumazet 已提交
2134
bnad_netdev_qstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
2135 2136 2137 2138 2139 2140
{
	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 已提交
2141
				stats->rx_packets += bnad->rx_info[i].
2142
				rx_ctrl[j].ccb->rcb[0]->rxq->rx_packets;
E
Eric Dumazet 已提交
2143
				stats->rx_bytes += bnad->rx_info[i].
2144 2145 2146 2147
					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 已提交
2148
					stats->rx_packets +=
2149 2150
						bnad->rx_info[i].rx_ctrl[j].
						ccb->rcb[1]->rxq->rx_packets;
E
Eric Dumazet 已提交
2151
					stats->rx_bytes +=
2152 2153 2154 2155 2156 2157 2158 2159 2160
						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 已提交
2161
				stats->tx_packets +=
2162
				bnad->tx_info[i].tcb[j]->txq->tx_packets;
E
Eric Dumazet 已提交
2163
				stats->tx_bytes +=
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
					bnad->tx_info[i].tcb[j]->txq->tx_bytes;
			}
		}
	}
}

/*
 * Must be called with the bna_lock held.
 */
void
E
Eric Dumazet 已提交
2174
bnad_netdev_hwstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
2175
{
2176 2177
	struct bfi_enet_stats_mac *mac_stats;
	u32 bmap;
2178 2179
	int i;

2180
	mac_stats = &bnad->stats.bna_stats->hw_stats.mac_stats;
E
Eric Dumazet 已提交
2181
	stats->rx_errors =
2182 2183 2184
		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 已提交
2185
	stats->tx_errors = mac_stats->tx_fcs_error +
2186
					mac_stats->tx_undersize;
E
Eric Dumazet 已提交
2187 2188 2189 2190
	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;
2191

E
Eric Dumazet 已提交
2192
	stats->rx_length_errors = mac_stats->rx_frame_length_error;
2193 2194 2195

	/* receive ring buffer overflow  ?? */

E
Eric Dumazet 已提交
2196 2197
	stats->rx_crc_errors = mac_stats->rx_fcs_error;
	stats->rx_frame_errors = mac_stats->rx_alignment_error;
2198
	/* recv'r fifo overrun */
2199 2200
	bmap = bna_rx_rid_mask(&bnad->bna);
	for (i = 0; bmap; i++) {
2201
		if (bmap & 1) {
E
Eric Dumazet 已提交
2202
			stats->rx_fifo_errors +=
2203
				bnad->stats.bna_stats->
2204
					hw_stats.rxf_stats[i].frame_drops;
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
			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)
2219
		irq = bnad->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector;
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279
	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(),
2280
			(uint)(BNAD_MAX_RX * BNAD_MAX_RXP_PER_RX));
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297

	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
2298
bnad_q_num_adjust(struct bnad *bnad, int msix_vectors, int temp)
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
{
	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;
}

2311 2312 2313
/* Enable / disable ioceth */
static int
bnad_ioceth_disable(struct bnad *bnad)
2314 2315
{
	unsigned long flags;
2316
	int err = 0;
2317 2318

	spin_lock_irqsave(&bnad->bna_lock, flags);
2319 2320
	init_completion(&bnad->bnad_completions.ioc_comp);
	bna_ioceth_disable(&bnad->bna.ioceth, BNA_HARD_CLEANUP);
2321 2322
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

2323 2324 2325 2326 2327
	wait_for_completion_timeout(&bnad->bnad_completions.ioc_comp,
		msecs_to_jiffies(BNAD_IOCETH_TIMEOUT));

	err = bnad->bnad_completions.ioc_comp_status;
	return err;
2328 2329 2330
}

static int
2331
bnad_ioceth_enable(struct bnad *bnad)
2332 2333 2334 2335 2336
{
	int err = 0;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);
2337 2338 2339
	init_completion(&bnad->bnad_completions.ioc_comp);
	bnad->bnad_completions.ioc_comp_status = BNA_CB_WAITING;
	bna_ioceth_enable(&bnad->bna.ioceth);
2340 2341
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

2342 2343
	wait_for_completion_timeout(&bnad->bnad_completions.ioc_comp,
		msecs_to_jiffies(BNAD_IOCETH_TIMEOUT));
2344

2345
	err = bnad->bnad_completions.ioc_comp_status;
2346 2347 2348 2349 2350 2351

	return err;
}

/* Free BNA resources */
static void
2352 2353
bnad_res_free(struct bnad *bnad, struct bna_res_info *res_info,
		u32 res_val_max)
2354 2355 2356
{
	int i;

2357 2358
	for (i = 0; i < res_val_max; i++)
		bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
2359 2360 2361 2362
}

/* Allocates memory and interrupt resources for BNA */
static int
2363 2364
bnad_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
		u32 res_val_max)
2365 2366 2367
{
	int i, err;

2368 2369
	for (i = 0; i < res_val_max; i++) {
		err = bnad_mem_alloc(bnad, &res_info[i].res_u.mem_info);
2370 2371 2372 2373 2374 2375
		if (err)
			goto err_return;
	}
	return 0;

err_return:
2376
	bnad_res_free(bnad, res_info, res_val_max);
2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
	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 已提交
2398
		kcalloc(bnad->msix_num, sizeof(struct msix_entry), GFP_KERNEL);
2399 2400 2401 2402

	if (!bnad->msix_table)
		goto intx_mode;

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

R
Rasesh Mody 已提交
2406
	ret = pci_enable_msix(bnad->pcidev, bnad->msix_table, bnad->msix_num);
2407 2408
	if (ret > 0) {
		/* Not enough MSI-X vectors. */
R
Rasesh Mody 已提交
2409 2410
		pr_warn("BNA: %d MSI-X vectors allocated < %d requested\n",
			ret, bnad->msix_num);
2411 2412 2413

		spin_lock_irqsave(&bnad->bna_lock, flags);
		/* ret = #of vectors that we got */
R
Rasesh Mody 已提交
2414 2415
		bnad_q_num_adjust(bnad, (ret - BNAD_MAILBOX_MSIX_VECTORS) / 2,
			(ret - BNAD_MAILBOX_MSIX_VECTORS) / 2);
2416 2417
		spin_unlock_irqrestore(&bnad->bna_lock, flags);

R
Rasesh Mody 已提交
2418
		bnad->msix_num = BNAD_NUM_TXQ + BNAD_NUM_RXP +
2419 2420
			 BNAD_MAILBOX_MSIX_VECTORS;

2421 2422 2423
		if (bnad->msix_num > ret)
			goto intx_mode;

2424 2425 2426
		/* 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 已提交
2427
				      bnad->msix_num);
2428 2429 2430 2431 2432
		if (ret)
			goto intx_mode;

	} else if (ret < 0)
		goto intx_mode;
2433 2434 2435

	pci_intx(bnad->pcidev, 0);

2436 2437 2438
	return;

intx_mode:
R
Rasesh Mody 已提交
2439
	pr_warn("BNA: MSI-X enable failed - operating in INTx mode\n");
2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494

	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;
	int mtu;
	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;

2495
	mtu = ETH_HLEN + VLAN_HLEN + bnad->netdev->mtu + ETH_FCS_LEN;
2496 2497

	spin_lock_irqsave(&bnad->bna_lock, flags);
2498 2499 2500
	bna_enet_mtu_set(&bnad->bna.enet, mtu, NULL);
	bna_enet_pause_config(&bnad->bna.enet, &pause_config, NULL);
	bna_enet_enable(&bnad->bna.enet);
2501 2502 2503 2504 2505
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	/* Enable broadcast */
	bnad_enable_default_bcast(bnad);

R
Rasesh Mody 已提交
2506 2507 2508
	/* Restore VLANs, if any */
	bnad_restore_vlans(bnad, 0);

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
	/* 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:
	bnad_cleanup_tx(bnad, 0);

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);

2540
	init_completion(&bnad->bnad_completions.enet_comp);
2541 2542

	spin_lock_irqsave(&bnad->bna_lock, flags);
2543 2544
	bna_enet_disable(&bnad->bna.enet, BNA_HARD_CLEANUP,
			bnad_cb_enet_disabled);
2545 2546
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

2547
	wait_for_completion(&bnad->bnad_completions.enet_comp);
2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568

	bnad_cleanup_tx(bnad, 0);
	bnad_cleanup_rx(bnad, 0);

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

	mutex_unlock(&bnad->conf_mutex);

	return 0;
}

/* TX */
/*
 * 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);
2569 2570
	u32 txq_id = 0;
	struct bna_tcb *tcb = bnad->tx_info[0].tcb[txq_id];
2571

R
Rasesh Mody 已提交
2572 2573 2574 2575
	u16		txq_prod, vlan_tag = 0;
	u32		unmap_prod, wis, wis_used, wi_range;
	u32		vectors, vect_id, i, acked;
	int			err;
R
Rasesh Mody 已提交
2576 2577
	unsigned int		len;
	u32				gso_size;
2578

2579
	struct bnad_unmap_q *unmap_q = tcb->unmap_q;
R
Rasesh Mody 已提交
2580
	dma_addr_t		dma_addr;
2581
	struct bna_txq_entry *txqent;
2582
	u16	flags;
2583

R
Rasesh Mody 已提交
2584 2585 2586 2587 2588 2589
	if (unlikely(skb->len <= ETH_HLEN)) {
		dev_kfree_skb(skb);
		BNAD_UPDATE_CTR(bnad, tx_skb_too_short);
		return NETDEV_TX_OK;
	}
	if (unlikely(skb_headlen(skb) > BFI_TX_MAX_DATA_PER_VECTOR)) {
2590
		dev_kfree_skb(skb);
R
Rasesh Mody 已提交
2591 2592 2593 2594 2595 2596
		BNAD_UPDATE_CTR(bnad, tx_skb_headlen_too_long);
		return NETDEV_TX_OK;
	}
	if (unlikely(skb_headlen(skb) == 0)) {
		dev_kfree_skb(skb);
		BNAD_UPDATE_CTR(bnad, tx_skb_headlen_zero);
2597 2598 2599 2600 2601
		return NETDEV_TX_OK;
	}

	/*
	 * Takes care of the Tx that is scheduled between clearing the flag
R
Rasesh Mody 已提交
2602
	 * and the netif_tx_stop_all_queues() call.
2603
	 */
R
Rasesh Mody 已提交
2604
	if (unlikely(!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))) {
2605
		dev_kfree_skb(skb);
R
Rasesh Mody 已提交
2606
		BNAD_UPDATE_CTR(bnad, tx_skb_stopping);
2607 2608 2609 2610
		return NETDEV_TX_OK;
	}

	vectors = 1 + skb_shinfo(skb)->nr_frags;
R
Rasesh Mody 已提交
2611
	if (unlikely(vectors > BFI_TX_MAX_VECTORS_PER_PKT)) {
2612
		dev_kfree_skb(skb);
R
Rasesh Mody 已提交
2613
		BNAD_UPDATE_CTR(bnad, tx_skb_max_vectors);
2614 2615 2616 2617
		return NETDEV_TX_OK;
	}
	wis = BNA_TXQ_WI_NEEDED(vectors);	/* 4 vectors per work item */
	acked = 0;
2618 2619
	if (unlikely(wis > BNA_QE_FREE_CNT(tcb, tcb->q_depth) ||
			vectors > BNA_QE_FREE_CNT(unmap_q, unmap_q->q_depth))) {
2620 2621 2622 2623
		if ((u16) (*tcb->hw_consumer_index) !=
		    tcb->consumer_index &&
		    !test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags)) {
			acked = bnad_free_txbufs(bnad, tcb);
R
Rasesh Mody 已提交
2624 2625
			if (likely(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
				bna_ib_ack(tcb->i_dbell, acked);
2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657
			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, tcb->q_depth) ||
		     vectors > BNA_QE_FREE_CNT(unmap_q, unmap_q->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);
		}
	}

	unmap_prod = unmap_q->producer_index;
	flags = 0;

	txq_prod = tcb->producer_index;
	BNA_TXQ_QPGE_PTR_GET(txq_prod, tcb->sw_qpt, txqent, wi_range);
	txqent->hdr.wi.reserved = 0;
	txqent->hdr.wi.num_vectors = vectors;

2658
	if (vlan_tx_tag_present(skb)) {
2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670
		vlan_tag = (u16) vlan_tx_tag_get(skb);
		flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
	}
	if (test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags)) {
		vlan_tag =
			(tcb->priority & 0x7) << 13 | (vlan_tag & 0x1fff);
		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 已提交
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
		gso_size = skb_shinfo(skb)->gso_size;

		if (unlikely(gso_size > netdev->mtu)) {
			dev_kfree_skb(skb);
			BNAD_UPDATE_CTR(bnad, tx_skb_mss_too_long);
			return NETDEV_TX_OK;
		}
		if (unlikely((gso_size + skb_transport_offset(skb) +
			tcp_hdrlen(skb)) >= skb->len)) {
			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);
		}

2690
		err = bnad_tso_prepare(bnad, skb);
R
Rasesh Mody 已提交
2691
		if (unlikely(err)) {
2692
			dev_kfree_skb(skb);
R
Rasesh Mody 已提交
2693
			BNAD_UPDATE_CTR(bnad, tx_skb_tso_prepare);
2694 2695 2696 2697 2698 2699 2700
			return NETDEV_TX_OK;
		}
		flags |= (BNA_TXQ_WI_CF_IP_CKSUM | BNA_TXQ_WI_CF_TCP_CKSUM);
		txqent->hdr.wi.l4_hdr_size_n_offset =
			htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
			      (tcp_hdrlen(skb) >> 2,
			       skb_transport_offset(skb)));
R
Rasesh Mody 已提交
2701 2702
	} else {
		txqent->hdr.wi.opcode =	__constant_htons(BNA_TXQ_WI_SEND);
2703 2704
		txqent->hdr.wi.lso_mss = 0;

R
Rasesh Mody 已提交
2705 2706 2707 2708
		if (unlikely(skb->len > (netdev->mtu + ETH_HLEN))) {
			dev_kfree_skb(skb);
			BNAD_UPDATE_CTR(bnad, tx_skb_non_tso_too_long);
			return NETDEV_TX_OK;
2709 2710
		}

R
Rasesh Mody 已提交
2711 2712
		if (skb->ip_summed == CHECKSUM_PARTIAL) {
			u8 proto = 0;
2713

R
Rasesh Mody 已提交
2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734
			if (skb->protocol == __constant_htons(ETH_P_IP))
				proto = ip_hdr(skb)->protocol;
			else if (skb->protocol ==
				 __constant_htons(ETH_P_IPV6)) {
				/* nexthdr may not be TCP immediately. */
				proto = ipv6_hdr(skb)->nexthdr;
			}
			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) <
				skb_transport_offset(skb) + tcp_hdrlen(skb))) {
					dev_kfree_skb(skb);
					BNAD_UPDATE_CTR(bnad, tx_skb_tcp_hdr);
					return NETDEV_TX_OK;
				}
2735

R
Rasesh Mody 已提交
2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750
			} 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) <
				    skb_transport_offset(skb) +
				    sizeof(struct udphdr))) {
					dev_kfree_skb(skb);
					BNAD_UPDATE_CTR(bnad, tx_skb_udp_hdr);
					return NETDEV_TX_OK;
				}
			} else {
2751
				dev_kfree_skb(skb);
R
Rasesh Mody 已提交
2752
				BNAD_UPDATE_CTR(bnad, tx_skb_csum_err);
2753 2754
				return NETDEV_TX_OK;
			}
R
Rasesh Mody 已提交
2755 2756
		} else {
			txqent->hdr.wi.l4_hdr_size_n_offset = 0;
2757 2758 2759 2760 2761 2762 2763 2764
		}
	}

	txqent->hdr.wi.flags = htons(flags);

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

	unmap_q->unmap_array[unmap_prod].skb = skb;
R
Rasesh Mody 已提交
2765 2766
	len = skb_headlen(skb);
	txqent->vector[0].length = htons(len);
I
Ivan Vecera 已提交
2767 2768 2769
	dma_addr = dma_map_single(&bnad->pcidev->dev, skb->data,
				  skb_headlen(skb), DMA_TO_DEVICE);
	dma_unmap_addr_set(&unmap_q->unmap_array[unmap_prod], dma_addr,
2770 2771
			   dma_addr);

R
Rasesh Mody 已提交
2772
	BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[0].host_addr);
2773 2774
	BNA_QE_INDX_ADD(unmap_prod, 1, unmap_q->q_depth);

R
Rasesh Mody 已提交
2775 2776 2777
	vect_id = 0;
	wis_used = 1;

2778
	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
E
Eric Dumazet 已提交
2779 2780
		const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
		u16		size = skb_frag_size(frag);
2781

R
Rasesh Mody 已提交
2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795
		if (unlikely(size == 0)) {
			unmap_prod = unmap_q->producer_index;

			unmap_prod = bnad_pci_unmap_skb(&bnad->pcidev->dev,
					   unmap_q->unmap_array,
					   unmap_prod, unmap_q->q_depth, skb,
					   i);
			dev_kfree_skb(skb);
			BNAD_UPDATE_CTR(bnad, tx_skb_frag_zero);
			return NETDEV_TX_OK;
		}

		len += size;

2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807
		if (++vect_id == BFI_TX_MAX_VECTORS_PER_WI) {
			vect_id = 0;
			if (--wi_range)
				txqent++;
			else {
				BNA_QE_INDX_ADD(txq_prod, wis_used,
						tcb->q_depth);
				wis_used = 0;
				BNA_TXQ_QPGE_PTR_GET(txq_prod, tcb->sw_qpt,
						     txqent, wi_range);
			}
			wis_used++;
R
Rasesh Mody 已提交
2808 2809
			txqent->hdr.wi_ext.opcode =
				__constant_htons(BNA_TXQ_WI_EXTENSION);
2810 2811 2812 2813
		}

		BUG_ON(!(size <= BFI_TX_MAX_DATA_PER_VECTOR));
		txqent->vector[vect_id].length = htons(size);
2814 2815
		dma_addr = skb_frag_dma_map(&bnad->pcidev->dev, frag,
					    0, size, DMA_TO_DEVICE);
I
Ivan Vecera 已提交
2816
		dma_unmap_addr_set(&unmap_q->unmap_array[unmap_prod], dma_addr,
2817 2818 2819 2820 2821
				   dma_addr);
		BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[vect_id].host_addr);
		BNA_QE_INDX_ADD(unmap_prod, 1, unmap_q->q_depth);
	}

R
Rasesh Mody 已提交
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833
	if (unlikely(len != skb->len)) {
		unmap_prod = unmap_q->producer_index;

		unmap_prod = bnad_pci_unmap_skb(&bnad->pcidev->dev,
				unmap_q->unmap_array, unmap_prod,
				unmap_q->q_depth, skb,
				skb_shinfo(skb)->nr_frags);
		dev_kfree_skb(skb);
		BNAD_UPDATE_CTR(bnad, tx_skb_len_mismatch);
		return NETDEV_TX_OK;
	}

2834 2835 2836 2837 2838
	unmap_q->producer_index = unmap_prod;
	BNA_QE_INDX_ADD(txq_prod, wis_used, tcb->q_depth);
	tcb->producer_index = txq_prod;

	smp_mb();
R
Rasesh Mody 已提交
2839 2840 2841 2842

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

2843
	bna_txq_prod_indx_doorbell(tcb);
R
Rasesh Mody 已提交
2844
	smp_mb();
2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855

	if ((u16) (*tcb->hw_consumer_index) != tcb->consumer_index)
		tasklet_schedule(&bnad->tx_free_tasklet);

	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 已提交
2856 2857
static struct rtnl_link_stats64 *
bnad_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
2858 2859 2860 2861 2862 2863
{
	struct bnad *bnad = netdev_priv(netdev);
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);

E
Eric Dumazet 已提交
2864 2865
	bnad_netdev_qstats_fill(bnad, stats);
	bnad_netdev_hwstats_fill(bnad, stats);
2866 2867 2868

	spin_unlock_irqrestore(&bnad->bna_lock, flags);

E
Eric Dumazet 已提交
2869
	return stats;
2870 2871
}

R
Rasesh Mody 已提交
2872
void
2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910
bnad_set_rx_mode(struct net_device *netdev)
{
	struct bnad *bnad = netdev_priv(netdev);
	u32	new_mask, valid_mask;
	unsigned long flags;

	spin_lock_irqsave(&bnad->bna_lock, flags);

	new_mask = valid_mask = 0;

	if (netdev->flags & IFF_PROMISC) {
		if (!(bnad->cfg_flags & BNAD_CF_PROMISC)) {
			new_mask = BNAD_RXMODE_PROMISC_DEFAULT;
			valid_mask = BNAD_RXMODE_PROMISC_DEFAULT;
			bnad->cfg_flags |= BNAD_CF_PROMISC;
		}
	} else {
		if (bnad->cfg_flags & BNAD_CF_PROMISC) {
			new_mask = ~BNAD_RXMODE_PROMISC_DEFAULT;
			valid_mask = BNAD_RXMODE_PROMISC_DEFAULT;
			bnad->cfg_flags &= ~BNAD_CF_PROMISC;
		}
	}

	if (netdev->flags & IFF_ALLMULTI) {
		if (!(bnad->cfg_flags & BNAD_CF_ALLMULTI)) {
			new_mask |= BNA_RXMODE_ALLMULTI;
			valid_mask |= BNA_RXMODE_ALLMULTI;
			bnad->cfg_flags |= BNAD_CF_ALLMULTI;
		}
	} else {
		if (bnad->cfg_flags & BNAD_CF_ALLMULTI) {
			new_mask &= ~BNA_RXMODE_ALLMULTI;
			valid_mask |= BNA_RXMODE_ALLMULTI;
			bnad->cfg_flags &= ~BNAD_CF_ALLMULTI;
		}
	}

R
Rasesh Mody 已提交
2911 2912 2913
	if (bnad->rx_info[0].rx == NULL)
		goto unlock;

2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924
	bna_rx_mode_set(bnad->rx_info[0].rx, new_mask, valid_mask, NULL);

	if (!netdev_mc_empty(netdev)) {
		u8 *mcaddr_list;
		int mc_count = netdev_mc_count(netdev);

		/* Index 0 holds the broadcast address */
		mcaddr_list =
			kzalloc((mc_count + 1) * ETH_ALEN,
				GFP_ATOMIC);
		if (!mcaddr_list)
J
Jiri Slaby 已提交
2925
			goto unlock;
2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937

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

		/* Copy rest of the MC addresses */
		bnad_netdev_mc_list_get(netdev, mcaddr_list);

		bna_rx_mcast_listset(bnad->rx_info[0].rx, mc_count + 1,
					mcaddr_list, NULL);

		/* Should we enable BNAD_CF_ALLMULTI for err != 0 ? */
		kfree(mcaddr_list);
	}
J
Jiri Slaby 已提交
2938
unlock:
2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967
	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
2968
bnad_mtu_set(struct bnad *bnad, int mtu)
2969 2970 2971
{
	unsigned long flags;

2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986
	init_completion(&bnad->bnad_completions.mtu_comp);

	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_enet_mtu_set(&bnad->bna.enet, mtu, bnad_cb_enet_mtu_set);
	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)
{
	int err, mtu = netdev->mtu;
2987 2988 2989 2990 2991 2992 2993 2994 2995
	struct bnad *bnad = netdev_priv(netdev);

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

	mutex_lock(&bnad->conf_mutex);

	netdev->mtu = new_mtu;

2996 2997 2998 2999
	mtu = ETH_HLEN + VLAN_HLEN + new_mtu + ETH_FCS_LEN;
	err = bnad_mtu_set(bnad, mtu);
	if (err)
		err = -EBUSY;
3000 3001 3002 3003 3004

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

3005
static int
3006 3007 3008 3009 3010 3011 3012
bnad_vlan_rx_add_vid(struct net_device *netdev,
				 unsigned short vid)
{
	struct bnad *bnad = netdev_priv(netdev);
	unsigned long flags;

	if (!bnad->rx_info[0].rx)
3013
		return 0;
3014 3015 3016 3017 3018

	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 已提交
3019
	set_bit(vid, bnad->active_vlans);
3020 3021 3022
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	mutex_unlock(&bnad->conf_mutex);
3023 3024

	return 0;
3025 3026
}

3027
static int
3028 3029 3030 3031 3032 3033 3034
bnad_vlan_rx_kill_vid(struct net_device *netdev,
				  unsigned short vid)
{
	struct bnad *bnad = netdev_priv(netdev);
	unsigned long flags;

	if (!bnad->rx_info[0].rx)
3035
		return 0;
3036 3037 3038 3039

	mutex_lock(&bnad->conf_mutex);

	spin_lock_irqsave(&bnad->bna_lock, flags);
J
Jiri Pirko 已提交
3040
	clear_bit(vid, bnad->active_vlans);
3041 3042 3043 3044
	bna_rx_vlan_del(bnad->rx_info[0].rx, vid);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

	mutex_unlock(&bnad->conf_mutex);
3045 3046

	return 0;
3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063
}

#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 已提交
3064 3065 3066 3067 3068 3069
		/*
		 * Tx processing may happen in sending context, so no need
		 * to explicitly process completions here
		 */

		/* Rx processing */
3070 3071 3072 3073 3074 3075
		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 已提交
3076
				if (rx_ctrl->ccb)
3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
					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 已提交
3089
	.ndo_get_stats64		= bnad_get_stats64,
3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105
	.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;

3106 3107 3108
	netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
		NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
		NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_HW_VLAN_TX;
3109

3110 3111 3112
	netdev->vlan_features = NETIF_F_SG | NETIF_F_HIGHDMA |
		NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
		NETIF_F_TSO | NETIF_F_TSO6;
3113

3114 3115
	netdev->features |= netdev->hw_features |
		NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;
3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 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

	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
 * 3. Initialze Tx free tasklet
 * 4. Initialize no. of TxQ & CQs & MSIX vectors
 */
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");
		pci_set_drvdata(pdev, NULL);
		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;

	tasklet_init(&bnad->tx_free_tasklet, bnad_tx_free_tasklet,
		     (unsigned long)bnad);

	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)
{
	if (bnad->bar0)
		iounmap(bnad->bar0);
	pci_set_drvdata(bnad->pcidev, NULL);
}

/*
 * Initialize locks
3195
	a) Per ioceth mutes used for serializing configuration
3196 3197 3198 3199 3200 3201 3202 3203
	   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);
3204
	mutex_init(&bnad_list_mutex);
3205 3206 3207 3208 3209 3210
}

static void
bnad_lock_uninit(struct bnad *bnad)
{
	mutex_destroy(&bnad->conf_mutex);
3211
	mutex_destroy(&bnad_list_mutex);
3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226
}

/* 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;
I
Ivan Vecera 已提交
3227 3228
	if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
	    !dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64))) {
3229
		*using_dac = true;
3230
	} else {
I
Ivan Vecera 已提交
3231
		err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3232
		if (err) {
I
Ivan Vecera 已提交
3233 3234
			err = dma_set_coherent_mask(&pdev->dev,
						    DMA_BIT_MASK(32));
3235 3236 3237
			if (err)
				goto release_regions;
		}
3238
		*using_dac = false;
3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261
	}
	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);
}

static int __devinit
bnad_pci_probe(struct pci_dev *pdev,
		const struct pci_device_id *pcidev_id)
{
3262
	bool	using_dac;
R
Rasesh Mody 已提交
3263
	int	err;
3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286
	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) {
3287
		dev_err(&pdev->dev, "netdev allocation failed\n");
3288 3289 3290 3291
		err = -ENOMEM;
		return err;
	}
	bnad = netdev_priv(netdev);
3292
	bnad_lock_init(bnad);
3293
	bnad_add_to_list(bnad);
3294 3295

	mutex_lock(&bnad->conf_mutex);
3296 3297
	/*
	 * PCI initialization
R
Rasesh Mody 已提交
3298
	 *	Output : using_dac = 1 for 64 bit DMA
R
Rasesh Mody 已提交
3299
	 *			   = 0 for 32 bit DMA
3300 3301 3302
	 */
	err = bnad_pci_init(bnad, pdev, &using_dac);
	if (err)
3303
		goto unlock_mutex;
3304 3305 3306 3307 3308 3309 3310 3311 3312

	/*
	 * Initialize bnad structure
	 * Setup relation between pci_dev & netdev
	 * Init Tx free tasklet
	 */
	err = bnad_init(bnad, pdev, netdev);
	if (err)
		goto pci_uninit;
3313

3314 3315 3316
	/* Initialize netdev structure, set up ethtool ops */
	bnad_netdev_init(bnad, using_dac);

3317 3318 3319
	/* Set link to down state */
	netif_carrier_off(netdev);

K
Krishna Gudipati 已提交
3320 3321 3322 3323
	/* Setup the debugfs node for this bfad */
	if (bna_debugfs_enable)
		bnad_debugfs_init(bnad);

3324
	/* Get resource requirement form bna */
3325
	spin_lock_irqsave(&bnad->bna_lock, flags);
3326
	bna_res_req(&bnad->res_info[0]);
3327
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
3328 3329

	/* Allocate resources from bna */
3330
	err = bnad_res_alloc(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
3331
	if (err)
3332
		goto drv_uninit;
3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347

	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;

3348 3349 3350 3351 3352 3353
	bnad_enable_msix(bnad);
	err = bnad_mbox_irq_alloc(bnad);
	if (err)
		goto res_free;


3354
	/* Set up timers */
3355
	setup_timer(&bnad->bna.ioceth.ioc.ioc_timer, bnad_ioc_timeout,
3356
				((unsigned long)bnad));
3357
	setup_timer(&bnad->bna.ioceth.ioc.hb_timer, bnad_ioc_hb_check,
3358
				((unsigned long)bnad));
3359
	setup_timer(&bnad->bna.ioceth.ioc.iocpf_timer, bnad_iocpf_timeout,
R
Rasesh Mody 已提交
3360
				((unsigned long)bnad));
3361
	setup_timer(&bnad->bna.ioceth.ioc.sem_timer, bnad_iocpf_sem_timeout,
3362 3363 3364
				((unsigned long)bnad));

	/* Now start the timer before calling IOC */
3365
	mod_timer(&bnad->bna.ioceth.ioc.iocpf_timer,
3366 3367 3368 3369
		  jiffies + msecs_to_jiffies(BNA_IOC_TIMER_FREQ));

	/*
	 * Start the chip
3370 3371
	 * If the call back comes with error, we bail out.
	 * This is a catastrophic error.
3372
	 */
3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388
	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;
	}
3389 3390 3391 3392 3393
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
	if (err)
		goto disable_ioceth;

	spin_lock_irqsave(&bnad->bna_lock, flags);
3394 3395 3396 3397
	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 已提交
3398 3399
	if (err) {
		err = -EIO;
3400
		goto disable_ioceth;
R
Rasesh Mody 已提交
3401
	}
3402 3403 3404 3405

	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_mod_init(&bnad->bna, &bnad->mod_res_info[0]);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
3406 3407 3408

	/* Get the burnt-in mac */
	spin_lock_irqsave(&bnad->bna_lock, flags);
3409
	bna_enet_perm_mac_get(&bna->enet, &bnad->perm_addr);
3410 3411 3412
	bnad_set_netdev_perm_addr(bnad);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

R
Rasesh Mody 已提交
3413 3414
	mutex_unlock(&bnad->conf_mutex);

3415 3416 3417 3418
	/* Finally, reguister with net_device layer */
	err = register_netdev(netdev);
	if (err) {
		pr_err("BNA : Registering with netdev failed\n");
3419
		goto probe_uninit;
3420
	}
3421
	set_bit(BNAD_RF_NETDEV_REGISTERED, &bnad->run_flags);
3422

R
Rasesh Mody 已提交
3423 3424
	return 0;

3425 3426
probe_success:
	mutex_unlock(&bnad->conf_mutex);
3427 3428
	return 0;

3429
probe_uninit:
R
Rasesh Mody 已提交
3430
	mutex_lock(&bnad->conf_mutex);
3431 3432 3433 3434 3435 3436
	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);
3437 3438 3439
	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_uninit(bna);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);
3440
	bnad_mbox_irq_free(bnad);
3441
	bnad_disable_msix(bnad);
3442 3443 3444
res_free:
	bnad_res_free(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
drv_uninit:
K
Krishna Gudipati 已提交
3445 3446 3447
	/* Remove the debugfs node for this bnad */
	kfree(bnad->regdata);
	bnad_debugfs_uninit(bnad);
3448
	bnad_uninit(bnad);
3449 3450
pci_uninit:
	bnad_pci_uninit(pdev);
3451
unlock_mutex:
3452
	mutex_unlock(&bnad->conf_mutex);
3453
	bnad_remove_from_list(bnad);
3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473
	bnad_lock_uninit(bnad);
	free_netdev(netdev);
	return err;
}

static void __devexit
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;

3474 3475
	if (test_and_clear_bit(BNAD_RF_NETDEV_REGISTERED, &bnad->run_flags))
		unregister_netdev(netdev);
3476 3477

	mutex_lock(&bnad->conf_mutex);
3478 3479 3480 3481
	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);
3482 3483 3484 3485
	spin_lock_irqsave(&bnad->bna_lock, flags);
	bna_uninit(bna);
	spin_unlock_irqrestore(&bnad->bna_lock, flags);

3486 3487 3488
	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);
3489 3490
	bnad_disable_msix(bnad);
	bnad_pci_uninit(pdev);
3491
	mutex_unlock(&bnad->conf_mutex);
3492
	bnad_remove_from_list(bnad);
3493
	bnad_lock_uninit(bnad);
K
Krishna Gudipati 已提交
3494 3495 3496
	/* Remove the debugfs node for this bnad */
	kfree(bnad->regdata);
	bnad_debugfs_uninit(bnad);
3497 3498 3499 3500
	bnad_uninit(bnad);
	free_netdev(netdev);
}

R
Rasesh Mody 已提交
3501
static DEFINE_PCI_DEVICE_TABLE(bnad_pci_id_table) = {
3502 3503 3504 3505 3506
	{
		PCI_DEVICE(PCI_VENDOR_ID_BROCADE,
			PCI_DEVICE_ID_BROCADE_CT),
		.class = PCI_CLASS_NETWORK_ETHERNET << 8,
		.class_mask =  0xffff00
R
Rasesh Mody 已提交
3507 3508 3509 3510 3511 3512 3513 3514
	},
	{
		PCI_DEVICE(PCI_VENDOR_ID_BROCADE,
			BFA_PCI_DEVICE_ID_CT2),
		.class = PCI_CLASS_NETWORK_ETHERNET << 8,
		.class_mask =  0xffff00
	},
	{0,  },
3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530
};

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,
	.remove = __devexit_p(bnad_pci_remove),
};

static int __init
bnad_module_init(void)
{
	int err;

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

3534
	bfa_nw_ioc_auto_recover(bnad_ioc_auto_recover);
3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562

	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);

	if (bfi_fw)
		release_firmware(bfi_fw);
}

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);
3563
MODULE_FIRMWARE(CNA_FW_FILE_CT2);