Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / net / ethernet / broadcom / bnx2x / bnx2x_cmn.c
blob5c3eb17c4f4a28f2bdfaa95008b8863155c41082
1 /* bnx2x_cmn.c: Broadcom Everest network driver.
3 * Copyright (c) 2007-2011 Broadcom Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation.
9 * Maintained by: Eilon Greenstein <eilong@broadcom.com>
10 * Written by: Eliezer Tamir
11 * Based on code from Michael Chan's bnx2 driver
12 * UDP CSUM errata workaround by Arik Gendelman
13 * Slowpath and fastpath rework by Vladislav Zolotarov
14 * Statistics and Link management by Yitchak Gertner
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/etherdevice.h>
21 #include <linux/if_vlan.h>
22 #include <linux/interrupt.h>
23 #include <linux/ip.h>
24 #include <net/ipv6.h>
25 #include <net/ip6_checksum.h>
26 #include <linux/firmware.h>
27 #include <linux/prefetch.h>
28 #include "bnx2x_cmn.h"
29 #include "bnx2x_init.h"
30 #include "bnx2x_sp.h"
34 /**
35 * bnx2x_bz_fp - zero content of the fastpath structure.
37 * @bp: driver handle
38 * @index: fastpath index to be zeroed
40 * Makes sure the contents of the bp->fp[index].napi is kept
41 * intact.
43 static inline void bnx2x_bz_fp(struct bnx2x *bp, int index)
45 struct bnx2x_fastpath *fp = &bp->fp[index];
46 struct napi_struct orig_napi = fp->napi;
47 /* bzero bnx2x_fastpath contents */
48 memset(fp, 0, sizeof(*fp));
50 /* Restore the NAPI object as it has been already initialized */
51 fp->napi = orig_napi;
53 fp->bp = bp;
54 fp->index = index;
55 if (IS_ETH_FP(fp))
56 fp->max_cos = bp->max_cos;
57 else
58 /* Special queues support only one CoS */
59 fp->max_cos = 1;
62 * set the tpa flag for each queue. The tpa flag determines the queue
63 * minimal size so it must be set prior to queue memory allocation
65 fp->disable_tpa = ((bp->flags & TPA_ENABLE_FLAG) == 0);
67 #ifdef BCM_CNIC
68 /* We don't want TPA on an FCoE L2 ring */
69 if (IS_FCOE_FP(fp))
70 fp->disable_tpa = 1;
71 #endif
74 /**
75 * bnx2x_move_fp - move content of the fastpath structure.
77 * @bp: driver handle
78 * @from: source FP index
79 * @to: destination FP index
81 * Makes sure the contents of the bp->fp[to].napi is kept
82 * intact.
84 static inline void bnx2x_move_fp(struct bnx2x *bp, int from, int to)
86 struct bnx2x_fastpath *from_fp = &bp->fp[from];
87 struct bnx2x_fastpath *to_fp = &bp->fp[to];
88 struct napi_struct orig_napi = to_fp->napi;
89 /* Move bnx2x_fastpath contents */
90 memcpy(to_fp, from_fp, sizeof(*to_fp));
91 to_fp->index = to;
93 /* Restore the NAPI object as it has been already initialized */
94 to_fp->napi = orig_napi;
97 int load_count[2][3] = { {0} }; /* per-path: 0-common, 1-port0, 2-port1 */
99 /* free skb in the packet ring at pos idx
100 * return idx of last bd freed
102 static u16 bnx2x_free_tx_pkt(struct bnx2x *bp, struct bnx2x_fp_txdata *txdata,
103 u16 idx)
105 struct sw_tx_bd *tx_buf = &txdata->tx_buf_ring[idx];
106 struct eth_tx_start_bd *tx_start_bd;
107 struct eth_tx_bd *tx_data_bd;
108 struct sk_buff *skb = tx_buf->skb;
109 u16 bd_idx = TX_BD(tx_buf->first_bd), new_cons;
110 int nbd;
112 /* prefetch skb end pointer to speedup dev_kfree_skb() */
113 prefetch(&skb->end);
115 DP(BNX2X_MSG_FP, "fp[%d]: pkt_idx %d buff @(%p)->skb %p\n",
116 txdata->txq_index, idx, tx_buf, skb);
118 /* unmap first bd */
119 DP(BNX2X_MSG_OFF, "free bd_idx %d\n", bd_idx);
120 tx_start_bd = &txdata->tx_desc_ring[bd_idx].start_bd;
121 dma_unmap_single(&bp->pdev->dev, BD_UNMAP_ADDR(tx_start_bd),
122 BD_UNMAP_LEN(tx_start_bd), DMA_TO_DEVICE);
125 nbd = le16_to_cpu(tx_start_bd->nbd) - 1;
126 #ifdef BNX2X_STOP_ON_ERROR
127 if ((nbd - 1) > (MAX_SKB_FRAGS + 2)) {
128 BNX2X_ERR("BAD nbd!\n");
129 bnx2x_panic();
131 #endif
132 new_cons = nbd + tx_buf->first_bd;
134 /* Get the next bd */
135 bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
137 /* Skip a parse bd... */
138 --nbd;
139 bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
141 /* ...and the TSO split header bd since they have no mapping */
142 if (tx_buf->flags & BNX2X_TSO_SPLIT_BD) {
143 --nbd;
144 bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
147 /* now free frags */
148 while (nbd > 0) {
150 DP(BNX2X_MSG_OFF, "free frag bd_idx %d\n", bd_idx);
151 tx_data_bd = &txdata->tx_desc_ring[bd_idx].reg_bd;
152 dma_unmap_page(&bp->pdev->dev, BD_UNMAP_ADDR(tx_data_bd),
153 BD_UNMAP_LEN(tx_data_bd), DMA_TO_DEVICE);
154 if (--nbd)
155 bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
158 /* release skb */
159 WARN_ON(!skb);
160 dev_kfree_skb_any(skb);
161 tx_buf->first_bd = 0;
162 tx_buf->skb = NULL;
164 return new_cons;
167 int bnx2x_tx_int(struct bnx2x *bp, struct bnx2x_fp_txdata *txdata)
169 struct netdev_queue *txq;
170 u16 hw_cons, sw_cons, bd_cons = txdata->tx_bd_cons;
172 #ifdef BNX2X_STOP_ON_ERROR
173 if (unlikely(bp->panic))
174 return -1;
175 #endif
177 txq = netdev_get_tx_queue(bp->dev, txdata->txq_index);
178 hw_cons = le16_to_cpu(*txdata->tx_cons_sb);
179 sw_cons = txdata->tx_pkt_cons;
181 while (sw_cons != hw_cons) {
182 u16 pkt_cons;
184 pkt_cons = TX_BD(sw_cons);
186 DP(NETIF_MSG_TX_DONE, "queue[%d]: hw_cons %u sw_cons %u "
187 " pkt_cons %u\n",
188 txdata->txq_index, hw_cons, sw_cons, pkt_cons);
190 bd_cons = bnx2x_free_tx_pkt(bp, txdata, pkt_cons);
191 sw_cons++;
194 txdata->tx_pkt_cons = sw_cons;
195 txdata->tx_bd_cons = bd_cons;
197 /* Need to make the tx_bd_cons update visible to start_xmit()
198 * before checking for netif_tx_queue_stopped(). Without the
199 * memory barrier, there is a small possibility that
200 * start_xmit() will miss it and cause the queue to be stopped
201 * forever.
202 * On the other hand we need an rmb() here to ensure the proper
203 * ordering of bit testing in the following
204 * netif_tx_queue_stopped(txq) call.
206 smp_mb();
208 if (unlikely(netif_tx_queue_stopped(txq))) {
209 /* Taking tx_lock() is needed to prevent reenabling the queue
210 * while it's empty. This could have happen if rx_action() gets
211 * suspended in bnx2x_tx_int() after the condition before
212 * netif_tx_wake_queue(), while tx_action (bnx2x_start_xmit()):
214 * stops the queue->sees fresh tx_bd_cons->releases the queue->
215 * sends some packets consuming the whole queue again->
216 * stops the queue
219 __netif_tx_lock(txq, smp_processor_id());
221 if ((netif_tx_queue_stopped(txq)) &&
222 (bp->state == BNX2X_STATE_OPEN) &&
223 (bnx2x_tx_avail(bp, txdata) >= MAX_SKB_FRAGS + 3))
224 netif_tx_wake_queue(txq);
226 __netif_tx_unlock(txq);
228 return 0;
231 static inline void bnx2x_update_last_max_sge(struct bnx2x_fastpath *fp,
232 u16 idx)
234 u16 last_max = fp->last_max_sge;
236 if (SUB_S16(idx, last_max) > 0)
237 fp->last_max_sge = idx;
240 static void bnx2x_update_sge_prod(struct bnx2x_fastpath *fp,
241 struct eth_fast_path_rx_cqe *fp_cqe)
243 struct bnx2x *bp = fp->bp;
244 u16 sge_len = SGE_PAGE_ALIGN(le16_to_cpu(fp_cqe->pkt_len) -
245 le16_to_cpu(fp_cqe->len_on_bd)) >>
246 SGE_PAGE_SHIFT;
247 u16 last_max, last_elem, first_elem;
248 u16 delta = 0;
249 u16 i;
251 if (!sge_len)
252 return;
254 /* First mark all used pages */
255 for (i = 0; i < sge_len; i++)
256 BIT_VEC64_CLEAR_BIT(fp->sge_mask,
257 RX_SGE(le16_to_cpu(fp_cqe->sgl_or_raw_data.sgl[i])));
259 DP(NETIF_MSG_RX_STATUS, "fp_cqe->sgl[%d] = %d\n",
260 sge_len - 1, le16_to_cpu(fp_cqe->sgl_or_raw_data.sgl[sge_len - 1]));
262 /* Here we assume that the last SGE index is the biggest */
263 prefetch((void *)(fp->sge_mask));
264 bnx2x_update_last_max_sge(fp,
265 le16_to_cpu(fp_cqe->sgl_or_raw_data.sgl[sge_len - 1]));
267 last_max = RX_SGE(fp->last_max_sge);
268 last_elem = last_max >> BIT_VEC64_ELEM_SHIFT;
269 first_elem = RX_SGE(fp->rx_sge_prod) >> BIT_VEC64_ELEM_SHIFT;
271 /* If ring is not full */
272 if (last_elem + 1 != first_elem)
273 last_elem++;
275 /* Now update the prod */
276 for (i = first_elem; i != last_elem; i = NEXT_SGE_MASK_ELEM(i)) {
277 if (likely(fp->sge_mask[i]))
278 break;
280 fp->sge_mask[i] = BIT_VEC64_ELEM_ONE_MASK;
281 delta += BIT_VEC64_ELEM_SZ;
284 if (delta > 0) {
285 fp->rx_sge_prod += delta;
286 /* clear page-end entries */
287 bnx2x_clear_sge_mask_next_elems(fp);
290 DP(NETIF_MSG_RX_STATUS,
291 "fp->last_max_sge = %d fp->rx_sge_prod = %d\n",
292 fp->last_max_sge, fp->rx_sge_prod);
295 static void bnx2x_tpa_start(struct bnx2x_fastpath *fp, u16 queue,
296 struct sk_buff *skb, u16 cons, u16 prod,
297 struct eth_fast_path_rx_cqe *cqe)
299 struct bnx2x *bp = fp->bp;
300 struct sw_rx_bd *cons_rx_buf = &fp->rx_buf_ring[cons];
301 struct sw_rx_bd *prod_rx_buf = &fp->rx_buf_ring[prod];
302 struct eth_rx_bd *prod_bd = &fp->rx_desc_ring[prod];
303 dma_addr_t mapping;
304 struct bnx2x_agg_info *tpa_info = &fp->tpa_info[queue];
305 struct sw_rx_bd *first_buf = &tpa_info->first_buf;
307 /* print error if current state != stop */
308 if (tpa_info->tpa_state != BNX2X_TPA_STOP)
309 BNX2X_ERR("start of bin not in stop [%d]\n", queue);
311 /* Try to map an empty skb from the aggregation info */
312 mapping = dma_map_single(&bp->pdev->dev,
313 first_buf->skb->data,
314 fp->rx_buf_size, DMA_FROM_DEVICE);
316 * ...if it fails - move the skb from the consumer to the producer
317 * and set the current aggregation state as ERROR to drop it
318 * when TPA_STOP arrives.
321 if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
322 /* Move the BD from the consumer to the producer */
323 bnx2x_reuse_rx_skb(fp, cons, prod);
324 tpa_info->tpa_state = BNX2X_TPA_ERROR;
325 return;
328 /* move empty skb from pool to prod */
329 prod_rx_buf->skb = first_buf->skb;
330 dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
331 /* point prod_bd to new skb */
332 prod_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
333 prod_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
335 /* move partial skb from cons to pool (don't unmap yet) */
336 *first_buf = *cons_rx_buf;
338 /* mark bin state as START */
339 tpa_info->parsing_flags =
340 le16_to_cpu(cqe->pars_flags.flags);
341 tpa_info->vlan_tag = le16_to_cpu(cqe->vlan_tag);
342 tpa_info->tpa_state = BNX2X_TPA_START;
343 tpa_info->len_on_bd = le16_to_cpu(cqe->len_on_bd);
344 tpa_info->placement_offset = cqe->placement_offset;
346 #ifdef BNX2X_STOP_ON_ERROR
347 fp->tpa_queue_used |= (1 << queue);
348 #ifdef _ASM_GENERIC_INT_L64_H
349 DP(NETIF_MSG_RX_STATUS, "fp->tpa_queue_used = 0x%lx\n",
350 #else
351 DP(NETIF_MSG_RX_STATUS, "fp->tpa_queue_used = 0x%llx\n",
352 #endif
353 fp->tpa_queue_used);
354 #endif
357 /* Timestamp option length allowed for TPA aggregation:
359 * nop nop kind length echo val
361 #define TPA_TSTAMP_OPT_LEN 12
363 * bnx2x_set_lro_mss - calculate the approximate value of the MSS
365 * @bp: driver handle
366 * @parsing_flags: parsing flags from the START CQE
367 * @len_on_bd: total length of the first packet for the
368 * aggregation.
370 * Approximate value of the MSS for this aggregation calculated using
371 * the first packet of it.
373 static inline u16 bnx2x_set_lro_mss(struct bnx2x *bp, u16 parsing_flags,
374 u16 len_on_bd)
377 * TPA arrgregation won't have either IP options or TCP options
378 * other than timestamp or IPv6 extension headers.
380 u16 hdrs_len = ETH_HLEN + sizeof(struct tcphdr);
382 if (GET_FLAG(parsing_flags, PARSING_FLAGS_OVER_ETHERNET_PROTOCOL) ==
383 PRS_FLAG_OVERETH_IPV6)
384 hdrs_len += sizeof(struct ipv6hdr);
385 else /* IPv4 */
386 hdrs_len += sizeof(struct iphdr);
389 /* Check if there was a TCP timestamp, if there is it's will
390 * always be 12 bytes length: nop nop kind length echo val.
392 * Otherwise FW would close the aggregation.
394 if (parsing_flags & PARSING_FLAGS_TIME_STAMP_EXIST_FLAG)
395 hdrs_len += TPA_TSTAMP_OPT_LEN;
397 return len_on_bd - hdrs_len;
400 static int bnx2x_fill_frag_skb(struct bnx2x *bp, struct bnx2x_fastpath *fp,
401 u16 queue, struct sk_buff *skb,
402 struct eth_end_agg_rx_cqe *cqe,
403 u16 cqe_idx)
405 struct sw_rx_page *rx_pg, old_rx_pg;
406 u32 i, frag_len, frag_size, pages;
407 int err;
408 int j;
409 struct bnx2x_agg_info *tpa_info = &fp->tpa_info[queue];
410 u16 len_on_bd = tpa_info->len_on_bd;
412 frag_size = le16_to_cpu(cqe->pkt_len) - len_on_bd;
413 pages = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
415 /* This is needed in order to enable forwarding support */
416 if (frag_size)
417 skb_shinfo(skb)->gso_size = bnx2x_set_lro_mss(bp,
418 tpa_info->parsing_flags, len_on_bd);
420 #ifdef BNX2X_STOP_ON_ERROR
421 if (pages > min_t(u32, 8, MAX_SKB_FRAGS)*SGE_PAGE_SIZE*PAGES_PER_SGE) {
422 BNX2X_ERR("SGL length is too long: %d. CQE index is %d\n",
423 pages, cqe_idx);
424 BNX2X_ERR("cqe->pkt_len = %d\n", cqe->pkt_len);
425 bnx2x_panic();
426 return -EINVAL;
428 #endif
430 /* Run through the SGL and compose the fragmented skb */
431 for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
432 u16 sge_idx = RX_SGE(le16_to_cpu(cqe->sgl_or_raw_data.sgl[j]));
434 /* FW gives the indices of the SGE as if the ring is an array
435 (meaning that "next" element will consume 2 indices) */
436 frag_len = min(frag_size, (u32)(SGE_PAGE_SIZE*PAGES_PER_SGE));
437 rx_pg = &fp->rx_page_ring[sge_idx];
438 old_rx_pg = *rx_pg;
440 /* If we fail to allocate a substitute page, we simply stop
441 where we are and drop the whole packet */
442 err = bnx2x_alloc_rx_sge(bp, fp, sge_idx);
443 if (unlikely(err)) {
444 fp->eth_q_stats.rx_skb_alloc_failed++;
445 return err;
448 /* Unmap the page as we r going to pass it to the stack */
449 dma_unmap_page(&bp->pdev->dev,
450 dma_unmap_addr(&old_rx_pg, mapping),
451 SGE_PAGE_SIZE*PAGES_PER_SGE, DMA_FROM_DEVICE);
453 /* Add one frag and update the appropriate fields in the skb */
454 skb_fill_page_desc(skb, j, old_rx_pg.page, 0, frag_len);
456 skb->data_len += frag_len;
457 skb->truesize += frag_len;
458 skb->len += frag_len;
460 frag_size -= frag_len;
463 return 0;
466 static void bnx2x_tpa_stop(struct bnx2x *bp, struct bnx2x_fastpath *fp,
467 u16 queue, struct eth_end_agg_rx_cqe *cqe,
468 u16 cqe_idx)
470 struct bnx2x_agg_info *tpa_info = &fp->tpa_info[queue];
471 struct sw_rx_bd *rx_buf = &tpa_info->first_buf;
472 u8 pad = tpa_info->placement_offset;
473 u16 len = tpa_info->len_on_bd;
474 struct sk_buff *skb = rx_buf->skb;
475 /* alloc new skb */
476 struct sk_buff *new_skb;
477 u8 old_tpa_state = tpa_info->tpa_state;
479 tpa_info->tpa_state = BNX2X_TPA_STOP;
481 /* If we there was an error during the handling of the TPA_START -
482 * drop this aggregation.
484 if (old_tpa_state == BNX2X_TPA_ERROR)
485 goto drop;
487 /* Try to allocate the new skb */
488 new_skb = netdev_alloc_skb(bp->dev, fp->rx_buf_size);
490 /* Unmap skb in the pool anyway, as we are going to change
491 pool entry status to BNX2X_TPA_STOP even if new skb allocation
492 fails. */
493 dma_unmap_single(&bp->pdev->dev, dma_unmap_addr(rx_buf, mapping),
494 fp->rx_buf_size, DMA_FROM_DEVICE);
496 if (likely(new_skb)) {
497 prefetch(skb);
498 prefetch(((char *)(skb)) + L1_CACHE_BYTES);
500 #ifdef BNX2X_STOP_ON_ERROR
501 if (pad + len > fp->rx_buf_size) {
502 BNX2X_ERR("skb_put is about to fail... "
503 "pad %d len %d rx_buf_size %d\n",
504 pad, len, fp->rx_buf_size);
505 bnx2x_panic();
506 return;
508 #endif
510 skb_reserve(skb, pad);
511 skb_put(skb, len);
513 skb->protocol = eth_type_trans(skb, bp->dev);
514 skb->ip_summed = CHECKSUM_UNNECESSARY;
516 if (!bnx2x_fill_frag_skb(bp, fp, queue, skb, cqe, cqe_idx)) {
517 if (tpa_info->parsing_flags & PARSING_FLAGS_VLAN)
518 __vlan_hwaccel_put_tag(skb, tpa_info->vlan_tag);
519 napi_gro_receive(&fp->napi, skb);
520 } else {
521 DP(NETIF_MSG_RX_STATUS, "Failed to allocate new pages"
522 " - dropping packet!\n");
523 dev_kfree_skb_any(skb);
527 /* put new skb in bin */
528 rx_buf->skb = new_skb;
530 return;
533 drop:
534 /* drop the packet and keep the buffer in the bin */
535 DP(NETIF_MSG_RX_STATUS,
536 "Failed to allocate or map a new skb - dropping packet!\n");
537 fp->eth_q_stats.rx_skb_alloc_failed++;
540 /* Set Toeplitz hash value in the skb using the value from the
541 * CQE (calculated by HW).
543 static inline void bnx2x_set_skb_rxhash(struct bnx2x *bp, union eth_rx_cqe *cqe,
544 struct sk_buff *skb)
546 /* Set Toeplitz hash from CQE */
547 if ((bp->dev->features & NETIF_F_RXHASH) &&
548 (cqe->fast_path_cqe.status_flags &
549 ETH_FAST_PATH_RX_CQE_RSS_HASH_FLG))
550 skb->rxhash =
551 le32_to_cpu(cqe->fast_path_cqe.rss_hash_result);
554 int bnx2x_rx_int(struct bnx2x_fastpath *fp, int budget)
556 struct bnx2x *bp = fp->bp;
557 u16 bd_cons, bd_prod, bd_prod_fw, comp_ring_cons;
558 u16 hw_comp_cons, sw_comp_cons, sw_comp_prod;
559 int rx_pkt = 0;
561 #ifdef BNX2X_STOP_ON_ERROR
562 if (unlikely(bp->panic))
563 return 0;
564 #endif
566 /* CQ "next element" is of the size of the regular element,
567 that's why it's ok here */
568 hw_comp_cons = le16_to_cpu(*fp->rx_cons_sb);
569 if ((hw_comp_cons & MAX_RCQ_DESC_CNT) == MAX_RCQ_DESC_CNT)
570 hw_comp_cons++;
572 bd_cons = fp->rx_bd_cons;
573 bd_prod = fp->rx_bd_prod;
574 bd_prod_fw = bd_prod;
575 sw_comp_cons = fp->rx_comp_cons;
576 sw_comp_prod = fp->rx_comp_prod;
578 /* Memory barrier necessary as speculative reads of the rx
579 * buffer can be ahead of the index in the status block
581 rmb();
583 DP(NETIF_MSG_RX_STATUS,
584 "queue[%d]: hw_comp_cons %u sw_comp_cons %u\n",
585 fp->index, hw_comp_cons, sw_comp_cons);
587 while (sw_comp_cons != hw_comp_cons) {
588 struct sw_rx_bd *rx_buf = NULL;
589 struct sk_buff *skb;
590 union eth_rx_cqe *cqe;
591 struct eth_fast_path_rx_cqe *cqe_fp;
592 u8 cqe_fp_flags;
593 enum eth_rx_cqe_type cqe_fp_type;
594 u16 len, pad;
596 #ifdef BNX2X_STOP_ON_ERROR
597 if (unlikely(bp->panic))
598 return 0;
599 #endif
601 comp_ring_cons = RCQ_BD(sw_comp_cons);
602 bd_prod = RX_BD(bd_prod);
603 bd_cons = RX_BD(bd_cons);
605 /* Prefetch the page containing the BD descriptor
606 at producer's index. It will be needed when new skb is
607 allocated */
608 prefetch((void *)(PAGE_ALIGN((unsigned long)
609 (&fp->rx_desc_ring[bd_prod])) -
610 PAGE_SIZE + 1));
612 cqe = &fp->rx_comp_ring[comp_ring_cons];
613 cqe_fp = &cqe->fast_path_cqe;
614 cqe_fp_flags = cqe_fp->type_error_flags;
615 cqe_fp_type = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
617 DP(NETIF_MSG_RX_STATUS, "CQE type %x err %x status %x"
618 " queue %x vlan %x len %u\n", CQE_TYPE(cqe_fp_flags),
619 cqe_fp_flags, cqe_fp->status_flags,
620 le32_to_cpu(cqe_fp->rss_hash_result),
621 le16_to_cpu(cqe_fp->vlan_tag), le16_to_cpu(cqe_fp->pkt_len));
623 /* is this a slowpath msg? */
624 if (unlikely(CQE_TYPE_SLOW(cqe_fp_type))) {
625 bnx2x_sp_event(fp, cqe);
626 goto next_cqe;
628 /* this is an rx packet */
629 } else {
630 rx_buf = &fp->rx_buf_ring[bd_cons];
631 skb = rx_buf->skb;
632 prefetch(skb);
634 if (!CQE_TYPE_FAST(cqe_fp_type)) {
635 #ifdef BNX2X_STOP_ON_ERROR
636 /* sanity check */
637 if (fp->disable_tpa &&
638 (CQE_TYPE_START(cqe_fp_type) ||
639 CQE_TYPE_STOP(cqe_fp_type)))
640 BNX2X_ERR("START/STOP packet while "
641 "disable_tpa type %x\n",
642 CQE_TYPE(cqe_fp_type));
643 #endif
645 if (CQE_TYPE_START(cqe_fp_type)) {
646 u16 queue = cqe_fp->queue_index;
647 DP(NETIF_MSG_RX_STATUS,
648 "calling tpa_start on queue %d\n",
649 queue);
651 bnx2x_tpa_start(fp, queue, skb,
652 bd_cons, bd_prod,
653 cqe_fp);
655 /* Set Toeplitz hash for LRO skb */
656 bnx2x_set_skb_rxhash(bp, cqe, skb);
658 goto next_rx;
660 } else {
661 u16 queue =
662 cqe->end_agg_cqe.queue_index;
663 DP(NETIF_MSG_RX_STATUS,
664 "calling tpa_stop on queue %d\n",
665 queue);
667 bnx2x_tpa_stop(bp, fp, queue,
668 &cqe->end_agg_cqe,
669 comp_ring_cons);
670 #ifdef BNX2X_STOP_ON_ERROR
671 if (bp->panic)
672 return 0;
673 #endif
675 bnx2x_update_sge_prod(fp, cqe_fp);
676 goto next_cqe;
679 /* non TPA */
680 len = le16_to_cpu(cqe_fp->pkt_len);
681 pad = cqe_fp->placement_offset;
682 dma_sync_single_for_cpu(&bp->pdev->dev,
683 dma_unmap_addr(rx_buf, mapping),
684 pad + RX_COPY_THRESH,
685 DMA_FROM_DEVICE);
686 prefetch(((char *)(skb)) + L1_CACHE_BYTES);
688 /* is this an error packet? */
689 if (unlikely(cqe_fp_flags & ETH_RX_ERROR_FALGS)) {
690 DP(NETIF_MSG_RX_ERR,
691 "ERROR flags %x rx packet %u\n",
692 cqe_fp_flags, sw_comp_cons);
693 fp->eth_q_stats.rx_err_discard_pkt++;
694 goto reuse_rx;
697 /* Since we don't have a jumbo ring
698 * copy small packets if mtu > 1500
700 if ((bp->dev->mtu > ETH_MAX_PACKET_SIZE) &&
701 (len <= RX_COPY_THRESH)) {
702 struct sk_buff *new_skb;
704 new_skb = netdev_alloc_skb(bp->dev, len + pad);
705 if (new_skb == NULL) {
706 DP(NETIF_MSG_RX_ERR,
707 "ERROR packet dropped "
708 "because of alloc failure\n");
709 fp->eth_q_stats.rx_skb_alloc_failed++;
710 goto reuse_rx;
713 /* aligned copy */
714 skb_copy_from_linear_data_offset(skb, pad,
715 new_skb->data + pad, len);
716 skb_reserve(new_skb, pad);
717 skb_put(new_skb, len);
719 bnx2x_reuse_rx_skb(fp, bd_cons, bd_prod);
721 skb = new_skb;
723 } else
724 if (likely(bnx2x_alloc_rx_skb(bp, fp, bd_prod) == 0)) {
725 dma_unmap_single(&bp->pdev->dev,
726 dma_unmap_addr(rx_buf, mapping),
727 fp->rx_buf_size,
728 DMA_FROM_DEVICE);
729 skb_reserve(skb, pad);
730 skb_put(skb, len);
732 } else {
733 DP(NETIF_MSG_RX_ERR,
734 "ERROR packet dropped because "
735 "of alloc failure\n");
736 fp->eth_q_stats.rx_skb_alloc_failed++;
737 reuse_rx:
738 bnx2x_reuse_rx_skb(fp, bd_cons, bd_prod);
739 goto next_rx;
742 skb->protocol = eth_type_trans(skb, bp->dev);
744 /* Set Toeplitz hash for a none-LRO skb */
745 bnx2x_set_skb_rxhash(bp, cqe, skb);
747 skb_checksum_none_assert(skb);
749 if (bp->dev->features & NETIF_F_RXCSUM) {
751 if (likely(BNX2X_RX_CSUM_OK(cqe)))
752 skb->ip_summed = CHECKSUM_UNNECESSARY;
753 else
754 fp->eth_q_stats.hw_csum_err++;
758 skb_record_rx_queue(skb, fp->index);
760 if (le16_to_cpu(cqe_fp->pars_flags.flags) &
761 PARSING_FLAGS_VLAN)
762 __vlan_hwaccel_put_tag(skb,
763 le16_to_cpu(cqe_fp->vlan_tag));
764 napi_gro_receive(&fp->napi, skb);
767 next_rx:
768 rx_buf->skb = NULL;
770 bd_cons = NEXT_RX_IDX(bd_cons);
771 bd_prod = NEXT_RX_IDX(bd_prod);
772 bd_prod_fw = NEXT_RX_IDX(bd_prod_fw);
773 rx_pkt++;
774 next_cqe:
775 sw_comp_prod = NEXT_RCQ_IDX(sw_comp_prod);
776 sw_comp_cons = NEXT_RCQ_IDX(sw_comp_cons);
778 if (rx_pkt == budget)
779 break;
780 } /* while */
782 fp->rx_bd_cons = bd_cons;
783 fp->rx_bd_prod = bd_prod_fw;
784 fp->rx_comp_cons = sw_comp_cons;
785 fp->rx_comp_prod = sw_comp_prod;
787 /* Update producers */
788 bnx2x_update_rx_prod(bp, fp, bd_prod_fw, sw_comp_prod,
789 fp->rx_sge_prod);
791 fp->rx_pkt += rx_pkt;
792 fp->rx_calls++;
794 return rx_pkt;
797 static irqreturn_t bnx2x_msix_fp_int(int irq, void *fp_cookie)
799 struct bnx2x_fastpath *fp = fp_cookie;
800 struct bnx2x *bp = fp->bp;
801 u8 cos;
803 DP(BNX2X_MSG_FP, "got an MSI-X interrupt on IDX:SB "
804 "[fp %d fw_sd %d igusb %d]\n",
805 fp->index, fp->fw_sb_id, fp->igu_sb_id);
806 bnx2x_ack_sb(bp, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
808 #ifdef BNX2X_STOP_ON_ERROR
809 if (unlikely(bp->panic))
810 return IRQ_HANDLED;
811 #endif
813 /* Handle Rx and Tx according to MSI-X vector */
814 prefetch(fp->rx_cons_sb);
816 for_each_cos_in_tx_queue(fp, cos)
817 prefetch(fp->txdata[cos].tx_cons_sb);
819 prefetch(&fp->sb_running_index[SM_RX_ID]);
820 napi_schedule(&bnx2x_fp(bp, fp->index, napi));
822 return IRQ_HANDLED;
825 /* HW Lock for shared dual port PHYs */
826 void bnx2x_acquire_phy_lock(struct bnx2x *bp)
828 mutex_lock(&bp->port.phy_mutex);
830 if (bp->port.need_hw_lock)
831 bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_MDIO);
834 void bnx2x_release_phy_lock(struct bnx2x *bp)
836 if (bp->port.need_hw_lock)
837 bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_MDIO);
839 mutex_unlock(&bp->port.phy_mutex);
842 /* calculates MF speed according to current linespeed and MF configuration */
843 u16 bnx2x_get_mf_speed(struct bnx2x *bp)
845 u16 line_speed = bp->link_vars.line_speed;
846 if (IS_MF(bp)) {
847 u16 maxCfg = bnx2x_extract_max_cfg(bp,
848 bp->mf_config[BP_VN(bp)]);
850 /* Calculate the current MAX line speed limit for the MF
851 * devices
853 if (IS_MF_SI(bp))
854 line_speed = (line_speed * maxCfg) / 100;
855 else { /* SD mode */
856 u16 vn_max_rate = maxCfg * 100;
858 if (vn_max_rate < line_speed)
859 line_speed = vn_max_rate;
863 return line_speed;
867 * bnx2x_fill_report_data - fill link report data to report
869 * @bp: driver handle
870 * @data: link state to update
872 * It uses a none-atomic bit operations because is called under the mutex.
874 static inline void bnx2x_fill_report_data(struct bnx2x *bp,
875 struct bnx2x_link_report_data *data)
877 u16 line_speed = bnx2x_get_mf_speed(bp);
879 memset(data, 0, sizeof(*data));
881 /* Fill the report data: efective line speed */
882 data->line_speed = line_speed;
884 /* Link is down */
885 if (!bp->link_vars.link_up || (bp->flags & MF_FUNC_DIS))
886 __set_bit(BNX2X_LINK_REPORT_LINK_DOWN,
887 &data->link_report_flags);
889 /* Full DUPLEX */
890 if (bp->link_vars.duplex == DUPLEX_FULL)
891 __set_bit(BNX2X_LINK_REPORT_FD, &data->link_report_flags);
893 /* Rx Flow Control is ON */
894 if (bp->link_vars.flow_ctrl & BNX2X_FLOW_CTRL_RX)
895 __set_bit(BNX2X_LINK_REPORT_RX_FC_ON, &data->link_report_flags);
897 /* Tx Flow Control is ON */
898 if (bp->link_vars.flow_ctrl & BNX2X_FLOW_CTRL_TX)
899 __set_bit(BNX2X_LINK_REPORT_TX_FC_ON, &data->link_report_flags);
903 * bnx2x_link_report - report link status to OS.
905 * @bp: driver handle
907 * Calls the __bnx2x_link_report() under the same locking scheme
908 * as a link/PHY state managing code to ensure a consistent link
909 * reporting.
912 void bnx2x_link_report(struct bnx2x *bp)
914 bnx2x_acquire_phy_lock(bp);
915 __bnx2x_link_report(bp);
916 bnx2x_release_phy_lock(bp);
920 * __bnx2x_link_report - report link status to OS.
922 * @bp: driver handle
924 * None atomic inmlementation.
925 * Should be called under the phy_lock.
927 void __bnx2x_link_report(struct bnx2x *bp)
929 struct bnx2x_link_report_data cur_data;
931 /* reread mf_cfg */
932 if (!CHIP_IS_E1(bp))
933 bnx2x_read_mf_cfg(bp);
935 /* Read the current link report info */
936 bnx2x_fill_report_data(bp, &cur_data);
938 /* Don't report link down or exactly the same link status twice */
939 if (!memcmp(&cur_data, &bp->last_reported_link, sizeof(cur_data)) ||
940 (test_bit(BNX2X_LINK_REPORT_LINK_DOWN,
941 &bp->last_reported_link.link_report_flags) &&
942 test_bit(BNX2X_LINK_REPORT_LINK_DOWN,
943 &cur_data.link_report_flags)))
944 return;
946 bp->link_cnt++;
948 /* We are going to report a new link parameters now -
949 * remember the current data for the next time.
951 memcpy(&bp->last_reported_link, &cur_data, sizeof(cur_data));
953 if (test_bit(BNX2X_LINK_REPORT_LINK_DOWN,
954 &cur_data.link_report_flags)) {
955 netif_carrier_off(bp->dev);
956 netdev_err(bp->dev, "NIC Link is Down\n");
957 return;
958 } else {
959 const char *duplex;
960 const char *flow;
962 netif_carrier_on(bp->dev);
964 if (test_and_clear_bit(BNX2X_LINK_REPORT_FD,
965 &cur_data.link_report_flags))
966 duplex = "full";
967 else
968 duplex = "half";
970 /* Handle the FC at the end so that only these flags would be
971 * possibly set. This way we may easily check if there is no FC
972 * enabled.
974 if (cur_data.link_report_flags) {
975 if (test_bit(BNX2X_LINK_REPORT_RX_FC_ON,
976 &cur_data.link_report_flags)) {
977 if (test_bit(BNX2X_LINK_REPORT_TX_FC_ON,
978 &cur_data.link_report_flags))
979 flow = "ON - receive & transmit";
980 else
981 flow = "ON - receive";
982 } else {
983 flow = "ON - transmit";
985 } else {
986 flow = "none";
988 netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
989 cur_data.line_speed, duplex, flow);
993 void bnx2x_init_rx_rings(struct bnx2x *bp)
995 int func = BP_FUNC(bp);
996 int max_agg_queues = CHIP_IS_E1(bp) ? ETH_MAX_AGGREGATION_QUEUES_E1 :
997 ETH_MAX_AGGREGATION_QUEUES_E1H_E2;
998 u16 ring_prod;
999 int i, j;
1001 /* Allocate TPA resources */
1002 for_each_rx_queue(bp, j) {
1003 struct bnx2x_fastpath *fp = &bp->fp[j];
1005 DP(NETIF_MSG_IFUP,
1006 "mtu %d rx_buf_size %d\n", bp->dev->mtu, fp->rx_buf_size);
1008 if (!fp->disable_tpa) {
1009 /* Fill the per-aggregtion pool */
1010 for (i = 0; i < max_agg_queues; i++) {
1011 struct bnx2x_agg_info *tpa_info =
1012 &fp->tpa_info[i];
1013 struct sw_rx_bd *first_buf =
1014 &tpa_info->first_buf;
1016 first_buf->skb = netdev_alloc_skb(bp->dev,
1017 fp->rx_buf_size);
1018 if (!first_buf->skb) {
1019 BNX2X_ERR("Failed to allocate TPA "
1020 "skb pool for queue[%d] - "
1021 "disabling TPA on this "
1022 "queue!\n", j);
1023 bnx2x_free_tpa_pool(bp, fp, i);
1024 fp->disable_tpa = 1;
1025 break;
1027 dma_unmap_addr_set(first_buf, mapping, 0);
1028 tpa_info->tpa_state = BNX2X_TPA_STOP;
1031 /* "next page" elements initialization */
1032 bnx2x_set_next_page_sgl(fp);
1034 /* set SGEs bit mask */
1035 bnx2x_init_sge_ring_bit_mask(fp);
1037 /* Allocate SGEs and initialize the ring elements */
1038 for (i = 0, ring_prod = 0;
1039 i < MAX_RX_SGE_CNT*NUM_RX_SGE_PAGES; i++) {
1041 if (bnx2x_alloc_rx_sge(bp, fp, ring_prod) < 0) {
1042 BNX2X_ERR("was only able to allocate "
1043 "%d rx sges\n", i);
1044 BNX2X_ERR("disabling TPA for "
1045 "queue[%d]\n", j);
1046 /* Cleanup already allocated elements */
1047 bnx2x_free_rx_sge_range(bp, fp,
1048 ring_prod);
1049 bnx2x_free_tpa_pool(bp, fp,
1050 max_agg_queues);
1051 fp->disable_tpa = 1;
1052 ring_prod = 0;
1053 break;
1055 ring_prod = NEXT_SGE_IDX(ring_prod);
1058 fp->rx_sge_prod = ring_prod;
1062 for_each_rx_queue(bp, j) {
1063 struct bnx2x_fastpath *fp = &bp->fp[j];
1065 fp->rx_bd_cons = 0;
1067 /* Activate BD ring */
1068 /* Warning!
1069 * this will generate an interrupt (to the TSTORM)
1070 * must only be done after chip is initialized
1072 bnx2x_update_rx_prod(bp, fp, fp->rx_bd_prod, fp->rx_comp_prod,
1073 fp->rx_sge_prod);
1075 if (j != 0)
1076 continue;
1078 if (CHIP_IS_E1(bp)) {
1079 REG_WR(bp, BAR_USTRORM_INTMEM +
1080 USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(func),
1081 U64_LO(fp->rx_comp_mapping));
1082 REG_WR(bp, BAR_USTRORM_INTMEM +
1083 USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(func) + 4,
1084 U64_HI(fp->rx_comp_mapping));
1089 static void bnx2x_free_tx_skbs(struct bnx2x *bp)
1091 int i;
1092 u8 cos;
1094 for_each_tx_queue(bp, i) {
1095 struct bnx2x_fastpath *fp = &bp->fp[i];
1096 for_each_cos_in_tx_queue(fp, cos) {
1097 struct bnx2x_fp_txdata *txdata = &fp->txdata[cos];
1099 u16 bd_cons = txdata->tx_bd_cons;
1100 u16 sw_prod = txdata->tx_pkt_prod;
1101 u16 sw_cons = txdata->tx_pkt_cons;
1103 while (sw_cons != sw_prod) {
1104 bd_cons = bnx2x_free_tx_pkt(bp, txdata,
1105 TX_BD(sw_cons));
1106 sw_cons++;
1112 static void bnx2x_free_rx_bds(struct bnx2x_fastpath *fp)
1114 struct bnx2x *bp = fp->bp;
1115 int i;
1117 /* ring wasn't allocated */
1118 if (fp->rx_buf_ring == NULL)
1119 return;
1121 for (i = 0; i < NUM_RX_BD; i++) {
1122 struct sw_rx_bd *rx_buf = &fp->rx_buf_ring[i];
1123 struct sk_buff *skb = rx_buf->skb;
1125 if (skb == NULL)
1126 continue;
1127 dma_unmap_single(&bp->pdev->dev,
1128 dma_unmap_addr(rx_buf, mapping),
1129 fp->rx_buf_size, DMA_FROM_DEVICE);
1131 rx_buf->skb = NULL;
1132 dev_kfree_skb(skb);
1136 static void bnx2x_free_rx_skbs(struct bnx2x *bp)
1138 int j;
1140 for_each_rx_queue(bp, j) {
1141 struct bnx2x_fastpath *fp = &bp->fp[j];
1143 bnx2x_free_rx_bds(fp);
1145 if (!fp->disable_tpa)
1146 bnx2x_free_tpa_pool(bp, fp, CHIP_IS_E1(bp) ?
1147 ETH_MAX_AGGREGATION_QUEUES_E1 :
1148 ETH_MAX_AGGREGATION_QUEUES_E1H_E2);
1152 void bnx2x_free_skbs(struct bnx2x *bp)
1154 bnx2x_free_tx_skbs(bp);
1155 bnx2x_free_rx_skbs(bp);
1158 void bnx2x_update_max_mf_config(struct bnx2x *bp, u32 value)
1160 /* load old values */
1161 u32 mf_cfg = bp->mf_config[BP_VN(bp)];
1163 if (value != bnx2x_extract_max_cfg(bp, mf_cfg)) {
1164 /* leave all but MAX value */
1165 mf_cfg &= ~FUNC_MF_CFG_MAX_BW_MASK;
1167 /* set new MAX value */
1168 mf_cfg |= (value << FUNC_MF_CFG_MAX_BW_SHIFT)
1169 & FUNC_MF_CFG_MAX_BW_MASK;
1171 bnx2x_fw_command(bp, DRV_MSG_CODE_SET_MF_BW, mf_cfg);
1176 * bnx2x_free_msix_irqs - free previously requested MSI-X IRQ vectors
1178 * @bp: driver handle
1179 * @nvecs: number of vectors to be released
1181 static void bnx2x_free_msix_irqs(struct bnx2x *bp, int nvecs)
1183 int i, offset = 0;
1185 if (nvecs == offset)
1186 return;
1187 free_irq(bp->msix_table[offset].vector, bp->dev);
1188 DP(NETIF_MSG_IFDOWN, "released sp irq (%d)\n",
1189 bp->msix_table[offset].vector);
1190 offset++;
1191 #ifdef BCM_CNIC
1192 if (nvecs == offset)
1193 return;
1194 offset++;
1195 #endif
1197 for_each_eth_queue(bp, i) {
1198 if (nvecs == offset)
1199 return;
1200 DP(NETIF_MSG_IFDOWN, "about to release fp #%d->%d "
1201 "irq\n", i, bp->msix_table[offset].vector);
1203 free_irq(bp->msix_table[offset++].vector, &bp->fp[i]);
1207 void bnx2x_free_irq(struct bnx2x *bp)
1209 if (bp->flags & USING_MSIX_FLAG)
1210 bnx2x_free_msix_irqs(bp, BNX2X_NUM_ETH_QUEUES(bp) +
1211 CNIC_PRESENT + 1);
1212 else if (bp->flags & USING_MSI_FLAG)
1213 free_irq(bp->pdev->irq, bp->dev);
1214 else
1215 free_irq(bp->pdev->irq, bp->dev);
1218 int bnx2x_enable_msix(struct bnx2x *bp)
1220 int msix_vec = 0, i, rc, req_cnt;
1222 bp->msix_table[msix_vec].entry = msix_vec;
1223 DP(NETIF_MSG_IFUP, "msix_table[0].entry = %d (slowpath)\n",
1224 bp->msix_table[0].entry);
1225 msix_vec++;
1227 #ifdef BCM_CNIC
1228 bp->msix_table[msix_vec].entry = msix_vec;
1229 DP(NETIF_MSG_IFUP, "msix_table[%d].entry = %d (CNIC)\n",
1230 bp->msix_table[msix_vec].entry, bp->msix_table[msix_vec].entry);
1231 msix_vec++;
1232 #endif
1233 /* We need separate vectors for ETH queues only (not FCoE) */
1234 for_each_eth_queue(bp, i) {
1235 bp->msix_table[msix_vec].entry = msix_vec;
1236 DP(NETIF_MSG_IFUP, "msix_table[%d].entry = %d "
1237 "(fastpath #%u)\n", msix_vec, msix_vec, i);
1238 msix_vec++;
1241 req_cnt = BNX2X_NUM_ETH_QUEUES(bp) + CNIC_PRESENT + 1;
1243 rc = pci_enable_msix(bp->pdev, &bp->msix_table[0], req_cnt);
1246 * reconfigure number of tx/rx queues according to available
1247 * MSI-X vectors
1249 if (rc >= BNX2X_MIN_MSIX_VEC_CNT) {
1250 /* how less vectors we will have? */
1251 int diff = req_cnt - rc;
1253 DP(NETIF_MSG_IFUP,
1254 "Trying to use less MSI-X vectors: %d\n", rc);
1256 rc = pci_enable_msix(bp->pdev, &bp->msix_table[0], rc);
1258 if (rc) {
1259 DP(NETIF_MSG_IFUP,
1260 "MSI-X is not attainable rc %d\n", rc);
1261 return rc;
1264 * decrease number of queues by number of unallocated entries
1266 bp->num_queues -= diff;
1268 DP(NETIF_MSG_IFUP, "New queue configuration set: %d\n",
1269 bp->num_queues);
1270 } else if (rc) {
1271 /* fall to INTx if not enough memory */
1272 if (rc == -ENOMEM)
1273 bp->flags |= DISABLE_MSI_FLAG;
1274 DP(NETIF_MSG_IFUP, "MSI-X is not attainable rc %d\n", rc);
1275 return rc;
1278 bp->flags |= USING_MSIX_FLAG;
1280 return 0;
1283 static int bnx2x_req_msix_irqs(struct bnx2x *bp)
1285 int i, rc, offset = 0;
1287 rc = request_irq(bp->msix_table[offset++].vector,
1288 bnx2x_msix_sp_int, 0,
1289 bp->dev->name, bp->dev);
1290 if (rc) {
1291 BNX2X_ERR("request sp irq failed\n");
1292 return -EBUSY;
1295 #ifdef BCM_CNIC
1296 offset++;
1297 #endif
1298 for_each_eth_queue(bp, i) {
1299 struct bnx2x_fastpath *fp = &bp->fp[i];
1300 snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
1301 bp->dev->name, i);
1303 rc = request_irq(bp->msix_table[offset].vector,
1304 bnx2x_msix_fp_int, 0, fp->name, fp);
1305 if (rc) {
1306 BNX2X_ERR("request fp #%d irq (%d) failed rc %d\n", i,
1307 bp->msix_table[offset].vector, rc);
1308 bnx2x_free_msix_irqs(bp, offset);
1309 return -EBUSY;
1312 offset++;
1315 i = BNX2X_NUM_ETH_QUEUES(bp);
1316 offset = 1 + CNIC_PRESENT;
1317 netdev_info(bp->dev, "using MSI-X IRQs: sp %d fp[%d] %d"
1318 " ... fp[%d] %d\n",
1319 bp->msix_table[0].vector,
1320 0, bp->msix_table[offset].vector,
1321 i - 1, bp->msix_table[offset + i - 1].vector);
1323 return 0;
1326 int bnx2x_enable_msi(struct bnx2x *bp)
1328 int rc;
1330 rc = pci_enable_msi(bp->pdev);
1331 if (rc) {
1332 DP(NETIF_MSG_IFUP, "MSI is not attainable\n");
1333 return -1;
1335 bp->flags |= USING_MSI_FLAG;
1337 return 0;
1340 static int bnx2x_req_irq(struct bnx2x *bp)
1342 unsigned long flags;
1343 int rc;
1345 if (bp->flags & USING_MSI_FLAG)
1346 flags = 0;
1347 else
1348 flags = IRQF_SHARED;
1350 rc = request_irq(bp->pdev->irq, bnx2x_interrupt, flags,
1351 bp->dev->name, bp->dev);
1352 return rc;
1355 static inline int bnx2x_setup_irqs(struct bnx2x *bp)
1357 int rc = 0;
1358 if (bp->flags & USING_MSIX_FLAG) {
1359 rc = bnx2x_req_msix_irqs(bp);
1360 if (rc)
1361 return rc;
1362 } else {
1363 bnx2x_ack_int(bp);
1364 rc = bnx2x_req_irq(bp);
1365 if (rc) {
1366 BNX2X_ERR("IRQ request failed rc %d, aborting\n", rc);
1367 return rc;
1369 if (bp->flags & USING_MSI_FLAG) {
1370 bp->dev->irq = bp->pdev->irq;
1371 netdev_info(bp->dev, "using MSI IRQ %d\n",
1372 bp->pdev->irq);
1376 return 0;
1379 static inline void bnx2x_napi_enable(struct bnx2x *bp)
1381 int i;
1383 for_each_rx_queue(bp, i)
1384 napi_enable(&bnx2x_fp(bp, i, napi));
1387 static inline void bnx2x_napi_disable(struct bnx2x *bp)
1389 int i;
1391 for_each_rx_queue(bp, i)
1392 napi_disable(&bnx2x_fp(bp, i, napi));
1395 void bnx2x_netif_start(struct bnx2x *bp)
1397 if (netif_running(bp->dev)) {
1398 bnx2x_napi_enable(bp);
1399 bnx2x_int_enable(bp);
1400 if (bp->state == BNX2X_STATE_OPEN)
1401 netif_tx_wake_all_queues(bp->dev);
1405 void bnx2x_netif_stop(struct bnx2x *bp, int disable_hw)
1407 bnx2x_int_disable_sync(bp, disable_hw);
1408 bnx2x_napi_disable(bp);
1411 u16 bnx2x_select_queue(struct net_device *dev, struct sk_buff *skb)
1413 struct bnx2x *bp = netdev_priv(dev);
1415 #ifdef BCM_CNIC
1416 if (!NO_FCOE(bp)) {
1417 struct ethhdr *hdr = (struct ethhdr *)skb->data;
1418 u16 ether_type = ntohs(hdr->h_proto);
1420 /* Skip VLAN tag if present */
1421 if (ether_type == ETH_P_8021Q) {
1422 struct vlan_ethhdr *vhdr =
1423 (struct vlan_ethhdr *)skb->data;
1425 ether_type = ntohs(vhdr->h_vlan_encapsulated_proto);
1428 /* If ethertype is FCoE or FIP - use FCoE ring */
1429 if ((ether_type == ETH_P_FCOE) || (ether_type == ETH_P_FIP))
1430 return bnx2x_fcoe_tx(bp, txq_index);
1432 #endif
1433 /* select a non-FCoE queue */
1434 return __skb_tx_hash(dev, skb, BNX2X_NUM_ETH_QUEUES(bp));
1437 void bnx2x_set_num_queues(struct bnx2x *bp)
1439 switch (bp->multi_mode) {
1440 case ETH_RSS_MODE_DISABLED:
1441 bp->num_queues = 1;
1442 break;
1443 case ETH_RSS_MODE_REGULAR:
1444 bp->num_queues = bnx2x_calc_num_queues(bp);
1445 break;
1447 default:
1448 bp->num_queues = 1;
1449 break;
1452 /* Add special queues */
1453 bp->num_queues += NON_ETH_CONTEXT_USE;
1457 * bnx2x_set_real_num_queues - configure netdev->real_num_[tx,rx]_queues
1459 * @bp: Driver handle
1461 * We currently support for at most 16 Tx queues for each CoS thus we will
1462 * allocate a multiple of 16 for ETH L2 rings according to the value of the
1463 * bp->max_cos.
1465 * If there is an FCoE L2 queue the appropriate Tx queue will have the next
1466 * index after all ETH L2 indices.
1468 * If the actual number of Tx queues (for each CoS) is less than 16 then there
1469 * will be the holes at the end of each group of 16 ETh L2 indices (0..15,
1470 * 16..31,...) with indicies that are not coupled with any real Tx queue.
1472 * The proper configuration of skb->queue_mapping is handled by
1473 * bnx2x_select_queue() and __skb_tx_hash().
1475 * bnx2x_setup_tc() takes care of the proper TC mappings so that __skb_tx_hash()
1476 * will return a proper Tx index if TC is enabled (netdev->num_tc > 0).
1478 static inline int bnx2x_set_real_num_queues(struct bnx2x *bp)
1480 int rc, tx, rx;
1482 tx = MAX_TXQS_PER_COS * bp->max_cos;
1483 rx = BNX2X_NUM_ETH_QUEUES(bp);
1485 /* account for fcoe queue */
1486 #ifdef BCM_CNIC
1487 if (!NO_FCOE(bp)) {
1488 rx += FCOE_PRESENT;
1489 tx += FCOE_PRESENT;
1491 #endif
1493 rc = netif_set_real_num_tx_queues(bp->dev, tx);
1494 if (rc) {
1495 BNX2X_ERR("Failed to set real number of Tx queues: %d\n", rc);
1496 return rc;
1498 rc = netif_set_real_num_rx_queues(bp->dev, rx);
1499 if (rc) {
1500 BNX2X_ERR("Failed to set real number of Rx queues: %d\n", rc);
1501 return rc;
1504 DP(NETIF_MSG_DRV, "Setting real num queues to (tx, rx) (%d, %d)\n",
1505 tx, rx);
1507 return rc;
1510 static inline void bnx2x_set_rx_buf_size(struct bnx2x *bp)
1512 int i;
1514 for_each_queue(bp, i) {
1515 struct bnx2x_fastpath *fp = &bp->fp[i];
1517 /* Always use a mini-jumbo MTU for the FCoE L2 ring */
1518 if (IS_FCOE_IDX(i))
1520 * Although there are no IP frames expected to arrive to
1521 * this ring we still want to add an
1522 * IP_HEADER_ALIGNMENT_PADDING to prevent a buffer
1523 * overrun attack.
1525 fp->rx_buf_size =
1526 BNX2X_FCOE_MINI_JUMBO_MTU + ETH_OVREHEAD +
1527 BNX2X_FW_RX_ALIGN + IP_HEADER_ALIGNMENT_PADDING;
1528 else
1529 fp->rx_buf_size =
1530 bp->dev->mtu + ETH_OVREHEAD +
1531 BNX2X_FW_RX_ALIGN + IP_HEADER_ALIGNMENT_PADDING;
1535 static inline int bnx2x_init_rss_pf(struct bnx2x *bp)
1537 int i;
1538 u8 ind_table[T_ETH_INDIRECTION_TABLE_SIZE] = {0};
1539 u8 num_eth_queues = BNX2X_NUM_ETH_QUEUES(bp);
1542 * Prepare the inital contents fo the indirection table if RSS is
1543 * enabled
1545 if (bp->multi_mode != ETH_RSS_MODE_DISABLED) {
1546 for (i = 0; i < sizeof(ind_table); i++)
1547 ind_table[i] =
1548 bp->fp->cl_id + (i % num_eth_queues);
1552 * For 57710 and 57711 SEARCHER configuration (rss_keys) is
1553 * per-port, so if explicit configuration is needed , do it only
1554 * for a PMF.
1556 * For 57712 and newer on the other hand it's a per-function
1557 * configuration.
1559 return bnx2x_config_rss_pf(bp, ind_table,
1560 bp->port.pmf || !CHIP_IS_E1x(bp));
1563 int bnx2x_config_rss_pf(struct bnx2x *bp, u8 *ind_table, bool config_hash)
1565 struct bnx2x_config_rss_params params = {0};
1566 int i;
1568 /* Although RSS is meaningless when there is a single HW queue we
1569 * still need it enabled in order to have HW Rx hash generated.
1571 * if (!is_eth_multi(bp))
1572 * bp->multi_mode = ETH_RSS_MODE_DISABLED;
1575 params.rss_obj = &bp->rss_conf_obj;
1577 __set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
1579 /* RSS mode */
1580 switch (bp->multi_mode) {
1581 case ETH_RSS_MODE_DISABLED:
1582 __set_bit(BNX2X_RSS_MODE_DISABLED, &params.rss_flags);
1583 break;
1584 case ETH_RSS_MODE_REGULAR:
1585 __set_bit(BNX2X_RSS_MODE_REGULAR, &params.rss_flags);
1586 break;
1587 case ETH_RSS_MODE_VLAN_PRI:
1588 __set_bit(BNX2X_RSS_MODE_VLAN_PRI, &params.rss_flags);
1589 break;
1590 case ETH_RSS_MODE_E1HOV_PRI:
1591 __set_bit(BNX2X_RSS_MODE_E1HOV_PRI, &params.rss_flags);
1592 break;
1593 case ETH_RSS_MODE_IP_DSCP:
1594 __set_bit(BNX2X_RSS_MODE_IP_DSCP, &params.rss_flags);
1595 break;
1596 default:
1597 BNX2X_ERR("Unknown multi_mode: %d\n", bp->multi_mode);
1598 return -EINVAL;
1601 /* If RSS is enabled */
1602 if (bp->multi_mode != ETH_RSS_MODE_DISABLED) {
1603 /* RSS configuration */
1604 __set_bit(BNX2X_RSS_IPV4, &params.rss_flags);
1605 __set_bit(BNX2X_RSS_IPV4_TCP, &params.rss_flags);
1606 __set_bit(BNX2X_RSS_IPV6, &params.rss_flags);
1607 __set_bit(BNX2X_RSS_IPV6_TCP, &params.rss_flags);
1609 /* Hash bits */
1610 params.rss_result_mask = MULTI_MASK;
1612 memcpy(params.ind_table, ind_table, sizeof(params.ind_table));
1614 if (config_hash) {
1615 /* RSS keys */
1616 for (i = 0; i < sizeof(params.rss_key) / 4; i++)
1617 params.rss_key[i] = random32();
1619 __set_bit(BNX2X_RSS_SET_SRCH, &params.rss_flags);
1623 return bnx2x_config_rss(bp, &params);
1626 static inline int bnx2x_init_hw(struct bnx2x *bp, u32 load_code)
1628 struct bnx2x_func_state_params func_params = {0};
1630 /* Prepare parameters for function state transitions */
1631 __set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
1633 func_params.f_obj = &bp->func_obj;
1634 func_params.cmd = BNX2X_F_CMD_HW_INIT;
1636 func_params.params.hw_init.load_phase = load_code;
1638 return bnx2x_func_state_change(bp, &func_params);
1642 * Cleans the object that have internal lists without sending
1643 * ramrods. Should be run when interrutps are disabled.
1645 static void bnx2x_squeeze_objects(struct bnx2x *bp)
1647 int rc;
1648 unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
1649 struct bnx2x_mcast_ramrod_params rparam = {0};
1650 struct bnx2x_vlan_mac_obj *mac_obj = &bp->fp->mac_obj;
1652 /***************** Cleanup MACs' object first *************************/
1654 /* Wait for completion of requested */
1655 __set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
1656 /* Perform a dry cleanup */
1657 __set_bit(RAMROD_DRV_CLR_ONLY, &ramrod_flags);
1659 /* Clean ETH primary MAC */
1660 __set_bit(BNX2X_ETH_MAC, &vlan_mac_flags);
1661 rc = mac_obj->delete_all(bp, &bp->fp->mac_obj, &vlan_mac_flags,
1662 &ramrod_flags);
1663 if (rc != 0)
1664 BNX2X_ERR("Failed to clean ETH MACs: %d\n", rc);
1666 /* Cleanup UC list */
1667 vlan_mac_flags = 0;
1668 __set_bit(BNX2X_UC_LIST_MAC, &vlan_mac_flags);
1669 rc = mac_obj->delete_all(bp, mac_obj, &vlan_mac_flags,
1670 &ramrod_flags);
1671 if (rc != 0)
1672 BNX2X_ERR("Failed to clean UC list MACs: %d\n", rc);
1674 /***************** Now clean mcast object *****************************/
1675 rparam.mcast_obj = &bp->mcast_obj;
1676 __set_bit(RAMROD_DRV_CLR_ONLY, &rparam.ramrod_flags);
1678 /* Add a DEL command... */
1679 rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_DEL);
1680 if (rc < 0)
1681 BNX2X_ERR("Failed to add a new DEL command to a multi-cast "
1682 "object: %d\n", rc);
1684 /* ...and wait until all pending commands are cleared */
1685 rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_CONT);
1686 while (rc != 0) {
1687 if (rc < 0) {
1688 BNX2X_ERR("Failed to clean multi-cast object: %d\n",
1689 rc);
1690 return;
1693 rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_CONT);
1697 #ifndef BNX2X_STOP_ON_ERROR
1698 #define LOAD_ERROR_EXIT(bp, label) \
1699 do { \
1700 (bp)->state = BNX2X_STATE_ERROR; \
1701 goto label; \
1702 } while (0)
1703 #else
1704 #define LOAD_ERROR_EXIT(bp, label) \
1705 do { \
1706 (bp)->state = BNX2X_STATE_ERROR; \
1707 (bp)->panic = 1; \
1708 return -EBUSY; \
1709 } while (0)
1710 #endif
1712 /* must be called with rtnl_lock */
1713 int bnx2x_nic_load(struct bnx2x *bp, int load_mode)
1715 int port = BP_PORT(bp);
1716 u32 load_code;
1717 int i, rc;
1719 #ifdef BNX2X_STOP_ON_ERROR
1720 if (unlikely(bp->panic))
1721 return -EPERM;
1722 #endif
1724 bp->state = BNX2X_STATE_OPENING_WAIT4_LOAD;
1726 /* Set the initial link reported state to link down */
1727 bnx2x_acquire_phy_lock(bp);
1728 memset(&bp->last_reported_link, 0, sizeof(bp->last_reported_link));
1729 __set_bit(BNX2X_LINK_REPORT_LINK_DOWN,
1730 &bp->last_reported_link.link_report_flags);
1731 bnx2x_release_phy_lock(bp);
1733 /* must be called before memory allocation and HW init */
1734 bnx2x_ilt_set_info(bp);
1737 * Zero fastpath structures preserving invariants like napi, which are
1738 * allocated only once, fp index, max_cos, bp pointer.
1739 * Also set fp->disable_tpa.
1741 for_each_queue(bp, i)
1742 bnx2x_bz_fp(bp, i);
1745 /* Set the receive queues buffer size */
1746 bnx2x_set_rx_buf_size(bp);
1748 if (bnx2x_alloc_mem(bp))
1749 return -ENOMEM;
1751 /* As long as bnx2x_alloc_mem() may possibly update
1752 * bp->num_queues, bnx2x_set_real_num_queues() should always
1753 * come after it.
1755 rc = bnx2x_set_real_num_queues(bp);
1756 if (rc) {
1757 BNX2X_ERR("Unable to set real_num_queues\n");
1758 LOAD_ERROR_EXIT(bp, load_error0);
1761 /* configure multi cos mappings in kernel.
1762 * this configuration may be overriden by a multi class queue discipline
1763 * or by a dcbx negotiation result.
1765 bnx2x_setup_tc(bp->dev, bp->max_cos);
1767 bnx2x_napi_enable(bp);
1769 /* Send LOAD_REQUEST command to MCP
1770 * Returns the type of LOAD command:
1771 * if it is the first port to be initialized
1772 * common blocks should be initialized, otherwise - not
1774 if (!BP_NOMCP(bp)) {
1775 load_code = bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_REQ, 0);
1776 if (!load_code) {
1777 BNX2X_ERR("MCP response failure, aborting\n");
1778 rc = -EBUSY;
1779 LOAD_ERROR_EXIT(bp, load_error1);
1781 if (load_code == FW_MSG_CODE_DRV_LOAD_REFUSED) {
1782 rc = -EBUSY; /* other port in diagnostic mode */
1783 LOAD_ERROR_EXIT(bp, load_error1);
1786 } else {
1787 int path = BP_PATH(bp);
1789 DP(NETIF_MSG_IFUP, "NO MCP - load counts[%d] %d, %d, %d\n",
1790 path, load_count[path][0], load_count[path][1],
1791 load_count[path][2]);
1792 load_count[path][0]++;
1793 load_count[path][1 + port]++;
1794 DP(NETIF_MSG_IFUP, "NO MCP - new load counts[%d] %d, %d, %d\n",
1795 path, load_count[path][0], load_count[path][1],
1796 load_count[path][2]);
1797 if (load_count[path][0] == 1)
1798 load_code = FW_MSG_CODE_DRV_LOAD_COMMON;
1799 else if (load_count[path][1 + port] == 1)
1800 load_code = FW_MSG_CODE_DRV_LOAD_PORT;
1801 else
1802 load_code = FW_MSG_CODE_DRV_LOAD_FUNCTION;
1805 if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
1806 (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) ||
1807 (load_code == FW_MSG_CODE_DRV_LOAD_PORT)) {
1808 bp->port.pmf = 1;
1810 * We need the barrier to ensure the ordering between the
1811 * writing to bp->port.pmf here and reading it from the
1812 * bnx2x_periodic_task().
1814 smp_mb();
1815 queue_delayed_work(bnx2x_wq, &bp->period_task, 0);
1816 } else
1817 bp->port.pmf = 0;
1819 DP(NETIF_MSG_LINK, "pmf %d\n", bp->port.pmf);
1821 /* Init Function state controlling object */
1822 bnx2x__init_func_obj(bp);
1824 /* Initialize HW */
1825 rc = bnx2x_init_hw(bp, load_code);
1826 if (rc) {
1827 BNX2X_ERR("HW init failed, aborting\n");
1828 bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
1829 LOAD_ERROR_EXIT(bp, load_error2);
1832 /* Connect to IRQs */
1833 rc = bnx2x_setup_irqs(bp);
1834 if (rc) {
1835 bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
1836 LOAD_ERROR_EXIT(bp, load_error2);
1839 /* Setup NIC internals and enable interrupts */
1840 bnx2x_nic_init(bp, load_code);
1842 /* Init per-function objects */
1843 bnx2x_init_bp_objs(bp);
1845 if (((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
1846 (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP)) &&
1847 (bp->common.shmem2_base)) {
1848 if (SHMEM2_HAS(bp, dcc_support))
1849 SHMEM2_WR(bp, dcc_support,
1850 (SHMEM_DCC_SUPPORT_DISABLE_ENABLE_PF_TLV |
1851 SHMEM_DCC_SUPPORT_BANDWIDTH_ALLOCATION_TLV));
1854 bp->state = BNX2X_STATE_OPENING_WAIT4_PORT;
1855 rc = bnx2x_func_start(bp);
1856 if (rc) {
1857 BNX2X_ERR("Function start failed!\n");
1858 bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
1859 LOAD_ERROR_EXIT(bp, load_error3);
1862 /* Send LOAD_DONE command to MCP */
1863 if (!BP_NOMCP(bp)) {
1864 load_code = bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
1865 if (!load_code) {
1866 BNX2X_ERR("MCP response failure, aborting\n");
1867 rc = -EBUSY;
1868 LOAD_ERROR_EXIT(bp, load_error3);
1872 rc = bnx2x_setup_leading(bp);
1873 if (rc) {
1874 BNX2X_ERR("Setup leading failed!\n");
1875 LOAD_ERROR_EXIT(bp, load_error3);
1878 #ifdef BCM_CNIC
1879 /* Enable Timer scan */
1880 REG_WR(bp, TM_REG_EN_LINEAR0_TIMER + port*4, 1);
1881 #endif
1883 for_each_nondefault_queue(bp, i) {
1884 rc = bnx2x_setup_queue(bp, &bp->fp[i], 0);
1885 if (rc)
1886 LOAD_ERROR_EXIT(bp, load_error4);
1889 rc = bnx2x_init_rss_pf(bp);
1890 if (rc)
1891 LOAD_ERROR_EXIT(bp, load_error4);
1893 /* Now when Clients are configured we are ready to work */
1894 bp->state = BNX2X_STATE_OPEN;
1896 /* Configure a ucast MAC */
1897 rc = bnx2x_set_eth_mac(bp, true);
1898 if (rc)
1899 LOAD_ERROR_EXIT(bp, load_error4);
1901 if (bp->pending_max) {
1902 bnx2x_update_max_mf_config(bp, bp->pending_max);
1903 bp->pending_max = 0;
1906 if (bp->port.pmf)
1907 bnx2x_initial_phy_init(bp, load_mode);
1909 /* Start fast path */
1911 /* Initialize Rx filter. */
1912 netif_addr_lock_bh(bp->dev);
1913 bnx2x_set_rx_mode(bp->dev);
1914 netif_addr_unlock_bh(bp->dev);
1916 /* Start the Tx */
1917 switch (load_mode) {
1918 case LOAD_NORMAL:
1919 /* Tx queue should be only reenabled */
1920 netif_tx_wake_all_queues(bp->dev);
1921 break;
1923 case LOAD_OPEN:
1924 netif_tx_start_all_queues(bp->dev);
1925 smp_mb__after_clear_bit();
1926 break;
1928 case LOAD_DIAG:
1929 bp->state = BNX2X_STATE_DIAG;
1930 break;
1932 default:
1933 break;
1936 if (!bp->port.pmf)
1937 bnx2x__link_status_update(bp);
1939 /* start the timer */
1940 mod_timer(&bp->timer, jiffies + bp->current_interval);
1942 #ifdef BCM_CNIC
1943 bnx2x_setup_cnic_irq_info(bp);
1944 if (bp->state == BNX2X_STATE_OPEN)
1945 bnx2x_cnic_notify(bp, CNIC_CTL_START_CMD);
1946 #endif
1947 bnx2x_inc_load_cnt(bp);
1949 /* Wait for all pending SP commands to complete */
1950 if (!bnx2x_wait_sp_comp(bp, ~0x0UL)) {
1951 BNX2X_ERR("Timeout waiting for SP elements to complete\n");
1952 bnx2x_nic_unload(bp, UNLOAD_CLOSE);
1953 return -EBUSY;
1956 bnx2x_dcbx_init(bp);
1957 return 0;
1959 #ifndef BNX2X_STOP_ON_ERROR
1960 load_error4:
1961 #ifdef BCM_CNIC
1962 /* Disable Timer scan */
1963 REG_WR(bp, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
1964 #endif
1965 load_error3:
1966 bnx2x_int_disable_sync(bp, 1);
1968 /* Clean queueable objects */
1969 bnx2x_squeeze_objects(bp);
1971 /* Free SKBs, SGEs, TPA pool and driver internals */
1972 bnx2x_free_skbs(bp);
1973 for_each_rx_queue(bp, i)
1974 bnx2x_free_rx_sge_range(bp, bp->fp + i, NUM_RX_SGE);
1976 /* Release IRQs */
1977 bnx2x_free_irq(bp);
1978 load_error2:
1979 if (!BP_NOMCP(bp)) {
1980 bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
1981 bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_DONE, 0);
1984 bp->port.pmf = 0;
1985 load_error1:
1986 bnx2x_napi_disable(bp);
1987 load_error0:
1988 bnx2x_free_mem(bp);
1990 return rc;
1991 #endif /* ! BNX2X_STOP_ON_ERROR */
1994 /* must be called with rtnl_lock */
1995 int bnx2x_nic_unload(struct bnx2x *bp, int unload_mode)
1997 int i;
1998 bool global = false;
2000 if ((bp->state == BNX2X_STATE_CLOSED) ||
2001 (bp->state == BNX2X_STATE_ERROR)) {
2002 /* We can get here if the driver has been unloaded
2003 * during parity error recovery and is either waiting for a
2004 * leader to complete or for other functions to unload and
2005 * then ifdown has been issued. In this case we want to
2006 * unload and let other functions to complete a recovery
2007 * process.
2009 bp->recovery_state = BNX2X_RECOVERY_DONE;
2010 bp->is_leader = 0;
2011 bnx2x_release_leader_lock(bp);
2012 smp_mb();
2014 DP(NETIF_MSG_HW, "Releasing a leadership...\n");
2016 return -EINVAL;
2020 * It's important to set the bp->state to the value different from
2021 * BNX2X_STATE_OPEN and only then stop the Tx. Otherwise bnx2x_tx_int()
2022 * may restart the Tx from the NAPI context (see bnx2x_tx_int()).
2024 bp->state = BNX2X_STATE_CLOSING_WAIT4_HALT;
2025 smp_mb();
2027 /* Stop Tx */
2028 bnx2x_tx_disable(bp);
2030 #ifdef BCM_CNIC
2031 bnx2x_cnic_notify(bp, CNIC_CTL_STOP_CMD);
2032 #endif
2034 bp->rx_mode = BNX2X_RX_MODE_NONE;
2036 del_timer_sync(&bp->timer);
2038 /* Set ALWAYS_ALIVE bit in shmem */
2039 bp->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
2041 bnx2x_drv_pulse(bp);
2043 bnx2x_stats_handle(bp, STATS_EVENT_STOP);
2045 /* Cleanup the chip if needed */
2046 if (unload_mode != UNLOAD_RECOVERY)
2047 bnx2x_chip_cleanup(bp, unload_mode);
2048 else {
2049 /* Send the UNLOAD_REQUEST to the MCP */
2050 bnx2x_send_unload_req(bp, unload_mode);
2053 * Prevent transactions to host from the functions on the
2054 * engine that doesn't reset global blocks in case of global
2055 * attention once gloabl blocks are reset and gates are opened
2056 * (the engine which leader will perform the recovery
2057 * last).
2059 if (!CHIP_IS_E1x(bp))
2060 bnx2x_pf_disable(bp);
2062 /* Disable HW interrupts, NAPI */
2063 bnx2x_netif_stop(bp, 1);
2065 /* Release IRQs */
2066 bnx2x_free_irq(bp);
2068 /* Report UNLOAD_DONE to MCP */
2069 bnx2x_send_unload_done(bp);
2073 * At this stage no more interrupts will arrive so we may safly clean
2074 * the queueable objects here in case they failed to get cleaned so far.
2076 bnx2x_squeeze_objects(bp);
2078 /* There should be no more pending SP commands at this stage */
2079 bp->sp_state = 0;
2081 bp->port.pmf = 0;
2083 /* Free SKBs, SGEs, TPA pool and driver internals */
2084 bnx2x_free_skbs(bp);
2085 for_each_rx_queue(bp, i)
2086 bnx2x_free_rx_sge_range(bp, bp->fp + i, NUM_RX_SGE);
2088 bnx2x_free_mem(bp);
2090 bp->state = BNX2X_STATE_CLOSED;
2092 /* Check if there are pending parity attentions. If there are - set
2093 * RECOVERY_IN_PROGRESS.
2095 if (bnx2x_chk_parity_attn(bp, &global, false)) {
2096 bnx2x_set_reset_in_progress(bp);
2098 /* Set RESET_IS_GLOBAL if needed */
2099 if (global)
2100 bnx2x_set_reset_global(bp);
2104 /* The last driver must disable a "close the gate" if there is no
2105 * parity attention or "process kill" pending.
2107 if (!bnx2x_dec_load_cnt(bp) && bnx2x_reset_is_done(bp, BP_PATH(bp)))
2108 bnx2x_disable_close_the_gate(bp);
2110 return 0;
2113 int bnx2x_set_power_state(struct bnx2x *bp, pci_power_t state)
2115 u16 pmcsr;
2117 /* If there is no power capability, silently succeed */
2118 if (!bp->pm_cap) {
2119 DP(NETIF_MSG_HW, "No power capability. Breaking.\n");
2120 return 0;
2123 pci_read_config_word(bp->pdev, bp->pm_cap + PCI_PM_CTRL, &pmcsr);
2125 switch (state) {
2126 case PCI_D0:
2127 pci_write_config_word(bp->pdev, bp->pm_cap + PCI_PM_CTRL,
2128 ((pmcsr & ~PCI_PM_CTRL_STATE_MASK) |
2129 PCI_PM_CTRL_PME_STATUS));
2131 if (pmcsr & PCI_PM_CTRL_STATE_MASK)
2132 /* delay required during transition out of D3hot */
2133 msleep(20);
2134 break;
2136 case PCI_D3hot:
2137 /* If there are other clients above don't
2138 shut down the power */
2139 if (atomic_read(&bp->pdev->enable_cnt) != 1)
2140 return 0;
2141 /* Don't shut down the power for emulation and FPGA */
2142 if (CHIP_REV_IS_SLOW(bp))
2143 return 0;
2145 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
2146 pmcsr |= 3;
2148 if (bp->wol)
2149 pmcsr |= PCI_PM_CTRL_PME_ENABLE;
2151 pci_write_config_word(bp->pdev, bp->pm_cap + PCI_PM_CTRL,
2152 pmcsr);
2154 /* No more memory access after this point until
2155 * device is brought back to D0.
2157 break;
2159 default:
2160 return -EINVAL;
2162 return 0;
2166 * net_device service functions
2168 int bnx2x_poll(struct napi_struct *napi, int budget)
2170 int work_done = 0;
2171 u8 cos;
2172 struct bnx2x_fastpath *fp = container_of(napi, struct bnx2x_fastpath,
2173 napi);
2174 struct bnx2x *bp = fp->bp;
2176 while (1) {
2177 #ifdef BNX2X_STOP_ON_ERROR
2178 if (unlikely(bp->panic)) {
2179 napi_complete(napi);
2180 return 0;
2182 #endif
2184 for_each_cos_in_tx_queue(fp, cos)
2185 if (bnx2x_tx_queue_has_work(&fp->txdata[cos]))
2186 bnx2x_tx_int(bp, &fp->txdata[cos]);
2189 if (bnx2x_has_rx_work(fp)) {
2190 work_done += bnx2x_rx_int(fp, budget - work_done);
2192 /* must not complete if we consumed full budget */
2193 if (work_done >= budget)
2194 break;
2197 /* Fall out from the NAPI loop if needed */
2198 if (!(bnx2x_has_rx_work(fp) || bnx2x_has_tx_work(fp))) {
2199 #ifdef BCM_CNIC
2200 /* No need to update SB for FCoE L2 ring as long as
2201 * it's connected to the default SB and the SB
2202 * has been updated when NAPI was scheduled.
2204 if (IS_FCOE_FP(fp)) {
2205 napi_complete(napi);
2206 break;
2208 #endif
2210 bnx2x_update_fpsb_idx(fp);
2211 /* bnx2x_has_rx_work() reads the status block,
2212 * thus we need to ensure that status block indices
2213 * have been actually read (bnx2x_update_fpsb_idx)
2214 * prior to this check (bnx2x_has_rx_work) so that
2215 * we won't write the "newer" value of the status block
2216 * to IGU (if there was a DMA right after
2217 * bnx2x_has_rx_work and if there is no rmb, the memory
2218 * reading (bnx2x_update_fpsb_idx) may be postponed
2219 * to right before bnx2x_ack_sb). In this case there
2220 * will never be another interrupt until there is
2221 * another update of the status block, while there
2222 * is still unhandled work.
2224 rmb();
2226 if (!(bnx2x_has_rx_work(fp) || bnx2x_has_tx_work(fp))) {
2227 napi_complete(napi);
2228 /* Re-enable interrupts */
2229 DP(NETIF_MSG_HW,
2230 "Update index to %d\n", fp->fp_hc_idx);
2231 bnx2x_ack_sb(bp, fp->igu_sb_id, USTORM_ID,
2232 le16_to_cpu(fp->fp_hc_idx),
2233 IGU_INT_ENABLE, 1);
2234 break;
2239 return work_done;
2242 /* we split the first BD into headers and data BDs
2243 * to ease the pain of our fellow microcode engineers
2244 * we use one mapping for both BDs
2245 * So far this has only been observed to happen
2246 * in Other Operating Systems(TM)
2248 static noinline u16 bnx2x_tx_split(struct bnx2x *bp,
2249 struct bnx2x_fp_txdata *txdata,
2250 struct sw_tx_bd *tx_buf,
2251 struct eth_tx_start_bd **tx_bd, u16 hlen,
2252 u16 bd_prod, int nbd)
2254 struct eth_tx_start_bd *h_tx_bd = *tx_bd;
2255 struct eth_tx_bd *d_tx_bd;
2256 dma_addr_t mapping;
2257 int old_len = le16_to_cpu(h_tx_bd->nbytes);
2259 /* first fix first BD */
2260 h_tx_bd->nbd = cpu_to_le16(nbd);
2261 h_tx_bd->nbytes = cpu_to_le16(hlen);
2263 DP(NETIF_MSG_TX_QUEUED, "TSO split header size is %d "
2264 "(%x:%x) nbd %d\n", h_tx_bd->nbytes, h_tx_bd->addr_hi,
2265 h_tx_bd->addr_lo, h_tx_bd->nbd);
2267 /* now get a new data BD
2268 * (after the pbd) and fill it */
2269 bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
2270 d_tx_bd = &txdata->tx_desc_ring[bd_prod].reg_bd;
2272 mapping = HILO_U64(le32_to_cpu(h_tx_bd->addr_hi),
2273 le32_to_cpu(h_tx_bd->addr_lo)) + hlen;
2275 d_tx_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
2276 d_tx_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
2277 d_tx_bd->nbytes = cpu_to_le16(old_len - hlen);
2279 /* this marks the BD as one that has no individual mapping */
2280 tx_buf->flags |= BNX2X_TSO_SPLIT_BD;
2282 DP(NETIF_MSG_TX_QUEUED,
2283 "TSO split data size is %d (%x:%x)\n",
2284 d_tx_bd->nbytes, d_tx_bd->addr_hi, d_tx_bd->addr_lo);
2286 /* update tx_bd */
2287 *tx_bd = (struct eth_tx_start_bd *)d_tx_bd;
2289 return bd_prod;
2292 static inline u16 bnx2x_csum_fix(unsigned char *t_header, u16 csum, s8 fix)
2294 if (fix > 0)
2295 csum = (u16) ~csum_fold(csum_sub(csum,
2296 csum_partial(t_header - fix, fix, 0)));
2298 else if (fix < 0)
2299 csum = (u16) ~csum_fold(csum_add(csum,
2300 csum_partial(t_header, -fix, 0)));
2302 return swab16(csum);
2305 static inline u32 bnx2x_xmit_type(struct bnx2x *bp, struct sk_buff *skb)
2307 u32 rc;
2309 if (skb->ip_summed != CHECKSUM_PARTIAL)
2310 rc = XMIT_PLAIN;
2312 else {
2313 if (vlan_get_protocol(skb) == htons(ETH_P_IPV6)) {
2314 rc = XMIT_CSUM_V6;
2315 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
2316 rc |= XMIT_CSUM_TCP;
2318 } else {
2319 rc = XMIT_CSUM_V4;
2320 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2321 rc |= XMIT_CSUM_TCP;
2325 if (skb_is_gso_v6(skb))
2326 rc |= XMIT_GSO_V6 | XMIT_CSUM_TCP | XMIT_CSUM_V6;
2327 else if (skb_is_gso(skb))
2328 rc |= XMIT_GSO_V4 | XMIT_CSUM_V4 | XMIT_CSUM_TCP;
2330 return rc;
2333 #if (MAX_SKB_FRAGS >= MAX_FETCH_BD - 3)
2334 /* check if packet requires linearization (packet is too fragmented)
2335 no need to check fragmentation if page size > 8K (there will be no
2336 violation to FW restrictions) */
2337 static int bnx2x_pkt_req_lin(struct bnx2x *bp, struct sk_buff *skb,
2338 u32 xmit_type)
2340 int to_copy = 0;
2341 int hlen = 0;
2342 int first_bd_sz = 0;
2344 /* 3 = 1 (for linear data BD) + 2 (for PBD and last BD) */
2345 if (skb_shinfo(skb)->nr_frags >= (MAX_FETCH_BD - 3)) {
2347 if (xmit_type & XMIT_GSO) {
2348 unsigned short lso_mss = skb_shinfo(skb)->gso_size;
2349 /* Check if LSO packet needs to be copied:
2350 3 = 1 (for headers BD) + 2 (for PBD and last BD) */
2351 int wnd_size = MAX_FETCH_BD - 3;
2352 /* Number of windows to check */
2353 int num_wnds = skb_shinfo(skb)->nr_frags - wnd_size;
2354 int wnd_idx = 0;
2355 int frag_idx = 0;
2356 u32 wnd_sum = 0;
2358 /* Headers length */
2359 hlen = (int)(skb_transport_header(skb) - skb->data) +
2360 tcp_hdrlen(skb);
2362 /* Amount of data (w/o headers) on linear part of SKB*/
2363 first_bd_sz = skb_headlen(skb) - hlen;
2365 wnd_sum = first_bd_sz;
2367 /* Calculate the first sum - it's special */
2368 for (frag_idx = 0; frag_idx < wnd_size - 1; frag_idx++)
2369 wnd_sum +=
2370 skb_shinfo(skb)->frags[frag_idx].size;
2372 /* If there was data on linear skb data - check it */
2373 if (first_bd_sz > 0) {
2374 if (unlikely(wnd_sum < lso_mss)) {
2375 to_copy = 1;
2376 goto exit_lbl;
2379 wnd_sum -= first_bd_sz;
2382 /* Others are easier: run through the frag list and
2383 check all windows */
2384 for (wnd_idx = 0; wnd_idx <= num_wnds; wnd_idx++) {
2385 wnd_sum +=
2386 skb_shinfo(skb)->frags[wnd_idx + wnd_size - 1].size;
2388 if (unlikely(wnd_sum < lso_mss)) {
2389 to_copy = 1;
2390 break;
2392 wnd_sum -=
2393 skb_shinfo(skb)->frags[wnd_idx].size;
2395 } else {
2396 /* in non-LSO too fragmented packet should always
2397 be linearized */
2398 to_copy = 1;
2402 exit_lbl:
2403 if (unlikely(to_copy))
2404 DP(NETIF_MSG_TX_QUEUED,
2405 "Linearization IS REQUIRED for %s packet. "
2406 "num_frags %d hlen %d first_bd_sz %d\n",
2407 (xmit_type & XMIT_GSO) ? "LSO" : "non-LSO",
2408 skb_shinfo(skb)->nr_frags, hlen, first_bd_sz);
2410 return to_copy;
2412 #endif
2414 static inline void bnx2x_set_pbd_gso_e2(struct sk_buff *skb, u32 *parsing_data,
2415 u32 xmit_type)
2417 *parsing_data |= (skb_shinfo(skb)->gso_size <<
2418 ETH_TX_PARSE_BD_E2_LSO_MSS_SHIFT) &
2419 ETH_TX_PARSE_BD_E2_LSO_MSS;
2420 if ((xmit_type & XMIT_GSO_V6) &&
2421 (ipv6_hdr(skb)->nexthdr == NEXTHDR_IPV6))
2422 *parsing_data |= ETH_TX_PARSE_BD_E2_IPV6_WITH_EXT_HDR;
2426 * bnx2x_set_pbd_gso - update PBD in GSO case.
2428 * @skb: packet skb
2429 * @pbd: parse BD
2430 * @xmit_type: xmit flags
2432 static inline void bnx2x_set_pbd_gso(struct sk_buff *skb,
2433 struct eth_tx_parse_bd_e1x *pbd,
2434 u32 xmit_type)
2436 pbd->lso_mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
2437 pbd->tcp_send_seq = swab32(tcp_hdr(skb)->seq);
2438 pbd->tcp_flags = pbd_tcp_flags(skb);
2440 if (xmit_type & XMIT_GSO_V4) {
2441 pbd->ip_id = swab16(ip_hdr(skb)->id);
2442 pbd->tcp_pseudo_csum =
2443 swab16(~csum_tcpudp_magic(ip_hdr(skb)->saddr,
2444 ip_hdr(skb)->daddr,
2445 0, IPPROTO_TCP, 0));
2447 } else
2448 pbd->tcp_pseudo_csum =
2449 swab16(~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
2450 &ipv6_hdr(skb)->daddr,
2451 0, IPPROTO_TCP, 0));
2453 pbd->global_data |= ETH_TX_PARSE_BD_E1X_PSEUDO_CS_WITHOUT_LEN;
2457 * bnx2x_set_pbd_csum_e2 - update PBD with checksum and return header length
2459 * @bp: driver handle
2460 * @skb: packet skb
2461 * @parsing_data: data to be updated
2462 * @xmit_type: xmit flags
2464 * 57712 related
2466 static inline u8 bnx2x_set_pbd_csum_e2(struct bnx2x *bp, struct sk_buff *skb,
2467 u32 *parsing_data, u32 xmit_type)
2469 *parsing_data |=
2470 ((((u8 *)skb_transport_header(skb) - skb->data) >> 1) <<
2471 ETH_TX_PARSE_BD_E2_TCP_HDR_START_OFFSET_W_SHIFT) &
2472 ETH_TX_PARSE_BD_E2_TCP_HDR_START_OFFSET_W;
2474 if (xmit_type & XMIT_CSUM_TCP) {
2475 *parsing_data |= ((tcp_hdrlen(skb) / 4) <<
2476 ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW_SHIFT) &
2477 ETH_TX_PARSE_BD_E2_TCP_HDR_LENGTH_DW;
2479 return skb_transport_header(skb) + tcp_hdrlen(skb) - skb->data;
2480 } else
2481 /* We support checksum offload for TCP and UDP only.
2482 * No need to pass the UDP header length - it's a constant.
2484 return skb_transport_header(skb) +
2485 sizeof(struct udphdr) - skb->data;
2488 static inline void bnx2x_set_sbd_csum(struct bnx2x *bp, struct sk_buff *skb,
2489 struct eth_tx_start_bd *tx_start_bd, u32 xmit_type)
2491 tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_L4_CSUM;
2493 if (xmit_type & XMIT_CSUM_V4)
2494 tx_start_bd->bd_flags.as_bitfield |=
2495 ETH_TX_BD_FLAGS_IP_CSUM;
2496 else
2497 tx_start_bd->bd_flags.as_bitfield |=
2498 ETH_TX_BD_FLAGS_IPV6;
2500 if (!(xmit_type & XMIT_CSUM_TCP))
2501 tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_IS_UDP;
2505 * bnx2x_set_pbd_csum - update PBD with checksum and return header length
2507 * @bp: driver handle
2508 * @skb: packet skb
2509 * @pbd: parse BD to be updated
2510 * @xmit_type: xmit flags
2512 static inline u8 bnx2x_set_pbd_csum(struct bnx2x *bp, struct sk_buff *skb,
2513 struct eth_tx_parse_bd_e1x *pbd,
2514 u32 xmit_type)
2516 u8 hlen = (skb_network_header(skb) - skb->data) >> 1;
2518 /* for now NS flag is not used in Linux */
2519 pbd->global_data =
2520 (hlen | ((skb->protocol == cpu_to_be16(ETH_P_8021Q)) <<
2521 ETH_TX_PARSE_BD_E1X_LLC_SNAP_EN_SHIFT));
2523 pbd->ip_hlen_w = (skb_transport_header(skb) -
2524 skb_network_header(skb)) >> 1;
2526 hlen += pbd->ip_hlen_w;
2528 /* We support checksum offload for TCP and UDP only */
2529 if (xmit_type & XMIT_CSUM_TCP)
2530 hlen += tcp_hdrlen(skb) / 2;
2531 else
2532 hlen += sizeof(struct udphdr) / 2;
2534 pbd->total_hlen_w = cpu_to_le16(hlen);
2535 hlen = hlen*2;
2537 if (xmit_type & XMIT_CSUM_TCP) {
2538 pbd->tcp_pseudo_csum = swab16(tcp_hdr(skb)->check);
2540 } else {
2541 s8 fix = SKB_CS_OFF(skb); /* signed! */
2543 DP(NETIF_MSG_TX_QUEUED,
2544 "hlen %d fix %d csum before fix %x\n",
2545 le16_to_cpu(pbd->total_hlen_w), fix, SKB_CS(skb));
2547 /* HW bug: fixup the CSUM */
2548 pbd->tcp_pseudo_csum =
2549 bnx2x_csum_fix(skb_transport_header(skb),
2550 SKB_CS(skb), fix);
2552 DP(NETIF_MSG_TX_QUEUED, "csum after fix %x\n",
2553 pbd->tcp_pseudo_csum);
2556 return hlen;
2559 /* called with netif_tx_lock
2560 * bnx2x_tx_int() runs without netif_tx_lock unless it needs to call
2561 * netif_wake_queue()
2563 netdev_tx_t bnx2x_start_xmit(struct sk_buff *skb, struct net_device *dev)
2565 struct bnx2x *bp = netdev_priv(dev);
2567 struct bnx2x_fastpath *fp;
2568 struct netdev_queue *txq;
2569 struct bnx2x_fp_txdata *txdata;
2570 struct sw_tx_bd *tx_buf;
2571 struct eth_tx_start_bd *tx_start_bd, *first_bd;
2572 struct eth_tx_bd *tx_data_bd, *total_pkt_bd = NULL;
2573 struct eth_tx_parse_bd_e1x *pbd_e1x = NULL;
2574 struct eth_tx_parse_bd_e2 *pbd_e2 = NULL;
2575 u32 pbd_e2_parsing_data = 0;
2576 u16 pkt_prod, bd_prod;
2577 int nbd, txq_index, fp_index, txdata_index;
2578 dma_addr_t mapping;
2579 u32 xmit_type = bnx2x_xmit_type(bp, skb);
2580 int i;
2581 u8 hlen = 0;
2582 __le16 pkt_size = 0;
2583 struct ethhdr *eth;
2584 u8 mac_type = UNICAST_ADDRESS;
2586 #ifdef BNX2X_STOP_ON_ERROR
2587 if (unlikely(bp->panic))
2588 return NETDEV_TX_BUSY;
2589 #endif
2591 txq_index = skb_get_queue_mapping(skb);
2592 txq = netdev_get_tx_queue(dev, txq_index);
2594 BUG_ON(txq_index >= MAX_ETH_TXQ_IDX(bp) + FCOE_PRESENT);
2596 /* decode the fastpath index and the cos index from the txq */
2597 fp_index = TXQ_TO_FP(txq_index);
2598 txdata_index = TXQ_TO_COS(txq_index);
2600 #ifdef BCM_CNIC
2602 * Override the above for the FCoE queue:
2603 * - FCoE fp entry is right after the ETH entries.
2604 * - FCoE L2 queue uses bp->txdata[0] only.
2606 if (unlikely(!NO_FCOE(bp) && (txq_index ==
2607 bnx2x_fcoe_tx(bp, txq_index)))) {
2608 fp_index = FCOE_IDX;
2609 txdata_index = 0;
2611 #endif
2613 /* enable this debug print to view the transmission queue being used
2614 DP(BNX2X_MSG_FP, "indices: txq %d, fp %d, txdata %d\n",
2615 txq_index, fp_index, txdata_index); */
2617 /* locate the fastpath and the txdata */
2618 fp = &bp->fp[fp_index];
2619 txdata = &fp->txdata[txdata_index];
2621 /* enable this debug print to view the tranmission details
2622 DP(BNX2X_MSG_FP,"transmitting packet cid %d fp index %d txdata_index %d"
2623 " tx_data ptr %p fp pointer %p\n",
2624 txdata->cid, fp_index, txdata_index, txdata, fp); */
2626 if (unlikely(bnx2x_tx_avail(bp, txdata) <
2627 (skb_shinfo(skb)->nr_frags + 3))) {
2628 fp->eth_q_stats.driver_xoff++;
2629 netif_tx_stop_queue(txq);
2630 BNX2X_ERR("BUG! Tx ring full when queue awake!\n");
2631 return NETDEV_TX_BUSY;
2634 DP(NETIF_MSG_TX_QUEUED, "queue[%d]: SKB: summed %x protocol %x "
2635 "protocol(%x,%x) gso type %x xmit_type %x\n",
2636 txq_index, skb->ip_summed, skb->protocol, ipv6_hdr(skb)->nexthdr,
2637 ip_hdr(skb)->protocol, skb_shinfo(skb)->gso_type, xmit_type);
2639 eth = (struct ethhdr *)skb->data;
2641 /* set flag according to packet type (UNICAST_ADDRESS is default)*/
2642 if (unlikely(is_multicast_ether_addr(eth->h_dest))) {
2643 if (is_broadcast_ether_addr(eth->h_dest))
2644 mac_type = BROADCAST_ADDRESS;
2645 else
2646 mac_type = MULTICAST_ADDRESS;
2649 #if (MAX_SKB_FRAGS >= MAX_FETCH_BD - 3)
2650 /* First, check if we need to linearize the skb (due to FW
2651 restrictions). No need to check fragmentation if page size > 8K
2652 (there will be no violation to FW restrictions) */
2653 if (bnx2x_pkt_req_lin(bp, skb, xmit_type)) {
2654 /* Statistics of linearization */
2655 bp->lin_cnt++;
2656 if (skb_linearize(skb) != 0) {
2657 DP(NETIF_MSG_TX_QUEUED, "SKB linearization failed - "
2658 "silently dropping this SKB\n");
2659 dev_kfree_skb_any(skb);
2660 return NETDEV_TX_OK;
2663 #endif
2664 /* Map skb linear data for DMA */
2665 mapping = dma_map_single(&bp->pdev->dev, skb->data,
2666 skb_headlen(skb), DMA_TO_DEVICE);
2667 if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
2668 DP(NETIF_MSG_TX_QUEUED, "SKB mapping failed - "
2669 "silently dropping this SKB\n");
2670 dev_kfree_skb_any(skb);
2671 return NETDEV_TX_OK;
2674 Please read carefully. First we use one BD which we mark as start,
2675 then we have a parsing info BD (used for TSO or xsum),
2676 and only then we have the rest of the TSO BDs.
2677 (don't forget to mark the last one as last,
2678 and to unmap only AFTER you write to the BD ...)
2679 And above all, all pdb sizes are in words - NOT DWORDS!
2682 /* get current pkt produced now - advance it just before sending packet
2683 * since mapping of pages may fail and cause packet to be dropped
2685 pkt_prod = txdata->tx_pkt_prod;
2686 bd_prod = TX_BD(txdata->tx_bd_prod);
2688 /* get a tx_buf and first BD
2689 * tx_start_bd may be changed during SPLIT,
2690 * but first_bd will always stay first
2692 tx_buf = &txdata->tx_buf_ring[TX_BD(pkt_prod)];
2693 tx_start_bd = &txdata->tx_desc_ring[bd_prod].start_bd;
2694 first_bd = tx_start_bd;
2696 tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
2697 SET_FLAG(tx_start_bd->general_data, ETH_TX_START_BD_ETH_ADDR_TYPE,
2698 mac_type);
2700 /* header nbd */
2701 SET_FLAG(tx_start_bd->general_data, ETH_TX_START_BD_HDR_NBDS, 1);
2703 /* remember the first BD of the packet */
2704 tx_buf->first_bd = txdata->tx_bd_prod;
2705 tx_buf->skb = skb;
2706 tx_buf->flags = 0;
2708 DP(NETIF_MSG_TX_QUEUED,
2709 "sending pkt %u @%p next_idx %u bd %u @%p\n",
2710 pkt_prod, tx_buf, txdata->tx_pkt_prod, bd_prod, tx_start_bd);
2712 if (vlan_tx_tag_present(skb)) {
2713 tx_start_bd->vlan_or_ethertype =
2714 cpu_to_le16(vlan_tx_tag_get(skb));
2715 tx_start_bd->bd_flags.as_bitfield |=
2716 (X_ETH_OUTBAND_VLAN << ETH_TX_BD_FLAGS_VLAN_MODE_SHIFT);
2717 } else
2718 tx_start_bd->vlan_or_ethertype = cpu_to_le16(pkt_prod);
2720 /* turn on parsing and get a BD */
2721 bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
2723 if (xmit_type & XMIT_CSUM)
2724 bnx2x_set_sbd_csum(bp, skb, tx_start_bd, xmit_type);
2726 if (!CHIP_IS_E1x(bp)) {
2727 pbd_e2 = &txdata->tx_desc_ring[bd_prod].parse_bd_e2;
2728 memset(pbd_e2, 0, sizeof(struct eth_tx_parse_bd_e2));
2729 /* Set PBD in checksum offload case */
2730 if (xmit_type & XMIT_CSUM)
2731 hlen = bnx2x_set_pbd_csum_e2(bp, skb,
2732 &pbd_e2_parsing_data,
2733 xmit_type);
2734 if (IS_MF_SI(bp)) {
2736 * fill in the MAC addresses in the PBD - for local
2737 * switching
2739 bnx2x_set_fw_mac_addr(&pbd_e2->src_mac_addr_hi,
2740 &pbd_e2->src_mac_addr_mid,
2741 &pbd_e2->src_mac_addr_lo,
2742 eth->h_source);
2743 bnx2x_set_fw_mac_addr(&pbd_e2->dst_mac_addr_hi,
2744 &pbd_e2->dst_mac_addr_mid,
2745 &pbd_e2->dst_mac_addr_lo,
2746 eth->h_dest);
2748 } else {
2749 pbd_e1x = &txdata->tx_desc_ring[bd_prod].parse_bd_e1x;
2750 memset(pbd_e1x, 0, sizeof(struct eth_tx_parse_bd_e1x));
2751 /* Set PBD in checksum offload case */
2752 if (xmit_type & XMIT_CSUM)
2753 hlen = bnx2x_set_pbd_csum(bp, skb, pbd_e1x, xmit_type);
2757 /* Setup the data pointer of the first BD of the packet */
2758 tx_start_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
2759 tx_start_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
2760 nbd = 2; /* start_bd + pbd + frags (updated when pages are mapped) */
2761 tx_start_bd->nbytes = cpu_to_le16(skb_headlen(skb));
2762 pkt_size = tx_start_bd->nbytes;
2764 DP(NETIF_MSG_TX_QUEUED, "first bd @%p addr (%x:%x) nbd %d"
2765 " nbytes %d flags %x vlan %x\n",
2766 tx_start_bd, tx_start_bd->addr_hi, tx_start_bd->addr_lo,
2767 le16_to_cpu(tx_start_bd->nbd), le16_to_cpu(tx_start_bd->nbytes),
2768 tx_start_bd->bd_flags.as_bitfield,
2769 le16_to_cpu(tx_start_bd->vlan_or_ethertype));
2771 if (xmit_type & XMIT_GSO) {
2773 DP(NETIF_MSG_TX_QUEUED,
2774 "TSO packet len %d hlen %d total len %d tso size %d\n",
2775 skb->len, hlen, skb_headlen(skb),
2776 skb_shinfo(skb)->gso_size);
2778 tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_SW_LSO;
2780 if (unlikely(skb_headlen(skb) > hlen))
2781 bd_prod = bnx2x_tx_split(bp, txdata, tx_buf,
2782 &tx_start_bd, hlen,
2783 bd_prod, ++nbd);
2784 if (!CHIP_IS_E1x(bp))
2785 bnx2x_set_pbd_gso_e2(skb, &pbd_e2_parsing_data,
2786 xmit_type);
2787 else
2788 bnx2x_set_pbd_gso(skb, pbd_e1x, xmit_type);
2791 /* Set the PBD's parsing_data field if not zero
2792 * (for the chips newer than 57711).
2794 if (pbd_e2_parsing_data)
2795 pbd_e2->parsing_data = cpu_to_le32(pbd_e2_parsing_data);
2797 tx_data_bd = (struct eth_tx_bd *)tx_start_bd;
2799 /* Handle fragmented skb */
2800 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2801 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2803 mapping = skb_frag_dma_map(&bp->pdev->dev, frag, 0, frag->size,
2804 DMA_TO_DEVICE);
2805 if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
2807 DP(NETIF_MSG_TX_QUEUED, "Unable to map page - "
2808 "dropping packet...\n");
2810 /* we need unmap all buffers already mapped
2811 * for this SKB;
2812 * first_bd->nbd need to be properly updated
2813 * before call to bnx2x_free_tx_pkt
2815 first_bd->nbd = cpu_to_le16(nbd);
2816 bnx2x_free_tx_pkt(bp, txdata,
2817 TX_BD(txdata->tx_pkt_prod));
2818 return NETDEV_TX_OK;
2821 bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
2822 tx_data_bd = &txdata->tx_desc_ring[bd_prod].reg_bd;
2823 if (total_pkt_bd == NULL)
2824 total_pkt_bd = &txdata->tx_desc_ring[bd_prod].reg_bd;
2826 tx_data_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
2827 tx_data_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
2828 tx_data_bd->nbytes = cpu_to_le16(frag->size);
2829 le16_add_cpu(&pkt_size, frag->size);
2830 nbd++;
2832 DP(NETIF_MSG_TX_QUEUED,
2833 "frag %d bd @%p addr (%x:%x) nbytes %d\n",
2834 i, tx_data_bd, tx_data_bd->addr_hi, tx_data_bd->addr_lo,
2835 le16_to_cpu(tx_data_bd->nbytes));
2838 DP(NETIF_MSG_TX_QUEUED, "last bd @%p\n", tx_data_bd);
2840 /* update with actual num BDs */
2841 first_bd->nbd = cpu_to_le16(nbd);
2843 bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
2845 /* now send a tx doorbell, counting the next BD
2846 * if the packet contains or ends with it
2848 if (TX_BD_POFF(bd_prod) < nbd)
2849 nbd++;
2851 /* total_pkt_bytes should be set on the first data BD if
2852 * it's not an LSO packet and there is more than one
2853 * data BD. In this case pkt_size is limited by an MTU value.
2854 * However we prefer to set it for an LSO packet (while we don't
2855 * have to) in order to save some CPU cycles in a none-LSO
2856 * case, when we much more care about them.
2858 if (total_pkt_bd != NULL)
2859 total_pkt_bd->total_pkt_bytes = pkt_size;
2861 if (pbd_e1x)
2862 DP(NETIF_MSG_TX_QUEUED,
2863 "PBD (E1X) @%p ip_data %x ip_hlen %u ip_id %u lso_mss %u"
2864 " tcp_flags %x xsum %x seq %u hlen %u\n",
2865 pbd_e1x, pbd_e1x->global_data, pbd_e1x->ip_hlen_w,
2866 pbd_e1x->ip_id, pbd_e1x->lso_mss, pbd_e1x->tcp_flags,
2867 pbd_e1x->tcp_pseudo_csum, pbd_e1x->tcp_send_seq,
2868 le16_to_cpu(pbd_e1x->total_hlen_w));
2869 if (pbd_e2)
2870 DP(NETIF_MSG_TX_QUEUED,
2871 "PBD (E2) @%p dst %x %x %x src %x %x %x parsing_data %x\n",
2872 pbd_e2, pbd_e2->dst_mac_addr_hi, pbd_e2->dst_mac_addr_mid,
2873 pbd_e2->dst_mac_addr_lo, pbd_e2->src_mac_addr_hi,
2874 pbd_e2->src_mac_addr_mid, pbd_e2->src_mac_addr_lo,
2875 pbd_e2->parsing_data);
2876 DP(NETIF_MSG_TX_QUEUED, "doorbell: nbd %d bd %u\n", nbd, bd_prod);
2878 txdata->tx_pkt_prod++;
2880 * Make sure that the BD data is updated before updating the producer
2881 * since FW might read the BD right after the producer is updated.
2882 * This is only applicable for weak-ordered memory model archs such
2883 * as IA-64. The following barrier is also mandatory since FW will
2884 * assumes packets must have BDs.
2886 wmb();
2888 txdata->tx_db.data.prod += nbd;
2889 barrier();
2891 DOORBELL(bp, txdata->cid, txdata->tx_db.raw);
2893 mmiowb();
2895 txdata->tx_bd_prod += nbd;
2897 if (unlikely(bnx2x_tx_avail(bp, txdata) < MAX_SKB_FRAGS + 3)) {
2898 netif_tx_stop_queue(txq);
2900 /* paired memory barrier is in bnx2x_tx_int(), we have to keep
2901 * ordering of set_bit() in netif_tx_stop_queue() and read of
2902 * fp->bd_tx_cons */
2903 smp_mb();
2905 fp->eth_q_stats.driver_xoff++;
2906 if (bnx2x_tx_avail(bp, txdata) >= MAX_SKB_FRAGS + 3)
2907 netif_tx_wake_queue(txq);
2909 txdata->tx_pkt++;
2911 return NETDEV_TX_OK;
2915 * bnx2x_setup_tc - routine to configure net_device for multi tc
2917 * @netdev: net device to configure
2918 * @tc: number of traffic classes to enable
2920 * callback connected to the ndo_setup_tc function pointer
2922 int bnx2x_setup_tc(struct net_device *dev, u8 num_tc)
2924 int cos, prio, count, offset;
2925 struct bnx2x *bp = netdev_priv(dev);
2927 /* setup tc must be called under rtnl lock */
2928 ASSERT_RTNL();
2930 /* no traffic classes requested. aborting */
2931 if (!num_tc) {
2932 netdev_reset_tc(dev);
2933 return 0;
2936 /* requested to support too many traffic classes */
2937 if (num_tc > bp->max_cos) {
2938 DP(NETIF_MSG_TX_ERR, "support for too many traffic classes"
2939 " requested: %d. max supported is %d\n",
2940 num_tc, bp->max_cos);
2941 return -EINVAL;
2944 /* declare amount of supported traffic classes */
2945 if (netdev_set_num_tc(dev, num_tc)) {
2946 DP(NETIF_MSG_TX_ERR, "failed to declare %d traffic classes\n",
2947 num_tc);
2948 return -EINVAL;
2951 /* configure priority to traffic class mapping */
2952 for (prio = 0; prio < BNX2X_MAX_PRIORITY; prio++) {
2953 netdev_set_prio_tc_map(dev, prio, bp->prio_to_cos[prio]);
2954 DP(BNX2X_MSG_SP, "mapping priority %d to tc %d\n",
2955 prio, bp->prio_to_cos[prio]);
2959 /* Use this configuration to diffrentiate tc0 from other COSes
2960 This can be used for ets or pfc, and save the effort of setting
2961 up a multio class queue disc or negotiating DCBX with a switch
2962 netdev_set_prio_tc_map(dev, 0, 0);
2963 DP(BNX2X_MSG_SP, "mapping priority %d to tc %d\n", 0, 0);
2964 for (prio = 1; prio < 16; prio++) {
2965 netdev_set_prio_tc_map(dev, prio, 1);
2966 DP(BNX2X_MSG_SP, "mapping priority %d to tc %d\n", prio, 1);
2967 } */
2969 /* configure traffic class to transmission queue mapping */
2970 for (cos = 0; cos < bp->max_cos; cos++) {
2971 count = BNX2X_NUM_ETH_QUEUES(bp);
2972 offset = cos * MAX_TXQS_PER_COS;
2973 netdev_set_tc_queue(dev, cos, count, offset);
2974 DP(BNX2X_MSG_SP, "mapping tc %d to offset %d count %d\n",
2975 cos, offset, count);
2978 return 0;
2981 /* called with rtnl_lock */
2982 int bnx2x_change_mac_addr(struct net_device *dev, void *p)
2984 struct sockaddr *addr = p;
2985 struct bnx2x *bp = netdev_priv(dev);
2986 int rc = 0;
2988 if (!is_valid_ether_addr((u8 *)(addr->sa_data)))
2989 return -EINVAL;
2991 if (netif_running(dev)) {
2992 rc = bnx2x_set_eth_mac(bp, false);
2993 if (rc)
2994 return rc;
2997 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2999 if (netif_running(dev))
3000 rc = bnx2x_set_eth_mac(bp, true);
3002 return rc;
3005 static void bnx2x_free_fp_mem_at(struct bnx2x *bp, int fp_index)
3007 union host_hc_status_block *sb = &bnx2x_fp(bp, fp_index, status_blk);
3008 struct bnx2x_fastpath *fp = &bp->fp[fp_index];
3009 u8 cos;
3011 /* Common */
3012 #ifdef BCM_CNIC
3013 if (IS_FCOE_IDX(fp_index)) {
3014 memset(sb, 0, sizeof(union host_hc_status_block));
3015 fp->status_blk_mapping = 0;
3017 } else {
3018 #endif
3019 /* status blocks */
3020 if (!CHIP_IS_E1x(bp))
3021 BNX2X_PCI_FREE(sb->e2_sb,
3022 bnx2x_fp(bp, fp_index,
3023 status_blk_mapping),
3024 sizeof(struct host_hc_status_block_e2));
3025 else
3026 BNX2X_PCI_FREE(sb->e1x_sb,
3027 bnx2x_fp(bp, fp_index,
3028 status_blk_mapping),
3029 sizeof(struct host_hc_status_block_e1x));
3030 #ifdef BCM_CNIC
3032 #endif
3033 /* Rx */
3034 if (!skip_rx_queue(bp, fp_index)) {
3035 bnx2x_free_rx_bds(fp);
3037 /* fastpath rx rings: rx_buf rx_desc rx_comp */
3038 BNX2X_FREE(bnx2x_fp(bp, fp_index, rx_buf_ring));
3039 BNX2X_PCI_FREE(bnx2x_fp(bp, fp_index, rx_desc_ring),
3040 bnx2x_fp(bp, fp_index, rx_desc_mapping),
3041 sizeof(struct eth_rx_bd) * NUM_RX_BD);
3043 BNX2X_PCI_FREE(bnx2x_fp(bp, fp_index, rx_comp_ring),
3044 bnx2x_fp(bp, fp_index, rx_comp_mapping),
3045 sizeof(struct eth_fast_path_rx_cqe) *
3046 NUM_RCQ_BD);
3048 /* SGE ring */
3049 BNX2X_FREE(bnx2x_fp(bp, fp_index, rx_page_ring));
3050 BNX2X_PCI_FREE(bnx2x_fp(bp, fp_index, rx_sge_ring),
3051 bnx2x_fp(bp, fp_index, rx_sge_mapping),
3052 BCM_PAGE_SIZE * NUM_RX_SGE_PAGES);
3055 /* Tx */
3056 if (!skip_tx_queue(bp, fp_index)) {
3057 /* fastpath tx rings: tx_buf tx_desc */
3058 for_each_cos_in_tx_queue(fp, cos) {
3059 struct bnx2x_fp_txdata *txdata = &fp->txdata[cos];
3061 DP(BNX2X_MSG_SP,
3062 "freeing tx memory of fp %d cos %d cid %d\n",
3063 fp_index, cos, txdata->cid);
3065 BNX2X_FREE(txdata->tx_buf_ring);
3066 BNX2X_PCI_FREE(txdata->tx_desc_ring,
3067 txdata->tx_desc_mapping,
3068 sizeof(union eth_tx_bd_types) * NUM_TX_BD);
3071 /* end of fastpath */
3074 void bnx2x_free_fp_mem(struct bnx2x *bp)
3076 int i;
3077 for_each_queue(bp, i)
3078 bnx2x_free_fp_mem_at(bp, i);
3081 static inline void set_sb_shortcuts(struct bnx2x *bp, int index)
3083 union host_hc_status_block status_blk = bnx2x_fp(bp, index, status_blk);
3084 if (!CHIP_IS_E1x(bp)) {
3085 bnx2x_fp(bp, index, sb_index_values) =
3086 (__le16 *)status_blk.e2_sb->sb.index_values;
3087 bnx2x_fp(bp, index, sb_running_index) =
3088 (__le16 *)status_blk.e2_sb->sb.running_index;
3089 } else {
3090 bnx2x_fp(bp, index, sb_index_values) =
3091 (__le16 *)status_blk.e1x_sb->sb.index_values;
3092 bnx2x_fp(bp, index, sb_running_index) =
3093 (__le16 *)status_blk.e1x_sb->sb.running_index;
3097 static int bnx2x_alloc_fp_mem_at(struct bnx2x *bp, int index)
3099 union host_hc_status_block *sb;
3100 struct bnx2x_fastpath *fp = &bp->fp[index];
3101 int ring_size = 0;
3102 u8 cos;
3104 /* if rx_ring_size specified - use it */
3105 int rx_ring_size = bp->rx_ring_size ? bp->rx_ring_size :
3106 MAX_RX_AVAIL/BNX2X_NUM_RX_QUEUES(bp);
3108 /* allocate at least number of buffers required by FW */
3109 rx_ring_size = max_t(int, bp->disable_tpa ? MIN_RX_SIZE_NONTPA :
3110 MIN_RX_SIZE_TPA,
3111 rx_ring_size);
3113 /* Common */
3114 sb = &bnx2x_fp(bp, index, status_blk);
3115 #ifdef BCM_CNIC
3116 if (!IS_FCOE_IDX(index)) {
3117 #endif
3118 /* status blocks */
3119 if (!CHIP_IS_E1x(bp))
3120 BNX2X_PCI_ALLOC(sb->e2_sb,
3121 &bnx2x_fp(bp, index, status_blk_mapping),
3122 sizeof(struct host_hc_status_block_e2));
3123 else
3124 BNX2X_PCI_ALLOC(sb->e1x_sb,
3125 &bnx2x_fp(bp, index, status_blk_mapping),
3126 sizeof(struct host_hc_status_block_e1x));
3127 #ifdef BCM_CNIC
3129 #endif
3131 /* FCoE Queue uses Default SB and doesn't ACK the SB, thus no need to
3132 * set shortcuts for it.
3134 if (!IS_FCOE_IDX(index))
3135 set_sb_shortcuts(bp, index);
3137 /* Tx */
3138 if (!skip_tx_queue(bp, index)) {
3139 /* fastpath tx rings: tx_buf tx_desc */
3140 for_each_cos_in_tx_queue(fp, cos) {
3141 struct bnx2x_fp_txdata *txdata = &fp->txdata[cos];
3143 DP(BNX2X_MSG_SP, "allocating tx memory of "
3144 "fp %d cos %d\n",
3145 index, cos);
3147 BNX2X_ALLOC(txdata->tx_buf_ring,
3148 sizeof(struct sw_tx_bd) * NUM_TX_BD);
3149 BNX2X_PCI_ALLOC(txdata->tx_desc_ring,
3150 &txdata->tx_desc_mapping,
3151 sizeof(union eth_tx_bd_types) * NUM_TX_BD);
3155 /* Rx */
3156 if (!skip_rx_queue(bp, index)) {
3157 /* fastpath rx rings: rx_buf rx_desc rx_comp */
3158 BNX2X_ALLOC(bnx2x_fp(bp, index, rx_buf_ring),
3159 sizeof(struct sw_rx_bd) * NUM_RX_BD);
3160 BNX2X_PCI_ALLOC(bnx2x_fp(bp, index, rx_desc_ring),
3161 &bnx2x_fp(bp, index, rx_desc_mapping),
3162 sizeof(struct eth_rx_bd) * NUM_RX_BD);
3164 BNX2X_PCI_ALLOC(bnx2x_fp(bp, index, rx_comp_ring),
3165 &bnx2x_fp(bp, index, rx_comp_mapping),
3166 sizeof(struct eth_fast_path_rx_cqe) *
3167 NUM_RCQ_BD);
3169 /* SGE ring */
3170 BNX2X_ALLOC(bnx2x_fp(bp, index, rx_page_ring),
3171 sizeof(struct sw_rx_page) * NUM_RX_SGE);
3172 BNX2X_PCI_ALLOC(bnx2x_fp(bp, index, rx_sge_ring),
3173 &bnx2x_fp(bp, index, rx_sge_mapping),
3174 BCM_PAGE_SIZE * NUM_RX_SGE_PAGES);
3175 /* RX BD ring */
3176 bnx2x_set_next_page_rx_bd(fp);
3178 /* CQ ring */
3179 bnx2x_set_next_page_rx_cq(fp);
3181 /* BDs */
3182 ring_size = bnx2x_alloc_rx_bds(fp, rx_ring_size);
3183 if (ring_size < rx_ring_size)
3184 goto alloc_mem_err;
3187 return 0;
3189 /* handles low memory cases */
3190 alloc_mem_err:
3191 BNX2X_ERR("Unable to allocate full memory for queue %d (size %d)\n",
3192 index, ring_size);
3193 /* FW will drop all packets if queue is not big enough,
3194 * In these cases we disable the queue
3195 * Min size is different for OOO, TPA and non-TPA queues
3197 if (ring_size < (fp->disable_tpa ?
3198 MIN_RX_SIZE_NONTPA : MIN_RX_SIZE_TPA)) {
3199 /* release memory allocated for this queue */
3200 bnx2x_free_fp_mem_at(bp, index);
3201 return -ENOMEM;
3203 return 0;
3206 int bnx2x_alloc_fp_mem(struct bnx2x *bp)
3208 int i;
3211 * 1. Allocate FP for leading - fatal if error
3212 * 2. {CNIC} Allocate FCoE FP - fatal if error
3213 * 3. {CNIC} Allocate OOO + FWD - disable OOO if error
3214 * 4. Allocate RSS - fix number of queues if error
3217 /* leading */
3218 if (bnx2x_alloc_fp_mem_at(bp, 0))
3219 return -ENOMEM;
3221 #ifdef BCM_CNIC
3222 if (!NO_FCOE(bp))
3223 /* FCoE */
3224 if (bnx2x_alloc_fp_mem_at(bp, FCOE_IDX))
3225 /* we will fail load process instead of mark
3226 * NO_FCOE_FLAG
3228 return -ENOMEM;
3229 #endif
3231 /* RSS */
3232 for_each_nondefault_eth_queue(bp, i)
3233 if (bnx2x_alloc_fp_mem_at(bp, i))
3234 break;
3236 /* handle memory failures */
3237 if (i != BNX2X_NUM_ETH_QUEUES(bp)) {
3238 int delta = BNX2X_NUM_ETH_QUEUES(bp) - i;
3240 WARN_ON(delta < 0);
3241 #ifdef BCM_CNIC
3243 * move non eth FPs next to last eth FP
3244 * must be done in that order
3245 * FCOE_IDX < FWD_IDX < OOO_IDX
3248 /* move FCoE fp even NO_FCOE_FLAG is on */
3249 bnx2x_move_fp(bp, FCOE_IDX, FCOE_IDX - delta);
3250 #endif
3251 bp->num_queues -= delta;
3252 BNX2X_ERR("Adjusted num of queues from %d to %d\n",
3253 bp->num_queues + delta, bp->num_queues);
3256 return 0;
3259 void bnx2x_free_mem_bp(struct bnx2x *bp)
3261 kfree(bp->fp);
3262 kfree(bp->msix_table);
3263 kfree(bp->ilt);
3266 int __devinit bnx2x_alloc_mem_bp(struct bnx2x *bp)
3268 struct bnx2x_fastpath *fp;
3269 struct msix_entry *tbl;
3270 struct bnx2x_ilt *ilt;
3271 int msix_table_size = 0;
3274 * The biggest MSI-X table we might need is as a maximum number of fast
3275 * path IGU SBs plus default SB (for PF).
3277 msix_table_size = bp->igu_sb_cnt + 1;
3279 /* fp array: RSS plus CNIC related L2 queues */
3280 fp = kzalloc((BNX2X_MAX_RSS_COUNT(bp) + NON_ETH_CONTEXT_USE) *
3281 sizeof(*fp), GFP_KERNEL);
3282 if (!fp)
3283 goto alloc_err;
3284 bp->fp = fp;
3286 /* msix table */
3287 tbl = kzalloc(msix_table_size * sizeof(*tbl), GFP_KERNEL);
3288 if (!tbl)
3289 goto alloc_err;
3290 bp->msix_table = tbl;
3292 /* ilt */
3293 ilt = kzalloc(sizeof(*ilt), GFP_KERNEL);
3294 if (!ilt)
3295 goto alloc_err;
3296 bp->ilt = ilt;
3298 return 0;
3299 alloc_err:
3300 bnx2x_free_mem_bp(bp);
3301 return -ENOMEM;
3305 int bnx2x_reload_if_running(struct net_device *dev)
3307 struct bnx2x *bp = netdev_priv(dev);
3309 if (unlikely(!netif_running(dev)))
3310 return 0;
3312 bnx2x_nic_unload(bp, UNLOAD_NORMAL);
3313 return bnx2x_nic_load(bp, LOAD_NORMAL);
3316 int bnx2x_get_cur_phy_idx(struct bnx2x *bp)
3318 u32 sel_phy_idx = 0;
3319 if (bp->link_params.num_phys <= 1)
3320 return INT_PHY;
3322 if (bp->link_vars.link_up) {
3323 sel_phy_idx = EXT_PHY1;
3324 /* In case link is SERDES, check if the EXT_PHY2 is the one */
3325 if ((bp->link_vars.link_status & LINK_STATUS_SERDES_LINK) &&
3326 (bp->link_params.phy[EXT_PHY2].supported & SUPPORTED_FIBRE))
3327 sel_phy_idx = EXT_PHY2;
3328 } else {
3330 switch (bnx2x_phy_selection(&bp->link_params)) {
3331 case PORT_HW_CFG_PHY_SELECTION_HARDWARE_DEFAULT:
3332 case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY:
3333 case PORT_HW_CFG_PHY_SELECTION_FIRST_PHY_PRIORITY:
3334 sel_phy_idx = EXT_PHY1;
3335 break;
3336 case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY:
3337 case PORT_HW_CFG_PHY_SELECTION_SECOND_PHY_PRIORITY:
3338 sel_phy_idx = EXT_PHY2;
3339 break;
3343 return sel_phy_idx;
3346 int bnx2x_get_link_cfg_idx(struct bnx2x *bp)
3348 u32 sel_phy_idx = bnx2x_get_cur_phy_idx(bp);
3350 * The selected actived PHY is always after swapping (in case PHY
3351 * swapping is enabled). So when swapping is enabled, we need to reverse
3352 * the configuration
3355 if (bp->link_params.multi_phy_config &
3356 PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
3357 if (sel_phy_idx == EXT_PHY1)
3358 sel_phy_idx = EXT_PHY2;
3359 else if (sel_phy_idx == EXT_PHY2)
3360 sel_phy_idx = EXT_PHY1;
3362 return LINK_CONFIG_IDX(sel_phy_idx);
3365 #if defined(NETDEV_FCOE_WWNN) && defined(BCM_CNIC)
3366 int bnx2x_fcoe_get_wwn(struct net_device *dev, u64 *wwn, int type)
3368 struct bnx2x *bp = netdev_priv(dev);
3369 struct cnic_eth_dev *cp = &bp->cnic_eth_dev;
3371 switch (type) {
3372 case NETDEV_FCOE_WWNN:
3373 *wwn = HILO_U64(cp->fcoe_wwn_node_name_hi,
3374 cp->fcoe_wwn_node_name_lo);
3375 break;
3376 case NETDEV_FCOE_WWPN:
3377 *wwn = HILO_U64(cp->fcoe_wwn_port_name_hi,
3378 cp->fcoe_wwn_port_name_lo);
3379 break;
3380 default:
3381 return -EINVAL;
3384 return 0;
3386 #endif
3388 /* called with rtnl_lock */
3389 int bnx2x_change_mtu(struct net_device *dev, int new_mtu)
3391 struct bnx2x *bp = netdev_priv(dev);
3393 if (bp->recovery_state != BNX2X_RECOVERY_DONE) {
3394 pr_err("Handling parity error recovery. Try again later\n");
3395 return -EAGAIN;
3398 if ((new_mtu > ETH_MAX_JUMBO_PACKET_SIZE) ||
3399 ((new_mtu + ETH_HLEN) < ETH_MIN_PACKET_SIZE))
3400 return -EINVAL;
3402 /* This does not race with packet allocation
3403 * because the actual alloc size is
3404 * only updated as part of load
3406 dev->mtu = new_mtu;
3408 return bnx2x_reload_if_running(dev);
3411 u32 bnx2x_fix_features(struct net_device *dev, u32 features)
3413 struct bnx2x *bp = netdev_priv(dev);
3415 /* TPA requires Rx CSUM offloading */
3416 if (!(features & NETIF_F_RXCSUM) || bp->disable_tpa)
3417 features &= ~NETIF_F_LRO;
3419 return features;
3422 int bnx2x_set_features(struct net_device *dev, u32 features)
3424 struct bnx2x *bp = netdev_priv(dev);
3425 u32 flags = bp->flags;
3426 bool bnx2x_reload = false;
3428 if (features & NETIF_F_LRO)
3429 flags |= TPA_ENABLE_FLAG;
3430 else
3431 flags &= ~TPA_ENABLE_FLAG;
3433 if (features & NETIF_F_LOOPBACK) {
3434 if (bp->link_params.loopback_mode != LOOPBACK_BMAC) {
3435 bp->link_params.loopback_mode = LOOPBACK_BMAC;
3436 bnx2x_reload = true;
3438 } else {
3439 if (bp->link_params.loopback_mode != LOOPBACK_NONE) {
3440 bp->link_params.loopback_mode = LOOPBACK_NONE;
3441 bnx2x_reload = true;
3445 if (flags ^ bp->flags) {
3446 bp->flags = flags;
3447 bnx2x_reload = true;
3450 if (bnx2x_reload) {
3451 if (bp->recovery_state == BNX2X_RECOVERY_DONE)
3452 return bnx2x_reload_if_running(dev);
3453 /* else: bnx2x_nic_load() will be called at end of recovery */
3456 return 0;
3459 void bnx2x_tx_timeout(struct net_device *dev)
3461 struct bnx2x *bp = netdev_priv(dev);
3463 #ifdef BNX2X_STOP_ON_ERROR
3464 if (!bp->panic)
3465 bnx2x_panic();
3466 #endif
3468 smp_mb__before_clear_bit();
3469 set_bit(BNX2X_SP_RTNL_TX_TIMEOUT, &bp->sp_rtnl_state);
3470 smp_mb__after_clear_bit();
3472 /* This allows the netif to be shutdown gracefully before resetting */
3473 schedule_delayed_work(&bp->sp_rtnl_task, 0);
3476 int bnx2x_suspend(struct pci_dev *pdev, pm_message_t state)
3478 struct net_device *dev = pci_get_drvdata(pdev);
3479 struct bnx2x *bp;
3481 if (!dev) {
3482 dev_err(&pdev->dev, "BAD net device from bnx2x_init_one\n");
3483 return -ENODEV;
3485 bp = netdev_priv(dev);
3487 rtnl_lock();
3489 pci_save_state(pdev);
3491 if (!netif_running(dev)) {
3492 rtnl_unlock();
3493 return 0;
3496 netif_device_detach(dev);
3498 bnx2x_nic_unload(bp, UNLOAD_CLOSE);
3500 bnx2x_set_power_state(bp, pci_choose_state(pdev, state));
3502 rtnl_unlock();
3504 return 0;
3507 int bnx2x_resume(struct pci_dev *pdev)
3509 struct net_device *dev = pci_get_drvdata(pdev);
3510 struct bnx2x *bp;
3511 int rc;
3513 if (!dev) {
3514 dev_err(&pdev->dev, "BAD net device from bnx2x_init_one\n");
3515 return -ENODEV;
3517 bp = netdev_priv(dev);
3519 if (bp->recovery_state != BNX2X_RECOVERY_DONE) {
3520 pr_err("Handling parity error recovery. Try again later\n");
3521 return -EAGAIN;
3524 rtnl_lock();
3526 pci_restore_state(pdev);
3528 if (!netif_running(dev)) {
3529 rtnl_unlock();
3530 return 0;
3533 bnx2x_set_power_state(bp, PCI_D0);
3534 netif_device_attach(dev);
3536 /* Since the chip was reset, clear the FW sequence number */
3537 bp->fw_seq = 0;
3538 rc = bnx2x_nic_load(bp, LOAD_OPEN);
3540 rtnl_unlock();
3542 return rc;
3546 void bnx2x_set_ctx_validation(struct bnx2x *bp, struct eth_context *cxt,
3547 u32 cid)
3549 /* ustorm cxt validation */
3550 cxt->ustorm_ag_context.cdu_usage =
3551 CDU_RSRVD_VALUE_TYPE_A(HW_CID(bp, cid),
3552 CDU_REGION_NUMBER_UCM_AG, ETH_CONNECTION_TYPE);
3553 /* xcontext validation */
3554 cxt->xstorm_ag_context.cdu_reserved =
3555 CDU_RSRVD_VALUE_TYPE_A(HW_CID(bp, cid),
3556 CDU_REGION_NUMBER_XCM_AG, ETH_CONNECTION_TYPE);
3559 static inline void storm_memset_hc_timeout(struct bnx2x *bp, u8 port,
3560 u8 fw_sb_id, u8 sb_index,
3561 u8 ticks)
3564 u32 addr = BAR_CSTRORM_INTMEM +
3565 CSTORM_STATUS_BLOCK_DATA_TIMEOUT_OFFSET(fw_sb_id, sb_index);
3566 REG_WR8(bp, addr, ticks);
3567 DP(NETIF_MSG_HW, "port %x fw_sb_id %d sb_index %d ticks %d\n",
3568 port, fw_sb_id, sb_index, ticks);
3571 static inline void storm_memset_hc_disable(struct bnx2x *bp, u8 port,
3572 u16 fw_sb_id, u8 sb_index,
3573 u8 disable)
3575 u32 enable_flag = disable ? 0 : (1 << HC_INDEX_DATA_HC_ENABLED_SHIFT);
3576 u32 addr = BAR_CSTRORM_INTMEM +
3577 CSTORM_STATUS_BLOCK_DATA_FLAGS_OFFSET(fw_sb_id, sb_index);
3578 u16 flags = REG_RD16(bp, addr);
3579 /* clear and set */
3580 flags &= ~HC_INDEX_DATA_HC_ENABLED;
3581 flags |= enable_flag;
3582 REG_WR16(bp, addr, flags);
3583 DP(NETIF_MSG_HW, "port %x fw_sb_id %d sb_index %d disable %d\n",
3584 port, fw_sb_id, sb_index, disable);
3587 void bnx2x_update_coalesce_sb_index(struct bnx2x *bp, u8 fw_sb_id,
3588 u8 sb_index, u8 disable, u16 usec)
3590 int port = BP_PORT(bp);
3591 u8 ticks = usec / BNX2X_BTR;
3593 storm_memset_hc_timeout(bp, port, fw_sb_id, sb_index, ticks);
3595 disable = disable ? 1 : (usec ? 0 : 1);
3596 storm_memset_hc_disable(bp, port, fw_sb_id, sb_index, disable);