blob: fd6a174dea2f701100609e9046054f4c1a30a5fe [file] [log] [blame]
Michael Chanc0c050c2015-10-22 16:01:17 -04001/* Broadcom NetXtreme-C/E network driver.
2 *
Michael Chan11f15ed2016-04-05 14:08:55 -04003 * Copyright (c) 2014-2016 Broadcom Corporation
Michael Chanc0c050c2015-10-22 16:01:17 -04004 *
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
10#include <linux/module.h>
11
12#include <linux/stringify.h>
13#include <linux/kernel.h>
14#include <linux/timer.h>
15#include <linux/errno.h>
16#include <linux/ioport.h>
17#include <linux/slab.h>
18#include <linux/vmalloc.h>
19#include <linux/interrupt.h>
20#include <linux/pci.h>
21#include <linux/netdevice.h>
22#include <linux/etherdevice.h>
23#include <linux/skbuff.h>
24#include <linux/dma-mapping.h>
25#include <linux/bitops.h>
26#include <linux/io.h>
27#include <linux/irq.h>
28#include <linux/delay.h>
29#include <asm/byteorder.h>
30#include <asm/page.h>
31#include <linux/time.h>
32#include <linux/mii.h>
33#include <linux/if.h>
34#include <linux/if_vlan.h>
35#include <net/ip.h>
36#include <net/tcp.h>
37#include <net/udp.h>
38#include <net/checksum.h>
39#include <net/ip6_checksum.h>
Alexander Duyckad51b8e2016-06-16 12:21:19 -070040#include <net/udp_tunnel.h>
Michael Chanc0c050c2015-10-22 16:01:17 -040041#ifdef CONFIG_NET_RX_BUSY_POLL
42#include <net/busy_poll.h>
43#endif
44#include <linux/workqueue.h>
45#include <linux/prefetch.h>
46#include <linux/cache.h>
47#include <linux/log2.h>
48#include <linux/aer.h>
49#include <linux/bitmap.h>
50#include <linux/cpu_rmap.h>
51
52#include "bnxt_hsi.h"
53#include "bnxt.h"
54#include "bnxt_sriov.h"
55#include "bnxt_ethtool.h"
56
57#define BNXT_TX_TIMEOUT (5 * HZ)
58
59static const char version[] =
60 "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
61
62MODULE_LICENSE("GPL");
63MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
64MODULE_VERSION(DRV_MODULE_VERSION);
65
66#define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
67#define BNXT_RX_DMA_OFFSET NET_SKB_PAD
68#define BNXT_RX_COPY_THRESH 256
69
Michael Chan4419dbe2016-02-10 17:33:49 -050070#define BNXT_TX_PUSH_THRESH 164
Michael Chanc0c050c2015-10-22 16:01:17 -040071
72enum board_idx {
David Christensenfbc9a522015-12-27 18:19:29 -050073 BCM57301,
Michael Chanc0c050c2015-10-22 16:01:17 -040074 BCM57302,
75 BCM57304,
Michael Chanb24eb6a2016-06-13 02:25:36 -040076 BCM57311,
77 BCM57312,
David Christensenfbc9a522015-12-27 18:19:29 -050078 BCM57402,
Michael Chanc0c050c2015-10-22 16:01:17 -040079 BCM57404,
80 BCM57406,
Michael Chanebcd4ee2016-06-13 02:25:32 -040081 BCM57404_NPAR,
Michael Chanb24eb6a2016-06-13 02:25:36 -040082 BCM57412,
83 BCM57414,
84 BCM57416,
85 BCM57417,
86 BCM57414_NPAR,
Michael Chan5049e332016-05-15 03:04:50 -040087 BCM57314,
Michael Chanc0c050c2015-10-22 16:01:17 -040088 BCM57304_VF,
89 BCM57404_VF,
Michael Chanb24eb6a2016-06-13 02:25:36 -040090 BCM57414_VF,
91 BCM57314_VF,
Michael Chanc0c050c2015-10-22 16:01:17 -040092};
93
94/* indexed by enum above */
95static const struct {
96 char *name;
97} board_info[] = {
David Christensenfbc9a522015-12-27 18:19:29 -050098 { "Broadcom BCM57301 NetXtreme-C Single-port 10Gb Ethernet" },
99 { "Broadcom BCM57302 NetXtreme-C Dual-port 10Gb/25Gb Ethernet" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400100 { "Broadcom BCM57304 NetXtreme-C Dual-port 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400101 { "Broadcom BCM57311 NetXtreme-C Single-port 10Gb Ethernet" },
102 { "Broadcom BCM57312 NetXtreme-C Dual-port 10Gb/25Gb Ethernet" },
David Christensenfbc9a522015-12-27 18:19:29 -0500103 { "Broadcom BCM57402 NetXtreme-E Dual-port 10Gb Ethernet" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400104 { "Broadcom BCM57404 NetXtreme-E Dual-port 10Gb/25Gb Ethernet" },
David Christensenfbc9a522015-12-27 18:19:29 -0500105 { "Broadcom BCM57406 NetXtreme-E Dual-port 10GBase-T Ethernet" },
Michael Chanebcd4ee2016-06-13 02:25:32 -0400106 { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400107 { "Broadcom BCM57412 NetXtreme-E Dual-port 10Gb Ethernet" },
108 { "Broadcom BCM57414 NetXtreme-E Dual-port 10Gb/25Gb Ethernet" },
109 { "Broadcom BCM57416 NetXtreme-E Dual-port 10GBase-T Ethernet" },
110 { "Broadcom BCM57417 NetXtreme-E Dual-port 10GBase-T Ethernet" },
111 { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
Michael Chan5049e332016-05-15 03:04:50 -0400112 { "Broadcom BCM57314 NetXtreme-C Dual-port 10Gb/25Gb/40Gb/50Gb Ethernet" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400113 { "Broadcom BCM57304 NetXtreme-C Ethernet Virtual Function" },
114 { "Broadcom BCM57404 NetXtreme-E Ethernet Virtual Function" },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400115 { "Broadcom BCM57414 NetXtreme-E Ethernet Virtual Function" },
116 { "Broadcom BCM57314 NetXtreme-E Ethernet Virtual Function" },
Michael Chanc0c050c2015-10-22 16:01:17 -0400117};
118
119static const struct pci_device_id bnxt_pci_tbl[] = {
David Christensenfbc9a522015-12-27 18:19:29 -0500120 { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400121 { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
122 { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400123 { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
124 { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
David Christensenfbc9a522015-12-27 18:19:29 -0500125 { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400126 { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
127 { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
Michael Chanebcd4ee2016-06-13 02:25:32 -0400128 { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57404_NPAR },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400129 { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
130 { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
131 { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
132 { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
133 { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57414_NPAR },
Michael Chan5049e332016-05-15 03:04:50 -0400134 { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
Michael Chanc0c050c2015-10-22 16:01:17 -0400135#ifdef CONFIG_BNXT_SRIOV
136 { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = BCM57304_VF },
137 { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = BCM57404_VF },
Michael Chanb24eb6a2016-06-13 02:25:36 -0400138 { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = BCM57414_VF },
139 { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = BCM57314_VF },
Michael Chanc0c050c2015-10-22 16:01:17 -0400140#endif
141 { 0 }
142};
143
144MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
145
146static const u16 bnxt_vf_req_snif[] = {
147 HWRM_FUNC_CFG,
148 HWRM_PORT_PHY_QCFG,
149 HWRM_CFA_L2_FILTER_ALLOC,
150};
151
Michael Chan25be8622016-04-05 14:09:00 -0400152static const u16 bnxt_async_events_arr[] = {
153 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
154 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
Michael Chan4bb13ab2016-04-05 14:09:01 -0400155 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
Michael Chanfc0f1922016-06-13 02:25:30 -0400156 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
Michael Chan8cbde112016-04-11 04:11:14 -0400157 HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
Michael Chan25be8622016-04-05 14:09:00 -0400158};
159
Michael Chanc0c050c2015-10-22 16:01:17 -0400160static bool bnxt_vf_pciid(enum board_idx idx)
161{
Michael Chanb24eb6a2016-06-13 02:25:36 -0400162 return (idx == BCM57304_VF || idx == BCM57404_VF ||
163 idx == BCM57314_VF || idx == BCM57414_VF);
Michael Chanc0c050c2015-10-22 16:01:17 -0400164}
165
166#define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
167#define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
168#define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
169
170#define BNXT_CP_DB_REARM(db, raw_cons) \
171 writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
172
173#define BNXT_CP_DB(db, raw_cons) \
174 writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
175
176#define BNXT_CP_DB_IRQ_DIS(db) \
177 writel(DB_CP_IRQ_DIS_FLAGS, db)
178
179static inline u32 bnxt_tx_avail(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
180{
181 /* Tell compiler to fetch tx indices from memory. */
182 barrier();
183
184 return bp->tx_ring_size -
185 ((txr->tx_prod - txr->tx_cons) & bp->tx_ring_mask);
186}
187
188static const u16 bnxt_lhint_arr[] = {
189 TX_BD_FLAGS_LHINT_512_AND_SMALLER,
190 TX_BD_FLAGS_LHINT_512_TO_1023,
191 TX_BD_FLAGS_LHINT_1024_TO_2047,
192 TX_BD_FLAGS_LHINT_1024_TO_2047,
193 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
194 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
195 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
196 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
197 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
198 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
199 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
200 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
201 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
202 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
203 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
204 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
205 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
206 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
207 TX_BD_FLAGS_LHINT_2048_AND_LARGER,
208};
209
210static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
211{
212 struct bnxt *bp = netdev_priv(dev);
213 struct tx_bd *txbd;
214 struct tx_bd_ext *txbd1;
215 struct netdev_queue *txq;
216 int i;
217 dma_addr_t mapping;
218 unsigned int length, pad = 0;
219 u32 len, free_size, vlan_tag_flags, cfa_action, flags;
220 u16 prod, last_frag;
221 struct pci_dev *pdev = bp->pdev;
Michael Chanc0c050c2015-10-22 16:01:17 -0400222 struct bnxt_tx_ring_info *txr;
223 struct bnxt_sw_tx_bd *tx_buf;
224
225 i = skb_get_queue_mapping(skb);
226 if (unlikely(i >= bp->tx_nr_rings)) {
227 dev_kfree_skb_any(skb);
228 return NETDEV_TX_OK;
229 }
230
Michael Chanb6ab4b02016-01-02 23:44:59 -0500231 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -0400232 txq = netdev_get_tx_queue(dev, i);
233 prod = txr->tx_prod;
234
235 free_size = bnxt_tx_avail(bp, txr);
236 if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
237 netif_tx_stop_queue(txq);
238 return NETDEV_TX_BUSY;
239 }
240
241 length = skb->len;
242 len = skb_headlen(skb);
243 last_frag = skb_shinfo(skb)->nr_frags;
244
245 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
246
247 txbd->tx_bd_opaque = prod;
248
249 tx_buf = &txr->tx_buf_ring[prod];
250 tx_buf->skb = skb;
251 tx_buf->nr_frags = last_frag;
252
253 vlan_tag_flags = 0;
254 cfa_action = 0;
255 if (skb_vlan_tag_present(skb)) {
256 vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
257 skb_vlan_tag_get(skb);
258 /* Currently supports 8021Q, 8021AD vlan offloads
259 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
260 */
261 if (skb->vlan_proto == htons(ETH_P_8021Q))
262 vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
263 }
264
265 if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
Michael Chan4419dbe2016-02-10 17:33:49 -0500266 struct tx_push_buffer *tx_push_buf = txr->tx_push;
267 struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
268 struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
269 void *pdata = tx_push_buf->data;
270 u64 *end;
271 int j, push_len;
Michael Chanc0c050c2015-10-22 16:01:17 -0400272
273 /* Set COAL_NOW to be ready quickly for the next push */
274 tx_push->tx_bd_len_flags_type =
275 cpu_to_le32((length << TX_BD_LEN_SHIFT) |
276 TX_BD_TYPE_LONG_TX_BD |
277 TX_BD_FLAGS_LHINT_512_AND_SMALLER |
278 TX_BD_FLAGS_COAL_NOW |
279 TX_BD_FLAGS_PACKET_END |
280 (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
281
282 if (skb->ip_summed == CHECKSUM_PARTIAL)
283 tx_push1->tx_bd_hsize_lflags =
284 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
285 else
286 tx_push1->tx_bd_hsize_lflags = 0;
287
288 tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
289 tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
290
Michael Chanfbb0fa82016-02-22 02:10:26 -0500291 end = pdata + length;
292 end = PTR_ALIGN(end, 8) - 1;
Michael Chan4419dbe2016-02-10 17:33:49 -0500293 *end = 0;
294
Michael Chanc0c050c2015-10-22 16:01:17 -0400295 skb_copy_from_linear_data(skb, pdata, len);
296 pdata += len;
297 for (j = 0; j < last_frag; j++) {
298 skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
299 void *fptr;
300
301 fptr = skb_frag_address_safe(frag);
302 if (!fptr)
303 goto normal_tx;
304
305 memcpy(pdata, fptr, skb_frag_size(frag));
306 pdata += skb_frag_size(frag);
307 }
308
Michael Chan4419dbe2016-02-10 17:33:49 -0500309 txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
310 txbd->tx_bd_haddr = txr->data_mapping;
Michael Chanc0c050c2015-10-22 16:01:17 -0400311 prod = NEXT_TX(prod);
312 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
313 memcpy(txbd, tx_push1, sizeof(*txbd));
314 prod = NEXT_TX(prod);
Michael Chan4419dbe2016-02-10 17:33:49 -0500315 tx_push->doorbell =
Michael Chanc0c050c2015-10-22 16:01:17 -0400316 cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
317 txr->tx_prod = prod;
318
Michael Chanb9a84602016-06-06 02:37:14 -0400319 tx_buf->is_push = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -0400320 netdev_tx_sent_queue(txq, skb->len);
Michael Chanb9a84602016-06-06 02:37:14 -0400321 wmb(); /* Sync is_push and byte queue before pushing data */
Michael Chanc0c050c2015-10-22 16:01:17 -0400322
Michael Chan4419dbe2016-02-10 17:33:49 -0500323 push_len = (length + sizeof(*tx_push) + 7) / 8;
324 if (push_len > 16) {
325 __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
326 __iowrite64_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
327 push_len - 16);
328 } else {
329 __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
330 push_len);
331 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400332
Michael Chanc0c050c2015-10-22 16:01:17 -0400333 goto tx_done;
334 }
335
336normal_tx:
337 if (length < BNXT_MIN_PKT_SIZE) {
338 pad = BNXT_MIN_PKT_SIZE - length;
339 if (skb_pad(skb, pad)) {
340 /* SKB already freed. */
341 tx_buf->skb = NULL;
342 return NETDEV_TX_OK;
343 }
344 length = BNXT_MIN_PKT_SIZE;
345 }
346
347 mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
348
349 if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
350 dev_kfree_skb_any(skb);
351 tx_buf->skb = NULL;
352 return NETDEV_TX_OK;
353 }
354
355 dma_unmap_addr_set(tx_buf, mapping, mapping);
356 flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
357 ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
358
359 txbd->tx_bd_haddr = cpu_to_le64(mapping);
360
361 prod = NEXT_TX(prod);
362 txbd1 = (struct tx_bd_ext *)
363 &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
364
365 txbd1->tx_bd_hsize_lflags = 0;
366 if (skb_is_gso(skb)) {
367 u32 hdr_len;
368
369 if (skb->encapsulation)
370 hdr_len = skb_inner_network_offset(skb) +
371 skb_inner_network_header_len(skb) +
372 inner_tcp_hdrlen(skb);
373 else
374 hdr_len = skb_transport_offset(skb) +
375 tcp_hdrlen(skb);
376
377 txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
378 TX_BD_FLAGS_T_IPID |
379 (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
380 length = skb_shinfo(skb)->gso_size;
381 txbd1->tx_bd_mss = cpu_to_le32(length);
382 length += hdr_len;
383 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
384 txbd1->tx_bd_hsize_lflags =
385 cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
386 txbd1->tx_bd_mss = 0;
387 }
388
389 length >>= 9;
390 flags |= bnxt_lhint_arr[length];
391 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
392
393 txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
394 txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
395 for (i = 0; i < last_frag; i++) {
396 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
397
398 prod = NEXT_TX(prod);
399 txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
400
401 len = skb_frag_size(frag);
402 mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
403 DMA_TO_DEVICE);
404
405 if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
406 goto tx_dma_error;
407
408 tx_buf = &txr->tx_buf_ring[prod];
409 dma_unmap_addr_set(tx_buf, mapping, mapping);
410
411 txbd->tx_bd_haddr = cpu_to_le64(mapping);
412
413 flags = len << TX_BD_LEN_SHIFT;
414 txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
415 }
416
417 flags &= ~TX_BD_LEN;
418 txbd->tx_bd_len_flags_type =
419 cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
420 TX_BD_FLAGS_PACKET_END);
421
422 netdev_tx_sent_queue(txq, skb->len);
423
424 /* Sync BD data before updating doorbell */
425 wmb();
426
427 prod = NEXT_TX(prod);
428 txr->tx_prod = prod;
429
430 writel(DB_KEY_TX | prod, txr->tx_doorbell);
431 writel(DB_KEY_TX | prod, txr->tx_doorbell);
432
433tx_done:
434
435 mmiowb();
436
437 if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
438 netif_tx_stop_queue(txq);
439
440 /* netif_tx_stop_queue() must be done before checking
441 * tx index in bnxt_tx_avail() below, because in
442 * bnxt_tx_int(), we update tx index before checking for
443 * netif_tx_queue_stopped().
444 */
445 smp_mb();
446 if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
447 netif_tx_wake_queue(txq);
448 }
449 return NETDEV_TX_OK;
450
451tx_dma_error:
452 last_frag = i;
453
454 /* start back at beginning and unmap skb */
455 prod = txr->tx_prod;
456 tx_buf = &txr->tx_buf_ring[prod];
457 tx_buf->skb = NULL;
458 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
459 skb_headlen(skb), PCI_DMA_TODEVICE);
460 prod = NEXT_TX(prod);
461
462 /* unmap remaining mapped pages */
463 for (i = 0; i < last_frag; i++) {
464 prod = NEXT_TX(prod);
465 tx_buf = &txr->tx_buf_ring[prod];
466 dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
467 skb_frag_size(&skb_shinfo(skb)->frags[i]),
468 PCI_DMA_TODEVICE);
469 }
470
471 dev_kfree_skb_any(skb);
472 return NETDEV_TX_OK;
473}
474
475static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
476{
Michael Chanb6ab4b02016-01-02 23:44:59 -0500477 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chanb81a90d2016-01-02 23:45:01 -0500478 int index = txr - &bp->tx_ring[0];
Michael Chanc0c050c2015-10-22 16:01:17 -0400479 struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, index);
480 u16 cons = txr->tx_cons;
481 struct pci_dev *pdev = bp->pdev;
482 int i;
483 unsigned int tx_bytes = 0;
484
485 for (i = 0; i < nr_pkts; i++) {
486 struct bnxt_sw_tx_bd *tx_buf;
487 struct sk_buff *skb;
488 int j, last;
489
490 tx_buf = &txr->tx_buf_ring[cons];
491 cons = NEXT_TX(cons);
492 skb = tx_buf->skb;
493 tx_buf->skb = NULL;
494
495 if (tx_buf->is_push) {
496 tx_buf->is_push = 0;
497 goto next_tx_int;
498 }
499
500 dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
501 skb_headlen(skb), PCI_DMA_TODEVICE);
502 last = tx_buf->nr_frags;
503
504 for (j = 0; j < last; j++) {
505 cons = NEXT_TX(cons);
506 tx_buf = &txr->tx_buf_ring[cons];
507 dma_unmap_page(
508 &pdev->dev,
509 dma_unmap_addr(tx_buf, mapping),
510 skb_frag_size(&skb_shinfo(skb)->frags[j]),
511 PCI_DMA_TODEVICE);
512 }
513
514next_tx_int:
515 cons = NEXT_TX(cons);
516
517 tx_bytes += skb->len;
518 dev_kfree_skb_any(skb);
519 }
520
521 netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
522 txr->tx_cons = cons;
523
524 /* Need to make the tx_cons update visible to bnxt_start_xmit()
525 * before checking for netif_tx_queue_stopped(). Without the
526 * memory barrier, there is a small possibility that bnxt_start_xmit()
527 * will miss it and cause the queue to be stopped forever.
528 */
529 smp_mb();
530
531 if (unlikely(netif_tx_queue_stopped(txq)) &&
532 (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
533 __netif_tx_lock(txq, smp_processor_id());
534 if (netif_tx_queue_stopped(txq) &&
535 bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
536 txr->dev_state != BNXT_DEV_STATE_CLOSING)
537 netif_tx_wake_queue(txq);
538 __netif_tx_unlock(txq);
539 }
540}
541
542static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
543 gfp_t gfp)
544{
545 u8 *data;
546 struct pci_dev *pdev = bp->pdev;
547
548 data = kmalloc(bp->rx_buf_size, gfp);
549 if (!data)
550 return NULL;
551
552 *mapping = dma_map_single(&pdev->dev, data + BNXT_RX_DMA_OFFSET,
553 bp->rx_buf_use_size, PCI_DMA_FROMDEVICE);
554
555 if (dma_mapping_error(&pdev->dev, *mapping)) {
556 kfree(data);
557 data = NULL;
558 }
559 return data;
560}
561
562static inline int bnxt_alloc_rx_data(struct bnxt *bp,
563 struct bnxt_rx_ring_info *rxr,
564 u16 prod, gfp_t gfp)
565{
566 struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
567 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
568 u8 *data;
569 dma_addr_t mapping;
570
571 data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
572 if (!data)
573 return -ENOMEM;
574
575 rx_buf->data = data;
576 dma_unmap_addr_set(rx_buf, mapping, mapping);
577
578 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
579
580 return 0;
581}
582
583static void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons,
584 u8 *data)
585{
586 u16 prod = rxr->rx_prod;
587 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
588 struct rx_bd *cons_bd, *prod_bd;
589
590 prod_rx_buf = &rxr->rx_buf_ring[prod];
591 cons_rx_buf = &rxr->rx_buf_ring[cons];
592
593 prod_rx_buf->data = data;
594
595 dma_unmap_addr_set(prod_rx_buf, mapping,
596 dma_unmap_addr(cons_rx_buf, mapping));
597
598 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
599 cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
600
601 prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
602}
603
604static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
605{
606 u16 next, max = rxr->rx_agg_bmap_size;
607
608 next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
609 if (next >= max)
610 next = find_first_zero_bit(rxr->rx_agg_bmap, max);
611 return next;
612}
613
614static inline int bnxt_alloc_rx_page(struct bnxt *bp,
615 struct bnxt_rx_ring_info *rxr,
616 u16 prod, gfp_t gfp)
617{
618 struct rx_bd *rxbd =
619 &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
620 struct bnxt_sw_rx_agg_bd *rx_agg_buf;
621 struct pci_dev *pdev = bp->pdev;
622 struct page *page;
623 dma_addr_t mapping;
624 u16 sw_prod = rxr->rx_sw_agg_prod;
Michael Chan89d0a062016-04-25 02:30:51 -0400625 unsigned int offset = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -0400626
Michael Chan89d0a062016-04-25 02:30:51 -0400627 if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
628 page = rxr->rx_page;
629 if (!page) {
630 page = alloc_page(gfp);
631 if (!page)
632 return -ENOMEM;
633 rxr->rx_page = page;
634 rxr->rx_page_offset = 0;
635 }
636 offset = rxr->rx_page_offset;
637 rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
638 if (rxr->rx_page_offset == PAGE_SIZE)
639 rxr->rx_page = NULL;
640 else
641 get_page(page);
642 } else {
643 page = alloc_page(gfp);
644 if (!page)
645 return -ENOMEM;
646 }
Michael Chanc0c050c2015-10-22 16:01:17 -0400647
Michael Chan89d0a062016-04-25 02:30:51 -0400648 mapping = dma_map_page(&pdev->dev, page, offset, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400649 PCI_DMA_FROMDEVICE);
650 if (dma_mapping_error(&pdev->dev, mapping)) {
651 __free_page(page);
652 return -EIO;
653 }
654
655 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
656 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
657
658 __set_bit(sw_prod, rxr->rx_agg_bmap);
659 rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
660 rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
661
662 rx_agg_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400663 rx_agg_buf->offset = offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400664 rx_agg_buf->mapping = mapping;
665 rxbd->rx_bd_haddr = cpu_to_le64(mapping);
666 rxbd->rx_bd_opaque = sw_prod;
667 return 0;
668}
669
670static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
671 u32 agg_bufs)
672{
673 struct bnxt *bp = bnapi->bp;
674 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500675 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400676 u16 prod = rxr->rx_agg_prod;
677 u16 sw_prod = rxr->rx_sw_agg_prod;
678 u32 i;
679
680 for (i = 0; i < agg_bufs; i++) {
681 u16 cons;
682 struct rx_agg_cmp *agg;
683 struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
684 struct rx_bd *prod_bd;
685 struct page *page;
686
687 agg = (struct rx_agg_cmp *)
688 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
689 cons = agg->rx_agg_cmp_opaque;
690 __clear_bit(cons, rxr->rx_agg_bmap);
691
692 if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
693 sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
694
695 __set_bit(sw_prod, rxr->rx_agg_bmap);
696 prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
697 cons_rx_buf = &rxr->rx_agg_ring[cons];
698
699 /* It is possible for sw_prod to be equal to cons, so
700 * set cons_rx_buf->page to NULL first.
701 */
702 page = cons_rx_buf->page;
703 cons_rx_buf->page = NULL;
704 prod_rx_buf->page = page;
Michael Chan89d0a062016-04-25 02:30:51 -0400705 prod_rx_buf->offset = cons_rx_buf->offset;
Michael Chanc0c050c2015-10-22 16:01:17 -0400706
707 prod_rx_buf->mapping = cons_rx_buf->mapping;
708
709 prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
710
711 prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
712 prod_bd->rx_bd_opaque = sw_prod;
713
714 prod = NEXT_RX_AGG(prod);
715 sw_prod = NEXT_RX_AGG(sw_prod);
716 cp_cons = NEXT_CMP(cp_cons);
717 }
718 rxr->rx_agg_prod = prod;
719 rxr->rx_sw_agg_prod = sw_prod;
720}
721
722static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
723 struct bnxt_rx_ring_info *rxr, u16 cons,
724 u16 prod, u8 *data, dma_addr_t dma_addr,
725 unsigned int len)
726{
727 int err;
728 struct sk_buff *skb;
729
730 err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
731 if (unlikely(err)) {
732 bnxt_reuse_rx_data(rxr, cons, data);
733 return NULL;
734 }
735
736 skb = build_skb(data, 0);
737 dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
738 PCI_DMA_FROMDEVICE);
739 if (!skb) {
740 kfree(data);
741 return NULL;
742 }
743
744 skb_reserve(skb, BNXT_RX_OFFSET);
745 skb_put(skb, len);
746 return skb;
747}
748
749static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
750 struct sk_buff *skb, u16 cp_cons,
751 u32 agg_bufs)
752{
753 struct pci_dev *pdev = bp->pdev;
754 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -0500755 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -0400756 u16 prod = rxr->rx_agg_prod;
757 u32 i;
758
759 for (i = 0; i < agg_bufs; i++) {
760 u16 cons, frag_len;
761 struct rx_agg_cmp *agg;
762 struct bnxt_sw_rx_agg_bd *cons_rx_buf;
763 struct page *page;
764 dma_addr_t mapping;
765
766 agg = (struct rx_agg_cmp *)
767 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
768 cons = agg->rx_agg_cmp_opaque;
769 frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
770 RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
771
772 cons_rx_buf = &rxr->rx_agg_ring[cons];
Michael Chan89d0a062016-04-25 02:30:51 -0400773 skb_fill_page_desc(skb, i, cons_rx_buf->page,
774 cons_rx_buf->offset, frag_len);
Michael Chanc0c050c2015-10-22 16:01:17 -0400775 __clear_bit(cons, rxr->rx_agg_bmap);
776
777 /* It is possible for bnxt_alloc_rx_page() to allocate
778 * a sw_prod index that equals the cons index, so we
779 * need to clear the cons entry now.
780 */
781 mapping = dma_unmap_addr(cons_rx_buf, mapping);
782 page = cons_rx_buf->page;
783 cons_rx_buf->page = NULL;
784
785 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
786 struct skb_shared_info *shinfo;
787 unsigned int nr_frags;
788
789 shinfo = skb_shinfo(skb);
790 nr_frags = --shinfo->nr_frags;
791 __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
792
793 dev_kfree_skb(skb);
794
795 cons_rx_buf->page = page;
796
797 /* Update prod since possibly some pages have been
798 * allocated already.
799 */
800 rxr->rx_agg_prod = prod;
801 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
802 return NULL;
803 }
804
Michael Chan2839f282016-04-25 02:30:50 -0400805 dma_unmap_page(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
Michael Chanc0c050c2015-10-22 16:01:17 -0400806 PCI_DMA_FROMDEVICE);
807
808 skb->data_len += frag_len;
809 skb->len += frag_len;
810 skb->truesize += PAGE_SIZE;
811
812 prod = NEXT_RX_AGG(prod);
813 cp_cons = NEXT_CMP(cp_cons);
814 }
815 rxr->rx_agg_prod = prod;
816 return skb;
817}
818
819static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
820 u8 agg_bufs, u32 *raw_cons)
821{
822 u16 last;
823 struct rx_agg_cmp *agg;
824
825 *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
826 last = RING_CMP(*raw_cons);
827 agg = (struct rx_agg_cmp *)
828 &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
829 return RX_AGG_CMP_VALID(agg, *raw_cons);
830}
831
832static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
833 unsigned int len,
834 dma_addr_t mapping)
835{
836 struct bnxt *bp = bnapi->bp;
837 struct pci_dev *pdev = bp->pdev;
838 struct sk_buff *skb;
839
840 skb = napi_alloc_skb(&bnapi->napi, len);
841 if (!skb)
842 return NULL;
843
844 dma_sync_single_for_cpu(&pdev->dev, mapping,
845 bp->rx_copy_thresh, PCI_DMA_FROMDEVICE);
846
847 memcpy(skb->data - BNXT_RX_OFFSET, data, len + BNXT_RX_OFFSET);
848
849 dma_sync_single_for_device(&pdev->dev, mapping,
850 bp->rx_copy_thresh,
851 PCI_DMA_FROMDEVICE);
852
853 skb_put(skb, len);
854 return skb;
855}
856
Michael Chanfa7e2812016-05-10 19:18:00 -0400857static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
858 u32 *raw_cons, void *cmp)
859{
860 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
861 struct rx_cmp *rxcmp = cmp;
862 u32 tmp_raw_cons = *raw_cons;
863 u8 cmp_type, agg_bufs = 0;
864
865 cmp_type = RX_CMP_TYPE(rxcmp);
866
867 if (cmp_type == CMP_TYPE_RX_L2_CMP) {
868 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
869 RX_CMP_AGG_BUFS) >>
870 RX_CMP_AGG_BUFS_SHIFT;
871 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
872 struct rx_tpa_end_cmp *tpa_end = cmp;
873
874 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
875 RX_TPA_END_CMP_AGG_BUFS) >>
876 RX_TPA_END_CMP_AGG_BUFS_SHIFT;
877 }
878
879 if (agg_bufs) {
880 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
881 return -EBUSY;
882 }
883 *raw_cons = tmp_raw_cons;
884 return 0;
885}
886
887static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
888{
889 if (!rxr->bnapi->in_reset) {
890 rxr->bnapi->in_reset = true;
891 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
892 schedule_work(&bp->sp_task);
893 }
894 rxr->rx_next_cons = 0xffff;
895}
896
Michael Chanc0c050c2015-10-22 16:01:17 -0400897static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
898 struct rx_tpa_start_cmp *tpa_start,
899 struct rx_tpa_start_cmp_ext *tpa_start1)
900{
901 u8 agg_id = TPA_START_AGG_ID(tpa_start);
902 u16 cons, prod;
903 struct bnxt_tpa_info *tpa_info;
904 struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
905 struct rx_bd *prod_bd;
906 dma_addr_t mapping;
907
908 cons = tpa_start->rx_tpa_start_cmp_opaque;
909 prod = rxr->rx_prod;
910 cons_rx_buf = &rxr->rx_buf_ring[cons];
911 prod_rx_buf = &rxr->rx_buf_ring[prod];
912 tpa_info = &rxr->rx_tpa[agg_id];
913
Michael Chanfa7e2812016-05-10 19:18:00 -0400914 if (unlikely(cons != rxr->rx_next_cons)) {
915 bnxt_sched_reset(bp, rxr);
916 return;
917 }
918
Michael Chanc0c050c2015-10-22 16:01:17 -0400919 prod_rx_buf->data = tpa_info->data;
920
921 mapping = tpa_info->mapping;
922 dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
923
924 prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
925
926 prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
927
928 tpa_info->data = cons_rx_buf->data;
929 cons_rx_buf->data = NULL;
930 tpa_info->mapping = dma_unmap_addr(cons_rx_buf, mapping);
931
932 tpa_info->len =
933 le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
934 RX_TPA_START_CMP_LEN_SHIFT;
935 if (likely(TPA_START_HASH_VALID(tpa_start))) {
936 u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
937
938 tpa_info->hash_type = PKT_HASH_TYPE_L4;
939 tpa_info->gso_type = SKB_GSO_TCPV4;
940 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
941 if (hash_type == 3)
942 tpa_info->gso_type = SKB_GSO_TCPV6;
943 tpa_info->rss_hash =
944 le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
945 } else {
946 tpa_info->hash_type = PKT_HASH_TYPE_NONE;
947 tpa_info->gso_type = 0;
948 if (netif_msg_rx_err(bp))
949 netdev_warn(bp->dev, "TPA packet without valid hash\n");
950 }
951 tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
952 tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
Michael Chan94758f82016-06-13 02:25:35 -0400953 tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
Michael Chanc0c050c2015-10-22 16:01:17 -0400954
955 rxr->rx_prod = NEXT_RX(prod);
956 cons = NEXT_RX(cons);
Michael Chan376a5b82016-05-10 19:17:59 -0400957 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -0400958 cons_rx_buf = &rxr->rx_buf_ring[cons];
959
960 bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
961 rxr->rx_prod = NEXT_RX(rxr->rx_prod);
962 cons_rx_buf->data = NULL;
963}
964
965static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
966 u16 cp_cons, u32 agg_bufs)
967{
968 if (agg_bufs)
969 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
970}
971
Michael Chan94758f82016-06-13 02:25:35 -0400972static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
973 int payload_off, int tcp_ts,
974 struct sk_buff *skb)
975{
976#ifdef CONFIG_INET
977 struct tcphdr *th;
978 int len, nw_off;
979 u16 outer_ip_off, inner_ip_off, inner_mac_off;
980 u32 hdr_info = tpa_info->hdr_info;
981 bool loopback = false;
982
983 inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
984 inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
985 outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
986
987 /* If the packet is an internal loopback packet, the offsets will
988 * have an extra 4 bytes.
989 */
990 if (inner_mac_off == 4) {
991 loopback = true;
992 } else if (inner_mac_off > 4) {
993 __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
994 ETH_HLEN - 2));
995
996 /* We only support inner iPv4/ipv6. If we don't see the
997 * correct protocol ID, it must be a loopback packet where
998 * the offsets are off by 4.
999 */
Dan Carpenter09a76362016-07-07 11:23:09 +03001000 if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
Michael Chan94758f82016-06-13 02:25:35 -04001001 loopback = true;
1002 }
1003 if (loopback) {
1004 /* internal loopback packet, subtract all offsets by 4 */
1005 inner_ip_off -= 4;
1006 inner_mac_off -= 4;
1007 outer_ip_off -= 4;
1008 }
1009
1010 nw_off = inner_ip_off - ETH_HLEN;
1011 skb_set_network_header(skb, nw_off);
1012 if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1013 struct ipv6hdr *iph = ipv6_hdr(skb);
1014
1015 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1016 len = skb->len - skb_transport_offset(skb);
1017 th = tcp_hdr(skb);
1018 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1019 } else {
1020 struct iphdr *iph = ip_hdr(skb);
1021
1022 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1023 len = skb->len - skb_transport_offset(skb);
1024 th = tcp_hdr(skb);
1025 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1026 }
1027
1028 if (inner_mac_off) { /* tunnel */
1029 struct udphdr *uh = NULL;
1030 __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1031 ETH_HLEN - 2));
1032
1033 if (proto == htons(ETH_P_IP)) {
1034 struct iphdr *iph = (struct iphdr *)skb->data;
1035
1036 if (iph->protocol == IPPROTO_UDP)
1037 uh = (struct udphdr *)(iph + 1);
1038 } else {
1039 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1040
1041 if (iph->nexthdr == IPPROTO_UDP)
1042 uh = (struct udphdr *)(iph + 1);
1043 }
1044 if (uh) {
1045 if (uh->check)
1046 skb_shinfo(skb)->gso_type |=
1047 SKB_GSO_UDP_TUNNEL_CSUM;
1048 else
1049 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1050 }
1051 }
1052#endif
1053 return skb;
1054}
1055
Michael Chanc0c050c2015-10-22 16:01:17 -04001056#define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
1057#define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1058
Michael Chan309369c2016-06-13 02:25:34 -04001059static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1060 int payload_off, int tcp_ts,
Michael Chanc0c050c2015-10-22 16:01:17 -04001061 struct sk_buff *skb)
1062{
Michael Chand1611c32015-10-25 22:27:57 -04001063#ifdef CONFIG_INET
Michael Chanc0c050c2015-10-22 16:01:17 -04001064 struct tcphdr *th;
Michael Chan309369c2016-06-13 02:25:34 -04001065 int len, nw_off, tcp_opt_len;
Michael Chanc0c050c2015-10-22 16:01:17 -04001066
Michael Chan309369c2016-06-13 02:25:34 -04001067 if (tcp_ts)
Michael Chanc0c050c2015-10-22 16:01:17 -04001068 tcp_opt_len = 12;
1069
Michael Chanc0c050c2015-10-22 16:01:17 -04001070 if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1071 struct iphdr *iph;
1072
1073 nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1074 ETH_HLEN;
1075 skb_set_network_header(skb, nw_off);
1076 iph = ip_hdr(skb);
1077 skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1078 len = skb->len - skb_transport_offset(skb);
1079 th = tcp_hdr(skb);
1080 th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1081 } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1082 struct ipv6hdr *iph;
1083
1084 nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1085 ETH_HLEN;
1086 skb_set_network_header(skb, nw_off);
1087 iph = ipv6_hdr(skb);
1088 skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1089 len = skb->len - skb_transport_offset(skb);
1090 th = tcp_hdr(skb);
1091 th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1092 } else {
1093 dev_kfree_skb_any(skb);
1094 return NULL;
1095 }
1096 tcp_gro_complete(skb);
1097
1098 if (nw_off) { /* tunnel */
1099 struct udphdr *uh = NULL;
1100
1101 if (skb->protocol == htons(ETH_P_IP)) {
1102 struct iphdr *iph = (struct iphdr *)skb->data;
1103
1104 if (iph->protocol == IPPROTO_UDP)
1105 uh = (struct udphdr *)(iph + 1);
1106 } else {
1107 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1108
1109 if (iph->nexthdr == IPPROTO_UDP)
1110 uh = (struct udphdr *)(iph + 1);
1111 }
1112 if (uh) {
1113 if (uh->check)
1114 skb_shinfo(skb)->gso_type |=
1115 SKB_GSO_UDP_TUNNEL_CSUM;
1116 else
1117 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1118 }
1119 }
1120#endif
1121 return skb;
1122}
1123
Michael Chan309369c2016-06-13 02:25:34 -04001124static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1125 struct bnxt_tpa_info *tpa_info,
1126 struct rx_tpa_end_cmp *tpa_end,
1127 struct rx_tpa_end_cmp_ext *tpa_end1,
1128 struct sk_buff *skb)
1129{
1130#ifdef CONFIG_INET
1131 int payload_off;
1132 u16 segs;
1133
1134 segs = TPA_END_TPA_SEGS(tpa_end);
1135 if (segs == 1)
1136 return skb;
1137
1138 NAPI_GRO_CB(skb)->count = segs;
1139 skb_shinfo(skb)->gso_size =
1140 le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1141 skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1142 payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1143 RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
1144 RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
1145 skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1146#endif
1147 return skb;
1148}
1149
Michael Chanc0c050c2015-10-22 16:01:17 -04001150static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1151 struct bnxt_napi *bnapi,
1152 u32 *raw_cons,
1153 struct rx_tpa_end_cmp *tpa_end,
1154 struct rx_tpa_end_cmp_ext *tpa_end1,
1155 bool *agg_event)
1156{
1157 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001158 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001159 u8 agg_id = TPA_END_AGG_ID(tpa_end);
1160 u8 *data, agg_bufs;
1161 u16 cp_cons = RING_CMP(*raw_cons);
1162 unsigned int len;
1163 struct bnxt_tpa_info *tpa_info;
1164 dma_addr_t mapping;
1165 struct sk_buff *skb;
1166
Michael Chanfa7e2812016-05-10 19:18:00 -04001167 if (unlikely(bnapi->in_reset)) {
1168 int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
1169
1170 if (rc < 0)
1171 return ERR_PTR(-EBUSY);
1172 return NULL;
1173 }
1174
Michael Chanc0c050c2015-10-22 16:01:17 -04001175 tpa_info = &rxr->rx_tpa[agg_id];
1176 data = tpa_info->data;
1177 prefetch(data);
1178 len = tpa_info->len;
1179 mapping = tpa_info->mapping;
1180
1181 agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
1182 RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
1183
1184 if (agg_bufs) {
1185 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1186 return ERR_PTR(-EBUSY);
1187
1188 *agg_event = true;
1189 cp_cons = NEXT_CMP(cp_cons);
1190 }
1191
1192 if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
1193 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1194 netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1195 agg_bufs, (int)MAX_SKB_FRAGS);
1196 return NULL;
1197 }
1198
1199 if (len <= bp->rx_copy_thresh) {
1200 skb = bnxt_copy_skb(bnapi, data, len, mapping);
1201 if (!skb) {
1202 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1203 return NULL;
1204 }
1205 } else {
1206 u8 *new_data;
1207 dma_addr_t new_mapping;
1208
1209 new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
1210 if (!new_data) {
1211 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1212 return NULL;
1213 }
1214
1215 tpa_info->data = new_data;
1216 tpa_info->mapping = new_mapping;
1217
1218 skb = build_skb(data, 0);
1219 dma_unmap_single(&bp->pdev->dev, mapping, bp->rx_buf_use_size,
1220 PCI_DMA_FROMDEVICE);
1221
1222 if (!skb) {
1223 kfree(data);
1224 bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
1225 return NULL;
1226 }
1227 skb_reserve(skb, BNXT_RX_OFFSET);
1228 skb_put(skb, len);
1229 }
1230
1231 if (agg_bufs) {
1232 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1233 if (!skb) {
1234 /* Page reuse already handled by bnxt_rx_pages(). */
1235 return NULL;
1236 }
1237 }
1238 skb->protocol = eth_type_trans(skb, bp->dev);
1239
1240 if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1241 skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1242
Michael Chan8852ddb2016-06-06 02:37:16 -04001243 if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
1244 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001245 u16 vlan_proto = tpa_info->metadata >>
1246 RX_CMP_FLAGS2_METADATA_TPID_SFT;
Michael Chan8852ddb2016-06-06 02:37:16 -04001247 u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001248
Michael Chan8852ddb2016-06-06 02:37:16 -04001249 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001250 }
1251
1252 skb_checksum_none_assert(skb);
1253 if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1254 skb->ip_summed = CHECKSUM_UNNECESSARY;
1255 skb->csum_level =
1256 (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1257 }
1258
1259 if (TPA_END_GRO(tpa_end))
Michael Chan309369c2016-06-13 02:25:34 -04001260 skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
Michael Chanc0c050c2015-10-22 16:01:17 -04001261
1262 return skb;
1263}
1264
1265/* returns the following:
1266 * 1 - 1 packet successfully received
1267 * 0 - successful TPA_START, packet not completed yet
1268 * -EBUSY - completion ring does not have all the agg buffers yet
1269 * -ENOMEM - packet aborted due to out of memory
1270 * -EIO - packet aborted due to hw error indicated in BD
1271 */
1272static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
1273 bool *agg_event)
1274{
1275 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
Michael Chanb6ab4b02016-01-02 23:44:59 -05001276 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001277 struct net_device *dev = bp->dev;
1278 struct rx_cmp *rxcmp;
1279 struct rx_cmp_ext *rxcmp1;
1280 u32 tmp_raw_cons = *raw_cons;
1281 u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
1282 struct bnxt_sw_rx_bd *rx_buf;
1283 unsigned int len;
1284 u8 *data, agg_bufs, cmp_type;
1285 dma_addr_t dma_addr;
1286 struct sk_buff *skb;
1287 int rc = 0;
1288
1289 rxcmp = (struct rx_cmp *)
1290 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1291
1292 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
1293 cp_cons = RING_CMP(tmp_raw_cons);
1294 rxcmp1 = (struct rx_cmp_ext *)
1295 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1296
1297 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1298 return -EBUSY;
1299
1300 cmp_type = RX_CMP_TYPE(rxcmp);
1301
1302 prod = rxr->rx_prod;
1303
1304 if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
1305 bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
1306 (struct rx_tpa_start_cmp_ext *)rxcmp1);
1307
1308 goto next_rx_no_prod;
1309
1310 } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1311 skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
1312 (struct rx_tpa_end_cmp *)rxcmp,
1313 (struct rx_tpa_end_cmp_ext *)rxcmp1,
1314 agg_event);
1315
1316 if (unlikely(IS_ERR(skb)))
1317 return -EBUSY;
1318
1319 rc = -ENOMEM;
1320 if (likely(skb)) {
1321 skb_record_rx_queue(skb, bnapi->index);
1322 skb_mark_napi_id(skb, &bnapi->napi);
1323 if (bnxt_busy_polling(bnapi))
1324 netif_receive_skb(skb);
1325 else
1326 napi_gro_receive(&bnapi->napi, skb);
1327 rc = 1;
1328 }
1329 goto next_rx_no_prod;
1330 }
1331
1332 cons = rxcmp->rx_cmp_opaque;
1333 rx_buf = &rxr->rx_buf_ring[cons];
1334 data = rx_buf->data;
Michael Chanfa7e2812016-05-10 19:18:00 -04001335 if (unlikely(cons != rxr->rx_next_cons)) {
1336 int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
1337
1338 bnxt_sched_reset(bp, rxr);
1339 return rc1;
1340 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001341 prefetch(data);
1342
1343 agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & RX_CMP_AGG_BUFS) >>
1344 RX_CMP_AGG_BUFS_SHIFT;
1345
1346 if (agg_bufs) {
1347 if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1348 return -EBUSY;
1349
1350 cp_cons = NEXT_CMP(cp_cons);
1351 *agg_event = true;
1352 }
1353
1354 rx_buf->data = NULL;
1355 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
1356 bnxt_reuse_rx_data(rxr, cons, data);
1357 if (agg_bufs)
1358 bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
1359
1360 rc = -EIO;
1361 goto next_rx;
1362 }
1363
1364 len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
1365 dma_addr = dma_unmap_addr(rx_buf, mapping);
1366
1367 if (len <= bp->rx_copy_thresh) {
1368 skb = bnxt_copy_skb(bnapi, data, len, dma_addr);
1369 bnxt_reuse_rx_data(rxr, cons, data);
1370 if (!skb) {
1371 rc = -ENOMEM;
1372 goto next_rx;
1373 }
1374 } else {
1375 skb = bnxt_rx_skb(bp, rxr, cons, prod, data, dma_addr, len);
1376 if (!skb) {
1377 rc = -ENOMEM;
1378 goto next_rx;
1379 }
1380 }
1381
1382 if (agg_bufs) {
1383 skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
1384 if (!skb) {
1385 rc = -ENOMEM;
1386 goto next_rx;
1387 }
1388 }
1389
1390 if (RX_CMP_HASH_VALID(rxcmp)) {
1391 u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
1392 enum pkt_hash_types type = PKT_HASH_TYPE_L4;
1393
1394 /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1395 if (hash_type != 1 && hash_type != 3)
1396 type = PKT_HASH_TYPE_L3;
1397 skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
1398 }
1399
1400 skb->protocol = eth_type_trans(skb, dev);
1401
Michael Chan8852ddb2016-06-06 02:37:16 -04001402 if ((rxcmp1->rx_cmp_flags2 &
1403 cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
1404 (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04001405 u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
Michael Chan8852ddb2016-06-06 02:37:16 -04001406 u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04001407 u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
1408
Michael Chan8852ddb2016-06-06 02:37:16 -04001409 __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
Michael Chanc0c050c2015-10-22 16:01:17 -04001410 }
1411
1412 skb_checksum_none_assert(skb);
1413 if (RX_CMP_L4_CS_OK(rxcmp1)) {
1414 if (dev->features & NETIF_F_RXCSUM) {
1415 skb->ip_summed = CHECKSUM_UNNECESSARY;
1416 skb->csum_level = RX_CMP_ENCAP(rxcmp1);
1417 }
1418 } else {
Satish Baddipadige665e3502015-12-27 18:19:21 -05001419 if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
1420 if (dev->features & NETIF_F_RXCSUM)
1421 cpr->rx_l4_csum_errors++;
1422 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001423 }
1424
1425 skb_record_rx_queue(skb, bnapi->index);
1426 skb_mark_napi_id(skb, &bnapi->napi);
1427 if (bnxt_busy_polling(bnapi))
1428 netif_receive_skb(skb);
1429 else
1430 napi_gro_receive(&bnapi->napi, skb);
1431 rc = 1;
1432
1433next_rx:
1434 rxr->rx_prod = NEXT_RX(prod);
Michael Chan376a5b82016-05-10 19:17:59 -04001435 rxr->rx_next_cons = NEXT_RX(cons);
Michael Chanc0c050c2015-10-22 16:01:17 -04001436
1437next_rx_no_prod:
1438 *raw_cons = tmp_raw_cons;
1439
1440 return rc;
1441}
1442
Michael Chan4bb13ab2016-04-05 14:09:01 -04001443#define BNXT_GET_EVENT_PORT(data) \
1444 ((data) & \
1445 HWRM_ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
1446
Michael Chanc0c050c2015-10-22 16:01:17 -04001447static int bnxt_async_event_process(struct bnxt *bp,
1448 struct hwrm_async_event_cmpl *cmpl)
1449{
1450 u16 event_id = le16_to_cpu(cmpl->event_id);
1451
1452 /* TODO CHIMP_FW: Define event id's for link change, error etc */
1453 switch (event_id) {
Michael Chan8cbde112016-04-11 04:11:14 -04001454 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
1455 u32 data1 = le32_to_cpu(cmpl->event_data1);
1456 struct bnxt_link_info *link_info = &bp->link_info;
1457
1458 if (BNXT_VF(bp))
1459 goto async_event_process_exit;
1460 if (data1 & 0x20000) {
1461 u16 fw_speed = link_info->force_link_speed;
1462 u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
1463
1464 netdev_warn(bp->dev, "Link speed %d no longer supported\n",
1465 speed);
1466 }
1467 /* fall thru */
1468 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001469 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
1470 set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
Jeffrey Huang19241362016-02-26 04:00:00 -05001471 break;
1472 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
1473 set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04001474 break;
Michael Chan4bb13ab2016-04-05 14:09:01 -04001475 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
1476 u32 data1 = le32_to_cpu(cmpl->event_data1);
1477 u16 port_id = BNXT_GET_EVENT_PORT(data1);
1478
1479 if (BNXT_VF(bp))
1480 break;
1481
1482 if (bp->pf.port_id != port_id)
1483 break;
1484
Michael Chan4bb13ab2016-04-05 14:09:01 -04001485 set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
1486 break;
1487 }
Michael Chanfc0f1922016-06-13 02:25:30 -04001488 case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
1489 if (BNXT_PF(bp))
1490 goto async_event_process_exit;
1491 set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
1492 break;
Michael Chanc0c050c2015-10-22 16:01:17 -04001493 default:
1494 netdev_err(bp->dev, "unhandled ASYNC event (id 0x%x)\n",
1495 event_id);
Jeffrey Huang19241362016-02-26 04:00:00 -05001496 goto async_event_process_exit;
Michael Chanc0c050c2015-10-22 16:01:17 -04001497 }
Jeffrey Huang19241362016-02-26 04:00:00 -05001498 schedule_work(&bp->sp_task);
1499async_event_process_exit:
Michael Chanc0c050c2015-10-22 16:01:17 -04001500 return 0;
1501}
1502
1503static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
1504{
1505 u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
1506 struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
1507 struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
1508 (struct hwrm_fwd_req_cmpl *)txcmp;
1509
1510 switch (cmpl_type) {
1511 case CMPL_BASE_TYPE_HWRM_DONE:
1512 seq_id = le16_to_cpu(h_cmpl->sequence_id);
1513 if (seq_id == bp->hwrm_intr_seq_id)
1514 bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
1515 else
1516 netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
1517 break;
1518
1519 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
1520 vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
1521
1522 if ((vf_id < bp->pf.first_vf_id) ||
1523 (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
1524 netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
1525 vf_id);
1526 return -EINVAL;
1527 }
1528
1529 set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
1530 set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
1531 schedule_work(&bp->sp_task);
1532 break;
1533
1534 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
1535 bnxt_async_event_process(bp,
1536 (struct hwrm_async_event_cmpl *)txcmp);
1537
1538 default:
1539 break;
1540 }
1541
1542 return 0;
1543}
1544
1545static irqreturn_t bnxt_msix(int irq, void *dev_instance)
1546{
1547 struct bnxt_napi *bnapi = dev_instance;
1548 struct bnxt *bp = bnapi->bp;
1549 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1550 u32 cons = RING_CMP(cpr->cp_raw_cons);
1551
1552 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1553 napi_schedule(&bnapi->napi);
1554 return IRQ_HANDLED;
1555}
1556
1557static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
1558{
1559 u32 raw_cons = cpr->cp_raw_cons;
1560 u16 cons = RING_CMP(raw_cons);
1561 struct tx_cmp *txcmp;
1562
1563 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1564
1565 return TX_CMP_VALID(txcmp, raw_cons);
1566}
1567
Michael Chanc0c050c2015-10-22 16:01:17 -04001568static irqreturn_t bnxt_inta(int irq, void *dev_instance)
1569{
1570 struct bnxt_napi *bnapi = dev_instance;
1571 struct bnxt *bp = bnapi->bp;
1572 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1573 u32 cons = RING_CMP(cpr->cp_raw_cons);
1574 u32 int_status;
1575
1576 prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
1577
1578 if (!bnxt_has_work(bp, cpr)) {
Jeffrey Huang11809492015-11-05 16:25:49 -05001579 int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
Michael Chanc0c050c2015-10-22 16:01:17 -04001580 /* return if erroneous interrupt */
1581 if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
1582 return IRQ_NONE;
1583 }
1584
1585 /* disable ring IRQ */
1586 BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
1587
1588 /* Return here if interrupt is shared and is disabled. */
1589 if (unlikely(atomic_read(&bp->intr_sem) != 0))
1590 return IRQ_HANDLED;
1591
1592 napi_schedule(&bnapi->napi);
1593 return IRQ_HANDLED;
1594}
1595
1596static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
1597{
1598 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1599 u32 raw_cons = cpr->cp_raw_cons;
1600 u32 cons;
1601 int tx_pkts = 0;
1602 int rx_pkts = 0;
1603 bool rx_event = false;
1604 bool agg_event = false;
1605 struct tx_cmp *txcmp;
1606
1607 while (1) {
1608 int rc;
1609
1610 cons = RING_CMP(raw_cons);
1611 txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
1612
1613 if (!TX_CMP_VALID(txcmp, raw_cons))
1614 break;
1615
Michael Chan67a95e22016-05-04 16:56:43 -04001616 /* The valid test of the entry must be done first before
1617 * reading any further.
1618 */
Michael Chanb67daab2016-05-15 03:04:51 -04001619 dma_rmb();
Michael Chanc0c050c2015-10-22 16:01:17 -04001620 if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
1621 tx_pkts++;
1622 /* return full budget so NAPI will complete. */
1623 if (unlikely(tx_pkts > bp->tx_wake_thresh))
1624 rx_pkts = budget;
1625 } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1626 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1627 if (likely(rc >= 0))
1628 rx_pkts += rc;
1629 else if (rc == -EBUSY) /* partial completion */
1630 break;
1631 rx_event = true;
1632 } else if (unlikely((TX_CMP_TYPE(txcmp) ==
1633 CMPL_BASE_TYPE_HWRM_DONE) ||
1634 (TX_CMP_TYPE(txcmp) ==
1635 CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
1636 (TX_CMP_TYPE(txcmp) ==
1637 CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
1638 bnxt_hwrm_handler(bp, txcmp);
1639 }
1640 raw_cons = NEXT_RAW_CMP(raw_cons);
1641
1642 if (rx_pkts == budget)
1643 break;
1644 }
1645
1646 cpr->cp_raw_cons = raw_cons;
1647 /* ACK completion ring before freeing tx ring and producing new
1648 * buffers in rx/agg rings to prevent overflowing the completion
1649 * ring.
1650 */
1651 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1652
1653 if (tx_pkts)
1654 bnxt_tx_int(bp, bnapi, tx_pkts);
1655
1656 if (rx_event) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001657 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chanc0c050c2015-10-22 16:01:17 -04001658
1659 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1660 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1661 if (agg_event) {
1662 writel(DB_KEY_RX | rxr->rx_agg_prod,
1663 rxr->rx_agg_doorbell);
1664 writel(DB_KEY_RX | rxr->rx_agg_prod,
1665 rxr->rx_agg_doorbell);
1666 }
1667 }
1668 return rx_pkts;
1669}
1670
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04001671static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
1672{
1673 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1674 struct bnxt *bp = bnapi->bp;
1675 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1676 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1677 struct tx_cmp *txcmp;
1678 struct rx_cmp_ext *rxcmp1;
1679 u32 cp_cons, tmp_raw_cons;
1680 u32 raw_cons = cpr->cp_raw_cons;
1681 u32 rx_pkts = 0;
1682 bool agg_event = false;
1683
1684 while (1) {
1685 int rc;
1686
1687 cp_cons = RING_CMP(raw_cons);
1688 txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1689
1690 if (!TX_CMP_VALID(txcmp, raw_cons))
1691 break;
1692
1693 if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
1694 tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
1695 cp_cons = RING_CMP(tmp_raw_cons);
1696 rxcmp1 = (struct rx_cmp_ext *)
1697 &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1698
1699 if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
1700 break;
1701
1702 /* force an error to recycle the buffer */
1703 rxcmp1->rx_cmp_cfa_code_errors_v2 |=
1704 cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
1705
1706 rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
1707 if (likely(rc == -EIO))
1708 rx_pkts++;
1709 else if (rc == -EBUSY) /* partial completion */
1710 break;
1711 } else if (unlikely(TX_CMP_TYPE(txcmp) ==
1712 CMPL_BASE_TYPE_HWRM_DONE)) {
1713 bnxt_hwrm_handler(bp, txcmp);
1714 } else {
1715 netdev_err(bp->dev,
1716 "Invalid completion received on special ring\n");
1717 }
1718 raw_cons = NEXT_RAW_CMP(raw_cons);
1719
1720 if (rx_pkts == budget)
1721 break;
1722 }
1723
1724 cpr->cp_raw_cons = raw_cons;
1725 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
1726 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1727 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
1728
1729 if (agg_event) {
1730 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1731 writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
1732 }
1733
1734 if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
1735 napi_complete(napi);
1736 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1737 }
1738 return rx_pkts;
1739}
1740
Michael Chanc0c050c2015-10-22 16:01:17 -04001741static int bnxt_poll(struct napi_struct *napi, int budget)
1742{
1743 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1744 struct bnxt *bp = bnapi->bp;
1745 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1746 int work_done = 0;
1747
1748 if (!bnxt_lock_napi(bnapi))
1749 return budget;
1750
1751 while (1) {
1752 work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
1753
1754 if (work_done >= budget)
1755 break;
1756
1757 if (!bnxt_has_work(bp, cpr)) {
1758 napi_complete(napi);
1759 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1760 break;
1761 }
1762 }
1763 mmiowb();
1764 bnxt_unlock_napi(bnapi);
1765 return work_done;
1766}
1767
1768#ifdef CONFIG_NET_RX_BUSY_POLL
1769static int bnxt_busy_poll(struct napi_struct *napi)
1770{
1771 struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
1772 struct bnxt *bp = bnapi->bp;
1773 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
1774 int rx_work, budget = 4;
1775
1776 if (atomic_read(&bp->intr_sem) != 0)
1777 return LL_FLUSH_FAILED;
1778
1779 if (!bnxt_lock_poll(bnapi))
1780 return LL_FLUSH_BUSY;
1781
1782 rx_work = bnxt_poll_work(bp, bnapi, budget);
1783
1784 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
1785
1786 bnxt_unlock_poll(bnapi);
1787 return rx_work;
1788}
1789#endif
1790
1791static void bnxt_free_tx_skbs(struct bnxt *bp)
1792{
1793 int i, max_idx;
1794 struct pci_dev *pdev = bp->pdev;
1795
Michael Chanb6ab4b02016-01-02 23:44:59 -05001796 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001797 return;
1798
1799 max_idx = bp->tx_nr_pages * TX_DESC_CNT;
1800 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001801 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001802 int j;
1803
Michael Chanc0c050c2015-10-22 16:01:17 -04001804 for (j = 0; j < max_idx;) {
1805 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
1806 struct sk_buff *skb = tx_buf->skb;
1807 int k, last;
1808
1809 if (!skb) {
1810 j++;
1811 continue;
1812 }
1813
1814 tx_buf->skb = NULL;
1815
1816 if (tx_buf->is_push) {
1817 dev_kfree_skb(skb);
1818 j += 2;
1819 continue;
1820 }
1821
1822 dma_unmap_single(&pdev->dev,
1823 dma_unmap_addr(tx_buf, mapping),
1824 skb_headlen(skb),
1825 PCI_DMA_TODEVICE);
1826
1827 last = tx_buf->nr_frags;
1828 j += 2;
Michael Chand612a572016-01-28 03:11:22 -05001829 for (k = 0; k < last; k++, j++) {
1830 int ring_idx = j & bp->tx_ring_mask;
Michael Chanc0c050c2015-10-22 16:01:17 -04001831 skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
1832
Michael Chand612a572016-01-28 03:11:22 -05001833 tx_buf = &txr->tx_buf_ring[ring_idx];
Michael Chanc0c050c2015-10-22 16:01:17 -04001834 dma_unmap_page(
1835 &pdev->dev,
1836 dma_unmap_addr(tx_buf, mapping),
1837 skb_frag_size(frag), PCI_DMA_TODEVICE);
1838 }
1839 dev_kfree_skb(skb);
1840 }
1841 netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
1842 }
1843}
1844
1845static void bnxt_free_rx_skbs(struct bnxt *bp)
1846{
1847 int i, max_idx, max_agg_idx;
1848 struct pci_dev *pdev = bp->pdev;
1849
Michael Chanb6ab4b02016-01-02 23:44:59 -05001850 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001851 return;
1852
1853 max_idx = bp->rx_nr_pages * RX_DESC_CNT;
1854 max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
1855 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001856 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001857 int j;
1858
Michael Chanc0c050c2015-10-22 16:01:17 -04001859 if (rxr->rx_tpa) {
1860 for (j = 0; j < MAX_TPA; j++) {
1861 struct bnxt_tpa_info *tpa_info =
1862 &rxr->rx_tpa[j];
1863 u8 *data = tpa_info->data;
1864
1865 if (!data)
1866 continue;
1867
1868 dma_unmap_single(
1869 &pdev->dev,
1870 dma_unmap_addr(tpa_info, mapping),
1871 bp->rx_buf_use_size,
1872 PCI_DMA_FROMDEVICE);
1873
1874 tpa_info->data = NULL;
1875
1876 kfree(data);
1877 }
1878 }
1879
1880 for (j = 0; j < max_idx; j++) {
1881 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
1882 u8 *data = rx_buf->data;
1883
1884 if (!data)
1885 continue;
1886
1887 dma_unmap_single(&pdev->dev,
1888 dma_unmap_addr(rx_buf, mapping),
1889 bp->rx_buf_use_size,
1890 PCI_DMA_FROMDEVICE);
1891
1892 rx_buf->data = NULL;
1893
1894 kfree(data);
1895 }
1896
1897 for (j = 0; j < max_agg_idx; j++) {
1898 struct bnxt_sw_rx_agg_bd *rx_agg_buf =
1899 &rxr->rx_agg_ring[j];
1900 struct page *page = rx_agg_buf->page;
1901
1902 if (!page)
1903 continue;
1904
1905 dma_unmap_page(&pdev->dev,
1906 dma_unmap_addr(rx_agg_buf, mapping),
Michael Chan2839f282016-04-25 02:30:50 -04001907 BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE);
Michael Chanc0c050c2015-10-22 16:01:17 -04001908
1909 rx_agg_buf->page = NULL;
1910 __clear_bit(j, rxr->rx_agg_bmap);
1911
1912 __free_page(page);
1913 }
Michael Chan89d0a062016-04-25 02:30:51 -04001914 if (rxr->rx_page) {
1915 __free_page(rxr->rx_page);
1916 rxr->rx_page = NULL;
1917 }
Michael Chanc0c050c2015-10-22 16:01:17 -04001918 }
1919}
1920
1921static void bnxt_free_skbs(struct bnxt *bp)
1922{
1923 bnxt_free_tx_skbs(bp);
1924 bnxt_free_rx_skbs(bp);
1925}
1926
1927static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1928{
1929 struct pci_dev *pdev = bp->pdev;
1930 int i;
1931
1932 for (i = 0; i < ring->nr_pages; i++) {
1933 if (!ring->pg_arr[i])
1934 continue;
1935
1936 dma_free_coherent(&pdev->dev, ring->page_size,
1937 ring->pg_arr[i], ring->dma_arr[i]);
1938
1939 ring->pg_arr[i] = NULL;
1940 }
1941 if (ring->pg_tbl) {
1942 dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
1943 ring->pg_tbl, ring->pg_tbl_map);
1944 ring->pg_tbl = NULL;
1945 }
1946 if (ring->vmem_size && *ring->vmem) {
1947 vfree(*ring->vmem);
1948 *ring->vmem = NULL;
1949 }
1950}
1951
1952static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
1953{
1954 int i;
1955 struct pci_dev *pdev = bp->pdev;
1956
1957 if (ring->nr_pages > 1) {
1958 ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
1959 ring->nr_pages * 8,
1960 &ring->pg_tbl_map,
1961 GFP_KERNEL);
1962 if (!ring->pg_tbl)
1963 return -ENOMEM;
1964 }
1965
1966 for (i = 0; i < ring->nr_pages; i++) {
1967 ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
1968 ring->page_size,
1969 &ring->dma_arr[i],
1970 GFP_KERNEL);
1971 if (!ring->pg_arr[i])
1972 return -ENOMEM;
1973
1974 if (ring->nr_pages > 1)
1975 ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
1976 }
1977
1978 if (ring->vmem_size) {
1979 *ring->vmem = vzalloc(ring->vmem_size);
1980 if (!(*ring->vmem))
1981 return -ENOMEM;
1982 }
1983 return 0;
1984}
1985
1986static void bnxt_free_rx_rings(struct bnxt *bp)
1987{
1988 int i;
1989
Michael Chanb6ab4b02016-01-02 23:44:59 -05001990 if (!bp->rx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04001991 return;
1992
1993 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05001994 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04001995 struct bnxt_ring_struct *ring;
1996
Michael Chanc0c050c2015-10-22 16:01:17 -04001997 kfree(rxr->rx_tpa);
1998 rxr->rx_tpa = NULL;
1999
2000 kfree(rxr->rx_agg_bmap);
2001 rxr->rx_agg_bmap = NULL;
2002
2003 ring = &rxr->rx_ring_struct;
2004 bnxt_free_ring(bp, ring);
2005
2006 ring = &rxr->rx_agg_ring_struct;
2007 bnxt_free_ring(bp, ring);
2008 }
2009}
2010
2011static int bnxt_alloc_rx_rings(struct bnxt *bp)
2012{
2013 int i, rc, agg_rings = 0, tpa_rings = 0;
2014
Michael Chanb6ab4b02016-01-02 23:44:59 -05002015 if (!bp->rx_ring)
2016 return -ENOMEM;
2017
Michael Chanc0c050c2015-10-22 16:01:17 -04002018 if (bp->flags & BNXT_FLAG_AGG_RINGS)
2019 agg_rings = 1;
2020
2021 if (bp->flags & BNXT_FLAG_TPA)
2022 tpa_rings = 1;
2023
2024 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002025 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002026 struct bnxt_ring_struct *ring;
2027
Michael Chanc0c050c2015-10-22 16:01:17 -04002028 ring = &rxr->rx_ring_struct;
2029
2030 rc = bnxt_alloc_ring(bp, ring);
2031 if (rc)
2032 return rc;
2033
2034 if (agg_rings) {
2035 u16 mem_size;
2036
2037 ring = &rxr->rx_agg_ring_struct;
2038 rc = bnxt_alloc_ring(bp, ring);
2039 if (rc)
2040 return rc;
2041
2042 rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
2043 mem_size = rxr->rx_agg_bmap_size / 8;
2044 rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
2045 if (!rxr->rx_agg_bmap)
2046 return -ENOMEM;
2047
2048 if (tpa_rings) {
2049 rxr->rx_tpa = kcalloc(MAX_TPA,
2050 sizeof(struct bnxt_tpa_info),
2051 GFP_KERNEL);
2052 if (!rxr->rx_tpa)
2053 return -ENOMEM;
2054 }
2055 }
2056 }
2057 return 0;
2058}
2059
2060static void bnxt_free_tx_rings(struct bnxt *bp)
2061{
2062 int i;
2063 struct pci_dev *pdev = bp->pdev;
2064
Michael Chanb6ab4b02016-01-02 23:44:59 -05002065 if (!bp->tx_ring)
Michael Chanc0c050c2015-10-22 16:01:17 -04002066 return;
2067
2068 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002069 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002070 struct bnxt_ring_struct *ring;
2071
Michael Chanc0c050c2015-10-22 16:01:17 -04002072 if (txr->tx_push) {
2073 dma_free_coherent(&pdev->dev, bp->tx_push_size,
2074 txr->tx_push, txr->tx_push_mapping);
2075 txr->tx_push = NULL;
2076 }
2077
2078 ring = &txr->tx_ring_struct;
2079
2080 bnxt_free_ring(bp, ring);
2081 }
2082}
2083
2084static int bnxt_alloc_tx_rings(struct bnxt *bp)
2085{
2086 int i, j, rc;
2087 struct pci_dev *pdev = bp->pdev;
2088
2089 bp->tx_push_size = 0;
2090 if (bp->tx_push_thresh) {
2091 int push_size;
2092
2093 push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
2094 bp->tx_push_thresh);
2095
Michael Chan4419dbe2016-02-10 17:33:49 -05002096 if (push_size > 256) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002097 push_size = 0;
2098 bp->tx_push_thresh = 0;
2099 }
2100
2101 bp->tx_push_size = push_size;
2102 }
2103
2104 for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002105 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002106 struct bnxt_ring_struct *ring;
2107
Michael Chanc0c050c2015-10-22 16:01:17 -04002108 ring = &txr->tx_ring_struct;
2109
2110 rc = bnxt_alloc_ring(bp, ring);
2111 if (rc)
2112 return rc;
2113
2114 if (bp->tx_push_size) {
Michael Chanc0c050c2015-10-22 16:01:17 -04002115 dma_addr_t mapping;
2116
2117 /* One pre-allocated DMA buffer to backup
2118 * TX push operation
2119 */
2120 txr->tx_push = dma_alloc_coherent(&pdev->dev,
2121 bp->tx_push_size,
2122 &txr->tx_push_mapping,
2123 GFP_KERNEL);
2124
2125 if (!txr->tx_push)
2126 return -ENOMEM;
2127
Michael Chanc0c050c2015-10-22 16:01:17 -04002128 mapping = txr->tx_push_mapping +
2129 sizeof(struct tx_push_bd);
Michael Chan4419dbe2016-02-10 17:33:49 -05002130 txr->data_mapping = cpu_to_le64(mapping);
Michael Chanc0c050c2015-10-22 16:01:17 -04002131
Michael Chan4419dbe2016-02-10 17:33:49 -05002132 memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
Michael Chanc0c050c2015-10-22 16:01:17 -04002133 }
2134 ring->queue_id = bp->q_info[j].queue_id;
2135 if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
2136 j++;
2137 }
2138 return 0;
2139}
2140
2141static void bnxt_free_cp_rings(struct bnxt *bp)
2142{
2143 int i;
2144
2145 if (!bp->bnapi)
2146 return;
2147
2148 for (i = 0; i < bp->cp_nr_rings; i++) {
2149 struct bnxt_napi *bnapi = bp->bnapi[i];
2150 struct bnxt_cp_ring_info *cpr;
2151 struct bnxt_ring_struct *ring;
2152
2153 if (!bnapi)
2154 continue;
2155
2156 cpr = &bnapi->cp_ring;
2157 ring = &cpr->cp_ring_struct;
2158
2159 bnxt_free_ring(bp, ring);
2160 }
2161}
2162
2163static int bnxt_alloc_cp_rings(struct bnxt *bp)
2164{
2165 int i, rc;
2166
2167 for (i = 0; i < bp->cp_nr_rings; i++) {
2168 struct bnxt_napi *bnapi = bp->bnapi[i];
2169 struct bnxt_cp_ring_info *cpr;
2170 struct bnxt_ring_struct *ring;
2171
2172 if (!bnapi)
2173 continue;
2174
2175 cpr = &bnapi->cp_ring;
2176 ring = &cpr->cp_ring_struct;
2177
2178 rc = bnxt_alloc_ring(bp, ring);
2179 if (rc)
2180 return rc;
2181 }
2182 return 0;
2183}
2184
2185static void bnxt_init_ring_struct(struct bnxt *bp)
2186{
2187 int i;
2188
2189 for (i = 0; i < bp->cp_nr_rings; i++) {
2190 struct bnxt_napi *bnapi = bp->bnapi[i];
2191 struct bnxt_cp_ring_info *cpr;
2192 struct bnxt_rx_ring_info *rxr;
2193 struct bnxt_tx_ring_info *txr;
2194 struct bnxt_ring_struct *ring;
2195
2196 if (!bnapi)
2197 continue;
2198
2199 cpr = &bnapi->cp_ring;
2200 ring = &cpr->cp_ring_struct;
2201 ring->nr_pages = bp->cp_nr_pages;
2202 ring->page_size = HW_CMPD_RING_SIZE;
2203 ring->pg_arr = (void **)cpr->cp_desc_ring;
2204 ring->dma_arr = cpr->cp_desc_mapping;
2205 ring->vmem_size = 0;
2206
Michael Chanb6ab4b02016-01-02 23:44:59 -05002207 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002208 if (!rxr)
2209 goto skip_rx;
2210
Michael Chanc0c050c2015-10-22 16:01:17 -04002211 ring = &rxr->rx_ring_struct;
2212 ring->nr_pages = bp->rx_nr_pages;
2213 ring->page_size = HW_RXBD_RING_SIZE;
2214 ring->pg_arr = (void **)rxr->rx_desc_ring;
2215 ring->dma_arr = rxr->rx_desc_mapping;
2216 ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
2217 ring->vmem = (void **)&rxr->rx_buf_ring;
2218
2219 ring = &rxr->rx_agg_ring_struct;
2220 ring->nr_pages = bp->rx_agg_nr_pages;
2221 ring->page_size = HW_RXBD_RING_SIZE;
2222 ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
2223 ring->dma_arr = rxr->rx_agg_desc_mapping;
2224 ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
2225 ring->vmem = (void **)&rxr->rx_agg_ring;
2226
Michael Chan3b2b7d92016-01-02 23:45:00 -05002227skip_rx:
Michael Chanb6ab4b02016-01-02 23:44:59 -05002228 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002229 if (!txr)
2230 continue;
2231
Michael Chanc0c050c2015-10-22 16:01:17 -04002232 ring = &txr->tx_ring_struct;
2233 ring->nr_pages = bp->tx_nr_pages;
2234 ring->page_size = HW_RXBD_RING_SIZE;
2235 ring->pg_arr = (void **)txr->tx_desc_ring;
2236 ring->dma_arr = txr->tx_desc_mapping;
2237 ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
2238 ring->vmem = (void **)&txr->tx_buf_ring;
2239 }
2240}
2241
2242static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
2243{
2244 int i;
2245 u32 prod;
2246 struct rx_bd **rx_buf_ring;
2247
2248 rx_buf_ring = (struct rx_bd **)ring->pg_arr;
2249 for (i = 0, prod = 0; i < ring->nr_pages; i++) {
2250 int j;
2251 struct rx_bd *rxbd;
2252
2253 rxbd = rx_buf_ring[i];
2254 if (!rxbd)
2255 continue;
2256
2257 for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
2258 rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
2259 rxbd->rx_bd_opaque = prod;
2260 }
2261 }
2262}
2263
2264static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
2265{
2266 struct net_device *dev = bp->dev;
Michael Chanc0c050c2015-10-22 16:01:17 -04002267 struct bnxt_rx_ring_info *rxr;
2268 struct bnxt_ring_struct *ring;
2269 u32 prod, type;
2270 int i;
2271
Michael Chanc0c050c2015-10-22 16:01:17 -04002272 type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
2273 RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
2274
2275 if (NET_IP_ALIGN == 2)
2276 type |= RX_BD_FLAGS_SOP;
2277
Michael Chanb6ab4b02016-01-02 23:44:59 -05002278 rxr = &bp->rx_ring[ring_nr];
Michael Chanc0c050c2015-10-22 16:01:17 -04002279 ring = &rxr->rx_ring_struct;
2280 bnxt_init_rxbd_pages(ring, type);
2281
2282 prod = rxr->rx_prod;
2283 for (i = 0; i < bp->rx_ring_size; i++) {
2284 if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
2285 netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
2286 ring_nr, i, bp->rx_ring_size);
2287 break;
2288 }
2289 prod = NEXT_RX(prod);
2290 }
2291 rxr->rx_prod = prod;
2292 ring->fw_ring_id = INVALID_HW_RING_ID;
2293
Michael Chanedd0c2c2015-12-27 18:19:19 -05002294 ring = &rxr->rx_agg_ring_struct;
2295 ring->fw_ring_id = INVALID_HW_RING_ID;
2296
Michael Chanc0c050c2015-10-22 16:01:17 -04002297 if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
2298 return 0;
2299
Michael Chan2839f282016-04-25 02:30:50 -04002300 type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
Michael Chanc0c050c2015-10-22 16:01:17 -04002301 RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
2302
2303 bnxt_init_rxbd_pages(ring, type);
2304
2305 prod = rxr->rx_agg_prod;
2306 for (i = 0; i < bp->rx_agg_ring_size; i++) {
2307 if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
2308 netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
2309 ring_nr, i, bp->rx_ring_size);
2310 break;
2311 }
2312 prod = NEXT_RX_AGG(prod);
2313 }
2314 rxr->rx_agg_prod = prod;
Michael Chanc0c050c2015-10-22 16:01:17 -04002315
2316 if (bp->flags & BNXT_FLAG_TPA) {
2317 if (rxr->rx_tpa) {
2318 u8 *data;
2319 dma_addr_t mapping;
2320
2321 for (i = 0; i < MAX_TPA; i++) {
2322 data = __bnxt_alloc_rx_data(bp, &mapping,
2323 GFP_KERNEL);
2324 if (!data)
2325 return -ENOMEM;
2326
2327 rxr->rx_tpa[i].data = data;
2328 rxr->rx_tpa[i].mapping = mapping;
2329 }
2330 } else {
2331 netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
2332 return -ENOMEM;
2333 }
2334 }
2335
2336 return 0;
2337}
2338
2339static int bnxt_init_rx_rings(struct bnxt *bp)
2340{
2341 int i, rc = 0;
2342
2343 for (i = 0; i < bp->rx_nr_rings; i++) {
2344 rc = bnxt_init_one_rx_ring(bp, i);
2345 if (rc)
2346 break;
2347 }
2348
2349 return rc;
2350}
2351
2352static int bnxt_init_tx_rings(struct bnxt *bp)
2353{
2354 u16 i;
2355
2356 bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
2357 MAX_SKB_FRAGS + 1);
2358
2359 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05002360 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04002361 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
2362
2363 ring->fw_ring_id = INVALID_HW_RING_ID;
2364 }
2365
2366 return 0;
2367}
2368
2369static void bnxt_free_ring_grps(struct bnxt *bp)
2370{
2371 kfree(bp->grp_info);
2372 bp->grp_info = NULL;
2373}
2374
2375static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
2376{
2377 int i;
2378
2379 if (irq_re_init) {
2380 bp->grp_info = kcalloc(bp->cp_nr_rings,
2381 sizeof(struct bnxt_ring_grp_info),
2382 GFP_KERNEL);
2383 if (!bp->grp_info)
2384 return -ENOMEM;
2385 }
2386 for (i = 0; i < bp->cp_nr_rings; i++) {
2387 if (irq_re_init)
2388 bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
2389 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
2390 bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
2391 bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
2392 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
2393 }
2394 return 0;
2395}
2396
2397static void bnxt_free_vnics(struct bnxt *bp)
2398{
2399 kfree(bp->vnic_info);
2400 bp->vnic_info = NULL;
2401 bp->nr_vnics = 0;
2402}
2403
2404static int bnxt_alloc_vnics(struct bnxt *bp)
2405{
2406 int num_vnics = 1;
2407
2408#ifdef CONFIG_RFS_ACCEL
2409 if (bp->flags & BNXT_FLAG_RFS)
2410 num_vnics += bp->rx_nr_rings;
2411#endif
2412
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04002413 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
2414 num_vnics++;
2415
Michael Chanc0c050c2015-10-22 16:01:17 -04002416 bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
2417 GFP_KERNEL);
2418 if (!bp->vnic_info)
2419 return -ENOMEM;
2420
2421 bp->nr_vnics = num_vnics;
2422 return 0;
2423}
2424
2425static void bnxt_init_vnics(struct bnxt *bp)
2426{
2427 int i;
2428
2429 for (i = 0; i < bp->nr_vnics; i++) {
2430 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2431
2432 vnic->fw_vnic_id = INVALID_HW_RING_ID;
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04002433 vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
2434 vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04002435 vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
2436
2437 if (bp->vnic_info[i].rss_hash_key) {
2438 if (i == 0)
2439 prandom_bytes(vnic->rss_hash_key,
2440 HW_HASH_KEY_SIZE);
2441 else
2442 memcpy(vnic->rss_hash_key,
2443 bp->vnic_info[0].rss_hash_key,
2444 HW_HASH_KEY_SIZE);
2445 }
2446 }
2447}
2448
2449static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
2450{
2451 int pages;
2452
2453 pages = ring_size / desc_per_pg;
2454
2455 if (!pages)
2456 return 1;
2457
2458 pages++;
2459
2460 while (pages & (pages - 1))
2461 pages++;
2462
2463 return pages;
2464}
2465
2466static void bnxt_set_tpa_flags(struct bnxt *bp)
2467{
2468 bp->flags &= ~BNXT_FLAG_TPA;
2469 if (bp->dev->features & NETIF_F_LRO)
2470 bp->flags |= BNXT_FLAG_LRO;
Michael Chan94758f82016-06-13 02:25:35 -04002471 if (bp->dev->features & NETIF_F_GRO)
Michael Chanc0c050c2015-10-22 16:01:17 -04002472 bp->flags |= BNXT_FLAG_GRO;
2473}
2474
2475/* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
2476 * be set on entry.
2477 */
2478void bnxt_set_ring_params(struct bnxt *bp)
2479{
2480 u32 ring_size, rx_size, rx_space;
2481 u32 agg_factor = 0, agg_ring_size = 0;
2482
2483 /* 8 for CRC and VLAN */
2484 rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
2485
2486 rx_space = rx_size + NET_SKB_PAD +
2487 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2488
2489 bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
2490 ring_size = bp->rx_ring_size;
2491 bp->rx_agg_ring_size = 0;
2492 bp->rx_agg_nr_pages = 0;
2493
2494 if (bp->flags & BNXT_FLAG_TPA)
Michael Chan2839f282016-04-25 02:30:50 -04002495 agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
Michael Chanc0c050c2015-10-22 16:01:17 -04002496
2497 bp->flags &= ~BNXT_FLAG_JUMBO;
2498 if (rx_space > PAGE_SIZE) {
2499 u32 jumbo_factor;
2500
2501 bp->flags |= BNXT_FLAG_JUMBO;
2502 jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
2503 if (jumbo_factor > agg_factor)
2504 agg_factor = jumbo_factor;
2505 }
2506 agg_ring_size = ring_size * agg_factor;
2507
2508 if (agg_ring_size) {
2509 bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
2510 RX_DESC_CNT);
2511 if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
2512 u32 tmp = agg_ring_size;
2513
2514 bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
2515 agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
2516 netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
2517 tmp, agg_ring_size);
2518 }
2519 bp->rx_agg_ring_size = agg_ring_size;
2520 bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
2521 rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
2522 rx_space = rx_size + NET_SKB_PAD +
2523 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2524 }
2525
2526 bp->rx_buf_use_size = rx_size;
2527 bp->rx_buf_size = rx_space;
2528
2529 bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
2530 bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
2531
2532 ring_size = bp->tx_ring_size;
2533 bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
2534 bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
2535
2536 ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
2537 bp->cp_ring_size = ring_size;
2538
2539 bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
2540 if (bp->cp_nr_pages > MAX_CP_PAGES) {
2541 bp->cp_nr_pages = MAX_CP_PAGES;
2542 bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
2543 netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
2544 ring_size, bp->cp_ring_size);
2545 }
2546 bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
2547 bp->cp_ring_mask = bp->cp_bit - 1;
2548}
2549
2550static void bnxt_free_vnic_attributes(struct bnxt *bp)
2551{
2552 int i;
2553 struct bnxt_vnic_info *vnic;
2554 struct pci_dev *pdev = bp->pdev;
2555
2556 if (!bp->vnic_info)
2557 return;
2558
2559 for (i = 0; i < bp->nr_vnics; i++) {
2560 vnic = &bp->vnic_info[i];
2561
2562 kfree(vnic->fw_grp_ids);
2563 vnic->fw_grp_ids = NULL;
2564
2565 kfree(vnic->uc_list);
2566 vnic->uc_list = NULL;
2567
2568 if (vnic->mc_list) {
2569 dma_free_coherent(&pdev->dev, vnic->mc_list_size,
2570 vnic->mc_list, vnic->mc_list_mapping);
2571 vnic->mc_list = NULL;
2572 }
2573
2574 if (vnic->rss_table) {
2575 dma_free_coherent(&pdev->dev, PAGE_SIZE,
2576 vnic->rss_table,
2577 vnic->rss_table_dma_addr);
2578 vnic->rss_table = NULL;
2579 }
2580
2581 vnic->rss_hash_key = NULL;
2582 vnic->flags = 0;
2583 }
2584}
2585
2586static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
2587{
2588 int i, rc = 0, size;
2589 struct bnxt_vnic_info *vnic;
2590 struct pci_dev *pdev = bp->pdev;
2591 int max_rings;
2592
2593 for (i = 0; i < bp->nr_vnics; i++) {
2594 vnic = &bp->vnic_info[i];
2595
2596 if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
2597 int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
2598
2599 if (mem_size > 0) {
2600 vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
2601 if (!vnic->uc_list) {
2602 rc = -ENOMEM;
2603 goto out;
2604 }
2605 }
2606 }
2607
2608 if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
2609 vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
2610 vnic->mc_list =
2611 dma_alloc_coherent(&pdev->dev,
2612 vnic->mc_list_size,
2613 &vnic->mc_list_mapping,
2614 GFP_KERNEL);
2615 if (!vnic->mc_list) {
2616 rc = -ENOMEM;
2617 goto out;
2618 }
2619 }
2620
2621 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
2622 max_rings = bp->rx_nr_rings;
2623 else
2624 max_rings = 1;
2625
2626 vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
2627 if (!vnic->fw_grp_ids) {
2628 rc = -ENOMEM;
2629 goto out;
2630 }
2631
2632 /* Allocate rss table and hash key */
2633 vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2634 &vnic->rss_table_dma_addr,
2635 GFP_KERNEL);
2636 if (!vnic->rss_table) {
2637 rc = -ENOMEM;
2638 goto out;
2639 }
2640
2641 size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
2642
2643 vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
2644 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
2645 }
2646 return 0;
2647
2648out:
2649 return rc;
2650}
2651
2652static void bnxt_free_hwrm_resources(struct bnxt *bp)
2653{
2654 struct pci_dev *pdev = bp->pdev;
2655
2656 dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
2657 bp->hwrm_cmd_resp_dma_addr);
2658
2659 bp->hwrm_cmd_resp_addr = NULL;
2660 if (bp->hwrm_dbg_resp_addr) {
2661 dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
2662 bp->hwrm_dbg_resp_addr,
2663 bp->hwrm_dbg_resp_dma_addr);
2664
2665 bp->hwrm_dbg_resp_addr = NULL;
2666 }
2667}
2668
2669static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
2670{
2671 struct pci_dev *pdev = bp->pdev;
2672
2673 bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
2674 &bp->hwrm_cmd_resp_dma_addr,
2675 GFP_KERNEL);
2676 if (!bp->hwrm_cmd_resp_addr)
2677 return -ENOMEM;
2678 bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
2679 HWRM_DBG_REG_BUF_SIZE,
2680 &bp->hwrm_dbg_resp_dma_addr,
2681 GFP_KERNEL);
2682 if (!bp->hwrm_dbg_resp_addr)
2683 netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
2684
2685 return 0;
2686}
2687
2688static void bnxt_free_stats(struct bnxt *bp)
2689{
2690 u32 size, i;
2691 struct pci_dev *pdev = bp->pdev;
2692
Michael Chan3bdf56c2016-03-07 15:38:45 -05002693 if (bp->hw_rx_port_stats) {
2694 dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
2695 bp->hw_rx_port_stats,
2696 bp->hw_rx_port_stats_map);
2697 bp->hw_rx_port_stats = NULL;
2698 bp->flags &= ~BNXT_FLAG_PORT_STATS;
2699 }
2700
Michael Chanc0c050c2015-10-22 16:01:17 -04002701 if (!bp->bnapi)
2702 return;
2703
2704 size = sizeof(struct ctx_hw_stats);
2705
2706 for (i = 0; i < bp->cp_nr_rings; i++) {
2707 struct bnxt_napi *bnapi = bp->bnapi[i];
2708 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2709
2710 if (cpr->hw_stats) {
2711 dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
2712 cpr->hw_stats_map);
2713 cpr->hw_stats = NULL;
2714 }
2715 }
2716}
2717
2718static int bnxt_alloc_stats(struct bnxt *bp)
2719{
2720 u32 size, i;
2721 struct pci_dev *pdev = bp->pdev;
2722
2723 size = sizeof(struct ctx_hw_stats);
2724
2725 for (i = 0; i < bp->cp_nr_rings; i++) {
2726 struct bnxt_napi *bnapi = bp->bnapi[i];
2727 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2728
2729 cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
2730 &cpr->hw_stats_map,
2731 GFP_KERNEL);
2732 if (!cpr->hw_stats)
2733 return -ENOMEM;
2734
2735 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
2736 }
Michael Chan3bdf56c2016-03-07 15:38:45 -05002737
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04002738 if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
Michael Chan3bdf56c2016-03-07 15:38:45 -05002739 bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
2740 sizeof(struct tx_port_stats) + 1024;
2741
2742 bp->hw_rx_port_stats =
2743 dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
2744 &bp->hw_rx_port_stats_map,
2745 GFP_KERNEL);
2746 if (!bp->hw_rx_port_stats)
2747 return -ENOMEM;
2748
2749 bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
2750 512;
2751 bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
2752 sizeof(struct rx_port_stats) + 512;
2753 bp->flags |= BNXT_FLAG_PORT_STATS;
2754 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002755 return 0;
2756}
2757
2758static void bnxt_clear_ring_indices(struct bnxt *bp)
2759{
2760 int i;
2761
2762 if (!bp->bnapi)
2763 return;
2764
2765 for (i = 0; i < bp->cp_nr_rings; i++) {
2766 struct bnxt_napi *bnapi = bp->bnapi[i];
2767 struct bnxt_cp_ring_info *cpr;
2768 struct bnxt_rx_ring_info *rxr;
2769 struct bnxt_tx_ring_info *txr;
2770
2771 if (!bnapi)
2772 continue;
2773
2774 cpr = &bnapi->cp_ring;
2775 cpr->cp_raw_cons = 0;
2776
Michael Chanb6ab4b02016-01-02 23:44:59 -05002777 txr = bnapi->tx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002778 if (txr) {
2779 txr->tx_prod = 0;
2780 txr->tx_cons = 0;
2781 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002782
Michael Chanb6ab4b02016-01-02 23:44:59 -05002783 rxr = bnapi->rx_ring;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002784 if (rxr) {
2785 rxr->rx_prod = 0;
2786 rxr->rx_agg_prod = 0;
2787 rxr->rx_sw_agg_prod = 0;
Michael Chan376a5b82016-05-10 19:17:59 -04002788 rxr->rx_next_cons = 0;
Michael Chan3b2b7d92016-01-02 23:45:00 -05002789 }
Michael Chanc0c050c2015-10-22 16:01:17 -04002790 }
2791}
2792
2793static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
2794{
2795#ifdef CONFIG_RFS_ACCEL
2796 int i;
2797
2798 /* Under rtnl_lock and all our NAPIs have been disabled. It's
2799 * safe to delete the hash table.
2800 */
2801 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
2802 struct hlist_head *head;
2803 struct hlist_node *tmp;
2804 struct bnxt_ntuple_filter *fltr;
2805
2806 head = &bp->ntp_fltr_hash_tbl[i];
2807 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
2808 hlist_del(&fltr->hash);
2809 kfree(fltr);
2810 }
2811 }
2812 if (irq_reinit) {
2813 kfree(bp->ntp_fltr_bmap);
2814 bp->ntp_fltr_bmap = NULL;
2815 }
2816 bp->ntp_fltr_count = 0;
2817#endif
2818}
2819
2820static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
2821{
2822#ifdef CONFIG_RFS_ACCEL
2823 int i, rc = 0;
2824
2825 if (!(bp->flags & BNXT_FLAG_RFS))
2826 return 0;
2827
2828 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
2829 INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
2830
2831 bp->ntp_fltr_count = 0;
2832 bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
2833 GFP_KERNEL);
2834
2835 if (!bp->ntp_fltr_bmap)
2836 rc = -ENOMEM;
2837
2838 return rc;
2839#else
2840 return 0;
2841#endif
2842}
2843
2844static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
2845{
2846 bnxt_free_vnic_attributes(bp);
2847 bnxt_free_tx_rings(bp);
2848 bnxt_free_rx_rings(bp);
2849 bnxt_free_cp_rings(bp);
2850 bnxt_free_ntp_fltrs(bp, irq_re_init);
2851 if (irq_re_init) {
2852 bnxt_free_stats(bp);
2853 bnxt_free_ring_grps(bp);
2854 bnxt_free_vnics(bp);
Michael Chanb6ab4b02016-01-02 23:44:59 -05002855 kfree(bp->tx_ring);
2856 bp->tx_ring = NULL;
2857 kfree(bp->rx_ring);
2858 bp->rx_ring = NULL;
Michael Chanc0c050c2015-10-22 16:01:17 -04002859 kfree(bp->bnapi);
2860 bp->bnapi = NULL;
2861 } else {
2862 bnxt_clear_ring_indices(bp);
2863 }
2864}
2865
2866static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
2867{
Michael Chan01657bc2016-01-02 23:45:03 -05002868 int i, j, rc, size, arr_size;
Michael Chanc0c050c2015-10-22 16:01:17 -04002869 void *bnapi;
2870
2871 if (irq_re_init) {
2872 /* Allocate bnapi mem pointer array and mem block for
2873 * all queues
2874 */
2875 arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
2876 bp->cp_nr_rings);
2877 size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
2878 bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
2879 if (!bnapi)
2880 return -ENOMEM;
2881
2882 bp->bnapi = bnapi;
2883 bnapi += arr_size;
2884 for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
2885 bp->bnapi[i] = bnapi;
2886 bp->bnapi[i]->index = i;
2887 bp->bnapi[i]->bp = bp;
2888 }
2889
Michael Chanb6ab4b02016-01-02 23:44:59 -05002890 bp->rx_ring = kcalloc(bp->rx_nr_rings,
2891 sizeof(struct bnxt_rx_ring_info),
2892 GFP_KERNEL);
2893 if (!bp->rx_ring)
2894 return -ENOMEM;
2895
2896 for (i = 0; i < bp->rx_nr_rings; i++) {
2897 bp->rx_ring[i].bnapi = bp->bnapi[i];
2898 bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
2899 }
2900
2901 bp->tx_ring = kcalloc(bp->tx_nr_rings,
2902 sizeof(struct bnxt_tx_ring_info),
2903 GFP_KERNEL);
2904 if (!bp->tx_ring)
2905 return -ENOMEM;
2906
Michael Chan01657bc2016-01-02 23:45:03 -05002907 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
2908 j = 0;
2909 else
2910 j = bp->rx_nr_rings;
2911
2912 for (i = 0; i < bp->tx_nr_rings; i++, j++) {
2913 bp->tx_ring[i].bnapi = bp->bnapi[j];
2914 bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
Michael Chanb6ab4b02016-01-02 23:44:59 -05002915 }
2916
Michael Chanc0c050c2015-10-22 16:01:17 -04002917 rc = bnxt_alloc_stats(bp);
2918 if (rc)
2919 goto alloc_mem_err;
2920
2921 rc = bnxt_alloc_ntp_fltrs(bp);
2922 if (rc)
2923 goto alloc_mem_err;
2924
2925 rc = bnxt_alloc_vnics(bp);
2926 if (rc)
2927 goto alloc_mem_err;
2928 }
2929
2930 bnxt_init_ring_struct(bp);
2931
2932 rc = bnxt_alloc_rx_rings(bp);
2933 if (rc)
2934 goto alloc_mem_err;
2935
2936 rc = bnxt_alloc_tx_rings(bp);
2937 if (rc)
2938 goto alloc_mem_err;
2939
2940 rc = bnxt_alloc_cp_rings(bp);
2941 if (rc)
2942 goto alloc_mem_err;
2943
2944 bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
2945 BNXT_VNIC_UCAST_FLAG;
2946 rc = bnxt_alloc_vnic_attributes(bp);
2947 if (rc)
2948 goto alloc_mem_err;
2949 return 0;
2950
2951alloc_mem_err:
2952 bnxt_free_mem(bp, true);
2953 return rc;
2954}
2955
2956void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
2957 u16 cmpl_ring, u16 target_id)
2958{
Michael Chana8643e12016-02-26 04:00:05 -05002959 struct input *req = request;
Michael Chanc0c050c2015-10-22 16:01:17 -04002960
Michael Chana8643e12016-02-26 04:00:05 -05002961 req->req_type = cpu_to_le16(req_type);
2962 req->cmpl_ring = cpu_to_le16(cmpl_ring);
2963 req->target_id = cpu_to_le16(target_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04002964 req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
2965}
2966
Michael Chanfbfbc482016-02-26 04:00:07 -05002967static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
2968 int timeout, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04002969{
Michael Chana11fa2b2016-05-15 03:04:47 -04002970 int i, intr_process, rc, tmo_count;
Michael Chana8643e12016-02-26 04:00:05 -05002971 struct input *req = msg;
Michael Chanc0c050c2015-10-22 16:01:17 -04002972 u32 *data = msg;
2973 __le32 *resp_len, *valid;
2974 u16 cp_ring_id, len = 0;
2975 struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
2976
Michael Chana8643e12016-02-26 04:00:05 -05002977 req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
Michael Chanc0c050c2015-10-22 16:01:17 -04002978 memset(resp, 0, PAGE_SIZE);
Michael Chana8643e12016-02-26 04:00:05 -05002979 cp_ring_id = le16_to_cpu(req->cmpl_ring);
Michael Chanc0c050c2015-10-22 16:01:17 -04002980 intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
2981
2982 /* Write request msg to hwrm channel */
2983 __iowrite32_copy(bp->bar0, data, msg_len / 4);
2984
Michael Chane6ef2692016-03-28 19:46:05 -04002985 for (i = msg_len; i < BNXT_HWRM_MAX_REQ_LEN; i += 4)
Michael Chand79979a2016-01-07 19:56:57 -05002986 writel(0, bp->bar0 + i);
2987
Michael Chanc0c050c2015-10-22 16:01:17 -04002988 /* currently supports only one outstanding message */
2989 if (intr_process)
Michael Chana8643e12016-02-26 04:00:05 -05002990 bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04002991
2992 /* Ring channel doorbell */
2993 writel(1, bp->bar0 + 0x100);
2994
Michael Chanff4fe812016-02-26 04:00:04 -05002995 if (!timeout)
2996 timeout = DFLT_HWRM_CMD_TIMEOUT;
2997
Michael Chanc0c050c2015-10-22 16:01:17 -04002998 i = 0;
Michael Chana11fa2b2016-05-15 03:04:47 -04002999 tmo_count = timeout * 40;
Michael Chanc0c050c2015-10-22 16:01:17 -04003000 if (intr_process) {
3001 /* Wait until hwrm response cmpl interrupt is processed */
3002 while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
Michael Chana11fa2b2016-05-15 03:04:47 -04003003 i++ < tmo_count) {
3004 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003005 }
3006
3007 if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
3008 netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
Michael Chana8643e12016-02-26 04:00:05 -05003009 le16_to_cpu(req->req_type));
Michael Chanc0c050c2015-10-22 16:01:17 -04003010 return -1;
3011 }
3012 } else {
3013 /* Check if response len is updated */
3014 resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
Michael Chana11fa2b2016-05-15 03:04:47 -04003015 for (i = 0; i < tmo_count; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003016 len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
3017 HWRM_RESP_LEN_SFT;
3018 if (len)
3019 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003020 usleep_range(25, 40);
Michael Chanc0c050c2015-10-22 16:01:17 -04003021 }
3022
Michael Chana11fa2b2016-05-15 03:04:47 -04003023 if (i >= tmo_count) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003024 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003025 timeout, le16_to_cpu(req->req_type),
Michael Chan8578d6c2016-05-15 03:04:48 -04003026 le16_to_cpu(req->seq_id), len);
Michael Chanc0c050c2015-10-22 16:01:17 -04003027 return -1;
3028 }
3029
3030 /* Last word of resp contains valid bit */
3031 valid = bp->hwrm_cmd_resp_addr + len - 4;
Michael Chana11fa2b2016-05-15 03:04:47 -04003032 for (i = 0; i < 5; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003033 if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
3034 break;
Michael Chana11fa2b2016-05-15 03:04:47 -04003035 udelay(1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003036 }
3037
Michael Chana11fa2b2016-05-15 03:04:47 -04003038 if (i >= 5) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003039 netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
Michael Chana8643e12016-02-26 04:00:05 -05003040 timeout, le16_to_cpu(req->req_type),
3041 le16_to_cpu(req->seq_id), len, *valid);
Michael Chanc0c050c2015-10-22 16:01:17 -04003042 return -1;
3043 }
3044 }
3045
3046 rc = le16_to_cpu(resp->error_code);
Michael Chanfbfbc482016-02-26 04:00:07 -05003047 if (rc && !silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04003048 netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
3049 le16_to_cpu(resp->req_type),
3050 le16_to_cpu(resp->seq_id), rc);
Michael Chanfbfbc482016-02-26 04:00:07 -05003051 return rc;
3052}
3053
3054int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3055{
3056 return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
Michael Chanc0c050c2015-10-22 16:01:17 -04003057}
3058
3059int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
3060{
3061 int rc;
3062
3063 mutex_lock(&bp->hwrm_cmd_lock);
3064 rc = _hwrm_send_message(bp, msg, msg_len, timeout);
3065 mutex_unlock(&bp->hwrm_cmd_lock);
3066 return rc;
3067}
3068
Michael Chan90e209212016-02-26 04:00:08 -05003069int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
3070 int timeout)
3071{
3072 int rc;
3073
3074 mutex_lock(&bp->hwrm_cmd_lock);
3075 rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
3076 mutex_unlock(&bp->hwrm_cmd_lock);
3077 return rc;
3078}
3079
Michael Chanc0c050c2015-10-22 16:01:17 -04003080static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
3081{
3082 struct hwrm_func_drv_rgtr_input req = {0};
3083 int i;
Michael Chan25be8622016-04-05 14:09:00 -04003084 DECLARE_BITMAP(async_events_bmap, 256);
3085 u32 *events = (u32 *)async_events_bmap;
Michael Chanc0c050c2015-10-22 16:01:17 -04003086
3087 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
3088
3089 req.enables =
3090 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
3091 FUNC_DRV_RGTR_REQ_ENABLES_VER |
3092 FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
3093
Michael Chan25be8622016-04-05 14:09:00 -04003094 memset(async_events_bmap, 0, sizeof(async_events_bmap));
3095 for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
3096 __set_bit(bnxt_async_events_arr[i], async_events_bmap);
3097
3098 for (i = 0; i < 8; i++)
3099 req.async_event_fwd[i] |= cpu_to_le32(events[i]);
3100
Michael Chan11f15ed2016-04-05 14:08:55 -04003101 req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
Michael Chanc0c050c2015-10-22 16:01:17 -04003102 req.ver_maj = DRV_VER_MAJ;
3103 req.ver_min = DRV_VER_MIN;
3104 req.ver_upd = DRV_VER_UPD;
3105
3106 if (BNXT_PF(bp)) {
Michael Chande68f5de2015-12-09 19:35:41 -05003107 DECLARE_BITMAP(vf_req_snif_bmap, 256);
Michael Chanc0c050c2015-10-22 16:01:17 -04003108 u32 *data = (u32 *)vf_req_snif_bmap;
3109
Michael Chande68f5de2015-12-09 19:35:41 -05003110 memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
Michael Chanc0c050c2015-10-22 16:01:17 -04003111 for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
3112 __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
3113
Michael Chande68f5de2015-12-09 19:35:41 -05003114 for (i = 0; i < 8; i++)
3115 req.vf_req_fwd[i] = cpu_to_le32(data[i]);
3116
Michael Chanc0c050c2015-10-22 16:01:17 -04003117 req.enables |=
3118 cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
3119 }
3120
3121 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3122}
3123
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05003124static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
3125{
3126 struct hwrm_func_drv_unrgtr_input req = {0};
3127
3128 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
3129 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3130}
3131
Michael Chanc0c050c2015-10-22 16:01:17 -04003132static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
3133{
3134 u32 rc = 0;
3135 struct hwrm_tunnel_dst_port_free_input req = {0};
3136
3137 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
3138 req.tunnel_type = tunnel_type;
3139
3140 switch (tunnel_type) {
3141 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
3142 req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
3143 break;
3144 case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
3145 req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
3146 break;
3147 default:
3148 break;
3149 }
3150
3151 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3152 if (rc)
3153 netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
3154 rc);
3155 return rc;
3156}
3157
3158static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
3159 u8 tunnel_type)
3160{
3161 u32 rc = 0;
3162 struct hwrm_tunnel_dst_port_alloc_input req = {0};
3163 struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3164
3165 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
3166
3167 req.tunnel_type = tunnel_type;
3168 req.tunnel_dst_port_val = port;
3169
3170 mutex_lock(&bp->hwrm_cmd_lock);
3171 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3172 if (rc) {
3173 netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
3174 rc);
3175 goto err_out;
3176 }
3177
3178 if (tunnel_type & TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN)
3179 bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
3180
3181 else if (tunnel_type & TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE)
3182 bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
3183err_out:
3184 mutex_unlock(&bp->hwrm_cmd_lock);
3185 return rc;
3186}
3187
3188static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
3189{
3190 struct hwrm_cfa_l2_set_rx_mask_input req = {0};
3191 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3192
3193 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
Michael Chanc1935542015-12-27 18:19:28 -05003194 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003195
3196 req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
3197 req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
3198 req.mask = cpu_to_le32(vnic->rx_mask);
3199 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3200}
3201
3202#ifdef CONFIG_RFS_ACCEL
3203static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
3204 struct bnxt_ntuple_filter *fltr)
3205{
3206 struct hwrm_cfa_ntuple_filter_free_input req = {0};
3207
3208 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
3209 req.ntuple_filter_id = fltr->filter_id;
3210 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3211}
3212
3213#define BNXT_NTP_FLTR_FLAGS \
3214 (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
3215 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
3216 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
3217 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
3218 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
3219 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
3220 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
3221 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
3222 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
3223 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
3224 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
3225 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
3226 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
Michael Chanc1935542015-12-27 18:19:28 -05003227 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003228
3229static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
3230 struct bnxt_ntuple_filter *fltr)
3231{
3232 int rc = 0;
3233 struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
3234 struct hwrm_cfa_ntuple_filter_alloc_output *resp =
3235 bp->hwrm_cmd_resp_addr;
3236 struct flow_keys *keys = &fltr->fkeys;
3237 struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
3238
3239 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
3240 req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[0];
3241
3242 req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
3243
3244 req.ethertype = htons(ETH_P_IP);
3245 memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
Michael Chanc1935542015-12-27 18:19:28 -05003246 req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
Michael Chanc0c050c2015-10-22 16:01:17 -04003247 req.ip_protocol = keys->basic.ip_proto;
3248
3249 req.src_ipaddr[0] = keys->addrs.v4addrs.src;
3250 req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3251 req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
3252 req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
3253
3254 req.src_port = keys->ports.src;
3255 req.src_port_mask = cpu_to_be16(0xffff);
3256 req.dst_port = keys->ports.dst;
3257 req.dst_port_mask = cpu_to_be16(0xffff);
3258
Michael Chanc1935542015-12-27 18:19:28 -05003259 req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003260 mutex_lock(&bp->hwrm_cmd_lock);
3261 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3262 if (!rc)
3263 fltr->filter_id = resp->ntuple_filter_id;
3264 mutex_unlock(&bp->hwrm_cmd_lock);
3265 return rc;
3266}
3267#endif
3268
3269static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
3270 u8 *mac_addr)
3271{
3272 u32 rc = 0;
3273 struct hwrm_cfa_l2_filter_alloc_input req = {0};
3274 struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3275
3276 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003277 req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
3278 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
3279 req.flags |=
3280 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
Michael Chanc1935542015-12-27 18:19:28 -05003281 req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003282 req.enables =
3283 cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
Michael Chanc1935542015-12-27 18:19:28 -05003284 CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
Michael Chanc0c050c2015-10-22 16:01:17 -04003285 CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
3286 memcpy(req.l2_addr, mac_addr, ETH_ALEN);
3287 req.l2_addr_mask[0] = 0xff;
3288 req.l2_addr_mask[1] = 0xff;
3289 req.l2_addr_mask[2] = 0xff;
3290 req.l2_addr_mask[3] = 0xff;
3291 req.l2_addr_mask[4] = 0xff;
3292 req.l2_addr_mask[5] = 0xff;
3293
3294 mutex_lock(&bp->hwrm_cmd_lock);
3295 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3296 if (!rc)
3297 bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
3298 resp->l2_filter_id;
3299 mutex_unlock(&bp->hwrm_cmd_lock);
3300 return rc;
3301}
3302
3303static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
3304{
3305 u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
3306 int rc = 0;
3307
3308 /* Any associated ntuple filters will also be cleared by firmware. */
3309 mutex_lock(&bp->hwrm_cmd_lock);
3310 for (i = 0; i < num_of_vnics; i++) {
3311 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3312
3313 for (j = 0; j < vnic->uc_filter_count; j++) {
3314 struct hwrm_cfa_l2_filter_free_input req = {0};
3315
3316 bnxt_hwrm_cmd_hdr_init(bp, &req,
3317 HWRM_CFA_L2_FILTER_FREE, -1, -1);
3318
3319 req.l2_filter_id = vnic->fw_l2_filter_id[j];
3320
3321 rc = _hwrm_send_message(bp, &req, sizeof(req),
3322 HWRM_CMD_TIMEOUT);
3323 }
3324 vnic->uc_filter_count = 0;
3325 }
3326 mutex_unlock(&bp->hwrm_cmd_lock);
3327
3328 return rc;
3329}
3330
3331static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
3332{
3333 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3334 struct hwrm_vnic_tpa_cfg_input req = {0};
3335
3336 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
3337
3338 if (tpa_flags) {
3339 u16 mss = bp->dev->mtu - 40;
3340 u32 nsegs, n, segs = 0, flags;
3341
3342 flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
3343 VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
3344 VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
3345 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
3346 VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
3347 if (tpa_flags & BNXT_FLAG_GRO)
3348 flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
3349
3350 req.flags = cpu_to_le32(flags);
3351
3352 req.enables =
3353 cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
Michael Chanc1935542015-12-27 18:19:28 -05003354 VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
3355 VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04003356
3357 /* Number of segs are log2 units, and first packet is not
3358 * included as part of this units.
3359 */
Michael Chan2839f282016-04-25 02:30:50 -04003360 if (mss <= BNXT_RX_PAGE_SIZE) {
3361 n = BNXT_RX_PAGE_SIZE / mss;
Michael Chanc0c050c2015-10-22 16:01:17 -04003362 nsegs = (MAX_SKB_FRAGS - 1) * n;
3363 } else {
Michael Chan2839f282016-04-25 02:30:50 -04003364 n = mss / BNXT_RX_PAGE_SIZE;
3365 if (mss & (BNXT_RX_PAGE_SIZE - 1))
Michael Chanc0c050c2015-10-22 16:01:17 -04003366 n++;
3367 nsegs = (MAX_SKB_FRAGS - n) / n;
3368 }
3369
3370 segs = ilog2(nsegs);
3371 req.max_agg_segs = cpu_to_le16(segs);
3372 req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
Michael Chanc1935542015-12-27 18:19:28 -05003373
3374 req.min_agg_len = cpu_to_le32(512);
Michael Chanc0c050c2015-10-22 16:01:17 -04003375 }
3376 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3377
3378 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3379}
3380
3381static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
3382{
3383 u32 i, j, max_rings;
3384 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3385 struct hwrm_vnic_rss_cfg_input req = {0};
3386
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003387 if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
Michael Chanc0c050c2015-10-22 16:01:17 -04003388 return 0;
3389
3390 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
3391 if (set_rss) {
3392 vnic->hash_type = BNXT_RSS_HASH_TYPE_FLAG_IPV4 |
3393 BNXT_RSS_HASH_TYPE_FLAG_TCP_IPV4 |
3394 BNXT_RSS_HASH_TYPE_FLAG_IPV6 |
3395 BNXT_RSS_HASH_TYPE_FLAG_TCP_IPV6;
3396
3397 req.hash_type = cpu_to_le32(vnic->hash_type);
3398
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003399 if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
3400 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
3401 max_rings = bp->rx_nr_rings - 1;
3402 else
3403 max_rings = bp->rx_nr_rings;
3404 } else {
Michael Chanc0c050c2015-10-22 16:01:17 -04003405 max_rings = 1;
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003406 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003407
3408 /* Fill the RSS indirection table with ring group ids */
3409 for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
3410 if (j == max_rings)
3411 j = 0;
3412 vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
3413 }
3414
3415 req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
3416 req.hash_key_tbl_addr =
3417 cpu_to_le64(vnic->rss_hash_key_dma_addr);
3418 }
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003419 req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003420 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3421}
3422
3423static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
3424{
3425 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3426 struct hwrm_vnic_plcmodes_cfg_input req = {0};
3427
3428 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
3429 req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
3430 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
3431 VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
3432 req.enables =
3433 cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
3434 VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
3435 /* thresholds not implemented in firmware yet */
3436 req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
3437 req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
3438 req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
3439 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3440}
3441
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003442static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
3443 u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003444{
3445 struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
3446
3447 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
3448 req.rss_cos_lb_ctx_id =
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003449 cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
Michael Chanc0c050c2015-10-22 16:01:17 -04003450
3451 hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003452 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003453}
3454
3455static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
3456{
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003457 int i, j;
Michael Chanc0c050c2015-10-22 16:01:17 -04003458
3459 for (i = 0; i < bp->nr_vnics; i++) {
3460 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
3461
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003462 for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
3463 if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
3464 bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
3465 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003466 }
3467 bp->rsscos_nr_ctxs = 0;
3468}
3469
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003470static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
Michael Chanc0c050c2015-10-22 16:01:17 -04003471{
3472 int rc;
3473 struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
3474 struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
3475 bp->hwrm_cmd_resp_addr;
3476
3477 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
3478 -1);
3479
3480 mutex_lock(&bp->hwrm_cmd_lock);
3481 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3482 if (!rc)
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003483 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
Michael Chanc0c050c2015-10-22 16:01:17 -04003484 le16_to_cpu(resp->rss_cos_lb_ctx_id);
3485 mutex_unlock(&bp->hwrm_cmd_lock);
3486
3487 return rc;
3488}
3489
3490static int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
3491{
Michael Chanb81a90d2016-01-02 23:45:01 -05003492 unsigned int ring = 0, grp_idx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003493 struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
3494 struct hwrm_vnic_cfg_input req = {0};
Michael Chancf6645f2016-06-13 02:25:28 -04003495 u16 def_vlan = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003496
3497 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003498
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04003499 req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
3500 /* Only RSS support for now TBD: COS & LB */
3501 if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
3502 req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
3503 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
3504 VNIC_CFG_REQ_ENABLES_MRU);
3505 } else {
3506 req.rss_rule = cpu_to_le16(0xffff);
3507 }
3508
3509 if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
3510 (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003511 req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
3512 req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
3513 } else {
3514 req.cos_rule = cpu_to_le16(0xffff);
3515 }
3516
Michael Chanc0c050c2015-10-22 16:01:17 -04003517 if (vnic->flags & BNXT_VNIC_RSS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003518 ring = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04003519 else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
Michael Chanb81a90d2016-01-02 23:45:01 -05003520 ring = vnic_id - 1;
Prashant Sreedharan76595192016-07-18 07:15:22 -04003521 else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
3522 ring = bp->rx_nr_rings - 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04003523
Michael Chanb81a90d2016-01-02 23:45:01 -05003524 grp_idx = bp->rx_ring[ring].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003525 req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
3526 req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
3527
3528 req.lb_rule = cpu_to_le16(0xffff);
3529 req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
3530 VLAN_HLEN);
3531
Michael Chancf6645f2016-06-13 02:25:28 -04003532#ifdef CONFIG_BNXT_SRIOV
3533 if (BNXT_VF(bp))
3534 def_vlan = bp->vf.vlan;
3535#endif
3536 if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
Michael Chanc0c050c2015-10-22 16:01:17 -04003537 req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
3538
3539 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3540}
3541
3542static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
3543{
3544 u32 rc = 0;
3545
3546 if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
3547 struct hwrm_vnic_free_input req = {0};
3548
3549 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
3550 req.vnic_id =
3551 cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
3552
3553 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3554 if (rc)
3555 return rc;
3556 bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
3557 }
3558 return rc;
3559}
3560
3561static void bnxt_hwrm_vnic_free(struct bnxt *bp)
3562{
3563 u16 i;
3564
3565 for (i = 0; i < bp->nr_vnics; i++)
3566 bnxt_hwrm_vnic_free_one(bp, i);
3567}
3568
Michael Chanb81a90d2016-01-02 23:45:01 -05003569static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
3570 unsigned int start_rx_ring_idx,
3571 unsigned int nr_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04003572{
Michael Chanb81a90d2016-01-02 23:45:01 -05003573 int rc = 0;
3574 unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003575 struct hwrm_vnic_alloc_input req = {0};
3576 struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3577
3578 /* map ring groups to this vnic */
Michael Chanb81a90d2016-01-02 23:45:01 -05003579 for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
3580 grp_idx = bp->rx_ring[i].bnapi->index;
3581 if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
Michael Chanc0c050c2015-10-22 16:01:17 -04003582 netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05003583 j, nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04003584 break;
3585 }
3586 bp->vnic_info[vnic_id].fw_grp_ids[j] =
Michael Chanb81a90d2016-01-02 23:45:01 -05003587 bp->grp_info[grp_idx].fw_grp_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003588 }
3589
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04003590 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
3591 bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003592 if (vnic_id == 0)
3593 req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
3594
3595 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
3596
3597 mutex_lock(&bp->hwrm_cmd_lock);
3598 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3599 if (!rc)
3600 bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
3601 mutex_unlock(&bp->hwrm_cmd_lock);
3602 return rc;
3603}
3604
3605static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
3606{
3607 u16 i;
3608 u32 rc = 0;
3609
3610 mutex_lock(&bp->hwrm_cmd_lock);
3611 for (i = 0; i < bp->rx_nr_rings; i++) {
3612 struct hwrm_ring_grp_alloc_input req = {0};
3613 struct hwrm_ring_grp_alloc_output *resp =
3614 bp->hwrm_cmd_resp_addr;
Michael Chanb81a90d2016-01-02 23:45:01 -05003615 unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003616
3617 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
3618
Michael Chanb81a90d2016-01-02 23:45:01 -05003619 req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
3620 req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
3621 req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
3622 req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
Michael Chanc0c050c2015-10-22 16:01:17 -04003623
3624 rc = _hwrm_send_message(bp, &req, sizeof(req),
3625 HWRM_CMD_TIMEOUT);
3626 if (rc)
3627 break;
3628
Michael Chanb81a90d2016-01-02 23:45:01 -05003629 bp->grp_info[grp_idx].fw_grp_id =
3630 le32_to_cpu(resp->ring_group_id);
Michael Chanc0c050c2015-10-22 16:01:17 -04003631 }
3632 mutex_unlock(&bp->hwrm_cmd_lock);
3633 return rc;
3634}
3635
3636static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
3637{
3638 u16 i;
3639 u32 rc = 0;
3640 struct hwrm_ring_grp_free_input req = {0};
3641
3642 if (!bp->grp_info)
3643 return 0;
3644
3645 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
3646
3647 mutex_lock(&bp->hwrm_cmd_lock);
3648 for (i = 0; i < bp->cp_nr_rings; i++) {
3649 if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
3650 continue;
3651 req.ring_group_id =
3652 cpu_to_le32(bp->grp_info[i].fw_grp_id);
3653
3654 rc = _hwrm_send_message(bp, &req, sizeof(req),
3655 HWRM_CMD_TIMEOUT);
3656 if (rc)
3657 break;
3658 bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
3659 }
3660 mutex_unlock(&bp->hwrm_cmd_lock);
3661 return rc;
3662}
3663
3664static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
3665 struct bnxt_ring_struct *ring,
3666 u32 ring_type, u32 map_index,
3667 u32 stats_ctx_id)
3668{
3669 int rc = 0, err = 0;
3670 struct hwrm_ring_alloc_input req = {0};
3671 struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
3672 u16 ring_id;
3673
3674 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
3675
3676 req.enables = 0;
3677 if (ring->nr_pages > 1) {
3678 req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
3679 /* Page size is in log2 units */
3680 req.page_size = BNXT_PAGE_SHIFT;
3681 req.page_tbl_depth = 1;
3682 } else {
3683 req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
3684 }
3685 req.fbo = 0;
3686 /* Association of ring index with doorbell index and MSIX number */
3687 req.logical_id = cpu_to_le16(map_index);
3688
3689 switch (ring_type) {
3690 case HWRM_RING_ALLOC_TX:
3691 req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
3692 /* Association of transmit ring with completion ring */
3693 req.cmpl_ring_id =
3694 cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
3695 req.length = cpu_to_le32(bp->tx_ring_mask + 1);
3696 req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
3697 req.queue_id = cpu_to_le16(ring->queue_id);
3698 break;
3699 case HWRM_RING_ALLOC_RX:
3700 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3701 req.length = cpu_to_le32(bp->rx_ring_mask + 1);
3702 break;
3703 case HWRM_RING_ALLOC_AGG:
3704 req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
3705 req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
3706 break;
3707 case HWRM_RING_ALLOC_CMPL:
3708 req.ring_type = RING_ALLOC_REQ_RING_TYPE_CMPL;
3709 req.length = cpu_to_le32(bp->cp_ring_mask + 1);
3710 if (bp->flags & BNXT_FLAG_USING_MSIX)
3711 req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
3712 break;
3713 default:
3714 netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
3715 ring_type);
3716 return -1;
3717 }
3718
3719 mutex_lock(&bp->hwrm_cmd_lock);
3720 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3721 err = le16_to_cpu(resp->error_code);
3722 ring_id = le16_to_cpu(resp->ring_id);
3723 mutex_unlock(&bp->hwrm_cmd_lock);
3724
3725 if (rc || err) {
3726 switch (ring_type) {
3727 case RING_FREE_REQ_RING_TYPE_CMPL:
3728 netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
3729 rc, err);
3730 return -1;
3731
3732 case RING_FREE_REQ_RING_TYPE_RX:
3733 netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
3734 rc, err);
3735 return -1;
3736
3737 case RING_FREE_REQ_RING_TYPE_TX:
3738 netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
3739 rc, err);
3740 return -1;
3741
3742 default:
3743 netdev_err(bp->dev, "Invalid ring\n");
3744 return -1;
3745 }
3746 }
3747 ring->fw_ring_id = ring_id;
3748 return rc;
3749}
3750
3751static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
3752{
3753 int i, rc = 0;
3754
Michael Chanedd0c2c2015-12-27 18:19:19 -05003755 for (i = 0; i < bp->cp_nr_rings; i++) {
3756 struct bnxt_napi *bnapi = bp->bnapi[i];
3757 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3758 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04003759
Prashant Sreedharan33e52d82016-03-28 19:46:04 -04003760 cpr->cp_doorbell = bp->bar1 + i * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003761 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
3762 INVALID_STATS_CTX_ID);
3763 if (rc)
3764 goto err_out;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003765 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
3766 bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003767 }
3768
Michael Chanedd0c2c2015-12-27 18:19:19 -05003769 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003770 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003771 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003772 u32 map_idx = txr->bnapi->index;
3773 u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
Michael Chanc0c050c2015-10-22 16:01:17 -04003774
Michael Chanb81a90d2016-01-02 23:45:01 -05003775 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
3776 map_idx, fw_stats_ctx);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003777 if (rc)
3778 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003779 txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003780 }
3781
Michael Chanedd0c2c2015-12-27 18:19:19 -05003782 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003783 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003784 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003785 u32 map_idx = rxr->bnapi->index;
Michael Chanc0c050c2015-10-22 16:01:17 -04003786
Michael Chanb81a90d2016-01-02 23:45:01 -05003787 rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
3788 map_idx, INVALID_STATS_CTX_ID);
Michael Chanedd0c2c2015-12-27 18:19:19 -05003789 if (rc)
3790 goto err_out;
Michael Chanb81a90d2016-01-02 23:45:01 -05003791 rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanedd0c2c2015-12-27 18:19:19 -05003792 writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003793 bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003794 }
3795
3796 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
3797 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003798 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04003799 struct bnxt_ring_struct *ring =
3800 &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003801 u32 grp_idx = rxr->bnapi->index;
3802 u32 map_idx = grp_idx + bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04003803
3804 rc = hwrm_ring_alloc_send_msg(bp, ring,
3805 HWRM_RING_ALLOC_AGG,
Michael Chanb81a90d2016-01-02 23:45:01 -05003806 map_idx,
Michael Chanc0c050c2015-10-22 16:01:17 -04003807 INVALID_STATS_CTX_ID);
3808 if (rc)
3809 goto err_out;
3810
Michael Chanb81a90d2016-01-02 23:45:01 -05003811 rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
Michael Chanc0c050c2015-10-22 16:01:17 -04003812 writel(DB_KEY_RX | rxr->rx_agg_prod,
3813 rxr->rx_agg_doorbell);
Michael Chanb81a90d2016-01-02 23:45:01 -05003814 bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003815 }
3816 }
3817err_out:
3818 return rc;
3819}
3820
3821static int hwrm_ring_free_send_msg(struct bnxt *bp,
3822 struct bnxt_ring_struct *ring,
3823 u32 ring_type, int cmpl_ring_id)
3824{
3825 int rc;
3826 struct hwrm_ring_free_input req = {0};
3827 struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
3828 u16 error_code;
3829
Prashant Sreedharan74608fc2016-01-28 03:11:20 -05003830 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003831 req.ring_type = ring_type;
3832 req.ring_id = cpu_to_le16(ring->fw_ring_id);
3833
3834 mutex_lock(&bp->hwrm_cmd_lock);
3835 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
3836 error_code = le16_to_cpu(resp->error_code);
3837 mutex_unlock(&bp->hwrm_cmd_lock);
3838
3839 if (rc || error_code) {
3840 switch (ring_type) {
3841 case RING_FREE_REQ_RING_TYPE_CMPL:
3842 netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
3843 rc);
3844 return rc;
3845 case RING_FREE_REQ_RING_TYPE_RX:
3846 netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
3847 rc);
3848 return rc;
3849 case RING_FREE_REQ_RING_TYPE_TX:
3850 netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
3851 rc);
3852 return rc;
3853 default:
3854 netdev_err(bp->dev, "Invalid ring\n");
3855 return -1;
3856 }
3857 }
3858 return 0;
3859}
3860
Michael Chanedd0c2c2015-12-27 18:19:19 -05003861static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
Michael Chanc0c050c2015-10-22 16:01:17 -04003862{
Michael Chanedd0c2c2015-12-27 18:19:19 -05003863 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04003864
3865 if (!bp->bnapi)
Michael Chanedd0c2c2015-12-27 18:19:19 -05003866 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04003867
Michael Chanedd0c2c2015-12-27 18:19:19 -05003868 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003869 struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003870 struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003871 u32 grp_idx = txr->bnapi->index;
3872 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003873
Michael Chanedd0c2c2015-12-27 18:19:19 -05003874 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3875 hwrm_ring_free_send_msg(bp, ring,
3876 RING_FREE_REQ_RING_TYPE_TX,
3877 close_path ? cmpl_ring_id :
3878 INVALID_HW_RING_ID);
3879 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003880 }
3881 }
3882
Michael Chanedd0c2c2015-12-27 18:19:19 -05003883 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003884 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003885 struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003886 u32 grp_idx = rxr->bnapi->index;
3887 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003888
Michael Chanedd0c2c2015-12-27 18:19:19 -05003889 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3890 hwrm_ring_free_send_msg(bp, ring,
3891 RING_FREE_REQ_RING_TYPE_RX,
3892 close_path ? cmpl_ring_id :
3893 INVALID_HW_RING_ID);
3894 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05003895 bp->grp_info[grp_idx].rx_fw_ring_id =
3896 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003897 }
3898 }
3899
Michael Chanedd0c2c2015-12-27 18:19:19 -05003900 for (i = 0; i < bp->rx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05003901 struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
Michael Chanedd0c2c2015-12-27 18:19:19 -05003902 struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
Michael Chanb81a90d2016-01-02 23:45:01 -05003903 u32 grp_idx = rxr->bnapi->index;
3904 u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04003905
Michael Chanedd0c2c2015-12-27 18:19:19 -05003906 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3907 hwrm_ring_free_send_msg(bp, ring,
3908 RING_FREE_REQ_RING_TYPE_RX,
3909 close_path ? cmpl_ring_id :
3910 INVALID_HW_RING_ID);
3911 ring->fw_ring_id = INVALID_HW_RING_ID;
Michael Chanb81a90d2016-01-02 23:45:01 -05003912 bp->grp_info[grp_idx].agg_fw_ring_id =
3913 INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003914 }
3915 }
3916
Michael Chanedd0c2c2015-12-27 18:19:19 -05003917 for (i = 0; i < bp->cp_nr_rings; i++) {
3918 struct bnxt_napi *bnapi = bp->bnapi[i];
3919 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3920 struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
Michael Chanc0c050c2015-10-22 16:01:17 -04003921
Michael Chanedd0c2c2015-12-27 18:19:19 -05003922 if (ring->fw_ring_id != INVALID_HW_RING_ID) {
3923 hwrm_ring_free_send_msg(bp, ring,
3924 RING_FREE_REQ_RING_TYPE_CMPL,
3925 INVALID_HW_RING_ID);
3926 ring->fw_ring_id = INVALID_HW_RING_ID;
3927 bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
Michael Chanc0c050c2015-10-22 16:01:17 -04003928 }
3929 }
Michael Chanc0c050c2015-10-22 16:01:17 -04003930}
3931
Michael Chanbb053f52016-02-26 04:00:02 -05003932static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
3933 u32 buf_tmrs, u16 flags,
3934 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
3935{
3936 req->flags = cpu_to_le16(flags);
3937 req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
3938 req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
3939 req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
3940 req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
3941 /* Minimum time between 2 interrupts set to buf_tmr x 2 */
3942 req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
3943 req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
3944 req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
3945}
3946
Michael Chanc0c050c2015-10-22 16:01:17 -04003947int bnxt_hwrm_set_coal(struct bnxt *bp)
3948{
3949 int i, rc = 0;
Michael Chandfc9c942016-02-26 04:00:03 -05003950 struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
3951 req_tx = {0}, *req;
Michael Chanc0c050c2015-10-22 16:01:17 -04003952 u16 max_buf, max_buf_irq;
3953 u16 buf_tmr, buf_tmr_irq;
3954 u32 flags;
3955
Michael Chandfc9c942016-02-26 04:00:03 -05003956 bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
3957 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
3958 bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
3959 HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003960
Michael Chandfb5b892016-02-26 04:00:01 -05003961 /* Each rx completion (2 records) should be DMAed immediately.
3962 * DMA 1/4 of the completion buffers at a time.
3963 */
3964 max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
Michael Chanc0c050c2015-10-22 16:01:17 -04003965 /* max_buf must not be zero */
3966 max_buf = clamp_t(u16, max_buf, 1, 63);
Michael Chandfb5b892016-02-26 04:00:01 -05003967 max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
3968 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
3969 /* buf timer set to 1/4 of interrupt timer */
3970 buf_tmr = max_t(u16, buf_tmr / 4, 1);
3971 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
3972 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04003973
3974 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
3975
3976 /* RING_IDLE generates more IRQs for lower latency. Enable it only
3977 * if coal_ticks is less than 25 us.
3978 */
Michael Chandfb5b892016-02-26 04:00:01 -05003979 if (bp->rx_coal_ticks < 25)
Michael Chanc0c050c2015-10-22 16:01:17 -04003980 flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
3981
Michael Chanbb053f52016-02-26 04:00:02 -05003982 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
Michael Chandfc9c942016-02-26 04:00:03 -05003983 buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
3984
3985 /* max_buf must not be zero */
3986 max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
3987 max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
3988 buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
3989 /* buf timer set to 1/4 of interrupt timer */
3990 buf_tmr = max_t(u16, buf_tmr / 4, 1);
3991 buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
3992 buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
3993
3994 flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
3995 bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
3996 buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
Michael Chanc0c050c2015-10-22 16:01:17 -04003997
3998 mutex_lock(&bp->hwrm_cmd_lock);
3999 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chandfc9c942016-02-26 04:00:03 -05004000 struct bnxt_napi *bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004001
Michael Chandfc9c942016-02-26 04:00:03 -05004002 req = &req_rx;
4003 if (!bnapi->rx_ring)
4004 req = &req_tx;
4005 req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
4006
4007 rc = _hwrm_send_message(bp, req, sizeof(*req),
Michael Chanc0c050c2015-10-22 16:01:17 -04004008 HWRM_CMD_TIMEOUT);
4009 if (rc)
4010 break;
4011 }
4012 mutex_unlock(&bp->hwrm_cmd_lock);
4013 return rc;
4014}
4015
4016static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
4017{
4018 int rc = 0, i;
4019 struct hwrm_stat_ctx_free_input req = {0};
4020
4021 if (!bp->bnapi)
4022 return 0;
4023
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004024 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4025 return 0;
4026
Michael Chanc0c050c2015-10-22 16:01:17 -04004027 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
4028
4029 mutex_lock(&bp->hwrm_cmd_lock);
4030 for (i = 0; i < bp->cp_nr_rings; i++) {
4031 struct bnxt_napi *bnapi = bp->bnapi[i];
4032 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4033
4034 if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
4035 req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
4036
4037 rc = _hwrm_send_message(bp, &req, sizeof(req),
4038 HWRM_CMD_TIMEOUT);
4039 if (rc)
4040 break;
4041
4042 cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
4043 }
4044 }
4045 mutex_unlock(&bp->hwrm_cmd_lock);
4046 return rc;
4047}
4048
4049static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
4050{
4051 int rc = 0, i;
4052 struct hwrm_stat_ctx_alloc_input req = {0};
4053 struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
4054
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004055 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4056 return 0;
4057
Michael Chanc0c050c2015-10-22 16:01:17 -04004058 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
4059
Michael Chan51f30782016-07-01 18:46:29 -04004060 req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
Michael Chanc0c050c2015-10-22 16:01:17 -04004061
4062 mutex_lock(&bp->hwrm_cmd_lock);
4063 for (i = 0; i < bp->cp_nr_rings; i++) {
4064 struct bnxt_napi *bnapi = bp->bnapi[i];
4065 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4066
4067 req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
4068
4069 rc = _hwrm_send_message(bp, &req, sizeof(req),
4070 HWRM_CMD_TIMEOUT);
4071 if (rc)
4072 break;
4073
4074 cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
4075
4076 bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
4077 }
4078 mutex_unlock(&bp->hwrm_cmd_lock);
4079 return 0;
4080}
4081
Michael Chancf6645f2016-06-13 02:25:28 -04004082static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
4083{
4084 struct hwrm_func_qcfg_input req = {0};
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004085 struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chancf6645f2016-06-13 02:25:28 -04004086 int rc;
4087
4088 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
4089 req.fid = cpu_to_le16(0xffff);
4090 mutex_lock(&bp->hwrm_cmd_lock);
4091 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4092 if (rc)
4093 goto func_qcfg_exit;
4094
4095#ifdef CONFIG_BNXT_SRIOV
4096 if (BNXT_VF(bp)) {
Michael Chancf6645f2016-06-13 02:25:28 -04004097 struct bnxt_vf_info *vf = &bp->vf;
4098
4099 vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
4100 }
4101#endif
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04004102 switch (resp->port_partition_type) {
4103 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
4104 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
4105 case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
4106 bp->port_partition_type = resp->port_partition_type;
4107 break;
4108 }
Michael Chancf6645f2016-06-13 02:25:28 -04004109
4110func_qcfg_exit:
4111 mutex_unlock(&bp->hwrm_cmd_lock);
4112 return rc;
4113}
4114
Michael Chan4a21b492015-12-27 18:19:26 -05004115int bnxt_hwrm_func_qcaps(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04004116{
4117 int rc = 0;
4118 struct hwrm_func_qcaps_input req = {0};
4119 struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
4120
4121 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
4122 req.fid = cpu_to_le16(0xffff);
4123
4124 mutex_lock(&bp->hwrm_cmd_lock);
4125 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4126 if (rc)
4127 goto hwrm_func_qcaps_exit;
4128
4129 if (BNXT_PF(bp)) {
4130 struct bnxt_pf_info *pf = &bp->pf;
4131
4132 pf->fw_fid = le16_to_cpu(resp->fid);
4133 pf->port_id = le16_to_cpu(resp->port_id);
Michael Chan87027db2016-07-01 18:46:28 -04004134 bp->dev->dev_port = pf->port_id;
Michael Chan11f15ed2016-04-05 14:08:55 -04004135 memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004136 memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
Michael Chanc0c050c2015-10-22 16:01:17 -04004137 pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4138 pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4139 pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004140 pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004141 pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4142 if (!pf->max_hw_ring_grps)
4143 pf->max_hw_ring_grps = pf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004144 pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4145 pf->max_vnics = le16_to_cpu(resp->max_vnics);
4146 pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
4147 pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
4148 pf->max_vfs = le16_to_cpu(resp->max_vfs);
4149 pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
4150 pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
4151 pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
4152 pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
4153 pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
4154 pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
4155 } else {
Michael Chan379a80a2015-10-23 15:06:19 -04004156#ifdef CONFIG_BNXT_SRIOV
Michael Chanc0c050c2015-10-22 16:01:17 -04004157 struct bnxt_vf_info *vf = &bp->vf;
4158
4159 vf->fw_fid = le16_to_cpu(resp->fid);
Michael Chan11f15ed2016-04-05 14:08:55 -04004160 memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05004161 if (is_valid_ether_addr(vf->mac_addr))
4162 /* overwrite netdev dev_adr with admin VF MAC */
4163 memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
4164 else
4165 random_ether_addr(bp->dev->dev_addr);
Michael Chanc0c050c2015-10-22 16:01:17 -04004166
4167 vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
4168 vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
4169 vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
4170 vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
Michael Chanb72d4a62015-12-27 18:19:27 -05004171 vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
4172 if (!vf->max_hw_ring_grps)
4173 vf->max_hw_ring_grps = vf->max_tx_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004174 vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
4175 vf->max_vnics = le16_to_cpu(resp->max_vnics);
4176 vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
Michael Chan379a80a2015-10-23 15:06:19 -04004177#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04004178 }
4179
4180 bp->tx_push_thresh = 0;
4181 if (resp->flags &
4182 cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
4183 bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
4184
4185hwrm_func_qcaps_exit:
4186 mutex_unlock(&bp->hwrm_cmd_lock);
4187 return rc;
4188}
4189
4190static int bnxt_hwrm_func_reset(struct bnxt *bp)
4191{
4192 struct hwrm_func_reset_input req = {0};
4193
4194 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
4195 req.enables = 0;
4196
4197 return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
4198}
4199
4200static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
4201{
4202 int rc = 0;
4203 struct hwrm_queue_qportcfg_input req = {0};
4204 struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
4205 u8 i, *qptr;
4206
4207 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
4208
4209 mutex_lock(&bp->hwrm_cmd_lock);
4210 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4211 if (rc)
4212 goto qportcfg_exit;
4213
4214 if (!resp->max_configurable_queues) {
4215 rc = -EINVAL;
4216 goto qportcfg_exit;
4217 }
4218 bp->max_tc = resp->max_configurable_queues;
4219 if (bp->max_tc > BNXT_MAX_QUEUE)
4220 bp->max_tc = BNXT_MAX_QUEUE;
4221
4222 qptr = &resp->queue_id0;
4223 for (i = 0; i < bp->max_tc; i++) {
4224 bp->q_info[i].queue_id = *qptr++;
4225 bp->q_info[i].queue_profile = *qptr++;
4226 }
4227
4228qportcfg_exit:
4229 mutex_unlock(&bp->hwrm_cmd_lock);
4230 return rc;
4231}
4232
4233static int bnxt_hwrm_ver_get(struct bnxt *bp)
4234{
4235 int rc;
4236 struct hwrm_ver_get_input req = {0};
4237 struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
4238
Michael Chane6ef2692016-03-28 19:46:05 -04004239 bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
Michael Chanc0c050c2015-10-22 16:01:17 -04004240 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
4241 req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
4242 req.hwrm_intf_min = HWRM_VERSION_MINOR;
4243 req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
4244 mutex_lock(&bp->hwrm_cmd_lock);
4245 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4246 if (rc)
4247 goto hwrm_ver_get_exit;
4248
4249 memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
4250
Michael Chan11f15ed2016-04-05 14:08:55 -04004251 bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
4252 resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
Michael Chanc1935542015-12-27 18:19:28 -05004253 if (resp->hwrm_intf_maj < 1) {
4254 netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
Michael Chanc0c050c2015-10-22 16:01:17 -04004255 resp->hwrm_intf_maj, resp->hwrm_intf_min,
Michael Chanc1935542015-12-27 18:19:28 -05004256 resp->hwrm_intf_upd);
4257 netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04004258 }
Rob Swindell3ebf6f02016-02-26 04:00:06 -05004259 snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
Michael Chanc0c050c2015-10-22 16:01:17 -04004260 resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
4261 resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
4262
Michael Chanff4fe812016-02-26 04:00:04 -05004263 bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
4264 if (!bp->hwrm_cmd_timeout)
4265 bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
4266
Michael Chane6ef2692016-03-28 19:46:05 -04004267 if (resp->hwrm_intf_maj >= 1)
4268 bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
4269
Michael Chan659c8052016-06-13 02:25:33 -04004270 bp->chip_num = le16_to_cpu(resp->chip_num);
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04004271 if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
4272 !resp->chip_metal)
4273 bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
Michael Chan659c8052016-06-13 02:25:33 -04004274
Michael Chanc0c050c2015-10-22 16:01:17 -04004275hwrm_ver_get_exit:
4276 mutex_unlock(&bp->hwrm_cmd_lock);
4277 return rc;
4278}
4279
Michael Chan3bdf56c2016-03-07 15:38:45 -05004280static int bnxt_hwrm_port_qstats(struct bnxt *bp)
4281{
4282 int rc;
4283 struct bnxt_pf_info *pf = &bp->pf;
4284 struct hwrm_port_qstats_input req = {0};
4285
4286 if (!(bp->flags & BNXT_FLAG_PORT_STATS))
4287 return 0;
4288
4289 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
4290 req.port_id = cpu_to_le16(pf->port_id);
4291 req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
4292 req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
4293 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
4294 return rc;
4295}
4296
Michael Chanc0c050c2015-10-22 16:01:17 -04004297static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
4298{
4299 if (bp->vxlan_port_cnt) {
4300 bnxt_hwrm_tunnel_dst_port_free(
4301 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
4302 }
4303 bp->vxlan_port_cnt = 0;
4304 if (bp->nge_port_cnt) {
4305 bnxt_hwrm_tunnel_dst_port_free(
4306 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
4307 }
4308 bp->nge_port_cnt = 0;
4309}
4310
4311static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
4312{
4313 int rc, i;
4314 u32 tpa_flags = 0;
4315
4316 if (set_tpa)
4317 tpa_flags = bp->flags & BNXT_FLAG_TPA;
4318 for (i = 0; i < bp->nr_vnics; i++) {
4319 rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
4320 if (rc) {
4321 netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
4322 rc, i);
4323 return rc;
4324 }
4325 }
4326 return 0;
4327}
4328
4329static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
4330{
4331 int i;
4332
4333 for (i = 0; i < bp->nr_vnics; i++)
4334 bnxt_hwrm_vnic_set_rss(bp, i, false);
4335}
4336
4337static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
4338 bool irq_re_init)
4339{
4340 if (bp->vnic_info) {
4341 bnxt_hwrm_clear_vnic_filter(bp);
4342 /* clear all RSS setting before free vnic ctx */
4343 bnxt_hwrm_clear_vnic_rss(bp);
4344 bnxt_hwrm_vnic_ctx_free(bp);
4345 /* before free the vnic, undo the vnic tpa settings */
4346 if (bp->flags & BNXT_FLAG_TPA)
4347 bnxt_set_tpa(bp, false);
4348 bnxt_hwrm_vnic_free(bp);
4349 }
4350 bnxt_hwrm_ring_free(bp, close_path);
4351 bnxt_hwrm_ring_grp_free(bp);
4352 if (irq_re_init) {
4353 bnxt_hwrm_stat_ctx_free(bp);
4354 bnxt_hwrm_free_tunnel_ports(bp);
4355 }
4356}
4357
4358static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
4359{
4360 int rc;
4361
4362 /* allocate context for vnic */
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004363 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
Michael Chanc0c050c2015-10-22 16:01:17 -04004364 if (rc) {
4365 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4366 vnic_id, rc);
4367 goto vnic_setup_err;
4368 }
4369 bp->rsscos_nr_ctxs++;
4370
Prashant Sreedharan94ce9ca2016-07-18 07:15:21 -04004371 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4372 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
4373 if (rc) {
4374 netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
4375 vnic_id, rc);
4376 goto vnic_setup_err;
4377 }
4378 bp->rsscos_nr_ctxs++;
4379 }
4380
Michael Chanc0c050c2015-10-22 16:01:17 -04004381 /* configure default vnic, ring grp */
4382 rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
4383 if (rc) {
4384 netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
4385 vnic_id, rc);
4386 goto vnic_setup_err;
4387 }
4388
4389 /* Enable RSS hashing on vnic */
4390 rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
4391 if (rc) {
4392 netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
4393 vnic_id, rc);
4394 goto vnic_setup_err;
4395 }
4396
4397 if (bp->flags & BNXT_FLAG_AGG_RINGS) {
4398 rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
4399 if (rc) {
4400 netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
4401 vnic_id, rc);
4402 }
4403 }
4404
4405vnic_setup_err:
4406 return rc;
4407}
4408
4409static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
4410{
4411#ifdef CONFIG_RFS_ACCEL
4412 int i, rc = 0;
4413
4414 for (i = 0; i < bp->rx_nr_rings; i++) {
4415 u16 vnic_id = i + 1;
4416 u16 ring_id = i;
4417
4418 if (vnic_id >= bp->nr_vnics)
4419 break;
4420
4421 bp->vnic_info[vnic_id].flags |= BNXT_VNIC_RFS_FLAG;
Michael Chanb81a90d2016-01-02 23:45:01 -05004422 rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
Michael Chanc0c050c2015-10-22 16:01:17 -04004423 if (rc) {
4424 netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
4425 vnic_id, rc);
4426 break;
4427 }
4428 rc = bnxt_setup_vnic(bp, vnic_id);
4429 if (rc)
4430 break;
4431 }
4432 return rc;
4433#else
4434 return 0;
4435#endif
4436}
4437
Michael Chan17c71ac2016-07-01 18:46:27 -04004438/* Allow PF and VF with default VLAN to be in promiscuous mode */
4439static bool bnxt_promisc_ok(struct bnxt *bp)
4440{
4441#ifdef CONFIG_BNXT_SRIOV
4442 if (BNXT_VF(bp) && !bp->vf.vlan)
4443 return false;
4444#endif
4445 return true;
4446}
4447
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004448static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
4449{
4450 unsigned int rc = 0;
4451
4452 rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
4453 if (rc) {
4454 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4455 rc);
4456 return rc;
4457 }
4458
4459 rc = bnxt_hwrm_vnic_cfg(bp, 1);
4460 if (rc) {
4461 netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
4462 rc);
4463 return rc;
4464 }
4465 return rc;
4466}
4467
Michael Chanb664f002015-12-02 01:54:08 -05004468static int bnxt_cfg_rx_mode(struct bnxt *);
Michael Chan7d2837d2016-05-04 16:56:44 -04004469static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
Michael Chanb664f002015-12-02 01:54:08 -05004470
Michael Chanc0c050c2015-10-22 16:01:17 -04004471static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
4472{
Michael Chan7d2837d2016-05-04 16:56:44 -04004473 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
Michael Chanc0c050c2015-10-22 16:01:17 -04004474 int rc = 0;
Prashant Sreedharan76595192016-07-18 07:15:22 -04004475 unsigned int rx_nr_rings = bp->rx_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004476
4477 if (irq_re_init) {
4478 rc = bnxt_hwrm_stat_ctx_alloc(bp);
4479 if (rc) {
4480 netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
4481 rc);
4482 goto err_out;
4483 }
4484 }
4485
4486 rc = bnxt_hwrm_ring_alloc(bp);
4487 if (rc) {
4488 netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
4489 goto err_out;
4490 }
4491
4492 rc = bnxt_hwrm_ring_grp_alloc(bp);
4493 if (rc) {
4494 netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
4495 goto err_out;
4496 }
4497
Prashant Sreedharan76595192016-07-18 07:15:22 -04004498 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4499 rx_nr_rings--;
4500
Michael Chanc0c050c2015-10-22 16:01:17 -04004501 /* default vnic 0 */
Prashant Sreedharan76595192016-07-18 07:15:22 -04004502 rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004503 if (rc) {
4504 netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
4505 goto err_out;
4506 }
4507
4508 rc = bnxt_setup_vnic(bp, 0);
4509 if (rc)
4510 goto err_out;
4511
4512 if (bp->flags & BNXT_FLAG_RFS) {
4513 rc = bnxt_alloc_rfs_vnics(bp);
4514 if (rc)
4515 goto err_out;
4516 }
4517
4518 if (bp->flags & BNXT_FLAG_TPA) {
4519 rc = bnxt_set_tpa(bp, true);
4520 if (rc)
4521 goto err_out;
4522 }
4523
4524 if (BNXT_VF(bp))
4525 bnxt_update_vf_mac(bp);
4526
4527 /* Filter for default vnic 0 */
4528 rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
4529 if (rc) {
4530 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
4531 goto err_out;
4532 }
Michael Chan7d2837d2016-05-04 16:56:44 -04004533 vnic->uc_filter_count = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004534
Michael Chan7d2837d2016-05-04 16:56:44 -04004535 vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
Michael Chanc0c050c2015-10-22 16:01:17 -04004536
Michael Chan17c71ac2016-07-01 18:46:27 -04004537 if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chan7d2837d2016-05-04 16:56:44 -04004538 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
4539
4540 if (bp->dev->flags & IFF_ALLMULTI) {
4541 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
4542 vnic->mc_list_count = 0;
4543 } else {
4544 u32 mask = 0;
4545
4546 bnxt_mc_list_updated(bp, &mask);
4547 vnic->rx_mask |= mask;
4548 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004549
Michael Chanb664f002015-12-02 01:54:08 -05004550 rc = bnxt_cfg_rx_mode(bp);
4551 if (rc)
Michael Chanc0c050c2015-10-22 16:01:17 -04004552 goto err_out;
Michael Chanc0c050c2015-10-22 16:01:17 -04004553
4554 rc = bnxt_hwrm_set_coal(bp);
4555 if (rc)
4556 netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
Prashant Sreedharandc52c6c2016-07-18 07:15:24 -04004557 rc);
4558
4559 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4560 rc = bnxt_setup_nitroa0_vnic(bp);
4561 if (rc)
4562 netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
4563 rc);
4564 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004565
Michael Chancf6645f2016-06-13 02:25:28 -04004566 if (BNXT_VF(bp)) {
4567 bnxt_hwrm_func_qcfg(bp);
4568 netdev_update_features(bp->dev);
4569 }
4570
Michael Chanc0c050c2015-10-22 16:01:17 -04004571 return 0;
4572
4573err_out:
4574 bnxt_hwrm_resource_free(bp, 0, true);
4575
4576 return rc;
4577}
4578
4579static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
4580{
4581 bnxt_hwrm_resource_free(bp, 1, irq_re_init);
4582 return 0;
4583}
4584
4585static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
4586{
4587 bnxt_init_rx_rings(bp);
4588 bnxt_init_tx_rings(bp);
4589 bnxt_init_ring_grps(bp, irq_re_init);
4590 bnxt_init_vnics(bp);
4591
4592 return bnxt_init_chip(bp, irq_re_init);
4593}
4594
4595static void bnxt_disable_int(struct bnxt *bp)
4596{
4597 int i;
4598
4599 if (!bp->bnapi)
4600 return;
4601
4602 for (i = 0; i < bp->cp_nr_rings; i++) {
4603 struct bnxt_napi *bnapi = bp->bnapi[i];
4604 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4605
4606 BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
4607 }
4608}
4609
4610static void bnxt_enable_int(struct bnxt *bp)
4611{
4612 int i;
4613
4614 atomic_set(&bp->intr_sem, 0);
4615 for (i = 0; i < bp->cp_nr_rings; i++) {
4616 struct bnxt_napi *bnapi = bp->bnapi[i];
4617 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
4618
4619 BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
4620 }
4621}
4622
4623static int bnxt_set_real_num_queues(struct bnxt *bp)
4624{
4625 int rc;
4626 struct net_device *dev = bp->dev;
4627
4628 rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
4629 if (rc)
4630 return rc;
4631
4632 rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
4633 if (rc)
4634 return rc;
4635
4636#ifdef CONFIG_RFS_ACCEL
Michael Chan45019a12015-12-27 18:19:22 -05004637 if (bp->flags & BNXT_FLAG_RFS)
Michael Chanc0c050c2015-10-22 16:01:17 -04004638 dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
Michael Chanc0c050c2015-10-22 16:01:17 -04004639#endif
4640
4641 return rc;
4642}
4643
Michael Chan6e6c5a52016-01-02 23:45:02 -05004644static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
4645 bool shared)
4646{
4647 int _rx = *rx, _tx = *tx;
4648
4649 if (shared) {
4650 *rx = min_t(int, _rx, max);
4651 *tx = min_t(int, _tx, max);
4652 } else {
4653 if (max < 2)
4654 return -ENOMEM;
4655
4656 while (_rx + _tx > max) {
4657 if (_rx > _tx && _rx > 1)
4658 _rx--;
4659 else if (_tx > 1)
4660 _tx--;
4661 }
4662 *rx = _rx;
4663 *tx = _tx;
4664 }
4665 return 0;
4666}
4667
Michael Chanc0c050c2015-10-22 16:01:17 -04004668static int bnxt_setup_msix(struct bnxt *bp)
4669{
4670 struct msix_entry *msix_ent;
4671 struct net_device *dev = bp->dev;
Michael Chan01657bc2016-01-02 23:45:03 -05004672 int i, total_vecs, rc = 0, min = 1;
Michael Chanc0c050c2015-10-22 16:01:17 -04004673 const int len = sizeof(bp->irq_tbl[0].name);
4674
4675 bp->flags &= ~BNXT_FLAG_USING_MSIX;
4676 total_vecs = bp->cp_nr_rings;
4677
4678 msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
4679 if (!msix_ent)
4680 return -ENOMEM;
4681
4682 for (i = 0; i < total_vecs; i++) {
4683 msix_ent[i].entry = i;
4684 msix_ent[i].vector = 0;
4685 }
4686
Michael Chan01657bc2016-01-02 23:45:03 -05004687 if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
4688 min = 2;
4689
4690 total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
Michael Chanc0c050c2015-10-22 16:01:17 -04004691 if (total_vecs < 0) {
4692 rc = -ENODEV;
4693 goto msix_setup_exit;
4694 }
4695
4696 bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
4697 if (bp->irq_tbl) {
4698 int tcs;
4699
4700 /* Trim rings based upon num of vectors allocated */
Michael Chan6e6c5a52016-01-02 23:45:02 -05004701 rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
Michael Chan01657bc2016-01-02 23:45:03 -05004702 total_vecs, min == 1);
Michael Chan6e6c5a52016-01-02 23:45:02 -05004703 if (rc)
4704 goto msix_setup_exit;
4705
Michael Chanc0c050c2015-10-22 16:01:17 -04004706 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4707 tcs = netdev_get_num_tc(dev);
4708 if (tcs > 1) {
4709 bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
4710 if (bp->tx_nr_rings_per_tc == 0) {
4711 netdev_reset_tc(dev);
4712 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
4713 } else {
4714 int i, off, count;
4715
4716 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
4717 for (i = 0; i < tcs; i++) {
4718 count = bp->tx_nr_rings_per_tc;
4719 off = i * count;
4720 netdev_set_tc_queue(dev, i, count, off);
4721 }
4722 }
4723 }
Michael Chan01657bc2016-01-02 23:45:03 -05004724 bp->cp_nr_rings = total_vecs;
Michael Chanc0c050c2015-10-22 16:01:17 -04004725
4726 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chan01657bc2016-01-02 23:45:03 -05004727 char *attr;
4728
Michael Chanc0c050c2015-10-22 16:01:17 -04004729 bp->irq_tbl[i].vector = msix_ent[i].vector;
Michael Chan01657bc2016-01-02 23:45:03 -05004730 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
4731 attr = "TxRx";
4732 else if (i < bp->rx_nr_rings)
4733 attr = "rx";
4734 else
4735 attr = "tx";
4736
Michael Chanc0c050c2015-10-22 16:01:17 -04004737 snprintf(bp->irq_tbl[i].name, len,
Michael Chan01657bc2016-01-02 23:45:03 -05004738 "%s-%s-%d", dev->name, attr, i);
Michael Chanc0c050c2015-10-22 16:01:17 -04004739 bp->irq_tbl[i].handler = bnxt_msix;
4740 }
4741 rc = bnxt_set_real_num_queues(bp);
4742 if (rc)
4743 goto msix_setup_exit;
4744 } else {
4745 rc = -ENOMEM;
4746 goto msix_setup_exit;
4747 }
4748 bp->flags |= BNXT_FLAG_USING_MSIX;
4749 kfree(msix_ent);
4750 return 0;
4751
4752msix_setup_exit:
4753 netdev_err(bp->dev, "bnxt_setup_msix err: %x\n", rc);
4754 pci_disable_msix(bp->pdev);
4755 kfree(msix_ent);
4756 return rc;
4757}
4758
4759static int bnxt_setup_inta(struct bnxt *bp)
4760{
4761 int rc;
4762 const int len = sizeof(bp->irq_tbl[0].name);
4763
4764 if (netdev_get_num_tc(bp->dev))
4765 netdev_reset_tc(bp->dev);
4766
4767 bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
4768 if (!bp->irq_tbl) {
4769 rc = -ENOMEM;
4770 return rc;
4771 }
4772 bp->rx_nr_rings = 1;
4773 bp->tx_nr_rings = 1;
4774 bp->cp_nr_rings = 1;
4775 bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
Michael Chan01657bc2016-01-02 23:45:03 -05004776 bp->flags |= BNXT_FLAG_SHARED_RINGS;
Michael Chanc0c050c2015-10-22 16:01:17 -04004777 bp->irq_tbl[0].vector = bp->pdev->irq;
4778 snprintf(bp->irq_tbl[0].name, len,
4779 "%s-%s-%d", bp->dev->name, "TxRx", 0);
4780 bp->irq_tbl[0].handler = bnxt_inta;
4781 rc = bnxt_set_real_num_queues(bp);
4782 return rc;
4783}
4784
4785static int bnxt_setup_int_mode(struct bnxt *bp)
4786{
4787 int rc = 0;
4788
4789 if (bp->flags & BNXT_FLAG_MSIX_CAP)
4790 rc = bnxt_setup_msix(bp);
4791
Michael Chan1fa72e22016-04-25 02:30:49 -04004792 if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004793 /* fallback to INTA */
4794 rc = bnxt_setup_inta(bp);
4795 }
4796 return rc;
4797}
4798
4799static void bnxt_free_irq(struct bnxt *bp)
4800{
4801 struct bnxt_irq *irq;
4802 int i;
4803
4804#ifdef CONFIG_RFS_ACCEL
4805 free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
4806 bp->dev->rx_cpu_rmap = NULL;
4807#endif
4808 if (!bp->irq_tbl)
4809 return;
4810
4811 for (i = 0; i < bp->cp_nr_rings; i++) {
4812 irq = &bp->irq_tbl[i];
4813 if (irq->requested)
4814 free_irq(irq->vector, bp->bnapi[i]);
4815 irq->requested = 0;
4816 }
4817 if (bp->flags & BNXT_FLAG_USING_MSIX)
4818 pci_disable_msix(bp->pdev);
4819 kfree(bp->irq_tbl);
4820 bp->irq_tbl = NULL;
4821}
4822
4823static int bnxt_request_irq(struct bnxt *bp)
4824{
Michael Chanb81a90d2016-01-02 23:45:01 -05004825 int i, j, rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04004826 unsigned long flags = 0;
4827#ifdef CONFIG_RFS_ACCEL
4828 struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
4829#endif
4830
4831 if (!(bp->flags & BNXT_FLAG_USING_MSIX))
4832 flags = IRQF_SHARED;
4833
Michael Chanb81a90d2016-01-02 23:45:01 -05004834 for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004835 struct bnxt_irq *irq = &bp->irq_tbl[i];
4836#ifdef CONFIG_RFS_ACCEL
Michael Chanb81a90d2016-01-02 23:45:01 -05004837 if (rmap && bp->bnapi[i]->rx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004838 rc = irq_cpu_rmap_add(rmap, irq->vector);
4839 if (rc)
4840 netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
Michael Chanb81a90d2016-01-02 23:45:01 -05004841 j);
4842 j++;
Michael Chanc0c050c2015-10-22 16:01:17 -04004843 }
4844#endif
4845 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
4846 bp->bnapi[i]);
4847 if (rc)
4848 break;
4849
4850 irq->requested = 1;
4851 }
4852 return rc;
4853}
4854
4855static void bnxt_del_napi(struct bnxt *bp)
4856{
4857 int i;
4858
4859 if (!bp->bnapi)
4860 return;
4861
4862 for (i = 0; i < bp->cp_nr_rings; i++) {
4863 struct bnxt_napi *bnapi = bp->bnapi[i];
4864
4865 napi_hash_del(&bnapi->napi);
4866 netif_napi_del(&bnapi->napi);
4867 }
4868}
4869
4870static void bnxt_init_napi(struct bnxt *bp)
4871{
4872 int i;
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04004873 unsigned int cp_nr_rings = bp->cp_nr_rings;
Michael Chanc0c050c2015-10-22 16:01:17 -04004874 struct bnxt_napi *bnapi;
4875
4876 if (bp->flags & BNXT_FLAG_USING_MSIX) {
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04004877 if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4878 cp_nr_rings--;
4879 for (i = 0; i < cp_nr_rings; i++) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004880 bnapi = bp->bnapi[i];
4881 netif_napi_add(bp->dev, &bnapi->napi,
4882 bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04004883 }
Prashant Sreedharan10bbdaf2016-07-18 07:15:23 -04004884 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
4885 bnapi = bp->bnapi[cp_nr_rings];
4886 netif_napi_add(bp->dev, &bnapi->napi,
4887 bnxt_poll_nitroa0, 64);
4888 napi_hash_add(&bnapi->napi);
4889 }
Michael Chanc0c050c2015-10-22 16:01:17 -04004890 } else {
4891 bnapi = bp->bnapi[0];
4892 netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
Michael Chanc0c050c2015-10-22 16:01:17 -04004893 }
4894}
4895
4896static void bnxt_disable_napi(struct bnxt *bp)
4897{
4898 int i;
4899
4900 if (!bp->bnapi)
4901 return;
4902
4903 for (i = 0; i < bp->cp_nr_rings; i++) {
4904 napi_disable(&bp->bnapi[i]->napi);
4905 bnxt_disable_poll(bp->bnapi[i]);
4906 }
4907}
4908
4909static void bnxt_enable_napi(struct bnxt *bp)
4910{
4911 int i;
4912
4913 for (i = 0; i < bp->cp_nr_rings; i++) {
Michael Chanfa7e2812016-05-10 19:18:00 -04004914 bp->bnapi[i]->in_reset = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04004915 bnxt_enable_poll(bp->bnapi[i]);
4916 napi_enable(&bp->bnapi[i]->napi);
4917 }
4918}
4919
4920static void bnxt_tx_disable(struct bnxt *bp)
4921{
4922 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004923 struct bnxt_tx_ring_info *txr;
4924 struct netdev_queue *txq;
4925
Michael Chanb6ab4b02016-01-02 23:44:59 -05004926 if (bp->tx_ring) {
Michael Chanc0c050c2015-10-22 16:01:17 -04004927 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004928 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004929 txq = netdev_get_tx_queue(bp->dev, i);
4930 __netif_tx_lock(txq, smp_processor_id());
4931 txr->dev_state = BNXT_DEV_STATE_CLOSING;
4932 __netif_tx_unlock(txq);
4933 }
4934 }
4935 /* Stop all TX queues */
4936 netif_tx_disable(bp->dev);
4937 netif_carrier_off(bp->dev);
4938}
4939
4940static void bnxt_tx_enable(struct bnxt *bp)
4941{
4942 int i;
Michael Chanc0c050c2015-10-22 16:01:17 -04004943 struct bnxt_tx_ring_info *txr;
4944 struct netdev_queue *txq;
4945
4946 for (i = 0; i < bp->tx_nr_rings; i++) {
Michael Chanb6ab4b02016-01-02 23:44:59 -05004947 txr = &bp->tx_ring[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04004948 txq = netdev_get_tx_queue(bp->dev, i);
4949 txr->dev_state = 0;
4950 }
4951 netif_tx_wake_all_queues(bp->dev);
4952 if (bp->link_info.link_up)
4953 netif_carrier_on(bp->dev);
4954}
4955
4956static void bnxt_report_link(struct bnxt *bp)
4957{
4958 if (bp->link_info.link_up) {
4959 const char *duplex;
4960 const char *flow_ctrl;
4961 u16 speed;
4962
4963 netif_carrier_on(bp->dev);
4964 if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
4965 duplex = "full";
4966 else
4967 duplex = "half";
4968 if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
4969 flow_ctrl = "ON - receive & transmit";
4970 else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
4971 flow_ctrl = "ON - transmit";
4972 else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
4973 flow_ctrl = "ON - receive";
4974 else
4975 flow_ctrl = "none";
4976 speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
4977 netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
4978 speed, duplex, flow_ctrl);
Michael Chan170ce012016-04-05 14:08:57 -04004979 if (bp->flags & BNXT_FLAG_EEE_CAP)
4980 netdev_info(bp->dev, "EEE is %s\n",
4981 bp->eee.eee_active ? "active" :
4982 "not active");
Michael Chanc0c050c2015-10-22 16:01:17 -04004983 } else {
4984 netif_carrier_off(bp->dev);
4985 netdev_err(bp->dev, "NIC Link is Down\n");
4986 }
4987}
4988
Michael Chan170ce012016-04-05 14:08:57 -04004989static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
4990{
4991 int rc = 0;
4992 struct hwrm_port_phy_qcaps_input req = {0};
4993 struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
Michael Chan93ed8112016-06-13 02:25:37 -04004994 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chan170ce012016-04-05 14:08:57 -04004995
4996 if (bp->hwrm_spec_code < 0x10201)
4997 return 0;
4998
4999 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
5000
5001 mutex_lock(&bp->hwrm_cmd_lock);
5002 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5003 if (rc)
5004 goto hwrm_phy_qcaps_exit;
5005
5006 if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
5007 struct ethtool_eee *eee = &bp->eee;
5008 u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
5009
5010 bp->flags |= BNXT_FLAG_EEE_CAP;
5011 eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5012 bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
5013 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
5014 bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
5015 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
5016 }
Michael Chan93ed8112016-06-13 02:25:37 -04005017 link_info->support_auto_speeds =
5018 le16_to_cpu(resp->supported_speeds_auto_mode);
Michael Chan170ce012016-04-05 14:08:57 -04005019
5020hwrm_phy_qcaps_exit:
5021 mutex_unlock(&bp->hwrm_cmd_lock);
5022 return rc;
5023}
5024
Michael Chanc0c050c2015-10-22 16:01:17 -04005025static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
5026{
5027 int rc = 0;
5028 struct bnxt_link_info *link_info = &bp->link_info;
5029 struct hwrm_port_phy_qcfg_input req = {0};
5030 struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
5031 u8 link_up = link_info->link_up;
5032
5033 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
5034
5035 mutex_lock(&bp->hwrm_cmd_lock);
5036 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5037 if (rc) {
5038 mutex_unlock(&bp->hwrm_cmd_lock);
5039 return rc;
5040 }
5041
5042 memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
5043 link_info->phy_link_status = resp->link;
5044 link_info->duplex = resp->duplex;
5045 link_info->pause = resp->pause;
5046 link_info->auto_mode = resp->auto_mode;
5047 link_info->auto_pause_setting = resp->auto_pause;
Michael Chan32773602016-03-07 15:38:42 -05005048 link_info->lp_pause = resp->link_partner_adv_pause;
Michael Chanc0c050c2015-10-22 16:01:17 -04005049 link_info->force_pause_setting = resp->force_pause;
Michael Chanc1935542015-12-27 18:19:28 -05005050 link_info->duplex_setting = resp->duplex;
Michael Chanc0c050c2015-10-22 16:01:17 -04005051 if (link_info->phy_link_status == BNXT_LINK_LINK)
5052 link_info->link_speed = le16_to_cpu(resp->link_speed);
5053 else
5054 link_info->link_speed = 0;
5055 link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
Michael Chanc0c050c2015-10-22 16:01:17 -04005056 link_info->support_speeds = le16_to_cpu(resp->support_speeds);
5057 link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
Michael Chan32773602016-03-07 15:38:42 -05005058 link_info->lp_auto_link_speeds =
5059 le16_to_cpu(resp->link_partner_adv_speeds);
Michael Chanc0c050c2015-10-22 16:01:17 -04005060 link_info->preemphasis = le32_to_cpu(resp->preemphasis);
5061 link_info->phy_ver[0] = resp->phy_maj;
5062 link_info->phy_ver[1] = resp->phy_min;
5063 link_info->phy_ver[2] = resp->phy_bld;
5064 link_info->media_type = resp->media_type;
Michael Chan03efbec2016-04-11 04:11:11 -04005065 link_info->phy_type = resp->phy_type;
Michael Chan11f15ed2016-04-05 14:08:55 -04005066 link_info->transceiver = resp->xcvr_pkg_type;
Michael Chan170ce012016-04-05 14:08:57 -04005067 link_info->phy_addr = resp->eee_config_phy_addr &
5068 PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
Ajit Khaparde42ee18f2016-05-15 03:04:44 -04005069 link_info->module_status = resp->module_status;
Michael Chanc0c050c2015-10-22 16:01:17 -04005070
Michael Chan170ce012016-04-05 14:08:57 -04005071 if (bp->flags & BNXT_FLAG_EEE_CAP) {
5072 struct ethtool_eee *eee = &bp->eee;
5073 u16 fw_speeds;
5074
5075 eee->eee_active = 0;
5076 if (resp->eee_config_phy_addr &
5077 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
5078 eee->eee_active = 1;
5079 fw_speeds = le16_to_cpu(
5080 resp->link_partner_adv_eee_link_speed_mask);
5081 eee->lp_advertised =
5082 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5083 }
5084
5085 /* Pull initial EEE config */
5086 if (!chng_link_state) {
5087 if (resp->eee_config_phy_addr &
5088 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
5089 eee->eee_enabled = 1;
5090
5091 fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
5092 eee->advertised =
5093 _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
5094
5095 if (resp->eee_config_phy_addr &
5096 PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
5097 __le32 tmr;
5098
5099 eee->tx_lpi_enabled = 1;
5100 tmr = resp->xcvr_identifier_type_tx_lpi_timer;
5101 eee->tx_lpi_timer = le32_to_cpu(tmr) &
5102 PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
5103 }
5104 }
5105 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005106 /* TODO: need to add more logic to report VF link */
5107 if (chng_link_state) {
5108 if (link_info->phy_link_status == BNXT_LINK_LINK)
5109 link_info->link_up = 1;
5110 else
5111 link_info->link_up = 0;
5112 if (link_up != link_info->link_up)
5113 bnxt_report_link(bp);
5114 } else {
5115 /* alwasy link down if not require to update link state */
5116 link_info->link_up = 0;
5117 }
5118 mutex_unlock(&bp->hwrm_cmd_lock);
5119 return 0;
5120}
5121
Michael Chan10289be2016-05-15 03:04:49 -04005122static void bnxt_get_port_module_status(struct bnxt *bp)
5123{
5124 struct bnxt_link_info *link_info = &bp->link_info;
5125 struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
5126 u8 module_status;
5127
5128 if (bnxt_update_link(bp, true))
5129 return;
5130
5131 module_status = link_info->module_status;
5132 switch (module_status) {
5133 case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
5134 case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
5135 case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
5136 netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
5137 bp->pf.port_id);
5138 if (bp->hwrm_spec_code >= 0x10201) {
5139 netdev_warn(bp->dev, "Module part number %s\n",
5140 resp->phy_vendor_partnumber);
5141 }
5142 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
5143 netdev_warn(bp->dev, "TX is disabled\n");
5144 if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
5145 netdev_warn(bp->dev, "SFP+ module is shutdown\n");
5146 }
5147}
5148
Michael Chanc0c050c2015-10-22 16:01:17 -04005149static void
5150bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
5151{
5152 if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
Michael Chanc9ee9512016-04-05 14:08:56 -04005153 if (bp->hwrm_spec_code >= 0x10201)
5154 req->auto_pause =
5155 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
Michael Chanc0c050c2015-10-22 16:01:17 -04005156 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5157 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
5158 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
Michael Chan49b5c7a2016-03-28 19:46:06 -04005159 req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
Michael Chanc0c050c2015-10-22 16:01:17 -04005160 req->enables |=
5161 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5162 } else {
5163 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
5164 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
5165 if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
5166 req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
5167 req->enables |=
5168 cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
Michael Chanc9ee9512016-04-05 14:08:56 -04005169 if (bp->hwrm_spec_code >= 0x10201) {
5170 req->auto_pause = req->force_pause;
5171 req->enables |= cpu_to_le32(
5172 PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
5173 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005174 }
5175}
5176
5177static void bnxt_hwrm_set_link_common(struct bnxt *bp,
5178 struct hwrm_port_phy_cfg_input *req)
5179{
5180 u8 autoneg = bp->link_info.autoneg;
5181 u16 fw_link_speed = bp->link_info.req_link_speed;
5182 u32 advertising = bp->link_info.advertising;
5183
5184 if (autoneg & BNXT_AUTONEG_SPEED) {
5185 req->auto_mode |=
Michael Chan11f15ed2016-04-05 14:08:55 -04005186 PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
Michael Chanc0c050c2015-10-22 16:01:17 -04005187
5188 req->enables |= cpu_to_le32(
5189 PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
5190 req->auto_link_speed_mask = cpu_to_le16(advertising);
5191
5192 req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
5193 req->flags |=
5194 cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
5195 } else {
5196 req->force_link_speed = cpu_to_le16(fw_link_speed);
5197 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
5198 }
5199
Michael Chanc0c050c2015-10-22 16:01:17 -04005200 /* tell chimp that the setting takes effect immediately */
5201 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
5202}
5203
5204int bnxt_hwrm_set_pause(struct bnxt *bp)
5205{
5206 struct hwrm_port_phy_cfg_input req = {0};
5207 int rc;
5208
5209 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5210 bnxt_hwrm_set_pause_common(bp, &req);
5211
5212 if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
5213 bp->link_info.force_link_chng)
5214 bnxt_hwrm_set_link_common(bp, &req);
5215
5216 mutex_lock(&bp->hwrm_cmd_lock);
5217 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5218 if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
5219 /* since changing of pause setting doesn't trigger any link
5220 * change event, the driver needs to update the current pause
5221 * result upon successfully return of the phy_cfg command
5222 */
5223 bp->link_info.pause =
5224 bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
5225 bp->link_info.auto_pause_setting = 0;
5226 if (!bp->link_info.force_link_chng)
5227 bnxt_report_link(bp);
5228 }
5229 bp->link_info.force_link_chng = false;
5230 mutex_unlock(&bp->hwrm_cmd_lock);
5231 return rc;
5232}
5233
Michael Chan939f7f02016-04-05 14:08:58 -04005234static void bnxt_hwrm_set_eee(struct bnxt *bp,
5235 struct hwrm_port_phy_cfg_input *req)
5236{
5237 struct ethtool_eee *eee = &bp->eee;
5238
5239 if (eee->eee_enabled) {
5240 u16 eee_speeds;
5241 u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
5242
5243 if (eee->tx_lpi_enabled)
5244 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
5245 else
5246 flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
5247
5248 req->flags |= cpu_to_le32(flags);
5249 eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
5250 req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
5251 req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
5252 } else {
5253 req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
5254 }
5255}
5256
5257int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
Michael Chanc0c050c2015-10-22 16:01:17 -04005258{
5259 struct hwrm_port_phy_cfg_input req = {0};
5260
5261 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5262 if (set_pause)
5263 bnxt_hwrm_set_pause_common(bp, &req);
5264
5265 bnxt_hwrm_set_link_common(bp, &req);
Michael Chan939f7f02016-04-05 14:08:58 -04005266
5267 if (set_eee)
5268 bnxt_hwrm_set_eee(bp, &req);
Michael Chanc0c050c2015-10-22 16:01:17 -04005269 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5270}
5271
Michael Chan33f7d552016-04-11 04:11:12 -04005272static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
5273{
5274 struct hwrm_port_phy_cfg_input req = {0};
5275
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04005276 if (!BNXT_SINGLE_PF(bp))
Michael Chan33f7d552016-04-11 04:11:12 -04005277 return 0;
5278
5279 if (pci_num_vf(bp->pdev))
5280 return 0;
5281
5282 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
5283 req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DOWN);
5284 return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
5285}
5286
Michael Chan939f7f02016-04-05 14:08:58 -04005287static bool bnxt_eee_config_ok(struct bnxt *bp)
5288{
5289 struct ethtool_eee *eee = &bp->eee;
5290 struct bnxt_link_info *link_info = &bp->link_info;
5291
5292 if (!(bp->flags & BNXT_FLAG_EEE_CAP))
5293 return true;
5294
5295 if (eee->eee_enabled) {
5296 u32 advertising =
5297 _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
5298
5299 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5300 eee->eee_enabled = 0;
5301 return false;
5302 }
5303 if (eee->advertised & ~advertising) {
5304 eee->advertised = advertising & eee->supported;
5305 return false;
5306 }
5307 }
5308 return true;
5309}
5310
Michael Chanc0c050c2015-10-22 16:01:17 -04005311static int bnxt_update_phy_setting(struct bnxt *bp)
5312{
5313 int rc;
5314 bool update_link = false;
5315 bool update_pause = false;
Michael Chan939f7f02016-04-05 14:08:58 -04005316 bool update_eee = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04005317 struct bnxt_link_info *link_info = &bp->link_info;
5318
5319 rc = bnxt_update_link(bp, true);
5320 if (rc) {
5321 netdev_err(bp->dev, "failed to update link (rc: %x)\n",
5322 rc);
5323 return rc;
5324 }
5325 if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
Michael Chanc9ee9512016-04-05 14:08:56 -04005326 (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
5327 link_info->req_flow_ctrl)
Michael Chanc0c050c2015-10-22 16:01:17 -04005328 update_pause = true;
5329 if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
5330 link_info->force_pause_setting != link_info->req_flow_ctrl)
5331 update_pause = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005332 if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
5333 if (BNXT_AUTO_MODE(link_info->auto_mode))
5334 update_link = true;
5335 if (link_info->req_link_speed != link_info->force_link_speed)
5336 update_link = true;
Michael Chande730182016-02-19 19:43:20 -05005337 if (link_info->req_duplex != link_info->duplex_setting)
5338 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005339 } else {
5340 if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
5341 update_link = true;
5342 if (link_info->advertising != link_info->auto_link_speeds)
5343 update_link = true;
Michael Chanc0c050c2015-10-22 16:01:17 -04005344 }
5345
Michael Chan939f7f02016-04-05 14:08:58 -04005346 if (!bnxt_eee_config_ok(bp))
5347 update_eee = true;
5348
Michael Chanc0c050c2015-10-22 16:01:17 -04005349 if (update_link)
Michael Chan939f7f02016-04-05 14:08:58 -04005350 rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
Michael Chanc0c050c2015-10-22 16:01:17 -04005351 else if (update_pause)
5352 rc = bnxt_hwrm_set_pause(bp);
5353 if (rc) {
5354 netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
5355 rc);
5356 return rc;
5357 }
5358
5359 return rc;
5360}
5361
Jeffrey Huang11809492015-11-05 16:25:49 -05005362/* Common routine to pre-map certain register block to different GRC window.
5363 * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
5364 * in PF and 3 windows in VF that can be customized to map in different
5365 * register blocks.
5366 */
5367static void bnxt_preset_reg_win(struct bnxt *bp)
5368{
5369 if (BNXT_PF(bp)) {
5370 /* CAG registers map to GRC window #4 */
5371 writel(BNXT_CAG_REG_BASE,
5372 bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
5373 }
5374}
5375
Michael Chanc0c050c2015-10-22 16:01:17 -04005376static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5377{
5378 int rc = 0;
5379
Jeffrey Huang11809492015-11-05 16:25:49 -05005380 bnxt_preset_reg_win(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005381 netif_carrier_off(bp->dev);
5382 if (irq_re_init) {
5383 rc = bnxt_setup_int_mode(bp);
5384 if (rc) {
5385 netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
5386 rc);
5387 return rc;
5388 }
5389 }
5390 if ((bp->flags & BNXT_FLAG_RFS) &&
5391 !(bp->flags & BNXT_FLAG_USING_MSIX)) {
5392 /* disable RFS if falling back to INTA */
5393 bp->dev->hw_features &= ~NETIF_F_NTUPLE;
5394 bp->flags &= ~BNXT_FLAG_RFS;
5395 }
5396
5397 rc = bnxt_alloc_mem(bp, irq_re_init);
5398 if (rc) {
5399 netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
5400 goto open_err_free_mem;
5401 }
5402
5403 if (irq_re_init) {
5404 bnxt_init_napi(bp);
5405 rc = bnxt_request_irq(bp);
5406 if (rc) {
5407 netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
5408 goto open_err;
5409 }
5410 }
5411
5412 bnxt_enable_napi(bp);
5413
5414 rc = bnxt_init_nic(bp, irq_re_init);
5415 if (rc) {
5416 netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
5417 goto open_err;
5418 }
5419
5420 if (link_re_init) {
5421 rc = bnxt_update_phy_setting(bp);
5422 if (rc)
Michael Chanba41d462016-02-19 19:43:21 -05005423 netdev_warn(bp->dev, "failed to update phy settings\n");
Michael Chanc0c050c2015-10-22 16:01:17 -04005424 }
5425
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07005426 if (irq_re_init)
Alexander Duyckad51b8e2016-06-16 12:21:19 -07005427 udp_tunnel_get_rx_info(bp->dev);
Michael Chanc0c050c2015-10-22 16:01:17 -04005428
Michael Chancaefe522015-12-09 19:35:42 -05005429 set_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005430 bnxt_enable_int(bp);
5431 /* Enable TX queues */
5432 bnxt_tx_enable(bp);
5433 mod_timer(&bp->timer, jiffies + bp->current_interval);
Michael Chan10289be2016-05-15 03:04:49 -04005434 /* Poll link status and check for SFP+ module status */
5435 bnxt_get_port_module_status(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005436
5437 return 0;
5438
5439open_err:
5440 bnxt_disable_napi(bp);
5441 bnxt_del_napi(bp);
5442
5443open_err_free_mem:
5444 bnxt_free_skbs(bp);
5445 bnxt_free_irq(bp);
5446 bnxt_free_mem(bp, true);
5447 return rc;
5448}
5449
5450/* rtnl_lock held */
5451int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5452{
5453 int rc = 0;
5454
5455 rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
5456 if (rc) {
5457 netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
5458 dev_close(bp->dev);
5459 }
5460 return rc;
5461}
5462
5463static int bnxt_open(struct net_device *dev)
5464{
5465 struct bnxt *bp = netdev_priv(dev);
5466 int rc = 0;
5467
Michael Chan2a5bedf2016-07-01 18:46:21 -04005468 if (!test_bit(BNXT_STATE_FN_RST_DONE, &bp->state)) {
5469 rc = bnxt_hwrm_func_reset(bp);
5470 if (rc) {
5471 netdev_err(bp->dev, "hwrm chip reset failure rc: %x\n",
5472 rc);
5473 rc = -EBUSY;
5474 return rc;
5475 }
5476 /* Do func_reset during the 1st PF open only to prevent killing
5477 * the VFs when the PF is brought down and up.
5478 */
5479 if (BNXT_PF(bp))
5480 set_bit(BNXT_STATE_FN_RST_DONE, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04005481 }
5482 return __bnxt_open_nic(bp, true, true);
5483}
5484
5485static void bnxt_disable_int_sync(struct bnxt *bp)
5486{
5487 int i;
5488
5489 atomic_inc(&bp->intr_sem);
5490 if (!netif_running(bp->dev))
5491 return;
5492
5493 bnxt_disable_int(bp);
5494 for (i = 0; i < bp->cp_nr_rings; i++)
5495 synchronize_irq(bp->irq_tbl[i].vector);
5496}
5497
5498int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
5499{
5500 int rc = 0;
5501
5502#ifdef CONFIG_BNXT_SRIOV
5503 if (bp->sriov_cfg) {
5504 rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
5505 !bp->sriov_cfg,
5506 BNXT_SRIOV_CFG_WAIT_TMO);
5507 if (rc)
5508 netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
5509 }
5510#endif
5511 /* Change device state to avoid TX queue wake up's */
5512 bnxt_tx_disable(bp);
5513
Michael Chancaefe522015-12-09 19:35:42 -05005514 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chan4cebdce2015-12-09 19:35:43 -05005515 smp_mb__after_atomic();
5516 while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
5517 msleep(20);
Michael Chanc0c050c2015-10-22 16:01:17 -04005518
5519 /* Flush rings before disabling interrupts */
5520 bnxt_shutdown_nic(bp, irq_re_init);
5521
5522 /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
5523
5524 bnxt_disable_napi(bp);
5525 bnxt_disable_int_sync(bp);
5526 del_timer_sync(&bp->timer);
5527 bnxt_free_skbs(bp);
5528
5529 if (irq_re_init) {
5530 bnxt_free_irq(bp);
5531 bnxt_del_napi(bp);
5532 }
5533 bnxt_free_mem(bp, irq_re_init);
5534 return rc;
5535}
5536
5537static int bnxt_close(struct net_device *dev)
5538{
5539 struct bnxt *bp = netdev_priv(dev);
5540
5541 bnxt_close_nic(bp, true, true);
Michael Chan33f7d552016-04-11 04:11:12 -04005542 bnxt_hwrm_shutdown_link(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04005543 return 0;
5544}
5545
5546/* rtnl_lock held */
5547static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
5548{
5549 switch (cmd) {
5550 case SIOCGMIIPHY:
5551 /* fallthru */
5552 case SIOCGMIIREG: {
5553 if (!netif_running(dev))
5554 return -EAGAIN;
5555
5556 return 0;
5557 }
5558
5559 case SIOCSMIIREG:
5560 if (!netif_running(dev))
5561 return -EAGAIN;
5562
5563 return 0;
5564
5565 default:
5566 /* do nothing */
5567 break;
5568 }
5569 return -EOPNOTSUPP;
5570}
5571
5572static struct rtnl_link_stats64 *
5573bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
5574{
5575 u32 i;
5576 struct bnxt *bp = netdev_priv(dev);
5577
5578 memset(stats, 0, sizeof(struct rtnl_link_stats64));
5579
5580 if (!bp->bnapi)
5581 return stats;
5582
5583 /* TODO check if we need to synchronize with bnxt_close path */
5584 for (i = 0; i < bp->cp_nr_rings; i++) {
5585 struct bnxt_napi *bnapi = bp->bnapi[i];
5586 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5587 struct ctx_hw_stats *hw_stats = cpr->hw_stats;
5588
5589 stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
5590 stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
5591 stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
5592
5593 stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
5594 stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
5595 stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
5596
5597 stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
5598 stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
5599 stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
5600
5601 stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
5602 stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
5603 stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
5604
5605 stats->rx_missed_errors +=
5606 le64_to_cpu(hw_stats->rx_discard_pkts);
5607
5608 stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
5609
Michael Chanc0c050c2015-10-22 16:01:17 -04005610 stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
5611 }
5612
Michael Chan9947f832016-03-07 15:38:46 -05005613 if (bp->flags & BNXT_FLAG_PORT_STATS) {
5614 struct rx_port_stats *rx = bp->hw_rx_port_stats;
5615 struct tx_port_stats *tx = bp->hw_tx_port_stats;
5616
5617 stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
5618 stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
5619 stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
5620 le64_to_cpu(rx->rx_ovrsz_frames) +
5621 le64_to_cpu(rx->rx_runt_frames);
5622 stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
5623 le64_to_cpu(rx->rx_jbr_frames);
5624 stats->collisions = le64_to_cpu(tx->tx_total_collisions);
5625 stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
5626 stats->tx_errors = le64_to_cpu(tx->tx_err);
5627 }
5628
Michael Chanc0c050c2015-10-22 16:01:17 -04005629 return stats;
5630}
5631
5632static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
5633{
5634 struct net_device *dev = bp->dev;
5635 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5636 struct netdev_hw_addr *ha;
5637 u8 *haddr;
5638 int mc_count = 0;
5639 bool update = false;
5640 int off = 0;
5641
5642 netdev_for_each_mc_addr(ha, dev) {
5643 if (mc_count >= BNXT_MAX_MC_ADDRS) {
5644 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5645 vnic->mc_list_count = 0;
5646 return false;
5647 }
5648 haddr = ha->addr;
5649 if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
5650 memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
5651 update = true;
5652 }
5653 off += ETH_ALEN;
5654 mc_count++;
5655 }
5656 if (mc_count)
5657 *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
5658
5659 if (mc_count != vnic->mc_list_count) {
5660 vnic->mc_list_count = mc_count;
5661 update = true;
5662 }
5663 return update;
5664}
5665
5666static bool bnxt_uc_list_updated(struct bnxt *bp)
5667{
5668 struct net_device *dev = bp->dev;
5669 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5670 struct netdev_hw_addr *ha;
5671 int off = 0;
5672
5673 if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
5674 return true;
5675
5676 netdev_for_each_uc_addr(ha, dev) {
5677 if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
5678 return true;
5679
5680 off += ETH_ALEN;
5681 }
5682 return false;
5683}
5684
5685static void bnxt_set_rx_mode(struct net_device *dev)
5686{
5687 struct bnxt *bp = netdev_priv(dev);
5688 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5689 u32 mask = vnic->rx_mask;
5690 bool mc_update = false;
5691 bool uc_update;
5692
5693 if (!netif_running(dev))
5694 return;
5695
5696 mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
5697 CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
5698 CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
5699
Michael Chan17c71ac2016-07-01 18:46:27 -04005700 if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04005701 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5702
5703 uc_update = bnxt_uc_list_updated(bp);
5704
5705 if (dev->flags & IFF_ALLMULTI) {
5706 mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
5707 vnic->mc_list_count = 0;
5708 } else {
5709 mc_update = bnxt_mc_list_updated(bp, &mask);
5710 }
5711
5712 if (mask != vnic->rx_mask || uc_update || mc_update) {
5713 vnic->rx_mask = mask;
5714
5715 set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
5716 schedule_work(&bp->sp_task);
5717 }
5718}
5719
Michael Chanb664f002015-12-02 01:54:08 -05005720static int bnxt_cfg_rx_mode(struct bnxt *bp)
Michael Chanc0c050c2015-10-22 16:01:17 -04005721{
5722 struct net_device *dev = bp->dev;
5723 struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
5724 struct netdev_hw_addr *ha;
5725 int i, off = 0, rc;
5726 bool uc_update;
5727
5728 netif_addr_lock_bh(dev);
5729 uc_update = bnxt_uc_list_updated(bp);
5730 netif_addr_unlock_bh(dev);
5731
5732 if (!uc_update)
5733 goto skip_uc;
5734
5735 mutex_lock(&bp->hwrm_cmd_lock);
5736 for (i = 1; i < vnic->uc_filter_count; i++) {
5737 struct hwrm_cfa_l2_filter_free_input req = {0};
5738
5739 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
5740 -1);
5741
5742 req.l2_filter_id = vnic->fw_l2_filter_id[i];
5743
5744 rc = _hwrm_send_message(bp, &req, sizeof(req),
5745 HWRM_CMD_TIMEOUT);
5746 }
5747 mutex_unlock(&bp->hwrm_cmd_lock);
5748
5749 vnic->uc_filter_count = 1;
5750
5751 netif_addr_lock_bh(dev);
5752 if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
5753 vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
5754 } else {
5755 netdev_for_each_uc_addr(ha, dev) {
5756 memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
5757 off += ETH_ALEN;
5758 vnic->uc_filter_count++;
5759 }
5760 }
5761 netif_addr_unlock_bh(dev);
5762
5763 for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
5764 rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
5765 if (rc) {
5766 netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
5767 rc);
5768 vnic->uc_filter_count = i;
Michael Chanb664f002015-12-02 01:54:08 -05005769 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005770 }
5771 }
5772
5773skip_uc:
5774 rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
5775 if (rc)
5776 netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
5777 rc);
Michael Chanb664f002015-12-02 01:54:08 -05005778
5779 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04005780}
5781
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005782static bool bnxt_rfs_capable(struct bnxt *bp)
5783{
5784#ifdef CONFIG_RFS_ACCEL
5785 struct bnxt_pf_info *pf = &bp->pf;
5786 int vnics;
5787
5788 if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
5789 return false;
5790
5791 vnics = 1 + bp->rx_nr_rings;
5792 if (vnics > pf->max_rsscos_ctxs || vnics > pf->max_vnics)
5793 return false;
5794
5795 return true;
5796#else
5797 return false;
5798#endif
5799}
5800
Michael Chanc0c050c2015-10-22 16:01:17 -04005801static netdev_features_t bnxt_fix_features(struct net_device *dev,
5802 netdev_features_t features)
5803{
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005804 struct bnxt *bp = netdev_priv(dev);
5805
5806 if (!bnxt_rfs_capable(bp))
5807 features &= ~NETIF_F_NTUPLE;
Michael Chan5a9f6b22016-06-06 02:37:15 -04005808
5809 /* Both CTAG and STAG VLAN accelaration on the RX side have to be
5810 * turned on or off together.
5811 */
5812 if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
5813 (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
5814 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
5815 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
5816 NETIF_F_HW_VLAN_STAG_RX);
5817 else
5818 features |= NETIF_F_HW_VLAN_CTAG_RX |
5819 NETIF_F_HW_VLAN_STAG_RX;
5820 }
Michael Chancf6645f2016-06-13 02:25:28 -04005821#ifdef CONFIG_BNXT_SRIOV
5822 if (BNXT_VF(bp)) {
5823 if (bp->vf.vlan) {
5824 features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
5825 NETIF_F_HW_VLAN_STAG_RX);
5826 }
5827 }
5828#endif
Michael Chanc0c050c2015-10-22 16:01:17 -04005829 return features;
5830}
5831
5832static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
5833{
5834 struct bnxt *bp = netdev_priv(dev);
5835 u32 flags = bp->flags;
5836 u32 changes;
5837 int rc = 0;
5838 bool re_init = false;
5839 bool update_tpa = false;
5840
5841 flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04005842 if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04005843 flags |= BNXT_FLAG_GRO;
5844 if (features & NETIF_F_LRO)
5845 flags |= BNXT_FLAG_LRO;
5846
5847 if (features & NETIF_F_HW_VLAN_CTAG_RX)
5848 flags |= BNXT_FLAG_STRIP_VLAN;
5849
5850 if (features & NETIF_F_NTUPLE)
5851 flags |= BNXT_FLAG_RFS;
5852
5853 changes = flags ^ bp->flags;
5854 if (changes & BNXT_FLAG_TPA) {
5855 update_tpa = true;
5856 if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
5857 (flags & BNXT_FLAG_TPA) == 0)
5858 re_init = true;
5859 }
5860
5861 if (changes & ~BNXT_FLAG_TPA)
5862 re_init = true;
5863
5864 if (flags != bp->flags) {
5865 u32 old_flags = bp->flags;
5866
5867 bp->flags = flags;
5868
Michael Chan2bcfa6f2015-12-27 18:19:24 -05005869 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04005870 if (update_tpa)
5871 bnxt_set_ring_params(bp);
5872 return rc;
5873 }
5874
5875 if (re_init) {
5876 bnxt_close_nic(bp, false, false);
5877 if (update_tpa)
5878 bnxt_set_ring_params(bp);
5879
5880 return bnxt_open_nic(bp, false, false);
5881 }
5882 if (update_tpa) {
5883 rc = bnxt_set_tpa(bp,
5884 (flags & BNXT_FLAG_TPA) ?
5885 true : false);
5886 if (rc)
5887 bp->flags = old_flags;
5888 }
5889 }
5890 return rc;
5891}
5892
Michael Chan9f554592016-01-02 23:44:58 -05005893static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
5894{
Michael Chanb6ab4b02016-01-02 23:44:59 -05005895 struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05005896 int i = bnapi->index;
5897
Michael Chan3b2b7d92016-01-02 23:45:00 -05005898 if (!txr)
5899 return;
5900
Michael Chan9f554592016-01-02 23:44:58 -05005901 netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
5902 i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
5903 txr->tx_cons);
5904}
5905
5906static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
5907{
Michael Chanb6ab4b02016-01-02 23:44:59 -05005908 struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
Michael Chan9f554592016-01-02 23:44:58 -05005909 int i = bnapi->index;
5910
Michael Chan3b2b7d92016-01-02 23:45:00 -05005911 if (!rxr)
5912 return;
5913
Michael Chan9f554592016-01-02 23:44:58 -05005914 netdev_info(bnapi->bp->dev, "[%d]: rx{fw_ring: %d prod: %x} rx_agg{fw_ring: %d agg_prod: %x sw_agg_prod: %x}\n",
5915 i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
5916 rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
5917 rxr->rx_sw_agg_prod);
5918}
5919
5920static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
5921{
5922 struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5923 int i = bnapi->index;
5924
5925 netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
5926 i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
5927}
5928
Michael Chanc0c050c2015-10-22 16:01:17 -04005929static void bnxt_dbg_dump_states(struct bnxt *bp)
5930{
5931 int i;
5932 struct bnxt_napi *bnapi;
Michael Chanc0c050c2015-10-22 16:01:17 -04005933
5934 for (i = 0; i < bp->cp_nr_rings; i++) {
5935 bnapi = bp->bnapi[i];
Michael Chanc0c050c2015-10-22 16:01:17 -04005936 if (netif_msg_drv(bp)) {
Michael Chan9f554592016-01-02 23:44:58 -05005937 bnxt_dump_tx_sw_state(bnapi);
5938 bnxt_dump_rx_sw_state(bnapi);
5939 bnxt_dump_cp_sw_state(bnapi);
Michael Chanc0c050c2015-10-22 16:01:17 -04005940 }
5941 }
5942}
5943
Michael Chan6988bd92016-06-13 02:25:29 -04005944static void bnxt_reset_task(struct bnxt *bp, bool silent)
Michael Chanc0c050c2015-10-22 16:01:17 -04005945{
Michael Chan6988bd92016-06-13 02:25:29 -04005946 if (!silent)
5947 bnxt_dbg_dump_states(bp);
Michael Chan028de142015-12-09 19:35:44 -05005948 if (netif_running(bp->dev)) {
5949 bnxt_close_nic(bp, false, false);
5950 bnxt_open_nic(bp, false, false);
5951 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005952}
5953
5954static void bnxt_tx_timeout(struct net_device *dev)
5955{
5956 struct bnxt *bp = netdev_priv(dev);
5957
5958 netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
5959 set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
5960 schedule_work(&bp->sp_task);
5961}
5962
5963#ifdef CONFIG_NET_POLL_CONTROLLER
5964static void bnxt_poll_controller(struct net_device *dev)
5965{
5966 struct bnxt *bp = netdev_priv(dev);
5967 int i;
5968
5969 for (i = 0; i < bp->cp_nr_rings; i++) {
5970 struct bnxt_irq *irq = &bp->irq_tbl[i];
5971
5972 disable_irq(irq->vector);
5973 irq->handler(irq->vector, bp->bnapi[i]);
5974 enable_irq(irq->vector);
5975 }
5976}
5977#endif
5978
5979static void bnxt_timer(unsigned long data)
5980{
5981 struct bnxt *bp = (struct bnxt *)data;
5982 struct net_device *dev = bp->dev;
5983
5984 if (!netif_running(dev))
5985 return;
5986
5987 if (atomic_read(&bp->intr_sem) != 0)
5988 goto bnxt_restart_timer;
5989
Michael Chan3bdf56c2016-03-07 15:38:45 -05005990 if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
5991 set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
5992 schedule_work(&bp->sp_task);
5993 }
Michael Chanc0c050c2015-10-22 16:01:17 -04005994bnxt_restart_timer:
5995 mod_timer(&bp->timer, jiffies + bp->current_interval);
5996}
5997
Michael Chan6988bd92016-06-13 02:25:29 -04005998/* Only called from bnxt_sp_task() */
5999static void bnxt_reset(struct bnxt *bp, bool silent)
6000{
6001 /* bnxt_reset_task() calls bnxt_close_nic() which waits
6002 * for BNXT_STATE_IN_SP_TASK to clear.
6003 * If there is a parallel dev_close(), bnxt_close() may be holding
6004 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
6005 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
6006 */
6007 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6008 rtnl_lock();
6009 if (test_bit(BNXT_STATE_OPEN, &bp->state))
6010 bnxt_reset_task(bp, silent);
6011 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6012 rtnl_unlock();
6013}
6014
Michael Chanc0c050c2015-10-22 16:01:17 -04006015static void bnxt_cfg_ntp_filters(struct bnxt *);
6016
6017static void bnxt_sp_task(struct work_struct *work)
6018{
6019 struct bnxt *bp = container_of(work, struct bnxt, sp_task);
6020 int rc;
6021
Michael Chan4cebdce2015-12-09 19:35:43 -05006022 set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
6023 smp_mb__after_atomic();
6024 if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
6025 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006026 return;
Michael Chan4cebdce2015-12-09 19:35:43 -05006027 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006028
6029 if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
6030 bnxt_cfg_rx_mode(bp);
6031
6032 if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
6033 bnxt_cfg_ntp_filters(bp);
6034 if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
6035 rc = bnxt_update_link(bp, true);
6036 if (rc)
6037 netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
6038 rc);
6039 }
6040 if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
6041 bnxt_hwrm_exec_fwd_req(bp);
6042 if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6043 bnxt_hwrm_tunnel_dst_port_alloc(
6044 bp, bp->vxlan_port,
6045 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6046 }
6047 if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6048 bnxt_hwrm_tunnel_dst_port_free(
6049 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
6050 }
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006051 if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
6052 bnxt_hwrm_tunnel_dst_port_alloc(
6053 bp, bp->nge_port,
6054 TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6055 }
6056 if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
6057 bnxt_hwrm_tunnel_dst_port_free(
6058 bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
6059 }
Michael Chan6988bd92016-06-13 02:25:29 -04006060 if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
6061 bnxt_reset(bp, false);
Michael Chan4cebdce2015-12-09 19:35:43 -05006062
Michael Chanfc0f1922016-06-13 02:25:30 -04006063 if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
6064 bnxt_reset(bp, true);
6065
Michael Chan4bb13ab2016-04-05 14:09:01 -04006066 if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event))
Michael Chan10289be2016-05-15 03:04:49 -04006067 bnxt_get_port_module_status(bp);
Michael Chan4bb13ab2016-04-05 14:09:01 -04006068
Michael Chan3bdf56c2016-03-07 15:38:45 -05006069 if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
6070 bnxt_hwrm_port_qstats(bp);
6071
Michael Chan4cebdce2015-12-09 19:35:43 -05006072 smp_mb__before_atomic();
6073 clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006074}
6075
6076static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
6077{
6078 int rc;
6079 struct bnxt *bp = netdev_priv(dev);
6080
6081 SET_NETDEV_DEV(dev, &pdev->dev);
6082
6083 /* enable device (incl. PCI PM wakeup), and bus-mastering */
6084 rc = pci_enable_device(pdev);
6085 if (rc) {
6086 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
6087 goto init_err;
6088 }
6089
6090 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
6091 dev_err(&pdev->dev,
6092 "Cannot find PCI device base address, aborting\n");
6093 rc = -ENODEV;
6094 goto init_err_disable;
6095 }
6096
6097 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
6098 if (rc) {
6099 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
6100 goto init_err_disable;
6101 }
6102
6103 if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
6104 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
6105 dev_err(&pdev->dev, "System does not support DMA, aborting\n");
6106 goto init_err_disable;
6107 }
6108
6109 pci_set_master(pdev);
6110
6111 bp->dev = dev;
6112 bp->pdev = pdev;
6113
6114 bp->bar0 = pci_ioremap_bar(pdev, 0);
6115 if (!bp->bar0) {
6116 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
6117 rc = -ENOMEM;
6118 goto init_err_release;
6119 }
6120
6121 bp->bar1 = pci_ioremap_bar(pdev, 2);
6122 if (!bp->bar1) {
6123 dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
6124 rc = -ENOMEM;
6125 goto init_err_release;
6126 }
6127
6128 bp->bar2 = pci_ioremap_bar(pdev, 4);
6129 if (!bp->bar2) {
6130 dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
6131 rc = -ENOMEM;
6132 goto init_err_release;
6133 }
6134
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006135 pci_enable_pcie_error_reporting(pdev);
6136
Michael Chanc0c050c2015-10-22 16:01:17 -04006137 INIT_WORK(&bp->sp_task, bnxt_sp_task);
6138
6139 spin_lock_init(&bp->ntp_fltr_lock);
6140
6141 bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
6142 bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
6143
Michael Chandfb5b892016-02-26 04:00:01 -05006144 /* tick values in micro seconds */
Michael Chandfc9c942016-02-26 04:00:03 -05006145 bp->rx_coal_ticks = 12;
6146 bp->rx_coal_bufs = 30;
Michael Chandfb5b892016-02-26 04:00:01 -05006147 bp->rx_coal_ticks_irq = 1;
6148 bp->rx_coal_bufs_irq = 2;
Michael Chanc0c050c2015-10-22 16:01:17 -04006149
Michael Chandfc9c942016-02-26 04:00:03 -05006150 bp->tx_coal_ticks = 25;
6151 bp->tx_coal_bufs = 30;
6152 bp->tx_coal_ticks_irq = 2;
6153 bp->tx_coal_bufs_irq = 2;
6154
Michael Chan51f30782016-07-01 18:46:29 -04006155 bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
6156
Michael Chanc0c050c2015-10-22 16:01:17 -04006157 init_timer(&bp->timer);
6158 bp->timer.data = (unsigned long)bp;
6159 bp->timer.function = bnxt_timer;
6160 bp->current_interval = BNXT_TIMER_INTERVAL;
6161
Michael Chancaefe522015-12-09 19:35:42 -05006162 clear_bit(BNXT_STATE_OPEN, &bp->state);
Michael Chanc0c050c2015-10-22 16:01:17 -04006163
6164 return 0;
6165
6166init_err_release:
6167 if (bp->bar2) {
6168 pci_iounmap(pdev, bp->bar2);
6169 bp->bar2 = NULL;
6170 }
6171
6172 if (bp->bar1) {
6173 pci_iounmap(pdev, bp->bar1);
6174 bp->bar1 = NULL;
6175 }
6176
6177 if (bp->bar0) {
6178 pci_iounmap(pdev, bp->bar0);
6179 bp->bar0 = NULL;
6180 }
6181
6182 pci_release_regions(pdev);
6183
6184init_err_disable:
6185 pci_disable_device(pdev);
6186
6187init_err:
6188 return rc;
6189}
6190
6191/* rtnl_lock held */
6192static int bnxt_change_mac_addr(struct net_device *dev, void *p)
6193{
6194 struct sockaddr *addr = p;
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006195 struct bnxt *bp = netdev_priv(dev);
6196 int rc = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006197
6198 if (!is_valid_ether_addr(addr->sa_data))
6199 return -EADDRNOTAVAIL;
6200
Michael Chan84c33dd2016-04-11 04:11:13 -04006201 rc = bnxt_approve_mac(bp, addr->sa_data);
6202 if (rc)
6203 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006204
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006205 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
6206 return 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006207
Jeffrey Huang1fc2cfd2015-12-02 01:54:06 -05006208 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
6209 if (netif_running(dev)) {
6210 bnxt_close_nic(bp, false, false);
6211 rc = bnxt_open_nic(bp, false, false);
6212 }
6213
6214 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006215}
6216
6217/* rtnl_lock held */
6218static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
6219{
6220 struct bnxt *bp = netdev_priv(dev);
6221
Vasundhara Volamdc7aadb2016-07-01 18:46:26 -04006222 if (new_mtu < 60 || new_mtu > 9500)
Michael Chanc0c050c2015-10-22 16:01:17 -04006223 return -EINVAL;
6224
6225 if (netif_running(dev))
6226 bnxt_close_nic(bp, false, false);
6227
6228 dev->mtu = new_mtu;
6229 bnxt_set_ring_params(bp);
6230
6231 if (netif_running(dev))
6232 return bnxt_open_nic(bp, false, false);
6233
6234 return 0;
6235}
6236
John Fastabend16e5cc62016-02-16 21:16:43 -08006237static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
6238 struct tc_to_netdev *ntc)
Michael Chanc0c050c2015-10-22 16:01:17 -04006239{
6240 struct bnxt *bp = netdev_priv(dev);
John Fastabend16e5cc62016-02-16 21:16:43 -08006241 u8 tc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006242
John Fastabend5eb4dce2016-02-29 11:26:13 -08006243 if (ntc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -08006244 return -EINVAL;
6245
John Fastabend16e5cc62016-02-16 21:16:43 -08006246 tc = ntc->tc;
6247
Michael Chanc0c050c2015-10-22 16:01:17 -04006248 if (tc > bp->max_tc) {
6249 netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
6250 tc, bp->max_tc);
6251 return -EINVAL;
6252 }
6253
6254 if (netdev_get_num_tc(dev) == tc)
6255 return 0;
6256
6257 if (tc) {
Michael Chan6e6c5a52016-01-02 23:45:02 -05006258 int max_rx_rings, max_tx_rings, rc;
Michael Chan01657bc2016-01-02 23:45:03 -05006259 bool sh = false;
Michael Chanc0c050c2015-10-22 16:01:17 -04006260
Michael Chan01657bc2016-01-02 23:45:03 -05006261 if (bp->flags & BNXT_FLAG_SHARED_RINGS)
6262 sh = true;
6263
6264 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006265 if (rc || bp->tx_nr_rings_per_tc * tc > max_tx_rings)
Michael Chanc0c050c2015-10-22 16:01:17 -04006266 return -ENOMEM;
6267 }
6268
6269 /* Needs to close the device and do hw resource re-allocations */
6270 if (netif_running(bp->dev))
6271 bnxt_close_nic(bp, true, false);
6272
6273 if (tc) {
6274 bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
6275 netdev_set_num_tc(dev, tc);
6276 } else {
6277 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6278 netdev_reset_tc(dev);
6279 }
6280 bp->cp_nr_rings = max_t(int, bp->tx_nr_rings, bp->rx_nr_rings);
6281 bp->num_stat_ctxs = bp->cp_nr_rings;
6282
6283 if (netif_running(bp->dev))
6284 return bnxt_open_nic(bp, true, false);
6285
6286 return 0;
6287}
6288
6289#ifdef CONFIG_RFS_ACCEL
6290static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
6291 struct bnxt_ntuple_filter *f2)
6292{
6293 struct flow_keys *keys1 = &f1->fkeys;
6294 struct flow_keys *keys2 = &f2->fkeys;
6295
6296 if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
6297 keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
6298 keys1->ports.ports == keys2->ports.ports &&
6299 keys1->basic.ip_proto == keys2->basic.ip_proto &&
6300 keys1->basic.n_proto == keys2->basic.n_proto &&
6301 ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr))
6302 return true;
6303
6304 return false;
6305}
6306
6307static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
6308 u16 rxq_index, u32 flow_id)
6309{
6310 struct bnxt *bp = netdev_priv(dev);
6311 struct bnxt_ntuple_filter *fltr, *new_fltr;
6312 struct flow_keys *fkeys;
6313 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
Michael Chan84e86b92015-11-05 16:25:50 -05006314 int rc = 0, idx, bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04006315 struct hlist_head *head;
6316
6317 if (skb->encapsulation)
6318 return -EPROTONOSUPPORT;
6319
6320 new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
6321 if (!new_fltr)
6322 return -ENOMEM;
6323
6324 fkeys = &new_fltr->fkeys;
6325 if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
6326 rc = -EPROTONOSUPPORT;
6327 goto err_free;
6328 }
6329
6330 if ((fkeys->basic.n_proto != htons(ETH_P_IP)) ||
6331 ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
6332 (fkeys->basic.ip_proto != IPPROTO_UDP))) {
6333 rc = -EPROTONOSUPPORT;
6334 goto err_free;
6335 }
6336
6337 memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
6338
6339 idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
6340 head = &bp->ntp_fltr_hash_tbl[idx];
6341 rcu_read_lock();
6342 hlist_for_each_entry_rcu(fltr, head, hash) {
6343 if (bnxt_fltr_match(fltr, new_fltr)) {
6344 rcu_read_unlock();
6345 rc = 0;
6346 goto err_free;
6347 }
6348 }
6349 rcu_read_unlock();
6350
6351 spin_lock_bh(&bp->ntp_fltr_lock);
Michael Chan84e86b92015-11-05 16:25:50 -05006352 bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6353 BNXT_NTP_FLTR_MAX_FLTR, 0);
6354 if (bit_id < 0) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006355 spin_unlock_bh(&bp->ntp_fltr_lock);
6356 rc = -ENOMEM;
6357 goto err_free;
6358 }
6359
Michael Chan84e86b92015-11-05 16:25:50 -05006360 new_fltr->sw_id = (u16)bit_id;
Michael Chanc0c050c2015-10-22 16:01:17 -04006361 new_fltr->flow_id = flow_id;
6362 new_fltr->rxq = rxq_index;
6363 hlist_add_head_rcu(&new_fltr->hash, head);
6364 bp->ntp_fltr_count++;
6365 spin_unlock_bh(&bp->ntp_fltr_lock);
6366
6367 set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
6368 schedule_work(&bp->sp_task);
6369
6370 return new_fltr->sw_id;
6371
6372err_free:
6373 kfree(new_fltr);
6374 return rc;
6375}
6376
6377static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6378{
6379 int i;
6380
6381 for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
6382 struct hlist_head *head;
6383 struct hlist_node *tmp;
6384 struct bnxt_ntuple_filter *fltr;
6385 int rc;
6386
6387 head = &bp->ntp_fltr_hash_tbl[i];
6388 hlist_for_each_entry_safe(fltr, tmp, head, hash) {
6389 bool del = false;
6390
6391 if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
6392 if (rps_may_expire_flow(bp->dev, fltr->rxq,
6393 fltr->flow_id,
6394 fltr->sw_id)) {
6395 bnxt_hwrm_cfa_ntuple_filter_free(bp,
6396 fltr);
6397 del = true;
6398 }
6399 } else {
6400 rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
6401 fltr);
6402 if (rc)
6403 del = true;
6404 else
6405 set_bit(BNXT_FLTR_VALID, &fltr->state);
6406 }
6407
6408 if (del) {
6409 spin_lock_bh(&bp->ntp_fltr_lock);
6410 hlist_del_rcu(&fltr->hash);
6411 bp->ntp_fltr_count--;
6412 spin_unlock_bh(&bp->ntp_fltr_lock);
6413 synchronize_rcu();
6414 clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
6415 kfree(fltr);
6416 }
6417 }
6418 }
Jeffrey Huang19241362016-02-26 04:00:00 -05006419 if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
6420 netdev_info(bp->dev, "Receive PF driver unload event!");
Michael Chanc0c050c2015-10-22 16:01:17 -04006421}
6422
6423#else
6424
6425static void bnxt_cfg_ntp_filters(struct bnxt *bp)
6426{
6427}
6428
6429#endif /* CONFIG_RFS_ACCEL */
6430
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006431static void bnxt_udp_tunnel_add(struct net_device *dev,
6432 struct udp_tunnel_info *ti)
Michael Chanc0c050c2015-10-22 16:01:17 -04006433{
6434 struct bnxt *bp = netdev_priv(dev);
6435
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006436 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6437 return;
6438
Michael Chanc0c050c2015-10-22 16:01:17 -04006439 if (!netif_running(dev))
6440 return;
6441
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006442 switch (ti->type) {
6443 case UDP_TUNNEL_TYPE_VXLAN:
6444 if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
6445 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006446
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006447 bp->vxlan_port_cnt++;
6448 if (bp->vxlan_port_cnt == 1) {
6449 bp->vxlan_port = ti->port;
6450 set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
Michael Chanc0c050c2015-10-22 16:01:17 -04006451 schedule_work(&bp->sp_task);
6452 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006453 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006454 case UDP_TUNNEL_TYPE_GENEVE:
6455 if (bp->nge_port_cnt && bp->nge_port != ti->port)
6456 return;
6457
6458 bp->nge_port_cnt++;
6459 if (bp->nge_port_cnt == 1) {
6460 bp->nge_port = ti->port;
6461 set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
6462 }
6463 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006464 default:
6465 return;
Michael Chanc0c050c2015-10-22 16:01:17 -04006466 }
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006467
6468 schedule_work(&bp->sp_task);
6469}
6470
6471static void bnxt_udp_tunnel_del(struct net_device *dev,
6472 struct udp_tunnel_info *ti)
6473{
6474 struct bnxt *bp = netdev_priv(dev);
6475
6476 if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
6477 return;
6478
6479 if (!netif_running(dev))
6480 return;
6481
6482 switch (ti->type) {
6483 case UDP_TUNNEL_TYPE_VXLAN:
6484 if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
6485 return;
6486 bp->vxlan_port_cnt--;
6487
6488 if (bp->vxlan_port_cnt != 0)
6489 return;
6490
6491 set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
6492 break;
Alexander Duyck7cdd5fc2016-06-16 12:21:36 -07006493 case UDP_TUNNEL_TYPE_GENEVE:
6494 if (!bp->nge_port_cnt || bp->nge_port != ti->port)
6495 return;
6496 bp->nge_port_cnt--;
6497
6498 if (bp->nge_port_cnt != 0)
6499 return;
6500
6501 set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
6502 break;
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006503 default:
6504 return;
6505 }
6506
6507 schedule_work(&bp->sp_task);
Michael Chanc0c050c2015-10-22 16:01:17 -04006508}
6509
6510static const struct net_device_ops bnxt_netdev_ops = {
6511 .ndo_open = bnxt_open,
6512 .ndo_start_xmit = bnxt_start_xmit,
6513 .ndo_stop = bnxt_close,
6514 .ndo_get_stats64 = bnxt_get_stats64,
6515 .ndo_set_rx_mode = bnxt_set_rx_mode,
6516 .ndo_do_ioctl = bnxt_ioctl,
6517 .ndo_validate_addr = eth_validate_addr,
6518 .ndo_set_mac_address = bnxt_change_mac_addr,
6519 .ndo_change_mtu = bnxt_change_mtu,
6520 .ndo_fix_features = bnxt_fix_features,
6521 .ndo_set_features = bnxt_set_features,
6522 .ndo_tx_timeout = bnxt_tx_timeout,
6523#ifdef CONFIG_BNXT_SRIOV
6524 .ndo_get_vf_config = bnxt_get_vf_config,
6525 .ndo_set_vf_mac = bnxt_set_vf_mac,
6526 .ndo_set_vf_vlan = bnxt_set_vf_vlan,
6527 .ndo_set_vf_rate = bnxt_set_vf_bw,
6528 .ndo_set_vf_link_state = bnxt_set_vf_link_state,
6529 .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
6530#endif
6531#ifdef CONFIG_NET_POLL_CONTROLLER
6532 .ndo_poll_controller = bnxt_poll_controller,
6533#endif
6534 .ndo_setup_tc = bnxt_setup_tc,
6535#ifdef CONFIG_RFS_ACCEL
6536 .ndo_rx_flow_steer = bnxt_rx_flow_steer,
6537#endif
Alexander Duyckad51b8e2016-06-16 12:21:19 -07006538 .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
6539 .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
Michael Chanc0c050c2015-10-22 16:01:17 -04006540#ifdef CONFIG_NET_RX_BUSY_POLL
6541 .ndo_busy_poll = bnxt_busy_poll,
6542#endif
6543};
6544
6545static void bnxt_remove_one(struct pci_dev *pdev)
6546{
6547 struct net_device *dev = pci_get_drvdata(pdev);
6548 struct bnxt *bp = netdev_priv(dev);
6549
6550 if (BNXT_PF(bp))
6551 bnxt_sriov_disable(bp);
6552
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006553 pci_disable_pcie_error_reporting(pdev);
Michael Chanc0c050c2015-10-22 16:01:17 -04006554 unregister_netdev(dev);
6555 cancel_work_sync(&bp->sp_task);
6556 bp->sp_event = 0;
6557
Jeffrey Huangbe58a0d2015-12-27 18:19:18 -05006558 bnxt_hwrm_func_drv_unrgtr(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006559 bnxt_free_hwrm_resources(bp);
6560 pci_iounmap(pdev, bp->bar2);
6561 pci_iounmap(pdev, bp->bar1);
6562 pci_iounmap(pdev, bp->bar0);
6563 free_netdev(dev);
6564
6565 pci_release_regions(pdev);
6566 pci_disable_device(pdev);
6567}
6568
6569static int bnxt_probe_phy(struct bnxt *bp)
6570{
6571 int rc = 0;
6572 struct bnxt_link_info *link_info = &bp->link_info;
Michael Chanc0c050c2015-10-22 16:01:17 -04006573
Michael Chan170ce012016-04-05 14:08:57 -04006574 rc = bnxt_hwrm_phy_qcaps(bp);
6575 if (rc) {
6576 netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
6577 rc);
6578 return rc;
6579 }
6580
Michael Chanc0c050c2015-10-22 16:01:17 -04006581 rc = bnxt_update_link(bp, false);
6582 if (rc) {
6583 netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
6584 rc);
6585 return rc;
6586 }
6587
Michael Chan93ed8112016-06-13 02:25:37 -04006588 /* Older firmware does not have supported_auto_speeds, so assume
6589 * that all supported speeds can be autonegotiated.
6590 */
6591 if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
6592 link_info->support_auto_speeds = link_info->support_speeds;
6593
Michael Chanc0c050c2015-10-22 16:01:17 -04006594 /*initialize the ethool setting copy with NVM settings */
Michael Chan0d8abf02016-02-10 17:33:47 -05006595 if (BNXT_AUTO_MODE(link_info->auto_mode)) {
Michael Chanc9ee9512016-04-05 14:08:56 -04006596 link_info->autoneg = BNXT_AUTONEG_SPEED;
6597 if (bp->hwrm_spec_code >= 0x10201) {
6598 if (link_info->auto_pause_setting &
6599 PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
6600 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6601 } else {
6602 link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
6603 }
Michael Chan0d8abf02016-02-10 17:33:47 -05006604 link_info->advertising = link_info->auto_link_speeds;
Michael Chan0d8abf02016-02-10 17:33:47 -05006605 } else {
6606 link_info->req_link_speed = link_info->force_link_speed;
6607 link_info->req_duplex = link_info->duplex_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04006608 }
Michael Chanc9ee9512016-04-05 14:08:56 -04006609 if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
6610 link_info->req_flow_ctrl =
6611 link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
6612 else
6613 link_info->req_flow_ctrl = link_info->force_pause_setting;
Michael Chanc0c050c2015-10-22 16:01:17 -04006614 return rc;
6615}
6616
6617static int bnxt_get_max_irq(struct pci_dev *pdev)
6618{
6619 u16 ctrl;
6620
6621 if (!pdev->msix_cap)
6622 return 1;
6623
6624 pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
6625 return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
6626}
6627
Michael Chan6e6c5a52016-01-02 23:45:02 -05006628static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
6629 int *max_cp)
Michael Chanc0c050c2015-10-22 16:01:17 -04006630{
Michael Chan6e6c5a52016-01-02 23:45:02 -05006631 int max_ring_grps = 0;
Michael Chanc0c050c2015-10-22 16:01:17 -04006632
Michael Chan379a80a2015-10-23 15:06:19 -04006633#ifdef CONFIG_BNXT_SRIOV
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006634 if (!BNXT_PF(bp)) {
Michael Chanc0c050c2015-10-22 16:01:17 -04006635 *max_tx = bp->vf.max_tx_rings;
6636 *max_rx = bp->vf.max_rx_rings;
Michael Chan6e6c5a52016-01-02 23:45:02 -05006637 *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
6638 *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
Michael Chanb72d4a62015-12-27 18:19:27 -05006639 max_ring_grps = bp->vf.max_hw_ring_grps;
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006640 } else
Michael Chan379a80a2015-10-23 15:06:19 -04006641#endif
Arnd Bergmann415b6f12016-01-12 16:05:08 +01006642 {
6643 *max_tx = bp->pf.max_tx_rings;
6644 *max_rx = bp->pf.max_rx_rings;
6645 *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
6646 *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
6647 max_ring_grps = bp->pf.max_hw_ring_grps;
Michael Chanc0c050c2015-10-22 16:01:17 -04006648 }
Prashant Sreedharan76595192016-07-18 07:15:22 -04006649 if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
6650 *max_cp -= 1;
6651 *max_rx -= 2;
6652 }
Michael Chanc0c050c2015-10-22 16:01:17 -04006653 if (bp->flags & BNXT_FLAG_AGG_RINGS)
6654 *max_rx >>= 1;
Michael Chanb72d4a62015-12-27 18:19:27 -05006655 *max_rx = min_t(int, *max_rx, max_ring_grps);
Michael Chan6e6c5a52016-01-02 23:45:02 -05006656}
6657
6658int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
6659{
6660 int rx, tx, cp;
6661
6662 _bnxt_get_max_rings(bp, &rx, &tx, &cp);
6663 if (!rx || !tx || !cp)
6664 return -ENOMEM;
6665
6666 *max_rx = rx;
6667 *max_tx = tx;
6668 return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
6669}
6670
6671static int bnxt_set_dflt_rings(struct bnxt *bp)
6672{
6673 int dflt_rings, max_rx_rings, max_tx_rings, rc;
6674 bool sh = true;
6675
6676 if (sh)
6677 bp->flags |= BNXT_FLAG_SHARED_RINGS;
6678 dflt_rings = netif_get_num_default_rss_queues();
6679 rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
6680 if (rc)
6681 return rc;
6682 bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
6683 bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
6684 bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
6685 bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
6686 bp->tx_nr_rings + bp->rx_nr_rings;
6687 bp->num_stat_ctxs = bp->cp_nr_rings;
Prashant Sreedharan76595192016-07-18 07:15:22 -04006688 if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
6689 bp->rx_nr_rings++;
6690 bp->cp_nr_rings++;
6691 }
Michael Chan6e6c5a52016-01-02 23:45:02 -05006692 return rc;
Michael Chanc0c050c2015-10-22 16:01:17 -04006693}
6694
Ajit Khaparde90c4f782016-05-15 03:04:45 -04006695static void bnxt_parse_log_pcie_link(struct bnxt *bp)
6696{
6697 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
6698 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
6699
6700 if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
6701 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
6702 netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
6703 else
6704 netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
6705 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
6706 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
6707 speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
6708 "Unknown", width);
6709}
6710
Michael Chanc0c050c2015-10-22 16:01:17 -04006711static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6712{
6713 static int version_printed;
6714 struct net_device *dev;
6715 struct bnxt *bp;
Michael Chan6e6c5a52016-01-02 23:45:02 -05006716 int rc, max_irqs;
Michael Chanc0c050c2015-10-22 16:01:17 -04006717
6718 if (version_printed++ == 0)
6719 pr_info("%s", version);
6720
6721 max_irqs = bnxt_get_max_irq(pdev);
6722 dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
6723 if (!dev)
6724 return -ENOMEM;
6725
6726 bp = netdev_priv(dev);
6727
6728 if (bnxt_vf_pciid(ent->driver_data))
6729 bp->flags |= BNXT_FLAG_VF;
6730
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006731 if (pdev->msix_cap)
Michael Chanc0c050c2015-10-22 16:01:17 -04006732 bp->flags |= BNXT_FLAG_MSIX_CAP;
Michael Chanc0c050c2015-10-22 16:01:17 -04006733
6734 rc = bnxt_init_board(pdev, dev);
6735 if (rc < 0)
6736 goto init_err_free;
6737
6738 dev->netdev_ops = &bnxt_netdev_ops;
6739 dev->watchdog_timeo = BNXT_TX_TIMEOUT;
6740 dev->ethtool_ops = &bnxt_ethtool_ops;
6741
6742 pci_set_drvdata(pdev, dev);
6743
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006744 rc = bnxt_alloc_hwrm_resources(bp);
6745 if (rc)
6746 goto init_err;
6747
6748 mutex_init(&bp->hwrm_cmd_lock);
6749 rc = bnxt_hwrm_ver_get(bp);
6750 if (rc)
6751 goto init_err;
6752
Michael Chanc0c050c2015-10-22 16:01:17 -04006753 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
6754 NETIF_F_TSO | NETIF_F_TSO6 |
6755 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Tom Herbert7e133182016-05-18 09:06:10 -07006756 NETIF_F_GSO_IPXIP4 |
Alexander Duyck152971e2016-05-02 09:38:55 -07006757 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
6758 NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006759 NETIF_F_RXCSUM | NETIF_F_GRO;
6760
6761 if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6762 dev->hw_features |= NETIF_F_LRO;
Michael Chanc0c050c2015-10-22 16:01:17 -04006763
Michael Chanc0c050c2015-10-22 16:01:17 -04006764 dev->hw_enc_features =
6765 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
6766 NETIF_F_TSO | NETIF_F_TSO6 |
6767 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
Alexander Duyck152971e2016-05-02 09:38:55 -07006768 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
Tom Herbert7e133182016-05-18 09:06:10 -07006769 NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
Alexander Duyck152971e2016-05-02 09:38:55 -07006770 dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
6771 NETIF_F_GSO_GRE_CSUM;
Michael Chanc0c050c2015-10-22 16:01:17 -04006772 dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
6773 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
6774 NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
6775 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
6776 dev->priv_flags |= IFF_UNICAST_FLT;
6777
6778#ifdef CONFIG_BNXT_SRIOV
6779 init_waitqueue_head(&bp->sriov_cfg_wait);
6780#endif
Michael Chan309369c2016-06-13 02:25:34 -04006781 bp->gro_func = bnxt_gro_func_5730x;
Michael Chan94758f82016-06-13 02:25:35 -04006782 if (BNXT_CHIP_NUM_57X1X(bp->chip_num))
6783 bp->gro_func = bnxt_gro_func_5731x;
Michael Chan309369c2016-06-13 02:25:34 -04006784
Michael Chanc0c050c2015-10-22 16:01:17 -04006785 rc = bnxt_hwrm_func_drv_rgtr(bp);
6786 if (rc)
6787 goto init_err;
6788
6789 /* Get the MAX capabilities for this function */
6790 rc = bnxt_hwrm_func_qcaps(bp);
6791 if (rc) {
6792 netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
6793 rc);
6794 rc = -1;
6795 goto init_err;
6796 }
6797
6798 rc = bnxt_hwrm_queue_qportcfg(bp);
6799 if (rc) {
6800 netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
6801 rc);
6802 rc = -1;
6803 goto init_err;
6804 }
6805
Satish Baddipadige567b2ab2016-06-13 02:25:31 -04006806 bnxt_hwrm_func_qcfg(bp);
6807
Michael Chanc0c050c2015-10-22 16:01:17 -04006808 bnxt_set_tpa_flags(bp);
6809 bnxt_set_ring_params(bp);
Jeffrey Huangbdd43472015-12-02 01:54:07 -05006810 if (BNXT_PF(bp))
Michael Chanc0c050c2015-10-22 16:01:17 -04006811 bp->pf.max_irqs = max_irqs;
Michael Chan379a80a2015-10-23 15:06:19 -04006812#if defined(CONFIG_BNXT_SRIOV)
Jeffrey Huangbdd43472015-12-02 01:54:07 -05006813 else
Michael Chanc0c050c2015-10-22 16:01:17 -04006814 bp->vf.max_irqs = max_irqs;
Michael Chan379a80a2015-10-23 15:06:19 -04006815#endif
Michael Chan6e6c5a52016-01-02 23:45:02 -05006816 bnxt_set_dflt_rings(bp);
Michael Chanc0c050c2015-10-22 16:01:17 -04006817
Prashant Sreedharan3e8060f2016-07-18 07:15:20 -04006818 if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) {
Michael Chan2bcfa6f2015-12-27 18:19:24 -05006819 dev->hw_features |= NETIF_F_NTUPLE;
6820 if (bnxt_rfs_capable(bp)) {
6821 bp->flags |= BNXT_FLAG_RFS;
6822 dev->features |= NETIF_F_NTUPLE;
6823 }
6824 }
6825
Michael Chanc0c050c2015-10-22 16:01:17 -04006826 if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
6827 bp->flags |= BNXT_FLAG_STRIP_VLAN;
6828
6829 rc = bnxt_probe_phy(bp);
6830 if (rc)
6831 goto init_err;
6832
6833 rc = register_netdev(dev);
6834 if (rc)
6835 goto init_err;
6836
6837 netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
6838 board_info[ent->driver_data].name,
6839 (long)pci_resource_start(pdev, 0), dev->dev_addr);
6840
Ajit Khaparde90c4f782016-05-15 03:04:45 -04006841 bnxt_parse_log_pcie_link(bp);
6842
Michael Chanc0c050c2015-10-22 16:01:17 -04006843 return 0;
6844
6845init_err:
6846 pci_iounmap(pdev, bp->bar0);
6847 pci_release_regions(pdev);
6848 pci_disable_device(pdev);
6849
6850init_err_free:
6851 free_netdev(dev);
6852 return rc;
6853}
6854
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006855/**
6856 * bnxt_io_error_detected - called when PCI error is detected
6857 * @pdev: Pointer to PCI device
6858 * @state: The current pci connection state
6859 *
6860 * This function is called after a PCI bus error affecting
6861 * this device has been detected.
6862 */
6863static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
6864 pci_channel_state_t state)
6865{
6866 struct net_device *netdev = pci_get_drvdata(pdev);
Michael Chan2a5bedf2016-07-01 18:46:21 -04006867 struct bnxt *bp = netdev_priv(netdev);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006868
6869 netdev_info(netdev, "PCI I/O error detected\n");
6870
6871 rtnl_lock();
6872 netif_device_detach(netdev);
6873
6874 if (state == pci_channel_io_perm_failure) {
6875 rtnl_unlock();
6876 return PCI_ERS_RESULT_DISCONNECT;
6877 }
6878
6879 if (netif_running(netdev))
6880 bnxt_close(netdev);
6881
Michael Chan2a5bedf2016-07-01 18:46:21 -04006882 /* So that func_reset will be done during slot_reset */
6883 clear_bit(BNXT_STATE_FN_RST_DONE, &bp->state);
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006884 pci_disable_device(pdev);
6885 rtnl_unlock();
6886
6887 /* Request a slot slot reset. */
6888 return PCI_ERS_RESULT_NEED_RESET;
6889}
6890
6891/**
6892 * bnxt_io_slot_reset - called after the pci bus has been reset.
6893 * @pdev: Pointer to PCI device
6894 *
6895 * Restart the card from scratch, as if from a cold-boot.
6896 * At this point, the card has exprienced a hard reset,
6897 * followed by fixups by BIOS, and has its config space
6898 * set up identically to what it was at cold boot.
6899 */
6900static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
6901{
6902 struct net_device *netdev = pci_get_drvdata(pdev);
6903 struct bnxt *bp = netdev_priv(netdev);
6904 int err = 0;
6905 pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
6906
6907 netdev_info(bp->dev, "PCI Slot Reset\n");
6908
6909 rtnl_lock();
6910
6911 if (pci_enable_device(pdev)) {
6912 dev_err(&pdev->dev,
6913 "Cannot re-enable PCI device after reset.\n");
6914 } else {
6915 pci_set_master(pdev);
6916
6917 if (netif_running(netdev))
6918 err = bnxt_open(netdev);
6919
6920 if (!err)
6921 result = PCI_ERS_RESULT_RECOVERED;
6922 }
6923
6924 if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
6925 dev_close(netdev);
6926
6927 rtnl_unlock();
6928
6929 err = pci_cleanup_aer_uncorrect_error_status(pdev);
6930 if (err) {
6931 dev_err(&pdev->dev,
6932 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
6933 err); /* non-fatal, continue */
6934 }
6935
6936 return PCI_ERS_RESULT_RECOVERED;
6937}
6938
6939/**
6940 * bnxt_io_resume - called when traffic can start flowing again.
6941 * @pdev: Pointer to PCI device
6942 *
6943 * This callback is called when the error recovery driver tells
6944 * us that its OK to resume normal operation.
6945 */
6946static void bnxt_io_resume(struct pci_dev *pdev)
6947{
6948 struct net_device *netdev = pci_get_drvdata(pdev);
6949
6950 rtnl_lock();
6951
6952 netif_device_attach(netdev);
6953
6954 rtnl_unlock();
6955}
6956
6957static const struct pci_error_handlers bnxt_err_handler = {
6958 .error_detected = bnxt_io_error_detected,
6959 .slot_reset = bnxt_io_slot_reset,
6960 .resume = bnxt_io_resume
6961};
6962
Michael Chanc0c050c2015-10-22 16:01:17 -04006963static struct pci_driver bnxt_pci_driver = {
6964 .name = DRV_MODULE_NAME,
6965 .id_table = bnxt_pci_tbl,
6966 .probe = bnxt_init_one,
6967 .remove = bnxt_remove_one,
Satish Baddipadige6316ea62016-03-07 15:38:48 -05006968 .err_handler = &bnxt_err_handler,
Michael Chanc0c050c2015-10-22 16:01:17 -04006969#if defined(CONFIG_BNXT_SRIOV)
6970 .sriov_configure = bnxt_sriov_configure,
6971#endif
6972};
6973
6974module_pci_driver(bnxt_pci_driver);