BNX2X: spatch ether->mtu -> ether->maxmtu
[akaros.git] / kern / drivers / net / bnx2x / bnx2x_cmn.c
1 /* bnx2x_cmn.c: Broadcom Everest network driver.
2  *
3  * Copyright (c) 2007-2013 Broadcom Corporation
4  *
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.
8  *
9  * Maintained by: Ariel Elior <ariel.elior@qlogic.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
15  *
16  */
17
18 #include "akaros_compat.h"
19
20 #include "bnx2x_cmn.h"
21 #include "bnx2x_init.h"
22 #include "bnx2x_sp.h"
23
24 static void bnx2x_free_fp_mem_cnic(struct bnx2x *bp);
25 static int bnx2x_alloc_fp_mem_cnic(struct bnx2x *bp);
26 static int bnx2x_alloc_fp_mem(struct bnx2x *bp);
27 static int bnx2x_poll(struct napi_struct *napi, int budget);
28
29 static void bnx2x_add_all_napi_cnic(struct bnx2x *bp)
30 {
31         int i;
32
33         /* Add NAPI objects */
34         for_each_rx_queue_cnic(bp, i) {
35                 netif_napi_add(bp->dev, &bnx2x_fp(bp, i, napi),
36                                bnx2x_poll, NAPI_POLL_WEIGHT);
37                 napi_hash_add(&bnx2x_fp(bp, i, napi));
38         }
39 }
40
41 static void bnx2x_add_all_napi(struct bnx2x *bp)
42 {
43         int i;
44
45         /* Add NAPI objects */
46         for_each_eth_queue(bp, i) {
47                 netif_napi_add(bp->dev, &bnx2x_fp(bp, i, napi),
48                                bnx2x_poll, NAPI_POLL_WEIGHT);
49                 napi_hash_add(&bnx2x_fp(bp, i, napi));
50         }
51 }
52
53 static int bnx2x_calc_num_queues(struct bnx2x *bp)
54 {
55         int nq = bnx2x_num_queues ? : netif_get_num_default_rss_queues();
56
57         /* Reduce memory usage in kdump environment by using only one queue */
58         if (is_kdump_kernel())
59                 nq = 1;
60
61         nq = CLAMP(nq, 1, BNX2X_MAX_QUEUES(bp));
62         return nq;
63 }
64
65 /**
66  * bnx2x_move_fp - move content of the fastpath structure.
67  *
68  * @bp:         driver handle
69  * @from:       source FP index
70  * @to:         destination FP index
71  *
72  * Makes sure the contents of the bp->fp[to].napi is kept
73  * intact. This is done by first copying the napi struct from
74  * the target to the source, and then mem copying the entire
75  * source onto the target. Update txdata pointers and related
76  * content.
77  */
78 static inline void bnx2x_move_fp(struct bnx2x *bp, int from, int to)
79 {
80         struct bnx2x_fastpath *from_fp = &bp->fp[from];
81         struct bnx2x_fastpath *to_fp = &bp->fp[to];
82         struct bnx2x_sp_objs *from_sp_objs = &bp->sp_objs[from];
83         struct bnx2x_sp_objs *to_sp_objs = &bp->sp_objs[to];
84         struct bnx2x_fp_stats *from_fp_stats = &bp->fp_stats[from];
85         struct bnx2x_fp_stats *to_fp_stats = &bp->fp_stats[to];
86         int old_max_eth_txqs, new_max_eth_txqs;
87         int old_txdata_index = 0, new_txdata_index = 0;
88         struct bnx2x_agg_info *old_tpa_info = to_fp->tpa_info;
89
90         /* Copy the NAPI object as it has been already initialized */
91         from_fp->napi = to_fp->napi;
92
93         /* Move bnx2x_fastpath contents */
94         memcpy(to_fp, from_fp, sizeof(*to_fp));
95         to_fp->index = to;
96
97         /* Retain the tpa_info of the original `to' version as we don't want
98          * 2 FPs to contain the same tpa_info pointer.
99          */
100         to_fp->tpa_info = old_tpa_info;
101
102         /* move sp_objs contents as well, as their indices match fp ones */
103         memcpy(to_sp_objs, from_sp_objs, sizeof(*to_sp_objs));
104
105         /* move fp_stats contents as well, as their indices match fp ones */
106         memcpy(to_fp_stats, from_fp_stats, sizeof(*to_fp_stats));
107
108         /* Update txdata pointers in fp and move txdata content accordingly:
109          * Each fp consumes 'max_cos' txdata structures, so the index should be
110          * decremented by max_cos x delta.
111          */
112
113         old_max_eth_txqs = BNX2X_NUM_ETH_QUEUES(bp) * (bp)->max_cos;
114         new_max_eth_txqs = (BNX2X_NUM_ETH_QUEUES(bp) - from + to) *
115                                 (bp)->max_cos;
116         if (from == FCOE_IDX(bp)) {
117                 old_txdata_index = old_max_eth_txqs + FCOE_TXQ_IDX_OFFSET;
118                 new_txdata_index = new_max_eth_txqs + FCOE_TXQ_IDX_OFFSET;
119         }
120
121         memcpy(&bp->bnx2x_txq[new_txdata_index],
122                &bp->bnx2x_txq[old_txdata_index],
123                sizeof(struct bnx2x_fp_txdata));
124         to_fp->txdata_ptr[0] = &bp->bnx2x_txq[new_txdata_index];
125 }
126
127 /**
128  * bnx2x_fill_fw_str - Fill buffer with FW version string.
129  *
130  * @bp:        driver handle
131  * @buf:       character buffer to fill with the fw name
132  * @buf_len:   length of the above buffer
133  *
134  */
135 void bnx2x_fill_fw_str(struct bnx2x *bp, char *buf, size_t buf_len)
136 {
137         if (IS_PF(bp)) {
138                 uint8_t phy_fw_ver[PHY_FW_VER_LEN];
139
140                 phy_fw_ver[0] = '\0';
141                 bnx2x_get_ext_phy_fw_version(&bp->link_params,
142                                              phy_fw_ver, PHY_FW_VER_LEN);
143                 strlcpy(buf, bp->fw_ver, buf_len);
144                 snprintf(buf + strlen(bp->fw_ver), 32 - strlen(bp->fw_ver),
145                          "bc %d.%d.%d%s%s",
146                          (bp->common.bc_ver & 0xff0000) >> 16,
147                          (bp->common.bc_ver & 0xff00) >> 8,
148                          (bp->common.bc_ver & 0xff),
149                          ((phy_fw_ver[0] != '\0') ? " phy " : ""), phy_fw_ver);
150         } else {
151                 bnx2x_vf_fill_fw_str(bp, buf, buf_len);
152         }
153 }
154
155 /**
156  * bnx2x_shrink_eth_fp - guarantees fastpath structures stay intact
157  *
158  * @bp: driver handle
159  * @delta:      number of eth queues which were not allocated
160  */
161 static void bnx2x_shrink_eth_fp(struct bnx2x *bp, int delta)
162 {
163         int i, cos, old_eth_num = BNX2X_NUM_ETH_QUEUES(bp);
164
165         /* Queue pointer cannot be re-set on an fp-basis, as moving pointer
166          * backward along the array could cause memory to be overridden
167          */
168         for (cos = 1; cos < bp->max_cos; cos++) {
169                 for (i = 0; i < old_eth_num - delta; i++) {
170                         struct bnx2x_fastpath *fp = &bp->fp[i];
171                         int new_idx = cos * (old_eth_num - delta) + i;
172
173                         memcpy(&bp->bnx2x_txq[new_idx], fp->txdata_ptr[cos],
174                                sizeof(struct bnx2x_fp_txdata));
175                         fp->txdata_ptr[cos] = &bp->bnx2x_txq[new_idx];
176                 }
177         }
178 }
179
180 int bnx2x_load_count[2][3] = { {0} }; /* per-path: 0-common, 1-port0, 2-port1 */
181
182 /* free skb in the packet ring at pos idx
183  * return idx of last bd freed
184  */
185 static uint16_t bnx2x_free_tx_pkt(struct bnx2x *bp, struct bnx2x_fp_txdata *txdata,
186                              uint16_t idx, unsigned int *pkts_compl,
187                              unsigned int *bytes_compl)
188 {
189         struct sw_tx_bd *tx_buf = &txdata->tx_buf_ring[idx];
190         struct eth_tx_start_bd *tx_start_bd;
191         struct eth_tx_bd *tx_data_bd;
192         struct sk_buff *skb = tx_buf->skb;
193         uint16_t bd_idx = TX_BD(tx_buf->first_bd), new_cons;
194         int nbd;
195         uint16_t split_bd_len = 0;
196
197         /* prefetch skb end pointer to speedup dev_kfree_skb() */
198         prefetch(&skb->end);
199
200         DP(NETIF_MSG_TX_DONE, "fp[%d]: pkt_idx %d  buff @(%p)->skb %p\n",
201            txdata->txq_index, idx, tx_buf, skb);
202
203         tx_start_bd = &txdata->tx_desc_ring[bd_idx].start_bd;
204
205         nbd = le16_to_cpu(tx_start_bd->nbd) - 1;
206 #ifdef BNX2X_STOP_ON_ERROR
207         if ((nbd - 1) > (MAX_SKB_FRAGS + 2)) {
208                 BNX2X_ERR("BAD nbd!\n");
209                 bnx2x_panic();
210         }
211 #endif
212         new_cons = nbd + tx_buf->first_bd;
213
214         /* Get the next bd */
215         bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
216
217         /* Skip a parse bd... */
218         --nbd;
219         bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
220
221         if (tx_buf->flags & BNX2X_HAS_SECOND_PBD) {
222                 /* Skip second parse bd... */
223                 --nbd;
224                 bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
225         }
226
227         /* TSO headers+data bds share a common mapping. See bnx2x_tx_split() */
228         if (tx_buf->flags & BNX2X_TSO_SPLIT_BD) {
229                 tx_data_bd = &txdata->tx_desc_ring[bd_idx].reg_bd;
230                 split_bd_len = BD_UNMAP_LEN(tx_data_bd);
231                 --nbd;
232                 bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
233         }
234
235         /* unmap first bd */
236         dma_unmap_single(&bp->pdev->dev, BD_UNMAP_ADDR(tx_start_bd),
237                          BD_UNMAP_LEN(tx_start_bd) + split_bd_len,
238                          DMA_TO_DEVICE);
239
240         /* now free frags */
241         while (nbd > 0) {
242
243                 tx_data_bd = &txdata->tx_desc_ring[bd_idx].reg_bd;
244                 dma_unmap_page(&bp->pdev->dev, BD_UNMAP_ADDR(tx_data_bd),
245                                BD_UNMAP_LEN(tx_data_bd), DMA_TO_DEVICE);
246                 if (--nbd)
247                         bd_idx = TX_BD(NEXT_TX_IDX(bd_idx));
248         }
249
250         /* release skb */
251         WARN_ON(!skb);
252         if (likely(skb)) {
253                 (*pkts_compl)++;
254                 (*bytes_compl) += skb->len;
255         }
256
257         dev_kfree_skb_any(skb);
258         tx_buf->first_bd = 0;
259         tx_buf->skb = NULL;
260
261         return new_cons;
262 }
263
264 int bnx2x_tx_int(struct bnx2x *bp, struct bnx2x_fp_txdata *txdata)
265 {
266         struct netdev_queue *txq;
267         uint16_t hw_cons, sw_cons, bd_cons = txdata->tx_bd_cons;
268         unsigned int pkts_compl = 0, bytes_compl = 0;
269
270 #ifdef BNX2X_STOP_ON_ERROR
271         if (unlikely(bp->panic))
272                 return -1;
273 #endif
274
275         txq = netdev_get_tx_queue(bp->dev, txdata->txq_index);
276         hw_cons = le16_to_cpu(*txdata->tx_cons_sb);
277         sw_cons = txdata->tx_pkt_cons;
278
279         while (sw_cons != hw_cons) {
280                 uint16_t pkt_cons;
281
282                 pkt_cons = TX_BD(sw_cons);
283
284                 DP(NETIF_MSG_TX_DONE,
285                    "queue[%d]: hw_cons %u  sw_cons %u  pkt_cons %u\n",
286                    txdata->txq_index, hw_cons, sw_cons, pkt_cons);
287
288                 bd_cons = bnx2x_free_tx_pkt(bp, txdata, pkt_cons,
289                                             &pkts_compl, &bytes_compl);
290
291                 sw_cons++;
292         }
293
294         netdev_tx_completed_queue(txq, pkts_compl, bytes_compl);
295
296         txdata->tx_pkt_cons = sw_cons;
297         txdata->tx_bd_cons = bd_cons;
298
299         /* Need to make the tx_bd_cons update visible to start_xmit()
300          * before checking for netif_tx_queue_stopped().  Without the
301          * memory barrier, there is a small possibility that
302          * start_xmit() will miss it and cause the queue to be stopped
303          * forever.
304          * On the other hand we need an rmb() here to ensure the proper
305          * ordering of bit testing in the following
306          * netif_tx_queue_stopped(txq) call.
307          */
308         mb();
309
310         if (unlikely(netif_tx_queue_stopped(txq))) {
311                 /* Taking tx_lock() is needed to prevent re-enabling the queue
312                  * while it's empty. This could have happen if rx_action() gets
313                  * suspended in bnx2x_tx_int() after the condition before
314                  * netif_tx_wake_queue(), while tx_action (bnx2x_start_xmit()):
315                  *
316                  * stops the queue->sees fresh tx_bd_cons->releases the queue->
317                  * sends some packets consuming the whole queue again->
318                  * stops the queue
319                  */
320
321                 __netif_tx_lock(txq, core_id());
322
323                 if ((netif_tx_queue_stopped(txq)) &&
324                     (bp->state == BNX2X_STATE_OPEN) &&
325                     (bnx2x_tx_avail(bp, txdata) >= MAX_DESC_PER_TX_PKT))
326                         netif_tx_wake_queue(txq);
327
328                 __netif_tx_unlock(txq);
329         }
330         return 0;
331 }
332
333 static inline void bnx2x_update_last_max_sge(struct bnx2x_fastpath *fp,
334                                              uint16_t idx)
335 {
336         uint16_t last_max = fp->last_max_sge;
337
338         if (SUB_S16(idx, last_max) > 0)
339                 fp->last_max_sge = idx;
340 }
341
342 static inline void bnx2x_update_sge_prod(struct bnx2x_fastpath *fp,
343                                          uint16_t sge_len,
344                                          struct eth_end_agg_rx_cqe *cqe)
345 {
346         struct bnx2x *bp = fp->bp;
347         uint16_t last_max, last_elem, first_elem;
348         uint16_t delta = 0;
349         uint16_t i;
350
351         if (!sge_len)
352                 return;
353
354         /* First mark all used pages */
355         for (i = 0; i < sge_len; i++)
356                 BIT_VEC64_CLEAR_BIT(fp->sge_mask,
357                         RX_SGE(le16_to_cpu(cqe->sgl_or_raw_data.sgl[i])));
358
359         DP(NETIF_MSG_RX_STATUS, "fp_cqe->sgl[%d] = %d\n",
360            sge_len - 1, le16_to_cpu(cqe->sgl_or_raw_data.sgl[sge_len - 1]));
361
362         /* Here we assume that the last SGE index is the biggest */
363         prefetch((void *)(fp->sge_mask));
364         bnx2x_update_last_max_sge(fp,
365                 le16_to_cpu(cqe->sgl_or_raw_data.sgl[sge_len - 1]));
366
367         last_max = RX_SGE(fp->last_max_sge);
368         last_elem = last_max >> BIT_VEC64_ELEM_SHIFT;
369         first_elem = RX_SGE(fp->rx_sge_prod) >> BIT_VEC64_ELEM_SHIFT;
370
371         /* If ring is not full */
372         if (last_elem + 1 != first_elem)
373                 last_elem++;
374
375         /* Now update the prod */
376         for (i = first_elem; i != last_elem; i = NEXT_SGE_MASK_ELEM(i)) {
377                 if (likely(fp->sge_mask[i]))
378                         break;
379
380                 fp->sge_mask[i] = BIT_VEC64_ELEM_ONE_MASK;
381                 delta += BIT_VEC64_ELEM_SZ;
382         }
383
384         if (delta > 0) {
385                 fp->rx_sge_prod += delta;
386                 /* clear page-end entries */
387                 bnx2x_clear_sge_mask_next_elems(fp);
388         }
389
390         DP(NETIF_MSG_RX_STATUS,
391            "fp->last_max_sge = %d  fp->rx_sge_prod = %d\n",
392            fp->last_max_sge, fp->rx_sge_prod);
393 }
394
395 /* Get Toeplitz hash value in the skb using the value from the
396  * CQE (calculated by HW).
397  */
398 static uint32_t bnx2x_get_rxhash(const struct bnx2x *bp,
399                             const struct eth_fast_path_rx_cqe *cqe,
400                             enum pkt_hash_types *rxhash_type)
401 {
402         /* Get Toeplitz hash from CQE */
403         if ((bp->dev->feat & NETIF_F_RXHASH) &&
404             (cqe->status_flags & ETH_FAST_PATH_RX_CQE_RSS_HASH_FLG)) {
405                 enum eth_rss_hash_type htype;
406
407                 htype = cqe->status_flags & ETH_FAST_PATH_RX_CQE_RSS_HASH_TYPE;
408                 *rxhash_type = ((htype == TCP_IPV4_HASH_TYPE) ||
409                                 (htype == TCP_IPV6_HASH_TYPE)) ?
410                                PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3;
411
412                 return le32_to_cpu(cqe->rss_hash_result);
413         }
414         *rxhash_type = PKT_HASH_TYPE_NONE;
415         return 0;
416 }
417
418 static void bnx2x_tpa_start(struct bnx2x_fastpath *fp, uint16_t queue,
419                             uint16_t cons, uint16_t prod,
420                             struct eth_fast_path_rx_cqe *cqe)
421 {
422         struct bnx2x *bp = fp->bp;
423         struct sw_rx_bd *cons_rx_buf = &fp->rx_buf_ring[cons];
424         struct sw_rx_bd *prod_rx_buf = &fp->rx_buf_ring[prod];
425         struct eth_rx_bd *prod_bd = &fp->rx_desc_ring[prod];
426         dma_addr_t mapping;
427         struct bnx2x_agg_info *tpa_info = &fp->tpa_info[queue];
428         struct sw_rx_bd *first_buf = &tpa_info->first_buf;
429
430         /* print error if current state != stop */
431         if (tpa_info->tpa_state != BNX2X_TPA_STOP)
432                 BNX2X_ERR("start of bin not in stop [%d]\n", queue);
433
434         /* Try to map an empty data buffer from the aggregation info  */
435         mapping = dma_map_single(&bp->pdev->dev,
436                                  first_buf->data + NET_SKB_PAD,
437                                  fp->rx_buf_size, DMA_FROM_DEVICE);
438         /*
439          *  ...if it fails - move the skb from the consumer to the producer
440          *  and set the current aggregation state as ERROR to drop it
441          *  when TPA_STOP arrives.
442          */
443
444         if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
445                 /* Move the BD from the consumer to the producer */
446                 bnx2x_reuse_rx_data(fp, cons, prod);
447                 tpa_info->tpa_state = BNX2X_TPA_ERROR;
448                 return;
449         }
450
451         /* move empty data from pool to prod */
452         prod_rx_buf->data = first_buf->data;
453         dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
454         /* point prod_bd to new data */
455         prod_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
456         prod_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
457
458         /* move partial skb from cons to pool (don't unmap yet) */
459         *first_buf = *cons_rx_buf;
460
461         /* mark bin state as START */
462         tpa_info->parsing_flags =
463                 le16_to_cpu(cqe->pars_flags.flags);
464         tpa_info->vlan_tag = le16_to_cpu(cqe->vlan_tag);
465         tpa_info->tpa_state = BNX2X_TPA_START;
466         tpa_info->len_on_bd = le16_to_cpu(cqe->len_on_bd);
467         tpa_info->placement_offset = cqe->placement_offset;
468         tpa_info->rxhash = bnx2x_get_rxhash(bp, cqe, &tpa_info->rxhash_type);
469         if (fp->mode == TPA_MODE_GRO) {
470                 uint16_t gro_size = le16_to_cpu(cqe->pkt_len_or_gro_seg_len);
471                 tpa_info->full_page = SGE_PAGES / gro_size * gro_size;
472                 tpa_info->gro_size = gro_size;
473         }
474
475 #ifdef BNX2X_STOP_ON_ERROR
476         fp->tpa_queue_used |= (1 << queue);
477         DP(NETIF_MSG_RX_STATUS, "fp->tpa_queue_used = 0x%llx\n",
478            fp->tpa_queue_used);
479 #endif
480 }
481
482 /* Timestamp option length allowed for TPA aggregation:
483  *
484  *              nop nop kind length echo val
485  */
486 #define TPA_TSTAMP_OPT_LEN      12
487 /**
488  * bnx2x_set_gro_params - compute GRO values
489  *
490  * @skb:                packet skb
491  * @parsing_flags:      parsing flags from the START CQE
492  * @len_on_bd:          total length of the first packet for the
493  *                      aggregation.
494  * @pkt_len:            length of all segments
495  *
496  * Approximate value of the MSS for this aggregation calculated using
497  * the first packet of it.
498  * Compute number of aggregated segments, and gso_type.
499  */
500 static void bnx2x_set_gro_params(struct sk_buff *skb, uint16_t parsing_flags,
501                                  uint16_t len_on_bd, unsigned int pkt_len,
502                                  uint16_t num_of_coalesced_segs)
503 {
504         /* TPA aggregation won't have either IP options or TCP options
505          * other than timestamp or IPv6 extension headers.
506          */
507         uint16_t hdrs_len = ETHERHDRSIZE + sizeof(struct tcphdr);
508
509         if (GET_FLAG(parsing_flags, PARSING_FLAGS_OVER_ETHERNET_PROTOCOL) ==
510             PRS_FLAG_OVERETH_IPV6) {
511                 hdrs_len += sizeof(struct ipv6hdr);
512                 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
513         } else {
514                 hdrs_len += sizeof(struct iphdr);
515                 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
516         }
517
518         /* Check if there was a TCP timestamp, if there is it's will
519          * always be 12 bytes length: nop nop kind length echo val.
520          *
521          * Otherwise FW would close the aggregation.
522          */
523         if (parsing_flags & PARSING_FLAGS_TIME_STAMP_EXIST_FLAG)
524                 hdrs_len += TPA_TSTAMP_OPT_LEN;
525
526         skb_shinfo(skb)->gso_size = len_on_bd - hdrs_len;
527
528         /* tcp_gro_complete() will copy NAPI_GRO_CB(skb)->count
529          * to skb_shinfo(skb)->gso_segs
530          */
531         NAPI_GRO_CB(skb)->count = num_of_coalesced_segs;
532 }
533
534 static int bnx2x_alloc_rx_sge(struct bnx2x *bp, struct bnx2x_fastpath *fp,
535                               uint16_t index, gfp_t gfp_mask)
536 {
537         struct page *page = get_cont_pages(PAGES_PER_SGE_SHIFT, gfp_mask);
538         struct sw_rx_page *sw_buf = &fp->rx_page_ring[index];
539         struct eth_rx_sge *sge = &fp->rx_sge_ring[index];
540         dma_addr_t mapping;
541
542         if (unlikely(page == NULL)) {
543                 BNX2X_ERR("Can't alloc sge\n");
544                 return -ENOMEM;
545         }
546
547         mapping = dma_map_page(&bp->pdev->dev, page, 0,
548                                SGE_PAGES, DMA_FROM_DEVICE);
549         if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
550                 free_cont_pages(page, PAGES_PER_SGE_SHIFT);
551                 BNX2X_ERR("Can't map sge\n");
552                 return -ENOMEM;
553         }
554
555         sw_buf->page = page;
556         dma_unmap_addr_set(sw_buf, mapping, mapping);
557
558         sge->addr_hi = cpu_to_le32(U64_HI(mapping));
559         sge->addr_lo = cpu_to_le32(U64_LO(mapping));
560
561         return 0;
562 }
563
564 static int bnx2x_fill_frag_skb(struct bnx2x *bp, struct bnx2x_fastpath *fp,
565                                struct bnx2x_agg_info *tpa_info,
566                                uint16_t pages,
567                                struct sk_buff *skb,
568                                struct eth_end_agg_rx_cqe *cqe,
569                                uint16_t cqe_idx)
570 {
571         struct sw_rx_page *rx_pg, old_rx_pg;
572         uint32_t i, frag_len, frag_size;
573         int err, j, frag_id = 0;
574         uint16_t len_on_bd = tpa_info->len_on_bd;
575         uint16_t full_page = 0, gro_size = 0;
576
577         frag_size = le16_to_cpu(cqe->pkt_len) - len_on_bd;
578
579         if (fp->mode == TPA_MODE_GRO) {
580                 gro_size = tpa_info->gro_size;
581                 full_page = tpa_info->full_page;
582         }
583
584         /* This is needed in order to enable forwarding support */
585         if (frag_size)
586                 bnx2x_set_gro_params(skb, tpa_info->parsing_flags, len_on_bd,
587                                      le16_to_cpu(cqe->pkt_len),
588                                      le16_to_cpu(cqe->num_of_coalesced_segs));
589
590 #ifdef BNX2X_STOP_ON_ERROR
591         if (pages > MIN_T(uint32_t, 8, MAX_SKB_FRAGS) * SGE_PAGES) {
592                 BNX2X_ERR("SGL length is too long: %d. CQE index is %d\n",
593                           pages, cqe_idx);
594                 BNX2X_ERR("cqe->pkt_len = %d\n", cqe->pkt_len);
595                 bnx2x_panic();
596                 return -EINVAL;
597         }
598 #endif
599
600         /* Run through the SGL and compose the fragmented skb */
601         for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
602                 uint16_t sge_idx = RX_SGE(le16_to_cpu(cqe->sgl_or_raw_data.sgl[j]));
603
604                 /* FW gives the indices of the SGE as if the ring is an array
605                    (meaning that "next" element will consume 2 indices) */
606                 if (fp->mode == TPA_MODE_GRO)
607                         frag_len = MIN_T(uint32_t, frag_size,
608                                          (uint32_t)full_page);
609                 else /* LRO */
610                         frag_len = MIN_T(uint32_t, frag_size,
611                                          (uint32_t)SGE_PAGES);
612
613                 rx_pg = &fp->rx_page_ring[sge_idx];
614                 old_rx_pg = *rx_pg;
615
616                 /* If we fail to allocate a substitute page, we simply stop
617                    where we are and drop the whole packet */
618                 err = bnx2x_alloc_rx_sge(bp, fp, sge_idx, 0);
619                 if (unlikely(err)) {
620                         bnx2x_fp_qstats(bp, fp)->rx_skb_alloc_failed++;
621                         return err;
622                 }
623
624                 /* Unmap the page as we're going to pass it to the stack */
625                 dma_unmap_page(&bp->pdev->dev,
626                                dma_unmap_addr(&old_rx_pg, mapping),
627                                SGE_PAGES, DMA_FROM_DEVICE);
628                 /* Add one frag and update the appropriate fields in the skb */
629                 if (fp->mode == TPA_MODE_LRO)
630                         skb_fill_page_desc(skb, j, old_rx_pg.page, 0, frag_len);
631                 else { /* GRO */
632                         int rem;
633                         int offset = 0;
634                         for (rem = frag_len; rem > 0; rem -= gro_size) {
635                                 int len = rem > gro_size ? gro_size : rem;
636                                 skb_fill_page_desc(skb, frag_id++,
637                                                    old_rx_pg.page, offset, len);
638                                 if (offset)
639                                         page_incref(old_rx_pg.page);
640                                 offset += len;
641                         }
642                 }
643
644                 skb->data_len += frag_len;
645                 skb->truesize += SGE_PAGES;
646                 skb->len += frag_len;
647
648                 frag_size -= frag_len;
649         }
650
651         return 0;
652 }
653
654 static void bnx2x_frag_free(const struct bnx2x_fastpath *fp, void *data)
655 {
656         if (fp->rx_frag_size)
657                 page_decref(kva2page(data));
658         else
659                 kfree(data);
660 }
661
662 static void *bnx2x_frag_alloc(const struct bnx2x_fastpath *fp, gfp_t gfp_mask)
663 {
664         if (fp->rx_frag_size) {
665                 /* GFP_KERNEL allocations are used only during initialization */
666                 if (unlikely(gfp_mask & KMALLOC_WAIT))
667                         return (void *)kpage_alloc_addr();
668
669                 return netdev_alloc_frag(fp->rx_frag_size);
670         }
671
672         return kmalloc(fp->rx_buf_size + NET_SKB_PAD, gfp_mask);
673 }
674
675 #ifdef CONFIG_INET
676 static void bnx2x_gro_ip_csum(struct bnx2x *bp, struct sk_buff *skb)
677 {
678         const struct iphdr *iph = ip_hdr(skb);
679         struct tcphdr *th;
680
681         skb_set_transport_header(skb, sizeof(struct iphdr));
682         th = tcp_hdr(skb);
683
684         th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb),
685                                   iph->saddr, iph->daddr, 0);
686 }
687
688 static void bnx2x_gro_ipv6_csum(struct bnx2x *bp, struct sk_buff *skb)
689 {
690         struct ipv6hdr *iph = ipv6_hdr(skb);
691         struct tcphdr *th;
692
693         skb_set_transport_header(skb, sizeof(struct ipv6hdr));
694         th = tcp_hdr(skb);
695
696         th->check = ~tcp_v6_check(skb->len - skb_transport_offset(skb),
697                                   &iph->saddr, &iph->daddr, 0);
698 }
699
700 static void bnx2x_gro_csum(struct bnx2x *bp, struct sk_buff *skb,
701                             void (*gro_func)(struct bnx2x*, struct sk_buff*))
702 {
703         skb_set_network_header(skb, 0);
704         gro_func(bp, skb);
705         tcp_gro_complete(skb);
706 }
707 #endif
708
709 static void bnx2x_gro_receive(struct bnx2x *bp, struct bnx2x_fastpath *fp,
710                                struct sk_buff *skb)
711 {
712 #ifdef CONFIG_INET
713         if (skb_shinfo(skb)->gso_size) {
714                 switch (be16_to_cpu(skb->protocol)) {
715                 case ETH_P_IP:
716                         bnx2x_gro_csum(bp, skb, bnx2x_gro_ip_csum);
717                         break;
718                 case ETH_P_IPV6:
719                         bnx2x_gro_csum(bp, skb, bnx2x_gro_ipv6_csum);
720                         break;
721                 default:
722                         BNX2X_ERR("Error: FW GRO supports only IPv4/IPv6, not 0x%04x\n",
723                                   be16_to_cpu(skb->protocol));
724                 }
725         }
726 #endif
727         skb_record_rx_queue(skb, fp->rx_queue);
728         napi_gro_receive(&fp->napi, skb);
729 }
730
731 static void bnx2x_tpa_stop(struct bnx2x *bp, struct bnx2x_fastpath *fp,
732                            struct bnx2x_agg_info *tpa_info,
733                            uint16_t pages,
734                            struct eth_end_agg_rx_cqe *cqe,
735                            uint16_t cqe_idx)
736 {
737         struct sw_rx_bd *rx_buf = &tpa_info->first_buf;
738         uint8_t pad = tpa_info->placement_offset;
739         uint16_t len = tpa_info->len_on_bd;
740         struct sk_buff *skb = NULL;
741         uint8_t *new_data, *data = rx_buf->data;
742         uint8_t old_tpa_state = tpa_info->tpa_state;
743
744         tpa_info->tpa_state = BNX2X_TPA_STOP;
745
746         /* If we there was an error during the handling of the TPA_START -
747          * drop this aggregation.
748          */
749         if (old_tpa_state == BNX2X_TPA_ERROR)
750                 goto drop;
751
752         /* Try to allocate the new data */
753         new_data = bnx2x_frag_alloc(fp, 0);
754         /* Unmap skb in the pool anyway, as we are going to change
755            pool entry status to BNX2X_TPA_STOP even if new skb allocation
756            fails. */
757         dma_unmap_single(&bp->pdev->dev, dma_unmap_addr(rx_buf, mapping),
758                          fp->rx_buf_size, DMA_FROM_DEVICE);
759         if (likely(new_data))
760                 skb = build_skb(data, fp->rx_frag_size);
761
762         if (likely(skb)) {
763 #ifdef BNX2X_STOP_ON_ERROR
764                 if (pad + len > fp->rx_buf_size) {
765                         BNX2X_ERR("skb_put is about to fail...  pad %d  len %d  rx_buf_size %d\n",
766                                   pad, len, fp->rx_buf_size);
767                         bnx2x_panic();
768                         return;
769                 }
770 #endif
771
772                 skb_reserve(skb, pad + NET_SKB_PAD);
773                 skb_put(skb, len);
774                 skb_set_hash(skb, tpa_info->rxhash, tpa_info->rxhash_type);
775
776                 skb->protocol = eth_type_trans(skb, bp->dev);
777                 skb->ip_summed = CHECKSUM_UNNECESSARY;
778
779                 if (!bnx2x_fill_frag_skb(bp, fp, tpa_info, pages,
780                                          skb, cqe, cqe_idx)) {
781                         if (tpa_info->parsing_flags & PARSING_FLAGS_VLAN)
782                                 __vlan_hwaccel_put_tag(skb,
783                                                        cpu_to_be16(ETH_P_8021Q),
784                                                        tpa_info->vlan_tag);
785                         bnx2x_gro_receive(bp, fp, skb);
786                 } else {
787                         DP(NETIF_MSG_RX_STATUS,
788                            "Failed to allocate new pages - dropping packet!\n");
789                         dev_kfree_skb_any(skb);
790                 }
791
792                 /* put new data in bin */
793                 rx_buf->data = new_data;
794
795                 return;
796         }
797         if (new_data)
798                 bnx2x_frag_free(fp, new_data);
799 drop:
800         /* drop the packet and keep the buffer in the bin */
801         DP(NETIF_MSG_RX_STATUS,
802            "Failed to allocate or map a new skb - dropping packet!\n");
803         bnx2x_fp_stats(bp, fp)->eth_q_stats.rx_skb_alloc_failed++;
804 }
805
806 static int bnx2x_alloc_rx_data(struct bnx2x *bp, struct bnx2x_fastpath *fp,
807                                uint16_t index, gfp_t gfp_mask)
808 {
809         uint8_t *data;
810         struct sw_rx_bd *rx_buf = &fp->rx_buf_ring[index];
811         struct eth_rx_bd *rx_bd = &fp->rx_desc_ring[index];
812         dma_addr_t mapping;
813
814         data = bnx2x_frag_alloc(fp, gfp_mask);
815         if (unlikely(data == NULL))
816                 return -ENOMEM;
817
818         mapping = dma_map_single(&bp->pdev->dev, data + NET_SKB_PAD,
819                                  fp->rx_buf_size,
820                                  DMA_FROM_DEVICE);
821         if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
822                 bnx2x_frag_free(fp, data);
823                 BNX2X_ERR("Can't map rx data\n");
824                 return -ENOMEM;
825         }
826
827         rx_buf->data = data;
828         dma_unmap_addr_set(rx_buf, mapping, mapping);
829
830         rx_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
831         rx_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
832
833         return 0;
834 }
835
836 static
837 void bnx2x_csum_validate(struct sk_buff *skb, union eth_rx_cqe *cqe,
838                                  struct bnx2x_fastpath *fp,
839                                  struct bnx2x_eth_q_stats *qstats)
840 {
841         /* Do nothing if no L4 csum validation was done.
842          * We do not check whether IP csum was validated. For IPv4 we assume
843          * that if the card got as far as validating the L4 csum, it also
844          * validated the IP csum. IPv6 has no IP csum.
845          */
846         if (cqe->fast_path_cqe.status_flags &
847             ETH_FAST_PATH_RX_CQE_L4_XSUM_NO_VALIDATION_FLG)
848                 return;
849
850         /* If L4 validation was done, check if an error was found. */
851
852         if (cqe->fast_path_cqe.type_error_flags &
853             (ETH_FAST_PATH_RX_CQE_IP_BAD_XSUM_FLG |
854              ETH_FAST_PATH_RX_CQE_L4_BAD_XSUM_FLG))
855                 qstats->hw_csum_err++;
856         else
857                 skb->ip_summed = CHECKSUM_UNNECESSARY;
858 }
859
860 static int bnx2x_rx_int(struct bnx2x_fastpath *fp, int budget)
861 {
862         struct bnx2x *bp = fp->bp;
863         uint16_t bd_cons, bd_prod, bd_prod_fw, comp_ring_cons;
864         uint16_t sw_comp_cons, sw_comp_prod;
865         int rx_pkt = 0;
866         union eth_rx_cqe *cqe;
867         struct eth_fast_path_rx_cqe *cqe_fp;
868
869 #ifdef BNX2X_STOP_ON_ERROR
870         if (unlikely(bp->panic))
871                 return 0;
872 #endif
873         if (budget <= 0)
874                 return rx_pkt;
875
876         bd_cons = fp->rx_bd_cons;
877         bd_prod = fp->rx_bd_prod;
878         bd_prod_fw = bd_prod;
879         sw_comp_cons = fp->rx_comp_cons;
880         sw_comp_prod = fp->rx_comp_prod;
881
882         comp_ring_cons = RCQ_BD(sw_comp_cons);
883         cqe = &fp->rx_comp_ring[comp_ring_cons];
884         cqe_fp = &cqe->fast_path_cqe;
885
886         DP(NETIF_MSG_RX_STATUS,
887            "queue[%d]: sw_comp_cons %u\n", fp->index, sw_comp_cons);
888
889         while (BNX2X_IS_CQE_COMPLETED(cqe_fp)) {
890                 struct sw_rx_bd *rx_buf = NULL;
891                 struct sk_buff *skb;
892                 uint8_t cqe_fp_flags;
893                 enum eth_rx_cqe_type cqe_fp_type;
894                 uint16_t len, pad, queue;
895                 uint8_t *data;
896                 uint32_t rxhash;
897                 enum pkt_hash_types rxhash_type;
898
899 #ifdef BNX2X_STOP_ON_ERROR
900                 if (unlikely(bp->panic))
901                         return 0;
902 #endif
903
904                 bd_prod = RX_BD(bd_prod);
905                 bd_cons = RX_BD(bd_cons);
906
907                 /* A rmb() is required to ensure that the CQE is not read
908                  * before it is written by the adapter DMA.  PCI ordering
909                  * rules will make sure the other fields are written before
910                  * the marker at the end of struct eth_fast_path_rx_cqe
911                  * but without rmb() a weakly ordered processor can process
912                  * stale data.  Without the barrier TPA state-machine might
913                  * enter inconsistent state and kernel stack might be
914                  * provided with incorrect packet description - these lead
915                  * to various kernel crashed.
916                  */
917                 rmb();
918
919                 cqe_fp_flags = cqe_fp->type_error_flags;
920                 cqe_fp_type = cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE;
921
922                 DP(NETIF_MSG_RX_STATUS,
923                    "CQE type %x  err %x  status %x  queue %x  vlan %x  len %u\n",
924                    CQE_TYPE(cqe_fp_flags),
925                    cqe_fp_flags, cqe_fp->status_flags,
926                    le32_to_cpu(cqe_fp->rss_hash_result),
927                    le16_to_cpu(cqe_fp->vlan_tag),
928                    le16_to_cpu(cqe_fp->pkt_len_or_gro_seg_len));
929
930                 /* is this a slowpath msg? */
931                 if (unlikely(CQE_TYPE_SLOW(cqe_fp_type))) {
932                         bnx2x_sp_event(fp, cqe);
933                         goto next_cqe;
934                 }
935
936                 rx_buf = &fp->rx_buf_ring[bd_cons];
937                 data = rx_buf->data;
938
939                 if (!CQE_TYPE_FAST(cqe_fp_type)) {
940                         struct bnx2x_agg_info *tpa_info;
941                         uint16_t frag_size, pages;
942 #ifdef BNX2X_STOP_ON_ERROR
943                         /* sanity check */
944                         if (fp->disable_tpa &&
945                             (CQE_TYPE_START(cqe_fp_type) ||
946                              CQE_TYPE_STOP(cqe_fp_type)))
947                                 BNX2X_ERR("START/STOP packet while disable_tpa type %x\n",
948                                           CQE_TYPE(cqe_fp_type));
949 #endif
950
951                         if (CQE_TYPE_START(cqe_fp_type)) {
952                                 uint16_t queue = cqe_fp->queue_index;
953                                 DP(NETIF_MSG_RX_STATUS,
954                                    "calling tpa_start on queue %d\n",
955                                    queue);
956
957                                 bnx2x_tpa_start(fp, queue,
958                                                 bd_cons, bd_prod,
959                                                 cqe_fp);
960
961                                 goto next_rx;
962                         }
963                         queue = cqe->end_agg_cqe.queue_index;
964                         tpa_info = &fp->tpa_info[queue];
965                         DP(NETIF_MSG_RX_STATUS,
966                            "calling tpa_stop on queue %d\n",
967                            queue);
968
969                         frag_size = le16_to_cpu(cqe->end_agg_cqe.pkt_len) -
970                                     tpa_info->len_on_bd;
971
972                         if (fp->mode == TPA_MODE_GRO)
973                                 pages = (frag_size + tpa_info->full_page - 1) /
974                                          tpa_info->full_page;
975                         else
976                                 pages = SGE_PAGE_ALIGN(frag_size) >>
977                                         SGE_PAGE_SHIFT;
978
979                         bnx2x_tpa_stop(bp, fp, tpa_info, pages,
980                                        &cqe->end_agg_cqe, comp_ring_cons);
981 #ifdef BNX2X_STOP_ON_ERROR
982                         if (bp->panic)
983                                 return 0;
984 #endif
985
986                         bnx2x_update_sge_prod(fp, pages, &cqe->end_agg_cqe);
987                         goto next_cqe;
988                 }
989                 /* non TPA */
990                 len = le16_to_cpu(cqe_fp->pkt_len_or_gro_seg_len);
991                 pad = cqe_fp->placement_offset;
992                 dma_sync_single_for_cpu(&bp->pdev->dev,
993                                         dma_unmap_addr(rx_buf, mapping),
994                                         pad + RX_COPY_THRESH,
995                                         DMA_FROM_DEVICE);
996                 pad += NET_SKB_PAD;
997                 prefetch(data + pad); /* speedup eth_type_trans() */
998                 /* is this an error packet? */
999                 if (unlikely(cqe_fp_flags & ETH_RX_ERROR_FALGS)) {
1000                         DP(NETIF_MSG_RX_ERR | NETIF_MSG_RX_STATUS,
1001                            "ERROR  flags %x  rx packet %u\n",
1002                            cqe_fp_flags, sw_comp_cons);
1003                         bnx2x_fp_qstats(bp, fp)->rx_err_discard_pkt++;
1004                         goto reuse_rx;
1005                 }
1006
1007                 /* Since we don't have a jumbo ring
1008                  * copy small packets if mtu > 1500
1009                  */
1010                 if ((bp->dev->maxmtu > ETH_MAX_PACKET_SIZE) &&
1011                     (len <= RX_COPY_THRESH)) {
1012                         skb = napi_alloc_skb(&fp->napi, len);
1013                         if (skb == NULL) {
1014                                 DP(NETIF_MSG_RX_ERR | NETIF_MSG_RX_STATUS,
1015                                    "ERROR  packet dropped because of alloc failure\n");
1016                                 bnx2x_fp_qstats(bp, fp)->rx_skb_alloc_failed++;
1017                                 goto reuse_rx;
1018                         }
1019                         memcpy(skb->data, data + pad, len);
1020                         bnx2x_reuse_rx_data(fp, bd_cons, bd_prod);
1021                 } else {
1022                         if (likely(bnx2x_alloc_rx_data(bp, fp, bd_prod,
1023                                                        0) == 0)) {
1024                                 dma_unmap_single(&bp->pdev->dev,
1025                                                  dma_unmap_addr(rx_buf, mapping),
1026                                                  fp->rx_buf_size,
1027                                                  DMA_FROM_DEVICE);
1028                                 skb = build_skb(data, fp->rx_frag_size);
1029                                 if (unlikely(!skb)) {
1030                                         bnx2x_frag_free(fp, data);
1031                                         bnx2x_fp_qstats(bp, fp)->
1032                                                         rx_skb_alloc_failed++;
1033                                         goto next_rx;
1034                                 }
1035                                 skb_reserve(skb, pad);
1036                         } else {
1037                                 DP(NETIF_MSG_RX_ERR | NETIF_MSG_RX_STATUS,
1038                                    "ERROR  packet dropped because of alloc failure\n");
1039                                 bnx2x_fp_qstats(bp, fp)->rx_skb_alloc_failed++;
1040 reuse_rx:
1041                                 bnx2x_reuse_rx_data(fp, bd_cons, bd_prod);
1042                                 goto next_rx;
1043                         }
1044                 }
1045
1046                 skb_put(skb, len);
1047                 skb->protocol = eth_type_trans(skb, bp->dev);
1048
1049                 /* Set Toeplitz hash for a none-LRO skb */
1050                 rxhash = bnx2x_get_rxhash(bp, cqe_fp, &rxhash_type);
1051                 skb_set_hash(skb, rxhash, rxhash_type);
1052
1053                 skb_checksum_none_assert(skb);
1054
1055                 if (bp->dev->feat & NETIF_F_RXCSUM)
1056                         bnx2x_csum_validate(skb, cqe, fp,
1057                                             bnx2x_fp_qstats(bp, fp));
1058
1059                 skb_record_rx_queue(skb, fp->rx_queue);
1060
1061                 /* Check if this packet was timestamped */
1062                 if (unlikely(cqe->fast_path_cqe.type_error_flags &
1063                              (1 << ETH_FAST_PATH_RX_CQE_PTP_PKT_SHIFT)))
1064                         bnx2x_set_rx_ts(bp, skb);
1065
1066                 if (le16_to_cpu(cqe_fp->pars_flags.flags) &
1067                     PARSING_FLAGS_VLAN)
1068                         __vlan_hwaccel_put_tag(skb, cpu_to_be16(ETH_P_8021Q),
1069                                                le16_to_cpu(cqe_fp->vlan_tag));
1070
1071                 skb_mark_napi_id(skb, &fp->napi);
1072
1073                 if (bnx2x_fp_ll_polling(fp))
1074                         netif_receive_skb(skb);
1075                 else
1076                         napi_gro_receive(&fp->napi, skb);
1077 next_rx:
1078                 rx_buf->data = NULL;
1079
1080                 bd_cons = NEXT_RX_IDX(bd_cons);
1081                 bd_prod = NEXT_RX_IDX(bd_prod);
1082                 bd_prod_fw = NEXT_RX_IDX(bd_prod_fw);
1083                 rx_pkt++;
1084 next_cqe:
1085                 sw_comp_prod = NEXT_RCQ_IDX(sw_comp_prod);
1086                 sw_comp_cons = NEXT_RCQ_IDX(sw_comp_cons);
1087
1088                 /* mark CQE as free */
1089                 BNX2X_SEED_CQE(cqe_fp);
1090
1091                 if (rx_pkt == budget)
1092                         break;
1093
1094                 comp_ring_cons = RCQ_BD(sw_comp_cons);
1095                 cqe = &fp->rx_comp_ring[comp_ring_cons];
1096                 cqe_fp = &cqe->fast_path_cqe;
1097         } /* while */
1098
1099         fp->rx_bd_cons = bd_cons;
1100         fp->rx_bd_prod = bd_prod_fw;
1101         fp->rx_comp_cons = sw_comp_cons;
1102         fp->rx_comp_prod = sw_comp_prod;
1103
1104         /* Update producers */
1105         bnx2x_update_rx_prod(bp, fp, bd_prod_fw, sw_comp_prod,
1106                              fp->rx_sge_prod);
1107
1108         fp->rx_pkt += rx_pkt;
1109         fp->rx_calls++;
1110
1111         return rx_pkt;
1112 }
1113
1114 static irqreturn_t bnx2x_msix_fp_int(int irq, void *fp_cookie)
1115 {
1116         struct bnx2x_fastpath *fp = fp_cookie;
1117         struct bnx2x *bp = fp->bp;
1118         uint8_t cos;
1119
1120         DP(NETIF_MSG_INTR,
1121            "got an MSI-X interrupt on IDX:SB [fp %d fw_sd %d igusb %d]\n",
1122            fp->index, fp->fw_sb_id, fp->igu_sb_id);
1123
1124         bnx2x_ack_sb(bp, fp->igu_sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
1125
1126 #ifdef BNX2X_STOP_ON_ERROR
1127         if (unlikely(bp->panic))
1128                 return IRQ_HANDLED;
1129 #endif
1130
1131         /* Handle Rx and Tx according to MSI-X vector */
1132         for_each_cos_in_tx_queue(fp, cos)
1133                 prefetch(fp->txdata_ptr[cos]->tx_cons_sb);
1134
1135         prefetch(&fp->sb_running_index[SM_RX_ID]);
1136         napi_schedule_irqoff(&bnx2x_fp(bp, fp->index, napi));
1137
1138         return IRQ_HANDLED;
1139 }
1140
1141 /* HW Lock for shared dual port PHYs */
1142 void bnx2x_acquire_phy_lock(struct bnx2x *bp)
1143 {
1144         qlock(&bp->port.phy_mutex);
1145
1146         bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_MDIO);
1147 }
1148
1149 void bnx2x_release_phy_lock(struct bnx2x *bp)
1150 {
1151         bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_MDIO);
1152
1153         qunlock(&bp->port.phy_mutex);
1154 }
1155
1156 /* calculates MF speed according to current linespeed and MF configuration */
1157 uint16_t bnx2x_get_mf_speed(struct bnx2x *bp)
1158 {
1159         uint16_t line_speed = bp->link_vars.line_speed;
1160         if (IS_MF(bp)) {
1161                 uint16_t maxCfg = bnx2x_extract_max_cfg(bp,
1162                                                    bp->mf_config[BP_VN(bp)]);
1163
1164                 /* Calculate the current MAX line speed limit for the MF
1165                  * devices
1166                  */
1167                 if (IS_MF_SI(bp))
1168                         line_speed = (line_speed * maxCfg) / 100;
1169                 else { /* SD mode */
1170                         uint16_t vn_max_rate = maxCfg * 100;
1171
1172                         if (vn_max_rate < line_speed)
1173                                 line_speed = vn_max_rate;
1174                 }
1175         }
1176
1177         return line_speed;
1178 }
1179
1180 /**
1181  * bnx2x_fill_report_data - fill link report data to report
1182  *
1183  * @bp:         driver handle
1184  * @data:       link state to update
1185  *
1186  * It uses a none-atomic bit operations because is called under the mutex.
1187  */
1188 static void bnx2x_fill_report_data(struct bnx2x *bp,
1189                                    struct bnx2x_link_report_data *data)
1190 {
1191         memset(data, 0, sizeof(*data));
1192
1193         if (IS_PF(bp)) {
1194                 /* Fill the report data: effective line speed */
1195                 data->line_speed = bnx2x_get_mf_speed(bp);
1196
1197                 /* Link is down */
1198                 if (!bp->link_vars.link_up || (bp->flags & MF_FUNC_DIS))
1199                         __set_bit(BNX2X_LINK_REPORT_LINK_DOWN,
1200                                   &data->link_report_flags);
1201
1202                 if (!BNX2X_NUM_ETH_QUEUES(bp))
1203                         __set_bit(BNX2X_LINK_REPORT_LINK_DOWN,
1204                                   &data->link_report_flags);
1205
1206                 /* Full DUPLEX */
1207                 if (bp->link_vars.duplex == DUPLEX_FULL)
1208                         __set_bit(BNX2X_LINK_REPORT_FD,
1209                                   &data->link_report_flags);
1210
1211                 /* Rx Flow Control is ON */
1212                 if (bp->link_vars.flow_ctrl & BNX2X_FLOW_CTRL_RX)
1213                         __set_bit(BNX2X_LINK_REPORT_RX_FC_ON,
1214                                   &data->link_report_flags);
1215
1216                 /* Tx Flow Control is ON */
1217                 if (bp->link_vars.flow_ctrl & BNX2X_FLOW_CTRL_TX)
1218                         __set_bit(BNX2X_LINK_REPORT_TX_FC_ON,
1219                                   &data->link_report_flags);
1220         } else { /* VF */
1221                 *data = bp->vf_link_vars;
1222         }
1223 }
1224
1225 /**
1226  * bnx2x_link_report - report link status to OS.
1227  *
1228  * @bp:         driver handle
1229  *
1230  * Calls the __bnx2x_link_report() under the same locking scheme
1231  * as a link/PHY state managing code to ensure a consistent link
1232  * reporting.
1233  */
1234
1235 void bnx2x_link_report(struct bnx2x *bp)
1236 {
1237         bnx2x_acquire_phy_lock(bp);
1238         __bnx2x_link_report(bp);
1239         bnx2x_release_phy_lock(bp);
1240 }
1241
1242 /**
1243  * __bnx2x_link_report - report link status to OS.
1244  *
1245  * @bp:         driver handle
1246  *
1247  * None atomic implementation.
1248  * Should be called under the phy_lock.
1249  */
1250 void __bnx2x_link_report(struct bnx2x *bp)
1251 {
1252         struct bnx2x_link_report_data cur_data;
1253
1254         /* reread mf_cfg */
1255         if (IS_PF(bp) && !CHIP_IS_E1(bp))
1256                 bnx2x_read_mf_cfg(bp);
1257
1258         /* Read the current link report info */
1259         bnx2x_fill_report_data(bp, &cur_data);
1260
1261         /* Don't report link down or exactly the same link status twice */
1262         if (!memcmp(&cur_data, &bp->last_reported_link, sizeof(cur_data)) ||
1263             (test_bit(BNX2X_LINK_REPORT_LINK_DOWN,
1264                       &bp->last_reported_link.link_report_flags) &&
1265              test_bit(BNX2X_LINK_REPORT_LINK_DOWN,
1266                       &cur_data.link_report_flags)))
1267                 return;
1268
1269         bp->link_cnt++;
1270
1271         /* We are going to report a new link parameters now -
1272          * remember the current data for the next time.
1273          */
1274         memcpy(&bp->last_reported_link, &cur_data, sizeof(cur_data));
1275
1276         /* propagate status to VFs */
1277         if (IS_PF(bp))
1278                 bnx2x_iov_link_update(bp);
1279
1280         if (test_bit(BNX2X_LINK_REPORT_LINK_DOWN,
1281                      &cur_data.link_report_flags)) {
1282                 netif_carrier_off(bp->dev);
1283                 netdev_err(bp->dev, "NIC Link is Down\n");
1284                 return;
1285         } else {
1286                 const char *duplex;
1287                 const char *flow;
1288
1289                 netif_carrier_on(bp->dev);
1290
1291                 if (test_and_clear_bit(BNX2X_LINK_REPORT_FD,
1292                                        &cur_data.link_report_flags))
1293                         duplex = "full";
1294                 else
1295                         duplex = "half";
1296
1297                 /* Handle the FC at the end so that only these flags would be
1298                  * possibly set. This way we may easily check if there is no FC
1299                  * enabled.
1300                  */
1301                 if (cur_data.link_report_flags) {
1302                         if (test_bit(BNX2X_LINK_REPORT_RX_FC_ON,
1303                                      &cur_data.link_report_flags)) {
1304                                 if (test_bit(BNX2X_LINK_REPORT_TX_FC_ON,
1305                                      &cur_data.link_report_flags))
1306                                         flow = "ON - receive & transmit";
1307                                 else
1308                                         flow = "ON - receive";
1309                         } else {
1310                                 flow = "ON - transmit";
1311                         }
1312                 } else {
1313                         flow = "none";
1314                 }
1315                 netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
1316                             cur_data.line_speed, duplex, flow);
1317         }
1318 }
1319
1320 static void bnx2x_set_next_page_sgl(struct bnx2x_fastpath *fp)
1321 {
1322         int i;
1323
1324         for (i = 1; i <= NUM_RX_SGE_PAGES; i++) {
1325                 struct eth_rx_sge *sge;
1326
1327                 sge = &fp->rx_sge_ring[RX_SGE_CNT * i - 2];
1328                 sge->addr_hi =
1329                         cpu_to_le32(U64_HI(fp->rx_sge_mapping +
1330                         BCM_PAGE_SIZE*(i % NUM_RX_SGE_PAGES)));
1331
1332                 sge->addr_lo =
1333                         cpu_to_le32(U64_LO(fp->rx_sge_mapping +
1334                         BCM_PAGE_SIZE*(i % NUM_RX_SGE_PAGES)));
1335         }
1336 }
1337
1338 static void bnx2x_free_tpa_pool(struct bnx2x *bp,
1339                                 struct bnx2x_fastpath *fp, int last)
1340 {
1341         int i;
1342
1343         for (i = 0; i < last; i++) {
1344                 struct bnx2x_agg_info *tpa_info = &fp->tpa_info[i];
1345                 struct sw_rx_bd *first_buf = &tpa_info->first_buf;
1346                 uint8_t *data = first_buf->data;
1347
1348                 if (data == NULL) {
1349                         DP(NETIF_MSG_IFDOWN, "tpa bin %d empty on free\n", i);
1350                         continue;
1351                 }
1352                 if (tpa_info->tpa_state == BNX2X_TPA_START)
1353                         dma_unmap_single(&bp->pdev->dev,
1354                                          dma_unmap_addr(first_buf, mapping),
1355                                          fp->rx_buf_size, DMA_FROM_DEVICE);
1356                 bnx2x_frag_free(fp, data);
1357                 first_buf->data = NULL;
1358         }
1359 }
1360
1361 void bnx2x_init_rx_rings_cnic(struct bnx2x *bp)
1362 {
1363         int j;
1364
1365         for_each_rx_queue_cnic(bp, j) {
1366                 struct bnx2x_fastpath *fp = &bp->fp[j];
1367
1368                 fp->rx_bd_cons = 0;
1369
1370                 /* Activate BD ring */
1371                 /* Warning!
1372                  * this will generate an interrupt (to the TSTORM)
1373                  * must only be done after chip is initialized
1374                  */
1375                 bnx2x_update_rx_prod(bp, fp, fp->rx_bd_prod, fp->rx_comp_prod,
1376                                      fp->rx_sge_prod);
1377         }
1378 }
1379
1380 void bnx2x_init_rx_rings(struct bnx2x *bp)
1381 {
1382         int func = BP_FUNC(bp);
1383         uint16_t ring_prod;
1384         int i, j;
1385
1386         /* Allocate TPA resources */
1387         for_each_eth_queue(bp, j) {
1388                 struct bnx2x_fastpath *fp = &bp->fp[j];
1389
1390                 DP(NETIF_MSG_IFUP,
1391                    "mtu %d  rx_buf_size %d\n", bp->dev->maxmtu, fp->rx_buf_size);
1392
1393                 if (!fp->disable_tpa) {
1394                         /* Fill the per-aggregation pool */
1395                         for (i = 0; i < MAX_AGG_QS(bp); i++) {
1396                                 struct bnx2x_agg_info *tpa_info =
1397                                         &fp->tpa_info[i];
1398                                 struct sw_rx_bd *first_buf =
1399                                         &tpa_info->first_buf;
1400
1401                                 first_buf->data =
1402                                         bnx2x_frag_alloc(fp, KMALLOC_WAIT);
1403                                 if (!first_buf->data) {
1404                                         BNX2X_ERR("Failed to allocate TPA skb pool for queue[%d] - disabling TPA on this queue!\n",
1405                                                   j);
1406                                         bnx2x_free_tpa_pool(bp, fp, i);
1407                                         fp->disable_tpa = 1;
1408                                         break;
1409                                 }
1410                                 dma_unmap_addr_set(first_buf, mapping, 0);
1411                                 tpa_info->tpa_state = BNX2X_TPA_STOP;
1412                         }
1413
1414                         /* "next page" elements initialization */
1415                         bnx2x_set_next_page_sgl(fp);
1416
1417                         /* set SGEs bit mask */
1418                         bnx2x_init_sge_ring_bit_mask(fp);
1419
1420                         /* Allocate SGEs and initialize the ring elements */
1421                         for (i = 0, ring_prod = 0;
1422                              i < MAX_RX_SGE_CNT*NUM_RX_SGE_PAGES; i++) {
1423
1424                                 if (bnx2x_alloc_rx_sge(bp, fp, ring_prod,
1425                                                        KMALLOC_WAIT) < 0) {
1426                                         BNX2X_ERR("was only able to allocate %d rx sges\n",
1427                                                   i);
1428                                         BNX2X_ERR("disabling TPA for queue[%d]\n",
1429                                                   j);
1430                                         /* Cleanup already allocated elements */
1431                                         bnx2x_free_rx_sge_range(bp, fp,
1432                                                                 ring_prod);
1433                                         bnx2x_free_tpa_pool(bp, fp,
1434                                                             MAX_AGG_QS(bp));
1435                                         fp->disable_tpa = 1;
1436                                         ring_prod = 0;
1437                                         break;
1438                                 }
1439                                 ring_prod = NEXT_SGE_IDX(ring_prod);
1440                         }
1441
1442                         fp->rx_sge_prod = ring_prod;
1443                 }
1444         }
1445
1446         for_each_eth_queue(bp, j) {
1447                 struct bnx2x_fastpath *fp = &bp->fp[j];
1448
1449                 fp->rx_bd_cons = 0;
1450
1451                 /* Activate BD ring */
1452                 /* Warning!
1453                  * this will generate an interrupt (to the TSTORM)
1454                  * must only be done after chip is initialized
1455                  */
1456                 bnx2x_update_rx_prod(bp, fp, fp->rx_bd_prod, fp->rx_comp_prod,
1457                                      fp->rx_sge_prod);
1458
1459                 if (j != 0)
1460                         continue;
1461
1462                 if (CHIP_IS_E1(bp)) {
1463                         REG_WR(bp, BAR_USTRORM_INTMEM +
1464                                USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(func),
1465                                U64_LO(fp->rx_comp_mapping));
1466                         REG_WR(bp, BAR_USTRORM_INTMEM +
1467                                USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(func) + 4,
1468                                U64_HI(fp->rx_comp_mapping));
1469                 }
1470         }
1471 }
1472
1473 static void bnx2x_free_tx_skbs_queue(struct bnx2x_fastpath *fp)
1474 {
1475         uint8_t cos;
1476         struct bnx2x *bp = fp->bp;
1477
1478         for_each_cos_in_tx_queue(fp, cos) {
1479                 struct bnx2x_fp_txdata *txdata = fp->txdata_ptr[cos];
1480                 unsigned pkts_compl = 0, bytes_compl = 0;
1481
1482                 uint16_t sw_prod = txdata->tx_pkt_prod;
1483                 uint16_t sw_cons = txdata->tx_pkt_cons;
1484
1485                 while (sw_cons != sw_prod) {
1486                         bnx2x_free_tx_pkt(bp, txdata, TX_BD(sw_cons),
1487                                           &pkts_compl, &bytes_compl);
1488                         sw_cons++;
1489                 }
1490
1491                 netdev_tx_reset_queue(
1492                         netdev_get_tx_queue(bp->dev,
1493                                             txdata->txq_index));
1494         }
1495 }
1496
1497 static void bnx2x_free_tx_skbs_cnic(struct bnx2x *bp)
1498 {
1499         int i;
1500
1501         for_each_tx_queue_cnic(bp, i) {
1502                 bnx2x_free_tx_skbs_queue(&bp->fp[i]);
1503         }
1504 }
1505
1506 static void bnx2x_free_tx_skbs(struct bnx2x *bp)
1507 {
1508         int i;
1509
1510         for_each_eth_queue(bp, i) {
1511                 bnx2x_free_tx_skbs_queue(&bp->fp[i]);
1512         }
1513 }
1514
1515 static void bnx2x_free_rx_bds(struct bnx2x_fastpath *fp)
1516 {
1517         struct bnx2x *bp = fp->bp;
1518         int i;
1519
1520         /* ring wasn't allocated */
1521         if (fp->rx_buf_ring == NULL)
1522                 return;
1523
1524         for (i = 0; i < NUM_RX_BD; i++) {
1525                 struct sw_rx_bd *rx_buf = &fp->rx_buf_ring[i];
1526                 uint8_t *data = rx_buf->data;
1527
1528                 if (data == NULL)
1529                         continue;
1530                 dma_unmap_single(&bp->pdev->dev,
1531                                  dma_unmap_addr(rx_buf, mapping),
1532                                  fp->rx_buf_size, DMA_FROM_DEVICE);
1533
1534                 rx_buf->data = NULL;
1535                 bnx2x_frag_free(fp, data);
1536         }
1537 }
1538
1539 static void bnx2x_free_rx_skbs_cnic(struct bnx2x *bp)
1540 {
1541         int j;
1542
1543         for_each_rx_queue_cnic(bp, j) {
1544                 bnx2x_free_rx_bds(&bp->fp[j]);
1545         }
1546 }
1547
1548 static void bnx2x_free_rx_skbs(struct bnx2x *bp)
1549 {
1550         int j;
1551
1552         for_each_eth_queue(bp, j) {
1553                 struct bnx2x_fastpath *fp = &bp->fp[j];
1554
1555                 bnx2x_free_rx_bds(fp);
1556
1557                 if (!fp->disable_tpa)
1558                         bnx2x_free_tpa_pool(bp, fp, MAX_AGG_QS(bp));
1559         }
1560 }
1561
1562 static void bnx2x_free_skbs_cnic(struct bnx2x *bp)
1563 {
1564         bnx2x_free_tx_skbs_cnic(bp);
1565         bnx2x_free_rx_skbs_cnic(bp);
1566 }
1567
1568 void bnx2x_free_skbs(struct bnx2x *bp)
1569 {
1570         bnx2x_free_tx_skbs(bp);
1571         bnx2x_free_rx_skbs(bp);
1572 }
1573
1574 void bnx2x_update_max_mf_config(struct bnx2x *bp, uint32_t value)
1575 {
1576         /* load old values */
1577         uint32_t mf_cfg = bp->mf_config[BP_VN(bp)];
1578
1579         if (value != bnx2x_extract_max_cfg(bp, mf_cfg)) {
1580                 /* leave all but MAX value */
1581                 mf_cfg &= ~FUNC_MF_CFG_MAX_BW_MASK;
1582
1583                 /* set new MAX value */
1584                 mf_cfg |= (value << FUNC_MF_CFG_MAX_BW_SHIFT)
1585                                 & FUNC_MF_CFG_MAX_BW_MASK;
1586
1587                 bnx2x_fw_command(bp, DRV_MSG_CODE_SET_MF_BW, mf_cfg);
1588         }
1589 }
1590
1591 /**
1592  * bnx2x_free_msix_irqs - free previously requested MSI-X IRQ vectors
1593  *
1594  * @bp:         driver handle
1595  * @nvecs:      number of vectors to be released
1596  */
1597 static void bnx2x_free_msix_irqs(struct bnx2x *bp, int nvecs)
1598 {
1599         int i, offset = 0;
1600
1601         if (nvecs == offset)
1602                 return;
1603
1604         /* VFs don't have a default SB */
1605         if (IS_PF(bp)) {
1606                 free_irq(bp->msix_table[offset].vector, bp->dev);
1607                 DP(NETIF_MSG_IFDOWN, "released sp irq (%d)\n",
1608                    bp->msix_table[offset].vector);
1609                 offset++;
1610         }
1611
1612         if (CNIC_SUPPORT(bp)) {
1613                 if (nvecs == offset)
1614                         return;
1615                 offset++;
1616         }
1617
1618         for_each_eth_queue(bp, i) {
1619                 if (nvecs == offset)
1620                         return;
1621                 DP(NETIF_MSG_IFDOWN, "about to release fp #%d->%d irq\n",
1622                    i, bp->msix_table[offset].vector);
1623
1624                 free_irq(bp->msix_table[offset++].vector, &bp->fp[i]);
1625         }
1626 }
1627
1628 void bnx2x_free_irq(struct bnx2x *bp)
1629 {
1630         if (bp->flags & USING_MSIX_FLAG &&
1631             !(bp->flags & USING_SINGLE_MSIX_FLAG)) {
1632                 int nvecs = BNX2X_NUM_ETH_QUEUES(bp) + CNIC_SUPPORT(bp);
1633
1634                 /* vfs don't have a default status block */
1635                 if (IS_PF(bp))
1636                         nvecs++;
1637
1638                 bnx2x_free_msix_irqs(bp, nvecs);
1639         } else {
1640                 free_irq(bp->dev->irq, bp->dev);
1641         }
1642 }
1643
1644 int bnx2x_enable_msix(struct bnx2x *bp)
1645 {
1646         int msix_vec = 0, i, rc;
1647
1648         /* VFs don't have a default status block */
1649         if (IS_PF(bp)) {
1650                 bp->msix_table[msix_vec].entry = msix_vec;
1651                 BNX2X_DEV_INFO("msix_table[0].entry = %d (slowpath)\n",
1652                                bp->msix_table[0].entry);
1653                 msix_vec++;
1654         }
1655
1656         /* Cnic requires an msix vector for itself */
1657         if (CNIC_SUPPORT(bp)) {
1658                 bp->msix_table[msix_vec].entry = msix_vec;
1659                 BNX2X_DEV_INFO("msix_table[%d].entry = %d (CNIC)\n",
1660                                msix_vec, bp->msix_table[msix_vec].entry);
1661                 msix_vec++;
1662         }
1663
1664         /* We need separate vectors for ETH queues only (not FCoE) */
1665         for_each_eth_queue(bp, i) {
1666                 bp->msix_table[msix_vec].entry = msix_vec;
1667                 BNX2X_DEV_INFO("msix_table[%d].entry = %d (fastpath #%u)\n",
1668                                msix_vec, msix_vec, i);
1669                 msix_vec++;
1670         }
1671
1672         DP(BNX2X_MSG_SP, "about to request enable msix with %d vectors\n",
1673            msix_vec);
1674
1675         rc = pci_enable_msix_range(bp->pdev, &bp->msix_table[0],
1676                                    BNX2X_MIN_MSIX_VEC_CNT(bp), msix_vec);
1677         /*
1678          * reconfigure number of tx/rx queues according to available
1679          * MSI-X vectors
1680          */
1681         if (rc == -ENOSPC) {
1682                 /* Get by with single vector */
1683                 rc = pci_enable_msix_range(bp->pdev, &bp->msix_table[0], 1, 1);
1684                 if (rc < 0) {
1685                         BNX2X_DEV_INFO("Single MSI-X is not attainable rc %d\n",
1686                                        rc);
1687                         goto no_msix;
1688                 }
1689
1690                 BNX2X_DEV_INFO("Using single MSI-X vector\n");
1691                 bp->flags |= USING_SINGLE_MSIX_FLAG;
1692
1693                 BNX2X_DEV_INFO("set number of queues to 1\n");
1694                 bp->num_ethernet_queues = 1;
1695                 bp->num_queues = bp->num_ethernet_queues + bp->num_cnic_queues;
1696         } else if (rc < 0) {
1697                 BNX2X_DEV_INFO("MSI-X is not attainable rc %d\n", rc);
1698                 goto no_msix;
1699         } else if (rc < msix_vec) {
1700                 /* how less vectors we will have? */
1701                 int diff = msix_vec - rc;
1702
1703                 BNX2X_DEV_INFO("Trying to use less MSI-X vectors: %d\n", rc);
1704
1705                 /*
1706                  * decrease number of queues by number of unallocated entries
1707                  */
1708                 bp->num_ethernet_queues -= diff;
1709                 bp->num_queues = bp->num_ethernet_queues + bp->num_cnic_queues;
1710
1711                 BNX2X_DEV_INFO("New queue configuration set: %d\n",
1712                                bp->num_queues);
1713         }
1714
1715         bp->flags |= USING_MSIX_FLAG;
1716
1717         return 0;
1718
1719 no_msix:
1720         /* fall to INTx if not enough memory */
1721         if (rc == -ENOMEM)
1722                 bp->flags |= DISABLE_MSI_FLAG;
1723
1724         return rc;
1725 }
1726
1727 static int bnx2x_req_msix_irqs(struct bnx2x *bp)
1728 {
1729         int i, rc, offset = 0;
1730
1731         /* no default status block for vf */
1732         if (IS_PF(bp)) {
1733                 rc = register_irq(bp->msix_table[offset++].vector,
1734                                   bnx2x_msix_sp_int, bp->dev,
1735                                   pci_to_tbdf(bp->pdev));
1736                 if (rc) {
1737                         BNX2X_ERR("request sp irq failed\n");
1738                         return -EBUSY;
1739                 }
1740         }
1741
1742         if (CNIC_SUPPORT(bp))
1743                 offset++;
1744
1745         for_each_eth_queue(bp, i) {
1746                 struct bnx2x_fastpath *fp = &bp->fp[i];
1747                 snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
1748                          bp->dev->name, i);
1749
1750                 rc = register_irq(bp->msix_table[offset].vector,
1751                                   bnx2x_msix_fp_int, fp, pci_to_tbdf(bp->pdev));
1752                 if (rc) {
1753                         BNX2X_ERR("request fp #%d irq (%d) failed  rc %d\n", i,
1754                               bp->msix_table[offset].vector, rc);
1755                         bnx2x_free_msix_irqs(bp, offset);
1756                         return -EBUSY;
1757                 }
1758
1759                 offset++;
1760         }
1761
1762         i = BNX2X_NUM_ETH_QUEUES(bp);
1763         if (IS_PF(bp)) {
1764                 offset = 1 + CNIC_SUPPORT(bp);
1765                 netdev_info(bp->dev,
1766                             "using MSI-X  IRQs: sp %d  fp[%d] %d ... fp[%d] %d\n",
1767                             bp->msix_table[0].vector,
1768                             0, bp->msix_table[offset].vector,
1769                             i - 1, bp->msix_table[offset + i - 1].vector);
1770         } else {
1771                 offset = CNIC_SUPPORT(bp);
1772                 netdev_info(bp->dev,
1773                             "using MSI-X  IRQs: fp[%d] %d ... fp[%d] %d\n",
1774                             0, bp->msix_table[offset].vector,
1775                             i - 1, bp->msix_table[offset + i - 1].vector);
1776         }
1777         return 0;
1778 }
1779
1780 int bnx2x_enable_msi(struct bnx2x *bp)
1781 {
1782         int rc;
1783
1784         rc = pci_enable_msi(bp->pdev);
1785         if (rc) {
1786                 BNX2X_DEV_INFO("MSI is not attainable\n");
1787                 return -1;
1788         }
1789         bp->flags |= USING_MSI_FLAG;
1790
1791         return 0;
1792 }
1793
1794 static int bnx2x_req_irq(struct bnx2x *bp)
1795 {
1796         unsigned long flags;
1797         unsigned int irq;
1798
1799         if (bp->flags & (USING_MSI_FLAG | USING_MSIX_FLAG))
1800                 flags = 0;
1801         else
1802                 flags = IRQF_SHARED;
1803
1804         if (bp->flags & USING_MSIX_FLAG)
1805                 irq = bp->msix_table[0].vector;
1806         else
1807                 irq = bp->pdev->irq;
1808
1809         return register_irq(irq, bnx2x_interrupt, bp->dev,
1810                             pci_to_tbdf(bp->pdev));
1811 }
1812
1813 static int bnx2x_setup_irqs(struct bnx2x *bp)
1814 {
1815         int rc = 0;
1816         if (bp->flags & USING_MSIX_FLAG &&
1817             !(bp->flags & USING_SINGLE_MSIX_FLAG)) {
1818                 rc = bnx2x_req_msix_irqs(bp);
1819                 if (rc)
1820                         return rc;
1821         } else {
1822                 rc = bnx2x_req_irq(bp);
1823                 if (rc) {
1824                         BNX2X_ERR("IRQ request failed  rc %d, aborting\n", rc);
1825                         return rc;
1826                 }
1827                 if (bp->flags & USING_MSI_FLAG) {
1828                         bp->dev->irq = bp->pdev->irq;
1829                         netdev_info(bp->dev, "using MSI IRQ %d\n",
1830                                     bp->dev->irq);
1831                 }
1832                 if (bp->flags & USING_MSIX_FLAG) {
1833                         bp->dev->irq = bp->msix_table[0].vector;
1834                         netdev_info(bp->dev, "using MSIX IRQ %d\n",
1835                                     bp->dev->irq);
1836                 }
1837         }
1838
1839         return 0;
1840 }
1841
1842 static void bnx2x_napi_enable_cnic(struct bnx2x *bp)
1843 {
1844         int i;
1845
1846         for_each_rx_queue_cnic(bp, i) {
1847                 bnx2x_fp_init_lock(&bp->fp[i]);
1848                 napi_enable(&bnx2x_fp(bp, i, napi));
1849         }
1850 }
1851
1852 static void bnx2x_napi_enable(struct bnx2x *bp)
1853 {
1854         int i;
1855
1856         for_each_eth_queue(bp, i) {
1857                 bnx2x_fp_init_lock(&bp->fp[i]);
1858                 napi_enable(&bnx2x_fp(bp, i, napi));
1859         }
1860 }
1861
1862 static void bnx2x_napi_disable_cnic(struct bnx2x *bp)
1863 {
1864         int i;
1865
1866         for_each_rx_queue_cnic(bp, i) {
1867                 napi_disable(&bnx2x_fp(bp, i, napi));
1868                 while (!bnx2x_fp_ll_disable(&bp->fp[i]))
1869                         kthread_usleep(1000);
1870         }
1871 }
1872
1873 static void bnx2x_napi_disable(struct bnx2x *bp)
1874 {
1875         int i;
1876
1877         for_each_eth_queue(bp, i) {
1878                 napi_disable(&bnx2x_fp(bp, i, napi));
1879                 while (!bnx2x_fp_ll_disable(&bp->fp[i]))
1880                         kthread_usleep(1000);
1881         }
1882 }
1883
1884 void bnx2x_netif_start(struct bnx2x *bp)
1885 {
1886         if (netif_running(bp->dev)) {
1887                 bnx2x_napi_enable(bp);
1888                 if (CNIC_LOADED(bp))
1889                         bnx2x_napi_enable_cnic(bp);
1890                 bnx2x_int_enable(bp);
1891                 if (bp->state == BNX2X_STATE_OPEN)
1892                         netif_tx_wake_all_queues(bp->dev);
1893         }
1894 }
1895
1896 void bnx2x_netif_stop(struct bnx2x *bp, int disable_hw)
1897 {
1898         bnx2x_int_disable_sync(bp, disable_hw);
1899         bnx2x_napi_disable(bp);
1900         if (CNIC_LOADED(bp))
1901                 bnx2x_napi_disable_cnic(bp);
1902 }
1903
1904 uint16_t bnx2x_select_queue(struct ether *dev, struct sk_buff *skb,
1905                        void *accel_priv, select_queue_fallback_t fallback)
1906 {
1907         struct bnx2x *bp = netdev_priv(dev);
1908
1909         if (CNIC_LOADED(bp) && !NO_FCOE(bp)) {
1910                 struct ethhdr *hdr = (struct ethhdr *)skb->data;
1911                 uint16_t ether_type = be16_to_cpu(hdr->h_proto);
1912
1913                 /* Skip VLAN tag if present */
1914                 if (ether_type == ETH_P_8021Q) {
1915                         struct vlan_ethhdr *vhdr =
1916                                 (struct vlan_ethhdr *)skb->data;
1917
1918                         ether_type = be16_to_cpu(vhdr->h_vlan_encapsulated_proto);
1919                 }
1920
1921                 /* If ethertype is FCoE or FIP - use FCoE ring */
1922                 if ((ether_type == ETH_P_FCOE) || (ether_type == ETH_P_FIP))
1923                         return bnx2x_fcoe_tx(bp, txq_index);
1924         }
1925
1926         /* select a non-FCoE queue */
1927         return fallback(dev, skb) % BNX2X_NUM_ETH_QUEUES(bp);
1928 }
1929
1930 void bnx2x_set_num_queues(struct bnx2x *bp)
1931 {
1932         /* RSS queues */
1933         bp->num_ethernet_queues = bnx2x_calc_num_queues(bp);
1934
1935         /* override in STORAGE SD modes */
1936         if (IS_MF_STORAGE_ONLY(bp))
1937                 bp->num_ethernet_queues = 1;
1938
1939         /* Add special queues */
1940         bp->num_cnic_queues = CNIC_SUPPORT(bp); /* For FCOE */
1941         bp->num_queues = bp->num_ethernet_queues + bp->num_cnic_queues;
1942
1943         BNX2X_DEV_INFO("set number of queues to %d\n", bp->num_queues);
1944 }
1945
1946 /**
1947  * bnx2x_set_real_num_queues - configure netdev->real_num_[tx,rx]_queues
1948  *
1949  * @bp:         Driver handle
1950  *
1951  * We currently support for at most 16 Tx queues for each CoS thus we will
1952  * allocate a multiple of 16 for ETH L2 rings according to the value of the
1953  * bp->max_cos.
1954  *
1955  * If there is an FCoE L2 queue the appropriate Tx queue will have the next
1956  * index after all ETH L2 indices.
1957  *
1958  * If the actual number of Tx queues (for each CoS) is less than 16 then there
1959  * will be the holes at the end of each group of 16 ETh L2 indices (0..15,
1960  * 16..31,...) with indices that are not coupled with any real Tx queue.
1961  *
1962  * The proper configuration of skb->queue_mapping is handled by
1963  * bnx2x_select_queue() and __skb_tx_hash().
1964  *
1965  * bnx2x_setup_tc() takes care of the proper TC mappings so that __skb_tx_hash()
1966  * will return a proper Tx index if TC is enabled (netdev->num_tc > 0).
1967  */
1968 static int bnx2x_set_real_num_queues(struct bnx2x *bp, int include_cnic)
1969 {
1970         int rc, tx, rx;
1971
1972         tx = BNX2X_NUM_ETH_QUEUES(bp) * bp->max_cos;
1973         rx = BNX2X_NUM_ETH_QUEUES(bp);
1974
1975 /* account for fcoe queue */
1976         if (include_cnic && !NO_FCOE(bp)) {
1977                 rx++;
1978                 tx++;
1979         }
1980
1981         rc = netif_set_real_num_tx_queues(bp->dev, tx);
1982         if (rc) {
1983                 BNX2X_ERR("Failed to set real number of Tx queues: %d\n", rc);
1984                 return rc;
1985         }
1986         rc = netif_set_real_num_rx_queues(bp->dev, rx);
1987         if (rc) {
1988                 BNX2X_ERR("Failed to set real number of Rx queues: %d\n", rc);
1989                 return rc;
1990         }
1991
1992         DP(NETIF_MSG_IFUP, "Setting real num queues to (tx, rx) (%d, %d)\n",
1993                           tx, rx);
1994
1995         return rc;
1996 }
1997
1998 static void bnx2x_set_rx_buf_size(struct bnx2x *bp)
1999 {
2000         int i;
2001
2002         for_each_queue(bp, i) {
2003                 struct bnx2x_fastpath *fp = &bp->fp[i];
2004                 uint32_t mtu;
2005
2006                 /* Always use a mini-jumbo MTU for the FCoE L2 ring */
2007                 if (IS_FCOE_IDX(i))
2008                         /*
2009                          * Although there are no IP frames expected to arrive to
2010                          * this ring we still want to add an
2011                          * IP_HEADER_ALIGNMENT_PADDING to prevent a buffer
2012                          * overrun attack.
2013                          */
2014                         mtu = BNX2X_FCOE_MINI_JUMBO_MTU;
2015                 else
2016                         mtu = bp->dev->maxmtu;
2017                 fp->rx_buf_size = BNX2X_FW_RX_ALIGN_START +
2018                                   IP_HEADER_ALIGNMENT_PADDING +
2019                                   ETH_OVREHEAD +
2020                                   mtu +
2021                                   BNX2X_FW_RX_ALIGN_END;
2022                 /* Note : rx_buf_size doesn't take into account NET_SKB_PAD */
2023                 if (fp->rx_buf_size + NET_SKB_PAD <= PAGE_SIZE)
2024                         fp->rx_frag_size = fp->rx_buf_size + NET_SKB_PAD;
2025                 else
2026                         fp->rx_frag_size = 0;
2027         }
2028 }
2029
2030 static int bnx2x_init_rss(struct bnx2x *bp)
2031 {
2032         int i;
2033         uint8_t num_eth_queues = BNX2X_NUM_ETH_QUEUES(bp);
2034
2035         /* Prepare the initial contents for the indirection table if RSS is
2036          * enabled
2037          */
2038         for (i = 0; i < sizeof(bp->rss_conf_obj.ind_table); i++)
2039                 bp->rss_conf_obj.ind_table[i] =
2040                         bp->fp->cl_id +
2041                         ethtool_rxfh_indir_default(i, num_eth_queues);
2042
2043         /*
2044          * For 57710 and 57711 SEARCHER configuration (rss_keys) is
2045          * per-port, so if explicit configuration is needed , do it only
2046          * for a PMF.
2047          *
2048          * For 57712 and newer on the other hand it's a per-function
2049          * configuration.
2050          */
2051         return bnx2x_config_rss_eth(bp, bp->port.pmf || !CHIP_IS_E1x(bp));
2052 }
2053
2054 int bnx2x_rss(struct bnx2x *bp, struct bnx2x_rss_config_obj *rss_obj,
2055               bool config_hash, bool enable)
2056 {
2057         struct bnx2x_config_rss_params params = {NULL};
2058
2059         /* Although RSS is meaningless when there is a single HW queue we
2060          * still need it enabled in order to have HW Rx hash generated.
2061          *
2062          * if (!is_eth_multi(bp))
2063          *      bp->multi_mode = ETH_RSS_MODE_DISABLED;
2064          */
2065
2066         params.rss_obj = rss_obj;
2067
2068         __set_bit(RAMROD_COMP_WAIT, &params.ramrod_flags);
2069
2070         if (enable) {
2071                 __set_bit(BNX2X_RSS_MODE_REGULAR, &params.rss_flags);
2072
2073                 /* RSS configuration */
2074                 __set_bit(BNX2X_RSS_IPV4, &params.rss_flags);
2075                 __set_bit(BNX2X_RSS_IPV4_TCP, &params.rss_flags);
2076                 __set_bit(BNX2X_RSS_IPV6, &params.rss_flags);
2077                 __set_bit(BNX2X_RSS_IPV6_TCP, &params.rss_flags);
2078                 if (rss_obj->udp_rss_v4)
2079                         __set_bit(BNX2X_RSS_IPV4_UDP, &params.rss_flags);
2080                 if (rss_obj->udp_rss_v6)
2081                         __set_bit(BNX2X_RSS_IPV6_UDP, &params.rss_flags);
2082
2083                 if (!CHIP_IS_E1x(bp))
2084                         /* valid only for TUNN_MODE_GRE tunnel mode */
2085                         __set_bit(BNX2X_RSS_GRE_INNER_HDRS, &params.rss_flags);
2086         } else {
2087                 __set_bit(BNX2X_RSS_MODE_DISABLED, &params.rss_flags);
2088         }
2089
2090         /* Hash bits */
2091         params.rss_result_mask = MULTI_MASK;
2092
2093         memcpy(params.ind_table, rss_obj->ind_table, sizeof(params.ind_table));
2094
2095         if (config_hash) {
2096                 /* RSS keys */
2097                 netdev_rss_key_fill(params.rss_key, T_ETH_RSS_KEY * 4);
2098                 __set_bit(BNX2X_RSS_SET_SRCH, &params.rss_flags);
2099         }
2100
2101         if (IS_PF(bp))
2102                 return bnx2x_config_rss(bp, &params);
2103         else
2104                 return bnx2x_vfpf_config_rss(bp, &params);
2105 }
2106
2107 static int bnx2x_init_hw(struct bnx2x *bp, uint32_t load_code)
2108 {
2109         struct bnx2x_func_state_params func_params = {NULL};
2110
2111         /* Prepare parameters for function state transitions */
2112         __set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
2113
2114         func_params.f_obj = &bp->func_obj;
2115         func_params.cmd = BNX2X_F_CMD_HW_INIT;
2116
2117         func_params.params.hw_init.load_phase = load_code;
2118
2119         return bnx2x_func_state_change(bp, &func_params);
2120 }
2121
2122 /*
2123  * Cleans the object that have internal lists without sending
2124  * ramrods. Should be run when interrupts are disabled.
2125  */
2126 void bnx2x_squeeze_objects(struct bnx2x *bp)
2127 {
2128         int rc;
2129         unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
2130         struct bnx2x_mcast_ramrod_params rparam = {NULL};
2131         struct bnx2x_vlan_mac_obj *mac_obj = &bp->sp_objs->mac_obj;
2132
2133         /***************** Cleanup MACs' object first *************************/
2134
2135         /* Wait for completion of requested */
2136         __set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
2137         /* Perform a dry cleanup */
2138         __set_bit(RAMROD_DRV_CLR_ONLY, &ramrod_flags);
2139
2140         /* Clean ETH primary MAC */
2141         __set_bit(BNX2X_ETH_MAC, &vlan_mac_flags);
2142         rc = mac_obj->delete_all(bp, &bp->sp_objs->mac_obj, &vlan_mac_flags,
2143                                  &ramrod_flags);
2144         if (rc != 0)
2145                 BNX2X_ERR("Failed to clean ETH MACs: %d\n", rc);
2146
2147         /* Cleanup UC list */
2148         vlan_mac_flags = 0;
2149         __set_bit(BNX2X_UC_LIST_MAC, &vlan_mac_flags);
2150         rc = mac_obj->delete_all(bp, mac_obj, &vlan_mac_flags,
2151                                  &ramrod_flags);
2152         if (rc != 0)
2153                 BNX2X_ERR("Failed to clean UC list MACs: %d\n", rc);
2154
2155         /***************** Now clean mcast object *****************************/
2156         rparam.mcast_obj = &bp->mcast_obj;
2157         __set_bit(RAMROD_DRV_CLR_ONLY, &rparam.ramrod_flags);
2158
2159         /* Add a DEL command... - Since we're doing a driver cleanup only,
2160          * we take a lock surrounding both the initial send and the CONTs,
2161          * as we don't want a true completion to disrupt us in the middle.
2162          */
2163         qlock(&bp->dev->qlock);
2164         rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_DEL);
2165         if (rc < 0)
2166                 BNX2X_ERR("Failed to add a new DEL command to a multi-cast object: %d\n",
2167                           rc);
2168
2169         /* ...and wait until all pending commands are cleared */
2170         rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_CONT);
2171         while (rc != 0) {
2172                 if (rc < 0) {
2173                         BNX2X_ERR("Failed to clean multi-cast object: %d\n",
2174                                   rc);
2175                         qunlock(&bp->dev->qlock);
2176                         return;
2177                 }
2178
2179                 rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_CONT);
2180         }
2181         qunlock(&bp->dev->qlock);
2182 }
2183
2184 #ifndef BNX2X_STOP_ON_ERROR
2185 #define LOAD_ERROR_EXIT(bp, label) \
2186         do { \
2187                 (bp)->state = BNX2X_STATE_ERROR; \
2188                 goto label; \
2189         } while (0)
2190
2191 #define LOAD_ERROR_EXIT_CNIC(bp, label) \
2192         do { \
2193                 bp->cnic_loaded = false; \
2194                 goto label; \
2195         } while (0)
2196 #else /*BNX2X_STOP_ON_ERROR*/
2197 #define LOAD_ERROR_EXIT(bp, label) \
2198         do { \
2199                 (bp)->state = BNX2X_STATE_ERROR; \
2200                 (bp)->panic = 1; \
2201                 return -EBUSY; \
2202         } while (0)
2203 #define LOAD_ERROR_EXIT_CNIC(bp, label) \
2204         do { \
2205                 bp->cnic_loaded = false; \
2206                 (bp)->panic = 1; \
2207                 return -EBUSY; \
2208         } while (0)
2209 #endif /*BNX2X_STOP_ON_ERROR*/
2210
2211 static void bnx2x_free_fw_stats_mem(struct bnx2x *bp)
2212 {
2213         BNX2X_PCI_FREE(bp->fw_stats, bp->fw_stats_mapping,
2214                        bp->fw_stats_data_sz + bp->fw_stats_req_sz);
2215         return;
2216 }
2217
2218 static int bnx2x_alloc_fw_stats_mem(struct bnx2x *bp)
2219 {
2220         int num_groups, vf_headroom = 0;
2221         int is_fcoe_stats = NO_FCOE(bp) ? 0 : 1;
2222
2223         /* number of queues for statistics is number of eth queues + FCoE */
2224         uint8_t num_queue_stats = BNX2X_NUM_ETH_QUEUES(bp) + is_fcoe_stats;
2225
2226         /* Total number of FW statistics requests =
2227          * 1 for port stats + 1 for PF stats + potential 2 for FCoE (fcoe proper
2228          * and fcoe l2 queue) stats + num of queues (which includes another 1
2229          * for fcoe l2 queue if applicable)
2230          */
2231         bp->fw_stats_num = 2 + is_fcoe_stats + num_queue_stats;
2232
2233         /* vf stats appear in the request list, but their data is allocated by
2234          * the VFs themselves. We don't include them in the bp->fw_stats_num as
2235          * it is used to determine where to place the vf stats queries in the
2236          * request struct
2237          */
2238         if (IS_SRIOV(bp))
2239                 vf_headroom = bnx2x_vf_headroom(bp);
2240
2241         /* Request is built from stats_query_header and an array of
2242          * stats_query_cmd_group each of which contains
2243          * STATS_QUERY_CMD_COUNT rules. The real number or requests is
2244          * configured in the stats_query_header.
2245          */
2246         num_groups =
2247                 (((bp->fw_stats_num + vf_headroom) / STATS_QUERY_CMD_COUNT) +
2248                  (((bp->fw_stats_num + vf_headroom) % STATS_QUERY_CMD_COUNT) ?
2249                  1 : 0));
2250
2251         DP(BNX2X_MSG_SP, "stats fw_stats_num %d, vf headroom %d, num_groups %d\n",
2252            bp->fw_stats_num, vf_headroom, num_groups);
2253         bp->fw_stats_req_sz = sizeof(struct stats_query_header) +
2254                 num_groups * sizeof(struct stats_query_cmd_group);
2255
2256         /* Data for statistics requests + stats_counter
2257          * stats_counter holds per-STORM counters that are incremented
2258          * when STORM has finished with the current request.
2259          * memory for FCoE offloaded statistics are counted anyway,
2260          * even if they will not be sent.
2261          * VF stats are not accounted for here as the data of VF stats is stored
2262          * in memory allocated by the VF, not here.
2263          */
2264         bp->fw_stats_data_sz = sizeof(struct per_port_stats) +
2265                 sizeof(struct per_pf_stats) +
2266                 sizeof(struct fcoe_statistics_params) +
2267                 sizeof(struct per_queue_stats) * num_queue_stats +
2268                 sizeof(struct stats_counter);
2269
2270         bp->fw_stats = BNX2X_PCI_ALLOC(&bp->fw_stats_mapping,
2271                                        bp->fw_stats_data_sz + bp->fw_stats_req_sz);
2272         if (!bp->fw_stats)
2273                 goto alloc_mem_err;
2274
2275         /* Set shortcuts */
2276         bp->fw_stats_req = (struct bnx2x_fw_stats_req *)bp->fw_stats;
2277         bp->fw_stats_req_mapping = bp->fw_stats_mapping;
2278         bp->fw_stats_data = (struct bnx2x_fw_stats_data *)
2279                 ((uint8_t *)bp->fw_stats + bp->fw_stats_req_sz);
2280         bp->fw_stats_data_mapping = bp->fw_stats_mapping +
2281                 bp->fw_stats_req_sz;
2282
2283         DP(BNX2X_MSG_SP, "statistics request base address set to %x %x\n",
2284            U64_HI(bp->fw_stats_req_mapping),
2285            U64_LO(bp->fw_stats_req_mapping));
2286         DP(BNX2X_MSG_SP, "statistics data base address set to %x %x\n",
2287            U64_HI(bp->fw_stats_data_mapping),
2288            U64_LO(bp->fw_stats_data_mapping));
2289         return 0;
2290
2291 alloc_mem_err:
2292         bnx2x_free_fw_stats_mem(bp);
2293         BNX2X_ERR("Can't allocate FW stats memory\n");
2294         return -ENOMEM;
2295 }
2296
2297 /* send load request to mcp and analyze response */
2298 static int bnx2x_nic_load_request(struct bnx2x *bp, uint32_t *load_code)
2299 {
2300         uint32_t param;
2301
2302         /* init fw_seq */
2303         bp->fw_seq =
2304                 (SHMEM_RD(bp, func_mb[BP_FW_MB_IDX(bp)].drv_mb_header) &
2305                  DRV_MSG_SEQ_NUMBER_MASK);
2306         BNX2X_DEV_INFO("fw_seq 0x%08x\n", bp->fw_seq);
2307
2308         /* Get current FW pulse sequence */
2309         bp->fw_drv_pulse_wr_seq =
2310                 (SHMEM_RD(bp, func_mb[BP_FW_MB_IDX(bp)].drv_pulse_mb) &
2311                  DRV_PULSE_SEQ_MASK);
2312         BNX2X_DEV_INFO("drv_pulse 0x%x\n", bp->fw_drv_pulse_wr_seq);
2313
2314         param = DRV_MSG_CODE_LOAD_REQ_WITH_LFA;
2315
2316         if (IS_MF_SD(bp) && bnx2x_port_after_undi(bp))
2317                 param |= DRV_MSG_CODE_LOAD_REQ_FORCE_LFA;
2318
2319         /* load request */
2320         (*load_code) = bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_REQ, param);
2321
2322         /* if mcp fails to respond we must abort */
2323         if (!(*load_code)) {
2324                 BNX2X_ERR("MCP response failure, aborting\n");
2325                 return -EBUSY;
2326         }
2327
2328         /* If mcp refused (e.g. other port is in diagnostic mode) we
2329          * must abort
2330          */
2331         if ((*load_code) == FW_MSG_CODE_DRV_LOAD_REFUSED) {
2332                 BNX2X_ERR("MCP refused load request, aborting\n");
2333                 return -EBUSY;
2334         }
2335         return 0;
2336 }
2337
2338 /* check whether another PF has already loaded FW to chip. In
2339  * virtualized environments a pf from another VM may have already
2340  * initialized the device including loading FW
2341  */
2342 int bnx2x_compare_fw_ver(struct bnx2x *bp, uint32_t load_code,
2343                          bool print_err)
2344 {
2345         /* is another pf loaded on this engine? */
2346         if (load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP &&
2347             load_code != FW_MSG_CODE_DRV_LOAD_COMMON) {
2348                 /* build my FW version dword */
2349                 uint32_t my_fw = (BCM_5710_FW_MAJOR_VERSION) +
2350                         (BCM_5710_FW_MINOR_VERSION << 8) +
2351                         (BCM_5710_FW_REVISION_VERSION << 16) +
2352                         (BCM_5710_FW_ENGINEERING_VERSION << 24);
2353
2354                 /* read loaded FW from chip */
2355                 uint32_t loaded_fw = REG_RD(bp, XSEM_REG_PRAM);
2356
2357                 DP(BNX2X_MSG_SP, "loaded fw %x, my fw %x\n",
2358                    loaded_fw, my_fw);
2359
2360                 /* abort nic load if version mismatch */
2361                 if (my_fw != loaded_fw) {
2362                         if (print_err)
2363                                 BNX2X_ERR("bnx2x with FW %x was already loaded which mismatches my %x FW. Aborting\n",
2364                                           loaded_fw, my_fw);
2365                         else
2366                                 BNX2X_DEV_INFO("bnx2x with FW %x was already loaded which mismatches my %x FW, possibly due to MF UNDI\n",
2367                                                loaded_fw, my_fw);
2368                         return -EBUSY;
2369                 }
2370         }
2371         return 0;
2372 }
2373
2374 /* returns the "mcp load_code" according to global load_count array */
2375 static int bnx2x_nic_load_no_mcp(struct bnx2x *bp, int port)
2376 {
2377         int path = BP_PATH(bp);
2378
2379         DP(NETIF_MSG_IFUP, "NO MCP - load counts[%d]      %d, %d, %d\n",
2380            path, bnx2x_load_count[path][0], bnx2x_load_count[path][1],
2381            bnx2x_load_count[path][2]);
2382         bnx2x_load_count[path][0]++;
2383         bnx2x_load_count[path][1 + port]++;
2384         DP(NETIF_MSG_IFUP, "NO MCP - new load counts[%d]  %d, %d, %d\n",
2385            path, bnx2x_load_count[path][0], bnx2x_load_count[path][1],
2386            bnx2x_load_count[path][2]);
2387         if (bnx2x_load_count[path][0] == 1)
2388                 return FW_MSG_CODE_DRV_LOAD_COMMON;
2389         else if (bnx2x_load_count[path][1 + port] == 1)
2390                 return FW_MSG_CODE_DRV_LOAD_PORT;
2391         else
2392                 return FW_MSG_CODE_DRV_LOAD_FUNCTION;
2393 }
2394
2395 /* mark PMF if applicable */
2396 static void bnx2x_nic_load_pmf(struct bnx2x *bp, uint32_t load_code)
2397 {
2398         if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
2399             (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) ||
2400             (load_code == FW_MSG_CODE_DRV_LOAD_PORT)) {
2401                 bp->port.pmf = 1;
2402                 /* We need the barrier to ensure the ordering between the
2403                  * writing to bp->port.pmf here and reading it from the
2404                  * bnx2x_periodic_task().
2405                  */
2406                 mb();
2407         } else {
2408                 bp->port.pmf = 0;
2409         }
2410
2411         DP(NETIF_MSG_LINK, "pmf %d\n", bp->port.pmf);
2412 }
2413
2414 static void bnx2x_nic_load_afex_dcc(struct bnx2x *bp, int load_code)
2415 {
2416         if (((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
2417              (load_code == FW_MSG_CODE_DRV_LOAD_COMMON_CHIP)) &&
2418             (bp->common.shmem2_base)) {
2419                 if (SHMEM2_HAS(bp, dcc_support))
2420                         SHMEM2_WR(bp, dcc_support,
2421                                   (SHMEM_DCC_SUPPORT_DISABLE_ENABLE_PF_TLV |
2422                                    SHMEM_DCC_SUPPORT_BANDWIDTH_ALLOCATION_TLV));
2423                 if (SHMEM2_HAS(bp, afex_driver_support))
2424                         SHMEM2_WR(bp, afex_driver_support,
2425                                   SHMEM_AFEX_SUPPORTED_VERSION_ONE);
2426         }
2427
2428         /* Set AFEX default VLAN tag to an invalid value */
2429         bp->afex_def_vlan_tag = -1;
2430 }
2431
2432 /**
2433  * bnx2x_bz_fp - zero content of the fastpath structure.
2434  *
2435  * @bp:         driver handle
2436  * @index:      fastpath index to be zeroed
2437  *
2438  * Makes sure the contents of the bp->fp[index].napi is kept
2439  * intact.
2440  */
2441 static void bnx2x_bz_fp(struct bnx2x *bp, int index)
2442 {
2443         struct bnx2x_fastpath *fp = &bp->fp[index];
2444         int cos;
2445         struct napi_struct orig_napi = fp->napi;
2446         struct bnx2x_agg_info *orig_tpa_info = fp->tpa_info;
2447
2448         /* bzero bnx2x_fastpath contents */
2449         if (fp->tpa_info)
2450                 memset(fp->tpa_info, 0, ETH_MAX_AGGREGATION_QUEUES_E1H_E2 *
2451                        sizeof(struct bnx2x_agg_info));
2452         memset(fp, 0, sizeof(*fp));
2453
2454         /* Restore the NAPI object as it has been already initialized */
2455         fp->napi = orig_napi;
2456         fp->tpa_info = orig_tpa_info;
2457         fp->bp = bp;
2458         fp->index = index;
2459         if (IS_ETH_FP(fp))
2460                 fp->max_cos = bp->max_cos;
2461         else
2462                 /* Special queues support only one CoS */
2463                 fp->max_cos = 1;
2464
2465         /* Init txdata pointers */
2466         if (IS_FCOE_FP(fp))
2467                 fp->txdata_ptr[0] = &bp->bnx2x_txq[FCOE_TXQ_IDX(bp)];
2468         if (IS_ETH_FP(fp))
2469                 for_each_cos_in_tx_queue(fp, cos)
2470                         fp->txdata_ptr[cos] = &bp->bnx2x_txq[cos *
2471                                 BNX2X_NUM_ETH_QUEUES(bp) + index];
2472
2473         /* set the tpa flag for each queue. The tpa flag determines the queue
2474          * minimal size so it must be set prior to queue memory allocation
2475          */
2476         fp->disable_tpa = !(bp->flags & TPA_ENABLE_FLAG ||
2477                                   (bp->flags & GRO_ENABLE_FLAG &&
2478                                    bnx2x_mtu_allows_gro(bp->dev->maxmtu)));
2479         if (bp->flags & TPA_ENABLE_FLAG)
2480                 fp->mode = TPA_MODE_LRO;
2481         else if (bp->flags & GRO_ENABLE_FLAG)
2482                 fp->mode = TPA_MODE_GRO;
2483
2484         /* We don't want TPA on an FCoE L2 ring */
2485         if (IS_FCOE_FP(fp))
2486                 fp->disable_tpa = 1;
2487 }
2488
2489 int bnx2x_load_cnic(struct bnx2x *bp)
2490 {
2491         int i, rc, port = BP_PORT(bp);
2492
2493         DP(NETIF_MSG_IFUP, "Starting CNIC-related load\n");
2494
2495         mutex_init(&bp->cnic_mutex);
2496
2497         if (IS_PF(bp)) {
2498                 rc = bnx2x_alloc_mem_cnic(bp);
2499                 if (rc) {
2500                         BNX2X_ERR("Unable to allocate bp memory for cnic\n");
2501                         LOAD_ERROR_EXIT_CNIC(bp, load_error_cnic0);
2502                 }
2503         }
2504
2505         rc = bnx2x_alloc_fp_mem_cnic(bp);
2506         if (rc) {
2507                 BNX2X_ERR("Unable to allocate memory for cnic fps\n");
2508                 LOAD_ERROR_EXIT_CNIC(bp, load_error_cnic0);
2509         }
2510
2511         /* Update the number of queues with the cnic queues */
2512         rc = bnx2x_set_real_num_queues(bp, 1);
2513         if (rc) {
2514                 BNX2X_ERR("Unable to set real_num_queues including cnic\n");
2515                 LOAD_ERROR_EXIT_CNIC(bp, load_error_cnic0);
2516         }
2517
2518         /* Add all CNIC NAPI objects */
2519         bnx2x_add_all_napi_cnic(bp);
2520         DP(NETIF_MSG_IFUP, "cnic napi added\n");
2521         bnx2x_napi_enable_cnic(bp);
2522
2523         rc = bnx2x_init_hw_func_cnic(bp);
2524         if (rc)
2525                 LOAD_ERROR_EXIT_CNIC(bp, load_error_cnic1);
2526
2527         bnx2x_nic_init_cnic(bp);
2528
2529         if (IS_PF(bp)) {
2530                 /* Enable Timer scan */
2531                 REG_WR(bp, TM_REG_EN_LINEAR0_TIMER + port*4, 1);
2532
2533                 /* setup cnic queues */
2534                 for_each_cnic_queue(bp, i) {
2535                         rc = bnx2x_setup_queue(bp, &bp->fp[i], 0);
2536                         if (rc) {
2537                                 BNX2X_ERR("Queue setup failed\n");
2538                                 LOAD_ERROR_EXIT(bp, load_error_cnic2);
2539                         }
2540                 }
2541         }
2542
2543         /* Initialize Rx filter. */
2544         bnx2x_set_rx_mode_inner(bp);
2545
2546         /* re-read iscsi info */
2547         bnx2x_get_iscsi_info(bp);
2548         bnx2x_setup_cnic_irq_info(bp);
2549         bnx2x_setup_cnic_info(bp);
2550         bp->cnic_loaded = true;
2551         if (bp->state == BNX2X_STATE_OPEN)
2552                 bnx2x_cnic_notify(bp, CNIC_CTL_START_CMD);
2553
2554         DP(NETIF_MSG_IFUP, "Ending successfully CNIC-related load\n");
2555
2556         return 0;
2557
2558 #ifndef BNX2X_STOP_ON_ERROR
2559 load_error_cnic2:
2560         /* Disable Timer scan */
2561         REG_WR(bp, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
2562
2563 load_error_cnic1:
2564         bnx2x_napi_disable_cnic(bp);
2565         /* Update the number of queues without the cnic queues */
2566         if (bnx2x_set_real_num_queues(bp, 0))
2567                 BNX2X_ERR("Unable to set real_num_queues not including cnic\n");
2568 load_error_cnic0:
2569         BNX2X_ERR("CNIC-related load failed\n");
2570         bnx2x_free_fp_mem_cnic(bp);
2571         bnx2x_free_mem_cnic(bp);
2572         return rc;
2573 #endif /* ! BNX2X_STOP_ON_ERROR */
2574 }
2575
2576 /* must be called with rtnl_lock */
2577 int bnx2x_nic_load(struct bnx2x *bp, int load_mode)
2578 {
2579         int port = BP_PORT(bp);
2580         int i, rc = 0, load_code = 0;
2581
2582         DP(NETIF_MSG_IFUP, "Starting NIC load\n");
2583         DP(NETIF_MSG_IFUP,
2584            "CNIC is %s\n", CNIC_ENABLED(bp) ? "enabled" : "disabled");
2585
2586 #ifdef BNX2X_STOP_ON_ERROR
2587         if (unlikely(bp->panic)) {
2588                 BNX2X_ERR("Can't load NIC when there is panic\n");
2589                 return -EPERM;
2590         }
2591 #endif
2592
2593         bp->state = BNX2X_STATE_OPENING_WAIT4_LOAD;
2594
2595         /* zero the structure w/o any lock, before SP handler is initialized */
2596         memset(&bp->last_reported_link, 0, sizeof(bp->last_reported_link));
2597         __set_bit(BNX2X_LINK_REPORT_LINK_DOWN,
2598                 &bp->last_reported_link.link_report_flags);
2599
2600         if (IS_PF(bp))
2601                 /* must be called before memory allocation and HW init */
2602                 bnx2x_ilt_set_info(bp);
2603
2604         /*
2605          * Zero fastpath structures preserving invariants like napi, which are
2606          * allocated only once, fp index, max_cos, bp pointer.
2607          * Also set fp->disable_tpa and txdata_ptr.
2608          */
2609         DP(NETIF_MSG_IFUP, "num queues: %d", bp->num_queues);
2610         for_each_queue(bp, i)
2611                 bnx2x_bz_fp(bp, i);
2612         memset(bp->bnx2x_txq, 0, (BNX2X_MAX_RSS_COUNT(bp) * BNX2X_MULTI_TX_COS +
2613                                   bp->num_cnic_queues) *
2614                                   sizeof(struct bnx2x_fp_txdata));
2615
2616         bp->fcoe_init = false;
2617
2618         /* Set the receive queues buffer size */
2619         bnx2x_set_rx_buf_size(bp);
2620
2621         if (IS_PF(bp)) {
2622                 rc = bnx2x_alloc_mem(bp);
2623                 if (rc) {
2624                         BNX2X_ERR("Unable to allocate bp memory\n");
2625                         return rc;
2626                 }
2627         }
2628
2629         /* need to be done after alloc mem, since it's self adjusting to amount
2630          * of memory available for RSS queues
2631          */
2632         rc = bnx2x_alloc_fp_mem(bp);
2633         if (rc) {
2634                 BNX2X_ERR("Unable to allocate memory for fps\n");
2635                 LOAD_ERROR_EXIT(bp, load_error0);
2636         }
2637
2638         /* Allocated memory for FW statistics  */
2639         if (bnx2x_alloc_fw_stats_mem(bp))
2640                 LOAD_ERROR_EXIT(bp, load_error0);
2641
2642         /* request pf to initialize status blocks */
2643         if (IS_VF(bp)) {
2644                 rc = bnx2x_vfpf_init(bp);
2645                 if (rc)
2646                         LOAD_ERROR_EXIT(bp, load_error0);
2647         }
2648
2649         /* As long as bnx2x_alloc_mem() may possibly update
2650          * bp->num_queues, bnx2x_set_real_num_queues() should always
2651          * come after it. At this stage cnic queues are not counted.
2652          */
2653         rc = bnx2x_set_real_num_queues(bp, 0);
2654         if (rc) {
2655                 BNX2X_ERR("Unable to set real_num_queues\n");
2656                 LOAD_ERROR_EXIT(bp, load_error0);
2657         }
2658
2659         /* configure multi cos mappings in kernel.
2660          * this configuration may be overridden by a multi class queue
2661          * discipline or by a dcbx negotiation result.
2662          */
2663         bnx2x_setup_tc(bp->dev, bp->max_cos);
2664
2665         /* Add all NAPI objects */
2666         bnx2x_add_all_napi(bp);
2667         DP(NETIF_MSG_IFUP, "napi added\n");
2668         bnx2x_napi_enable(bp);
2669
2670         if (IS_PF(bp)) {
2671                 /* set pf load just before approaching the MCP */
2672                 bnx2x_set_pf_load(bp);
2673
2674                 /* if mcp exists send load request and analyze response */
2675                 if (!BP_NOMCP(bp)) {
2676                         /* attempt to load pf */
2677                         rc = bnx2x_nic_load_request(bp, &load_code);
2678                         if (rc)
2679                                 LOAD_ERROR_EXIT(bp, load_error1);
2680
2681                         /* what did mcp say? */
2682                         rc = bnx2x_compare_fw_ver(bp, load_code, true);
2683                         if (rc) {
2684                                 bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
2685                                 LOAD_ERROR_EXIT(bp, load_error2);
2686                         }
2687                 } else {
2688                         load_code = bnx2x_nic_load_no_mcp(bp, port);
2689                 }
2690
2691                 /* mark pmf if applicable */
2692                 bnx2x_nic_load_pmf(bp, load_code);
2693
2694                 /* Init Function state controlling object */
2695                 bnx2x__init_func_obj(bp);
2696
2697                 /* Initialize HW */
2698                 rc = bnx2x_init_hw(bp, load_code);
2699                 if (rc) {
2700                         BNX2X_ERR("HW init failed, aborting\n");
2701                         bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
2702                         LOAD_ERROR_EXIT(bp, load_error2);
2703                 }
2704         }
2705
2706         bnx2x_pre_irq_nic_init(bp);
2707
2708         /* Connect to IRQs */
2709         rc = bnx2x_setup_irqs(bp);
2710         if (rc) {
2711                 BNX2X_ERR("setup irqs failed\n");
2712                 if (IS_PF(bp))
2713                         bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
2714                 LOAD_ERROR_EXIT(bp, load_error2);
2715         }
2716
2717         /* Init per-function objects */
2718         if (IS_PF(bp)) {
2719                 /* Setup NIC internals and enable interrupts */
2720                 bnx2x_post_irq_nic_init(bp, load_code);
2721
2722                 bnx2x_init_bp_objs(bp);
2723                 bnx2x_iov_nic_init(bp);
2724
2725                 /* Set AFEX default VLAN tag to an invalid value */
2726                 bp->afex_def_vlan_tag = -1;
2727                 bnx2x_nic_load_afex_dcc(bp, load_code);
2728                 bp->state = BNX2X_STATE_OPENING_WAIT4_PORT;
2729                 rc = bnx2x_func_start(bp);
2730                 if (rc) {
2731                         BNX2X_ERR("Function start failed!\n");
2732                         bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
2733
2734                         LOAD_ERROR_EXIT(bp, load_error3);
2735                 }
2736
2737                 /* Send LOAD_DONE command to MCP */
2738                 if (!BP_NOMCP(bp)) {
2739                         load_code = bnx2x_fw_command(bp,
2740                                                      DRV_MSG_CODE_LOAD_DONE, 0);
2741                         if (!load_code) {
2742                                 BNX2X_ERR("MCP response failure, aborting\n");
2743                                 rc = -EBUSY;
2744                                 LOAD_ERROR_EXIT(bp, load_error3);
2745                         }
2746                 }
2747
2748                 /* initialize FW coalescing state machines in RAM */
2749                 bnx2x_update_coalesce(bp);
2750         }
2751
2752         /* setup the leading queue */
2753         rc = bnx2x_setup_leading(bp);
2754         if (rc) {
2755                 BNX2X_ERR("Setup leading failed!\n");
2756                 LOAD_ERROR_EXIT(bp, load_error3);
2757         }
2758
2759         /* set up the rest of the queues */
2760         for_each_nondefault_eth_queue(bp, i) {
2761                 if (IS_PF(bp))
2762                         rc = bnx2x_setup_queue(bp, &bp->fp[i], false);
2763                 else /* VF */
2764                         rc = bnx2x_vfpf_setup_q(bp, &bp->fp[i], false);
2765                 if (rc) {
2766                         BNX2X_ERR("Queue %d setup failed\n", i);
2767                         LOAD_ERROR_EXIT(bp, load_error3);
2768                 }
2769         }
2770
2771         /* setup rss */
2772         rc = bnx2x_init_rss(bp);
2773         if (rc) {
2774                 BNX2X_ERR("PF RSS init failed\n");
2775                 LOAD_ERROR_EXIT(bp, load_error3);
2776         }
2777
2778         /* Now when Clients are configured we are ready to work */
2779         bp->state = BNX2X_STATE_OPEN;
2780
2781         /* Configure a ucast MAC */
2782         if (IS_PF(bp))
2783                 rc = bnx2x_set_eth_mac(bp, true);
2784         else /* vf */
2785                 rc = bnx2x_vfpf_config_mac(bp, bp->dev->dev_addr, bp->fp->index,
2786                                            true);
2787         if (rc) {
2788                 BNX2X_ERR("Setting Ethernet MAC failed\n");
2789                 LOAD_ERROR_EXIT(bp, load_error3);
2790         }
2791
2792         if (IS_PF(bp) && bp->pending_max) {
2793                 bnx2x_update_max_mf_config(bp, bp->pending_max);
2794                 bp->pending_max = 0;
2795         }
2796
2797         if (bp->port.pmf) {
2798                 rc = bnx2x_initial_phy_init(bp, load_mode);
2799                 if (rc)
2800                         LOAD_ERROR_EXIT(bp, load_error3);
2801         }
2802         bp->link_params.feature_config_flags &= ~FEATURE_CONFIG_BOOT_FROM_SAN;
2803
2804         /* Start fast path */
2805
2806         /* Initialize Rx filter. */
2807         bnx2x_set_rx_mode_inner(bp);
2808
2809         if (bp->flags & PTP_SUPPORTED) {
2810                 bnx2x_init_ptp(bp);
2811                 bnx2x_configure_ptp_filters(bp);
2812         }
2813         /* Start Tx */
2814         switch (load_mode) {
2815         case LOAD_NORMAL:
2816                 /* Tx queue should be only re-enabled */
2817                 netif_tx_wake_all_queues(bp->dev);
2818                 break;
2819
2820         case LOAD_OPEN:
2821                 netif_tx_start_all_queues(bp->dev);
2822                 cmb();
2823                 break;
2824
2825         case LOAD_DIAG:
2826         case LOAD_LOOPBACK_EXT:
2827                 bp->state = BNX2X_STATE_DIAG;
2828                 break;
2829
2830         default:
2831                 break;
2832         }
2833
2834         if (bp->port.pmf)
2835                 bnx2x_update_drv_flags(bp, 1 << DRV_FLAGS_PORT_MASK, 0);
2836         else
2837                 bnx2x__link_status_update(bp);
2838
2839         /* start the timer */
2840         mod_timer(&bp->timer, jiffies + bp->current_interval);
2841
2842         if (CNIC_ENABLED(bp))
2843                 bnx2x_load_cnic(bp);
2844
2845         if (IS_PF(bp))
2846                 bnx2x_schedule_sp_rtnl(bp, BNX2X_SP_RTNL_GET_DRV_VERSION, 0);
2847
2848         if (IS_PF(bp) && SHMEM2_HAS(bp, drv_capabilities_flag)) {
2849                 /* mark driver is loaded in shmem2 */
2850                 uint32_t val;
2851                 val = SHMEM2_RD(bp, drv_capabilities_flag[BP_FW_MB_IDX(bp)]);
2852                 SHMEM2_WR(bp, drv_capabilities_flag[BP_FW_MB_IDX(bp)],
2853                           val | DRV_FLAGS_CAPABILITIES_LOADED_SUPPORTED |
2854                           DRV_FLAGS_CAPABILITIES_LOADED_L2);
2855         }
2856
2857         /* Wait for all pending SP commands to complete */
2858         if (IS_PF(bp) && !bnx2x_wait_sp_comp(bp, ~0x0UL)) {
2859                 BNX2X_ERR("Timeout waiting for SP elements to complete\n");
2860                 bnx2x_nic_unload(bp, UNLOAD_CLOSE, false);
2861                 return -EBUSY;
2862         }
2863
2864         /* If PMF - send ADMIN DCBX msg to MFW to initiate DCBX FSM */
2865         if (bp->port.pmf && (bp->state != BNX2X_STATE_DIAG))
2866                 bnx2x_dcbx_init(bp, false);
2867
2868         DP(NETIF_MSG_IFUP, "Ending successfully NIC load\n");
2869
2870         return 0;
2871
2872 #ifndef BNX2X_STOP_ON_ERROR
2873 load_error3:
2874         if (IS_PF(bp)) {
2875                 bnx2x_int_disable_sync(bp, 1);
2876
2877                 /* Clean queueable objects */
2878                 bnx2x_squeeze_objects(bp);
2879         }
2880
2881         /* Free SKBs, SGEs, TPA pool and driver internals */
2882         bnx2x_free_skbs(bp);
2883         for_each_rx_queue(bp, i)
2884                 bnx2x_free_rx_sge_range(bp, bp->fp + i, NUM_RX_SGE);
2885
2886         /* Release IRQs */
2887         bnx2x_free_irq(bp);
2888 load_error2:
2889         if (IS_PF(bp) && !BP_NOMCP(bp)) {
2890                 bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
2891                 bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_DONE, 0);
2892         }
2893
2894         bp->port.pmf = 0;
2895 load_error1:
2896         bnx2x_napi_disable(bp);
2897         bnx2x_del_all_napi(bp);
2898
2899         /* clear pf_load status, as it was already set */
2900         if (IS_PF(bp))
2901                 bnx2x_clear_pf_load(bp);
2902 load_error0:
2903         bnx2x_free_fw_stats_mem(bp);
2904         bnx2x_free_fp_mem(bp);
2905         bnx2x_free_mem(bp);
2906
2907         return rc;
2908 #endif /* ! BNX2X_STOP_ON_ERROR */
2909 }
2910
2911 int bnx2x_drain_tx_queues(struct bnx2x *bp)
2912 {
2913         uint8_t rc = 0, cos, i;
2914
2915         /* Wait until tx fastpath tasks complete */
2916         for_each_tx_queue(bp, i) {
2917                 struct bnx2x_fastpath *fp = &bp->fp[i];
2918
2919                 for_each_cos_in_tx_queue(fp, cos)
2920                         rc = bnx2x_clean_tx_queue(bp, fp->txdata_ptr[cos]);
2921                 if (rc)
2922                         return rc;
2923         }
2924         return 0;
2925 }
2926
2927 /* must be called with rtnl_lock */
2928 int bnx2x_nic_unload(struct bnx2x *bp, int unload_mode, bool keep_link)
2929 {
2930         int i;
2931         bool global = false;
2932
2933         DP(NETIF_MSG_IFUP, "Starting NIC unload\n");
2934
2935         /* mark driver is unloaded in shmem2 */
2936         if (IS_PF(bp) && SHMEM2_HAS(bp, drv_capabilities_flag)) {
2937                 uint32_t val;
2938                 val = SHMEM2_RD(bp, drv_capabilities_flag[BP_FW_MB_IDX(bp)]);
2939                 SHMEM2_WR(bp, drv_capabilities_flag[BP_FW_MB_IDX(bp)],
2940                           val & ~DRV_FLAGS_CAPABILITIES_LOADED_L2);
2941         }
2942
2943         if (IS_PF(bp) && bp->recovery_state != BNX2X_RECOVERY_DONE &&
2944             (bp->state == BNX2X_STATE_CLOSED ||
2945              bp->state == BNX2X_STATE_ERROR)) {
2946                 /* We can get here if the driver has been unloaded
2947                  * during parity error recovery and is either waiting for a
2948                  * leader to complete or for other functions to unload and
2949                  * then ifdown has been issued. In this case we want to
2950                  * unload and let other functions to complete a recovery
2951                  * process.
2952                  */
2953                 bp->recovery_state = BNX2X_RECOVERY_DONE;
2954                 bp->is_leader = 0;
2955                 bnx2x_release_leader_lock(bp);
2956                 mb();
2957
2958                 DP(NETIF_MSG_IFDOWN, "Releasing a leadership...\n");
2959                 BNX2X_ERR("Can't unload in closed or error state\n");
2960                 return -EINVAL;
2961         }
2962
2963         /* Nothing to do during unload if previous bnx2x_nic_load()
2964          * have not completed successfully - all resources are released.
2965          *
2966          * we can get here only after unsuccessful ndo_* callback, during which
2967          * dev->IFF_UP flag is still on.
2968          */
2969         if (bp->state == BNX2X_STATE_CLOSED || bp->state == BNX2X_STATE_ERROR)
2970                 return 0;
2971
2972         /* It's important to set the bp->state to the value different from
2973          * BNX2X_STATE_OPEN and only then stop the Tx. Otherwise bnx2x_tx_int()
2974          * may restart the Tx from the NAPI context (see bnx2x_tx_int()).
2975          */
2976         bp->state = BNX2X_STATE_CLOSING_WAIT4_HALT;
2977         mb();
2978
2979         /* indicate to VFs that the PF is going down */
2980         bnx2x_iov_channel_down(bp);
2981
2982         if (CNIC_LOADED(bp))
2983                 bnx2x_cnic_notify(bp, CNIC_CTL_STOP_CMD);
2984
2985         /* Stop Tx */
2986         bnx2x_tx_disable(bp);
2987         netdev_reset_tc(bp->dev);
2988
2989         bp->rx_mode = BNX2X_RX_MODE_NONE;
2990
2991         del_timer_sync(&bp->timer);
2992
2993         if (IS_PF(bp)) {
2994                 /* Set ALWAYS_ALIVE bit in shmem */
2995                 bp->fw_drv_pulse_wr_seq |= DRV_PULSE_ALWAYS_ALIVE;
2996                 bnx2x_drv_pulse(bp);
2997                 bnx2x_stats_handle(bp, STATS_EVENT_STOP);
2998                 bnx2x_save_statistics(bp);
2999         }
3000
3001         /* wait till consumers catch up with producers in all queues */
3002         bnx2x_drain_tx_queues(bp);
3003
3004         /* if VF indicate to PF this function is going down (PF will delete sp
3005          * elements and clear initializations
3006          */
3007         if (IS_VF(bp))
3008                 bnx2x_vfpf_close_vf(bp);
3009         else if (unload_mode != UNLOAD_RECOVERY)
3010                 /* if this is a normal/close unload need to clean up chip*/
3011                 bnx2x_chip_cleanup(bp, unload_mode, keep_link);
3012         else {
3013                 /* Send the UNLOAD_REQUEST to the MCP */
3014                 bnx2x_send_unload_req(bp, unload_mode);
3015
3016                 /* Prevent transactions to host from the functions on the
3017                  * engine that doesn't reset global blocks in case of global
3018                  * attention once global blocks are reset and gates are opened
3019                  * (the engine which leader will perform the recovery
3020                  * last).
3021                  */
3022                 if (!CHIP_IS_E1x(bp))
3023                         bnx2x_pf_disable(bp);
3024
3025                 /* Disable HW interrupts, NAPI */
3026                 bnx2x_netif_stop(bp, 1);
3027                 /* Delete all NAPI objects */
3028                 bnx2x_del_all_napi(bp);
3029                 if (CNIC_LOADED(bp))
3030                         bnx2x_del_all_napi_cnic(bp);
3031                 /* Release IRQs */
3032                 bnx2x_free_irq(bp);
3033
3034                 /* Report UNLOAD_DONE to MCP */
3035                 bnx2x_send_unload_done(bp, false);
3036         }
3037
3038         /*
3039          * At this stage no more interrupts will arrive so we may safely clean
3040          * the queueable objects here in case they failed to get cleaned so far.
3041          */
3042         if (IS_PF(bp))
3043                 bnx2x_squeeze_objects(bp);
3044
3045         /* There should be no more pending SP commands at this stage */
3046         bp->sp_state = 0;
3047
3048         bp->port.pmf = 0;
3049
3050         /* clear pending work in rtnl task */
3051         bp->sp_rtnl_state = 0;
3052         mb();
3053
3054         /* Free SKBs, SGEs, TPA pool and driver internals */
3055         bnx2x_free_skbs(bp);
3056         if (CNIC_LOADED(bp))
3057                 bnx2x_free_skbs_cnic(bp);
3058         for_each_rx_queue(bp, i)
3059                 bnx2x_free_rx_sge_range(bp, bp->fp + i, NUM_RX_SGE);
3060
3061         bnx2x_free_fp_mem(bp);
3062         if (CNIC_LOADED(bp))
3063                 bnx2x_free_fp_mem_cnic(bp);
3064
3065         if (IS_PF(bp)) {
3066                 if (CNIC_LOADED(bp))
3067                         bnx2x_free_mem_cnic(bp);
3068         }
3069         bnx2x_free_mem(bp);
3070
3071         bp->state = BNX2X_STATE_CLOSED;
3072         bp->cnic_loaded = false;
3073
3074         /* Clear driver version indication in shmem */
3075         if (IS_PF(bp))
3076                 bnx2x_update_mng_version(bp);
3077
3078         /* Check if there are pending parity attentions. If there are - set
3079          * RECOVERY_IN_PROGRESS.
3080          */
3081         if (IS_PF(bp) && bnx2x_chk_parity_attn(bp, &global, false)) {
3082                 bnx2x_set_reset_in_progress(bp);
3083
3084                 /* Set RESET_IS_GLOBAL if needed */
3085                 if (global)
3086                         bnx2x_set_reset_global(bp);
3087         }
3088
3089         /* The last driver must disable a "close the gate" if there is no
3090          * parity attention or "process kill" pending.
3091          */
3092         if (IS_PF(bp) &&
3093             !bnx2x_clear_pf_load(bp) &&
3094             bnx2x_reset_is_done(bp, BP_PATH(bp)))
3095                 bnx2x_disable_close_the_gate(bp);
3096
3097         DP(NETIF_MSG_IFUP, "Ending NIC unload\n");
3098
3099         return 0;
3100 }
3101
3102 int bnx2x_set_power_state(struct bnx2x *bp, pci_power_t state)
3103 {
3104         uint16_t pmcsr;
3105
3106         /* If there is no power capability, silently succeed */
3107         if (!bp->pdev->pm_cap) {
3108                 BNX2X_DEV_INFO("No power capability. Breaking.\n");
3109                 return 0;
3110         }
3111
3112         pci_read_config_word(bp->pdev, bp->pdev->pm_cap + PCI_PM_CTRL, &pmcsr);
3113
3114         switch (state) {
3115         case PCI_D0:
3116                 pci_write_config_word(bp->pdev, bp->pdev->pm_cap + PCI_PM_CTRL,
3117                                       ((pmcsr & ~PCI_PM_CTRL_STATE_MASK) |
3118                                        PCI_PM_CTRL_PME_STATUS));
3119
3120                 if (pmcsr & PCI_PM_CTRL_STATE_MASK)
3121                         /* delay required during transition out of D3hot */
3122                         kthread_usleep(1000 * 20);
3123                 break;
3124
3125         case PCI_D3hot:
3126                 /* If there are other clients above don't
3127                    shut down the power */
3128                 if (atomic_read(&bp->pdev->enable_cnt) != 1)
3129                         return 0;
3130                 /* Don't shut down the power for emulation and FPGA */
3131                 if (CHIP_REV_IS_SLOW(bp))
3132                         return 0;
3133
3134                 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
3135                 pmcsr |= 3;
3136
3137                 if (bp->wol)
3138                         pmcsr |= PCI_PM_CTRL_PME_ENABLE;
3139
3140                 pci_write_config_word(bp->pdev, bp->pdev->pm_cap + PCI_PM_CTRL,
3141                                       pmcsr);
3142
3143                 /* No more memory access after this point until
3144                 * device is brought back to D0.
3145                 */
3146                 break;
3147
3148         default:
3149                 dev_err(&bp->pdev->dev, "Can't support state = %d\n", state);
3150                 return -EINVAL;
3151         }
3152         return 0;
3153 }
3154
3155 /*
3156  * net_device service functions
3157  */
3158 static int bnx2x_poll(struct napi_struct *napi, int budget)
3159 {
3160         int work_done = 0;
3161         uint8_t cos;
3162         struct bnx2x_fastpath *fp = container_of(napi, struct bnx2x_fastpath,
3163                                                  napi);
3164         struct bnx2x *bp = fp->bp;
3165
3166         while (1) {
3167 #ifdef BNX2X_STOP_ON_ERROR
3168                 if (unlikely(bp->panic)) {
3169                         napi_complete(napi);
3170                         return 0;
3171                 }
3172 #endif
3173                 if (!bnx2x_fp_lock_napi(fp))
3174                         return budget;
3175
3176                 for_each_cos_in_tx_queue(fp, cos)
3177                         if (bnx2x_tx_queue_has_work(fp->txdata_ptr[cos]))
3178                                 bnx2x_tx_int(bp, fp->txdata_ptr[cos]);
3179
3180                 if (bnx2x_has_rx_work(fp)) {
3181                         work_done += bnx2x_rx_int(fp, budget - work_done);
3182
3183                         /* must not complete if we consumed full budget */
3184                         if (work_done >= budget) {
3185                                 bnx2x_fp_unlock_napi(fp);
3186                                 break;
3187                         }
3188                 }
3189
3190                 /* Fall out from the NAPI loop if needed */
3191                 if (!bnx2x_fp_unlock_napi(fp) &&
3192                     !(bnx2x_has_rx_work(fp) || bnx2x_has_tx_work(fp))) {
3193
3194                         /* No need to update SB for FCoE L2 ring as long as
3195                          * it's connected to the default SB and the SB
3196                          * has been updated when NAPI was scheduled.
3197                          */
3198                         if (IS_FCOE_FP(fp)) {
3199                                 napi_complete(napi);
3200                                 break;
3201                         }
3202                         bnx2x_update_fpsb_idx(fp);
3203                         /* bnx2x_has_rx_work() reads the status block,
3204                          * thus we need to ensure that status block indices
3205                          * have been actually read (bnx2x_update_fpsb_idx)
3206                          * prior to this check (bnx2x_has_rx_work) so that
3207                          * we won't write the "newer" value of the status block
3208                          * to IGU (if there was a DMA right after
3209                          * bnx2x_has_rx_work and if there is no rmb, the memory
3210                          * reading (bnx2x_update_fpsb_idx) may be postponed
3211                          * to right before bnx2x_ack_sb). In this case there
3212                          * will never be another interrupt until there is
3213                          * another update of the status block, while there
3214                          * is still unhandled work.
3215                          */
3216                         rmb();
3217
3218                         if (!(bnx2x_has_rx_work(fp) || bnx2x_has_tx_work(fp))) {
3219                                 napi_complete(napi);
3220                                 /* Re-enable interrupts */
3221                                 DP(NETIF_MSG_RX_STATUS,
3222                                    "Update index to %d\n", fp->fp_hc_idx);
3223                                 bnx2x_ack_sb(bp, fp->igu_sb_id, USTORM_ID,
3224                                              le16_to_cpu(fp->fp_hc_idx),
3225                                              IGU_INT_ENABLE, 1);
3226                                 break;
3227                         }
3228                 }
3229         }
3230
3231         return work_done;
3232 }
3233
3234 #ifdef CONFIG_NET_RX_BUSY_POLL
3235 /* must be called with local_bh_disable()d */
3236 int bnx2x_low_latency_recv(struct napi_struct *napi)
3237 {
3238         struct bnx2x_fastpath *fp = container_of(napi, struct bnx2x_fastpath,
3239                                                  napi);
3240         struct bnx2x *bp = fp->bp;
3241         int found = 0;
3242
3243         if ((bp->state == BNX2X_STATE_CLOSED) ||
3244             (bp->state == BNX2X_STATE_ERROR) ||
3245             (bp->flags & (TPA_ENABLE_FLAG | GRO_ENABLE_FLAG)))
3246                 return LL_FLUSH_FAILED;
3247
3248         if (!bnx2x_fp_lock_poll(fp))
3249                 return LL_FLUSH_BUSY;
3250
3251         if (bnx2x_has_rx_work(fp))
3252                 found = bnx2x_rx_int(fp, 4);
3253
3254         bnx2x_fp_unlock_poll(fp);
3255
3256         return found;
3257 }
3258 #endif
3259
3260 /* we split the first BD into headers and data BDs
3261  * to ease the pain of our fellow microcode engineers
3262  * we use one mapping for both BDs
3263  */
3264 static uint16_t bnx2x_tx_split(struct bnx2x *bp,
3265                           struct bnx2x_fp_txdata *txdata,
3266                           struct sw_tx_bd *tx_buf,
3267                           struct eth_tx_start_bd **tx_bd, uint16_t hlen,
3268                           uint16_t bd_prod)
3269 {
3270         struct eth_tx_start_bd *h_tx_bd = *tx_bd;
3271         struct eth_tx_bd *d_tx_bd;
3272         dma_addr_t mapping;
3273         int old_len = le16_to_cpu(h_tx_bd->nbytes);
3274
3275         /* first fix first BD */
3276         h_tx_bd->nbytes = cpu_to_le16(hlen);
3277
3278         DP(NETIF_MSG_TX_QUEUED, "TSO split header size is %d (%x:%x)\n",
3279            h_tx_bd->nbytes, h_tx_bd->addr_hi, h_tx_bd->addr_lo);
3280
3281         /* now get a new data BD
3282          * (after the pbd) and fill it */
3283         bd_prod = TX_BD(NEXT_TX_IDX(bd_prod));
3284         d_tx_bd = &txdata->tx_desc_ring[bd_prod].reg_bd;
3285
3286         mapping = HILO_U64(le32_to_cpu(h_tx_bd->addr_hi),
3287                            le32_to_cpu(h_tx_bd->addr_lo)) + hlen;
3288
3289         d_tx_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
3290         d_tx_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
3291         d_tx_bd->nbytes = cpu_to_le16(old_len - hlen);
3292
3293         /* this marks the BD as one that has no individual mapping */
3294         tx_buf->flags |= BNX2X_TSO_SPLIT_BD;
3295
3296         DP(NETIF_MSG_TX_QUEUED,
3297            "TSO split data size is %d (%x:%x)\n",
3298            d_tx_bd->nbytes, d_tx_bd->addr_hi, d_tx_bd->addr_lo);
3299
3300         /* update tx_bd */
3301         *tx_bd = (struct eth_tx_start_bd *)d_tx_bd;
3302
3303         return bd_prod;
3304 }
3305
3306 #define bswab32(b32) ((__force __le32) swab32((__force __u32) (b32)))
3307 #define bswab16(b16) ((__force __le16) swab16((__force __u16) (b16)))
3308 static __le16 bnx2x_csum_fix(unsigned char *t_header, uint16_t csum,
3309                              int8_t fix)
3310 {
3311         __sum16 tsum = (__force __sum16) csum;
3312
3313         if (fix > 0)
3314                 tsum = ~csum_fold(csum_sub((__force __wsum) csum,
3315                                   csum_partial(t_header - fix, fix, 0)));
3316
3317         else if (fix < 0)
3318                 tsum = ~csum_fold(csum_add((__force __wsum) csum,
3319                                   csum_partial(t_header, -fix, 0)));
3320
3321         return bswab16(tsum);
3322 }
3323
3324 static uint32_t bnx2x_xmit_type(struct bnx2x *bp, struct sk_buff *skb)
3325 {
3326         uint32_t rc;
3327         __u8 prot = 0;
3328         __be16 protocol;
3329
3330         if (skb->ip_summed != CHECKSUM_PARTIAL)
3331                 return XMIT_PLAIN;
3332
3333         protocol = vlan_get_protocol(skb);
3334         if (protocol == cpu_to_be16(ETH_P_IPV6)) {
3335                 rc = XMIT_CSUM_V6;
3336                 prot = ipv6_hdr(skb)->nexthdr;
3337         } else {
3338                 rc = XMIT_CSUM_V4;
3339                 prot = ip_hdr(skb)->protocol;
3340         }
3341
3342         if (!CHIP_IS_E1x(bp) && skb->encapsulation) {
3343                 if (inner_ip_hdr(skb)->version == 6) {
3344                         rc |= XMIT_CSUM_ENC_V6;
3345                         if (inner_ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
3346                                 rc |= XMIT_CSUM_TCP;
3347                 } else {
3348                         rc |= XMIT_CSUM_ENC_V4;
3349                         if (inner_ip_hdr(skb)->protocol == IPPROTO_TCP)
3350                                 rc |= XMIT_CSUM_TCP;
3351                 }
3352         }
3353         if (prot == IPPROTO_TCP)
3354                 rc |= XMIT_CSUM_TCP;
3355
3356         if (skb_is_gso(skb)) {
3357                 if (skb_is_gso_v6(skb)) {
3358                         rc |= (XMIT_GSO_V6 | XMIT_CSUM_TCP);
3359                         if (rc & XMIT_CSUM_ENC)
3360                                 rc |= XMIT_GSO_ENC_V6;
3361                 } else {
3362                         rc |= (XMIT_GSO_V4 | XMIT_CSUM_TCP);
3363                         if (rc & XMIT_CSUM_ENC)
3364                                 rc |= XMIT_GSO_ENC_V4;
3365                 }
3366         }
3367
3368         return rc;
3369 }
3370
3371 #if (MAX_SKB_FRAGS >= MAX_FETCH_BD - 3)
3372 /* check if packet requires linearization (packet is too fragmented)
3373    no need to check fragmentation if page size > 8K (there will be no
3374    violation to FW restrictions) */
3375 static int bnx2x_pkt_req_lin(struct bnx2x *bp, struct sk_buff *skb,
3376                              uint32_t xmit_type)
3377 {
3378         int to_copy = 0;
3379         int hlen = 0;
3380         int first_bd_sz = 0;
3381
3382         /* 3 = 1 (for linear data BD) + 2 (for PBD and last BD) */
3383         if (skb_shinfo(skb)->nr_frags >= (MAX_FETCH_BD - 3)) {
3384
3385                 if (xmit_type & XMIT_GSO) {
3386                         unsigned short lso_mss = skb_shinfo(skb)->gso_size;
3387                         /* Check if LSO packet needs to be copied:
3388                            3 = 1 (for headers BD) + 2 (for PBD and last BD) */
3389                         int wnd_size = MAX_FETCH_BD - 3;
3390                         /* Number of windows to check */
3391                         int num_wnds = skb_shinfo(skb)->nr_frags - wnd_size;
3392                         int wnd_idx = 0;
3393                         int frag_idx = 0;
3394                         uint32_t wnd_sum = 0;
3395
3396                         /* Headers length */
3397                         hlen = (int)(skb_transport_header(skb) - skb->data) +
3398                                 tcp_hdrlen(skb);
3399
3400                         /* Amount of data (w/o headers) on linear part of SKB*/
3401                         first_bd_sz = skb_headlen(skb) - hlen;
3402
3403                         wnd_sum  = first_bd_sz;
3404
3405                         /* Calculate the first sum - it's special */
3406                         for (frag_idx = 0; frag_idx < wnd_size - 1; frag_idx++)
3407                                 wnd_sum +=
3408                                         skb_frag_size(&skb_shinfo(skb)->frags[frag_idx]);
3409
3410                         /* If there was data on linear skb data - check it */
3411                         if (first_bd_sz > 0) {
3412                                 if (unlikely(wnd_sum < lso_mss)) {
3413                                         to_copy = 1;
3414                                         goto exit_lbl;
3415                                 }
3416
3417                                 wnd_sum -= first_bd_sz;
3418                         }
3419
3420                         /* Others are easier: run through the frag list and
3421                            check all windows */
3422                         for (wnd_idx = 0; wnd_idx <= num_wnds; wnd_idx++) {
3423                                 wnd_sum +=
3424                           skb_frag_size(&skb_shinfo(skb)->frags[wnd_idx + wnd_size - 1]);
3425
3426                                 if (unlikely(wnd_sum < lso_mss)) {
3427                                         to_copy = 1;
3428                                         break;
3429                                 }
3430                                 wnd_sum -=
3431                                         skb_frag_size(&skb_shinfo(skb)->frags[wnd_idx]);
3432                         }
3433                 } else {
3434                         /* in non-LSO too fragmented packet should always
3435                            be linearized */
3436                         to_copy = 1;
3437                 }
3438         }
3439
3440 exit_lbl:
3441         if (unlikely(to_copy))
3442                 DP(NETIF_MSG_TX_QUEUED,
3443                    "Linearization IS REQUIRED for %s packet. num_frags %d  hlen %d  first_bd_sz %d\n",
3444                    (xmit_type & XMIT_GSO) ? "LSO" : "non-LSO",
3445                    skb_shinfo(skb)->nr_frags, hlen, first_bd_sz);
3446
3447         return to_copy;
3448 }
3449 #endif
3450
3451 /**
3452  * bnx2x_set_pbd_gso - update PBD in GSO case.